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