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