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 |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 19 | #else |
| 20 | #define SIZE_LONG 4 |
| 21 | #define SAVE_LONG(reg, idx) sw reg, (idx*SIZE_LONG)(sp) |
| 22 | #define LOAD_LONG(reg, idx) lw reg, (idx*SIZE_LONG)(sp) |
Heinrich Schuchardt | d680e9a | 2019-07-11 06:39:32 +0200 | [diff] [blame] | 23 | #define PE_MACHINE IMAGE_FILE_MACHINE_RISCV32 |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 24 | #define PE_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 25 | #endif |
| 26 | |
| 27 | |
| 28 | .section .text.head |
| 29 | |
| 30 | /* |
| 31 | * Magic "MZ" signature for PE/COFF |
| 32 | */ |
| 33 | .globl ImageBase |
| 34 | ImageBase: |
Heinrich Schuchardt | d680e9a | 2019-07-11 06:39:32 +0200 | [diff] [blame] | 35 | .short IMAGE_DOS_SIGNATURE /* 'MZ' */ |
Heinrich Schuchardt | 5629aaa | 2021-05-28 22:24:37 +0200 | [diff] [blame] | 36 | .skip 46 /* 'MZ' + pad + offset == 64 */ |
| 37 | .long 0x43534952 /* Linux magic "RISCV */ |
| 38 | .long 0x00000056 |
| 39 | .long 0x05435352 /* Linux magic2 "RSC\x05*/ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 40 | .long pe_header - ImageBase /* Offset to the PE header */ |
| 41 | pe_header: |
Heinrich Schuchardt | d680e9a | 2019-07-11 06:39:32 +0200 | [diff] [blame] | 42 | .long IMAGE_NT_SIGNATURE /* 'PE' */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 43 | coff_header: |
| 44 | .short PE_MACHINE /* RISC-V 64/32-bit */ |
| 45 | .short 2 /* nr_sections */ |
| 46 | .long 0 /* TimeDateStamp */ |
| 47 | .long 0 /* PointerToSymbolTable */ |
Bin Meng | 5354888 | 2018-10-02 07:39:34 -0700 | [diff] [blame] | 48 | .long 0 /* NumberOfSymbols */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 49 | .short section_table - optional_header /* SizeOfOptionalHeader */ |
Bin Meng | 5354888 | 2018-10-02 07:39:34 -0700 | [diff] [blame] | 50 | /* Characteristics */ |
| 51 | .short (IMAGE_FILE_EXECUTABLE_IMAGE | \ |
| 52 | IMAGE_FILE_LINE_NUMS_STRIPPED | \ |
| 53 | IMAGE_FILE_LOCAL_SYMS_STRIPPED | \ |
| 54 | IMAGE_FILE_DEBUG_STRIPPED) |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 55 | optional_header: |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 56 | .short PE_MAGIC /* PE32(+) format */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 57 | .byte 0x02 /* MajorLinkerVersion */ |
| 58 | .byte 0x14 /* MinorLinkerVersion */ |
| 59 | .long _edata - _start /* SizeOfCode */ |
| 60 | .long 0 /* SizeOfInitializedData */ |
| 61 | .long 0 /* SizeOfUninitializedData */ |
| 62 | .long _start - ImageBase /* AddressOfEntryPoint */ |
| 63 | .long _start - ImageBase /* BaseOfCode */ |
Atish Patra | fa36696 | 2020-10-13 12:23:31 -0700 | [diff] [blame] | 64 | #if __riscv_xlen == 32 |
| 65 | .long 0 /* BaseOfData */ |
| 66 | #endif |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 67 | |
| 68 | extra_header_fields: |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 69 | #if __riscv_xlen == 32 |
| 70 | .long 0 /* ImageBase */ |
| 71 | #else |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 72 | .quad 0 /* ImageBase */ |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 73 | #endif |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 74 | .long 0x20 /* SectionAlignment */ |
| 75 | .long 0x8 /* FileAlignment */ |
| 76 | .short 0 /* MajorOperatingSystemVersion */ |
| 77 | .short 0 /* MinorOperatingSystemVersion */ |
Heinrich Schuchardt | 5629aaa | 2021-05-28 22:24:37 +0200 | [diff] [blame] | 78 | .short 1 /* MajorImageVersion */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 79 | .short 0 /* MinorImageVersion */ |
| 80 | .short 0 /* MajorSubsystemVersion */ |
| 81 | .short 0 /* MinorSubsystemVersion */ |
| 82 | .long 0 /* Win32VersionValue */ |
| 83 | |
| 84 | .long _edata - ImageBase /* SizeOfImage */ |
| 85 | |
| 86 | /* |
| 87 | * Everything before the kernel image is considered part of the header |
| 88 | */ |
| 89 | .long _start - ImageBase /* SizeOfHeaders */ |
| 90 | .long 0 /* CheckSum */ |
| 91 | .short IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */ |
| 92 | .short 0 /* DllCharacteristics */ |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 93 | #if __riscv_xlen == 32 |
| 94 | .long 0 /* SizeOfStackReserve */ |
| 95 | .long 0 /* SizeOfStackCommit */ |
| 96 | .long 0 /* SizeOfHeapReserve */ |
| 97 | .long 0 /* SizeOfHeapCommit */ |
| 98 | #else |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 99 | .quad 0 /* SizeOfStackReserve */ |
| 100 | .quad 0 /* SizeOfStackCommit */ |
| 101 | .quad 0 /* SizeOfHeapReserve */ |
| 102 | .quad 0 /* SizeOfHeapCommit */ |
Leo Yu-Chi Liang | b68402d | 2020-11-12 10:09:52 +0800 | [diff] [blame] | 103 | #endif |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 104 | .long 0 /* LoaderFlags */ |
| 105 | .long 0x6 /* NumberOfRvaAndSizes */ |
| 106 | |
| 107 | .quad 0 /* ExportTable */ |
| 108 | .quad 0 /* ImportTable */ |
| 109 | .quad 0 /* ResourceTable */ |
| 110 | .quad 0 /* ExceptionTable */ |
| 111 | .quad 0 /* CertificationTable */ |
| 112 | .quad 0 /* BaseRelocationTable */ |
Leo Yu-Chi Liang | dd5d79b | 2020-11-16 17:07:41 +0800 | [diff] [blame] | 113 | .quad 0 /* Debug */ |
| 114 | .quad 0 /* Architecture */ |
| 115 | .quad 0 /* Global Ptr */ |
| 116 | .quad 0 /* TLS Table */ |
| 117 | .quad 0 /* Load Config Table */ |
| 118 | .quad 0 /* Bound Import */ |
| 119 | .quad 0 /* IAT */ |
| 120 | .quad 0 /* Delay Import Descriptor */ |
| 121 | .quad 0 /* CLR Runtime Header */ |
| 122 | .quad 0 /* Reserved */ |
Alexander Graf | 31bdde9 | 2018-04-23 07:59:45 +0200 | [diff] [blame] | 123 | |
| 124 | /* Section table */ |
| 125 | section_table: |
| 126 | |
| 127 | /* |
| 128 | * The EFI application loader requires a relocation section |
| 129 | * because EFI applications must be relocatable. This is a |
| 130 | * dummy section as far as we are concerned. |
| 131 | */ |
| 132 | .ascii ".reloc" |
| 133 | .byte 0 |
| 134 | .byte 0 /* end of 0 padding of section name */ |
| 135 | .long 0 |
| 136 | .long 0 |
| 137 | .long 0 /* SizeOfRawData */ |
| 138 | .long 0 /* PointerToRawData */ |
| 139 | .long 0 /* PointerToRelocations */ |
| 140 | .long 0 /* PointerToLineNumbers */ |
| 141 | .short 0 /* NumberOfRelocations */ |
| 142 | .short 0 /* NumberOfLineNumbers */ |
| 143 | .long 0x42100040 /* Characteristics (section flags) */ |
| 144 | |
| 145 | |
| 146 | .ascii ".text" |
| 147 | .byte 0 |
| 148 | .byte 0 |
| 149 | .byte 0 /* end of 0 padding of section name */ |
| 150 | .long _edata - _start /* VirtualSize */ |
| 151 | .long _start - ImageBase /* VirtualAddress */ |
| 152 | .long _edata - _start /* SizeOfRawData */ |
| 153 | .long _start - ImageBase /* PointerToRawData */ |
| 154 | |
| 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 | .long 0xe0500020 /* Characteristics (section flags) */ |
| 160 | |
| 161 | _start: |
| 162 | addi sp, sp, -(SIZE_LONG * 3) |
| 163 | SAVE_LONG(a0, 0) |
| 164 | SAVE_LONG(a1, 1) |
| 165 | SAVE_LONG(ra, 2) |
| 166 | |
| 167 | lla a0, ImageBase |
| 168 | lla a1, _DYNAMIC |
| 169 | call _relocate |
| 170 | bne a0, zero, 0f |
| 171 | |
| 172 | LOAD_LONG(a1, 1) |
| 173 | LOAD_LONG(a0, 0) |
| 174 | call efi_main |
| 175 | |
| 176 | LOAD_LONG(ra, 2) |
| 177 | |
| 178 | 0: addi sp, sp, (SIZE_LONG * 3) |
| 179 | ret |