From 60cf2c125444db70da70008cd0ba4ab618e770d9 Mon Sep 17 00:00:00 2001 From: Luigi Rizzo Date: Sun, 10 Mar 2002 20:08:44 +0000 Subject: [PATCH] Export a (machine dependent) kernel variable bootdev as machdep.guessed_bootdev, and add code to sysctl to parse its value and give a (not necessarily correct) name to the device we booted from (the main motivation for this code is to use the info in the PicoBSD boot scripts, and the impact on the kernel is minimal). NOTE: the information available in bootdev is not always reliable, so you should not trust it too much. The parsing code is the same as in boot2.c, and cannot cover all cases -- as it is, it seems to work fine with floppies and IDE disks recognised by the BIOS. It _should_ work as well with SCSI disks recognised by the BIOS. Booting from a CDROM in floppy emulation will return /dev/fd0 (because this is what the BIOS tells us). Booting off the network (e.g. with etherboot) leaves bootdev unset so the value will be printed as "invalid (0xffffffff)". Finally, this feature might go away at some point, hopefully when we have a more reliable way to get the same information. MFC-after: 5 days --- sbin/sysctl/sysctl.c | 51 ++++++++++++++++++++++++++++++++++++++ sys/amd64/amd64/autoconf.c | 2 -- sys/amd64/amd64/machdep.c | 4 +++ sys/i386/i386/autoconf.c | 2 -- sys/i386/i386/machdep.c | 4 +++ 5 files changed, 59 insertions(+), 4 deletions(-) diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c index 4d5e3e14193f..7d66552c0c18 100644 --- a/sbin/sysctl/sysctl.c +++ b/sbin/sysctl/sysctl.c @@ -45,8 +45,10 @@ static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ +#include /* used for bootdev parsing */ #include #include +#include /* used for bootdev parsing */ #include #include #include @@ -403,6 +405,53 @@ oidfmt(int *oid, int len, char *fmt, u_int *kind) return 0; } +/* + * Code to map a bootdev major number into a suitable device name. + * Major numbers are mapped into names as in boot2.c + */ +struct _foo { + int majdev; + char *name; +} maj2name[] = { + 30, "ad", + 0, "wd", + 1, "wfd", + 2, "fd", + 4, "da", + -1, NULL /* terminator */ +}; + +static int +machdep_bootdev(u_long value) +{ + int majdev, unit, slice, part; + struct _foo *p; + + if (value & B_MAGICMASK != B_DEVMAGIC) { + printf("invalid (0x%08x)", value); + return 0; + } + majdev = B_TYPE(value); + unit = B_UNIT(value); + slice = B_SLICE(value); + part = B_PARTITION(value); + if (majdev == 2) { /* floppy, as known to the boot block... */ + printf("/dev/fd%d", unit); + return 0; + } + for (p = maj2name; p->name != NULL && p->majdev != majdev ; p++) ; + if (p->name != NULL) { /* found */ + if (slice == WHOLE_DISK_SLICE) + printf("/dev/%s%d%c", p->name, unit, part); + else + printf("/dev/%s%ds%d%c", + p->name, unit, slice - BASE_SLICE + 1, part + 'a'); + } else + printf("unknown (major %d unit %d slice %d part %d)", + majdev, unit, slice, part); + return 0; +} + /* * This formats and outputs the value of one variable * @@ -496,6 +545,8 @@ show_var(int *oid, int nlen) if (!nflag) printf("%s%s", name, sep); fmt++; + if (!strcmp(name, "machdep.guessed_bootdev")) + return machdep_bootdev(*(unsigned long *)p); val = ""; while (len >= sizeof(long)) { if(*fmt == 'U') diff --git a/sys/amd64/amd64/autoconf.c b/sys/amd64/amd64/autoconf.c index 618b2c69a621..af2cb5fb1d6b 100644 --- a/sys/amd64/amd64/autoconf.c +++ b/sys/amd64/amd64/autoconf.c @@ -238,8 +238,6 @@ cpu_rootconf() } SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL) -u_long bootdev = 0; /* not a dev_t - encoding is different */ - #if defined(NFSCLIENT) && defined(NFS_ROOT) && !defined(BOOTP_NFSROOT) static int diff --git a/sys/amd64/amd64/machdep.c b/sys/amd64/amd64/machdep.c index 3c459d4c7b02..b9d51c71886a 100644 --- a/sys/amd64/amd64/machdep.c +++ b/sys/amd64/amd64/machdep.c @@ -985,6 +985,10 @@ SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo, SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock, CTLFLAG_RW, &wall_cmos_clock, 0, ""); +u_long bootdev; /* not a dev_t - encoding is different */ +SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev, + CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in dev_t format)"); + /* * Initialize 386 and configure to run kernel */ diff --git a/sys/i386/i386/autoconf.c b/sys/i386/i386/autoconf.c index 618b2c69a621..af2cb5fb1d6b 100644 --- a/sys/i386/i386/autoconf.c +++ b/sys/i386/i386/autoconf.c @@ -238,8 +238,6 @@ cpu_rootconf() } SYSINIT(cpu_rootconf, SI_SUB_ROOT_CONF, SI_ORDER_FIRST, cpu_rootconf, NULL) -u_long bootdev = 0; /* not a dev_t - encoding is different */ - #if defined(NFSCLIENT) && defined(NFS_ROOT) && !defined(BOOTP_NFSROOT) static int diff --git a/sys/i386/i386/machdep.c b/sys/i386/i386/machdep.c index 3c459d4c7b02..b9d51c71886a 100644 --- a/sys/i386/i386/machdep.c +++ b/sys/i386/i386/machdep.c @@ -985,6 +985,10 @@ SYSCTL_STRUCT(_machdep, CPU_BOOTINFO, bootinfo, SYSCTL_INT(_machdep, CPU_WALLCLOCK, wall_cmos_clock, CTLFLAG_RW, &wall_cmos_clock, 0, ""); +u_long bootdev; /* not a dev_t - encoding is different */ +SYSCTL_ULONG(_machdep, OID_AUTO, guessed_bootdev, + CTLFLAG_RD, &bootdev, 0, "Maybe the Boot device (not in dev_t format)"); + /* * Initialize 386 and configure to run kernel */