2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015 Neel Natu <neel@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
36 #include <machine/vmm.h>
50 #define MAX_BOOTROM_SIZE (16 * 1024 * 1024) /* 16 MB */
53 bootrom_init(struct vmctx *ctx, const char *romfile)
62 fd = open(romfile, O_RDONLY);
64 EPRINTLN("Error opening bootrom \"%s\": %s",
65 romfile, strerror(errno));
69 if (fstat(fd, &sbuf) < 0) {
70 EPRINTLN("Could not fstat bootrom file \"%s\": %s",
71 romfile, strerror(errno));
76 * Limit bootrom size to 16MB so it doesn't encroach into reserved
77 * MMIO space (e.g. APIC, HPET, MSI).
79 if (sbuf.st_size > MAX_BOOTROM_SIZE || sbuf.st_size < PAGE_SIZE) {
80 EPRINTLN("Invalid bootrom size %ld", sbuf.st_size);
84 if (sbuf.st_size & PAGE_MASK) {
85 EPRINTLN("Bootrom size %ld is not a multiple of the "
86 "page size", sbuf.st_size);
90 ptr = vm_create_devmem(ctx, VM_BOOTROM, "bootrom", sbuf.st_size);
91 if (ptr == MAP_FAILED)
94 /* Map the bootrom into the guest address space */
95 prot = PROT_READ | PROT_EXEC;
96 gpa = (1ULL << 32) - sbuf.st_size;
97 if (vm_mmap_memseg(ctx, gpa, VM_BOOTROM, 0, sbuf.st_size, prot) != 0)
100 /* Read 'romfile' into the guest address space */
101 for (i = 0; i < sbuf.st_size / PAGE_SIZE; i++) {
102 rlen = read(fd, ptr + i * PAGE_SIZE, PAGE_SIZE);
103 if (rlen != PAGE_SIZE) {
104 EPRINTLN("Incomplete read of page %d of bootrom "
105 "file %s: %ld bytes", i, romfile, rlen);