metadata.c revision 124139
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/i386/libi386/bootinfo.c,v 1.29 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/sys/boot/sparc64/loader/metadata.c 124139 2004-01-04 23:21:18Z obrien $"); 31 32#include <stand.h> 33#include <sys/param.h> 34#include <sys/reboot.h> 35#include <sys/linker.h> 36 37#include <machine/metadata.h> 38 39#include "bootstrap.h" 40#include "libofw.h" 41 42extern struct tlb_entry *dtlb_store; 43extern struct tlb_entry *itlb_store; 44 45extern int dtlb_slot; 46extern int itlb_slot; 47 48/* 49 * Return a 'boothowto' value corresponding to the kernel arguments in 50 * (kargs) and any relevant environment variables. 51 */ 52static struct 53{ 54 const char *ev; 55 int mask; 56} howto_names[] = { 57 {"boot_askname", RB_ASKNAME}, 58 {"boot_cdrom", RB_CDROM}, 59 {"boot_userconfig", RB_CONFIG}, 60 {"boot_ddb", RB_KDB}, 61 {"boot_gdb", RB_GDB}, 62 {"boot_single", RB_SINGLE}, 63 {"boot_verbose", RB_VERBOSE}, 64 {"boot_multicons", RB_MULTIPLE}, 65 {"boot_serial", RB_SERIAL}, 66 {NULL, 0} 67}; 68 69int 70md_getboothowto(char *kargs) 71{ 72 char buf[32]; 73 phandle_t options; 74 char *cp; 75 int howto; 76 int active; 77 int i; 78 79 /* Parse kargs */ 80 howto = 0; 81 if (kargs != NULL) { 82 cp = kargs; 83 active = 0; 84 while (*cp != 0) { 85 if (!active && (*cp == '-')) { 86 active = 1; 87 } else if (active) 88 switch (*cp) { 89 case 'a': 90 howto |= RB_ASKNAME; 91 break; 92 case 'c': 93 howto |= RB_CONFIG; 94 break; 95 case 'C': 96 howto |= RB_CDROM; 97 break; 98 case 'd': 99 howto |= RB_KDB; 100 break; 101 case 'D': 102 howto |= RB_MULTIPLE; 103 break; 104 case 'm': 105 howto |= RB_MUTE; 106 break; 107 case 'g': 108 howto |= RB_GDB; 109 break; 110 case 'h': 111 howto |= RB_SERIAL; 112 break; 113 case 'r': 114 howto |= RB_DFLTROOT; 115 break; 116 case 's': 117 howto |= RB_SINGLE; 118 break; 119 case 'v': 120 howto |= RB_VERBOSE; 121 break; 122 default: 123 active = 0; 124 break; 125 } 126 cp++; 127 } 128 } 129 /* get equivalents from the environment */ 130 for (i = 0; howto_names[i].ev != NULL; i++) 131 if (getenv(howto_names[i].ev) != NULL) 132 howto |= howto_names[i].mask; 133 options = OF_finddevice("/options"); 134 OF_getprop(options, "output-device", buf, sizeof(buf)); 135 if (strcmp(buf, "ttya") == 0 || strcmp(buf, "ttyb") == 0) 136 howto |= RB_SERIAL; 137 return(howto); 138} 139 140/* 141 * Copy the environment into the load area starting at (addr). 142 * Each variable is formatted as <name>=<value>, with a single nul 143 * separating each variable, and a double nul terminating the environment. 144 */ 145vm_offset_t 146md_copyenv(vm_offset_t addr) 147{ 148 struct env_var *ep; 149 150 /* traverse the environment */ 151 for (ep = environ; ep != NULL; ep = ep->ev_next) { 152 archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name)); 153 addr += strlen(ep->ev_name); 154 archsw.arch_copyin("=", addr, 1); 155 addr++; 156 if (ep->ev_value != NULL) { 157 archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value)); 158 addr += strlen(ep->ev_value); 159 } 160 archsw.arch_copyin("", addr, 1); 161 addr++; 162 } 163 archsw.arch_copyin("", addr, 1); 164 addr++; 165 return(addr); 166} 167 168/* 169 * Copy module-related data into the load area, where it can be 170 * used as a directory for loaded modules. 171 * 172 * Module data is presented in a self-describing format. Each datum 173 * is preceded by a 32-bit identifier and a 32-bit size field. 174 * 175 * Currently, the following data are saved: 176 * 177 * MOD_NAME (variable) module name (string) 178 * MOD_TYPE (variable) module type (string) 179 * MOD_ARGS (variable) module parameters (string) 180 * MOD_ADDR sizeof(vm_offset_t) module load address 181 * MOD_SIZE sizeof(size_t) module size 182 * MOD_METADATA (variable) type-specific metadata 183 */ 184#define COPY32(v, a, c) { \ 185 u_int32_t x = (v); \ 186 if (c) \ 187 archsw.arch_copyin(&x, a, sizeof(x)); \ 188 a += sizeof(x); \ 189} 190 191#define MOD_STR(t, a, s, c) { \ 192 COPY32(t, a, c); \ 193 COPY32(strlen(s) + 1, a, c) \ 194 if (c) \ 195 archsw.arch_copyin(s, a, strlen(s) + 1);\ 196 a += roundup(strlen(s) + 1, sizeof(u_long));\ 197} 198 199#define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c) 200#define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c) 201#define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c) 202 203#define MOD_VAR(t, a, s, c) { \ 204 COPY32(t, a, c); \ 205 COPY32(sizeof(s), a, c); \ 206 if (c) \ 207 archsw.arch_copyin(&s, a, sizeof(s)); \ 208 a += roundup(sizeof(s), sizeof(u_long)); \ 209} 210 211#define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c) 212#define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c) 213 214#define MOD_METADATA(a, mm, c) { \ 215 COPY32(MODINFO_METADATA | mm->md_type, a, c);\ 216 COPY32(mm->md_size, a, c); \ 217 if (c) \ 218 archsw.arch_copyin(mm->md_data, a, mm->md_size);\ 219 a += roundup(mm->md_size, sizeof(u_long)); \ 220} 221 222#define MOD_END(a, c) { \ 223 COPY32(MODINFO_END, a, c); \ 224 COPY32(0, a, c); \ 225} 226 227vm_offset_t 228md_copymodules(vm_offset_t addr) 229{ 230 struct preloaded_file *fp; 231 struct file_metadata *md; 232 int c; 233 234 c = addr != 0; 235 /* start with the first module on the list, should be the kernel */ 236 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) { 237 238 MOD_NAME(addr, fp->f_name, c); /* this field must come first */ 239 MOD_TYPE(addr, fp->f_type, c); 240 if (fp->f_args) 241 MOD_ARGS(addr, fp->f_args, c); 242 MOD_ADDR(addr, fp->f_addr, c); 243 MOD_SIZE(addr, fp->f_size, c); 244 for (md = fp->f_metadata; md != NULL; md = md->md_next) { 245 if (!(md->md_type & MODINFOMD_NOCOPY)) { 246 MOD_METADATA(addr, md, c); 247 } 248 } 249 } 250 MOD_END(addr, c); 251 return(addr); 252} 253 254/* 255 * Load the information expected by a sparc64 kernel. 256 * 257 * - The 'boothowto' argument is constructed 258 * - The 'bootdev' argument is constructed 259 * - The kernel environment is copied into kernel space. 260 * - Module metadata are formatted and placed in kernel space. 261 */ 262int 263md_load(char *args, vm_offset_t *modulep) 264{ 265 struct preloaded_file *kfp; 266 struct preloaded_file *xp; 267 struct file_metadata *md; 268 vm_offset_t kernend; 269 vm_offset_t addr; 270 vm_offset_t envp; 271 vm_offset_t size; 272 char *rootdevname; 273 int howto; 274 275 howto = md_getboothowto(args); 276 277 /* 278 * Allow the environment variable 'rootdev' to override the supplied device 279 * This should perhaps go to MI code and/or have $rootdev tested/set by 280 * MI code before launching the kernel. 281 */ 282 if ((rootdevname = getenv("rootdev")) == NULL) 283 rootdevname = getenv("currdev"); 284 getrootmount(rootdevname); 285 286 /* find the last module in the chain */ 287 addr = 0; 288 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 289 if (addr < (xp->f_addr + xp->f_size)) 290 addr = xp->f_addr + xp->f_size; 291 } 292 /* pad to a page boundary */ 293 addr = roundup(addr, PAGE_SIZE); 294 295 /* copy our environment */ 296 envp = addr; 297 addr = md_copyenv(addr); 298 299 /* pad to a page boundary */ 300 addr = roundup(addr, PAGE_SIZE); 301 302 kernend = 0; 303 kfp = file_findfile(NULL, "elf64 kernel"); 304 if (kfp == NULL) 305 kfp = file_findfile(NULL, "elf kernel"); 306 if (kfp == NULL) 307 panic("can't find kernel file"); 308 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto); 309 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp); 310 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend); 311 file_addmetadata(kfp, MODINFOMD_DTLB_SLOTS, sizeof dtlb_slot, &dtlb_slot); 312 file_addmetadata(kfp, MODINFOMD_ITLB_SLOTS, sizeof itlb_slot, &itlb_slot); 313 file_addmetadata(kfp, MODINFOMD_DTLB, 314 dtlb_slot * sizeof(*dtlb_store), dtlb_store); 315 file_addmetadata(kfp, MODINFOMD_ITLB, 316 itlb_slot * sizeof(*itlb_store), itlb_store); 317 318 *modulep = addr; 319 size = md_copymodules(0); 320 kernend = roundup(addr + size, PAGE_SIZE); 321 322 md = file_findmetadata(kfp, MODINFOMD_KERNEND); 323 bcopy(&kernend, md->md_data, sizeof kernend); 324 325 (void)md_copymodules(addr); 326 327 return(0); 328} 329