bootinfo.c revision 44572
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 * $Id: bootinfo.c,v 1.16 1999/01/24 00:12:04 msmith Exp $ 27 */ 28 29#include <stand.h> 30#include <sys/param.h> 31#include <sys/reboot.h> 32#include <sys/linker.h> 33#include <machine/bootinfo.h> 34#include "bootstrap.h" 35#include "libi386.h" 36#include "btxv86.h" 37 38static struct bootinfo bi; 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 char *ev; 47 int mask; 48} howto_names[] = { 49 {"boot_askname", RB_ASKNAME}, 50 {"boot_userconfig", RB_CONFIG}, 51 {"boot_ddb", RB_KDB}, 52 {"boot_gdb", RB_GDB}, 53 {"boot_single", RB_SINGLE}, 54 {"boot_verbose", RB_VERBOSE}, 55 {NULL, 0} 56}; 57 58int 59bi_getboothowto(char *kargs) 60{ 61 char *cp; 62 int howto; 63 int active; 64 int i; 65 66 /* Parse kargs */ 67 howto = 0; 68 if (kargs != NULL) { 69 cp = kargs; 70 active = 0; 71 while (*cp != 0) { 72 if (!active && (*cp == '-')) { 73 active = 1; 74 } else if (active) 75 switch (*cp) { 76 case 'a': 77 howto |= RB_ASKNAME; 78 break; 79 case 'c': 80 howto |= RB_CONFIG; 81 break; 82 case 'd': 83 howto |= RB_KDB; 84 break; 85 case 'g': 86 howto |= RB_GDB; 87 break; 88 case 'h': 89 howto |= RB_SERIAL; 90 break; 91 case 'r': 92 howto |= RB_DFLTROOT; 93 break; 94 case 's': 95 howto |= RB_SINGLE; 96 break; 97 case 'v': 98 howto |= RB_VERBOSE; 99 break; 100 default: 101 active = 0; 102 break; 103 } 104 cp++; 105 } 106 } 107 /* get equivalents from the environment */ 108 for (i = 0; howto_names[i].ev != NULL; i++) 109 if (getenv(howto_names[i].ev) != NULL) 110 howto |= howto_names[i].mask; 111 if (!strcmp(getenv("console"), "comconsole")) 112 howto |= RB_SERIAL; 113 return(howto); 114} 115 116/* 117 * Copy the environment into the load area starting at (addr). 118 * Each variable is formatted as <name>=<value>, with a single nul 119 * separating each variable, and a double nul terminating the environment. 120 */ 121vm_offset_t 122bi_copyenv(vm_offset_t addr) 123{ 124 struct env_var *ep; 125 126 /* traverse the environment */ 127 for (ep = environ; ep != NULL; ep = ep->ev_next) { 128 i386_copyin(ep->ev_name, addr, strlen(ep->ev_name)); 129 addr += strlen(ep->ev_name); 130 i386_copyin("=", addr, 1); 131 addr++; 132 if (ep->ev_value != NULL) { 133 i386_copyin(ep->ev_value, addr, strlen(ep->ev_value)); 134 addr += strlen(ep->ev_value); 135 } 136 i386_copyin("", addr, 1); 137 addr++; 138 } 139 i386_copyin("", addr, 1); 140 addr++; 141 return(addr); 142} 143 144/* 145 * Copy module-related data into the load area, where it can be 146 * used as a directory for loaded modules. 147 * 148 * Module data is presented in a self-describing format. Each datum 149 * is preceeded by a 32-bit identifier and a 32-bit size field. 150 * 151 * Currently, the following data are saved: 152 * 153 * MOD_NAME (variable) module name (string) 154 * MOD_TYPE (variable) module type (string) 155 * MOD_ARGS (variable) module parameters (string) 156 * MOD_ADDR sizeof(vm_offset_t) module load address 157 * MOD_SIZE sizeof(size_t) module size 158 * MOD_METADATA (variable) type-specific metadata 159 */ 160#define COPY32(v, a) { \ 161 u_int32_t x = (v); \ 162 i386_copyin(&x, a, sizeof(x)); \ 163 a += sizeof(x); \ 164} 165 166#define MOD_STR(t, a, s) { \ 167 COPY32(t, a); \ 168 COPY32(strlen(s) + 1, a); \ 169 i386_copyin(s, a, strlen(s) + 1); \ 170 a += roundup(strlen(s) + 1, sizeof(u_long));\ 171} 172 173#define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s) 174#define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s) 175#define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s) 176 177#define MOD_VAR(t, a, s) { \ 178 COPY32(t, a); \ 179 COPY32(sizeof(s), a); \ 180 i386_copyin(&s, a, sizeof(s)); \ 181 a += roundup(sizeof(s), sizeof(u_long)); \ 182} 183 184#define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s) 185#define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s) 186 187#define MOD_METADATA(a, mm) { \ 188 COPY32(MODINFO_METADATA | mm->md_type, a); \ 189 COPY32(mm->md_size, a); \ 190 i386_copyin(mm->md_data, a, mm->md_size); \ 191 a += roundup(mm->md_size, sizeof(u_long));\ 192} 193 194#define MOD_END(a) { \ 195 COPY32(MODINFO_END, a); \ 196 COPY32(0, a); \ 197} 198 199vm_offset_t 200bi_copymodules(vm_offset_t addr) 201{ 202 struct loaded_module *mp; 203 struct module_metadata *md; 204 205 /* start with the first module on the list, should be the kernel */ 206 for (mp = mod_findmodule(NULL, NULL); mp != NULL; mp = mp->m_next) { 207 208 MOD_NAME(addr, mp->m_name); /* this field must come first */ 209 MOD_TYPE(addr, mp->m_type); 210 MOD_ARGS(addr, mp->m_args); 211 MOD_ADDR(addr, mp->m_addr); 212 MOD_SIZE(addr, mp->m_size); 213 for (md = mp->m_metadata; md != NULL; md = md->md_next) 214 if (!(md->md_type & MODINFOMD_NOCOPY)) 215 MOD_METADATA(addr, md); 216 } 217 MOD_END(addr); 218 return(addr); 219} 220 221/* 222 * Load the information expected by an i386 kernel. 223 * 224 * - The 'boothowto' argument is constructed 225 * - The 'botdev' argument is constructed 226 * - The 'bootinfo' struct is constructed, and copied into the kernel space. 227 * - The kernel environment is copied into kernel space. 228 * - Module metadata are formatted and placed in kernel space. 229 */ 230int 231bi_load(char *args, int *howtop, int *bootdevp, vm_offset_t *bip) 232{ 233 struct loaded_module *xp; 234 struct i386_devdesc *rootdev; 235 vm_offset_t addr, bootinfo_addr; 236 char *rootdevname; 237 int bootdevnr; 238 u_int pad; 239 char *kernelname; 240 const char *kernelpath; 241 242 *howtop = bi_getboothowto(args); 243 244 /* 245 * Allow the environment variable 'rootdev' to override the supplied device 246 * This should perhaps go to MI code and/or have $rootdev tested/set by 247 * MI code before launching the kernel. 248 */ 249 rootdevname = getenv("rootdev"); 250 i386_getdev((void **)(&rootdev), rootdevname, NULL); 251 if (rootdev == NULL) { /* bad $rootdev/$currdev */ 252 printf("can't determine root device\n"); 253 return(EINVAL); 254 } 255 256 switch(rootdev->d_type) { 257 case DEVT_DISK: 258 /* pass in the BIOS device number of the current disk */ 259 bi.bi_bios_dev = bd_unit2bios(rootdev->d_kind.biosdisk.unit); 260 bootdevnr = bd_getdev(rootdev); 261 if (bootdevnr != -1) 262 break; 263 printf("root device %s invalid\n", i386_fmtdev(rootdev)); 264 return(EINVAL); 265 266 default: 267 printf("aout_exec: WARNING - don't know how to boot from device type %d\n", rootdev->d_type); 268 } 269 free(rootdev); 270 *bootdevp = bootdevnr; 271 272 /* legacy bootinfo structure */ 273 bi.bi_version = BOOTINFO_VERSION; 274 bi.bi_kernelname = 0; /* XXX char * -> kernel name */ 275 bi.bi_nfs_diskless = 0; /* struct nfs_diskless * */ 276 bi.bi_n_bios_used = 0; /* XXX would have to hook biosdisk driver for these */ 277 /* bi.bi_bios_geom[] */ 278 bi.bi_size = sizeof(bi); 279 bi.bi_memsizes_valid = 1; 280 bi.bi_basemem = getbasemem(); 281 bi.bi_extmem = getextmem(); 282 283 /* find the last module in the chain */ 284 addr = 0; 285 for (xp = mod_findmodule(NULL, NULL); xp != NULL; xp = xp->m_next) { 286 if (addr < (xp->m_addr + xp->m_size)) 287 addr = xp->m_addr + xp->m_size; 288 } 289 /* pad to a page boundary */ 290 pad = (u_int)addr & PAGE_MASK; 291 if (pad != 0) { 292 pad = PAGE_SIZE - pad; 293 addr += pad; 294 } 295 296 /* copy our environment */ 297 bi.bi_envp = addr; 298 addr = bi_copyenv(addr); 299 300 /* pad to a page boundary */ 301 pad = (u_int)addr & PAGE_MASK; 302 if (pad != 0) { 303 pad = PAGE_SIZE - pad; 304 addr += pad; 305 } 306 /* copy module list and metadata */ 307 bi.bi_modulep = addr; 308 addr = bi_copymodules(addr); 309 310 /* all done copying stuff in, save end of loaded object space */ 311 bi.bi_kernend = addr; 312 313 *howtop |= RB_BOOTINFO; /* it's there now */ 314 315 /* 316 * Get the kernel name, strip off any device prefix. 317 */ 318 kernelname = getenv("kernelname"); 319 i386_getdev(NULL, kernelname, &kernelpath); 320 bi.bi_kernelname = VTOP(kernelpath); 321 *bip = VTOP(&bi); 322 323 return(0); 324} 325