blob: 793eefdd8833a53f9c9ea3a94e52ae87cc4b88a5 [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
Heinrich Schuchardte84ab962022-12-23 02:16:03 +010019#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 Graf31bdde92018-04-23 07:59:45 +020025#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 Schuchardtd680e9a2019-07-11 06:39:32 +020029#define PE_MACHINE IMAGE_FILE_MACHINE_RISCV32
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080030#define PE_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC
Heinrich Schuchardte84ab962022-12-23 02:16:03 +010031#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 Graf31bdde92018-04-23 07:59:45 +020036#endif
37
38
39 .section .text.head
40
41 /*
42 * Magic "MZ" signature for PE/COFF
43 */
44 .globl ImageBase
45ImageBase:
Heinrich Schuchardtd680e9a2019-07-11 06:39:32 +020046 .short IMAGE_DOS_SIGNATURE /* 'MZ' */
Heinrich Schuchardt5629aaa2021-05-28 22:24:37 +020047 .skip 46 /* 'MZ' + pad + offset == 64 */
48 .long 0x43534952 /* Linux magic "RISCV */
49 .long 0x00000056
50 .long 0x05435352 /* Linux magic2 "RSC\x05*/
Alexander Graf31bdde92018-04-23 07:59:45 +020051 .long pe_header - ImageBase /* Offset to the PE header */
52pe_header:
Heinrich Schuchardtd680e9a2019-07-11 06:39:32 +020053 .long IMAGE_NT_SIGNATURE /* 'PE' */
Alexander Graf31bdde92018-04-23 07:59:45 +020054coff_header:
55 .short PE_MACHINE /* RISC-V 64/32-bit */
56 .short 2 /* nr_sections */
57 .long 0 /* TimeDateStamp */
58 .long 0 /* PointerToSymbolTable */
Bin Meng53548882018-10-02 07:39:34 -070059 .long 0 /* NumberOfSymbols */
Alexander Graf31bdde92018-04-23 07:59:45 +020060 .short section_table - optional_header /* SizeOfOptionalHeader */
Heinrich Schuchardte84ab962022-12-23 02:16:03 +010061 .short IMG_CHARACTERISTICS /* Characteristics */
Alexander Graf31bdde92018-04-23 07:59:45 +020062optional_header:
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080063 .short PE_MAGIC /* PE32(+) format */
Alexander Graf31bdde92018-04-23 07:59:45 +020064 .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 Patrafa366962020-10-13 12:23:31 -070071#if __riscv_xlen == 32
72 .long 0 /* BaseOfData */
73#endif
Alexander Graf31bdde92018-04-23 07:59:45 +020074
75extra_header_fields:
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080076#if __riscv_xlen == 32
77 .long 0 /* ImageBase */
78#else
Alexander Graf31bdde92018-04-23 07:59:45 +020079 .quad 0 /* ImageBase */
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080080#endif
Heinrich Schuchardtf2634792022-01-14 21:40:15 +010081 .long 0x200 /* SectionAlignment */
82 .long 0x200 /* FileAlignment */
Alexander Graf31bdde92018-04-23 07:59:45 +020083 .short 0 /* MajorOperatingSystemVersion */
84 .short 0 /* MinorOperatingSystemVersion */
Heinrich Schuchardt5629aaa2021-05-28 22:24:37 +020085 .short 1 /* MajorImageVersion */
Alexander Graf31bdde92018-04-23 07:59:45 +020086 .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 */
99 .short 0 /* DllCharacteristics */
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +0800100#if __riscv_xlen == 32
101 .long 0 /* SizeOfStackReserve */
102 .long 0 /* SizeOfStackCommit */
103 .long 0 /* SizeOfHeapReserve */
104 .long 0 /* SizeOfHeapCommit */
105#else
Alexander Graf31bdde92018-04-23 07:59:45 +0200106 .quad 0 /* SizeOfStackReserve */
107 .quad 0 /* SizeOfStackCommit */
108 .quad 0 /* SizeOfHeapReserve */
109 .quad 0 /* SizeOfHeapCommit */
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +0800110#endif
Alexander Graf31bdde92018-04-23 07:59:45 +0200111 .long 0 /* LoaderFlags */
112 .long 0x6 /* NumberOfRvaAndSizes */
113
114 .quad 0 /* ExportTable */
115 .quad 0 /* ImportTable */
116 .quad 0 /* ResourceTable */
117 .quad 0 /* ExceptionTable */
118 .quad 0 /* CertificationTable */
119 .quad 0 /* BaseRelocationTable */
Alexander Graf31bdde92018-04-23 07:59:45 +0200120
121 /* Section table */
122section_table:
123
124 /*
125 * The EFI application loader requires a relocation section
126 * because EFI applications must be relocatable. This is a
127 * dummy section as far as we are concerned.
128 */
129 .ascii ".reloc"
130 .byte 0
131 .byte 0 /* end of 0 padding of section name */
132 .long 0
133 .long 0
134 .long 0 /* SizeOfRawData */
135 .long 0 /* PointerToRawData */
136 .long 0 /* PointerToRelocations */
137 .long 0 /* PointerToLineNumbers */
138 .short 0 /* NumberOfRelocations */
139 .short 0 /* NumberOfLineNumbers */
140 .long 0x42100040 /* Characteristics (section flags) */
141
142
143 .ascii ".text"
144 .byte 0
145 .byte 0
146 .byte 0 /* end of 0 padding of section name */
147 .long _edata - _start /* VirtualSize */
148 .long _start - ImageBase /* VirtualAddress */
149 .long _edata - _start /* SizeOfRawData */
150 .long _start - ImageBase /* PointerToRawData */
151
152 .long 0 /* PointerToRelocations (0 for executables) */
153 .long 0 /* PointerToLineNumbers (0 for executables) */
154 .short 0 /* NumberOfRelocations (0 for executables) */
155 .short 0 /* NumberOfLineNumbers (0 for executables) */
156 .long 0xe0500020 /* Characteristics (section flags) */
157
Heinrich Schuchardtf2634792022-01-14 21:40:15 +0100158 .align 9
Alexander Graf31bdde92018-04-23 07:59:45 +0200159_start:
160 addi sp, sp, -(SIZE_LONG * 3)
161 SAVE_LONG(a0, 0)
162 SAVE_LONG(a1, 1)
163 SAVE_LONG(ra, 2)
164
165 lla a0, ImageBase
166 lla a1, _DYNAMIC
167 call _relocate
168 bne a0, zero, 0f
169
170 LOAD_LONG(a1, 1)
171 LOAD_LONG(a0, 0)
172 call efi_main
173
174 LOAD_LONG(ra, 2)
175
1760: addi sp, sp, (SIZE_LONG * 3)
177 ret