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 #include "debug.h" 49 50 #define MAX_BOOTROM_SIZE (16 * 1024 * 1024) /* 16 MB */ 51 52 int 53 bootrom_init(struct vmctx *ctx, const char *romfile) 54 { 55 struct stat sbuf; 56 vm_paddr_t gpa; 57 ssize_t rlen; 58 char *ptr; 59 int fd, i, rv, prot; 60 61 rv = -1; 62 fd = open(romfile, O_RDONLY); 63 if (fd < 0) { 64 EPRINTLN("Error opening bootrom \"%s\": %s", 65 romfile, strerror(errno)); 66 goto done; 67 } 68 69 if (fstat(fd, &sbuf) < 0) { 70 EPRINTLN("Could not fstat bootrom file \"%s\": %s", 71 romfile, strerror(errno)); 72 goto done; 73 } 74 75 /* 76 * Limit bootrom size to 16MB so it doesn't encroach into reserved 77 * MMIO space (e.g. APIC, HPET, MSI). 78 */ 79 if (sbuf.st_size > MAX_BOOTROM_SIZE || sbuf.st_size < PAGE_SIZE) { 80 EPRINTLN("Invalid bootrom size %ld", sbuf.st_size); 81 goto done; 82 } 83 84 if (sbuf.st_size & PAGE_MASK) { 85 EPRINTLN("Bootrom size %ld is not a multiple of the " 86 "page size", sbuf.st_size); 87 goto done; 88 } 89 90 ptr = vm_create_devmem(ctx, VM_BOOTROM, "bootrom", sbuf.st_size); 91 if (ptr == MAP_FAILED) 92 goto done; 93 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) 98 goto done; 99 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); 106 goto done; 107 } 108 } 109 rv = 0; 110 done: 111 if (fd >= 0) 112 close(fd); 113 return (rv); 114 } 115