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