Path: blob/main/stand/efi/loader/arch/riscv/exec.c
104992 views
/*-1* Copyright (c) 2001 Benno Rice <[email protected]>2* Copyright (c) 2007 Semihalf, Rafal Jaworowski <[email protected]>3* All rights reserved.4* Copyright (c) 2024 The FreeBSD Foundation5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25* SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/linker.h>3031#include <machine/md_var.h>32#include <machine/metadata.h>33#include <machine/elf.h>3435#include <stand.h>3637#include <efi.h>38#include <efilib.h>3940#include "bootstrap.h"41#include "loader_efi.h"4243#include <Uefi.h>44#include <Protocol/RiscVBootProtocol.h>4546static void47riscv_set_boot_hart(struct preloaded_file *fp)48{49// No #define in EDK2 for this50EFI_GUID riscvboot = { 0xccd15fec, 0x6f73, 0x4eec, { 0x83, 0x95, 0x3e, 0x69, 0xe4, 0xb9, 0x40, 0xbf }};51RISCV_EFI_BOOT_PROTOCOL *proto;52EFI_STATUS status = 0;53uint64_t boot_hartid = ULONG_MAX;5455status = BS->LocateProtocol(&riscvboot, NULL, (void **)&proto);56if (EFI_ERROR(status)) {57return;58}5960status = proto->GetBootHartId(proto, &boot_hartid);61if (EFI_ERROR(status)) {62return;63}6465file_addmetadata(fp, MODINFOMD_BOOT_HARTID, sizeof(boot_hartid),66&boot_hartid);67}6869static int70__elfN(exec)(struct preloaded_file *fp)71{72struct file_metadata *fmp;73vm_offset_t modulep, kernend;74Elf_Ehdr *e;75int error;76void (*entry)(void *);7778if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)79return (EFTYPE);8081riscv_set_boot_hart(fp);8283e = (Elf_Ehdr *)&fmp->md_data;8485efi_time_fini();8687entry = efi_translate(e->e_entry);8889printf("Kernel entry at %p...\n", entry);90printf("Kernel args: %s\n", fp->f_args);9192/*93* we have to cleanup here because net_cleanup() doesn't work after94* we call ExitBootServices95*/96dev_cleanup();9798if ((error = bi_load(fp->f_args, &modulep, &kernend, true)) != 0) {99efi_time_init();100return (error);101}102103(*entry)((void *)modulep);104panic("exec returned");105}106107static struct file_format riscv_elf = {108.l_load = __elfN(loadfile),109.l_exec = __elfN(exec)110};111112struct file_format *file_formats[] = {113&riscv_elf,114NULL115};116117118