xref: /netbsd/sys/arch/ia64/stand/ia64/ski/bootinfo.c (revision 6550d01e)
1 /*	$NetBSD: bootinfo.c,v 1.3 2009/07/20 04:59:04 kiyohara Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 /* __FBSDID("$FreeBSD: src/sys/boot/ia64/libski/bootinfo.c,v 1.9 2003/09/08 09:11:32 obrien Exp $"); */
31 
32 #include <lib/libsa/stand.h>
33 #include <lib/libsa/loadfile.h>
34 
35 #include <sys/param.h>
36 #include <sys/reboot.h>
37 #include <sys/boot_flag.h>
38 #include <sys/exec_elf.h>
39 #include <sys/lock.h>
40 
41 #include <machine/vmparam.h>
42 #include <machine/elf_machdep.h>
43 #include <machine/bootinfo.h>
44 
45 
46 #include "bootstrap.h"
47 
48 /*
49  * Return a 'boothowto' value corresponding to the kernel arguments in
50  * (kargs) and any relevant environment variables.
51  */
52 static struct
53 {
54     const char	*ev;
55     int		mask;
56 } howto_names[] = {
57     {"boot_askname",	RB_ASKNAME},
58     {"boot_single",	RB_SINGLE},
59     {"boot_nosync",     RB_NOSYNC},
60     {"boot_halt",       RB_HALT},
61     {"boot_kdb",	RB_KDB},
62     {"boot_rdonly",     RB_RDONLY},
63     {"boot_dump",       RB_DUMP},
64     {"boot_miniroot",   RB_MINIROOT},
65     {"boot_userconf",   RB_USERCONF},
66     {NULL,	0}
67 };
68 
69 vaddr_t ia64_unwindtab = 0;		/* This is set by the libsa/loadfile_elf32.c MD_LOADSEG callback */
70 vsize_t ia64_unwindtablen = 0;
71 
72 
73 extern char *ski_fmtdev(void *vdev);
74 extern int ski_init_stubs(struct bootinfo *);
75 
76 int
77 bi_getboothowto(char *kargs)
78 {
79     char	*cp;
80     int		howto;
81     int		active;
82     int		i;
83 
84     /* Parse kargs */
85     howto = 0;
86     if (kargs  != NULL) {
87 	cp = kargs;
88 	active = 0;
89 	while (*cp != 0) {
90 	    if (!active && (*cp == '-')) {
91 		active = 1;
92 	    } else if (active)
93 
94 	      BOOT_FLAG(*cp, howto);
95 
96 	    cp++;
97 	}
98     }
99     /* get equivalents from the environment */
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     return(howto);
104 }
105 
106 
107 /*
108  * Copy the environment into the load area starting at (addr).
109  * Each variable is formatted as <name>=<value>, with a single nul
110  * separating each variable, and a double nul terminating the environment.
111  */
112 vaddr_t
113 bi_copyenv(vaddr_t addr)
114 {
115     struct env_var	*ep;
116 
117     /* traverse the environment */
118     for (ep = environ; ep != NULL; ep = ep->ev_next) {
119 	ski_copyin(ep->ev_name, addr, strlen(ep->ev_name));
120 	addr += strlen(ep->ev_name);
121 	ski_copyin("=", addr, 1);
122 	addr++;
123 	if (ep->ev_value != NULL) {
124 	    ski_copyin(ep->ev_value, addr, strlen(ep->ev_value));
125 	    addr += strlen(ep->ev_value);
126 	}
127 	ski_copyin("", addr, 1);
128 	addr++;
129     }
130     ski_copyin("", addr, 1);
131     addr++;
132     return(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 preceded by a 32-bit identifier and a 32-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_ARGS	(variable)		module parameters (string)
147  * MOD_ADDR	sizeof(vm_offset_t)	module load address
148  * MOD_SIZE	sizeof(size_t)		module size
149  * MOD_METADATA	(variable)		type-specific metadata
150  */
151 #define COPY32(v, a) {				\
152     u_int32_t	x = (v);			\
153     ski_copyin(&x, a, sizeof(x));		\
154     a += sizeof(x);				\
155 }
156 
157 #define MOD_STR(t, a, s) {			\
158     COPY32(t, a);				\
159     COPY32(strlen(s) + 1, a);			\
160     ski_copyin(s, a, strlen(s) + 1);		\
161     a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
162 }
163 
164 #define MOD_NAME(a, s)	MOD_STR(MODINFO_NAME, a, s)
165 #define MOD_TYPE(a, s)	MOD_STR(MODINFO_TYPE, a, s)
166 #define MOD_ARGS(a, s)	MOD_STR(MODINFO_ARGS, a, s)
167 
168 #define MOD_VAR(t, a, s) {			\
169     COPY32(t, a);				\
170     COPY32(sizeof(s), a);			\
171     ski_copyin(&s, a, sizeof(s));		\
172     a += roundup(sizeof(s), sizeof(u_int64_t));	\
173 }
174 
175 #define MOD_ADDR(a, s)	MOD_VAR(MODINFO_ADDR, a, s)
176 #define MOD_SIZE(a, s)	MOD_VAR(MODINFO_SIZE, a, s)
177 
178 #define MOD_METADATA(a, mm) {			\
179     COPY32(MODINFO_METADATA | mm->md_type, a);	\
180     COPY32(mm->md_size, a);			\
181     ski_copyin(mm->md_data, a, mm->md_size);	\
182     a += roundup(mm->md_size, sizeof(u_int64_t));\
183 }
184 
185 #define MOD_END(a) {				\
186     COPY32(MODINFO_END, a);			\
187     COPY32(0, a);				\
188 }
189 
190 /*
191  * Load the information expected by an ia64 kernel.
192  *
193  * - The kernel environment is copied into kernel space.
194  * - Module metadata are formatted and placed in kernel space.
195  */
196 int
197 bi_load(struct bootinfo *bi, struct preloaded_file *fp, char *args)
198 {
199     char			*rootdevname;
200     struct ski_devdesc		*rootdev;
201     struct preloaded_file	*xp;
202     vaddr_t			addr, bootinfo_addr;
203     char			*kernelname;
204     vaddr_t			ssym, esym;
205     struct file_metadata	*md;
206 
207     /*
208      * Version 1 bootinfo.
209      */
210     bi->bi_magic = BOOTINFO_MAGIC;
211     bi->bi_version = 1;
212 
213     /*
214      * Calculate boothowto.
215      */
216     bi->bi_boothowto = bi_getboothowto(fp->f_args);
217 
218     /*
219      * Allow the environment variable 'rootdev' to override the supplied device
220      * This should perhaps go to MI code and/or have $rootdev tested/set by
221      * MI code before launching the kernel.
222      */
223     rootdevname = getenv("rootdev");
224     ski_getdev((void **)(&rootdev), rootdevname, NULL);
225     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
226 	printf("can't determine root device\n");
227 	return(EINVAL);
228     }
229 
230     /* Try reading the /etc/fstab file to select the root device */
231     getrootmount(ski_fmtdev((void *)rootdev));
232     free(rootdev);
233 
234     ssym = esym = 0;
235 
236     ssym = fp->marks[MARK_SYM];
237     esym = fp->marks[MARK_END];
238 
239     if (ssym == 0 || esym == 0)
240 	ssym = esym = 0;		/* sanity */
241 
242     bi->bi_symtab = ssym;
243     bi->bi_esymtab = esym;
244 
245     /* find the last module in the chain */
246     addr = 0;
247     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
248 	if (addr < (xp->f_addr + xp->f_size))
249 	    addr = xp->f_addr + xp->f_size;
250     }
251 
252     /* pad to a page boundary */
253     addr = (addr + PAGE_MASK) & ~PAGE_MASK;
254 
255     /* copy our environment */
256     bi->bi_envp = addr;
257     addr = bi_copyenv(addr);
258 
259     /* copy the unwind table pointer */
260     if((bi->bi_unwindtab = (uint64_t) ia64_unwindtab) == 0)
261 	    printf("Warning: ia64 unwind sections not found. \n"
262 		   "See: include/loadfile_machdep.h \n");
263 
264     if((bi->bi_unwindtablen = (uint64_t) ia64_unwindtablen) == 0)
265 	    printf("Warning: ia64 unwind section length is zero. \n "
266 		   "See: include/loadfile_machdep.h \n");
267 
268     /* pad to a page boundary */
269     addr = (addr + PAGE_MASK) & ~PAGE_MASK;
270 
271     /* all done copying stuff in, save end of loaded object space */
272     bi->bi_kernend = addr;
273 
274     return (ski_init_stubs(bi));
275 }
276