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