xref: /freebsd/usr.sbin/bhyve/bootrom.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015 Neel Natu <neel@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 
34 #include <machine/vmm.h>
35 
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdbool.h>
44 
45 #include <vmmapi.h>
46 
47 #include "bhyverun.h"
48 #include "bootrom.h"
49 #include "debug.h"
50 #include "mem.h"
51 
52 #define	BOOTROM_SIZE	(16 * 1024 * 1024)	/* 16 MB */
53 
54 /*
55  * ROM region is 16 MB at the top of 4GB ("low") memory.
56  *
57  * The size is limited so it doesn't encroach into reserved MMIO space (e.g.,
58  * APIC, HPET, MSI).
59  *
60  * It is allocated in page-multiple blocks on a first-come first-serve basis,
61  * from high to low, during initialization, and does not change at runtime.
62  */
63 static char *romptr;	/* Pointer to userspace-mapped bootrom region. */
64 static vm_paddr_t gpa_base;	/* GPA of low end of region. */
65 static vm_paddr_t gpa_allocbot;	/* Low GPA of free region. */
66 static vm_paddr_t gpa_alloctop;	/* High GPA, minus 1, of free region. */
67 
68 #define CFI_BCS_WRITE_BYTE      0x10
69 #define CFI_BCS_CLEAR_STATUS    0x50
70 #define CFI_BCS_READ_STATUS     0x70
71 #define CFI_BCS_READ_ARRAY      0xff
72 
73 static struct bootrom_var_state {
74 	uint8_t		*mmap;
75 	uint64_t	gpa;
76 	off_t		size;
77 	uint8_t		cmd;
78 } var = { NULL, 0, 0, CFI_BCS_READ_ARRAY };
79 
80 /*
81  * Emulate just those CFI basic commands that will convince EDK II
82  * that the Firmware Volume area is writable and persistent.
83  */
84 static int
85 bootrom_var_mem_handler(struct vcpu *vcpu __unused, int dir, uint64_t addr,
86     int size, uint64_t *val, void *arg1 __unused, long arg2 __unused)
87 {
88 	off_t offset;
89 
90 	offset = addr - var.gpa;
91 	if (offset + size > var.size || offset < 0 || offset + size <= offset)
92 		return (EINVAL);
93 
94 	if (dir == MEM_F_WRITE) {
95 		switch (var.cmd) {
96 		case CFI_BCS_WRITE_BYTE:
97 			memcpy(var.mmap + offset, val, size);
98 			var.cmd = CFI_BCS_READ_ARRAY;
99 			break;
100 		default:
101 			var.cmd = *(uint8_t *)val;
102 		}
103 	} else {
104 		switch (var.cmd) {
105 		case CFI_BCS_CLEAR_STATUS:
106 		case CFI_BCS_READ_STATUS:
107 			memset(val, 0, size);
108 			var.cmd = CFI_BCS_READ_ARRAY;
109 			break;
110 		default:
111 			memcpy(val, var.mmap + offset, size);
112 			break;
113 		}
114 	}
115 	return (0);
116 }
117 
118 void
119 init_bootrom(struct vmctx *ctx)
120 {
121 	romptr = vm_create_devmem(ctx, VM_BOOTROM, "bootrom", BOOTROM_SIZE);
122 	if (romptr == MAP_FAILED)
123 		err(4, "%s: vm_create_devmem", __func__);
124 	gpa_base = (1ULL << 32) - BOOTROM_SIZE;
125 	gpa_allocbot = gpa_base;
126 	gpa_alloctop = (1ULL << 32) - 1;
127 }
128 
129 int
130 bootrom_alloc(struct vmctx *ctx, size_t len, int prot, int flags,
131     char **region_out, uint64_t *gpa_out)
132 {
133 	static const int bootrom_valid_flags = BOOTROM_ALLOC_TOP;
134 
135 	vm_paddr_t gpa;
136 	vm_ooffset_t segoff;
137 
138 	if (flags & ~bootrom_valid_flags) {
139 		warnx("%s: Invalid flags: %x", __func__,
140 		    flags & ~bootrom_valid_flags);
141 		return (EINVAL);
142 	}
143 	if (prot & ~_PROT_ALL) {
144 		warnx("%s: Invalid protection: %x", __func__,
145 		    prot & ~_PROT_ALL);
146 		return (EINVAL);
147 	}
148 
149 	if (len == 0 || len > BOOTROM_SIZE) {
150 		warnx("ROM size %zu is invalid", len);
151 		return (EINVAL);
152 	}
153 	if (len & PAGE_MASK) {
154 		warnx("ROM size %zu is not a multiple of the page size",
155 		    len);
156 		return (EINVAL);
157 	}
158 
159 	if (flags & BOOTROM_ALLOC_TOP) {
160 		gpa = (gpa_alloctop - len) + 1;
161 		if (gpa < gpa_allocbot) {
162 			warnx("No room for %zu ROM in bootrom region", len);
163 			return (ENOMEM);
164 		}
165 	} else {
166 		gpa = gpa_allocbot;
167 		if (gpa > (gpa_alloctop - len) + 1) {
168 			warnx("No room for %zu ROM in bootrom region", len);
169 			return (ENOMEM);
170 		}
171 	}
172 
173 	segoff = gpa - gpa_base;
174 	if (vm_mmap_memseg(ctx, gpa, VM_BOOTROM, segoff, len, prot) != 0) {
175 		int serrno = errno;
176 		warn("%s: vm_mmap_mapseg", __func__);
177 		return (serrno);
178 	}
179 
180 	if (flags & BOOTROM_ALLOC_TOP)
181 		gpa_alloctop = gpa - 1;
182 	else
183 		gpa_allocbot = gpa + len;
184 
185 	*region_out = romptr + segoff;
186 	if (gpa_out != NULL)
187 		*gpa_out = gpa;
188 	return (0);
189 }
190 
191 int
192 bootrom_loadrom(struct vmctx *ctx, const nvlist_t *nvl)
193 {
194 	struct stat sbuf;
195 	ssize_t rlen;
196 	off_t rom_size, var_size, total_size;
197 	char *ptr, *romfile;
198 	int fd, varfd, i, rv;
199 	const char *bootrom, *varfile;
200 
201 	rv = -1;
202 	varfd = -1;
203 
204 	bootrom = get_config_value_node(nvl, "bootrom");
205 	if (bootrom == NULL) {
206 		return (-1);
207 	}
208 
209 	/*
210 	 * get_config_value_node may use a thread local buffer to return
211 	 * variables. So, when we query the second variable, the first variable
212 	 * might get overwritten. For that reason, the bootrom should be
213 	 * duplicated.
214 	 */
215 	romfile = strdup(bootrom);
216 	if (romfile == NULL) {
217 		return (-1);
218 	}
219 
220 	fd = open(romfile, O_RDONLY);
221 	if (fd < 0) {
222 		EPRINTLN("Error opening bootrom \"%s\": %s",
223 		    romfile, strerror(errno));
224 		goto done;
225 	}
226 
227 	if (fstat(fd, &sbuf) < 0) {
228 		EPRINTLN("Could not fstat bootrom file \"%s\": %s", romfile,
229 		    strerror(errno));
230 		goto done;
231 	}
232 
233 	rom_size = sbuf.st_size;
234 
235 	varfile = get_config_value_node(nvl, "bootvars");
236 	var_size = 0;
237 	if (varfile != NULL) {
238 		varfd = open(varfile, O_RDWR);
239 		if (varfd < 0) {
240 			EPRINTLN("Error opening bootrom variable file "
241 			    "\"%s\": %s", varfile, strerror(errno));
242 			goto done;
243 		}
244 
245 		if (fstat(varfd, &sbuf) < 0) {
246 			EPRINTLN(
247 			    "Could not fstat bootrom variable file \"%s\": %s",
248 			    varfile, strerror(errno));
249 			goto done;
250 		}
251 
252 		var_size = sbuf.st_size;
253 	}
254 
255 	if (var_size > BOOTROM_SIZE ||
256 	    (var_size != 0 && var_size < PAGE_SIZE)) {
257 		EPRINTLN("Invalid bootrom variable size %ld",
258 		    var_size);
259 		goto done;
260 	}
261 
262 	total_size = rom_size + var_size;
263 
264 	if (total_size > BOOTROM_SIZE) {
265 		EPRINTLN("Invalid bootrom and variable aggregate size %ld",
266 		    total_size);
267 		goto done;
268 	}
269 
270 	/* Map the bootrom into the guest address space */
271 	if (bootrom_alloc(ctx, rom_size, PROT_READ | PROT_EXEC,
272 	    BOOTROM_ALLOC_TOP, &ptr, NULL) != 0) {
273 		goto done;
274 	}
275 
276 	/* Read 'romfile' into the guest address space */
277 	for (i = 0; i < rom_size / PAGE_SIZE; i++) {
278 		rlen = read(fd, ptr + i * PAGE_SIZE, PAGE_SIZE);
279 		if (rlen != PAGE_SIZE) {
280 			EPRINTLN("Incomplete read of page %d of bootrom "
281 			    "file %s: %ld bytes", i, romfile, rlen);
282 			goto done;
283 		}
284 	}
285 
286 	if (varfd >= 0) {
287 		var.mmap = mmap(NULL, var_size, PROT_READ | PROT_WRITE,
288 		    MAP_SHARED, varfd, 0);
289 		if (var.mmap == MAP_FAILED)
290 			goto done;
291 		var.size = var_size;
292 		var.gpa = (gpa_alloctop - var_size) + 1;
293 		gpa_alloctop = var.gpa - 1;
294 		rv = register_mem(&(struct mem_range){
295 		    .name = "bootrom variable",
296 		    .flags = MEM_F_RW,
297 		    .handler = bootrom_var_mem_handler,
298 		    .base = var.gpa,
299 		    .size = var.size,
300 		});
301 		if (rv != 0)
302 			goto done;
303 	}
304 
305 	rv = 0;
306 done:
307 	if (varfd >= 0)
308 		close(varfd);
309 	if (fd >= 0)
310 		close(fd);
311 	free(romfile);
312 	return (rv);
313 }
314