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>
49 #define MAX_BOOTROM_SIZE (16 * 1024 * 1024) /* 16 MB */
52 bootrom_init(struct vmctx *ctx, const char *romfile)
61 fd = open(romfile, O_RDONLY);
63 fprintf(stderr, "Error opening bootrom \"%s\": %s\n",
64 romfile, strerror(errno));
68 if (fstat(fd, &sbuf) < 0) {
69 fprintf(stderr, "Could not fstat bootrom file \"%s\": %s\n",
70 romfile, strerror(errno));
75 * Limit bootrom size to 16MB so it doesn't encroach into reserved
76 * MMIO space (e.g. APIC, HPET, MSI).
78 if (sbuf.st_size > MAX_BOOTROM_SIZE || sbuf.st_size < PAGE_SIZE) {
79 fprintf(stderr, "Invalid bootrom size %ld\n", sbuf.st_size);
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);
89 ptr = vm_create_devmem(ctx, VM_BOOTROM, "bootrom", sbuf.st_size);
90 if (ptr == MAP_FAILED)
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)
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);