bootinfo32.c revision 39902
138465Smsmith/*-
238465Smsmith * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
338465Smsmith * All rights reserved.
438465Smsmith *
538465Smsmith * Redistribution and use in source and binary forms, with or without
638465Smsmith * modification, are permitted provided that the following conditions
738465Smsmith * are met:
838465Smsmith * 1. Redistributions of source code must retain the above copyright
938465Smsmith *    notice, this list of conditions and the following disclaimer.
1038465Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1138465Smsmith *    notice, this list of conditions and the following disclaimer in the
1238465Smsmith *    documentation and/or other materials provided with the distribution.
1338465Smsmith *
1438465Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1538465Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1638465Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1738465Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1838465Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1938465Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2038465Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2138465Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2238465Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2338465Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2438465Smsmith * SUCH DAMAGE.
2538465Smsmith *
2639902Smsmith *	$Id: bootinfo.c,v 1.5 1998/09/28 21:59:21 peter Exp $
2738465Smsmith */
2838465Smsmith
2939902Smsmith#include <stand.h>
3039902Smsmith#include <sys/param.h>
3138465Smsmith#include <sys/reboot.h>
3239902Smsmith#include <machine/bootinfo.h>
3338465Smsmith#include "bootstrap.h"
3439902Smsmith#include "libi386.h"
3539902Smsmith#include "btxv86.h"
3638465Smsmith
3739902Smsmithstatic struct bootinfo	bi;
3838465Smsmith
3938465Smsmith/*
4038465Smsmith * Return a 'boothowto' value corresponding to the kernel arguments in
4138465Smsmith * (kargs) and any relevant environment variables.
4238465Smsmith */
4338465Smsmithstatic struct
4438465Smsmith{
4538465Smsmith    char	*ev;
4638465Smsmith    int		mask;
4738465Smsmith} howto_names[] = {
4838465Smsmith    {"boot_askname",	RB_ASKNAME},
4938465Smsmith    {"boot_userconfig",	RB_CONFIG},
5038465Smsmith    {"boot_ddb",	RB_KDB},
5138465Smsmith    {"boot_gdb",	RB_GDB},
5238465Smsmith    {"boot_single",	RB_SINGLE},
5338465Smsmith    {"boot_verbose",	RB_VERBOSE},
5438465Smsmith    {NULL,	0}
5538465Smsmith};
5638465Smsmith
5738465Smsmithint
5838465Smsmithbi_getboothowto(char *kargs)
5938465Smsmith{
6038465Smsmith    char	*cp;
6138465Smsmith    int		howto;
6238465Smsmith    int		active;
6338465Smsmith    int		i;
6438465Smsmith
6539178Smsmith    /* Parse kargs */
6638465Smsmith    howto = 0;
6738465Smsmith    if (kargs  != NULL) {
6838465Smsmith	cp = kargs;
6938465Smsmith	active = 0;
7038465Smsmith	while (*cp != 0) {
7138465Smsmith	    if (!active && (*cp == '-')) {
7238465Smsmith		active = 1;
7338465Smsmith	    } else if (active)
7438465Smsmith		switch (*cp) {
7538465Smsmith		case 'a':
7638465Smsmith		    howto |= RB_ASKNAME;
7738465Smsmith		    break;
7838465Smsmith		case 'c':
7938465Smsmith		    howto |= RB_CONFIG;
8038465Smsmith		    break;
8138465Smsmith		case 'd':
8238465Smsmith		    howto |= RB_KDB;
8338465Smsmith		    break;
8438465Smsmith		case 'g':
8538465Smsmith		    howto |= RB_GDB;
8638465Smsmith		    break;
8738465Smsmith		case 'h':
8838465Smsmith		    howto |= RB_SERIAL;
8938465Smsmith		    break;
9038465Smsmith		case 'r':
9138465Smsmith		    howto |= RB_DFLTROOT;
9238465Smsmith		    break;
9338465Smsmith		case 's':
9438465Smsmith		    howto |= RB_SINGLE;
9538465Smsmith		    break;
9638465Smsmith		case 'v':
9738465Smsmith		    howto |= RB_VERBOSE;
9838465Smsmith		    break;
9938465Smsmith		default:
10038465Smsmith		    active = 0;
10138465Smsmith		    break;
10238465Smsmith		}
10339178Smsmith	    active = 0;
10439178Smsmith	    cp++;
10538465Smsmith	}
10638465Smsmith    }
10739178Smsmith    /* get equivalents from the environment */
10838465Smsmith    for (i = 0; howto_names[i].ev != NULL; i++)
10938465Smsmith	if (getenv(howto_names[i].ev) != NULL)
11038465Smsmith	    howto |= howto_names[i].mask;
11138465Smsmith    if (!strcmp(getenv("console"), "comconsole"))
11238465Smsmith	howto |= RB_SERIAL;
11338465Smsmith    return(howto);
11438465Smsmith}
11538465Smsmith
11638465Smsmith/*
11738465Smsmith * Copy the environment into the load area starting at (addr).
11838465Smsmith * Each variable is formatted as <name>=<value>, with a single nul
11938465Smsmith * separating each variable, and a double nul terminating the environment.
12038465Smsmith */
12138465Smsmithvm_offset_t
12238465Smsmithbi_copyenv(vm_offset_t addr)
12338465Smsmith{
12438465Smsmith    struct env_var	*ep;
12538465Smsmith
12638465Smsmith    /* traverse the environment */
12738465Smsmith    for (ep = environ; ep != NULL; ep = ep->ev_next) {
12839441Smsmith	i386_copyin(ep->ev_name, addr, strlen(ep->ev_name));
12938465Smsmith	addr += strlen(ep->ev_name);
13039441Smsmith	i386_copyin("=", addr, 1);
13138465Smsmith	addr++;
13238465Smsmith	if (ep->ev_value != NULL) {
13339441Smsmith	    i386_copyin(ep->ev_value, addr, strlen(ep->ev_value));
13438465Smsmith	    addr += strlen(ep->ev_value);
13538465Smsmith	}
13639441Smsmith	i386_copyin("", addr, 1);
13738465Smsmith	addr++;
13838465Smsmith    }
13939441Smsmith    i386_copyin("", addr, 1);
14038465Smsmith    addr++;
14139730Speter    return(addr);
14238465Smsmith}
14338465Smsmith
14438465Smsmith/*
14538465Smsmith * Copy module-related data into the load area, where it can be
14638465Smsmith * used as a directory for loaded modules.
14738465Smsmith *
14838465Smsmith * Module data is presented in a self-describing format.  Each datum
14939178Smsmith * is preceeded by a 32-bit identifier and a 32-bit size field.
15038465Smsmith *
15138465Smsmith * Currently, the following data are saved:
15238465Smsmith *
15338465Smsmith * MOD_NAME	(variable)		module name (string)
15438465Smsmith * MOD_TYPE	(variable)		module type (string)
15538465Smsmith * MOD_ADDR	sizeof(vm_offset_t)	module load address
15638465Smsmith * MOD_SIZE	sizeof(size_t)		module size
15738465Smsmith * MOD_METADATA	(variable)		type-specific metadata
15838465Smsmith */
15938465Smsmith#define MOD_STR(t, a, s) {				\
16038465Smsmith    u_int32_t ident = (t << 16) + strlen(s) + 1;	\
16139441Smsmith    i386_copyin(&ident, a, sizeof(ident));		\
16238465Smsmith    a += sizeof(ident);					\
16339441Smsmith    i386_copyin(s, a, strlen(s) + 1);			\
16438465Smsmith    a += strlen(s) + 1;					\
16538465Smsmith}
16638465Smsmith
16738465Smsmith#define MOD_NAME(a, s)	MOD_STR(MODINFO_NAME, a, s)
16838465Smsmith#define MOD_TYPE(a, s)	MOD_STR(MODINFO_TYPE, a, s)
16938465Smsmith
17038465Smsmith#define MOD_VAR(t, a, s) {			\
17138465Smsmith    u_int32_t ident = (t << 16) + sizeof(s);	\
17239441Smsmith    i386_copyin(&ident, a, sizeof(ident));	\
17338465Smsmith    a += sizeof(ident);				\
17439441Smsmith    i386_copyin(&s, a, sizeof(s));		\
17538465Smsmith    a += sizeof(s);				\
17638465Smsmith}
17738465Smsmith
17838465Smsmith#define MOD_ADDR(a, s)	MOD_VAR(MODINFO_ADDR, a, s)
17938465Smsmith#define MOD_SIZE(a, s)	MOD_VAR(MODINFO_SIZE, a, s)
18038465Smsmith
18138465Smsmith#define MOD_METADATA(a, mm) {							\
18238465Smsmith    u_int32_t ident = ((MODINFO_METADATA | mm->md_type) << 16) + mm->md_size;	\
18339441Smsmith    i386_copyin(&ident, a, sizeof(ident));					\
18438465Smsmith    a += sizeof(ident);								\
18539441Smsmith    i386_copyin(mm->md_data, a, mm->md_size);					\
18638465Smsmith    a += mm->md_size;								\
18738465Smsmith}
18838465Smsmith
18939441Smsmith#define MOD_END(a) {				\
19039441Smsmith    u_int32_t ident = 0;			\
19139441Smsmith    i386_copyin(&ident, a, sizeof(ident));	\
19239441Smsmith    a += sizeof(ident);				\
19339441Smsmith    i386_copyin(&ident, a, sizeof(ident));	\
19439441Smsmith    a += sizeof(ident);				\
19539178Smsmith}
19639178Smsmith
19738465Smsmithvm_offset_t
19838465Smsmithbi_copymodules(vm_offset_t addr)
19938465Smsmith{
20038465Smsmith    struct loaded_module	*mp;
20138465Smsmith    struct module_metadata	*md;
20238465Smsmith
20338465Smsmith    /* start with the first module on the list, should be the kernel */
20438465Smsmith    for (mp = mod_findmodule(NULL, NULL); mp != NULL; mp = mp->m_next) {
20538465Smsmith
20639178Smsmith	MOD_NAME(addr, mp->m_name);	/* this field must come first */
20738465Smsmith	MOD_TYPE(addr, mp->m_type);
20838465Smsmith	MOD_ADDR(addr, mp->m_addr);
20938465Smsmith	MOD_SIZE(addr, mp->m_size);
21038465Smsmith	for (md = mp->m_metadata; md != NULL; md = md->md_next)
21138764Smsmith	    if (!(md->md_type & MODINFOMD_NOCOPY))
21238764Smsmith		MOD_METADATA(addr, md);
21338465Smsmith    }
21439178Smsmith    MOD_END(addr);
21538465Smsmith    return(addr);
21638465Smsmith}
21739902Smsmith
21839902Smsmith/*
21939902Smsmith * Load the information expected by an i386 kernel.
22039902Smsmith *
22139902Smsmith * - The 'boothowto' argument is constructed
22239902Smsmith * - The 'botdev' argument is constructed
22339902Smsmith * - The 'bootinfo' struct is constructed, and copied into the kernel space.
22439902Smsmith * - The kernel environment is copied into kernel space.
22539902Smsmith * - Module metadata are formatted and placed in kernel space.
22639902Smsmith */
22739902Smsmithint
22839902Smsmithbi_load(char *args, int *howtop, int *bootdevp, vm_offset_t *bip)
22939902Smsmith{
23039902Smsmith    struct loaded_module	*xp;
23139902Smsmith    struct i386_devdesc		*rootdev;
23239902Smsmith    vm_offset_t			addr, bootinfo_addr;
23339902Smsmith    char			*rootdevname;
23439902Smsmith    int				bootdevnr;
23539902Smsmith    u_int			pad;
23639902Smsmith    char			*kernelname;
23739902Smsmith
23839902Smsmith    *howtop = bi_getboothowto(args);
23939902Smsmith
24039902Smsmith    /*
24139902Smsmith     * Allow the environment variable 'rootdev' to override the supplied device
24239902Smsmith     * This should perhaps go to MI code and/or have $rootdev tested/set by
24339902Smsmith     * MI code before launching the kernel.
24439902Smsmith     */
24539902Smsmith    rootdevname = getenv("rootdev");
24639902Smsmith    i386_getdev((void **)(&rootdev), rootdevname, NULL);
24739902Smsmith    if (rootdev == NULL) {		/* bad $rootdev/$currdev */
24839902Smsmith	printf("can't determine root device\n");
24939902Smsmith	return(EINVAL);
25039902Smsmith    }
25139902Smsmith
25239902Smsmith    /* Boot from whatever the current device is */
25339902Smsmith    i386_getdev((void **)(&rootdev), NULL, NULL);
25439902Smsmith    switch(rootdev->d_type) {
25539902Smsmith    case DEVT_DISK:
25639902Smsmith	/* pass in the BIOS device number of the current disk */
25739902Smsmith	bi.bi_bios_dev = bd_unit2bios(rootdev->d_kind.biosdisk.unit);
25839902Smsmith	bootdevnr = bd_getdev(rootdev);
25939902Smsmith	break;
26039902Smsmith
26139902Smsmith    default:
26239902Smsmith	printf("aout_exec: WARNING - don't know how to boot from device type %d\n", rootdev->d_type);
26339902Smsmith    }
26439902Smsmith    free(rootdev);
26539902Smsmith    *bootdevp = bootdevnr;
26639902Smsmith
26739902Smsmith    /* legacy bootinfo structure */
26839902Smsmith    bi.bi_version = BOOTINFO_VERSION;
26939902Smsmith    bi.bi_kernelname = 0;		/* XXX char * -> kernel name */
27039902Smsmith    bi.bi_nfs_diskless = 0;		/* struct nfs_diskless * */
27139902Smsmith    bi.bi_n_bios_used = 0;		/* XXX would have to hook biosdisk driver for these */
27239902Smsmith    /* bi.bi_bios_geom[] */
27339902Smsmith    bi.bi_size = sizeof(bi);
27439902Smsmith    bi.bi_memsizes_valid = 1;
27539902Smsmith    bi.bi_vesa = 0;			/* XXX correct value? */
27639902Smsmith    bi.bi_basemem = getbasemem();
27739902Smsmith    bi.bi_extmem = getextmem();
27839902Smsmith
27939902Smsmith    /* find the last module in the chain */
28039902Smsmith    for (xp = mod_findmodule(NULL, NULL); xp->m_next != NULL; xp = xp->m_next)
28139902Smsmith	;
28239902Smsmith    addr = xp->m_addr + xp->m_size;
28339902Smsmith    /* pad to a page boundary */
28439902Smsmith    pad = (u_int)addr & PAGE_MASK;
28539902Smsmith    if (pad != 0) {
28639902Smsmith	pad = PAGE_SIZE - pad;
28739902Smsmith	addr += pad;
28839902Smsmith    }
28939902Smsmith
29039902Smsmith    /* copy our environment */
29139902Smsmith    bi.bi_envp = addr;
29239902Smsmith    addr = bi_copyenv(addr);
29339902Smsmith
29439902Smsmith    /* pad to a page boundary */
29539902Smsmith    pad = (u_int)addr & PAGE_MASK;
29639902Smsmith    if (pad != 0) {
29739902Smsmith	pad = PAGE_SIZE - pad;
29839902Smsmith	addr += pad;
29939902Smsmith    }
30039902Smsmith    /* copy module list and metadata */
30139902Smsmith    bi.bi_modulep = addr;
30239902Smsmith    addr = bi_copymodules(addr);
30339902Smsmith
30439902Smsmith    /* all done copying stuff in, save end of loaded object space */
30539902Smsmith    bi.bi_kernend = addr;
30639902Smsmith
30739902Smsmith    *howtop |= RB_BOOTINFO;		/* it's there now */
30839902Smsmith
30939902Smsmith    kernelname = getenv("kernelname");
31039902Smsmith    bi.bi_kernelname = VTOP(kernelname);
31139902Smsmith    *bip = VTOP(&bi);
31239902Smsmith
31339902Smsmith    return(0);
31439902Smsmith}
31539902Smsmith
316