exec.c revision 1.3.2.2 1 1.3.2.2 pgoyette /* $NetBSD: exec.c,v 1.3.2.2 2018/09/06 06:56:47 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.2 pgoyette
32 1.3.2.2 pgoyette #include <loadfile.h>
33 1.3.2.2 pgoyette
34 1.3.2.2 pgoyette u_long load_offset = 0;
35 1.3.2.2 pgoyette
36 1.3.2.2 pgoyette int
37 1.3.2.2 pgoyette exec_netbsd(const char *fname, const char *args)
38 1.3.2.2 pgoyette {
39 1.3.2.2 pgoyette EFI_PHYSICAL_ADDRESS addr;
40 1.3.2.2 pgoyette u_long marks[MARK_MAX], alloc_size;
41 1.3.2.2 pgoyette EFI_STATUS status;
42 1.3.2.2 pgoyette int fd;
43 1.3.2.2 pgoyette
44 1.3.2.2 pgoyette memset(marks, 0, sizeof(marks));
45 1.3.2.2 pgoyette fd = loadfile(fname, marks, COUNT_KERNEL | LOAD_NOTE);
46 1.3.2.2 pgoyette if (fd < 0) {
47 1.3.2.2 pgoyette printf("boot: %s: %s\n", fname, strerror(errno));
48 1.3.2.2 pgoyette return EIO;
49 1.3.2.2 pgoyette }
50 1.3.2.2 pgoyette close(fd);
51 1.3.2.2 pgoyette marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) & (-sizeof(int));
52 1.3.2.2 pgoyette alloc_size = marks[MARK_END] - marks[MARK_START] + EFIBOOT_ALIGN;
53 1.3.2.2 pgoyette
54 1.3.2.2 pgoyette #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
55 1.3.2.2 pgoyette addr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
56 1.3.2.2 pgoyette status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
57 1.3.2.2 pgoyette EFI_SIZE_TO_PAGES(alloc_size), &addr);
58 1.3.2.2 pgoyette #else
59 1.3.2.2 pgoyette addr = 0;
60 1.3.2.2 pgoyette status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
61 1.3.2.2 pgoyette EFI_SIZE_TO_PAGES(alloc_size), &addr);
62 1.3.2.2 pgoyette #endif
63 1.3.2.2 pgoyette if (EFI_ERROR(status)) {
64 1.3.2.2 pgoyette printf("Failed to allocate %lu bytes for kernel image (error %lu)\n",
65 1.3.2.2 pgoyette alloc_size, status);
66 1.3.2.2 pgoyette return ENOMEM;
67 1.3.2.2 pgoyette }
68 1.3.2.2 pgoyette
69 1.3.2.2 pgoyette memset(marks, 0, sizeof(marks));
70 1.3.2.2 pgoyette load_offset = (addr + EFIBOOT_ALIGN) & ~(EFIBOOT_ALIGN - 1);
71 1.3.2.2 pgoyette fd = loadfile(fname, marks, LOAD_KERNEL);
72 1.3.2.2 pgoyette if (fd < 0) {
73 1.3.2.2 pgoyette printf("boot: %s: %s\n", fname, strerror(errno));
74 1.3.2.2 pgoyette goto cleanup;
75 1.3.2.2 pgoyette }
76 1.3.2.2 pgoyette close(fd);
77 1.3.2.2 pgoyette load_offset = 0;
78 1.3.2.2 pgoyette
79 1.3.2.2 pgoyette if (efi_fdt_size() > 0) {
80 1.3.2.2 pgoyette efi_fdt_bootargs(args);
81 1.3.2.2 pgoyette efi_fdt_memory_map();
82 1.3.2.2 pgoyette }
83 1.3.2.2 pgoyette
84 1.3.2.2 pgoyette efi_cleanup();
85 1.3.2.2 pgoyette efi_boot_kernel(marks);
86 1.3.2.2 pgoyette
87 1.3.2.2 pgoyette /* This should not happen.. */
88 1.3.2.2 pgoyette printf("boot returned\n");
89 1.3.2.2 pgoyette
90 1.3.2.2 pgoyette cleanup:
91 1.3.2.2 pgoyette uefi_call_wrapper(BS->FreePages, 2, addr, EFI_SIZE_TO_PAGES(alloc_size));
92 1.3.2.2 pgoyette return EIO;
93 1.3.2.2 pgoyette }
94