]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/efi/loader/bootinfo.c
Extend loader(8) geli support to all architectures and all disk-like devices.
[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 #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 "framebuffer.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 static int
68 bi_getboothowto(char *kargs)
69 {
70         const char *sw;
71         char *opts;
72         char *console;
73         int howto;
74
75         howto = boot_parse_cmdline(kargs);
76         howto |= boot_env_to_howto();
77
78         console = getenv("console");
79         if (console != NULL) {
80                 if (strcmp(console, "comconsole") == 0)
81                         howto |= RB_SERIAL;
82                 if (strcmp(console, "nullconsole") == 0)
83                         howto |= RB_MUTE;
84         }
85
86         return (howto);
87 }
88
89 /*
90  * Copy the environment into the load area starting at (addr).
91  * Each variable is formatted as <name>=<value>, with a single nul
92  * separating each variable, and a double nul terminating the environment.
93  */
94 static vm_offset_t
95 bi_copyenv(vm_offset_t start)
96 {
97         struct env_var *ep;
98         vm_offset_t addr, last;
99         size_t len;
100
101         addr = last = start;
102
103         /* Traverse the environment. */
104         for (ep = environ; ep != NULL; ep = ep->ev_next) {
105                 len = strlen(ep->ev_name);
106                 if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
107                         break;
108                 addr += len;
109                 if (archsw.arch_copyin("=", addr, 1) != 1)
110                         break;
111                 addr++;
112                 if (ep->ev_value != NULL) {
113                         len = strlen(ep->ev_value);
114                         if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
115                                 break;
116                         addr += len;
117                 }
118                 if (archsw.arch_copyin("", addr, 1) != 1)
119                         break;
120                 last = ++addr;
121         }
122
123         if (archsw.arch_copyin("", last++, 1) != 1)
124                 last = start;
125         return(last);
126 }
127
128 /*
129  * Copy module-related data into the load area, where it can be
130  * used as a directory for loaded modules.
131  *
132  * Module data is presented in a self-describing format.  Each datum
133  * is preceded by a 32-bit identifier and a 32-bit size field.
134  *
135  * Currently, the following data are saved:
136  *
137  * MOD_NAME     (variable)              module name (string)
138  * MOD_TYPE     (variable)              module type (string)
139  * MOD_ARGS     (variable)              module parameters (string)
140  * MOD_ADDR     sizeof(vm_offset_t)     module load address
141  * MOD_SIZE     sizeof(size_t)          module size
142  * MOD_METADATA (variable)              type-specific metadata
143  */
144 #define COPY32(v, a, c) {                                       \
145         uint32_t x = (v);                                       \
146         if (c)                                                  \
147                 archsw.arch_copyin(&x, a, sizeof(x));           \
148         a += sizeof(x);                                         \
149 }
150
151 #define MOD_STR(t, a, s, c) {                                   \
152         COPY32(t, a, c);                                        \
153         COPY32(strlen(s) + 1, a, c);                            \
154         if (c)                                                  \
155                 archsw.arch_copyin(s, a, strlen(s) + 1);        \
156         a += roundup(strlen(s) + 1, sizeof(u_long));            \
157 }
158
159 #define MOD_NAME(a, s, c)       MOD_STR(MODINFO_NAME, a, s, c)
160 #define MOD_TYPE(a, s, c)       MOD_STR(MODINFO_TYPE, a, s, c)
161 #define MOD_ARGS(a, s, c)       MOD_STR(MODINFO_ARGS, a, s, c)
162
163 #define MOD_VAR(t, a, s, c) {                                   \
164         COPY32(t, a, c);                                        \
165         COPY32(sizeof(s), a, c);                                \
166         if (c)                                                  \
167                 archsw.arch_copyin(&s, a, sizeof(s));           \
168         a += roundup(sizeof(s), sizeof(u_long));                \
169 }
170
171 #define MOD_ADDR(a, s, c)       MOD_VAR(MODINFO_ADDR, a, s, c)
172 #define MOD_SIZE(a, s, c)       MOD_VAR(MODINFO_SIZE, a, s, c)
173
174 #define MOD_METADATA(a, mm, c) {                                \
175         COPY32(MODINFO_METADATA | mm->md_type, a, c);           \
176         COPY32(mm->md_size, a, c);                              \
177         if (c)                                                  \
178                 archsw.arch_copyin(mm->md_data, a, mm->md_size);        \
179         a += roundup(mm->md_size, sizeof(u_long));              \
180 }
181
182 #define MOD_END(a, c) {                                         \
183         COPY32(MODINFO_END, a, c);                              \
184         COPY32(0, a, c);                                        \
185 }
186
187 static vm_offset_t
188 bi_copymodules(vm_offset_t addr)
189 {
190         struct preloaded_file *fp;
191         struct file_metadata *md;
192         int c;
193         uint64_t v;
194
195         c = addr != 0;
196         /* Start with the first module on the list, should be the kernel. */
197         for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
198                 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
199                 MOD_TYPE(addr, fp->f_type, c);
200                 if (fp->f_args)
201                         MOD_ARGS(addr, fp->f_args, c);
202                 v = fp->f_addr;
203 #if defined(__arm__)
204                 v -= __elfN(relocation_offset);
205 #endif
206                 MOD_ADDR(addr, v, c);
207                 v = fp->f_size;
208                 MOD_SIZE(addr, v, c);
209                 for (md = fp->f_metadata; md != NULL; md = md->md_next)
210                         if (!(md->md_type & MODINFOMD_NOCOPY))
211                                 MOD_METADATA(addr, md, c);
212         }
213         MOD_END(addr, c);
214         return(addr);
215 }
216
217 static EFI_STATUS
218 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver)
219 {
220         EFI_MEMORY_DESCRIPTOR *desc, *viter, *vmap;
221         EFI_STATUS ret;
222         int curr, ndesc, nset;
223
224         nset = 0;
225         desc = mm;
226         ndesc = sz / mmsz;
227         vmap = malloc(sz);
228         if (vmap == NULL)
229                 /* This isn't really an EFI error case, but pretend it is */
230                 return (EFI_OUT_OF_RESOURCES);
231         viter = vmap;
232         for (curr = 0; curr < ndesc;
233             curr++, desc = NextMemoryDescriptor(desc, mmsz)) {
234                 if ((desc->Attribute & EFI_MEMORY_RUNTIME) != 0) {
235                         ++nset;
236                         desc->VirtualStart = desc->PhysicalStart;
237                         *viter = *desc;
238                         viter = NextMemoryDescriptor(viter, mmsz);
239                 }
240         }
241         ret = RS->SetVirtualAddressMap(nset * mmsz, mmsz, mmver, vmap);
242         free(vmap);
243         return (ret);
244 }
245
246 static int
247 bi_load_efi_data(struct preloaded_file *kfp)
248 {
249         EFI_MEMORY_DESCRIPTOR *mm;
250         EFI_PHYSICAL_ADDRESS addr;
251         EFI_STATUS status;
252         const char *efi_novmap;
253         size_t efisz;
254         UINTN efi_mapkey;
255         UINTN mmsz, pages, retry, sz;
256         UINT32 mmver;
257         struct efi_map_header *efihdr;
258         bool do_vmap;
259
260 #if defined(__amd64__) || defined(__aarch64__)
261         struct efi_fb efifb;
262
263         if (efi_find_framebuffer(&efifb) == 0) {
264                 printf("EFI framebuffer information:\n");
265                 printf("addr, size     0x%jx, 0x%jx\n", efifb.fb_addr,
266                     efifb.fb_size);
267                 printf("dimensions     %d x %d\n", efifb.fb_width,
268                     efifb.fb_height);
269                 printf("stride         %d\n", efifb.fb_stride);
270                 printf("masks          0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
271                     efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
272                     efifb.fb_mask_reserved);
273
274                 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
275         }
276 #endif
277
278         do_vmap = true;
279         efi_novmap = getenv("efi_disable_vmap");
280         if (efi_novmap != NULL)
281                 do_vmap = strcasecmp(efi_novmap, "YES") != 0;
282
283         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
284
285         /*
286          * Assgin size of EFI_MEMORY_DESCRIPTOR to keep compatible with
287          * u-boot which doesn't fill this value when buffer for memory
288          * descriptors is too small (eg. 0 to obtain memory map size)
289          */
290         mmsz = sizeof(EFI_MEMORY_DESCRIPTOR);
291
292         /*
293          * It is possible that the first call to ExitBootServices may change
294          * the map key. Fetch a new map key and retry ExitBootServices in that
295          * case.
296          */
297         for (retry = 2; retry > 0; retry--) {
298                 /*
299                  * Allocate enough pages to hold the bootinfo block and the
300                  * memory map EFI will return to us. The memory map has an
301                  * unknown size, so we have to determine that first. Note that
302                  * the AllocatePages call can itself modify the memory map, so
303                  * we have to take that into account as well. The changes to
304                  * the memory map are caused by splitting a range of free
305                  * memory into two (AFAICT), so that one is marked as being
306                  * loader data.
307                  */
308                 sz = 0;
309                 BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
310                 sz += mmsz;
311                 sz = (sz + 0xf) & ~0xf;
312                 pages = EFI_SIZE_TO_PAGES(sz + efisz);
313                 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
314                      pages, &addr);
315                 if (EFI_ERROR(status)) {
316                         printf("%s: AllocatePages error %lu\n", __func__,
317                             EFI_ERROR_CODE(status));
318                         return (ENOMEM);
319                 }
320
321                 /*
322                  * Read the memory map and stash it after bootinfo. Align the
323                  * memory map on a 16-byte boundary (the bootinfo block is page
324                  * aligned).
325                  */
326                 efihdr = (struct efi_map_header *)addr;
327                 mm = (void *)((uint8_t *)efihdr + efisz);
328                 sz = (EFI_PAGE_SIZE * pages) - efisz;
329
330                 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
331                 if (EFI_ERROR(status)) {
332                         printf("%s: GetMemoryMap error %lu\n", __func__,
333                             EFI_ERROR_CODE(status));
334                         return (EINVAL);
335                 }
336                 status = BS->ExitBootServices(IH, efi_mapkey);
337                 if (EFI_ERROR(status) == 0) {
338                         /*
339                          * This may be disabled by setting efi_disable_vmap in
340                          * loader.conf(5). By default we will setup the virtual
341                          * map entries.
342                          */
343                         if (do_vmap)
344                                 efi_do_vmap(mm, sz, mmsz, mmver);
345                         efihdr->memory_size = sz;
346                         efihdr->descriptor_size = mmsz;
347                         efihdr->descriptor_version = mmver;
348                         file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
349                             efihdr);
350                         return (0);
351                 }
352                 BS->FreePages(addr, pages);
353         }
354         printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
355         return (EINVAL);
356 }
357
358 /*
359  * Load the information expected by an amd64 kernel.
360  *
361  * - The 'boothowto' argument is constructed.
362  * - The 'bootdev' argument is constructed.
363  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
364  * - The kernel environment is copied into kernel space.
365  * - Module metadata are formatted and placed in kernel space.
366  */
367 int
368 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
369 {
370         struct preloaded_file *xp, *kfp;
371         struct devdesc *rootdev;
372         struct file_metadata *md;
373         vm_offset_t addr;
374         uint64_t kernend;
375         uint64_t envp;
376         vm_offset_t size;
377         char *rootdevname;
378         int howto;
379 #if defined(LOADER_FDT_SUPPORT)
380         vm_offset_t dtbp;
381         int dtb_size;
382 #endif
383 #if defined(__arm__)
384         vm_offset_t vaddr;
385         size_t i;
386         /*
387          * These metadata addreses must be converted for kernel after
388          * relocation.
389          */
390         uint32_t                mdt[] = {
391             MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
392             MODINFOMD_ENVP,
393 #if defined(LOADER_FDT_SUPPORT)
394             MODINFOMD_DTBP
395 #endif
396         };
397 #endif
398
399         howto = bi_getboothowto(args);
400
401         /*
402          * Allow the environment variable 'rootdev' to override the supplied
403          * device. This should perhaps go to MI code and/or have $rootdev
404          * tested/set by MI code before launching the kernel.
405          */
406         rootdevname = getenv("rootdev");
407         archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
408         if (rootdev == NULL) {
409                 printf("Can't determine root device.\n");
410                 return(EINVAL);
411         }
412
413         /* Try reading the /etc/fstab file to select the root device */
414         getrootmount(efi_fmtdev((void *)rootdev));
415
416         addr = 0;
417         for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
418                 if (addr < (xp->f_addr + xp->f_size))
419                         addr = xp->f_addr + xp->f_size;
420         }
421
422         /* Pad to a page boundary. */
423         addr = roundup(addr, PAGE_SIZE);
424
425         /* Copy our environment. */
426         envp = addr;
427         addr = bi_copyenv(addr);
428
429         /* Pad to a page boundary. */
430         addr = roundup(addr, PAGE_SIZE);
431
432 #if defined(LOADER_FDT_SUPPORT)
433         /* Handle device tree blob */
434         dtbp = addr;
435         dtb_size = fdt_copy(addr);
436                 
437         /* Pad to a page boundary */
438         if (dtb_size)
439                 addr += roundup(dtb_size, PAGE_SIZE);
440 #endif
441
442         kfp = file_findfile(NULL, "elf kernel");
443         if (kfp == NULL)
444                 kfp = file_findfile(NULL, "elf64 kernel");
445         if (kfp == NULL)
446                 panic("can't find kernel file");
447         kernend = 0;    /* fill it in later */
448         file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
449         file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
450 #if defined(LOADER_FDT_SUPPORT)
451         if (dtb_size)
452                 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
453         else
454                 printf("WARNING! Trying to fire up the kernel, but no "
455                     "device tree blob found!\n");
456 #endif
457         file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
458         file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
459 #ifdef LOADER_GELI_SUPPORT
460         geli_export_key_metadata(kfp);
461 #endif
462         bi_load_efi_data(kfp);
463
464         /* Figure out the size and location of the metadata. */
465         *modulep = addr;
466         size = bi_copymodules(0);
467         kernend = roundup(addr + size, PAGE_SIZE);
468         *kernendp = kernend;
469
470         /* patch MODINFOMD_KERNEND */
471         md = file_findmetadata(kfp, MODINFOMD_KERNEND);
472         bcopy(&kernend, md->md_data, sizeof kernend);
473
474 #if defined(__arm__)
475         *modulep -= __elfN(relocation_offset);
476
477         /* Do relocation fixup on metadata of each module. */
478         for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
479                 for (i = 0; i < nitems(mdt); i++) {
480                         md = file_findmetadata(xp, mdt[i]);
481                         if (md) {
482                                 bcopy(md->md_data, &vaddr, sizeof vaddr);
483                                 vaddr -= __elfN(relocation_offset);
484                                 bcopy(&vaddr, md->md_data, sizeof vaddr);
485                         }
486                 }
487         }
488 #endif
489
490         /* Copy module list and metadata. */
491         (void)bi_copymodules(addr);
492
493         return (0);
494 }