xref: /illumos-gate/usr/src/boot/efi/loader/bootinfo.c (revision 7a87437fd36d10a97e4d5952dba549b6f0fadbfc)
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,
135 			    addr, len) != len)
136 				break;
137 			addr += len;
138 		}
139 		if (archsw.arch_copyin("", addr, 1) != 1)
140 			break;
141 		last = ++addr;
142 	}
143 
144 	if (archsw.arch_copyin("", last++, 1) != 1)
145 		last = start;
146 	return (last);
147 }
148 
149 /*
150  * Copy module-related data into the load area, where it can be
151  * used as a directory for loaded modules.
152  *
153  * Module data is presented in a self-describing format.  Each datum
154  * is preceded by a 32-bit identifier and a 32-bit size field.
155  *
156  * Currently, the following data are saved:
157  *
158  * MOD_NAME	(variable)		module name (string)
159  * MOD_TYPE	(variable)		module type (string)
160  * MOD_ARGS	(variable)		module parameters (string)
161  * MOD_ADDR	sizeof(vm_offset_t)	module load address
162  * MOD_SIZE	sizeof(size_t)		module size
163  * MOD_METADATA	(variable)		type-specific metadata
164  */
165 #define	COPY32(v, a, c) {					\
166 	uint32_t x = (v);					\
167 	if (c)							\
168 		archsw.arch_copyin(&x, a, sizeof (x));		\
169 	a += sizeof (x);					\
170 }
171 
172 #define	MOD_STR(t, a, s, c) {					\
173 	COPY32(t, a, c);					\
174 	COPY32(strlen(s) + 1, a, c);				\
175 	if (c)							\
176 		archsw.arch_copyin(s, a, strlen(s) + 1);	\
177 	a += roundup(strlen(s) + 1, sizeof (ulong_t));		\
178 }
179 
180 #define	MOD_NAME(a, s, c)	MOD_STR(MODINFO_NAME, a, s, c)
181 #define	MOD_TYPE(a, s, c)	MOD_STR(MODINFO_TYPE, a, s, c)
182 #define	MOD_ARGS(a, s, c)	MOD_STR(MODINFO_ARGS, a, s, c)
183 
184 #define	MOD_VAR(t, a, s, c) {					\
185 	COPY32(t, a, c);					\
186 	COPY32(sizeof (s), a, c);				\
187 	if (c)							\
188 		archsw.arch_copyin(&s, a, sizeof (s));		\
189 	a += roundup(sizeof (s), sizeof (ulong_t));		\
190 }
191 
192 #define	MOD_ADDR(a, s, c)	MOD_VAR(MODINFO_ADDR, a, s, c)
193 #define	MOD_SIZE(a, s, c)	MOD_VAR(MODINFO_SIZE, a, s, c)
194 
195 #define	MOD_METADATA(a, mm, c) {				\
196 	COPY32(MODINFO_METADATA | mm->md_type, a, c);		\
197 	COPY32(mm->md_size, a, c);				\
198 	if (c)							\
199 		archsw.arch_copyin(mm->md_data, a, mm->md_size);	\
200 	a += roundup(mm->md_size, sizeof (ulong_t));		\
201 }
202 
203 #define	MOD_END(a, c) {						\
204 	COPY32(MODINFO_END, a, c);				\
205 	COPY32(0, a, c);					\
206 }
207 
208 static vm_offset_t
209 bi_copymodules(vm_offset_t addr)
210 {
211 	struct preloaded_file *fp;
212 	struct file_metadata *md;
213 	int c;
214 	uint64_t v;
215 
216 	c = addr != 0;
217 	/* Start with the first module on the list, should be the kernel. */
218 	for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
219 		MOD_NAME(addr, fp->f_name, c); /* This must come first. */
220 		MOD_TYPE(addr, fp->f_type, c);
221 		if (fp->f_args)
222 			MOD_ARGS(addr, fp->f_args, c);
223 		v = fp->f_addr;
224 #if defined(__arm__)
225 		v -= __elfN(relocation_offset);
226 #endif
227 		MOD_ADDR(addr, v, c);
228 		v = fp->f_size;
229 		MOD_SIZE(addr, v, c);
230 		for (md = fp->f_metadata; md != NULL; md = md->md_next)
231 			if (!(md->md_type & MODINFOMD_NOCOPY))
232 				MOD_METADATA(addr, md, c);
233 	}
234 	MOD_END(addr, c);
235 	return (addr);
236 }
237 
238 static int
239 bi_load_efi_data(struct preloaded_file *kfp)
240 {
241 	EFI_MEMORY_DESCRIPTOR *mm;
242 	EFI_PHYSICAL_ADDRESS addr;
243 	EFI_STATUS status;
244 	size_t efisz;
245 	UINTN efi_mapkey;
246 	UINTN mmsz, pages, retry, sz;
247 	UINT32 mmver;
248 	struct efi_map_header *efihdr;
249 
250 #if defined(__amd64__)
251 	struct efi_fb efifb;
252 
253 	if (efi_find_framebuffer(&efifb) == 0) {
254 		printf("EFI framebuffer information:\n");
255 		printf("addr, size     0x%lx, 0x%lx\n", efifb.fb_addr,
256 		    efifb.fb_size);
257 		printf("dimensions     %d x %d\n", efifb.fb_width,
258 		    efifb.fb_height);
259 		printf("stride         %d\n", efifb.fb_stride);
260 		printf("masks          0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
261 		    efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
262 		    efifb.fb_mask_reserved);
263 
264 		file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof (efifb), &efifb);
265 	}
266 #endif
267 
268 	efisz = (sizeof (struct efi_map_header) + 0xf) & ~0xf;
269 
270 	/*
271 	 * It is possible that the first call to ExitBootServices may change
272 	 * the map key. Fetch a new map key and retry ExitBootServices in that
273 	 * case.
274 	 */
275 	for (retry = 2; retry > 0; retry--) {
276 		/*
277 		 * Allocate enough pages to hold the bootinfo block and the
278 		 * memory map EFI will return to us. The memory map has an
279 		 * unknown size, so we have to determine that first. Note that
280 		 * the AllocatePages call can itself modify the memory map, so
281 		 * we have to take that into account as well. The changes to
282 		 * the memory map are caused by splitting a range of free
283 		 * memory into two (AFAICT), so that one is marked as being
284 		 * loader data.
285 		 */
286 		sz = 0;
287 		BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
288 		sz += mmsz;
289 		sz = (sz + 0xf) & ~0xf;
290 		pages = EFI_SIZE_TO_PAGES(sz + efisz);
291 		status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
292 		    pages, &addr);
293 		if (EFI_ERROR(status)) {
294 			printf("%s: AllocatePages error %lu\n", __func__,
295 			    DECODE_ERROR(status));
296 			return (ENOMEM);
297 		}
298 
299 		/*
300 		 * Read the memory map and stash it after bootinfo. Align the
301 		 * memory map on a 16-byte boundary (the bootinfo block is page
302 		 * aligned).
303 		 */
304 		efihdr = (struct efi_map_header *)(uintptr_t)addr;
305 		mm = (void *)((uint8_t *)efihdr + efisz);
306 		sz = (EFI_PAGE_SIZE * pages) - efisz;
307 
308 		status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
309 		if (EFI_ERROR(status)) {
310 			printf("%s: GetMemoryMap error %lu\n", __func__,
311 			    DECODE_ERROR(status));
312 			return (EINVAL);
313 		}
314 		status = BS->ExitBootServices(IH, efi_mapkey);
315 		if (EFI_ERROR(status) == 0) {
316 			efihdr->memory_size = sz;
317 			efihdr->descriptor_size = mmsz;
318 			efihdr->descriptor_version = mmver;
319 			file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
320 			    efihdr);
321 			return (0);
322 		}
323 		BS->FreePages(addr, pages);
324 	}
325 	printf("ExitBootServices error %lu\n", DECODE_ERROR(status));
326 	return (EINVAL);
327 }
328 
329 /*
330  * Load the information expected by an amd64 kernel.
331  *
332  * - The 'boothowto' argument is constructed.
333  * - The 'bootdev' argument is constructed.
334  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
335  * - The kernel environment is copied into kernel space.
336  * - Module metadata are formatted and placed in kernel space.
337  */
338 int
339 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
340 {
341 	struct preloaded_file *xp, *kfp;
342 	struct devdesc *rootdev;
343 	struct file_metadata *md;
344 	vm_offset_t addr;
345 	uint64_t kernend;
346 	uint64_t envp;
347 	vm_offset_t size;
348 	char *rootdevname;
349 	int howto;
350 #if defined(LOADER_FDT_SUPPORT)
351 	vm_offset_t dtbp;
352 	int dtb_size;
353 #endif
354 #if defined(__arm__)
355 	vm_offset_t vaddr;
356 	size_t i;
357 	/*
358 	 * These metadata addreses must be converted for kernel after
359 	 * relocation.
360 	 */
361 	uint32_t		mdt[] = {
362 	    MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
363 	    MODINFOMD_ENVP,
364 #if defined(LOADER_FDT_SUPPORT)
365 	    MODINFOMD_DTBP
366 #endif
367 	};
368 #endif
369 
370 	howto = bi_getboothowto(args);
371 
372 	/*
373 	 * Allow the environment variable 'rootdev' to override the supplied
374 	 * device. This should perhaps go to MI code and/or have $rootdev
375 	 * tested/set by MI code before launching the kernel.
376 	 */
377 	rootdevname = getenv("rootdev");
378 	archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
379 	if (rootdev == NULL) {
380 		printf("Can't determine root device.\n");
381 		return (EINVAL);
382 	}
383 
384 	/* Try reading the /etc/fstab file to select the root device */
385 	getrootmount(efi_fmtdev((void *)rootdev));
386 
387 	addr = 0;
388 	for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
389 		if (addr < (xp->f_addr + xp->f_size))
390 			addr = xp->f_addr + xp->f_size;
391 	}
392 
393 	/* Pad to a page boundary. */
394 	addr = roundup(addr, PAGE_SIZE);
395 
396 	/* Copy our environment. */
397 	envp = addr;
398 	addr = bi_copyenv(addr);
399 
400 	/* Pad to a page boundary. */
401 	addr = roundup(addr, PAGE_SIZE);
402 
403 #if defined(LOADER_FDT_SUPPORT)
404 	/* Handle device tree blob */
405 	dtbp = addr;
406 	dtb_size = fdt_copy(addr);
407 
408 	/* Pad to a page boundary */
409 	if (dtb_size)
410 		addr += roundup(dtb_size, PAGE_SIZE);
411 #endif
412 
413 	kfp = file_findfile(NULL, "elf kernel");
414 	if (kfp == NULL)
415 		kfp = file_findfile(NULL, "elf64 kernel");
416 	if (kfp == NULL)
417 		panic("can't find kernel file");
418 	kernend = 0;	/* fill it in later */
419 	file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof (howto), &howto);
420 	file_addmetadata(kfp, MODINFOMD_ENVP, sizeof (envp), &envp);
421 #if defined(LOADER_FDT_SUPPORT)
422 	if (dtb_size)
423 		file_addmetadata(kfp, MODINFOMD_DTBP, sizeof (dtbp), &dtbp);
424 	else
425 		pager_output("WARNING! Trying to fire up the kernel, but no "
426 		    "device tree blob found!\n");
427 #endif
428 	file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof (kernend), &kernend);
429 	file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof (ST), &ST);
430 
431 	bi_load_efi_data(kfp);
432 
433 	/* Figure out the size and location of the metadata. */
434 	*modulep = addr;
435 	size = bi_copymodules(0);
436 	kernend = roundup(addr + size, PAGE_SIZE);
437 	*kernendp = kernend;
438 
439 	/* patch MODINFOMD_KERNEND */
440 	md = file_findmetadata(kfp, MODINFOMD_KERNEND);
441 	bcopy(&kernend, md->md_data, sizeof (kernend));
442 
443 #if defined(__arm__)
444 	*modulep -= __elfN(relocation_offset);
445 
446 	/* Do relocation fixup on metadata of each module. */
447 	for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
448 		for (i = 0; i < nitems(mdt); i++) {
449 			md = file_findmetadata(xp, mdt[i]);
450 			if (md) {
451 				bcopy(md->md_data, &vaddr, sizeof (vaddr));
452 				vaddr -= __elfN(relocation_offset);
453 				bcopy(&vaddr, md->md_data, sizeof (vaddr));
454 			}
455 		}
456 	}
457 #endif
458 
459 	/* Copy module list and metadata. */
460 	(void) bi_copymodules(addr);
461 
462 	return (0);
463 }
464