xref: /openbmc/u-boot/arch/riscv/lib/crt0_riscv_efi.S (revision 5c8fd32b)
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	0x5064
18#else
19#define SIZE_LONG	4
20#define SAVE_LONG(reg, idx)	sw	reg, (idx*SIZE_LONG)(sp)
21#define LOAD_LONG(reg, idx)	lw	reg, (idx*SIZE_LONG)(sp)
22#define PE_MACHINE	0x5032
23#endif
24
25
26	.section	.text.head
27
28	/*
29	 * Magic "MZ" signature for PE/COFF
30	 */
31	.globl	ImageBase
32ImageBase:
33	.ascii	"MZ"
34	.skip	58				/* 'MZ' + pad + offset == 64 */
35	.long	pe_header - ImageBase		/* Offset to the PE header */
36pe_header:
37	.ascii	"PE"
38	.short	0
39coff_header:
40	.short	PE_MACHINE			/* RISC-V 64/32-bit */
41	.short	2				/* nr_sections */
42	.long	0				/* TimeDateStamp */
43	.long	0				/* PointerToSymbolTable */
44	.long	1				/* NumberOfSymbols */
45	.short	section_table - optional_header	/* SizeOfOptionalHeader */
46	/*
47	 * Characteristics: IMAGE_FILE_DEBUG_STRIPPED |
48	 * IMAGE_FILE_EXECUTABLE_IMAGE | IMAGE_FILE_LINE_NUMS_STRIPPED
49	 */
50	.short	0x206
51optional_header:
52	.short	0x20b				/* PE32+ format */
53	.byte	0x02				/* MajorLinkerVersion */
54	.byte	0x14				/* MinorLinkerVersion */
55	.long	_edata - _start			/* SizeOfCode */
56	.long	0				/* SizeOfInitializedData */
57	.long	0				/* SizeOfUninitializedData */
58	.long	_start - ImageBase		/* AddressOfEntryPoint */
59	.long	_start - ImageBase		/* BaseOfCode */
60
61extra_header_fields:
62	.quad	0				/* ImageBase */
63	.long	0x20				/* SectionAlignment */
64	.long	0x8				/* FileAlignment */
65	.short	0				/* MajorOperatingSystemVersion */
66	.short	0				/* MinorOperatingSystemVersion */
67	.short	0				/* MajorImageVersion */
68	.short	0				/* MinorImageVersion */
69	.short	0				/* MajorSubsystemVersion */
70	.short	0				/* MinorSubsystemVersion */
71	.long	0				/* Win32VersionValue */
72
73	.long	_edata - ImageBase		/* SizeOfImage */
74
75	/*
76	 * Everything before the kernel image is considered part of the header
77	 */
78	.long	_start - ImageBase		/* SizeOfHeaders */
79	.long	0				/* CheckSum */
80	.short	IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
81	.short	0				/* DllCharacteristics */
82	.quad	0				/* SizeOfStackReserve */
83	.quad	0				/* SizeOfStackCommit */
84	.quad	0				/* SizeOfHeapReserve */
85	.quad	0				/* SizeOfHeapCommit */
86	.long	0				/* LoaderFlags */
87	.long	0x6				/* NumberOfRvaAndSizes */
88
89	.quad	0				/* ExportTable */
90	.quad	0				/* ImportTable */
91	.quad	0				/* ResourceTable */
92	.quad	0				/* ExceptionTable */
93	.quad	0				/* CertificationTable */
94	.quad	0				/* BaseRelocationTable */
95
96	/* Section table */
97section_table:
98
99	/*
100	 * The EFI application loader requires a relocation section
101	 * because EFI applications must be relocatable.  This is a
102	 * dummy section as far as we are concerned.
103	 */
104	.ascii	".reloc"
105	.byte	0
106	.byte	0			/* end of 0 padding of section name */
107	.long	0
108	.long	0
109	.long	0			/* SizeOfRawData */
110	.long	0			/* PointerToRawData */
111	.long	0			/* PointerToRelocations */
112	.long	0			/* PointerToLineNumbers */
113	.short	0			/* NumberOfRelocations */
114	.short	0			/* NumberOfLineNumbers */
115	.long	0x42100040		/* Characteristics (section flags) */
116
117
118	.ascii	".text"
119	.byte	0
120	.byte	0
121	.byte	0			/* end of 0 padding of section name */
122	.long	_edata - _start		/* VirtualSize */
123	.long	_start - ImageBase	/* VirtualAddress */
124	.long	_edata - _start		/* SizeOfRawData */
125	.long	_start - ImageBase	/* PointerToRawData */
126
127	.long	0		/* PointerToRelocations (0 for executables) */
128	.long	0		/* PointerToLineNumbers (0 for executables) */
129	.short	0		/* NumberOfRelocations  (0 for executables) */
130	.short	0		/* NumberOfLineNumbers  (0 for executables) */
131	.long	0xe0500020	/* Characteristics (section flags) */
132
133_start:
134	addi		sp, sp, -(SIZE_LONG * 3)
135	SAVE_LONG(a0, 0)
136	SAVE_LONG(a1, 1)
137	SAVE_LONG(ra, 2)
138
139	lla		a0, ImageBase
140	lla		a1, _DYNAMIC
141	call		_relocate
142	bne		a0, zero, 0f
143
144	LOAD_LONG(a1, 1)
145	LOAD_LONG(a0, 0)
146	call		efi_main
147
148	LOAD_LONG(ra, 2)
149
1500:	addi		sp, sp, (SIZE_LONG * 3)
151	ret
152