metadata.c revision 85720
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 *	from: FreeBSD: src/sys/boot/i386/libi386/bootinfo.c,v 1.29
27 * $FreeBSD: head/sys/boot/sparc64/loader/metadata.c 85720 2001-10-30 06:37:36Z jake $
28 */
29
30#include <stand.h>
31#include <sys/param.h>
32#include <sys/reboot.h>
33#include <sys/linker.h>
34
35#include "bootstrap.h"
36#include "libofw.h"
37
38/*
39 * Return a 'boothowto' value corresponding to the kernel arguments in
40 * (kargs) and any relevant environment variables.
41 */
42static struct
43{
44    const char	*ev;
45    int		mask;
46} howto_names[] = {
47    {"boot_askname",	RB_ASKNAME},
48    {"boot_cdrom",	RB_CDROM},
49    {"boot_userconfig",	RB_CONFIG},
50    {"boot_ddb",	RB_KDB},
51    {"boot_gdb",	RB_GDB},
52    {"boot_single",	RB_SINGLE},
53    {"boot_verbose",	RB_VERBOSE},
54    {NULL,	0}
55};
56
57int
58md_getboothowto(char *kargs)
59{
60    char	*cp;
61    int		howto;
62    int		active;
63    int		i;
64
65    /* Parse kargs */
66    howto = 0;
67    if (kargs != NULL) {
68	cp = kargs;
69	active = 0;
70	while (*cp != 0) {
71	    if (!active && (*cp == '-')) {
72		active = 1;
73	    } else if (active)
74		switch (*cp) {
75		case 'a':
76		    howto |= RB_ASKNAME;
77		    break;
78		case 'c':
79		    howto |= RB_CONFIG;
80		    break;
81		case 'C':
82		    howto |= RB_CDROM;
83		    break;
84		case 'd':
85		    howto |= RB_KDB;
86		    break;
87		case 'D':
88		    howto |= RB_MULTIPLE;
89		    break;
90		case 'm':
91		    howto |= RB_MUTE;
92		    break;
93		case 'g':
94		    howto |= RB_GDB;
95		    break;
96		case 'h':
97		    howto |= RB_SERIAL;
98		    break;
99		case 'r':
100		    howto |= RB_DFLTROOT;
101		    break;
102		case 's':
103		    howto |= RB_SINGLE;
104		    break;
105		case 'v':
106		    howto |= RB_VERBOSE;
107		    break;
108		default:
109		    active = 0;
110		    break;
111		}
112	    cp++;
113	}
114    }
115    /* get equivalents from the environment */
116    for (i = 0; howto_names[i].ev != NULL; i++)
117	if (getenv(howto_names[i].ev) != NULL)
118	    howto |= howto_names[i].mask;
119    if (!strcmp(getenv("console"), "comconsole"))
120	howto |= RB_SERIAL;
121    if (!strcmp(getenv("console"), "nullconsole"))
122	howto |= RB_MUTE;
123    return(howto);
124}
125
126/*
127 * Copy the environment into the load area starting at (addr).
128 * Each variable is formatted as <name>=<value>, with a single nul
129 * separating each variable, and a double nul terminating the environment.
130 */
131vm_offset_t
132md_copyenv(vm_offset_t addr)
133{
134    struct env_var	*ep;
135
136    /* traverse the environment */
137    for (ep = environ; ep != NULL; ep = ep->ev_next) {
138	archsw.arch_copyin(ep->ev_name, addr, strlen(ep->ev_name));
139	addr += strlen(ep->ev_name);
140	archsw.arch_copyin("=", addr, 1);
141	addr++;
142	if (ep->ev_value != NULL) {
143	    archsw.arch_copyin(ep->ev_value, addr, strlen(ep->ev_value));
144	    addr += strlen(ep->ev_value);
145	}
146	archsw.arch_copyin("", addr, 1);
147	addr++;
148    }
149    archsw.arch_copyin("", addr, 1);
150    addr++;
151    return(addr);
152}
153
154/*
155 * Copy module-related data into the load area, where it can be
156 * used as a directory for loaded modules.
157 *
158 * Module data is presented in a self-describing format.  Each datum
159 * is preceded by a 32-bit identifier and a 32-bit size field.
160 *
161 * Currently, the following data are saved:
162 *
163 * MOD_NAME	(variable)		module name (string)
164 * MOD_TYPE	(variable)		module type (string)
165 * MOD_ARGS	(variable)		module parameters (string)
166 * MOD_ADDR	sizeof(vm_offset_t)	module load address
167 * MOD_SIZE	sizeof(size_t)		module size
168 * MOD_METADATA	(variable)		type-specific metadata
169 */
170#define COPY32(v, a, c) {			\
171    u_int32_t	x = (v);			\
172    if (c)					\
173        archsw.arch_copyin(&x, a, sizeof(x));	\
174    a += sizeof(x);				\
175}
176
177#define MOD_STR(t, a, s, c) {			\
178    COPY32(t, a, c);				\
179    COPY32(strlen(s) + 1, a, c)			\
180    if (c)					\
181        archsw.arch_copyin(s, a, strlen(s) + 1);\
182    a += roundup(strlen(s) + 1, sizeof(u_long));\
183}
184
185#define MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
186#define MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
187#define MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
188
189#define MOD_VAR(t, a, s, c) {			\
190    COPY32(t, a, c);				\
191    COPY32(sizeof(s), a, c);			\
192    if (c)					\
193        archsw.arch_copyin(&s, a, sizeof(s));	\
194    a += roundup(sizeof(s), sizeof(u_long));	\
195}
196
197#define MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
198#define MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
199
200#define MOD_METADATA(a, mm, c) {		\
201    COPY32(MODINFO_METADATA | mm->md_type, a, c);\
202    COPY32(mm->md_size, a, c);			\
203    if (c)					\
204        archsw.arch_copyin(mm->md_data, a, mm->md_size);\
205    a += roundup(mm->md_size, sizeof(u_long));	\
206}
207
208#define MOD_END(a, c) {				\
209    COPY32(MODINFO_END, a, c);			\
210    COPY32(0, a, c);				\
211}
212
213vm_offset_t
214md_copymodules(vm_offset_t addr)
215{
216    struct preloaded_file	*fp;
217    struct file_metadata	*md;
218    int				c;
219
220    c = addr != 0;
221    /* start with the first module on the list, should be the kernel */
222    for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
223
224	MOD_NAME(addr, fp->f_name, c);	/* this field must come first */
225	MOD_TYPE(addr, fp->f_type, c);
226	if (fp->f_args)
227	    MOD_ARGS(addr, fp->f_args, c);
228	MOD_ADDR(addr, fp->f_addr, c);
229	MOD_SIZE(addr, fp->f_size, c);
230	for (md = fp->f_metadata; md != NULL; md = md->md_next) {
231	    if (!(md->md_type & MODINFOMD_NOCOPY)) {
232		MOD_METADATA(addr, md, c);
233	    }
234	}
235    }
236    MOD_END(addr, c);
237    return(addr);
238}
239
240/*
241 * Load the information expected by a sparc64 kernel.
242 *
243 * - The 'boothowto' argument is constructed
244 * - The 'bootdev' argument is constructed
245 * - The kernel environment is copied into kernel space.
246 * - Module metadata are formatted and placed in kernel space.
247 */
248int
249md_load(char *args, vm_offset_t *modulep)
250{
251    struct preloaded_file	*kfp;
252    struct preloaded_file	*xp;
253    struct file_metadata	*md;
254    struct ofw_devdesc		*rootdev;
255    vm_offset_t			kernend;
256    vm_offset_t			addr;
257    vm_offset_t			envp;
258    vm_offset_t			size;
259    char			*rootdevname;
260    int				howto;
261
262    howto = md_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    ofw_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(ofw_fmtdev((void *)rootdev));
278
279    free(rootdev);
280
281    /* find the last module in the chain */
282    addr = 0;
283    for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
284	if (addr < (xp->f_addr + xp->f_size))
285	    addr = xp->f_addr + xp->f_size;
286    }
287    /* pad to a page boundary */
288    addr = roundup(addr, PAGE_SIZE);
289
290    /* copy our environment */
291    envp = addr;
292    addr = md_copyenv(addr);
293
294    /* pad to a page boundary */
295    addr = roundup(addr, PAGE_SIZE);
296
297    kernend = 0;
298    kfp = file_findfile(NULL, "elf kernel");
299    if (kfp == NULL)
300	panic("can't find kernel file");
301    file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
302    file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
303    file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
304
305    *modulep = addr;
306    size = md_copymodules(0);
307    kernend = roundup(addr + size, PAGE_SIZE);
308
309    md = file_findmetadata(kfp, MODINFOMD_KERNEND);
310    bcopy(&kernend, md->md_data, sizeof kernend);
311
312    (void)md_copymodules(addr);
313
314    return(0);
315}
316