2 * Copyright (c) 2015 Neel Natu <neel@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/param.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
34 #include <machine/vmm.h>
47 #define MAX_BOOTROM_SIZE (16 * 1024 * 1024) /* 16 MB */
50 bootrom_init(struct vmctx *ctx, const char *romfile)
59 fd = open(romfile, O_RDONLY);
61 fprintf(stderr, "Error opening bootrom \"%s\": %s\n",
62 romfile, strerror(errno));
66 if (fstat(fd, &sbuf) < 0) {
67 fprintf(stderr, "Could not fstat bootrom file \"%s\": %s\n",
68 romfile, strerror(errno));
73 * Limit bootrom size to 16MB so it doesn't encroach into reserved
74 * MMIO space (e.g. APIC, HPET, MSI).
76 if (sbuf.st_size > MAX_BOOTROM_SIZE || sbuf.st_size < PAGE_SIZE) {
77 fprintf(stderr, "Invalid bootrom size %ld\n", sbuf.st_size);
81 if (sbuf.st_size & PAGE_MASK) {
82 fprintf(stderr, "Bootrom size %ld is not a multiple of the "
83 "page size\n", sbuf.st_size);
87 ptr = vm_create_devmem(ctx, VM_BOOTROM, "bootrom", sbuf.st_size);
88 if (ptr == MAP_FAILED)
91 /* Map the bootrom into the guest address space */
92 prot = PROT_READ | PROT_EXEC;
93 gpa = (1ULL << 32) - sbuf.st_size;
94 if (vm_mmap_memseg(ctx, gpa, VM_BOOTROM, 0, sbuf.st_size, prot) != 0)
97 /* Read 'romfile' into the guest address space */
98 for (i = 0; i < sbuf.st_size / PAGE_SIZE; i++) {
99 rlen = read(fd, ptr + i * PAGE_SIZE, PAGE_SIZE);
100 if (rlen != PAGE_SIZE) {
101 fprintf(stderr, "Incomplete read of page %d of bootrom "
102 "file %s: %ld bytes\n", i, romfile, rlen);