xref: /illumos-gate/usr/src/boot/efi/loader/bootinfo.c (revision ca783257c986cddcc674ae22916a6766b98a2d36)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2004, 2006 Marcel Moolenaar
4  * Copyright (c) 2014 The FreeBSD Foundation
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 
31 #include <stand.h>
32 #include <string.h>
33 #include <sys/param.h>
34 #include <sys/reboot.h>
35 #include <sys/linker.h>
36 #include <sys/boot.h>
37 #include <machine/cpufunc.h>
38 #include <machine/elf.h>
39 #include <machine/metadata.h>
40 #include <machine/psl.h>
41 
42 #include <efi.h>
43 #include <efilib.h>
44 
45 #include "bootstrap.h"
46 #include "loader_efi.h"
47 
48 #if defined(__amd64__)
49 #include <machine/specialreg.h>
50 #include "framebuffer.h"
51 #endif
52 
53 #if defined(LOADER_FDT_SUPPORT)
54 #include <fdt_platform.h>
55 #endif
56 
57 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
58 
59 extern EFI_SYSTEM_TABLE	*ST;
60 
61 static const char howto_switches[] = "aCdrgDmphsv";
62 static int howto_masks[] = {
63 	RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
64 	RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
65 };
66 
67 static int
68 bi_getboothowto(char *kargs)
69 {
70 	const char *sw;
71 	char *opts;
72 	char *console;
73 	int howto, i;
74 
75 	howto = 0;
76 
77 	/* Get the boot options from the environment first. */
78 	for (i = 0; howto_names[i].ev != NULL; i++) {
79 		if (getenv(howto_names[i].ev) != NULL)
80 			howto |= howto_names[i].mask;
81 	}
82 
83 	console = getenv("console");
84 	if (console != NULL) {
85 		if (strcmp(console, "comconsole") == 0)
86 			howto |= RB_SERIAL;
87 		if (strcmp(console, "nullconsole") == 0)
88 			howto |= RB_MUTE;
89 	}
90 
91 	/* Parse kargs */
92 	if (kargs == NULL)
93 		return (howto);
94 
95 	opts = strchr(kargs, '-');
96 	while (opts != NULL) {
97 		while (*(++opts) != '\0') {
98 			sw = strchr(howto_switches, *opts);
99 			if (sw == NULL)
100 				break;
101 			howto |= howto_masks[sw - howto_switches];
102 		}
103 		opts = strchr(opts, '-');
104 	}
105 
106 	return (howto);
107 }
108 
109 /*
110  * Copy the environment into the load area starting at (addr).
111  * Each variable is formatted as <name>=<value>, with a single nul
112  * separating each variable, and a double nul terminating the environment.
113  */
114 vm_offset_t
115 bi_copyenv(vm_offset_t start)
116 {
117 	struct env_var *ep;
118 	vm_offset_t addr, last;
119 	size_t len;
120 
121 	addr = last = start;
122 
123 	/* Traverse the environment. */
124 	for (ep = environ; ep != NULL; ep = ep->ev_next) {
125 		len = strlen(ep->ev_name);
126 		if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
127 			break;
128 		addr += len;
129 		if (archsw.arch_copyin("=", addr, 1) != 1)
130 			break;
131 		addr++;
132 		if (ep->ev_value != NULL) {
133 			len = strlen(ep->ev_value);
134 			if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
135 				break;
136 			addr += len;
137 		}
138 		if (archsw.arch_copyin("", addr, 1) != 1)
139 			break;
140 		last = ++addr;
141 	}
142 
143 	if (archsw.arch_copyin("", last++, 1) != 1)
144 		last = start;
145 	return(last);
146 }
147 
148 /*
149  * Copy module-related data into the load area, where it can be
150  * used as a directory for loaded modules.
151  *
152  * Module data is presented in a self-describing format.  Each datum
153  * is preceded by a 32-bit identifier and a 32-bit size field.
154  *
155  * Currently, the following data are saved:
156  *
157  * MOD_NAME	(variable)		module name (string)
158  * MOD_TYPE	(variable)		module type (string)
159  * MOD_ARGS	(variable)		module parameters (string)
160  * MOD_ADDR	sizeof(vm_offset_t)	module load address
161  * MOD_SIZE	sizeof(size_t)		module size
162  * MOD_METADATA	(variable)		type-specific metadata
163  */
164 #define	COPY32(v, a, c) {					\
165 	uint32_t x = (v);					\
166 	if (c)							\
167 		archsw.arch_copyin(&x, a, sizeof(x));		\
168 	a += sizeof(x);						\
169 }
170 
171 #define	MOD_STR(t, a, s, c) {					\
172 	COPY32(t, a, c);					\
173 	COPY32(strlen(s) + 1, a, c);				\
174 	if (c)							\
175 		archsw.arch_copyin(s, a, strlen(s) + 1);	\
176 	a += roundup(strlen(s) + 1, sizeof(u_long));		\
177 }
178 
179 #define	MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
180 #define	MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
181 #define	MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
182 
183 #define	MOD_VAR(t, a, s, c) {					\
184 	COPY32(t, a, c);					\
185 	COPY32(sizeof(s), a, c);				\
186 	if (c)							\
187 		archsw.arch_copyin(&s, a, sizeof(s));		\
188 	a += roundup(sizeof(s), sizeof(u_long));		\
189 }
190 
191 #define	MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
192 #define	MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
193 
194 #define	MOD_METADATA(a, mm, c) {				\
195 	COPY32(MODINFO_METADATA | mm->md_type, a, c);		\
196 	COPY32(mm->md_size, a, c);				\
197 	if (c)							\
198 		archsw.arch_copyin(mm->md_data, a, mm->md_size);	\
199 	a += roundup(mm->md_size, sizeof(u_long));		\
200 }
201 
202 #define	MOD_END(a, c) {						\
203 	COPY32(MODINFO_END, a, c);				\
204 	COPY32(0, a, c);					\
205 }
206 
207 static vm_offset_t
208 bi_copymodules(vm_offset_t addr)
209 {
210 	struct preloaded_file *fp;
211 	struct file_metadata *md;
212 	int c;
213 	uint64_t v;
214 
215 	c = addr != 0;
216 	/* Start with the first module on the list, should be the kernel. */
217 	for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
218 		MOD_NAME(addr, fp->f_name, c); /* This must come first. */
219 		MOD_TYPE(addr, fp->f_type, c);
220 		if (fp->f_args)
221 			MOD_ARGS(addr, fp->f_args, c);
222 		v = fp->f_addr;
223 #if defined(__arm__)
224 		v -= __elfN(relocation_offset);
225 #endif
226 		MOD_ADDR(addr, v, c);
227 		v = fp->f_size;
228 		MOD_SIZE(addr, v, c);
229 		for (md = fp->f_metadata; md != NULL; md = md->md_next)
230 			if (!(md->md_type & MODINFOMD_NOCOPY))
231 				MOD_METADATA(addr, md, c);
232 	}
233 	MOD_END(addr, c);
234 	return(addr);
235 }
236 
237 static int
238 bi_load_efi_data(struct preloaded_file *kfp)
239 {
240 	EFI_MEMORY_DESCRIPTOR *mm;
241 	EFI_PHYSICAL_ADDRESS addr;
242 	EFI_STATUS status;
243 	size_t efisz;
244 	UINTN efi_mapkey;
245 	UINTN mmsz, pages, retry, sz;
246 	UINT32 mmver;
247 	struct efi_map_header *efihdr;
248 
249 #if defined(__amd64__)
250 	struct efi_fb efifb;
251 
252 	if (efi_find_framebuffer(&efifb) == 0) {
253 		printf("EFI framebuffer information:\n");
254 		printf("addr, size     0x%lx, 0x%lx\n", efifb.fb_addr,
255 		    efifb.fb_size);
256 		printf("dimensions     %d x %d\n", efifb.fb_width,
257 		    efifb.fb_height);
258 		printf("stride         %d\n", efifb.fb_stride);
259 		printf("masks          0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
260 		    efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
261 		    efifb.fb_mask_reserved);
262 
263 		file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
264 	}
265 #endif
266 
267 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
268 
269 	/*
270 	 * It is possible that the first call to ExitBootServices may change
271 	 * the map key. Fetch a new map key and retry ExitBootServices in that
272 	 * case.
273 	 */
274 	for (retry = 2; retry > 0; retry--) {
275 		/*
276 		 * Allocate enough pages to hold the bootinfo block and the
277 		 * memory map EFI will return to us. The memory map has an
278 		 * unknown size, so we have to determine that first. Note that
279 		 * the AllocatePages call can itself modify the memory map, so
280 		 * we have to take that into account as well. The changes to
281 		 * the memory map are caused by splitting a range of free
282 		 * memory into two (AFAICT), so that one is marked as being
283 		 * loader data.
284 		 */
285 		sz = 0;
286 		BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
287 		sz += mmsz;
288 		sz = (sz + 0xf) & ~0xf;
289 		pages = EFI_SIZE_TO_PAGES(sz + efisz);
290 		status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
291 		     pages, &addr);
292 		if (EFI_ERROR(status)) {
293 			printf("%s: AllocatePages error %lu\n", __func__,
294 			    EFI_ERROR_CODE(status));
295 			return (ENOMEM);
296 		}
297 
298 		/*
299 		 * Read the memory map and stash it after bootinfo. Align the
300 		 * memory map on a 16-byte boundary (the bootinfo block is page
301 		 * aligned).
302 		 */
303 		efihdr = (struct efi_map_header *)(uintptr_t)addr;
304 		mm = (void *)((uint8_t *)efihdr + efisz);
305 		sz = (EFI_PAGE_SIZE * pages) - efisz;
306 
307 		status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
308 		if (EFI_ERROR(status)) {
309 			printf("%s: GetMemoryMap error %lu\n", __func__,
310 			    EFI_ERROR_CODE(status));
311 			return (EINVAL);
312 		}
313 		status = BS->ExitBootServices(IH, efi_mapkey);
314 		if (EFI_ERROR(status) == 0) {
315 			efihdr->memory_size = sz;
316 			efihdr->descriptor_size = mmsz;
317 			efihdr->descriptor_version = mmver;
318 			file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
319 			    efihdr);
320 			return (0);
321 		}
322 		BS->FreePages(addr, pages);
323 	}
324 	printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
325 	return (EINVAL);
326 }
327 
328 /*
329  * Load the information expected by an amd64 kernel.
330  *
331  * - The 'boothowto' argument is constructed.
332  * - The 'bootdev' argument is constructed.
333  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
334  * - The kernel environment is copied into kernel space.
335  * - Module metadata are formatted and placed in kernel space.
336  */
337 int
338 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
339 {
340 	struct preloaded_file *xp, *kfp;
341 	struct devdesc *rootdev;
342 	struct file_metadata *md;
343 	vm_offset_t addr;
344 	uint64_t kernend;
345 	uint64_t envp;
346 	vm_offset_t size;
347 	char *rootdevname;
348 	int howto;
349 #if defined(LOADER_FDT_SUPPORT)
350 	vm_offset_t dtbp;
351 	int dtb_size;
352 #endif
353 #if defined(__arm__)
354 	vm_offset_t vaddr;
355 	size_t i;
356 	/*
357 	 * These metadata addreses must be converted for kernel after
358 	 * relocation.
359 	 */
360 	uint32_t		mdt[] = {
361 	    MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
362 	    MODINFOMD_ENVP,
363 #if defined(LOADER_FDT_SUPPORT)
364 	    MODINFOMD_DTBP
365 #endif
366 	};
367 #endif
368 
369 	howto = bi_getboothowto(args);
370 
371 	/*
372 	 * Allow the environment variable 'rootdev' to override the supplied
373 	 * device. This should perhaps go to MI code and/or have $rootdev
374 	 * tested/set by MI code before launching the kernel.
375 	 */
376 	rootdevname = getenv("rootdev");
377 	archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
378 	if (rootdev == NULL) {
379 		printf("Can't determine root device.\n");
380 		return(EINVAL);
381 	}
382 
383 	/* Try reading the /etc/fstab file to select the root device */
384 	getrootmount(efi_fmtdev((void *)rootdev));
385 
386 	addr = 0;
387 	for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
388 		if (addr < (xp->f_addr + xp->f_size))
389 			addr = xp->f_addr + xp->f_size;
390 	}
391 
392 	/* Pad to a page boundary. */
393 	addr = roundup(addr, PAGE_SIZE);
394 
395 	/* Copy our environment. */
396 	envp = addr;
397 	addr = bi_copyenv(addr);
398 
399 	/* Pad to a page boundary. */
400 	addr = roundup(addr, PAGE_SIZE);
401 
402 #if defined(LOADER_FDT_SUPPORT)
403 	/* Handle device tree blob */
404 	dtbp = addr;
405 	dtb_size = fdt_copy(addr);
406 
407 	/* Pad to a page boundary */
408 	if (dtb_size)
409 		addr += roundup(dtb_size, PAGE_SIZE);
410 #endif
411 
412 	kfp = file_findfile(NULL, "elf kernel");
413 	if (kfp == NULL)
414 		kfp = file_findfile(NULL, "elf64 kernel");
415 	if (kfp == NULL)
416 		panic("can't find kernel file");
417 	kernend = 0;	/* fill it in later */
418 	file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
419 	file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
420 #if defined(LOADER_FDT_SUPPORT)
421 	if (dtb_size)
422 		file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
423 	else
424 		pager_output("WARNING! Trying to fire up the kernel, but no "
425 		    "device tree blob found!\n");
426 #endif
427 	file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
428 	file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
429 
430 	bi_load_efi_data(kfp);
431 
432 	/* Figure out the size and location of the metadata. */
433 	*modulep = addr;
434 	size = bi_copymodules(0);
435 	kernend = roundup(addr + size, PAGE_SIZE);
436 	*kernendp = kernend;
437 
438 	/* patch MODINFOMD_KERNEND */
439 	md = file_findmetadata(kfp, MODINFOMD_KERNEND);
440 	bcopy(&kernend, md->md_data, sizeof kernend);
441 
442 #if defined(__arm__)
443 	*modulep -= __elfN(relocation_offset);
444 
445 	/* Do relocation fixup on metadata of each module. */
446 	for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
447 		for (i = 0; i < nitems(mdt); i++) {
448 			md = file_findmetadata(xp, mdt[i]);
449 			if (md) {
450 				bcopy(md->md_data, &vaddr, sizeof vaddr);
451 				vaddr -= __elfN(relocation_offset);
452 				bcopy(&vaddr, md->md_data, sizeof vaddr);
453 			}
454 		}
455 	}
456 #endif
457 
458 	/* Copy module list and metadata. */
459 	(void)bi_copymodules(addr);
460 
461 	return (0);
462 }
463