1 /* $NetBSD: bootinfo.c,v 1.4 2012/12/27 20:21:51 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 /* __FBSDID("$FreeBSD: src/sys/boot/ia64/libski/bootinfo.c,v 1.9 2003/09/08 09:11:32 obrien Exp $"); */ 31 32 #include <lib/libsa/stand.h> 33 #include <lib/libsa/loadfile.h> 34 35 #include <sys/param.h> 36 #include <sys/reboot.h> 37 #include <sys/boot_flag.h> 38 #include <sys/exec_elf.h> 39 #include <sys/lock.h> 40 41 #include <machine/vmparam.h> 42 #include <machine/elf_machdep.h> 43 #include <machine/bootinfo.h> 44 45 46 #include "bootstrap.h" 47 #include "libski.h" 48 49 /* 50 * Return a 'boothowto' value corresponding to the kernel arguments in 51 * (kargs) and any relevant environment variables. 52 */ 53 static struct 54 { 55 const char *ev; 56 int mask; 57 } howto_names[] = { 58 {"boot_askname", RB_ASKNAME}, 59 {"boot_single", RB_SINGLE}, 60 {"boot_nosync", RB_NOSYNC}, 61 {"boot_halt", RB_HALT}, 62 {"boot_kdb", RB_KDB}, 63 {"boot_rdonly", RB_RDONLY}, 64 {"boot_dump", RB_DUMP}, 65 {"boot_miniroot", RB_MINIROOT}, 66 {"boot_userconf", RB_USERCONF}, 67 {NULL, 0} 68 }; 69 70 vaddr_t ia64_unwindtab = 0; /* This is set by the libsa/loadfile_elf32.c MD_LOADSEG callback */ 71 vsize_t ia64_unwindtablen = 0; 72 73 74 extern char *ski_fmtdev(void *vdev); 75 extern int ski_init_stubs(struct bootinfo *); 76 77 int 78 bi_getboothowto(char *kargs) 79 { 80 char *cp; 81 int howto; 82 int active; 83 int i; 84 85 /* Parse kargs */ 86 howto = 0; 87 if (kargs != NULL) { 88 cp = kargs; 89 active = 0; 90 while (*cp != 0) { 91 if (!active && (*cp == '-')) { 92 active = 1; 93 } else if (active) 94 95 BOOT_FLAG(*cp, howto); 96 97 cp++; 98 } 99 } 100 /* get equivalents from the environment */ 101 for (i = 0; howto_names[i].ev != NULL; i++) 102 if (getenv(howto_names[i].ev) != NULL) 103 howto |= howto_names[i].mask; 104 return(howto); 105 } 106 107 108 /* 109 * Copy the environment into the load area starting at (addr). 110 * Each variable is formatted as <name>=<value>, with a single nul 111 * separating each variable, and a double nul terminating the environment. 112 */ 113 vaddr_t 114 bi_copyenv(vaddr_t addr) 115 { 116 struct env_var *ep; 117 118 /* traverse the environment */ 119 for (ep = environ; ep != NULL; ep = ep->ev_next) { 120 ski_copyin(ep->ev_name, addr, strlen(ep->ev_name)); 121 addr += strlen(ep->ev_name); 122 ski_copyin("=", addr, 1); 123 addr++; 124 if (ep->ev_value != NULL) { 125 ski_copyin(ep->ev_value, addr, strlen(ep->ev_value)); 126 addr += strlen(ep->ev_value); 127 } 128 ski_copyin("", addr, 1); 129 addr++; 130 } 131 ski_copyin("", addr, 1); 132 addr++; 133 return(addr); 134 } 135 136 /* 137 * Copy module-related data into the load area, where it can be 138 * used as a directory for loaded modules. 139 * 140 * Module data is presented in a self-describing format. Each datum 141 * is preceded by a 32-bit identifier and a 32-bit size field. 142 * 143 * Currently, the following data are saved: 144 * 145 * MOD_NAME (variable) module name (string) 146 * MOD_TYPE (variable) module type (string) 147 * MOD_ARGS (variable) module parameters (string) 148 * MOD_ADDR sizeof(vm_offset_t) module load address 149 * MOD_SIZE sizeof(size_t) module size 150 * MOD_METADATA (variable) type-specific metadata 151 */ 152 #define COPY32(v, a) { \ 153 u_int32_t x = (v); \ 154 ski_copyin(&x, a, sizeof(x)); \ 155 a += sizeof(x); \ 156 } 157 158 #define MOD_STR(t, a, s) { \ 159 COPY32(t, a); \ 160 COPY32(strlen(s) + 1, a); \ 161 ski_copyin(s, a, strlen(s) + 1); \ 162 a += roundup(strlen(s) + 1, sizeof(u_int64_t));\ 163 } 164 165 #define MOD_NAME(a, s) MOD_STR(MODINFO_NAME, a, s) 166 #define MOD_TYPE(a, s) MOD_STR(MODINFO_TYPE, a, s) 167 #define MOD_ARGS(a, s) MOD_STR(MODINFO_ARGS, a, s) 168 169 #define MOD_VAR(t, a, s) { \ 170 COPY32(t, a); \ 171 COPY32(sizeof(s), a); \ 172 ski_copyin(&s, a, sizeof(s)); \ 173 a += roundup(sizeof(s), sizeof(u_int64_t)); \ 174 } 175 176 #define MOD_ADDR(a, s) MOD_VAR(MODINFO_ADDR, a, s) 177 #define MOD_SIZE(a, s) MOD_VAR(MODINFO_SIZE, a, s) 178 179 #define MOD_METADATA(a, mm) { \ 180 COPY32(MODINFO_METADATA | mm->md_type, a); \ 181 COPY32(mm->md_size, a); \ 182 ski_copyin(mm->md_data, a, mm->md_size); \ 183 a += roundup(mm->md_size, sizeof(u_int64_t));\ 184 } 185 186 #define MOD_END(a) { \ 187 COPY32(MODINFO_END, a); \ 188 COPY32(0, a); \ 189 } 190 191 /* 192 * Load the information expected by an ia64 kernel. 193 * 194 * - The kernel environment is copied into kernel space. 195 * - Module metadata are formatted and placed in kernel space. 196 */ 197 int 198 bi_load(struct bootinfo *bi, struct preloaded_file *fp, char *args) 199 { 200 char *rootdevname; 201 struct ski_devdesc *rootdev; 202 struct preloaded_file *xp; 203 vaddr_t addr, bootinfo_addr; 204 char *kernelname; 205 vaddr_t ssym, esym; 206 struct file_metadata *md; 207 208 /* 209 * Version 1 bootinfo. 210 */ 211 bi->bi_magic = BOOTINFO_MAGIC; 212 bi->bi_version = 1; 213 214 /* 215 * Calculate boothowto. 216 */ 217 bi->bi_boothowto = bi_getboothowto(fp->f_args); 218 219 /* 220 * Allow the environment variable 'rootdev' to override the supplied device 221 * This should perhaps go to MI code and/or have $rootdev tested/set by 222 * MI code before launching the kernel. 223 */ 224 rootdevname = getenv("rootdev"); 225 ski_getdev((void **)(&rootdev), rootdevname, NULL); 226 if (rootdev == NULL) { /* bad $rootdev/$currdev */ 227 printf("can't determine root device\n"); 228 return(EINVAL); 229 } 230 231 /* Try reading the /etc/fstab file to select the root device */ 232 getrootmount(ski_fmtdev((void *)rootdev)); 233 free(rootdev); 234 235 ssym = esym = 0; 236 237 ssym = fp->marks[MARK_SYM]; 238 esym = fp->marks[MARK_END]; 239 240 if (ssym == 0 || esym == 0) 241 ssym = esym = 0; /* sanity */ 242 243 bi->bi_symtab = ssym; 244 bi->bi_esymtab = esym; 245 246 /* find the last module in the chain */ 247 addr = 0; 248 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 249 if (addr < (xp->f_addr + xp->f_size)) 250 addr = xp->f_addr + xp->f_size; 251 } 252 253 /* pad to a page boundary */ 254 addr = (addr + PAGE_MASK) & ~PAGE_MASK; 255 256 /* copy our environment */ 257 bi->bi_envp = addr; 258 addr = bi_copyenv(addr); 259 260 /* copy the unwind table pointer */ 261 if((bi->bi_unwindtab = (uint64_t) ia64_unwindtab) == 0) 262 printf("Warning: ia64 unwind sections not found. \n" 263 "See: include/loadfile_machdep.h \n"); 264 265 if((bi->bi_unwindtablen = (uint64_t) ia64_unwindtablen) == 0) 266 printf("Warning: ia64 unwind section length is zero. \n " 267 "See: include/loadfile_machdep.h \n"); 268 269 /* pad to a page boundary */ 270 addr = (addr + PAGE_MASK) & ~PAGE_MASK; 271 272 /* all done copying stuff in, save end of loaded object space */ 273 bi->bi_kernend = addr; 274 275 return (ski_init_stubs(bi)); 276 } 277