xref: /illumos-gate/usr/src/boot/efi/loader/bootinfo.c (revision 22028508)
1199767f8SToomas Soome /*-
2199767f8SToomas Soome  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3199767f8SToomas Soome  * Copyright (c) 2004, 2006 Marcel Moolenaar
4199767f8SToomas Soome  * Copyright (c) 2014 The FreeBSD Foundation
5199767f8SToomas Soome  * All rights reserved.
6199767f8SToomas Soome  *
7199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
8199767f8SToomas Soome  * modification, are permitted provided that the following conditions
9199767f8SToomas Soome  * are met:
10199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
11199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
12199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
13199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
14199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
15199767f8SToomas Soome  *
16199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26199767f8SToomas Soome  * SUCH DAMAGE.
27199767f8SToomas Soome  */
28199767f8SToomas Soome 
29199767f8SToomas Soome #include <sys/cdefs.h>
30199767f8SToomas Soome 
31199767f8SToomas Soome #include <stand.h>
32199767f8SToomas Soome #include <string.h>
33199767f8SToomas Soome #include <sys/param.h>
34199767f8SToomas Soome #include <sys/reboot.h>
35199767f8SToomas Soome #include <sys/linker.h>
36199767f8SToomas Soome #include <sys/boot.h>
37199767f8SToomas Soome #include <machine/cpufunc.h>
38199767f8SToomas Soome #include <machine/elf.h>
39199767f8SToomas Soome #include <machine/metadata.h>
40199767f8SToomas Soome #include <machine/psl.h>
41199767f8SToomas Soome 
42199767f8SToomas Soome #include <efi.h>
43199767f8SToomas Soome #include <efilib.h>
44199767f8SToomas Soome 
45199767f8SToomas Soome #include "bootstrap.h"
46199767f8SToomas Soome #include "loader_efi.h"
47199767f8SToomas Soome 
48199767f8SToomas Soome #if defined(__amd64__)
49199767f8SToomas Soome #include <machine/specialreg.h>
50199767f8SToomas Soome #include "framebuffer.h"
51199767f8SToomas Soome #endif
52199767f8SToomas Soome 
53199767f8SToomas Soome #if defined(LOADER_FDT_SUPPORT)
54199767f8SToomas Soome #include <fdt_platform.h>
55199767f8SToomas Soome #endif
56199767f8SToomas Soome 
57199767f8SToomas Soome int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
58199767f8SToomas Soome 
59199767f8SToomas Soome extern EFI_SYSTEM_TABLE	*ST;
60199767f8SToomas Soome 
61199767f8SToomas Soome static const char howto_switches[] = "aCdrgDmphsv";
62199767f8SToomas Soome static int howto_masks[] = {
63199767f8SToomas Soome 	RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
64199767f8SToomas Soome 	RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
65199767f8SToomas Soome };
66199767f8SToomas Soome 
67199767f8SToomas Soome static int
bi_getboothowto(char * kargs)68199767f8SToomas Soome bi_getboothowto(char *kargs)
69199767f8SToomas Soome {
70199767f8SToomas Soome 	const char *sw;
71199767f8SToomas Soome 	char *opts;
72199767f8SToomas Soome 	char *console;
73199767f8SToomas Soome 	int howto, i;
74199767f8SToomas Soome 
75199767f8SToomas Soome 	howto = 0;
76199767f8SToomas Soome 
77199767f8SToomas Soome 	/* Get the boot options from the environment first. */
78199767f8SToomas Soome 	for (i = 0; howto_names[i].ev != NULL; i++) {
79199767f8SToomas Soome 		if (getenv(howto_names[i].ev) != NULL)
80199767f8SToomas Soome 			howto |= howto_names[i].mask;
81199767f8SToomas Soome 	}
82199767f8SToomas Soome 
83199767f8SToomas Soome 	console = getenv("console");
84199767f8SToomas Soome 	if (console != NULL) {
85199767f8SToomas Soome 		if (strcmp(console, "comconsole") == 0)
86199767f8SToomas Soome 			howto |= RB_SERIAL;
87199767f8SToomas Soome 		if (strcmp(console, "nullconsole") == 0)
88199767f8SToomas Soome 			howto |= RB_MUTE;
89199767f8SToomas Soome 	}
90199767f8SToomas Soome 
91199767f8SToomas Soome 	/* Parse kargs */
92199767f8SToomas Soome 	if (kargs == NULL)
93199767f8SToomas Soome 		return (howto);
94199767f8SToomas Soome 
95199767f8SToomas Soome 	opts = strchr(kargs, '-');
96199767f8SToomas Soome 	while (opts != NULL) {
97199767f8SToomas Soome 		while (*(++opts) != '\0') {
98199767f8SToomas Soome 			sw = strchr(howto_switches, *opts);
99199767f8SToomas Soome 			if (sw == NULL)
100199767f8SToomas Soome 				break;
101199767f8SToomas Soome 			howto |= howto_masks[sw - howto_switches];
102199767f8SToomas Soome 		}
103199767f8SToomas Soome 		opts = strchr(opts, '-');
104199767f8SToomas Soome 	}
105199767f8SToomas Soome 
106199767f8SToomas Soome 	return (howto);
107199767f8SToomas Soome }
108199767f8SToomas Soome 
109199767f8SToomas Soome /*
110199767f8SToomas Soome  * Copy the environment into the load area starting at (addr).
111199767f8SToomas Soome  * Each variable is formatted as <name>=<value>, with a single nul
112199767f8SToomas Soome  * separating each variable, and a double nul terminating the environment.
113199767f8SToomas Soome  */
11476608ff7SToomas Soome vm_offset_t
bi_copyenv(vm_offset_t start)115199767f8SToomas Soome bi_copyenv(vm_offset_t start)
116199767f8SToomas Soome {
117199767f8SToomas Soome 	struct env_var *ep;
118199767f8SToomas Soome 	vm_offset_t addr, last;
119199767f8SToomas Soome 	size_t len;
120199767f8SToomas Soome 
121199767f8SToomas Soome 	addr = last = start;
122199767f8SToomas Soome 
123199767f8SToomas Soome 	/* Traverse the environment. */
124199767f8SToomas Soome 	for (ep = environ; ep != NULL; ep = ep->ev_next) {
125199767f8SToomas Soome 		len = strlen(ep->ev_name);
126199767f8SToomas Soome 		if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
127199767f8SToomas Soome 			break;
128199767f8SToomas Soome 		addr += len;
129199767f8SToomas Soome 		if (archsw.arch_copyin("=", addr, 1) != 1)
130199767f8SToomas Soome 			break;
131199767f8SToomas Soome 		addr++;
132199767f8SToomas Soome 		if (ep->ev_value != NULL) {
133199767f8SToomas Soome 			len = strlen(ep->ev_value);
134199767f8SToomas Soome 			if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
135199767f8SToomas Soome 				break;
136199767f8SToomas Soome 			addr += len;
137199767f8SToomas Soome 		}
138199767f8SToomas Soome 		if (archsw.arch_copyin("", addr, 1) != 1)
139199767f8SToomas Soome 			break;
140199767f8SToomas Soome 		last = ++addr;
141199767f8SToomas Soome 	}
142199767f8SToomas Soome 
143199767f8SToomas Soome 	if (archsw.arch_copyin("", last++, 1) != 1)
144199767f8SToomas Soome 		last = start;
145199767f8SToomas Soome 	return(last);
146199767f8SToomas Soome }
147199767f8SToomas Soome 
148199767f8SToomas Soome /*
149199767f8SToomas Soome  * Copy module-related data into the load area, where it can be
150199767f8SToomas Soome  * used as a directory for loaded modules.
151199767f8SToomas Soome  *
152199767f8SToomas Soome  * Module data is presented in a self-describing format.  Each datum
153199767f8SToomas Soome  * is preceded by a 32-bit identifier and a 32-bit size field.
154199767f8SToomas Soome  *
155199767f8SToomas Soome  * Currently, the following data are saved:
156199767f8SToomas Soome  *
157199767f8SToomas Soome  * MOD_NAME	(variable)		module name (string)
158199767f8SToomas Soome  * MOD_TYPE	(variable)		module type (string)
159199767f8SToomas Soome  * MOD_ARGS	(variable)		module parameters (string)
160199767f8SToomas Soome  * MOD_ADDR	sizeof(vm_offset_t)	module load address
161199767f8SToomas Soome  * MOD_SIZE	sizeof(size_t)		module size
162199767f8SToomas Soome  * MOD_METADATA	(variable)		type-specific metadata
163199767f8SToomas Soome  */
164199767f8SToomas Soome #define	COPY32(v, a, c) {					\
165199767f8SToomas Soome 	uint32_t x = (v);					\
166199767f8SToomas Soome 	if (c)							\
167199767f8SToomas Soome 		archsw.arch_copyin(&x, a, sizeof(x));		\
168199767f8SToomas Soome 	a += sizeof(x);						\
169199767f8SToomas Soome }
170199767f8SToomas Soome 
171199767f8SToomas Soome #define	MOD_STR(t, a, s, c) {					\
172199767f8SToomas Soome 	COPY32(t, a, c);					\
173199767f8SToomas Soome 	COPY32(strlen(s) + 1, a, c);				\
174199767f8SToomas Soome 	if (c)							\
175199767f8SToomas Soome 		archsw.arch_copyin(s, a, strlen(s) + 1);	\
176199767f8SToomas Soome 	a += roundup(strlen(s) + 1, sizeof(u_long));		\
177199767f8SToomas Soome }
178199767f8SToomas Soome 
179199767f8SToomas Soome #define	MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
180199767f8SToomas Soome #define	MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
181199767f8SToomas Soome #define	MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
182199767f8SToomas Soome 
183199767f8SToomas Soome #define	MOD_VAR(t, a, s, c) {					\
184199767f8SToomas Soome 	COPY32(t, a, c);					\
185199767f8SToomas Soome 	COPY32(sizeof(s), a, c);				\
186199767f8SToomas Soome 	if (c)							\
187199767f8SToomas Soome 		archsw.arch_copyin(&s, a, sizeof(s));		\
188199767f8SToomas Soome 	a += roundup(sizeof(s), sizeof(u_long));		\
189199767f8SToomas Soome }
190199767f8SToomas Soome 
191199767f8SToomas Soome #define	MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
192199767f8SToomas Soome #define	MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
193199767f8SToomas Soome 
194199767f8SToomas Soome #define	MOD_METADATA(a, mm, c) {				\
195199767f8SToomas Soome 	COPY32(MODINFO_METADATA | mm->md_type, a, c);		\
196199767f8SToomas Soome 	COPY32(mm->md_size, a, c);				\
197199767f8SToomas Soome 	if (c)							\
198199767f8SToomas Soome 		archsw.arch_copyin(mm->md_data, a, mm->md_size);	\
199199767f8SToomas Soome 	a += roundup(mm->md_size, sizeof(u_long));		\
200199767f8SToomas Soome }
201199767f8SToomas Soome 
202199767f8SToomas Soome #define	MOD_END(a, c) {						\
203199767f8SToomas Soome 	COPY32(MODINFO_END, a, c);				\
204199767f8SToomas Soome 	COPY32(0, a, c);					\
205199767f8SToomas Soome }
206199767f8SToomas Soome 
207199767f8SToomas Soome static vm_offset_t
bi_copymodules(vm_offset_t addr)208199767f8SToomas Soome bi_copymodules(vm_offset_t addr)
209199767f8SToomas Soome {
210199767f8SToomas Soome 	struct preloaded_file *fp;
211199767f8SToomas Soome 	struct file_metadata *md;
212199767f8SToomas Soome 	int c;
213199767f8SToomas Soome 	uint64_t v;
214199767f8SToomas Soome 
215199767f8SToomas Soome 	c = addr != 0;
216199767f8SToomas Soome 	/* Start with the first module on the list, should be the kernel. */
217199767f8SToomas Soome 	for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
218199767f8SToomas Soome 		MOD_NAME(addr, fp->f_name, c); /* This must come first. */
219199767f8SToomas Soome 		MOD_TYPE(addr, fp->f_type, c);
220199767f8SToomas Soome 		if (fp->f_args)
221199767f8SToomas Soome 			MOD_ARGS(addr, fp->f_args, c);
222199767f8SToomas Soome 		v = fp->f_addr;
223199767f8SToomas Soome #if defined(__arm__)
224199767f8SToomas Soome 		v -= __elfN(relocation_offset);
225199767f8SToomas Soome #endif
226199767f8SToomas Soome 		MOD_ADDR(addr, v, c);
227199767f8SToomas Soome 		v = fp->f_size;
228199767f8SToomas Soome 		MOD_SIZE(addr, v, c);
229199767f8SToomas Soome 		for (md = fp->f_metadata; md != NULL; md = md->md_next)
230199767f8SToomas Soome 			if (!(md->md_type & MODINFOMD_NOCOPY))
231199767f8SToomas Soome 				MOD_METADATA(addr, md, c);
232199767f8SToomas Soome 	}
233199767f8SToomas Soome 	MOD_END(addr, c);
234199767f8SToomas Soome 	return(addr);
235199767f8SToomas Soome }
236199767f8SToomas Soome 
237199767f8SToomas Soome static int
bi_load_efi_data(struct preloaded_file * kfp)238199767f8SToomas Soome bi_load_efi_data(struct preloaded_file *kfp)
239199767f8SToomas Soome {
240199767f8SToomas Soome 	EFI_MEMORY_DESCRIPTOR *mm;
241199767f8SToomas Soome 	EFI_PHYSICAL_ADDRESS addr;
242199767f8SToomas Soome 	EFI_STATUS status;
243199767f8SToomas Soome 	size_t efisz;
244199767f8SToomas Soome 	UINTN efi_mapkey;
245199767f8SToomas Soome 	UINTN mmsz, pages, retry, sz;
246199767f8SToomas Soome 	UINT32 mmver;
247199767f8SToomas Soome 	struct efi_map_header *efihdr;
248199767f8SToomas Soome 
249199767f8SToomas Soome #if defined(__amd64__)
250199767f8SToomas Soome 	struct efi_fb efifb;
251199767f8SToomas Soome 
252199767f8SToomas Soome 	if (efi_find_framebuffer(&efifb) == 0) {
253199767f8SToomas Soome 		printf("EFI framebuffer information:\n");
254199767f8SToomas Soome 		printf("addr, size     0x%lx, 0x%lx\n", efifb.fb_addr,
255199767f8SToomas Soome 		    efifb.fb_size);
256199767f8SToomas Soome 		printf("dimensions     %d x %d\n", efifb.fb_width,
257199767f8SToomas Soome 		    efifb.fb_height);
258199767f8SToomas Soome 		printf("stride         %d\n", efifb.fb_stride);
259199767f8SToomas Soome 		printf("masks          0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
260199767f8SToomas Soome 		    efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
261199767f8SToomas Soome 		    efifb.fb_mask_reserved);
262199767f8SToomas Soome 
263199767f8SToomas Soome 		file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
264199767f8SToomas Soome 	}
265199767f8SToomas Soome #endif
266199767f8SToomas Soome 
267199767f8SToomas Soome 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
268199767f8SToomas Soome 
269199767f8SToomas Soome 	/*
270199767f8SToomas Soome 	 * It is possible that the first call to ExitBootServices may change
271199767f8SToomas Soome 	 * the map key. Fetch a new map key and retry ExitBootServices in that
272199767f8SToomas Soome 	 * case.
273199767f8SToomas Soome 	 */
274199767f8SToomas Soome 	for (retry = 2; retry > 0; retry--) {
275199767f8SToomas Soome 		/*
276199767f8SToomas Soome 		 * Allocate enough pages to hold the bootinfo block and the
277199767f8SToomas Soome 		 * memory map EFI will return to us. The memory map has an
278199767f8SToomas Soome 		 * unknown size, so we have to determine that first. Note that
279199767f8SToomas Soome 		 * the AllocatePages call can itself modify the memory map, so
280199767f8SToomas Soome 		 * we have to take that into account as well. The changes to
281199767f8SToomas Soome 		 * the memory map are caused by splitting a range of free
282199767f8SToomas Soome 		 * memory into two (AFAICT), so that one is marked as being
283199767f8SToomas Soome 		 * loader data.
284199767f8SToomas Soome 		 */
285199767f8SToomas Soome 		sz = 0;
286199767f8SToomas Soome 		BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
287199767f8SToomas Soome 		sz += mmsz;
288199767f8SToomas Soome 		sz = (sz + 0xf) & ~0xf;
289199767f8SToomas Soome 		pages = EFI_SIZE_TO_PAGES(sz + efisz);
290199767f8SToomas Soome 		status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
291199767f8SToomas Soome 		     pages, &addr);
292199767f8SToomas Soome 		if (EFI_ERROR(status)) {
293199767f8SToomas Soome 			printf("%s: AllocatePages error %lu\n", __func__,
294199767f8SToomas Soome 			    EFI_ERROR_CODE(status));
295199767f8SToomas Soome 			return (ENOMEM);
296199767f8SToomas Soome 		}
297199767f8SToomas Soome 
298199767f8SToomas Soome 		/*
299199767f8SToomas Soome 		 * Read the memory map and stash it after bootinfo. Align the
300199767f8SToomas Soome 		 * memory map on a 16-byte boundary (the bootinfo block is page
301199767f8SToomas Soome 		 * aligned).
302199767f8SToomas Soome 		 */
30383b4671eSToomas Soome 		efihdr = (struct efi_map_header *)(uintptr_t)addr;
304199767f8SToomas Soome 		mm = (void *)((uint8_t *)efihdr + efisz);
305199767f8SToomas Soome 		sz = (EFI_PAGE_SIZE * pages) - efisz;
306199767f8SToomas Soome 
307199767f8SToomas Soome 		status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
308199767f8SToomas Soome 		if (EFI_ERROR(status)) {
309199767f8SToomas Soome 			printf("%s: GetMemoryMap error %lu\n", __func__,
310199767f8SToomas Soome 			    EFI_ERROR_CODE(status));
311199767f8SToomas Soome 			return (EINVAL);
312199767f8SToomas Soome 		}
313199767f8SToomas Soome 		status = BS->ExitBootServices(IH, efi_mapkey);
314199767f8SToomas Soome 		if (EFI_ERROR(status) == 0) {
315199767f8SToomas Soome 			efihdr->memory_size = sz;
316199767f8SToomas Soome 			efihdr->descriptor_size = mmsz;
317199767f8SToomas Soome 			efihdr->descriptor_version = mmver;
318199767f8SToomas Soome 			file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
319199767f8SToomas Soome 			    efihdr);
320199767f8SToomas Soome 			return (0);
321199767f8SToomas Soome 		}
322199767f8SToomas Soome 		BS->FreePages(addr, pages);
323199767f8SToomas Soome 	}
324199767f8SToomas Soome 	printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
325199767f8SToomas Soome 	return (EINVAL);
326199767f8SToomas Soome }
327199767f8SToomas Soome 
328199767f8SToomas Soome /*
329199767f8SToomas Soome  * Load the information expected by an amd64 kernel.
330199767f8SToomas Soome  *
331199767f8SToomas Soome  * - The 'boothowto' argument is constructed.
332199767f8SToomas Soome  * - The 'bootdev' argument is constructed.
333199767f8SToomas Soome  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
334199767f8SToomas Soome  * - The kernel environment is copied into kernel space.
335199767f8SToomas Soome  * - Module metadata are formatted and placed in kernel space.
336199767f8SToomas Soome  */
337199767f8SToomas Soome int
bi_load(char * args,vm_offset_t * modulep,vm_offset_t * kernendp)338199767f8SToomas Soome bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
339199767f8SToomas Soome {
340199767f8SToomas Soome 	struct preloaded_file *xp, *kfp;
341199767f8SToomas Soome 	struct devdesc *rootdev;
342199767f8SToomas Soome 	struct file_metadata *md;
343199767f8SToomas Soome 	vm_offset_t addr;
344199767f8SToomas Soome 	uint64_t kernend;
345199767f8SToomas Soome 	uint64_t envp;
346199767f8SToomas Soome 	vm_offset_t size;
347199767f8SToomas Soome 	char *rootdevname;
348199767f8SToomas Soome 	int howto;
349199767f8SToomas Soome #if defined(LOADER_FDT_SUPPORT)
350199767f8SToomas Soome 	vm_offset_t dtbp;
351199767f8SToomas Soome 	int dtb_size;
352199767f8SToomas Soome #endif
353199767f8SToomas Soome #if defined(__arm__)
354199767f8SToomas Soome 	vm_offset_t vaddr;
355199767f8SToomas Soome 	size_t i;
356199767f8SToomas Soome 	/*
357199767f8SToomas Soome 	 * These metadata addreses must be converted for kernel after
358199767f8SToomas Soome 	 * relocation.
359199767f8SToomas Soome 	 */
360199767f8SToomas Soome 	uint32_t		mdt[] = {
361199767f8SToomas Soome 	    MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
362199767f8SToomas Soome 	    MODINFOMD_ENVP,
363199767f8SToomas Soome #if defined(LOADER_FDT_SUPPORT)
364199767f8SToomas Soome 	    MODINFOMD_DTBP
365199767f8SToomas Soome #endif
366199767f8SToomas Soome 	};
367199767f8SToomas Soome #endif
368199767f8SToomas Soome 
369199767f8SToomas Soome 	howto = bi_getboothowto(args);
370199767f8SToomas Soome 
371199767f8SToomas Soome 	/*
372199767f8SToomas Soome 	 * Allow the environment variable 'rootdev' to override the supplied
373199767f8SToomas Soome 	 * device. This should perhaps go to MI code and/or have $rootdev
374199767f8SToomas Soome 	 * tested/set by MI code before launching the kernel.
375199767f8SToomas Soome 	 */
376199767f8SToomas Soome 	rootdevname = getenv("rootdev");
377199767f8SToomas Soome 	archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
378199767f8SToomas Soome 	if (rootdev == NULL) {
379199767f8SToomas Soome 		printf("Can't determine root device.\n");
380199767f8SToomas Soome 		return(EINVAL);
381199767f8SToomas Soome 	}
382199767f8SToomas Soome 
383199767f8SToomas Soome 	/* Try reading the /etc/fstab file to select the root device */
384199767f8SToomas Soome 	getrootmount(efi_fmtdev((void *)rootdev));
385199767f8SToomas Soome 
386199767f8SToomas Soome 	addr = 0;
387199767f8SToomas Soome 	for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
388199767f8SToomas Soome 		if (addr < (xp->f_addr + xp->f_size))
389199767f8SToomas Soome 			addr = xp->f_addr + xp->f_size;
390199767f8SToomas Soome 	}
391199767f8SToomas Soome 
392199767f8SToomas Soome 	/* Pad to a page boundary. */
393199767f8SToomas Soome 	addr = roundup(addr, PAGE_SIZE);
394199767f8SToomas Soome 
395199767f8SToomas Soome 	/* Copy our environment. */
396199767f8SToomas Soome 	envp = addr;
397199767f8SToomas Soome 	addr = bi_copyenv(addr);
398199767f8SToomas Soome 
399199767f8SToomas Soome 	/* Pad to a page boundary. */
400199767f8SToomas Soome 	addr = roundup(addr, PAGE_SIZE);
401199767f8SToomas Soome 
402199767f8SToomas Soome #if defined(LOADER_FDT_SUPPORT)
403199767f8SToomas Soome 	/* Handle device tree blob */
404199767f8SToomas Soome 	dtbp = addr;
405199767f8SToomas Soome 	dtb_size = fdt_copy(addr);
406190f051bSToomas Soome 
407199767f8SToomas Soome 	/* Pad to a page boundary */
408199767f8SToomas Soome 	if (dtb_size)
409199767f8SToomas Soome 		addr += roundup(dtb_size, PAGE_SIZE);
410199767f8SToomas Soome #endif
411199767f8SToomas Soome 
412199767f8SToomas Soome 	kfp = file_findfile(NULL, "elf kernel");
413199767f8SToomas Soome 	if (kfp == NULL)
414199767f8SToomas Soome 		kfp = file_findfile(NULL, "elf64 kernel");
415199767f8SToomas Soome 	if (kfp == NULL)
416199767f8SToomas Soome 		panic("can't find kernel file");
417199767f8SToomas Soome 	kernend = 0;	/* fill it in later */
418199767f8SToomas Soome 	file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
419199767f8SToomas Soome 	file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
420199767f8SToomas Soome #if defined(LOADER_FDT_SUPPORT)
421199767f8SToomas Soome 	if (dtb_size)
422199767f8SToomas Soome 		file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
423199767f8SToomas Soome 	else
424199767f8SToomas Soome 		pager_output("WARNING! Trying to fire up the kernel, but no "
425199767f8SToomas Soome 		    "device tree blob found!\n");
426199767f8SToomas Soome #endif
427199767f8SToomas Soome 	file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
428199767f8SToomas Soome 	file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
429199767f8SToomas Soome 
430199767f8SToomas Soome 	bi_load_efi_data(kfp);
431199767f8SToomas Soome 
432199767f8SToomas Soome 	/* Figure out the size and location of the metadata. */
433199767f8SToomas Soome 	*modulep = addr;
434199767f8SToomas Soome 	size = bi_copymodules(0);
435199767f8SToomas Soome 	kernend = roundup(addr + size, PAGE_SIZE);
436199767f8SToomas Soome 	*kernendp = kernend;
437199767f8SToomas Soome 
438199767f8SToomas Soome 	/* patch MODINFOMD_KERNEND */
439199767f8SToomas Soome 	md = file_findmetadata(kfp, MODINFOMD_KERNEND);
440199767f8SToomas Soome 	bcopy(&kernend, md->md_data, sizeof kernend);
441199767f8SToomas Soome 
442199767f8SToomas Soome #if defined(__arm__)
443199767f8SToomas Soome 	*modulep -= __elfN(relocation_offset);
444199767f8SToomas Soome 
445199767f8SToomas Soome 	/* Do relocation fixup on metadata of each module. */
446199767f8SToomas Soome 	for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
4473867c045SToomas Soome 		for (i = 0; i < nitems(mdt); i++) {
448199767f8SToomas Soome 			md = file_findmetadata(xp, mdt[i]);
449199767f8SToomas Soome 			if (md) {
450199767f8SToomas Soome 				bcopy(md->md_data, &vaddr, sizeof vaddr);
451199767f8SToomas Soome 				vaddr -= __elfN(relocation_offset);
452199767f8SToomas Soome 				bcopy(&vaddr, md->md_data, sizeof vaddr);
453199767f8SToomas Soome 			}
454199767f8SToomas Soome 		}
455199767f8SToomas Soome 	}
456199767f8SToomas Soome #endif
457199767f8SToomas Soome 
458199767f8SToomas Soome 	/* Copy module list and metadata. */
459199767f8SToomas Soome 	(void)bi_copymodules(addr);
460199767f8SToomas Soome 
461199767f8SToomas Soome 	return (0);
462199767f8SToomas Soome }
463