1 /*- 2 * Copyright (c) 2015 Neel Natu <neel@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/param.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/types.h> 31 #include <sys/mman.h> 32 #include <sys/stat.h> 33 34 #include <machine/vmm.h> 35 36 #include <errno.h> 37 #include <fcntl.h> 38 #include <stdio.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include <stdbool.h> 42 43 #include <vmmapi.h> 44 #include "bhyverun.h" 45 #include "bootrom.h" 46 47 #define MAX_BOOTROM_SIZE (16 * 1024 * 1024) /* 16 MB */ 48 49 int 50 bootrom_init(struct vmctx *ctx, const char *romfile) 51 { 52 struct stat sbuf; 53 vm_paddr_t gpa; 54 ssize_t rlen; 55 char *ptr; 56 int fd, i, rv, prot; 57 58 rv = -1; 59 fd = open(romfile, O_RDONLY); 60 if (fd < 0) { 61 fprintf(stderr, "Error opening bootrom \"%s\": %s\n", 62 romfile, strerror(errno)); 63 goto done; 64 } 65 66 if (fstat(fd, &sbuf) < 0) { 67 fprintf(stderr, "Could not fstat bootrom file \"%s\": %s\n", 68 romfile, strerror(errno)); 69 goto done; 70 } 71 72 /* 73 * Limit bootrom size to 16MB so it doesn't encroach into reserved 74 * MMIO space (e.g. APIC, HPET, MSI). 75 */ 76 if (sbuf.st_size > MAX_BOOTROM_SIZE || sbuf.st_size < PAGE_SIZE) { 77 fprintf(stderr, "Invalid bootrom size %ld\n", sbuf.st_size); 78 goto done; 79 } 80 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); 84 goto done; 85 } 86 87 ptr = vm_create_devmem(ctx, VM_BOOTROM, "bootrom", sbuf.st_size); 88 if (ptr == MAP_FAILED) 89 goto done; 90 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) 95 goto done; 96 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); 103 goto done; 104 } 105 } 106 rv = 0; 107 done: 108 if (fd >= 0) 109 close(fd); 110 return (rv); 111 } 112