bootinfo.c revision 39178
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: bootinfo.c,v 1.2 1998/09/03 02:10:09 msmith Exp $
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    /* Parse kargs */
61    howto = 0;
62    if (kargs  != NULL) {
63	cp = kargs;
64	active = 0;
65	while (*cp != 0) {
66	    if (!active && (*cp == '-')) {
67		active = 1;
68	    } else if (active)
69		switch (*cp) {
70		case 'a':
71		    howto |= RB_ASKNAME;
72		    break;
73		case 'c':
74		    howto |= RB_CONFIG;
75		    break;
76		case 'd':
77		    howto |= RB_KDB;
78		    break;
79		case 'g':
80		    howto |= RB_GDB;
81		    break;
82		case 'h':
83		    howto |= RB_SERIAL;
84		    break;
85		case 'r':
86		    howto |= RB_DFLTROOT;
87		    break;
88		case 's':
89		    howto |= RB_SINGLE;
90		    break;
91		case 'v':
92		    howto |= RB_VERBOSE;
93		    break;
94		default:
95		    active = 0;
96		    break;
97		}
98	    active = 0;
99	    cp++;
100	}
101    }
102    /* get equivalents from the environment */
103    for (i = 0; howto_names[i].ev != NULL; i++)
104	if (getenv(howto_names[i].ev) != NULL)
105	    howto |= howto_names[i].mask;
106    if (!strcmp(getenv("console"), "comconsole"))
107	howto |= RB_SERIAL;
108    return(howto);
109}
110
111/*
112 * Copy the environment into the load area starting at (addr).
113 * Each variable is formatted as <name>=<value>, with a single nul
114 * separating each variable, and a double nul terminating the environment.
115 */
116vm_offset_t
117bi_copyenv(vm_offset_t addr)
118{
119    struct env_var	*ep;
120
121    /* traverse the environment */
122    for (ep = environ; ep != NULL; ep = ep->ev_next) {
123	vpbcopy(ep->ev_name, addr, strlen(ep->ev_name));
124	addr += strlen(ep->ev_name);
125	vpbcopy("=", addr, 1);
126	addr++;
127	if (ep->ev_value != NULL) {
128	    vpbcopy(ep->ev_value, addr, strlen(ep->ev_value));
129	    addr += strlen(ep->ev_value);
130	}
131	vpbcopy("", addr, 1);
132	addr++;
133    }
134    vpbcopy("", addr, 1);
135    addr++;
136}
137
138/*
139 * Copy module-related data into the load area, where it can be
140 * used as a directory for loaded modules.
141 *
142 * Module data is presented in a self-describing format.  Each datum
143 * is preceeded by a 32-bit identifier and a 32-bit size field.
144 *
145 * Currently, the following data are saved:
146 *
147 * MOD_NAME	(variable)		module name (string)
148 * MOD_TYPE	(variable)		module type (string)
149 * MOD_ADDR	sizeof(vm_offset_t)	module load address
150 * MOD_SIZE	sizeof(size_t)		module size
151 * MOD_METADATA	(variable)		type-specific metadata
152 */
153#define MOD_STR(t, a, s) {				\
154    u_int32_t ident = (t << 16) + strlen(s) + 1;	\
155    vpbcopy(&ident, a, sizeof(ident));			\
156    a += sizeof(ident);					\
157    vpbcopy(s, a, strlen(s) + 1);			\
158    a += strlen(s) + 1;					\
159}
160
161#define MOD_NAME(a, s)	MOD_STR(MODINFO_NAME, a, s)
162#define MOD_TYPE(a, s)	MOD_STR(MODINFO_TYPE, a, s)
163
164#define MOD_VAR(t, a, s) {			\
165    u_int32_t ident = (t << 16) + sizeof(s);	\
166    vpbcopy(&ident, a, sizeof(ident));		\
167    a += sizeof(ident);				\
168    vpbcopy(&s, a, sizeof(s));			\
169    a += sizeof(s);				\
170}
171
172#define MOD_ADDR(a, s)	MOD_VAR(MODINFO_ADDR, a, s)
173#define MOD_SIZE(a, s)	MOD_VAR(MODINFO_SIZE, a, s)
174
175#define MOD_METADATA(a, mm) {							\
176    u_int32_t ident = ((MODINFO_METADATA | mm->md_type) << 16) + mm->md_size;	\
177    vpbcopy(&ident, a, sizeof(ident));						\
178    a += sizeof(ident);								\
179    vpbcopy(mm->md_data, a, mm->md_size);					\
180    a += mm->md_size;								\
181}
182
183#define MOD_END(a) {			\
184    u_int32_t ident = 0;		\
185    vpbcopy(&ident, a, sizeof(ident));	\
186    a += sizeof(ident);			\
187    vpbcopy(&ident, a, sizeof(ident));	\
188    a += sizeof(ident);			\
189}
190
191vm_offset_t
192bi_copymodules(vm_offset_t addr)
193{
194    struct loaded_module	*mp;
195    struct module_metadata	*md;
196
197    /* start with the first module on the list, should be the kernel */
198    for (mp = mod_findmodule(NULL, NULL); mp != NULL; mp = mp->m_next) {
199
200	MOD_NAME(addr, mp->m_name);	/* this field must come first */
201	MOD_TYPE(addr, mp->m_type);
202	MOD_ADDR(addr, mp->m_addr);
203	MOD_SIZE(addr, mp->m_size);
204	for (md = mp->m_metadata; md != NULL; md = md->md_next)
205	    if (!(md->md_type & MODINFOMD_NOCOPY))
206		MOD_METADATA(addr, md);
207    }
208    MOD_END(addr);
209    return(addr);
210}
211