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 static void 45 riscv_set_boot_hart(struct preloaded_file *fp) 46 { 47 EFI_GUID riscvboot = RISCV_EFI_BOOT_PROTOCOL_GUID; 48 RISCV_EFI_BOOT_PROTOCOL *proto; 49 EFI_STATUS status = 0; 50 uint64_t boot_hartid = ULONG_MAX; 51 52 status = BS->LocateProtocol(&riscvboot, NULL, (void **)&proto); 53 if (EFI_ERROR(status)) { 54 return; 55 } 56 57 status = proto->GetBootHartId(proto, &boot_hartid); 58 if (EFI_ERROR(status)) { 59 return; 60 } 61 62 file_addmetadata(fp, MODINFOMD_BOOT_HARTID, sizeof(boot_hartid), 63 &boot_hartid); 64 } 65 66 static int 67 __elfN(exec)(struct preloaded_file *fp) 68 { 69 struct file_metadata *fmp; 70 vm_offset_t modulep, kernend; 71 Elf_Ehdr *e; 72 int error; 73 void (*entry)(void *); 74 75 if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL) 76 return (EFTYPE); 77 78 riscv_set_boot_hart(fp); 79 80 e = (Elf_Ehdr *)&fmp->md_data; 81 82 efi_time_fini(); 83 84 entry = efi_translate(e->e_entry); 85 86 printf("Kernel entry at %p...\n", entry); 87 printf("Kernel args: %s\n", fp->f_args); 88 89 /* 90 * we have to cleanup here because net_cleanup() doesn't work after 91 * we call ExitBootServices 92 */ 93 dev_cleanup(); 94 95 if ((error = bi_load(fp->f_args, &modulep, &kernend, true)) != 0) { 96 efi_time_init(); 97 return (error); 98 } 99 100 (*entry)((void *)modulep); 101 panic("exec returned"); 102 } 103 104 static struct file_format riscv_elf = { 105 __elfN(loadfile), 106 __elfN(exec) 107 }; 108 109 struct file_format *file_formats[] = { 110 &riscv_elf, 111 NULL 112 }; 113