1 /*-
2 * Copyright (c) 2001 Benno Rice <benno@FreeBSD.org>
3 * Copyright (c) 2007 Semihalf, Rafal Jaworowski <raj@semihalf.com>
4 * All rights reserved.
5 * Copyright (c) 2024 The FreeBSD Foundation
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 AUTHOR 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 AUTHOR 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 <sys/param.h>
30 #include <sys/linker.h>
31
32 #include <machine/md_var.h>
33 #include <machine/metadata.h>
34 #include <machine/elf.h>
35
36 #include <stand.h>
37
38 #include <efi.h>
39 #include <efilib.h>
40
41 #include "bootstrap.h"
42 #include "loader_efi.h"
43
44 #include <Uefi.h>
45 #include <Protocol/RiscVBootProtocol.h>
46
47 static void
riscv_set_boot_hart(struct preloaded_file * fp)48 riscv_set_boot_hart(struct preloaded_file *fp)
49 {
50 // No #define in EDK2 for this
51 EFI_GUID riscvboot = { 0xccd15fec, 0x6f73, 0x4eec, { 0x83, 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf }};
52 RISCV_EFI_BOOT_PROTOCOL *proto;
53 EFI_STATUS status = 0;
54 uint64_t boot_hartid = ULONG_MAX;
55
56 status = BS->LocateProtocol(&riscvboot, NULL, (void **)&proto);
57 if (EFI_ERROR(status)) {
58 return;
59 }
60
61 status = proto->GetBootHartId(proto, &boot_hartid);
62 if (EFI_ERROR(status)) {
63 return;
64 }
65
66 file_addmetadata(fp, MODINFOMD_BOOT_HARTID, sizeof(boot_hartid),
67 &boot_hartid);
68 }
69
70 static int
__elfN(exec)71 __elfN(exec)(struct preloaded_file *fp)
72 {
73 struct file_metadata *fmp;
74 vm_offset_t modulep, kernend;
75 Elf_Ehdr *e;
76 int error;
77 void (*entry)(void *);
78
79 if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)
80 return (EFTYPE);
81
82 riscv_set_boot_hart(fp);
83
84 e = (Elf_Ehdr *)&fmp->md_data;
85
86 efi_time_fini();
87
88 entry = efi_translate(e->e_entry);
89
90 printf("Kernel entry at %p...\n", entry);
91 printf("Kernel args: %s\n", fp->f_args);
92
93 /*
94 * we have to cleanup here because net_cleanup() doesn't work after
95 * we call ExitBootServices
96 */
97 dev_cleanup();
98
99 if ((error = bi_load(fp->f_args, &modulep, &kernend, true)) != 0) {
100 efi_time_init();
101 return (error);
102 }
103
104 (*entry)((void *)modulep);
105 panic("exec returned");
106 }
107
108 static struct file_format riscv_elf = {
109 __elfN(loadfile),
110 __elfN(exec)
111 };
112
113 struct file_format *file_formats[] = {
114 &riscv_elf,
115 NULL
116 };
117