[Midnightbsd-cvs] src [8410] trunk/sys/boot: Initial import of FreeBSD boot loader rework from 9.2 release.
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Sun Sep 18 16:24:57 EDT 2016
Revision: 8410
http://svnweb.midnightbsd.org/src/?rev=8410
Author: laffer1
Date: 2016-09-18 16:24:57 -0400 (Sun, 18 Sep 2016)
Log Message:
-----------
Initial import of FreeBSD boot loader rework from 9.2 release.
userboot 2
todo: add more midnightbsd partition type handling.
Obtained from: FreeBSD svn 243243
Modified Paths:
--------------
trunk/sys/boot/common/Makefile.inc
trunk/sys/boot/common/disk.c
trunk/sys/boot/common/disk.h
trunk/sys/boot/common/module.c
trunk/sys/boot/i386/libi386/Makefile
trunk/sys/boot/i386/libi386/biosdisk.c
trunk/sys/boot/i386/libi386/devicename.c
trunk/sys/boot/i386/libi386/libi386.h
trunk/sys/boot/i386/loader/Makefile
trunk/sys/boot/i386/loader/conf.c
trunk/sys/boot/i386/loader/main.c
trunk/sys/boot/i386/pmbr/pmbr.s
trunk/sys/boot/i386/zfsboot/zfsboot.c
trunk/sys/boot/uboot/common/main.c
trunk/sys/boot/uboot/lib/Makefile
trunk/sys/boot/uboot/lib/devicename.c
trunk/sys/boot/uboot/lib/disk.c
trunk/sys/boot/uboot/lib/libuboot.h
trunk/sys/boot/userboot/test/test.c
trunk/sys/boot/userboot/userboot/Makefile
trunk/sys/boot/userboot/userboot/bootinfo32.c
trunk/sys/boot/userboot/userboot/copy.c
trunk/sys/boot/userboot/userboot/devicename.c
trunk/sys/boot/userboot/userboot/libuserboot.h
trunk/sys/boot/userboot/userboot/main.c
trunk/sys/boot/userboot/userboot/userboot_disk.c
trunk/sys/boot/userboot/userboot.h
trunk/sys/boot/zfs/zfs.c
Modified: trunk/sys/boot/common/Makefile.inc
===================================================================
--- trunk/sys/boot/common/Makefile.inc 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/common/Makefile.inc 2016-09-18 20:24:57 UTC (rev 8410)
@@ -1,6 +1,6 @@
# $MidnightBSD$
-SRCS+= boot.c commands.c console.c devopen.c disk.c interp.c
+SRCS+= boot.c commands.c console.c devopen.c interp.c
SRCS+= interp_backslash.c interp_parse.c ls.c misc.c
SRCS+= module.c panic.c
@@ -24,6 +24,18 @@
SRCS+= dev_net.c
.endif
+.if !defined(LOADER_NO_DISK_SUPPORT)
+SRCS+= disk.c part.c
+CFLAGS+= -DLOADER_DISK_SUPPORT
+.if !defined(LOADER_NO_GPT_SUPPORT)
+SRCS+= crc32.c
+CFLAGS+= -DLOADER_GPT_SUPPORT
+.endif
+.if !defined(LOADER_NO_MBR_SUPPORT)
+CFLAGS+= -DLOADER_MBR_SUPPORT
+.endif
+.endif
+
.if defined(HAVE_BCACHE)
SRCS+= bcache.c
.endif
Modified: trunk/sys/boot/common/disk.c
===================================================================
--- trunk/sys/boot/common/disk.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/common/disk.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -1,5 +1,6 @@
/*-
* Copyright (c) 1998 Michael Smith <msmith at freebsd.org>
+ * Copyright (c) 2012 Andrey V. Elsukov <ae at FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -25,28 +26,15 @@
*/
#include <sys/cdefs.h>
-__MBSDID("$MidnightBSD: src/sys/boot/common/disk.c,v 1.2 2012/12/29 04:58:20 laffer1 Exp $");
+/* $FreeBSD: release/9.2.0/sys/boot/common/disk.c 243243 2012-11-18 17:09:29Z ae $ */
+__MBSDID("$MidnightBSD$");
-/*
- * MBR/GPT partitioned disk device handling.
- *
- * Ideas and algorithms from:
- *
- * - NetBSD libi386/biosdisk.c
- * - FreeBSD biosboot/disk.c
- *
- */
-
+#include <sys/disk.h>
+#include <sys/queue.h>
#include <stand.h>
-
-#include <sys/diskmbr.h>
-#include <sys/disklabel.h>
-#include <sys/gpt.h>
-
#include <stdarg.h>
-#include <uuid.h>
-
#include <bootstrap.h>
+#include <part.h>
#include "disk.h"
@@ -56,57 +44,118 @@
# define DEBUG(fmt, args...)
#endif
-/*
- * Search for a slice with the following preferences:
- *
- * 1: Active FreeBSD slice
- * 2: Non-active FreeBSD slice
- * 3: Active Linux slice
- * 4: non-active Linux slice
- * 5: Active FAT/FAT32 slice
- * 6: non-active FAT/FAT32 slice
- */
-#define PREF_RAWDISK 0
-#define PREF_FBSD_ACT 1
-#define PREF_FBSD 2
-#define PREF_LINUX_ACT 3
-#define PREF_LINUX 4
-#define PREF_DOS_ACT 5
-#define PREF_DOS 6
-#define PREF_NONE 7
+struct open_disk {
+ struct ptable *table;
+ off_t mediasize;
+ u_int sectorsize;
+ u_int flags;
+ int rcnt;
+};
-#ifdef LOADER_GPT_SUPPORT
+struct print_args {
+ struct disk_devdesc *dev;
+ const char *prefix;
+ int verbose;
+};
-struct gpt_part {
- int gp_index;
- uuid_t gp_type;
- uint64_t gp_start;
- uint64_t gp_end;
+struct dentry {
+ const struct devsw *d_dev;
+ int d_unit;
+ int d_slice;
+ int d_partition;
+
+ struct open_disk *od;
+ off_t d_offset;
+ STAILQ_ENTRY(dentry) entry;
+#ifdef DISK_DEBUG
+ uint32_t count;
+#endif
};
-static uuid_t efi = GPT_ENT_TYPE_EFI;
-static uuid_t freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
-static uuid_t freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
-static uuid_t freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
-static uuid_t freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
-static uuid_t midnightbsd_boot = GPT_ENT_TYPE_MIDNIGHTBSD_BOOT;
-static uuid_t midnightbsd_ufs = GPT_ENT_TYPE_MIDNIGHTBSD_UFS;
-static uuid_t midnightbsd_swap = GPT_ENT_TYPE_MIDNIGHTBSD_SWAP;
-static uuid_t midnightbsd_zfs = GPT_ENT_TYPE_MIDNIGHTBSD_ZFS;
-static uuid_t ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
+static STAILQ_HEAD(, dentry) opened_disks =
+ STAILQ_HEAD_INITIALIZER(opened_disks);
+static int
+disk_lookup(struct disk_devdesc *dev)
+{
+ struct dentry *entry;
+ int rc;
+
+ rc = ENOENT;
+ STAILQ_FOREACH(entry, &opened_disks, entry) {
+ if (entry->d_dev != dev->d_dev ||
+ entry->d_unit != dev->d_unit)
+ continue;
+ dev->d_opendata = entry->od;
+ if (entry->d_slice == dev->d_slice &&
+ entry->d_partition == dev->d_partition) {
+ dev->d_offset = entry->d_offset;
+ DEBUG("%s offset %lld", disk_fmtdev(dev),
+ dev->d_offset);
+#ifdef DISK_DEBUG
+ entry->count++;
#endif
+ return (0);
+ }
+ rc = EAGAIN;
+ }
+ return (rc);
+}
-#if defined(LOADER_GPT_SUPPORT) || defined(LOADER_MBR_SUPPORT)
+static void
+disk_insert(struct disk_devdesc *dev)
+{
+ struct dentry *entry;
-/* Given a size in 512 byte sectors, convert it to a human-readable number. */
+ entry = (struct dentry *)malloc(sizeof(struct dentry));
+ if (entry == NULL) {
+ DEBUG("no memory");
+ return;
+ }
+ entry->d_dev = dev->d_dev;
+ entry->d_unit = dev->d_unit;
+ entry->d_slice = dev->d_slice;
+ entry->d_partition = dev->d_partition;
+ entry->od = (struct open_disk *)dev->d_opendata;
+ entry->od->rcnt++;
+ entry->d_offset = dev->d_offset;
+#ifdef DISK_DEBUG
+ entry->count = 1;
+#endif
+ STAILQ_INSERT_TAIL(&opened_disks, entry, entry);
+ DEBUG("%s cached", disk_fmtdev(dev));
+}
+
+#ifdef DISK_DEBUG
+COMMAND_SET(dcachestat, "dcachestat", "get disk cache stats",
+ command_dcachestat);
+
+static int
+command_dcachestat(int argc, char *argv[])
+{
+ struct disk_devdesc dev;
+ struct dentry *entry;
+
+ STAILQ_FOREACH(entry, &opened_disks, entry) {
+ dev.d_dev = (struct devsw *)entry->d_dev;
+ dev.d_unit = entry->d_unit;
+ dev.d_slice = entry->d_slice;
+ dev.d_partition = entry->d_partition;
+ printf("%s %d => %p [%d]\n", disk_fmtdev(&dev), entry->count,
+ entry->od, entry->od->rcnt);
+ }
+ return (CMD_OK);
+}
+#endif /* DISK_DEBUG */
+
+/* Convert size to a human-readable number. */
static char *
-display_size(uint64_t size)
+display_size(uint64_t size, u_int sectorsize)
{
static char buf[80];
char unit;
- size /= 2;
+ size = size * sectorsize / 1024;
unit = 'K';
if (size >= 10485760000LL) {
size /= 1073741824;
@@ -118,697 +167,328 @@
size /= 1024;
unit = 'M';
}
- sprintf(buf, "%.6ld%cB", (long)size, unit);
+ sprintf(buf, "%ld%cB", (long)size, unit);
return (buf);
}
-#endif
-
-#ifdef LOADER_MBR_SUPPORT
-
-static void
-disk_checkextended(struct disk_devdesc *dev,
- struct dos_partition *slicetab, int slicenum, int *nslicesp)
+static int
+ptblread(void *d, void *buf, size_t blocks, off_t offset)
{
- uint8_t buf[DISK_SECSIZE];
- struct dos_partition *dp;
- uint32_t base;
- int rc, i, start, end;
+ struct disk_devdesc *dev;
+ struct open_disk *od;
- dp = &slicetab[slicenum];
- start = *nslicesp;
-
- if (dp->dp_size == 0)
- goto done;
- if (dp->dp_typ != DOSPTYP_EXT)
- goto done;
- rc = dev->d_dev->dv_strategy(dev, F_READ, dp->dp_start, DISK_SECSIZE,
- (char *) buf, NULL);
- if (rc)
- goto done;
- if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) {
- DEBUG("no magic in extended table");
- goto done;
- }
- base = dp->dp_start;
- dp = (struct dos_partition *) &buf[DOSPARTOFF];
- for (i = 0; i < NDOSPART; i++, dp++) {
- if (dp->dp_size == 0)
- continue;
- if (*nslicesp == NEXTDOSPART)
- goto done;
- dp->dp_start += base;
- bcopy(dp, &slicetab[*nslicesp], sizeof(*dp));
- (*nslicesp)++;
- }
- end = *nslicesp;
-
- /*
- * now, recursively check the slices we just added
- */
- for (i = start; i < end; i++)
- disk_checkextended(dev, slicetab, i, nslicesp);
-done:
- return;
+ dev = (struct disk_devdesc *)d;
+ od = (struct open_disk *)dev->d_opendata;
+ return (dev->d_dev->dv_strategy(dev, F_READ, offset,
+ blocks * od->sectorsize, (char *)buf, NULL));
}
-static int
-disk_readslicetab(struct disk_devdesc *dev,
- struct dos_partition **slicetabp, int *nslicesp)
+#define PWIDTH 35
+static void
+ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
{
- struct dos_partition *slicetab = NULL;
- int nslices, i;
- int rc;
- uint8_t buf[DISK_SECSIZE];
+ struct print_args *pa, bsd;
+ struct open_disk *od;
+ struct ptable *table;
+ char line[80];
- /*
- * Find the slice in the DOS slice table.
- */
- rc = dev->d_dev->dv_strategy(dev, F_READ, 0, DISK_SECSIZE,
- (char *) buf, NULL);
- if (rc) {
- DEBUG("error reading MBR");
- return (rc);
+ pa = (struct print_args *)arg;
+ od = (struct open_disk *)pa->dev->d_opendata;
+ sprintf(line, " %s%s: %s", pa->prefix, pname,
+ parttype2str(part->type));
+ if (pa->verbose)
+ sprintf(line, "%-*s%s", PWIDTH, line,
+ display_size(part->end - part->start + 1,
+ od->sectorsize));
+ strcat(line, "\n");
+ pager_output(line);
+ if (part->type == PART_FREEBSD) {
+ /* Open slice with BSD label */
+ pa->dev->d_offset = part->start;
+ table = ptable_open(pa->dev, part->end - part->start + 1,
+ od->sectorsize, ptblread);
+ if (table == NULL)
+ return;
+ sprintf(line, " %s%s", pa->prefix, pname);
+ bsd.dev = pa->dev;
+ bsd.prefix = line;
+ bsd.verbose = pa->verbose;
+ ptable_iterate(table, &bsd, ptable_print);
+ ptable_close(table);
}
-
- /*
- * Check the slice table magic.
- */
- if (buf[0x1fe] != 0x55 || buf[0x1ff] != 0xaa) {
- DEBUG("no slice table/MBR (no magic)");
- return (rc);
- }
-
- /*
- * copy the partition table, then pick up any extended partitions.
- */
- slicetab = malloc(NEXTDOSPART * sizeof(struct dos_partition));
- bcopy(buf + DOSPARTOFF, slicetab,
- sizeof(struct dos_partition) * NDOSPART);
- nslices = NDOSPART; /* extended slices start here */
- for (i = 0; i < NDOSPART; i++)
- disk_checkextended(dev, slicetab, i, &nslices);
-
- *slicetabp = slicetab;
- *nslicesp = nslices;
- return (0);
}
+#undef PWIDTH
-/*
- * Search for the best MBR slice (typically the first FreeBSD slice).
- */
-static int
-disk_bestslice(struct dos_partition *slicetab, int nslices)
+void
+disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
{
- struct dos_partition *dp;
- int pref, preflevel;
- int i, prefslice;
+ struct open_disk *od;
+ struct print_args pa;
- prefslice = 0;
- preflevel = PREF_NONE;
-
- dp = &slicetab[0];
- for (i = 0; i < nslices; i++, dp++) {
- switch (dp->dp_typ) {
- case DOSPTYP_386BSD: /* FreeBSD */
- pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
- break;
-
- case DOSPTYP_LINUX:
- pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
- break;
-
- case 0x01: /* DOS/Windows */
- case 0x04:
- case 0x06:
- case 0x0b:
- case 0x0c:
- case 0x0e:
- pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
- break;
-
- default:
- pref = PREF_NONE;
- }
- if (pref < preflevel) {
- preflevel = pref;
- prefslice = i + 1;
- }
- }
- return (prefslice);
+ /* Disk should be opened */
+ od = (struct open_disk *)dev->d_opendata;
+ pa.dev = dev;
+ pa.prefix = prefix;
+ pa.verbose = verbose;
+ ptable_iterate(od->table, &pa, ptable_print);
}
-static int
-disk_openmbr(struct disk_devdesc *dev)
+int
+disk_open(struct disk_devdesc *dev, off_t mediasize, u_int sectorsize,
+ u_int flags)
{
- struct dos_partition *slicetab = NULL, *dptr;
- int nslices, sector, slice;
- int rc;
- uint8_t buf[DISK_SECSIZE];
- struct disklabel *lp;
+ struct open_disk *od;
+ struct ptable *table;
+ struct ptable_entry part;
+ int rc, slice, partition;
- /*
- * Following calculations attempt to determine the correct value
- * for dev->d_offset by looking for the slice and partition specified,
- * or searching for reasonable defaults.
- */
- rc = disk_readslicetab(dev, &slicetab, &nslices);
- if (rc)
- return (rc);
-
- /*
- * if a slice number was supplied but not found, this is an error.
- */
- if (dev->d_slice > 0) {
- slice = dev->d_slice - 1;
- if (slice >= nslices) {
- DEBUG("slice %d not found", slice);
- rc = EPART;
- goto out;
- }
+ rc = 0;
+ if ((flags & DISK_F_NOCACHE) == 0) {
+ rc = disk_lookup(dev);
+ if (rc == 0)
+ return (0);
}
-
/*
- * Check for the historically bogus MBR found on true dedicated disks
+ * While we are reading disk metadata, make sure we do it relative
+ * to the start of the disk
*/
- if (slicetab[3].dp_typ == DOSPTYP_386BSD &&
- slicetab[3].dp_start == 0 && slicetab[3].dp_size == 50000) {
- sector = 0;
- goto unsliced;
- }
-
- /*
- * Try to auto-detect the best slice; this should always give
- * a slice number
- */
- if (dev->d_slice == 0) {
- slice = disk_bestslice(slicetab, nslices);
- if (slice == -1) {
- rc = ENOENT;
- goto out;
+ dev->d_offset = 0;
+ table = NULL;
+ slice = dev->d_slice;
+ partition = dev->d_partition;
+ if (rc == EAGAIN) {
+ /*
+ * This entire disk was already opened and there is no
+ * need to allocate new open_disk structure and open the
+ * main partition table.
+ */
+ od = (struct open_disk *)dev->d_opendata;
+ DEBUG("%s unit %d, slice %d, partition %d => %p (cached)",
+ disk_fmtdev(dev), dev->d_unit, dev->d_slice,
+ dev->d_partition, od);
+ goto opened;
+ } else {
+ od = (struct open_disk *)malloc(sizeof(struct open_disk));
+ if (od == NULL) {
+ DEBUG("no memory");
+ return (ENOMEM);
}
- dev->d_slice = slice;
+ dev->d_opendata = od;
+ od->rcnt = 0;
}
+ od->mediasize = mediasize;
+ od->sectorsize = sectorsize;
+ od->flags = flags;
+ DEBUG("%s unit %d, slice %d, partition %d => %p",
+ disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
- /*
- * Accept the supplied slice number unequivocally (we may be looking
- * at a DOS partition).
- * Note: we number 1-4, offsets are 0-3
- */
- dptr = &slicetab[dev->d_slice - 1];
- sector = dptr->dp_start;
- DEBUG("slice entry %d at %d, %d sectors",
- dev->d_slice - 1, sector, dptr->dp_size);
-
-unsliced:
- /*
- * Now we have the slice offset, look for the partition in the
- * disklabel if we have a partition to start with.
- *
- * XXX we might want to check the label checksum.
- */
- if (dev->d_partition < 0) {
- /* no partition, must be after the slice */
- DEBUG("opening raw slice");
- dev->d_offset = sector;
- rc = 0;
+ /* Determine disk layout. */
+ od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,
+ ptblread);
+ if (od->table == NULL) {
+ DEBUG("Can't read partition table");
+ rc = ENXIO;
goto out;
}
-
- rc = dev->d_dev->dv_strategy(dev, F_READ, sector + LABELSECTOR,
- DISK_SECSIZE, (char *) buf, NULL);
- if (rc) {
- DEBUG("error reading disklabel");
- goto out;
- }
-
- lp = (struct disklabel *) buf;
-
- if (lp->d_magic != DISKMAGIC) {
- DEBUG("no disklabel");
- rc = ENOENT;
- goto out;
- }
- if (dev->d_partition >= lp->d_npartitions) {
- DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
- 'a' + dev->d_partition,
- 'a' + lp->d_npartitions);
- rc = EPART;
- goto out;
- }
-
- dev->d_offset =
- lp->d_partitions[dev->d_partition].p_offset -
- lp->d_partitions[RAW_PART].p_offset +
- sector;
+opened:
rc = 0;
-
-out:
- if (slicetab)
- free(slicetab);
- return (rc);
-}
-
-/*
- * Print out each valid partition in the disklabel of a FreeBSD slice.
- * For size calculations, we assume a 512 byte sector size.
- */
-static void
-disk_printbsdslice(struct disk_devdesc *dev, daddr_t offset,
- char *prefix, int verbose)
-{
- char line[80];
- char buf[DISK_SECSIZE];
- struct disklabel *lp;
- int i, rc, fstype;
-
- /* read disklabel */
- rc = dev->d_dev->dv_strategy(dev, F_READ, offset + LABELSECTOR,
- DISK_SECSIZE, (char *) buf, NULL);
- if (rc)
- return;
- lp =(struct disklabel *)(&buf[0]);
- if (lp->d_magic != DISKMAGIC) {
- sprintf(line, "%s: FFS bad disklabel\n", prefix);
- pager_output(line);
- return;
- }
-
- /* Print partitions */
- for (i = 0; i < lp->d_npartitions; i++) {
+ if (ptable_gettype(od->table) == PTABLE_BSD &&
+ partition >= 0) {
+ /* It doesn't matter what value has d_slice */
+ rc = ptable_getpart(od->table, &part, partition);
+ if (rc == 0)
+ dev->d_offset = part.start;
+ } else if (slice >= 0) {
+ /* Try to get information about partition */
+ if (slice == 0)
+ rc = ptable_getbestpart(od->table, &part);
+ else
+ rc = ptable_getpart(od->table, &part, slice);
+ if (rc != 0) /* Partition doesn't exist */
+ goto out;
+ dev->d_offset = part.start;
+ slice = part.index;
+ if (ptable_gettype(od->table) == PTABLE_GPT) {
+ partition = 255;
+ goto out; /* Nothing more to do */
+ } else if (partition == 255) {
+ /*
+ * When we try to open GPT partition, but partition
+ * table isn't GPT, reset d_partition value to -1
+ * and try to autodetect appropriate value.
+ */
+ partition = -1;
+ }
/*
- * For each partition, make sure we know what type of fs it
- * is. If not, then skip it.
+ * If d_partition < 0 and we are looking at a BSD slice,
+ * then try to read BSD label, otherwise return the
+ * whole MBR slice.
*/
- fstype = lp->d_partitions[i].p_fstype;
- if (fstype != FS_BSDFFS &&
- fstype != FS_SWAP &&
- fstype != FS_VINUM)
- continue;
-
- /* Only print out statistics in verbose mode */
- if (verbose)
- sprintf(line, " %s%c: %s %s (%d - %d)\n",
- prefix, 'a' + i,
- (fstype == FS_SWAP) ? "swap " :
- (fstype == FS_VINUM) ? "vinum" :
- "FFS ",
- display_size(lp->d_partitions[i].p_size),
- lp->d_partitions[i].p_offset,
- (lp->d_partitions[i].p_offset
- + lp->d_partitions[i].p_size));
- else
- sprintf(line, " %s%c: %s\n", prefix, 'a' + i,
- (fstype == FS_SWAP) ? "swap" :
- (fstype == FS_VINUM) ? "vinum" :
- "FFS");
- pager_output(line);
- }
-}
-
-static void
-disk_printslice(struct disk_devdesc *dev, int slice,
- struct dos_partition *dp, char *prefix, int verbose)
-{
- char stats[80];
- char line[80];
-
- if (verbose)
- sprintf(stats, " %s (%d - %d)", display_size(dp->dp_size),
- dp->dp_start, dp->dp_start + dp->dp_size);
- else
- stats[0] = '\0';
-
- switch (dp->dp_typ) {
- case DOSPTYP_386BSD:
- disk_printbsdslice(dev, (daddr_t)dp->dp_start,
- prefix, verbose);
- return;
- case DOSPTYP_LINSWP:
- sprintf(line, "%s: Linux swap%s\n", prefix, stats);
- break;
- case DOSPTYP_LINUX:
+ if (partition == -1 &&
+ part.type != PART_FREEBSD)
+ goto out;
+ /* Try to read BSD label */
+ table = ptable_open(dev, part.end - part.start + 1,
+ od->sectorsize, ptblread);
+ if (table == NULL) {
+ DEBUG("Can't read BSD label");
+ rc = ENXIO;
+ goto out;
+ }
/*
- * XXX
- * read the superblock to confirm this is an ext2fs partition?
+ * If slice contains BSD label and d_partition < 0, then
+ * assume the 'a' partition. Otherwise just return the
+ * whole MBR slice, because it can contain ZFS.
*/
- sprintf(line, "%s: ext2fs%s\n", prefix, stats);
- break;
- case 0x00: /* unused partition */
- case DOSPTYP_EXT:
- return;
- case 0x01:
- sprintf(line, "%s: FAT-12%s\n", prefix, stats);
- break;
- case 0x04:
- case 0x06:
- case 0x0e:
- sprintf(line, "%s: FAT-16%s\n", prefix, stats);
- break;
- case 0x07:
- sprintf(line, "%s: NTFS/HPFS%s\n", prefix, stats);
- break;
- case 0x0b:
- case 0x0c:
- sprintf(line, "%s: FAT-32%s\n", prefix, stats);
- break;
- default:
- sprintf(line, "%s: Unknown fs: 0x%x %s\n", prefix, dp->dp_typ,
- stats);
- }
- pager_output(line);
-}
-
-static int
-disk_printmbr(struct disk_devdesc *dev, char *prefix, int verbose)
-{
- struct dos_partition *slicetab;
- int nslices, i;
- int rc;
- char line[80];
-
- rc = disk_readslicetab(dev, &slicetab, &nslices);
- if (rc)
- return (rc);
- for (i = 0; i < nslices; i++) {
- sprintf(line, "%ss%d", prefix, i + 1);
- disk_printslice(dev, i, &slicetab[i], line, verbose);
- }
- free(slicetab);
- return (0);
-}
-
-#endif
-
-#ifdef LOADER_GPT_SUPPORT
-
-static int
-disk_readgpt(struct disk_devdesc *dev, struct gpt_part **gptp, int *ngptp)
-{
- struct dos_partition *dp;
- struct gpt_hdr *hdr;
- struct gpt_ent *ent;
- struct gpt_part *gptab = NULL;
- int entries_per_sec, rc, i, part;
- daddr_t lba, elba;
- uint8_t gpt[DISK_SECSIZE], tbl[DISK_SECSIZE];
-
- /*
- * Following calculations attempt to determine the correct value
- * for dev->d_offset by looking for the slice and partition specified,
- * or searching for reasonable defaults.
- */
- rc = 0;
-
- /* First, read the MBR and see if we have a PMBR. */
- rc = dev->d_dev->dv_strategy(dev, F_READ, 0, DISK_SECSIZE,
- (char *) tbl, NULL);
- if (rc) {
- DEBUG("error reading MBR");
- return (EIO);
- }
-
- /* Check the slice table magic. */
- if (tbl[0x1fe] != 0x55 || tbl[0x1ff] != 0xaa)
- return (ENXIO);
-
- /* Check for GPT slice. */
- part = 0;
- dp = (struct dos_partition *)(tbl + DOSPARTOFF);
- for (i = 0; i < NDOSPART; i++) {
- if (dp[i].dp_typ == 0xee)
- part++;
- else if ((part != 1) && (dp[i].dp_typ != 0x00))
- return (EINVAL);
- }
- if (part != 1)
- return (EINVAL);
-
- /* Read primary GPT table header. */
- rc = dev->d_dev->dv_strategy(dev, F_READ, 1, DISK_SECSIZE,
- (char *) gpt, NULL);
- if (rc) {
- DEBUG("error reading GPT header");
- return (EIO);
- }
- hdr = (struct gpt_hdr *)gpt;
- if (bcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0 ||
- hdr->hdr_lba_self != 1 || hdr->hdr_revision < 0x00010000 ||
- hdr->hdr_entsz < sizeof(*ent) ||
- DISK_SECSIZE % hdr->hdr_entsz != 0) {
- DEBUG("Invalid GPT header\n");
- return (EINVAL);
- }
-
- /* Walk the partition table to count valid partitions. */
- part = 0;
- entries_per_sec = DISK_SECSIZE / hdr->hdr_entsz;
- elba = hdr->hdr_lba_table + hdr->hdr_entries / entries_per_sec;
- for (lba = hdr->hdr_lba_table; lba < elba; lba++) {
- rc = dev->d_dev->dv_strategy(dev, F_READ, lba, DISK_SECSIZE,
- (char *) tbl, NULL);
- if (rc) {
- DEBUG("error reading GPT table");
- return (EIO);
+ if (partition < 0) {
+ if (ptable_gettype(table) != PTABLE_BSD)
+ goto out;
+ partition = 0;
}
- for (i = 0; i < entries_per_sec; i++) {
- ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
- if (uuid_is_nil(&ent->ent_type, NULL) ||
- ent->ent_lba_start == 0 ||
- ent->ent_lba_end < ent->ent_lba_start)
- continue;
- part++;
- }
+ rc = ptable_getpart(table, &part, partition);
+ if (rc != 0)
+ goto out;
+ dev->d_offset += part.start;
}
+out:
+ if (table != NULL)
+ ptable_close(table);
- /* Save the important information about all the valid partitions. */
- if (part != 0) {
- gptab = malloc(part * sizeof(struct gpt_part));
- part = 0;
- for (lba = hdr->hdr_lba_table; lba < elba; lba++) {
- rc = dev->d_dev->dv_strategy(dev, F_READ, lba, DISK_SECSIZE,
- (char *) tbl, NULL);
- if (rc) {
- DEBUG("error reading GPT table");
- free(gptab);
- return (EIO);
- }
- for (i = 0; i < entries_per_sec; i++) {
- ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
- if (uuid_is_nil(&ent->ent_type, NULL) ||
- ent->ent_lba_start == 0 ||
- ent->ent_lba_end < ent->ent_lba_start)
- continue;
- gptab[part].gp_index = (lba - hdr->hdr_lba_table) *
- entries_per_sec + i + 1;
- gptab[part].gp_type = ent->ent_type;
- gptab[part].gp_start = ent->ent_lba_start;
- gptab[part].gp_end = ent->ent_lba_end;
- part++;
- }
+ if (rc != 0) {
+ if (od->rcnt < 1) {
+ if (od->table != NULL)
+ ptable_close(od->table);
+ free(od);
}
+ DEBUG("%s could not open", disk_fmtdev(dev));
+ } else {
+ if ((flags & DISK_F_NOCACHE) == 0)
+ disk_insert(dev);
+ /* Save the slice and partition number to the dev */
+ dev->d_slice = slice;
+ dev->d_partition = partition;
+ DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
+ dev->d_offset, od);
}
-
- *gptp = gptab;
- *ngptp = part;
- return (0);
+ return (rc);
}
-static struct gpt_part *
-disk_bestgpt(struct gpt_part *gpt, int ngpt)
+int
+disk_close(struct disk_devdesc *dev)
{
- struct gpt_part *gp, *prefpart;
- int i, pref, preflevel;
+ struct open_disk *od;
- prefpart = NULL;
- preflevel = PREF_NONE;
-
- gp = gpt;
- for (i = 0; i < ngpt; i++, gp++) {
- /* Windows. XXX: Also Linux. */
- if (uuid_equal(&gp->gp_type, &ms_basic_data, NULL))
- pref = PREF_DOS;
- /* FreeBSD */
- else if (uuid_equal(&gp->gp_type, &freebsd_ufs, NULL) ||
- uuid_equal(&gp->gp_type, &freebsd_zfs, NULL) ||
- uuid_equal(&gp->gp_type, &midnightbsd_ufs, NULL) ||
- uuid_equal(&gp->gp_type, &midnightbsd_zfs, NULL))
- pref = PREF_FBSD;
- else
- pref = PREF_NONE;
- if (pref < preflevel) {
- preflevel = pref;
- prefpart = gp;
- }
+ od = (struct open_disk *)dev->d_opendata;
+ DEBUG("%s closed => %p [%d]", disk_fmtdev(dev), od, od->rcnt);
+ if (od->flags & DISK_F_NOCACHE) {
+ ptable_close(od->table);
+ free(od);
}
- return (prefpart);
+ return (0);
}
-static int
-disk_opengpt(struct disk_devdesc *dev)
+void
+disk_cleanup(const struct devsw *d_dev)
{
- struct gpt_part *gpt = NULL, *gp;
- int rc, ngpt, i;
+#ifdef DISK_DEBUG
+ struct disk_devdesc dev;
+#endif
+ struct dentry *entry, *tmp;
- rc = disk_readgpt(dev, &gpt, &ngpt);
- if (rc)
- return (rc);
-
- /* Is this a request for the whole disk? */
- if (dev->d_slice < 0) {
- dev->d_offset = 0;
- rc = 0;
- goto out;
- }
-
- /*
- * If a partition number was supplied, then the user is trying to use
- * an MBR address rather than a GPT address, so fail.
- */
- if (dev->d_partition != 0xff) {
- rc = ENOENT;
- goto out;
- }
-
- /* If a slice number was supplied but not found, this is an error. */
- gp = NULL;
- if (dev->d_slice > 0) {
- for (i = 0; i < ngpt; i++) {
- if (gpt[i].gp_index == dev->d_slice) {
- gp = &gpt[i];
- break;
- }
+ STAILQ_FOREACH_SAFE(entry, &opened_disks, entry, tmp) {
+ if (entry->d_dev != d_dev)
+ continue;
+ entry->od->rcnt--;
+#ifdef DISK_DEBUG
+ dev.d_dev = (struct devsw *)entry->d_dev;
+ dev.d_unit = entry->d_unit;
+ dev.d_slice = entry->d_slice;
+ dev.d_partition = entry->d_partition;
+ DEBUG("%s was freed => %p [%d]", disk_fmtdev(&dev),
+ entry->od, entry->od->rcnt);
+#endif
+ STAILQ_REMOVE(&opened_disks, entry, dentry, entry);
+ if (entry->od->rcnt < 1) {
+ if (entry->od->table != NULL)
+ ptable_close(entry->od->table);
+ free(entry->od);
}
- if (gp == NULL) {
- DEBUG("partition %d not found", dev->d_slice);
- rc = ENOENT;
- goto out;
- }
+ free(entry);
}
-
- /* Try to auto-detect the best partition. */
- if (dev->d_slice == 0) {
- gp = disk_bestgpt(gpt, ngpt);
- if (gp == NULL) {
- rc = ENOENT;
- goto out;
- }
- dev->d_slice = gp->gp_index;
- }
-
- dev->d_offset = gp->gp_start;
- rc = 0;
-
-out:
- if (gpt)
- free(gpt);
- return (rc);
}
-static void
-disk_printgptpart(struct disk_devdesc *dev, struct gpt_part *gp,
- char *prefix, int verbose)
+char*
+disk_fmtdev(struct disk_devdesc *dev)
{
- char stats[80];
- char line[96];
+ static char buf[128];
+ char *cp;
- if (verbose)
- sprintf(stats, " %s",
- display_size(gp->gp_end + 1 - gp->gp_start));
- else
- stats[0] = '\0';
-
- if (uuid_equal(&gp->gp_type, &efi, NULL))
- sprintf(line, "%s: EFI %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &ms_basic_data, NULL))
- sprintf(line, "%s: FAT/NTFS %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &midnightbsd_boot, NULL))
- sprintf(line, "%s: MidnightBSD boot%s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &midnightbsd_ufs, NULL))
- sprintf(line, "%s: MidnightBSD UFS %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &midnightbsd_zfs, NULL))
- sprintf(line, "%s: MidnightBSD ZFS %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &midnightbsd_swap, NULL))
- sprintf(line, "%s: MidnightBSD swap%s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &freebsd_boot, NULL))
- sprintf(line, "%s: FreeBSD boot%s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &freebsd_ufs, NULL))
- sprintf(line, "%s: FreeBSD UFS %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &freebsd_zfs, NULL))
- sprintf(line, "%s: FreeBSD ZFS %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &freebsd_swap, NULL))
- sprintf(line, "%s: FreeBSD swap%s\n", prefix, stats);
- else
- sprintf(line,
- "%s: %08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x%s\n",
- prefix,
- gp->gp_type.time_low, gp->gp_type.time_mid,
- gp->gp_type.time_hi_and_version,
- gp->gp_type.clock_seq_hi_and_reserved,
- gp->gp_type.clock_seq_low,
- gp->gp_type.node[0],
- gp->gp_type.node[1],
- gp->gp_type.node[2],
- gp->gp_type.node[3],
- gp->gp_type.node[4],
- gp->gp_type.node[5],
- stats);
- pager_output(line);
-}
-
-static int
-disk_printgpt(struct disk_devdesc *dev, char *prefix, int verbose)
-{
- struct gpt_part *gpt = NULL;
- int rc, ngpt, i;
- char line[80];
-
- rc = disk_readgpt(dev, &gpt, &ngpt);
- if (rc)
- return (rc);
- for (i = 0; i < ngpt; i++) {
- sprintf(line, "%sp%d", prefix, i + 1);
- disk_printgptpart(dev, &gpt[i], line, verbose);
- }
- free(gpt);
- return (0);
-}
-
-#endif
-
-int
-disk_open(struct disk_devdesc *dev)
-{
- int rc;
-
- rc = 0;
- /*
- * While we are reading disk metadata, make sure we do it relative
- * to the start of the disk
- */
- dev->d_offset = 0;
-
+ cp = buf + sprintf(buf, "%s%d", dev->d_dev->dv_name, dev->d_unit);
+ if (dev->d_slice >= 0) {
#ifdef LOADER_GPT_SUPPORT
- rc = disk_opengpt(dev);
- if (rc == 0)
- return (0);
+ if (dev->d_partition == 255) {
+ sprintf(cp, "p%d:", dev->d_slice);
+ return (buf);
+ } else
#endif
#ifdef LOADER_MBR_SUPPORT
- rc = disk_openmbr(dev);
+ cp += sprintf(cp, "s%d", dev->d_slice);
#endif
-
- return (rc);
+ }
+ if (dev->d_partition >= 0)
+ cp += sprintf(cp, "%c", dev->d_partition + 'a');
+ strcat(cp, ":");
+ return (buf);
}
-void
-disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
+int
+disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
{
+ int unit, slice, partition;
+ const char *np;
+ char *cp;
+ np = devspec;
+ unit = slice = partition = -1;
+ if (*np != '\0' && *np != ':') {
+ unit = strtol(np, &cp, 10);
+ if (cp == np)
+ return (EUNIT);
#ifdef LOADER_GPT_SUPPORT
- if (disk_printgpt(dev, prefix, verbose) == 0)
- return;
+ if (*cp == 'p') {
+ np = cp + 1;
+ slice = strtol(np, &cp, 10);
+ if (np == cp)
+ return (ESLICE);
+ /* we don't support nested partitions on GPT */
+ if (*cp != '\0' && *cp != ':')
+ return (EINVAL);
+ partition = 255;
+ } else
#endif
#ifdef LOADER_MBR_SUPPORT
- disk_printmbr(dev, prefix, verbose);
+ if (*cp == 's') {
+ np = cp + 1;
+ slice = strtol(np, &cp, 10);
+ if (np == cp)
+ return (ESLICE);
+ }
#endif
+ if (*cp != '\0' && *cp != ':') {
+ partition = *cp - 'a';
+ if (partition < 0)
+ return (EPART);
+ cp++;
+ }
+ } else
+ return (EINVAL);
+
+ if (*cp != '\0' && *cp != ':')
+ return (EINVAL);
+ dev->d_unit = unit;
+ dev->d_slice = slice;
+ dev->d_partition = partition;
+ if (path != NULL)
+ *path = (*cp == '\0') ? cp: cp + 1;
+ return (0);
}
Modified: trunk/sys/boot/common/disk.h
===================================================================
--- trunk/sys/boot/common/disk.h 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/common/disk.h 2016-09-18 20:24:57 UTC (rev 8410)
@@ -27,8 +27,7 @@
*/
/*
- * Device descriptor for partitioned disks. We assume that all disk addresses
- * are 512 byte block offsets from the start of the disk. To use, set the
+ * Device descriptor for partitioned disks. To use, set the
* d_slice and d_partition variables as follows:
*
* Whole disk access:
@@ -46,6 +45,11 @@
* d_slice = MBR slice number (typically 1..4)
* d_partition = disklabel partition (typically 0..7)
*
+ * BSD disklabel partition on the true dedicated disk:
+ *
+ * d_slice = -1
+ * d_partition = disklabel partition (typically 0..7)
+ *
* GPT partition:
*
* d_slice = GPT partition number (typically 1..N)
@@ -74,8 +78,6 @@
* the device's strategy method.
*/
-#define DISK_SECSIZE 512
-
struct disk_devdesc
{
struct devsw *d_dev;
@@ -84,16 +86,23 @@
void *d_opendata;
int d_slice;
int d_partition;
- int d_offset;
+ off_t d_offset;
};
/*
* Parse disk metadata and initialise dev->d_offset.
*/
-extern int disk_open(struct disk_devdesc * dev);
+extern int disk_open(struct disk_devdesc *dev, off_t mediasize,
+ u_int sectorsize, u_int flags);
+#define DISK_F_NOCACHE 0x0001 /* Do not use metadata caching */
+extern int disk_close(struct disk_devdesc *dev);
+extern void disk_cleanup(const struct devsw *d_dev);
/*
- * Print information about slices on a disk. For the size calculations we
- * assume a 512 byte sector.
+ * Print information about slices on a disk.
*/
extern void disk_print(struct disk_devdesc *dev, char *prefix, int verbose);
+extern char* disk_fmtdev(struct disk_devdesc *dev);
+extern int disk_parsedev(struct disk_devdesc *dev, const char *devspec,
+ const char **path);
+
Modified: trunk/sys/boot/common/module.c
===================================================================
--- trunk/sys/boot/common/module.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/common/module.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -271,6 +271,7 @@
int
file_load(char *filename, vm_offset_t dest, struct preloaded_file **result)
{
+ static int last_file_format = 0;
struct preloaded_file *fp;
int error;
int i;
@@ -279,12 +280,18 @@
dest = archsw.arch_loadaddr(LOAD_RAW, filename, dest);
error = EFTYPE;
- for (i = 0, fp = NULL; file_formats[i] && fp == NULL; i++) {
+ for (i = last_file_format, fp = NULL;
+ file_formats[i] && fp == NULL; i++) {
error = (file_formats[i]->l_load)(filename, dest, &fp);
if (error == 0) {
- fp->f_loader = i; /* remember the loader */
+ fp->f_loader = last_file_format = i; /* remember the loader */
*result = fp;
break;
+ } else if (last_file_format == i && i != 0) {
+ /* Restart from the beginning */
+ last_file_format = i = 0;
+ fp = NULL;
+ continue;
}
if (error == EFTYPE)
continue; /* Unknown to this handler? */
Modified: trunk/sys/boot/i386/libi386/Makefile
===================================================================
--- trunk/sys/boot/i386/libi386/Makefile 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/i386/libi386/Makefile 2016-09-18 20:24:57 UTC (rev 8410)
@@ -39,10 +39,6 @@
.endif
.endif
-.if !defined(LOADER_NO_GPT_SUPPORT)
-CFLAGS+= -DLOADER_GPT_SUPPORT
-.endif
-
# Include simple terminal emulation (cons25-compatible)
CFLAGS+= -DTERM_EMU
Modified: trunk/sys/boot/i386/libi386/biosdisk.c
===================================================================
--- trunk/sys/boot/i386/libi386/biosdisk.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/i386/libi386/biosdisk.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -1,6 +1,6 @@
/*-
- * Copyright (c) 2011 Lucas Holt <luke at foolishgames.com>
* Copyright (c) 1998 Michael Smith <msmith at freebsd.org>
+ * Copyright (c) 2012 Andrey V. Elsukov <ae at FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -26,8 +26,7 @@
*/
#include <sys/cdefs.h>
-__MBSDID("$MidnightBSD: src/sys/boot/i386/libi386/biosdisk.c,v 1.5 2012/07/21 16:42:00 laffer1 Exp $");
-/* $FreeBSD: src/sys/boot/i386/libi386/biosdisk.c,v 1.48.2.5.2.1 2008/11/25 02:59:29 kensmith Exp $ */
+__MBSDID("$MidnightBSD$");
/*
* BIOS disk device handling.
@@ -39,21 +38,19 @@
*
*/
+#include <sys/disk.h>
#include <stand.h>
-
-#include <sys/disklabel.h>
-#include <sys/diskmbr.h>
-#include <sys/gpt.h>
#include <machine/bootinfo.h>
-
#include <stdarg.h>
-#include <uuid.h>
#include <bootstrap.h>
#include <btxv86.h>
#include <edd.h>
+#include "disk.h"
#include "libi386.h"
+CTASSERT(sizeof(struct i386_devdesc) >= sizeof(struct disk_devdesc));
+
#define BIOS_NUMDRIVES 0x475
#define BIOSDISK_SECSIZE 512
#define BUFSIZE (1 * BIOSDISK_SECSIZE)
@@ -70,56 +67,6 @@
# define DEBUG(fmt, args...)
#endif
-#ifdef LOADER_GPT_SUPPORT
-struct gpt_part {
- int gp_index;
- uuid_t gp_type;
- uint64_t gp_start;
- uint64_t gp_end;
-};
-#endif
-
-struct open_disk {
- int od_dkunit; /* disk unit number */
- int od_unit; /* BIOS unit number */
- int od_cyl; /* BIOS geometry */
- int od_hds;
- int od_sec;
- daddr_t od_boff; /* block offset from beginning of BIOS disk */
- int od_flags;
-#define BD_MODEINT13 0x0000
-#define BD_MODEEDD1 0x0001
-#define BD_MODEEDD3 0x0002
-#define BD_MODEMASK 0x0003
-#define BD_FLOPPY 0x0004
-#define BD_LABELOK 0x0008
-#define BD_PARTTABOK 0x0010
-#ifdef LOADER_GPT_SUPPORT
-#define BD_GPTOK 0x0020
-#endif
- union {
- struct {
- struct disklabel mbr_disklabel;
- int mbr_nslices; /* slice count */
- struct dos_partition mbr_slicetab[NEXTDOSPART];
- } _mbr;
-#ifdef LOADER_GPT_SUPPORT
- struct {
- int gpt_nparts;
- struct gpt_part *gpt_partitions;
- } _gpt;
-#endif
- } _data;
-};
-
-#define od_disklabel _data._mbr.mbr_disklabel
-#define od_nslices _data._mbr.mbr_nslices
-#define od_slicetab _data._mbr.mbr_slicetab
-#ifdef LOADER_GPT_SUPPORT
-#define od_nparts _data._gpt.gpt_nparts
-#define od_partitions _data._gpt.gpt_partitions
-#endif
-
/*
* List of BIOS devices, translation from disk unit number to
* BIOS unit number.
@@ -126,60 +73,53 @@
*/
static struct bdinfo
{
- int bd_unit; /* BIOS unit number */
- int bd_flags;
- int bd_type; /* BIOS 'drive type' (floppy only) */
+ int bd_unit; /* BIOS unit number */
+ int bd_cyl; /* BIOS geometry */
+ int bd_hds;
+ int bd_sec;
+ int bd_flags;
+#define BD_MODEINT13 0x0000
+#define BD_MODEEDD1 0x0001
+#define BD_MODEEDD3 0x0002
+#define BD_MODEMASK 0x0003
+#define BD_FLOPPY 0x0004
+ int bd_type; /* BIOS 'drive type' (floppy only) */
+ uint16_t bd_sectorsize; /* Sector size */
+ uint64_t bd_sectors; /* Disk size */
} bdinfo [MAXBDDEV];
static int nbdinfo = 0;
-static int bd_getgeom(struct open_disk *od);
-static int bd_read(struct open_disk *od, daddr_t dblk, int blks,
- caddr_t dest);
-static int bd_write(struct open_disk *od, daddr_t dblk, int blks,
- caddr_t dest);
+#define BD(dev) (bdinfo[(dev)->d_unit])
-static int bd_int13probe(struct bdinfo *bd);
+static int bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks,
+ caddr_t dest);
+static int bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks,
+ caddr_t dest);
+static int bd_int13probe(struct bdinfo *bd);
-#ifdef LOADER_GPT_SUPPORT
-static void bd_printgptpart(struct open_disk *od, struct gpt_part *gp,
- char *prefix, int verbose);
-#endif
-static void bd_printslice(struct open_disk *od, struct dos_partition *dp,
- char *prefix, int verbose);
-static void bd_printbsdslice(struct open_disk *od, daddr_t offset,
- char *prefix, int verbose);
+static int bd_init(void);
+static int bd_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
+ char *buf, size_t *rsize);
+static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
+ size_t size, char *buf, size_t *rsize);
+static int bd_open(struct open_file *f, ...);
+static int bd_close(struct open_file *f);
+static int bd_ioctl(struct open_file *f, u_long cmd, void *data);
+static void bd_print(int verbose);
+static void bd_cleanup(void);
-static int bd_init(void);
-static int bd_strategy(void *devdata, int flag, daddr_t dblk,
- size_t size, char *buf, size_t *rsize);
-static int bd_realstrategy(void *devdata, int flag, daddr_t dblk,
- size_t size, char *buf, size_t *rsize);
-static int bd_open(struct open_file *f, ...);
-static int bd_close(struct open_file *f);
-static void bd_print(int verbose);
-
struct devsw biosdisk = {
- "disk",
- DEVT_DISK,
- bd_init,
- bd_strategy,
- bd_open,
- bd_close,
- noioctl,
- bd_print,
- NULL
+ "disk",
+ DEVT_DISK,
+ bd_init,
+ bd_strategy,
+ bd_open,
+ bd_close,
+ bd_ioctl,
+ bd_print,
+ bd_cleanup
};
-static int bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev);
-static void bd_closedisk(struct open_disk *od);
-static int bd_open_mbr(struct open_disk *od, struct i386_devdesc *dev);
-static int bd_bestslice(struct open_disk *od);
-static void bd_checkextended(struct open_disk *od, int slicenum);
-#ifdef LOADER_GPT_SUPPORT
-static int bd_open_gpt(struct open_disk *od, struct i386_devdesc *dev);
-static struct gpt_part *bd_best_gptpart(struct open_disk *od);
-#endif
-
/*
* Translate between BIOS device numbers and our private unit numbers.
*/
@@ -186,58 +126,67 @@
int
bd_bios2unit(int biosdev)
{
- int i;
-
- DEBUG("looking for bios device 0x%x", biosdev);
- for (i = 0; i < nbdinfo; i++) {
- DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
- if (bdinfo[i].bd_unit == biosdev)
- return(i);
- }
- return(-1);
+ int i;
+
+ DEBUG("looking for bios device 0x%x", biosdev);
+ for (i = 0; i < nbdinfo; i++) {
+ DEBUG("bd unit %d is BIOS device 0x%x", i, bdinfo[i].bd_unit);
+ if (bdinfo[i].bd_unit == biosdev)
+ return (i);
+ }
+ return (-1);
}
int
bd_unit2bios(int unit)
{
- if ((unit >= 0) && (unit < nbdinfo))
- return(bdinfo[unit].bd_unit);
- return(-1);
+
+ if ((unit >= 0) && (unit < nbdinfo))
+ return (bdinfo[unit].bd_unit);
+ return (-1);
}
-/*
+/*
* Quiz the BIOS for disk devices, save a little info about them.
*/
static int
-bd_init(void)
+bd_init(void)
{
- int base, unit, nfd = 0;
+ int base, unit, nfd = 0;
- /* sequence 0, 0x80 */
- for (base = 0; base <= 0x80; base += 0x80) {
- for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
+ /* sequence 0, 0x80 */
+ for (base = 0; base <= 0x80; base += 0x80) {
+ for (unit = base; (nbdinfo < MAXBDDEV); unit++) {
#ifndef VIRTUALBOX
- /* check the BIOS equipment list for number of fixed disks */
- if((base == 0x80) &&
- (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
- break;
+ /*
+ * Check the BIOS equipment list for number
+ * of fixed disks.
+ */
+ if(base == 0x80 &&
+ (nfd >= *(unsigned char *)PTOV(BIOS_NUMDRIVES)))
+ break;
#endif
+ bdinfo[nbdinfo].bd_unit = unit;
+ bdinfo[nbdinfo].bd_flags = unit < 0x80 ? BD_FLOPPY: 0;
+ if (!bd_int13probe(&bdinfo[nbdinfo]))
+ break;
- bdinfo[nbdinfo].bd_unit = unit;
- bdinfo[nbdinfo].bd_flags = (unit < 0x80) ? BD_FLOPPY : 0;
+ /* XXX we need "disk aliases" to make this simpler */
+ printf("BIOS drive %c: is disk%d\n", (unit < 0x80) ?
+ ('A' + unit): ('C' + unit - 0x80), nbdinfo);
+ nbdinfo++;
+ if (base == 0x80)
+ nfd++;
+ }
+ }
+ return(0);
+}
- if (!bd_int13probe(&bdinfo[nbdinfo]))
- break;
+static void
+bd_cleanup(void)
+{
- /* XXX we need "disk aliases" to make this simpler */
- printf("BIOS drive %c: is disk%d\n",
- (unit < 0x80) ? ('A' + unit) : ('C' + unit - 0x80), nbdinfo);
- nbdinfo++;
- if (base == 0x80)
- nfd++;
- }
- }
- return(0);
+ disk_cleanup(&biosdisk);
}
/*
@@ -246,36 +195,64 @@
static int
bd_int13probe(struct bdinfo *bd)
{
- v86.ctl = V86_FLAGS;
- v86.addr = 0x13;
- v86.eax = 0x800;
- v86.edx = bd->bd_unit;
- v86int();
-
- if (!(V86_CY(v86.efl)) && /* carry clear */
- ((v86.edx & 0xff) > ((unsigned)bd->bd_unit & 0x7f))) { /* unit # OK */
- if ((v86.ecx & 0x3f) == 0) { /* absurd sector size */
- DEBUG("Invalid geometry for unit %d", bd->bd_unit);
- return(0); /* skip device */
- }
+ struct edd_params params;
+
+ v86.ctl = V86_FLAGS;
+ v86.addr = 0x13;
+ v86.eax = 0x800;
+ v86.edx = bd->bd_unit;
+ v86int();
+
+ if (V86_CY(v86.efl) || /* carry set */
+ (v86.ecx & 0x3f) == 0 || /* absurd sector number */
+ (v86.edx & 0xff) <= (unsigned)(bd->bd_unit & 0x7f)) /* unit # bad */
+ return (0); /* skip device */
+
+ /* Convert max cyl # -> # of cylinders */
+ bd->bd_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
+ /* Convert max head # -> # of heads */
+ bd->bd_hds = ((v86.edx & 0xff00) >> 8) + 1;
+ bd->bd_sec = v86.ecx & 0x3f;
+ bd->bd_type = v86.ebx & 0xff;
bd->bd_flags |= BD_MODEINT13;
- bd->bd_type = v86.ebx & 0xff;
+ /* Calculate sectors count from the geometry */
+ bd->bd_sectors = bd->bd_cyl * bd->bd_hds * bd->bd_sec;
+ bd->bd_sectorsize = BIOSDISK_SECSIZE;
+ DEBUG("unit 0x%x geometry %d/%d/%d", bd->bd_unit, bd->bd_cyl,
+ bd->bd_hds, bd->bd_sec);
+
/* Determine if we can use EDD with this device. */
+ v86.ctl = V86_FLAGS;
+ v86.addr = 0x13;
v86.eax = 0x4100;
v86.edx = bd->bd_unit;
v86.ebx = 0x55aa;
v86int();
- if (!(V86_CY(v86.efl)) && /* carry clear */
- ((v86.ebx & 0xffff) == 0xaa55) && /* signature */
- (v86.ecx & EDD_INTERFACE_FIXED_DISK)) { /* packets mode ok */
- bd->bd_flags |= BD_MODEEDD1;
- if ((v86.eax & 0xff00) >= 0x3000)
- bd->bd_flags |= BD_MODEEDD3;
+ if (V86_CY(v86.efl) || /* carry set */
+ (v86.ebx & 0xffff) != 0xaa55 || /* signature */
+ (v86.ecx & EDD_INTERFACE_FIXED_DISK) == 0)
+ return (1);
+ /* EDD supported */
+ bd->bd_flags |= BD_MODEEDD1;
+ if ((v86.eax & 0xff00) >= 0x3000)
+ bd->bd_flags |= BD_MODEEDD3;
+ /* Get disk params */
+ params.len = sizeof(struct edd_params);
+ v86.ctl = V86_FLAGS;
+ v86.addr = 0x13;
+ v86.eax = 0x4800;
+ v86.edx = bd->bd_unit;
+ v86.ds = VTOPSEG(¶ms);
+ v86.esi = VTOPOFF(¶ms);
+ v86int();
+ if (!V86_CY(v86.efl)) {
+ bd->bd_sectors = params.sectors;
+ bd->bd_sectorsize = params.sector_size;
}
- return(1);
- }
- return(0);
+ DEBUG("unit 0x%x flags %x, sectors %llu, sectorsize %u",
+ bd->bd_unit, bd->bd_flags, bd->bd_sectors, bd->bd_sectorsize);
+ return (1);
}
/*
@@ -284,238 +261,32 @@
static void
bd_print(int verbose)
{
- int i, j;
- char line[80];
- struct i386_devdesc dev;
- struct open_disk *od;
- struct dos_partition *dptr;
-
- for (i = 0; i < nbdinfo; i++) {
- sprintf(line, " disk%d: BIOS drive %c:\n", i,
- (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit) : ('C' + bdinfo[i].bd_unit - 0x80));
- pager_output(line);
+ static char line[80];
+ struct disk_devdesc dev;
+ int i;
- /* try to open the whole disk */
- dev.d_unit = i;
- dev.d_kind.biosdisk.slice = -1;
- dev.d_kind.biosdisk.partition = -1;
-
- if (!bd_opendisk(&od, &dev)) {
-
-#ifdef LOADER_GPT_SUPPORT
- /* Do we have a GPT table? */
- if (od->od_flags & BD_GPTOK) {
- for (j = 0; j < od->od_nparts; j++) {
- sprintf(line, " disk%dp%d", i,
- od->od_partitions[j].gp_index);
- bd_printgptpart(od, &od->od_partitions[j], line, verbose);
+ for (i = 0; i < nbdinfo; i++) {
+ sprintf(line, " disk%d: BIOS drive %c:\n", i,
+ (bdinfo[i].bd_unit < 0x80) ? ('A' + bdinfo[i].bd_unit):
+ ('C' + bdinfo[i].bd_unit - 0x80));
+ pager_output(line);
+ dev.d_dev = &biosdisk;
+ dev.d_unit = i;
+ dev.d_slice = -1;
+ dev.d_partition = -1;
+ if (disk_open(&dev,
+ bdinfo[i].bd_sectorsize * bdinfo[i].bd_sectors,
+ bdinfo[i].bd_sectorsize,
+ (bdinfo[i].bd_flags & BD_FLOPPY) ?
+ DISK_F_NOCACHE: 0) == 0) {
+ sprintf(line, " disk%d", i);
+ disk_print(&dev, line, verbose);
+ disk_close(&dev);
}
- } else
-#endif
- /* Do we have a partition table? */
- if (od->od_flags & BD_PARTTABOK) {
- dptr = &od->od_slicetab[0];
-
- /* Check for a "dedicated" disk */
- if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
- (dptr[3].dp_start == 0) &&
- (dptr[3].dp_size == 50000)) {
- sprintf(line, " disk%d", i);
- bd_printbsdslice(od, 0, line, verbose);
- } else {
- for (j = 0; j < od->od_nslices; j++) {
- sprintf(line, " disk%ds%d", i, j + 1);
- bd_printslice(od, &dptr[j], line, verbose);
- }
- }
- }
- bd_closedisk(od);
}
- }
}
-/* Given a size in 512 byte sectors, convert it to a human-readable number. */
-static char *
-display_size(uint64_t size)
-{
- static char buf[80];
- char unit;
-
- size /= 2;
- unit = 'K';
- if (size >= 10485760000LL) {
- size /= 1073741824;
- unit = 'T';
- } else if (size >= 10240000) {
- size /= 1048576;
- unit = 'G';
- } else if (size >= 10000) {
- size /= 1024;
- unit = 'M';
- }
- sprintf(buf, "%.6ld%cB", (long)size, unit);
- return (buf);
-}
-
-#ifdef LOADER_GPT_SUPPORT
-static uuid_t efi = GPT_ENT_TYPE_EFI;
-static uuid_t midnightbsd_boot = GPT_ENT_TYPE_MIDNIGHTBSD_BOOT;
-static uuid_t midnightbsd_ufs = GPT_ENT_TYPE_MIDNIGHTBSD_UFS;
-static uuid_t midnightbsd_swap = GPT_ENT_TYPE_MIDNIGHTBSD_SWAP;
-static uuid_t midnightbsd_zfs = GPT_ENT_TYPE_MIDNIGHTBSD_ZFS;
-static uuid_t ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
-
-static void
-bd_printgptpart(struct open_disk *od, struct gpt_part *gp, char *prefix,
- int verbose)
-{
- char stats[80];
- char line[96];
-
- if (verbose)
- sprintf(stats, " %s", display_size(gp->gp_end + 1 - gp->gp_start));
- else
- stats[0] = '\0';
-
- if (uuid_equal(&gp->gp_type, &efi, NULL))
- sprintf(line, "%s: EFI %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &ms_basic_data, NULL))
- sprintf(line, "%s: FAT/NTFS %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &midnightbsd_boot, NULL))
- sprintf(line, "%s: MidnightBSD boot%s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &midnightbsd_ufs, NULL))
- sprintf(line, "%s: MidnightBSD UFS %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &midnightbsd_zfs, NULL))
- sprintf(line, "%s: MidnightBSD ZFS %s\n", prefix, stats);
- else if (uuid_equal(&gp->gp_type, &midnightbsd_swap, NULL))
- sprintf(line, "%s: MidnightBSD swap%s\n", prefix, stats);
- else
- sprintf(line, "%s: %08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x%s\n",
- prefix,
- gp->gp_type.time_low, gp->gp_type.time_mid,
- gp->gp_type.time_hi_and_version,
- gp->gp_type.clock_seq_hi_and_reserved, gp->gp_type.clock_seq_low,
- gp->gp_type.node[0], gp->gp_type.node[1], gp->gp_type.node[2],
- gp->gp_type.node[3], gp->gp_type.node[4], gp->gp_type.node[5],
- stats);
- pager_output(line);
-}
-#endif
-
/*
- * Print information about slices on a disk. For the size calculations we
- * assume a 512 byte sector.
- */
-static void
-bd_printslice(struct open_disk *od, struct dos_partition *dp, char *prefix,
- int verbose)
-{
- char stats[80];
- char line[80];
-
- if (verbose)
- sprintf(stats, " %s (%d - %d)", display_size(dp->dp_size),
- dp->dp_start, dp->dp_start + dp->dp_size);
- else
- stats[0] = '\0';
-
- switch (dp->dp_typ) {
- case DOSPTYP_386BSD:
- bd_printbsdslice(od, (daddr_t)dp->dp_start, prefix, verbose);
- return;
- case DOSPTYP_LINSWP:
- sprintf(line, "%s: Linux swap%s\n", prefix, stats);
- break;
- case DOSPTYP_LINUX:
- /*
- * XXX
- * read the superblock to confirm this is an ext2fs partition?
- */
- sprintf(line, "%s: ext2fs%s\n", prefix, stats);
- break;
- case 0x00: /* unused partition */
- case DOSPTYP_EXT:
- return;
- case 0x01:
- sprintf(line, "%s: FAT-12%s\n", prefix, stats);
- break;
- case 0x04:
- case 0x06:
- case 0x0e:
- sprintf(line, "%s: FAT-16%s\n", prefix, stats);
- break;
- case 0x07:
- sprintf(line, "%s: NTFS/HPFS%s\n", prefix, stats);
- break;
- case 0x0b:
- case 0x0c:
- sprintf(line, "%s: FAT-32%s\n", prefix, stats);
- break;
- default:
- sprintf(line, "%s: Unknown fs: 0x%x %s\n", prefix, dp->dp_typ,
- stats);
- }
- pager_output(line);
-}
-
-/*
- * Print out each valid partition in the disklabel of a FreeBSD slice.
- * For size calculations, we assume a 512 byte sector size.
- */
-static void
-bd_printbsdslice(struct open_disk *od, daddr_t offset, char *prefix,
- int verbose)
-{
- char line[80];
- char buf[BIOSDISK_SECSIZE];
- struct disklabel *lp;
- int i;
-
- /* read disklabel */
- if (bd_read(od, offset + LABELSECTOR, 1, buf))
- return;
- lp =(struct disklabel *)(&buf[0]);
- if (lp->d_magic != DISKMAGIC) {
- sprintf(line, "%s: FFS bad disklabel\n", prefix);
- pager_output(line);
- return;
- }
-
- /* Print partitions */
- for (i = 0; i < lp->d_npartitions; i++) {
- /*
- * For each partition, make sure we know what type of fs it is. If
- * not, then skip it. However, since floppies often have bogus
- * fstypes, print the 'a' partition on a floppy even if it is marked
- * unused.
- */
- if ((lp->d_partitions[i].p_fstype == FS_BSDFFS) ||
- (lp->d_partitions[i].p_fstype == FS_SWAP) ||
- (lp->d_partitions[i].p_fstype == FS_VINUM) ||
- ((lp->d_partitions[i].p_fstype == FS_UNUSED) &&
- (od->od_flags & BD_FLOPPY) && (i == 0))) {
-
- /* Only print out statistics in verbose mode */
- if (verbose)
- sprintf(line, " %s%c: %s %s (%d - %d)\n", prefix, 'a' + i,
- (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap " :
- (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
- "FFS ",
- display_size(lp->d_partitions[i].p_size),
- lp->d_partitions[i].p_offset,
- lp->d_partitions[i].p_offset + lp->d_partitions[i].p_size);
- else
- sprintf(line, " %s%c: %s\n", prefix, 'a' + i,
- (lp->d_partitions[i].p_fstype == FS_SWAP) ? "swap" :
- (lp->d_partitions[i].p_fstype == FS_VINUM) ? "vinum" :
- "FFS");
- pager_output(line);
- }
- }
-}
-
-
-/*
* Attempt to open the disk described by (dev) for use by (f).
*
* Note that the philosophy here is "give them exactly what
@@ -525,564 +296,84 @@
* sliced - are they after the first BSD slice, or the DOS
* slice before it?)
*/
-static int
+static int
bd_open(struct open_file *f, ...)
{
- va_list ap;
- struct i386_devdesc *dev;
- struct open_disk *od;
- int error;
+ struct disk_devdesc *dev;
+ va_list ap;
- va_start(ap, f);
- dev = va_arg(ap, struct i386_devdesc *);
- va_end(ap);
- if ((error = bd_opendisk(&od, dev)))
- return(error);
-
- /*
- * Save our context
- */
- ((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data = od;
- DEBUG("open_disk %p, partition at 0x%x", od, od->od_boff);
- return(0);
-}
+ va_start(ap, f);
+ dev = va_arg(ap, struct disk_devdesc *);
+ va_end(ap);
-static int
-bd_opendisk(struct open_disk **odp, struct i386_devdesc *dev)
-{
- struct open_disk *od;
- int error;
+ if (dev->d_unit < 0 || dev->d_unit >= nbdinfo)
+ return (EIO);
- if (dev->d_unit >= nbdinfo) {
- DEBUG("attempt to open nonexistent disk");
- return(ENXIO);
- }
-
- od = (struct open_disk *)malloc(sizeof(struct open_disk));
- if (!od) {
- DEBUG("no memory");
- return (ENOMEM);
- }
-
- /* Look up BIOS unit number, initalise open_disk structure */
- od->od_dkunit = dev->d_unit;
- od->od_unit = bdinfo[od->od_dkunit].bd_unit;
- od->od_flags = bdinfo[od->od_dkunit].bd_flags;
- od->od_boff = 0;
- error = 0;
- DEBUG("open '%s', unit 0x%x slice %d partition %d",
- i386_fmtdev(dev), dev->d_unit,
- dev->d_kind.biosdisk.slice, dev->d_kind.biosdisk.partition);
-
- /* Get geometry for this open (removable device may have changed) */
- if (bd_getgeom(od)) {
- DEBUG("can't get geometry");
- error = ENXIO;
- goto out;
- }
-
- /* Determine disk layout. */
-#ifdef LOADER_GPT_SUPPORT
- error = bd_open_gpt(od, dev);
- if (error)
-#endif
- error = bd_open_mbr(od, dev);
-
- out:
- if (error) {
- free(od);
- } else {
- *odp = od; /* return the open disk */
- }
- return(error);
+ return (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
+ BD(dev).bd_sectorsize, (BD(dev).bd_flags & BD_FLOPPY) ?
+ DISK_F_NOCACHE: 0));
}
static int
-bd_open_mbr(struct open_disk *od, struct i386_devdesc *dev)
+bd_close(struct open_file *f)
{
- struct dos_partition *dptr;
- struct disklabel *lp;
- int sector, slice, i;
- int error;
- char buf[BUFSIZE];
+ struct disk_devdesc *dev;
- /*
- * Following calculations attempt to determine the correct value
- * for d->od_boff by looking for the slice and partition specified,
- * or searching for reasonable defaults.
- */
-
- /*
- * Find the slice in the DOS slice table.
- */
- od->od_nslices = 0;
- if (bd_read(od, 0, 1, buf)) {
- DEBUG("error reading MBR");
- return (EIO);
- }
-
- /*
- * Check the slice table magic.
- */
- if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
- /* If a slice number was explicitly supplied, this is an error */
- if (dev->d_kind.biosdisk.slice > 0) {
- DEBUG("no slice table/MBR (no magic)");
- return (ENOENT);
- }
- sector = 0;
- goto unsliced; /* may be a floppy */
- }
-
- /*
- * copy the partition table, then pick up any extended partitions.
- */
- bcopy(buf + DOSPARTOFF, &od->od_slicetab,
- sizeof(struct dos_partition) * NDOSPART);
- od->od_nslices = 4; /* extended slices start here */
- for (i = 0; i < NDOSPART; i++)
- bd_checkextended(od, i);
- od->od_flags |= BD_PARTTABOK;
- dptr = &od->od_slicetab[0];
-
- /* Is this a request for the whole disk? */
- if (dev->d_kind.biosdisk.slice == -1) {
- sector = 0;
- goto unsliced;
- }
-
- /*
- * if a slice number was supplied but not found, this is an error.
- */
- if (dev->d_kind.biosdisk.slice > 0) {
- slice = dev->d_kind.biosdisk.slice - 1;
- if (slice >= od->od_nslices) {
- DEBUG("slice %d not found", slice);
- return (ENOENT);
- }
- }
-
- /*
- * Check for the historically bogus MBR found on true dedicated disks
- */
- if ((dptr[3].dp_typ == DOSPTYP_386BSD) &&
- (dptr[3].dp_start == 0) &&
- (dptr[3].dp_size == 50000)) {
- sector = 0;
- goto unsliced;
- }
-
- /* Try to auto-detect the best slice; this should always give a slice number */
- if (dev->d_kind.biosdisk.slice == 0) {
- slice = bd_bestslice(od);
- if (slice == -1) {
- return (ENOENT);
- }
- dev->d_kind.biosdisk.slice = slice;
- }
-
- dptr = &od->od_slicetab[0];
- /*
- * Accept the supplied slice number unequivocally (we may be looking
- * at a DOS partition).
- */
- dptr += (dev->d_kind.biosdisk.slice - 1); /* we number 1-4, offsets are 0-3 */
- sector = dptr->dp_start;
- DEBUG("slice entry %d at %d, %d sectors", dev->d_kind.biosdisk.slice - 1, sector, dptr->dp_size);
-
- /*
- * If we are looking at a BSD slice, and the partition is < 0, assume the 'a' partition
- */
- if ((dptr->dp_typ == DOSPTYP_386BSD) && (dev->d_kind.biosdisk.partition < 0))
- dev->d_kind.biosdisk.partition = 0;
-
- unsliced:
- /*
- * Now we have the slice offset, look for the partition in the disklabel if we have
- * a partition to start with.
- *
- * XXX we might want to check the label checksum.
- */
- if (dev->d_kind.biosdisk.partition < 0) {
- od->od_boff = sector; /* no partition, must be after the slice */
- DEBUG("opening raw slice");
- } else {
-
- if (bd_read(od, sector + LABELSECTOR, 1, buf)) {
- DEBUG("error reading disklabel");
- return (EIO);
- }
- DEBUG("copy %d bytes of label from %p to %p", sizeof(struct disklabel), buf + LABELOFFSET, &od->od_disklabel);
- bcopy(buf + LABELOFFSET, &od->od_disklabel, sizeof(struct disklabel));
- lp = &od->od_disklabel;
- od->od_flags |= BD_LABELOK;
-
- if (lp->d_magic != DISKMAGIC) {
- DEBUG("no disklabel");
- return (ENOENT);
- }
- if (dev->d_kind.biosdisk.partition >= lp->d_npartitions) {
- DEBUG("partition '%c' exceeds partitions in table (a-'%c')",
- 'a' + dev->d_kind.biosdisk.partition, 'a' + lp->d_npartitions);
- return (EPART);
- }
-
-#ifdef DISK_DEBUG
- /* Complain if the partition is unused unless this is a floppy. */
- if ((lp->d_partitions[dev->d_kind.biosdisk.partition].p_fstype == FS_UNUSED) &&
- !(od->od_flags & BD_FLOPPY))
- DEBUG("warning, partition marked as unused");
-#endif
-
- od->od_boff =
- lp->d_partitions[dev->d_kind.biosdisk.partition].p_offset -
- lp->d_partitions[RAW_PART].p_offset +
- sector;
- }
- return (0);
+ dev = (struct disk_devdesc *)f->f_devdata;
+ return (disk_close(dev));
}
-static void
-bd_checkextended(struct open_disk *od, int slicenum)
+static int
+bd_ioctl(struct open_file *f, u_long cmd, void *data)
{
- char buf[BIOSDISK_SECSIZE];
- struct dos_partition *dp;
- u_int base;
- int i, start, end;
+ struct disk_devdesc *dev;
- dp = &od->od_slicetab[slicenum];
- start = od->od_nslices;
-
- if (dp->dp_size == 0)
- goto done;
- if (dp->dp_typ != DOSPTYP_EXT)
- goto done;
- if (bd_read(od, (daddr_t)dp->dp_start, 1, buf))
- goto done;
- if (((u_char)buf[0x1fe] != 0x55) || ((u_char)buf[0x1ff] != 0xaa)) {
- DEBUG("no magic in extended table");
- goto done;
+ dev = (struct disk_devdesc *)f->f_devdata;
+ switch (cmd) {
+ case DIOCGSECTORSIZE:
+ *(u_int *)data = BD(dev).bd_sectorsize;
+ break;
+ case DIOCGMEDIASIZE:
+ *(off_t *)data = BD(dev).bd_sectors * BD(dev).bd_sectorsize;
+ break;
+ default:
+ return (ENOTTY);
}
- base = dp->dp_start;
- dp = (struct dos_partition *)(&buf[DOSPARTOFF]);
- for (i = 0; i < NDOSPART; i++, dp++) {
- if (dp->dp_size == 0)
- continue;
- if (od->od_nslices == NEXTDOSPART)
- goto done;
- dp->dp_start += base;
- bcopy(dp, &od->od_slicetab[od->od_nslices], sizeof(*dp));
- od->od_nslices++;
- }
- end = od->od_nslices;
-
- /*
- * now, recursively check the slices we just added
- */
- for (i = start; i < end; i++)
- bd_checkextended(od, i);
-done:
- return;
+ return (0);
}
-/*
- * Search for a slice with the following preferences:
- *
- * 1: Active FreeBSD slice
- * 2: Non-active FreeBSD slice
- * 3: Active Linux slice
- * 4: non-active Linux slice
- * 5: Active FAT/FAT32 slice
- * 6: non-active FAT/FAT32 slice
- */
-#define PREF_RAWDISK 0
-#define PREF_FBSD_ACT 1
-#define PREF_FBSD 2
-#define PREF_LINUX_ACT 3
-#define PREF_LINUX 4
-#define PREF_DOS_ACT 5
-#define PREF_DOS 6
-#define PREF_NONE 7
-
-/*
- * slicelimit is in the range 0 .. NDOSPART
- */
static int
-bd_bestslice(struct open_disk *od)
+bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf,
+ size_t *rsize)
{
- struct dos_partition *dp;
- int pref, preflevel;
- int i, prefslice;
-
- prefslice = 0;
- preflevel = PREF_NONE;
+ struct bcache_devdata bcd;
+ struct disk_devdesc *dev;
- dp = &od->od_slicetab[0];
- for (i = 0; i < od->od_nslices; i++, dp++) {
-
- switch (dp->dp_typ) {
- case DOSPTYP_386BSD: /* FreeBSD */
- pref = dp->dp_flag & 0x80 ? PREF_FBSD_ACT : PREF_FBSD;
- break;
-
- case DOSPTYP_LINUX:
- pref = dp->dp_flag & 0x80 ? PREF_LINUX_ACT : PREF_LINUX;
- break;
-
- case 0x01: /* DOS/Windows */
- case 0x04:
- case 0x06:
- case 0x0b:
- case 0x0c:
- case 0x0e:
- pref = dp->dp_flag & 0x80 ? PREF_DOS_ACT : PREF_DOS;
- break;
-
- default:
- pref = PREF_NONE;
- }
- if (pref < preflevel) {
- preflevel = pref;
- prefslice = i + 1;
- }
- }
- return (prefslice);
+ dev = (struct disk_devdesc *)devdata;
+ bcd.dv_strategy = bd_realstrategy;
+ bcd.dv_devdata = devdata;
+ return (bcache_strategy(&bcd, BD(dev).bd_unit, rw, dblk + dev->d_offset,
+ size, buf, rsize));
}
-#ifdef LOADER_GPT_SUPPORT
static int
-bd_open_gpt(struct open_disk *od, struct i386_devdesc *dev)
+bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf,
+ size_t *rsize)
{
- struct dos_partition *dp;
- struct gpt_hdr *hdr;
- struct gpt_ent *ent;
- struct gpt_part *gp;
- int entries_per_sec, error, i, part;
- daddr_t lba, elba;
- char gpt[BIOSDISK_SECSIZE], tbl[BIOSDISK_SECSIZE];
-
- /*
- * Following calculations attempt to determine the correct value
- * for d->od_boff by looking for the slice and partition specified,
- * or searching for reasonable defaults.
- */
- error = 0;
-
- /* First, read the MBR and see if we have a PMBR. */
- if (bd_read(od, 0, 1, tbl)) {
- DEBUG("error reading MBR");
- return (EIO);
- }
-
- /* Check the slice table magic. */
- if (((u_char)tbl[0x1fe] != 0x55) || ((u_char)tbl[0x1ff] != 0xaa))
- return (ENXIO);
-
- /* Check for GPT slice. */
- part = 0;
- dp = (struct dos_partition *)(tbl + DOSPARTOFF);
- for (i = 0; i < NDOSPART; i++) {
- if (dp[i].dp_typ == 0xee)
- part++;
- else if ((part != 1) && (dp[i].dp_typ != 0x00))
- return (EINVAL);
- }
- if (part != 1)
- return (EINVAL);
-
- /* Read primary GPT table header. */
- if (bd_read(od, 1, 1, gpt)) {
- DEBUG("error reading GPT header");
- return (EIO);
- }
- hdr = (struct gpt_hdr *)gpt;
- if (bcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0 ||
- hdr->hdr_lba_self != 1 || hdr->hdr_revision < 0x00010000 ||
- hdr->hdr_entsz < sizeof(*ent) ||
- BIOSDISK_SECSIZE % hdr->hdr_entsz != 0) {
- DEBUG("Invalid GPT header\n");
- return (EINVAL);
- }
-
- /* Now walk the partition table to count the number of valid partitions. */
- part = 0;
- entries_per_sec = BIOSDISK_SECSIZE / hdr->hdr_entsz;
- elba = hdr->hdr_lba_table + hdr->hdr_entries / entries_per_sec;
- for (lba = hdr->hdr_lba_table; lba < elba; lba++) {
- if (bd_read(od, lba, 1, tbl)) {
- DEBUG("error reading GPT table");
- return (EIO);
- }
- for (i = 0; i < entries_per_sec; i++) {
- ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
- if (uuid_is_nil(&ent->ent_type, NULL) || ent->ent_lba_start == 0 ||
- ent->ent_lba_end < ent->ent_lba_start)
- continue;
- part++;
- }
- }
-
- /* Save the important information about all the valid partitions. */
- od->od_nparts = part;
- if (part != 0) {
- od->od_partitions = malloc(part * sizeof(struct gpt_part));
- part = 0;
- for (lba = hdr->hdr_lba_table; lba < elba; lba++) {
- if (bd_read(od, lba, 1, tbl)) {
- DEBUG("error reading GPT table");
- error = EIO;
- goto out;
- }
- for (i = 0; i < entries_per_sec; i++) {
- ent = (struct gpt_ent *)(tbl + i * hdr->hdr_entsz);
- if (uuid_is_nil(&ent->ent_type, NULL) ||
- ent->ent_lba_start == 0 ||
- ent->ent_lba_end < ent->ent_lba_start)
- continue;
- od->od_partitions[part].gp_index = (lba - hdr->hdr_lba_table) *
- entries_per_sec + i + 1;
- od->od_partitions[part].gp_type = ent->ent_type;
- od->od_partitions[part].gp_start = ent->ent_lba_start;
- od->od_partitions[part].gp_end = ent->ent_lba_end;
- part++;
- }
- }
- }
- od->od_flags |= BD_GPTOK;
-
- /* Is this a request for the whole disk? */
- if (dev->d_kind.biosdisk.slice < 0) {
- od->od_boff = 0;
- return (0);
- }
-
- /*
- * If a partition number was supplied, then the user is trying to use
- * an MBR address rather than a GPT address, so fail.
- */
- if (dev->d_kind.biosdisk.partition != 0xff) {
- error = ENOENT;
- goto out;
- }
-
- /* If a slice number was supplied but not found, this is an error. */
- gp = NULL;
- if (dev->d_kind.biosdisk.slice > 0) {
- for (i = 0; i < od->od_nparts; i++) {
- if (od->od_partitions[i].gp_index == dev->d_kind.biosdisk.slice) {
- gp = &od->od_partitions[i];
- break;
- }
- }
- if (gp == NULL) {
- DEBUG("partition %d not found", dev->d_kind.biosdisk.slice);
- error = ENOENT;
- goto out;
- }
- }
-
- /* Try to auto-detect the best partition. */
- if (dev->d_kind.biosdisk.slice == 0) {
- gp = bd_best_gptpart(od);
- if (gp == NULL) {
- error = ENOENT;
- goto out;
- }
- dev->d_kind.biosdisk.slice = gp->gp_index;
- }
- od->od_boff = gp->gp_start;
-
-out:
- if (error) {
- if (od->od_nparts > 0)
- free(od->od_partitions);
- od->od_flags &= ~BD_GPTOK;
- }
- return (error);
-}
-
-static struct gpt_part *
-bd_best_gptpart(struct open_disk *od)
-{
- struct gpt_part *gp, *prefpart;
- int i, pref, preflevel;
-
- prefpart = NULL;
- preflevel = PREF_NONE;
-
- gp = od->od_partitions;
- for (i = 0; i < od->od_nparts; i++, gp++) {
- /* Windows. XXX: Also Linux. */
- if (uuid_equal(&gp->gp_type, &ms_basic_data, NULL))
- pref = PREF_DOS;
- /* MidnightBSD */
- else if (uuid_equal(&gp->gp_type, &midnightbsd_ufs, NULL) ||
- uuid_equal(&gp->gp_type, &midnightbsd_zfs, NULL))
- pref = PREF_FBSD;
- else
- pref = PREF_NONE;
- if (pref < preflevel) {
- preflevel = pref;
- prefpart = gp;
- }
- }
- return (prefpart);
-}
-#endif
-
-static int
-bd_close(struct open_file *f)
-{
- struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)(f->f_devdata))->d_kind.biosdisk.data);
-
- bd_closedisk(od);
- return(0);
-}
-
-static void
-bd_closedisk(struct open_disk *od)
-{
- DEBUG("open_disk %p", od);
-#if 0
- /* XXX is this required? (especially if disk already open...) */
- if (od->od_flags & BD_FLOPPY)
- delay(3000000);
-#endif
-#ifdef LOADER_GPT_SUPPORT
- if (od->od_flags & BD_GPTOK && od->od_nparts > 0)
- free(od->od_partitions);
-#endif
- free(od);
-}
-
-static int
-bd_strategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
-{
- struct bcache_devdata bcd;
- struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
-
- bcd.dv_strategy = bd_realstrategy;
- bcd.dv_devdata = devdata;
- return(bcache_strategy(&bcd, od->od_unit, rw, dblk+od->od_boff, size, buf, rsize));
-}
-
-static int
-bd_realstrategy(void *devdata, int rw, daddr_t dblk, size_t size, char *buf, size_t *rsize)
-{
- struct open_disk *od = (struct open_disk *)(((struct i386_devdesc *)devdata)->d_kind.biosdisk.data);
+ struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
int blks;
-#ifdef BD_SUPPORT_FRAGS
+#ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
char fragbuf[BIOSDISK_SECSIZE];
size_t fragsize;
fragsize = size % BIOSDISK_SECSIZE;
#else
- if (size % BIOSDISK_SECSIZE)
+ if (size % BD(dev).bd_sectorsize)
panic("bd_strategy: %d bytes I/O not multiple of block size", size);
#endif
- DEBUG("open_disk %p", od);
- blks = size / BIOSDISK_SECSIZE;
+ DEBUG("open_disk %p", dev);
+ blks = size / BD(dev).bd_sectorsize;
if (rsize)
*rsize = 0;
@@ -1090,11 +381,11 @@
case F_READ:
DEBUG("read %d from %lld to %p", blks, dblk, buf);
- if (blks && bd_read(od, dblk, blks, buf)) {
+ if (blks && bd_read(dev, dblk, blks, buf)) {
DEBUG("read error");
return (EIO);
}
-#ifdef BD_SUPPORT_FRAGS
+#ifdef BD_SUPPORT_FRAGS /* XXX: sector size */
DEBUG("bd_strategy: frag read %d from %d+%d to %p",
fragsize, dblk, blks, buf + (blks * BIOSDISK_SECSIZE));
if (fragsize && bd_read(od, dblk + blks, 1, fragsize)) {
@@ -1107,7 +398,7 @@
case F_WRITE :
DEBUG("write %d from %d to %p", blks, dblk, buf);
- if (blks && bd_write(od, dblk, blks, buf)) {
+ if (blks && bd_write(dev, dblk, blks, buf)) {
DEBUG("write error");
return (EIO);
}
@@ -1132,7 +423,8 @@
#define FLOPPY_BOUNCEBUF 18
static int
-bd_edd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
+bd_edd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
+ int write)
{
static struct edd_packet packet;
@@ -1148,7 +440,7 @@
v86.eax = 0x4300;
else
v86.eax = 0x4200;
- v86.edx = od->od_unit;
+ v86.edx = BD(dev).bd_unit;
v86.ds = VTOPSEG(&packet);
v86.esi = VTOPOFF(&packet);
v86int();
@@ -1156,16 +448,17 @@
}
static int
-bd_chs_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
+bd_chs_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest,
+ int write)
{
u_int x, bpc, cyl, hd, sec;
- bpc = (od->od_sec * od->od_hds); /* blocks per cylinder */
+ bpc = BD(dev).bd_sec * BD(dev).bd_hds; /* blocks per cylinder */
x = dblk;
cyl = x / bpc; /* block # / blocks per cylinder */
x %= bpc; /* block offset into cylinder */
- hd = x / od->od_sec; /* offset / blocks per track */
- sec = x % od->od_sec; /* offset into track */
+ hd = x / BD(dev).bd_sec; /* offset / blocks per track */
+ sec = x % BD(dev).bd_sec; /* offset into track */
/* correct sector number for 1-based BIOS numbering */
sec++;
@@ -1181,7 +474,7 @@
else
v86.eax = 0x200 | blks;
v86.ecx = ((cyl & 0xff) << 8) | ((cyl & 0x300) >> 2) | sec;
- v86.edx = (hd << 8) | od->od_unit;
+ v86.edx = (hd << 8) | BD(dev).bd_unit;
v86.es = VTOPSEG(dest);
v86.ebx = VTOPOFF(dest);
v86int();
@@ -1189,7 +482,7 @@
}
static int
-bd_io(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest, int write)
+bd_io(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest, int write)
{
u_int x, sec, result, resid, retry, maxfer;
caddr_t p, xp, bbuf, breg;
@@ -1202,8 +495,9 @@
p = dest;
/* Decide whether we have to bounce */
- if (VTOP(dest) >> 20 != 0 || ((od->od_unit < 0x80) &&
- ((VTOP(dest) >> 16) != (VTOP(dest + blks * BIOSDISK_SECSIZE) >> 16)))) {
+ if (VTOP(dest) >> 20 != 0 || (BD(dev).bd_unit < 0x80 &&
+ (VTOP(dest) >> 16) != (VTOP(dest +
+ blks * BD(dev).bd_sectorsize) >> 16))) {
/*
* There is a 64k physical boundary somewhere in the
@@ -1214,12 +508,12 @@
* there, in which case we use the top half.
*/
x = min(FLOPPY_BOUNCEBUF, (unsigned)blks);
- bbuf = alloca(x * 2 * BIOSDISK_SECSIZE);
+ bbuf = alloca(x * 2 * BD(dev).bd_sectorsize);
if (((u_int32_t)VTOP(bbuf) & 0xffff0000) ==
- ((u_int32_t)VTOP(bbuf + x * BIOSDISK_SECSIZE) & 0xffff0000)) {
+ ((u_int32_t)VTOP(bbuf + x * BD(dev).bd_sectorsize) & 0xffff0000)) {
breg = bbuf;
} else {
- breg = bbuf + x * BIOSDISK_SECSIZE;
+ breg = bbuf + x * BD(dev).bd_sectorsize;
}
maxfer = x; /* limit transfers to bounce region size */
} else {
@@ -1232,8 +526,8 @@
* Play it safe and don't cross track boundaries.
* (XXX this is probably unnecessary)
*/
- sec = dblk % od->od_sec; /* offset into track */
- x = min(od->od_sec - sec, resid);
+ sec = dblk % BD(dev).bd_sec; /* offset into track */
+ x = min(BD(dev).bd_sec - sec, resid);
if (maxfer > 0)
x = min(x, maxfer); /* fit bounce buffer */
@@ -1245,7 +539,7 @@
* Put your Data In, and shake it all about
*/
if (write && bbuf != NULL)
- bcopy(p, breg, x * BIOSDISK_SECSIZE);
+ bcopy(p, breg, x * BD(dev).bd_sectorsize);
/*
* Loop retrying the operation a couple of times. The BIOS
@@ -1257,14 +551,14 @@
v86.ctl = V86_FLAGS;
v86.addr = 0x13;
v86.eax = 0;
- v86.edx = od->od_unit;
+ v86.edx = BD(dev).bd_unit;
v86int();
}
- if (od->od_flags & BD_MODEEDD1)
- result = bd_edd_io(od, dblk, x, xp, write);
+ if (BD(dev).bd_flags & BD_MODEEDD1)
+ result = bd_edd_io(dev, dblk, x, xp, write);
else
- result = bd_chs_io(od, dblk, x, xp, write);
+ result = bd_chs_io(dev, dblk, x, xp, write);
if (result == 0)
break;
}
@@ -1279,54 +573,30 @@
return(-1);
}
if (!write && bbuf != NULL)
- bcopy(breg, p, x * BIOSDISK_SECSIZE);
- p += (x * BIOSDISK_SECSIZE);
+ bcopy(breg, p, x * BD(dev).bd_sectorsize);
+ p += (x * BD(dev).bd_sectorsize);
dblk += x;
resid -= x;
}
-/* hexdump(dest, (blks * BIOSDISK_SECSIZE)); */
+/* hexdump(dest, (blks * BD(dev).bd_sectorsize)); */
return(0);
}
static int
-bd_read(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
+bd_read(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
{
- return (bd_io(od, dblk, blks, dest, 0));
+ return (bd_io(dev, dblk, blks, dest, 0));
}
static int
-bd_write(struct open_disk *od, daddr_t dblk, int blks, caddr_t dest)
+bd_write(struct disk_devdesc *dev, daddr_t dblk, int blks, caddr_t dest)
{
- return (bd_io(od, dblk, blks, dest, 1));
+ return (bd_io(dev, dblk, blks, dest, 1));
}
-static int
-bd_getgeom(struct open_disk *od)
-{
-
- v86.ctl = V86_FLAGS;
- v86.addr = 0x13;
- v86.eax = 0x800;
- v86.edx = od->od_unit;
- v86int();
-
- if ((V86_CY(v86.efl)) || /* carry set */
- ((v86.edx & 0xff) <= (unsigned)(od->od_unit & 0x7f))) /* unit # bad */
- return(1);
-
- /* convert max cyl # -> # of cylinders */
- od->od_cyl = ((v86.ecx & 0xc0) << 2) + ((v86.ecx & 0xff00) >> 8) + 1;
- /* convert max head # -> # of heads */
- od->od_hds = ((v86.edx & 0xff00) >> 8) + 1;
- od->od_sec = v86.ecx & 0x3f;
-
- DEBUG("unit 0x%x geometry %d/%d/%d", od->od_unit, od->od_cyl, od->od_hds, od->od_sec);
- return(0);
-}
-
/*
* Return the BIOS geometry of a given "fixed drive" in a format
* suitable for the legacy bootinfo structure. Since the kernel is
@@ -1363,21 +633,26 @@
* IDE disks to be specified in $num_ide_disks. There should be a Better Way.
*/
int
-bd_getdev(struct i386_devdesc *dev)
+bd_getdev(struct i386_devdesc *d)
{
- struct open_disk *od;
+ struct disk_devdesc *dev;
int biosdev;
int major;
int rootdev;
char *nip, *cp;
- int unitofs = 0, i, unit;
+ int i, unit;
+ dev = (struct disk_devdesc *)d;
biosdev = bd_unit2bios(dev->d_unit);
DEBUG("unit %d BIOS device %d", dev->d_unit, biosdev);
if (biosdev == -1) /* not a BIOS device */
return(-1);
- if (bd_opendisk(&od, dev) != 0) /* oops, not a viable device */
- return(-1);
+ if (disk_open(dev, BD(dev).bd_sectors * BD(dev).bd_sectorsize,
+ BD(dev).bd_sectorsize,(BD(dev).bd_flags & BD_FLOPPY) ?
+ DISK_F_NOCACHE: 0) != 0) /* oops, not a viable device */
+ return (-1);
+ else
+ disk_close(dev);
if (biosdev < 0x80) {
/* floppy (or emulated floppy) or ATAPI device */
@@ -1389,24 +664,11 @@
major = FDMAJOR;
}
} else {
- /* harddisk */
- if ((od->od_flags & BD_LABELOK) && (od->od_disklabel.d_type == DTYPE_SCSI)) {
- /* label OK, disk labelled as SCSI */
- major = DAMAJOR;
- /* check for unit number correction hint, now deprecated */
- if ((nip = getenv("num_ide_disks")) != NULL) {
- i = strtol(nip, &cp, 0);
- /* check for parse error */
- if ((cp != nip) && (*cp == 0))
- unitofs = i;
- }
- } else {
/* assume an IDE disk */
major = WDMAJOR;
- }
}
/* default root disk unit number */
- unit = (biosdev & 0x7f) - unitofs;
+ unit = biosdev & 0x7f;
/* XXX a better kludge to set the root disk unit number */
if ((nip = getenv("root_disk_unit")) != NULL) {
@@ -1416,8 +678,7 @@
unit = i;
}
- rootdev = MAKEBOOTDEV(major, dev->d_kind.biosdisk.slice + 1, unit,
- dev->d_kind.biosdisk.partition);
+ rootdev = MAKEBOOTDEV(major, dev->d_slice + 1, unit, dev->d_partition);
DEBUG("dev is 0x%x\n", rootdev);
return(rootdev);
}
Modified: trunk/sys/boot/i386/libi386/devicename.c
===================================================================
--- trunk/sys/boot/i386/libi386/devicename.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/i386/libi386/devicename.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -29,8 +29,8 @@
#include <stand.h>
#include <string.h>
-#include <sys/disklabel.h>
#include "bootstrap.h"
+#include "disk.h"
#include "libi386.h"
#include "../zfs/libzfs.h"
@@ -86,7 +86,7 @@
{
struct i386_devdesc *idev;
struct devsw *dv;
- int i, unit, slice, partition, err;
+ int i, unit, err;
char *cp;
const char *np;
@@ -112,62 +112,9 @@
break;
case DEVT_DISK:
- unit = -1;
- slice = -1;
- partition = -1;
- if (*np && (*np != ':')) {
- unit = strtol(np, &cp, 10); /* next comes the unit number */
- if (cp == np) {
- err = EUNIT;
- goto fail;
- }
-#ifdef LOADER_GPT_SUPPORT
- if (*cp == 'p') { /* got a GPT partition */
- np = cp + 1;
- slice = strtol(np, &cp, 10);
- if (cp == np) {
- err = ESLICE;
- goto fail;
- }
- if (*cp && (*cp != ':')) {
- err = EINVAL;
- goto fail;
- }
- partition = 0xff;
- } else {
-#endif
- if (*cp == 's') { /* got a slice number */
- np = cp + 1;
- slice = strtol(np, &cp, 10);
- if (cp == np) {
- err = ESLICE;
- goto fail;
- }
- }
- if (*cp && (*cp != ':')) {
- partition = *cp - 'a'; /* got a partition number */
- if ((partition < 0) || (partition >= MAXPARTITIONS)) {
- err = EPART;
- goto fail;
- }
- cp++;
- }
-#ifdef LOADER_GPT_SUPPORT
- }
-#endif
- } else {
- cp = np;
- }
- if (*cp && (*cp != ':')) {
- err = EINVAL;
+ err = disk_parsedev((struct disk_devdesc *)idev, np, path);
+ if (err != 0)
goto fail;
- }
-
- idev->d_unit = unit;
- idev->d_kind.biosdisk.slice = slice;
- idev->d_kind.biosdisk.partition = partition;
- if (path != NULL)
- *path = (*cp == 0) ? cp : cp + 1;
break;
case DEVT_CD:
@@ -221,8 +168,7 @@
{
struct i386_devdesc *dev = (struct i386_devdesc *)vdev;
static char buf[128]; /* XXX device length constant? */
- char *cp;
-
+
switch(dev->d_type) {
case DEVT_NONE:
strcpy(buf, "(no device)");
@@ -229,30 +175,13 @@
break;
case DEVT_CD:
+ case DEVT_NET:
sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit);
break;
case DEVT_DISK:
- cp = buf;
- cp += sprintf(cp, "%s%d", dev->d_dev->dv_name, dev->d_unit);
-#ifdef LOADER_GPT_SUPPORT
- if (dev->d_kind.biosdisk.partition == 0xff) {
- cp += sprintf(cp, "p%d", dev->d_kind.biosdisk.slice);
- } else {
-#endif
- if (dev->d_kind.biosdisk.slice > 0)
- cp += sprintf(cp, "s%d", dev->d_kind.biosdisk.slice);
- if (dev->d_kind.biosdisk.partition >= 0)
- cp += sprintf(cp, "%c", dev->d_kind.biosdisk.partition + 'a');
-#ifdef LOADER_GPT_SUPPORT
- }
-#endif
- strcat(cp, ":");
- break;
+ return (disk_fmtdev(vdev));
- case DEVT_NET:
- sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit);
- break;
case DEVT_ZFS:
return(zfs_fmtdev(vdev));
}
Modified: trunk/sys/boot/i386/libi386/libi386.h
===================================================================
--- trunk/sys/boot/i386/libi386/libi386.h 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/i386/libi386/libi386.h 2016-09-18 20:24:57 UTC (rev 8410)
@@ -45,6 +45,7 @@
void *data;
int slice;
int partition;
+ off_t offset;
} biosdisk;
struct
{
Modified: trunk/sys/boot/i386/loader/Makefile
===================================================================
--- trunk/sys/boot/i386/loader/Makefile 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/i386/loader/Makefile 2016-09-18 20:24:57 UTC (rev 8410)
@@ -1,4 +1,4 @@
-# $MidnightBSD: src/sys/boot/i386/loader/Makefile,v 1.5 2012/07/21 16:23:10 laffer1 Exp $
+# $MidnightBSD$
.include <bsd.own.mk>
MK_SSP= no
@@ -50,9 +50,6 @@
.if !defined(LOADER_NO_GZIP_SUPPORT)
CFLAGS+= -DLOADER_GZIP_SUPPORT
.endif
-.if !defined(LOADER_NO_GPT_SUPPORT)
-CFLAGS+= -DLOADER_GPT_SUPPORT
-.endif
# Always add MI sources
.PATH: ${.CURDIR}/../../common
@@ -126,6 +123,7 @@
.if ${MACHINE_CPUARCH} == "amd64"
beforedepend ${OBJS}: machine
CLEANFILES+= machine
+CFLAGS+= -DLOADER_PREFER_AMD64
machine:
ln -sf ${.CURDIR}/../../../i386/include machine
.endif
Modified: trunk/sys/boot/i386/loader/conf.c
===================================================================
--- trunk/sys/boot/i386/loader/conf.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/i386/loader/conf.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -70,13 +70,15 @@
};
struct fs_ops *file_system[] = {
+#if defined(LOADER_ZFS_SUPPORT)
+ &zfs_fsops,
+#endif
&ufs_fsops,
&ext2fs_fsops,
&dosfs_fsops,
&cd9660_fsops,
+#ifdef LOADER_SPLIT_SUPPORT
&splitfs_fsops,
-#if defined(LOADER_ZFS_SUPPORT)
- &zfs_fsops,
#endif
#ifdef LOADER_GZIP_SUPPORT
&gzipfs_fsops,
@@ -104,10 +106,16 @@
extern struct file_format amd64_elf_obj;
struct file_format *file_formats[] = {
+#ifdef LOADER_PREFER_AMD64
+ &amd64_elf,
+ &amd64_elf_obj,
+#endif
&i386_elf,
&i386_elf_obj,
+#ifndef LOADER_PREFER_AMD64
&amd64_elf,
&amd64_elf_obj,
+#endif
NULL
};
Modified: trunk/sys/boot/i386/loader/main.c
===================================================================
--- trunk/sys/boot/i386/loader/main.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/i386/loader/main.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -373,25 +373,17 @@
i386_zfs_probe(void)
{
char devname[32];
- int unit, slice;
+ int unit;
/*
* Open all the disks we can find and see if we can reconstruct
- * ZFS pools from them. Bogusly assumes that the disks are named
- * diskN, diskNpM or diskNsM.
+ * ZFS pools from them.
*/
for (unit = 0; unit < MAXBDDEV; unit++) {
+ if (bd_unit2bios(unit) == -1)
+ break;
sprintf(devname, "disk%d:", unit);
- if (zfs_probe_dev(devname, NULL) == ENXIO)
- continue;
- for (slice = 1; slice <= 128; slice++) {
- sprintf(devname, "disk%dp%d:", unit, slice);
- zfs_probe_dev(devname, NULL);
- }
- for (slice = 1; slice <= 4; slice++) {
- sprintf(devname, "disk%ds%d:", unit, slice);
- zfs_probe_dev(devname, NULL);
- }
+ zfs_probe_dev(devname, NULL);
}
}
#endif
Modified: trunk/sys/boot/i386/pmbr/pmbr.s
===================================================================
--- trunk/sys/boot/i386/pmbr/pmbr.s 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/i386/pmbr/pmbr.s 2016-09-18 20:24:57 UTC (rev 8410)
@@ -44,8 +44,8 @@
.set STACK,EXEC+SECSIZE*4 # Stack address
.set GPT_ADDR,STACK # GPT header address
.set GPT_SIG,0
- .set GPT_SIG_0,0x20494645
- .set GPT_SIG_1,0x54524150
+ .set GPT_SIG_0,0x20494645 # "EFI "
+ .set GPT_SIG_1,0x54524150 # "PART"
.set GPT_MYLBA,24
.set GPT_PART_LBA,72
.set GPT_NPART,80
@@ -54,6 +54,8 @@
.set PART_TYPE,0
.set PART_START_LBA,32
.set PART_END_LBA,40
+ .set DPBUF,PART_ADDR+SECSIZE
+ .set DPBUF_SEC,0x10 # Number of sectors
.set NHRDRV,0x475 # Number of hard drives
@@ -93,16 +95,33 @@
jb main.2 # Yes
main.1: movb $0x80,%dl # Assume drive 0x80
#
-# Load the primary GPT header from LBA 1 and verify signature.
+# Load the GPT header and verify signature. Try LBA 1 for the primary one and
+# the last LBA for the backup if it is broken.
#
-main.2: movw $GPT_ADDR,%bx
+main.2: call getdrvparams # Read drive parameters
+ movb $1,%dh # %dh := 1 (reading primary)
+main.2a: movw $GPT_ADDR,%bx
movw $lba,%si
- call read
+ call read # Read header and check GPT sig
cmpl $GPT_SIG_0,GPT_ADDR+GPT_SIG
- jnz err_pt
+ jnz main.2b
cmpl $GPT_SIG_1,GPT_ADDR+GPT_SIG+4
- jnz err_pt
+ jnz main.2b
+ jmp load_part
+main.2b: cmpb $1,%dh # Reading primary?
+ jne err_pt # If no - invalid table found
#
+# Try alternative LBAs from the last sector for the GPT header.
+#
+main.3: movb $0,%dh # %dh := 0 (reading backup)
+ movw $DPBUF+DPBUF_SEC,%si # %si = last sector + 1
+ movw $lba,%di # %di = $lba
+main.3a: decl (%si) # 0x0(%si) = last sec (0-31)
+ movw $2,%cx
+ rep
+ movsw # $lastsec--, copy it to $lba
+ jmp main.2a # Read the next sector
+#
# Load a partition table sector from disk and look for a MidnightBSD boot
# partition.
#
@@ -174,6 +193,16 @@
jc err_rd # If error
ret
#
+# Check the number of LBAs on the drive index %dx. Trashes %ax and %si.
+#
+getdrvparams:
+ movw $DPBUF,%si # Set the address of result buf
+ movw $0x001e,(%si) # len
+ movw $0x4800,%ax # BIOS: Read Drive Parameters
+ int $0x13 # Call the BIOS
+ jc err_rd # "I/O error" if error
+ ret
+#
# Various error message entry points.
#
err_big: movw $msg_big,%si # "Boot loader too
Modified: trunk/sys/boot/i386/zfsboot/zfsboot.c
===================================================================
--- trunk/sys/boot/i386/zfsboot/zfsboot.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/i386/zfsboot/zfsboot.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -14,7 +14,8 @@
*/
#include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+/* $FreeBSD: release/9.2.0/sys/boot/i386/zfsboot/zfsboot.c 242562 2012-11-04 13:37:33Z avg $ */
+__MBSDID("$MidnightBSD$"):
#include <sys/param.h>
#include <sys/errno.h>
Modified: trunk/sys/boot/uboot/common/main.c
===================================================================
--- trunk/sys/boot/uboot/common/main.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/uboot/common/main.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -185,7 +185,7 @@
if (strncmp(devsw[i]->dv_name, "disk",
strlen(devsw[i]->dv_name)) == 0) {
f.f_devdata = &currdev;
- currdev.d_disk.pnum = 0;
+ currdev.d_disk.slice = 0;
if (devsw[i]->dv_open(&f,&currdev) == 0)
break;
}
Modified: trunk/sys/boot/uboot/lib/Makefile
===================================================================
--- trunk/sys/boot/uboot/lib/Makefile 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/uboot/lib/Makefile 2016-09-18 20:24:57 UTC (rev 8410)
@@ -6,7 +6,7 @@
INTERNALLIB=
WARNS?= 2
-SRCS= crc32.c console.c copy.c devicename.c disk.c elf_freebsd.c glue.c
+SRCS= crc32.c console.c copy.c devicename.c elf_freebsd.c glue.c
SRCS+= module.c net.c reboot.c time.c
CFLAGS+= -ffreestanding -msoft-float
@@ -13,6 +13,11 @@
CFLAGS+= -I${.CURDIR}/../../../../lib/libstand/
+.if !defined(LOADER_NO_DISK_SUPPORT)
+SRCS+= disk.c
+CFLAGS+= -DLOADER_DISK_SUPPORT
+.endif
+
# Pick up FDT includes
CFLAGS+= -I${.CURDIR}/../../../../sys/contrib/libfdt/
Modified: trunk/sys/boot/uboot/lib/devicename.c
===================================================================
--- trunk/sys/boot/uboot/lib/devicename.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/uboot/lib/devicename.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -27,12 +27,11 @@
#include <sys/cdefs.h>
__MBSDID("$MidnightBSD$");
-#include <sys/disklabel.h>
-
#include <stand.h>
#include <string.h>
#include "bootstrap.h"
+#include "disk.h"
#include "libuboot.h"
static int uboot_parsedev(struct uboot_devdesc **dev, const char *devspec,
@@ -90,7 +89,7 @@
struct devsw *dv;
char *cp;
const char *np;
- int i, unit, pnum, ptype, err;
+ int i, unit, err;
/* minimum length check */
if (strlen(devspec) < 2)
@@ -114,51 +113,13 @@
case DEVT_NONE:
break;
+#ifdef LOADER_DISK_SUPPORT
case DEVT_DISK:
- unit = -1;
- pnum = -1;
- ptype = -1;
- if (*np && (*np != ':')) {
- /* next comes the unit number */
- unit = strtol(np, &cp, 10);
- if (cp == np) {
- err = EUNIT;
- goto fail;
- }
- if (*cp && (*cp != ':')) {
- /* get partition */
- if (*cp == 'p' && *(cp + 1) &&
- *(cp + 1) != ':') {
- pnum = strtol(cp + 1, &cp, 10);
- ptype = PTYPE_GPT;
- } else if (*cp == 's' && *(cp + 1) &&
- *(cp + 1) != ':') {
- pnum = strtol(cp + 1, &cp, 10);
- ptype = PTYPE_MBR;
- } else {
- pnum = *cp - 'a';
- ptype = PTYPE_BSDLABEL;
- if ((pnum < 0) ||
- (pnum >= MAXPARTITIONS)) {
- err = EPART;
- goto fail;
- }
- cp++;
- }
- }
- }
- if (*cp && (*cp != ':')) {
- err = EINVAL;
+ err = disk_parsedev((struct disk_devdesc *)idev, np, path);
+ if (err != 0)
goto fail;
- }
-
- idev->d_unit = unit;
- idev->d_disk.pnum = pnum;
- idev->d_disk.ptype = ptype;
- idev->d_disk.data = NULL;
- if (path != NULL)
- *path = (*cp == 0) ? cp : cp + 1;
break;
+#endif
case DEVT_NET:
unit = 0;
@@ -204,7 +165,6 @@
uboot_fmtdev(void *vdev)
{
struct uboot_devdesc *dev = (struct uboot_devdesc *)vdev;
- char *cp;
static char buf[128];
switch(dev->d_type) {
@@ -213,23 +173,10 @@
break;
case DEVT_DISK:
- cp = buf;
- cp += sprintf(cp, "%s%d", dev->d_dev->dv_name, dev->d_unit);
- if (dev->d_kind.disk.pnum >= 0) {
- if (dev->d_kind.disk.ptype == PTYPE_BSDLABEL)
- cp += sprintf(cp, "%c",
- dev->d_kind.disk.pnum + 'a');
- else if (dev->d_kind.disk.ptype == PTYPE_GPT)
- cp += sprintf(cp, "p%i",
- dev->d_kind.disk.pnum);
- else if (dev->d_kind.disk.ptype == PTYPE_MBR)
- cp += sprintf(cp, "s%i",
- dev->d_kind.disk.pnum);
- }
+#ifdef LOADER_DISK_SUPPORT
+ return (disk_fmtdev(vdev));
+#endif
- strcat(cp, ":");
- break;
-
case DEVT_NET:
sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit);
break;
Modified: trunk/sys/boot/uboot/lib/disk.c
===================================================================
--- trunk/sys/boot/uboot/lib/disk.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/uboot/lib/disk.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -1,6 +1,7 @@
/*-
* Copyright (c) 2008 Semihalf, Rafal Jaworowski
* Copyright (c) 2009 Semihalf, Piotr Ziecik
+ * Copyright (c) 2012 Andrey V. Elsukov <ae at FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -34,20 +35,13 @@
__MBSDID("$MidnightBSD$");
#include <sys/param.h>
-#include <sys/endian.h>
-#include <sys/queue.h>
-#include <netinet/in.h>
+#include <sys/disk.h>
#include <machine/stdarg.h>
#include <stand.h>
-#include <uuid.h>
-#define FSTYPENAMES
-#include <sys/disklabel.h>
-#include <sys/diskmbr.h>
-#include <sys/gpt.h>
-
#include "api_public.h"
#include "bootstrap.h"
+#include "disk.h"
#include "glue.h"
#include "libuboot.h"
@@ -66,44 +60,19 @@
#define debugf(fmt, args...)
#endif
-struct gpt_part {
- int gp_index;
- uuid_t gp_type;
- uint64_t gp_start;
- uint64_t gp_end;
-};
+static struct {
+ int opened; /* device is opened */
+ int handle; /* storage device handle */
+ int type; /* storage type */
+ off_t blocks; /* block count */
+ u_int bsize; /* block size */
+} stor_info[UB_MAX_DEV];
-struct open_dev {
- int od_bsize; /* block size */
- int od_bstart; /* start block offset from beginning of disk */
- union {
- struct {
- struct disklabel bsdlabel;
- } _bsd;
- struct {
- struct gpt_part *gpt_partitions;
- int gpt_nparts;
- } _gpt;
- } _data;
-};
+#define SI(dev) (stor_info[(dev)->d_unit])
-#define od_bsdlabel _data._bsd.bsdlabel
-#define od_nparts _data._gpt.gpt_nparts
-#define od_partitions _data._gpt.gpt_partitions
-
-static uuid_t efi = GPT_ENT_TYPE_EFI;
-static uuid_t freebsd_boot = GPT_ENT_TYPE_FREEBSD_BOOT;
-static uuid_t freebsd_ufs = GPT_ENT_TYPE_FREEBSD_UFS;
-static uuid_t freebsd_swap = GPT_ENT_TYPE_FREEBSD_SWAP;
-static uuid_t freebsd_zfs = GPT_ENT_TYPE_FREEBSD_ZFS;
-static uuid_t ms_basic_data = GPT_ENT_TYPE_MS_BASIC_DATA;
-
-static int stor_info[UB_MAX_DEV];
static int stor_info_no = 0;
-static int stor_opendev(struct open_dev **, struct uboot_devdesc *);
-static int stor_closedev(struct uboot_devdesc *);
-static int stor_readdev(struct uboot_devdesc *, daddr_t, size_t, char *);
-static int stor_open_count = 0;
+static int stor_opendev(struct disk_devdesc *);
+static int stor_readdev(struct disk_devdesc *, daddr_t, size_t, char *);
/* devsw I/F */
static int stor_init(void);
@@ -110,7 +79,9 @@
static int stor_strategy(void *, int, daddr_t, size_t, char *, size_t *);
static int stor_open(struct open_file *, ...);
static int stor_close(struct open_file *);
+static int stor_ioctl(struct open_file *f, u_long cmd, void *data);
static void stor_print(int);
+static void stor_cleanup(void);
struct devsw uboot_storage = {
"disk",
@@ -119,24 +90,16 @@
stor_strategy,
stor_open,
stor_close,
- noioctl,
- stor_print
+ stor_ioctl,
+ stor_print,
+ stor_cleanup
};
-static void
-uuid_letoh(uuid_t *uuid)
-{
-
- uuid->time_low = le32toh(uuid->time_low);
- uuid->time_mid = le16toh(uuid->time_mid);
- uuid->time_hi_and_version = le16toh(uuid->time_hi_and_version);
-}
-
static int
stor_init(void)
{
struct device_info *di;
- int i, found = 0;
+ int i;
if (devs_no == 0) {
printf("No U-Boot devices! Really enumerated?\n");
@@ -151,12 +114,18 @@
stor_info_no);
return (-1);
}
- stor_info[stor_info_no++] = i;
- found = 1;
+ stor_info[stor_info_no].handle = i;
+ stor_info[stor_info_no].opened = 0;
+ stor_info[stor_info_no].type = di->type;
+ stor_info[stor_info_no].blocks =
+ di->di_stor.block_count;
+ stor_info[stor_info_no].bsize =
+ di->di_stor.block_size;
+ stor_info_no++;
}
}
- if (!found) {
+ if (!stor_info_no) {
debugf("No storage devices\n");
return (-1);
}
@@ -165,32 +134,40 @@
return (0);
}
+static void
+stor_cleanup(void)
+{
+ int i;
+
+ for (i = 0; i < stor_info_no; i++)
+ if (stor_info[i].opened > 0)
+ ub_dev_close(stor_info[i].handle);
+ disk_cleanup(&uboot_storage);
+}
+
static int
stor_strategy(void *devdata, int rw, daddr_t blk, size_t size, char *buf,
size_t *rsize)
{
- struct uboot_devdesc *dev = (struct uboot_devdesc *)devdata;
- struct open_dev *od = (struct open_dev *)dev->d_disk.data;
- int bcount, err;
+ struct disk_devdesc *dev = (struct disk_devdesc *)devdata;
+ daddr_t bcount;
+ int err;
- debugf("od=%p, size=%d, bsize=%d\n", od, size, od->od_bsize);
-
if (rw != F_READ) {
stor_printf("write attempt, operation not supported!\n");
return (EROFS);
}
- if (size % od->od_bsize) {
+ if (size % SI(dev).bsize) {
stor_printf("size=%d not multiple of device block size=%d\n",
- size, od->od_bsize);
+ size, SI(dev).bsize);
return (EIO);
}
- bcount = size / od->od_bsize;
-
+ bcount = size / SI(dev).bsize;
if (rsize)
*rsize = 0;
- err = stor_readdev(dev, blk + od->od_bstart, bcount, buf);
+ err = stor_readdev(dev, blk + dev->d_offset, bcount, buf);
if (!err && rsize)
*rsize = size;
@@ -201,340 +178,54 @@
stor_open(struct open_file *f, ...)
{
va_list ap;
- struct open_dev *od;
- struct uboot_devdesc *dev;
- int err;
+ struct disk_devdesc *dev;
va_start(ap, f);
- dev = va_arg(ap, struct uboot_devdesc *);
+ dev = va_arg(ap, struct disk_devdesc *);
va_end(ap);
- if ((err = stor_opendev(&od, dev)) != 0)
- return (err);
-
- ((struct uboot_devdesc *)(f->f_devdata))->d_disk.data = od;
-
- return (0);
+ return (stor_opendev(dev));
}
static int
-stor_close(struct open_file *f)
+stor_opendev(struct disk_devdesc *dev)
{
- struct uboot_devdesc *dev;
+ int err;
- dev = (struct uboot_devdesc *)(f->f_devdata);
+ if (dev->d_unit < 0 || dev->d_unit >= stor_info_no)
+ return (EIO);
- return (stor_closedev(dev));
-}
-
-static int
-stor_open_gpt(struct open_dev *od, struct uboot_devdesc *dev)
-{
- char *buf;
- struct dos_partition *dp;
- struct gpt_hdr *hdr;
- struct gpt_ent *ent;
- daddr_t slba, lba, elba;
- int eps, part, i;
- int err = 0;
-
- od->od_nparts = 0;
- od->od_partitions = NULL;
-
- /* Devices with block size smaller than 512 bytes cannot use GPT */
- if (od->od_bsize < 512)
- return (ENXIO);
-
- /* Allocate 1 block */
- buf = malloc(od->od_bsize);
- if (!buf) {
- stor_printf("could not allocate memory for GPT\n");
- return (ENOMEM);
- }
-
- /* Read MBR */
- err = stor_readdev(dev, 0, 1, buf);
- if (err) {
- stor_printf("GPT read error=%d\n", err);
- err = EIO;
- goto out;
- }
-
- /* Check the slice table magic. */
- if (le16toh(*((uint16_t *)(buf + DOSMAGICOFFSET))) != DOSMAGIC) {
- err = ENXIO;
- goto out;
- }
-
- /* Check GPT slice */
- dp = (struct dos_partition *)(buf + DOSPARTOFF);
- part = 0;
-
- for (i = 0; i < NDOSPART; i++) {
- if (dp[i].dp_typ == 0xee)
- part += 1;
- else if (dp[i].dp_typ != 0x00) {
- err = EINVAL;
- goto out;
+ if (SI(dev).opened == 0) {
+ err = ub_dev_open(SI(dev).handle);
+ if (err != 0) {
+ stor_printf("device open failed with error=%d, "
+ "handle=%d\n", err, SI(dev).handle);
+ return (ENXIO);
}
+ SI(dev).opened++;
}
-
- if (part != 1) {
- err = EINVAL;
- goto out;
- }
-
- /* Read primary GPT header */
- err = stor_readdev(dev, 1, 1, buf);
- if (err) {
- stor_printf("GPT read error=%d\n", err);
- err = EIO;
- goto out;
- }
-
- hdr = (struct gpt_hdr *)buf;
-
- /* Check GPT header */
- if (bcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)) != 0 ||
- le64toh(hdr->hdr_lba_self) != 1 ||
- le32toh(hdr->hdr_revision) < 0x00010000 ||
- le32toh(hdr->hdr_entsz) < sizeof(*ent) ||
- od->od_bsize % le32toh(hdr->hdr_entsz) != 0) {
- debugf("Invalid GPT header!\n");
- err = EINVAL;
- goto out;
- }
-
- /* Count number of valid partitions */
- part = 0;
- eps = od->od_bsize / le32toh(hdr->hdr_entsz);
- slba = le64toh(hdr->hdr_lba_table);
- elba = slba + le32toh(hdr->hdr_entries) / eps;
-
- for (lba = slba; lba < elba; lba++) {
- err = stor_readdev(dev, lba, 1, buf);
- if (err) {
- stor_printf("GPT read error=%d\n", err);
- err = EIO;
- goto out;
- }
-
- ent = (struct gpt_ent *)buf;
-
- for (i = 0; i < eps; i++) {
- if (uuid_is_nil(&ent[i].ent_type, NULL) ||
- le64toh(ent[i].ent_lba_start) == 0 ||
- le64toh(ent[i].ent_lba_end) <
- le64toh(ent[i].ent_lba_start))
- continue;
-
- part += 1;
- }
- }
-
- /* Save information about partitions */
- if (part != 0) {
- od->od_nparts = part;
- od->od_partitions = malloc(part * sizeof(struct gpt_part));
- if (!od->od_partitions) {
- stor_printf("could not allocate memory for GPT\n");
- err = ENOMEM;
- goto out;
- }
-
- part = 0;
- for (lba = slba; lba < elba; lba++) {
- err = stor_readdev(dev, lba, 1, buf);
- if (err) {
- stor_printf("GPT read error=%d\n", err);
- err = EIO;
- goto out;
- }
-
- ent = (struct gpt_ent *)buf;
-
- for (i = 0; i < eps; i++) {
- if (uuid_is_nil(&ent[i].ent_type, NULL) ||
- le64toh(ent[i].ent_lba_start) == 0 ||
- le64toh(ent[i].ent_lba_end) <
- le64toh(ent[i].ent_lba_start))
- continue;
-
- od->od_partitions[part].gp_index = (lba - slba)
- * eps + i + 1;
- od->od_partitions[part].gp_type =
- ent[i].ent_type;
- od->od_partitions[part].gp_start =
- le64toh(ent[i].ent_lba_start);
- od->od_partitions[part].gp_end =
- le64toh(ent[i].ent_lba_end);
-
- uuid_letoh(&od->od_partitions[part].gp_type);
- part += 1;
- }
- }
- }
-
- dev->d_disk.ptype = PTYPE_GPT;
- /*
- * If index of partition to open (dev->d_disk.pnum) is not defined
- * we set it to the index of the first existing partition. This
- * handles cases when only a disk device is specified (without full
- * partition information) by the caller.
- */
- if ((od->od_nparts > 0) && (dev->d_disk.pnum == 0))
- dev->d_disk.pnum = od->od_partitions[0].gp_index;
-
- for (i = 0; i < od->od_nparts; i++)
- if (od->od_partitions[i].gp_index == dev->d_disk.pnum)
- od->od_bstart = od->od_partitions[i].gp_start;
-
-out:
- if (err && od->od_partitions)
- free(od->od_partitions);
-
- free(buf);
- return (err);
+ return (disk_open(dev, SI(dev).blocks * SI(dev).bsize,
+ SI(dev).bsize, 0));
}
static int
-stor_open_mbr(struct open_dev *od, struct uboot_devdesc *dev)
+stor_close(struct open_file *f)
{
- char *buf = NULL;
- struct dos_partition *dp;
- int err, i, part;
+ struct disk_devdesc *dev;
- od->od_nparts = 0;
- od->od_partitions = NULL;
-
- /* Block size must be at least 512 bytes. */
- if (od->od_bsize < 512)
- return (ENXIO);
-
- /* Read MBR */
- buf = malloc(od->od_bsize);
- if (!buf) {
- stor_printf("could not allocate memory for MBR\n");
- return (ENOMEM);
- }
- err = stor_readdev(dev, 0, 1, buf);
- if (err) {
- stor_printf("MBR read error=%d\n", err);
- err = EIO;
- goto out;
- }
-
- /* Check the slice table magic. */
- if (le16toh(*((uint16_t *)(buf + DOSMAGICOFFSET))) != DOSMAGIC) {
- err = ENXIO;
- goto out;
- }
-
- /* Save information about partitions. */
- dp = (struct dos_partition *)(buf + DOSPARTOFF);
- od->od_partitions = calloc(NDOSPART, sizeof(struct gpt_part));
- if (!od->od_partitions) {
- stor_printf("could not allocate memory for MBR partitions\n");
- err = ENOMEM;
- goto out;
- }
-
- part = 0;
- for (i = 0; i < NDOSPART; i++) {
- u_int32_t start = le32dec(&dp[i].dp_start);
- u_int32_t size = le32dec(&dp[i].dp_size);
- uuid_t *u = NULL;
-
- /* Map MBR partition types to GPT partition types. */
- switch (dp[i].dp_typ) {
- case DOSPTYP_386BSD:
- u = &freebsd_ufs;
- break;
- /* XXX Other types XXX */
- }
-
- if (u) {
- od->od_partitions[part].gp_type = *u;
- od->od_partitions[part].gp_index = i + 1;
- od->od_partitions[part].gp_start = start;
- od->od_partitions[part].gp_end = start + size;
- part += 1;
- }
- }
- od->od_nparts = part;
-
- if (od->od_nparts == 0) {
- err = EINVAL;
- goto out;
- }
-
- dev->d_disk.ptype = PTYPE_MBR;
-
- /* XXX Be smarter here? XXX */
- if (dev->d_disk.pnum == 0)
- dev->d_disk.pnum = od->od_partitions[0].gp_index;
-
- for (i = 0; i < od->od_nparts; i++)
- if (od->od_partitions[i].gp_index == dev->d_disk.pnum)
- od->od_bstart = od->od_partitions[i].gp_start;
-
-out:
- if (err && od->od_partitions)
- free(od->od_partitions);
- free(buf);
- return (err);
+ dev = (struct disk_devdesc *)(f->f_devdata);
+ return (disk_close(dev));
}
static int
-stor_open_bsdlabel(struct open_dev *od, struct uboot_devdesc *dev)
+stor_readdev(struct disk_devdesc *dev, daddr_t blk, size_t size, char *buf)
{
- char *buf;
- struct disklabel *dl;
- int err = 0;
-
- /* Allocate 1 block */
- buf = malloc(od->od_bsize);
- if (!buf) {
- stor_printf("could not allocate memory for disklabel\n");
- return (ENOMEM);
- }
-
- /* Read disklabel */
- err = stor_readdev(dev, LABELSECTOR, 1, buf);
- if (err) {
- stor_printf("disklabel read error=%d\n", err);
- err = ERDLAB;
- goto out;
- }
- bcopy(buf + LABELOFFSET, &od->od_bsdlabel, sizeof(struct disklabel));
- dl = &od->od_bsdlabel;
-
- if (dl->d_magic != DISKMAGIC) {
- stor_printf("no disklabel magic!\n");
- err = EUNLAB;
- goto out;
- }
-
- od->od_bstart = dl->d_partitions[dev->d_disk.pnum].p_offset;
- dev->d_disk.ptype = PTYPE_BSDLABEL;
-
- debugf("bstart=%d\n", od->od_bstart);
-
-out:
- free(buf);
- return (err);
-}
-
-static int
-stor_readdev(struct uboot_devdesc *dev, daddr_t blk, size_t size, char *buf)
-{
lbasize_t real_size;
- int err, handle;
+ int err;
debugf("reading blk=%d size=%d @ 0x%08x\n", (int)blk, size, (uint32_t)buf);
- handle = stor_info[dev->d_unit];
- err = ub_dev_read(handle, buf, size, blk, &real_size);
+ err = ub_dev_read(SI(dev).handle, buf, size, blk, &real_size);
if (err != 0) {
stor_printf("read failed, error=%d\n", err);
return (EIO);
@@ -548,229 +239,45 @@
return (err);
}
-
-static int
-stor_opendev(struct open_dev **odp, struct uboot_devdesc *dev)
-{
- struct device_info *di;
- struct open_dev *od;
- int err, h;
-
- h = stor_info[dev->d_unit];
-
- debugf("refcount=%d\n", stor_open_count);
-
- /*
- * There can be recursive open calls from the infrastructure, but at
- * U-Boot level open the device only the first time.
- */
- if (stor_open_count > 0)
- stor_open_count++;
- else if ((err = ub_dev_open(h)) != 0) {
- stor_printf("device open failed with error=%d, handle=%d\n",
- err, h);
- *odp = NULL;
- return (ENXIO);
- }
-
- if ((di = ub_dev_get(h)) == NULL)
- panic("could not retrieve U-Boot device_info, handle=%d", h);
-
- if ((od = malloc(sizeof(struct open_dev))) == NULL) {
- stor_printf("could not allocate memory for open_dev\n");
- return (ENOMEM);
- }
- od->od_bsize = di->di_stor.block_size;
- od->od_bstart = 0;
-
- err = stor_open_gpt(od, dev);
- if (err != 0)
- err = stor_open_mbr(od, dev);
- if (err != 0)
- err = stor_open_bsdlabel(od, dev);
-
- if (err != 0)
- free(od);
- else {
- stor_open_count = 1;
- *odp = od;
- }
-
- return (err);
-}
-
-static int
-stor_closedev(struct uboot_devdesc *dev)
-{
- struct open_dev *od;
- int err, h;
-
- od = (struct open_dev *)dev->d_disk.data;
- if (dev->d_disk.ptype == PTYPE_GPT && od->od_nparts != 0)
- free(od->od_partitions);
- if (dev->d_disk.ptype == PTYPE_MBR && od->od_nparts != 0)
- free(od->od_partitions);
-
- free(od);
- dev->d_disk.data = NULL;
-
- if (--stor_open_count == 0) {
- h = stor_info[dev->d_unit];
- if ((err = ub_dev_close(h)) != 0) {
- stor_printf("device close failed with error=%d, "
- "handle=%d\n", err, h);
- return (ENXIO);
- }
- }
-
- return (0);
-}
-
-/* Given a size in 512 byte sectors, convert it to a human-readable number. */
-/* XXX stolen from sys/boot/i386/libi386/biosdisk.c, should really be shared */
-static char *
-display_size(uint64_t size)
-{
- static char buf[80];
- char unit;
-
- size /= 2;
- unit = 'K';
- if (size >= 10485760000LL) {
- size /= 1073741824;
- unit = 'T';
- } else if (size >= 10240000) {
- size /= 1048576;
- unit = 'G';
- } else if (size >= 10000) {
- size /= 1024;
- unit = 'M';
- }
- sprintf(buf, "%.6ld%cB", (long)size, unit);
- return (buf);
-}
-
static void
-stor_print_bsdlabel(struct uboot_devdesc *dev, char *prefix, int verbose)
+stor_print(int verbose)
{
- char buf[512], line[80];
- struct disklabel *dl;
- uint32_t off, size;
- int err, i, t;
+ struct disk_devdesc dev;
+ static char line[80];
+ int i;
- /* Read disklabel */
- err = stor_readdev(dev, LABELSECTOR, 1, buf);
- if (err) {
- sprintf(line, "%s%d: disklabel read error=%d\n",
- dev->d_dev->dv_name, dev->d_unit, err);
+ for (i = 0; i < stor_info_no; i++) {
+ dev.d_dev = &uboot_storage;
+ dev.d_unit = i;
+ dev.d_slice = -1;
+ dev.d_partition = -1;
+ sprintf(line, "\tdisk%d (%s)\n", i,
+ ub_stor_type(SI(&dev).type));
pager_output(line);
- return;
- }
- dl = (struct disklabel *)buf;
-
- if (dl->d_magic != DISKMAGIC) {
- sprintf(line, "%s%d: no disklabel magic!\n",
- dev->d_dev->dv_name, dev->d_unit);
- pager_output(line);
- return;
- }
-
- /* Print partitions info */
- for (i = 0; i < dl->d_npartitions; i++) {
- if ((t = dl->d_partitions[i].p_fstype) < FSMAXTYPES) {
-
- off = dl->d_partitions[i].p_offset;
- size = dl->d_partitions[i].p_size;
- if (fstypenames[t] == NULL || size == 0)
- continue;
-
- if ((('a' + i) == 'c') && (!verbose))
- continue;
-
- sprintf(line, " %s%c: %s %s (%d - %d)\n", prefix,
- 'a' + i, fstypenames[t], display_size(size),
- off, off + size);
-
- pager_output(line);
+ if (stor_opendev(&dev) == 0) {
+ sprintf(line, "\tdisk%d", i);
+ disk_print(&dev, line, verbose);
+ disk_close(&dev);
}
}
}
-static void
-stor_print_gpt(struct uboot_devdesc *dev, char *prefix, int verbose)
+static int
+stor_ioctl(struct open_file *f, u_long cmd, void *data)
{
- struct open_dev *od = (struct open_dev *)dev->d_disk.data;
- struct gpt_part *gp;
- char line[80];
- char *fs;
- int i;
+ struct disk_devdesc *dev;
- for (i = 0; i < od->od_nparts; i++) {
- gp = &od->od_partitions[i];
-
- if (uuid_equal(&gp->gp_type, &efi, NULL))
- fs = "EFI";
- else if (uuid_equal(&gp->gp_type, &ms_basic_data, NULL))
- fs = "FAT/NTFS";
- else if (uuid_equal(&gp->gp_type, &freebsd_boot, NULL))
- fs = "FreeBSD Boot";
- else if (uuid_equal(&gp->gp_type, &freebsd_ufs, NULL))
- fs = "FreeBSD UFS";
- else if (uuid_equal(&gp->gp_type, &freebsd_swap, NULL))
- fs = "FreeBSD Swap";
- else if (uuid_equal(&gp->gp_type, &freebsd_zfs, NULL))
- fs = "FreeBSD ZFS";
- else
- fs = "unknown";
-
- sprintf(line, " %sp%u: %s %s (%lld - %lld)\n", prefix,
- gp->gp_index, fs,
- display_size(gp->gp_end + 1 - gp->gp_start), gp->gp_start,
- gp->gp_end);
-
- pager_output(line);
+ dev = (struct disk_devdesc *)f->f_devdata;
+ switch (cmd) {
+ case DIOCGSECTORSIZE:
+ *(u_int *)data = SI(dev).bsize;
+ break;
+ case DIOCGMEDIASIZE:
+ *(off_t *)data = SI(dev).bsize * SI(dev).blocks;
+ break;
+ default:
+ return (ENOTTY);
}
+ return (0);
}
-static void
-stor_print_one(int i, struct device_info *di, int verbose)
-{
- struct uboot_devdesc dev;
- struct open_dev *od;
- char line[80];
-
- sprintf(line, "\tdisk%d (%s)\n", i, ub_stor_type(di->type));
- pager_output(line);
-
- dev.d_dev = &uboot_storage;
- dev.d_unit = i;
- dev.d_disk.pnum = -1;
- dev.d_disk.data = NULL;
-
- if (stor_opendev(&od, &dev) == 0) {
- dev.d_disk.data = od;
-
- if (dev.d_disk.ptype == PTYPE_GPT) {
- sprintf(line, "\t\tdisk%d", i);
- stor_print_gpt(&dev, line, verbose);
- } else if (dev.d_disk.ptype == PTYPE_BSDLABEL) {
- sprintf(line, "\t\tdisk%d", i);
- stor_print_bsdlabel(&dev, line, verbose);
- }
-
- stor_closedev(&dev);
- }
-}
-
-static void
-stor_print(int verbose)
-{
- struct device_info *di;
- int i;
-
- for (i = 0; i < stor_info_no; i++) {
- di = ub_dev_get(stor_info[i]);
- if (di != NULL)
- stor_print_one(i, di, verbose);
- }
-}
Modified: trunk/sys/boot/uboot/lib/libuboot.h
===================================================================
--- trunk/sys/boot/uboot/lib/libuboot.h 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/uboot/lib/libuboot.h 2016-09-18 20:24:57 UTC (rev 8410)
@@ -35,8 +35,9 @@
union {
struct {
void *data;
- int pnum;
- int ptype;
+ int slice;
+ int partition;
+ off_t offset;
} disk;
} d_kind;
};
@@ -43,10 +44,6 @@
#define d_disk d_kind.disk
-#define PTYPE_BSDLABEL 1
-#define PTYPE_GPT 2
-#define PTYPE_MBR 3
-
/*
* Default network packet alignment in memory
*/
Modified: trunk/sys/boot/userboot/test/test.c
===================================================================
--- trunk/sys/boot/userboot/test/test.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/userboot/test/test.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -26,6 +26,8 @@
* $MidnightBSD$
*/
+#include <sys/types.h>
+#include <sys/disk.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <dirent.h>
@@ -251,6 +253,29 @@
return (0);
}
+int
+test_diskioctl(void *arg, int unit, u_long cmd, void *data)
+{
+ struct stat sb;
+
+ if (unit != 0 || disk_fd == -1)
+ return (EBADF);
+ switch (cmd) {
+ case DIOCGSECTORSIZE:
+ *(u_int *)data = 512;
+ break;
+ case DIOCGMEDIASIZE:
+ if (fstat(disk_fd, &sb) == 0)
+ *(off_t *)data = sb.st_size;
+ else
+ return (ENOTTY);
+ break;
+ default:
+ return (ENOTTY);
+ };
+ return (0);
+}
+
/*
* Guest virtual machine i/o
*
@@ -339,7 +364,7 @@
*highmem = 0;
}
-struct loader_callbacks_v1 cb = {
+struct loader_callbacks cb = {
.putc = test_putc,
.getc = test_getc,
.poll = test_poll,
@@ -353,6 +378,7 @@
.stat = test_stat,
.diskread = test_diskread,
+ .diskioctl = test_diskioctl,
.copyin = test_copyin,
.copyout = test_copyout,
@@ -379,7 +405,7 @@
main(int argc, char** argv)
{
void *h;
- void (*func)(struct loader_callbacks_v1 *, void *, int, int);
+ void (*func)(struct loader_callbacks *, void *, int, int);
int opt;
char *disk_image = NULL;
@@ -424,5 +450,5 @@
term.c_lflag &= ~(ICANON|ECHO);
tcsetattr(0, TCSAFLUSH, &term);
- func(&cb, NULL, USERBOOT_VERSION_1, disk_fd >= 0);
+ func(&cb, NULL, USERBOOT_VERSION_2, disk_fd >= 0);
}
Modified: trunk/sys/boot/userboot/userboot/Makefile
===================================================================
--- trunk/sys/boot/userboot/userboot/Makefile 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/userboot/userboot/Makefile 2016-09-18 20:24:57 UTC (rev 8410)
@@ -32,7 +32,6 @@
CFLAGS+= -I${.CURDIR}/../../..
CFLAGS+= -I${.CURDIR}/../../../../lib/libstand
CFLAGS+= -ffreestanding -I.
-CFLAGS+= -DLOADER_GPT_SUPPORT -DLOADER_MBR_SUPPORT
LDFLAGS+= -nostdlib -Wl,-Bsymbolic
Modified: trunk/sys/boot/userboot/userboot/bootinfo32.c
===================================================================
--- trunk/sys/boot/userboot/userboot/bootinfo32.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/userboot/userboot/bootinfo32.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -143,7 +143,7 @@
vm_offset_t size;
vm_offset_t ssym, esym;
char *rootdevname;
- int bootdevnr, i, howto;
+ int bootdevnr, howto;
char *kernelname;
const char *kernelpath;
Modified: trunk/sys/boot/userboot/userboot/copy.c
===================================================================
--- trunk/sys/boot/userboot/userboot/copy.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/userboot/userboot/copy.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -50,7 +50,6 @@
ssize_t
userboot_readin(int fd, vm_offset_t va, size_t len)
{
- void *pa;
ssize_t res, s;
size_t sz;
char buf[4096];
Modified: trunk/sys/boot/userboot/userboot/devicename.c
===================================================================
--- trunk/sys/boot/userboot/userboot/devicename.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/userboot/userboot/devicename.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -29,7 +29,6 @@
#include <stand.h>
#include <string.h>
-#include <sys/disklabel.h>
#include "bootstrap.h"
#include "disk.h"
@@ -87,7 +86,7 @@
{
struct disk_devdesc *idev;
struct devsw *dv;
- int i, unit, slice, partition, err;
+ int i, unit, err;
char *cp;
const char *np;
@@ -113,62 +112,9 @@
break;
case DEVT_DISK:
- unit = -1;
- slice = -1;
- partition = -1;
- if (*np && (*np != ':')) {
- unit = strtol(np, &cp, 10); /* next comes the unit number */
- if (cp == np) {
- err = EUNIT;
- goto fail;
- }
-#ifdef LOADER_GPT_SUPPORT
- if (*cp == 'p') { /* got a GPT partition */
- np = cp + 1;
- slice = strtol(np, &cp, 10);
- if (cp == np) {
- err = ESLICE;
- goto fail;
- }
- if (*cp && (*cp != ':')) {
- err = EINVAL;
- goto fail;
- }
- partition = 0xff;
- } else {
-#endif
- if (*cp == 's') { /* got a slice number */
- np = cp + 1;
- slice = strtol(np, &cp, 10);
- if (cp == np) {
- err = ESLICE;
- goto fail;
- }
- }
- if (*cp && (*cp != ':')) {
- partition = *cp - 'a'; /* got a partition number */
- if ((partition < 0) || (partition >= MAXPARTITIONS)) {
- err = EPART;
- goto fail;
- }
- cp++;
- }
-#ifdef LOADER_GPT_SUPPORT
- }
-#endif
- } else {
- cp = np;
- }
- if (*cp && (*cp != ':')) {
- err = EINVAL;
+ err = disk_parsedev(idev, np, path);
+ if (err != 0)
goto fail;
- }
-
- idev->d_unit = unit;
- idev->d_slice = slice;
- idev->d_partition = partition;
- if (path != NULL)
- *path = (*cp == 0) ? cp : cp + 1;
break;
case DEVT_CD:
@@ -219,8 +165,7 @@
{
struct disk_devdesc *dev = (struct disk_devdesc *)vdev;
static char buf[128]; /* XXX device length constant? */
- char *cp;
-
+
switch(dev->d_type) {
case DEVT_NONE:
strcpy(buf, "(no device)");
@@ -231,22 +176,7 @@
break;
case DEVT_DISK:
- cp = buf;
- cp += sprintf(cp, "%s%d", dev->d_dev->dv_name, dev->d_unit);
-#ifdef LOADER_GPT_SUPPORT
- if (dev->d_partition == 0xff) {
- cp += sprintf(cp, "p%d", dev->d_slice);
- } else {
-#endif
- if (dev->d_slice > 0)
- cp += sprintf(cp, "s%d", dev->d_slice);
- if (dev->d_partition >= 0)
- cp += sprintf(cp, "%c", dev->d_partition + 'a');
-#ifdef LOADER_GPT_SUPPORT
- }
-#endif
- strcat(cp, ":");
- break;
+ return (disk_fmtdev(vdev));
case DEVT_NET:
case DEVT_ZFS:
Modified: trunk/sys/boot/userboot/userboot/libuserboot.h
===================================================================
--- trunk/sys/boot/userboot/userboot/libuserboot.h 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/userboot/userboot/libuserboot.h 2016-09-18 20:24:57 UTC (rev 8410)
@@ -28,7 +28,7 @@
#include "userboot.h"
-extern struct loader_callbacks_v1 *callbacks;
+extern struct loader_callbacks *callbacks;
extern void *callbacks_arg;
#define CALLBACK(fn, args...) (callbacks->fn(callbacks_arg , ##args))
Modified: trunk/sys/boot/userboot/userboot/main.c
===================================================================
--- trunk/sys/boot/userboot/userboot/main.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/userboot/userboot/main.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -36,7 +36,9 @@
#include "disk.h"
#include "libuserboot.h"
-struct loader_callbacks_v1 *callbacks;
+#define USERBOOT_VERSION USERBOOT_VERSION_2
+
+struct loader_callbacks *callbacks;
void *callbacks_arg;
extern char bootprog_name[];
@@ -65,12 +67,12 @@
}
void
-loader_main(struct loader_callbacks_v1 *cb, void *arg, int version, int ndisks)
+loader_main(struct loader_callbacks *cb, void *arg, int version, int ndisks)
{
static char malloc[512*1024];
int i;
- if (version != USERBOOT_VERSION_1)
+ if (version != USERBOOT_VERSION)
abort();
callbacks = cb;
Modified: trunk/sys/boot/userboot/userboot/userboot_disk.c
===================================================================
--- trunk/sys/boot/userboot/userboot/userboot_disk.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/userboot/userboot/userboot_disk.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -31,23 +31,31 @@
* Userboot disk image handling.
*/
+#include <sys/disk.h>
#include <stand.h>
-
#include <stdarg.h>
-#include <uuid.h>
-
#include <bootstrap.h>
#include "disk.h"
#include "libuserboot.h"
+struct userdisk_info {
+ uint64_t mediasize;
+ uint16_t sectorsize;
+};
+
int userboot_disk_maxunit = 0;
+static int userdisk_maxunit = 0;
+static struct userdisk_info *ud_info;
+
static int userdisk_init(void);
+static void userdisk_cleanup(void);
static int userdisk_strategy(void *devdata, int flag, daddr_t dblk,
size_t size, char *buf, size_t *rsize);
static int userdisk_open(struct open_file *f, ...);
static int userdisk_close(struct open_file *f);
+static int userdisk_ioctl(struct open_file *f, u_long cmd, void *data);
static void userdisk_print(int verbose);
struct devsw userboot_disk = {
@@ -57,21 +65,48 @@
userdisk_strategy,
userdisk_open,
userdisk_close,
- noioctl,
+ userdisk_ioctl,
userdisk_print,
- NULL
+ userdisk_cleanup
};
/*
- * Nothing to do here.
+ * Initialize userdisk_info structure for each disk.
*/
static int
userdisk_init(void)
{
+ off_t mediasize;
+ u_int sectorsize;
+ int i;
+ userdisk_maxunit = userboot_disk_maxunit;
+ if (userdisk_maxunit > 0) {
+ ud_info = malloc(sizeof(*ud_info) * userdisk_maxunit);
+ if (ud_info == NULL)
+ return (ENOMEM);
+ for (i = 0; i < userdisk_maxunit; i++) {
+ if (CALLBACK(diskioctl, i, DIOCGSECTORSIZE,
+ §orsize) != 0 || CALLBACK(diskioctl, i,
+ DIOCGMEDIASIZE, &mediasize) != 0)
+ return (ENXIO);
+ ud_info[i].mediasize = mediasize;
+ ud_info[i].sectorsize = sectorsize;
+ }
+ }
+
return(0);
}
+static void
+userdisk_cleanup(void)
+{
+
+ if (userdisk_maxunit > 0)
+ free(ud_info);
+ disk_cleanup(&userboot_disk);
+}
+
/*
* Print information about disks
*/
@@ -78,11 +113,11 @@
static void
userdisk_print(int verbose)
{
- int i;
- char line[80];
- struct disk_devdesc dev;
+ struct disk_devdesc dev;
+ char line[80];
+ int i;
- for (i = 0; i < userboot_disk_maxunit; i++) {
+ for (i = 0; i < userdisk_maxunit; i++) {
sprintf(line, " disk%d: Guest drive image\n", i);
pager_output(line);
dev.d_dev = &userboot_disk;
@@ -89,9 +124,12 @@
dev.d_unit = i;
dev.d_slice = -1;
dev.d_partition = -1;
- dev.d_offset = 0;
- sprintf(line, " disk%d", i);
- disk_print(&dev, line, verbose);
+ if (disk_open(&dev, ud_info[i].mediasize,
+ ud_info[i].sectorsize, 0) == 0) {
+ sprintf(line, " disk%d", i);
+ disk_print(&dev, line, verbose);
+ disk_close(&dev);
+ }
}
}
@@ -108,17 +146,20 @@
dev = va_arg(ap, struct disk_devdesc *);
va_end(ap);
- if (dev->d_unit < 0 || dev->d_unit >= userboot_disk_maxunit)
+ if (dev->d_unit < 0 || dev->d_unit >= userdisk_maxunit)
return (EIO);
- return (disk_open(dev));
+ return (disk_open(dev, ud_info[dev->d_unit].mediasize,
+ ud_info[dev->d_unit].sectorsize, 0));
}
static int
userdisk_close(struct open_file *f)
{
+ struct disk_devdesc *dev;
- return(0);
+ dev = (struct disk_devdesc *)f->f_devdata;
+ return (disk_close(dev));
}
static int
@@ -136,7 +177,7 @@
return (EINVAL);
if (rsize)
*rsize = 0;
- off = (dblk + dev->d_offset) * DISK_SECSIZE;
+ off = (dblk + dev->d_offset) * ud_info[dev->d_unit].sectorsize;
rc = CALLBACK(diskread, dev->d_unit, off, buf, size, &resid);
if (rc)
return (rc);
@@ -144,3 +185,12 @@
*rsize = size - resid;
return (0);
}
+
+static int
+userdisk_ioctl(struct open_file *f, u_long cmd, void *data)
+{
+ struct disk_devdesc *dev;
+
+ dev = (struct disk_devdesc *)f->f_devdata;
+ return (CALLBACK(diskioctl, dev->d_unit, cmd, data));
+}
Modified: trunk/sys/boot/userboot/userboot.h
===================================================================
--- trunk/sys/boot/userboot/userboot.h 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/userboot/userboot.h 2016-09-18 20:24:57 UTC (rev 8410)
@@ -30,6 +30,7 @@
* USERBOOT interface versions
*/
#define USERBOOT_VERSION_1 1
+#define USERBOOT_VERSION_2 2
/*
* Exit codes from the loader
@@ -37,7 +38,7 @@
#define USERBOOT_EXIT_QUIT 1
#define USERBOOT_EXIT_REBOOT 2
-struct loader_callbacks_v1 {
+struct loader_callbacks {
/*
* Console i/o
*/
@@ -175,4 +176,9 @@
*/
void (*getmem)(void *arg, uint64_t *lowmem,
uint64_t *highmem);
+ /*
+ * ioctl interface to the disk device
+ */
+ int (*diskioctl)(void *arg, int unit, u_long cmd,
+ void *data);
};
Modified: trunk/sys/boot/zfs/zfs.c
===================================================================
--- trunk/sys/boot/zfs/zfs.c 2016-09-18 19:52:59 UTC (rev 8409)
+++ trunk/sys/boot/zfs/zfs.c 2016-09-18 20:24:57 UTC (rev 8410)
@@ -33,10 +33,11 @@
* Stand-alone file reading package.
*/
+#include <sys/disk.h>
#include <sys/param.h>
-#include <sys/disklabel.h>
#include <sys/time.h>
#include <sys/queue.h>
+#include <part.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
@@ -394,21 +395,104 @@
return (0);
}
+struct zfs_probe_args {
+ int fd;
+ const char *devname;
+ uint64_t *pool_guid;
+ uint16_t secsz;
+};
+
+static int
+zfs_diskread(void *arg, void *buf, size_t blocks, off_t offset)
+{
+ struct zfs_probe_args *ppa;
+
+ ppa = (struct zfs_probe_args *)arg;
+ return (vdev_read(NULL, (void *)(uintptr_t)ppa->fd,
+ offset * ppa->secsz, buf, blocks * ppa->secsz));
+}
+
+static int
+zfs_probe(int fd, uint64_t *pool_guid)
+{
+ spa_t *spa;
+ int ret;
+
+ ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
+ if (ret == 0 && pool_guid != NULL)
+ *pool_guid = spa->spa_guid;
+ return (ret);
+}
+
+static void
+zfs_probe_partition(void *arg, const char *partname,
+ const struct ptable_entry *part)
+{
+ struct zfs_probe_args *ppa, pa;
+ struct ptable *table;
+ char devname[32];
+ int ret;
+
+ /* Probe only freebsd-zfs and freebsd partitions */
+ if (part->type != PART_FREEBSD &&
+ part->type != PART_FREEBSD_ZFS)
+ return;
+
+ ppa = (struct zfs_probe_args *)arg;
+ strncpy(devname, ppa->devname, strlen(ppa->devname) - 1);
+ devname[strlen(ppa->devname) - 1] = '\0';
+ sprintf(devname, "%s%s:", devname, partname);
+ pa.fd = open(devname, O_RDONLY);
+ if (pa.fd == -1)
+ return;
+ ret = zfs_probe(pa.fd, ppa->pool_guid);
+ if (ret == 0)
+ return;
+ /* Do we have BSD label here? */
+ if (part->type == PART_FREEBSD) {
+ pa.devname = devname;
+ pa.pool_guid = ppa->pool_guid;
+ pa.secsz = ppa->secsz;
+ table = ptable_open(&pa, part->end - part->start + 1,
+ ppa->secsz, zfs_diskread);
+ if (table != NULL) {
+ ptable_iterate(table, &pa, zfs_probe_partition);
+ ptable_close(table);
+ }
+ }
+ close(pa.fd);
+}
+
int
zfs_probe_dev(const char *devname, uint64_t *pool_guid)
{
- spa_t *spa;
- int fd;
+ struct ptable *table;
+ struct zfs_probe_args pa;
+ off_t mediasz;
int ret;
- fd = open(devname, O_RDONLY);
- if (fd == -1)
+ pa.fd = open(devname, O_RDONLY);
+ if (pa.fd == -1)
return (ENXIO);
- ret = vdev_probe(vdev_read, (void *)(uintptr_t)fd, &spa);
- if (ret != 0)
- close(fd);
- else if (pool_guid != NULL)
- *pool_guid = spa->spa_guid;
+ /* Probe the whole disk */
+ ret = zfs_probe(pa.fd, pool_guid);
+ if (ret == 0)
+ return (0);
+ /* Probe each partition */
+ ret = ioctl(pa.fd, DIOCGMEDIASIZE, &mediasz);
+ if (ret == 0)
+ ret = ioctl(pa.fd, DIOCGSECTORSIZE, &pa.secsz);
+ if (ret == 0) {
+ pa.devname = devname;
+ pa.pool_guid = pool_guid;
+ table = ptable_open(&pa, mediasz / pa.secsz, pa.secsz,
+ zfs_diskread);
+ if (table != NULL) {
+ ptable_iterate(table, &pa, zfs_probe_partition);
+ ptable_close(table);
+ }
+ }
+ close(pa.fd);
return (0);
}
More information about the Midnightbsd-cvs
mailing list