1 1.5 scole /* $NetBSD: bootinfo.c,v 1.5 2016/08/04 18:07:43 scole Exp $ */ 2 1.1 cherry 3 1.1 cherry /*- 4 1.1 cherry * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org> 5 1.1 cherry * All rights reserved. 6 1.1 cherry * 7 1.1 cherry * Redistribution and use in source and binary forms, with or without 8 1.1 cherry * modification, are permitted provided that the following conditions 9 1.1 cherry * are met: 10 1.1 cherry * 1. Redistributions of source code must retain the above copyright 11 1.1 cherry * notice, this list of conditions and the following disclaimer. 12 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 cherry * notice, this list of conditions and the following disclaimer in the 14 1.1 cherry * documentation and/or other materials provided with the distribution. 15 1.1 cherry * 16 1.1 cherry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 1.1 cherry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 1.1 cherry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 1.1 cherry * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 1.1 cherry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 1.1 cherry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 1.1 cherry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 1.1 cherry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 1.1 cherry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 cherry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 cherry * SUCH DAMAGE. 27 1.1 cherry */ 28 1.1 cherry 29 1.1 cherry #include <sys/cdefs.h> 30 1.2 cherry /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/bootinfo.c,v 1.10 2004/01/04 23:28:16 obrien Exp $"); */ 31 1.1 cherry 32 1.1 cherry #include <lib/libsa/stand.h> 33 1.1 cherry #include <lib/libsa/loadfile.h> 34 1.4 martin #include <lib/libkern/libkern.h> 35 1.1 cherry 36 1.1 cherry #include <sys/param.h> 37 1.1 cherry #include <sys/reboot.h> 38 1.1 cherry #include <sys/boot_flag.h> 39 1.1 cherry #include <sys/exec_elf.h> 40 1.1 cherry #include <sys/lock.h> 41 1.1 cherry 42 1.1 cherry #include <machine/vmparam.h> 43 1.1 cherry #include <machine/elf_machdep.h> 44 1.1 cherry #include <machine/bootinfo.h> 45 1.1 cherry 46 1.1 cherry 47 1.1 cherry #include <efi.h> 48 1.1 cherry #include <efilib.h> 49 1.4 martin #include <efiboot.h> 50 1.1 cherry 51 1.5 scole #include <machine/efilib.h> 52 1.5 scole 53 1.1 cherry #include "bootstrap.h" 54 1.1 cherry 55 1.1 cherry static EFI_GUID hcdp = HCDP_TABLE_GUID; 56 1.1 cherry 57 1.1 cherry /* 58 1.1 cherry * Return a 'boothowto' value corresponding to the kernel arguments in 59 1.1 cherry * (kargs) and any relevant environment variables. 60 1.1 cherry */ 61 1.1 cherry static struct 62 1.1 cherry { 63 1.1 cherry const char *ev; 64 1.1 cherry int mask; 65 1.1 cherry } howto_names[] = { 66 1.1 cherry {"boot_askname", RB_ASKNAME}, 67 1.1 cherry {"boot_single", RB_SINGLE}, 68 1.1 cherry {"boot_nosync", RB_NOSYNC}, 69 1.1 cherry {"boot_halt", RB_HALT}, 70 1.1 cherry {"boot_kdb", RB_KDB}, 71 1.1 cherry {"boot_rdonly", RB_RDONLY}, 72 1.1 cherry {"boot_dump", RB_DUMP}, 73 1.1 cherry {"boot_miniroot", RB_MINIROOT}, 74 1.1 cherry {"boot_userconf", RB_USERCONF}, 75 1.1 cherry {NULL, 0} 76 1.1 cherry }; 77 1.1 cherry 78 1.1 cherry extern char *efi_fmtdev(void *vdev); 79 1.1 cherry 80 1.1 cherry int 81 1.1 cherry bi_getboothowto(char *kargs) 82 1.1 cherry { 83 1.1 cherry char *cp; 84 1.1 cherry int howto; 85 1.1 cherry int active; 86 1.1 cherry int i; 87 1.1 cherry 88 1.1 cherry /* Parse kargs */ 89 1.1 cherry howto = 0; 90 1.1 cherry if (kargs != NULL) { 91 1.1 cherry cp = kargs; 92 1.1 cherry active = 0; 93 1.1 cherry while (*cp != 0) { 94 1.1 cherry if (!active && (*cp == '-')) { 95 1.1 cherry active = 1; 96 1.1 cherry } else if (active) 97 1.1 cherry 98 1.1 cherry BOOT_FLAG(*cp, howto); 99 1.1 cherry 100 1.1 cherry cp++; 101 1.1 cherry } 102 1.1 cherry } 103 1.1 cherry /* get equivalents from the environment */ 104 1.1 cherry for (i = 0; howto_names[i].ev != NULL; i++) 105 1.1 cherry if (getenv(howto_names[i].ev) != NULL) 106 1.1 cherry howto |= howto_names[i].mask; 107 1.1 cherry return(howto); 108 1.1 cherry } 109 1.1 cherry 110 1.1 cherry /* 111 1.1 cherry * Copy the environment into the load area starting at (addr). 112 1.1 cherry * Each variable is formatted as <name>=<value>, with a single nul 113 1.1 cherry * separating each variable, and a double nul terminating the environment. 114 1.1 cherry */ 115 1.1 cherry vaddr_t 116 1.1 cherry bi_copyenv(vaddr_t addr) 117 1.1 cherry { 118 1.1 cherry struct env_var *ep; 119 1.1 cherry 120 1.1 cherry /* traverse the environment */ 121 1.1 cherry for (ep = environ; ep != NULL; ep = ep->ev_next) { 122 1.1 cherry efi_copyin(ep->ev_name, addr, strlen(ep->ev_name)); 123 1.1 cherry addr += strlen(ep->ev_name); 124 1.1 cherry efi_copyin("=", addr, 1); 125 1.1 cherry addr++; 126 1.1 cherry if (ep->ev_value != NULL) { 127 1.1 cherry efi_copyin(ep->ev_value, addr, strlen(ep->ev_value)); 128 1.1 cherry addr += strlen(ep->ev_value); 129 1.1 cherry } 130 1.1 cherry efi_copyin("", addr, 1); 131 1.1 cherry addr++; 132 1.1 cherry } 133 1.1 cherry efi_copyin("", addr, 1); 134 1.1 cherry addr++; 135 1.1 cherry return(addr); 136 1.1 cherry } 137 1.1 cherry 138 1.1 cherry /* 139 1.1 cherry * Copy module-related data into the load area, where it can be 140 1.1 cherry * used as a directory for loaded modules. 141 1.1 cherry * 142 1.1 cherry * Module data is presented in a self-describing format. Each datum 143 1.1 cherry * is preceded by a 32-bit identifier and a 32-bit size field. 144 1.1 cherry * 145 1.1 cherry * Currently, the following data are saved: 146 1.1 cherry * 147 1.1 cherry * MOD_NAME (variable) module name (string) 148 1.1 cherry * MOD_TYPE (variable) module type (string) 149 1.1 cherry * MOD_ARGS (variable) module parameters (string) 150 1.1 cherry * MOD_ADDR sizeof(vm_offset_t) module load address 151 1.1 cherry * MOD_SIZE sizeof(size_t) module size 152 1.1 cherry * MOD_METADATA (variable) type-specific metadata 153 1.1 cherry */ 154 1.1 cherry #define COPY32(v, a) { \ 155 1.1 cherry u_int32_t x = (v); \ 156 1.1 cherry efi_copyin(&x, a, sizeof(x)); \ 157 1.1 cherry a += sizeof(x); \ 158 1.1 cherry } 159 1.1 cherry 160 1.1 cherry #define MOD_STR(t, a, s) { \ 161 1.1 cherry COPY32(t, a); \ 162 1.1 cherry COPY32(strlen(s) + 1, a); \ 163 1.1 cherry efi_copyin(s, a, strlen(s) + 1); \ 164 1.1 cherry a += roundup(strlen(s) + 1, sizeof(u_int64_t));\ 165 1.1 cherry } 166 1.1 cherry 167 1.1 cherry #define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s) 168 1.1 cherry #define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s) 169 1.1 cherry #define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s) 170 1.1 cherry 171 1.1 cherry #define MOD_VAR(t, a, s) { \ 172 1.1 cherry COPY32(t, a); \ 173 1.1 cherry COPY32(sizeof(s), a); \ 174 1.1 cherry efi_copyin(&s, a, sizeof(s)); \ 175 1.1 cherry a += roundup(sizeof(s), sizeof(u_int64_t)); \ 176 1.1 cherry } 177 1.1 cherry 178 1.1 cherry #define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s) 179 1.1 cherry #define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s) 180 1.1 cherry 181 1.1 cherry #define MOD_METADATA(a, mm) { \ 182 1.1 cherry COPY32(MODINFO_METADATA | mm->md_type, a); \ 183 1.1 cherry COPY32(mm->md_size, a); \ 184 1.1 cherry efi_copyin(mm->md_data, a, mm->md_size); \ 185 1.1 cherry a += roundup(mm->md_size, sizeof(u_int64_t));\ 186 1.1 cherry } 187 1.1 cherry 188 1.1 cherry #define MOD_END(a) { \ 189 1.1 cherry COPY32(MODINFO_END, a); \ 190 1.1 cherry COPY32(0, a); \ 191 1.1 cherry } 192 1.1 cherry 193 1.1 cherry /* 194 1.3 cherry * Load the information expected by the kernel. 195 1.1 cherry * 196 1.1 cherry * - The kernel environment is copied into kernel space. 197 1.1 cherry * - Module metadata are formatted and placed in kernel space. 198 1.1 cherry */ 199 1.1 cherry int 200 1.1 cherry bi_load(struct bootinfo *bi, struct preloaded_file *fp, UINTN *mapkey, 201 1.1 cherry UINTN pages) 202 1.1 cherry { 203 1.1 cherry char *rootdevname; 204 1.1 cherry struct efi_devdesc *rootdev; 205 1.1 cherry struct preloaded_file *xp; 206 1.1 cherry vaddr_t addr, bootinfo_addr; 207 1.1 cherry vaddr_t ssym, esym; 208 1.1 cherry struct file_metadata *md; 209 1.1 cherry EFI_STATUS status; 210 1.1 cherry UINTN bisz, key; 211 1.1 cherry 212 1.1 cherry /* 213 1.1 cherry * Version 1 bootinfo. 214 1.1 cherry */ 215 1.1 cherry bi->bi_magic = BOOTINFO_MAGIC; 216 1.1 cherry bi->bi_version = 1; 217 1.1 cherry 218 1.1 cherry /* 219 1.1 cherry * Calculate boothowto. 220 1.1 cherry */ 221 1.1 cherry bi->bi_boothowto = bi_getboothowto(fp->f_args); 222 1.1 cherry 223 1.1 cherry /* 224 1.1 cherry * Stash EFI System Table. 225 1.1 cherry */ 226 1.1 cherry bi->bi_systab = (u_int64_t) ST; 227 1.1 cherry 228 1.1 cherry /* 229 1.1 cherry * Allow the environment variable 'rootdev' to override the supplied 230 1.1 cherry * device. This should perhaps go to MI code and/or have $rootdev 231 1.1 cherry * tested/set by MI code before launching the kernel. 232 1.1 cherry */ 233 1.1 cherry rootdevname = getenv("rootdev"); 234 1.1 cherry efi_getdev((void **)(&rootdev), rootdevname, NULL); 235 1.1 cherry if (rootdev == NULL) { /* bad $rootdev/$currdev */ 236 1.1 cherry printf("can't determine root device\n"); 237 1.1 cherry return(EINVAL); 238 1.1 cherry } 239 1.1 cherry 240 1.1 cherry /* Try reading the /etc/fstab file to select the root device */ 241 1.1 cherry getrootmount(efi_fmtdev((void *)rootdev)); 242 1.1 cherry free(rootdev); 243 1.1 cherry 244 1.1 cherry ssym = esym = 0; 245 1.1 cherry 246 1.1 cherry ssym = fp->marks[MARK_SYM]; 247 1.1 cherry esym = fp->marks[MARK_END]; 248 1.1 cherry 249 1.1 cherry if (ssym == 0 || esym == 0) 250 1.1 cherry ssym = esym = 0; /* sanity */ 251 1.1 cherry 252 1.1 cherry bi->bi_symtab = ssym; 253 1.1 cherry bi->bi_esymtab = esym; 254 1.1 cherry 255 1.1 cherry bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp); /* DIG64 HCDP table addr. */ 256 1.1 cherry fpswa_init(&bi->bi_fpswa); /* find FPSWA interface */ 257 1.1 cherry 258 1.1 cherry /* find the last module in the chain */ 259 1.1 cherry addr = 0; 260 1.1 cherry for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 261 1.1 cherry if (addr < (xp->f_addr + xp->f_size)) 262 1.1 cherry addr = xp->f_addr + xp->f_size; 263 1.1 cherry } 264 1.1 cherry 265 1.1 cherry /* pad to a page boundary */ 266 1.1 cherry addr = (addr + PAGE_MASK) & ~PAGE_MASK; 267 1.1 cherry 268 1.1 cherry /* copy our environment */ 269 1.1 cherry bi->bi_envp = addr; 270 1.1 cherry addr = bi_copyenv(addr); 271 1.1 cherry 272 1.1 cherry /* pad to a page boundary */ 273 1.1 cherry addr = (addr + PAGE_MASK) & ~PAGE_MASK; 274 1.1 cherry 275 1.1 cherry /* all done copying stuff in, save end of loaded object space */ 276 1.1 cherry bi->bi_kernend = addr; 277 1.1 cherry 278 1.1 cherry /* 279 1.1 cherry * Read the memory map and stash it after bootinfo. Align the memory map 280 1.1 cherry * on a 16-byte boundary (the bootinfo block is page aligned). 281 1.1 cherry */ 282 1.1 cherry bisz = (sizeof(struct bootinfo) + 0x0f) & ~0x0f; 283 1.1 cherry bi->bi_memmap = ((u_int64_t)bi) + bisz; 284 1.1 cherry bi->bi_memmap_size = EFI_PAGE_SIZE * pages - bisz; 285 1.1 cherry status = BS->GetMemoryMap(&bi->bi_memmap_size, 286 1.1 cherry (EFI_MEMORY_DESCRIPTOR *)bi->bi_memmap, &key, 287 1.1 cherry &bi->bi_memdesc_size, &bi->bi_memdesc_version); 288 1.1 cherry if (EFI_ERROR(status)) { 289 1.1 cherry printf("bi_load: Can't read memory map\n"); 290 1.1 cherry return EINVAL; 291 1.1 cherry } 292 1.1 cherry *mapkey = key; 293 1.1 cherry 294 1.1 cherry return(0); 295 1.1 cherry } 296