biosdisk.c revision 50477
1/*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/boot/pc98/libpc98/biosdisk.c 50477 1999-08-28 01:08:13Z peter $ 27 */ 28 29/* 30 * BIOS disk device handling. 31 * 32 * Ideas and algorithms from: 33 * 34 * - NetBSD libi386/biosdisk.c 35 * - FreeBSD biosboot/disk.c 36 * 37 * XXX Todo: add bad144 support. 38 */ 39 40#include <stand.h> 41 42#include <sys/disklabel.h> 43#include <sys/diskslice.h> 44#include <sys/reboot.h> 45 46#include <stdarg.h> 47 48#include <bootstrap.h> 49#include <btxv86.h> 50#include "libi386.h" 51 52#define BIOSDISK_SECSIZE 512 53#define BUFSIZE (1 * BIOSDISK_SECSIZE) 54#define MAXBDDEV MAXDEV 55 56#define DT_ATAPI 0x10 /* disk type for ATAPI floppies */ 57#define WDMAJOR 0 /* major numbers for devices we frontend for */ 58#define WFDMAJOR 1 59#define FDMAJOR 2 60#define DAMAJOR 4 61 62#ifdef DISK_DEBUG 63# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __FUNCTION__ , ## args) 64#else 65# define DEBUG(fmt, args...) 66#endif 67 68struct open_disk { 69 int od_dkunit; /* disk unit number */ 70 int od_unit; /* BIOS unit number */ 71 int od_cyl; /* BIOS geometry */ 72 int od_hds; 73 int od_sec; 74 int od_boff; /* block offset from beginning of BIOS disk */ 75 int od_flags; 76#define BD_MODEMASK 0x3 77#define BD_MODEINT13 0x0 78#define BD_MODEEDD1 0x1 79#define BD_MODEEDD3 0x2 80#define BD_FLOPPY (1<<2) 81 struct disklabel od_disklabel; 82 struct dos_partition od_parttab[NDOSPART]; /* XXX needs to grow for extended partitions */ 83#define BD_LABELOK (1<<3) 84#define BD_PARTTABOK (1<<4) 85}; 86 87/* 88 * List of BIOS devices, translation from disk unit number to 89 * BIOS unit number. 90 */ 91static struct bdinfo 92{ 93 int bd_unit; /* BIOS unit number */ 94 int bd_flags; 95 int bd_type; /* BIOS 'drive type' (floppy only) */ 96#ifdef PC98 97 int bd_da_unit; /* kernel unit number for da */ 98#endif 99} bdinfo [MAXBDDEV]; 100static int nbdinfo = 0; 101 102static int bd_getgeom(struct open_disk *od); 103static int bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest); 104 105static int bd_int13probe(struct bdinfo *bd); 106 107static void bd_printslice(struct open_disk *od, int offset, char *prefix); 108 109static int bd_init(void); 110static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize); 111static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize); 112static int bd_open(struct open_file *f, ...); 113static int bd_close(struct open_file *f); 114static void bd_print(int verbose); 115 116struct devsw biosdisk = { 117 "disk", 118 DEVT_DISK, 119 bd_init, 120 bd_strategy, 121 bd_open, 122 bd_close, 123 noioctl, 124 bd_print 125}; 126 127static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev); 128static void bd_closedisk(struct open_disk *od); 129static int bd_bestslice(struct dos_partition *dptr); 130 131/* 132 * Translate between BIOS device numbers and our private unit numbers. 133 */ 134int 135bd_bios2unit(int biosdev) 136{ 137 int i; 138 139 DEBUG("looking for bios device 0x%x", biosdev); 140 for (i = 0; i < nbdinfo; i++) { 141 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit); 142 if (bdinfo[i].bd_unit == biosdev) 143 return(i); 144 } 145 return(-1); 146} 147 148int 149bd_unit2bios(int unit) 150{ 151 if ((unit >= 0) && (unit < nbdinfo)) 152 return(bdinfo[unit].bd_unit); 153 return(-1); 154} 155 156/* 157 * Quiz the BIOS for disk devices, save a little info about them. 158 * 159 * XXX should we be consulting the BIOS equipment list, specifically 160 * the value at 0x475? 161 */ 162static int 163bd_init(void) 164{ 165 int base, unit; 166 167#ifdef PC98 168 int da_drive=0, n=-0x10; 169 /* sequence 0x90, 0x80, 0xa0 */ 170 for (base = 0x90; base <= 0xa0; base += n, n += 0x30) { 171 for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); unit++) { 172 bdinfo[nbdinfo].bd_unit = unit; 173 bdinfo[nbdinfo].bd_flags = (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0; 174 175 /* XXX add EDD probes */ 176 if (!bd_int13probe(&bdinfo[nbdinfo])){ 177 if (((unit & 0xf0) == 0x90 && (unit & 0x0f) < 4) || 178 ((unit & 0xf0) == 0xa0 && (unit & 0x0f) < 6)) 179 continue; /* Target IDs are not contiguous. */ 180 else 181 break; 182 } 183 184 if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){ 185 /* available 1.44MB access? */ 186 if (*(u_char *)PTOV(0xA15AE) & (1<<(unit & 0xf))){ 187 /* boot media 1.2MB FD? */ 188 if ((*(u_char *)PTOV(0xA1584) & 0xf0) != 0x90) 189 bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf); 190 } 191 } 192 else { 193 if ((unit & 0xa0) == 0xa0) 194 bdinfo[nbdinfo].bd_da_unit = da_drive++; 195 } 196 /* XXX we need "disk aliases" to make this simpler */ 197 printf("BIOS drive %c: is disk%d\n", 198 'A' + nbdinfo, nbdinfo); 199 nbdinfo++; 200 } 201 } 202#else 203 /* sequence 0, 0x80 */ 204 for (base = 0; base <= 0x80; base += 0x80) { 205 for (unit = base; (nbdinfo < MAXBDDEV); unit++) { 206 bdinfo[nbdinfo].bd_unit = unit; 207 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0; 208 209 /* XXX add EDD probes */ 210 if (!bd_int13probe(&bdinfo[nbdinfo])) 211 break; 212 213 /* XXX we need "disk aliases" to make this simpler */ 214 printf("BIOS drive %c: is disk%d\n", 215 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo); 216 nbdinfo++; 217 } 218 } 219#endif 220 return(0); 221} 222 223/* 224 * Try to detect a device supported by the legacy int13 BIOS 225 */ 226 227static int 228bd_int13probe(struct bdinfo *bd) 229{ 230#ifdef PC98 231 int addr; 232 if (bd->bd_flags & BD_FLOPPY){ 233 addr = 0xa155c; 234 } 235 else { 236 if ((bd->bd_unit & 0xf0) == 0x80) 237 addr = 0xa155d; 238 else 239 addr = 0xa1482; 240 } 241 if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) { 242 bd->bd_flags |= BD_MODEINT13; 243 return(1); 244 } 245 return(0); 246#else 247 v86.ctl = V86_FLAGS; 248 v86.addr = 0x13; 249 v86.eax = 0x800; 250 v86.edx = bd->bd_unit; 251 v86int(); 252 253 if (!(v86.efl & 0x1) && /* carry clear */ 254 ((v86.edx & 0xff) > (bd->bd_unit & 0x7f))) { /* unit # OK */ 255 bd->bd_flags |= BD_MODEINT13; 256 bd->bd_type = v86.ebx & 0xff; 257 return(1); 258 } 259#endif 260 return(0); 261} 262 263/* 264 * Print information about disks 265 */ 266static void 267bd_print(int verbose) 268{ 269 int i, j; 270 char line[80]; 271 struct i386_devdesc dev; 272 struct open_disk *od; 273 struct dos_partition *dptr; 274 275 for (i = 0; i < nbdinfo; i++) { 276#ifdef PC98 277 sprintf(line, " disk%d: BIOS drive %c:\n", i, 'A' + i); 278#else 279 sprintf(line, " disk%d: BIOS drive %c:\n", i, 280 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80)); 281#endif 282 pager_output(line); 283 284 /* try to open the whole disk */ 285 dev.d_kind.biosdisk.unit = i; 286 dev.d_kind.biosdisk.slice = -1; 287 dev.d_kind.biosdisk.partition = -1; 288 289 if (!bd_opendisk(&od, &dev)) { 290 291 /* Do we have a partition table? */ 292 if (od->od_flags & BD_PARTTABOK) { 293 dptr = &od->od_parttab[0]; 294 295 /* Check for a "truly dedicated" disk */ 296#ifdef PC98 297 for (j = 0; j < NDOSPART; j++) { 298 switch(dptr[j].dp_mid) { 299 case DOSMID_386BSD: 300 sprintf(line, " disk%ds%d", i, j + 1); 301 bd_printslice(od, dptr[j].dp_scyl * od->od_hds * od->od_sec + dptr[j].dp_shd * od->od_sec + dptr[j].dp_ssect, line); 302 break; 303 default: 304 } 305 } 306#else 307 if ((dptr[3].dp_typ == DOSPTYP_386BSD) && 308 (dptr[3].dp_start == 0) && 309 (dptr[3].dp_size == 50000)) { 310 sprintf(line, " disk%d", i); 311 bd_printslice(od, 0, line); 312 } else { 313 for (j = 0; j < NDOSPART; j++) { 314 switch(dptr[j].dp_typ) { 315 case DOSPTYP_386BSD: 316 sprintf(line, " disk%ds%d", i, j + 1); 317 bd_printslice(od, dptr[j].dp_start, line); 318 break; 319 default: 320 } 321 } 322 323 } 324#endif 325 } 326 bd_closedisk(od); 327 } 328 } 329} 330 331static void 332bd_printslice(struct open_disk *od, int offset, char *prefix) 333{ 334 char line[80]; 335 u_char buf[BIOSDISK_SECSIZE]; 336 struct disklabel *lp; 337 int i; 338 339 /* read disklabel */ 340 if (bd_read(od, offset + LABELSECTOR, 1, buf)) 341 return; 342 lp =(struct disklabel *)(&buf[0]); 343 if (lp->d_magic != DISKMAGIC) { 344 sprintf(line, "bad disklabel\n"); 345 pager_output(line); 346 return; 347 } 348 349 /* Print partitions */ 350 for (i = 0; i < lp->d_npartitions; i++) { 351 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) || (lp->d_partitions[i].p_fstype == FS_SWAP) || 352 ((lp->d_partitions[i].p_fstype == FS_UNUSED) && 353 (od->od_flags & BD_FLOPPY) && (i == 0))) { /* Floppies often have bogus fstype, print 'a' */ 354 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i, 355 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : "FFS", 356 lp->d_partitions[i].p_size / 2048, /* 512-byte sector assumption */ 357 lp->d_partitions[i].p_offset, lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size); 358 pager_output(line); 359 } 360 } 361} 362 363 364/* 365 * Attempt to open the disk described by (dev) for use by (f). 366 * 367 * Note that the philosophy here is "give them exactly what 368 * they ask for". This is necessary because being too "smart" 369 * about what the user might want leads to complications. 370 * (eg. given no slice or partition value, with a disk that is 371 * sliced - are they after the first BSD slice, or the DOS 372 * slice before it?) 373 */ 374static int 375bd_open(struct open_file *f, ...) 376{ 377 va_list ap; 378 struct i386_devdesc *dev; 379 struct open_disk *od; 380 int error; 381 382 va_start(ap, f); 383 dev = va_arg(ap, struct i386_devdesc *); 384 va_end(ap); 385 if ((error = bd_opendisk(&od, dev))) 386 return(error); 387 388 /* 389 * Save our context 390 */ 391 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od; 392 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff); 393 return(0); 394} 395 396static int 397bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev) 398{ 399 struct dos_partition *dptr; 400 struct disklabel *lp; 401 struct open_disk *od; 402 int sector, slice, i; 403 int error; 404 u_char buf[BUFSIZE]; 405 daddr_t pref_slice[4]; 406 407 if (dev->d_kind.biosdisk.unit >= nbdinfo) { 408 DEBUG("attempt to open nonexistent disk"); 409 return(ENXIO); 410 } 411 412 od = (struct open_disk *)malloc(sizeof(struct open_disk)); 413 if (!od) { 414 DEBUG("no memory"); 415 return (ENOMEM); 416 } 417 418 /* Look up BIOS unit number, intialise open_disk structure */ 419 od->od_dkunit = dev->d_kind.biosdisk.unit; 420 od->od_unit = bdinfo[od->od_dkunit].bd_unit; 421 od->od_flags = bdinfo[od->od_dkunit].bd_flags; 422 od->od_boff = 0; 423 error = 0; 424 DEBUG("open '%s', unit 0x%x slice %d partition %c", 425 i386_fmtdev(dev), dev->d_kind.biosdisk.unit, 426 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a'); 427 428 /* Get geometry for this open (removable device may have changed) */ 429 if (bd_getgeom(od)) { 430 DEBUG("can't get geometry"); 431 error = ENXIO; 432 goto out; 433 } 434 435 /* 436 * Following calculations attempt to determine the correct value 437 * for d->od_boff by looking for the slice and partition specified, 438 * or searching for reasonable defaults. 439 */ 440 441 /* 442 * Find the slice in the DOS slice table. 443 */ 444#ifdef PC98 445 if (od->od_flags & BD_FLOPPY) { 446 sector = 0; 447 goto unsliced; 448 } 449#endif 450 if (bd_read(od, 0, 1, buf)) { 451 DEBUG("error reading MBR"); 452 error = EIO; 453 goto out; 454 } 455 456 /* 457 * Check the slice table magic. 458 */ 459 if ((buf[0x1fe] != 0x55) || (buf[0x1ff] != 0xaa)) { 460 /* If a slice number was explicitly supplied, this is an error */ 461 if (dev->d_kind.biosdisk.slice > 0) { 462 DEBUG("no slice table/MBR (no magic)"); 463 error = ENOENT; 464 goto out; 465 } 466 sector = 0; 467 goto unsliced; /* may be a floppy */ 468 } 469#ifdef PC98 470 if (bd_read(od, 1, 1, buf)) { 471 DEBUG("error reading MBR"); 472 error = EIO; 473 goto out; 474 } 475#endif 476 bcopy(buf + DOSPARTOFF, &od->od_parttab, sizeof(struct dos_partition) * NDOSPART); 477 dptr = &od->od_parttab[0]; 478 od->od_flags |= BD_PARTTABOK; 479 480 /* Is this a request for the whole disk? */ 481 if (dev->d_kind.biosdisk.slice == -1) { 482 sector = 0; 483 goto unsliced; 484 } 485 486 /* Try to auto-detect the best slice; this should always give a slice number */ 487 if (dev->d_kind.biosdisk.slice == 0) 488 dev->d_kind.biosdisk.slice = bd_bestslice(dptr); 489 490 switch (dev->d_kind.biosdisk.slice) { 491 case -1: 492 error = ENOENT; 493 goto out; 494 case 0: 495 sector = 0; 496 goto unsliced; 497 default: 498 break; 499 } 500 501 /* 502 * Accept the supplied slice number unequivocally (we may be looking 503 * at a DOS partition). 504 */ 505 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */ 506#ifdef PC98 507 sector = dptr->dp_scyl * od->od_hds * od->od_sec + dptr->dp_shd * od->od_sec + dptr->dp_ssect; 508 { 509 int end = dptr->dp_ecyl * od->od_hds * od->od_sec + dptr->dp_ehd * od->od_sec + dptr->dp_esect; 510 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, end-sector); 511 } 512#else 513 sector = dptr->dp_start; 514 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size); 515#endif 516 517 /* 518 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition 519 */ 520#ifdef PC98 521 if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 522#else 523 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 524#endif 525 dev->d_kind.biosdisk.partition = 0; 526 527 unsliced: 528 /* 529 * Now we have the slice offset, look for the partition in the disklabel if we have 530 * a partition to start with. 531 * 532 * XXX we might want to check the label checksum. 533 */ 534 if (dev->d_kind.biosdisk.partition < 0) { 535 od->od_boff = sector; /* no partition, must be after the slice */ 536 DEBUG("opening raw slice"); 537 } else { 538 if (bd_read(od, sector + LABELSECTOR, 1, buf)) { 539 DEBUG("error reading disklabel"); 540 error = EIO; 541 goto out; 542 } 543 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel); 544 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel)); 545 lp = &od->od_disklabel; 546 od->od_flags |= BD_LABELOK; 547 548 if (lp->d_magic != DISKMAGIC) { 549 DEBUG("no disklabel"); 550 error = ENOENT; 551 goto out; 552 } 553 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) { 554 DEBUG("partition '%c' exceeds partitions in table (a-'%c')", 555 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions); 556 error = EPART; 557 goto out; 558 559 } 560 561 /* Complain if the partition type is wrong */ 562 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) && 563 !(od->od_flags & BD_FLOPPY)) /* Floppies often have bogus fstype */ 564 DEBUG("warning, partition marked as unused"); 565 566 od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset; 567 } 568 569 out: 570 if (error) { 571 free(od); 572 } else { 573 *odp = od; /* return the open disk */ 574 } 575 return(error); 576} 577 578 579/* 580 * Search for a slice with the following preferences: 581 * 582 * 1: Active FreeBSD slice 583 * 2: Non-active FreeBSD slice 584 * 3: Active FAT/FAT32 slice 585 * 4: non-active FAT/FAT32 slice 586 */ 587#define PREF_FBSD_ACT 0 588#define PREF_FBSD 1 589#define PREF_DOS_ACT 2 590#define PREF_DOS 3 591#define PREF_NONE 4 592 593static int 594bd_bestslice(struct dos_partition *dptr) 595{ 596 int i; 597 int preflevel, pref; 598 599 600#ifndef PC98 601 /* 602 * Check for the historically bogus MBR found on true dedicated disks 603 */ 604 if ((dptr[3].dp_typ == DOSPTYP_386BSD) && 605 (dptr[3].dp_start == 0) && 606 (dptr[3].dp_size == 50000)) 607 return(0); 608#endif 609 610 preflevel = PREF_NONE; 611 pref = -1; 612 613 /* 614 * XXX No support here for 'extended' slices 615 */ 616 for (i = 0; i < NDOSPART; i++) { 617#ifdef PC98 618 switch(dptr[i].dp_mid & 0x7f) { 619 case DOSMID_386BSD & 0x7f: /* FreeBSD */ 620 if ((dptr[i].dp_mid & 0x80) && (preflevel > PREF_FBSD_ACT)) { 621 pref = i; 622 preflevel = PREF_FBSD_ACT; 623 } else if (preflevel > PREF_FBSD) { 624 pref = i; 625 preflevel = PREF_FBSD; 626 } 627 break; 628 629 case 0x11: /* DOS/Windows */ 630 case 0x20: 631 case 0x21: 632 case 0x22: 633 case 0x23: 634 case 0x63: 635 if ((dptr[i].dp_mid & 0x80) && (preflevel > PREF_DOS_ACT)) { 636 pref = i; 637 preflevel = PREF_DOS_ACT; 638 } else if (preflevel > PREF_DOS) { 639 pref = i; 640 preflevel = PREF_DOS; 641 } 642 break; 643 } 644#else 645 switch(dptr[i].dp_typ) { 646 case DOSPTYP_386BSD: /* FreeBSD */ 647 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_FBSD_ACT)) { 648 pref = i; 649 preflevel = PREF_FBSD_ACT; 650 } else if (preflevel > PREF_FBSD) { 651 pref = i; 652 preflevel = PREF_FBSD; 653 } 654 break; 655 656 case 0x04: /* DOS/Windows */ 657 case 0x06: 658 case 0x0b: 659 case 0x0c: 660 case 0x0e: 661 case 0x63: 662 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_DOS_ACT)) { 663 pref = i; 664 preflevel = PREF_DOS_ACT; 665 } else if (preflevel > PREF_DOS) { 666 pref = i; 667 preflevel = PREF_DOS; 668 } 669 break; 670 } 671#endif 672 } 673 return(pref + 1); /* slices numbered 1-4 */ 674} 675 676 677static int 678bd_close(struct open_file *f) 679{ 680 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data); 681 682 bd_closedisk(od); 683 return(0); 684} 685 686static void 687bd_closedisk(struct open_disk *od) 688{ 689 DEBUG("open_disk %p", od); 690#if 0 691 /* XXX is this required? (especially if disk already open...) */ 692 if (od->od_flags & BD_FLOPPY) 693 delay(3000000); 694#endif 695 free(od); 696} 697 698static int 699bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize) 700{ 701 struct bcache_devdata bcd; 702 703 bcd.dv_strategy = bd_realstrategy; 704 bcd.dv_devdata = devdata; 705 return(bcache_strategy(&bcd, rw, dblk, size, buf, rsize)); 706} 707 708static int 709bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize) 710{ 711 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 712 int blks; 713#ifdef BD_SUPPORT_FRAGS 714 char fragbuf[BIOSDISK_SECSIZE]; 715 size_t fragsize; 716 717 fragsize = size % BIOSDISK_SECSIZE; 718#else 719 if (size % BIOSDISK_SECSIZE) 720 panic("bd_strategy: %d bytes I/O not multiple of block size", size); 721#endif 722 723 DEBUG("open_disk %p", od); 724 725 if (rw != F_READ) 726 return(EROFS); 727 728 729 blks = size / BIOSDISK_SECSIZE; 730 DEBUG("read %d from %d+%d to %p", blks, od->od_boff, dblk, buf); 731 732 if (rsize) 733 *rsize = 0; 734 if (blks && bd_read(od, dblk + od->od_boff, blks, buf)) { 735 DEBUG("read error"); 736 return (EIO); 737 } 738#ifdef BD_SUPPORT_FRAGS 739 DEBUG("bd_strategy: frag read %d from %d+%d+d to %p", 740 fragsize, od->od_boff, dblk, blks, buf + (blks * BIOSDISK_SECSIZE)); 741 if (fragsize && bd_read(od, dblk + od->od_boff + blks, 1, fragsize)) { 742 DEBUG("frag read error"); 743 return(EIO); 744 } 745 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize); 746#endif 747 if (rsize) 748 *rsize = size; 749 return (0); 750} 751 752/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */ 753#define FLOPPY_BOUNCEBUF 18 754 755static int 756bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 757{ 758 int x, bpc, cyl, hd, sec, result, resid, cnt, retry, maxfer; 759 caddr_t p, xp, bbuf, breg; 760 761 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */ 762 resid = blks; 763 p = dest; 764 765 /* Decide whether we have to bounce */ 766#ifdef PC98 767 if (((od->od_unit & 0xf0) == 0x90 || (od->od_unit & 0xf0) == 0x30) && 768#else 769 if ((od->od_unit < 0x80) && 770#endif 771 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) { 772 773 /* 774 * There is a 64k physical boundary somewhere in the destination buffer, so we have 775 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we 776 * need to. Use the bottom half unless there is a break there, in which case we 777 * use the top half. 778 */ 779 x = min(FLOPPY_BOUNCEBUF, blks); 780 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE); 781 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) { 782 breg = bbuf; 783 } else { 784 breg = bbuf + x * BIOSDISK_SECSIZE; 785 } 786 maxfer = x; /* limit transfers to bounce region size */ 787 } else { 788 bbuf = NULL; 789 maxfer = 0; 790 } 791 792 while (resid > 0) { 793 x = dblk; 794 cyl = x / bpc; /* block # / blocks per cylinder */ 795 x %= bpc; /* block offset into cylinder */ 796 hd = x / od->od_sec; /* offset / blocks per track */ 797 sec = x % od->od_sec; /* offset into track */ 798 799 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */ 800 x = min(od->od_sec - sec, resid); 801 if (maxfer > 0) 802 x = min(x, maxfer); /* fit bounce buffer */ 803 804 /* where do we transfer to? */ 805 xp = bbuf == NULL ? p : breg; 806 807 /* correct sector number for 1-based BIOS numbering */ 808#ifdef PC98 809 if ((od->od_unit & 0xf0) == 0x30 || (od->od_unit & 0xf0) == 0x90) 810 sec++; 811#else 812 sec++; 813#endif 814 815 /* Loop retrying the operation a couple of times. The BIOS may also retry. */ 816 for (retry = 0; retry < 3; retry++) { 817 /* if retrying, reset the drive */ 818 if (retry > 0) { 819#ifdef PC98 820#else 821 v86.ctl = V86_FLAGS; 822 v86.addr = 0x13; 823 v86.eax = 0; 824 v86.edx = od->od_unit; 825 v86int(); 826#endif 827 } 828 829 /* build request XXX support EDD requests too */ 830#ifdef PC98 831 v86.ctl = 0; 832 v86.addr = 0x1b; 833 if (od->od_flags & BD_FLOPPY) { 834 v86.eax = 0xd600 | od->od_unit; 835 v86.ecx = 0x0200 | (cyl & 0xff); 836 } 837 else { 838 v86.eax = 0x0600 | od->od_unit; 839 v86.ecx = cyl; 840 } 841 v86.edx = (hd << 8) | sec; 842 v86.ebx = x * BIOSDISK_SECSIZE; 843 v86.es = VTOPSEG(xp); 844 v86.ebp = VTOPOFF(xp); 845 v86int(); 846#else 847 v86.ctl = V86_FLAGS; 848 v86.addr = 0x13; 849 v86.eax = 0x200 | x; 850 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec; 851 v86.edx = (hd << 8) | od->od_unit; 852 v86.es = VTOPSEG(xp); 853 v86.ebx = VTOPOFF(xp); 854 v86int(); 855#endif 856 result = (v86.efl & 0x1); 857 if (result == 0) 858 break; 859 } 860 861#ifdef PC98 862 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, od->od_flags & BD_FLOPPY ? sec - 1 : sec, p, VTOP(p), result ? "failed" : "ok"); 863 /* BUG here, cannot use v86 in printf because putchar uses it too */ 864 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 865 od->od_flags & BD_FLOPPY ? 0xd600 | od->od_unit : 0x0600 | od->od_unit, 866 od->od_flags & BD_FLOPPY ? 0x0200 | cyl : cyl, (hd << 8) | sec, 867 (v86.eax >> 8) & 0xff); 868#else 869 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok"); 870 /* BUG here, cannot use v86 in printf because putchar uses it too */ 871 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 872 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff); 873#endif 874 if (result) { 875 if (bbuf != NULL) 876 free(bbuf); 877 return(-1); 878 } 879 if (bbuf != NULL) 880 bcopy(breg, p, x * BIOSDISK_SECSIZE); 881 p += (x * BIOSDISK_SECSIZE); 882 dblk += x; 883 resid -= x; 884 } 885 886/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */ 887 if (bbuf != NULL) 888 free(bbuf); 889 return(0); 890} 891 892static int 893bd_getgeom(struct open_disk *od) 894{ 895 896#ifdef PC98 897 if (od->od_flags & BD_FLOPPY) { 898 od->od_cyl = 79; 899 od->od_hds = 2; 900 od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15; 901 } 902 else { 903 v86.ctl = 0; 904 v86.addr = 0x1b; 905 v86.eax = 0x8400 | od->od_unit; 906 v86int(); 907 908 od->od_cyl = v86.ecx; 909 od->od_hds = (v86.edx >> 8) & 0xff; 910 od->od_sec = v86.edx & 0xff; 911 } 912#else 913 v86.ctl = V86_FLAGS; 914 v86.addr = 0x13; 915 v86.eax = 0x800; 916 v86.edx = od->od_unit; 917 v86int(); 918 919 if ((v86.efl & 0x1) || /* carry set */ 920 ((v86.edx & 0xff) <= (od->od_unit & 0x7f))) /* unit # bad */ 921 return(1); 922 923 /* convert max cyl # -> # of cylinders */ 924 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1; 925 /* convert max head # -> # of heads */ 926 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1; 927 od->od_sec = v86.ecx & 0x3f; 928#endif 929 930 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec); 931 return(0); 932} 933 934/* 935 * Return a suitable dev_t value for (dev). 936 * 937 * In the case where it looks like (dev) is a SCSI disk, we allow the number of 938 * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 939 */ 940int 941bd_getdev(struct i386_devdesc *dev) 942{ 943 struct open_disk *od; 944 int biosdev; 945 int major; 946 int rootdev; 947 char *nip, *cp; 948 int unitofs = 0, i, unit; 949 950 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit); 951 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev); 952 if (biosdev == -1) /* not a BIOS device */ 953 return(-1); 954 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */ 955 return(-1); 956 957#ifdef PC98 958 if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) { 959#else 960 if (biosdev < 0x80) { 961#endif 962 /* floppy (or emulated floppy) or ATAPI device */ 963 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) { 964 /* is an ATAPI disk */ 965 major = WFDMAJOR; 966 } else { 967 /* is a floppy disk */ 968 major = FDMAJOR; 969 } 970 } else { 971 /* harddisk */ 972 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) { 973 /* label OK, disk labelled as SCSI */ 974 major = DAMAJOR; 975 /* check for unit number correction hint, now deprecated */ 976 if ((nip = getenv("num_ide_disks")) != NULL) { 977 i = strtol(nip, &cp, 0); 978 /* check for parse error */ 979 if ((cp != nip) && (*cp == 0)) 980 unitofs = i; 981 } 982 } else { 983 /* assume an IDE disk */ 984 major = WDMAJOR; 985 } 986 } 987 /* XXX a better kludge to set the root disk unit number */ 988 if ((nip = getenv("root_disk_unit")) != NULL) { 989 i = strtol(nip, &cp, 0); 990 /* check for parse error */ 991 if ((cp != nip) && (*cp == 0)) 992 unit = i; 993 } else { 994#ifdef PC98 995 if ((biosdev & 0xf0) == 0xa0) 996 unit = bdinfo[dev->d_kind.biosdisk.unit].bd_da_unit; 997 else 998 unit = biosdev & 0xf; 999#else 1000 unit = (biosdev & 0x7f) - unitofs; /* allow for #wd compenstation in da case */ 1001#endif 1002 } 1003 1004 rootdev = MAKEBOOTDEV(major, 1005 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */ 1006 (dev->d_kind.biosdisk.slice + 1) & 0xf, 1007 unit, 1008 dev->d_kind.biosdisk.partition); 1009 DEBUG("dev is 0x%x\n", rootdev); 1010 return(rootdev); 1011} 1012 1013/* 1014 * Fix (dev) so that it refers to the 'real' disk/slice/partition that it implies. 1015 */ 1016int 1017bd_fixupdev(struct i386_devdesc *dev) 1018{ 1019 struct open_disk *od; 1020 1021 /* 1022 * Open the disk. This will fix up the slice and partition fields. 1023 */ 1024 if (bd_opendisk(&od, dev) != 0) 1025 return(ENOENT); 1026 1027 bd_closedisk(od); 1028} 1029