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