Home | History | Annotate | Line # | Download | only in efiboot
exec.c revision 1.19.6.2
      1  1.19.6.2   thorpej /* $NetBSD: exec.c,v 1.19.6.2 2021/06/17 04:46:36 thorpej Exp $ */
      2       1.1  jmcneill 
      3       1.1  jmcneill /*-
      4      1.10   thorpej  * Copyright (c) 2019 Jason R. Thorpe
      5       1.1  jmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      6       1.1  jmcneill  * All rights reserved.
      7       1.1  jmcneill  *
      8       1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      9       1.1  jmcneill  * modification, are permitted provided that the following conditions
     10       1.1  jmcneill  * are met:
     11       1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     12       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     13       1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     14       1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     15       1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     16       1.1  jmcneill  *
     17       1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     18       1.1  jmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19       1.1  jmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20       1.1  jmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     21       1.1  jmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22       1.1  jmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23       1.1  jmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24       1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25       1.1  jmcneill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26       1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27       1.1  jmcneill  * SUCH DAMAGE.
     28       1.1  jmcneill  */
     29       1.1  jmcneill 
     30       1.1  jmcneill #include "efiboot.h"
     31       1.1  jmcneill #include "efifdt.h"
     32       1.7  jmcneill #include "efiacpi.h"
     33      1.14  riastrad #include "efirng.h"
     34      1.16  jmcneill #include "module.h"
     35      1.17   thorpej #include "overlay.h"
     36       1.1  jmcneill 
     37      1.16  jmcneill #include <sys/param.h>
     38       1.6  jmcneill #include <sys/reboot.h>
     39       1.1  jmcneill 
     40      1.13  jmcneill extern char twiddle_toggle;
     41      1.13  jmcneill 
     42       1.3  jmcneill u_long load_offset = 0;
     43       1.3  jmcneill 
     44       1.4  jmcneill #define	FDT_SPACE	(4 * 1024 * 1024)
     45  1.19.6.1   thorpej #define	FDT_ALIGN	(2 * 1024 * 1024)
     46       1.4  jmcneill 
     47      1.14  riastrad static EFI_PHYSICAL_ADDRESS initrd_addr, dtb_addr, rndseed_addr, efirng_addr;
     48      1.14  riastrad static u_long initrd_size = 0, dtb_size = 0, rndseed_size = 0, efirng_size = 0;
     49       1.4  jmcneill 
     50       1.4  jmcneill static int
     51      1.10   thorpej load_file(const char *path, u_long extra, bool quiet_errors,
     52      1.10   thorpej     EFI_PHYSICAL_ADDRESS *paddr, u_long *psize)
     53       1.4  jmcneill {
     54       1.4  jmcneill 	EFI_STATUS status;
     55       1.4  jmcneill 	struct stat st;
     56       1.4  jmcneill 	ssize_t len;
     57      1.10   thorpej 	ssize_t expectedlen;
     58       1.4  jmcneill 	int fd;
     59       1.4  jmcneill 
     60       1.4  jmcneill 	if (strlen(path) == 0)
     61       1.4  jmcneill 		return 0;
     62       1.4  jmcneill 
     63       1.4  jmcneill 	fd = open(path, 0);
     64       1.4  jmcneill 	if (fd < 0) {
     65      1.10   thorpej 		if (!quiet_errors) {
     66      1.10   thorpej 			printf("boot: failed to open %s: %s\n", path,
     67      1.10   thorpej 			    strerror(errno));
     68      1.10   thorpej 		}
     69       1.4  jmcneill 		return errno;
     70       1.4  jmcneill 	}
     71       1.4  jmcneill 	if (fstat(fd, &st) < 0) {
     72       1.4  jmcneill 		printf("boot: failed to fstat %s: %s\n", path, strerror(errno));
     73       1.4  jmcneill 		close(fd);
     74       1.4  jmcneill 		return errno;
     75       1.4  jmcneill 	}
     76       1.4  jmcneill 	if (st.st_size == 0) {
     77      1.10   thorpej 		if (!quiet_errors) {
     78      1.10   thorpej 			printf("boot: empty file %s\n", path);
     79      1.10   thorpej 		}
     80       1.4  jmcneill 		close(fd);
     81       1.4  jmcneill 		return EINVAL;
     82       1.4  jmcneill 	}
     83       1.4  jmcneill 
     84      1.10   thorpej 	expectedlen = st.st_size;
     85      1.10   thorpej 	*psize = st.st_size + extra;
     86       1.4  jmcneill 
     87       1.4  jmcneill #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
     88       1.5  jmcneill 	*paddr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
     89       1.4  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
     90       1.5  jmcneill 	    EFI_SIZE_TO_PAGES(*psize), paddr);
     91       1.4  jmcneill #else
     92       1.5  jmcneill 	*paddr = 0;
     93       1.4  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
     94       1.5  jmcneill 	    EFI_SIZE_TO_PAGES(*psize), paddr);
     95       1.4  jmcneill #endif
     96       1.4  jmcneill 	if (EFI_ERROR(status)) {
     97       1.5  jmcneill 		printf("Failed to allocate %lu bytes for %s (error %lu)\n",
     98       1.9  jmcneill 		    *psize, path, (u_long)status);
     99       1.4  jmcneill 		close(fd);
    100      1.10   thorpej 		*paddr = 0;
    101       1.4  jmcneill 		return ENOMEM;
    102       1.4  jmcneill 	}
    103       1.4  jmcneill 
    104       1.4  jmcneill 	printf("boot: loading %s ", path);
    105      1.10   thorpej 	len = read(fd, (void *)(uintptr_t)*paddr, expectedlen);
    106       1.4  jmcneill 	close(fd);
    107       1.4  jmcneill 
    108      1.10   thorpej 	if (len != expectedlen) {
    109      1.10   thorpej 		if (len < 0) {
    110       1.4  jmcneill 			printf(": %s\n", strerror(errno));
    111      1.10   thorpej 		} else {
    112      1.10   thorpej 			printf(": returned %ld (expected %ld)\n", len,
    113      1.10   thorpej 			    expectedlen);
    114      1.10   thorpej 		}
    115       1.4  jmcneill 		return EIO;
    116       1.4  jmcneill 	}
    117       1.4  jmcneill 
    118       1.4  jmcneill 	printf("done.\n");
    119       1.4  jmcneill 
    120       1.5  jmcneill 	efi_dcache_flush(*paddr, *psize);
    121       1.4  jmcneill 
    122       1.4  jmcneill 	return 0;
    123       1.4  jmcneill }
    124       1.4  jmcneill 
    125      1.10   thorpej static void
    126      1.17   thorpej apply_overlay(const char *path, void *dtbo)
    127      1.10   thorpej {
    128      1.10   thorpej 
    129      1.10   thorpej 	if (!efi_fdt_overlay_is_compatible(dtbo)) {
    130      1.17   thorpej 		printf("boot: %s: incompatible overlay\n", path);
    131      1.15   thorpej 		return;
    132      1.10   thorpej 	}
    133      1.10   thorpej 
    134      1.10   thorpej 	int fdterr;
    135      1.10   thorpej 
    136      1.10   thorpej 	if (efi_fdt_overlay_apply(dtbo, &fdterr) != 0) {
    137      1.17   thorpej 		printf("boot: %s: error %d applying overlay\n", path, fdterr);
    138      1.10   thorpej 	}
    139      1.10   thorpej }
    140      1.10   thorpej 
    141      1.10   thorpej static void
    142      1.10   thorpej apply_overlay_file(const char *path)
    143      1.10   thorpej {
    144      1.10   thorpej 	EFI_PHYSICAL_ADDRESS dtbo_addr;
    145      1.10   thorpej 	u_long dtbo_size;
    146      1.10   thorpej 
    147      1.10   thorpej 	if (strlen(path) == 0)
    148      1.10   thorpej 		return;
    149      1.10   thorpej 
    150      1.10   thorpej 	if (load_file(path, 0, false, &dtbo_addr, &dtbo_size) != 0 ||
    151      1.10   thorpej 	    dtbo_addr == 0) {
    152      1.10   thorpej 		/* Error messages have already been displayed. */
    153      1.10   thorpej 		goto out;
    154      1.10   thorpej 	}
    155      1.10   thorpej 
    156      1.17   thorpej 	apply_overlay(path, (void *)(uintptr_t)dtbo_addr);
    157      1.10   thorpej 
    158      1.10   thorpej out:
    159      1.10   thorpej 	if (dtbo_addr) {
    160      1.10   thorpej 		uefi_call_wrapper(BS->FreePages, 2, dtbo_addr,
    161      1.10   thorpej 		    EFI_SIZE_TO_PAGES(dtbo_size));
    162      1.10   thorpej 	}
    163      1.10   thorpej }
    164      1.10   thorpej 
    165      1.10   thorpej static void
    166      1.10   thorpej load_fdt_overlays(void)
    167      1.10   thorpej {
    168      1.17   thorpej 	if (!dtoverlay_enabled)
    169      1.10   thorpej 		return;
    170      1.10   thorpej 
    171      1.17   thorpej 	dtoverlay_foreach(apply_overlay_file);
    172      1.10   thorpej }
    173      1.10   thorpej 
    174      1.14  riastrad static void
    175      1.16  jmcneill load_module(const char *module_name)
    176      1.16  jmcneill {
    177      1.16  jmcneill 	EFI_PHYSICAL_ADDRESS addr;
    178      1.16  jmcneill 	u_long size;
    179      1.16  jmcneill 	char path[PATH_MAX];
    180      1.16  jmcneill 
    181      1.16  jmcneill 	snprintf(path, sizeof(path), "%s/%s/%s.kmod", module_prefix,
    182      1.16  jmcneill 	    module_name, module_name);
    183      1.16  jmcneill 
    184      1.16  jmcneill 	if (load_file(path, 0, false, &addr, &size) != 0 || addr == 0 || size == 0)
    185      1.16  jmcneill 		return;
    186      1.16  jmcneill 
    187      1.16  jmcneill 	efi_fdt_module(module_name, (u_long)addr, size);
    188      1.16  jmcneill }
    189      1.16  jmcneill 
    190      1.16  jmcneill static void
    191      1.16  jmcneill load_modules(const char *kernel_name)
    192      1.16  jmcneill {
    193      1.16  jmcneill 	if (!module_enabled)
    194      1.16  jmcneill 		return;
    195      1.16  jmcneill 
    196      1.16  jmcneill 	module_init(kernel_name);
    197      1.16  jmcneill 	module_foreach(load_module);
    198      1.16  jmcneill }
    199      1.16  jmcneill 
    200      1.16  jmcneill static void
    201      1.14  riastrad generate_efirng(void)
    202      1.14  riastrad {
    203      1.14  riastrad 	EFI_PHYSICAL_ADDRESS addr;
    204      1.14  riastrad 	u_long size = EFI_PAGE_SIZE;
    205      1.14  riastrad 	EFI_STATUS status;
    206      1.14  riastrad 
    207      1.14  riastrad 	/* Check whether the RNG is available before bothering.  */
    208      1.14  riastrad 	if (!efi_rng_available())
    209      1.14  riastrad 		return;
    210      1.14  riastrad 
    211      1.14  riastrad 	/*
    212      1.14  riastrad 	 * Allocate a page.  This is the smallest unit we can pass into
    213      1.14  riastrad 	 * the kernel conveniently.
    214      1.14  riastrad 	 */
    215      1.14  riastrad #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
    216      1.14  riastrad 	addr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
    217      1.14  riastrad 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
    218      1.14  riastrad 	    EfiLoaderData, EFI_SIZE_TO_PAGES(size), &addr);
    219      1.14  riastrad #else
    220      1.14  riastrad 	addr = 0;
    221      1.14  riastrad 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages,
    222      1.14  riastrad 	    EfiLoaderData, EFI_SIZE_TO_PAGES(size), &addr);
    223      1.14  riastrad #endif
    224      1.14  riastrad 	if (EFI_ERROR(status)) {
    225      1.14  riastrad 		Print(L"Failed to allocate page for EFI RNG output: %r\n",
    226      1.14  riastrad 		    status);
    227      1.14  riastrad 		return;
    228      1.14  riastrad 	}
    229      1.14  riastrad 
    230      1.14  riastrad 	/* Fill the page with whatever the EFI RNG will do.  */
    231      1.14  riastrad 	if (efi_rng((void *)(uintptr_t)addr, size)) {
    232      1.14  riastrad 		uefi_call_wrapper(BS->FreePages, 2, addr, size);
    233      1.14  riastrad 		return;
    234      1.14  riastrad 	}
    235      1.14  riastrad 
    236      1.14  riastrad 	/* Success!  */
    237      1.14  riastrad 	efirng_addr = addr;
    238      1.14  riastrad 	efirng_size = size;
    239      1.14  riastrad }
    240      1.14  riastrad 
    241       1.1  jmcneill int
    242       1.1  jmcneill exec_netbsd(const char *fname, const char *args)
    243       1.1  jmcneill {
    244       1.1  jmcneill 	EFI_PHYSICAL_ADDRESS addr;
    245       1.1  jmcneill 	u_long marks[MARK_MAX], alloc_size;
    246       1.1  jmcneill 	EFI_STATUS status;
    247       1.6  jmcneill 	int fd, ohowto;
    248       1.1  jmcneill 
    249      1.10   thorpej 	load_file(get_initrd_path(), 0, false, &initrd_addr, &initrd_size);
    250      1.10   thorpej 	load_file(get_dtb_path(), 0, false, &dtb_addr, &dtb_size);
    251      1.14  riastrad 	generate_efirng();
    252       1.4  jmcneill 
    253       1.1  jmcneill 	memset(marks, 0, sizeof(marks));
    254       1.6  jmcneill 	ohowto = howto;
    255       1.6  jmcneill 	howto |= AB_SILENT;
    256       1.1  jmcneill 	fd = loadfile(fname, marks, COUNT_KERNEL | LOAD_NOTE);
    257       1.6  jmcneill 	howto = ohowto;
    258       1.1  jmcneill 	if (fd < 0) {
    259       1.1  jmcneill 		printf("boot: %s: %s\n", fname, strerror(errno));
    260       1.1  jmcneill 		return EIO;
    261       1.1  jmcneill 	}
    262       1.1  jmcneill 	close(fd);
    263  1.19.6.1   thorpej 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) & -sizeof(int);
    264       1.4  jmcneill 	alloc_size = marks[MARK_END] - marks[MARK_START] + FDT_SPACE + EFIBOOT_ALIGN;
    265       1.1  jmcneill 
    266       1.1  jmcneill #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
    267       1.1  jmcneill 	addr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
    268       1.1  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
    269       1.1  jmcneill 	    EFI_SIZE_TO_PAGES(alloc_size), &addr);
    270       1.1  jmcneill #else
    271       1.1  jmcneill 	addr = 0;
    272       1.1  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
    273       1.1  jmcneill 	    EFI_SIZE_TO_PAGES(alloc_size), &addr);
    274       1.1  jmcneill #endif
    275       1.1  jmcneill 	if (EFI_ERROR(status)) {
    276       1.1  jmcneill 		printf("Failed to allocate %lu bytes for kernel image (error %lu)\n",
    277       1.9  jmcneill 		    alloc_size, (u_long)status);
    278       1.1  jmcneill 		return ENOMEM;
    279       1.1  jmcneill 	}
    280       1.1  jmcneill 
    281       1.1  jmcneill 	memset(marks, 0, sizeof(marks));
    282  1.19.6.1   thorpej 	load_offset = (addr + EFIBOOT_ALIGN - 1) & -EFIBOOT_ALIGN;
    283       1.1  jmcneill 	fd = loadfile(fname, marks, LOAD_KERNEL);
    284       1.1  jmcneill 	if (fd < 0) {
    285       1.1  jmcneill 		printf("boot: %s: %s\n", fname, strerror(errno));
    286       1.1  jmcneill 		goto cleanup;
    287       1.1  jmcneill 	}
    288       1.1  jmcneill 	close(fd);
    289       1.3  jmcneill 	load_offset = 0;
    290       1.1  jmcneill 
    291       1.7  jmcneill #ifdef EFIBOOT_ACPI
    292  1.19.6.2   thorpej 	/* ACPI support only works for little endian kernels */
    293  1.19.6.2   thorpej 	efi_acpi_enable(netbsd_elf_data == ELFDATA2LSB);
    294  1.19.6.2   thorpej 
    295  1.19.6.2   thorpej 	if (efi_acpi_available() && efi_acpi_enabled()) {
    296       1.7  jmcneill 		efi_acpi_create_fdt();
    297       1.7  jmcneill 	} else
    298       1.7  jmcneill #endif
    299       1.9  jmcneill 	if (dtb_addr && efi_fdt_set_data((void *)(uintptr_t)dtb_addr) != 0) {
    300       1.5  jmcneill 		printf("boot: invalid DTB data\n");
    301       1.5  jmcneill 		goto cleanup;
    302       1.5  jmcneill 	}
    303       1.5  jmcneill 
    304       1.1  jmcneill 	if (efi_fdt_size() > 0) {
    305      1.12  riastrad 		/*
    306      1.12  riastrad 		 * Load the rndseed as late as possible -- after we
    307      1.12  riastrad 		 * have committed to using fdt and executing this
    308      1.12  riastrad 		 * kernel -- so that it doesn't hang around in memory
    309      1.12  riastrad 		 * if we have to bail or the kernel won't use it.
    310      1.12  riastrad 		 */
    311      1.12  riastrad 		load_file(get_rndseed_path(), 0, false,
    312      1.12  riastrad 		    &rndseed_addr, &rndseed_size);
    313      1.12  riastrad 
    314  1.19.6.1   thorpej 		efi_fdt_init((marks[MARK_END] + FDT_ALIGN - 1) & -FDT_ALIGN, FDT_ALIGN);
    315      1.16  jmcneill 		load_modules(fname);
    316      1.10   thorpej 		load_fdt_overlays();
    317       1.4  jmcneill 		efi_fdt_initrd(initrd_addr, initrd_size);
    318      1.12  riastrad 		efi_fdt_rndseed(rndseed_addr, rndseed_size);
    319      1.14  riastrad 		efi_fdt_efirng(efirng_addr, efirng_size);
    320       1.2  jmcneill 		efi_fdt_bootargs(args);
    321      1.19  jmcneill 		efi_fdt_system_table();
    322      1.19  jmcneill 		efi_fdt_gop();
    323       1.4  jmcneill 		efi_fdt_memory_map();
    324       1.8  jmcneill 	}
    325       1.8  jmcneill 
    326       1.8  jmcneill 	efi_cleanup();
    327       1.8  jmcneill 
    328       1.8  jmcneill 	if (efi_fdt_size() > 0) {
    329       1.4  jmcneill 		efi_fdt_fini();
    330       1.1  jmcneill 	}
    331       1.1  jmcneill 
    332       1.1  jmcneill 	efi_boot_kernel(marks);
    333       1.1  jmcneill 
    334       1.1  jmcneill 	/* This should not happen.. */
    335       1.1  jmcneill 	printf("boot returned\n");
    336       1.1  jmcneill 
    337       1.1  jmcneill cleanup:
    338       1.1  jmcneill 	uefi_call_wrapper(BS->FreePages, 2, addr, EFI_SIZE_TO_PAGES(alloc_size));
    339       1.4  jmcneill 	if (initrd_addr) {
    340       1.4  jmcneill 		uefi_call_wrapper(BS->FreePages, 2, initrd_addr, EFI_SIZE_TO_PAGES(initrd_size));
    341       1.4  jmcneill 		initrd_addr = 0;
    342       1.4  jmcneill 		initrd_size = 0;
    343       1.4  jmcneill 	}
    344       1.5  jmcneill 	if (dtb_addr) {
    345       1.5  jmcneill 		uefi_call_wrapper(BS->FreePages, 2, dtb_addr, EFI_SIZE_TO_PAGES(dtb_size));
    346       1.5  jmcneill 		dtb_addr = 0;
    347       1.5  jmcneill 		dtb_size = 0;
    348       1.5  jmcneill 	}
    349       1.1  jmcneill 	return EIO;
    350       1.1  jmcneill }
    351