-/* $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 $ */
/*
#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 *);
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",
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:
-/* $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 <jsing@openbsd.org>
*
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);
-/* $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 <jsing@openbsd.org>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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().
*