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 <stand.h>
30 #include <string.h>
31 #include <sys/param.h>
32 #include <sys/linker.h>
33 #include <sys/reboot.h>
34 #include <sys/boot.h>
35 #include <machine/cpufunc.h>
36 #include <machine/elf.h>
37 #include <machine/metadata.h>
38 #include <machine/psl.h>
39
40 #ifdef EFI
41 #include <efi.h>
42 #include <efilib.h>
43 #else
44 #include "kboot.h"
45 #endif
46
47 #include "bootstrap.h"
48 #include "modinfo.h"
49
50 #if defined(__amd64__)
51 #include <machine/specialreg.h>
52 #endif
53
54 #ifdef EFI
55 #include "loader_efi.h"
56 #include "gfx_fb.h"
57 #endif
58
59 #if defined(LOADER_FDT_SUPPORT)
60 #include <fdt_platform.h>
61 #endif
62
63 #ifdef LOADER_GELI_SUPPORT
64 #include "geliboot.h"
65 #endif
66
67 static int
bi_getboothowto(char * kargs)68 bi_getboothowto(char *kargs)
69 {
70 #if defined(EFI) && (defined(__i386__) || defined(__amd64__))
71 const char *tmp;
72 int speed, port;
73 char buf[50];
74 #endif
75 char *console;
76 int howto;
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 #ifdef EFI
88 #if defined(__i386__) || defined(__amd64__)
89 if (strcmp(console, "efi") == 0 &&
90 getenv("efi_8250_uid") != NULL &&
91 getenv("hw.uart.console") == NULL) {
92 /*
93 * If we found a 8250 com port and com speed, we need to
94 * tell the kernel where the serial port is, and how
95 * fast. Ideally, we'd get the port from ACPI, but that
96 * isn't running in the loader. Do the next best thing
97 * by allowing it to be set by a loader.conf variable,
98 * either a EFI specific one, or the compatible
99 * comconsole_port if not. PCI support is needed, but
100 * for that we'd ideally refactor the
101 * libi386/comconsole.c code to have identical behavior.
102 * We only try to set the port for cases where we saw
103 * the Serial(x) node when parsing, otherwise
104 * specialized hardware that has Uart nodes will have a
105 * bogus address set.
106 * But if someone specifically setup hw.uart.console,
107 * don't override that.
108 */
109 speed = -1;
110 port = -1;
111 tmp = getenv("efi_com_speed");
112 if (tmp != NULL)
113 speed = strtol(tmp, NULL, 0);
114 tmp = getenv("efi_com_port");
115 if (tmp != NULL)
116 port = strtol(tmp, NULL, 0);
117 if (port <= 0) {
118 tmp = getenv("comconsole_port");
119 if (tmp != NULL)
120 port = strtol(tmp, NULL, 0);
121 else {
122 if (port == 0)
123 port = 0x3f8;
124 }
125 }
126 if (speed != -1 && port != -1) {
127 snprintf(buf, sizeof(buf), "io:%d,br:%d", port,
128 speed);
129 env_setenv("hw.uart.console", EV_VOLATILE, buf,
130 NULL, NULL);
131 }
132 }
133 #endif
134 #endif
135 }
136
137 return (howto);
138 }
139
140 #ifdef EFI
141 static EFI_STATUS
efi_do_vmap(EFI_MEMORY_DESCRIPTOR * mm,UINTN sz,UINTN mmsz,UINT32 mmver)142 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver)
143 {
144 EFI_MEMORY_DESCRIPTOR *desc, *viter, *vmap;
145 EFI_STATUS ret;
146 int curr, ndesc, nset;
147
148 nset = 0;
149 desc = mm;
150 ndesc = sz / mmsz;
151 vmap = malloc(sz);
152 if (vmap == NULL)
153 /* This isn't really an EFI error case, but pretend it is */
154 return (EFI_OUT_OF_RESOURCES);
155 viter = vmap;
156 for (curr = 0; curr < ndesc;
157 curr++, desc = NextMemoryDescriptor(desc, mmsz)) {
158 if ((desc->Attribute & EFI_MEMORY_RUNTIME) != 0) {
159 ++nset;
160 desc->VirtualStart = desc->PhysicalStart;
161 *viter = *desc;
162 viter = NextMemoryDescriptor(viter, mmsz);
163 }
164 }
165 ret = RS->SetVirtualAddressMap(nset * mmsz, mmsz, mmver, vmap);
166 free(vmap);
167 return (ret);
168 }
169
170 static int
bi_load_efi_data(struct preloaded_file * kfp,bool exit_bs)171 bi_load_efi_data(struct preloaded_file *kfp, bool exit_bs)
172 {
173 EFI_MEMORY_DESCRIPTOR *mm;
174 EFI_PHYSICAL_ADDRESS addr = 0;
175 EFI_STATUS status;
176 const char *efi_novmap;
177 size_t efisz;
178 UINTN efi_mapkey;
179 UINTN dsz, pages, retry, sz;
180 UINT32 mmver;
181 struct efi_map_header *efihdr;
182 bool do_vmap;
183
184 #ifdef MODINFOMD_EFI_FB
185 struct efi_fb efifb;
186
187 efifb.fb_addr = gfx_state.tg_fb.fb_addr;
188 efifb.fb_size = gfx_state.tg_fb.fb_size;
189 efifb.fb_height = gfx_state.tg_fb.fb_height;
190 efifb.fb_width = gfx_state.tg_fb.fb_width;
191 efifb.fb_stride = gfx_state.tg_fb.fb_stride;
192 efifb.fb_mask_red = gfx_state.tg_fb.fb_mask_red;
193 efifb.fb_mask_green = gfx_state.tg_fb.fb_mask_green;
194 efifb.fb_mask_blue = gfx_state.tg_fb.fb_mask_blue;
195 efifb.fb_mask_reserved = gfx_state.tg_fb.fb_mask_reserved;
196
197 if (efifb.fb_addr != 0) {
198 printf("EFI framebuffer information:\n");
199 printf("addr, size 0x%jx, 0x%jx\n",
200 efifb.fb_addr, efifb.fb_size);
201 printf("dimensions %d x %d\n",
202 efifb.fb_width, efifb.fb_height);
203 printf("stride %d\n", efifb.fb_stride);
204 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
205 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
206 efifb.fb_mask_reserved);
207
208 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
209 }
210 #endif
211
212 #if defined(__amd64__) || defined(__i386__)
213 /*
214 * Staging can't move after this point, so report the final value before
215 * we try to exit boot services below. The metadata added is added to
216 * the malloced arena that we setup when we started and doesn't interact
217 * with boot services.
218 */
219 printf("staging %#jx\n", (uintmax_t)staging);
220 #endif
221
222 do_vmap = true;
223 efi_novmap = getenv("efi_disable_vmap");
224 if (efi_novmap != NULL)
225 do_vmap = strcasecmp(efi_novmap, "YES") != 0;
226
227 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
228
229 /*
230 * Assign size of EFI_MEMORY_DESCRIPTOR to keep compatible with
231 * u-boot which doesn't fill this value when buffer for memory
232 * descriptors is too small (eg. 0 to obtain memory map size)
233 */
234 dsz = sizeof(EFI_MEMORY_DESCRIPTOR);
235
236 /*
237 * Allocate enough pages to hold the bootinfo block and the
238 * memory map EFI will return to us. The memory map has an
239 * unknown size, so we have to determine that first. Note that
240 * the AllocatePages call can itself modify the memory map, so
241 * we have to take that into account as well. The changes to
242 * the memory map are caused by splitting a range of free
243 * memory into two, so that one is marked as being loader
244 * data.
245 *
246 * Perform the allocation before the ExitBootServices retry
247 * loop so that no boot service calls (AllocatePages,
248 * FreePages) are made between GetMemoryMap and
249 * ExitBootServices. The UEFI specification (s7.4.6) requires
250 * that the memory map key passed to ExitBootServices match
251 * the current map; calling any boot service in between may
252 * invalidate the key. This matches the pre-allocation
253 * strategy used by the Linux EFI stub.
254 */
255 sz = 0;
256 status = BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &dsz, &mmver);
257 if (status != EFI_BUFFER_TOO_SMALL) {
258 printf("%s: GetMemoryMap error %lu\n", __func__,
259 DECODE_ERROR(status));
260 return (EINVAL);
261 }
262 sz += (10 * dsz);
263 pages = EFI_SIZE_TO_PAGES(sz + efisz);
264 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
265 pages, &addr);
266 if (EFI_ERROR(status)) {
267 printf("%s: AllocatePages error %lu\n", __func__,
268 DECODE_ERROR(status));
269 return (ENOMEM);
270 }
271
272 /*
273 * Read the memory map and stash it after bootinfo. Align the
274 * memory map on a 16-byte boundary (the bootinfo block is page
275 * aligned).
276 */
277 efihdr = (struct efi_map_header *)(uintptr_t)addr;
278 mm = (void *)((uint8_t *)efihdr + efisz);
279
280 /*
281 * Matthew Garrett has observed at least one system changing the
282 * memory map when calling ExitBootServices, causing it to return
283 * an error, probably because EBS event handlers allocate memory.
284 *
285 * The retry loop must not call any boot services between
286 * GetMemoryMap and ExitBootServices. Any boot service call
287 * (including printf/OutputString) can invalidate the memory
288 * map key, and the spec requires the key to be current.
289 */
290 for (retry = 2; retry > 0; retry--) {
291 sz = (EFI_PAGE_SIZE * pages) - efisz;
292 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &dsz, &mmver);
293 if (EFI_ERROR(status)) {
294 BS->FreePages(addr, pages);
295 printf("%s: GetMemoryMap error %lu\n", __func__,
296 DECODE_ERROR(status));
297 return (EINVAL);
298 }
299
300 if (!exit_bs)
301 break;
302 status = efi_exit_boot_services(efi_mapkey);
303 if (!EFI_ERROR(status))
304 break;
305 }
306
307 if (retry == 0) {
308 BS->FreePages(addr, pages);
309 printf("ExitBootServices error %lu\n", DECODE_ERROR(status));
310 return (EINVAL);
311 }
312
313 /*
314 * This may be disabled by setting efi_disable_vmap in
315 * loader.conf(5). By default we will setup the virtual
316 * map entries.
317 */
318 if (do_vmap)
319 efi_do_vmap(mm, sz, dsz, mmver);
320
321 /*
322 * Add the memory map to the metadata. addmetadata copies the data into
323 * the malloc arena, so we can safely free the memory map pages after.
324 * Or could if boot services was still running.
325 */
326 efihdr->memory_size = sz;
327 efihdr->descriptor_size = dsz;
328 efihdr->descriptor_version = mmver;
329 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
330 efihdr);
331 /* BS->FreePages(addr, pages); */
332
333 return (0);
334 }
335 #endif
336
337 /*
338 * Load the information expected by an amd64 kernel.
339 *
340 * - The 'boothowto' argument is constructed.
341 * - The 'bootdev' argument is constructed.
342 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
343 * - The kernel environment is copied into kernel space.
344 * - Module metadata are formatted and placed in kernel space.
345 */
346 int
bi_load(char * args,vm_offset_t * modulep,vm_offset_t * kernendp,bool exit_bs)347 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, bool exit_bs)
348 {
349 struct preloaded_file *xp, *kfp;
350 struct devdesc *rootdev;
351 struct file_metadata *md;
352 vm_offset_t addr;
353 uint64_t kernend;
354 #ifdef MODINFOMD_MODULEP
355 uint64_t module;
356 #endif
357 uint64_t envp;
358 vm_offset_t size;
359 char *rootdevname;
360 int howto;
361 #ifdef __i386__
362 /*
363 * The 32-bit UEFI loader is used to
364 * boot the 64-bit kernel on machines
365 * that support it.
366 */
367 bool is64 = true;
368 #else
369 bool is64 = sizeof(long) == 8;
370 #endif
371 #if defined(LOADER_FDT_SUPPORT)
372 vm_offset_t dtbp;
373 int dtb_size;
374 #endif
375 #if defined(__arm__)
376 vm_offset_t vaddr;
377 size_t i;
378 /*
379 * These metadata addreses must be converted for kernel after
380 * relocation.
381 */
382 uint32_t mdt[] = {
383 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
384 MODINFOMD_ENVP, MODINFOMD_FONT,
385 #if defined(LOADER_FDT_SUPPORT)
386 MODINFOMD_DTBP
387 #endif
388 };
389 #endif
390 howto = bi_getboothowto(args);
391
392 /*
393 * Allow the environment variable 'rootdev' to override the supplied
394 * device. This should perhaps go to MI code and/or have $rootdev
395 * tested/set by MI code before launching the kernel.
396 */
397 rootdevname = getenv("rootdev");
398 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
399 if (rootdev == NULL) {
400 printf("Can't determine root device.\n");
401 return(EINVAL);
402 }
403
404 /* Try reading the /etc/fstab file to select the root device */
405 getrootmount(devformat(rootdev));
406
407 addr = 0;
408 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
409 if (addr < xp->f_addr + xp->f_size)
410 addr = xp->f_addr + xp->f_size;
411 }
412
413 /* Pad to a page boundary. */
414 addr = md_align(addr);
415
416 #ifdef EFI
417 addr = build_font_module(addr);
418
419 /* Pad to a page boundary. */
420 addr = md_align(addr);
421
422 addr = build_splash_module(addr, SPLASH_STARTUP);
423
424 /* Pad to a page boundary. */
425 addr = md_align(addr);
426
427 addr = build_splash_module(addr, SPLASH_SHUTDOWN);
428
429 /* Pad to a page boundary. */
430 addr = md_align(addr);
431 #endif
432
433 /* Copy our environment. */
434 envp = addr;
435 addr = md_copyenv(addr);
436
437 /* Pad to a page boundary. */
438 addr = md_align(addr);
439
440 #if defined(LOADER_FDT_SUPPORT)
441 /* Handle device tree blob */
442 dtbp = addr;
443 dtb_size = fdt_copy(addr);
444
445 /* Pad to a page boundary */
446 if (dtb_size)
447 addr += md_align(dtb_size);
448 #endif
449
450 kfp = file_findfile(NULL, md_kerntype);
451 if (kfp == NULL)
452 panic("can't find kernel file");
453 kernend = 0; /* fill it in later */
454
455 /* Figure out the size and location of the metadata. */
456 *modulep = addr;
457
458 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof(howto), &howto);
459 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof(envp), &envp);
460 #if defined(LOADER_FDT_SUPPORT)
461 if (dtb_size)
462 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof(dtbp), &dtbp);
463 else if (getenv("acpi.revision") == NULL) {
464 printf("WARNING! Trying to fire up the kernel, but no "
465 "device tree blob found!\n");
466 }
467 #endif
468 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof(kernend), &kernend);
469 #ifdef MODINFOMD_MODULEP
470 module = *modulep;
471 file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof(module), &module);
472 #endif
473 #ifdef EFI
474 #ifndef __i386__
475 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof(ST), &ST);
476 #endif
477 #if defined(__amd64__) || defined(__i386__)
478 file_addmetadata(kfp, MODINFOMD_EFI_ARCH, sizeof(MACHINE_ARCH),
479 MACHINE_ARCH);
480 #endif
481 #endif
482 #ifdef LOADER_GELI_SUPPORT
483 geli_export_key_metadata(kfp);
484 #endif
485 #ifdef EFI
486 bi_load_efi_data(kfp, exit_bs);
487 #else
488 bi_loadsmap(kfp);
489 #endif
490
491 size = md_copymodules(0, is64); /* Find the size of the modules */
492 kernend = md_align(addr + size);
493 *kernendp = kernend;
494
495 /* patch MODINFOMD_KERNEND */
496 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
497 bcopy(&kernend, md->md_data, sizeof kernend);
498
499 #if defined(__arm__)
500 *modulep -= __elfN(relocation_offset);
501
502 /* Do relocation fixup on metadata of each module. */
503 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
504 for (i = 0; i < nitems(mdt); i++) {
505 md = file_findmetadata(xp, mdt[i]);
506 if (md) {
507 bcopy(md->md_data, &vaddr, sizeof vaddr);
508 vaddr -= __elfN(relocation_offset);
509 bcopy(&vaddr, md->md_data, sizeof vaddr);
510 }
511 }
512 }
513 #endif
514
515 /* Copy module list and metadata. */
516 (void)md_copymodules(addr, is64);
517
518 return (0);
519 }
520