biosdisk.c revision 54819
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 54819 1999-12-19 14:05:46Z nyan $ 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 */ 38 39#include <stand.h> 40 41#include <sys/disklabel.h> 42#include <sys/diskslice.h> 43#include <sys/reboot.h> 44 45#include <stdarg.h> 46 47#include <bootstrap.h> 48#include <btxv86.h> 49#include "libi386.h" 50 51#define BIOSDISK_SECSIZE 512 52#define BUFSIZE (1 * BIOSDISK_SECSIZE) 53#define MAXBDDEV MAXDEV 54 55#define DT_ATAPI 0x10 /* disk type for ATAPI floppies */ 56#define WDMAJOR 0 /* major numbers for devices we frontend for */ 57#define WFDMAJOR 1 58#define FDMAJOR 2 59#define DAMAJOR 4 60 61#ifdef DISK_DEBUG 62# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __FUNCTION__ , ## args) 63#else 64# define DEBUG(fmt, args...) 65#endif 66 67struct open_disk { 68 int od_dkunit; /* disk unit number */ 69 int od_unit; /* BIOS unit number */ 70 int od_cyl; /* BIOS geometry */ 71 int od_hds; 72 int od_sec; 73 int od_boff; /* block offset from beginning of BIOS disk */ 74 int od_flags; 75#define BD_MODEMASK 0x3 76#define BD_MODEINT13 0x0 77#define BD_MODEEDD1 0x1 78#define BD_MODEEDD3 0x2 79#define BD_FLOPPY (1<<2) 80 struct disklabel od_disklabel; 81 struct dos_partition od_parttab[NDOSPART]; /* XXX needs to grow for extended partitions */ 82#define BD_LABELOK (1<<3) 83#define BD_PARTTABOK (1<<4) 84}; 85 86/* 87 * List of BIOS devices, translation from disk unit number to 88 * BIOS unit number. 89 */ 90static struct bdinfo 91{ 92 int bd_unit; /* BIOS unit number */ 93 int bd_flags; 94 int bd_type; /* BIOS 'drive type' (floppy only) */ 95#ifdef PC98 96 int bd_da_unit; /* kernel unit number for da */ 97#endif 98} bdinfo [MAXBDDEV]; 99static int nbdinfo = 0; 100 101static int bd_getgeom(struct open_disk *od); 102static int bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest); 103 104static int bd_int13probe(struct bdinfo *bd); 105 106static void bd_printslice(struct open_disk *od, int offset, char *prefix); 107 108static int bd_init(void); 109static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize); 110static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize); 111static int bd_open(struct open_file *f, ...); 112static int bd_close(struct open_file *f); 113static void bd_print(int verbose); 114 115struct devsw biosdisk = { 116 "disk", 117 DEVT_DISK, 118 bd_init, 119 bd_strategy, 120 bd_open, 121 bd_close, 122 noioctl, 123 bd_print 124}; 125 126static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev); 127static void bd_closedisk(struct open_disk *od); 128static int bd_bestslice(struct dos_partition *dptr); 129 130/* 131 * Translate between BIOS device numbers and our private unit numbers. 132 */ 133int 134bd_bios2unit(int biosdev) 135{ 136 int i; 137 138 DEBUG("looking for bios device 0x%x", biosdev); 139 for (i = 0; i < nbdinfo; i++) { 140 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit); 141 if (bdinfo[i].bd_unit == biosdev) 142 return(i); 143 } 144 return(-1); 145} 146 147int 148bd_unit2bios(int unit) 149{ 150 if ((unit >= 0) && (unit < nbdinfo)) 151 return(bdinfo[unit].bd_unit); 152 return(-1); 153} 154 155/* 156 * Quiz the BIOS for disk devices, save a little info about them. 157 * 158 * XXX should we be consulting the BIOS equipment list, specifically 159 * the value at 0x475? 160 */ 161static int 162bd_init(void) 163{ 164 int base, unit; 165 166#ifdef PC98 167 int da_drive=0, n=-0x10; 168 /* sequence 0x90, 0x80, 0xa0 */ 169 for (base = 0x90; base <= 0xa0; base += n, n += 0x30) { 170 for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); unit++) { 171 bdinfo[nbdinfo].bd_unit = unit; 172 bdinfo[nbdinfo].bd_flags = (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0; 173 174 /* XXX add EDD probes */ 175 if (!bd_int13probe(&bdinfo[nbdinfo])){ 176 if (((unit & 0xf0) == 0x90 && (unit & 0x0f) < 4) || 177 ((unit & 0xf0) == 0xa0 && (unit & 0x0f) < 6)) 178 continue; /* Target IDs are not contiguous. */ 179 else 180 break; 181 } 182 183 if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){ 184 /* available 1.44MB access? */ 185 if (*(u_char *)PTOV(0xA15AE) & (1<<(unit & 0xf))){ 186 /* boot media 1.2MB FD? */ 187 if ((*(u_char *)PTOV(0xA1584) & 0xf0) != 0x90) 188 bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf); 189 } 190 } 191 else { 192 if ((unit & 0xa0) == 0xa0) 193 bdinfo[nbdinfo].bd_da_unit = da_drive++; 194 } 195 /* XXX we need "disk aliases" to make this simpler */ 196 printf("BIOS drive %c: is disk%d\n", 197 'A' + nbdinfo, nbdinfo); 198 nbdinfo++; 199 } 200 } 201#else 202 /* sequence 0, 0x80 */ 203 for (base = 0; base <= 0x80; base += 0x80) { 204 for (unit = base; (nbdinfo < MAXBDDEV); unit++) { 205 bdinfo[nbdinfo].bd_unit = unit; 206 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0; 207 208 /* XXX add EDD probes */ 209 if (!bd_int13probe(&bdinfo[nbdinfo])) 210 break; 211 212 /* XXX we need "disk aliases" to make this simpler */ 213 printf("BIOS drive %c: is disk%d\n", 214 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo); 215 nbdinfo++; 216 } 217 } 218#endif 219 return(0); 220} 221 222/* 223 * Try to detect a device supported by the legacy int13 BIOS 224 */ 225 226static int 227bd_int13probe(struct bdinfo *bd) 228{ 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 539 if (bd_read(od, sector + LABELSECTOR, 1, buf)) { 540 DEBUG("error reading disklabel"); 541 error = EIO; 542 goto out; 543 } 544 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel); 545 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel)); 546 lp = &od->od_disklabel; 547 od->od_flags |= BD_LABELOK; 548 549 if (lp->d_magic != DISKMAGIC) { 550 DEBUG("no disklabel"); 551 error = ENOENT; 552 goto out; 553 } 554 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) { 555 DEBUG("partition '%c' exceeds partitions in table (a-'%c')", 556 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions); 557 error = EPART; 558 goto out; 559 560 } 561 562 /* Complain if the partition type is wrong */ 563 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) && 564 !(od->od_flags & BD_FLOPPY)) /* Floppies often have bogus fstype */ 565 DEBUG("warning, partition marked as unused"); 566 567 od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset; 568 } 569 570 out: 571 if (error) { 572 free(od); 573 } else { 574 *odp = od; /* return the open disk */ 575 } 576 return(error); 577} 578 579 580/* 581 * Search for a slice with the following preferences: 582 * 583 * 1: Active FreeBSD slice 584 * 2: Non-active FreeBSD slice 585 * 3: Active FAT/FAT32 slice 586 * 4: non-active FAT/FAT32 slice 587 */ 588#define PREF_FBSD_ACT 0 589#define PREF_FBSD 1 590#define PREF_DOS_ACT 2 591#define PREF_DOS 3 592#define PREF_NONE 4 593 594static int 595bd_bestslice(struct dos_partition *dptr) 596{ 597 int i; 598 int preflevel, pref; 599 600 601#ifndef PC98 602 /* 603 * Check for the historically bogus MBR found on true dedicated disks 604 */ 605 if ((dptr[3].dp_typ == DOSPTYP_386BSD) && 606 (dptr[3].dp_start == 0) && 607 (dptr[3].dp_size == 50000)) 608 return(0); 609#endif 610 611 preflevel = PREF_NONE; 612 pref = -1; 613 614 /* 615 * XXX No support here for 'extended' slices 616 */ 617 for (i = 0; i < NDOSPART; i++) { 618#ifdef PC98 619 switch(dptr[i].dp_mid & 0x7f) { 620 case DOSMID_386BSD & 0x7f: /* FreeBSD */ 621 if ((dptr[i].dp_mid & 0x80) && (preflevel > PREF_FBSD_ACT)) { 622 pref = i; 623 preflevel = PREF_FBSD_ACT; 624 } else if (preflevel > PREF_FBSD) { 625 pref = i; 626 preflevel = PREF_FBSD; 627 } 628 break; 629 630 case 0x11: /* DOS/Windows */ 631 case 0x20: 632 case 0x21: 633 case 0x22: 634 case 0x23: 635 case 0x63: 636 if ((dptr[i].dp_mid & 0x80) && (preflevel > PREF_DOS_ACT)) { 637 pref = i; 638 preflevel = PREF_DOS_ACT; 639 } else if (preflevel > PREF_DOS) { 640 pref = i; 641 preflevel = PREF_DOS; 642 } 643 break; 644 } 645#else 646 switch(dptr[i].dp_typ) { 647 case DOSPTYP_386BSD: /* FreeBSD */ 648 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_FBSD_ACT)) { 649 pref = i; 650 preflevel = PREF_FBSD_ACT; 651 } else if (preflevel > PREF_FBSD) { 652 pref = i; 653 preflevel = PREF_FBSD; 654 } 655 break; 656 657 case 0x04: /* DOS/Windows */ 658 case 0x06: 659 case 0x0b: 660 case 0x0c: 661 case 0x0e: 662 case 0x63: 663 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_DOS_ACT)) { 664 pref = i; 665 preflevel = PREF_DOS_ACT; 666 } else if (preflevel > PREF_DOS) { 667 pref = i; 668 preflevel = PREF_DOS; 669 } 670 break; 671 } 672#endif 673 } 674 return(pref + 1); /* slices numbered 1-4 */ 675} 676 677 678static int 679bd_close(struct open_file *f) 680{ 681 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data); 682 683 bd_closedisk(od); 684 return(0); 685} 686 687static void 688bd_closedisk(struct open_disk *od) 689{ 690 DEBUG("open_disk %p", od); 691#if 0 692 /* XXX is this required? (especially if disk already open...) */ 693 if (od->od_flags & BD_FLOPPY) 694 delay(3000000); 695#endif 696 free(od); 697} 698 699static int 700bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize) 701{ 702 struct bcache_devdata bcd; 703 704 bcd.dv_strategy = bd_realstrategy; 705 bcd.dv_devdata = devdata; 706 return(bcache_strategy(&bcd, rw, dblk, size, buf, rsize)); 707} 708 709static int 710bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize) 711{ 712 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 713 int blks; 714#ifdef BD_SUPPORT_FRAGS 715 char fragbuf[BIOSDISK_SECSIZE]; 716 size_t fragsize; 717 718 fragsize = size % BIOSDISK_SECSIZE; 719#else 720 if (size % BIOSDISK_SECSIZE) 721 panic("bd_strategy: %d bytes I/O not multiple of block size", size); 722#endif 723 724 DEBUG("open_disk %p", od); 725 726 if (rw != F_READ) 727 return(EROFS); 728 729 730 blks = size / BIOSDISK_SECSIZE; 731 DEBUG("read %d from %d+%d to %p", blks, od->od_boff, dblk, buf); 732 733 if (rsize) 734 *rsize = 0; 735 if (blks && bd_read(od, dblk + od->od_boff, blks, buf)) { 736 DEBUG("read error"); 737 return (EIO); 738 } 739#ifdef BD_SUPPORT_FRAGS 740 DEBUG("bd_strategy: frag read %d from %d+%d+d to %p", 741 fragsize, od->od_boff, dblk, blks, buf + (blks * BIOSDISK_SECSIZE)); 742 if (fragsize && bd_read(od, dblk + od->od_boff + blks, 1, fragsize)) { 743 DEBUG("frag read error"); 744 return(EIO); 745 } 746 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize); 747#endif 748 if (rsize) 749 *rsize = size; 750 return (0); 751} 752 753/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */ 754#define FLOPPY_BOUNCEBUF 18 755 756static int 757bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 758{ 759 int x, bpc, cyl, hd, sec, result, resid, cnt, retry, maxfer; 760 caddr_t p, xp, bbuf, breg; 761 762 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */ 763 resid = blks; 764 p = dest; 765 766 /* Decide whether we have to bounce */ 767#ifdef PC98 768 if ( 769#else 770 if ((od->od_unit < 0x80) && 771#endif 772 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) { 773 774 /* 775 * There is a 64k physical boundary somewhere in the destination buffer, so we have 776 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we 777 * need to. Use the bottom half unless there is a break there, in which case we 778 * use the top half. 779 */ 780#ifdef PC98 781 x = min(od->od_sec, blks); 782#else 783 x = min(FLOPPY_BOUNCEBUF, blks); 784#endif 785 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE); 786 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) { 787 breg = bbuf; 788 } else { 789 breg = bbuf + x * BIOSDISK_SECSIZE; 790 } 791 maxfer = x; /* limit transfers to bounce region size */ 792 } else { 793 bbuf = NULL; 794 maxfer = 0; 795 } 796 797 while (resid > 0) { 798 x = dblk; 799 cyl = x / bpc; /* block # / blocks per cylinder */ 800 x %= bpc; /* block offset into cylinder */ 801 hd = x / od->od_sec; /* offset / blocks per track */ 802 sec = x % od->od_sec; /* offset into track */ 803 804 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */ 805 x = min(od->od_sec - sec, resid); 806 if (maxfer > 0) 807 x = min(x, maxfer); /* fit bounce buffer */ 808 809 /* where do we transfer to? */ 810 xp = bbuf == NULL ? p : breg; 811 812 /* correct sector number for 1-based BIOS numbering */ 813#ifdef PC98 814 if ((od->od_unit & 0xf0) == 0x30 || (od->od_unit & 0xf0) == 0x90) 815 sec++; 816#else 817 sec++; 818#endif 819 820 /* Loop retrying the operation a couple of times. The BIOS may also retry. */ 821 for (retry = 0; retry < 3; retry++) { 822 /* if retrying, reset the drive */ 823 if (retry > 0) { 824#ifdef PC98 825 v86.ctl = V86_FLAGS; 826 v86.addr = 0x1b; 827 v86.eax = 0x0300 | od->od_unit; 828#else 829 v86.ctl = V86_FLAGS; 830 v86.addr = 0x13; 831 v86.eax = 0; 832 v86.edx = od->od_unit; 833#endif 834 v86int(); 835 } 836 837 /* build request XXX support EDD requests too */ 838 v86.ctl = V86_FLAGS; 839#ifdef PC98 840 v86.addr = 0x1b; 841 if (od->od_flags & BD_FLOPPY) { 842 v86.eax = 0xd600 | od->od_unit; 843 v86.ecx = 0x0200 | (cyl & 0xff); 844 } 845 else { 846 v86.eax = 0x0600 | od->od_unit; 847 v86.ecx = cyl; 848 } 849 v86.edx = (hd << 8) | sec; 850 v86.ebx = x * BIOSDISK_SECSIZE; 851 v86.es = VTOPSEG(xp); 852 v86.ebp = VTOPOFF(xp); 853#else 854 v86.addr = 0x13; 855 v86.eax = 0x200 | x; 856 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec; 857 v86.edx = (hd << 8) | od->od_unit; 858 v86.es = VTOPSEG(xp); 859 v86.ebx = VTOPOFF(xp); 860#endif 861 v86int(); 862 result = (v86.efl & 0x1); 863 if (result == 0) 864 break; 865 } 866 867#ifdef PC98 868 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"); 869 /* BUG here, cannot use v86 in printf because putchar uses it too */ 870 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 871 od->od_flags & BD_FLOPPY ? 0xd600 | od->od_unit : 0x0600 | od->od_unit, 872 od->od_flags & BD_FLOPPY ? 0x0200 | cyl : cyl, (hd << 8) | sec, 873 (v86.eax >> 8) & 0xff); 874#else 875 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok"); 876 /* BUG here, cannot use v86 in printf because putchar uses it too */ 877 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 878 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff); 879#endif 880 if (result) { 881 if (bbuf != NULL) 882 free(bbuf); 883 return(-1); 884 } 885 if (bbuf != NULL) 886 bcopy(breg, p, x * BIOSDISK_SECSIZE); 887 p += (x * BIOSDISK_SECSIZE); 888 dblk += x; 889 resid -= x; 890 } 891 892/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */ 893 if (bbuf != NULL) 894 free(bbuf); 895 return(0); 896} 897 898static int 899bd_getgeom(struct open_disk *od) 900{ 901 902#ifdef PC98 903 if (od->od_flags & BD_FLOPPY) { 904 od->od_cyl = 79; 905 od->od_hds = 2; 906 od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15; 907 } 908 else { 909 v86.ctl = V86_FLAGS; 910 v86.addr = 0x1b; 911 v86.eax = 0x8400 | od->od_unit; 912 v86int(); 913 914 od->od_cyl = v86.ecx; 915 od->od_hds = (v86.edx >> 8) & 0xff; 916 od->od_sec = v86.edx & 0xff; 917 if (v86.efl & 0x1) 918 return(1); 919 } 920#else 921 v86.ctl = V86_FLAGS; 922 v86.addr = 0x13; 923 v86.eax = 0x800; 924 v86.edx = od->od_unit; 925 v86int(); 926 927 if ((v86.efl & 0x1) || /* carry set */ 928 ((v86.edx & 0xff) <= (od->od_unit & 0x7f))) /* unit # bad */ 929 return(1); 930 931 /* convert max cyl # -> # of cylinders */ 932 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1; 933 /* convert max head # -> # of heads */ 934 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1; 935 od->od_sec = v86.ecx & 0x3f; 936#endif 937 938 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec); 939 return(0); 940} 941 942/* 943 * Return the BIOS geometry of a given "fixed drive" in a format 944 * suitable for the legacy bootinfo structure. Since the kernel is 945 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we 946 * prefer to get the information directly, rather than rely on being 947 * able to put it together from information already maintained for 948 * different purposes and for a probably different number of drives. 949 * 950 * For valid drives, the geometry is expected in the format (31..0) 951 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are 952 * indicated by returning the geometry of a "1.2M" PC-format floppy 953 * disk. And, incidentally, what is returned is not the geometry as 954 * such but the highest valid cylinder, head, and sector numbers. 955 */ 956u_int32_t 957bd_getbigeom(int bunit) 958{ 959 960#ifdef PC98 961 int unit = 0x80; 962 int hds = 0; 963 u_int addr = 0xA155d; 964 while (unit < 0xa7) { 965 if (*(u_char *)PTOV(addr) & ((1 << unit) & 0xf)) 966 if (++hds == bunit) 967 break; 968 if (++unit == 0x84) { 969 unit = 0xa0; 970 addr = 0xA1482; 971 } 972 } 973 if (unit == 0xa7) 974 return 0x4f010f; 975 v86.ctl = V86_FLAGS; 976 v86.addr = 0x1b; 977 v86.eax = 0x8400 | unit; 978 v86int(); 979 if (v86.efl & 0x1) 980 return 0x4f010f; 981 return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff); 982#else 983 v86.ctl = V86_FLAGS; 984 v86.addr = 0x13; 985 v86.eax = 0x800; 986 v86.edx = 0x80 + bunit; 987 v86int(); 988 if (v86.efl & 0x1) 989 return 0x4f010f; 990 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) | 991 (v86.edx & 0xff00) | (v86.ecx & 0x3f); 992#endif 993} 994 995/* 996 * Return a suitable dev_t value for (dev). 997 * 998 * In the case where it looks like (dev) is a SCSI disk, we allow the number of 999 * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 1000 */ 1001int 1002bd_getdev(struct i386_devdesc *dev) 1003{ 1004 struct open_disk *od; 1005 int biosdev; 1006 int major; 1007 int rootdev; 1008 char *nip, *cp; 1009 int unitofs = 0, i, unit; 1010 1011 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit); 1012 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev); 1013 if (biosdev == -1) /* not a BIOS device */ 1014 return(-1); 1015 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */ 1016 return(-1); 1017 1018#ifdef PC98 1019 if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) { 1020#else 1021 if (biosdev < 0x80) { 1022#endif 1023 /* floppy (or emulated floppy) or ATAPI device */ 1024 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) { 1025 /* is an ATAPI disk */ 1026 major = WFDMAJOR; 1027 } else { 1028 /* is a floppy disk */ 1029 major = FDMAJOR; 1030 } 1031 } else { 1032 /* harddisk */ 1033 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) { 1034 /* label OK, disk labelled as SCSI */ 1035 major = DAMAJOR; 1036 /* check for unit number correction hint, now deprecated */ 1037 if ((nip = getenv("num_ide_disks")) != NULL) { 1038 i = strtol(nip, &cp, 0); 1039 /* check for parse error */ 1040 if ((cp != nip) && (*cp == 0)) 1041 unitofs = i; 1042 } 1043 } else { 1044 /* assume an IDE disk */ 1045 major = WDMAJOR; 1046 } 1047 } 1048 /* XXX a better kludge to set the root disk unit number */ 1049 if ((nip = getenv("root_disk_unit")) != NULL) { 1050 i = strtol(nip, &cp, 0); 1051 /* check for parse error */ 1052 if ((cp != nip) && (*cp == 0)) 1053 unit = i; 1054 } else { 1055#ifdef PC98 1056 if ((biosdev & 0xf0) == 0xa0) 1057 unit = bdinfo[dev->d_kind.biosdisk.unit].bd_da_unit; 1058 else 1059 unit = biosdev & 0xf; 1060#else 1061 unit = (biosdev & 0x7f) - unitofs; /* allow for #wd compenstation in da case */ 1062#endif 1063 } 1064 1065 rootdev = MAKEBOOTDEV(major, 1066 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */ 1067 (dev->d_kind.biosdisk.slice + 1) & 0xf, 1068 unit, 1069 dev->d_kind.biosdisk.partition); 1070 DEBUG("dev is 0x%x\n", rootdev); 1071 return(rootdev); 1072} 1073 1074/* 1075 * Fix (dev) so that it refers to the 'real' disk/slice/partition that it implies. 1076 */ 1077int 1078bd_fixupdev(struct i386_devdesc *dev) 1079{ 1080 struct open_disk *od; 1081 1082 /* 1083 * Open the disk. This will fix up the slice and partition fields. 1084 */ 1085 if (bd_opendisk(&od, dev) != 0) 1086 return(ENOENT); 1087 1088 bd_closedisk(od); 1089} 1090