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 * $Id: biosdisk.c,v 1.22 1999/01/24 06:03:44 msmith Exp $
27 */
28
29/*
30 * BIOS disk device handling.
31 *
32 * Ideas and algorithms from:
33 *
34 * - NetBSD libi386/biosdisk.c
35 * - FreeBSD biosboot/disk.c
36 *
37 * XXX Todo: add bad144 support.
38 */
39
40#include <stand.h>
41
42#include <sys/disklabel.h>
43#include <sys/diskslice.h>
44#include <sys/reboot.h>
45
46#include <stdarg.h>
47
48#include <bootstrap.h>
49#include <btxv86.h>
50#include "libi386.h"
51
52#define BIOSDISK_SECSIZE 512
53#define BUFSIZE (1 * BIOSDISK_SECSIZE)
54#define MAXBDDEV MAXDEV
55
56#define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
57#define WDMAJOR 0 /* major numbers for devices we frontend for */
58#define WFDMAJOR 1
59#define FDMAJOR 2
60#define DAMAJOR 4
61
62#ifdef DISK_DEBUG
63# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __FUNCTION__ , ## args)
64#else
65# define DEBUG(fmt, args...)
66#endif
67
68struct open_disk {
69 int od_dkunit; /* disk unit number */
70 int od_unit; /* BIOS unit number */
71 int od_cyl; /* BIOS geometry */
72 int od_hds;
73 int od_sec;
74 int od_boff; /* block offset from beginning of BIOS disk */
75 int od_flags;
76#define BD_MODEMASK 0x3
77#define BD_MODEINT13 0x0
78#define BD_MODEEDD1 0x1
79#define BD_MODEEDD3 0x2
80#define BD_FLOPPY (1<<2)
81 struct disklabel od_disklabel;
82 struct dos_partition od_parttab[NDOSPART]; /* XXX needs to grow for extended partitions */
83#define BD_LABELOK (1<<3)
84#define BD_PARTTABOK (1<<4)
85};
86
87/*
88 * List of BIOS devices, translation from disk unit number to
89 * BIOS unit number.
90 */
91static struct bdinfo
92{
93 int bd_unit; /* BIOS unit number */
94 int bd_flags;
95 int bd_type; /* BIOS 'drive type' (floppy only) */
96} bdinfo [MAXBDDEV];
97static int nbdinfo = 0;
98
99static int bd_getgeom(struct open_disk *od);
100static int bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest);
101
102static int bd_int13probe(struct bdinfo *bd);
103
104static void bd_printslice(struct open_disk *od, int offset, char *prefix);
105
106static int bd_init(void);
107static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize);
108static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize);
109static int bd_open(struct open_file *f, ...);
110static int bd_close(struct open_file *f);
111static void bd_print(int verbose);
112
113struct devsw biosdisk = {
114 "disk",
115 DEVT_DISK,
116 bd_init,
117 bd_strategy,
118 bd_open,
119 bd_close,
120 noioctl,
121 bd_print
122};
123
124static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
125static void bd_closedisk(struct open_disk *od);
126static int bd_bestslice(struct dos_partition *dptr);
127
128/*
129 * Translate between BIOS device numbers and our private unit numbers.
130 */
131int
132bd_bios2unit(int biosdev)
133{
134 int i;
135
136 DEBUG("looking for bios device 0x%x", biosdev);
137 for (i = 0; i < nbdinfo; i++) {
138 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
139 if (bdinfo[i].bd_unit == biosdev)
140 return(i);
141 }
142 return(-1);
143}
144
145int
146bd_unit2bios(int unit)
147{
148 if ((unit >= 0) && (unit < nbdinfo))
149 return(bdinfo[unit].bd_unit);
150 return(-1);
151}
152
153/*
154 * Quiz the BIOS for disk devices, save a little info about them.
155 *
156 * XXX should we be consulting the BIOS equipment list, specifically
157 * the value at 0x475?
158 */
159static int
160bd_init(void)
161{
162 int base, unit;
163
164 /* sequence 0, 0x80 */
165 for (base = 0; base <= 0x80; base += 0x80) {
166 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
167 bdinfo[nbdinfo].bd_unit = unit;
168 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
169
170 /* XXX add EDD probes */
171 if (!bd_int13probe(&bdinfo[nbdinfo]))
172 break;
173
174 /* XXX we need "disk aliases" to make this simpler */
175 printf("BIOS drive %c: is disk%d\n",
176 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
177 nbdinfo++;
178 }
179 }
180 return(0);
181}
182
183/*
184 * Try to detect a device supported by the legacy int13 BIOS
185 */
186
187static int
188bd_int13probe(struct bdinfo *bd)
189{
190
191 v86.ctl = V86_FLAGS;
192 v86.addr = 0x13;
193 v86.eax = 0x800;
194 v86.edx = bd->bd_unit;
195 v86int();
196
197 if (!(v86.efl & 0x1) && /* carry clear */
198 ((v86.edx & 0xff) > (bd->bd_unit & 0x7f))) { /* unit # OK */
199 bd->bd_flags |= BD_MODEINT13;
200 bd->bd_type = v86.ebx & 0xff;
201 return(1);
202 }
203 return(0);
204}
205
206/*
207 * Print information about disks
208 */
209static void
210bd_print(int verbose)
211{
212 int i, j;
213 char line[80];
214 struct i386_devdesc dev;
215 struct open_disk *od;
216 struct dos_partition *dptr;
217
218 for (i = 0; i < nbdinfo; i++) {
219 sprintf(line, " disk%d: BIOS drive %c:\n", i,
220 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
221 pager_output(line);
222
223 /* try to open the whole disk */
224 dev.d_kind.biosdisk.unit = i;
225 dev.d_kind.biosdisk.slice = -1;
226 dev.d_kind.biosdisk.partition = -1;
227
228 if (!bd_opendisk(&od, &dev)) {
229
230 /* Do we have a partition table? */
231 if (od->od_flags & BD_PARTTABOK) {
232 dptr = &od->od_parttab[0];
233
234 /* Check for a "truly dedicated" disk */
235 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
236 (dptr[3].dp_start == 0) &&
237 (dptr[3].dp_size == 50000)) {
238 sprintf(line, " disk%d", i);
239 bd_printslice(od, 0, line);
240 } else {
241 for (j = 0; j < NDOSPART; j++) {
242 switch(dptr[j].dp_typ) {
243 case DOSPTYP_386BSD:
244 sprintf(line, " disk%ds%d", i, j + 1);
245 bd_printslice(od, dptr[j].dp_start, line);
246 break;
247 default:
248 }
249 }
250
251 }
252 }
253 bd_closedisk(od);
254 }
255 }
256}
257
258static void
259bd_printslice(struct open_disk *od, int offset, char *prefix)
260{
261 char line[80];
262 u_char buf[BIOSDISK_SECSIZE];
263 struct disklabel *lp;
264 int i;
265
266 /* read disklabel */
267 if (bd_read(od, offset + LABELSECTOR, 1, buf))
268 return;
269 lp =(struct disklabel *)(&buf[0]);
270 if (lp->d_magic != DISKMAGIC) {
271 sprintf(line, "bad disklabel\n");
272 pager_output(line);
273 return;
274 }
275
276 /* Print partitions */
277 for (i = 0; i < lp->d_npartitions; i++) {
278 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) || (lp->d_partitions[i].p_fstype == FS_SWAP) ||
279 ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
280 (od->od_flags & BD_FLOPPY) && (i == 0))) { /* Floppies often have bogus fstype, print 'a' */
281 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i,
282 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : "FFS",
283 lp->d_partitions[i].p_size / 2048, /* 512-byte sector assumption */
284 lp->d_partitions[i].p_offset, lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
285 pager_output(line);
286 }
287 }
288}
289
290
291/*
292 * Attempt to open the disk described by (dev) for use by (f).
293 *
294 * Note that the philosophy here is "give them exactly what
295 * they ask for". This is necessary because being too "smart"
296 * about what the user might want leads to complications.
297 * (eg. given no slice or partition value, with a disk that is
298 * sliced - are they after the first BSD slice, or the DOS
299 * slice before it?)
300 */
301static int
302bd_open(struct open_file *f, ...)
303{
304 va_list ap;
305 struct i386_devdesc *dev;
306 struct open_disk *od;
307 int error;
308
309 va_start(ap, f);
310 dev = va_arg(ap, struct i386_devdesc *);
311 va_end(ap);
312 if ((error = bd_opendisk(&od, dev)))
313 return(error);
314
315 /*
316 * Save our context
317 */
318 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
319 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
320 return(0);
321}
322
323static int
324bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
325{
326 struct dos_partition *dptr;
327 struct disklabel *lp;
328 struct open_disk *od;
329 int sector, slice, i;
330 int error;
331 u_char buf[BUFSIZE];
332 daddr_t pref_slice[4];
333
334 if (dev->d_kind.biosdisk.unit >= nbdinfo) {
335 DEBUG("attempt to open nonexistent disk");
336 return(ENXIO);
337 }
338
339 od = (struct open_disk *)malloc(sizeof(struct open_disk));
340 if (!od) {
341 DEBUG("no memory");
342 return (ENOMEM);
343 }
344
345 /* Look up BIOS unit number, intialise open_disk structure */
346 od->od_dkunit = dev->d_kind.biosdisk.unit;
347 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
348 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
349 od->od_boff = 0;
350 error = 0;
351 DEBUG("open '%s', unit 0x%x slice %d partition %c",
352 i386_fmtdev(dev), dev->d_kind.biosdisk.unit,
353 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
354
355 /* Get geometry for this open (removable device may have changed) */
356 if (bd_getgeom(od)) {
357 DEBUG("can't get geometry");
358 error = ENXIO;
359 goto out;
360 }
361
362 /*
363 * Following calculations attempt to determine the correct value
364 * for d->od_boff by looking for the slice and partition specified,
365 * or searching for reasonable defaults.
366 */
367
368 /*
369 * Find the slice in the DOS slice table.
370 */
371 if (bd_read(od, 0, 1, buf)) {
372 DEBUG("error reading MBR");
373 error = EIO;
374 goto out;
375 }
376
377 /*
378 * Check the slice table magic.
379 */
380 if ((buf[0x1fe] != 0x55) || (buf[0x1ff] != 0xaa)) {
381 /* If a slice number was explicitly supplied, this is an error */
382 if (dev->d_kind.biosdisk.slice > 0) {
383 DEBUG("no slice table/MBR (no magic)");
384 error = ENOENT;
385 goto out;
386 }
387 sector = 0;
388 goto unsliced; /* may be a floppy */
389 }
390 bcopy(buf + DOSPARTOFF, &od->od_parttab, sizeof(struct dos_partition) * NDOSPART);
391 dptr = &od->od_parttab[0];
392 od->od_flags |= BD_PARTTABOK;
393
394 /* Is this a request for the whole disk? */
395 if (dev->d_kind.biosdisk.slice == -1) {
396 sector = 0;
397 goto unsliced;
398 }
399
400 /* Try to auto-detect the best slice; this should always give a slice number */
401 if (dev->d_kind.biosdisk.slice == 0)
402 dev->d_kind.biosdisk.slice = bd_bestslice(dptr);
403
404 switch (dev->d_kind.biosdisk.slice) {
405 case -1:
406 error = ENOENT;
407 goto out;
408 case 0:
409 sector = 0;
410 goto unsliced;
411 default:
412 break;
413 }
414
415 /*
416 * Accept the supplied slice number unequivocally (we may be looking
417 * at a DOS partition).
418 */
419 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
420 sector = dptr->dp_start;
421 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
422
423 /*
424 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
425 */
426 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
427 dev->d_kind.biosdisk.partition = 0;
428
429 unsliced:
430 /*
431 * Now we have the slice offset, look for the partition in the disklabel if we have
432 * a partition to start with.
433 *
434 * XXX we might want to check the label checksum.
435 */
436 if (dev->d_kind.biosdisk.partition < 0) {
437 od->od_boff = sector; /* no partition, must be after the slice */
438 DEBUG("opening raw slice");
439 } else {
440
441 if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
442 DEBUG("error reading disklabel");
443 error = EIO;
444 goto out;
445 }
446 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
447 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
448 lp = &od->od_disklabel;
449 od->od_flags |= BD_LABELOK;
450
451 if (lp->d_magic != DISKMAGIC) {
452 DEBUG("no disklabel");
453 error = ENOENT;
454 goto out;
455 }
456 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
457 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
458 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
459 error = EPART;
460 goto out;
461
462 }
463
464 /* Complain if the partition type is wrong */
465 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
466 !(od->od_flags & BD_FLOPPY)) /* Floppies often have bogus fstype */
467 DEBUG("warning, partition marked as unused");
468
469 od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset;
470 }
471
472 out:
473 if (error) {
474 free(od);
475 } else {
476 *odp = od; /* return the open disk */
477 }
478 return(error);
479}
480
481
482/*
483 * Search for a slice with the following preferences:
484 *
485 * 1: Active FreeBSD slice
486 * 2: Non-active FreeBSD slice
487 * 3: Active FAT/FAT32 slice
488 * 4: non-active FAT/FAT32 slice
489 */
490#define PREF_FBSD_ACT 0
491#define PREF_FBSD 1
492#define PREF_DOS_ACT 2
493#define PREF_DOS 3
494#define PREF_NONE 4
495
496static int
497bd_bestslice(struct dos_partition *dptr)
498{
499 int i;
500 int preflevel, pref;
501
502
503 /*
504 * Check for the historically bogus MBR found on true dedicated disks
505 */
506 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
507 (dptr[3].dp_start == 0) &&
508 (dptr[3].dp_size == 50000))
509 return(0);
510
511 preflevel = PREF_NONE;
512 pref = -1;
513
514 /*
515 * XXX No support here for 'extended' slices
516 */
517 for (i = 0; i < NDOSPART; i++) {
518 switch(dptr[i].dp_typ) {
519 case DOSPTYP_386BSD: /* FreeBSD */
520 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_FBSD_ACT)) {
521 pref = i;
522 preflevel = PREF_FBSD_ACT;
523 } else if (preflevel > PREF_FBSD) {
524 pref = i;
525 preflevel = PREF_FBSD;
526 }
527 break;
528
529 case 0x04: /* DOS/Windows */
530 case 0x06:
531 case 0x0b:
532 case 0x0c:
533 case 0x0e:
534 case 0x63:
535 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_DOS_ACT)) {
536 pref = i;
537 preflevel = PREF_DOS_ACT;
538 } else if (preflevel > PREF_DOS) {
539 pref = i;
540 preflevel = PREF_DOS;
541 }
542 break;
543 }
544 }
545 return(pref + 1); /* slices numbered 1-4 */
546}
547
548
549static int
550bd_close(struct open_file *f)
551{
552 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
553
554 bd_closedisk(od);
555 return(0);
556}
557
558static void
559bd_closedisk(struct open_disk *od)
560{
561 DEBUG("open_disk %p", od);
562#if 0
563 /* XXX is this required? (especially if disk already open...) */
564 if (od->od_flags & BD_FLOPPY)
565 delay(3000000);
566#endif
567 free(od);
568}
569
570static int
571bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
572{
573 struct bcache_devdata bcd;
574
575 bcd.dv_strategy = bd_realstrategy;
576 bcd.dv_devdata = devdata;
577 return(bcache_strategy(&bcd, rw, dblk, size, buf, rsize));
578}
579
580static int
581bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
582{
583 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
584 int blks;
585#ifdef BD_SUPPORT_FRAGS
586 char fragbuf[BIOSDISK_SECSIZE];
587 size_t fragsize;
588
589 fragsize = size % BIOSDISK_SECSIZE;
590#else
591 if (size % BIOSDISK_SECSIZE)
592 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
593#endif
594
595 DEBUG("open_disk %p", od);
596
597 if (rw != F_READ)
598 return(EROFS);
599
600
601 blks = size / BIOSDISK_SECSIZE;
602 DEBUG("read %d from %d+%d to %p", blks, od->od_boff, dblk, buf);
603
604 if (rsize)
605 *rsize = 0;
606 if (blks && bd_read(od, dblk + od->od_boff, blks, buf)) {
607 DEBUG("read error");
608 return (EIO);
609 }
610#ifdef BD_SUPPORT_FRAGS
611 DEBUG("bd_strategy: frag read %d from %d+%d+d to %p",
612 fragsize, od->od_boff, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
613 if (fragsize && bd_read(od, dblk + od->od_boff + blks, 1, fragsize)) {
614 DEBUG("frag read error");
615 return(EIO);
616 }
617 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
618#endif
619 if (rsize)
620 *rsize = size;
621 return (0);
622}
623
624/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
625#define FLOPPY_BOUNCEBUF 18
626
627static int
628bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
629{
630 int x, bpc, cyl, hd, sec, result, resid, cnt, retry, maxfer;
631 caddr_t p, xp, bbuf, breg;
632
633 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
634 resid = blks;
635 p = dest;
636
637 /* Decide whether we have to bounce */
638 if ((od->od_unit < 0x80) &&
639 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
640
641 /*
642 * There is a 64k physical boundary somewhere in the destination buffer, so we have
643 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
644 * need to. Use the bottom half unless there is a break there, in which case we
645 * use the top half.
646 */
647 x = min(FLOPPY_BOUNCEBUF, blks);
648 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
649 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
650 breg = bbuf;
651 } else {
652 breg = bbuf + x * BIOSDISK_SECSIZE;
653 }
654 maxfer = x; /* limit transfers to bounce region size */
655 } else {
656 bbuf = NULL;
657 maxfer = 0;
658 }
659
660 while (resid > 0) {
661 x = dblk;
662 cyl = x / bpc; /* block # / blocks per cylinder */
663 x %= bpc; /* block offset into cylinder */
664 hd = x / od->od_sec; /* offset / blocks per track */
665 sec = x % od->od_sec; /* offset into track */
666
667 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
668 x = min(od->od_sec - sec, resid);
669 if (maxfer > 0)
670 x = min(x, maxfer); /* fit bounce buffer */
671
672 /* where do we transfer to? */
673 xp = bbuf == NULL ? p : breg;
674
675 /* correct sector number for 1-based BIOS numbering */
676 sec++;
677
678 /* Loop retrying the operation a couple of times. The BIOS may also retry. */
679 for (retry = 0; retry < 3; retry++) {
680 /* if retrying, reset the drive */
681 if (retry > 0) {
682 v86.ctl = V86_FLAGS;
683 v86.addr = 0x13;
684 v86.eax = 0;
685 v86.edx = od->od_unit;
686 v86int();
687 }
688
689 /* build request XXX support EDD requests too */
690 v86.ctl = V86_FLAGS;
691 v86.addr = 0x13;
692 v86.eax = 0x200 | x;
693 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
694 v86.edx = (hd << 8) | od->od_unit;
695 v86.es = VTOPSEG(xp);
696 v86.ebx = VTOPOFF(xp);
697 v86int();
698 result = (v86.efl & 0x1);
699 if (result == 0)
700 break;
701 }
702
703 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
704 /* BUG here, cannot use v86 in printf because putchar uses it too */
705 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
706 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
707 if (result) {
708 if (bbuf != NULL)
709 free(bbuf);
710 return(-1);
711 }
712 if (bbuf != NULL)
713 bcopy(breg, p, x * BIOSDISK_SECSIZE);
714 p += (x * BIOSDISK_SECSIZE);
715 dblk += x;
716 resid -= x;
717 }
718
719/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
720 if (bbuf != NULL)
721 free(bbuf);
722 return(0);
723}
724
725static int
726bd_getgeom(struct open_disk *od)
727{
728
729 v86.ctl = V86_FLAGS;
730 v86.addr = 0x13;
731 v86.eax = 0x800;
732 v86.edx = od->od_unit;
733 v86int();
734
735 if ((v86.efl & 0x1) || /* carry set */
736 ((v86.edx & 0xff) <= (od->od_unit & 0x7f))) /* unit # bad */
737 return(1);
738
739 /* convert max cyl # -> # of cylinders */
740 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
741 /* convert max head # -> # of heads */
742 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
743 od->od_sec = v86.ecx & 0x3f;
744
745 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
746 return(0);
747}
748
749/*
750 * Return a suitable dev_t value for (dev).
751 *
752 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
753 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
754 */
755int
756bd_getdev(struct i386_devdesc *dev)
757{
758 struct open_disk *od;
759 int biosdev;
760 int major;
761 int rootdev;
762 char *nip, *cp;
763 int unitofs = 0, i, unit;
764
765 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
766 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
767 if (biosdev == -1) /* not a BIOS device */
768 return(-1);
769 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
770 return(-1);
771
772 if (biosdev < 0x80) {
773 /* floppy (or emulated floppy) or ATAPI device */
774 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
775 /* is an ATAPI disk */
776 major = WFDMAJOR;
777 } else {
778 /* is a floppy disk */
779 major = FDMAJOR;
780 }
781 } else {
782 /* harddisk */
783 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
784 /* label OK, disk labelled as SCSI */
785 major = DAMAJOR;
786 /* check for unit number correction hint, now deprecated */
787 if ((nip = getenv("num_ide_disks")) != NULL) {
788 i = strtol(nip, &cp, 0);
789 /* check for parse error */
790 if ((cp != nip) && (*cp == 0))
791 unitofs = i;
792 }
793 } else {
794 /* assume an IDE disk */
795 major = WDMAJOR;
796 }
797 }
798 /* XXX a better kludge to set the root disk unit number */
799 if ((nip = getenv("root_disk_unit")) != NULL) {
800 i = strtol(nip, &cp, 0);
801 /* check for parse error */
802 if ((cp != nip) && (*cp == 0))
803 unit = i;
804 } else {
805 unit = (biosdev & 0x7f) - unitofs; /* allow for #wd compenstation in da case */
806 }
807
808 rootdev = MAKEBOOTDEV(major,
809 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */
810 (dev->d_kind.biosdisk.slice + 1) & 0xf,
811 unit,
812 dev->d_kind.biosdisk.partition);
813 DEBUG("dev is 0x%x\n", rootdev);
814 return(rootdev);
815}
816
817/*
818 * Fix (dev) so that it refers to the 'real' disk/slice/partition that it implies.
819 */
820int
821bd_fixupdev(struct i386_devdesc *dev)
822{
823 struct open_disk *od;
824
825 /*
826 * Open the disk. This will fix up the slice and partition fields.
827 */
828 if (bd_opendisk(&od, dev) != 0)
829 return(ENOENT);
830
831 bd_closedisk(od);
832}
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 * $Id: biosdisk.c,v 1.22 1999/01/24 06:03:44 msmith Exp $
27 */
28
29/*
30 * BIOS disk device handling.
31 *
32 * Ideas and algorithms from:
33 *
34 * - NetBSD libi386/biosdisk.c
35 * - FreeBSD biosboot/disk.c
36 *
37 * XXX Todo: add bad144 support.
38 */
39
40#include <stand.h>
41
42#include <sys/disklabel.h>
43#include <sys/diskslice.h>
44#include <sys/reboot.h>
45
46#include <stdarg.h>
47
48#include <bootstrap.h>
49#include <btxv86.h>
50#include "libi386.h"
51
52#define BIOSDISK_SECSIZE 512
53#define BUFSIZE (1 * BIOSDISK_SECSIZE)
54#define MAXBDDEV MAXDEV
55
56#define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
57#define WDMAJOR 0 /* major numbers for devices we frontend for */
58#define WFDMAJOR 1
59#define FDMAJOR 2
60#define DAMAJOR 4
61
62#ifdef DISK_DEBUG
63# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __FUNCTION__ , ## args)
64#else
65# define DEBUG(fmt, args...)
66#endif
67
68struct open_disk {
69 int od_dkunit; /* disk unit number */
70 int od_unit; /* BIOS unit number */
71 int od_cyl; /* BIOS geometry */
72 int od_hds;
73 int od_sec;
74 int od_boff; /* block offset from beginning of BIOS disk */
75 int od_flags;
76#define BD_MODEMASK 0x3
77#define BD_MODEINT13 0x0
78#define BD_MODEEDD1 0x1
79#define BD_MODEEDD3 0x2
80#define BD_FLOPPY (1<<2)
81 struct disklabel od_disklabel;
82 struct dos_partition od_parttab[NDOSPART]; /* XXX needs to grow for extended partitions */
83#define BD_LABELOK (1<<3)
84#define BD_PARTTABOK (1<<4)
85};
86
87/*
88 * List of BIOS devices, translation from disk unit number to
89 * BIOS unit number.
90 */
91static struct bdinfo
92{
93 int bd_unit; /* BIOS unit number */
94 int bd_flags;
95 int bd_type; /* BIOS 'drive type' (floppy only) */
96} bdinfo [MAXBDDEV];
97static int nbdinfo = 0;
98
99static int bd_getgeom(struct open_disk *od);
100static int bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest);
101
102static int bd_int13probe(struct bdinfo *bd);
103
104static void bd_printslice(struct open_disk *od, int offset, char *prefix);
105
106static int bd_init(void);
107static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize);
108static int bd_realstrategy(void *devdata, int flag, daddr_t dblk, size_t size, void *buf, size_t *rsize);
109static int bd_open(struct open_file *f, ...);
110static int bd_close(struct open_file *f);
111static void bd_print(int verbose);
112
113struct devsw biosdisk = {
114 "disk",
115 DEVT_DISK,
116 bd_init,
117 bd_strategy,
118 bd_open,
119 bd_close,
120 noioctl,
121 bd_print
122};
123
124static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
125static void bd_closedisk(struct open_disk *od);
126static int bd_bestslice(struct dos_partition *dptr);
127
128/*
129 * Translate between BIOS device numbers and our private unit numbers.
130 */
131int
132bd_bios2unit(int biosdev)
133{
134 int i;
135
136 DEBUG("looking for bios device 0x%x", biosdev);
137 for (i = 0; i < nbdinfo; i++) {
138 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
139 if (bdinfo[i].bd_unit == biosdev)
140 return(i);
141 }
142 return(-1);
143}
144
145int
146bd_unit2bios(int unit)
147{
148 if ((unit >= 0) && (unit < nbdinfo))
149 return(bdinfo[unit].bd_unit);
150 return(-1);
151}
152
153/*
154 * Quiz the BIOS for disk devices, save a little info about them.
155 *
156 * XXX should we be consulting the BIOS equipment list, specifically
157 * the value at 0x475?
158 */
159static int
160bd_init(void)
161{
162 int base, unit;
163
164 /* sequence 0, 0x80 */
165 for (base = 0; base <= 0x80; base += 0x80) {
166 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
167 bdinfo[nbdinfo].bd_unit = unit;
168 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
169
170 /* XXX add EDD probes */
171 if (!bd_int13probe(&bdinfo[nbdinfo]))
172 break;
173
174 /* XXX we need "disk aliases" to make this simpler */
175 printf("BIOS drive %c: is disk%d\n",
176 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
177 nbdinfo++;
178 }
179 }
180 return(0);
181}
182
183/*
184 * Try to detect a device supported by the legacy int13 BIOS
185 */
186
187static int
188bd_int13probe(struct bdinfo *bd)
189{
190
191 v86.ctl = V86_FLAGS;
192 v86.addr = 0x13;
193 v86.eax = 0x800;
194 v86.edx = bd->bd_unit;
195 v86int();
196
197 if (!(v86.efl & 0x1) && /* carry clear */
198 ((v86.edx & 0xff) > (bd->bd_unit & 0x7f))) { /* unit # OK */
199 bd->bd_flags |= BD_MODEINT13;
200 bd->bd_type = v86.ebx & 0xff;
201 return(1);
202 }
203 return(0);
204}
205
206/*
207 * Print information about disks
208 */
209static void
210bd_print(int verbose)
211{
212 int i, j;
213 char line[80];
214 struct i386_devdesc dev;
215 struct open_disk *od;
216 struct dos_partition *dptr;
217
218 for (i = 0; i < nbdinfo; i++) {
219 sprintf(line, " disk%d: BIOS drive %c:\n", i,
220 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
221 pager_output(line);
222
223 /* try to open the whole disk */
224 dev.d_kind.biosdisk.unit = i;
225 dev.d_kind.biosdisk.slice = -1;
226 dev.d_kind.biosdisk.partition = -1;
227
228 if (!bd_opendisk(&od, &dev)) {
229
230 /* Do we have a partition table? */
231 if (od->od_flags & BD_PARTTABOK) {
232 dptr = &od->od_parttab[0];
233
234 /* Check for a "truly dedicated" disk */
235 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
236 (dptr[3].dp_start == 0) &&
237 (dptr[3].dp_size == 50000)) {
238 sprintf(line, " disk%d", i);
239 bd_printslice(od, 0, line);
240 } else {
241 for (j = 0; j < NDOSPART; j++) {
242 switch(dptr[j].dp_typ) {
243 case DOSPTYP_386BSD:
244 sprintf(line, " disk%ds%d", i, j + 1);
245 bd_printslice(od, dptr[j].dp_start, line);
246 break;
247 default:
248 }
249 }
250
251 }
252 }
253 bd_closedisk(od);
254 }
255 }
256}
257
258static void
259bd_printslice(struct open_disk *od, int offset, char *prefix)
260{
261 char line[80];
262 u_char buf[BIOSDISK_SECSIZE];
263 struct disklabel *lp;
264 int i;
265
266 /* read disklabel */
267 if (bd_read(od, offset + LABELSECTOR, 1, buf))
268 return;
269 lp =(struct disklabel *)(&buf[0]);
270 if (lp->d_magic != DISKMAGIC) {
271 sprintf(line, "bad disklabel\n");
272 pager_output(line);
273 return;
274 }
275
276 /* Print partitions */
277 for (i = 0; i < lp->d_npartitions; i++) {
278 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) || (lp->d_partitions[i].p_fstype == FS_SWAP) ||
279 ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
280 (od->od_flags & BD_FLOPPY) && (i == 0))) { /* Floppies often have bogus fstype, print 'a' */
281 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i,
282 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" : "FFS",
283 lp->d_partitions[i].p_size / 2048, /* 512-byte sector assumption */
284 lp->d_partitions[i].p_offset, lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
285 pager_output(line);
286 }
287 }
288}
289
290
291/*
292 * Attempt to open the disk described by (dev) for use by (f).
293 *
294 * Note that the philosophy here is "give them exactly what
295 * they ask for". This is necessary because being too "smart"
296 * about what the user might want leads to complications.
297 * (eg. given no slice or partition value, with a disk that is
298 * sliced - are they after the first BSD slice, or the DOS
299 * slice before it?)
300 */
301static int
302bd_open(struct open_file *f, ...)
303{
304 va_list ap;
305 struct i386_devdesc *dev;
306 struct open_disk *od;
307 int error;
308
309 va_start(ap, f);
310 dev = va_arg(ap, struct i386_devdesc *);
311 va_end(ap);
312 if ((error = bd_opendisk(&od, dev)))
313 return(error);
314
315 /*
316 * Save our context
317 */
318 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
319 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
320 return(0);
321}
322
323static int
324bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
325{
326 struct dos_partition *dptr;
327 struct disklabel *lp;
328 struct open_disk *od;
329 int sector, slice, i;
330 int error;
331 u_char buf[BUFSIZE];
332 daddr_t pref_slice[4];
333
334 if (dev->d_kind.biosdisk.unit >= nbdinfo) {
335 DEBUG("attempt to open nonexistent disk");
336 return(ENXIO);
337 }
338
339 od = (struct open_disk *)malloc(sizeof(struct open_disk));
340 if (!od) {
341 DEBUG("no memory");
342 return (ENOMEM);
343 }
344
345 /* Look up BIOS unit number, intialise open_disk structure */
346 od->od_dkunit = dev->d_kind.biosdisk.unit;
347 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
348 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
349 od->od_boff = 0;
350 error = 0;
351 DEBUG("open '%s', unit 0x%x slice %d partition %c",
352 i386_fmtdev(dev), dev->d_kind.biosdisk.unit,
353 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
354
355 /* Get geometry for this open (removable device may have changed) */
356 if (bd_getgeom(od)) {
357 DEBUG("can't get geometry");
358 error = ENXIO;
359 goto out;
360 }
361
362 /*
363 * Following calculations attempt to determine the correct value
364 * for d->od_boff by looking for the slice and partition specified,
365 * or searching for reasonable defaults.
366 */
367
368 /*
369 * Find the slice in the DOS slice table.
370 */
371 if (bd_read(od, 0, 1, buf)) {
372 DEBUG("error reading MBR");
373 error = EIO;
374 goto out;
375 }
376
377 /*
378 * Check the slice table magic.
379 */
380 if ((buf[0x1fe] != 0x55) || (buf[0x1ff] != 0xaa)) {
381 /* If a slice number was explicitly supplied, this is an error */
382 if (dev->d_kind.biosdisk.slice > 0) {
383 DEBUG("no slice table/MBR (no magic)");
384 error = ENOENT;
385 goto out;
386 }
387 sector = 0;
388 goto unsliced; /* may be a floppy */
389 }
390 bcopy(buf + DOSPARTOFF, &od->od_parttab, sizeof(struct dos_partition) * NDOSPART);
391 dptr = &od->od_parttab[0];
392 od->od_flags |= BD_PARTTABOK;
393
394 /* Is this a request for the whole disk? */
395 if (dev->d_kind.biosdisk.slice == -1) {
396 sector = 0;
397 goto unsliced;
398 }
399
400 /* Try to auto-detect the best slice; this should always give a slice number */
401 if (dev->d_kind.biosdisk.slice == 0)
402 dev->d_kind.biosdisk.slice = bd_bestslice(dptr);
403
404 switch (dev->d_kind.biosdisk.slice) {
405 case -1:
406 error = ENOENT;
407 goto out;
408 case 0:
409 sector = 0;
410 goto unsliced;
411 default:
412 break;
413 }
414
415 /*
416 * Accept the supplied slice number unequivocally (we may be looking
417 * at a DOS partition).
418 */
419 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
420 sector = dptr->dp_start;
421 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
422
423 /*
424 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
425 */
426 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
427 dev->d_kind.biosdisk.partition = 0;
428
429 unsliced:
430 /*
431 * Now we have the slice offset, look for the partition in the disklabel if we have
432 * a partition to start with.
433 *
434 * XXX we might want to check the label checksum.
435 */
436 if (dev->d_kind.biosdisk.partition < 0) {
437 od->od_boff = sector; /* no partition, must be after the slice */
438 DEBUG("opening raw slice");
439 } else {
440
441 if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
442 DEBUG("error reading disklabel");
443 error = EIO;
444 goto out;
445 }
446 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
447 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
448 lp = &od->od_disklabel;
449 od->od_flags |= BD_LABELOK;
450
451 if (lp->d_magic != DISKMAGIC) {
452 DEBUG("no disklabel");
453 error = ENOENT;
454 goto out;
455 }
456 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
457 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
458 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
459 error = EPART;
460 goto out;
461
462 }
463
464 /* Complain if the partition type is wrong */
465 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
466 !(od->od_flags & BD_FLOPPY)) /* Floppies often have bogus fstype */
467 DEBUG("warning, partition marked as unused");
468
469 od->od_boff = lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset;
470 }
471
472 out:
473 if (error) {
474 free(od);
475 } else {
476 *odp = od; /* return the open disk */
477 }
478 return(error);
479}
480
481
482/*
483 * Search for a slice with the following preferences:
484 *
485 * 1: Active FreeBSD slice
486 * 2: Non-active FreeBSD slice
487 * 3: Active FAT/FAT32 slice
488 * 4: non-active FAT/FAT32 slice
489 */
490#define PREF_FBSD_ACT 0
491#define PREF_FBSD 1
492#define PREF_DOS_ACT 2
493#define PREF_DOS 3
494#define PREF_NONE 4
495
496static int
497bd_bestslice(struct dos_partition *dptr)
498{
499 int i;
500 int preflevel, pref;
501
502
503 /*
504 * Check for the historically bogus MBR found on true dedicated disks
505 */
506 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
507 (dptr[3].dp_start == 0) &&
508 (dptr[3].dp_size == 50000))
509 return(0);
510
511 preflevel = PREF_NONE;
512 pref = -1;
513
514 /*
515 * XXX No support here for 'extended' slices
516 */
517 for (i = 0; i < NDOSPART; i++) {
518 switch(dptr[i].dp_typ) {
519 case DOSPTYP_386BSD: /* FreeBSD */
520 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_FBSD_ACT)) {
521 pref = i;
522 preflevel = PREF_FBSD_ACT;
523 } else if (preflevel > PREF_FBSD) {
524 pref = i;
525 preflevel = PREF_FBSD;
526 }
527 break;
528
529 case 0x04: /* DOS/Windows */
530 case 0x06:
531 case 0x0b:
532 case 0x0c:
533 case 0x0e:
534 case 0x63:
535 if ((dptr[i].dp_flag & 0x80) && (preflevel > PREF_DOS_ACT)) {
536 pref = i;
537 preflevel = PREF_DOS_ACT;
538 } else if (preflevel > PREF_DOS) {
539 pref = i;
540 preflevel = PREF_DOS;
541 }
542 break;
543 }
544 }
545 return(pref + 1); /* slices numbered 1-4 */
546}
547
548
549static int
550bd_close(struct open_file *f)
551{
552 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
553
554 bd_closedisk(od);
555 return(0);
556}
557
558static void
559bd_closedisk(struct open_disk *od)
560{
561 DEBUG("open_disk %p", od);
562#if 0
563 /* XXX is this required? (especially if disk already open...) */
564 if (od->od_flags & BD_FLOPPY)
565 delay(3000000);
566#endif
567 free(od);
568}
569
570static int
571bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
572{
573 struct bcache_devdata bcd;
574
575 bcd.dv_strategy = bd_realstrategy;
576 bcd.dv_devdata = devdata;
577 return(bcache_strategy(&bcd, rw, dblk, size, buf, rsize));
578}
579
580static int
581bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, void *buf, size_t *rsize)
582{
583 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
584 int blks;
585#ifdef BD_SUPPORT_FRAGS
586 char fragbuf[BIOSDISK_SECSIZE];
587 size_t fragsize;
588
589 fragsize = size % BIOSDISK_SECSIZE;
590#else
591 if (size % BIOSDISK_SECSIZE)
592 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
593#endif
594
595 DEBUG("open_disk %p", od);
596
597 if (rw != F_READ)
598 return(EROFS);
599
600
601 blks = size / BIOSDISK_SECSIZE;
602 DEBUG("read %d from %d+%d to %p", blks, od->od_boff, dblk, buf);
603
604 if (rsize)
605 *rsize = 0;
606 if (blks && bd_read(od, dblk + od->od_boff, blks, buf)) {
607 DEBUG("read error");
608 return (EIO);
609 }
610#ifdef BD_SUPPORT_FRAGS
611 DEBUG("bd_strategy: frag read %d from %d+%d+d to %p",
612 fragsize, od->od_boff, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
613 if (fragsize && bd_read(od, dblk + od->od_boff + blks, 1, fragsize)) {
614 DEBUG("frag read error");
615 return(EIO);
616 }
617 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
618#endif
619 if (rsize)
620 *rsize = size;
621 return (0);
622}
623
624/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
625#define FLOPPY_BOUNCEBUF 18
626
627static int
628bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
629{
630 int x, bpc, cyl, hd, sec, result, resid, cnt, retry, maxfer;
631 caddr_t p, xp, bbuf, breg;
632
633 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
634 resid = blks;
635 p = dest;
636
637 /* Decide whether we have to bounce */
638 if ((od->od_unit < 0x80) &&
639 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
640
641 /*
642 * There is a 64k physical boundary somewhere in the destination buffer, so we have
643 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
644 * need to. Use the bottom half unless there is a break there, in which case we
645 * use the top half.
646 */
647 x = min(FLOPPY_BOUNCEBUF, blks);
648 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
649 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(dest + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
650 breg = bbuf;
651 } else {
652 breg = bbuf + x * BIOSDISK_SECSIZE;
653 }
654 maxfer = x; /* limit transfers to bounce region size */
655 } else {
656 bbuf = NULL;
657 maxfer = 0;
658 }
659
660 while (resid > 0) {
661 x = dblk;
662 cyl = x / bpc; /* block # / blocks per cylinder */
663 x %= bpc; /* block offset into cylinder */
664 hd = x / od->od_sec; /* offset / blocks per track */
665 sec = x % od->od_sec; /* offset into track */
666
667 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
668 x = min(od->od_sec - sec, resid);
669 if (maxfer > 0)
670 x = min(x, maxfer); /* fit bounce buffer */
671
672 /* where do we transfer to? */
673 xp = bbuf == NULL ? p : breg;
674
675 /* correct sector number for 1-based BIOS numbering */
676 sec++;
677
678 /* Loop retrying the operation a couple of times. The BIOS may also retry. */
679 for (retry = 0; retry < 3; retry++) {
680 /* if retrying, reset the drive */
681 if (retry > 0) {
682 v86.ctl = V86_FLAGS;
683 v86.addr = 0x13;
684 v86.eax = 0;
685 v86.edx = od->od_unit;
686 v86int();
687 }
688
689 /* build request XXX support EDD requests too */
690 v86.ctl = V86_FLAGS;
691 v86.addr = 0x13;
692 v86.eax = 0x200 | x;
693 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
694 v86.edx = (hd << 8) | od->od_unit;
695 v86.es = VTOPSEG(xp);
696 v86.ebx = VTOPOFF(xp);
697 v86int();
698 result = (v86.efl & 0x1);
699 if (result == 0)
700 break;
701 }
702
703 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
704 /* BUG here, cannot use v86 in printf because putchar uses it too */
705 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
706 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
707 if (result) {
708 if (bbuf != NULL)
709 free(bbuf);
710 return(-1);
711 }
712 if (bbuf != NULL)
713 bcopy(breg, p, x * BIOSDISK_SECSIZE);
714 p += (x * BIOSDISK_SECSIZE);
715 dblk += x;
716 resid -= x;
717 }
718
719/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
720 if (bbuf != NULL)
721 free(bbuf);
722 return(0);
723}
724
725static int
726bd_getgeom(struct open_disk *od)
727{
728
729 v86.ctl = V86_FLAGS;
730 v86.addr = 0x13;
731 v86.eax = 0x800;
732 v86.edx = od->od_unit;
733 v86int();
734
735 if ((v86.efl & 0x1) || /* carry set */
736 ((v86.edx & 0xff) <= (od->od_unit & 0x7f))) /* unit # bad */
737 return(1);
738
739 /* convert max cyl # -> # of cylinders */
740 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
741 /* convert max head # -> # of heads */
742 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
743 od->od_sec = v86.ecx & 0x3f;
744
745 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
746 return(0);
747}
748
749/*
750 * Return a suitable dev_t value for (dev).
751 *
752 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
753 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
754 */
755int
756bd_getdev(struct i386_devdesc *dev)
757{
758 struct open_disk *od;
759 int biosdev;
760 int major;
761 int rootdev;
762 char *nip, *cp;
763 int unitofs = 0, i, unit;
764
765 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
766 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
767 if (biosdev == -1) /* not a BIOS device */
768 return(-1);
769 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
770 return(-1);
771
772 if (biosdev < 0x80) {
773 /* floppy (or emulated floppy) or ATAPI device */
774 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
775 /* is an ATAPI disk */
776 major = WFDMAJOR;
777 } else {
778 /* is a floppy disk */
779 major = FDMAJOR;
780 }
781 } else {
782 /* harddisk */
783 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
784 /* label OK, disk labelled as SCSI */
785 major = DAMAJOR;
786 /* check for unit number correction hint, now deprecated */
787 if ((nip = getenv("num_ide_disks")) != NULL) {
788 i = strtol(nip, &cp, 0);
789 /* check for parse error */
790 if ((cp != nip) && (*cp == 0))
791 unitofs = i;
792 }
793 } else {
794 /* assume an IDE disk */
795 major = WDMAJOR;
796 }
797 }
798 /* XXX a better kludge to set the root disk unit number */
799 if ((nip = getenv("root_disk_unit")) != NULL) {
800 i = strtol(nip, &cp, 0);
801 /* check for parse error */
802 if ((cp != nip) && (*cp == 0))
803 unit = i;
804 } else {
805 unit = (biosdev & 0x7f) - unitofs; /* allow for #wd compenstation in da case */
806 }
807
808 rootdev = MAKEBOOTDEV(major,
809 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */
810 (dev->d_kind.biosdisk.slice + 1) & 0xf,
811 unit,
812 dev->d_kind.biosdisk.partition);
813 DEBUG("dev is 0x%x\n", rootdev);
814 return(rootdev);
815}
816
817/*
818 * Fix (dev) so that it refers to the 'real' disk/slice/partition that it implies.
819 */
820int
821bd_fixupdev(struct i386_devdesc *dev)
822{
823 struct open_disk *od;
824
825 /*
826 * Open the disk. This will fix up the slice and partition fields.
827 */
828 if (bd_opendisk(&od, dev) != 0)
829 return(ENOENT);
830
831 bd_closedisk(od);
832}