1 /*-
2 * Copyright (c) 1998 Michael Smith <[email protected]>
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 __FBSDID("$FreeBSD$");
31
32 #include <stand.h>
33 #include <string.h>
34 #include <sys/param.h>
35 #include <sys/linker.h>
36 #include <sys/reboot.h>
37 #include <sys/boot.h>
38 #include <machine/cpufunc.h>
39 #include <machine/elf.h>
40 #include <machine/metadata.h>
41 #include <machine/psl.h>
42
43 #include <efi.h>
44 #include <efilib.h>
45
46 #include "bootstrap.h"
47 #include "loader_efi.h"
48
49 #if defined(__amd64__)
50 #include <machine/specialreg.h>
51 #endif
52
53 #include "gfx_fb.h"
54
55 #if defined(LOADER_FDT_SUPPORT)
56 #include <fdt_platform.h>
57 #endif
58
59 #ifdef LOADER_GELI_SUPPORT
60 #include "geliboot.h"
61 #endif
62
63 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
64
65 extern EFI_SYSTEM_TABLE *ST;
66
67 int boot_services_gone;
68
69 static int
bi_getboothowto(char * kargs)70 bi_getboothowto(char *kargs)
71 {
72 const char *sw, *tmp;
73 char *opts;
74 char *console;
75 int howto, speed, port;
76 char buf[50];
77
78 howto = boot_parse_cmdline(kargs);
79 howto |= boot_env_to_howto();
80
81 console = getenv("console");
82 if (console != NULL) {
83 if (strcmp(console, "comconsole") == 0)
84 howto |= RB_SERIAL;
85 if (strcmp(console, "nullconsole") == 0)
86 howto |= RB_MUTE;
87 #if defined(__i386__) || defined(__amd64__)
88 if (strcmp(console, "efi") == 0 &&
89 getenv("efi_8250_uid") != NULL &&
90 getenv("hw.uart.console") == NULL) {
91 /*
92 * If we found a 8250 com port and com speed, we need to
93 * tell the kernel where the serial port is, and how
94 * fast. Ideally, we'd get the port from ACPI, but that
95 * isn't running in the loader. Do the next best thing
96 * by allowing it to be set by a loader.conf variable,
97 * either a EFI specific one, or the compatible
98 * comconsole_port if not. PCI support is needed, but
99 * for that we'd ideally refactor the
100 * libi386/comconsole.c code to have identical behavior.
101 * We only try to set the port for cases where we saw
102 * the Serial(x) node when parsing, otherwise
103 * specialized hardware that has Uart nodes will have a
104 * bogus address set.
105 * But if someone specifically setup hw.uart.console,
106 * don't override that.
107 */
108 speed = -1;
109 port = -1;
110 tmp = getenv("efi_com_speed");
111 if (tmp != NULL)
112 speed = strtol(tmp, NULL, 0);
113 tmp = getenv("efi_com_port");
114 if (tmp == NULL)
115 tmp = getenv("comconsole_port");
116 if (tmp != NULL)
117 port = strtol(tmp, NULL, 0);
118 if (speed != -1 && port != -1) {
119 snprintf(buf, sizeof(buf), "io:%d,br:%d", port,
120 speed);
121 env_setenv("hw.uart.console", EV_VOLATILE, buf,
122 NULL, NULL);
123 }
124 }
125 #endif
126 }
127
128 return (howto);
129 }
130
131 /*
132 * Copy the environment into the load area starting at (addr).
133 * Each variable is formatted as <name>=<value>, with a single nul
134 * separating each variable, and a double nul terminating the environment.
135 */
136 static vm_offset_t
bi_copyenv(vm_offset_t start)137 bi_copyenv(vm_offset_t start)
138 {
139 struct env_var *ep;
140 vm_offset_t addr, last;
141 size_t len;
142
143 addr = last = start;
144
145 /* Traverse the environment. */
146 for (ep = environ; ep != NULL; ep = ep->ev_next) {
147 len = strlen(ep->ev_name);
148 if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
149 break;
150 addr += len;
151 if (archsw.arch_copyin("=", addr, 1) != 1)
152 break;
153 addr++;
154 if (ep->ev_value != NULL) {
155 len = strlen(ep->ev_value);
156 if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
157 break;
158 addr += len;
159 }
160 if (archsw.arch_copyin("", addr, 1) != 1)
161 break;
162 last = ++addr;
163 }
164
165 if (archsw.arch_copyin("", last++, 1) != 1)
166 last = start;
167 return(last);
168 }
169
170 /*
171 * Copy module-related data into the load area, where it can be
172 * used as a directory for loaded modules.
173 *
174 * Module data is presented in a self-describing format. Each datum
175 * is preceded by a 32-bit identifier and a 32-bit size field.
176 *
177 * Currently, the following data are saved:
178 *
179 * MOD_NAME (variable) module name (string)
180 * MOD_TYPE (variable) module type (string)
181 * MOD_ARGS (variable) module parameters (string)
182 * MOD_ADDR sizeof(vm_offset_t) module load address
183 * MOD_SIZE sizeof(size_t) module size
184 * MOD_METADATA (variable) type-specific metadata
185 */
186 #define COPY32(v, a, c) { \
187 uint32_t x = (v); \
188 if (c) \
189 archsw.arch_copyin(&x, a, sizeof(x)); \
190 a += sizeof(x); \
191 }
192
193 #define MOD_STR(t, a, s, c) { \
194 COPY32(t, a, c); \
195 COPY32(strlen(s) + 1, a, c); \
196 if (c) \
197 archsw.arch_copyin(s, a, strlen(s) + 1); \
198 a += roundup(strlen(s) + 1, sizeof(u_long)); \
199 }
200
201 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
202 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
203 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
204
205 #define MOD_VAR(t, a, s, c) { \
206 COPY32(t, a, c); \
207 COPY32(sizeof(s), a, c); \
208 if (c) \
209 archsw.arch_copyin(&s, a, sizeof(s)); \
210 a += roundup(sizeof(s), sizeof(u_long)); \
211 }
212
213 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
214 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
215
216 #define MOD_METADATA(a, mm, c) { \
217 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
218 COPY32(mm->md_size, a, c); \
219 if (c) \
220 archsw.arch_copyin(mm->md_data, a, mm->md_size); \
221 a += roundup(mm->md_size, sizeof(u_long)); \
222 }
223
224 #define MOD_END(a, c) { \
225 COPY32(MODINFO_END, a, c); \
226 COPY32(0, a, c); \
227 }
228
229 static vm_offset_t
bi_copymodules(vm_offset_t addr)230 bi_copymodules(vm_offset_t addr)
231 {
232 struct preloaded_file *fp;
233 struct file_metadata *md;
234 int c;
235 uint64_t v;
236
237 c = addr != 0;
238 /* Start with the first module on the list, should be the kernel. */
239 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
240 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
241 MOD_TYPE(addr, fp->f_type, c);
242 if (fp->f_args)
243 MOD_ARGS(addr, fp->f_args, c);
244 v = fp->f_addr;
245 #if defined(__arm__)
246 v -= __elfN(relocation_offset);
247 #endif
248 MOD_ADDR(addr, v, c);
249 v = fp->f_size;
250 MOD_SIZE(addr, v, c);
251 for (md = fp->f_metadata; md != NULL; md = md->md_next)
252 if (!(md->md_type & MODINFOMD_NOCOPY))
253 MOD_METADATA(addr, md, c);
254 }
255 MOD_END(addr, c);
256 return(addr);
257 }
258
259 static EFI_STATUS
efi_do_vmap(EFI_MEMORY_DESCRIPTOR * mm,UINTN sz,UINTN mmsz,UINT32 mmver)260 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver)
261 {
262 EFI_MEMORY_DESCRIPTOR *desc, *viter, *vmap;
263 EFI_STATUS ret;
264 int curr, ndesc, nset;
265
266 nset = 0;
267 desc = mm;
268 ndesc = sz / mmsz;
269 vmap = malloc(sz);
270 if (vmap == NULL)
271 /* This isn't really an EFI error case, but pretend it is */
272 return (EFI_OUT_OF_RESOURCES);
273 viter = vmap;
274 for (curr = 0; curr < ndesc;
275 curr++, desc = NextMemoryDescriptor(desc, mmsz)) {
276 if ((desc->Attribute & EFI_MEMORY_RUNTIME) != 0) {
277 ++nset;
278 desc->VirtualStart = desc->PhysicalStart;
279 *viter = *desc;
280 viter = NextMemoryDescriptor(viter, mmsz);
281 }
282 }
283 ret = RS->SetVirtualAddressMap(nset * mmsz, mmsz, mmver, vmap);
284 free(vmap);
285 return (ret);
286 }
287
288 static int
bi_load_efi_data(struct preloaded_file * kfp)289 bi_load_efi_data(struct preloaded_file *kfp)
290 {
291 EFI_MEMORY_DESCRIPTOR *mm;
292 EFI_PHYSICAL_ADDRESS addr = 0;
293 EFI_STATUS status;
294 const char *efi_novmap;
295 size_t efisz;
296 UINTN efi_mapkey;
297 UINTN dsz, pages, retry, sz;
298 UINT32 mmver;
299 struct efi_map_header *efihdr;
300 bool do_vmap;
301
302 #if defined(__amd64__) || defined(__aarch64__)
303 struct efi_fb efifb;
304
305 efifb.fb_addr = gfx_state.tg_fb.fb_addr;
306 efifb.fb_size = gfx_state.tg_fb.fb_size;
307 efifb.fb_height = gfx_state.tg_fb.fb_height;
308 efifb.fb_width = gfx_state.tg_fb.fb_width;
309 efifb.fb_stride = gfx_state.tg_fb.fb_stride;
310 efifb.fb_mask_red = gfx_state.tg_fb.fb_mask_red;
311 efifb.fb_mask_green = gfx_state.tg_fb.fb_mask_green;
312 efifb.fb_mask_blue = gfx_state.tg_fb.fb_mask_blue;
313 efifb.fb_mask_reserved = gfx_state.tg_fb.fb_mask_reserved;
314
315 printf("EFI framebuffer information:\n");
316 printf("addr, size 0x%jx, 0x%jx\n", efifb.fb_addr, efifb.fb_size);
317 printf("dimensions %d x %d\n", efifb.fb_width, efifb.fb_height);
318 printf("stride %d\n", efifb.fb_stride);
319 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
320 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
321 efifb.fb_mask_reserved);
322
323 if (efifb.fb_addr != 0)
324 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
325 #endif
326
327 do_vmap = true;
328 efi_novmap = getenv("efi_disable_vmap");
329 if (efi_novmap != NULL)
330 do_vmap = strcasecmp(efi_novmap, "YES") != 0;
331
332 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
333
334 /*
335 * Assign size of EFI_MEMORY_DESCRIPTOR to keep compatible with
336 * u-boot which doesn't fill this value when buffer for memory
337 * descriptors is too small (eg. 0 to obtain memory map size)
338 */
339 dsz = sizeof(EFI_MEMORY_DESCRIPTOR);
340
341 /*
342 * Allocate enough pages to hold the bootinfo block and the
343 * memory map EFI will return to us. The memory map has an
344 * unknown size, so we have to determine that first. Note that
345 * the AllocatePages call can itself modify the memory map, so
346 * we have to take that into account as well. The changes to
347 * the memory map are caused by splitting a range of free
348 * memory into two, so that one is marked as being loader
349 * data.
350 */
351
352 sz = 0;
353
354 /*
355 * Matthew Garrett has observed at least one system changing the
356 * memory map when calling ExitBootServices, causing it to return an
357 * error, probably because callbacks are allocating memory.
358 * So we need to retry calling it at least once.
359 */
360 for (retry = 2; retry > 0; retry--) {
361 for (;;) {
362 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &dsz, &mmver);
363 if (!EFI_ERROR(status))
364 break;
365
366 if (status != EFI_BUFFER_TOO_SMALL) {
367 printf("%s: GetMemoryMap error %lu\n", __func__,
368 EFI_ERROR_CODE(status));
369 return (EINVAL);
370 }
371
372 if (addr != 0)
373 BS->FreePages(addr, pages);
374
375 /* Add 10 descriptors to the size to allow for
376 * fragmentation caused by calling AllocatePages */
377 sz += (10 * dsz);
378 pages = EFI_SIZE_TO_PAGES(sz + efisz);
379 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
380 pages, &addr);
381 if (EFI_ERROR(status)) {
382 printf("%s: AllocatePages error %lu\n", __func__,
383 EFI_ERROR_CODE(status));
384 return (ENOMEM);
385 }
386
387 /*
388 * Read the memory map and stash it after bootinfo. Align the
389 * memory map on a 16-byte boundary (the bootinfo block is page
390 * aligned).
391 */
392 efihdr = (struct efi_map_header *)(uintptr_t)addr;
393 mm = (void *)((uint8_t *)efihdr + efisz);
394 sz = (EFI_PAGE_SIZE * pages) - efisz;
395 }
396
397 status = BS->ExitBootServices(IH, efi_mapkey);
398 if (!EFI_ERROR(status)) {
399 boot_services_gone = 1;
400 break;
401 }
402 }
403
404 if (retry == 0) {
405 BS->FreePages(addr, pages);
406 printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
407 return (EINVAL);
408 }
409
410 /*
411 * This may be disabled by setting efi_disable_vmap in
412 * loader.conf(5). By default we will setup the virtual
413 * map entries.
414 */
415
416 if (do_vmap)
417 efi_do_vmap(mm, sz, dsz, mmver);
418 efihdr->memory_size = sz;
419 efihdr->descriptor_size = dsz;
420 efihdr->descriptor_version = mmver;
421 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
422 efihdr);
423
424 return (0);
425 }
426
427 /*
428 * Load the information expected by an amd64 kernel.
429 *
430 * - The 'boothowto' argument is constructed.
431 * - The 'bootdev' argument is constructed.
432 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
433 * - The kernel environment is copied into kernel space.
434 * - Module metadata are formatted and placed in kernel space.
435 */
436 int
bi_load(char * args,vm_offset_t * modulep,vm_offset_t * kernendp)437 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
438 {
439 struct preloaded_file *xp, *kfp;
440 struct devdesc *rootdev;
441 struct file_metadata *md;
442 vm_offset_t addr;
443 uint64_t kernend;
444 uint64_t envp;
445 vm_offset_t size;
446 char *rootdevname;
447 int howto;
448 #if defined(LOADER_FDT_SUPPORT)
449 vm_offset_t dtbp;
450 int dtb_size;
451 #endif
452 #if defined(__arm__)
453 vm_offset_t vaddr;
454 size_t i;
455 /*
456 * These metadata addreses must be converted for kernel after
457 * relocation.
458 */
459 uint32_t mdt[] = {
460 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
461 MODINFOMD_ENVP, MODINFOMD_FONT,
462 #if defined(LOADER_FDT_SUPPORT)
463 MODINFOMD_DTBP
464 #endif
465 };
466 #endif
467
468 howto = bi_getboothowto(args);
469
470 /*
471 * Allow the environment variable 'rootdev' to override the supplied
472 * device. This should perhaps go to MI code and/or have $rootdev
473 * tested/set by MI code before launching the kernel.
474 */
475 rootdevname = getenv("rootdev");
476 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
477 if (rootdev == NULL) {
478 printf("Can't determine root device.\n");
479 return(EINVAL);
480 }
481
482 /* Try reading the /etc/fstab file to select the root device */
483 getrootmount(efi_fmtdev((void *)rootdev));
484
485 addr = 0;
486 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
487 if (addr < (xp->f_addr + xp->f_size))
488 addr = xp->f_addr + xp->f_size;
489 }
490
491 /* Pad to a page boundary. */
492 addr = roundup(addr, PAGE_SIZE);
493
494 addr = build_font_module(addr);
495
496 /* Pad to a page boundary. */
497 addr = roundup(addr, PAGE_SIZE);
498
499 /* Copy our environment. */
500 envp = addr;
501 addr = bi_copyenv(addr);
502
503 /* Pad to a page boundary. */
504 addr = roundup(addr, PAGE_SIZE);
505
506 #if defined(LOADER_FDT_SUPPORT)
507 /* Handle device tree blob */
508 dtbp = addr;
509 dtb_size = fdt_copy(addr);
510
511 /* Pad to a page boundary */
512 if (dtb_size)
513 addr += roundup(dtb_size, PAGE_SIZE);
514 #endif
515
516 kfp = file_findfile(NULL, "elf kernel");
517 if (kfp == NULL)
518 kfp = file_findfile(NULL, "elf64 kernel");
519 if (kfp == NULL)
520 panic("can't find kernel file");
521 kernend = 0; /* fill it in later */
522 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof(howto), &howto);
523 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof(envp), &envp);
524 #if defined(LOADER_FDT_SUPPORT)
525 if (dtb_size)
526 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof(dtbp), &dtbp);
527 else
528 printf("WARNING! Trying to fire up the kernel, but no "
529 "device tree blob found!\n");
530 #endif
531 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof(kernend), &kernend);
532 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof(ST), &ST);
533 #ifdef LOADER_GELI_SUPPORT
534 geli_export_key_metadata(kfp);
535 #endif
536 bi_load_efi_data(kfp);
537
538 /* Figure out the size and location of the metadata. */
539 *modulep = addr;
540 size = bi_copymodules(0);
541 kernend = roundup(addr + size, PAGE_SIZE);
542 *kernendp = kernend;
543
544 /* patch MODINFOMD_KERNEND */
545 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
546 bcopy(&kernend, md->md_data, sizeof kernend);
547
548 #if defined(__arm__)
549 *modulep -= __elfN(relocation_offset);
550
551 /* Do relocation fixup on metadata of each module. */
552 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
553 for (i = 0; i < nitems(mdt); i++) {
554 md = file_findmetadata(xp, mdt[i]);
555 if (md) {
556 bcopy(md->md_data, &vaddr, sizeof vaddr);
557 vaddr -= __elfN(relocation_offset);
558 bcopy(&vaddr, md->md_data, sizeof vaddr);
559 }
560 }
561 }
562 #endif
563
564 /* Copy module list and metadata. */
565 (void)bi_copymodules(addr);
566
567 return (0);
568 }
569