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