From 3157a424e557a852707eaa5861994c5339bdbda9 Mon Sep 17 00:00:00 2001 From: Mark Kettenis Date: Tue, 20 Jul 2021 14:51:57 +0000 Subject: Add -p option to "prepare" (newfs) a filesystem that will be used for the bootloader. This is a no-op on architectures where such a filesystem isn't needed. ok krw@, deraadt@ --- usr.sbin/installboot/armv7_installboot.c | 86 +++++++++++++++++++++++----- usr.sbin/installboot/hppa_installboot.c | 7 ++- usr.sbin/installboot/i386_installboot.c | 80 +++++++++++++++++++++----- usr.sbin/installboot/i386_installboot.h | 3 +- usr.sbin/installboot/installboot.8 | 10 +++- usr.sbin/installboot/installboot.c | 15 ++++- usr.sbin/installboot/installboot.h | 3 +- usr.sbin/installboot/landisk_installboot.c | 7 ++- usr.sbin/installboot/loongson_installboot.c | 7 ++- usr.sbin/installboot/macppc_installboot.c | 80 +++++++++++++++++++++----- usr.sbin/installboot/octeon_installboot.c | 80 +++++++++++++++++++++----- usr.sbin/installboot/powerpc64_installboot.c | 80 +++++++++++++++++++++----- usr.sbin/installboot/sparc64_installboot.c | 7 ++- usr.sbin/installboot/stubs.c | 7 ++- 14 files changed, 394 insertions(+), 78 deletions(-) (limited to 'usr.sbin/installboot') diff --git a/usr.sbin/installboot/armv7_installboot.c b/usr.sbin/installboot/armv7_installboot.c index 0c589f1cd0b..8251deb6388 100644 --- a/usr.sbin/installboot/armv7_installboot.c +++ b/usr.sbin/installboot/armv7_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: armv7_installboot.c,v 1.10 2021/06/27 04:52:01 jsg Exp $ */ +/* $OpenBSD: armv7_installboot.c,v 1.11 2021/07/20 14:51:56 kettenis Exp $ */ /* $NetBSD: installboot.c,v 1.5 1995/11/17 23:23:50 gwr Exp $ */ /* @@ -55,6 +55,7 @@ #include "installboot.h" +static int create_filesystem(struct disklabel *, char); static void write_filesystem(struct disklabel *, char); static int findgptefisys(int, struct disklabel *); static int findmbrfat(int, struct disklabel *); @@ -69,6 +70,35 @@ md_loadboot(void) { } +void +md_prepareboot(int devfd, char *dev) +{ + struct disklabel dl; + int part; + + /* Get and check disklabel. */ + if (ioctl(devfd, DIOCGDINFO, &dl) == -1) + err(1, "disklabel: %s", dev); + if (dl.d_magic != DISKMAGIC) + errx(1, "bad disklabel magic=0x%08x", dl.d_magic); + + /* Warn on unknown disklabel types. */ + if (dl.d_type == 0) + warnx("disklabel type unknown"); + + part = findgptefisys(devfd, &dl); + if (part != -1) { + create_filesystem(&dl, (char)part); + return; + } + + part = findmbrfat(devfd, &dl); + if (part != -1) { + create_filesystem(&dl, (char)part); + return; + } +} + void md_installboot(int devfd, char *dev) { @@ -98,12 +128,51 @@ md_installboot(int devfd, char *dev) } } +static int +create_filesystem(struct disklabel *dl, char part) +{ + static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; + struct msdosfs_args args; + char cmd[60]; + int rslt; + + /* Mount . as msdos filesystem. */ + memset(&args, 0, sizeof(args)); + rslt = asprintf(&args.fspec, + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c", + dl->d_uid[0], dl->d_uid[1], dl->d_uid[2], dl->d_uid[3], + dl->d_uid[4], dl->d_uid[5], dl->d_uid[6], dl->d_uid[7], + part); + if (rslt == -1) { + warn("bad special device"); + return rslt; + } + + rslt = snprintf(cmd, sizeof(cmd), newfsfmt, args.fspec); + if (rslt >= sizeof(cmd)) { + warnx("can't build newfs command"); + rslt = -1; + return rslt; + } + + if (verbose) + fprintf(stderr, "%s %s\n", + (nowrite ? "would newfs" : "newfsing"), args.fspec); + if (!nowrite) { + rslt = system(cmd); + if (rslt == -1) { + warn("system('%s') failed", cmd); + return rslt; + } + } + + return 0; +} static void write_filesystem(struct disklabel *dl, char part) { static char *fsckfmt = "/sbin/fsck_msdos %s >/dev/null"; - static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; struct msdosfs_args args; char cmd[60]; char dst[PATH_MAX]; @@ -150,18 +219,9 @@ write_filesystem(struct disklabel *dl, char part) } if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) { /* Try newfs'ing it. */ - rslt = snprintf(cmd, sizeof(cmd), newfsfmt, - args.fspec); - if (rslt >= sizeof(cmd)) { - warnx("can't build newfs command"); - rslt = -1; + rslt = create_filesystem(dl, part); + if (rslt == -1) goto rmdir; - } - rslt = system(cmd); - if (rslt == -1) { - warn("system('%s') failed", cmd); - goto rmdir; - } rslt = mount(MOUNT_MSDOS, dst, 0, &args); if (rslt == -1) { warn("unable to mount EFI System partition"); diff --git a/usr.sbin/installboot/hppa_installboot.c b/usr.sbin/installboot/hppa_installboot.c index d0fa5461ce9..4fccfc1f3e0 100644 --- a/usr.sbin/installboot/hppa_installboot.c +++ b/usr.sbin/installboot/hppa_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hppa_installboot.c,v 1.3 2018/09/01 16:55:29 krw Exp $ */ +/* $OpenBSD: hppa_installboot.c,v 1.4 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2013 Joel Sing @@ -30,6 +30,11 @@ md_loadboot(void) { } +void +md_prepareboot(int devfd, char *dev) +{ +} + void md_installboot(int devfd, char *dev) { diff --git a/usr.sbin/installboot/i386_installboot.c b/usr.sbin/installboot/i386_installboot.c index 4a15560a097..c3bf4eaaa44 100644 --- a/usr.sbin/installboot/i386_installboot.c +++ b/usr.sbin/installboot/i386_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: i386_installboot.c,v 1.39 2021/06/02 22:44:27 krw Exp $ */ +/* $OpenBSD: i386_installboot.c,v 1.40 2021/07/20 14:51:56 kettenis Exp $ */ /* $NetBSD: installboot.c,v 1.5 1995/11/17 23:23:50 gwr Exp $ */ /* @@ -128,6 +128,29 @@ md_loadboot(void) errx(1, "proto bootblocks too big"); } +void +md_prepareboot(int devfd, char *dev) +{ + struct disklabel dl; + int part; + + /* Get and check disklabel. */ + if (ioctl(devfd, DIOCGDINFO, &dl) == -1) + err(1, "disklabel: %s", dev); + if (dl.d_magic != DISKMAGIC) + errx(1, "bad disklabel magic=0x%08x", dl.d_magic); + + /* Warn on unknown disklabel types. */ + if (dl.d_type == 0) + warnx("disklabel type unknown"); + + part = findgptefisys(devfd, &dl); + if (part != -1) { + create_filesystem(&dl, (char)part); + return; + } +} + void md_installboot(int devfd, char *dev) { @@ -221,11 +244,51 @@ write_bootblocks(int devfd, char *dev, struct disklabel *dl) } } +int +create_filesystem(struct disklabel *dl, char part) +{ + static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; + struct msdosfs_args args; + char cmd[60]; + int rslt; + + /* Mount . as msdos filesystem. */ + memset(&args, 0, sizeof(args)); + rslt = asprintf(&args.fspec, + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c", + dl->d_uid[0], dl->d_uid[1], dl->d_uid[2], dl->d_uid[3], + dl->d_uid[4], dl->d_uid[5], dl->d_uid[6], dl->d_uid[7], + part); + if (rslt == -1) { + warn("bad special device"); + return rslt; + } + + rslt = snprintf(cmd, sizeof(cmd), newfsfmt, args.fspec); + if (rslt >= sizeof(cmd)) { + warnx("can't build newfs command"); + rslt = -1; + return rslt; + } + + if (verbose) + fprintf(stderr, "%s %s\n", + (nowrite ? "would newfs" : "newfsing"), args.fspec); + if (!nowrite) { + rslt = system(cmd); + if (rslt == -1) { + warn("system('%s') failed", cmd); + return rslt; + } + } + + return 0; +} + void write_filesystem(struct disklabel *dl, char part) { static char *fsckfmt = "/sbin/fsck_msdos %s >/dev/null"; - static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; struct msdosfs_args args; char cmd[60]; char dst[PATH_MAX]; @@ -272,18 +335,9 @@ write_filesystem(struct disklabel *dl, char part) } if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) { /* Try newfs'ing it. */ - rslt = snprintf(cmd, sizeof(cmd), newfsfmt, - args.fspec); - if (rslt >= sizeof(cmd)) { - warnx("can't build newfs command"); - rslt = -1; + rslt = create_filesystem(dl, part); + if (rslt == -1) goto rmdir; - } - rslt = system(cmd); - if (rslt == -1) { - warn("system('%s') failed", cmd); - goto rmdir; - } rslt = mount(MOUNT_MSDOS, dst, 0, &args); if (rslt == -1) { warn("unable to mount EFI System partition"); diff --git a/usr.sbin/installboot/i386_installboot.h b/usr.sbin/installboot/i386_installboot.h index 0f75c057d06..2df9176fee2 100644 --- a/usr.sbin/installboot/i386_installboot.h +++ b/usr.sbin/installboot/i386_installboot.h @@ -1,4 +1,4 @@ -/* $OpenBSD: i386_installboot.h,v 1.6 2020/06/27 15:35:29 deraadt Exp $ */ +/* $OpenBSD: i386_installboot.h,v 1.7 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2011 Joel Sing * Copyright (c) 2003 Tom Cosgrove @@ -55,4 +55,5 @@ void pbr_set_symbols(char *, char *, struct sym_data *); void sym_set_value(struct sym_data *, char *, u_int32_t); void write_bootblocks(int, char *, struct disklabel *); int findgptefisys(int, struct disklabel *); +int create_filesystem(struct disklabel *, char); void write_filesystem(struct disklabel *, char); diff --git a/usr.sbin/installboot/installboot.8 b/usr.sbin/installboot/installboot.8 index 125cdb04d30..3429ffcfbf9 100644 --- a/usr.sbin/installboot/installboot.8 +++ b/usr.sbin/installboot/installboot.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: installboot.8,v 1.4 2014/01/18 18:14:21 jmc Exp $ +.\" $OpenBSD: installboot.8,v 1.5 2021/07/20 14:51:56 kettenis Exp $ .\" .\" Copyright (c) 2013, 2014 Joel Sing .\" @@ -14,7 +14,7 @@ .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. .\" -.Dd $Mdocdate: January 18 2014 $ +.Dd $Mdocdate: July 20 2021 $ .Dt INSTALLBOOT 8 .Os .Sh NAME @@ -22,7 +22,7 @@ .Nd install bootstrap on a disk .Sh SYNOPSIS .Nm installboot -.Op Fl nv +.Op Fl npv .Op Fl r Ar root .Ar disk .Op Ar stage1 Op Ar stage2 @@ -39,6 +39,10 @@ The options are as follows: .Bl -tag -width Ds .It Fl n Perform a dry run - do not actually write any bootstrap to the disk. +.It Fl p +Prepare filesystem. +This will create a new filesystem on the partition reserved for the +boot loader on architectures that require one. .It Fl r Ar root Specify the mount point of the .Ar root diff --git a/usr.sbin/installboot/installboot.c b/usr.sbin/installboot/installboot.c index cd293c02c56..6f79d397886 100644 --- a/usr.sbin/installboot/installboot.c +++ b/usr.sbin/installboot/installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: installboot.c,v 1.13 2019/10/29 17:41:45 deraadt Exp $ */ +/* $OpenBSD: installboot.c,v 1.14 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing @@ -27,6 +27,7 @@ #include "installboot.h" int nowrite; +int prepare; int stages; int verbose; @@ -39,7 +40,7 @@ usage(void) { extern char *__progname; - fprintf(stderr, "usage: %s [-nv] [-r root] disk [stage1%s]\n", + fprintf(stderr, "usage: %s [-npv] [-r root] disk [stage1%s]\n", __progname, (stages >= 2) ? " [stage2]" : ""); exit(1); @@ -53,11 +54,14 @@ main(int argc, char **argv) md_init(); - while ((opt = getopt(argc, argv, "nr:v")) != -1) { + while ((opt = getopt(argc, argv, "npr:v")) != -1) { switch (opt) { case 'n': nowrite = 1; break; + case 'p': + prepare = 1; + break; case 'r': root = strdup(optarg); if (root == NULL) @@ -115,6 +119,11 @@ main(int argc, char **argv) md_loadboot(); + if (prepare) { + md_prepareboot(devfd, realdev); + return 0; + } + #ifdef SOFTRAID sr_installboot(devfd, dev); #else diff --git a/usr.sbin/installboot/installboot.h b/usr.sbin/installboot/installboot.h index 74488f9e9b4..54d362c7ab4 100644 --- a/usr.sbin/installboot/installboot.h +++ b/usr.sbin/installboot/installboot.h @@ -1,4 +1,4 @@ -/* $OpenBSD: installboot.h,v 1.12 2020/06/08 19:17:12 kn Exp $ */ +/* $OpenBSD: installboot.h,v 1.13 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing * @@ -37,6 +37,7 @@ u_int32_t crc32(const u_char *, const u_int32_t); void md_init(void); void md_loadboot(void); +void md_prepareboot(int, char *); void md_installboot(int, char *); #ifdef SOFTRAID diff --git a/usr.sbin/installboot/landisk_installboot.c b/usr.sbin/installboot/landisk_installboot.c index f8c8d91c462..b61a536139c 100644 --- a/usr.sbin/installboot/landisk_installboot.c +++ b/usr.sbin/installboot/landisk_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: landisk_installboot.c,v 1.9 2018/09/01 16:55:29 krw Exp $ */ +/* $OpenBSD: landisk_installboot.c,v 1.10 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2013 Joel Sing @@ -38,6 +38,11 @@ md_loadboot(void) { } +void +md_prepareboot(int devfd, char *dev) +{ +} + void md_installboot(int devfd, char *dev) { diff --git a/usr.sbin/installboot/loongson_installboot.c b/usr.sbin/installboot/loongson_installboot.c index 30994071ed0..639cc1001f5 100644 --- a/usr.sbin/installboot/loongson_installboot.c +++ b/usr.sbin/installboot/loongson_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: loongson_installboot.c,v 1.3 2020/07/19 15:23:08 visa Exp $ */ +/* $OpenBSD: loongson_installboot.c,v 1.4 2021/07/20 14:51:56 kettenis Exp $ */ /* $NetBSD: installboot.c,v 1.5 1995/11/17 23:23:50 gwr Exp $ */ /* @@ -66,6 +66,11 @@ md_loadboot(void) { } +void +md_prepareboot(int devfd, char *dev) +{ +} + void md_installboot(int devfd, char *dev) { diff --git a/usr.sbin/installboot/macppc_installboot.c b/usr.sbin/installboot/macppc_installboot.c index 3afb3b66a3d..b3ee6534da5 100644 --- a/usr.sbin/installboot/macppc_installboot.c +++ b/usr.sbin/installboot/macppc_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: macppc_installboot.c,v 1.3 2020/07/22 05:06:38 deraadt Exp $ */ +/* $OpenBSD: macppc_installboot.c,v 1.4 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2011 Joel Sing @@ -53,6 +53,7 @@ #include "installboot.h" +static int create_filesystem(struct disklabel *, char); static void write_filesystem(struct disklabel *, char); static int findmbrfat(int, struct disklabel *); @@ -66,6 +67,29 @@ md_loadboot(void) { } +void +md_prepareboot(int devfd, char *dev) +{ + struct disklabel dl; + int part; + + /* Get and check disklabel. */ + if (ioctl(devfd, DIOCGDINFO, &dl) == -1) + err(1, "disklabel: %s", dev); + if (dl.d_magic != DISKMAGIC) + errx(1, "bad disklabel magic=0x%08x", dl.d_magic); + + /* Warn on unknown disklabel types. */ + if (dl.d_type == 0) + warnx("disklabel type unknown"); + + part = findmbrfat(devfd, &dl); + if (part != -1) { + create_filesystem(&dl, (char)part); + return; + } +} + void md_installboot(int devfd, char *dev) { @@ -89,12 +113,51 @@ md_installboot(int devfd, char *dev) } } +static int +create_filesystem(struct disklabel *dl, char part) +{ + static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; + struct msdosfs_args args; + char cmd[60]; + int rslt; + + /* Mount . as msdos filesystem. */ + memset(&args, 0, sizeof(args)); + rslt = asprintf(&args.fspec, + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c", + dl->d_uid[0], dl->d_uid[1], dl->d_uid[2], dl->d_uid[3], + dl->d_uid[4], dl->d_uid[5], dl->d_uid[6], dl->d_uid[7], + part); + if (rslt == -1) { + warn("bad special device"); + return rslt; + } + + rslt = snprintf(cmd, sizeof(cmd), newfsfmt, args.fspec); + if (rslt >= sizeof(cmd)) { + warnx("can't build newfs command"); + rslt = -1; + return rslt; + } + + if (verbose) + fprintf(stderr, "%s %s\n", + (nowrite ? "would newfs" : "newfsing"), args.fspec); + if (!nowrite) { + rslt = system(cmd); + if (rslt == -1) { + warn("system('%s') failed", cmd); + return rslt; + } + } + + return 0; +} static void write_filesystem(struct disklabel *dl, char part) { static char *fsckfmt = "/sbin/fsck_msdos %s >/dev/null"; - static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; struct msdosfs_args args; char cmd[60]; char dst[PATH_MAX]; @@ -141,18 +204,9 @@ write_filesystem(struct disklabel *dl, char part) } if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) { /* Try newfs'ing it. */ - rslt = snprintf(cmd, sizeof(cmd), newfsfmt, - args.fspec); - if (rslt >= sizeof(cmd)) { - warnx("can't build newfs command"); - rslt = -1; - goto rmdir; - } - rslt = system(cmd); - if (rslt == -1) { - warn("system('%s') failed", cmd); + rslt = create_filesystem(dl, part); + if (rslt == -1) goto rmdir; - } rslt = mount(MOUNT_MSDOS, dst, 0, &args); if (rslt == -1) { warn("unable to mount MSDOS partition"); diff --git a/usr.sbin/installboot/octeon_installboot.c b/usr.sbin/installboot/octeon_installboot.c index d9be070997d..c1dbf51256a 100644 --- a/usr.sbin/installboot/octeon_installboot.c +++ b/usr.sbin/installboot/octeon_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: octeon_installboot.c,v 1.3 2020/07/22 05:06:38 deraadt Exp $ */ +/* $OpenBSD: octeon_installboot.c,v 1.4 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2011 Joel Sing @@ -53,6 +53,7 @@ #include "installboot.h" +static int create_filesystem(struct disklabel *, char); static void write_filesystem(struct disklabel *, char); static int findmbrfat(int, struct disklabel *); @@ -66,6 +67,29 @@ md_loadboot(void) { } +void +md_prepareboot(int devfd, char *dev) +{ + struct disklabel dl; + int part; + + /* Get and check disklabel. */ + if (ioctl(devfd, DIOCGDINFO, &dl) == -1) + err(1, "disklabel: %s", dev); + if (dl.d_magic != DISKMAGIC) + errx(1, "bad disklabel magic=0x%08x", dl.d_magic); + + /* Warn on unknown disklabel types. */ + if (dl.d_type == 0) + warnx("disklabel type unknown"); + + part = findmbrfat(devfd, &dl); + if (part != -1) { + create_filesystem(&dl, (char)part); + return; + } +} + void md_installboot(int devfd, char *dev) { @@ -89,12 +113,51 @@ md_installboot(int devfd, char *dev) } } +static int +create_filesystem(struct disklabel *dl, char part) +{ + static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; + struct msdosfs_args args; + char cmd[60]; + int rslt; + + /* Mount . as msdos filesystem. */ + memset(&args, 0, sizeof(args)); + rslt = asprintf(&args.fspec, + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c", + dl->d_uid[0], dl->d_uid[1], dl->d_uid[2], dl->d_uid[3], + dl->d_uid[4], dl->d_uid[5], dl->d_uid[6], dl->d_uid[7], + part); + if (rslt == -1) { + warn("bad special device"); + return rslt; + } + + rslt = snprintf(cmd, sizeof(cmd), newfsfmt, args.fspec); + if (rslt >= sizeof(cmd)) { + warnx("can't build newfs command"); + rslt = -1; + return rslt; + } + + if (verbose) + fprintf(stderr, "%s %s\n", + (nowrite ? "would newfs" : "newfsing"), args.fspec); + if (!nowrite) { + rslt = system(cmd); + if (rslt == -1) { + warn("system('%s') failed", cmd); + return rslt; + } + } + + return 0; +} static void write_filesystem(struct disklabel *dl, char part) { static char *fsckfmt = "/sbin/fsck_msdos %s >/dev/null"; - static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; struct msdosfs_args args; char cmd[60]; char dst[PATH_MAX]; @@ -141,18 +204,9 @@ write_filesystem(struct disklabel *dl, char part) } if (mount(MOUNT_MSDOS, dst, 0, &args) == -1) { /* Try newfs'ing it. */ - rslt = snprintf(cmd, sizeof(cmd), newfsfmt, - args.fspec); - if (rslt >= sizeof(cmd)) { - warnx("can't build newfs command"); - rslt = -1; - goto rmdir; - } - rslt = system(cmd); - if (rslt == -1) { - warn("system('%s') failed", cmd); + rslt = create_filesystem(dl, part); + if (rslt == -1) goto rmdir; - } rslt = mount(MOUNT_MSDOS, dst, 0, &args); if (rslt == -1) { warn("unable to mount MSDOS partition"); diff --git a/usr.sbin/installboot/powerpc64_installboot.c b/usr.sbin/installboot/powerpc64_installboot.c index 351015934b1..0cd05835dc6 100644 --- a/usr.sbin/installboot/powerpc64_installboot.c +++ b/usr.sbin/installboot/powerpc64_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: powerpc64_installboot.c,v 1.2 2020/07/18 15:28:38 deraadt Exp $ */ +/* $OpenBSD: powerpc64_installboot.c,v 1.3 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2011 Joel Sing @@ -53,6 +53,7 @@ #include "installboot.h" +static int create_filesystem(struct disklabel *, char); static void write_filesystem(struct disklabel *, char); static int findmbrfat(int, struct disklabel *); @@ -68,6 +69,29 @@ md_loadboot(void) { } +void +md_prepareboot(int devfd, char *dev) +{ + struct disklabel dl; + int part; + + /* Get and check disklabel. */ + if (ioctl(devfd, DIOCGDINFO, &dl) == -1) + err(1, "disklabel: %s", dev); + if (dl.d_magic != DISKMAGIC) + errx(1, "bad disklabel magic=0x%08x", dl.d_magic); + + /* Warn on unknown disklabel types. */ + if (dl.d_type == 0) + warnx("disklabel type unknown"); + + part = findmbrfat(devfd, &dl); + if (part != -1) { + create_filesystem(&dl, (char)part); + return; + } +} + void md_installboot(int devfd, char *dev) { @@ -96,12 +120,51 @@ md_installboot(int devfd, char *dev) } } +static int +create_filesystem(struct disklabel *dl, char part) +{ + static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; + struct msdosfs_args args; + char cmd[60]; + int rslt; + + /* Mount . as msdos filesystem. */ + memset(&args, 0, sizeof(args)); + rslt = asprintf(&args.fspec, + "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.%c", + dl->d_uid[0], dl->d_uid[1], dl->d_uid[2], dl->d_uid[3], + dl->d_uid[4], dl->d_uid[5], dl->d_uid[6], dl->d_uid[7], + part); + if (rslt == -1) { + warn("bad special device"); + return rslt; + } + + rslt = snprintf(cmd, sizeof(cmd), newfsfmt, args.fspec); + if (rslt >= sizeof(cmd)) { + warnx("can't build newfs command"); + rslt = -1; + return rslt; + } + + if (verbose) + fprintf(stderr, "%s %s\n", + (nowrite ? "would newfs" : "newfsing"), args.fspec); + if (!nowrite) { + rslt = system(cmd); + if (rslt == -1) { + warn("system('%s') failed", cmd); + return rslt; + } + } + + return 0; +} static void write_filesystem(struct disklabel *dl, char part) { static char *fsckfmt = "/sbin/fsck_msdos %s >/dev/null"; - static char *newfsfmt ="/sbin/newfs_msdos %s >/dev/null"; struct msdosfs_args args; char cmd[60]; char dir[PATH_MAX]; @@ -149,18 +212,9 @@ write_filesystem(struct disklabel *dl, char part) } if (mount(MOUNT_MSDOS, dir, 0, &args) == -1) { /* Try newfs'ing it. */ - rslt = snprintf(cmd, sizeof(cmd), newfsfmt, - args.fspec); - if (rslt >= sizeof(cmd)) { - warnx("can't build newfs command"); - rslt = -1; + rslt = create_filesystem(dl, part); + if (rslt == -1) goto rmdir; - } - rslt = system(cmd); - if (rslt == -1) { - warn("system('%s') failed", cmd); - goto rmdir; - } rslt = mount(MOUNT_MSDOS, dir, 0, &args); if (rslt == -1) { warn("unable to mount MSDOS partition"); diff --git a/usr.sbin/installboot/sparc64_installboot.c b/usr.sbin/installboot/sparc64_installboot.c index bef2079a8af..3fb61efbf6b 100644 --- a/usr.sbin/installboot/sparc64_installboot.c +++ b/usr.sbin/installboot/sparc64_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sparc64_installboot.c,v 1.8 2019/06/28 13:32:48 deraadt Exp $ */ +/* $OpenBSD: sparc64_installboot.c,v 1.9 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing @@ -89,6 +89,11 @@ md_loadboot(void) close(fd); } +void +md_prepareboot(int devfd, char *dev) +{ +} + void md_installboot(int devfd, char *dev) { diff --git a/usr.sbin/installboot/stubs.c b/usr.sbin/installboot/stubs.c index ced93a1daf8..9517512de9f 100644 --- a/usr.sbin/installboot/stubs.c +++ b/usr.sbin/installboot/stubs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: stubs.c,v 1.4 2015/10/15 04:41:10 deraadt Exp $ */ +/* $OpenBSD: stubs.c,v 1.5 2021/07/20 14:51:56 kettenis Exp $ */ /* * Copyright (c) 2014 Joel Sing @@ -31,6 +31,11 @@ md_loadboot(void) { } +void +md_prepareboot(int devfd, char *dev) +{ +} + void md_installboot(int devfd, char *dev) { -- cgit v1.2.3