Add more bits for native hammer boot support.
[dragonfly.git] / sys / boot / pc32 / libi386 / biosdisk.c
CommitLineData
984263bc
MD
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 *
c7e270b1 26 * $FreeBSD: src/sys/boot/i386/libi386/biosdisk.c,v 1.45 2004/09/21 06:46:44 wes Exp $
2b961883 27 * $DragonFly: src/sys/boot/pc32/libi386/biosdisk.c,v 1.12 2007/06/18 05:13:42 dillon Exp $
984263bc
MD
28 */
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
2b961883 42#include <sys/disklabel32.h>
6080181b 43#include <sys/disklabel64.h>
5ee58eed 44#include <sys/diskmbr.h>
ba0cc1ab 45#include <sys/dtype.h>
5ee58eed 46#include <machine/bootinfo.h>
984263bc
MD
47
48#include <stdarg.h>
49
50#include <bootstrap.h>
51#include <btxv86.h>
52#include "libi386.h"
53
54#define BIOS_NUMDRIVES 0x475
55#define BIOSDISK_SECSIZE 512
6080181b 56#define BUFSIZE (4 * BIOSDISK_SECSIZE)
984263bc
MD
57#define MAXBDDEV MAXDEV
58
5ee58eed
MD
59#define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
60#define WDMAJOR 0 /* major numbers for devices we frontend for */
61#define WFDMAJOR 1
62#define FDMAJOR 2
63#define DAMAJOR 4
984263bc
MD
64
65#ifdef DISK_DEBUG
5ee58eed 66# define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
984263bc
MD
67#else
68# define DEBUG(fmt, args...)
69#endif
70
71struct open_disk {
72 int od_dkunit; /* disk unit number */
73 int od_unit; /* BIOS unit number */
74 int od_cyl; /* BIOS geometry */
75 int od_hds;
76 int od_sec;
77 int od_boff; /* block offset from beginning of BIOS disk */
78 int od_flags;
79#define BD_MODEINT13 0x0000
80#define BD_MODEEDD1 0x0001
81#define BD_MODEEDD3 0x0002
82#define BD_MODEMASK 0x0003
83#define BD_FLOPPY 0x0004
84#define BD_LABELOK 0x0008
85#define BD_PARTTABOK 0x0010
6080181b
SS
86 union {
87 struct disklabel32 od_disklabel;
88 struct disklabel64 od_disklabel64;
89 };
984263bc 90 int od_nslices; /* slice count */
5ee58eed 91 struct dos_partition od_slicetab[NEXTDOSPART];
984263bc
MD
92};
93
94/*
95 * List of BIOS devices, translation from disk unit number to
96 * BIOS unit number.
97 */
98static struct bdinfo
99{
100 int bd_unit; /* BIOS unit number */
101 int bd_flags;
102 int bd_type; /* BIOS 'drive type' (floppy only) */
103} bdinfo [MAXBDDEV];
104static int nbdinfo = 0;
105
106static int bd_getgeom(struct open_disk *od);
107static int bd_read(struct open_disk *od, daddr_t dblk, int blks,
108 caddr_t dest);
5ee58eed
MD
109static int bd_write(struct open_disk *od, daddr_t dblk, int blks,
110 caddr_t dest);
984263bc
MD
111
112static int bd_int13probe(struct bdinfo *bd);
113
114static void bd_printslice(struct open_disk *od, struct dos_partition *dp,
115 char *prefix, int verbose);
116static void bd_printbsdslice(struct open_disk *od, daddr_t offset,
117 char *prefix, int verbose);
118
119static int bd_init(void);
120static int bd_strategy(void *devdata, int flag, daddr_t dblk,
121 size_t size, char *buf, size_t *rsize);
122static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
123 size_t size, char *buf, size_t *rsize);
124static int bd_open(struct open_file *f, ...);
125static int bd_close(struct open_file *f);
126static void bd_print(int verbose);
127
128struct devsw biosdisk = {
129 "disk",
130 DEVT_DISK,
131 bd_init,
132 bd_strategy,
133 bd_open,
134 bd_close,
135 noioctl,
136 bd_print,
137 NULL
138};
139
140static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
141static void bd_closedisk(struct open_disk *od);
142static int bd_bestslice(struct open_disk *od);
7755d172 143static void bd_chainextended(struct open_disk *od, u_int32_t base, u_int32_t offset);
984263bc
MD
144
145/*
146 * Translate between BIOS device numbers and our private unit numbers.
147 */
148int
149bd_bios2unit(int biosdev)
150{
151 int i;
152
153 DEBUG("looking for bios device 0x%x", biosdev);
154 for (i = 0; i < nbdinfo; i++) {
155 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
156 if (bdinfo[i].bd_unit == biosdev)
157 return(i);
158 }
159 return(-1);
160}
161
162int
163bd_unit2bios(int unit)
164{
165 if ((unit >= 0) && (unit < nbdinfo))
166 return(bdinfo[unit].bd_unit);
167 return(-1);
168}
169
170/*
171 * Quiz the BIOS for disk devices, save a little info about them.
172 */
173static int
174bd_init(void)
175{
176 int base, unit, nfd = 0;
177
178 /* sequence 0, 0x80 */
179 for (base = 0; base <= 0x80; base += 0x80) {
180 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
181 /* check the BIOS equipment list for number of fixed disks */
182 if((base == 0x80) &&
183 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
184 break;
185
186 bdinfo[nbdinfo].bd_unit = unit;
187 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
188
189 if (!bd_int13probe(&bdinfo[nbdinfo]))
190 break;
191
192 /* XXX we need "disk aliases" to make this simpler */
193 printf("BIOS drive %c: is disk%d\n",
194 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
195 nbdinfo++;
196 if (base == 0x80)
197 nfd++;
198 }
199 }
200 return(0);
201}
202
203/*
204 * Try to detect a device supported by the legacy int13 BIOS
205 */
206static int
207bd_int13probe(struct bdinfo *bd)
208{
209 v86.ctl = V86_FLAGS;
210 v86.addr = 0x13;
211 v86.eax = 0x800;
212 v86.edx = bd->bd_unit;
213 v86int();
214
215 if (!(v86.efl & 0x1) && /* carry clear */
216 ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) { /* unit # OK */
c7e270b1
MD
217
218 /*
219 * Ignore devices with an absurd sector size.
220 */
221 if ((v86.ecx & 0x3f) == 0) {
222 DEBUG("Invalid geometry for unit %d", bd->bd_unit);
223 return(0);
224 }
984263bc
MD
225 bd->bd_flags |= BD_MODEINT13;
226 bd->bd_type = v86.ebx & 0xff;
227
228 /* Determine if we can use EDD with this device. */
229 v86.eax = 0x4100;
230 v86.edx = bd->bd_unit;
231 v86.ebx = 0x55aa;
232 v86int();
233 if (!(v86.efl & 0x1) && /* carry clear */
234 ((v86.ebx & 0xffff) == 0xaa55) && /* signature */
235 (v86.ecx & 0x1)) { /* packets mode ok */
236 bd->bd_flags |= BD_MODEEDD1;
237 if((v86.eax & 0xff00) > 0x300)
238 bd->bd_flags |= BD_MODEEDD3;
239 }
240 return(1);
241 }
242 return(0);
243}
244
245/*
246 * Print information about disks
247 */
248static void
249bd_print(int verbose)
250{
251 int i, j;
252 char line[80];
253 struct i386_devdesc dev;
254 struct open_disk *od;
255 struct dos_partition *dptr;
256
257 for (i = 0; i < nbdinfo; i++) {
258 sprintf(line, " disk%d: BIOS drive %c:\n", i,
259 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
260 pager_output(line);
261
262 /* try to open the whole disk */
263 dev.d_kind.biosdisk.unit = i;
264 dev.d_kind.biosdisk.slice = -1;
265 dev.d_kind.biosdisk.partition = -1;
266
267 if (!bd_opendisk(&od, &dev)) {
268
269 /* Do we have a partition table? */
270 if (od->od_flags & BD_PARTTABOK) {
271 dptr = &od->od_slicetab[0];
272
273 /* Check for a "dedicated" disk */
274 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
275 (dptr[3].dp_start == 0) &&
276 (dptr[3].dp_size == 50000)) {
277 sprintf(line, " disk%d", i);
278 bd_printbsdslice(od, 0, line, verbose);
279 } else {
280 for (j = 0; j < od->od_nslices; j++) {
281 sprintf(line, " disk%ds%d", i, j + 1);
282 bd_printslice(od, &dptr[j], line, verbose);
283 }
284 }
285 }
286 bd_closedisk(od);
287 }
288 }
289}
290
291/*
292 * Print information about slices on a disk. For the size calculations we
293 * assume a 512 byte sector.
294 */
295static void
296bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
297 int verbose)
298{
299 char line[80];
300
301 switch (dp->dp_typ) {
302 case DOSPTYP_386BSD:
303 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
304 return;
305 case DOSPTYP_LINSWP:
306 if (verbose)
307 sprintf(line, "%s: Linux swap %.6dMB (%d - %d)\n",
308 prefix, dp->dp_size / 2048,
309 dp->dp_start, dp->dp_start + dp->dp_size);
310 else
311 sprintf(line, "%s: Linux swap\n", prefix);
312 break;
313 case DOSPTYP_LINUX:
314 /*
315 * XXX
316 * read the superblock to confirm this is an ext2fs partition?
317 */
318 if (verbose)
319 sprintf(line, "%s: ext2fs %.6dMB (%d - %d)\n", prefix,
320 dp->dp_size / 2048, dp->dp_start,
321 dp->dp_start + dp->dp_size);
322 else
323 sprintf(line, "%s: ext2fs\n", prefix);
324 break;
325 case 0x00: /* unused partition */
326 case DOSPTYP_EXT:
327 return;
328 case 0x01:
329 if (verbose)
330 sprintf(line, "%s: FAT-12 %.6dMB (%d - %d)\n", prefix,
331 dp->dp_size / 2048, dp->dp_start,
332 dp->dp_start + dp->dp_size);
333 else
334 sprintf(line, "%s: FAT-12\n", prefix);
335 break;
336 case 0x04:
337 case 0x06:
338 case 0x0e:
339 if (verbose)
340 sprintf(line, "%s: FAT-16 %.6dMB (%d - %d)\n", prefix,
341 dp->dp_size / 2048, dp->dp_start,
342 dp->dp_start + dp->dp_size);
343 else
344 sprintf(line, "%s: FAT-16\n", prefix);
345 break;
346 case 0x0b:
347 case 0x0c:
348 if (verbose)
349 sprintf(line, "%s: FAT-32 %.6dMB (%d - %d)\n", prefix,
350 dp->dp_size / 2048, dp->dp_start,
351 dp->dp_start + dp->dp_size);
352 else
353 sprintf(line, "%s: FAT-32\n", prefix);
354 break;
355 default:
356 if (verbose)
357 sprintf(line, "%s: Unknown fs: 0x%x %.6dMB (%d - %d)\n",
358 prefix, dp->dp_typ, dp->dp_size / 2048,
359 dp->dp_start, dp->dp_start + dp->dp_size);
360 else
361 sprintf(line, "%s: Unknown fs: 0x%x\n", prefix,
362 dp->dp_typ);
363 }
364 pager_output(line);
365}
366
367/*
368 * Print out each valid partition in the disklabel of a FreeBSD slice.
369 * For size calculations, we assume a 512 byte sector size.
370 */
371static void
372bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
373 int verbose)
374{
375 char line[80];
376 char buf[BIOSDISK_SECSIZE];
2b961883 377 struct disklabel32 *lp;
984263bc
MD
378 int i;
379
380 /* read disklabel */
2b961883 381 if (bd_read(od, offset + LABELSECTOR32, 1, buf))
984263bc 382 return;
2b961883
MD
383 lp =(struct disklabel32 *)(&buf[0]);
384 if (lp->d_magic != DISKMAGIC32) {
984263bc
MD
385 sprintf(line, "%s: FFS bad disklabel\n", prefix);
386 pager_output(line);
387 return;
388 }
389
390 /* Print partitions */
391 for (i = 0; i < lp->d_npartitions; i++) {
392 /*
393 * For each partition, make sure we know what type of fs it is. If
394 * not, then skip it. However, since floppies often have bogus
395 * fstypes, print the 'a' partition on a floppy even if it is marked
396 * unused.
397 */
398 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
399 (lp->d_partitions[i].p_fstype == FS_SWAP) ||
400 (lp->d_partitions[i].p_fstype == FS_VINUM) ||
401 ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
402 (od->od_flags & BD_FLOPPY) && (i == 0))) {
403
404 /* Only print out statistics in verbose mode */
405 if (verbose)
406 sprintf(line, " %s%c: %s %.6dMB (%d - %d)\n", prefix, 'a' + i,
407 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
408 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
409 "FFS",
410 lp->d_partitions[i].p_size / 2048,
411 lp->d_partitions[i].p_offset,
412 lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
413 else
414 sprintf(line, " %s%c: %s\n", prefix, 'a' + i,
415 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
416 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
417 "FFS");
418 pager_output(line);
419 }
420 }
421}
422
423
424/*
425 * Attempt to open the disk described by (dev) for use by (f).
426 *
427 * Note that the philosophy here is "give them exactly what
428 * they ask for". This is necessary because being too "smart"
429 * about what the user might want leads to complications.
430 * (eg. given no slice or partition value, with a disk that is
431 * sliced - are they after the first BSD slice, or the DOS
432 * slice before it?)
433 */
434static int
435bd_open(struct open_file *f, ...)
436{
5ee58eed 437 va_list ap;
984263bc
MD
438 struct i386_devdesc *dev;
439 struct open_disk *od;
440 int error;
441
5ee58eed
MD
442 va_start(ap, f);
443 dev = va_arg(ap, struct i386_devdesc *);
444 va_end(ap);
984263bc
MD
445 if ((error = bd_opendisk(&od, dev)))
446 return(error);
447
448 /*
449 * Save our context
450 */
451 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
452 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
453 return(0);
454}
455
456static int
457bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
458{
459 struct dos_partition *dptr;
2b961883 460 struct disklabel32 *lp;
6080181b 461 struct disklabel64 *lp64;
984263bc
MD
462 struct open_disk *od;
463 int sector, slice, i;
464 int error;
6080181b 465 static char buf[BUFSIZE];
984263bc
MD
466
467 if (dev->d_kind.biosdisk.unit >= nbdinfo) {
468 DEBUG("attempt to open nonexistent disk");
469 return(ENXIO);
470 }
471
472 od = (struct open_disk *)malloc(sizeof(struct open_disk));
473 if (!od) {
474 DEBUG("no memory");
475 return (ENOMEM);
476 }
477
478 /* Look up BIOS unit number, intialise open_disk structure */
479 od->od_dkunit = dev->d_kind.biosdisk.unit;
480 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
481 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
482 od->od_boff = 0;
483 od->od_nslices = 0;
484 error = 0;
485 DEBUG("open '%s', unit 0x%x slice %d partition %c",
486 i386_fmtdev(dev), dev->d_kind.biosdisk.unit,
487 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition + 'a');
488
489 /* Get geometry for this open (removable device may have changed) */
490 if (bd_getgeom(od)) {
491 DEBUG("can't get geometry");
492 error = ENXIO;
493 goto out;
494 }
495
496 /*
497 * Following calculations attempt to determine the correct value
498 * for d->od_boff by looking for the slice and partition specified,
499 * or searching for reasonable defaults.
500 */
501
502 /*
503 * Find the slice in the DOS slice table.
504 */
505 if (bd_read(od, 0, 1, buf)) {
506 DEBUG("error reading MBR");
507 error = EIO;
508 goto out;
509 }
510
511 /*
512 * Check the slice table magic.
513 */
514 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
515 /* If a slice number was explicitly supplied, this is an error */
516 if (dev->d_kind.biosdisk.slice > 0) {
517 DEBUG("no slice table/MBR (no magic)");
518 error = ENOENT;
519 goto out;
520 }
521 sector = 0;
522 goto unsliced; /* may be a floppy */
523 }
524
525 /*
7755d172
MD
526 * copy the partition table, then pick up any extended partitions. The
527 * base partition table always has four entries, even if some of them
528 * represented extended partitions. However, any additional sub-extended
529 * partitions will be silently recursed and not included in the slice
530 * table.
984263bc
MD
531 */
532 bcopy(buf + DOSPARTOFF, &od->od_slicetab,
533 sizeof(struct dos_partition) * NDOSPART);
7755d172
MD
534 od->od_nslices = NDOSPART;
535
536 dptr = &od->od_slicetab[0];
537 for (i = 0; i < NDOSPART; i++, dptr++) {
538 if ((dptr->dp_typ == DOSPTYP_EXT) || (dptr->dp_typ == DOSPTYP_EXTLBA))
539 bd_chainextended(od, dptr->dp_start, 0); /* 1st offset is zero */
540 }
984263bc
MD
541 od->od_flags |= BD_PARTTABOK;
542 dptr = &od->od_slicetab[0];
543
7755d172
MD
544 /*
545 * Overflow entries are not loaded into memory but we still keep
546 * track of the count. Fix it up now.
547 */
548 if (od->od_nslices > NEXTDOSPART)
549 od->od_nslices = NEXTDOSPART;
550
551 /*
552 * Is this a request for the whole disk?
553 */
984263bc
MD
554 if (dev->d_kind.biosdisk.slice == -1) {
555 sector = 0;
556 goto unsliced;
557 }
558
559 /*
560 * if a slice number was supplied but not found, this is an error.
561 */
562 if (dev->d_kind.biosdisk.slice > 0) {
563 slice = dev->d_kind.biosdisk.slice - 1;
564 if (slice >= od->od_nslices) {
565 DEBUG("slice %d not found", slice);
566 error = ENOENT;
567 goto out;
568 }
569 }
570
571 /*
572 * Check for the historically bogus MBR found on true dedicated disks
573 */
574 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
575 (dptr[3].dp_start == 0) &&
576 (dptr[3].dp_size == 50000)) {
577 sector = 0;
578 goto unsliced;
579 }
580
581 /* Try to auto-detect the best slice; this should always give a slice number */
582 if (dev->d_kind.biosdisk.slice == 0) {
583 slice = bd_bestslice(od);
584 if (slice == -1) {
585 error = ENOENT;
586 goto out;
587 }
588 dev->d_kind.biosdisk.slice = slice;
589 }
590
591 dptr = &od->od_slicetab[0];
592 /*
593 * Accept the supplied slice number unequivocally (we may be looking
594 * at a DOS partition).
595 */
596 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
597 sector = dptr->dp_start;
598 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
599
600 /*
601 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
602 */
603 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
604 dev->d_kind.biosdisk.partition = 0;
605
606 unsliced:
607 /*
608 * Now we have the slice offset, look for the partition in the disklabel if we have
609 * a partition to start with.
610 *
611 * XXX we might want to check the label checksum.
612 */
613 if (dev->d_kind.biosdisk.partition < 0) {
614 od->od_boff = sector; /* no partition, must be after the slice */
615 DEBUG("opening raw slice");
616 } else {
617
2b961883 618 if (bd_read(od, sector + LABELSECTOR32, 1, buf)) {
984263bc
MD
619 DEBUG("error reading disklabel");
620 error = EIO;
621 goto out;
622 }
2b961883
MD
623 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel32), buf + LABELOFFSET32, &od->od_disklabel);
624 bcopy(buf + LABELOFFSET32, &od->od_disklabel, sizeof(struct disklabel32));
984263bc 625 lp = &od->od_disklabel;
984263bc 626
6080181b
SS
627 if (lp->d_magic == DISKMAGIC32) {
628 od->od_flags |= BD_LABELOK;
629
630 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
631 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
632 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
633 error = EPART;
634 goto out;
635
636 }
637
638#ifdef DISK_DEBUG
639 /* Complain if the partition is unused unless this is a floppy. */
640 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
641 !(od->od_flags & BD_FLOPPY))
642 DEBUG("warning, partition marked as unused");
643#endif
644
645 od->od_boff =
646 lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
647 lp->d_partitions[RAW_PART].p_offset +
648 sector;
649
984263bc
MD
650 goto out;
651 }
984263bc 652
6080181b
SS
653 /* else maybe DISKLABEL64? */
654
655 if (bd_read(od, sector, (sizeof(od->od_disklabel64) + 511) / 512, buf)) {
656 DEBUG("error reading disklabel");
657 error = EIO;
658 goto out;
984263bc 659 }
6080181b
SS
660 DEBUG("copy %d bytes of label from %p to %p", sizeof(od->od_disklabel64), buf, &od->od_disklabel64);
661 bcopy(buf, &od->od_disklabel64, sizeof(od->od_disklabel64));
662 lp64 = &od->od_disklabel64;
984263bc 663
6080181b
SS
664 if (lp64->d_magic == DISKMAGIC64) {
665 od->od_flags |= BD_LABELOK;
666
667 if (dev->d_kind.biosdisk.partition >= lp64->d_npartitions ||
668 lp64->d_partitions[dev->d_kind.biosdisk.partition].p_bsize == 0) {
669 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
670 'a' + dev->d_kind.biosdisk.partition, 'a' + lp64->d_npartitions);
671 error = EPART;
672 goto out;
673
674 }
675
676 od->od_boff =
677 lp64->d_partitions[dev->d_kind.biosdisk.partition].p_boffset / 512 +
678 sector;
679
680 DEBUG("disklabel64 slice at %d", od->od_boff);
681
682 } else {
683 DEBUG("no disklabel");
684 error = ENOENT;
685 }
984263bc 686 }
6080181b 687
984263bc
MD
688 out:
689 if (error) {
690 free(od);
691 } else {
692 *odp = od; /* return the open disk */
693 }
694 return(error);
695}
696
7755d172 697
984263bc 698static void
7755d172 699bd_chainextended(struct open_disk *od, u_int32_t base, u_int32_t offset)
984263bc 700{
7755d172
MD
701 char buf[BIOSDISK_SECSIZE];
702 struct dos_partition *dp1, *dp2;
703 int i;
704
705 if (bd_read(od, (daddr_t)(base + offset), 1, buf)) {
706 printf("\nerror reading extended partition table");
707 return;
984263bc 708 }
7755d172
MD
709
710 /*
711 * dp1 points to the first record in the on-disk XPT,
712 * dp2 points to the next entry in the in-memory parition table.
713 *
714 * NOTE: dp2 may be out of bounds if od_nslices >= NEXTDOSPART.
715 *
716 * NOTE: unlike the extended partitions in our primary dos partition
717 * table, we do not record recursed extended partitions themselves
718 * in our in-memory partition table.
719 *
720 * NOTE: recording within our in-memory table must be breadth first
721 * ot match what the kernel does. Thus, two passes are required.
722 *
723 * NOTE: partitioning programs which support extended partitions seem
724 * to always use the first entry for the user partition and the
725 * second entry to chain, and also appear to disallow more then one
726 * extended partition at each level. Nevertheless we make our code
727 * somewhat more generic (and the same as the kernel's own slice
728 * scan).
729 */
730 dp1 = (struct dos_partition *)(&buf[DOSPARTOFF]);
731 dp2 = &od->od_slicetab[od->od_nslices];
732
733 for (i = 0; i < NDOSPART; ++i, ++dp1) {
734 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
735 dp1->dp_ssect == 0 && dp1->dp_start == 0 &&
736 dp1->dp_size == 0) {
984263bc 737 continue;
7755d172
MD
738 }
739 if ((dp1->dp_typ == DOSPTYP_EXT) ||
740 (dp1->dp_typ == DOSPTYP_EXTLBA)) {
741 /*
742 * breadth first traversal, must skip in the
743 * first pass
744 */
745 continue;
746 }
747
748 /*
749 * Only load up the in-memory data if we haven't overflowed
750 * our in-memory array.
751 */
752 if (od->od_nslices < NEXTDOSPART) {
753 dp2->dp_typ = dp1->dp_typ;
754 dp2->dp_start = base + offset + dp1->dp_start;
755 dp2->dp_size = dp1->dp_size;
756 }
757 ++od->od_nslices;
758 ++dp2;
984263bc 759 }
984263bc
MD
760
761 /*
7755d172
MD
762 * Pass 2 - handle extended partitions. Note that the extended
763 * slice itself is not stored in the slice array when we recurse,
764 * but any 'original' top-level extended slices are. This is to
765 * match what the kernel does.
984263bc 766 */
7755d172
MD
767 dp1 -= NDOSPART;
768 for (i = 0; i < NDOSPART; ++i, ++dp1) {
769 if (dp1->dp_scyl == 0 && dp1->dp_shd == 0 &&
770 dp1->dp_ssect == 0 && dp1->dp_start == 0 &&
771 dp1->dp_size == 0) {
772 continue;
773 }
774 if ((dp1->dp_typ == DOSPTYP_EXT) ||
775 (dp1->dp_typ == DOSPTYP_EXTLBA)) {
776 bd_chainextended(od, base, dp1->dp_start);
777 continue;
778 }
779 }
984263bc
MD
780}
781
782/*
783 * Search for a slice with the following preferences:
784 *
785 * 1: Active FreeBSD slice
786 * 2: Non-active FreeBSD slice
787 * 3: Active Linux slice
788 * 4: non-active Linux slice
789 * 5: Active FAT/FAT32 slice
790 * 6: non-active FAT/FAT32 slice
791 */
792#define PREF_RAWDISK 0
793#define PREF_FBSD_ACT 1
794#define PREF_FBSD 2
795#define PREF_LINUX_ACT 3
796#define PREF_LINUX 4
797#define PREF_DOS_ACT 5
798#define PREF_DOS 6
799#define PREF_NONE 7
800
801/*
802 * slicelimit is in the range 0 .. NDOSPART
803 */
804static int
805bd_bestslice(struct open_disk *od)
806{
807 struct dos_partition *dp;
808 int pref, preflevel;
809 int i, prefslice;
810
811 prefslice = 0;
812 preflevel = PREF_NONE;
813
814 dp = &od->od_slicetab[0];
815 for (i = 0; i < od->od_nslices; i++, dp++) {
984263bc
MD
816 switch (dp->dp_typ) {
817 case DOSPTYP_386BSD: /* FreeBSD */
818 pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
819 break;
820
821 case DOSPTYP_LINUX:
822 pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
823 break;
824
825 case 0x01: /* DOS/Windows */
826 case 0x04:
827 case 0x06:
828 case 0x0b:
829 case 0x0c:
830 case 0x0e:
831 pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
832 break;
833
834 default:
835 pref = PREF_NONE;
836 }
837 if (pref < preflevel) {
838 preflevel = pref;
839 prefslice = i + 1;
840 }
841 }
842 return (prefslice);
843}
844
845static int
846bd_close(struct open_file *f)
847{
848 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
849
850 bd_closedisk(od);
851 return(0);
852}
853
854static void
855bd_closedisk(struct open_disk *od)
856{
857 DEBUG("open_disk %p", od);
858#if 0
859 /* XXX is this required? (especially if disk already open...) */
860 if (od->od_flags & BD_FLOPPY)
861 delay(3000000);
862#endif
863 free(od);
864}
865
866static int
867bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
868{
869 struct bcache_devdata bcd;
870 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
871
872 bcd.dv_strategy = bd_realstrategy;
873 bcd.dv_devdata = devdata;
874 return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
875}
876
877static int
878bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
879{
880 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
881 int blks;
882#ifdef BD_SUPPORT_FRAGS
883 char fragbuf[BIOSDISK_SECSIZE];
884 size_t fragsize;
885
886 fragsize = size % BIOSDISK_SECSIZE;
887#else
888 if (size % BIOSDISK_SECSIZE)
889 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
890#endif
891
892 DEBUG("open_disk %p", od);
893
5644a38d
MD
894 switch(rw){
895 case F_READ:
896 blks = size / BIOSDISK_SECSIZE;
897 DEBUG("read %d from %d to %p", blks, dblk, buf);
984263bc 898
5644a38d
MD
899 if (rsize)
900 *rsize = 0;
901 if (blks && bd_read(od, dblk, blks, buf)) {
902 DEBUG("read error");
903 return (EIO);
904 }
984263bc 905#ifdef BD_SUPPORT_FRAGS
5644a38d 906 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
984263bc 907 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
5644a38d
MD
908 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
909 DEBUG("frag read error");
910 return(EIO);
911 }
912 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
984263bc 913#endif
5644a38d
MD
914 if (rsize)
915 *rsize = size;
916 return (0);
917 case F_WRITE :
918 blks = size / BIOSDISK_SECSIZE;
919 DEBUG("write %d from %d to %p", blks, dblk, buf);
920
921 if (rsize)
922 *rsize = 0;
923 if (blks && bd_write(od, dblk, blks, buf)) {
924 DEBUG("write error");
925 return (EIO);
926 }
5ee58eed
MD
927#ifdef BD_SUPPORT_FRAGS
928 if(fragsize) {
5644a38d
MD
929 DEBUG("Attempted to write a frag");
930 return (EIO);
5ee58eed
MD
931 }
932#endif
5644a38d
MD
933 if (rsize)
934 *rsize = size;
935 return (0);
936 default:
937 break; /* DO NOTHING */
938 }
939 return EROFS;
984263bc
MD
940}
941
942/* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
943#define FLOPPY_BOUNCEBUF 18
944
945static int
946bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
947{
948 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
949 caddr_t p, xp, bbuf, breg;
950
951 /* Just in case some idiot actually tries to read -1 blocks... */
952 if (blks < 0)
953 return (-1);
954
955 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
956 resid = blks;
957 p = dest;
958
959 /* Decide whether we have to bounce */
960 if ((od->od_unit < 0x80) &&
961 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
962
963 /*
964 * There is a 64k physical boundary somewhere in the destination buffer, so we have
965 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
966 * need to. Use the bottom half unless there is a break there, in which case we
967 * use the top half.
968 */
969 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
970 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
c7e270b1 971 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
984263bc
MD
972 breg = bbuf;
973 } else {
974 breg = bbuf + x * BIOSDISK_SECSIZE;
975 }
976 maxfer = x; /* limit transfers to bounce region size */
977 } else {
978 breg = bbuf = NULL;
979 maxfer = 0;
980 }
981
982 while (resid > 0) {
983 x = dblk;
984 cyl = x / bpc; /* block # / blocks per cylinder */
985 x %= bpc; /* block offset into cylinder */
986 hd = x / od->od_sec; /* offset / blocks per track */
987 sec = x % od->od_sec; /* offset into track */
988
989 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
990 x = min(od->od_sec - sec, resid);
991 if (maxfer > 0)
992 x = min(x, maxfer); /* fit bounce buffer */
993
994 /* where do we transfer to? */
995 xp = bbuf == NULL ? p : breg;
996
997 /* correct sector number for 1-based BIOS numbering */
998 sec++;
999
ed987dc9
MD
1000 /*
1001 * Loop retrying the operation a couple of times. The BIOS may also
1002 * retry.
1003 */
984263bc 1004 for (retry = 0; retry < 3; retry++) {
ed987dc9
MD
1005 /*
1006 * If retrying, reset the drive.
1007 */
984263bc
MD
1008 if (retry > 0) {
1009 v86.ctl = V86_FLAGS;
1010 v86.addr = 0x13;
1011 v86.eax = 0;
1012 v86.edx = od->od_unit;
1013 v86int();
1014 }
1015
ed987dc9
MD
1016 /*
1017 * Always use EDD if the disk supports it, otherwise fall back
1018 * to CHS mode (returning an error if the cylinder number is
1019 * too large).
1020 */
1021 if (od->od_flags & BD_MODEEDD1) {
1022 static unsigned short packet[8];
1023
1024 packet[0] = 0x10;
1025 packet[1] = x;
1026 packet[2] = VTOPOFF(xp);
1027 packet[3] = VTOPSEG(xp);
1028 packet[4] = dblk & 0xffff;
1029 packet[5] = dblk >> 16;
1030 packet[6] = 0;
1031 packet[7] = 0;
1032 v86.ctl = V86_FLAGS;
1033 v86.addr = 0x13;
1034 v86.eax = 0x4200;
1035 v86.edx = od->od_unit;
1036 v86.ds = VTOPSEG(packet);
1037 v86.esi = VTOPOFF(packet);
1038 v86int();
1039 result = (v86.efl & 0x1);
1040 if (result == 0)
984263bc 1041 break;
ed987dc9 1042 } else if (cyl < 1024) {
984263bc
MD
1043 /* Use normal CHS addressing */
1044 v86.ctl = V86_FLAGS;
1045 v86.addr = 0x13;
1046 v86.eax = 0x200 | x;
1047 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1048 v86.edx = (hd << 8) | od->od_unit;
1049 v86.es = VTOPSEG(xp);
1050 v86.ebx = VTOPOFF(xp);
1051 v86int();
1052 result = (v86.efl & 0x1);
1053 if (result == 0)
ed987dc9
MD
1054 break;
1055 } else {
1056 result = 1;
1057 break;
984263bc
MD
1058 }
1059 }
1060
1061 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1062 /* BUG here, cannot use v86 in printf because putchar uses it too */
1063 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1064 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1065 if (result) {
1066 if (bbuf != NULL)
1067 free(bbuf);
1068 return(-1);
1069 }
1070 if (bbuf != NULL)
1071 bcopy(breg, p, x * BIOSDISK_SECSIZE);
1072 p += (x * BIOSDISK_SECSIZE);
1073 dblk += x;
1074 resid -= x;
1075 }
1076
1077/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1078 if (bbuf != NULL)
1079 free(bbuf);
1080 return(0);
1081}
1082
5ee58eed
MD
1083
1084static int
1085bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1086{
1087 u_int x, bpc, cyl, hd, sec, result, resid, retry, maxfer;
1088 caddr_t p, xp, bbuf, breg;
1089
1090 /* Just in case some idiot actually tries to read -1 blocks... */
1091 if (blks < 0)
1092 return (-1);
1093
1094 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
1095 resid = blks;
1096 p = dest;
1097
1098 /* Decide whether we have to bounce */
1099 if ((od->od_unit < 0x80) &&
1100 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16))) {
1101
1102 /*
1103 * There is a 64k physical boundary somewhere in the destination buffer, so we have
1104 * to arrange a suitable bounce buffer. Allocate a buffer twice as large as we
1105 * need to. Use the bottom half unless there is a break there, in which case we
1106 * use the top half.
1107 */
1108
1109 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1110 bbuf = malloc(x * 2 * BIOSDISK_SECSIZE);
c7e270b1 1111 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) == ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
5ee58eed
MD
1112 breg = bbuf;
1113 } else {
1114 breg = bbuf + x * BIOSDISK_SECSIZE;
1115 }
1116 maxfer = x; /* limit transfers to bounce region size */
1117 } else {
1118 breg = bbuf = NULL;
1119 maxfer = 0;
1120 }
1121
1122 while (resid > 0) {
1123 x = dblk;
1124 cyl = x / bpc; /* block # / blocks per cylinder */
1125 x %= bpc; /* block offset into cylinder */
1126 hd = x / od->od_sec; /* offset / blocks per track */
1127 sec = x % od->od_sec; /* offset into track */
1128
1129 /* play it safe and don't cross track boundaries (XXX this is probably unnecessary) */
1130 x = min(od->od_sec - sec, resid);
1131 if (maxfer > 0)
1132 x = min(x, maxfer); /* fit bounce buffer */
1133
1134 /* where do we transfer to? */
1135 xp = bbuf == NULL ? p : breg;
1136
1137 /* correct sector number for 1-based BIOS numbering */
1138 sec++;
1139
1140
1141 /* Put your Data In, Put your Data out,
1142 Put your Data In, and shake it all about
1143 */
1144 if (bbuf != NULL)
1145 bcopy(p, breg, x * BIOSDISK_SECSIZE);
1146 p += (x * BIOSDISK_SECSIZE);
1147 dblk += x;
1148 resid -= x;
1149
ed987dc9
MD
1150 /*
1151 * Loop retrying the operation a couple of times. The BIOS may also
1152 * retry.
1153 */
5ee58eed 1154 for (retry = 0; retry < 3; retry++) {
ed987dc9
MD
1155 /*
1156 * If retrying, reset the drive.
1157 */
5ee58eed
MD
1158 if (retry > 0) {
1159 v86.ctl = V86_FLAGS;
1160 v86.addr = 0x13;
1161 v86.eax = 0;
1162 v86.edx = od->od_unit;
1163 v86int();
1164 }
ed987dc9
MD
1165
1166 /*
1167 * Always use EDD if the disk supports it, otherwise fall back
1168 * to CHS mode (returning an error if the cylinder number is
1169 * too large).
1170 */
1171 if (od->od_flags & BD_MODEEDD1) {
1172 static unsigned short packet[8];
1173
1174 packet[0] = 0x10;
1175 packet[1] = x;
1176 packet[2] = VTOPOFF(xp);
1177 packet[3] = VTOPSEG(xp);
1178 packet[4] = dblk & 0xffff;
1179 packet[5] = dblk >> 16;
1180 packet[6] = 0;
1181 packet[7] = 0;
1182 v86.ctl = V86_FLAGS;
1183 v86.addr = 0x13;
1184 /* Should we Write with verify ?? 0x4302 ? */
1185 v86.eax = 0x4300;
1186 v86.edx = od->od_unit;
1187 v86.ds = VTOPSEG(packet);
1188 v86.esi = VTOPOFF(packet);
1189 v86int();
1190 result = (v86.efl & 0x1);
1191 if (result == 0)
5ee58eed 1192 break;
ed987dc9 1193 } else if (cyl < 1024) {
5ee58eed
MD
1194 /* Use normal CHS addressing */
1195 v86.ctl = V86_FLAGS;
1196 v86.addr = 0x13;
1197 v86.eax = 0x300 | x;
1198 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1199 v86.edx = (hd << 8) | od->od_unit;
1200 v86.es = VTOPSEG(xp);
1201 v86.ebx = VTOPOFF(xp);
1202 v86int();
1203 result = (v86.efl & 0x1);
1204 if (result == 0)
ed987dc9
MD
1205 break;
1206 } else {
1207 result = 1;
1208 break;
5ee58eed
MD
1209 }
1210 }
1211
1212 DEBUG("%d sectors from %d/%d/%d to %p (0x%x) %s", x, cyl, hd, sec - 1, p, VTOP(p), result ? "failed" : "ok");
1213 /* BUG here, cannot use v86 in printf because putchar uses it too */
1214 DEBUG("ax = 0x%04x cx = 0x%04x dx = 0x%04x status 0x%x",
1215 0x200 | x, ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec, (hd << 8) | od->od_unit, (v86.eax >> 8) & 0xff);
1216 if (result) {
1217 if (bbuf != NULL)
1218 free(bbuf);
1219 return(-1);
1220 }
1221 }
1222
1223/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1224 if (bbuf != NULL)
1225 free(bbuf);
1226 return(0);
1227}
984263bc
MD
1228static int
1229bd_getgeom(struct open_disk *od)
1230{
1231
1232 v86.ctl = V86_FLAGS;
1233 v86.addr = 0x13;
1234 v86.eax = 0x800;
1235 v86.edx = od->od_unit;
1236 v86int();
1237
1238 if ((v86.efl & 0x1) || /* carry set */
1239 ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f))) /* unit # bad */
1240 return(1);
1241
1242 /* convert max cyl # -> # of cylinders */
1243 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1244 /* convert max head # -> # of heads */
1245 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1246 od->od_sec = v86.ecx & 0x3f;
1247
1248 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1249 return(0);
1250}
1251
1252/*
1253 * Return the BIOS geometry of a given "fixed drive" in a format
1254 * suitable for the legacy bootinfo structure. Since the kernel is
1255 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1256 * prefer to get the information directly, rather than rely on being
1257 * able to put it together from information already maintained for
1258 * different purposes and for a probably different number of drives.
1259 *
1260 * For valid drives, the geometry is expected in the format (31..0)
1261 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1262 * indicated by returning the geometry of a "1.2M" PC-format floppy
1263 * disk. And, incidentally, what is returned is not the geometry as
1264 * such but the highest valid cylinder, head, and sector numbers.
1265 */
1266u_int32_t
1267bd_getbigeom(int bunit)
1268{
1269
1270 v86.ctl = V86_FLAGS;
1271 v86.addr = 0x13;
1272 v86.eax = 0x800;
1273 v86.edx = 0x80 + bunit;
1274 v86int();
1275 if (v86.efl & 0x1)
1276 return 0x4f010f;
1277 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1278 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1279}
1280
1281/*
b13267a5 1282 * Return a suitable cdev_t value for (dev).
984263bc
MD
1283 *
1284 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1285 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1286 */
1287int
1288bd_getdev(struct i386_devdesc *dev)
1289{
1290 struct open_disk *od;
1291 int biosdev;
1292 int major;
1293 int rootdev;
1294 char *nip, *cp;
1295 int unitofs = 0, i, unit;
1296
1297 biosdev = bd_unit2bios(dev->d_kind.biosdisk.unit);
1298 DEBUG("unit %d BIOS device %d", dev->d_kind.biosdisk.unit, biosdev);
1299 if (biosdev == -1) /* not a BIOS device */
1300 return(-1);
1301 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
1302 return(-1);
1303
1304 if (biosdev < 0x80) {
1305 /* floppy (or emulated floppy) or ATAPI device */
1306 if (bdinfo[dev->d_kind.biosdisk.unit].bd_type == DT_ATAPI) {
1307 /* is an ATAPI disk */
1308 major = WFDMAJOR;
1309 } else {
1310 /* is a floppy disk */
1311 major = FDMAJOR;
1312 }
1313 } else {
1314 /* harddisk */
1315 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1316 /* label OK, disk labelled as SCSI */
1317 major = DAMAJOR;
1318 /* check for unit number correction hint, now deprecated */
1319 if ((nip = getenv("num_ide_disks")) != NULL) {
1320 i = strtol(nip, &cp, 0);
1321 /* check for parse error */
1322 if ((cp != nip) && (*cp == 0))
1323 unitofs = i;
1324 }
1325 } else {
1326 /* assume an IDE disk */
1327 major = WDMAJOR;
1328 }
1329 }
1330 /* default root disk unit number */
1331 unit = (biosdev & 0x7f) - unitofs;
1332
1333 /* XXX a better kludge to set the root disk unit number */
1334 if ((nip = getenv("root_disk_unit")) != NULL) {
1335 i = strtol(nip, &cp, 0);
1336 /* check for parse error */
1337 if ((cp != nip) && (*cp == 0))
1338 unit = i;
1339 }
1340
1341 rootdev = MAKEBOOTDEV(major,
1342 (dev->d_kind.biosdisk.slice + 1) >> 4, /* XXX slices may be wrong here */
1343 (dev->d_kind.biosdisk.slice + 1) & 0xf,
1344 unit,
1345 dev->d_kind.biosdisk.partition);
1346 DEBUG("dev is 0x%x\n", rootdev);
1347 return(rootdev);
1348}