1 /*- 2 * Copyright (c) 1998 Michael Smith <msmith@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 AND CONTRIBUTORS ``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 * from: FreeBSD: src/sys/boot/sparc64/loader/metadata.c,v 1.6 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <stand.h> 33 #include <sys/param.h> 34 #include <sys/linker.h> 35 #include <sys/boot.h> 36 #include <sys/reboot.h> 37 #if defined(LOADER_FDT_SUPPORT) 38 #include <fdt_platform.h> 39 #endif 40 41 #ifdef __arm__ 42 #include <machine/elf.h> 43 #endif 44 #include <machine/metadata.h> 45 46 #include "bootstrap.h" 47 48 #ifdef LOADER_GELI_SUPPORT 49 #include "geliboot.h" 50 #endif 51 52 #if defined(__sparc64__) 53 #include <openfirm.h> 54 55 extern struct tlb_entry *dtlb_store; 56 extern struct tlb_entry *itlb_store; 57 58 extern int dtlb_slot; 59 extern int itlb_slot; 60 61 static int 62 md_bootserial(void) 63 { 64 char buf[64]; 65 ihandle_t inst; 66 phandle_t input; 67 phandle_t node; 68 phandle_t output; 69 70 if ((node = OF_finddevice("/options")) == -1) 71 return(-1); 72 if (OF_getprop(node, "input-device", buf, sizeof(buf)) == -1) 73 return(-1); 74 input = OF_finddevice(buf); 75 if (OF_getprop(node, "output-device", buf, sizeof(buf)) == -1) 76 return(-1); 77 output = OF_finddevice(buf); 78 if (input == -1 || output == -1 || 79 OF_getproplen(input, "keyboard") >= 0) { 80 if ((node = OF_finddevice("/chosen")) == -1) 81 return(-1); 82 if (OF_getprop(node, "stdin", &inst, sizeof(inst)) == -1) 83 return(-1); 84 if ((input = OF_instance_to_package(inst)) == -1) 85 return(-1); 86 if (OF_getprop(node, "stdout", &inst, sizeof(inst)) == -1) 87 return(-1); 88 if ((output = OF_instance_to_package(inst)) == -1) 89 return(-1); 90 } 91 if (input != output) 92 return(-1); 93 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1) 94 return(-1); 95 if (strcmp(buf, "serial") != 0) 96 return(-1); 97 return(0); 98 } 99 #endif 100 101 static int 102 md_getboothowto(char *kargs) 103 { 104 int howto; 105 106 /* Parse kargs */ 107 howto = boot_parse_cmdline(kargs); 108 howto |= boot_env_to_howto(); 109 #if defined(__sparc64__) 110 if (md_bootserial() != -1) 111 howto |= RB_SERIAL; 112 #else 113 if (!strcmp(getenv("console"), "comconsole")) 114 howto |= RB_SERIAL; 115 if (!strcmp(getenv("console"), "nullconsole")) 116 howto |= RB_MUTE; 117 #endif 118 return(howto); 119 } 120 121 /* 122 * Copy the environment into the load area starting at (addr). 123 * Each variable is formatted as <name>=<value>, with a single nul 124 * separating each variable, and a double nul terminating the environment. 125 */ 126 static vm_offset_t 127 md_copyenv(vm_offset_t addr) 128 { 129 struct env_var *ep; 130 131 /* traverse the environment */ 132 for (ep = environ; ep != NULL; ep = ep->ev_next) { 133 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name)); 134 addr += strlen(ep->ev_name); 135 archsw.arch_copyin("=", addr, 1); 136 addr++; 137 if (ep->ev_value != NULL) { 138 archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value)); 139 addr += strlen(ep->ev_value); 140 } 141 archsw.arch_copyin("", addr, 1); 142 addr++; 143 } 144 archsw.arch_copyin("", addr, 1); 145 addr++; 146 return(addr); 147 } 148 149 /* 150 * Copy module-related data into the load area, where it can be 151 * used as a directory for loaded modules. 152 * 153 * Module data is presented in a self-describing format. Each datum 154 * is preceded by a 32-bit identifier and a 32-bit size field. 155 * 156 * Currently, the following data are saved: 157 * 158 * MOD_NAME (variable) module name (string) 159 * MOD_TYPE (variable) module type (string) 160 * MOD_ARGS (variable) module parameters (string) 161 * MOD_ADDR sizeof(vm_offset_t) module load address 162 * MOD_SIZE sizeof(size_t) module size 163 * MOD_METADATA (variable) type-specific metadata 164 */ 165 166 static int align; 167 168 #define COPY32(v, a, c) { \ 169 uint32_t x = (v); \ 170 if (c) \ 171 archsw.arch_copyin(&x, a, sizeof(x)); \ 172 a += sizeof(x); \ 173 } 174 175 #define MOD_STR(t, a, s, c) { \ 176 COPY32(t, a, c); \ 177 COPY32(strlen(s) + 1, a, c) \ 178 if (c) \ 179 archsw.arch_copyin(s, a, strlen(s) + 1);\ 180 a += roundup(strlen(s) + 1, align); \ 181 } 182 183 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c) 184 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c) 185 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c) 186 187 #define MOD_VAR(t, a, s, c) { \ 188 COPY32(t, a, c); \ 189 COPY32(sizeof(s), a, c); \ 190 if (c) \ 191 archsw.arch_copyin(&s, a, sizeof(s)); \ 192 a += roundup(sizeof(s), align); \ 193 } 194 195 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c) 196 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c) 197 198 #define MOD_METADATA(a, mm, c) { \ 199 COPY32(MODINFO_METADATA | mm->md_type, a, c);\ 200 COPY32(mm->md_size, a, c); \ 201 if (c) \ 202 archsw.arch_copyin(mm->md_data, a, mm->md_size);\ 203 a += roundup(mm->md_size, align); \ 204 } 205 206 #define MOD_END(a, c) { \ 207 COPY32(MODINFO_END, a, c); \ 208 COPY32(0, a, c); \ 209 } 210 211 static vm_offset_t 212 md_copymodules(vm_offset_t addr, int kern64) 213 { 214 struct preloaded_file *fp; 215 struct file_metadata *md; 216 uint64_t scratch64; 217 uint32_t scratch32; 218 int c; 219 220 c = addr != 0; 221 /* start with the first module on the list, should be the kernel */ 222 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) { 223 224 MOD_NAME(addr, fp->f_name, c); /* this field must come first */ 225 MOD_TYPE(addr, fp->f_type, c); 226 if (fp->f_args) 227 MOD_ARGS(addr, fp->f_args, c); 228 if (kern64) { 229 scratch64 = fp->f_addr; 230 MOD_ADDR(addr, scratch64, c); 231 scratch64 = fp->f_size; 232 MOD_SIZE(addr, scratch64, c); 233 } else { 234 scratch32 = fp->f_addr; 235 #ifdef __arm__ 236 scratch32 -= __elfN(relocation_offset); 237 #endif 238 MOD_ADDR(addr, scratch32, c); 239 MOD_SIZE(addr, fp->f_size, c); 240 } 241 for (md = fp->f_metadata; md != NULL; md = md->md_next) { 242 if (!(md->md_type & MODINFOMD_NOCOPY)) { 243 MOD_METADATA(addr, md, c); 244 } 245 } 246 } 247 MOD_END(addr, c); 248 return(addr); 249 } 250 251 /* 252 * Load the information expected by a kernel. 253 * 254 * - The 'boothowto' argument is constructed 255 * - The 'bootdev' argument is constructed 256 * - The kernel environment is copied into kernel space. 257 * - Module metadata are formatted and placed in kernel space. 258 */ 259 static int 260 md_load_dual(char *args, vm_offset_t *modulep, vm_offset_t *dtb, int kern64) 261 { 262 struct preloaded_file *kfp; 263 struct preloaded_file *xp; 264 struct file_metadata *md; 265 vm_offset_t kernend; 266 vm_offset_t addr; 267 vm_offset_t envp; 268 #if defined(LOADER_FDT_SUPPORT) 269 vm_offset_t fdtp; 270 #endif 271 vm_offset_t size; 272 uint64_t scratch64; 273 char *rootdevname; 274 int howto; 275 #ifdef __arm__ 276 vm_offset_t vaddr; 277 int i; 278 279 /* 280 * These metadata addreses must be converted for kernel after 281 * relocation. 282 */ 283 uint32_t mdt[] = { 284 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND, 285 MODINFOMD_ENVP, 286 #if defined(LOADER_FDT_SUPPORT) 287 MODINFOMD_DTBP 288 #endif 289 }; 290 #endif 291 292 align = kern64 ? 8 : 4; 293 howto = md_getboothowto(args); 294 295 /* 296 * Allow the environment variable 'rootdev' to override the supplied 297 * device. This should perhaps go to MI code and/or have $rootdev 298 * tested/set by MI code before launching the kernel. 299 */ 300 rootdevname = getenv("rootdev"); 301 if (rootdevname == NULL) 302 rootdevname = getenv("currdev"); 303 /* Try reading the /etc/fstab file to select the root device */ 304 getrootmount(rootdevname); 305 306 /* Find the last module in the chain */ 307 addr = 0; 308 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 309 if (addr < (xp->f_addr + xp->f_size)) 310 addr = xp->f_addr + xp->f_size; 311 } 312 /* Pad to a page boundary */ 313 addr = roundup(addr, PAGE_SIZE); 314 315 /* Copy our environment */ 316 envp = addr; 317 addr = md_copyenv(addr); 318 319 /* Pad to a page boundary */ 320 addr = roundup(addr, PAGE_SIZE); 321 322 #if defined(LOADER_FDT_SUPPORT) 323 /* Copy out FDT */ 324 fdtp = 0; 325 #if defined(__powerpc__) 326 if (getenv("usefdt") != NULL) 327 #endif 328 { 329 size = fdt_copy(addr); 330 fdtp = addr; 331 addr = roundup(addr + size, PAGE_SIZE); 332 } 333 #endif 334 335 kernend = 0; 336 kfp = file_findfile(NULL, kern64 ? "elf64 kernel" : "elf32 kernel"); 337 if (kfp == NULL) 338 kfp = file_findfile(NULL, "elf kernel"); 339 if (kfp == NULL) 340 panic("can't find kernel file"); 341 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto); 342 if (kern64) { 343 scratch64 = envp; 344 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof scratch64, &scratch64); 345 #if defined(LOADER_FDT_SUPPORT) 346 if (fdtp != 0) { 347 scratch64 = fdtp; 348 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof scratch64, &scratch64); 349 } 350 #endif 351 scratch64 = kernend; 352 file_addmetadata(kfp, MODINFOMD_KERNEND, 353 sizeof scratch64, &scratch64); 354 } else { 355 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp); 356 #if defined(LOADER_FDT_SUPPORT) 357 if (fdtp != 0) 358 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof fdtp, &fdtp); 359 #endif 360 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend); 361 } 362 #ifdef LOADER_GELI_SUPPORT 363 geli_export_key_metadata(kfp); 364 #endif 365 #if defined(__sparc64__) 366 file_addmetadata(kfp, MODINFOMD_DTLB_SLOTS, 367 sizeof dtlb_slot, &dtlb_slot); 368 file_addmetadata(kfp, MODINFOMD_ITLB_SLOTS, 369 sizeof itlb_slot, &itlb_slot); 370 file_addmetadata(kfp, MODINFOMD_DTLB, 371 dtlb_slot * sizeof(*dtlb_store), dtlb_store); 372 file_addmetadata(kfp, MODINFOMD_ITLB, 373 itlb_slot * sizeof(*itlb_store), itlb_store); 374 #endif 375 376 *modulep = addr; 377 size = md_copymodules(0, kern64); 378 kernend = roundup(addr + size, PAGE_SIZE); 379 380 md = file_findmetadata(kfp, MODINFOMD_KERNEND); 381 if (kern64) { 382 scratch64 = kernend; 383 bcopy(&scratch64, md->md_data, sizeof scratch64); 384 } else { 385 bcopy(&kernend, md->md_data, sizeof kernend); 386 } 387 388 #ifdef __arm__ 389 /* Convert addresses to the final VA */ 390 *modulep -= __elfN(relocation_offset); 391 392 /* Do relocation fixup on metadata of each module. */ 393 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 394 for (i = 0; i < nitems(mdt); i++) { 395 md = file_findmetadata(xp, mdt[i]); 396 if (md) { 397 bcopy(md->md_data, &vaddr, sizeof vaddr); 398 vaddr -= __elfN(relocation_offset); 399 bcopy(&vaddr, md->md_data, sizeof vaddr); 400 } 401 } 402 } 403 #endif 404 405 (void)md_copymodules(addr, kern64); 406 #if defined(LOADER_FDT_SUPPORT) 407 if (dtb != NULL) 408 *dtb = fdtp; 409 #endif 410 411 return(0); 412 } 413 414 #if !defined(__sparc64__) 415 int 416 md_load(char *args, vm_offset_t *modulep, vm_offset_t *dtb) 417 { 418 return (md_load_dual(args, modulep, dtb, 0)); 419 } 420 #endif 421 422 #if defined(__mips__) || defined(__powerpc__) || defined(__sparc64__) 423 int 424 md_load64(char *args, vm_offset_t *modulep, vm_offset_t *dtb) 425 { 426 return (md_load_dual(args, modulep, dtb, 1)); 427 } 428 #endif 429