blob: 48ff89553b94509f6b191455bb8491d96edffcef [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' */
Alexander Graf31bdde92018-04-23 07:59:45 +020036 .skip 58 /* 'MZ' + pad + offset == 64 */
37 .long pe_header - ImageBase /* Offset to the PE header */
38pe_header:
Heinrich Schuchardtd680e9a2019-07-11 06:39:32 +020039 .long IMAGE_NT_SIGNATURE /* 'PE' */
Alexander Graf31bdde92018-04-23 07:59:45 +020040coff_header:
41 .short PE_MACHINE /* RISC-V 64/32-bit */
42 .short 2 /* nr_sections */
43 .long 0 /* TimeDateStamp */
44 .long 0 /* PointerToSymbolTable */
Bin Meng53548882018-10-02 07:39:34 -070045 .long 0 /* NumberOfSymbols */
Alexander Graf31bdde92018-04-23 07:59:45 +020046 .short section_table - optional_header /* SizeOfOptionalHeader */
Bin Meng53548882018-10-02 07:39:34 -070047 /* Characteristics */
48 .short (IMAGE_FILE_EXECUTABLE_IMAGE | \
49 IMAGE_FILE_LINE_NUMS_STRIPPED | \
50 IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
51 IMAGE_FILE_DEBUG_STRIPPED)
Alexander Graf31bdde92018-04-23 07:59:45 +020052optional_header:
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080053 .short PE_MAGIC /* PE32(+) format */
Alexander Graf31bdde92018-04-23 07:59:45 +020054 .byte 0x02 /* MajorLinkerVersion */
55 .byte 0x14 /* MinorLinkerVersion */
56 .long _edata - _start /* SizeOfCode */
57 .long 0 /* SizeOfInitializedData */
58 .long 0 /* SizeOfUninitializedData */
59 .long _start - ImageBase /* AddressOfEntryPoint */
60 .long _start - ImageBase /* BaseOfCode */
Atish Patrafa366962020-10-13 12:23:31 -070061#if __riscv_xlen == 32
62 .long 0 /* BaseOfData */
63#endif
Alexander Graf31bdde92018-04-23 07:59:45 +020064
65extra_header_fields:
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080066#if __riscv_xlen == 32
67 .long 0 /* ImageBase */
68#else
Alexander Graf31bdde92018-04-23 07:59:45 +020069 .quad 0 /* ImageBase */
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080070#endif
Alexander Graf31bdde92018-04-23 07:59:45 +020071 .long 0x20 /* SectionAlignment */
72 .long 0x8 /* FileAlignment */
73 .short 0 /* MajorOperatingSystemVersion */
74 .short 0 /* MinorOperatingSystemVersion */
75 .short 0 /* MajorImageVersion */
76 .short 0 /* MinorImageVersion */
77 .short 0 /* MajorSubsystemVersion */
78 .short 0 /* MinorSubsystemVersion */
79 .long 0 /* Win32VersionValue */
80
81 .long _edata - ImageBase /* SizeOfImage */
82
83 /*
84 * Everything before the kernel image is considered part of the header
85 */
86 .long _start - ImageBase /* SizeOfHeaders */
87 .long 0 /* CheckSum */
88 .short IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
89 .short 0 /* DllCharacteristics */
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +080090#if __riscv_xlen == 32
91 .long 0 /* SizeOfStackReserve */
92 .long 0 /* SizeOfStackCommit */
93 .long 0 /* SizeOfHeapReserve */
94 .long 0 /* SizeOfHeapCommit */
95#else
Alexander Graf31bdde92018-04-23 07:59:45 +020096 .quad 0 /* SizeOfStackReserve */
97 .quad 0 /* SizeOfStackCommit */
98 .quad 0 /* SizeOfHeapReserve */
99 .quad 0 /* SizeOfHeapCommit */
Leo Yu-Chi Liangb68402d2020-11-12 10:09:52 +0800100#endif
Alexander Graf31bdde92018-04-23 07:59:45 +0200101 .long 0 /* LoaderFlags */
102 .long 0x6 /* NumberOfRvaAndSizes */
103
104 .quad 0 /* ExportTable */
105 .quad 0 /* ImportTable */
106 .quad 0 /* ResourceTable */
107 .quad 0 /* ExceptionTable */
108 .quad 0 /* CertificationTable */
109 .quad 0 /* BaseRelocationTable */
110
111 /* Section table */
112section_table:
113
114 /*
115 * The EFI application loader requires a relocation section
116 * because EFI applications must be relocatable. This is a
117 * dummy section as far as we are concerned.
118 */
119 .ascii ".reloc"
120 .byte 0
121 .byte 0 /* end of 0 padding of section name */
122 .long 0
123 .long 0
124 .long 0 /* SizeOfRawData */
125 .long 0 /* PointerToRawData */
126 .long 0 /* PointerToRelocations */
127 .long 0 /* PointerToLineNumbers */
128 .short 0 /* NumberOfRelocations */
129 .short 0 /* NumberOfLineNumbers */
130 .long 0x42100040 /* Characteristics (section flags) */
131
132
133 .ascii ".text"
134 .byte 0
135 .byte 0
136 .byte 0 /* end of 0 padding of section name */
137 .long _edata - _start /* VirtualSize */
138 .long _start - ImageBase /* VirtualAddress */
139 .long _edata - _start /* SizeOfRawData */
140 .long _start - ImageBase /* PointerToRawData */
141
142 .long 0 /* PointerToRelocations (0 for executables) */
143 .long 0 /* PointerToLineNumbers (0 for executables) */
144 .short 0 /* NumberOfRelocations (0 for executables) */
145 .short 0 /* NumberOfLineNumbers (0 for executables) */
146 .long 0xe0500020 /* Characteristics (section flags) */
147
148_start:
149 addi sp, sp, -(SIZE_LONG * 3)
150 SAVE_LONG(a0, 0)
151 SAVE_LONG(a1, 1)
152 SAVE_LONG(ra, 2)
153
154 lla a0, ImageBase
155 lla a1, _DYNAMIC
156 call _relocate
157 bne a0, zero, 0f
158
159 LOAD_LONG(a1, 1)
160 LOAD_LONG(a0, 0)
161 call efi_main
162
163 LOAD_LONG(ra, 2)
164
1650: addi sp, sp, (SIZE_LONG * 3)
166 ret