Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F102809582
D19976.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Size
5 KB
Referenced Files
None
Subscribers
None
D19976.diff
View Options
diff --git a/usr.sbin/bhyve/bhyve.8 b/usr.sbin/bhyve/bhyve.8
--- a/usr.sbin/bhyve/bhyve.8
+++ b/usr.sbin/bhyve/bhyve.8
@@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
-.Dd October 13, 2021
+.Dd November 28, 2021
.Dt BHYVE 8
.Os
.Sh NAME
@@ -500,10 +500,15 @@
.Pp
Boot ROM device backends:
.Bl -tag -width 10n
-.It Ar romfile
+.It Ar romfile Ns Op Cm \&, Ns Ar varfile
Map
.Ar romfile
in the guest address space reserved for boot firmware.
+If
+.Ar varfile
+is provided, that file is also mapped in the boot firmware guest
+address space, and any modifications the guest makes will be saved
+to that file.
.El
.Pp
Pass-through device backends:
@@ -916,6 +921,20 @@
-l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI.fd \\
uefivm
.Ed
+.Pp
+Run a UEFI virtual machine with a VARS file to save EFI variables.
+Note that
+.Nm
+will write guest modifications to the given VARS file.
+Be sure to create a per-guest copy of the template VARS file from
+.Pa /usr .
+.Bd -literal -offset indent
+bhyve -c 2 -m 4g -w -H \\
+ -s 0,hostbridge \\
+ -s 31,lpc -p com1,stdio \\
+ -l bootrom,/usr/local/share/uefi-firmware/BHYVE_UEFI_CODE.fd,BHYVE_UEFI_VARS.fd
+ uefivm
+.Ed
.Sh SEE ALSO
.Xr bhyve 4 ,
.Xr netgraph 4 ,
diff --git a/usr.sbin/bhyve/bootrom.c b/usr.sbin/bhyve/bootrom.c
--- a/usr.sbin/bhyve/bootrom.c
+++ b/usr.sbin/bhyve/bootrom.c
@@ -39,14 +39,17 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <vmmapi.h>
+
#include "bhyverun.h"
#include "bootrom.h"
#include "debug.h"
+#include "mem.h"
#define BOOTROM_SIZE (16 * 1024 * 1024) /* 16 MB */
@@ -64,6 +67,56 @@
static vm_paddr_t gpa_allocbot; /* Low GPA of free region. */
static vm_paddr_t gpa_alloctop; /* High GPA, minus 1, of free region. */
+#define CFI_BCS_WRITE_BYTE 0x10
+#define CFI_BCS_CLEAR_STATUS 0x50
+#define CFI_BCS_READ_STATUS 0x70
+#define CFI_BCS_READ_ARRAY 0xff
+
+static struct bootrom_var_state {
+ uint8_t *mmap;
+ uint64_t gpa;
+ off_t size;
+ uint8_t cmd;
+} var = { NULL, 0, 0, CFI_BCS_READ_ARRAY };
+
+/*
+ * Emulate just those CFI basic commands that will convince EDK II
+ * that the Firmware Volume area is writable and persistent.
+ */
+static int
+bootrom_var_mem_handler(struct vmctx *ctx, int vcpu, int dir, uint64_t addr,
+ int size, uint64_t *val, void *arg1, long arg2)
+{
+ off_t offset;
+
+ offset = addr - var.gpa;
+ if (offset + size > var.size || offset < 0 || offset + size <= offset)
+ return (EINVAL);
+
+ if (dir == MEM_F_WRITE) {
+ switch (var.cmd) {
+ case CFI_BCS_WRITE_BYTE:
+ memcpy(var.mmap + offset, val, size);
+ var.cmd = CFI_BCS_READ_ARRAY;
+ break;
+ default:
+ var.cmd = *(uint8_t *)val;
+ }
+ } else {
+ switch (var.cmd) {
+ case CFI_BCS_CLEAR_STATUS:
+ case CFI_BCS_READ_STATUS:
+ memset(val, 0, size);
+ var.cmd = CFI_BCS_READ_ARRAY;
+ break;
+ default:
+ memcpy(val, var.mmap + offset, size);
+ break;
+ }
+ }
+ return (0);
+}
+
void
init_bootrom(struct vmctx *ctx)
{
@@ -142,10 +195,16 @@
{
struct stat sbuf;
ssize_t rlen;
- char *ptr;
- int fd, i, rv;
+ off_t rom_size, var_size, total_size;
+ char *ptr, *varfile;
+ int fd, varfd, i, rv;
rv = -1;
+ varfd = -1;
+
+ varfile = strdup(romfile);
+ romfile = strsep(&varfile, ",");
+
fd = open(romfile, O_RDONLY);
if (fd < 0) {
EPRINTLN("Error opening bootrom \"%s\": %s",
@@ -153,19 +212,56 @@
goto done;
}
- if (fstat(fd, &sbuf) < 0) {
+ if (varfile != NULL) {
+ varfd = open(varfile, O_RDWR);
+ if (varfd < 0) {
+ fprintf(stderr, "Error opening bootrom variable file "
+ "\"%s\": %s\n", varfile, strerror(errno));
+ goto done;
+ }
+ }
+
+ if (fstat(fd, &sbuf) < 0) {
EPRINTLN("Could not fstat bootrom file \"%s\": %s",
- romfile, strerror(errno));
+ romfile, strerror(errno));
goto done;
- }
+ }
+
+ rom_size = sbuf.st_size;
+ if (varfd < 0) {
+ var_size = 0;
+ } else {
+ if (fstat(varfd, &sbuf) < 0) {
+ fprintf(stderr, "Could not fstat bootrom variable file \"%s\": %s\n",
+ varfile, strerror(errno));
+ goto done;
+ }
+ var_size = sbuf.st_size;
+ }
+
+ if (var_size > BOOTROM_SIZE ||
+ (var_size != 0 && var_size < PAGE_SIZE)) {
+ fprintf(stderr, "Invalid bootrom variable size %ld\n",
+ var_size);
+ goto done;
+ }
+
+ total_size = rom_size + var_size;
+
+ if (total_size > BOOTROM_SIZE) {
+ fprintf(stderr, "Invalid bootrom and variable aggregate size "
+ "%ld\n", total_size);
+ goto done;
+ }
/* Map the bootrom into the guest address space */
- if (bootrom_alloc(ctx, sbuf.st_size, PROT_READ | PROT_EXEC,
- BOOTROM_ALLOC_TOP, &ptr, NULL) != 0)
+ if (bootrom_alloc(ctx, rom_size, PROT_READ | PROT_EXEC,
+ BOOTROM_ALLOC_TOP, &ptr, NULL) != 0) {
goto done;
+ }
/* Read 'romfile' into the guest address space */
- for (i = 0; i < sbuf.st_size / PAGE_SIZE; i++) {
+ for (i = 0; i < rom_size / PAGE_SIZE; i++) {
rlen = read(fd, ptr + i * PAGE_SIZE, PAGE_SIZE);
if (rlen != PAGE_SIZE) {
EPRINTLN("Incomplete read of page %d of bootrom "
@@ -173,6 +269,26 @@
goto done;
}
}
+
+ if (varfd >= 0) {
+ var.mmap = mmap(NULL, var_size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, varfd, 0);
+ if (var.mmap == MAP_FAILED)
+ goto done;
+ var.size = var_size;
+ var.gpa = (gpa_alloctop - var_size) + 1;
+ gpa_alloctop = var.gpa - 1;
+ rv = register_mem(&(struct mem_range){
+ .name = "bootrom variable",
+ .flags = MEM_F_RW,
+ .handler = bootrom_var_mem_handler,
+ .base = var.gpa,
+ .size = var.size,
+ });
+ if (rv != 0)
+ goto done;
+ }
+
rv = 0;
done:
if (fd >= 0)
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 18, 11:41 AM (21 h, 53 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
14695405
Default Alt Text
D19976.diff (5 KB)
Attached To
Mode
D19976: bhyve: Support a _VARS.fd file for bootrom
Attached
Detach File
Event Timeline
Log In to Comment