]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/efi/loader/bootinfo.c
Merge from head
[FreeBSD/FreeBSD.git] / sys / boot / 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/reboot.h>
36 #include <sys/linker.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 #include "framebuffer.h"
52 #endif
53
54 #if defined(LOADER_FDT_SUPPORT)
55 #include <fdt_platform.h>
56 #endif
57
58 UINTN efi_mapkey;
59
60 static const char howto_switches[] = "aCdrgDmphsv";
61 static int howto_masks[] = {
62         RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
63         RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
64 };
65
66 static int
67 bi_getboothowto(char *kargs)
68 {
69         const char *sw;
70         char *opts;
71         char *console;
72         int howto, i;
73
74         howto = 0;
75
76         /* Get the boot options from the environment first. */
77         for (i = 0; howto_names[i].ev != NULL; i++) {
78                 if (getenv(howto_names[i].ev) != NULL)
79                         howto |= howto_names[i].mask;
80         }
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         }
89
90         /* Parse kargs */
91         if (kargs == NULL)
92                 return (howto);
93
94         opts = strchr(kargs, '-');
95         while (opts != NULL) {
96                 while (*(++opts) != '\0') {
97                         sw = strchr(howto_switches, *opts);
98                         if (sw == NULL)
99                                 break;
100                         howto |= howto_masks[sw - howto_switches];
101                 }
102                 opts = strchr(opts, '-');
103         }
104
105         return (howto);
106 }
107
108 /*
109  * Copy the environment into the load area starting at (addr).
110  * Each variable is formatted as <name>=<value>, with a single nul
111  * separating each variable, and a double nul terminating the environment.
112  */
113 static vm_offset_t
114 bi_copyenv(vm_offset_t start)
115 {
116         struct env_var *ep;
117         vm_offset_t addr, last;
118         size_t len;
119
120         addr = last = start;
121
122         /* Traverse the environment. */
123         for (ep = environ; ep != NULL; ep = ep->ev_next) {
124                 len = strlen(ep->ev_name);
125                 if (archsw.arch_copyin(ep->ev_name, addr, len) != len)
126                         break;
127                 addr += len;
128                 if (archsw.arch_copyin("=", addr, 1) != 1)
129                         break;
130                 addr++;
131                 if (ep->ev_value != NULL) {
132                         len = strlen(ep->ev_value);
133                         if (archsw.arch_copyin(ep->ev_value, addr, len) != len)
134                                 break;
135                         addr += len;
136                 }
137                 if (archsw.arch_copyin("", addr, 1) != 1)
138                         break;
139                 last = ++addr;
140         }
141
142         if (archsw.arch_copyin("", last++, 1) != 1)
143                 last = start;
144         return(last);
145 }
146
147 /*
148  * Copy module-related data into the load area, where it can be
149  * used as a directory for loaded modules.
150  *
151  * Module data is presented in a self-describing format.  Each datum
152  * is preceded by a 32-bit identifier and a 32-bit size field.
153  *
154  * Currently, the following data are saved:
155  *
156  * MOD_NAME     (variable)              module name (string)
157  * MOD_TYPE     (variable)              module type (string)
158  * MOD_ARGS     (variable)              module parameters (string)
159  * MOD_ADDR     sizeof(vm_offset_t)     module load address
160  * MOD_SIZE     sizeof(size_t)          module size
161  * MOD_METADATA (variable)              type-specific metadata
162  */
163 #define COPY32(v, a, c) {                                       \
164         uint32_t x = (v);                                       \
165         if (c)                                                  \
166                 archsw.arch_copyin(&x, a, sizeof(x));           \
167         a += sizeof(x);                                         \
168 }
169
170 #define MOD_STR(t, a, s, c) {                                   \
171         COPY32(t, a, c);                                        \
172         COPY32(strlen(s) + 1, a, c);                            \
173         if (c)                                                  \
174                 archsw.arch_copyin(s, a, strlen(s) + 1);        \
175         a += roundup(strlen(s) + 1, sizeof(u_long));            \
176 }
177
178 #define MOD_NAME(a, s, c)       MOD_STR(MODINFO_NAME, a, s, c)
179 #define MOD_TYPE(a, s, c)       MOD_STR(MODINFO_TYPE, a, s, c)
180 #define MOD_ARGS(a, s, c)       MOD_STR(MODINFO_ARGS, a, s, c)
181
182 #define MOD_VAR(t, a, s, c) {                                   \
183         COPY32(t, a, c);                                        \
184         COPY32(sizeof(s), a, c);                                \
185         if (c)                                                  \
186                 archsw.arch_copyin(&s, a, sizeof(s));           \
187         a += roundup(sizeof(s), sizeof(u_long));                \
188 }
189
190 #define MOD_ADDR(a, s, c)       MOD_VAR(MODINFO_ADDR, a, s, c)
191 #define MOD_SIZE(a, s, c)       MOD_VAR(MODINFO_SIZE, a, s, c)
192
193 #define MOD_METADATA(a, mm, c) {                                \
194         COPY32(MODINFO_METADATA | mm->md_type, a, c);           \
195         COPY32(mm->md_size, a, c);                              \
196         if (c)                                                  \
197                 archsw.arch_copyin(mm->md_data, a, mm->md_size);        \
198         a += roundup(mm->md_size, sizeof(u_long));              \
199 }
200
201 #define MOD_END(a, c) {                                         \
202         COPY32(MODINFO_END, a, c);                              \
203         COPY32(0, a, c);                                        \
204 }
205
206 static vm_offset_t
207 bi_copymodules(vm_offset_t addr)
208 {
209         struct preloaded_file *fp;
210         struct file_metadata *md;
211         int c;
212         uint64_t v;
213
214         c = addr != 0;
215         /* Start with the first module on the list, should be the kernel. */
216         for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
217                 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
218                 MOD_TYPE(addr, fp->f_type, c);
219                 if (fp->f_args)
220                         MOD_ARGS(addr, fp->f_args, c);
221                 v = fp->f_addr;
222 #if defined(__arm__)
223                 v -= __elfN(relocation_offset);
224 #endif
225                 MOD_ADDR(addr, v, c);
226                 v = fp->f_size;
227                 MOD_SIZE(addr, v, c);
228                 for (md = fp->f_metadata; md != NULL; md = md->md_next)
229                         if (!(md->md_type & MODINFOMD_NOCOPY))
230                                 MOD_METADATA(addr, md, c);
231         }
232         MOD_END(addr, c);
233         return(addr);
234 }
235
236 static int
237 bi_load_efi_data(struct preloaded_file *kfp)
238 {
239         EFI_MEMORY_DESCRIPTOR *mm;
240         EFI_PHYSICAL_ADDRESS addr;
241         EFI_STATUS status;
242         size_t efisz;
243         UINTN mmsz, pages, sz;
244         UINT32 mmver;
245         struct efi_map_header *efihdr;
246
247 #if defined(__amd64__)
248         struct efi_fb efifb;
249
250         if (efi_find_framebuffer(&efifb) == 0) {
251                 printf("EFI framebuffer information:\n");
252                 printf("addr, size     0x%lx, 0x%lx\n", efifb.fb_addr,
253                     efifb.fb_size);
254                 printf("dimensions     %d x %d\n", efifb.fb_width,
255                     efifb.fb_height);
256                 printf("stride         %d\n", efifb.fb_stride);
257                 printf("masks          0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
258                     efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
259                     efifb.fb_mask_reserved);
260
261                 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
262         }
263 #endif
264
265         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
266
267         /*
268          * Allocate enough pages to hold the bootinfo block and the memory
269          * map EFI will return to us. The memory map has an unknown size,
270          * so we have to determine that first. Note that the AllocatePages
271          * call can itself modify the memory map, so we have to take that
272          * into account as well. The changes to the memory map are caused
273          * by splitting a range of free memory into two (AFAICT), so that
274          * one is marked as being loader data.
275          */
276         sz = 0;
277         BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
278         sz += mmsz;
279         sz = (sz + 0xf) & ~0xf;
280         pages = EFI_SIZE_TO_PAGES(sz + efisz);
281         status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
282             &addr);
283         if (EFI_ERROR(status)) {
284                 printf("%s: AllocatePages() returned 0x%lx\n", __func__,
285                     (long)status);
286                 return (ENOMEM);
287         }
288
289         /*
290          * Read the memory map and stash it after bootinfo. Align the
291          * memory map on a 16-byte boundary (the bootinfo block is page
292          * aligned).
293          */
294         efihdr = (struct efi_map_header *)addr;
295         mm = (void *)((uint8_t *)efihdr + efisz);
296         sz = (EFI_PAGE_SIZE * pages) - efisz;
297         status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
298         if (EFI_ERROR(status)) {
299                 printf("%s: GetMemoryMap() returned 0x%lx\n", __func__,
300                     (long)status);
301                 return (EINVAL);
302         }
303
304         efihdr->memory_size = sz;
305         efihdr->descriptor_size = mmsz;
306         efihdr->descriptor_version = mmver;
307
308         file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz, efihdr);
309
310         return (0);
311 }
312
313 /*
314  * Load the information expected by an amd64 kernel.
315  *
316  * - The 'boothowto' argument is constructed.
317  * - The 'bootdev' argument is constructed.
318  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
319  * - The kernel environment is copied into kernel space.
320  * - Module metadata are formatted and placed in kernel space.
321  */
322 int
323 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
324 {
325         struct preloaded_file *xp, *kfp;
326         struct devdesc *rootdev;
327         struct file_metadata *md;
328         vm_offset_t addr;
329         uint64_t kernend;
330         uint64_t envp;
331         vm_offset_t size;
332         char *rootdevname;
333         int howto;
334 #if defined(LOADER_FDT_SUPPORT)
335         vm_offset_t dtbp;
336         int dtb_size;
337 #endif
338 #if defined(__arm__)
339         vm_offset_t vaddr;
340         int i;
341         /*
342          * These metadata addreses must be converted for kernel after
343          * relocation.
344          */
345         uint32_t                mdt[] = {
346             MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
347             MODINFOMD_ENVP,
348 #if defined(LOADER_FDT_SUPPORT)
349             MODINFOMD_DTBP
350 #endif
351         };
352 #endif
353
354         howto = bi_getboothowto(args);
355
356         /*
357          * Allow the environment variable 'rootdev' to override the supplied
358          * device. This should perhaps go to MI code and/or have $rootdev
359          * tested/set by MI code before launching the kernel.
360          */
361         rootdevname = getenv("rootdev");
362         archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
363         if (rootdev == NULL) {
364                 printf("Can't determine root device.\n");
365                 return(EINVAL);
366         }
367
368         /* Try reading the /etc/fstab file to select the root device */
369         getrootmount(efi_fmtdev((void *)rootdev));
370
371         addr = 0;
372         for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
373                 if (addr < (xp->f_addr + xp->f_size))
374                         addr = xp->f_addr + xp->f_size;
375         }
376
377         /* Pad to a page boundary. */
378         addr = roundup(addr, PAGE_SIZE);
379
380         /* Copy our environment. */
381         envp = addr;
382         addr = bi_copyenv(addr);
383
384         /* Pad to a page boundary. */
385         addr = roundup(addr, PAGE_SIZE);
386
387 #if defined(LOADER_FDT_SUPPORT)
388         /* Handle device tree blob */
389         dtbp = addr;
390         dtb_size = fdt_copy(addr);
391                 
392         /* Pad to a page boundary */
393         if (dtb_size)
394                 addr += roundup(dtb_size, PAGE_SIZE);
395 #endif
396
397         kfp = file_findfile(NULL, "elf kernel");
398         if (kfp == NULL)
399                 kfp = file_findfile(NULL, "elf64 kernel");
400         if (kfp == NULL)
401                 panic("can't find kernel file");
402         kernend = 0;    /* fill it in later */
403         file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
404         file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
405 #if defined(LOADER_FDT_SUPPORT)
406         if (dtb_size)
407                 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
408         else
409                 pager_output("WARNING! Trying to fire up the kernel, but no "
410                     "device tree blob found!\n");
411 #endif
412         file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
413
414         bi_load_efi_data(kfp);
415
416         /* Figure out the size and location of the metadata. */
417         *modulep = addr;
418         size = bi_copymodules(0);
419         kernend = roundup(addr + size, PAGE_SIZE);
420         *kernendp = kernend;
421
422         /* patch MODINFOMD_KERNEND */
423         md = file_findmetadata(kfp, MODINFOMD_KERNEND);
424         bcopy(&kernend, md->md_data, sizeof kernend);
425
426 #if defined(__arm__)
427         *modulep -= __elfN(relocation_offset);
428
429         /* Do relocation fixup on metadata of each module. */
430         for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
431                 for (i = 0; i < sizeof mdt / sizeof mdt[0]; i++) {
432                         md = file_findmetadata(xp, mdt[i]);
433                         if (md) {
434                                 bcopy(md->md_data, &vaddr, sizeof vaddr);
435                                 vaddr -= __elfN(relocation_offset);
436                                 bcopy(&vaddr, md->md_data, sizeof vaddr);
437                         }
438                 }
439         }
440 #endif
441
442         /* Copy module list and metadata. */
443         (void)bi_copymodules(addr);
444
445         return (0);
446 }