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/i386/libi386/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 <machine/bootinfo.h>
35#include <machine/metadata.h>
36#include "bootstrap.h"
37#include "libi386.h"
38#include "btxv86.h"
39
40#ifdef LOADER_GELI_SUPPORT
41#include "geliboot.h"
42#endif
43
44static struct bootinfo  bi;
45
46/*
47 * Copy module-related data into the load area, where it can be
48 * used as a directory for loaded modules.
49 *
50 * Module data is presented in a self-describing format.  Each datum
51 * is preceded by a 32-bit identifier and a 32-bit size field.
52 *
53 * Currently, the following data are saved:
54 *
55 * MOD_NAME	(variable)		module name (string)
56 * MOD_TYPE	(variable)		module type (string)
57 * MOD_ARGS	(variable)		module parameters (string)
58 * MOD_ADDR	sizeof(vm_offset_t)	module load address
59 * MOD_SIZE	sizeof(size_t)		module size
60 * MOD_METADATA	(variable)		type-specific metadata
61 */
62#define COPY32(v, a, c) {			\
63    uint32_t	x = (v);			\
64    if (c)					\
65	i386_copyin(&x, a, sizeof(x));		\
66    a += sizeof(x);				\
67}
68
69#define MOD_STR(t, a, s, c) {			\
70    COPY32(t, a, c);				\
71    COPY32(strlen(s) + 1, a, c);		\
72    if (c)					\
73	i386_copyin(s, a, strlen(s) + 1);	\
74    a += roundup(strlen(s) + 1, sizeof(u_long));\
75}
76
77#define MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
78#define MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
79#define MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
80
81#define MOD_VAR(t, a, s, c) {			\
82    COPY32(t, a, c);				\
83    COPY32(sizeof(s), a, c);			\
84    if (c)					\
85	i386_copyin(&s, a, sizeof(s));		\
86    a += roundup(sizeof(s), sizeof(u_long));	\
87}
88
89#define MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
90#define MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
91
92#define MOD_METADATA(a, mm, c) {		\
93    COPY32(MODINFO_METADATA | mm->md_type, a, c); \
94    COPY32(mm->md_size, a, c);			\
95    if (c)					\
96	i386_copyin(mm->md_data, a, mm->md_size); \
97    a += roundup(mm->md_size, sizeof(u_long));\
98}
99
100#define MOD_END(a, c) {				\
101    COPY32(MODINFO_END, a, c);			\
102    COPY32(0, a, c);				\
103}
104
105static vm_offset_t
106bi_copymodules32(vm_offset_t addr)
107{
108    struct preloaded_file	*fp;
109    struct file_metadata	*md;
110    int				c;
111
112    c = addr != 0;
113    /* start with the first module on the list, should be the kernel */
114    for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
115
116	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
117	MOD_TYPE(addr, fp->f_type, c);
118	if (fp->f_args)
119	    MOD_ARGS(addr, fp->f_args, c);
120	MOD_ADDR(addr, fp->f_addr, c);
121	MOD_SIZE(addr, fp->f_size, c);
122	for (md = fp->f_metadata; md != NULL; md = md->md_next)
123	    if (!(md->md_type & MODINFOMD_NOCOPY))
124		MOD_METADATA(addr, md, c);
125    }
126    MOD_END(addr, c);
127    return(addr);
128}
129
130/*
131 * Load the information expected by an i386 kernel.
132 *
133 * - The 'boothowto' argument is constructed
134 * - The 'bootdev' argument is constructed
135 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
136 * - The kernel environment is copied into kernel space.
137 * - Module metadata are formatted and placed in kernel space.
138 */
139int
140bi_load32(char *args, int *howtop, int *bootdevp, vm_offset_t *bip, vm_offset_t *modulep, vm_offset_t *kernendp)
141{
142    struct preloaded_file	*xp, *kfp;
143    struct i386_devdesc		*rootdev;
144    struct file_metadata	*md;
145    vm_offset_t			addr;
146    vm_offset_t			kernend;
147    vm_offset_t			envp;
148    vm_offset_t			size;
149    vm_offset_t			ssym, esym;
150    char			*rootdevname;
151    int				bootdevnr, i, howto;
152    char			*kernelname;
153    const char			*kernelpath;
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    i386_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(i386_fmtdev((void *)rootdev));
171
172    /* Do legacy rootdev guessing */
173
174    /* XXX - use a default bootdev of 0.  Is this ok??? */
175    bootdevnr = 0;
176
177    switch(rootdev->dd.d_dev->dv_type) {
178    case DEVT_CD:
179	    /* Pass in BIOS device number. */
180	    bi.bi_bios_dev = bc_unit2bios(rootdev->dd.d_unit);
181	    bootdevnr = bc_getdev(rootdev);
182	    break;
183
184    case DEVT_DISK:
185	/* pass in the BIOS device number of the current disk */
186	bi.bi_bios_dev = bd_unit2bios(rootdev->dd.d_unit);
187	bootdevnr = bd_getdev(rootdev);
188	break;
189
190    case DEVT_NET:
191    case DEVT_ZFS:
192	    break;
193
194    default:
195	printf("WARNING - don't know how to boot from device type %d\n",
196	    rootdev->dd.d_dev->dv_type);
197    }
198    if (bootdevnr == -1) {
199	printf("root device %s invalid\n", i386_fmtdev(rootdev));
200	return (EINVAL);
201    }
202    free(rootdev);
203
204    /* find the last module in the chain */
205    addr = 0;
206    for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
207	if (addr < (xp->f_addr + xp->f_size))
208	    addr = xp->f_addr + xp->f_size;
209    }
210    /* pad to a page boundary */
211    addr = roundup(addr, PAGE_SIZE);
212
213    /* copy our environment */
214    envp = addr;
215    addr = bi_copyenv(addr);
216
217    /* pad to a page boundary */
218    addr = roundup(addr, PAGE_SIZE);
219
220    kfp = file_findfile(NULL, "elf kernel");
221    if (kfp == NULL)
222      kfp = file_findfile(NULL, "elf32 kernel");
223    if (kfp == NULL)
224	panic("can't find kernel file");
225    kernend = 0;	/* fill it in later */
226    file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
227    file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
228    file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
229    bios_addsmapdata(kfp);
230#ifdef LOADER_GELI_SUPPORT
231    geli_export_key_metadata(kfp);
232#endif
233
234    /* Figure out the size and location of the metadata */
235    *modulep = addr;
236    size = bi_copymodules32(0);
237    kernend = roundup(addr + size, PAGE_SIZE);
238    *kernendp = kernend;
239
240    /* patch MODINFOMD_KERNEND */
241    md = file_findmetadata(kfp, MODINFOMD_KERNEND);
242    bcopy(&kernend, md->md_data, sizeof kernend);
243
244    /* copy module list and metadata */
245    (void)bi_copymodules32(addr);
246
247    ssym = esym = 0;
248    md = file_findmetadata(kfp, MODINFOMD_SSYM);
249    if (md != NULL)
250	ssym = *((vm_offset_t *)&(md->md_data));
251    md = file_findmetadata(kfp, MODINFOMD_ESYM);
252    if (md != NULL)
253	esym = *((vm_offset_t *)&(md->md_data));
254    if (ssym == 0 || esym == 0)
255	ssym = esym = 0;		/* sanity */
256
257    /* legacy bootinfo structure */
258    kernelname = getenv("kernelname");
259    i386_getdev(NULL, kernelname, &kernelpath);
260    bi.bi_version = BOOTINFO_VERSION;
261    bi.bi_kernelname = 0;		/* XXX char * -> kernel name */
262    bi.bi_nfs_diskless = 0;		/* struct nfs_diskless * */
263    bi.bi_n_bios_used = 0;		/* XXX would have to hook biosdisk driver for these */
264    for (i = 0; i < N_BIOS_GEOM; i++)
265        bi.bi_bios_geom[i] = bd_getbigeom(i);
266    bi.bi_size = sizeof(bi);
267    bi.bi_memsizes_valid = 1;
268    bi.bi_basemem = bios_basemem / 1024;
269    bi.bi_extmem = bios_extmem / 1024;
270    bi.bi_envp = envp;
271    bi.bi_modulep = *modulep;
272    bi.bi_kernend = kernend;
273    bi.bi_kernelname = VTOP(kernelpath);
274    bi.bi_symtab = ssym;       /* XXX this is only the primary kernel symtab */
275    bi.bi_esymtab = esym;
276
277    /* legacy boot arguments */
278    *howtop = howto | RB_BOOTINFO;
279    *bootdevp = bootdevnr;
280    *bip = VTOP(&bi);
281
282    return(0);
283}
284