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_drive; 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 hd_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 */
| 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_drive; 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 hd_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 */
|
178 179 if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){ 180 bdinfo[nbdinfo].bd_drive = 'A' + (unit & 0xf); 181 if (*(u_char *)PTOV(0x5AE) & (1<<(unit & 0xf))) 182 /* available 1.44MB access */ 183 bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf); 184 } 185 else 186 bdinfo[nbdinfo].bd_drive = 'C' + hd_drive++; 187 /* XXX we need "disk aliases" to make this simpler */ 188 printf("BIOS drive %c: is disk%d\n", 189 bdinfo[nbdinfo].bd_drive, nbdinfo); 190 nbdinfo++; 191 } 192 } 193#else 194 /* sequence 0, 0x80 */ 195 for (base = 0; base <= 0x80; base += 0x80) { 196 for (unit = base; (nbdinfo < MAXBDDEV); unit++) { 197 bdinfo[nbdinfo].bd_unit = unit; 198 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0; 199 200 /* XXX add EDD probes */ 201 if (!bd_int13probe(&bdinfo[nbdinfo])) 202 break; 203 204 /* XXX we need "disk aliases" to make this simpler */ 205 printf("BIOS drive %c: is disk%d\n", 206 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo); 207 nbdinfo++; 208 } 209 } 210#endif 211 return(0); 212} 213 214/* 215 * Try to detect a device supported by the legacy int13 BIOS 216 */ 217 218static int 219bd_int13probe(struct bdinfo *bd) 220{ 221#ifdef PC98 222 int addr; 223 if (bd->bd_flags & BD_FLOPPY){ 224 addr = 0xa155c; 225 } 226 else { 227 if ((bd->bd_unit & 0xf0) == 0x80) 228 addr = 0xa155d; 229 else 230 addr = 0xa1482; 231 } 232 if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) { 233 bd->bd_flags |= BD_MODEINT13; 234 return(1); 235 } 236 return(0); 237#else 238 v86.ctl = V86_FLAGS; 239 v86.addr = 0x13; 240 v86.eax = 0x800; 241 v86.edx = bd->bd_unit; 242 v86int(); 243 244 if (!(v86.efl & 0x1) && /* carry clear */ 245 ((v86.edx & 0xff) > (bd->bd_unit & 0x7f))) { /* unit # OK */ 246 bd->bd_flags |= BD_MODEINT13; 247 bd->bd_type = v86.ebx & 0xff; 248 return(1); 249 } 250#endif 251 return(0); 252} 253 254/* 255 * Print information about disks 256 */ 257static void 258bd_print(int verbose) 259{ 260 int i, j; 261 char line[80]; 262 struct i386_devdesc dev; 263 struct open_disk *od; 264 struct dos_partition *dptr; 265 266 for (i = 0; i < nbdinfo; i++) { 267#ifdef PC98 268 sprintf(line, " disk%d: BIOS drive %c:\n", i, 269 bdinfo[i].bd_drive); 270#else 271 sprintf(line, " disk%d: BIOS drive %c:\n", i, 272 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80)); 273#endif 274 pager_output(line); 275 276 /* try to open the whole disk */ 277 dev.d_kind.biosdisk.unit = i; 278 dev.d_kind.biosdisk.slice = -1; 279 dev.d_kind.biosdisk.partition = -1; 280 281 if (!bd_opendisk(&od, &dev)) { 282 283 /* Do we have a partition table? */ 284 if (od->od_flags & BD_PARTTABOK) { 285 dptr = &od->od_parttab[0]; 286 287 /* Check for a "truly dedicated" disk */ 288#ifdef PC98 289 for (j = 0; j < NDOSPART; j++) { 290 switch(dptr[j].dp_mid) { 291 case DOSMID_386BSD: 292 sprintf(line, " disk%ds%d", i, j + 1); 293 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); 294 break; 295 default: 296 } 297 } 298#else 299 if ((dptr[3].dp_typ == DOSPTYP_386BSD) && 300 (dptr[3].dp_start == 0) && 301 (dptr[3].dp_size == 50000)) { 302 sprintf(line, " disk%d", i); 303 bd_printslice(od, 0, line); 304 } else { 305 for (j = 0; j < NDOSPART; j++) { 306 switch(dptr[j].dp_typ) { 307 case DOSPTYP_386BSD: 308 sprintf(line, " disk%ds%d", i, j + 1); 309 bd_printslice(od, dptr[j].dp_start, line); 310 break; 311 default: 312 } 313 } 314 315 } 316#endif 317 } 318 bd_closedisk(od); 319 } 320 } 321} 322 323static void 324bd_printslice(struct open_disk *od, int offset, char *prefix) 325{ 326 char line[80]; 327 u_char buf[BIOSDISK_SECSIZE]; 328 struct disklabel *lp; 329 int i; 330 331 /* read disklabel */ 332 if (bd_read(od, offset + LABELSECTOR, 1, buf)) 333 return; 334 lp =(struct disklabel *)(&buf[0]); 335 if (lp->d_magic != DISKMAGIC) { 336 sprintf(line, "bad disklabel\n"); 337 pager_output(line); 338 return; 339 } 340 341 /* Print partitions */ 342 for (i = 0; i < lp->d_npartitions; i++) { 343 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) || (lp->d_partitions[i].p_fstype == FS_SWAP) || 344 ((lp->d_partitions[i].p_fstype == FS_UNUSED) && 345 (od->od_flags & BD_FLOPPY) && (i == 0))) { /* Floppies often have bogus fstype, print 'a' */ 346 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i, 347 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : "FFS", 348 lp->d_partitions[i].p_size / 2048, /* 512-byte sector assumption */ 349 lp->d_partitions[i].p_offset, lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size); 350 pager_output(line); 351 } 352 } 353} 354 355 356/* 357 * Attempt to open the disk described by (dev) for use by (f). 358 * 359 * Note that the philosophy here is "give them exactly what 360 * they ask for". This is necessary because being too "smart" 361 * about what the user might want leads to complications. 362 * (eg. given no slice or partition value, with a disk that is 363 * sliced - are they after the first BSD slice, or the DOS 364 * slice before it?) 365 */ 366static int 367bd_open(struct open_file *f, ...) 368{ 369 va_list ap; 370 struct i386_devdesc *dev; 371 struct open_disk *od; 372 int error; 373 374 va_start(ap, f); 375 dev = va_arg(ap, struct i386_devdesc *); 376 va_end(ap); 377 if ((error = bd_opendisk(&od, dev))) 378 return(error); 379 380 /* 381 * Save our context 382 */ 383 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od; 384 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff); 385 return(0); 386} 387 388static int 389bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev) 390{ 391 struct dos_partition *dptr; 392 struct disklabel *lp; 393 struct open_disk *od; 394 int sector, slice, i; 395 int error; 396 u_char buf[BUFSIZE]; 397 daddr_t pref_slice[4]; 398 399 if (dev->d_kind.biosdisk.unit >= nbdinfo) { 400 DEBUG("attempt to open nonexistent disk"); 401 return(ENXIO); 402 } 403 404 od = (struct open_disk *)malloc(sizeof(struct open_disk)); 405 if (!od) { 406 DEBUG("no memory"); 407 return (ENOMEM); 408 } 409 410 /* Look up BIOS unit number, intialise open_disk structure */ 411 od->od_dkunit = dev->d_kind.biosdisk.unit; 412 od->od_unit = bdinfo[od->od_dkunit].bd_unit; 413 od->od_flags = bdinfo[od->od_dkunit].bd_flags; 414 od->od_boff = 0; 415 error = 0; 416 DEBUG("open '%s', unit 0x%x slice %d partition %c", 417 i386_fmtdev(dev), dev->d_kind.biosdisk.unit, 418 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a'); 419 420 /* Get geometry for this open (removable device may have changed) */ 421 if (bd_getgeom(od)) { 422 DEBUG("can't get geometry"); 423 error = ENXIO; 424 goto out; 425 } 426 427 /* 428 * Following calculations attempt to determine the correct value 429 * for d->od_boff by looking for the slice and partition specified, 430 * or searching for reasonable defaults. 431 */ 432 433 /* 434 * Find the slice in the DOS slice table. 435 */ 436#ifdef PC98 437 if (od->od_flags & BD_FLOPPY) { 438 sector = 0; 439 goto unsliced; 440 } 441#endif 442 if (bd_read(od, 0, 1, buf)) { 443 DEBUG("error reading MBR"); 444 error = EIO; 445 goto out; 446 } 447 448 /* 449 * Check the slice table magic. 450 */ 451 if ((buf[0x1fe] != 0x55) || (buf[0x1ff] != 0xaa)) { 452 /* If a slice number was explicitly supplied, this is an error */ 453 if (dev->d_kind.biosdisk.slice > 0) { 454 DEBUG("no slice table/MBR (no magic)"); 455 error = ENOENT; 456 goto out; 457 } 458 sector = 0; 459 goto unsliced; /* may be a floppy */ 460 } 461#ifdef PC98 462 if (bd_read(od, 1, 1, buf)) { 463 DEBUG("error reading MBR"); 464 error = EIO; 465 goto out; 466 } 467#endif 468 bcopy(buf + DOSPARTOFF, &od->od_parttab, sizeof(struct dos_partition) * NDOSPART); 469 dptr = &od->od_parttab[0]; 470 od->od_flags |= BD_PARTTABOK; 471 472 /* Is this a request for the whole disk? */ 473 if (dev->d_kind.biosdisk.slice == -1) { 474 sector = 0; 475 goto unsliced; 476 } 477 478 /* Try to auto-detect the best slice; this should always give a slice number */ 479 if (dev->d_kind.biosdisk.slice == 0) 480 dev->d_kind.biosdisk.slice = bd_bestslice(dptr); 481 482 switch (dev->d_kind.biosdisk.slice) { 483 case -1: 484 error = ENOENT; 485 goto out; 486 case 0: 487 sector = 0; 488 goto unsliced; 489 default: 490 break; 491 } 492 493 /* 494 * Accept the supplied slice number unequivocally (we may be looking 495 * at a DOS partition). 496 */ 497 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */ 498#ifdef PC98 499 sector = dptr->dp_scyl * od->od_hds * od->od_sec + dptr->dp_shd * od->od_sec + dptr->dp_ssect; 500 { 501 int end = dptr->dp_ecyl * od->od_hds * od->od_sec + dptr->dp_ehd * od->od_sec + dptr->dp_esect; 502 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, end-sector); 503 } 504#else 505 sector = dptr->dp_start; 506 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size); 507#endif 508 509 /* 510 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition 511 */ 512#ifdef PC98 513 if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 514#else 515 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 516#endif 517 dev->d_kind.biosdisk.partition = 0; 518 519 unsliced: 520 /* 521 * Now we have the slice offset, look for the partition in the disklabel if we have 522 * a partition to start with. 523 * 524 * XXX we might want to check the label checksum. 525 */ 526 if (dev->d_kind.biosdisk.partition < 0) { 527 od->od_boff = sector; /* no partition, must be after the slice */ 528 DEBUG("opening raw slice"); 529 } else { 530 if (bd_read(od, sector + LABELSECTOR, 1, buf)) { 531 DEBUG("error reading disklabel"); 532 error = EIO; 533 goto out; 534 } 535 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel); 536 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel)); 537 lp = &od->od_disklabel; 538 od->od_flags |= BD_LABELOK; 539 540 if (lp->d_magic != DISKMAGIC) { 541 DEBUG("no disklabel"); 542 error = ENOENT; 543 goto out; 544 } 545 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) { 546 DEBUG("partition '%c' exceeds partitions in table (a-'%c')", 547 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions); 548 error = EPART; 549 goto out; 550 551 } 552 553 /* Complain if the partition type is wrong */ 554 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) && 555 !(od->od_flags & BD_FLOPPY)) /* Floppies often have bogus fstype */ 556 DEBUG("warning, partition marked as unused"); 557 558 od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset; 559 } 560 561 out: 562 if (error) { 563 free(od); 564 } else { 565 *odp = od; /* return the open disk */ 566 } 567 return(error); 568} 569 570 571/* 572 * Search for a slice with the following preferences: 573 * 574 * 1: Active FreeBSD slice 575 * 2: Non-active FreeBSD slice 576 * 3: Active FAT/FAT32 slice 577 * 4: non-active FAT/FAT32 slice 578 */ 579#define PREF_FBSD_ACT 0 580#define PREF_FBSD 1 581#define PREF_DOS_ACT 2 582#define PREF_DOS 3 583#define PREF_NONE 4 584 585static int 586bd_bestslice(struct dos_partition *dptr) 587{ 588 int i; 589 int preflevel, pref; 590 591 592#ifndef PC98 593 /* 594 * Check for the historically bogus MBR found on true dedicated disks 595 */ 596 if ((dptr[3].dp_typ == DOSPTYP_386BSD) && 597 (dptr[3].dp_start == 0) && 598 (dptr[3].dp_size == 50000)) 599 return(0); 600#endif 601 602 preflevel = PREF_NONE; 603 pref = -1; 604 605 /* 606 * XXX No support here for 'extended' slices 607 */ 608 for (i = 0; i < NDOSPART; i++) { 609#ifdef PC98 610 switch(dptr[i].dp_mid & 0x7f) { 611 case DOSMID_386BSD & 0x7f: /* FreeBSD */ 612 if ((dptr[i].dp_mid & 0x80) && (preflevel > PREF_FBSD_ACT)) { 613 pref = i; 614 preflevel = PREF_FBSD_ACT; 615 } else if (preflevel > PREF_FBSD) { 616 pref = i; 617 preflevel = PREF_FBSD; 618 } 619 break; 620 621 case 0x11: /* DOS/Windows */ 622 case 0x20: 623 case 0x21: 624 case 0x22: 625 case 0x23: 626 case 0x63: 627 if ((dptr[i].dp_mid & 0x80) && (preflevel > PREF_DOS_ACT)) { 628 pref = i; 629 preflevel = PREF_DOS_ACT; 630 } else if (preflevel > PREF_DOS) { 631 pref = i; 632 preflevel = PREF_DOS; 633 } 634 break; 635 } 636#else 637 switch(dptr[i].dp_typ) { 638 case DOSPTYP_386BSD: /* FreeBSD */ 639 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_FBSD_ACT)) { 640 pref = i; 641 preflevel = PREF_FBSD_ACT; 642 } else if (preflevel > PREF_FBSD) { 643 pref = i; 644 preflevel = PREF_FBSD; 645 } 646 break; 647 648 case 0x04: /* DOS/Windows */ 649 case 0x06: 650 case 0x0b: 651 case 0x0c: 652 case 0x0e: 653 case 0x63: 654 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_DOS_ACT)) { 655 pref = i; 656 preflevel = PREF_DOS_ACT; 657 } else if (preflevel > PREF_DOS) { 658 pref = i; 659 preflevel = PREF_DOS; 660 } 661 break; 662 } 663#endif 664 } 665 return(pref + 1); /* slices numbered 1-4 */ 666} 667 668 669static int 670bd_close(struct open_file *f) 671{ 672 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data); 673 674 bd_closedisk(od); 675 return(0); 676} 677 678static void 679bd_closedisk(struct open_disk *od) 680{ 681 DEBUG("open_disk %p", od); 682#if 0 683 /* XXX is this required? (especially if disk already open...) */ 684 if (od->od_flags & BD_FLOPPY) 685 delay(3000000); 686#endif 687 free(od); 688} 689 690static int 691bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize) 692{ 693 struct bcache_devdata bcd; 694 695 bcd.dv_strategy = bd_realstrategy; 696 bcd.dv_devdata = devdata; 697 return(bcache_strategy(&bcd, rw, dblk, size, buf, rsize)); 698} 699 700static int 701bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize) 702{ 703 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 704 int blks; 705#ifdef BD_SUPPORT_FRAGS 706 char fragbuf[BIOSDISK_SECSIZE]; 707 size_t fragsize; 708 709 fragsize = size % BIOSDISK_SECSIZE; 710#else 711 if (size % BIOSDISK_SECSIZE) 712 panic("bd_strategy: %d bytes I/O not multiple of block size", size); 713#endif 714 715 DEBUG("open_disk %p", od); 716 717 if (rw != F_READ) 718 return(EROFS); 719 720 721 blks = size / BIOSDISK_SECSIZE; 722 DEBUG("read %d from %d+%d to %p", blks, od->od_boff, dblk, buf); 723 724 if (rsize) 725 *rsize = 0; 726 if (blks && bd_read(od, dblk + od->od_boff, blks, buf)) { 727 DEBUG("read error"); 728 return (EIO); 729 } 730#ifdef BD_SUPPORT_FRAGS 731 DEBUG("bd_strategy: frag read %d from %d+%d+d to %p", 732 fragsize, od->od_boff, dblk, blks, buf + (blks * BIOSDISK_SECSIZE)); 733 if (fragsize && bd_read(od, dblk + od->od_boff + blks, 1, fragsize)) { 734 DEBUG("frag read error"); 735 return(EIO); 736 } 737 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize); 738#endif 739 if (rsize) 740 *rsize = size; 741 return (0); 742} 743 744/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */ 745#define FLOPPY_BOUNCEBUF 18 746 747static int 748bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 749{ 750 int x, bpc, cyl, hd, sec, result, resid, cnt, retry, maxfer; 751 caddr_t p, xp, bbuf, breg; 752 753 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */ 754 resid = blks; 755 p = dest; 756 757 /* Decide whether we have to bounce */ 758#ifdef PC98 759 if (((od->od_unit & 0xf0) == 0x90 || (od->od_unit & 0xf0) == 0x30) && 760#else 761 if ((od->od_unit < 0x80) && 762#endif 763 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) { 764 765 /* 766 * There is a 64k physical boundary somewhere in the destination buffer, so we have 767 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we 768 * need to. Use the bottom half unless there is a break there, in which case we 769 * use the top half. 770 */ 771 x = min(FLOPPY_BOUNCEBUF, blks); 772 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE); 773 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) { 774 breg = bbuf; 775 } else { 776 breg = bbuf + x * BIOSDISK_SECSIZE; 777 } 778 maxfer = x; /* limit transfers to bounce region size */ 779 } else { 780 bbuf = NULL; 781 maxfer = 0; 782 } 783 784 while (resid > 0) { 785 x = dblk; 786 cyl = x / bpc; /* block # / blocks per cylinder */ 787 x %= bpc; /* block offset into cylinder */ 788 hd = x / od->od_sec; /* offset / blocks per track */ 789 sec = x % od->od_sec; /* offset into track */ 790 791 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */ 792 x = min(od->od_sec - sec, resid); 793 if (maxfer > 0) 794 x = min(x, maxfer); /* fit bounce buffer */ 795 796 /* where do we transfer to? */ 797 xp = bbuf == NULL ? p : breg; 798 799 /* correct sector number for 1-based BIOS numbering */ 800#ifdef PC98 801 if ((od->od_unit & 0xf0) == 0x30 || (od->od_unit & 0xf0) == 0x90) 802 sec++; 803#else 804 sec++; 805#endif 806 807 /* Loop retrying the operation a couple of times. The BIOS may also retry. */ 808 for (retry = 0; retry < 3; retry++) { 809 /* if retrying, reset the drive */ 810 if (retry > 0) { 811#ifdef PC98 812#else 813 v86.ctl = V86_FLAGS; 814 v86.addr = 0x13; 815 v86.eax = 0; 816 v86.edx = od->od_unit; 817 v86int(); 818#endif 819 } 820 821 /* build request XXX support EDD requests too */ 822#ifdef PC98 823 v86.ctl = 0; 824 v86.addr = 0x1b; 825 if (od->od_flags & BD_FLOPPY) { 826 v86.eax = 0xd600 | od->od_unit; 827 v86.ecx = 0x0200 | (cyl & 0xff); 828 } 829 else { 830 v86.eax = 0x0600 | od->od_unit; 831 v86.ecx = cyl; 832 } 833 v86.edx = (hd << 8) | sec; 834 v86.ebx = x * BIOSDISK_SECSIZE; 835 v86.es = VTOPSEG(xp); 836 v86.ebp = VTOPOFF(xp); 837 v86int(); 838#else 839 v86.ctl = V86_FLAGS; 840 v86.addr = 0x13; 841 v86.eax = 0x200 | x; 842 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec; 843 v86.edx = (hd << 8) | od->od_unit; 844 v86.es = VTOPSEG(xp); 845 v86.ebx = VTOPOFF(xp); 846 v86int(); 847#endif 848 result = (v86.efl & 0x1); 849 if (result == 0) 850 break; 851 } 852 853#ifdef PC98 854 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"); 855 /* BUG here, cannot use v86 in printf because putchar uses it too */ 856 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 857 od->od_flags & BD_FLOPPY ? 0xd600 | od->od_unit : 0x0600 | od->od_unit, 858 od->od_flags & BD_FLOPPY ? 0x0200 | cyl : cyl, (hd << 8) | sec, 859 (v86.eax >> 8) & 0xff); 860#else 861 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok"); 862 /* BUG here, cannot use v86 in printf because putchar uses it too */ 863 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 864 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff); 865#endif 866 if (result) { 867 if (bbuf != NULL) 868 free(bbuf); 869 return(-1); 870 } 871 if (bbuf != NULL) 872 bcopy(breg, p, x * BIOSDISK_SECSIZE); 873 p += (x * BIOSDISK_SECSIZE); 874 dblk += x; 875 resid -= x; 876 } 877 878/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */ 879 if (bbuf != NULL) 880 free(bbuf); 881 return(0); 882} 883 884static int 885bd_getgeom(struct open_disk *od) 886{ 887 888#ifdef PC98 889 if (od->od_flags & BD_FLOPPY) { 890 od->od_cyl = 79; 891 od->od_hds = 2; 892 od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15; 893 } 894 else { 895 v86.ctl = 0; 896 v86.addr = 0x1b; 897 v86.eax = 0x8400 | od->od_unit; 898 v86int(); 899 900 od->od_cyl = v86.ecx; 901 od->od_hds = (v86.edx >> 8) & 0xff; 902 od->od_sec = v86.edx & 0xff; 903 } 904#else 905 v86.ctl = V86_FLAGS; 906 v86.addr = 0x13; 907 v86.eax = 0x800; 908 v86.edx = od->od_unit; 909 v86int(); 910 911 if ((v86.efl & 0x1) || /* carry set */ 912 ((v86.edx & 0xff) <= (od->od_unit & 0x7f))) /* unit # bad */ 913 return(1); 914 915 /* convert max cyl # -> # of cylinders */ 916 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1; 917 /* convert max head # -> # of heads */ 918 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1; 919 od->od_sec = v86.ecx & 0x3f; 920#endif 921 922 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec); 923 return(0); 924} 925 926/* 927 * Return a suitable dev_t value for (dev). 928 * 929 * In the case where it looks like (dev) is a SCSI disk, we allow the number of 930 * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 931 */ 932int 933bd_getdev(struct i386_devdesc *dev) 934{ 935 struct open_disk *od; 936 int biosdev; 937 int major; 938 int rootdev; 939 char *nip, *cp; 940 int unitofs = 0, i, unit; 941 942 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit); 943 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev); 944 if (biosdev == -1) /* not a BIOS device */ 945 return(-1); 946 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */ 947 return(-1); 948 949#ifdef PC98 950 if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) { 951#else 952 if (biosdev < 0x80) { 953#endif 954 /* floppy (or emulated floppy) or ATAPI device */ 955 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) { 956 /* is an ATAPI disk */ 957 major = WFDMAJOR; 958 } else { 959 /* is a floppy disk */ 960 major = FDMAJOR; 961 } 962 } else { 963 /* harddisk */ 964 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) { 965 /* label OK, disk labelled as SCSI */ 966 major = DAMAJOR; 967 /* check for unit number correction hint, now deprecated */ 968 if ((nip = getenv("num_ide_disks")) != NULL) { 969 i = strtol(nip, &cp, 0); 970 /* check for parse error */ 971 if ((cp != nip) && (*cp == 0)) 972 unitofs = i; 973 } 974 } else { 975 /* assume an IDE disk */ 976 major = WDMAJOR; 977 } 978 } 979 /* XXX a better kludge to set the root disk unit number */ 980 if ((nip = getenv("root_disk_unit")) != NULL) { 981 i = strtol(nip, &cp, 0); 982 /* check for parse error */ 983 if ((cp != nip) && (*cp == 0)) 984 unit = i; 985 } else { 986#ifdef PC98 987 unit = biosdev & 0xf; /* allow for #wd compenstation in da case */ 988#else 989 unit = (biosdev & 0x7f) - unitofs; /* allow for #wd compenstation in da case */ 990#endif 991 } 992 993 rootdev = MAKEBOOTDEV(major, 994 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */ 995 (dev->d_kind.biosdisk.slice + 1) & 0xf, 996 unit, 997 dev->d_kind.biosdisk.partition); 998 DEBUG("dev is 0x%x\n", rootdev); 999 return(rootdev); 1000} 1001 1002/* 1003 * Fix (dev) so that it refers to the 'real' disk/slice/partition that it implies. 1004 */ 1005int 1006bd_fixupdev(struct i386_devdesc *dev) 1007{ 1008 struct open_disk *od; 1009 1010 /* 1011 * Open the disk. This will fix up the slice and partition fields. 1012 */ 1013 if (bd_opendisk(&od, dev) != 0) 1014 return(ENOENT); 1015 1016 bd_closedisk(od); 1017}
| 182 183 if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY){ 184 bdinfo[nbdinfo].bd_drive = 'A' + (unit & 0xf); 185 if (*(u_char *)PTOV(0x5AE) & (1<<(unit & 0xf))) 186 /* available 1.44MB access */ 187 bdinfo[nbdinfo].bd_unit = 0x30 + (unit & 0xf); 188 } 189 else 190 bdinfo[nbdinfo].bd_drive = 'C' + hd_drive++; 191 /* XXX we need "disk aliases" to make this simpler */ 192 printf("BIOS drive %c: is disk%d\n", 193 bdinfo[nbdinfo].bd_drive, nbdinfo); 194 nbdinfo++; 195 } 196 } 197#else 198 /* sequence 0, 0x80 */ 199 for (base = 0; base <= 0x80; base += 0x80) { 200 for (unit = base; (nbdinfo < MAXBDDEV); unit++) { 201 bdinfo[nbdinfo].bd_unit = unit; 202 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0; 203 204 /* XXX add EDD probes */ 205 if (!bd_int13probe(&bdinfo[nbdinfo])) 206 break; 207 208 /* XXX we need "disk aliases" to make this simpler */ 209 printf("BIOS drive %c: is disk%d\n", 210 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo); 211 nbdinfo++; 212 } 213 } 214#endif 215 return(0); 216} 217 218/* 219 * Try to detect a device supported by the legacy int13 BIOS 220 */ 221 222static int 223bd_int13probe(struct bdinfo *bd) 224{ 225#ifdef PC98 226 int addr; 227 if (bd->bd_flags & BD_FLOPPY){ 228 addr = 0xa155c; 229 } 230 else { 231 if ((bd->bd_unit & 0xf0) == 0x80) 232 addr = 0xa155d; 233 else 234 addr = 0xa1482; 235 } 236 if ( *(u_char *)PTOV(addr) & (1<<(bd->bd_unit & 0x0f))) { 237 bd->bd_flags |= BD_MODEINT13; 238 return(1); 239 } 240 return(0); 241#else 242 v86.ctl = V86_FLAGS; 243 v86.addr = 0x13; 244 v86.eax = 0x800; 245 v86.edx = bd->bd_unit; 246 v86int(); 247 248 if (!(v86.efl & 0x1) && /* carry clear */ 249 ((v86.edx & 0xff) > (bd->bd_unit & 0x7f))) { /* unit # OK */ 250 bd->bd_flags |= BD_MODEINT13; 251 bd->bd_type = v86.ebx & 0xff; 252 return(1); 253 } 254#endif 255 return(0); 256} 257 258/* 259 * Print information about disks 260 */ 261static void 262bd_print(int verbose) 263{ 264 int i, j; 265 char line[80]; 266 struct i386_devdesc dev; 267 struct open_disk *od; 268 struct dos_partition *dptr; 269 270 for (i = 0; i < nbdinfo; i++) { 271#ifdef PC98 272 sprintf(line, " disk%d: BIOS drive %c:\n", i, 273 bdinfo[i].bd_drive); 274#else 275 sprintf(line, " disk%d: BIOS drive %c:\n", i, 276 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80)); 277#endif 278 pager_output(line); 279 280 /* try to open the whole disk */ 281 dev.d_kind.biosdisk.unit = i; 282 dev.d_kind.biosdisk.slice = -1; 283 dev.d_kind.biosdisk.partition = -1; 284 285 if (!bd_opendisk(&od, &dev)) { 286 287 /* Do we have a partition table? */ 288 if (od->od_flags & BD_PARTTABOK) { 289 dptr = &od->od_parttab[0]; 290 291 /* Check for a "truly dedicated" disk */ 292#ifdef PC98 293 for (j = 0; j < NDOSPART; j++) { 294 switch(dptr[j].dp_mid) { 295 case DOSMID_386BSD: 296 sprintf(line, " disk%ds%d", i, j + 1); 297 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); 298 break; 299 default: 300 } 301 } 302#else 303 if ((dptr[3].dp_typ == DOSPTYP_386BSD) && 304 (dptr[3].dp_start == 0) && 305 (dptr[3].dp_size == 50000)) { 306 sprintf(line, " disk%d", i); 307 bd_printslice(od, 0, line); 308 } else { 309 for (j = 0; j < NDOSPART; j++) { 310 switch(dptr[j].dp_typ) { 311 case DOSPTYP_386BSD: 312 sprintf(line, " disk%ds%d", i, j + 1); 313 bd_printslice(od, dptr[j].dp_start, line); 314 break; 315 default: 316 } 317 } 318 319 } 320#endif 321 } 322 bd_closedisk(od); 323 } 324 } 325} 326 327static void 328bd_printslice(struct open_disk *od, int offset, char *prefix) 329{ 330 char line[80]; 331 u_char buf[BIOSDISK_SECSIZE]; 332 struct disklabel *lp; 333 int i; 334 335 /* read disklabel */ 336 if (bd_read(od, offset + LABELSECTOR, 1, buf)) 337 return; 338 lp =(struct disklabel *)(&buf[0]); 339 if (lp->d_magic != DISKMAGIC) { 340 sprintf(line, "bad disklabel\n"); 341 pager_output(line); 342 return; 343 } 344 345 /* Print partitions */ 346 for (i = 0; i < lp->d_npartitions; i++) { 347 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) || (lp->d_partitions[i].p_fstype == FS_SWAP) || 348 ((lp->d_partitions[i].p_fstype == FS_UNUSED) && 349 (od->od_flags & BD_FLOPPY) && (i == 0))) { /* Floppies often have bogus fstype, print 'a' */ 350 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i, 351 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : "FFS", 352 lp->d_partitions[i].p_size / 2048, /* 512-byte sector assumption */ 353 lp->d_partitions[i].p_offset, lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size); 354 pager_output(line); 355 } 356 } 357} 358 359 360/* 361 * Attempt to open the disk described by (dev) for use by (f). 362 * 363 * Note that the philosophy here is "give them exactly what 364 * they ask for". This is necessary because being too "smart" 365 * about what the user might want leads to complications. 366 * (eg. given no slice or partition value, with a disk that is 367 * sliced - are they after the first BSD slice, or the DOS 368 * slice before it?) 369 */ 370static int 371bd_open(struct open_file *f, ...) 372{ 373 va_list ap; 374 struct i386_devdesc *dev; 375 struct open_disk *od; 376 int error; 377 378 va_start(ap, f); 379 dev = va_arg(ap, struct i386_devdesc *); 380 va_end(ap); 381 if ((error = bd_opendisk(&od, dev))) 382 return(error); 383 384 /* 385 * Save our context 386 */ 387 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od; 388 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff); 389 return(0); 390} 391 392static int 393bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev) 394{ 395 struct dos_partition *dptr; 396 struct disklabel *lp; 397 struct open_disk *od; 398 int sector, slice, i; 399 int error; 400 u_char buf[BUFSIZE]; 401 daddr_t pref_slice[4]; 402 403 if (dev->d_kind.biosdisk.unit >= nbdinfo) { 404 DEBUG("attempt to open nonexistent disk"); 405 return(ENXIO); 406 } 407 408 od = (struct open_disk *)malloc(sizeof(struct open_disk)); 409 if (!od) { 410 DEBUG("no memory"); 411 return (ENOMEM); 412 } 413 414 /* Look up BIOS unit number, intialise open_disk structure */ 415 od->od_dkunit = dev->d_kind.biosdisk.unit; 416 od->od_unit = bdinfo[od->od_dkunit].bd_unit; 417 od->od_flags = bdinfo[od->od_dkunit].bd_flags; 418 od->od_boff = 0; 419 error = 0; 420 DEBUG("open '%s', unit 0x%x slice %d partition %c", 421 i386_fmtdev(dev), dev->d_kind.biosdisk.unit, 422 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a'); 423 424 /* Get geometry for this open (removable device may have changed) */ 425 if (bd_getgeom(od)) { 426 DEBUG("can't get geometry"); 427 error = ENXIO; 428 goto out; 429 } 430 431 /* 432 * Following calculations attempt to determine the correct value 433 * for d->od_boff by looking for the slice and partition specified, 434 * or searching for reasonable defaults. 435 */ 436 437 /* 438 * Find the slice in the DOS slice table. 439 */ 440#ifdef PC98 441 if (od->od_flags & BD_FLOPPY) { 442 sector = 0; 443 goto unsliced; 444 } 445#endif 446 if (bd_read(od, 0, 1, buf)) { 447 DEBUG("error reading MBR"); 448 error = EIO; 449 goto out; 450 } 451 452 /* 453 * Check the slice table magic. 454 */ 455 if ((buf[0x1fe] != 0x55) || (buf[0x1ff] != 0xaa)) { 456 /* If a slice number was explicitly supplied, this is an error */ 457 if (dev->d_kind.biosdisk.slice > 0) { 458 DEBUG("no slice table/MBR (no magic)"); 459 error = ENOENT; 460 goto out; 461 } 462 sector = 0; 463 goto unsliced; /* may be a floppy */ 464 } 465#ifdef PC98 466 if (bd_read(od, 1, 1, buf)) { 467 DEBUG("error reading MBR"); 468 error = EIO; 469 goto out; 470 } 471#endif 472 bcopy(buf + DOSPARTOFF, &od->od_parttab, sizeof(struct dos_partition) * NDOSPART); 473 dptr = &od->od_parttab[0]; 474 od->od_flags |= BD_PARTTABOK; 475 476 /* Is this a request for the whole disk? */ 477 if (dev->d_kind.biosdisk.slice == -1) { 478 sector = 0; 479 goto unsliced; 480 } 481 482 /* Try to auto-detect the best slice; this should always give a slice number */ 483 if (dev->d_kind.biosdisk.slice == 0) 484 dev->d_kind.biosdisk.slice = bd_bestslice(dptr); 485 486 switch (dev->d_kind.biosdisk.slice) { 487 case -1: 488 error = ENOENT; 489 goto out; 490 case 0: 491 sector = 0; 492 goto unsliced; 493 default: 494 break; 495 } 496 497 /* 498 * Accept the supplied slice number unequivocally (we may be looking 499 * at a DOS partition). 500 */ 501 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */ 502#ifdef PC98 503 sector = dptr->dp_scyl * od->od_hds * od->od_sec + dptr->dp_shd * od->od_sec + dptr->dp_ssect; 504 { 505 int end = dptr->dp_ecyl * od->od_hds * od->od_sec + dptr->dp_ehd * od->od_sec + dptr->dp_esect; 506 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, end-sector); 507 } 508#else 509 sector = dptr->dp_start; 510 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size); 511#endif 512 513 /* 514 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition 515 */ 516#ifdef PC98 517 if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 518#else 519 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 520#endif 521 dev->d_kind.biosdisk.partition = 0; 522 523 unsliced: 524 /* 525 * Now we have the slice offset, look for the partition in the disklabel if we have 526 * a partition to start with. 527 * 528 * XXX we might want to check the label checksum. 529 */ 530 if (dev->d_kind.biosdisk.partition < 0) { 531 od->od_boff = sector; /* no partition, must be after the slice */ 532 DEBUG("opening raw slice"); 533 } else { 534 if (bd_read(od, sector + LABELSECTOR, 1, buf)) { 535 DEBUG("error reading disklabel"); 536 error = EIO; 537 goto out; 538 } 539 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel); 540 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel)); 541 lp = &od->od_disklabel; 542 od->od_flags |= BD_LABELOK; 543 544 if (lp->d_magic != DISKMAGIC) { 545 DEBUG("no disklabel"); 546 error = ENOENT; 547 goto out; 548 } 549 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) { 550 DEBUG("partition '%c' exceeds partitions in table (a-'%c')", 551 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions); 552 error = EPART; 553 goto out; 554 555 } 556 557 /* Complain if the partition type is wrong */ 558 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) && 559 !(od->od_flags & BD_FLOPPY)) /* Floppies often have bogus fstype */ 560 DEBUG("warning, partition marked as unused"); 561 562 od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset; 563 } 564 565 out: 566 if (error) { 567 free(od); 568 } else { 569 *odp = od; /* return the open disk */ 570 } 571 return(error); 572} 573 574 575/* 576 * Search for a slice with the following preferences: 577 * 578 * 1: Active FreeBSD slice 579 * 2: Non-active FreeBSD slice 580 * 3: Active FAT/FAT32 slice 581 * 4: non-active FAT/FAT32 slice 582 */ 583#define PREF_FBSD_ACT 0 584#define PREF_FBSD 1 585#define PREF_DOS_ACT 2 586#define PREF_DOS 3 587#define PREF_NONE 4 588 589static int 590bd_bestslice(struct dos_partition *dptr) 591{ 592 int i; 593 int preflevel, pref; 594 595 596#ifndef PC98 597 /* 598 * Check for the historically bogus MBR found on true dedicated disks 599 */ 600 if ((dptr[3].dp_typ == DOSPTYP_386BSD) && 601 (dptr[3].dp_start == 0) && 602 (dptr[3].dp_size == 50000)) 603 return(0); 604#endif 605 606 preflevel = PREF_NONE; 607 pref = -1; 608 609 /* 610 * XXX No support here for 'extended' slices 611 */ 612 for (i = 0; i < NDOSPART; i++) { 613#ifdef PC98 614 switch(dptr[i].dp_mid & 0x7f) { 615 case DOSMID_386BSD & 0x7f: /* FreeBSD */ 616 if ((dptr[i].dp_mid & 0x80) && (preflevel > PREF_FBSD_ACT)) { 617 pref = i; 618 preflevel = PREF_FBSD_ACT; 619 } else if (preflevel > PREF_FBSD) { 620 pref = i; 621 preflevel = PREF_FBSD; 622 } 623 break; 624 625 case 0x11: /* DOS/Windows */ 626 case 0x20: 627 case 0x21: 628 case 0x22: 629 case 0x23: 630 case 0x63: 631 if ((dptr[i].dp_mid & 0x80) && (preflevel > PREF_DOS_ACT)) { 632 pref = i; 633 preflevel = PREF_DOS_ACT; 634 } else if (preflevel > PREF_DOS) { 635 pref = i; 636 preflevel = PREF_DOS; 637 } 638 break; 639 } 640#else 641 switch(dptr[i].dp_typ) { 642 case DOSPTYP_386BSD: /* FreeBSD */ 643 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_FBSD_ACT)) { 644 pref = i; 645 preflevel = PREF_FBSD_ACT; 646 } else if (preflevel > PREF_FBSD) { 647 pref = i; 648 preflevel = PREF_FBSD; 649 } 650 break; 651 652 case 0x04: /* DOS/Windows */ 653 case 0x06: 654 case 0x0b: 655 case 0x0c: 656 case 0x0e: 657 case 0x63: 658 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_DOS_ACT)) { 659 pref = i; 660 preflevel = PREF_DOS_ACT; 661 } else if (preflevel > PREF_DOS) { 662 pref = i; 663 preflevel = PREF_DOS; 664 } 665 break; 666 } 667#endif 668 } 669 return(pref + 1); /* slices numbered 1-4 */ 670} 671 672 673static int 674bd_close(struct open_file *f) 675{ 676 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data); 677 678 bd_closedisk(od); 679 return(0); 680} 681 682static void 683bd_closedisk(struct open_disk *od) 684{ 685 DEBUG("open_disk %p", od); 686#if 0 687 /* XXX is this required? (especially if disk already open...) */ 688 if (od->od_flags & BD_FLOPPY) 689 delay(3000000); 690#endif 691 free(od); 692} 693 694static int 695bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize) 696{ 697 struct bcache_devdata bcd; 698 699 bcd.dv_strategy = bd_realstrategy; 700 bcd.dv_devdata = devdata; 701 return(bcache_strategy(&bcd, rw, dblk, size, buf, rsize)); 702} 703 704static int 705bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize) 706{ 707 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 708 int blks; 709#ifdef BD_SUPPORT_FRAGS 710 char fragbuf[BIOSDISK_SECSIZE]; 711 size_t fragsize; 712 713 fragsize = size % BIOSDISK_SECSIZE; 714#else 715 if (size % BIOSDISK_SECSIZE) 716 panic("bd_strategy: %d bytes I/O not multiple of block size", size); 717#endif 718 719 DEBUG("open_disk %p", od); 720 721 if (rw != F_READ) 722 return(EROFS); 723 724 725 blks = size / BIOSDISK_SECSIZE; 726 DEBUG("read %d from %d+%d to %p", blks, od->od_boff, dblk, buf); 727 728 if (rsize) 729 *rsize = 0; 730 if (blks && bd_read(od, dblk + od->od_boff, blks, buf)) { 731 DEBUG("read error"); 732 return (EIO); 733 } 734#ifdef BD_SUPPORT_FRAGS 735 DEBUG("bd_strategy: frag read %d from %d+%d+d to %p", 736 fragsize, od->od_boff, dblk, blks, buf + (blks * BIOSDISK_SECSIZE)); 737 if (fragsize && bd_read(od, dblk + od->od_boff + blks, 1, fragsize)) { 738 DEBUG("frag read error"); 739 return(EIO); 740 } 741 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize); 742#endif 743 if (rsize) 744 *rsize = size; 745 return (0); 746} 747 748/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */ 749#define FLOPPY_BOUNCEBUF 18 750 751static int 752bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 753{ 754 int x, bpc, cyl, hd, sec, result, resid, cnt, retry, maxfer; 755 caddr_t p, xp, bbuf, breg; 756 757 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */ 758 resid = blks; 759 p = dest; 760 761 /* Decide whether we have to bounce */ 762#ifdef PC98 763 if (((od->od_unit & 0xf0) == 0x90 || (od->od_unit & 0xf0) == 0x30) && 764#else 765 if ((od->od_unit < 0x80) && 766#endif 767 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) { 768 769 /* 770 * There is a 64k physical boundary somewhere in the destination buffer, so we have 771 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we 772 * need to. Use the bottom half unless there is a break there, in which case we 773 * use the top half. 774 */ 775 x = min(FLOPPY_BOUNCEBUF, blks); 776 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE); 777 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) { 778 breg = bbuf; 779 } else { 780 breg = bbuf + x * BIOSDISK_SECSIZE; 781 } 782 maxfer = x; /* limit transfers to bounce region size */ 783 } else { 784 bbuf = NULL; 785 maxfer = 0; 786 } 787 788 while (resid > 0) { 789 x = dblk; 790 cyl = x / bpc; /* block # / blocks per cylinder */ 791 x %= bpc; /* block offset into cylinder */ 792 hd = x / od->od_sec; /* offset / blocks per track */ 793 sec = x % od->od_sec; /* offset into track */ 794 795 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */ 796 x = min(od->od_sec - sec, resid); 797 if (maxfer > 0) 798 x = min(x, maxfer); /* fit bounce buffer */ 799 800 /* where do we transfer to? */ 801 xp = bbuf == NULL ? p : breg; 802 803 /* correct sector number for 1-based BIOS numbering */ 804#ifdef PC98 805 if ((od->od_unit & 0xf0) == 0x30 || (od->od_unit & 0xf0) == 0x90) 806 sec++; 807#else 808 sec++; 809#endif 810 811 /* Loop retrying the operation a couple of times. The BIOS may also retry. */ 812 for (retry = 0; retry < 3; retry++) { 813 /* if retrying, reset the drive */ 814 if (retry > 0) { 815#ifdef PC98 816#else 817 v86.ctl = V86_FLAGS; 818 v86.addr = 0x13; 819 v86.eax = 0; 820 v86.edx = od->od_unit; 821 v86int(); 822#endif 823 } 824 825 /* build request XXX support EDD requests too */ 826#ifdef PC98 827 v86.ctl = 0; 828 v86.addr = 0x1b; 829 if (od->od_flags & BD_FLOPPY) { 830 v86.eax = 0xd600 | od->od_unit; 831 v86.ecx = 0x0200 | (cyl & 0xff); 832 } 833 else { 834 v86.eax = 0x0600 | od->od_unit; 835 v86.ecx = cyl; 836 } 837 v86.edx = (hd << 8) | sec; 838 v86.ebx = x * BIOSDISK_SECSIZE; 839 v86.es = VTOPSEG(xp); 840 v86.ebp = VTOPOFF(xp); 841 v86int(); 842#else 843 v86.ctl = V86_FLAGS; 844 v86.addr = 0x13; 845 v86.eax = 0x200 | x; 846 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec; 847 v86.edx = (hd << 8) | od->od_unit; 848 v86.es = VTOPSEG(xp); 849 v86.ebx = VTOPOFF(xp); 850 v86int(); 851#endif 852 result = (v86.efl & 0x1); 853 if (result == 0) 854 break; 855 } 856 857#ifdef PC98 858 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"); 859 /* BUG here, cannot use v86 in printf because putchar uses it too */ 860 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 861 od->od_flags & BD_FLOPPY ? 0xd600 | od->od_unit : 0x0600 | od->od_unit, 862 od->od_flags & BD_FLOPPY ? 0x0200 | cyl : cyl, (hd << 8) | sec, 863 (v86.eax >> 8) & 0xff); 864#else 865 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok"); 866 /* BUG here, cannot use v86 in printf because putchar uses it too */ 867 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x", 868 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff); 869#endif 870 if (result) { 871 if (bbuf != NULL) 872 free(bbuf); 873 return(-1); 874 } 875 if (bbuf != NULL) 876 bcopy(breg, p, x * BIOSDISK_SECSIZE); 877 p += (x * BIOSDISK_SECSIZE); 878 dblk += x; 879 resid -= x; 880 } 881 882/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */ 883 if (bbuf != NULL) 884 free(bbuf); 885 return(0); 886} 887 888static int 889bd_getgeom(struct open_disk *od) 890{ 891 892#ifdef PC98 893 if (od->od_flags & BD_FLOPPY) { 894 od->od_cyl = 79; 895 od->od_hds = 2; 896 od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15; 897 } 898 else { 899 v86.ctl = 0; 900 v86.addr = 0x1b; 901 v86.eax = 0x8400 | od->od_unit; 902 v86int(); 903 904 od->od_cyl = v86.ecx; 905 od->od_hds = (v86.edx >> 8) & 0xff; 906 od->od_sec = v86.edx & 0xff; 907 } 908#else 909 v86.ctl = V86_FLAGS; 910 v86.addr = 0x13; 911 v86.eax = 0x800; 912 v86.edx = od->od_unit; 913 v86int(); 914 915 if ((v86.efl & 0x1) || /* carry set */ 916 ((v86.edx & 0xff) <= (od->od_unit & 0x7f))) /* unit # bad */ 917 return(1); 918 919 /* convert max cyl # -> # of cylinders */ 920 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1; 921 /* convert max head # -> # of heads */ 922 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1; 923 od->od_sec = v86.ecx & 0x3f; 924#endif 925 926 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec); 927 return(0); 928} 929 930/* 931 * Return a suitable dev_t value for (dev). 932 * 933 * In the case where it looks like (dev) is a SCSI disk, we allow the number of 934 * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 935 */ 936int 937bd_getdev(struct i386_devdesc *dev) 938{ 939 struct open_disk *od; 940 int biosdev; 941 int major; 942 int rootdev; 943 char *nip, *cp; 944 int unitofs = 0, i, unit; 945 946 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit); 947 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev); 948 if (biosdev == -1) /* not a BIOS device */ 949 return(-1); 950 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */ 951 return(-1); 952 953#ifdef PC98 954 if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) { 955#else 956 if (biosdev < 0x80) { 957#endif 958 /* floppy (or emulated floppy) or ATAPI device */ 959 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) { 960 /* is an ATAPI disk */ 961 major = WFDMAJOR; 962 } else { 963 /* is a floppy disk */ 964 major = FDMAJOR; 965 } 966 } else { 967 /* harddisk */ 968 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) { 969 /* label OK, disk labelled as SCSI */ 970 major = DAMAJOR; 971 /* check for unit number correction hint, now deprecated */ 972 if ((nip = getenv("num_ide_disks")) != NULL) { 973 i = strtol(nip, &cp, 0); 974 /* check for parse error */ 975 if ((cp != nip) && (*cp == 0)) 976 unitofs = i; 977 } 978 } else { 979 /* assume an IDE disk */ 980 major = WDMAJOR; 981 } 982 } 983 /* XXX a better kludge to set the root disk unit number */ 984 if ((nip = getenv("root_disk_unit")) != NULL) { 985 i = strtol(nip, &cp, 0); 986 /* check for parse error */ 987 if ((cp != nip) && (*cp == 0)) 988 unit = i; 989 } else { 990#ifdef PC98 991 unit = biosdev & 0xf; /* allow for #wd compenstation in da case */ 992#else 993 unit = (biosdev & 0x7f) - unitofs; /* allow for #wd compenstation in da case */ 994#endif 995 } 996 997 rootdev = MAKEBOOTDEV(major, 998 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */ 999 (dev->d_kind.biosdisk.slice + 1) & 0xf, 1000 unit, 1001 dev->d_kind.biosdisk.partition); 1002 DEBUG("dev is 0x%x\n", rootdev); 1003 return(rootdev); 1004} 1005 1006/* 1007 * Fix (dev) so that it refers to the 'real' disk/slice/partition that it implies. 1008 */ 1009int 1010bd_fixupdev(struct i386_devdesc *dev) 1011{ 1012 struct open_disk *od; 1013 1014 /* 1015 * Open the disk. This will fix up the slice and partition fields. 1016 */ 1017 if (bd_opendisk(&od, dev) != 0) 1018 return(ENOENT); 1019 1020 bd_closedisk(od); 1021}
|