Path: blob/main/stand/efi/loader/arch/riscv/exec.c
34889 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"4243static void44riscv_set_boot_hart(struct preloaded_file *fp)45{46EFI_GUID riscvboot = RISCV_EFI_BOOT_PROTOCOL_GUID;47RISCV_EFI_BOOT_PROTOCOL *proto;48EFI_STATUS status = 0;49uint64_t boot_hartid = ULONG_MAX;5051status = BS->LocateProtocol(&riscvboot, NULL, (void **)&proto);52if (EFI_ERROR(status)) {53return;54}5556status = proto->GetBootHartId(proto, &boot_hartid);57if (EFI_ERROR(status)) {58return;59}6061file_addmetadata(fp, MODINFOMD_BOOT_HARTID, sizeof(boot_hartid),62&boot_hartid);63}6465static int66__elfN(exec)(struct preloaded_file *fp)67{68struct file_metadata *fmp;69vm_offset_t modulep, kernend;70Elf_Ehdr *e;71int error;72void (*entry)(void *);7374if ((fmp = file_findmetadata(fp, MODINFOMD_ELFHDR)) == NULL)75return (EFTYPE);7677riscv_set_boot_hart(fp);7879e = (Elf_Ehdr *)&fmp->md_data;8081efi_time_fini();8283entry = efi_translate(e->e_entry);8485printf("Kernel entry at %p...\n", entry);86printf("Kernel args: %s\n", fp->f_args);8788/*89* we have to cleanup here because net_cleanup() doesn't work after90* we call ExitBootServices91*/92dev_cleanup();9394if ((error = bi_load(fp->f_args, &modulep, &kernend, true)) != 0) {95efi_time_init();96return (error);97}9899(*entry)((void *)modulep);100panic("exec returned");101}102103static struct file_format riscv_elf = {104__elfN(loadfile),105__elfN(exec)106};107108struct file_format *file_formats[] = {109&riscv_elf,110NULL111};112113114