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