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