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