]> git.ipfire.org Git - thirdparty/u-boot.git/blob - arch/riscv/lib/crt0_riscv_efi.S
Merge tag 'efi-2022-04-rc1' of https://source.denx.de/u-boot/custodians/u-boot-efi
[thirdparty/u-boot.git] / arch / riscv / lib / crt0_riscv_efi.S
1 /* SPDX-License-Identifier: GPL-2.0+ */
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 *
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)
17 #define PE_MACHINE IMAGE_FILE_MACHINE_RISCV64
18 #define PE_MAGIC IMAGE_NT_OPTIONAL_HDR64_MAGIC
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)
23 #define PE_MACHINE IMAGE_FILE_MACHINE_RISCV32
24 #define PE_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC
25 #endif
26
27
28 .section .text.head
29
30 /*
31 * Magic "MZ" signature for PE/COFF
32 */
33 .globl ImageBase
34 ImageBase:
35 .short IMAGE_DOS_SIGNATURE /* 'MZ' */
36 .skip 46 /* 'MZ' + pad + offset == 64 */
37 .long 0x43534952 /* Linux magic "RISCV */
38 .long 0x00000056
39 .long 0x05435352 /* Linux magic2 "RSC\x05*/
40 .long pe_header - ImageBase /* Offset to the PE header */
41 pe_header:
42 .long IMAGE_NT_SIGNATURE /* 'PE' */
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 */
48 .long 0 /* NumberOfSymbols */
49 .short section_table - optional_header /* SizeOfOptionalHeader */
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)
55 optional_header:
56 .short PE_MAGIC /* PE32(+) format */
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 */
64 #if __riscv_xlen == 32
65 .long 0 /* BaseOfData */
66 #endif
67
68 extra_header_fields:
69 #if __riscv_xlen == 32
70 .long 0 /* ImageBase */
71 #else
72 .quad 0 /* ImageBase */
73 #endif
74 .long 0x200 /* SectionAlignment */
75 .long 0x200 /* FileAlignment */
76 .short 0 /* MajorOperatingSystemVersion */
77 .short 0 /* MinorOperatingSystemVersion */
78 .short 1 /* MajorImageVersion */
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 */
93 #if __riscv_xlen == 32
94 .long 0 /* SizeOfStackReserve */
95 .long 0 /* SizeOfStackCommit */
96 .long 0 /* SizeOfHeapReserve */
97 .long 0 /* SizeOfHeapCommit */
98 #else
99 .quad 0 /* SizeOfStackReserve */
100 .quad 0 /* SizeOfStackCommit */
101 .quad 0 /* SizeOfHeapReserve */
102 .quad 0 /* SizeOfHeapCommit */
103 #endif
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 */
113
114 /* Section table */
115 section_table:
116
117 /*
118 * The EFI application loader requires a relocation section
119 * because EFI applications must be relocatable. This is a
120 * dummy section as far as we are concerned.
121 */
122 .ascii ".reloc"
123 .byte 0
124 .byte 0 /* end of 0 padding of section name */
125 .long 0
126 .long 0
127 .long 0 /* SizeOfRawData */
128 .long 0 /* PointerToRawData */
129 .long 0 /* PointerToRelocations */
130 .long 0 /* PointerToLineNumbers */
131 .short 0 /* NumberOfRelocations */
132 .short 0 /* NumberOfLineNumbers */
133 .long 0x42100040 /* Characteristics (section flags) */
134
135
136 .ascii ".text"
137 .byte 0
138 .byte 0
139 .byte 0 /* end of 0 padding of section name */
140 .long _edata - _start /* VirtualSize */
141 .long _start - ImageBase /* VirtualAddress */
142 .long _edata - _start /* SizeOfRawData */
143 .long _start - ImageBase /* PointerToRawData */
144
145 .long 0 /* PointerToRelocations (0 for executables) */
146 .long 0 /* PointerToLineNumbers (0 for executables) */
147 .short 0 /* NumberOfRelocations (0 for executables) */
148 .short 0 /* NumberOfLineNumbers (0 for executables) */
149 .long 0xe0500020 /* Characteristics (section flags) */
150
151 .align 9
152 _start:
153 addi sp, sp, -(SIZE_LONG * 3)
154 SAVE_LONG(a0, 0)
155 SAVE_LONG(a1, 1)
156 SAVE_LONG(ra, 2)
157
158 lla a0, ImageBase
159 lla a1, _DYNAMIC
160 call _relocate
161 bne a0, zero, 0f
162
163 LOAD_LONG(a1, 1)
164 LOAD_LONG(a0, 0)
165 call efi_main
166
167 LOAD_LONG(ra, 2)
168
169 0: addi sp, sp, (SIZE_LONG * 3)
170 ret