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