Rick Chen | 9dbb973 | 2018-05-29 14:10:06 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 2 | /* |
| 3 | * crt0-efi-riscv.S - PE/COFF header for RISC-V EFI applications |
| 4 | * |
| 5 | * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org> |
| 6 | * Copright (C) 2018 Alexander Graf <agraf@suse.de> |
| 7 | * |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 8 | * This file is inspired by arch/arm/lib/crt0_aarch64_efi.S |
| 9 | */ |
| 10 | |
| 11 | #include <asm-generic/pe.h> |
| 12 | |
| 13 | #if __riscv_xlen == 64 |
| 14 | #define SIZE_LONG 8 |
| 15 | #define SAVE_LONG(reg, idx) sd reg, (idx*SIZE_LONG)(sp) |
| 16 | #define LOAD_LONG(reg, idx) ld reg, (idx*SIZE_LONG)(sp) |
Heinrich Schuchardt | d680e9a | 2019-07-11 06:39:32 +0200 | [diff] [blame] | 17 | #define PE_MACHINE IMAGE_FILE_MACHINE_RISCV64 |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 18 | #define PE_MAGIC IMAGE_NT_OPTIONAL_HDR64_MAGIC |
Heinrich Schuchardt | e84ab96 | 2022-12-23 02:16:03 +0100 | [diff] [blame] | 19 | #define IMG_CHARACTERISTICS \ |
| 20 | (IMAGE_FILE_EXECUTABLE_IMAGE | \ |
| 21 | IMAGE_FILE_LINE_NUMS_STRIPPED | \ |
| 22 | IMAGE_FILE_LOCAL_SYMS_STRIPPED | \ |
| 23 | IMAGE_FILE_LARGE_ADDRESS_AWARE | \ |
| 24 | IMAGE_FILE_DEBUG_STRIPPED) |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 25 | #else |
| 26 | #define SIZE_LONG 4 |
| 27 | #define SAVE_LONG(reg, idx) sw reg, (idx*SIZE_LONG)(sp) |
| 28 | #define LOAD_LONG(reg, idx) lw reg, (idx*SIZE_LONG)(sp) |
Heinrich Schuchardt | d680e9a | 2019-07-11 06:39:32 +0200 | [diff] [blame] | 29 | #define PE_MACHINE IMAGE_FILE_MACHINE_RISCV32 |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 30 | #define PE_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC |
Heinrich Schuchardt | e84ab96 | 2022-12-23 02:16:03 +0100 | [diff] [blame] | 31 | #define IMG_CHARACTERISTICS \ |
| 32 | (IMAGE_FILE_EXECUTABLE_IMAGE | \ |
| 33 | IMAGE_FILE_LINE_NUMS_STRIPPED | \ |
| 34 | IMAGE_FILE_LOCAL_SYMS_STRIPPED | \ |
| 35 | IMAGE_FILE_DEBUG_STRIPPED) |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 36 | #endif |
| 37 | |
| 38 | |
| 39 | .section .text.head |
| 40 | |
| 41 | /* |
| 42 | * Magic "MZ" signature for PE/COFF |
| 43 | */ |
| 44 | .globl ImageBase |
| 45 | ImageBase: |
Heinrich Schuchardt | d680e9a | 2019-07-11 06:39:32 +0200 | [diff] [blame] | 46 | .short IMAGE_DOS_SIGNATURE /* 'MZ' */ |
Heinrich Schuchardt | 5629aaa | 2021-05-28 22:24:37 +0200 | [diff] [blame] | 47 | .skip 46 /* 'MZ' + pad + offset == 64 */ |
| 48 | .long 0x43534952 /* Linux magic "RISCV */ |
| 49 | .long 0x00000056 |
| 50 | .long 0x05435352 /* Linux magic2 "RSC\x05*/ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 51 | .long pe_header - ImageBase /* Offset to the PE header */ |
| 52 | pe_header: |
Heinrich Schuchardt | d680e9a | 2019-07-11 06:39:32 +0200 | [diff] [blame] | 53 | .long IMAGE_NT_SIGNATURE /* 'PE' */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 54 | coff_header: |
| 55 | .short PE_MACHINE /* RISC-V 64/32-bit */ |
Heinrich Schuchardt | 9bcbe84 | 2024-01-25 09:38:15 +0100 | [diff] [blame] | 56 | .short 3 /* nr_sections */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 57 | .long 0 /* TimeDateStamp */ |
| 58 | .long 0 /* PointerToSymbolTable */ |
Bin Meng | 5354888 | 2018-10-02 07:39:34 -0700 | [diff] [blame] | 59 | .long 0 /* NumberOfSymbols */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 60 | .short section_table - optional_header /* SizeOfOptionalHeader */ |
Heinrich Schuchardt | e84ab96 | 2022-12-23 02:16:03 +0100 | [diff] [blame] | 61 | .short IMG_CHARACTERISTICS /* Characteristics */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 62 | optional_header: |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 63 | .short PE_MAGIC /* PE32(+) format */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 64 | .byte 0x02 /* MajorLinkerVersion */ |
| 65 | .byte 0x14 /* MinorLinkerVersion */ |
| 66 | .long _edata - _start /* SizeOfCode */ |
| 67 | .long 0 /* SizeOfInitializedData */ |
| 68 | .long 0 /* SizeOfUninitializedData */ |
| 69 | .long _start - ImageBase /* AddressOfEntryPoint */ |
| 70 | .long _start - ImageBase /* BaseOfCode */ |
Atish Patra | fa36696 | 2020-10-13 12:23:31 -0700 | [diff] [blame] | 71 | #if __riscv_xlen == 32 |
| 72 | .long 0 /* BaseOfData */ |
| 73 | #endif |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 74 | |
| 75 | extra_header_fields: |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 76 | #if __riscv_xlen == 32 |
| 77 | .long 0 /* ImageBase */ |
| 78 | #else |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 79 | .quad 0 /* ImageBase */ |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 80 | #endif |
Heinrich Schuchardt | f263479 | 2022-01-14 21:40:15 +0100 | [diff] [blame] | 81 | .long 0x200 /* SectionAlignment */ |
| 82 | .long 0x200 /* FileAlignment */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 83 | .short 0 /* MajorOperatingSystemVersion */ |
| 84 | .short 0 /* MinorOperatingSystemVersion */ |
Heinrich Schuchardt | 5629aaa | 2021-05-28 22:24:37 +0200 | [diff] [blame] | 85 | .short 1 /* MajorImageVersion */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 86 | .short 0 /* MinorImageVersion */ |
| 87 | .short 0 /* MajorSubsystemVersion */ |
| 88 | .short 0 /* MinorSubsystemVersion */ |
| 89 | .long 0 /* Win32VersionValue */ |
| 90 | |
| 91 | .long _edata - ImageBase /* SizeOfImage */ |
| 92 | |
| 93 | /* |
| 94 | * Everything before the kernel image is considered part of the header |
| 95 | */ |
| 96 | .long _start - ImageBase /* SizeOfHeaders */ |
| 97 | .long 0 /* CheckSum */ |
| 98 | .short IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */ |
Heinrich Schuchardt | 299425d | 2024-02-14 21:43:21 +0100 | [diff] [blame] | 99 | #if CONFIG_VENDOR_EFI |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 100 | .short 0 /* DllCharacteristics */ |
Heinrich Schuchardt | 299425d | 2024-02-14 21:43:21 +0100 | [diff] [blame] | 101 | #else |
| 102 | .short IMAGE_DLLCHARACTERISTICS_NX_COMPAT |
| 103 | #endif |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 104 | #if __riscv_xlen == 32 |
| 105 | .long 0 /* SizeOfStackReserve */ |
| 106 | .long 0 /* SizeOfStackCommit */ |
| 107 | .long 0 /* SizeOfHeapReserve */ |
| 108 | .long 0 /* SizeOfHeapCommit */ |
| 109 | #else |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 110 | .quad 0 /* SizeOfStackReserve */ |
| 111 | .quad 0 /* SizeOfStackCommit */ |
| 112 | .quad 0 /* SizeOfHeapReserve */ |
| 113 | .quad 0 /* SizeOfHeapCommit */ |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 114 | #endif |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 115 | .long 0 /* LoaderFlags */ |
| 116 | .long 0x6 /* NumberOfRvaAndSizes */ |
| 117 | |
| 118 | .quad 0 /* ExportTable */ |
| 119 | .quad 0 /* ImportTable */ |
| 120 | .quad 0 /* ResourceTable */ |
| 121 | .quad 0 /* ExceptionTable */ |
| 122 | .quad 0 /* CertificationTable */ |
| 123 | .quad 0 /* BaseRelocationTable */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 124 | |
| 125 | /* Section table */ |
| 126 | section_table: |
| 127 | |
| 128 | /* |
| 129 | * The EFI application loader requires a relocation section |
| 130 | * because EFI applications must be relocatable. This is a |
| 131 | * dummy section as far as we are concerned. |
| 132 | */ |
| 133 | .ascii ".reloc" |
| 134 | .byte 0 |
| 135 | .byte 0 /* end of 0 padding of section name */ |
| 136 | .long 0 |
| 137 | .long 0 |
| 138 | .long 0 /* SizeOfRawData */ |
| 139 | .long 0 /* PointerToRawData */ |
| 140 | .long 0 /* PointerToRelocations */ |
| 141 | .long 0 /* PointerToLineNumbers */ |
| 142 | .short 0 /* NumberOfRelocations */ |
| 143 | .short 0 /* NumberOfLineNumbers */ |
| 144 | .long 0x42100040 /* Characteristics (section flags) */ |
| 145 | |
| 146 | |
| 147 | .ascii ".text" |
| 148 | .byte 0 |
| 149 | .byte 0 |
| 150 | .byte 0 /* end of 0 padding of section name */ |
Heinrich Schuchardt | 9bcbe84 | 2024-01-25 09:38:15 +0100 | [diff] [blame] | 151 | .long _etext - _start /* VirtualSize */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 152 | .long _start - ImageBase /* VirtualAddress */ |
Heinrich Schuchardt | 9bcbe84 | 2024-01-25 09:38:15 +0100 | [diff] [blame] | 153 | .long _etext - _start /* SizeOfRawData */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 154 | .long _start - ImageBase /* PointerToRawData */ |
Heinrich Schuchardt | 9bcbe84 | 2024-01-25 09:38:15 +0100 | [diff] [blame] | 155 | .long 0 /* PointerToRelocations (0 for executables) */ |
| 156 | .long 0 /* PointerToLineNumbers (0 for executables) */ |
| 157 | .short 0 /* NumberOfRelocations (0 for executables) */ |
| 158 | .short 0 /* NumberOfLineNumbers (0 for executables) */ |
| 159 | /* Characteristics (section flags) */ |
| 160 | .long (IMAGE_SCN_MEM_READ | \ |
| 161 | IMAGE_SCN_MEM_EXECUTE | \ |
| 162 | IMAGE_SCN_CNT_CODE) |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 163 | |
Heinrich Schuchardt | 9bcbe84 | 2024-01-25 09:38:15 +0100 | [diff] [blame] | 164 | .ascii ".data" |
| 165 | .byte 0 |
| 166 | .byte 0 |
| 167 | .byte 0 /* end of 0 padding of section name */ |
| 168 | .long _edata - _data /* VirtualSize */ |
| 169 | .long _data - ImageBase /* VirtualAddress */ |
| 170 | .long _edata - _data /* SizeOfRawData */ |
| 171 | .long _data - ImageBase /* PointerToRawData */ |
| 172 | .long 0 /* PointerToRelocations */ |
| 173 | .long 0 /* PointerToLineNumbers */ |
| 174 | .short 0 /* NumberOfRelocations */ |
| 175 | .short 0 /* NumberOfLineNumbers */ |
| 176 | /* Characteristics (section flags) */ |
| 177 | .long (IMAGE_SCN_MEM_WRITE | \ |
| 178 | IMAGE_SCN_MEM_READ | \ |
| 179 | IMAGE_SCN_CNT_INITIALIZED_DATA) |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 180 | |
Heinrich Schuchardt | 9ceda68 | 2024-01-25 09:38:14 +0100 | [diff] [blame] | 181 | .align 12 |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 182 | _start: |
| 183 | addi sp, sp, -(SIZE_LONG * 3) |
| 184 | SAVE_LONG(a0, 0) |
| 185 | SAVE_LONG(a1, 1) |
| 186 | SAVE_LONG(ra, 2) |
| 187 | |
| 188 | lla a0, ImageBase |
| 189 | lla a1, _DYNAMIC |
| 190 | call _relocate |
| 191 | bne a0, zero, 0f |
| 192 | |
| 193 | LOAD_LONG(a1, 1) |
| 194 | LOAD_LONG(a0, 0) |
| 195 | call efi_main |
| 196 | |
| 197 | LOAD_LONG(ra, 2) |
| 198 | |
| 199 | 0: addi sp, sp, (SIZE_LONG * 3) |
| 200 | ret |