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 401 /* 402 * Ignore result from bd_int13probe(), we will use local 403 * workaround below. 404 */ 405 (void)bd_int13probe(bd); 406 407 if (bd->bd_cyl == 0) { 408 bd->bd_cyl = ((bc_sp.sp_cylsec & 0xc0) << 2) + 409 ((bc_sp.sp_cylsec & 0xff00) >> 8) + 1; 410 } 411 if (bd->bd_hds == 0) 412 bd->bd_hds = bc_sp.sp_head + 1; 413 if (bd->bd_sec == 0) 414 bd->bd_sec = bc_sp.sp_cylsec & 0x3f; 415 if (bd->bd_sectors == 0) 416 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec; 417 418 /* Still no size? use 7.961GB */ 419 if (bd->bd_sectors == 0) 420 bd->bd_sectors = 4173824; 421 422 STAILQ_INSERT_TAIL(&cdinfo, bd, bd_link); 423 printf("BIOS CD is cd%d\n", nbcinfo); 424 nbcinfo++; 425 bcache_add_dev(nbcinfo); /* register cd device in bcache */ 426 return(0); 427 } 428 429 /* 430 * Return EDD version or 0 if EDD is not supported on this drive. 431 */ 432 static int 433 bd_check_extensions(int unit) 434 { 435 /* do not use ext calls for floppy devices */ 436 if (unit < 0x80) 437 return (0); 438 439 /* Determine if we can use EDD with this device. */ 440 v86.ctl = V86_FLAGS; 441 v86.addr = DISK_BIOS; 442 v86.eax = CMD_CHECK_EDD; 443 v86.edx = unit; 444 v86.ebx = EDD_QUERY_MAGIC; 445 v86int(); 446 447 if (V86_CY(v86.efl) || /* carry set */ 448 (v86.ebx & 0xffff) != EDD_INSTALLED) /* signature */ 449 return (0); 450 451 /* extended disk access functions (AH=42h-44h,47h,48h) supported */ 452 if ((v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0) 453 return (0); 454 455 return ((v86.eax >> 8) & 0xff); 456 } 457 458 static void 459 bd_reset_disk(int unit) 460 { 461 /* reset disk */ 462 v86.ctl = V86_FLAGS; 463 v86.addr = DISK_BIOS; 464 v86.eax = CMD_RESET; 465 v86.edx = unit; 466 v86int(); 467 } 468 469 /* 470 * Read CHS info. Return 0 on success, error otherwise. 471 */ 472 static int 473 bd_get_diskinfo_std(struct bdinfo *bd) 474 { 475 bzero(&v86, sizeof(v86)); 476 v86.ctl = V86_FLAGS; 477 v86.addr = DISK_BIOS; 478 v86.eax = CMD_READ_PARAM; 479 v86.edx = bd->bd_unit; 480 v86int(); 481 482 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0)) 483 return ((v86.eax & 0xff00) >> 8); 484 485 /* return custom error on absurd sector number */ 486 if ((v86.ecx & 0x3f) == 0) 487 return (0x60); 488 489 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1; 490 /* Convert max head # -> # of heads */ 491 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1; 492 bd->bd_sec = v86.ecx & 0x3f; 493 bd->bd_type = v86.ebx; 494 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec; 495 496 return (0); 497 } 498 499 /* 500 * Read EDD info. Return 0 on success, error otherwise. 501 */ 502 static int 503 bd_get_diskinfo_ext(struct bdinfo *bd) 504 { 505 struct edd_params params; 506 uint64_t total; 507 508 /* Get disk params */ 509 bzero(¶ms, sizeof(params)); 510 params.len = sizeof(params); 511 v86.ctl = V86_FLAGS; 512 v86.addr = DISK_BIOS; 513 v86.eax = CMD_EXT_PARAM; 514 v86.edx = bd->bd_unit; 515 v86.ds = VTOPSEG(¶ms); 516 v86.esi = VTOPOFF(¶ms); 517 v86int(); 518 519 if (V86_CY(v86.efl) && ((v86.eax & 0xff00) != 0)) 520 return ((v86.eax & 0xff00) >> 8); 521 522 /* 523 * Sector size must be a multiple of 512 bytes. 524 * An alternate test would be to check power of 2, 525 * powerof2(params.sector_size). 526 * 16K is largest read buffer we can use at this time. 527 */ 528 if (params.sector_size >= 512 && 529 params.sector_size <= 16384 && 530 (params.sector_size % BIOSDISK_SECSIZE) == 0) 531 bd->bd_sectorsize = params.sector_size; 532 533 bd->bd_cyl = params.cylinders; 534 bd->bd_hds = params.heads; 535 bd->bd_sec = params.sectors_per_track; 536 537 if (params.sectors != 0) { 538 total = params.sectors; 539 } else { 540 total = (uint64_t)params.cylinders * 541 params.heads * params.sectors_per_track; 542 } 543 bd->bd_sectors = total; 544 545 return (0); 546 } 547 548 /* 549 * Try to detect a device supported by the legacy int13 BIOS 550 */ 551 static bool 552 bd_int13probe(bdinfo_t *bd) 553 { 554 int edd, ret; 555 556 bd->bd_flags &= ~BD_NO_MEDIA; 557 558 edd = bd_check_extensions(bd->bd_unit); 559 if (edd == 0) 560 bd->bd_flags |= BD_MODEINT13; 561 else if (edd < 0x30) 562 bd->bd_flags |= BD_MODEEDD1; 563 else 564 bd->bd_flags |= BD_MODEEDD3; 565 566 /* Default sector size */ 567 bd->bd_sectorsize = BIOSDISK_SECSIZE; 568 569 /* 570 * Test if the floppy device is present, so we can avoid receiving 571 * bogus information from bd_get_diskinfo_std(). 572 */ 573 if (bd->bd_unit < 0x80) { 574 /* reset disk */ 575 bd_reset_disk(bd->bd_unit); 576 577 /* Get disk type */ 578 v86.ctl = V86_FLAGS; 579 v86.addr = DISK_BIOS; 580 v86.eax = CMD_DRIVE_TYPE; 581 v86.edx = bd->bd_unit; 582 v86int(); 583 if (V86_CY(v86.efl) || (v86.eax & 0x300) == 0) 584 return (false); 585 } 586 587 ret = 1; 588 if (edd != 0) 589 ret = bd_get_diskinfo_ext(bd); 590 if (ret != 0 || bd->bd_sectors == 0) 591 ret = bd_get_diskinfo_std(bd); 592 593 if (ret != 0 && bd->bd_unit < 0x80) { 594 /* Set defaults for 1.44 floppy */ 595 bd->bd_cyl = 80; 596 bd->bd_hds = 2; 597 bd->bd_sec = 18; 598 bd->bd_sectors = 2880; 599 /* Since we are there, there most likely is no media */ 600 bd->bd_flags |= BD_NO_MEDIA; 601 ret = 0; 602 } 603 604 if (ret != 0) { 605 /* CD is special case, bc_add() has its own fallback. */ 606 if ((bd->bd_flags & BD_CDROM) != 0) 607 return (true); 608 609 if (bd->bd_sectors != 0 && edd != 0) { 610 bd->bd_sec = 63; 611 bd->bd_hds = 255; 612 bd->bd_cyl = 613 (bd->bd_sectors + bd->bd_sec * bd->bd_hds - 1) / 614 bd->bd_sec * bd->bd_hds; 615 } else { 616 const char *dv_name; 617 618 if ((bd->bd_flags & BD_FLOPPY) != 0) 619 dv_name = biosfd.dv_name; 620 else if ((bd->bd_flags & BD_CDROM) != 0) 621 dv_name = bioscd.dv_name; 622 else 623 dv_name = bioshd.dv_name; 624 625 printf("Can not get information about %s unit %#x\n", 626 dv_name, bd->bd_unit); 627 return (false); 628 } 629 } 630 631 if (bd->bd_sec == 0) 632 bd->bd_sec = 63; 633 if (bd->bd_hds == 0) 634 bd->bd_hds = 255; 635 636 if (bd->bd_sectors == 0) 637 bd->bd_sectors = (uint64_t)bd->bd_cyl * bd->bd_hds * bd->bd_sec; 638 639 DPRINTF("unit 0x%x geometry %d/%d/%d\n", bd->bd_unit, bd->bd_cyl, 640 bd->bd_hds, bd->bd_sec); 641 642 return (true); 643 } 644 645 static int 646 bd_count(bdinfo_list_t *bdi) 647 { 648 bdinfo_t *bd; 649 int i; 650 651 i = 0; 652 STAILQ_FOREACH(bd, bdi, bd_link) 653 i++; 654 return (i); 655 } 656 657 /* 658 * Print information about disks 659 */ 660 static int 661 bd_print_common(struct devsw *dev, bdinfo_list_t *bdi, int verbose) 662 { 663 char line[80]; 664 struct disk_devdesc devd; 665 bdinfo_t *bd; 666 int i, ret = 0; 667 char drive; 668 669 if (STAILQ_EMPTY(bdi)) 670 return (0); 671 672 printf("%s devices:", dev->dv_name); 673 if ((ret = pager_output("\n")) != 0) 674 return (ret); 675 676 i = -1; 677 STAILQ_FOREACH(bd, bdi, bd_link) { 678 i++; 679 680 switch (dev->dv_type) { 681 case DEVT_FD: 682 drive = 'A'; 683 break; 684 case DEVT_CD: 685 drive = 'C' + bd_count(&hdinfo); 686 break; 687 default: 688 drive = 'C'; 689 break; 690 } 691 692 snprintf(line, sizeof(line), 693 " %s%d: BIOS drive %c (%s%ju X %u):\n", 694 dev->dv_name, i, drive + i, 695 (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA ? 696 "no media, " : "", 697 (uintmax_t)bd->bd_sectors, 698 bd->bd_sectorsize); 699 if ((ret = pager_output(line)) != 0) 700 break; 701 702 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) 703 continue; 704 705 if (dev->dv_type != DEVT_DISK) 706 continue; 707 708 devd.dd.d_dev = dev; 709 devd.dd.d_unit = i; 710 devd.d_slice = D_SLICENONE; 711 devd.d_partition = D_PARTNONE; 712 if (disk_open(&devd, 713 bd->bd_sectorsize * bd->bd_sectors, 714 bd->bd_sectorsize) == 0) { 715 snprintf(line, sizeof(line), " %s%d", 716 dev->dv_name, i); 717 ret = disk_print(&devd, line, verbose); 718 disk_close(&devd); 719 if (ret != 0) 720 break; 721 } 722 } 723 return (ret); 724 } 725 726 static int 727 fd_print(int verbose) 728 { 729 return (bd_print_common(&biosfd, &fdinfo, verbose)); 730 } 731 732 static int 733 bd_print(int verbose) 734 { 735 return (bd_print_common(&bioshd, &hdinfo, verbose)); 736 } 737 738 static int 739 cd_print(int verbose) 740 { 741 return (bd_print_common(&bioscd, &cdinfo, verbose)); 742 } 743 744 /* 745 * Read disk size from partition. 746 * This is needed to work around buggy BIOS systems returning 747 * wrong (truncated) disk media size. 748 * During bd_probe() we tested if the multiplication of bd_sectors 749 * would overflow so it should be safe to perform here. 750 */ 751 static uint64_t 752 bd_disk_get_sectors(struct disk_devdesc *dev) 753 { 754 bdinfo_t *bd; 755 struct disk_devdesc disk; 756 uint64_t size; 757 758 bd = bd_get_bdinfo(&dev->dd); 759 if (bd == NULL) 760 return (0); 761 762 disk.dd.d_dev = dev->dd.d_dev; 763 disk.dd.d_unit = dev->dd.d_unit; 764 disk.d_slice = D_SLICENONE; 765 disk.d_partition = D_PARTNONE; 766 disk.d_offset = 0; 767 768 size = bd->bd_sectors * bd->bd_sectorsize; 769 if (disk_open(&disk, size, bd->bd_sectorsize) == 0) { 770 (void) disk_ioctl(&disk, DIOCGMEDIASIZE, &size); 771 disk_close(&disk); 772 } 773 return (size / bd->bd_sectorsize); 774 } 775 776 /* 777 * Attempt to open the disk described by (dev) for use by (f). 778 * 779 * Note that the philosophy here is "give them exactly what 780 * they ask for". This is necessary because being too "smart" 781 * about what the user might want leads to complications. 782 * (eg. given no slice or partition value, with a disk that is 783 * sliced - are they after the first BSD slice, or the DOS 784 * slice before it?) 785 */ 786 static int 787 bd_open(struct open_file *f, ...) 788 { 789 bdinfo_t *bd; 790 struct disk_devdesc *dev; 791 va_list ap; 792 int rc; 793 794 va_start(ap, f); 795 dev = va_arg(ap, struct disk_devdesc *); 796 va_end(ap); 797 798 bd = bd_get_bdinfo(&dev->dd); 799 if (bd == NULL) 800 return (EIO); 801 802 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) { 803 if (!bd_int13probe(bd)) 804 return (EIO); 805 if ((bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) 806 return (EIO); 807 } 808 if (bd->bd_bcache == NULL) 809 bd->bd_bcache = bcache_allocate(); 810 811 if (bd->bd_open == 0) 812 bd->bd_sectors = bd_disk_get_sectors(dev); 813 bd->bd_open++; 814 815 rc = 0; 816 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 817 rc = disk_open(dev, bd->bd_sectors * bd->bd_sectorsize, 818 bd->bd_sectorsize); 819 if (rc != 0) { 820 bd->bd_open--; 821 if (bd->bd_open == 0) { 822 bcache_free(bd->bd_bcache); 823 bd->bd_bcache = NULL; 824 } 825 } 826 } 827 return (rc); 828 } 829 830 static int 831 bd_close(struct open_file *f) 832 { 833 struct disk_devdesc *dev; 834 bdinfo_t *bd; 835 int rc = 0; 836 837 dev = (struct disk_devdesc *)f->f_devdata; 838 bd = bd_get_bdinfo(&dev->dd); 839 if (bd == NULL) 840 return (EIO); 841 842 bd->bd_open--; 843 if (bd->bd_open == 0) { 844 bcache_free(bd->bd_bcache); 845 bd->bd_bcache = NULL; 846 } 847 if (dev->dd.d_dev->dv_type == DEVT_DISK) 848 rc = disk_close(dev); 849 return (rc); 850 } 851 852 static int 853 bd_ioctl(struct open_file *f, u_long cmd, void *data) 854 { 855 bdinfo_t *bd; 856 struct disk_devdesc *dev; 857 int rc; 858 859 dev = (struct disk_devdesc *)f->f_devdata; 860 bd = bd_get_bdinfo(&dev->dd); 861 if (bd == NULL) 862 return (EIO); 863 864 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 865 rc = disk_ioctl(dev, cmd, data); 866 if (rc != ENOTTY) 867 return (rc); 868 } 869 870 switch (cmd) { 871 case DIOCGSECTORSIZE: 872 *(uint32_t *)data = bd->bd_sectorsize; 873 break; 874 case DIOCGMEDIASIZE: 875 *(uint64_t *)data = bd->bd_sectors * bd->bd_sectorsize; 876 break; 877 default: 878 return (ENOTTY); 879 } 880 return (0); 881 } 882 883 static int 884 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, 885 char *buf, size_t *rsize) 886 { 887 bdinfo_t *bd; 888 struct bcache_devdata bcd; 889 struct disk_devdesc *dev; 890 daddr_t offset; 891 892 dev = (struct disk_devdesc *)devdata; 893 bd = bd_get_bdinfo(&dev->dd); 894 if (bd == NULL) 895 return (EINVAL); 896 897 bcd.dv_strategy = bd_realstrategy; 898 bcd.dv_devdata = devdata; 899 bcd.dv_cache = bd->bd_bcache; 900 901 offset = 0; 902 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 903 904 offset = dev->d_offset * bd->bd_sectorsize; 905 offset /= BIOSDISK_SECSIZE; 906 } 907 return (bcache_strategy(&bcd, rw, dblk + offset, size, 908 buf, rsize)); 909 } 910 911 static int 912 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, 913 char *buf, size_t *rsize) 914 { 915 struct disk_devdesc *dev = (struct disk_devdesc *)devdata; 916 bdinfo_t *bd; 917 uint64_t disk_blocks, offset, d_offset; 918 size_t blks, blkoff, bsize, bio_size, rest; 919 caddr_t bbuf = NULL; 920 int rc; 921 922 bd = bd_get_bdinfo(&dev->dd); 923 if (bd == NULL || (bd->bd_flags & BD_NO_MEDIA) == BD_NO_MEDIA) 924 return (EIO); 925 926 /* 927 * First make sure the IO size is a multiple of 512 bytes. While we do 928 * process partial reads below, the strategy mechanism is built 929 * assuming IO is a multiple of 512B blocks. If the request is not 930 * a multiple of 512B blocks, it has to be some sort of bug. 931 */ 932 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) { 933 printf("bd_strategy: %d bytes I/O not multiple of %d\n", 934 size, BIOSDISK_SECSIZE); 935 return (EIO); 936 } 937 938 DPRINTF("open_disk %p", dev); 939 940 offset = dblk * BIOSDISK_SECSIZE; 941 dblk = offset / bd->bd_sectorsize; 942 blkoff = offset % bd->bd_sectorsize; 943 944 /* 945 * Check the value of the size argument. We do have quite small 946 * heap (64MB), but we do not know good upper limit, so we check against 947 * INT_MAX here. This will also protect us against possible overflows 948 * while translating block count to bytes. 949 */ 950 if (size > INT_MAX) { 951 DPRINTF("too large I/O: %zu bytes", size); 952 return (EIO); 953 } 954 955 blks = size / bd->bd_sectorsize; 956 if (blks == 0 || (size % bd->bd_sectorsize) != 0) 957 blks++; 958 959 if (dblk > dblk + blks) 960 return (EIO); 961 962 if (rsize) 963 *rsize = 0; 964 965 /* 966 * Get disk blocks, this value is either for whole disk or for 967 * partition. 968 */ 969 d_offset = 0; 970 disk_blocks = 0; 971 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 972 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) { 973 /* DIOCGMEDIASIZE does return bytes. */ 974 disk_blocks /= bd->bd_sectorsize; 975 } 976 d_offset = dev->d_offset; 977 } 978 if (disk_blocks == 0) 979 disk_blocks = bd->bd_sectors - d_offset; 980 981 /* Validate source block address. */ 982 if (dblk < d_offset || dblk >= d_offset + disk_blocks) 983 return (EIO); 984 985 /* 986 * Truncate if we are crossing disk or partition end. 987 */ 988 if (dblk + blks >= d_offset + disk_blocks) { 989 blks = d_offset + disk_blocks - dblk; 990 size = blks * bd->bd_sectorsize; 991 DPRINTF("short I/O %d", blks); 992 } 993 994 bio_size = min(BIO_BUFFER_SIZE, size); 995 while (bio_size > bd->bd_sectorsize) { 996 bbuf = bio_alloc(bio_size); 997 if (bbuf != NULL) 998 break; 999 bio_size -= bd->bd_sectorsize; 1000 } 1001 if (bbuf == NULL) { 1002 bio_size = V86_IO_BUFFER_SIZE; 1003 if (bio_size / bd->bd_sectorsize == 0) 1004 panic("BUG: Real mode buffer is too small"); 1005 1006 /* Use alternate 4k buffer */ 1007 bbuf = PTOV(V86_IO_BUFFER); 1008 } 1009 rest = size; 1010 rc = 0; 1011 while (blks > 0) { 1012 int x = min(blks, bio_size / bd->bd_sectorsize); 1013 1014 switch (rw & F_MASK) { 1015 case F_READ: 1016 DPRINTF("read %d from %lld to %p", x, dblk, buf); 1017 bsize = bd->bd_sectorsize * x - blkoff; 1018 if (rest < bsize) 1019 bsize = rest; 1020 1021 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD)) != 0) { 1022 rc = EIO; 1023 goto error; 1024 } 1025 1026 bcopy(bbuf + blkoff, buf, bsize); 1027 break; 1028 case F_WRITE : 1029 DPRINTF("write %d from %lld to %p", x, dblk, buf); 1030 if (blkoff != 0) { 1031 /* 1032 * We got offset to sector, read 1 sector to 1033 * bbuf. 1034 */ 1035 x = 1; 1036 bsize = bd->bd_sectorsize - blkoff; 1037 bsize = min(bsize, rest); 1038 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD); 1039 } else if (rest < bd->bd_sectorsize) { 1040 /* 1041 * The remaining block is not full 1042 * sector. Read 1 sector to bbuf. 1043 */ 1044 x = 1; 1045 bsize = rest; 1046 rc = bd_io(dev, bd, dblk, x, bbuf, BD_RD); 1047 } else { 1048 /* We can write full sector(s). */ 1049 bsize = bd->bd_sectorsize * x; 1050 } 1051 /* 1052 * Put your Data In, Put your Data out, 1053 * Put your Data In, and shake it all about 1054 */ 1055 bcopy(buf, bbuf + blkoff, bsize); 1056 if ((rc = bd_io(dev, bd, dblk, x, bbuf, BD_WR)) != 0) { 1057 rc = EIO; 1058 goto error; 1059 } 1060 1061 break; 1062 default: 1063 /* DO NOTHING */ 1064 rc = EROFS; 1065 goto error; 1066 } 1067 1068 blkoff = 0; 1069 buf += bsize; 1070 rest -= bsize; 1071 blks -= x; 1072 dblk += x; 1073 } 1074 1075 if (rsize != NULL) 1076 *rsize = size; 1077 error: 1078 if (bbuf != PTOV(V86_IO_BUFFER)) 1079 bio_free(bbuf, bio_size); 1080 return (rc); 1081 } 1082 1083 static int 1084 bd_edd_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest, 1085 int dowrite) 1086 { 1087 static struct edd_packet packet; 1088 1089 packet.len = sizeof(struct edd_packet); 1090 packet.count = blks; 1091 packet.off = VTOPOFF(dest); 1092 packet.seg = VTOPSEG(dest); 1093 packet.lba = dblk; 1094 v86.ctl = V86_FLAGS; 1095 v86.addr = DISK_BIOS; 1096 if (dowrite == BD_WR) 1097 v86.eax = CMD_WRITE_LBA; /* maybe Write with verify 0x4302? */ 1098 else 1099 v86.eax = CMD_READ_LBA; 1100 v86.edx = bd->bd_unit; 1101 v86.ds = VTOPSEG(&packet); 1102 v86.esi = VTOPOFF(&packet); 1103 v86int(); 1104 if (V86_CY(v86.efl)) 1105 return (v86.eax >> 8); 1106 return (0); 1107 } 1108 1109 static int 1110 bd_chs_io(bdinfo_t *bd, daddr_t dblk, int blks, caddr_t dest, 1111 int dowrite) 1112 { 1113 uint32_t x, bpc, cyl, hd, sec; 1114 1115 bpc = bd->bd_sec * bd->bd_hds; /* blocks per cylinder */ 1116 x = dblk; 1117 cyl = x / bpc; /* block # / blocks per cylinder */ 1118 x %= bpc; /* block offset into cylinder */ 1119 hd = x / bd->bd_sec; /* offset / blocks per track */ 1120 sec = x % bd->bd_sec; /* offset into track */ 1121 1122 /* correct sector number for 1-based BIOS numbering */ 1123 sec++; 1124 1125 if (cyl > 1023) { 1126 /* CHS doesn't support cylinders > 1023. */ 1127 return (1); 1128 } 1129 1130 v86.ctl = V86_FLAGS; 1131 v86.addr = DISK_BIOS; 1132 if (dowrite == BD_WR) 1133 v86.eax = CMD_WRITE_CHS | blks; 1134 else 1135 v86.eax = CMD_READ_CHS | blks; 1136 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec; 1137 v86.edx = (hd << 8) | bd->bd_unit; 1138 v86.es = VTOPSEG(dest); 1139 v86.ebx = VTOPOFF(dest); 1140 v86int(); 1141 if (V86_CY(v86.efl)) 1142 return (v86.eax >> 8); 1143 return (0); 1144 } 1145 1146 static void 1147 bd_io_workaround(bdinfo_t *bd) 1148 { 1149 uint8_t buf[8 * 1024]; 1150 1151 bd_edd_io(bd, 0xffffffff, 1, (caddr_t)buf, BD_RD); 1152 } 1153 1154 static int 1155 bd_io(struct disk_devdesc *dev, bdinfo_t *bd, daddr_t dblk, int blks, 1156 caddr_t dest, int dowrite) 1157 { 1158 int result, retry; 1159 1160 /* Just in case some idiot actually tries to read/write -1 blocks... */ 1161 if (blks < 0) 1162 return (-1); 1163 1164 /* 1165 * Workaround for a problem with some HP ProLiant BIOS failing to work 1166 * out the boot disk after installation. hrs and kuriyama discovered 1167 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and 1168 * discovered that an int13h call seems to cause a buffer overrun in 1169 * the bios. The problem is alleviated by doing an extra read before 1170 * the buggy read. It is not immediately known whether other models 1171 * are similarly affected. 1172 * Loop retrying the operation a couple of times. The BIOS 1173 * may also retry. 1174 */ 1175 if (dowrite == BD_RD && dblk >= 0x100000000) 1176 bd_io_workaround(bd); 1177 for (retry = 0; retry < 3; retry++) { 1178 if (bd->bd_flags & BD_MODEEDD) 1179 result = bd_edd_io(bd, dblk, blks, dest, dowrite); 1180 else 1181 result = bd_chs_io(bd, dblk, blks, dest, dowrite); 1182 1183 if (result == 0) { 1184 if (bd->bd_flags & BD_NO_MEDIA) 1185 bd->bd_flags &= ~BD_NO_MEDIA; 1186 break; 1187 } 1188 1189 bd_reset_disk(bd->bd_unit); 1190 1191 /* 1192 * Error codes: 1193 * 20h controller failure 1194 * 31h no media in drive (IBM/MS INT 13 extensions) 1195 * 80h no media in drive, VMWare (Fusion) 1196 * There is no reason to repeat the IO with errors above. 1197 */ 1198 if (result == 0x20 || result == 0x31 || result == 0x80) { 1199 bd->bd_flags |= BD_NO_MEDIA; 1200 break; 1201 } 1202 } 1203 1204 if (result != 0 && (bd->bd_flags & BD_NO_MEDIA) == 0) { 1205 if (dowrite == BD_WR) { 1206 printf("%s%d: Write %d sector(s) from %p (0x%x) " 1207 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name, 1208 dev->dd.d_unit, blks, dest, VTOP(dest), dblk, 1209 result); 1210 } else { 1211 printf("%s%d: Read %d sector(s) from %lld to %p " 1212 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name, 1213 dev->dd.d_unit, blks, dblk, dest, VTOP(dest), 1214 result); 1215 } 1216 } 1217 1218 return (result); 1219 } 1220 1221 /* 1222 * Return the BIOS geometry of a given "fixed drive" in a format 1223 * suitable for the legacy bootinfo structure. Since the kernel is 1224 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we 1225 * prefer to get the information directly, rather than rely on being 1226 * able to put it together from information already maintained for 1227 * different purposes and for a probably different number of drives. 1228 * 1229 * For valid drives, the geometry is expected in the format (31..0) 1230 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are 1231 * indicated by returning the geometry of a "1.2M" PC-format floppy 1232 * disk. And, incidentally, what is returned is not the geometry as 1233 * such but the highest valid cylinder, head, and sector numbers. 1234 */ 1235 uint32_t 1236 bd_getbigeom(int bunit) 1237 { 1238 1239 v86.ctl = V86_FLAGS; 1240 v86.addr = DISK_BIOS; 1241 v86.eax = CMD_READ_PARAM; 1242 v86.edx = 0x80 + bunit; 1243 v86int(); 1244 if (V86_CY(v86.efl)) 1245 return (0x4f010f); 1246 return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) | 1247 (v86.edx & 0xff00) | (v86.ecx & 0x3f)); 1248 } 1249 1250 /* 1251 * Return a suitable dev_t value for (dev). 1252 * 1253 * In the case where it looks like (dev) is a SCSI disk, we allow the number of 1254 * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 1255 */ 1256 int 1257 bd_getdev(struct i386_devdesc *d) 1258 { 1259 struct disk_devdesc *dev; 1260 bdinfo_t *bd; 1261 int biosdev; 1262 int major; 1263 int rootdev; 1264 char *nip, *cp; 1265 int i, unit, slice, partition; 1266 1267 /* XXX: Assume partition 'a'. */ 1268 slice = 0; 1269 partition = 0; 1270 1271 dev = (struct disk_devdesc *)d; 1272 bd = bd_get_bdinfo(&dev->dd); 1273 if (bd == NULL) 1274 return (-1); 1275 1276 biosdev = bd_unit2bios(d); 1277 DPRINTF("unit %d BIOS device %d", dev->dd.d_unit, biosdev); 1278 if (biosdev == -1) /* not a BIOS device */ 1279 return (-1); 1280 1281 if (dev->dd.d_dev->dv_type == DEVT_DISK) { 1282 if (disk_open(dev, bd->bd_sectors * bd->bd_sectorsize, 1283 bd->bd_sectorsize) != 0) /* oops, not a viable device */ 1284 return (-1); 1285 else 1286 disk_close(dev); 1287 slice = dev->d_slice + 1; 1288 partition = dev->d_partition; 1289 } 1290 1291 if (biosdev < 0x80) { 1292 /* floppy (or emulated floppy) or ATAPI device */ 1293 if (bd->bd_type == DT_ATAPI) { 1294 /* is an ATAPI disk */ 1295 major = WFDMAJOR; 1296 } else { 1297 /* is a floppy disk */ 1298 major = FDMAJOR; 1299 } 1300 } else { 1301 /* assume an IDE disk */ 1302 major = WDMAJOR; 1303 } 1304 /* default root disk unit number */ 1305 unit = biosdev & 0x7f; 1306 1307 if (dev->dd.d_dev->dv_type == DEVT_CD) { 1308 /* 1309 * XXX: Need to examine device spec here to figure out if 1310 * SCSI or ATAPI. No idea on how to figure out device number. 1311 * All we can really pass to the kernel is what bus and device 1312 * on which bus we were booted from, which dev_t isn't well 1313 * suited to since those number don't match to unit numbers 1314 * very well. We may just need to engage in a hack where 1315 * we pass -C to the boot args if we are the boot device. 1316 */ 1317 major = ACDMAJOR; 1318 unit = 0; /* XXX */ 1319 } 1320 1321 /* XXX a better kludge to set the root disk unit number */ 1322 if ((nip = getenv("root_disk_unit")) != NULL) { 1323 i = strtol(nip, &cp, 0); 1324 /* check for parse error */ 1325 if ((cp != nip) && (*cp == 0)) 1326 unit = i; 1327 } 1328 1329 rootdev = MAKEBOOTDEV(major, slice, unit, partition); 1330 DPRINTF("dev is 0x%x\n", rootdev); 1331 return (rootdev); 1332 } 1333