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