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