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