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 #include <stand.h>
29 #include <sys/param.h>
30 #include <sys/reboot.h>
31 #include <sys/linker.h>
32 #include <machine/bootinfo.h>
33 #include <machine/metadata.h>
34 #include "bootstrap.h"
35 #include "modinfo.h"
36 #include "libi386.h"
37 #include "btxv86.h"
38
39 #ifdef LOADER_GELI_SUPPORT
40 #include "geliboot.h"
41 #endif
42
43 static struct bootinfo *bi;
44
45 /*
46 * Load the information expected by an i386 kernel.
47 *
48 * - The 'boothowto' argument is constructed
49 * - The 'bootdev' argument is constructed
50 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
51 * - The kernel environment is copied into kernel space.
52 * - Module metadata are formatted and placed in kernel space.
53 */
54 int
bi_load32(char * args,int * howtop,int * bootdevp,vm_offset_t * bip,vm_offset_t * modulep,vm_offset_t * kernendp)55 bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t *modulep, vm_offset_t *kernendp)
56 {
57 struct preloaded_file *xp, *kfp;
58 struct i386_devdesc *rootdev;
59 struct file_metadata *md;
60 vm_offset_t addr;
61 vm_offset_t kernend;
62 vm_offset_t envp;
63 vm_offset_t size;
64 vm_offset_t ssym, esym;
65 char *rootdevname;
66 int bootdevnr, i, howto;
67 char *kernelname;
68 const char *kernelpath;
69
70 howto = bi_getboothowto(args);
71
72 /*
73 * Allow the environment variable 'rootdev' to override the supplied device
74 * This should perhaps go to MI code and/or have $rootdev tested/set by
75 * MI code before launching the kernel.
76 */
77 rootdevname = getenv("rootdev");
78 i386_getdev((void **)(&rootdev), rootdevname, NULL);
79 if (rootdev == NULL) { /* bad $rootdev/$currdev */
80 printf("can't determine root device\n");
81 return(EINVAL);
82 }
83
84 /* Try reading the /etc/fstab file to select the root device */
85 getrootmount(devformat(&rootdev->dd));
86
87 /* Do legacy rootdev guessing */
88
89 /* XXX - use a default bootdev of 0. Is this ok??? */
90 bootdevnr = 0;
91
92 bi = calloc(sizeof(*bi), 1);
93 switch(rootdev->dd.d_dev->dv_type) {
94 case DEVT_CD:
95 case DEVT_DISK:
96 /* pass in the BIOS device number of the current disk */
97 bi->bi_bios_dev = bd_unit2bios(rootdev);
98 bootdevnr = bd_getdev(rootdev);
99 break;
100
101 case DEVT_NET:
102 case DEVT_ZFS:
103 break;
104
105 default:
106 printf("WARNING - don't know how to boot from device type %d\n",
107 rootdev->dd.d_dev->dv_type);
108 }
109 if (bootdevnr == -1) {
110 printf("root device %s invalid\n", devformat(&rootdev->dd));
111 return (EINVAL);
112 }
113 free(rootdev);
114
115 /* find the last module in the chain */
116 addr = 0;
117 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
118 if (addr < (xp->f_addr + xp->f_size))
119 addr = xp->f_addr + xp->f_size;
120 }
121 /* pad to a page boundary */
122 addr = roundup(addr, PAGE_SIZE);
123
124 addr = build_font_module(addr);
125
126 /* copy our environment */
127 envp = addr;
128 addr = md_copyenv(addr);
129
130 /* pad to a page boundary */
131 addr = roundup(addr, PAGE_SIZE);
132
133 kfp = file_findfile(NULL, "elf kernel");
134 if (kfp == NULL)
135 kfp = file_findfile(NULL, "elf32 kernel");
136 if (kfp == NULL)
137 panic("can't find kernel file");
138 kernend = 0; /* fill it in later */
139 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
140 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
141 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
142 bios_addsmapdata(kfp);
143 #ifdef LOADER_GELI_SUPPORT
144 geli_export_key_metadata(kfp);
145 #endif
146 bi_load_vbe_data(kfp);
147
148 /* Figure out the size and location of the metadata */
149 *modulep = addr;
150 size = md_copymodules(0, false);
151 kernend = roundup(addr + size, PAGE_SIZE);
152 *kernendp = kernend;
153
154 /* patch MODINFOMD_KERNEND */
155 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
156 bcopy(&kernend, md->md_data, sizeof kernend);
157
158 /* copy module list and metadata */
159 (void)md_copymodules(addr, false);
160
161 ssym = esym = 0;
162 md = file_findmetadata(kfp, MODINFOMD_SSYM);
163 if (md != NULL)
164 ssym = *((vm_offset_t *)&(md->md_data));
165 md = file_findmetadata(kfp, MODINFOMD_ESYM);
166 if (md != NULL)
167 esym = *((vm_offset_t *)&(md->md_data));
168 if (ssym == 0 || esym == 0)
169 ssym = esym = 0; /* sanity */
170
171 /* legacy bootinfo structure */
172 kernelname = getenv("kernelname");
173 i386_getdev(NULL, kernelname, &kernelpath);
174 bi->bi_version = BOOTINFO_VERSION;
175 bi->bi_size = sizeof(*bi);
176 bi->bi_memsizes_valid = 1;
177 bi->bi_basemem = bios_basemem / 1024;
178 bi->bi_extmem = bios_extmem / 1024;
179 bi->bi_envp = envp;
180 bi->bi_modulep = *modulep;
181 bi->bi_kernend = kernend;
182 bi->bi_kernelname = VTOP(kernelpath);
183 bi->bi_symtab = ssym; /* XXX this is only the primary kernel symtab */
184 bi->bi_esymtab = esym;
185
186 /* legacy boot arguments */
187 *howtop = howto | RB_BOOTINFO;
188 *bootdevp = bootdevnr;
189 *bip = VTOP(bi);
190
191 return(0);
192 }
193