xref: /netbsd-src/sys/arch/ia64/stand/efi/libefi/bootinfo.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: bootinfo.c,v 1.3 2006/07/02 17:28:11 cherry 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/efi/libefi/bootinfo.c,v 1.10 2004/01/04 23:28:16 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 <efi.h>
47 #include <efilib.h>
48 
49 #include "bootstrap.h"
50 
51 static EFI_GUID hcdp = HCDP_TABLE_GUID;
52 
53 /*
54  * Return a 'boothowto' value corresponding to the kernel arguments in
55  * (kargs) and any relevant environment variables.
56  */
57 static struct
58 {
59     const char	*ev;
60     int		mask;
61 } howto_names[] = {
62     {"boot_askname",	RB_ASKNAME},
63     {"boot_single",	RB_SINGLE},
64     {"boot_nosync",     RB_NOSYNC},
65     {"boot_halt",       RB_HALT},
66     {"boot_kdb",	RB_KDB},
67     {"boot_rdonly",     RB_RDONLY},
68     {"boot_dump",       RB_DUMP},
69     {"boot_miniroot",   RB_MINIROOT},
70     {"boot_userconf",   RB_USERCONF},
71     {NULL,	0}
72 };
73 
74 extern char *efi_fmtdev(void *vdev);
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  * Copy the environment into the load area starting at (addr).
108  * Each variable is formatted as <name>=<value>, with a single nul
109  * separating each variable, and a double nul terminating the environment.
110  */
111 vaddr_t
112 bi_copyenv(vaddr_t addr)
113 {
114     struct env_var	*ep;
115 
116     /* traverse the environment */
117     for (ep = environ; ep != NULL; ep = ep->ev_next) {
118 	efi_copyin(ep->ev_name, addr, strlen(ep->ev_name));
119 	addr += strlen(ep->ev_name);
120 	efi_copyin("=", addr, 1);
121 	addr++;
122 	if (ep->ev_value != NULL) {
123 	    efi_copyin(ep->ev_value, addr, strlen(ep->ev_value));
124 	    addr += strlen(ep->ev_value);
125 	}
126 	efi_copyin("", addr, 1);
127 	addr++;
128     }
129     efi_copyin("", addr, 1);
130     addr++;
131     return(addr);
132 }
133 
134 /*
135  * Copy module-related data into the load area, where it can be
136  * used as a directory for loaded modules.
137  *
138  * Module data is presented in a self-describing format.  Each datum
139  * is preceded by a 32-bit identifier and a 32-bit size field.
140  *
141  * Currently, the following data are saved:
142  *
143  * MOD_NAME	(variable)		module name (string)
144  * MOD_TYPE	(variable)		module type (string)
145  * MOD_ARGS	(variable)		module parameters (string)
146  * MOD_ADDR	sizeof(vm_offset_t)	module load address
147  * MOD_SIZE	sizeof(size_t)		module size
148  * MOD_METADATA	(variable)		type-specific metadata
149  */
150 #define COPY32(v, a) {				\
151     u_int32_t	x = (v);			\
152     efi_copyin(&x, a, sizeof(x));		\
153     a += sizeof(x);				\
154 }
155 
156 #define MOD_STR(t, a, s) {			\
157     COPY32(t, a);				\
158     COPY32(strlen(s) + 1, a);			\
159     efi_copyin(s, a, strlen(s) + 1);		\
160     a += roundup(strlen(s) + 1, sizeof(u_int64_t));\
161 }
162 
163 #define MOD_NAME(a, s)	MOD_STR(MODINFO_NAME, a, s)
164 #define MOD_TYPE(a, s)	MOD_STR(MODINFO_TYPE, a, s)
165 #define MOD_ARGS(a, s)	MOD_STR(MODINFO_ARGS, a, s)
166 
167 #define MOD_VAR(t, a, s) {			\
168     COPY32(t, a);				\
169     COPY32(sizeof(s), a);			\
170     efi_copyin(&s, a, sizeof(s));		\
171     a += roundup(sizeof(s), sizeof(u_int64_t));	\
172 }
173 
174 #define MOD_ADDR(a, s)	MOD_VAR(MODINFO_ADDR, a, s)
175 #define MOD_SIZE(a, s)	MOD_VAR(MODINFO_SIZE, a, s)
176 
177 #define MOD_METADATA(a, mm) {			\
178     COPY32(MODINFO_METADATA | mm->md_type, a);	\
179     COPY32(mm->md_size, a);			\
180     efi_copyin(mm->md_data, a, mm->md_size);	\
181     a += roundup(mm->md_size, sizeof(u_int64_t));\
182 }
183 
184 #define MOD_END(a) {				\
185     COPY32(MODINFO_END, a);			\
186     COPY32(0, a);				\
187 }
188 
189 /*
190  * Load the information expected by the kernel.
191  *
192  * - The kernel environment is copied into kernel space.
193  * - Module metadata are formatted and placed in kernel space.
194  */
195 int
196 bi_load(struct bootinfo *bi, struct preloaded_file *fp, UINTN *mapkey,
197     UINTN pages)
198 {
199     char			*rootdevname;
200     struct efi_devdesc		*rootdev;
201     struct preloaded_file	*xp;
202     vaddr_t			addr, bootinfo_addr;
203     vaddr_t			ssym, esym;
204     struct file_metadata	*md;
205     EFI_STATUS			status;
206     UINTN			bisz, key;
207 
208     /*
209      * Version 1 bootinfo.
210      */
211     bi->bi_magic = BOOTINFO_MAGIC;
212     bi->bi_version = 1;
213 
214     /*
215      * Calculate boothowto.
216      */
217     bi->bi_boothowto = bi_getboothowto(fp->f_args);
218 
219     /*
220      * Stash EFI System Table.
221      */
222     bi->bi_systab = (u_int64_t) ST;
223 
224     /*
225      * Allow the environment variable 'rootdev' to override the supplied
226      * device. This should perhaps go to MI code and/or have $rootdev
227      * tested/set by MI code before launching the kernel.
228      */
229     rootdevname = getenv("rootdev");
230     efi_getdev((void **)(&rootdev), rootdevname, NULL);
231     if (rootdev == NULL) {		/* bad $rootdev/$currdev */
232 	printf("can't determine root device\n");
233 	return(EINVAL);
234     }
235 
236     /* Try reading the /etc/fstab file to select the root device */
237     getrootmount(efi_fmtdev((void *)rootdev));
238     free(rootdev);
239 
240     ssym = esym = 0;
241 
242     ssym = fp->marks[MARK_SYM];
243     esym = fp->marks[MARK_END];
244 
245     if (ssym == 0 || esym == 0)
246 	ssym = esym = 0;		/* sanity */
247 
248     bi->bi_symtab = ssym;
249     bi->bi_esymtab = esym;
250 
251     bi->bi_hcdp = (uint64_t)efi_get_table(&hcdp); /* DIG64 HCDP table addr. */
252     fpswa_init(&bi->bi_fpswa);		/* find FPSWA interface */
253 
254     /* find the last module in the chain */
255     addr = 0;
256     for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
257 	if (addr < (xp->f_addr + xp->f_size))
258 	    addr = xp->f_addr + xp->f_size;
259     }
260 
261     /* pad to a page boundary */
262     addr = (addr + PAGE_MASK) & ~PAGE_MASK;
263 
264     /* copy our environment */
265     bi->bi_envp = addr;
266     addr = bi_copyenv(addr);
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     /*
275      * Read the memory map and stash it after bootinfo. Align the memory map
276      * on a 16-byte boundary (the bootinfo block is page aligned).
277      */
278     bisz = (sizeof(struct bootinfo) + 0x0f) & ~0x0f;
279     bi->bi_memmap = ((u_int64_t)bi) + bisz;
280     bi->bi_memmap_size = EFI_PAGE_SIZE * pages - bisz;
281     status = BS->GetMemoryMap(&bi->bi_memmap_size,
282 		(EFI_MEMORY_DESCRIPTOR *)bi->bi_memmap, &key,
283 		&bi->bi_memdesc_size, &bi->bi_memdesc_version);
284     if (EFI_ERROR(status)) {
285 	printf("bi_load: Can't read memory map\n");
286 	return EINVAL;
287     }
288     *mapkey = key;
289 
290     return(0);
291 }
292