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