]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/boot/amd64/efi/bootinfo.c
MFC UEFI loader
[FreeBSD/stable/10.git] / sys / boot / amd64 / efi / 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/metadata.h>
40 #include <machine/psl.h>
41 #include <machine/specialreg.h>
42
43 #include <efi.h>
44 #include <efilib.h>
45
46 #include "bootstrap.h"
47 #include "framebuffer.h"
48 #include "x86_efi.h"
49
50 UINTN x86_efi_mapkey;
51
52 static const char howto_switches[] = "aCdrgDmphsv";
53 static int howto_masks[] = {
54         RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
55         RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
56 };
57
58 static int
59 bi_getboothowto(char *kargs)
60 {
61         const char *sw;
62         char *opts;
63         int howto, i;
64
65         howto = 0;
66
67         /* Get the boot options from the environment first. */
68         for (i = 0; howto_names[i].ev != NULL; i++) {
69                 if (getenv(howto_names[i].ev) != NULL)
70                         howto |= howto_names[i].mask;
71         }
72
73         /* Parse kargs */
74         if (kargs == NULL)
75                 return (howto);
76
77         opts = strchr(kargs, '-');
78         while (opts != NULL) {
79                 while (*(++opts) != '\0') {
80                         sw = strchr(howto_switches, *opts);
81                         if (sw == NULL)
82                                 break;
83                         howto |= howto_masks[sw - howto_switches];
84                 }
85                 opts = strchr(opts, '-');
86         }
87
88         return (howto);
89 }
90
91 /*
92  * Copy the environment into the load area starting at (addr).
93  * Each variable is formatted as <name>=<value>, with a single nul
94  * separating each variable, and a double nul terminating the environment.
95  */
96 static vm_offset_t
97 bi_copyenv(vm_offset_t start)
98 {
99         struct env_var *ep;
100         vm_offset_t addr, last;
101         size_t len;
102
103         addr = last = start;
104
105         /* Traverse the environment. */
106         for (ep = environ; ep != NULL; ep = ep->ev_next) {
107                 len = strlen(ep->ev_name);
108                 if (x86_efi_copyin(ep->ev_name, addr, len) != len)
109                         break;
110                 addr += len;
111                 if (x86_efi_copyin("=", addr, 1) != 1)
112                         break;
113                 addr++;
114                 if (ep->ev_value != NULL) {
115                         len = strlen(ep->ev_value);
116                         if (x86_efi_copyin(ep->ev_value, addr, len) != len)
117                                 break;
118                         addr += len;
119                 }
120                 if (x86_efi_copyin("", addr, 1) != 1)
121                         break;
122                 last = ++addr;
123         }
124
125         if (x86_efi_copyin("", last++, 1) != 1)
126                 last = start;
127         return(last);
128 }
129
130 /*
131  * Copy module-related data into the load area, where it can be
132  * used as a directory for loaded modules.
133  *
134  * Module data is presented in a self-describing format.  Each datum
135  * is preceded by a 32-bit identifier and a 32-bit size field.
136  *
137  * Currently, the following data are saved:
138  *
139  * MOD_NAME     (variable)              module name (string)
140  * MOD_TYPE     (variable)              module type (string)
141  * MOD_ARGS     (variable)              module parameters (string)
142  * MOD_ADDR     sizeof(vm_offset_t)     module load address
143  * MOD_SIZE     sizeof(size_t)          module size
144  * MOD_METADATA (variable)              type-specific metadata
145  */
146 #define COPY32(v, a, c) {                                       \
147         uint32_t x = (v);                                       \
148         if (c)                                                  \
149                 x86_efi_copyin(&x, a, sizeof(x));               \
150         a += sizeof(x);                                         \
151 }
152
153 #define MOD_STR(t, a, s, c) {                                   \
154         COPY32(t, a, c);                                        \
155         COPY32(strlen(s) + 1, a, c);                            \
156         if (c)                                                  \
157                 x86_efi_copyin(s, a, strlen(s) + 1);            \
158         a += roundup(strlen(s) + 1, sizeof(uint64_t));          \
159 }
160
161 #define MOD_NAME(a, s, c)       MOD_STR(MODINFO_NAME, a, s, c)
162 #define MOD_TYPE(a, s, c)       MOD_STR(MODINFO_TYPE, a, s, c)
163 #define MOD_ARGS(a, s, c)       MOD_STR(MODINFO_ARGS, a, s, c)
164
165 #define MOD_VAR(t, a, s, c) {                                   \
166         COPY32(t, a, c);                                        \
167         COPY32(sizeof(s), a, c);                                \
168         if (c)                                                  \
169                 x86_efi_copyin(&s, a, sizeof(s));               \
170         a += roundup(sizeof(s), sizeof(uint64_t));              \
171 }
172
173 #define MOD_ADDR(a, s, c)       MOD_VAR(MODINFO_ADDR, a, s, c)
174 #define MOD_SIZE(a, s, c)       MOD_VAR(MODINFO_SIZE, a, s, c)
175
176 #define MOD_METADATA(a, mm, c) {                                \
177         COPY32(MODINFO_METADATA | mm->md_type, a, c);           \
178         COPY32(mm->md_size, a, c);                              \
179         if (c)                                                  \
180                 x86_efi_copyin(mm->md_data, a, mm->md_size);    \
181         a += roundup(mm->md_size, sizeof(uint64_t));            \
182 }
183
184 #define MOD_END(a, c) {                                         \
185         COPY32(MODINFO_END, a, c);                              \
186         COPY32(0, a, c);                                        \
187 }
188
189 static vm_offset_t
190 bi_copymodules(vm_offset_t addr)
191 {
192         struct preloaded_file *fp;
193         struct file_metadata *md;
194         int c;
195         uint64_t v;
196
197         c = addr != 0;
198         /* Start with the first module on the list, should be the kernel. */
199         for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
200                 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
201                 MOD_TYPE(addr, fp->f_type, c);
202                 if (fp->f_args)
203                         MOD_ARGS(addr, fp->f_args, c);
204                 v = fp->f_addr;
205                 MOD_ADDR(addr, v, c);
206                 v = fp->f_size;
207                 MOD_SIZE(addr, v, c);
208                 for (md = fp->f_metadata; md != NULL; md = md->md_next)
209                         if (!(md->md_type & MODINFOMD_NOCOPY))
210                                 MOD_METADATA(addr, md, c);
211         }
212         MOD_END(addr, c);
213         return(addr);
214 }
215
216 static int
217 bi_load_efi_data(struct preloaded_file *kfp)
218 {
219         EFI_MEMORY_DESCRIPTOR *mm;
220         EFI_PHYSICAL_ADDRESS addr;
221         EFI_STATUS status;
222         size_t efisz;
223         UINTN mmsz, pages, sz;
224         UINT32 mmver;
225         struct efi_map_header *efihdr;
226         struct efi_fb efifb;
227
228         if (efi_find_framebuffer(&efifb) == 0)
229                 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
230
231         efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
232
233         /*
234          * Allocate enough pages to hold the bootinfo block and the memory
235          * map EFI will return to us. The memory map has an unknown size,
236          * so we have to determine that first. Note that the AllocatePages
237          * call can itself modify the memory map, so we have to take that
238          * into account as well. The changes to the memory map are caused
239          * by splitting a range of free memory into two (AFAICT), so that
240          * one is marked as being loader data.
241          */
242         sz = 0;
243         BS->GetMemoryMap(&sz, NULL, &x86_efi_mapkey, &mmsz, &mmver);
244         sz += mmsz;
245         sz = (sz + 0xf) & ~0xf;
246         pages = EFI_SIZE_TO_PAGES(sz + efisz);
247         status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
248             &addr);
249         if (EFI_ERROR(status)) {
250                 printf("%s: AllocatePages() returned 0x%lx\n", __func__,
251                     (long)status);
252                 return (ENOMEM);
253         }
254
255         /*
256          * Read the memory map and stash it after bootinfo. Align the
257          * memory map on a 16-byte boundary (the bootinfo block is page
258          * aligned).
259          */
260         efihdr = (struct efi_map_header *)addr;
261         mm = (void *)((uint8_t *)efihdr + efisz);
262         sz = (EFI_PAGE_SIZE * pages) - efisz;
263         status = BS->GetMemoryMap(&sz, mm, &x86_efi_mapkey, &mmsz, &mmver);
264         if (EFI_ERROR(status)) {
265                 printf("%s: GetMemoryMap() returned 0x%lx\n", __func__,
266                     (long)status);
267                 return (EINVAL);
268         }
269
270         efihdr->memory_size = sz;
271         efihdr->descriptor_size = mmsz;
272         efihdr->descriptor_version = mmver;
273
274         file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz, efihdr);
275
276         return (0);
277 }
278
279 /*
280  * Load the information expected by an amd64 kernel.
281  *
282  * - The 'boothowto' argument is constructed.
283  * - The 'bootdev' argument is constructed.
284  * - The 'bootinfo' struct is constructed, and copied into the kernel space.
285  * - The kernel environment is copied into kernel space.
286  * - Module metadata are formatted and placed in kernel space.
287  */
288 int
289 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
290 {
291         struct preloaded_file *xp, *kfp;
292         struct devdesc *rootdev;
293         struct file_metadata *md;
294         vm_offset_t addr;
295         uint64_t kernend;
296         uint64_t envp;
297         vm_offset_t size;
298         char *rootdevname;
299         int howto;
300
301         howto = bi_getboothowto(args);
302
303         /*
304          * Allow the environment variable 'rootdev' to override the supplied
305          * device. This should perhaps go to MI code and/or have $rootdev
306          * tested/set by MI code before launching the kernel.
307          */
308         rootdevname = getenv("rootdev");
309         x86_efi_getdev((void**)(&rootdev), rootdevname, NULL);
310         if (rootdev == NULL) {
311                 printf("Can't determine root device.\n");
312                 return(EINVAL);
313         }
314
315         /* Try reading the /etc/fstab file to select the root device */
316         getrootmount(x86_efi_fmtdev((void *)rootdev));
317
318         addr = 0;
319         for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
320                 if (addr < (xp->f_addr + xp->f_size))
321                         addr = xp->f_addr + xp->f_size;
322         }
323
324         /* Pad to a page boundary. */
325         addr = roundup(addr, PAGE_SIZE);
326
327         /* Copy our environment. */
328         envp = addr;
329         addr = bi_copyenv(addr);
330
331         /* Pad to a page boundary. */
332         addr = roundup(addr, PAGE_SIZE);
333
334         kfp = file_findfile(NULL, "elf kernel");
335         if (kfp == NULL)
336                 kfp = file_findfile(NULL, "elf64 kernel");
337         if (kfp == NULL)
338                 panic("can't find kernel file");
339         kernend = 0;    /* fill it in later */
340         file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
341         file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
342         file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
343
344         bi_load_efi_data(kfp);
345
346         /* Figure out the size and location of the metadata. */
347         *modulep = addr;
348         size = bi_copymodules(0);
349         kernend = roundup(addr + size, PAGE_SIZE);
350         *kernendp = kernend;
351
352         /* patch MODINFOMD_KERNEND */
353         md = file_findmetadata(kfp, MODINFOMD_KERNEND);
354         bcopy(&kernend, md->md_data, sizeof kernend);
355
356         /* Copy module list and metadata. */
357         (void)bi_copymodules(addr);
358         
359         return (0);
360 }