blob: a01e08a3c6d33fad37cdf8bad3584f619733a8a9 [file] [log] [blame]
Rick Chen9dbb9732018-05-29 14:10:06 +08001/* SPDX-License-Identifier: GPL-2.0+ */
Alexander Graf31bdde92018-04-23 07:59:45 +02002/*
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 Graf31bdde92018-04-23 07:59:45 +02008 * 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 Schuchardtd680e9a2019-07-11 06:39:32 +020017#define PE_MACHINE IMAGE_FILE_MACHINE_RISCV64
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080018#define PE_MAGIC IMAGE_NT_OPTIONAL_HDR64_MAGIC
Alexander Graf31bdde92018-04-23 07:59:45 +020019#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 Schuchardtd680e9a2019-07-11 06:39:32 +020023#define PE_MACHINE IMAGE_FILE_MACHINE_RISCV32
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080024#define PE_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC
Alexander Graf31bdde92018-04-23 07:59:45 +020025#endif
26
27
28 .section .text.head
29
30 /*
31 * Magic "MZ" signature for PE/COFF
32 */
33 .globl ImageBase
34ImageBase:
Heinrich Schuchardtd680e9a2019-07-11 06:39:32 +020035 .short IMAGE_DOS_SIGNATURE /* 'MZ' */
Heinrich Schuchardt5629aaa2021-05-28 22:24:37 +020036 .skip 46 /* 'MZ' + pad + offset == 64 */
37 .long 0x43534952 /* Linux magic "RISCV */
38 .long 0x00000056
39 .long 0x05435352 /* Linux magic2 "RSC\x05*/
Alexander Graf31bdde92018-04-23 07:59:45 +020040 .long pe_header - ImageBase /* Offset to the PE header */
41pe_header:
Heinrich Schuchardtd680e9a2019-07-11 06:39:32 +020042 .long IMAGE_NT_SIGNATURE /* 'PE' */
Alexander Graf31bdde92018-04-23 07:59:45 +020043coff_header:
44 .short PE_MACHINE /* RISC-V 64/32-bit */
45 .short 2 /* nr_sections */
46 .long 0 /* TimeDateStamp */
47 .long 0 /* PointerToSymbolTable */
Bin Meng53548882018-10-02 07:39:34 -070048 .long 0 /* NumberOfSymbols */
Alexander Graf31bdde92018-04-23 07:59:45 +020049 .short section_table - optional_header /* SizeOfOptionalHeader */
Bin Meng53548882018-10-02 07:39:34 -070050 /* 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 Graf31bdde92018-04-23 07:59:45 +020055optional_header:
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080056 .short PE_MAGIC /* PE32(+) format */
Alexander Graf31bdde92018-04-23 07:59:45 +020057 .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 Patrafa366962020-10-13 12:23:31 -070064#if __riscv_xlen == 32
65 .long 0 /* BaseOfData */
66#endif
Alexander Graf31bdde92018-04-23 07:59:45 +020067
68extra_header_fields:
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080069#if __riscv_xlen == 32
70 .long 0 /* ImageBase */
71#else
Alexander Graf31bdde92018-04-23 07:59:45 +020072 .quad 0 /* ImageBase */
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080073#endif
Heinrich Schuchardtf2634792022-01-14 21:40:15 +010074 .long 0x200 /* SectionAlignment */
75 .long 0x200 /* FileAlignment */
Alexander Graf31bdde92018-04-23 07:59:45 +020076 .short 0 /* MajorOperatingSystemVersion */
77 .short 0 /* MinorOperatingSystemVersion */
Heinrich Schuchardt5629aaa2021-05-28 22:24:37 +020078 .short 1 /* MajorImageVersion */
Alexander Graf31bdde92018-04-23 07:59:45 +020079 .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 Liangb68402d2020-11-12 10:09:52 +080093#if __riscv_xlen == 32
94 .long 0 /* SizeOfStackReserve */
95 .long 0 /* SizeOfStackCommit */
96 .long 0 /* SizeOfHeapReserve */
97 .long 0 /* SizeOfHeapCommit */
98#else
Alexander Graf31bdde92018-04-23 07:59:45 +020099 .quad 0 /* SizeOfStackReserve */
100 .quad 0 /* SizeOfStackCommit */
101 .quad 0 /* SizeOfHeapReserve */
102 .quad 0 /* SizeOfHeapCommit */
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +0800103#endif
Alexander Graf31bdde92018-04-23 07:59:45 +0200104 .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 */
Alexander Graf31bdde92018-04-23 07:59:45 +0200113
114 /* Section table */
115section_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
Heinrich Schuchardtf2634792022-01-14 21:40:15 +0100151 .align 9
Alexander Graf31bdde92018-04-23 07:59:45 +0200152_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
1690: addi sp, sp, (SIZE_LONG * 3)
170 ret