bootinfo32.c revision 344399
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 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: stable/11/stand/userboot/userboot/bootinfo32.c 344399 2019-02-20 23:55:35Z kevans $"); 29 30#include <stand.h> 31#include <sys/param.h> 32#include <sys/reboot.h> 33#include <sys/linker.h> 34#include <i386/include/bootinfo.h> 35 36#include "bootstrap.h" 37#include "libuserboot.h" 38 39#ifdef LOADER_GELI_SUPPORT 40#include "geliboot.h" 41#endif 42 43static struct bootinfo bi; 44 45/* 46 * Copy module-related data into the load area, where it can be 47 * used as a directory for loaded modules. 48 * 49 * Module data is presented in a self-describing format. Each datum 50 * is preceded by a 32-bit identifier and a 32-bit size field. 51 * 52 * Currently, the following data are saved: 53 * 54 * MOD_NAME (variable) module name (string) 55 * MOD_TYPE (variable) module type (string) 56 * MOD_ARGS (variable) module parameters (string) 57 * MOD_ADDR sizeof(vm_offset_t) module load address 58 * MOD_SIZE sizeof(size_t) module size 59 * MOD_METADATA (variable) type-specific metadata 60 */ 61#define COPY32(v, a, c) { \ 62 uint32_t x = (v); \ 63 if (c) \ 64 CALLBACK(copyin, &x, a, sizeof(x)); \ 65 a += sizeof(x); \ 66} 67 68#define MOD_STR(t, a, s, c) { \ 69 COPY32(t, a, c); \ 70 COPY32(strlen(s) + 1, a, c); \ 71 if (c) \ 72 CALLBACK(copyin, s, a, strlen(s) + 1); \ 73 a += roundup(strlen(s) + 1, sizeof(uint32_t));\ 74} 75 76#define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c) 77#define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c) 78#define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c) 79 80#define MOD_VAR(t, a, s, c) { \ 81 COPY32(t, a, c); \ 82 COPY32(sizeof(s), a, c); \ 83 if (c) \ 84 CALLBACK(copyin, &s, a, sizeof(s)); \ 85 a += roundup(sizeof(s), sizeof(uint32_t)); \ 86} 87 88#define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c) 89#define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c) 90 91#define MOD_METADATA(a, mm, c) { \ 92 COPY32(MODINFO_METADATA | mm->md_type, a, c); \ 93 COPY32(mm->md_size, a, c); \ 94 if (c) \ 95 CALLBACK(copyin, mm->md_data, a, mm->md_size); \ 96 a += roundup(mm->md_size, sizeof(uint32_t));\ 97} 98 99#define MOD_END(a, c) { \ 100 COPY32(MODINFO_END, a, c); \ 101 COPY32(0, a, c); \ 102} 103 104static vm_offset_t 105bi_copymodules32(vm_offset_t addr) 106{ 107 struct preloaded_file *fp; 108 struct file_metadata *md; 109 int c; 110 111 c = addr != 0; 112 /* start with the first module on the list, should be the kernel */ 113 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) { 114 115 MOD_NAME(addr, fp->f_name, c); /* this field must come first */ 116 MOD_TYPE(addr, fp->f_type, c); 117 if (fp->f_args) 118 MOD_ARGS(addr, fp->f_args, c); 119 MOD_ADDR(addr, fp->f_addr, c); 120 MOD_SIZE(addr, fp->f_size, c); 121 for (md = fp->f_metadata; md != NULL; md = md->md_next) 122 if (!(md->md_type & MODINFOMD_NOCOPY)) 123 MOD_METADATA(addr, md, c); 124 } 125 MOD_END(addr, c); 126 return(addr); 127} 128 129/* 130 * Load the information expected by an i386 kernel. 131 * 132 * - The 'boothowto' argument is constructed 133 * - The 'bootdev' argument is constructed 134 * - The 'bootinfo' struct is constructed, and copied into the kernel space. 135 * - The kernel environment is copied into kernel space. 136 * - Module metadata are formatted and placed in kernel space. 137 */ 138int 139bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t *modulep, vm_offset_t *kernendp) 140{ 141 struct preloaded_file *xp, *kfp; 142 struct i386_devdesc *rootdev; 143 struct file_metadata *md; 144 vm_offset_t addr; 145 vm_offset_t kernend; 146 vm_offset_t envp; 147 vm_offset_t size; 148 vm_offset_t ssym, esym; 149 char *rootdevname; 150 int bootdevnr, howto; 151 char *kernelname; 152 const char *kernelpath; 153 uint64_t lowmem, highmem; 154 155 howto = bi_getboothowto(args); 156 157 /* 158 * Allow the environment variable 'rootdev' to override the supplied device 159 * This should perhaps go to MI code and/or have $rootdev tested/set by 160 * MI code before launching the kernel. 161 */ 162 rootdevname = getenv("rootdev"); 163 userboot_getdev((void **)(&rootdev), rootdevname, NULL); 164 if (rootdev == NULL) { /* bad $rootdev/$currdev */ 165 printf("can't determine root device\n"); 166 return(EINVAL); 167 } 168 169 /* Try reading the /etc/fstab file to select the root device */ 170 getrootmount(userboot_fmtdev((void *)rootdev)); 171 172 bootdevnr = 0; 173#if 0 174 if (bootdevnr == -1) { 175 printf("root device %s invalid\n", i386_fmtdev(rootdev)); 176 return (EINVAL); 177 } 178#endif 179 free(rootdev); 180 181 /* find the last module in the chain */ 182 addr = 0; 183 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 184 if (addr < (xp->f_addr + xp->f_size)) 185 addr = xp->f_addr + xp->f_size; 186 } 187 /* pad to a page boundary */ 188 addr = roundup(addr, PAGE_SIZE); 189 190 /* copy our environment */ 191 envp = addr; 192 addr = bi_copyenv(addr); 193 194 /* pad to a page boundary */ 195 addr = roundup(addr, PAGE_SIZE); 196 197 kfp = file_findfile(NULL, "elf kernel"); 198 if (kfp == NULL) 199 kfp = file_findfile(NULL, "elf32 kernel"); 200 if (kfp == NULL) 201 panic("can't find kernel file"); 202 kernend = 0; /* fill it in later */ 203 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto); 204 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp); 205 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend); 206 bios_addsmapdata(kfp); 207#ifdef LOADER_GELI_SUPPORT 208 geli_export_key_metadata(kfp); 209#endif 210 211 /* Figure out the size and location of the metadata */ 212 *modulep = addr; 213 size = bi_copymodules32(0); 214 kernend = roundup(addr + size, PAGE_SIZE); 215 *kernendp = kernend; 216 217 /* patch MODINFOMD_KERNEND */ 218 md = file_findmetadata(kfp, MODINFOMD_KERNEND); 219 bcopy(&kernend, md->md_data, sizeof kernend); 220 221 /* copy module list and metadata */ 222 (void)bi_copymodules32(addr); 223 224 ssym = esym = 0; 225 md = file_findmetadata(kfp, MODINFOMD_SSYM); 226 if (md != NULL) 227 ssym = *((vm_offset_t *)&(md->md_data)); 228 md = file_findmetadata(kfp, MODINFOMD_ESYM); 229 if (md != NULL) 230 esym = *((vm_offset_t *)&(md->md_data)); 231 if (ssym == 0 || esym == 0) 232 ssym = esym = 0; /* sanity */ 233 234 /* legacy bootinfo structure */ 235 kernelname = getenv("kernelname"); 236 userboot_getdev(NULL, kernelname, &kernelpath); 237 bi.bi_version = BOOTINFO_VERSION; 238 bi.bi_kernelname = 0; /* XXX char * -> kernel name */ 239 bi.bi_nfs_diskless = 0; /* struct nfs_diskless * */ 240 bi.bi_n_bios_used = 0; /* XXX would have to hook biosdisk driver for these */ 241#if 0 242 for (i = 0; i < N_BIOS_GEOM; i++) 243 bi.bi_bios_geom[i] = bd_getbigeom(i); 244#endif 245 bi.bi_size = sizeof(bi); 246 CALLBACK(getmem, &lowmem, &highmem); 247 bi.bi_memsizes_valid = 1; 248 bi.bi_basemem = 640; 249 bi.bi_extmem = (lowmem - 0x100000) / 1024; 250 bi.bi_envp = envp; 251 bi.bi_modulep = *modulep; 252 bi.bi_kernend = kernend; 253 bi.bi_symtab = ssym; /* XXX this is only the primary kernel symtab */ 254 bi.bi_esymtab = esym; 255 256 /* 257 * Copy the legacy bootinfo and kernel name to the guest at 0x2000 258 */ 259 bi.bi_kernelname = 0x2000 + sizeof(bi); 260 CALLBACK(copyin, &bi, 0x2000, sizeof(bi)); 261 CALLBACK(copyin, kernelname, 0x2000 + sizeof(bi), strlen(kernelname) + 1); 262 263 /* legacy boot arguments */ 264 *howtop = howto | RB_BOOTINFO; 265 *bootdevp = bootdevnr; 266 *bip = 0x2000; 267 268 return(0); 269} 270