bootinfo64.c revision 72640
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 * $FreeBSD: head/sys/boot/i386/libi386/bootinfo64.c 72640 2001-02-18 10:25:42Z asmodai $ 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 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 59vm_offset_t bi_copymodules(vm_offset_t addr); 60 61int 62bi_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 'm': 92 howto |= RB_MUTE; 93 break; 94 case 'g': 95 howto |= RB_GDB; 96 break; 97 case 'h': 98 howto |= RB_SERIAL; 99 break; 100 case 'r': 101 howto |= RB_DFLTROOT; 102 break; 103 case 's': 104 howto |= RB_SINGLE; 105 break; 106 case 'v': 107 howto |= RB_VERBOSE; 108 break; 109 default: 110 active = 0; 111 break; 112 } 113 cp++; 114 } 115 } 116 /* get equivalents from the environment */ 117 for (i = 0; howto_names[i].ev != NULL; i++) 118 if (getenv(howto_names[i].ev) != NULL) 119 howto |= howto_names[i].mask; 120 if (!strcmp(getenv("console"), "comconsole")) 121 howto |= RB_SERIAL; 122 if (!strcmp(getenv("console"), "nullconsole")) 123 howto |= RB_MUTE; 124 return(howto); 125} 126 127/* 128 * Copy the environment into the load area starting at (addr). 129 * Each variable is formatted as <name>=<value>, with a single nul 130 * separating each variable, and a double nul terminating the environment. 131 */ 132vm_offset_t 133bi_copyenv(vm_offset_t addr) 134{ 135 struct env_var *ep; 136 137 /* traverse the environment */ 138 for (ep = environ; ep != NULL; ep = ep->ev_next) { 139 i386_copyin(ep->ev_name, addr, strlen(ep->ev_name)); 140 addr += strlen(ep->ev_name); 141 i386_copyin("=", addr, 1); 142 addr++; 143 if (ep->ev_value != NULL) { 144 i386_copyin(ep->ev_value, addr, strlen(ep->ev_value)); 145 addr += strlen(ep->ev_value); 146 } 147 i386_copyin("", addr, 1); 148 addr++; 149 } 150 i386_copyin("", addr, 1); 151 addr++; 152 return(addr); 153} 154 155/* 156 * Copy module-related data into the load area, where it can be 157 * used as a directory for loaded modules. 158 * 159 * Module data is presented in a self-describing format. Each datum 160 * is preceded by a 32-bit identifier and a 32-bit size field. 161 * 162 * Currently, the following data are saved: 163 * 164 * MOD_NAME (variable) module name (string) 165 * MOD_TYPE (variable) module type (string) 166 * MOD_ARGS (variable) module parameters (string) 167 * MOD_ADDR sizeof(vm_offset_t) module load address 168 * MOD_SIZE sizeof(size_t) module size 169 * MOD_METADATA (variable) type-specific metadata 170 */ 171#define COPY32(v, a) { \ 172 u_int32_t x = (v); \ 173 i386_copyin(&x, a, sizeof(x)); \ 174 a += sizeof(x); \ 175} 176 177#define MOD_STR(t, a, s) { \ 178 COPY32(t, a); \ 179 COPY32(strlen(s) + 1, a); \ 180 i386_copyin(s, a, strlen(s) + 1); \ 181 a += roundup(strlen(s) + 1, sizeof(u_long));\ 182} 183 184#define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s) 185#define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s) 186#define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s) 187 188#define MOD_VAR(t, a, s) { \ 189 COPY32(t, a); \ 190 COPY32(sizeof(s), a); \ 191 i386_copyin(&s, a, sizeof(s)); \ 192 a += roundup(sizeof(s), sizeof(u_long)); \ 193} 194 195#define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s) 196#define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s) 197 198#define MOD_METADATA(a, mm) { \ 199 COPY32(MODINFO_METADATA | mm->md_type, a); \ 200 COPY32(mm->md_size, a); \ 201 i386_copyin(mm->md_data, a, mm->md_size); \ 202 a += roundup(mm->md_size, sizeof(u_long));\ 203} 204 205#define MOD_END(a) { \ 206 COPY32(MODINFO_END, a); \ 207 COPY32(0, a); \ 208} 209 210vm_offset_t 211bi_copymodules(vm_offset_t addr) 212{ 213 struct preloaded_file *fp; 214 struct file_metadata *md; 215 216 /* start with the first module on the list, should be the kernel */ 217 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) { 218 219 MOD_NAME(addr, fp->f_name); /* this field must come first */ 220 MOD_TYPE(addr, fp->f_type); 221 if (fp->f_args) 222 MOD_ARGS(addr, fp->f_args); 223 MOD_ADDR(addr, fp->f_addr); 224 MOD_SIZE(addr, fp->f_size); 225 for (md = fp->f_metadata; md != NULL; md = md->md_next) 226 if (!(md->md_type & MODINFOMD_NOCOPY)) 227 MOD_METADATA(addr, md); 228 } 229 MOD_END(addr); 230 return(addr); 231} 232 233/* 234 * Load the information expected by an i386 kernel. 235 * 236 * - The 'boothowto' argument is constructed 237 * - The 'bootdev' argument is constructed 238 * - The 'bootinfo' struct is constructed, and copied into the kernel space. 239 * - The kernel environment is copied into kernel space. 240 * - Module metadata are formatted and placed in kernel space. 241 */ 242int 243bi_load(char *args, int *howtop, int *bootdevp, vm_offset_t *bip) 244{ 245 struct preloaded_file *xp; 246 struct i386_devdesc *rootdev; 247 vm_offset_t addr; 248 char *rootdevname; 249 int bootdevnr, i; 250 u_int pad; 251 char *kernelname; 252 const char *kernelpath; 253 254 *howtop = bi_getboothowto(args); 255 256 /* 257 * Allow the environment variable 'rootdev' to override the supplied device 258 * This should perhaps go to MI code and/or have $rootdev tested/set by 259 * MI code before launching the kernel. 260 */ 261 rootdevname = getenv("rootdev"); 262 i386_getdev((void **)(&rootdev), rootdevname, NULL); 263 if (rootdev == NULL) { /* bad $rootdev/$currdev */ 264 printf("can't determine root device\n"); 265 return(EINVAL); 266 } 267 268 /* Try reading the /etc/fstab file to select the root device */ 269 getrootmount(i386_fmtdev((void *)rootdev)); 270 271 /* Do legacy rootdev guessing */ 272 273 /* XXX - use a default bootdev of 0. Is this ok??? */ 274 bootdevnr = 0; 275 276 switch(rootdev->d_type) { 277 case DEVT_DISK: 278 /* pass in the BIOS device number of the current disk */ 279 bi.bi_bios_dev = bd_unit2bios(rootdev->d_kind.biosdisk.unit); 280 bootdevnr = bd_getdev(rootdev); 281 if (bootdevnr != -1) 282 break; 283 printf("root device %s invalid\n", i386_fmtdev(rootdev)); 284 return(EINVAL); 285 286 case DEVT_NET: 287 break; 288 289 default: 290 printf("WARNING - don't know how to boot from device type %d\n", rootdev->d_type); 291 } 292 free(rootdev); 293 *bootdevp = bootdevnr; 294 295 /* legacy bootinfo structure */ 296 bi.bi_version = BOOTINFO_VERSION; 297 bi.bi_kernelname = 0; /* XXX char * -> kernel name */ 298 bi.bi_nfs_diskless = 0; /* struct nfs_diskless * */ 299 bi.bi_n_bios_used = 0; /* XXX would have to hook biosdisk driver for these */ 300 for (i = 0; i < N_BIOS_GEOM; i++) 301 bi.bi_bios_geom[i] = bd_getbigeom(i); 302 bi.bi_size = sizeof(bi); 303 bi.bi_memsizes_valid = 1; 304 bi.bi_basemem = bios_basemem / 1024; 305 bi.bi_extmem = bios_extmem / 1024; 306 307 /* find the last module in the chain */ 308 addr = 0; 309 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 310 if (addr < (xp->f_addr + xp->f_size)) 311 addr = xp->f_addr + xp->f_size; 312 } 313 /* pad to a page boundary */ 314 pad = (u_int)addr & PAGE_MASK; 315 if (pad != 0) { 316 pad = PAGE_SIZE - pad; 317 addr += pad; 318 } 319 320 /* copy our environment */ 321 bi.bi_envp = addr; 322 addr = bi_copyenv(addr); 323 324 /* pad to a page boundary */ 325 pad = (u_int)addr & PAGE_MASK; 326 if (pad != 0) { 327 pad = PAGE_SIZE - pad; 328 addr += pad; 329 } 330 /* copy module list and metadata */ 331 bi.bi_modulep = addr; 332 addr = bi_copymodules(addr); 333 334 /* all done copying stuff in, save end of loaded object space */ 335 bi.bi_kernend = addr; 336 337 *howtop |= RB_BOOTINFO; /* it's there now */ 338 339 /* 340 * Get the kernel name, strip off any device prefix. 341 */ 342 kernelname = getenv("kernelname"); 343 i386_getdev(NULL, kernelname, &kernelpath); 344 bi.bi_kernelname = VTOP(kernelpath); 345 *bip = VTOP(&bi); 346 347 return(0); 348} 349