biosdisk.c revision 333049
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 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/11/stand/pc98/libpc98/biosdisk.c 333049 2018-04-27 02:39:36Z nyan $"); 29 30/* 31 * BIOS disk device handling. 32 * 33 * Ideas and algorithms from: 34 * 35 * - NetBSD libi386/biosdisk.c 36 * - FreeBSD biosboot/disk.c 37 * 38 */ 39 40#include <stand.h> 41 42#include <sys/disklabel.h> 43#include <sys/diskpc98.h> 44#include <machine/bootinfo.h> 45 46#include <stdarg.h> 47 48#include <bootstrap.h> 49#include <btxv86.h> 50#include "libi386.h" 51 52#define BIOS_NUMDRIVES 0x475 53#define BIOSDISK_SECSIZE 512 54#define BUFSIZE (1 * BIOSDISK_SECSIZE) 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" , __func__ , ## 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_MODEINT13 0x0000 77#define BD_MODEEDD1 0x0001 78#define BD_MODEEDD3 0x0002 79#define BD_MODEMASK 0x0003 80#define BD_FLOPPY 0x0004 81#define BD_LABELOK 0x0008 82#define BD_PARTTABOK 0x0010 83#define BD_OPTICAL 0x0020 84 struct disklabel od_disklabel; 85 int od_nslices; /* slice count */ 86 struct pc98_partition od_slicetab[PC98_NPARTS]; 87}; 88 89/* 90 * List of BIOS devices, translation from disk unit number to 91 * BIOS unit number. 92 */ 93static struct bdinfo 94{ 95 int bd_unit; /* BIOS unit number */ 96 int bd_flags; 97 int bd_type; /* BIOS 'drive type' (floppy only) */ 98 int bd_da_unit; /* kernel unit number for da */ 99 int bd_open; /* reference counter */ 100 void *bd_bcache; /* buffer cache data */ 101} bdinfo [MAXBDDEV]; 102static int nbdinfo = 0; 103 104#define BD(dev) (bdinfo[(dev)->dd.d_unit]) 105 106static int bd_getgeom(struct open_disk *od); 107static int bd_read(struct open_disk *od, daddr_t dblk, int blks, 108 caddr_t dest); 109static int bd_write(struct open_disk *od, daddr_t dblk, int blks, 110 caddr_t dest); 111static int bd_int13probe(struct bdinfo *bd); 112 113static int bd_printslice(struct open_disk *od, struct pc98_partition *dp, 114 char *prefix, int verbose); 115static int bd_printbsdslice(struct open_disk *od, daddr_t offset, 116 char *prefix, int verbose); 117 118static int bd_init(void); 119static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, 120 char *buf, size_t *rsize); 121static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, 122 char *buf, size_t *rsize); 123static int bd_open(struct open_file *f, ...); 124static int bd_close(struct open_file *f); 125static int bd_print(int verbose); 126 127struct devsw biosdisk = { 128 "disk", 129 DEVT_DISK, 130 bd_init, 131 bd_strategy, 132 bd_open, 133 bd_close, 134 noioctl, 135 bd_print, 136 NULL 137}; 138 139static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev); 140static void bd_closedisk(struct open_disk *od); 141static int bd_open_pc98(struct open_disk *od, struct i386_devdesc *dev); 142static int bd_bestslice(struct open_disk *od); 143static void bd_checkextended(struct open_disk *od, int slicenum); 144 145/* 146 * Translate between BIOS device numbers and our private unit numbers. 147 */ 148int 149bd_bios2unit(int biosdev) 150{ 151 int i; 152 153 DEBUG("looking for bios device 0x%x", biosdev); 154 for (i = 0; i < nbdinfo; i++) { 155 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit); 156 if (bdinfo[i].bd_unit == biosdev) 157 return (i); 158 } 159 return (-1); 160} 161 162int 163bd_unit2bios(int unit) 164{ 165 166 if ((unit >= 0) && (unit < nbdinfo)) 167 return (bdinfo[unit].bd_unit); 168 return (-1); 169} 170 171/* 172 * Quiz the BIOS for disk devices, save a little info about them. 173 */ 174static int 175bd_init(void) 176{ 177 int base, unit; 178 int da_drive=0, n=-0x10; 179 180 /* sequence 0x90, 0x80, 0xa0 */ 181 for (base = 0x90; base <= 0xa0; base += n, n += 0x30) { 182 for (unit = base; (nbdinfo < MAXBDDEV) || ((unit & 0x0f) < 4); 183 unit++) { 184 bdinfo[nbdinfo].bd_open = 0; 185 bdinfo[nbdinfo].bd_bcache = NULL; 186 bdinfo[nbdinfo].bd_unit = unit; 187 bdinfo[nbdinfo].bd_flags = 188 (unit & 0xf0) == 0x90 ? BD_FLOPPY : 0; 189 if (!bd_int13probe(&bdinfo[nbdinfo])) { 190 if (((unit & 0xf0) == 0x90 && 191 (unit & 0x0f) < 4) || 192 ((unit & 0xf0) == 0xa0 && 193 (unit & 0x0f) < 6)) 194 /* Target IDs are not contiguous. */ 195 continue; 196 else 197 break; 198 } 199 200 if (bdinfo[nbdinfo].bd_flags & BD_FLOPPY) { 201 /* available 1.44MB access? */ 202 if (*(u_char *)PTOV(0xA15AE) & 203 (1<<(unit & 0xf))) { 204 /* boot media 1.2MB FD? */ 205 if ((*(u_char *)PTOV(0xA1584) & 206 0xf0) != 0x90) 207 bdinfo[nbdinfo].bd_unit = 208 0x30 + (unit & 0xf); 209 } 210 } else { 211 if ((unit & 0xF0) == 0xA0) /* SCSI HD or MO */ 212 bdinfo[nbdinfo].bd_da_unit = 213 da_drive++; 214 } 215 /* XXX we need "disk aliases" to make this simpler */ 216 printf("BIOS drive %c: is disk%d\n", 217 'A' + nbdinfo, nbdinfo); 218 nbdinfo++; 219 } 220 } 221 bcache_add_dev(nbdinfo); 222 return(0); 223} 224 225/* 226 * Try to detect a device supported by the legacy int13 BIOS 227 */ 228static int 229bd_int13probe(struct bdinfo *bd) 230{ 231 int addr; 232 233 if (bd->bd_flags & BD_FLOPPY) { 234 addr = 0xa155c; 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 if ((bd->bd_unit & 0xF0) == 0xA0) { 246 int media = 247 ((unsigned *)PTOV(0xA1460))[bd->bd_unit & 0x0F] & 0x1F; 248 249 if (media == 7) { /* MO */ 250 bd->bd_flags |= BD_MODEINT13 | BD_OPTICAL; 251 return(1); 252 } 253 } 254 return (0); 255} 256 257/* 258 * Print information about disks 259 */ 260static int 261bd_print(int verbose) 262{ 263 int i, j, ret = 0; 264 char line[80]; 265 struct i386_devdesc dev; 266 struct open_disk *od; 267 struct pc98_partition *dptr; 268 269 if (nbdinfo == 0) 270 return (0); 271 272 printf("%s devices:", biosdisk.dv_name); 273 if ((ret = pager_output("\n")) != 0) 274 return (ret); 275 276 for (i = 0; i < nbdinfo; i++) { 277 snprintf(line, sizeof(line), " disk%d: BIOS drive %c:\n", 278 i, 'A' + i); 279 if ((ret = pager_output(line)) != 0) 280 break; 281 282 /* try to open the whole disk */ 283 dev.dd.d_unit = i; 284 dev.d_kind.biosdisk.slice = -1; 285 dev.d_kind.biosdisk.partition = -1; 286 287 if (!bd_opendisk(&od, &dev)) { 288 289 /* Do we have a partition table? */ 290 if (od->od_flags & BD_PARTTABOK) { 291 dptr = &od->od_slicetab[0]; 292 293 /* Check for a "dedicated" disk */ 294 for (j = 0; j < od->od_nslices; j++) { 295 snprintf(line, sizeof(line), 296 " disk%ds%d", i, j + 1); 297 if ((ret = bd_printslice(od, &dptr[j], 298 line, verbose)) != 0) 299 break; 300 } 301 } 302 bd_closedisk(od); 303 if (ret != 0) 304 break; 305 } 306 } 307 return (ret); 308} 309 310/* Given a size in 512 byte sectors, convert it to a human-readable number. */ 311static char * 312display_size(uint64_t size) 313{ 314 static char buf[80]; 315 char unit; 316 317 size /= 2; 318 unit = 'K'; 319 if (size >= 10485760000LL) { 320 size /= 1073741824; 321 unit = 'T'; 322 } else if (size >= 10240000) { 323 size /= 1048576; 324 unit = 'G'; 325 } else if (size >= 10000) { 326 size /= 1024; 327 unit = 'M'; 328 } 329 sprintf(buf, "%6ld%cB", (long)size, unit); 330 return (buf); 331} 332 333/* 334 * Print information about slices on a disk. For the size calculations we 335 * assume a 512 byte sector. 336 */ 337static int 338bd_printslice(struct open_disk *od, struct pc98_partition *dp, char *prefix, 339 int verbose) 340{ 341 int cylsecs, start, size; 342 char stats[80]; 343 char line[80]; 344 345 cylsecs = od->od_hds * od->od_sec; 346 start = dp->dp_scyl * cylsecs + dp->dp_shd * od->od_sec + dp->dp_ssect; 347 size = (dp->dp_ecyl - dp->dp_scyl + 1) * cylsecs; 348 349 if (verbose) 350 sprintf(stats, " %s (%d - %d)", display_size(size), 351 start, start + size); 352 else 353 stats[0] = '\0'; 354 355 switch(dp->dp_mid & PC98_MID_MASK) { 356 case PC98_MID_386BSD: 357 return (bd_printbsdslice(od, start, prefix, verbose)); 358 case 0x00: /* unused partition */ 359 return (0); 360 case 0x01: 361 sprintf(line, "%s: FAT-12%s\n", prefix, stats); 362 break; 363 case 0x11: 364 case 0x20: 365 case 0x21: 366 case 0x22: 367 case 0x23: 368 case 0x24: 369 sprintf(line, "%s: FAT-16%s\n", prefix, stats); 370 break; 371 default: 372 sprintf(line, "%s: Unknown fs: 0x%x %s\n", prefix, dp->dp_mid, 373 stats); 374 } 375 return (pager_output(line)); 376} 377 378/* 379 * Print out each valid partition in the disklabel of a FreeBSD slice. 380 * For size calculations, we assume a 512 byte sector size. 381 */ 382static int 383bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix, 384 int verbose) 385{ 386 char line[80]; 387 char buf[BIOSDISK_SECSIZE]; 388 struct disklabel *lp; 389 int i; 390 391 /* read disklabel */ 392 if (bd_read(od, offset + LABELSECTOR, 1, buf)) 393 return (0); 394 lp =(struct disklabel *)(&buf[0]); 395 if (lp->d_magic != DISKMAGIC) { 396 sprintf(line, "%s: FFS bad disklabel\n", prefix); 397 return (pager_output(line)); 398 } 399 400 /* Print partitions */ 401 for (i = 0; i < lp->d_npartitions; i++) { 402 /* 403 * For each partition, make sure we know what type of fs it is. If 404 * not, then skip it. However, since floppies often have bogus 405 * fstypes, print the 'a' partition on a floppy even if it is marked 406 * unused. 407 */ 408 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) || 409 (lp->d_partitions[i].p_fstype == FS_SWAP) || 410 (lp->d_partitions[i].p_fstype == FS_VINUM) || 411 ((lp->d_partitions[i].p_fstype == FS_UNUSED) && 412 (od->od_flags & BD_FLOPPY) && (i == 0))) { 413 414 /* Only print out statistics in verbose mode */ 415 if (verbose) 416 sprintf(line, " %s%c: %s %s (%d - %d)\n", prefix, 'a' + i, 417 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap " : 418 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" : 419 "FFS ", 420 display_size(lp->d_partitions[i].p_size), 421 lp->d_partitions[i].p_offset, 422 lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size); 423 else 424 sprintf(line, " %s%c: %s\n", prefix, 'a' + i, 425 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : 426 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" : 427 "FFS"); 428 if (pager_output(line)) 429 return (1); 430 } 431 } 432 return (0); 433} 434 435 436/* 437 * Attempt to open the disk described by (dev) for use by (f). 438 * 439 * Note that the philosophy here is "give them exactly what 440 * they ask for". This is necessary because being too "smart" 441 * about what the user might want leads to complications. 442 * (eg. given no slice or partition value, with a disk that is 443 * sliced - are they after the first BSD slice, or the DOS 444 * slice before it?) 445 */ 446static int 447bd_open(struct open_file *f, ...) 448{ 449 va_list ap; 450 struct i386_devdesc *dev; 451 struct open_disk *od; 452 int error; 453 454 va_start(ap, f); 455 dev = va_arg(ap, struct i386_devdesc *); 456 va_end(ap); 457 if ((error = bd_opendisk(&od, dev))) 458 return(error); 459 460 BD(dev).bd_open++; 461 if (BD(dev).bd_bcache == NULL) 462 BD(dev).bd_bcache = bcache_allocate(); 463 464 /* 465 * Save our context 466 */ 467 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od; 468 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff); 469 return(0); 470} 471 472static int 473bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev) 474{ 475 struct open_disk *od; 476 int error; 477 478 if (dev->dd.d_unit >= nbdinfo) { 479 DEBUG("attempt to open nonexistent disk"); 480 return(ENXIO); 481 } 482 483 od = (struct open_disk *)malloc(sizeof(struct open_disk)); 484 if (!od) { 485 DEBUG("no memory"); 486 return (ENOMEM); 487 } 488 489 /* Look up BIOS unit number, intialise open_disk structure */ 490 od->od_dkunit = dev->dd.d_unit; 491 od->od_unit = bdinfo[od->od_dkunit].bd_unit; 492 od->od_flags = bdinfo[od->od_dkunit].bd_flags; 493 od->od_boff = 0; 494 error = 0; 495 DEBUG("open '%s', unit 0x%x slice %d partition %d", 496 i386_fmtdev(dev), dev->dd.d_unit, 497 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition); 498 499 /* Get geometry for this open (removable device may have changed) */ 500 if (bd_getgeom(od)) { 501 DEBUG("can't get geometry"); 502 error = ENXIO; 503 goto out; 504 } 505 506 /* Determine disk layout. */ 507 error = bd_open_pc98(od, dev); 508 509 out: 510 if (error) { 511 free(od); 512 } else { 513 *odp = od; /* return the open disk */ 514 } 515 return(error); 516} 517 518static int 519bd_open_pc98(struct open_disk *od, struct i386_devdesc *dev) 520{ 521 struct pc98_partition *dptr; 522 struct disklabel *lp; 523 int sector, slice, i; 524 char buf[BUFSIZE]; 525 526 /* 527 * Following calculations attempt to determine the correct value 528 * for d->od_boff by looking for the slice and partition specified, 529 * or searching for reasonable defaults. 530 */ 531 532 /* 533 * Find the slice in the DOS slice table. 534 */ 535 od->od_nslices = 0; 536 if (od->od_flags & BD_FLOPPY) { 537 sector = 0; 538 goto unsliced; 539 } 540 if (bd_read(od, 0, 1, buf)) { 541 DEBUG("error reading MBR"); 542 return (EIO); 543 } 544 545 /* 546 * Check the slice table magic. 547 */ 548 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) { 549 /* If a slice number was explicitly supplied, this is an error */ 550 if (dev->d_kind.biosdisk.slice > 0) { 551 DEBUG("no slice table/MBR (no magic)"); 552 return (ENOENT); 553 } 554 sector = 0; 555 goto unsliced; /* may be a floppy */ 556 } 557 if (bd_read(od, 1, 1, buf)) { 558 DEBUG("error reading MBR"); 559 return (EIO); 560 } 561 562 /* 563 * copy the partition table, then pick up any extended partitions. 564 */ 565 bcopy(buf + PC98_PARTOFF, &od->od_slicetab, 566 sizeof(struct pc98_partition) * PC98_NPARTS); 567 od->od_nslices = PC98_NPARTS; /* extended slices start here */ 568 od->od_flags |= BD_PARTTABOK; 569 dptr = &od->od_slicetab[0]; 570 571 /* Is this a request for the whole disk? */ 572 if (dev->d_kind.biosdisk.slice == -1) { 573 sector = 0; 574 goto unsliced; 575 } 576 577 /* 578 * if a slice number was supplied but not found, this is an error. 579 */ 580 if (dev->d_kind.biosdisk.slice > 0) { 581 slice = dev->d_kind.biosdisk.slice - 1; 582 if (slice >= od->od_nslices) { 583 DEBUG("slice %d not found", slice); 584 return (ENOENT); 585 } 586 } 587 588 /* Try to auto-detect the best slice; this should always give a slice number */ 589 if (dev->d_kind.biosdisk.slice == 0) { 590 slice = bd_bestslice(od); 591 if (slice == -1) { 592 return (ENOENT); 593 } 594 dev->d_kind.biosdisk.slice = slice; 595 } 596 597 dptr = &od->od_slicetab[0]; 598 /* 599 * Accept the supplied slice number unequivocally (we may be looking 600 * at a DOS partition). 601 */ 602 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */ 603 sector = dptr->dp_scyl * od->od_hds * od->od_sec + 604 dptr->dp_shd * od->od_sec + dptr->dp_ssect; 605 { 606 int end = dptr->dp_ecyl * od->od_hds * od->od_sec + 607 dptr->dp_ehd * od->od_sec + dptr->dp_esect; 608 DEBUG("slice entry %d at %d, %d sectors", 609 dev->d_kind.biosdisk.slice - 1, sector, end-sector); 610 } 611 612 /* 613 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition 614 */ 615 if ((dptr->dp_mid == DOSMID_386BSD) && (dev->d_kind.biosdisk.partition < 0)) 616 dev->d_kind.biosdisk.partition = 0; 617 618 unsliced: 619 /* 620 * Now we have the slice offset, look for the partition in the disklabel if we have 621 * a partition to start with. 622 * 623 * XXX we might want to check the label checksum. 624 */ 625 if (dev->d_kind.biosdisk.partition < 0) { 626 od->od_boff = sector; /* no partition, must be after the slice */ 627 DEBUG("opening raw slice"); 628 } else { 629 630 if (bd_read(od, sector + LABELSECTOR, 1, buf)) { 631 DEBUG("error reading disklabel"); 632 return (EIO); 633 } 634 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel); 635 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel)); 636 lp = &od->od_disklabel; 637 od->od_flags |= BD_LABELOK; 638 639 if (lp->d_magic != DISKMAGIC) { 640 DEBUG("no disklabel"); 641 return (ENOENT); 642 } 643 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) { 644 DEBUG("partition '%c' exceeds partitions in table (a-'%c')", 645 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions); 646 return (EPART); 647 } 648 649#ifdef DISK_DEBUG 650 /* Complain if the partition is unused unless this is a floppy. */ 651 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) && 652 !(od->od_flags & BD_FLOPPY)) 653 DEBUG("warning, partition marked as unused"); 654#endif 655 656 od->od_boff = 657 lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset - 658 lp->d_partitions[RAW_PART].p_offset + 659 sector; 660 } 661 return (0); 662} 663 664/* 665 * Search for a slice with the following preferences: 666 * 667 * 1: Active FreeBSD slice 668 * 2: Non-active FreeBSD slice 669 * 3: Active Linux slice 670 * 4: non-active Linux slice 671 * 5: Active FAT/FAT32 slice 672 * 6: non-active FAT/FAT32 slice 673 */ 674#define PREF_RAWDISK 0 675#define PREF_FBSD_ACT 1 676#define PREF_FBSD 2 677#define PREF_LINUX_ACT 3 678#define PREF_LINUX 4 679#define PREF_DOS_ACT 5 680#define PREF_DOS 6 681#define PREF_NONE 7 682 683/* 684 * slicelimit is in the range 0 .. PC98_NPARTS 685 */ 686static int 687bd_bestslice(struct open_disk *od) 688{ 689 struct pc98_partition *dp; 690 int pref, preflevel; 691 int i, prefslice; 692 693 prefslice = 0; 694 preflevel = PREF_NONE; 695 696 dp = &od->od_slicetab[0]; 697 for (i = 0; i < od->od_nslices; i++, dp++) { 698 switch(dp->dp_mid & PC98_MID_MASK) { 699 case PC98_MID_386BSD: /* FreeBSD */ 700 if ((dp->dp_mid & PC98_MID_BOOTABLE) && 701 (preflevel > PREF_FBSD_ACT)) { 702 pref = i; 703 preflevel = PREF_FBSD_ACT; 704 } else if (preflevel > PREF_FBSD) { 705 pref = i; 706 preflevel = PREF_FBSD; 707 } 708 break; 709 710 case 0x11: /* DOS/Windows */ 711 case 0x20: 712 case 0x21: 713 case 0x22: 714 case 0x23: 715 case 0x63: 716 if ((dp->dp_mid & PC98_MID_BOOTABLE) && 717 (preflevel > PREF_DOS_ACT)) { 718 pref = i; 719 preflevel = PREF_DOS_ACT; 720 } else if (preflevel > PREF_DOS) { 721 pref = i; 722 preflevel = PREF_DOS; 723 } 724 break; 725 } 726 } 727 return (prefslice); 728} 729 730static int 731bd_close(struct open_file *f) 732{ 733 struct i386_devdesc *dev = f->f_devdata; 734 struct open_disk *od = (struct open_disk *)(dev->d_kind.biosdisk.data); 735 736 BD(dev).bd_open--; 737 if (BD(dev).bd_open == 0) { 738 bcache_free(BD(dev).bd_bcache); 739 BD(dev).bd_bcache = NULL; 740 } 741 742 bd_closedisk(od); 743 return(0); 744} 745 746static void 747bd_closedisk(struct open_disk *od) 748{ 749 DEBUG("open_disk %p", od); 750#if 0 751 /* XXX is this required? (especially if disk already open...) */ 752 if (od->od_flags & BD_FLOPPY) 753 delay(3000000); 754#endif 755 free(od); 756} 757 758static int 759bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, 760 char *buf, size_t *rsize) 761{ 762 struct bcache_devdata bcd; 763 struct i386_devdesc *dev = devdata; 764 struct open_disk *od = (struct open_disk *)(dev->d_kind.biosdisk.data); 765 766 bcd.dv_strategy = bd_realstrategy; 767 bcd.dv_devdata = devdata; 768 bcd.dv_cache = BD(dev).bd_bcache; 769 return(bcache_strategy(&bcd, rw, dblk+od->od_boff, size, buf, rsize)); 770} 771 772static int 773bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, 774 char *buf, size_t *rsize) 775{ 776 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data); 777 int blks; 778#ifdef BD_SUPPORT_FRAGS 779 char fragbuf[BIOSDISK_SECSIZE]; 780 size_t fragsize; 781 782 fragsize = size % BIOSDISK_SECSIZE; 783#else 784 if (size % BIOSDISK_SECSIZE) 785 panic("bd_strategy: %d bytes I/O not multiple of block size", size); 786#endif 787 788 DEBUG("open_disk %p", od); 789 blks = size / BIOSDISK_SECSIZE; 790 if (rsize) 791 *rsize = 0; 792 793 switch(rw){ 794 case F_READ: 795 DEBUG("read %d from %d to %p", blks, dblk, buf); 796 797 if (blks && bd_read(od, dblk, blks, buf)) { 798 DEBUG("read error"); 799 return (EIO); 800 } 801#ifdef BD_SUPPORT_FRAGS 802 DEBUG("bd_strategy: frag read %d from %d+%d to %p", 803 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE)); 804 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) { 805 DEBUG("frag read error"); 806 return(EIO); 807 } 808 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize); 809#endif 810 break; 811 case F_WRITE : 812 DEBUG("write %d from %d to %p", blks, dblk, buf); 813 814 if (blks && bd_write(od, dblk, blks, buf)) { 815 DEBUG("write error"); 816 return (EIO); 817 } 818#ifdef BD_SUPPORT_FRAGS 819 if(fragsize) { 820 DEBUG("Attempted to write a frag"); 821 return (EIO); 822 } 823#endif 824 break; 825 default: 826 /* DO NOTHING */ 827 return (EROFS); 828 } 829 830 if (rsize) 831 *rsize = size; 832 return (0); 833} 834 835/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */ 836#define FLOPPY_BOUNCEBUF 18 837 838static int 839bd_chs_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write) 840{ 841 u_int x, bpc, cyl, hd, sec; 842 843 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */ 844 x = dblk; 845 cyl = x / bpc; /* block # / blocks per cylinder */ 846 x %= bpc; /* block offset into cylinder */ 847 hd = x / od->od_sec; /* offset / blocks per track */ 848 sec = x % od->od_sec; /* offset into track */ 849 850 v86.ctl = V86_FLAGS; 851 v86.addr = 0x1b; 852 if (write) 853 v86.eax = 0x0500 | od->od_unit; 854 else 855 v86.eax = 0x0600 | od->od_unit; 856 if (od->od_flags & BD_FLOPPY) { 857 v86.eax |= 0xd000; 858 v86.ecx = 0x0200 | (cyl & 0xff); 859 v86.edx = (hd << 8) | (sec + 1); 860 } else if (od->od_flags & BD_OPTICAL) { 861 v86.eax &= 0xFF7F; 862 v86.ecx = dblk & 0xFFFF; 863 v86.edx = dblk >> 16; 864 } else { 865 v86.ecx = cyl; 866 v86.edx = (hd << 8) | sec; 867 } 868 v86.ebx = blks * BIOSDISK_SECSIZE; 869 v86.es = VTOPSEG(dest); 870 v86.ebp = VTOPOFF(dest); 871 v86int(); 872 return (V86_CY(v86.efl)); 873} 874 875static int 876bd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write) 877{ 878 u_int x, sec, result, resid, retry, maxfer; 879 caddr_t p, xp, bbuf, breg; 880 881 /* Just in case some idiot actually tries to read/write -1 blocks... */ 882 if (blks < 0) 883 return (-1); 884 885 resid = blks; 886 p = dest; 887 888 /* Decide whether we have to bounce */ 889 if (VTOP(dest) >> 20 != 0 || 890 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) { 891 892 /* 893 * There is a 64k physical boundary somewhere in the 894 * destination buffer, or the destination buffer is above 895 * first 1MB of physical memory so we have to arrange a 896 * suitable bounce buffer. Allocate a buffer twice as large 897 * as we need to. Use the bottom half unless there is a break 898 * there, in which case we use the top half. 899 */ 900 x = min(od->od_sec, (unsigned)blks); 901 bbuf = alloca(x * 2 * BIOSDISK_SECSIZE); 902 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == 903 ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) { 904 breg = bbuf; 905 } else { 906 breg = bbuf + x * BIOSDISK_SECSIZE; 907 } 908 maxfer = x; /* limit transfers to bounce region size */ 909 } else { 910 breg = bbuf = NULL; 911 maxfer = 0; 912 } 913 914 while (resid > 0) { 915 /* 916 * Play it safe and don't cross track boundaries. 917 * (XXX this is probably unnecessary) 918 */ 919 sec = dblk % od->od_sec; /* offset into track */ 920 x = min(od->od_sec - sec, resid); 921 if (maxfer > 0) 922 x = min(x, maxfer); /* fit bounce buffer */ 923 924 /* where do we transfer to? */ 925 xp = bbuf == NULL ? p : breg; 926 927 /* 928 * Put your Data In, Put your Data out, 929 * Put your Data In, and shake it all about 930 */ 931 if (write && bbuf != NULL) 932 bcopy(p, breg, x * BIOSDISK_SECSIZE); 933 934 /* 935 * Loop retrying the operation a couple of times. The BIOS 936 * may also retry. 937 */ 938 for (retry = 0; retry < 3; retry++) { 939 /* if retrying, reset the drive */ 940 if (retry > 0) { 941 v86.ctl = V86_FLAGS; 942 v86.addr = 0x1b; 943 v86.eax = 0x0300 | od->od_unit; 944 v86int(); 945 } 946 947 result = bd_chs_io(od, dblk, x, xp, write); 948 if (result == 0) 949 break; 950 } 951 952 if (write) 953 DEBUG("Write %d sector(s) from %p (0x%x) to %lld %s", x, 954 p, VTOP(p), dblk, result ? "failed" : "ok"); 955 else 956 DEBUG("Read %d sector(s) from %lld to %p (0x%x) %s", x, 957 dblk, p, VTOP(p), result ? "failed" : "ok"); 958 if (result) { 959 return(-1); 960 } 961 if (!write && bbuf != NULL) 962 bcopy(breg, p, x * BIOSDISK_SECSIZE); 963 p += (x * BIOSDISK_SECSIZE); 964 dblk += x; 965 resid -= x; 966 } 967 968/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */ 969 return(0); 970} 971 972static int 973bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 974{ 975 976 return (bd_io(od, dblk, blks, dest, 0)); 977} 978 979static int 980bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest) 981{ 982 983 return (bd_io(od, dblk, blks, dest, 1)); 984} 985 986static int 987bd_getgeom(struct open_disk *od) 988{ 989 990 if (od->od_flags & BD_FLOPPY) { 991 od->od_cyl = 79; 992 od->od_hds = 2; 993 od->od_sec = (od->od_unit & 0xf0) == 0x30 ? 18 : 15; 994 } else if (od->od_flags & BD_OPTICAL) { 995 od->od_cyl = 0xFFFE; 996 od->od_hds = 8; 997 od->od_sec = 32; 998 } else { 999 v86.ctl = V86_FLAGS; 1000 v86.addr = 0x1b; 1001 v86.eax = 0x8400 | od->od_unit; 1002 v86int(); 1003 1004 od->od_cyl = v86.ecx; 1005 od->od_hds = (v86.edx >> 8) & 0xff; 1006 od->od_sec = v86.edx & 0xff; 1007 if (V86_CY(v86.efl)) 1008 return(1); 1009 } 1010 1011 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec); 1012 return(0); 1013} 1014 1015/* 1016 * Return the BIOS geometry of a given "fixed drive" in a format 1017 * suitable for the legacy bootinfo structure. Since the kernel is 1018 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we 1019 * prefer to get the information directly, rather than rely on being 1020 * able to put it together from information already maintained for 1021 * different purposes and for a probably different number of drives. 1022 * 1023 * For valid drives, the geometry is expected in the format (31..0) 1024 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are 1025 * indicated by returning the geometry of a "1.2M" PC-format floppy 1026 * disk. And, incidentally, what is returned is not the geometry as 1027 * such but the highest valid cylinder, head, and sector numbers. 1028 */ 1029u_int32_t 1030bd_getbigeom(int bunit) 1031{ 1032 int hds = 0; 1033 int unit = 0x80; /* IDE HDD */ 1034 u_int addr = 0xA155d; 1035 1036 while (unit < 0xa7) { 1037 if (*(u_char *)PTOV(addr) & (1 << (unit & 0x0f))) 1038 if (hds++ == bunit) 1039 break; 1040 1041 if (unit >= 0xA0) { 1042 int media = ((unsigned *)PTOV(0xA1460))[unit & 0x0F] & 0x1F; 1043 1044 if (media == 7 && hds++ == bunit) /* SCSI MO */ 1045 return(0xFFFE0820); /* C:65535 H:8 S:32 */ 1046 } 1047 if (++unit == 0x84) { 1048 unit = 0xA0; /* SCSI HDD */ 1049 addr = 0xA1482; 1050 } 1051 } 1052 if (unit == 0xa7) 1053 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */ 1054 v86.ctl = V86_FLAGS; 1055 v86.addr = 0x1b; 1056 v86.eax = 0x8400 | unit; 1057 v86int(); 1058 if (V86_CY(v86.efl)) 1059 return 0x4F020F; /* 1200KB FD C:80 H:2 S:15 */ 1060 return ((v86.ecx & 0xffff) << 16) | (v86.edx & 0xffff); 1061} 1062 1063/* 1064 * Return a suitable dev_t value for (dev). 1065 * 1066 * In the case where it looks like (dev) is a SCSI disk, we allow the number of 1067 * IDE disks to be specified in $num_ide_disks. There should be a Better Way. 1068 */ 1069int 1070bd_getdev(struct i386_devdesc *dev) 1071{ 1072 struct open_disk *od; 1073 int biosdev; 1074 int major; 1075 int rootdev; 1076 char *nip, *cp; 1077 int unitofs = 0, i, unit; 1078 1079 biosdev = bd_unit2bios(dev->dd.d_unit); 1080 DEBUG("unit %d BIOS device %d", dev->dd.d_unit, biosdev); 1081 if (biosdev == -1) /* not a BIOS device */ 1082 return(-1); 1083 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */ 1084 return(-1); 1085 1086 if ((biosdev & 0xf0) == 0x90 || (biosdev & 0xf0) == 0x30) { 1087 /* floppy (or emulated floppy) or ATAPI device */ 1088 if (bdinfo[dev->dd.d_unit].bd_type == DT_ATAPI) { 1089 /* is an ATAPI disk */ 1090 major = WFDMAJOR; 1091 } else { 1092 /* is a floppy disk */ 1093 major = FDMAJOR; 1094 } 1095 } else { 1096 /* harddisk */ 1097 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) { 1098 /* label OK, disk labelled as SCSI */ 1099 major = DAMAJOR; 1100 /* check for unit number correction hint, now deprecated */ 1101 if ((nip = getenv("num_ide_disks")) != NULL) { 1102 i = strtol(nip, &cp, 0); 1103 /* check for parse error */ 1104 if ((cp != nip) && (*cp == 0)) 1105 unitofs = i; 1106 } 1107 } else { 1108 /* assume an IDE disk */ 1109 major = WDMAJOR; 1110 } 1111 } 1112 /* default root disk unit number */ 1113 if ((biosdev & 0xf0) == 0xa0) 1114 unit = bdinfo[dev->dd.d_unit].bd_da_unit; 1115 else 1116 unit = biosdev & 0xf; 1117 1118 /* XXX a better kludge to set the root disk unit number */ 1119 if ((nip = getenv("root_disk_unit")) != NULL) { 1120 i = strtol(nip, &cp, 0); 1121 /* check for parse error */ 1122 if ((cp != nip) && (*cp == 0)) 1123 unit = i; 1124 } 1125 1126 rootdev = MAKEBOOTDEV(major, dev->d_kind.biosdisk.slice + 1, unit, 1127 dev->d_kind.biosdisk.partition); 1128 DEBUG("dev is 0x%x\n", rootdev); 1129 return(rootdev); 1130} 1131