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