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