From 7762a34bea7c67d99ecb6c2d3cae4fbd5adf1134 Mon Sep 17 00:00:00 2001 From: jsing Date: Sat, 18 Jan 2014 03:07:05 +0000 Subject: [PATCH] Make installboot(8) easier to use - copy the second stage boot loader to the default location used by the given architecture. This eliminates the need to copy it over manually prior to running installboot. --- usr.sbin/installboot/i386/i386_installboot.c | 14 ++++- usr.sbin/installboot/installboot.8 | 7 +-- usr.sbin/installboot/installboot.h | 3 +- .../installboot/sparc64/sparc64_installboot.c | 12 +++- usr.sbin/installboot/util.c | 58 ++++++++++++++++++- 5 files changed, 83 insertions(+), 11 deletions(-) diff --git a/usr.sbin/installboot/i386/i386_installboot.c b/usr.sbin/installboot/i386/i386_installboot.c index 949b34a4597..c7c8311ee35 100644 --- a/usr.sbin/installboot/i386/i386_installboot.c +++ b/usr.sbin/installboot/i386/i386_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: i386_installboot.c,v 1.4 2013/12/27 15:02:49 jsing Exp $ */ +/* $OpenBSD: i386_installboot.c,v 1.5 2014/01/18 03:07:05 jsing Exp $ */ /* $NetBSD: installboot.c,v 1.5 1995/11/17 23:23:50 gwr Exp $ */ /* @@ -69,6 +69,8 @@ #include "installboot.h" #include "i386_installboot.h" +char *bootldr; + char *blkstore; size_t blksize; @@ -99,7 +101,9 @@ md_init(void) { stages = 2; stage1 = "/usr/mdec/biosboot"; - stage2 = "/boot"; + stage2 = "/usr/mdec/boot"; + + bootldr = "/boot"; } void @@ -132,8 +136,12 @@ md_installboot(int devfd, char *dev) if (dl.d_type == 0) warnx("disklabel type unknown"); + bootldr = fileprefix(root, bootldr); + if (!nowrite) + filecopy(stage2, bootldr); + /* Get bootstrap parameters to patch into proto. */ - if (getbootparams(stage2, devfd, &dl) != 0) + if (getbootparams(bootldr, devfd, &dl) != 0) exit(1); /* Write boot blocks to device. */ diff --git a/usr.sbin/installboot/installboot.8 b/usr.sbin/installboot/installboot.8 index 0f1a362024d..19fc7ba0649 100644 --- a/usr.sbin/installboot/installboot.8 +++ b/usr.sbin/installboot/installboot.8 @@ -1,4 +1,4 @@ -.\" $OpenBSD: installboot.8,v 1.2 2014/01/18 02:47:27 jsing Exp $ +.\" $OpenBSD: installboot.8,v 1.3 2014/01/18 03:07:05 jsing Exp $ .\" .\" Copyright (c) 2013, 2014 Joel Sing .\" @@ -72,11 +72,10 @@ To install bootstrap on an amd64 machine, using .Ar /usr/mdec/biosboot as the primary bootstrap and -.Ar /boot +.Ar /usr/mdec/boot as the secondary bootstrap: .Bd -literal -offset 3n -# cp /usr/mdec/boot /boot -# installboot -v wd0 /usr/mdec/biosboot /boot +# installboot -v wd0 /usr/mdec/biosboot /usr/mdec/boot .Ed .Sh SEE ALSO .Xr disklabel 8 diff --git a/usr.sbin/installboot/installboot.h b/usr.sbin/installboot/installboot.h index 69967e8c754..73a9594377c 100644 --- a/usr.sbin/installboot/installboot.h +++ b/usr.sbin/installboot/installboot.h @@ -1,4 +1,4 @@ -/* $OpenBSD: installboot.h,v 1.3 2014/01/18 02:47:27 jsing Exp $ */ +/* $OpenBSD: installboot.h,v 1.4 2014/01/18 03:07:05 jsing Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing * @@ -27,6 +27,7 @@ extern char *stage2; void bootstrap(int, char *, char *); #endif +void filecopy(const char *, const char *); char *fileprefix(const char *, const char *); void md_init(void); diff --git a/usr.sbin/installboot/sparc64/sparc64_installboot.c b/usr.sbin/installboot/sparc64/sparc64_installboot.c index 69357e255ac..391c7801be1 100644 --- a/usr.sbin/installboot/sparc64/sparc64_installboot.c +++ b/usr.sbin/installboot/sparc64/sparc64_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sparc64_installboot.c,v 1.3 2013/12/28 15:05:34 jsing Exp $ */ +/* $OpenBSD: sparc64_installboot.c,v 1.4 2014/01/18 03:07:05 jsing Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing @@ -30,6 +30,8 @@ #include "installboot.h" +char *bootldr; + char *blkstore; char *ldrstore; size_t blksize; @@ -40,7 +42,9 @@ md_init(void) { stages = 2; stage1 = "/usr/mdec/bootblk"; - stage2 = "/ofwboot"; + stage2 = "/usr/mdec/ofwboot"; + + bootldr = "/ofwboot"; } void @@ -92,6 +96,10 @@ md_installboot(int devfd, char *dev) /* XXX - is this necessary? */ sync(); + bootldr = fileprefix(root, bootldr); + if (!nowrite) + filecopy(stage2, bootldr); + /* Write bootblock into the superblock. */ if (lseek(devfd, DEV_BSIZE, SEEK_SET) != DEV_BSIZE) err(1, "lseek"); diff --git a/usr.sbin/installboot/util.c b/usr.sbin/installboot/util.c index 77cdc048180..42221e65e6d 100644 --- a/usr.sbin/installboot/util.c +++ b/usr.sbin/installboot/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.1 2014/01/18 02:47:27 jsing Exp $ */ +/* $OpenBSD: util.c,v 1.2 2014/01/18 03:07:05 jsing Exp $ */ /* * Copyright (c) 2014 Joel Sing @@ -17,13 +17,69 @@ */ #include +#include #include +#include #include #include #include +#include #include "installboot.h" +#define BUFSIZE 2048 + +void +filecopy(const char *srcfile, const char *dstfile) +{ + char *buf, tempfile[MAXPATHLEN]; + struct stat sb; + ssize_t sz, n; + int sfd, dfd; + + if ((buf = malloc(BUFSIZE)) == NULL) + err(1, "malloc"); + + sfd = open(srcfile, O_RDONLY); + if (sfd == -1) + err(1, "open"); + if (fstat(sfd, &sb) == -1) + err(1, "fstat"); + sz = sb.st_size; + + snprintf(tempfile, sizeof(tempfile), "%s.XXXXXXXX", dstfile); + dfd = mkstemp(tempfile); + if (dfd == -1) + err(1, "mkstemp"); + + if (chown(tempfile, 0, 0) == -1) + err(1, "chown"); + if (chmod(tempfile, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) + err(1, "chmod"); + + if (verbose) + fprintf(stderr, "Copying %s to %s\n", srcfile, tempfile); + + while (sz > 0) { + n = MIN(sz, BUFSIZE); + if ((n = read(sfd, buf, n)) == -1) + err(1, "read"); + sz -= n; + if (write(dfd, buf, n) != n) + err(1, "write"); + } + + close(dfd); + close(sfd); + free(buf); + + if (verbose) + fprintf(stderr, "Renaming %s to %s\n", tempfile, dstfile); + + if (rename(tempfile, dstfile) == -1) + err(1, "rename"); +} + char * fileprefix(const char *base, const char *path) { -- 2.20.1