Home | History | Annotate | Line # | Download | only in efiboot
exec.c revision 1.7
      1  1.7  jmcneill /* $NetBSD: exec.c,v 1.7 2018/10/12 22:08:04 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  jmcneill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  jmcneill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  jmcneill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     20  1.1  jmcneill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  jmcneill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  jmcneill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  jmcneill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  jmcneill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include "efiboot.h"
     30  1.1  jmcneill #include "efifdt.h"
     31  1.7  jmcneill #include "efiacpi.h"
     32  1.1  jmcneill 
     33  1.6  jmcneill #include <sys/reboot.h>
     34  1.1  jmcneill 
     35  1.3  jmcneill u_long load_offset = 0;
     36  1.3  jmcneill 
     37  1.4  jmcneill #define	FDT_SPACE	(4 * 1024 * 1024)
     38  1.4  jmcneill #define	FDT_ALIGN	((2 * 1024 * 1024) - 1)
     39  1.4  jmcneill 
     40  1.5  jmcneill static EFI_PHYSICAL_ADDRESS initrd_addr, dtb_addr;
     41  1.5  jmcneill static u_long initrd_size = 0, dtb_size = 0;
     42  1.4  jmcneill 
     43  1.4  jmcneill static int
     44  1.5  jmcneill load_file(char *path, EFI_PHYSICAL_ADDRESS *paddr, u_long *psize)
     45  1.4  jmcneill {
     46  1.4  jmcneill 	EFI_STATUS status;
     47  1.4  jmcneill 	struct stat st;
     48  1.4  jmcneill 	ssize_t len;
     49  1.4  jmcneill 	int fd;
     50  1.4  jmcneill 
     51  1.4  jmcneill 	if (strlen(path) == 0)
     52  1.4  jmcneill 		return 0;
     53  1.4  jmcneill 
     54  1.4  jmcneill 	fd = open(path, 0);
     55  1.4  jmcneill 	if (fd < 0) {
     56  1.4  jmcneill 		printf("boot: failed to open %s: %s\n", path, strerror(errno));
     57  1.4  jmcneill 		return errno;
     58  1.4  jmcneill 	}
     59  1.4  jmcneill 	if (fstat(fd, &st) < 0) {
     60  1.4  jmcneill 		printf("boot: failed to fstat %s: %s\n", path, strerror(errno));
     61  1.4  jmcneill 		close(fd);
     62  1.4  jmcneill 		return errno;
     63  1.4  jmcneill 	}
     64  1.4  jmcneill 	if (st.st_size == 0) {
     65  1.5  jmcneill 		printf("boot: empty file %s\n", path);
     66  1.4  jmcneill 		close(fd);
     67  1.4  jmcneill 		return EINVAL;
     68  1.4  jmcneill 	}
     69  1.4  jmcneill 
     70  1.5  jmcneill 	*psize = st.st_size;
     71  1.4  jmcneill 
     72  1.4  jmcneill #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
     73  1.5  jmcneill 	*paddr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
     74  1.4  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
     75  1.5  jmcneill 	    EFI_SIZE_TO_PAGES(*psize), paddr);
     76  1.4  jmcneill #else
     77  1.5  jmcneill 	*paddr = 0;
     78  1.4  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
     79  1.5  jmcneill 	    EFI_SIZE_TO_PAGES(*psize), paddr);
     80  1.4  jmcneill #endif
     81  1.4  jmcneill 	if (EFI_ERROR(status)) {
     82  1.5  jmcneill 		printf("Failed to allocate %lu bytes for %s (error %lu)\n",
     83  1.5  jmcneill 		    *psize, path, status);
     84  1.4  jmcneill 		close(fd);
     85  1.4  jmcneill 		return ENOMEM;
     86  1.4  jmcneill 	}
     87  1.4  jmcneill 
     88  1.4  jmcneill 	printf("boot: loading %s ", path);
     89  1.5  jmcneill 	len = read(fd, (void *)*paddr, *psize);
     90  1.4  jmcneill 	close(fd);
     91  1.4  jmcneill 
     92  1.5  jmcneill 	if (len != *psize) {
     93  1.4  jmcneill 		if (len < 0)
     94  1.4  jmcneill 			printf(": %s\n", strerror(errno));
     95  1.4  jmcneill 		else
     96  1.5  jmcneill 			printf(": returned %ld (expected %ld)\n", len, *psize);
     97  1.4  jmcneill 		return EIO;
     98  1.4  jmcneill 	}
     99  1.4  jmcneill 
    100  1.4  jmcneill 	printf("done.\n");
    101  1.4  jmcneill 
    102  1.5  jmcneill 	efi_dcache_flush(*paddr, *psize);
    103  1.4  jmcneill 
    104  1.4  jmcneill 	return 0;
    105  1.4  jmcneill }
    106  1.4  jmcneill 
    107  1.1  jmcneill int
    108  1.1  jmcneill exec_netbsd(const char *fname, const char *args)
    109  1.1  jmcneill {
    110  1.1  jmcneill 	EFI_PHYSICAL_ADDRESS addr;
    111  1.1  jmcneill 	u_long marks[MARK_MAX], alloc_size;
    112  1.1  jmcneill 	EFI_STATUS status;
    113  1.6  jmcneill 	int fd, ohowto;
    114  1.1  jmcneill 
    115  1.5  jmcneill 	load_file(get_initrd_path(), &initrd_addr, &initrd_size);
    116  1.5  jmcneill 	load_file(get_dtb_path(), &dtb_addr, &dtb_size);
    117  1.4  jmcneill 
    118  1.1  jmcneill 	memset(marks, 0, sizeof(marks));
    119  1.6  jmcneill 	ohowto = howto;
    120  1.6  jmcneill 	howto |= AB_SILENT;
    121  1.1  jmcneill 	fd = loadfile(fname, marks, COUNT_KERNEL | LOAD_NOTE);
    122  1.6  jmcneill 	howto = ohowto;
    123  1.1  jmcneill 	if (fd < 0) {
    124  1.1  jmcneill 		printf("boot: %s: %s\n", fname, strerror(errno));
    125  1.1  jmcneill 		return EIO;
    126  1.1  jmcneill 	}
    127  1.1  jmcneill 	close(fd);
    128  1.1  jmcneill 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) & (-sizeof(int));
    129  1.4  jmcneill 	alloc_size = marks[MARK_END] - marks[MARK_START] + FDT_SPACE + EFIBOOT_ALIGN;
    130  1.1  jmcneill 
    131  1.1  jmcneill #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
    132  1.1  jmcneill 	addr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
    133  1.1  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
    134  1.1  jmcneill 	    EFI_SIZE_TO_PAGES(alloc_size), &addr);
    135  1.1  jmcneill #else
    136  1.1  jmcneill 	addr = 0;
    137  1.1  jmcneill 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
    138  1.1  jmcneill 	    EFI_SIZE_TO_PAGES(alloc_size), &addr);
    139  1.1  jmcneill #endif
    140  1.1  jmcneill 	if (EFI_ERROR(status)) {
    141  1.1  jmcneill 		printf("Failed to allocate %lu bytes for kernel image (error %lu)\n",
    142  1.1  jmcneill 		    alloc_size, status);
    143  1.1  jmcneill 		return ENOMEM;
    144  1.1  jmcneill 	}
    145  1.1  jmcneill 
    146  1.1  jmcneill 	memset(marks, 0, sizeof(marks));
    147  1.3  jmcneill 	load_offset = (addr + EFIBOOT_ALIGN) & ~(EFIBOOT_ALIGN - 1);
    148  1.1  jmcneill 	fd = loadfile(fname, marks, LOAD_KERNEL);
    149  1.1  jmcneill 	if (fd < 0) {
    150  1.1  jmcneill 		printf("boot: %s: %s\n", fname, strerror(errno));
    151  1.1  jmcneill 		goto cleanup;
    152  1.1  jmcneill 	}
    153  1.1  jmcneill 	close(fd);
    154  1.3  jmcneill 	load_offset = 0;
    155  1.1  jmcneill 
    156  1.7  jmcneill #ifdef EFIBOOT_ACPI
    157  1.7  jmcneill 	if (efi_acpi_available()) {
    158  1.7  jmcneill 		efi_acpi_create_fdt();
    159  1.7  jmcneill 	} else
    160  1.7  jmcneill #endif
    161  1.5  jmcneill 	if (dtb_addr && efi_fdt_set_data((void *)dtb_addr) != 0) {
    162  1.5  jmcneill 		printf("boot: invalid DTB data\n");
    163  1.5  jmcneill 		goto cleanup;
    164  1.5  jmcneill 	}
    165  1.5  jmcneill 
    166  1.1  jmcneill 	if (efi_fdt_size() > 0) {
    167  1.4  jmcneill 		efi_fdt_init((marks[MARK_END] + FDT_ALIGN) & ~FDT_ALIGN, FDT_ALIGN + 1);
    168  1.4  jmcneill 		efi_fdt_initrd(initrd_addr, initrd_size);
    169  1.2  jmcneill 		efi_fdt_bootargs(args);
    170  1.4  jmcneill 		efi_fdt_memory_map();
    171  1.4  jmcneill 		efi_fdt_fini();
    172  1.1  jmcneill 	}
    173  1.1  jmcneill 
    174  1.1  jmcneill 	efi_cleanup();
    175  1.1  jmcneill 	efi_boot_kernel(marks);
    176  1.1  jmcneill 
    177  1.1  jmcneill 	/* This should not happen.. */
    178  1.1  jmcneill 	printf("boot returned\n");
    179  1.1  jmcneill 
    180  1.1  jmcneill cleanup:
    181  1.1  jmcneill 	uefi_call_wrapper(BS->FreePages, 2, addr, EFI_SIZE_TO_PAGES(alloc_size));
    182  1.4  jmcneill 	if (initrd_addr) {
    183  1.4  jmcneill 		uefi_call_wrapper(BS->FreePages, 2, initrd_addr, EFI_SIZE_TO_PAGES(initrd_size));
    184  1.4  jmcneill 		initrd_addr = 0;
    185  1.4  jmcneill 		initrd_size = 0;
    186  1.4  jmcneill 	}
    187  1.5  jmcneill 	if (dtb_addr) {
    188  1.5  jmcneill 		uefi_call_wrapper(BS->FreePages, 2, dtb_addr, EFI_SIZE_TO_PAGES(dtb_size));
    189  1.5  jmcneill 		dtb_addr = 0;
    190  1.5  jmcneill 		dtb_size = 0;
    191  1.5  jmcneill 	}
    192  1.1  jmcneill 	return EIO;
    193  1.1  jmcneill }
    194