Home | History | Annotate | Line # | Download | only in efiboot
exec.c revision 1.9
      1 /* $NetBSD: exec.c,v 1.9 2019/03/30 12:47:53 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      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 REGENTS 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 REGENTS 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 "efiboot.h"
     30 #include "efifdt.h"
     31 #include "efiacpi.h"
     32 
     33 #include <sys/reboot.h>
     34 
     35 u_long load_offset = 0;
     36 
     37 #define	FDT_SPACE	(4 * 1024 * 1024)
     38 #define	FDT_ALIGN	((2 * 1024 * 1024) - 1)
     39 
     40 static EFI_PHYSICAL_ADDRESS initrd_addr, dtb_addr;
     41 static u_long initrd_size = 0, dtb_size = 0;
     42 
     43 static int
     44 load_file(char *path, EFI_PHYSICAL_ADDRESS *paddr, u_long *psize)
     45 {
     46 	EFI_STATUS status;
     47 	struct stat st;
     48 	ssize_t len;
     49 	int fd;
     50 
     51 	if (strlen(path) == 0)
     52 		return 0;
     53 
     54 	fd = open(path, 0);
     55 	if (fd < 0) {
     56 		printf("boot: failed to open %s: %s\n", path, strerror(errno));
     57 		return errno;
     58 	}
     59 	if (fstat(fd, &st) < 0) {
     60 		printf("boot: failed to fstat %s: %s\n", path, strerror(errno));
     61 		close(fd);
     62 		return errno;
     63 	}
     64 	if (st.st_size == 0) {
     65 		printf("boot: empty file %s\n", path);
     66 		close(fd);
     67 		return EINVAL;
     68 	}
     69 
     70 	*psize = st.st_size;
     71 
     72 #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
     73 	*paddr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
     74 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
     75 	    EFI_SIZE_TO_PAGES(*psize), paddr);
     76 #else
     77 	*paddr = 0;
     78 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
     79 	    EFI_SIZE_TO_PAGES(*psize), paddr);
     80 #endif
     81 	if (EFI_ERROR(status)) {
     82 		printf("Failed to allocate %lu bytes for %s (error %lu)\n",
     83 		    *psize, path, (u_long)status);
     84 		close(fd);
     85 		return ENOMEM;
     86 	}
     87 
     88 	printf("boot: loading %s ", path);
     89 	len = read(fd, (void *)(uintptr_t)*paddr, *psize);
     90 	close(fd);
     91 
     92 	if (len != *psize) {
     93 		if (len < 0)
     94 			printf(": %s\n", strerror(errno));
     95 		else
     96 			printf(": returned %ld (expected %ld)\n", len, *psize);
     97 		return EIO;
     98 	}
     99 
    100 	printf("done.\n");
    101 
    102 	efi_dcache_flush(*paddr, *psize);
    103 
    104 	return 0;
    105 }
    106 
    107 int
    108 exec_netbsd(const char *fname, const char *args)
    109 {
    110 	EFI_PHYSICAL_ADDRESS addr;
    111 	u_long marks[MARK_MAX], alloc_size;
    112 	EFI_STATUS status;
    113 	int fd, ohowto;
    114 
    115 	load_file(get_initrd_path(), &initrd_addr, &initrd_size);
    116 	load_file(get_dtb_path(), &dtb_addr, &dtb_size);
    117 
    118 	memset(marks, 0, sizeof(marks));
    119 	ohowto = howto;
    120 	howto |= AB_SILENT;
    121 	fd = loadfile(fname, marks, COUNT_KERNEL | LOAD_NOTE);
    122 	howto = ohowto;
    123 	if (fd < 0) {
    124 		printf("boot: %s: %s\n", fname, strerror(errno));
    125 		return EIO;
    126 	}
    127 	close(fd);
    128 	marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) & (-sizeof(int));
    129 	alloc_size = marks[MARK_END] - marks[MARK_START] + FDT_SPACE + EFIBOOT_ALIGN;
    130 
    131 #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
    132 	addr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
    133 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
    134 	    EFI_SIZE_TO_PAGES(alloc_size), &addr);
    135 #else
    136 	addr = 0;
    137 	status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
    138 	    EFI_SIZE_TO_PAGES(alloc_size), &addr);
    139 #endif
    140 	if (EFI_ERROR(status)) {
    141 		printf("Failed to allocate %lu bytes for kernel image (error %lu)\n",
    142 		    alloc_size, (u_long)status);
    143 		return ENOMEM;
    144 	}
    145 
    146 	memset(marks, 0, sizeof(marks));
    147 	load_offset = (addr + EFIBOOT_ALIGN) & ~(EFIBOOT_ALIGN - 1);
    148 	fd = loadfile(fname, marks, LOAD_KERNEL);
    149 	if (fd < 0) {
    150 		printf("boot: %s: %s\n", fname, strerror(errno));
    151 		goto cleanup;
    152 	}
    153 	close(fd);
    154 	load_offset = 0;
    155 
    156 #ifdef EFIBOOT_ACPI
    157 	if (efi_acpi_available()) {
    158 		efi_acpi_create_fdt();
    159 	} else
    160 #endif
    161 	if (dtb_addr && efi_fdt_set_data((void *)(uintptr_t)dtb_addr) != 0) {
    162 		printf("boot: invalid DTB data\n");
    163 		goto cleanup;
    164 	}
    165 
    166 	if (efi_fdt_size() > 0) {
    167 		efi_fdt_init((marks[MARK_END] + FDT_ALIGN) & ~FDT_ALIGN, FDT_ALIGN + 1);
    168 		efi_fdt_initrd(initrd_addr, initrd_size);
    169 		efi_fdt_bootargs(args);
    170 		efi_fdt_memory_map();
    171 	}
    172 
    173 	efi_cleanup();
    174 
    175 	if (efi_fdt_size() > 0) {
    176 		efi_fdt_fini();
    177 	}
    178 
    179 	efi_boot_kernel(marks);
    180 
    181 	/* This should not happen.. */
    182 	printf("boot returned\n");
    183 
    184 cleanup:
    185 	uefi_call_wrapper(BS->FreePages, 2, addr, EFI_SIZE_TO_PAGES(alloc_size));
    186 	if (initrd_addr) {
    187 		uefi_call_wrapper(BS->FreePages, 2, initrd_addr, EFI_SIZE_TO_PAGES(initrd_size));
    188 		initrd_addr = 0;
    189 		initrd_size = 0;
    190 	}
    191 	if (dtb_addr) {
    192 		uefi_call_wrapper(BS->FreePages, 2, dtb_addr, EFI_SIZE_TO_PAGES(dtb_size));
    193 		dtb_addr = 0;
    194 		dtb_size = 0;
    195 	}
    196 	return EIO;
    197 }
    198