bootinfo.c revision 38465
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 *	$Id$
27 */
28
29#include <sys/reboot.h>
30#include <stand.h>
31#include "bootstrap.h"
32
33
34/*
35 * Return a 'boothowto' value corresponding to the kernel arguments in
36 * (kargs) and any relevant environment variables.
37 */
38static struct
39{
40    char	*ev;
41    int		mask;
42} howto_names[] = {
43    {"boot_askname",	RB_ASKNAME},
44    {"boot_userconfig",	RB_CONFIG},
45    {"boot_ddb",	RB_KDB},
46    {"boot_gdb",	RB_GDB},
47    {"boot_single",	RB_SINGLE},
48    {"boot_verbose",	RB_VERBOSE},
49    {NULL,	0}
50};
51
52int
53bi_getboothowto(char *kargs)
54{
55    char	*cp;
56    int		howto;
57    int		active;
58    int		i;
59
60    howto = 0;
61    if (kargs  != NULL) {
62	cp = kargs;
63	active = 0;
64	while (*cp != 0) {
65	    if (!active && (*cp == '-')) {
66		active = 1;
67	    } else if (active)
68		switch (*cp) {
69		case 'a':
70		    howto |= RB_ASKNAME;
71		    break;
72		case 'c':
73		    howto |= RB_CONFIG;
74		    break;
75		case 'd':
76		    howto |= RB_KDB;
77		    break;
78		case 'g':
79		    howto |= RB_GDB;
80		    break;
81		case 'h':
82		    howto |= RB_SERIAL;
83		    break;
84		case 'r':
85		    howto |= RB_DFLTROOT;
86		    break;
87		case 's':
88		    howto |= RB_SINGLE;
89		    break;
90		case 'v':
91		    howto |= RB_VERBOSE;
92		    break;
93		default:
94		    active = 0;
95		    break;
96		}
97	}
98	cp++;
99    }
100    for (i = 0; howto_names[i].ev != NULL; i++)
101	if (getenv(howto_names[i].ev) != NULL)
102	    howto |= howto_names[i].mask;
103    if (!strcmp(getenv("console"), "comconsole"))
104	howto |= RB_SERIAL;
105    return(howto);
106}
107
108/*
109 * Copy the environment into the load area starting at (addr).
110 * Each variable is formatted as <name>=<value>, with a single nul
111 * separating each variable, and a double nul terminating the environment.
112 */
113vm_offset_t
114bi_copyenv(vm_offset_t addr)
115{
116    struct env_var	*ep;
117
118    /* traverse the environment */
119    for (ep = environ; ep != NULL; ep = ep->ev_next) {
120	vpbcopy(ep->ev_name, addr, strlen(ep->ev_name));
121	addr += strlen(ep->ev_name);
122	vpbcopy("=", addr, 1);
123	addr++;
124	if (ep->ev_value != NULL) {
125	    vpbcopy(ep->ev_value, addr, strlen(ep->ev_value));
126	    addr += strlen(ep->ev_value);
127	}
128	vpbcopy("", addr, 1);
129	addr++;
130    }
131    vpbcopy("", addr, 1);
132    addr++;
133}
134
135/*
136 * Copy module-related data into the load area, where it can be
137 * used as a directory for loaded modules.
138 *
139 * Module data is presented in a self-describing format.  Each datum
140 * is preceeded by a 16-bit identifier and a 16-bit size field.
141 *
142 * Currently, the following data are saved:
143 *
144 * MOD_NAME	(variable)		module name (string)
145 * MOD_TYPE	(variable)		module type (string)
146 * MOD_ADDR	sizeof(vm_offset_t)	module load address
147 * MOD_SIZE	sizeof(size_t)		module size
148 * MOD_METADATA	(variable)		type-specific metadata
149 */
150#define MOD_STR(t, a, s) {				\
151    u_int32_t ident = (t << 16) + strlen(s) + 1;	\
152    vpbcopy(&ident, a, sizeof(ident));			\
153    a += sizeof(ident);					\
154    vpbcopy(s, a, strlen(s) + 1);			\
155    a += strlen(s) + 1;					\
156}
157
158#define MOD_NAME(a, s)	MOD_STR(MODINFO_NAME, a, s)
159#define MOD_TYPE(a, s)	MOD_STR(MODINFO_TYPE, a, s)
160
161#define MOD_VAR(t, a, s) {			\
162    u_int32_t ident = (t << 16) + sizeof(s);	\
163    vpbcopy(&ident, a, sizeof(ident));		\
164    a += sizeof(ident);				\
165    vpbcopy(&s, a, sizeof(s));			\
166    a += sizeof(s);				\
167}
168
169#define MOD_ADDR(a, s)	MOD_VAR(MODINFO_ADDR, a, s)
170#define MOD_SIZE(a, s)	MOD_VAR(MODINFO_SIZE, a, s)
171
172#define MOD_METADATA(a, mm) {							\
173    u_int32_t ident = ((MODINFO_METADATA | mm->md_type) << 16) + mm->md_size;	\
174    vpbcopy(&ident, a, sizeof(ident));						\
175    a += sizeof(ident);								\
176    vpbcopy(mm->md_data, a, mm->md_size);					\
177    a += mm->md_size;								\
178}
179
180vm_offset_t
181bi_copymodules(vm_offset_t addr)
182{
183    struct loaded_module	*mp;
184    struct module_metadata	*md;
185
186    /* start with the first module on the list, should be the kernel */
187    for (mp = mod_findmodule(NULL, NULL); mp != NULL; mp = mp->m_next) {
188
189	MOD_NAME(addr, mp->m_name);
190	MOD_TYPE(addr, mp->m_type);
191	MOD_ADDR(addr, mp->m_addr);
192	MOD_SIZE(addr, mp->m_size);
193	for (md = mp->m_metadata; md != NULL; md = md->md_next)
194	    MOD_METADATA(addr, md);
195    }
196    return(addr);
197}
198