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