xref: /illumos-gate/usr/src/cmd/bhyve/bootrom.c (revision 8fff7887)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31 
32 #include <sys/types.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35 
36 #include <machine/vmm.h>
37 
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdbool.h>
44 
45 #include <vmmapi.h>
46 #include "bhyverun.h"
47 #include "bootrom.h"
48 
49 #define	MAX_BOOTROM_SIZE	(16 * 1024 * 1024)	/* 16 MB */
50 
51 int
52 bootrom_init(struct vmctx *ctx, const char *romfile)
53 {
54 	struct stat sbuf;
55 	vm_paddr_t gpa;
56 	ssize_t rlen;
57 	char *ptr;
58 	int fd, i, rv, prot;
59 
60 	rv = -1;
61 	fd = open(romfile, O_RDONLY);
62 	if (fd < 0) {
63 		fprintf(stderr, "Error opening bootrom \"%s\": %s\n",
64 		    romfile, strerror(errno));
65 		goto done;
66 	}
67 
68         if (fstat(fd, &sbuf) < 0) {
69 		fprintf(stderr, "Could not fstat bootrom file \"%s\": %s\n",
70 		    romfile, strerror(errno));
71 		goto done;
72         }
73 
74 	/*
75 	 * Limit bootrom size to 16MB so it doesn't encroach into reserved
76 	 * MMIO space (e.g. APIC, HPET, MSI).
77 	 */
78 	if (sbuf.st_size > MAX_BOOTROM_SIZE || sbuf.st_size < PAGE_SIZE) {
79 		fprintf(stderr, "Invalid bootrom size %ld\n", sbuf.st_size);
80 		goto done;
81 	}
82 
83 	if (sbuf.st_size & PAGE_MASK) {
84 		fprintf(stderr, "Bootrom size %ld is not a multiple of the "
85 		    "page size\n", sbuf.st_size);
86 		goto done;
87 	}
88 
89 	ptr = vm_create_devmem(ctx, VM_BOOTROM, "bootrom", sbuf.st_size);
90 	if (ptr == MAP_FAILED)
91 		goto done;
92 
93 	/* Map the bootrom into the guest address space */
94 	prot = PROT_READ | PROT_EXEC;
95 	gpa = (1ULL << 32) - sbuf.st_size;
96 	if (vm_mmap_memseg(ctx, gpa, VM_BOOTROM, 0, sbuf.st_size, prot) != 0)
97 		goto done;
98 
99 	/* Read 'romfile' into the guest address space */
100 	for (i = 0; i < sbuf.st_size / PAGE_SIZE; i++) {
101 		rlen = read(fd, ptr + i * PAGE_SIZE, PAGE_SIZE);
102 		if (rlen != PAGE_SIZE) {
103 			fprintf(stderr, "Incomplete read of page %d of bootrom "
104 			    "file %s: %ld bytes\n", i, romfile, rlen);
105 			goto done;
106 		}
107 	}
108 	rv = 0;
109 done:
110 	if (fd >= 0)
111 		close(fd);
112 	return (rv);
113 }
114