From 4a5d5189481afe844124661f09aeaa4d60871d36 Mon Sep 17 00:00:00 2001 From: visa Date: Thu, 3 Feb 2022 10:25:14 +0000 Subject: [PATCH] installboot(8): Adjustments for EFI platforms Reduce #ifdef'ing within the control logic to make it clearer that there are no essential differences in behaviour between the platforms. Make installboot(8) write startup.nsh to enable simpler and more consistent code in install.md. Input and OK kettenis@ deraadt@ --- usr.sbin/installboot/efi_installboot.c | 73 ++++++++++++-------------- usr.sbin/installboot/installboot.h | 4 +- usr.sbin/installboot/util.c | 43 ++++++++++++++- 3 files changed, 79 insertions(+), 41 deletions(-) diff --git a/usr.sbin/installboot/efi_installboot.c b/usr.sbin/installboot/efi_installboot.c index 967b1acfe7e..e787a19500c 100644 --- a/usr.sbin/installboot/efi_installboot.c +++ b/usr.sbin/installboot/efi_installboot.c @@ -1,4 +1,4 @@ -/* $OpenBSD: efi_installboot.c,v 1.1 2022/02/03 10:21:13 visa Exp $ */ +/* $OpenBSD: efi_installboot.c,v 1.2 2022/02/03 10:25:14 visa Exp $ */ /* $NetBSD: installboot.c,v 1.5 1995/11/17 23:23:50 gwr Exp $ */ /* @@ -55,6 +55,19 @@ #include "installboot.h" +#if defined(__aarch64__) +#define BOOTEFI_SRC "BOOTAA64.EFI" +#define BOOTEFI_DST "bootaa64.efi" +#elif defined(__arm__) +#define BOOTEFI_SRC "BOOTARM.EFI" +#define BOOTEFI_DST "bootarm.efi" +#elif defined(__riscv) +#define BOOTEFI_SRC "BOOTRISCV64.EFI" +#define BOOTEFI_DST "bootriscv64.efi" +#else +#error "unhandled architecture" +#endif + static int create_filesystem(struct disklabel *, char); static void write_filesystem(struct disklabel *, char); static int findgptefisys(int, struct disklabel *); @@ -252,52 +265,18 @@ write_filesystem(struct disklabel *dl, char part) goto umount; } -#ifdef __aarch64__ - /* - * Copy BOOTAA64.EFI to /efi/boot/bootaa64.efi. - */ + /* Copy EFI bootblocks to /efi/boot/. */ pathlen = strlen(dst); - if (strlcat(dst, "/bootaa64.efi", sizeof(dst)) >= sizeof(dst)) { + if (strlcat(dst, "/" BOOTEFI_DST, sizeof(dst)) >= sizeof(dst)) { rslt = -1; - warn("unable to build /bootaa64.efi path"); + warn("unable to build /%s path", BOOTEFI_DST); goto umount; } - src = fileprefix(root, "/usr/mdec/BOOTAA64.EFI"); + src = fileprefix(root, "/usr/mdec/" BOOTEFI_SRC); if (src == NULL) { rslt = -1; goto umount; } -#elif defined(__arm__) - /* - * Copy BOOTARM.EFI to /efi/boot/bootarm.efi. - */ - pathlen = strlen(dst); - if (strlcat(dst, "/bootarm.efi", sizeof(dst)) >= sizeof(dst)) { - rslt = -1; - warn("unable to build /bootarm.efi path"); - goto umount; - } - src = fileprefix(root, "/usr/mdec/BOOTARM.EFI"); - if (src == NULL) { - rslt = -1; - goto umount; - } -#elif defined(__riscv) - /* - * Copy BOOTRISCV64.EFI to /efi/boot/bootriscv64.efi. - */ - pathlen = strlen(dst); - if (strlcat(dst, "/bootriscv64.efi", sizeof(dst)) >= sizeof(dst)) { - rslt = -1; - warn("unable to build /bootriscv64.efi path"); - goto umount; - } - src = fileprefix(root, "/usr/mdec/BOOTRISCV64.EFI"); - if (src == NULL) { - rslt = -1; - goto umount; - } -#endif srclen = strlen(src); if (verbose) fprintf(stderr, "%s %s to %s\n", @@ -308,6 +287,22 @@ write_filesystem(struct disklabel *dl, char part) goto umount; } + /* Write /efi/boot/startup.nsh. */ + dst[pathlen] = '\0'; + if (strlcat(dst, "/startup.nsh", sizeof(dst)) >= sizeof(dst)) { + rslt = -1; + warn("unable to build /startup.nsh path"); + goto umount; + } + if (verbose) + fprintf(stderr, "%s %s\n", + (nowrite ? "would write" : "writing"), dst); + if (!nowrite) { + rslt = fileprintf(dst, "%s\n", BOOTEFI_DST); + if (rslt == -1) + goto umount; + } + rslt = 0; umount: diff --git a/usr.sbin/installboot/installboot.h b/usr.sbin/installboot/installboot.h index 54d362c7ab4..0665592cb92 100644 --- a/usr.sbin/installboot/installboot.h +++ b/usr.sbin/installboot/installboot.h @@ -1,4 +1,4 @@ -/* $OpenBSD: installboot.h,v 1.13 2021/07/20 14:51:56 kettenis Exp $ */ +/* $OpenBSD: installboot.h,v 1.14 2022/02/03 10:25:14 visa Exp $ */ /* * Copyright (c) 2012, 2013 Joel Sing * @@ -33,6 +33,8 @@ void bootstrap(int, char *, char *); int filecopy(const char *, const char *); char *fileprefix(const char *, const char *); +int fileprintf(const char *, const char *, ...) + __attribute__((format(printf, 2, 3))); u_int32_t crc32(const u_char *, const u_int32_t); void md_init(void); diff --git a/usr.sbin/installboot/util.c b/usr.sbin/installboot/util.c index 0ffd5e742f9..a21ccdb2cc2 100644 --- a/usr.sbin/installboot/util.c +++ b/usr.sbin/installboot/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.15 2022/02/02 13:22:10 visa Exp $ */ +/* $OpenBSD: util.c,v 1.16 2022/02/03 10:25:14 visa Exp $ */ /* * Copyright (c) 2014 Joel Sing @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -137,6 +138,46 @@ err: return (NULL); } +int +fileprintf(const char *filename, const char *fmt, ...) +{ + va_list ap; + int fd, ret; + int rslt = -1; + + fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, + S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); + if (fd == -1) { + warn("open %s", filename); + return (-1); + } + if (fchown(fd, 0, 0) == -1) { + if (errno != EINVAL) { + warn("chown"); + goto err; + } + } + if (fchmod(fd, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH) == -1) { + warn("chmod"); + goto err; + } + + va_start(ap, fmt); + ret = vdprintf(fd, fmt, ap); + va_end(ap); + + if (ret < 0) { + warn("vdprintf"); + goto err; + } + + rslt = 0; + +err: + close(fd); + return (rslt); +} + /* * Adapted from Hacker's Delight crc32b(). * -- 2.20.1