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