2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * BIOS disk device handling.
33 * Ideas and algorithms from:
35 * - NetBSD libi386/biosdisk.c
36 * - FreeBSD biosboot/disk.c
42 #include <sys/disklabel.h>
43 #include <sys/diskmbr.h>
45 #include <machine/bootinfo.h>
50 #include <bootstrap.h>
54 #define BIOS_NUMDRIVES 0x475
55 #define BIOSDISK_SECSIZE 512
56 #define BUFSIZE (1 * BIOSDISK_SECSIZE)
57 #define MAXBDDEV MAXDEV
59 #define DT_ATAPI 0x10 /* disk type for ATAPI floppies */
60 #define WDMAJOR 0 /* major numbers for devices we frontend for */
66 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
68 # define DEBUG(fmt, args...)
79 int od_dkunit; /* disk unit number */
80 int od_unit; /* BIOS unit number */
81 int od_cyl; /* BIOS geometry */
84 int od_boff; /* block offset from beginning of BIOS disk */
86 #define BD_MODEINT13 0x0000
87 #define BD_MODEEDD1 0x0001
88 #define BD_MODEEDD3 0x0002
89 #define BD_MODEMASK 0x0003
90 #define BD_FLOPPY 0x0004
91 #define BD_LABELOK 0x0008
92 #define BD_PARTTABOK 0x0010
93 #define BD_GPTOK 0x0020
96 struct disklabel mbr_disklabel;
97 int mbr_nslices; /* slice count */
98 struct dos_partition mbr_slicetab[NEXTDOSPART];
102 struct gpt_part *gpt_partitions;
107 #define od_disklabel _data._mbr.mbr_disklabel
108 #define od_nslices _data._mbr.mbr_nslices
109 #define od_slicetab _data._mbr.mbr_slicetab
110 #define od_nparts _data._gpt.gpt_nparts
111 #define od_partitions _data._gpt.gpt_partitions
114 * List of BIOS devices, translation from disk unit number to
119 int bd_unit; /* BIOS unit number */
121 int bd_type; /* BIOS 'drive type' (floppy only) */
123 static int nbdinfo = 0;
125 static int bd_getgeom(struct open_disk *od);
126 static int bd_read(struct open_disk *od, daddr_t dblk, int blks,
128 static int bd_write(struct open_disk *od, daddr_t dblk, int blks,
131 static int bd_int13probe(struct bdinfo *bd);
133 static void bd_printgptpart(struct open_disk *od, struct gpt_part *gp,
134 char *prefix, int verbose);
135 static void bd_printslice(struct open_disk *od, struct dos_partition *dp,
136 char *prefix, int verbose);
137 static void bd_printbsdslice(struct open_disk *od, daddr_t offset,
138 char *prefix, int verbose);
140 static int bd_init(void);
141 static int bd_strategy(void *devdata, int flag, daddr_t dblk,
142 size_t size, char *buf, size_t *rsize);
143 static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
144 size_t size, char *buf, size_t *rsize);
145 static int bd_open(struct open_file *f, ...);
146 static int bd_close(struct open_file *f);
147 static void bd_print(int verbose);
149 struct devsw biosdisk = {
161 static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
162 static void bd_closedisk(struct open_disk *od);
163 static int bd_open_mbr(struct open_disk *od, struct i386_devdesc *dev);
164 static int bd_bestslice(struct open_disk *od);
165 static void bd_checkextended(struct open_disk *od, int slicenum);
166 static int bd_open_gpt(struct open_disk *od, struct i386_devdesc *dev);
167 static struct gpt_part *bd_best_gptpart(struct open_disk *od);
170 * Translate between BIOS device numbers and our private unit numbers.
173 bd_bios2unit(int biosdev)
177 DEBUG("looking for bios device 0x%x", biosdev);
178 for (i = 0; i < nbdinfo; i++) {
179 DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
180 if (bdinfo[i].bd_unit == biosdev)
187 bd_unit2bios(int unit)
189 if ((unit >= 0) && (unit < nbdinfo))
190 return(bdinfo[unit].bd_unit);
195 * Quiz the BIOS for disk devices, save a little info about them.
200 int base, unit, nfd = 0;
202 /* sequence 0, 0x80 */
203 for (base = 0; base <= 0x80; base += 0x80) {
204 for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
205 /* check the BIOS equipment list for number of fixed disks */
207 (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
210 bdinfo[nbdinfo].bd_unit = unit;
211 bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
213 if (!bd_int13probe(&bdinfo[nbdinfo]))
216 /* XXX we need "disk aliases" to make this simpler */
217 printf("BIOS drive %c: is disk%d\n",
218 (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
228 * Try to detect a device supported by the legacy int13 BIOS
231 bd_int13probe(struct bdinfo *bd)
236 v86.edx = bd->bd_unit;
239 if (!(v86.efl & 0x1) && /* carry clear */
240 ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) { /* unit # OK */
241 if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */
242 DEBUG("Invalid geometry for unit %d", bd->bd_unit);
243 return(0); /* skip device */
245 bd->bd_flags |= BD_MODEINT13;
246 bd->bd_type = v86.ebx & 0xff;
248 /* Determine if we can use EDD with this device. */
250 v86.edx = bd->bd_unit;
253 if (!(v86.efl & 0x1) && /* carry clear */
254 ((v86.ebx & 0xffff) == 0xaa55) && /* signature */
255 (v86.ecx & 0x1)) { /* packets mode ok */
256 bd->bd_flags |= BD_MODEEDD1;
257 if((v86.eax & 0xff00) >= 0x3000)
258 bd->bd_flags |= BD_MODEEDD3;
266 * Print information about disks
269 bd_print(int verbose)
273 struct i386_devdesc dev;
274 struct open_disk *od;
275 struct dos_partition *dptr;
277 for (i = 0; i < nbdinfo; i++) {
278 sprintf(line, " disk%d: BIOS drive %c:\n", i,
279 (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
282 /* try to open the whole disk */
284 dev.d_kind.biosdisk.slice = -1;
285 dev.d_kind.biosdisk.partition = -1;
287 if (!bd_opendisk(&od, &dev)) {
289 /* Do we have a GPT table? */
290 if (od->od_flags & BD_GPTOK) {
291 for (j = 0; j < od->od_nparts; j++) {
292 sprintf(line, " disk%dp%d", i,
293 od->od_partitions[j].gp_index);
294 bd_printgptpart(od, &od->od_partitions[j], line, verbose);
297 /* Do we have a partition table? */
298 } else if (od->od_flags & BD_PARTTABOK) {
299 dptr = &od->od_slicetab[0];
301 /* Check for a "dedicated" disk */
302 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
303 (dptr[3].dp_start == 0) &&
304 (dptr[3].dp_size == 50000)) {
305 sprintf(line, " disk%d", i);
306 bd_printbsdslice(od, 0, line, verbose);
308 for (j = 0; j < od->od_nslices; j++) {
309 sprintf(line, " disk%ds%d", i, j + 1);
310 bd_printslice(od, &dptr[j], line, verbose);
319 /* Given a size in 512 byte sectors, convert it to a human-readable number. */
321 display_size(uint64_t size)
328 if (size >= 10485760000LL) {
331 } else if (size >= 10240000) {
334 } else if (size >= 10000) {
338 sprintf(buf, "%.6ld%cB", (long)size, unit);
342 static uuid_t efi = GPT_ENT_TYPE_EFI;
343 static uuid_t freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
344 static uuid_t freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
345 static uuid_t freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
346 static uuid_t freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
347 static uuid_t ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
350 bd_printgptpart(struct open_disk *od, struct gpt_part *gp, char *prefix,
357 sprintf(stats, " %s", display_size(gp->gp_end + 1 - gp->gp_start));
361 if (uuid_equal(&gp->gp_type, &efi, NULL))
362 sprintf(line, "%s: EFI %s\n", prefix, stats);
363 else if (uuid_equal(&gp->gp_type, &ms_basic_data, NULL))
364 sprintf(line, "%s: FAT/NTFS %s\n", prefix, stats);
365 else if (uuid_equal(&gp->gp_type, &freebsd_boot, NULL))
366 sprintf(line, "%s: FreeBSD boot%s\n", prefix, stats);
367 else if (uuid_equal(&gp->gp_type, &freebsd_ufs, NULL))
368 sprintf(line, "%s: FreeBSD UFS %s\n", prefix, stats);
369 else if (uuid_equal(&gp->gp_type, &freebsd_zfs, NULL))
370 sprintf(line, "%s: FreeBSD ZFS %s\n", prefix, stats);
371 else if (uuid_equal(&gp->gp_type, &freebsd_swap, NULL))
372 sprintf(line, "%s: FreeBSD swap%s\n", prefix, stats);
374 sprintf(line, "%s: %08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x%s\n",
375 gp->gp_type.time_low, gp->gp_type.time_mid,
376 gp->gp_type.time_hi_and_version,
377 gp->gp_type.clock_seq_hi_and_reserved, gp->gp_type.clock_seq_low,
378 gp->gp_type.node[0], gp->gp_type.node[1], gp->gp_type.node[2],
379 gp->gp_type.node[3], gp->gp_type.node[4], gp->gp_type.node[5],
385 * Print information about slices on a disk. For the size calculations we
386 * assume a 512 byte sector.
389 bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
396 sprintf(stats, " %s (%d - %d)", display_size(dp->dp_size),
397 dp->dp_start, dp->dp_start + dp->dp_size);
401 switch (dp->dp_typ) {
403 bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
406 sprintf(line, "%s: Linux swap%s\n", prefix, stats);
411 * read the superblock to confirm this is an ext2fs partition?
413 sprintf(line, "%s: ext2fs%s\n", prefix, stats);
415 case 0x00: /* unused partition */
419 sprintf(line, "%s: FAT-12%s\n", prefix, stats);
424 sprintf(line, "%s: FAT-16%s\n", prefix, stats);
427 sprintf(line, "%s: NTFS/HPFS%s\n", prefix, stats);
431 sprintf(line, "%s: FAT-32%s\n", prefix, stats);
434 sprintf(line, "%s: Unknown fs: 0x%x %s\n", prefix, dp->dp_typ,
441 * Print out each valid partition in the disklabel of a FreeBSD slice.
442 * For size calculations, we assume a 512 byte sector size.
445 bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
449 char buf[BIOSDISK_SECSIZE];
450 struct disklabel *lp;
454 if (bd_read(od, offset + LABELSECTOR, 1, buf))
456 lp =(struct disklabel *)(&buf[0]);
457 if (lp->d_magic != DISKMAGIC) {
458 sprintf(line, "%s: FFS bad disklabel\n", prefix);
463 /* Print partitions */
464 for (i = 0; i < lp->d_npartitions; i++) {
466 * For each partition, make sure we know what type of fs it is. If
467 * not, then skip it. However, since floppies often have bogus
468 * fstypes, print the 'a' partition on a floppy even if it is marked
471 if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
472 (lp->d_partitions[i].p_fstype == FS_SWAP) ||
473 (lp->d_partitions[i].p_fstype == FS_VINUM) ||
474 ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
475 (od->od_flags & BD_FLOPPY) && (i == 0))) {
477 /* Only print out statistics in verbose mode */
479 sprintf(line, " %s%c: %s %s (%d - %d)\n", prefix, 'a' + i,
480 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap " :
481 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
483 display_size(lp->d_partitions[i].p_size),
484 lp->d_partitions[i].p_offset,
485 lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
487 sprintf(line, " %s%c: %s\n", prefix, 'a' + i,
488 (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
489 (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
498 * Attempt to open the disk described by (dev) for use by (f).
500 * Note that the philosophy here is "give them exactly what
501 * they ask for". This is necessary because being too "smart"
502 * about what the user might want leads to complications.
503 * (eg. given no slice or partition value, with a disk that is
504 * sliced - are they after the first BSD slice, or the DOS
508 bd_open(struct open_file *f, ...)
511 struct i386_devdesc *dev;
512 struct open_disk *od;
516 dev = va_arg(ap, struct i386_devdesc *);
518 if ((error = bd_opendisk(&od, dev)))
524 ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
525 DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
530 bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
532 struct open_disk *od;
535 if (dev->d_unit >= nbdinfo) {
536 DEBUG("attempt to open nonexistent disk");
540 od = (struct open_disk *)malloc(sizeof(struct open_disk));
546 /* Look up BIOS unit number, intialise open_disk structure */
547 od->od_dkunit = dev->d_unit;
548 od->od_unit = bdinfo[od->od_dkunit].bd_unit;
549 od->od_flags = bdinfo[od->od_dkunit].bd_flags;
552 DEBUG("open '%s', unit 0x%x slice %d partition %d",
553 i386_fmtdev(dev), dev->d_unit,
554 dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition);
556 /* Get geometry for this open (removable device may have changed) */
557 if (bd_getgeom(od)) {
558 DEBUG("can't get geometry");
563 /* Determine disk layout. */
564 error = bd_open_gpt(od, dev);
566 error = bd_open_mbr(od, dev);
572 *odp = od; /* return the open disk */
578 bd_open_mbr(struct open_disk *od, struct i386_devdesc *dev)
580 struct dos_partition *dptr;
581 struct disklabel *lp;
582 int sector, slice, i;
587 * Following calculations attempt to determine the correct value
588 * for d->od_boff by looking for the slice and partition specified,
589 * or searching for reasonable defaults.
593 * Find the slice in the DOS slice table.
596 if (bd_read(od, 0, 1, buf)) {
597 DEBUG("error reading MBR");
602 * Check the slice table magic.
604 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
605 /* If a slice number was explicitly supplied, this is an error */
606 if (dev->d_kind.biosdisk.slice > 0) {
607 DEBUG("no slice table/MBR (no magic)");
611 goto unsliced; /* may be a floppy */
615 * copy the partition table, then pick up any extended partitions.
617 bcopy(buf + DOSPARTOFF, &od->od_slicetab,
618 sizeof(struct dos_partition) * NDOSPART);
619 od->od_nslices = 4; /* extended slices start here */
620 for (i = 0; i < NDOSPART; i++)
621 bd_checkextended(od, i);
622 od->od_flags |= BD_PARTTABOK;
623 dptr = &od->od_slicetab[0];
625 /* Is this a request for the whole disk? */
626 if (dev->d_kind.biosdisk.slice == -1) {
632 * if a slice number was supplied but not found, this is an error.
634 if (dev->d_kind.biosdisk.slice > 0) {
635 slice = dev->d_kind.biosdisk.slice - 1;
636 if (slice >= od->od_nslices) {
637 DEBUG("slice %d not found", slice);
643 * Check for the historically bogus MBR found on true dedicated disks
645 if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
646 (dptr[3].dp_start == 0) &&
647 (dptr[3].dp_size == 50000)) {
652 /* Try to auto-detect the best slice; this should always give a slice number */
653 if (dev->d_kind.biosdisk.slice == 0) {
654 slice = bd_bestslice(od);
658 dev->d_kind.biosdisk.slice = slice;
661 dptr = &od->od_slicetab[0];
663 * Accept the supplied slice number unequivocally (we may be looking
664 * at a DOS partition).
666 dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
667 sector = dptr->dp_start;
668 DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
671 * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
673 if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
674 dev->d_kind.biosdisk.partition = 0;
678 * Now we have the slice offset, look for the partition in the disklabel if we have
679 * a partition to start with.
681 * XXX we might want to check the label checksum.
683 if (dev->d_kind.biosdisk.partition < 0) {
684 od->od_boff = sector; /* no partition, must be after the slice */
685 DEBUG("opening raw slice");
688 if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
689 DEBUG("error reading disklabel");
692 DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
693 bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
694 lp = &od->od_disklabel;
695 od->od_flags |= BD_LABELOK;
697 if (lp->d_magic != DISKMAGIC) {
698 DEBUG("no disklabel");
701 if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
702 DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
703 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
708 /* Complain if the partition is unused unless this is a floppy. */
709 if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
710 !(od->od_flags & BD_FLOPPY))
711 DEBUG("warning, partition marked as unused");
715 lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
716 lp->d_partitions[RAW_PART].p_offset +
723 bd_checkextended(struct open_disk *od, int slicenum)
725 char buf[BIOSDISK_SECSIZE];
726 struct dos_partition *dp;
730 dp = &od->od_slicetab[slicenum];
731 start = od->od_nslices;
733 if (dp->dp_size == 0)
735 if (dp->dp_typ != DOSPTYP_EXT)
737 if (bd_read(od, (daddr_t)dp->dp_start, 1, buf))
739 if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
740 DEBUG("no magic in extended table");
744 dp = (struct dos_partition *)(&buf[DOSPARTOFF]);
745 for (i = 0; i < NDOSPART; i++, dp++) {
746 if (dp->dp_size == 0)
748 if (od->od_nslices == NEXTDOSPART)
750 dp->dp_start += base;
751 bcopy(dp, &od->od_slicetab[od->od_nslices], sizeof(*dp));
754 end = od->od_nslices;
757 * now, recursively check the slices we just added
759 for (i = start; i < end; i++)
760 bd_checkextended(od, i);
766 * Search for a slice with the following preferences:
768 * 1: Active FreeBSD slice
769 * 2: Non-active FreeBSD slice
770 * 3: Active Linux slice
771 * 4: non-active Linux slice
772 * 5: Active FAT/FAT32 slice
773 * 6: non-active FAT/FAT32 slice
775 #define PREF_RAWDISK 0
776 #define PREF_FBSD_ACT 1
778 #define PREF_LINUX_ACT 3
780 #define PREF_DOS_ACT 5
785 * slicelimit is in the range 0 .. NDOSPART
788 bd_bestslice(struct open_disk *od)
790 struct dos_partition *dp;
795 preflevel = PREF_NONE;
797 dp = &od->od_slicetab[0];
798 for (i = 0; i < od->od_nslices; i++, dp++) {
800 switch (dp->dp_typ) {
801 case DOSPTYP_386BSD: /* FreeBSD */
802 pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
806 pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
809 case 0x01: /* DOS/Windows */
815 pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
821 if (pref < preflevel) {
830 bd_open_gpt(struct open_disk *od, struct i386_devdesc *dev)
832 struct dos_partition *dp;
836 int entries_per_sec, error, i, part;
838 char gpt[BIOSDISK_SECSIZE], tbl[BIOSDISK_SECSIZE];
841 * Following calculations attempt to determine the correct value
842 * for d->od_boff by looking for the slice and partition specified,
843 * or searching for reasonable defaults.
847 /* First, read the MBR and see if we have a PMBR. */
848 if (bd_read(od, 0, 1, tbl)) {
849 DEBUG("error reading MBR");
853 /* Check the slice table magic. */
854 if (((u_char)tbl[0x1fe] != 0x55) || ((u_char)tbl[0x1ff] != 0xaa))
857 /* Check for GPT slice. */
859 dp = (struct dos_partition *)(tbl + DOSPARTOFF);
860 for (i = 0; i < NDOSPART; i++) {
861 if (dp[i].dp_typ == 0xee)
863 else if (dp[i].dp_typ != 0x00)
869 /* Read primary GPT table header. */
870 if (bd_read(od, 1, 1, gpt)) {
871 DEBUG("error reading GPT header");
874 hdr = (struct gpt_hdr *)gpt;
875 if (bcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0 ||
876 hdr->hdr_lba_self != 1 || hdr->hdr_revision < 0x00010000 ||
877 hdr->hdr_entsz < sizeof(*ent) ||
878 BIOSDISK_SECSIZE % hdr->hdr_entsz != 0) {
879 DEBUG("Invalid GPT header\n");
883 /* Now walk the partition table to count the number of valid partitions. */
885 entries_per_sec = BIOSDISK_SECSIZE / hdr->hdr_entsz;
886 elba = hdr->hdr_lba_table + hdr->hdr_entries / entries_per_sec;
887 for (lba = hdr->hdr_lba_table; lba < elba; lba++) {
888 if (bd_read(od, lba, 1, tbl)) {
889 DEBUG("error reading GPT table");
892 for (i = 0; i < entries_per_sec; i++) {
893 ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
894 if (uuid_is_nil(&ent->ent_type, NULL) || ent->ent_lba_start == 0 ||
895 ent->ent_lba_end < ent->ent_lba_start)
901 /* Save the important information about all the valid partitions. */
902 od->od_nparts = part;
904 od->od_partitions = malloc(part * sizeof(struct gpt_part));
906 for (lba = hdr->hdr_lba_table; lba < elba; lba++) {
907 if (bd_read(od, lba, 1, tbl)) {
908 DEBUG("error reading GPT table");
912 for (i = 0; i < entries_per_sec; i++) {
913 ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
914 if (uuid_is_nil(&ent->ent_type, NULL) ||
915 ent->ent_lba_start == 0 ||
916 ent->ent_lba_end < ent->ent_lba_start)
918 od->od_partitions[part].gp_index = (lba - hdr->hdr_lba_table) *
919 entries_per_sec + i + 1;
920 od->od_partitions[part].gp_type = ent->ent_type;
921 od->od_partitions[part].gp_start = ent->ent_lba_start;
922 od->od_partitions[part].gp_end = ent->ent_lba_end;
927 od->od_flags |= BD_GPTOK;
929 /* Is this a request for the whole disk? */
930 if (dev->d_kind.biosdisk.slice < 0) {
936 * If a partition number was supplied, then the user is trying to use
937 * an MBR address rather than a GPT address, so fail.
939 if (dev->d_kind.biosdisk.partition != 0xff) {
944 /* If a slice number was supplied but not found, this is an error. */
946 if (dev->d_kind.biosdisk.slice > 0) {
947 for (i = 0; i < od->od_nparts; i++) {
948 if (od->od_partitions[i].gp_index == dev->d_kind.biosdisk.slice) {
949 gp = &od->od_partitions[i];
954 DEBUG("partition %d not found", dev->d_kind.biosdisk.slice);
960 /* Try to auto-detect the best partition. */
961 if (dev->d_kind.biosdisk.slice == 0) {
962 gp = bd_best_gptpart(od);
967 dev->d_kind.biosdisk.slice = gp->gp_index;
969 od->od_boff = gp->gp_start;
973 free(od->od_partitions);
977 static struct gpt_part *
978 bd_best_gptpart(struct open_disk *od)
980 struct gpt_part *gp, *prefpart;
981 int i, pref, preflevel;
984 preflevel = PREF_NONE;
986 gp = od->od_partitions;
987 for (i = 0; i < od->od_nparts; i++, gp++) {
988 /* Windows. XXX: Also Linux. */
989 if (uuid_equal(&gp->gp_type, &ms_basic_data, NULL))
992 else if (uuid_equal(&gp->gp_type, &freebsd_ufs, NULL) ||
993 uuid_equal(&gp->gp_type, &freebsd_zfs, NULL))
997 if (pref < preflevel) {
1006 bd_close(struct open_file *f)
1008 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
1015 bd_closedisk(struct open_disk *od)
1017 DEBUG("open_disk %p", od);
1019 /* XXX is this required? (especially if disk already open...) */
1020 if (od->od_flags & BD_FLOPPY)
1023 if (od->od_flags & BD_GPTOK)
1024 free(od->od_partitions);
1029 bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
1031 struct bcache_devdata bcd;
1032 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
1034 bcd.dv_strategy = bd_realstrategy;
1035 bcd.dv_devdata = devdata;
1036 return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
1040 bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
1042 struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
1044 #ifdef BD_SUPPORT_FRAGS
1045 char fragbuf[BIOSDISK_SECSIZE];
1048 fragsize = size % BIOSDISK_SECSIZE;
1050 if (size % BIOSDISK_SECSIZE)
1051 panic("bd_strategy: %d bytes I/O not multiple of block size", size);
1054 DEBUG("open_disk %p", od);
1055 blks = size / BIOSDISK_SECSIZE;
1061 DEBUG("read %d from %d to %p", blks, dblk, buf);
1063 if (blks && bd_read(od, dblk, blks, buf)) {
1064 DEBUG("read error");
1067 #ifdef BD_SUPPORT_FRAGS
1068 DEBUG("bd_strategy: frag read %d from %d+%d to %p",
1069 fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
1070 if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
1071 DEBUG("frag read error");
1074 bcopy(fragbuf, buf + (blks * BIOSDISK_SECSIZE), fragsize);
1078 DEBUG("write %d from %d to %p", blks, dblk, buf);
1080 if (blks && bd_write(od, dblk, blks, buf)) {
1081 DEBUG("write error");
1084 #ifdef BD_SUPPORT_FRAGS
1086 DEBUG("Attempted to write a frag");
1101 /* Max number of sectors to bounce-buffer if the request crosses a 64k boundary */
1102 #define FLOPPY_BOUNCEBUF 18
1113 bd_edd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
1115 static struct edd_packet packet;
1118 packet.count = blks;
1119 packet.offset = VTOPOFF(dest);
1120 packet.seg = VTOPSEG(dest);
1122 v86.ctl = V86_FLAGS;
1125 /* Should we Write with verify ?? 0x4302 ? */
1129 v86.edx = od->od_unit;
1130 v86.ds = VTOPSEG(&packet);
1131 v86.esi = VTOPOFF(&packet);
1133 return (v86.efl & 0x1);
1137 bd_chs_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
1139 u_int x, bpc, cyl, hd, sec;
1141 bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
1143 cyl = x / bpc; /* block # / blocks per cylinder */
1144 x %= bpc; /* block offset into cylinder */
1145 hd = x / od->od_sec; /* offset / blocks per track */
1146 sec = x % od->od_sec; /* offset into track */
1148 /* correct sector number for 1-based BIOS numbering */
1152 /* CHS doesn't support cylinders > 1023. */
1155 v86.ctl = V86_FLAGS;
1158 v86.eax = 0x300 | blks;
1160 v86.eax = 0x200 | blks;
1161 v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
1162 v86.edx = (hd << 8) | od->od_unit;
1163 v86.es = VTOPSEG(dest);
1164 v86.ebx = VTOPOFF(dest);
1166 return (v86.efl & 0x1);
1170 bd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
1172 u_int x, sec, result, resid, retry, maxfer;
1173 caddr_t p, xp, bbuf, breg;
1175 /* Just in case some idiot actually tries to read/write -1 blocks... */
1182 /* Decide whether we have to bounce */
1183 if (VTOP(dest) >> 20 != 0 || ((od->od_unit < 0x80) &&
1184 ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16)))) {
1187 * There is a 64k physical boundary somewhere in the
1188 * destination buffer, or the destination buffer is above
1189 * first 1MB of physical memory so we have to arrange a
1190 * suitable bounce buffer. Allocate a buffer twice as large
1191 * as we need to. Use the bottom half unless there is a break
1192 * there, in which case we use the top half.
1194 x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
1195 bbuf = alloca(x * 2 * BIOSDISK_SECSIZE);
1196 if (((u_int32_t)VTOP(bbuf) & 0xffff0000) ==
1197 ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
1200 breg = bbuf + x * BIOSDISK_SECSIZE;
1202 maxfer = x; /* limit transfers to bounce region size */
1210 * Play it safe and don't cross track boundaries.
1211 * (XXX this is probably unnecessary)
1213 sec = dblk % od->od_sec; /* offset into track */
1214 x = min(od->od_sec - sec, resid);
1216 x = min(x, maxfer); /* fit bounce buffer */
1218 /* where do we transfer to? */
1219 xp = bbuf == NULL ? p : breg;
1222 * Put your Data In, Put your Data out,
1223 * Put your Data In, and shake it all about
1225 if (write && bbuf != NULL)
1226 bcopy(p, breg, x * BIOSDISK_SECSIZE);
1229 * Loop retrying the operation a couple of times. The BIOS
1232 for (retry = 0; retry < 3; retry++) {
1233 /* if retrying, reset the drive */
1235 v86.ctl = V86_FLAGS;
1238 v86.edx = od->od_unit;
1242 if (od->od_flags & BD_MODEEDD1)
1243 result = bd_edd_io(od, dblk, x, xp, write);
1245 result = bd_chs_io(od, dblk, x, xp, write);
1251 DEBUG("%d sectors from %lld to %p (0x%x) %s", x, dblk, p, VTOP(p),
1252 result ? "failed" : "ok");
1254 DEBUG("%d sectors from %p (0x%x) to %lld %s", x, p, VTOP(p), dblk,
1255 result ? "failed" : "ok");
1259 if (!write && bbuf != NULL)
1260 bcopy(breg, p, x * BIOSDISK_SECSIZE);
1261 p += (x * BIOSDISK_SECSIZE);
1266 /* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
1271 bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1274 return (bd_io(od, dblk, blks, dest, 0));
1278 bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
1281 return (bd_io(od, dblk, blks, dest, 1));
1285 bd_getgeom(struct open_disk *od)
1288 v86.ctl = V86_FLAGS;
1291 v86.edx = od->od_unit;
1294 if ((v86.efl & 0x1) || /* carry set */
1295 ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f))) /* unit # bad */
1298 /* convert max cyl # -> # of cylinders */
1299 od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
1300 /* convert max head # -> # of heads */
1301 od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
1302 od->od_sec = v86.ecx & 0x3f;
1304 DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
1309 * Return the BIOS geometry of a given "fixed drive" in a format
1310 * suitable for the legacy bootinfo structure. Since the kernel is
1311 * expecting raw int 0x13/0x8 values for N_BIOS_GEOM drives, we
1312 * prefer to get the information directly, rather than rely on being
1313 * able to put it together from information already maintained for
1314 * different purposes and for a probably different number of drives.
1316 * For valid drives, the geometry is expected in the format (31..0)
1317 * "000000cc cccccccc hhhhhhhh 00ssssss"; and invalid drives are
1318 * indicated by returning the geometry of a "1.2M" PC-format floppy
1319 * disk. And, incidentally, what is returned is not the geometry as
1320 * such but the highest valid cylinder, head, and sector numbers.
1323 bd_getbigeom(int bunit)
1326 v86.ctl = V86_FLAGS;
1329 v86.edx = 0x80 + bunit;
1333 return ((v86.ecx & 0xc0) << 18) | ((v86.ecx & 0xff00) << 8) |
1334 (v86.edx & 0xff00) | (v86.ecx & 0x3f);
1338 * Return a suitable dev_t value for (dev).
1340 * In the case where it looks like (dev) is a SCSI disk, we allow the number of
1341 * IDE disks to be specified in $num_ide_disks. There should be a Better Way.
1344 bd_getdev(struct i386_devdesc *dev)
1346 struct open_disk *od;
1351 int unitofs = 0, i, unit;
1353 biosdev = bd_unit2bios(dev->d_unit);
1354 DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev);
1355 if (biosdev == -1) /* not a BIOS device */
1357 if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
1360 if (biosdev < 0x80) {
1361 /* floppy (or emulated floppy) or ATAPI device */
1362 if (bdinfo[dev->d_unit].bd_type == DT_ATAPI) {
1363 /* is an ATAPI disk */
1366 /* is a floppy disk */
1371 if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
1372 /* label OK, disk labelled as SCSI */
1374 /* check for unit number correction hint, now deprecated */
1375 if ((nip = getenv("num_ide_disks")) != NULL) {
1376 i = strtol(nip, &cp, 0);
1377 /* check for parse error */
1378 if ((cp != nip) && (*cp == 0))
1382 /* assume an IDE disk */
1386 /* default root disk unit number */
1387 unit = (biosdev & 0x7f) - unitofs;
1389 /* XXX a better kludge to set the root disk unit number */
1390 if ((nip = getenv("root_disk_unit")) != NULL) {
1391 i = strtol(nip, &cp, 0);
1392 /* check for parse error */
1393 if ((cp != nip) && (*cp == 0))
1397 rootdev = MAKEBOOTDEV(major, dev->d_kind.biosdisk.slice + 1, unit,
1398 dev->d_kind.biosdisk.partition);
1399 DEBUG("dev is 0x%x\n", rootdev);