Path: blob/main/stand/efi/loader/arch/riscv/start.S
34889 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2020 Mitchell Horne <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <machine/asm.h>2829/*30* We need to be a PE32+ file for EFI. On some architectures we can use31* objcopy to create the correct file, however on RISC-V we need to do32* it ourselves.33*/3435#define IMAGE_FILE_MACHINE_RISCV64 0x50643637#define IMAGE_SCN_CNT_CODE 0x0000002038#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x0000004039#define IMAGE_SCN_MEM_DISCARDABLE 0x0200000040#define IMAGE_SCN_MEM_EXECUTE 0x2000000041#define IMAGE_SCN_MEM_READ 0x400000004243.section .peheader,"a"44efi_start:45/* The MS-DOS Stub, only used to get the offset of the COFF header */46.ascii "MZ"47.short 048.space 0x3849.long pe_sig - efi_start5051/* The PE32 Signature. Needs to be 8-byte aligned */52.align 353pe_sig:54.ascii "PE"55.short 056coff_head:57.short IMAGE_FILE_MACHINE_RISCV64 /* RISC-V 64 file */58.short 2 /* 2 Sections */59.long 0 /* Timestamp */60.long 0 /* No symbol table */61.long 0 /* No symbols */62.short section_table - optional_header /* Optional header size */63.short 0 /* Characteristics TODO: Fill in */6465optional_header:66.short 0x020b /* PE32+ (64-bit addressing) */67.byte 0 /* Major linker version */68.byte 0 /* Minor linker version */69.long _edata - _end_header /* Code size */70.long 0 /* No initialized data */71.long 0 /* No uninitialized data */72.long _start - efi_start /* Entry point */73.long _end_header - efi_start /* Start of code */7475optional_windows_header:76.quad 0 /* Image base */77.long 32 /* Section Alignment */78.long 8 /* File alignment */79.short 0 /* Major OS version */80.short 0 /* Minor OS version */81.short 0 /* Major image version */82.short 0 /* Minor image version */83.short 0 /* Major subsystem version */84.short 0 /* Minor subsystem version */85.long 0 /* Win32 version */86.long _edata - efi_start /* Image size */87.long _end_header - efi_start /* Header size */88.long 0 /* Checksum */89.short 0xa /* Subsystem (EFI app) */90.short 0 /* DLL Characteristics */91.quad 0 /* Stack reserve */92.quad 0 /* Stack commit */93.quad 0 /* Heap reserve */94.quad 0 /* Heap commit */95.long 0 /* Loader flags */96.long 6 /* Number of RVAs */9798/* RVAs: */99.quad 0100.quad 0101.quad 0102.quad 0103.quad 0104.quad 0105106section_table:107/* We need a .reloc section for EFI */108.ascii ".reloc"109.byte 0110.byte 0 /* Pad to 8 bytes */111.long 0 /* Virtual size */112.long 0 /* Virtual address */113.long 0 /* Size of raw data */114.long 0 /* Pointer to raw data */115.long 0 /* Pointer to relocations */116.long 0 /* Pointer to line numbers */117.short 0 /* Number of relocations */118.short 0 /* Number of line numbers */119.long (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | \120IMAGE_SCN_MEM_DISCARDABLE) /* Characteristics */121122/* The contents of the loader */123.ascii ".text"124.byte 0125.byte 0126.byte 0 /* Pad to 8 bytes */127.long _edata - _end_header /* Virtual size */128.long _end_header - efi_start /* Virtual address */129.long _edata - _end_header /* Size of raw data */130.long _end_header - efi_start /* Pointer to raw data */131.long 0 /* Pointer to relocations */132.long 0 /* Pointer to line numbers */133.short 0 /* Number of relocations */134.short 0 /* Number of line numbers */135.long (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | \136IMAGE_SCN_MEM_READ) /* Characteristics */137_end_header:138139.text140.globl _start141_start:142/* Save the boot params to the stack */143addi sp, sp, -16144sd a0, 0(sp)145sd a1, 8(sp)146147/* Zero the BSS */148lla t0, __bss_start149lla t1, __bss_end1501511: sd zero, 0(t0)152addi t0, t0, 8153bltu t0, t1, 1b154155lla a0, ImageBase156lla a1, _DYNAMIC157call _C_LABEL(self_reloc)158159ld a1, 8(sp)160ld a0, 0(sp)161tail _C_LABEL(efi_main)162163/* NOTREACHED */1642: wfi165j 2b166167168