bootinfo64.c revision 38764
160484Sobrien/*-
277298Sobrien * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
360484Sobrien * All rights reserved.
460484Sobrien *
560484Sobrien * Redistribution and use in source and binary forms, with or without
660484Sobrien * modification, are permitted provided that the following conditions
760484Sobrien * are met:
860484Sobrien * 1. Redistributions of source code must retain the above copyright
960484Sobrien *    notice, this list of conditions and the following disclaimer.
1060484Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1160484Sobrien *    notice, this list of conditions and the following disclaimer in the
1260484Sobrien *    documentation and/or other materials provided with the distribution.
1360484Sobrien *
1460484Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1560484Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1660484Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1760484Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1860484Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1960484Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2060484Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2160484Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2260484Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2360484Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2460484Sobrien * SUCH DAMAGE.
2560484Sobrien *
2660484Sobrien *	$Id: bootinfo.c,v 1.1.1.1 1998/08/21 03:17:41 msmith Exp $
2760484Sobrien */
2860484Sobrien
2960484Sobrien#include <sys/reboot.h>
3060484Sobrien#include <stand.h>
3160484Sobrien#include "bootstrap.h"
3260484Sobrien
3360484Sobrien
3460484Sobrien/*
3560484Sobrien * Return a 'boothowto' value corresponding to the kernel arguments in
3660484Sobrien * (kargs) and any relevant environment variables.
3760484Sobrien */
3860484Sobrienstatic struct
3960484Sobrien{
4060484Sobrien    char	*ev;
4160484Sobrien    int		mask;
4260484Sobrien} howto_names[] = {
4360484Sobrien    {"boot_askname",	RB_ASKNAME},
4460484Sobrien    {"boot_userconfig",	RB_CONFIG},
4560484Sobrien    {"boot_ddb",	RB_KDB},
4677298Sobrien    {"boot_gdb",	RB_GDB},
4760484Sobrien    {"boot_single",	RB_SINGLE},
4860484Sobrien    {"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	    if (!(md->md_type & MODINFOMD_NOCOPY))
195		MOD_METADATA(addr, md);
196    }
197    return(addr);
198}
199