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