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 <stand.h> 44 #include <machine/bootinfo.h> 45 #include <stdarg.h> 46 #include <stdbool.h> 47 48 #include <bootstrap.h> 49 #include <btxv86.h> 50 #include <edd.h> 51 #include "disk.h" 52 #include "libi386.h" 53 54 #define BIOS_NUMDRIVES 0x475 55 #define BIOSDISK_SECSIZE 512 56 #define BUFSIZE (1 * BIOSDISK_SECSIZE) 57 58 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */ 59 #define WDMAJOR 0 /* major numbers for devices we frontend for */ 60 #define WFDMAJOR 1 61 #define FDMAJOR 2 62 #define DAMAJOR 4 63 64 #ifdef DISK_DEBUG 65 #define DEBUG(fmt, args...) printf("%s: " fmt "\n", __func__, ## args) 66 #else 67 #define DEBUG(fmt, args...) 68 #endif 69 70 /* 71 * List of BIOS devices, translation from disk unit number to 72 * BIOS unit number. 73 */ 74 static struct bdinfo 75 { 76 int bd_unit; /* BIOS unit number */ 77 int bd_cyl; /* BIOS geometry */ 78 int bd_hds; 79 int bd_sec; 80 int bd_flags; 81 #define BD_MODEINT13 0x0000 82 #define BD_MODEEDD1 0x0001 83 #define BD_MODEEDD3 0x0002 84 #define BD_MODEMASK 0x0003 85 #define BD_FLOPPY 0x0004 86 int bd_type; /* BIOS 'drive type' (floppy only) */ 87 uint16_t bd_sectorsize; /* Sector size */ 88 uint64_t bd_sectors; /* Disk size */ 89 int bd_open; /* reference counter */ 90 void *bd_bcache; /* buffer cache data */ 91 } bdinfo [MAXBDDEV]; 92 static int nbdinfo = 0; 93 94 #define BD(dev) (bdinfo[(dev)->dd.d_unit]) 95 #define BD_RD 0 96 #define BD_WR 1 97 98 static void bd_io_workaround(struct disk_devdesc *dev); 99 100 static int bd_io(struct disk_devdesc *, daddr_t, int, caddr_t, int); 101 static int bd_int13probe(struct bdinfo *bd); 102 103 static int bd_init(void); 104 static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, 105 char *buf, size_t *rsize); 106 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, 107 char *buf, size_t *rsize); 108 static int bd_open(struct open_file *f, ...); 109 static int bd_close(struct open_file *f); 110 static int bd_ioctl(struct open_file *f, u_long cmd, void *data); 111 static int bd_print(int verbose); 112 113 struct devsw biosdisk = { 114 "disk", 115 DEVT_DISK, 116 bd_init, 117 bd_strategy, 118 bd_open, 119 bd_close, 120 bd_ioctl, 121 bd_print, 122 NULL 123 }; 124 125 /* 126 * Translate between BIOS device numbers and our private unit numbers. 127 */ 128 int 129 bd_bios2unit(int biosdev) 130 { 131 int i; 132 133 DEBUG("looking for bios device 0x%x", biosdev); 134 for (i = 0; i < nbdinfo; i++) { 135 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit); 136 if (bdinfo[i].bd_unit == biosdev) 137 return (i); 138 } 139 return (-1); 140 } 141 142 int 143 bd_unit2bios(int unit) 144 { 145 146 if ((unit >= 0) && (unit < nbdinfo)) 147 return (bdinfo[unit].bd_unit); 148 return (-1); 149 } 150 151 /* 152 * Quiz the BIOS for disk devices, save a little info about them. 153 */ 154 static int 155 bd_init(void) 156 { 157 int base, unit, nfd = 0; 158 159 /* sequence 0, 0x80 */ 160 for (base = 0; base <= 0x80; base += 0x80) { 161 for (unit = base; (nbdinfo < MAXBDDEV); unit++) { 162 #ifndef VIRTUALBOX 163 /* 164 * Check the BIOS equipment list for number 165 * of fixed disks. 166 */ 167 if (base == 0x80 && 168 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES))) 169 break; 170 #endif 171 bdinfo[nbdinfo].bd_open = 0; 172 bdinfo[nbdinfo].bd_bcache = NULL; 173 bdinfo[nbdinfo].bd_unit = unit; 174 bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0; 175 if (!bd_int13probe(&bdinfo[nbdinfo])) 176 break; 177 178 /* XXX we need "disk aliases" to make this simpler */ 179 printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ? 180 ('A' + unit): ('C' + unit - 0x80), nbdinfo); 181 nbdinfo++; 182 if (base == 0x80) 183 nfd++; 184 } 185 } 186 bcache_add_dev(nbdinfo); 187 return (0); 188 } 189 190 /* 191 * Try to detect a device supported by the legacy int13 BIOS 192 */ 193 static int 194 bd_int13probe(struct bdinfo *bd) 195 { 196 struct edd_params params; 197 int ret = 1; /* assume success */ 198 199 v86.ctl = V86_FLAGS; 200 v86.addr = 0x13; 201 v86.eax = 0x800; 202 v86.edx = bd->bd_unit; 203 v86int(); 204 205 /* Don't error out if we get bad sector number, try EDD as well */ 206 if (V86_CY(v86.efl) || /* carry set */ 207 (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f)) /* unit # bad */ 208 return (0); /* skip device */ 209 210 if ((v86.ecx & 0x3f) == 0) /* absurd sector number */ 211 ret = 0; /* set error */ 212 213 /* Convert max cyl # -> # of cylinders */ 214 bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1; 215 /* Convert max head # -> # of heads */ 216 bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1; 217 bd->bd_sec = v86.ecx & 0x3f; 218 bd->bd_type = v86.ebx & 0xff; 219 bd->bd_flags |= BD_MODEINT13; 220 221 /* Calculate sectors count from the geometry */ 222 bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec; 223 bd->bd_sectorsize = BIOSDISK_SECSIZE; 224 DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl, 225 bd->bd_hds, bd->bd_sec); 226 227 /* Determine if we can use EDD with this device. */ 228 v86.ctl = V86_FLAGS; 229 v86.addr = 0x13; 230 v86.eax = 0x4100; 231 v86.edx = bd->bd_unit; 232 v86.ebx = 0x55aa; 233 v86int(); 234 if (V86_CY(v86.efl) || /* carry set */ 235 (v86.ebx & 0xffff) != 0xaa55 || /* signature */ 236 (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0) 237 return (ret); /* return code from int13 AH=08 */ 238 239 /* EDD supported */ 240 bd->bd_flags |= BD_MODEEDD1; 241 if ((v86.eax & 0xff00) >= 0x3000) 242 bd->bd_flags |= BD_MODEEDD3; 243 /* Get disk params */ 244 params.len = sizeof(struct edd_params); 245 v86.ctl = V86_FLAGS; 246 v86.addr = 0x13; 247 v86.eax = 0x4800; 248 v86.edx = bd->bd_unit; 249 v86.ds = VTOPSEG(¶ms); 250 v86.esi = VTOPOFF(¶ms); 251 v86int(); 252 if (!V86_CY(v86.efl)) { 253 uint64_t total; 254 255 /* 256 * Sector size must be a multiple of 512 bytes. 257 * An alternate test would be to check power of 2, 258 * powerof2(params.sector_size). 259 * 4K is largest read buffer we can use at this time. 260 */ 261 if (params.sector_size >= 512 && 262 params.sector_size <= 4096 && 263 (params.sector_size % BIOSDISK_SECSIZE) == 0) 264 bd->bd_sectorsize = params.sector_size; 265 266 total = bd->bd_sectorsize * params.sectors; 267 if (params.sectors != 0) { 268 /* Only update if we did not overflow. */ 269 if (total > params.sectors) 270 bd->bd_sectors = params.sectors; 271 } 272 273 total = (uint64_t)params.cylinders * 274 params.heads * params.sectors_per_track; 275 if (total > 0 && bd->bd_sectors > total) 276 bd->bd_sectors = total; 277 278 ret = 1; 279 } 280 DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u", 281 bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize); 282 return (ret); 283 } 284 285 /* 286 * Print information about disks 287 */ 288 static int 289 bd_print(int verbose) 290 { 291 static char line[80]; 292 struct disk_devdesc dev; 293 int i, ret = 0; 294 295 if (nbdinfo == 0) 296 return (0); 297 298 printf("%s devices:", biosdisk.dv_name); 299 if ((ret = pager_output("\n")) != 0) 300 return (ret); 301 302 for (i = 0; i < nbdinfo; i++) { 303 snprintf(line, sizeof(line), 304 " disk%d: BIOS drive %c (%ju X %u):\n", i, 305 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit): 306 ('C' + bdinfo[i].bd_unit - 0x80), 307 (uintmax_t)bdinfo[i].bd_sectors, 308 bdinfo[i].bd_sectorsize); 309 if ((ret = pager_output(line)) != 0) 310 break; 311 312 dev.dd.d_dev = &biosdisk; 313 dev.dd.d_unit = i; 314 dev.d_slice = -1; 315 dev.d_partition = -1; 316 if (disk_open(&dev, 317 bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors, 318 bdinfo[i].bd_sectorsize) == 0) { 319 snprintf(line, sizeof(line), " disk%d", i); 320 ret = disk_print(&dev, line, verbose); 321 disk_close(&dev); 322 if (ret != 0) 323 break; 324 } 325 } 326 return (ret); 327 } 328 329 /* 330 * Attempt to open the disk described by (dev) for use by (f). 331 * 332 * Note that the philosophy here is "give them exactly what 333 * they ask for". This is necessary because being too "smart" 334 * about what the user might want leads to complications. 335 * (eg. given no slice or partition value, with a disk that is 336 * sliced - are they after the first BSD slice, or the DOS 337 * slice before it?) 338 */ 339 static int 340 bd_open(struct open_file *f, ...) 341 { 342 struct disk_devdesc *dev; 343 struct disk_devdesc disk; 344 va_list ap; 345 uint64_t size; 346 int rc; 347 348 va_start(ap, f); 349 dev = va_arg(ap, struct disk_devdesc *); 350 va_end(ap); 351 352 if (dev->dd.d_unit < 0 || dev->dd.d_unit >= nbdinfo) 353 return (EIO); 354 BD(dev).bd_open++; 355 if (BD(dev).bd_bcache == NULL) 356 BD(dev).bd_bcache = bcache_allocate(); 357 358 /* 359 * Read disk size from partition. 360 * This is needed to work around buggy BIOS systems returning 361 * wrong (truncated) disk media size. 362 * During bd_probe() we tested if the mulitplication of bd_sectors 363 * would overflow so it should be safe to perform here. 364 */ 365 disk.dd.d_dev = dev->dd.d_dev; 366 disk.dd.d_unit = dev->dd.d_unit; 367 disk.d_slice = -1; 368 disk.d_partition = -1; 369 disk.d_offset = 0; 370 371 if (disk_open(&disk, BD(dev).bd_sectors * BD(dev).bd_sectorsize, 372 BD(dev).bd_sectorsize) == 0) { 373 374 if (disk_ioctl(&disk, DIOCGMEDIASIZE, &size) == 0) { 375 size /= BD(dev).bd_sectorsize; 376 if (size > BD(dev).bd_sectors) 377 BD(dev).bd_sectors = size; 378 } 379 disk_close(&disk); 380 } 381 382 rc = disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize, 383 BD(dev).bd_sectorsize); 384 if (rc != 0) { 385 BD(dev).bd_open--; 386 if (BD(dev).bd_open == 0) { 387 bcache_free(BD(dev).bd_bcache); 388 BD(dev).bd_bcache = NULL; 389 } 390 } 391 return (rc); 392 } 393 394 static int 395 bd_close(struct open_file *f) 396 { 397 struct disk_devdesc *dev; 398 399 dev = (struct disk_devdesc *)f->f_devdata; 400 BD(dev).bd_open--; 401 if (BD(dev).bd_open == 0) { 402 bcache_free(BD(dev).bd_bcache); 403 BD(dev).bd_bcache = NULL; 404 } 405 return (disk_close(dev)); 406 } 407 408 static int 409 bd_ioctl(struct open_file *f, u_long cmd, void *data) 410 { 411 struct disk_devdesc *dev; 412 int rc; 413 414 dev = (struct disk_devdesc *)f->f_devdata; 415 416 rc = disk_ioctl(dev, cmd, data); 417 if (rc != ENOTTY) 418 return (rc); 419 420 switch (cmd) { 421 case DIOCGSECTORSIZE: 422 *(uint32_t *)data = BD(dev).bd_sectorsize; 423 break; 424 case DIOCGMEDIASIZE: 425 *(uint64_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize; 426 break; 427 default: 428 return (ENOTTY); 429 } 430 return (0); 431 } 432 433 static int 434 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, 435 char *buf, size_t *rsize) 436 { 437 struct bcache_devdata bcd; 438 struct disk_devdesc *dev; 439 440 dev = (struct disk_devdesc *)devdata; 441 bcd.dv_strategy = bd_realstrategy; 442 bcd.dv_devdata = devdata; 443 bcd.dv_cache = BD(dev).bd_bcache; 444 return (bcache_strategy(&bcd, rw, dblk + dev->d_offset, size, 445 buf, rsize)); 446 } 447 448 static int 449 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, 450 char *buf, size_t *rsize) 451 { 452 struct disk_devdesc *dev = (struct disk_devdesc *)devdata; 453 uint64_t disk_blocks, offset; 454 size_t blks, blkoff, bsize, rest; 455 caddr_t bbuf; 456 int rc; 457 458 /* 459 * First make sure the IO size is a multiple of 512 bytes. While we do 460 * process partial reads below, the strategy mechanism is built 461 * assuming IO is a multiple of 512B blocks. If the request is not 462 * a multiple of 512B blocks, it has to be some sort of bug. 463 */ 464 if (size == 0 || (size % BIOSDISK_SECSIZE) != 0) { 465 printf("bd_strategy: %d bytes I/O not multiple of %d\n", 466 size, BIOSDISK_SECSIZE); 467 return (EIO); 468 } 469 470 DEBUG("open_disk %p", dev); 471 472 offset = dblk * BIOSDISK_SECSIZE; 473 dblk = offset / BD(dev).bd_sectorsize; 474 blkoff = offset % BD(dev).bd_sectorsize; 475 476 /* 477 * Check the value of the size argument. We do have quite small 478 * heap (64MB), but we do not know good upper limit, so we check against 479 * INT_MAX here. This will also protect us against possible overflows 480 * while translating block count to bytes. 481 */ 482 if (size > INT_MAX) { 483 DEBUG("too large I/O: %zu bytes", size); 484 return (EIO); 485 } 486 487 blks = size / BD(dev).bd_sectorsize; 488 if (blks == 0 || (size % BD(dev).bd_sectorsize) != 0) 489 blks++; 490 491 if (dblk > dblk + blks) 492 return (EIO); 493 494 if (rsize) 495 *rsize = 0; 496 497 /* 498 * Get disk blocks, this value is either for whole disk or for 499 * partition. 500 */ 501 if (disk_ioctl(dev, DIOCGMEDIASIZE, &disk_blocks) == 0) { 502 /* DIOCGMEDIASIZE returns bytes. */ 503 disk_blocks /= BD(dev).bd_sectorsize; 504 } else { 505 /* We should not get here. Just try to survive. */ 506 disk_blocks = BD(dev).bd_sectors - dev->d_offset; 507 } 508 509 /* Validate source block address. */ 510 if (dblk < dev->d_offset || dblk >= dev->d_offset + disk_blocks) 511 return (EIO); 512 513 /* 514 * Truncate if we are crossing disk or partition end. 515 */ 516 if (dblk + blks >= dev->d_offset + disk_blocks) { 517 blks = dev->d_offset + disk_blocks - dblk; 518 size = blks * BD(dev).bd_sectorsize; 519 DEBUG("short I/O %d", blks); 520 } 521 522 if (V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize == 0) 523 panic("BUG: Real mode buffer is too small\n"); 524 525 bbuf = PTOV(V86_IO_BUFFER); 526 rest = size; 527 528 while (blks > 0) { 529 int x = min(blks, V86_IO_BUFFER_SIZE / BD(dev).bd_sectorsize); 530 531 switch (rw & F_MASK) { 532 case F_READ: 533 DEBUG("read %d from %lld to %p", x, dblk, buf); 534 bsize = BD(dev).bd_sectorsize * x - blkoff; 535 if (rest < bsize) 536 bsize = rest; 537 538 if ((rc = bd_io(dev, dblk, x, bbuf, BD_RD)) != 0) 539 return (EIO); 540 541 bcopy(bbuf + blkoff, buf, bsize); 542 break; 543 case F_WRITE : 544 DEBUG("write %d from %lld to %p", x, dblk, buf); 545 if (blkoff != 0) { 546 /* 547 * We got offset to sector, read 1 sector to 548 * bbuf. 549 */ 550 x = 1; 551 bsize = BD(dev).bd_sectorsize - blkoff; 552 bsize = min(bsize, rest); 553 rc = bd_io(dev, dblk, x, bbuf, BD_RD); 554 } else if (rest < BD(dev).bd_sectorsize) { 555 /* 556 * The remaining block is not full 557 * sector. Read 1 sector to bbuf. 558 */ 559 x = 1; 560 bsize = rest; 561 rc = bd_io(dev, dblk, x, bbuf, BD_RD); 562 } else { 563 /* We can write full sector(s). */ 564 bsize = BD(dev).bd_sectorsize * x; 565 } 566 /* 567 * Put your Data In, Put your Data out, 568 * Put your Data In, and shake it all about 569 */ 570 bcopy(buf, bbuf + blkoff, bsize); 571 if ((rc = bd_io(dev, dblk, x, bbuf, BD_WR)) != 0) 572 return (EIO); 573 574 break; 575 default: 576 /* DO NOTHING */ 577 return (EROFS); 578 } 579 580 blkoff = 0; 581 buf += bsize; 582 rest -= bsize; 583 blks -= x; 584 dblk += x; 585 } 586 587 if (rsize != NULL) 588 *rsize = size; 589 return (0); 590 } 591 592 static int 593 bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest, 594 int dowrite) 595 { 596 static struct edd_packet packet; 597 598 packet.len = sizeof(struct edd_packet); 599 packet.count = blks; 600 packet.off = VTOPOFF(dest); 601 packet.seg = VTOPSEG(dest); 602 packet.lba = dblk; 603 v86.ctl = V86_FLAGS; 604 v86.addr = 0x13; 605 /* Should we Write with verify ?? 0x4302 ? */ 606 if (dowrite == BD_WR) 607 v86.eax = 0x4300; 608 else 609 v86.eax = 0x4200; 610 v86.edx = BD(dev).bd_unit; 611 v86.ds = VTOPSEG(&packet); 612 v86.esi = VTOPOFF(&packet); 613 v86int(); 614 if (V86_CY(v86.efl)) 615 return (v86.eax >> 8); 616 return (0); 617 } 618 619 static int 620 bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest, 621 int dowrite) 622 { 623 uint32_t x, bpc, cyl, hd, sec; 624 625 bpc = BD(dev).bd_sec * BD(dev).bd_hds; /* blocks per cylinder */ 626 x = dblk; 627 cyl = x / bpc; /* block # / blocks per cylinder */ 628 x %= bpc; /* block offset into cylinder */ 629 hd = x / BD(dev).bd_sec; /* offset / blocks per track */ 630 sec = x % BD(dev).bd_sec; /* offset into track */ 631 632 /* correct sector number for 1-based BIOS numbering */ 633 sec++; 634 635 if (cyl > 1023) { 636 /* CHS doesn't support cylinders > 1023. */ 637 return (1); 638 } 639 640 v86.ctl = V86_FLAGS; 641 v86.addr = 0x13; 642 if (dowrite == BD_WR) 643 v86.eax = 0x300 | blks; 644 else 645 v86.eax = 0x200 | blks; 646 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec; 647 v86.edx = (hd << 8) | BD(dev).bd_unit; 648 v86.es = VTOPSEG(dest); 649 v86.ebx = VTOPOFF(dest); 650 v86int(); 651 if (V86_CY(v86.efl)) 652 return (v86.eax >> 8); 653 return (0); 654 } 655 656 static void 657 bd_io_workaround(struct disk_devdesc *dev) 658 { 659 uint8_t buf[8 * 1024]; 660 661 bd_edd_io(dev, 0xffffffff, 1, (caddr_t)buf, BD_RD); 662 } 663 664 static int 665 bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest, 666 int dowrite) 667 { 668 int result, retry; 669 670 /* Just in case some idiot actually tries to read/write -1 blocks... */ 671 if (blks < 0) 672 return (-1); 673 674 /* 675 * Workaround for a problem with some HP ProLiant BIOS failing to work 676 * out the boot disk after installation. hrs and kuriyama discovered 677 * this problem with an HP ProLiant DL320e Gen 8 with a 3TB HDD, and 678 * discovered that an int13h call seems to cause a buffer overrun in 679 * the bios. The problem is alleviated by doing an extra read before 680 * the buggy read. It is not immediately known whether other models 681 * are similarly affected. 682 * Loop retrying the operation a couple of times. The BIOS 683 * may also retry. 684 */ 685 if (dowrite == BD_RD && dblk >= 0x100000000) 686 bd_io_workaround(dev); 687 for (retry = 0; retry < 3; retry++) { 688 /* if retrying, reset the drive */ 689 if (retry > 0) { 690 v86.ctl = V86_FLAGS; 691 v86.addr = 0x13; 692 v86.eax = 0; 693 v86.edx = BD(dev).bd_unit; 694 v86int(); 695 } 696 697 if (BD(dev).bd_flags & BD_MODEEDD1) 698 result = bd_edd_io(dev, dblk, blks, dest, dowrite); 699 else 700 result = bd_chs_io(dev, dblk, blks, dest, dowrite); 701 702 if (result == 0) 703 break; 704 } 705 706 /* 707 * 0x20 - Controller failure. This is common error when the 708 * media is not present. 709 */ 710 if (result != 0 && result != 0x20) { 711 if (dowrite == BD_WR) { 712 printf("%s%d: Write %d sector(s) from %p (0x%x) " 713 "to %lld: 0x%x\n", dev->dd.d_dev->dv_name, 714 dev->dd.d_unit, blks, dest, VTOP(dest), dblk, 715 result); 716 } else { 717 printf("%s%d: Read %d sector(s) from %lld to %p " 718 "(0x%x): 0x%x\n", dev->dd.d_dev->dv_name, 719 dev->dd.d_unit, blks, dblk, dest, VTOP(dest), 720 result); 721 } 722 } 723 724 return (result); 725 } 726 727 /* 728 * Return the BIOS geometry of a given "fixed drive" in a format 729 * suitable for the legacy bootinfo structure. Since the kernel is 730 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we 731 * prefer to get the information directly, rather than rely on being 732 * able to put it together from information already maintained for 733 * different purposes and for a probably different number of drives. 734 * 735 * For valid drives, the geometry is expected in the format (31..0) 736 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are 737 * indicated by returning the geometry of a "1.2M" PC-format floppy 738 * disk. And, incidentally, what is returned is not the geometry as 739 * such but the highest valid cylinder, head, and sector numbers. 740 */ 741 uint32_t 742 bd_getbigeom(int bunit) 743 { 744 745 v86.ctl = V86_FLAGS; 746 v86.addr = 0x13; 747 v86.eax = 0x800; 748 v86.edx = 0x80 + bunit; 749 v86int(); 750 if (V86_CY(v86.efl)) 751 return (0x4f010f); 752 return (((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) | 753 (v86.edx & 0xff00) | (v86.ecx & 0x3f)); 754 } 755 756 /* 757 * Return a suitable dev_t value for (dev). 758 * 759 * In the case where it looks like (dev) is a SCSI disk, we allow the number of 760 * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 761 */ 762 int 763 bd_getdev(struct i386_devdesc *d) 764 { 765 struct disk_devdesc *dev; 766 int biosdev; 767 int major; 768 int rootdev; 769 char *nip, *cp; 770 int i, unit; 771 772 dev = (struct disk_devdesc *)d; 773 biosdev = bd_unit2bios(dev->dd.d_unit); 774 DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev); 775 if (biosdev == -1) /* not a BIOS device */ 776 return (-1); 777 if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize, 778 BD(dev).bd_sectorsize) != 0) /* oops, not a viable device */ 779 return (-1); 780 else 781 disk_close(dev); 782 783 if (biosdev < 0x80) { 784 /* floppy (or emulated floppy) or ATAPI device */ 785 if (bdinfo[dev->dd.d_unit].bd_type == DT_ATAPI) { 786 /* is an ATAPI disk */ 787 major = WFDMAJOR; 788 } else { 789 /* is a floppy disk */ 790 major = FDMAJOR; 791 } 792 } else { 793 /* assume an IDE disk */ 794 major = WDMAJOR; 795 } 796 /* default root disk unit number */ 797 unit = biosdev & 0x7f; 798 799 /* XXX a better kludge to set the root disk unit number */ 800 if ((nip = getenv("root_disk_unit")) != NULL) { 801 i = strtol(nip, &cp, 0); 802 /* check for parse error */ 803 if ((cp != nip) && (*cp == 0)) 804 unit = i; 805 } 806 807 rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition); 808 DEBUG("dev is 0x%x\n", rootdev); 809 return (rootdev); 810 } 811