> Date: Wed, 2 Feb 2022 14:16:03 +0000
> From: Visa Hankala <[email protected]>
>
> On Tue, Feb 01, 2022 at 10:55:03AM -0700, Theo de Raadt wrote:
> > Mark Kettenis <[email protected]> wrote:
> >
> > > Maybe we should rename the file to efi_installboot.c and/or rearrange
> > > the code slightly such that is becomes more obvious that the name of
> > > the file is indeed the only difference.
> >
> > And obviously, efi_installboot.c would do this on all efi architectures:
> >
> > echo bootriscv64.efi > /mnt/mnt/efi/boot/startup.nsh
> >
> > OK let's call it
> >
> > write "boot%s.efi\n" to efi/boot/startup.nsh
> >
> > On all architectures. Always. Where %s is a rather strange substitution.
>
> Here is a revised diff that additionally makes the depicted installboot
> adjustments. For clarity, the diff refers to armv7_installboot.c, but
> I can rename the file to efi_installboot.c in a separate, preceding
> commit.
>
> This skips updating of install.md of arm64 and armv7. However, these
> installboot modifications should not break the scripts.
Looks good to me. The fchown/fchmod won't be very useful on a msdos
filesystem, but the code might be used in other places at some point.
I'll take care of arm64 once this is in and take a look at armv7.
ok kettenis@
> Index: distrib/riscv64/ramdisk/install.md
> ===================================================================
> RCS file: src/distrib/riscv64/ramdisk/install.md,v
> retrieving revision 1.3
> diff -u -p -r1.3 install.md
> --- distrib/riscv64/ramdisk/install.md 2 Aug 2021 21:46:39 -0000
> 1.3
> +++ distrib/riscv64/ramdisk/install.md 2 Feb 2022 14:11:08 -0000
> @@ -33,16 +33,13 @@
> #
>
> NCPU=$(sysctl -n hw.ncpufound)
> -NEWFSARGS_msdos="-F 16 -L boot"
> -MOUNT_ARGS_msdos="-o-l"
>
> md_installboot() {
> - local _disk=/dev/$1
> -
> - mount ${MOUNT_ARGS_msdos} ${_disk}i /mnt/mnt
> - mkdir -p /mnt/mnt/efi/boot
> - cp /mnt/usr/mdec/BOOTRISCV64.EFI /mnt/mnt/efi/boot/bootriscv64.efi
> - echo bootriscv64.efi > /mnt/mnt/efi/boot/startup.nsh
> + if ! installboot -r /mnt ${1}; then
> + echo "\nFailed to install bootblocks."
> + echo "You will not be able to boot OpenBSD from ${1}."
> + exit
> + fi
> }
>
> md_prep_fdisk() {
> @@ -53,7 +50,6 @@ md_prep_fdisk() {
> local bootsectorsize="32768"
> local bootsectorend=$(($bootsectorstart + $bootsectorsize))
> local bootfstype="msdos"
> - local newfs_args=${NEWFSARGS_msdos}
>
> while :; do
> _d=whole
> @@ -83,8 +79,7 @@ write
> quit
> __EOT
> echo "done."
> - disklabel $_disk 2>/dev/null | grep -q "^ i:" ||
> disklabel -w -d $_disk
> - newfs -t ${bootfstype} ${newfs_args} ${_disk}i
> + installboot -p $_disk
> return ;;
> [eE]*)
> # Manually configure the MBR.
> Index: distrib/riscv64/ramdisk/list
> ===================================================================
> RCS file: src/distrib/riscv64/ramdisk/list,v
> retrieving revision 1.5
> diff -u -p -r1.5 list
> --- distrib/riscv64/ramdisk/list 4 Oct 2021 01:34:29 -0000 1.5
> +++ distrib/riscv64/ramdisk/list 2 Feb 2022 14:11:08 -0000
> @@ -62,6 +62,7 @@ LINK instbin
> usr/bin/sed
> LINK instbin usr/bin/signify
> LINK instbin usr/bin/tee
> LINK instbin usr/sbin/chroot
> +LINK instbin usr/sbin/installboot
> LINK instbin usr/sbin/pwd_mkdb
> ARGVLINK ksh -sh
> SPECIAL rm bin/md5
> Index: distrib/special/installboot/Makefile
> ===================================================================
> RCS file: src/distrib/special/installboot/Makefile,v
> retrieving revision 1.14
> diff -u -p -r1.14 Makefile
> --- distrib/special/installboot/Makefile 20 Jul 2020 14:55:43 -0000
> 1.14
> +++ distrib/special/installboot/Makefile 2 Feb 2022 14:11:08 -0000
> @@ -15,7 +15,7 @@ CFLAGS += -DSOFTRAID
> SRCS += i386_installboot.c
> SRCS += i386_nlist.c
> SRCS += i386_softraid.c
> -.elif ${MACHINE} == "armv7" || ${MACHINE} == "arm64"
> +.elif ${MACHINE} == "armv7" || ${MACHINE} == "arm64" || ${MACHINE} ==
> "riscv64"
> SRCS += armv7_installboot.c
> .elif ${MACHINE} == "hppa"
> CFLAGS += -DBOOTSTRAP
> Index: usr.sbin/installboot/armv7_installboot.c
> ===================================================================
> RCS file: src/usr.sbin/installboot/armv7_installboot.c,v
> retrieving revision 1.11
> diff -u -p -r1.11 armv7_installboot.c
> --- usr.sbin/installboot/armv7_installboot.c 20 Jul 2021 14:51:56 -0000
> 1.11
> +++ usr.sbin/installboot/armv7_installboot.c 2 Feb 2022 14:11:08 -0000
> @@ -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, c
> 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, c
> 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:
> Index: usr.sbin/installboot/installboot.h
> ===================================================================
> RCS file: src/usr.sbin/installboot/installboot.h,v
> retrieving revision 1.13
> diff -u -p -r1.13 installboot.h
> --- usr.sbin/installboot/installboot.h 20 Jul 2021 14:51:56 -0000
> 1.13
> +++ usr.sbin/installboot/installboot.h 2 Feb 2022 14:11:08 -0000
> @@ -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);
> Index: usr.sbin/installboot/util.c
> ===================================================================
> RCS file: src/usr.sbin/installboot/util.c,v
> retrieving revision 1.15
> diff -u -p -r1.15 util.c
> --- usr.sbin/installboot/util.c 2 Feb 2022 13:22:10 -0000 1.15
> +++ usr.sbin/installboot/util.c 2 Feb 2022 14:11:08 -0000
> @@ -20,6 +20,7 @@
> #include <err.h>
> #include <errno.h>
> #include <fcntl.h>
> +#include <stdarg.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> @@ -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().
> *
>