1 /*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * BIOS disk device handling. 33 * 34 * Ideas and algorithms from: 35 * 36 * - NetBSD libi386/biosdisk.c 37 * - FreeBSD biosboot/disk.c 38 * 39 */ 40 41 #include <sys/disk.h> 42 #include <sys/limits.h> 43 #include <sys/queue.h> 44 #include <stand.h> 45 #include <machine/bootinfo.h> 46 #include <stdarg.h> 47 #include <stdbool.h> 48 49 #include <bootstrap.h> 50 #include <btxv86.h> 51 #include <edd.h> 52 #include "disk.h" 53 #include "libi386.h" 54 55 #define BIOS_NUMDRIVES 0x475 56 #define BIOSDISK_SECSIZE 512 57 #define BUFSIZE (1 * BIOSDISK_SECSIZE) 58 59 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */ 60 #define WDMAJOR 0 /* major numbers for devices we frontend for */ 61 #define WFDMAJOR 1 62 #define FDMAJOR 2 63 #define DAMAJOR 4 64 #define ACDMAJOR 117 65 #define CDMAJOR 15 66 67 /* 68 * INT13 commands 69 */ 70 #define CMD_RESET 0x0000 71 #define CMD_READ_CHS 0x0200 72 #define CMD_WRITE_CHS 0x0300 73 #define CMD_READ_PARAM 0x0800 74 #define CMD_DRIVE_TYPE 0x1500 75 #define CMD_CHECK_EDD 0x4100 76 #define CMD_READ_LBA 0x4200 77 #define CMD_WRITE_LBA 0x4300 78 #define CMD_EXT_PARAM 0x4800 79 #define CMD_CD_GET_STATUS 0x4b01 80 81 #define DISK_BIOS 0x13 82 83 #ifdef DISK_DEBUG 84 #define DPRINTF(fmt, args...) printf("%s: " fmt "\n", __func__, ## args) 85 #else 86 #define DPRINTF(fmt, args...) ((void)0) 87 #endif 88 89 struct specification_packet { 90 uint8_t sp_size; 91 uint8_t sp_bootmedia; 92 uint8_t sp_drive; 93 uint8_t sp_controller; 94 uint32_t sp_lba; 95 uint16_t sp_devicespec; 96 uint16_t sp_buffersegment; 97 uint16_t sp_loadsegment; 98 uint16_t sp_sectorcount; 99 uint16_t sp_cylsec; 100 uint8_t sp_head; 101 }; 102 103 /* 104 * List of BIOS devices, translation from disk unit number to 105 * BIOS unit number. 106 */ 107 typedef struct bdinfo 108 { 109 STAILQ_ENTRY(bdinfo) bd_link; /* link in device list */ 110 int bd_unit; /* BIOS unit number */ 111 int bd_cyl; /* BIOS geometry */ 112 int bd_hds; 113 int bd_sec; 114 int bd_flags; 115 #define BD_MODEINT13 0x0000 116 #define BD_MODEEDD1 0x0001 117 #define BD_MODEEDD3 0x0002 118 #define BD_MODEEDD (BD_MODEEDD1 | BD_MODEEDD3) 119 #define BD_MODEMASK 0x0003 120 #define BD_FLOPPY 0x0004 121 #define BD_CDROM 0x0008 122 #define BD_NO_MEDIA 0x0010 123 int bd_type; /* BIOS 'drive type' (floppy only) */ 124 uint16_t bd_sectorsize; /* Sector size */ 125 uint64_t bd_sectors; /* Disk size */ 126 int bd_open; /* reference counter */ 127 void *bd_bcache; /* buffer cache data */ 128 } bdinfo_t; 129 130 #define BD_RD 0 131 #define BD_WR 1 132 133 typedef STAILQ_HEAD(bdinfo_list, bdinfo) bdinfo_list_t; 134 static bdinfo_list_t fdinfo = STAILQ_HEAD_INITIALIZER(fdinfo); 135 static bdinfo_list_t cdinfo = STAILQ_HEAD_INITIALIZER(cdinfo); 136 static bdinfo_list_t hdinfo = STAILQ_HEAD_INITIALIZER(hdinfo); 137 138 static void bd_io_workaround(bdinfo_t *); 139 static int bd_io(struct disk_devdesc *, bdinfo_t *, daddr_t, int, caddr_t, int); 140 static bool bd_int13probe(bdinfo_t *); 141 142 static int bd_init(void); 143 static int cd_init(void); 144 static int fd_init(void); 145 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, 146 char *buf, size_t *rsize); 147 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, 148 char *buf, size_t *rsize); 149 static int bd_open(struct open_file *f, ...); 150 static int bd_close(struct open_file *f); 151 static int bd_ioctl(struct open_file *f, u_long cmd, void *data); 152 static int bd_print(int verbose); 153 static int cd_print(int verbose); 154 static int fd_print(int verbose); 155 static void bd_reset_disk(int); 156 static int bd_get_diskinfo_std(struct bdinfo *); 157 158 struct devsw biosfd = { 159 .dv_name = "fd", 160 .dv_type = DEVT_FD, 161 .dv_init = fd_init, 162 .dv_strategy = bd_strategy, 163 .dv_open = bd_open, 164 .dv_close = bd_close, 165 .dv_ioctl = bd_ioctl, 166 .dv_print = fd_print, 167 .dv_cleanup = NULL 168 }; 169 170 struct devsw bioscd = { 171 .dv_name = "cd", 172 .dv_type = DEVT_CD, 173 .dv_init = cd_init, 174 .dv_strategy = bd_strategy, 175 .dv_open = bd_open, 176 .dv_close = bd_close, 177 .dv_ioctl = bd_ioctl, 178 .dv_print = cd_print, 179 .dv_cleanup = NULL 180 }; 181 182 struct devsw bioshd = { 183 .dv_name = "disk", 184 .dv_type = DEVT_DISK, 185 .dv_init = bd_init, 186 .dv_strategy = bd_strategy, 187 .dv_open = bd_open, 188 .dv_close = bd_close, 189 .dv_ioctl = bd_ioctl, 190 .dv_print = bd_print, 191 .dv_cleanup = NULL 192 }; 193 194 static bdinfo_list_t * 195 bd_get_bdinfo_list(struct devsw *dev) 196 { 197 if (dev->dv_type == DEVT_DISK) 198 return (&hdinfo); 199 if (dev->dv_type == DEVT_CD) 200 return (&cdinfo); 201 if (dev->dv_type == DEVT_FD) 202 return (&fdinfo); 203 return (NULL); 204 } 205 206 /* XXX this gets called way way too often, investigate */ 207 static bdinfo_t * 208 bd_get_bdinfo(struct devdesc *dev) 209 { 210 bdinfo_list_t *bdi; 211 bdinfo_t *bd = NULL; 212 int unit; 213 214 bdi = bd_get_bdinfo_list(dev->d_dev); 215 if (bdi == NULL) 216 return (bd); 217 218 unit = 0; 219 STAILQ_FOREACH(bd, bdi, bd_link) { 220 if (unit == dev->d_unit) 221 return (bd); 222 unit++; 223 } 224 return (bd); 225 } 226 227 /* 228 * Translate between BIOS device numbers and our private unit numbers. 229 */ 230 int 231 bd_bios2unit(int biosdev) 232 { 233 bdinfo_list_t *bdi[] = { &fdinfo, &cdinfo, &hdinfo, NULL }; 234 bdinfo_t *bd; 235 int i, unit; 236 237 DPRINTF("looking for bios device 0x%x", biosdev); 238 for (i = 0; bdi[i] != NULL; i++) { 239 unit = 0; 240 STAILQ_FOREACH(bd, bdi[i], bd_link) { 241 if (bd->bd_unit == biosdev) { 242 DPRINTF("bd unit %d is BIOS device 0x%x", unit, 243 bd->bd_unit); 244 return (unit); 245 } 246 unit++; 247 } 248 } 249 return (-1); 250 } 251 252 int 253 bd_unit2bios(struct i386_devdesc *dev) 254 { 255 bdinfo_list_t *bdi; 256 bdinfo_t *bd; 257 int unit; 258 259 bdi = bd_get_bdinfo_list(dev->dd.d_dev); 260 if (bdi == NULL) 261 return (-1); 262 263 unit = 0; 264 STAILQ_FOREACH(bd, bdi, bd_link) { 265 if (unit == dev->dd.d_unit) 266 return (bd->bd_unit); 267 unit++; 268 } 269 return (-1); 270 } 271 272 /* 273 * Use INT13 AH=15 - Read Drive Type. 274 */ 275 static int 276 fd_count(void) 277 { 278 int drive; 279 280 for (drive = 0; drive < MAXBDDEV; drive++) { 281 bd_reset_disk(drive); 282 283 v86.ctl = V86_FLAGS; 284 v86.addr = DISK_BIOS; 285 v86.eax = CMD_DRIVE_TYPE; 286 v86.edx = drive; 287 v86int(); 288 289 if (V86_CY(v86.efl)) 290 break; 291 292 if ((v86.eax & 0x300) == 0) 293 break; 294 } 295 296 return (drive); 297 } 298 299 /* 300 * Quiz the BIOS for disk devices, save a little info about them. 301 */ 302 static int 303 fd_init(void) 304 { 305 int unit, numfd; 306 bdinfo_t *bd; 307 308 numfd = fd_count(); 309 for (unit = 0; unit < numfd; unit++) { 310 if ((bd = calloc(1, sizeof(*bd))) == NULL) 311 break; 312 313 bd->bd_sectorsize = BIOSDISK_SECSIZE; 314 bd->bd_flags = BD_FLOPPY; 315 bd->bd_unit = unit; 316 317 /* Use std diskinfo for floppy drive */ 318 if (bd_get_diskinfo_std(bd) != 0) { 319 free(bd); 320 break; 321 } 322 if (bd->bd_sectors == 0) 323 bd->bd_flags |= BD_NO_MEDIA; 324 325 printf("BIOS drive %c: is %s%d\n", ('A' + unit), 326 biosfd.dv_name, unit); 327 328 STAILQ_INSERT_TAIL(&fdinfo, bd, bd_link); 329 } 330 331 bcache_add_dev(unit); 332 return (0); 333 } 334 335 static int 336 bd_init(void) 337 { 338 int base, unit; 339 bdinfo_t *bd; 340 341 base = 0x80; 342 for (unit = 0; unit < *(unsigned char *)PTOV(BIOS_NUMDRIVES); unit++) { 343 /* 344 * Check the BIOS equipment list for number of fixed disks. 345 */ 346 if ((bd = calloc(1, sizeof(*bd))) == NULL) 347 break; 348 bd->bd_unit = base + unit; 349 if (!bd_int13probe(bd)) { 350 free(bd); 351 break; 352 } 353 354 printf("BIOS drive %c: is %s%d\n", ('C' + unit), 355 bioshd.dv_name, unit); 356 357 STAILQ_INSERT_TAIL(&hdinfo, bd, bd_link); 358 } 359 bcache_add_dev(unit); 360 return (0); 361 } 362 363 /* 364 * We can't quiz, we have to be told what device to use, so this function 365 * doesn't do anything. Instead, the loader calls bc_add() with the BIOS 366 * device number to add. 367 */ 368 static int 369 cd_init(void) 370 { 371 372 return (0); 373 } 374 375 int 376 bc_add(int biosdev) 377 { 378 bdinfo_t *bd; 379 struct specification_packet bc_sp; 380 int nbcinfo = 0; 381 382 if (!STAILQ_EMPTY(&cdinfo)) 383 return (-1); 384 385 v86.ctl = V86_FLAGS; 386 v86.addr = DISK_BIOS; 387 v86.eax = CMD_CD_GET_STATUS; 388 v86.edx = biosdev; 389 v86.ds = VTOPSEG(&bc_sp); 390 v86.esi = VTOPOFF(&bc_sp); 391 v86int(); 392 if ((v86.eax & 0xff00) != 0) 393 return (-1); 394 395 if ((bd = calloc(1, sizeof(*bd))) == NULL) 396 return (-1); 397 398 bd->bd_flags = BD_CDROM; 399 bd->bd_unit = biosdev; 400 bd->bd_sectorsize = 2048; 401 402 /* 403 * Ignore result from bd_int13probe(), we will use local 404 * workaround below. 405 */ 406 (void)bd_int13probe(bd); 407 408 if (bd->bd_cyl == 0) { 409 bd->bd_cyl = ((bc_sp.sp_cylsec & 0xc0) << 2) + 410 ((bc_sp.sp_cylsec & 0xff00) >> 8) + 1; 411 } 412 if (bd->bd_hds == 0) 413 bd->bd_hds = bc_sp.sp_head + 1; 414 if (bd->bd_sec == 0) 415 bd->bd_sec = bc_sp.sp_cylsec & 0x3f; 416 if (bd->bd_sectors == 0) 417 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec; 418 419 /* Still no size? use 7.961GB */ 420 if (bd->bd_sectors == 0) 421 bd->bd_sectors = 4173824; 422 423 STAILQ_INSERT_TAIL(&cdinfo, bd, bd_link); 424 printf("BIOS CD is cd%d\n", nbcinfo); 425 nbcinfo++; 426 bcache_add_dev(nbcinfo); /* register cd device in bcache */ 427 return(0); 428 } 429 430 /* 431 * Return EDD version or 0 if EDD is not supported on this drive. 432 */ 433 static int 434 bd_check_extensions(int unit) 435 { 436 /* do not use ext calls for floppy devices */ 437 if (unit < 0x80) 438 return (0); 439 440 /* Determine if we can use EDD with this device. */ 441 v86.ctl = V86_FLAGS; 442 v86.addr = DISK_BIOS; 443 v86.eax = CMD_CHECK_EDD; 444 v86.edx = unit; 445 v86.ebx = EDD_QUERY_MAGIC; 446 v86int(); 447 448 if (V86_CY(v86.efl) || /* carry set */ 449 (v86.ebx & 0xffff) != EDD_INSTALLED) /* signature */ 450 return (0); 451 452 /* extended disk access functions (AH=42h-44h,47h,48h) supported */ 453 if ((v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0) 454 return (0); 455 456 return ((v86.eax >> 8) & 0xff); 457 } 458 459 static void 460 bd_reset_disk(int unit) 461 { 462 /* reset disk */ 463 v86.ctl = V86_FLAGS; 464 v86.addr = DISK_BIOS; 465 v86.eax = CMD_RESET; 466 v86.edx = unit; 467 v86int(); 468 } 469 470 /* 471 * Read CHS info. Return 0 on success, error otherwise. 472 */ 473 static int 474 bd_get_diskinfo_std(struct bdinfo *bd) 475 { 476 bzero(&v86, sizeof(v86)); 477 v86.ctl = V86_FLAGS; 478 v86.addr = DISK_BIOS; 479 v86.eax = CMD_READ_PARAM; 480 v86.edx = bd->bd_unit; 481 v86int(); 482 483 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0)) 484 return ((v86.eax & 0xff00) >> 8); 485 486 /* return custom error on absurd sector number */ 487 if ((v86.ecx & 0x3f) == 0) 488 return (0x60); 489 490 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1; 491 /* Convert max head # -> # of heads */ 492 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1; 493 bd->bd_sec = v86.ecx & 0x3f; 494 bd->bd_type = v86.ebx; 495 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec; 496 497 return (0); 498 } 499 500 /* 501 * Read EDD info. Return 0 on success, error otherwise. 502 */ 503 static int 504 bd_get_diskinfo_ext(struct bdinfo *bd) 505 { 506 struct edd_params params; 507 uint64_t total; 508 509 /* Get disk params */ 510 bzero(¶ms, sizeof(params)); 511 params.len = sizeof(params); 512 v86.ctl = V86_FLAGS; 513 v86.addr = DISK_BIOS; 514 v86.eax = CMD_EXT_PARAM; 515 v86.edx = bd->bd_unit; 516 v86.ds = VTOPSEG(¶ms); 517 v86.esi = VTOPOFF(¶ms); 518 v86int(); 519 520 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0)) 521 return ((v86.eax & 0xff00) >> 8); 522 523 /* 524 * Sector size must be a multiple of 512 bytes. 525 * An alternate test would be to check power of 2, 526 * powerof2(params.sector_size). 527 * 16K is largest read buffer we can use at this time. 528 */ 529 if (params.sector_size >= 512 && 530 params.sector_size <= 16384 && 531 (params.sector_size % BIOSDISK_SECSIZE) == 0) 532 bd->bd_sectorsize = params.sector_size; 533 534 bd->bd_cyl = params.cylinders; 535 bd->bd_hds = params.heads; 536 bd->bd_sec = params.sectors_per_track; 537 538 if (params.sectors != 0) { 539 total = params.sectors; 540 } else { 541 total = (uint64_t)params.cylinders * 542 params.heads * params.sectors_per_track; 543 } 544 bd->bd_sectors = total; 545 546 return (0); 547 } 548 549 /* 550 * Try to detect a device supported by the legacy int13 BIOS 551 */ 552 static bool 553 bd_int13probe(bdinfo_t *bd) 554 { 555 int edd, ret; 556 557 bd->bd_flags &= ~BD_NO_MEDIA; 558 559 edd = bd_check_extensions(bd->bd_unit); 560 if (edd == 0) 561 bd->bd_flags |= BD_MODEINT13; 562 else if (edd < 0x30) 563 bd->bd_flags |= BD_MODEEDD1; 564 else 565 bd->bd_flags |= BD_MODEEDD3; 566 567 /* Default sector size */ 568 if (bd->bd_sectorsize == 0) 569 bd->bd_sectorsize = BIOSDISK_SECSIZE; 570 571 /* 572 * Test if the floppy device is present, so we can avoid receiving 573 * bogus information from bd_get_diskinfo_std(). 574 */ 575 if (bd->bd_unit < 0x80) { 576 /* reset disk */ 577 bd_reset_disk(bd->bd_unit); 578 579 /* Get disk type */ 580 v86.ctl = V86_FLAGS; 581 v86.addr = DISK_BIOS; 582 v86.eax = CMD_DRIVE_TYPE; 583 v86.edx = bd->bd_unit; 584 v86int(); 585 if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0) 586 return (false); 587 } 588 589 ret = 1; 590 if (edd != 0) 591 ret = bd_get_diskinfo_ext(bd); 592 if (ret != 0 || bd->bd_sectors == 0) 593 ret = bd_get_diskinfo_std(bd); 594 595 if (ret != 0 && bd->bd_unit < 0x80) { 596 /* Set defaults for 1.44 floppy */ 597 bd->bd_cyl = 80; 598 bd->bd_hds = 2; 599 bd->bd_sec = 18; 600 bd->bd_sectors = 2880; 601 /* Since we are there, there most likely is no media */ 602 bd->bd_flags |= BD_NO_MEDIA; 603 ret = 0; 604 } 605 606 if (ret != 0) { 607 /* CD is special case, bc_add() has its own fallback. */ 608 if ((bd->bd_flags & BD_CDROM) != 0) 609 return (true); 610 611 if (bd->bd_sectors != 0 && edd != 0) { 612 bd->bd_sec = 63; 613 bd->bd_hds = 255; 614 bd->bd_cyl = 615 (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) / 616 bd->bd_sec * bd->bd_hds; 617 } else { 618 const char *dv_name; 619 620 if ((bd->bd_flags & BD_FLOPPY) != 0) 621 dv_name = biosfd.dv_name; 622 else if ((bd->bd_flags & BD_CDROM) != 0) 623 dv_name = bioscd.dv_name; 624 else 625 dv_name = bioshd.dv_name; 626 627 printf("Can not get information about %s unit %#x\n", 628 dv_name, bd->bd_unit); 629 return (false); 630 } 631 } 632 633 if (bd->bd_sec == 0) 634 bd->bd_sec = 63; 635 if (bd->bd_hds == 0) 636 bd->bd_hds = 255; 637 638 if (bd->bd_sectors == 0) 639 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec; 640 641 DPRINTF("unit 0x%x geometry %d/%d/%d\n", bd->bd_unit, bd->bd_cyl, 642 bd->bd_hds, bd->bd_sec); 643 644 return (true); 645 } 646 647 static int 648 bd_count(bdinfo_list_t *bdi) 649 { 650 bdinfo_t *bd; 651 int i; 652 653 i = 0; 654 STAILQ_FOREACH(bd, bdi, bd_link) 655 i++; 656 return (i); 657 } 658 659 /* 660 * Print information about disks 661 */ 662 static int 663 bd_print_common(struct devsw *dev, bdinfo_list_t *bdi, int verbose) 664 { 665 char line[80]; 666 struct disk_devdesc devd; 667 bdinfo_t *bd; 668 int i, ret = 0; 669 char drive; 670 671 if (STAILQ_EMPTY(bdi)) 672 return (0); 673 674 printf("%s devices:", dev->dv_name); 675 if ((ret = pager_output("\n")) != 0) 676 return (ret); 677 678 i = -1; 679 STAILQ_FOREACH(bd, bdi, bd_link) { 680 i++; 681 682 switch (dev->dv_type) { 683 case DEVT_FD: 684 drive = 'A'; 685 break; 686 case DEVT_CD: 687 drive = 'C' + bd_count(&hdinfo); 688 break; 689 default: 690 drive = 'C'; 691 break; 692 } 693 694 snprintf(line, sizeof(line), 695 " %s%d: BIOS drive %c (%s%ju X %u):\n", 696 dev->dv_name, i, drive + i, 697 (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ? 698 "no media, " : "", 699 (uintmax_t)bd->bd_sectors, 700 bd->bd_sectorsize); 701 if ((ret = pager_output(line)) != 0) 702 break; 703 704 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) 705 continue; 706 707 if (dev->dv_type != DEVT_DISK) 708 continue; 709 710 devd.dd.d_dev = dev; 711 devd.dd.d_unit = i; 712 devd.d_slice = D_SLICENONE; 713 devd.d_partition = D_PARTNONE; 714 if (disk_open(&devd, 715 bd->bd_sectorsize * bd->bd_sectors, 716 bd->bd_sectorsize) == 0) { 717 snprintf(line, sizeof(line), " %s%d", 718 dev->dv_name, i); 719 ret = disk_print(&devd, line, verbose); 720 disk_close(&devd); 721 if (ret != 0) 722 break; 723 } 724 } 725 return (ret); 726 } 727 728 static int 729 fd_print(int verbose) 730 { 731 return (bd_print_common(&biosfd, &fdinfo, verbose)); 732 } 733 734 static int 735 bd_print(int verbose) 736 { 737 return (bd_print_common(&bioshd, &hdinfo, verbose)); 738 } 739 740 static int 741 cd_print(int verbose) 742 { 743 return (bd_print_common(&bioscd, &cdinfo, verbose)); 744 } 745 746 /* 747 * Read disk size from partition. 748 * This is needed to work around buggy BIOS systems returning 749 * wrong (truncated) disk media size. 750 * During bd_probe() we tested if the multiplication of bd_sectors 751 * would overflow so it should be safe to perform here. 752 */ 753 static uint64_t 754 bd_disk_get_sectors(struct disk_devdesc *dev) 755 { 756 bdinfo_t *bd; 757 struct disk_devdesc disk; 758 uint64_t size; 759 760 bd = bd_get_bdinfo(&dev->dd); 761 if (bd == NULL) 762 return (0); 763 764 disk.dd.d_dev = dev->dd.d_dev; 765 disk.dd.d_unit = dev->dd.d_unit; 766 disk.d_slice = D_SLICENONE; 767 disk.d_partition = D_PARTNONE; 768 disk.d_offset = 0; 769 770 size = bd->bd_sectors * bd->bd_sectorsize; 771 if (disk_open(&disk, size, bd->bd_sectorsize) == 0) { 772 (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size); 773 disk_close(&disk); 774 } 775 return (size / bd->bd_sectorsize); 776 } 777 778 /* 779 * Attempt to open the disk described by (dev) for use by (f). 780 * 781 * Note that the philosophy here is "give them exactly what 782 * they ask for". This is necessary because being too "smart" 783 * about what the user might want leads to complications. 784 * (eg. given no slice or partition value, with a disk that is 785 * sliced - are they after the first BSD slice, or the DOS 786 * slice before it?) 787 */ 788 static int 789 bd_open(struct open_file *f, ...) 790 { 791 bdinfo_t *bd; 792 struct disk_devdesc *dev; 793 va_list ap; 794 int rc; 795 796 va_start(ap, f); 797 dev = va_arg(ap, struct disk_devdesc *); 798 va_end(ap); 799 800 bd = bd_get_bdinfo(&dev->dd); 801 if (bd == NULL) 802 return (EIO); 803 804 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) { 805 if (!bd_int13probe(bd)) 806 return (EIO); 807 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) 808 return (EIO); 809 } 810 if (bd->bd_bcache == NULL) 811 bd->bd_bcache = bcache_allocate(); 812 813 if (bd->bd_open == 0) 814 bd->bd_sectors = bd_disk_get_sectors(dev); 815 bd->bd_open++; 816 817 rc = 0; 818 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 819 rc = disk_open(dev, bd->bd_sectors * bd->bd_sectorsize, 820 bd->bd_sectorsize); 821 if (rc != 0) { 822 bd->bd_open--; 823 if (bd->bd_open == 0) { 824 bcache_free(bd->bd_bcache); 825 bd->bd_bcache = NULL; 826 } 827 } 828 } 829 return (rc); 830 } 831 832 static int 833 bd_close(struct open_file *f) 834 { 835 struct disk_devdesc *dev; 836 bdinfo_t *bd; 837 int rc = 0; 838 839 dev = (struct disk_devdesc *)f->f_devdata; 840 bd = bd_get_bdinfo(&dev->dd); 841 if (bd == NULL) 842 return (EIO); 843 844 bd->bd_open--; 845 if (bd->bd_open == 0) { 846 bcache_free(bd->bd_bcache); 847 bd->bd_bcache = NULL; 848 } 849 if (dev->dd.d_dev->dv_type == DEVT_DISK) 850 rc = disk_close(dev); 851 return (rc); 852 } 853 854 static int 855 bd_ioctl(struct open_file *f, u_long cmd, void *data) 856 { 857 bdinfo_t *bd; 858 struct disk_devdesc *dev; 859 int rc; 860 861 dev = (struct disk_devdesc *)f->f_devdata; 862 bd = bd_get_bdinfo(&dev->dd); 863 if (bd == NULL) 864 return (EIO); 865 866 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 867 rc = disk_ioctl(dev, cmd, data); 868 if (rc != ENOTTY) 869 return (rc); 870 } 871 872 switch (cmd) { 873 case DIOCGSECTORSIZE: 874 *(uint32_t *)data = bd->bd_sectorsize; 875 break; 876 case DIOCGMEDIASIZE: 877 *(uint64_t *)data = bd->bd_sectors * bd->bd_sectorsize; 878 break; 879 default: 880 return (ENOTTY); 881 } 882 return (0); 883 } 884 885 static int 886 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, 887 char *buf, size_t *rsize) 888 { 889 bdinfo_t *bd; 890 struct bcache_devdata bcd; 891 struct disk_devdesc *dev; 892 daddr_t offset; 893 894 dev = (struct disk_devdesc *)devdata; 895 bd = bd_get_bdinfo(&dev->dd); 896 if (bd == NULL) 897 return (EINVAL); 898 899 bcd.dv_strategy = bd_realstrategy; 900 bcd.dv_devdata = devdata; 901 bcd.dv_cache = bd->bd_bcache; 902 903 offset = 0; 904 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 905 906 offset = dev->d_offset * bd->bd_sectorsize; 907 offset /= BIOSDISK_SECSIZE; 908 } 909 return (bcache_strategy(&bcd, rw, dblk + offset, size, 910 buf, rsize)); 911 } 912 913 static int 914 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, 915 char *buf, size_t *rsize) 916 { 917 struct disk_devdesc *dev = (struct disk_devdesc *)devdata; 918 bdinfo_t *bd; 919 uint64_t disk_blocks, offset, d_offset; 920 size_t blks, blkoff, bsize, bio_size, rest; 921 caddr_t bbuf = NULL; 922 int rc; 923 924 bd = bd_get_bdinfo(&dev->dd); 925 if (bd == NULL || (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) 926 return (EIO); 927 928 /* 929 * First make sure the IO size is a multiple of 512 bytes. While we do 930 * process partial reads below, the strategy mechanism is built 931 * assuming IO is a multiple of 512B blocks. If the request is not 932 * a multiple of 512B blocks, it has to be some sort of bug. 933 */ 934 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) { 935 printf("bd_strategy: %d bytes I/O not multiple of %d\n", 936 size, BIOSDISK_SECSIZE); 937 return (EIO); 938 } 939 940 DPRINTF("open_disk %p", dev); 941 942 offset = dblk * BIOSDISK_SECSIZE; 943 dblk = offset / bd->bd_sectorsize; 944 blkoff = offset % bd->bd_sectorsize; 945 946 /* 947 * Check the value of the size argument. We do have quite small 948 * heap (64MB), but we do not know good upper limit, so we check against 949 * INT_MAX here. This will also protect us against possible overflows 950 * while translating block count to bytes. 951 */ 952 if (size > INT_MAX) { 953 DPRINTF("too large I/O: %zu bytes", size); 954 return (EIO); 955 } 956 957 blks = size / bd->bd_sectorsize; 958 if (blks == 0 || (size % bd->bd_sectorsize) != 0) 959 blks++; 960 961 if (dblk > dblk + blks) 962 return (EIO); 963 964 if (rsize) 965 *rsize = 0; 966 967 /* 968 * Get disk blocks, this value is either for whole disk or for 969 * partition. 970 */ 971 d_offset = 0; 972 disk_blocks = 0; 973 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 974 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) { 975 /* DIOCGMEDIASIZE does return bytes. */ 976 disk_blocks /= bd->bd_sectorsize; 977 } 978 d_offset = dev->d_offset; 979 } 980 if (disk_blocks == 0) 981 disk_blocks = bd->bd_sectors - d_offset; 982 983 /* Validate source block address. */ 984 if (dblk < d_offset || dblk >= d_offset + disk_blocks) 985 return (EIO); 986 987 /* 988 * Truncate if we are crossing disk or partition end. 989 */ 990 if (dblk + blks >= d_offset + disk_blocks) { 991 blks = d_offset + disk_blocks - dblk; 992 size = blks * bd->bd_sectorsize; 993 DPRINTF("short I/O %d", blks); 994 } 995 996 bio_size = min(BIO_BUFFER_SIZE, size); 997 while (bio_size > bd->bd_sectorsize) { 998 bbuf = bio_alloc(bio_size); 999 if (bbuf != NULL) 1000 break; 1001 bio_size -= bd->bd_sectorsize; 1002 } 1003 if (bbuf == NULL) { 1004 bio_size = V86_IO_BUFFER_SIZE; 1005 if (bio_size / bd->bd_sectorsize == 0) 1006 panic("BUG: Real mode buffer is too small"); 1007 1008 /* Use alternate 4k buffer */ 1009 bbuf = PTOV(V86_IO_BUFFER); 1010 } 1011 rest = size; 1012 rc = 0; 1013 while (blks > 0) { 1014 int x = min(blks, bio_size / bd->bd_sectorsize); 1015 1016 switch (rw & F_MASK) { 1017 case F_READ: 1018 DPRINTF("read %d from %lld to %p", x, dblk, buf); 1019 bsize = bd->bd_sectorsize * x - blkoff; 1020 if (rest < bsize) 1021 bsize = rest; 1022 1023 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD)) != 0) { 1024 rc = EIO; 1025 goto error; 1026 } 1027 1028 bcopy(bbuf + blkoff, buf, bsize); 1029 break; 1030 case F_WRITE : 1031 DPRINTF("write %d from %lld to %p", x, dblk, buf); 1032 if (blkoff != 0) { 1033 /* 1034 * We got offset to sector, read 1 sector to 1035 * bbuf. 1036 */ 1037 x = 1; 1038 bsize = bd->bd_sectorsize - blkoff; 1039 bsize = min(bsize, rest); 1040 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD); 1041 } else if (rest < bd->bd_sectorsize) { 1042 /* 1043 * The remaining block is not full 1044 * sector. Read 1 sector to bbuf. 1045 */ 1046 x = 1; 1047 bsize = rest; 1048 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD); 1049 } else { 1050 /* We can write full sector(s). */ 1051 bsize = bd->bd_sectorsize * x; 1052 } 1053 /* 1054 * Put your Data In, Put your Data out, 1055 * Put your Data In, and shake it all about 1056 */ 1057 bcopy(buf, bbuf + blkoff, bsize); 1058 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_WR)) != 0) { 1059 rc = EIO; 1060 goto error; 1061 } 1062 1063 break; 1064 default: 1065 /* DO NOTHING */ 1066 rc = EROFS; 1067 goto error; 1068 } 1069 1070 blkoff = 0; 1071 buf += bsize; 1072 rest -= bsize; 1073 blks -= x; 1074 dblk += x; 1075 } 1076 1077 if (rsize != NULL) 1078 *rsize = size; 1079 error: 1080 if (bbuf != PTOV(V86_IO_BUFFER)) 1081 bio_free(bbuf, bio_size); 1082 return (rc); 1083 } 1084 1085 static int 1086 bd_edd_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest, 1087 int dowrite) 1088 { 1089 static struct edd_packet packet; 1090 1091 packet.len = sizeof(struct edd_packet); 1092 packet.count = blks; 1093 packet.off = VTOPOFF(dest); 1094 packet.seg = VTOPSEG(dest); 1095 packet.lba = dblk; 1096 v86.ctl = V86_FLAGS; 1097 v86.addr = DISK_BIOS; 1098 if (dowrite == BD_WR) 1099 v86.eax = CMD_WRITE_LBA; /* maybe Write with verify 0x4302? */ 1100 else 1101 v86.eax = CMD_READ_LBA; 1102 v86.edx = bd->bd_unit; 1103 v86.ds = VTOPSEG(&packet); 1104 v86.esi = VTOPOFF(&packet); 1105 v86int(); 1106 if (V86_CY(v86.efl)) 1107 return (v86.eax >> 8); 1108 return (0); 1109 } 1110 1111 static int 1112 bd_chs_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest, 1113 int dowrite) 1114 { 1115 uint32_t x, bpc, cyl, hd, sec; 1116 1117 bpc = bd->bd_sec * bd->bd_hds; /* blocks per cylinder */ 1118 x = dblk; 1119 cyl = x / bpc; /* block # / blocks per cylinder */ 1120 x %= bpc; /* block offset into cylinder */ 1121 hd = x / bd->bd_sec; /* offset / blocks per track */ 1122 sec = x % bd->bd_sec; /* offset into track */ 1123 1124 /* correct sector number for 1-based BIOS numbering */ 1125 sec++; 1126 1127 if (cyl > 1023) { 1128 /* CHS doesn't support cylinders > 1023. */ 1129 return (1); 1130 } 1131 1132 v86.ctl = V86_FLAGS; 1133 v86.addr = DISK_BIOS; 1134 if (dowrite == BD_WR) 1135 v86.eax = CMD_WRITE_CHS | blks; 1136 else 1137 v86.eax = CMD_READ_CHS | blks; 1138 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec; 1139 v86.edx = (hd << 8) | bd->bd_unit; 1140 v86.es = VTOPSEG(dest); 1141 v86.ebx = VTOPOFF(dest); 1142 v86int(); 1143 if (V86_CY(v86.efl)) 1144 return (v86.eax >> 8); 1145 return (0); 1146 } 1147 1148 static void 1149 bd_io_workaround(bdinfo_t *bd) 1150 { 1151 uint8_t buf[8 * 1024]; 1152 1153 bd_edd_io(bd, 0xffffffff, 1, (caddr_t)buf, BD_RD); 1154 } 1155 1156 static int 1157 bd_io(struct disk_devdesc *dev, bdinfo_t *bd, daddr_t dblk, int blks, 1158 caddr_t dest, int dowrite) 1159 { 1160 int result, retry; 1161 1162 /* Just in case some idiot actually tries to read/write -1 blocks... */ 1163 if (blks < 0) 1164 return (-1); 1165 1166 /* 1167 * Workaround for a problem with some HP ProLiant BIOS failing to work 1168 * out the boot disk after installation. hrs and kuriyama discovered 1169 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and 1170 * discovered that an int13h call seems to cause a buffer overrun in 1171 * the bios. The problem is alleviated by doing an extra read before 1172 * the buggy read. It is not immediately known whether other models 1173 * are similarly affected. 1174 * Loop retrying the operation a couple of times. The BIOS 1175 * may also retry. 1176 */ 1177 if (dowrite == BD_RD && dblk >= 0x100000000) 1178 bd_io_workaround(bd); 1179 for (retry = 0; retry < 3; retry++) { 1180 if (bd->bd_flags & BD_MODEEDD) 1181 result = bd_edd_io(bd, dblk, blks, dest, dowrite); 1182 else 1183 result = bd_chs_io(bd, dblk, blks, dest, dowrite); 1184 1185 if (result == 0) { 1186 if (bd->bd_flags & BD_NO_MEDIA) 1187 bd->bd_flags &= ~BD_NO_MEDIA; 1188 break; 1189 } 1190 1191 bd_reset_disk(bd->bd_unit); 1192 1193 /* 1194 * Error codes: 1195 * 20h controller failure 1196 * 31h no media in drive (IBM/MS INT 13 extensions) 1197 * 80h no media in drive, VMWare (Fusion) 1198 * There is no reason to repeat the IO with errors above. 1199 */ 1200 if (result == 0x20 || result == 0x31 || result == 0x80) { 1201 bd->bd_flags |= BD_NO_MEDIA; 1202 break; 1203 } 1204 } 1205 1206 if (result != 0 && (bd->bd_flags & BD_NO_MEDIA) == 0) { 1207 if (dowrite == BD_WR) { 1208 printf("%s%d: Write %d sector(s) from %p (0x%x) " 1209 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name, 1210 dev->dd.d_unit, blks, dest, VTOP(dest), dblk, 1211 result); 1212 } else { 1213 printf("%s%d: Read %d sector(s) from %lld to %p " 1214 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name, 1215 dev->dd.d_unit, blks, dblk, dest, VTOP(dest), 1216 result); 1217 } 1218 } 1219 1220 return (result); 1221 } 1222 1223 /* 1224 * Return the BIOS geometry of a given "fixed drive" in a format 1225 * suitable for the legacy bootinfo structure. Since the kernel is 1226 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we 1227 * prefer to get the information directly, rather than rely on being 1228 * able to put it together from information already maintained for 1229 * different purposes and for a probably different number of drives. 1230 * 1231 * For valid drives, the geometry is expected in the format (31..0) 1232 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are 1233 * indicated by returning the geometry of a "1.2M" PC-format floppy 1234 * disk. And, incidentally, what is returned is not the geometry as 1235 * such but the highest valid cylinder, head, and sector numbers. 1236 */ 1237 uint32_t 1238 bd_getbigeom(int bunit) 1239 { 1240 1241 v86.ctl = V86_FLAGS; 1242 v86.addr = DISK_BIOS; 1243 v86.eax = CMD_READ_PARAM; 1244 v86.edx = 0x80 + bunit; 1245 v86int(); 1246 if (V86_CY(v86.efl)) 1247 return (0x4f010f); 1248 return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) | 1249 (v86.edx & 0xff00) | (v86.ecx & 0x3f)); 1250 } 1251 1252 /* 1253 * Return a suitable dev_t value for (dev). 1254 * 1255 * In the case where it looks like (dev) is a SCSI disk, we allow the number of 1256 * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 1257 */ 1258 int 1259 bd_getdev(struct i386_devdesc *d) 1260 { 1261 struct disk_devdesc *dev; 1262 bdinfo_t *bd; 1263 int biosdev; 1264 int major; 1265 int rootdev; 1266 char *nip, *cp; 1267 int i, unit, slice, partition; 1268 1269 /* XXX: Assume partition 'a'. */ 1270 slice = 0; 1271 partition = 0; 1272 1273 dev = (struct disk_devdesc *)d; 1274 bd = bd_get_bdinfo(&dev->dd); 1275 if (bd == NULL) 1276 return (-1); 1277 1278 biosdev = bd_unit2bios(d); 1279 DPRINTF("unit %d BIOS device %d", dev->dd.d_unit, biosdev); 1280 if (biosdev == -1) /* not a BIOS device */ 1281 return (-1); 1282 1283 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 1284 if (disk_open(dev, bd->bd_sectors * bd->bd_sectorsize, 1285 bd->bd_sectorsize) != 0) /* oops, not a viable device */ 1286 return (-1); 1287 else 1288 disk_close(dev); 1289 slice = dev->d_slice + 1; 1290 partition = dev->d_partition; 1291 } 1292 1293 if (biosdev < 0x80) { 1294 /* floppy (or emulated floppy) or ATAPI device */ 1295 if (bd->bd_type == DT_ATAPI) { 1296 /* is an ATAPI disk */ 1297 major = WFDMAJOR; 1298 } else { 1299 /* is a floppy disk */ 1300 major = FDMAJOR; 1301 } 1302 } else { 1303 /* assume an IDE disk */ 1304 major = WDMAJOR; 1305 } 1306 /* default root disk unit number */ 1307 unit = biosdev & 0x7f; 1308 1309 if (dev->dd.d_dev->dv_type == DEVT_CD) { 1310 /* 1311 * XXX: Need to examine device spec here to figure out if 1312 * SCSI or ATAPI. No idea on how to figure out device number. 1313 * All we can really pass to the kernel is what bus and device 1314 * on which bus we were booted from, which dev_t isn't well 1315 * suited to since those number don't match to unit numbers 1316 * very well. We may just need to engage in a hack where 1317 * we pass -C to the boot args if we are the boot device. 1318 */ 1319 major = ACDMAJOR; 1320 unit = 0; /* XXX */ 1321 } 1322 1323 /* XXX a better kludge to set the root disk unit number */ 1324 if ((nip = getenv("root_disk_unit")) != NULL) { 1325 i = strtol(nip, &cp, 0); 1326 /* check for parse error */ 1327 if ((cp != nip) && (*cp == 0)) 1328 unit = i; 1329 } 1330 1331 rootdev = MAKEBOOTDEV(major, slice, unit, partition); 1332 DPRINTF("dev is 0x%x\n", rootdev); 1333 return (rootdev); 1334 } 1335