Home | History | Annotate | Line # | Download | only in efiboot
      1 /* $NetBSD: exec.c,v 1.26 2024/09/19 06:26:11 mlelstv Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2019 Jason R. Thorpe
      5  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  */
     29 
     30 #include "efiboot.h"
     31 #ifdef EFIBOOT_FDT
     32 #include "efifdt.h"
     33 #endif
     34 #ifdef EFIBOOT_ACPI
     35 #include "efiacpi.h"
     36 #endif
     37 #include "efirng.h"
     38 #include "module.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/reboot.h>
     42 
     43 extern char twiddle_toggle;
     44 
     45 u_long load_offset = 0;
     46 
     47 EFI_PHYSICAL_ADDRESS efirng_addr;
     48 u_long efirng_size = 0;
     49 
     50 int
     51 load_file(const char *path, u_long extra, bool quiet_errors,
     52     EFI_PHYSICAL_ADDRESS *paddr, u_long *psize)
     53 {
     54 	EFI_STATUS status;
     55 	struct stat st;
     56 	ssize_t len;
     57 	ssize_t expectedlen;
     58 	int fd;
     59 
     60 	if (strlen(path) == 0)
     61 		return 0;
     62 
     63 	fd = open(path, 0);
     64 	if (fd < 0) {
     65 		if (!quiet_errors) {
     66 			printf("boot: failed to open %s: %s\n", path,
     67 			    strerror(errno));
     68 		}
     69 		return errno;
     70 	}
     71 	if (fstat(fd, &st) < 0) {
     72 		printf("boot: failed to fstat %s: %s\n", path, strerror(errno));
     73 		close(fd);
     74 		return errno;
     75 	}
     76 	if (st.st_size == 0) {
     77 		if (!quiet_errors) {
     78 			printf("boot: empty file %s\n", path);
     79 		}
     80 		close(fd);
     81 		return EINVAL;
     82 	}
     83 
     84 	expectedlen = st.st_size;
     85 	*psize = st.st_size + extra;
     86 
     87 #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
     88 	*paddr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
     89 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
     90 	    EFI_SIZE_TO_PAGES(*psize), paddr);
     91 #else
     92 	*paddr = 0;
     93 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
     94 	    EFI_SIZE_TO_PAGES(*psize), paddr);
     95 #endif
     96 	if (EFI_ERROR(status)) {
     97 		printf("Failed to allocate %lu bytes for %s (error %lu)\n",
     98 		    *psize, path, (u_long)status);
     99 		close(fd);
    100 		*paddr = 0;
    101 		return ENOMEM;
    102 	}
    103 
    104 	printf("boot: loading %s ", path);
    105 	len = read(fd, (void *)(uintptr_t)*paddr, expectedlen);
    106 	close(fd);
    107 
    108 	if (len != expectedlen) {
    109 		if (len < 0) {
    110 			printf(": %s\n", strerror(errno));
    111 		} else {
    112 			printf(": returned %zd (expected %zd)\n", len,
    113 			    expectedlen);
    114 		}
    115 		return EIO;
    116 	}
    117 
    118 	printf("done.\n");
    119 
    120 	efi_dcache_flush(*paddr, *psize);
    121 
    122 	return 0;
    123 }
    124 
    125 static void
    126 generate_efirng(void)
    127 {
    128 	EFI_PHYSICAL_ADDRESS addr;
    129 	u_long size = EFI_PAGE_SIZE;
    130 	EFI_STATUS status;
    131 
    132 	/* Check whether the RNG is available before bothering.  */
    133 	if (!efi_rng_available())
    134 		return;
    135 
    136 	/*
    137 	 * Allocate a page.  This is the smallest unit we can pass into
    138 	 * the kernel conveniently.
    139 	 */
    140 #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
    141 	addr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
    142 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
    143 	    EfiLoaderData, EFI_SIZE_TO_PAGES(size), &addr);
    144 #else
    145 	addr = 0;
    146 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages,
    147 	    EfiLoaderData, EFI_SIZE_TO_PAGES(size), &addr);
    148 #endif
    149 	if (EFI_ERROR(status)) {
    150 		Print(L"Failed to allocate page for EFI RNG output: %r\n",
    151 		    status);
    152 		return;
    153 	}
    154 
    155 	/* Fill the page with whatever the EFI RNG will do.  */
    156 	if (efi_rng((void *)(uintptr_t)addr, size)) {
    157 		uefi_call_wrapper(BS->FreePages, 2, addr,
    158 		    EFI_SIZE_TO_PAGES(size));
    159 		return;
    160 	}
    161 
    162 	/* Success!  */
    163 	efirng_addr = addr;
    164 	efirng_size = size;
    165 }
    166 
    167 int
    168 exec_netbsd(const char *fname, const char *args)
    169 {
    170 	EFI_PHYSICAL_ADDRESS addr;
    171 	u_long marks[MARK_MAX], alloc_size;
    172 	EFI_STATUS status;
    173 	int fd, ohowto;
    174 
    175 	twiddle_toggle = 0;
    176 
    177 	generate_efirng();
    178 
    179 	memset(marks, 0, sizeof(marks));
    180 	ohowto = howto;
    181 	howto |= AB_SILENT;
    182 	fd = loadfile(fname, marks, COUNT_KERNEL | LOAD_NOTE);
    183 	howto = ohowto;
    184 	if (fd < 0) {
    185 		printf("boot: %s: %s\n", fname, strerror(errno));
    186 		return EIO;
    187 	}
    188 	close(fd);
    189 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) & -sizeof(int);
    190 	alloc_size = marks[MARK_END] - marks[MARK_START] + efi_fdt_alloc_size() + EFIBOOT_ALIGN;
    191 
    192 #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
    193 	addr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
    194 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
    195 	    EFI_SIZE_TO_PAGES(alloc_size), &addr);
    196 #else
    197 	addr = 0;
    198 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
    199 	    EFI_SIZE_TO_PAGES(alloc_size), &addr);
    200 #endif
    201 	if (EFI_ERROR(status)) {
    202 		printf("Failed to allocate %lu bytes for kernel image (error %lu)\n",
    203 		    alloc_size, (u_long)status);
    204 		return ENOMEM;
    205 	}
    206 
    207 	memset(marks, 0, sizeof(marks));
    208 	load_offset = (addr + EFIBOOT_ALIGN - 1) & -EFIBOOT_ALIGN;
    209 	fd = loadfile(fname, marks, LOAD_KERNEL);
    210 	if (fd < 0) {
    211 		printf("boot: %s: %s\n", fname, strerror(errno));
    212 		goto cleanup;
    213 	}
    214 	close(fd);
    215 	load_offset = 0;
    216 
    217 	if (efi_fdt_prepare_boot(fname, args, marks) != 0) {
    218 		goto cleanup;
    219 	}
    220 
    221 	efi_boot_kernel(marks);
    222 
    223 	/* This should not happen.. */
    224 	printf("boot returned\n");
    225 
    226 cleanup:
    227 	uefi_call_wrapper(BS->FreePages, 2, addr, EFI_SIZE_TO_PAGES(alloc_size));
    228 	efi_fdt_cleanup_boot();
    229 
    230 	return EIO;
    231 }
    232