1/*
2 * crt0-efi-aarch64.S - PE/COFF header for AArch64 EFI applications
3 *
4 * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice and this list of conditions, without modification.
11 * 2. The name of the author may not be used to endorse or promote products
12 *    derived from this software without specific prior written permission.
13 *
14 * Alternatively, this software may be distributed under the terms of the
15 * GNU General Public License as published by the Free Software Foundation;
16 * either version 2 of the License, or (at your option) any later version.
17 */
18
19	.section	.text.head
20
21	/*
22	 * Magic "MZ" signature for PE/COFF
23	 */
24	.globl	ImageBase
25ImageBase:
26	.ascii	"MZ"
27	.skip	58				// 'MZ' + pad + offset == 64
28	.long	pe_header - ImageBase		// Offset to the PE header.
29pe_header:
30	.ascii	"PE"
31	.short 	0
32coff_header:
33	.short	0xaa64				// AArch64
34	.short	2				// nr_sections
35	.long	0 				// TimeDateStamp
36	.long	0				// PointerToSymbolTable
37	.long	1				// NumberOfSymbols
38	.short	section_table - optional_header	// SizeOfOptionalHeader
39	.short	0x20e				// Characteristics.
40						// IMAGE_FILE_DEBUG_STRIPPED |
41						// IMAGE_FILE_EXECUTABLE_IMAGE |
42						// IMAGE_FILE_LINE_NUMS_STRIPPED
43optional_header:
44	.short	0x20b				// PE32+ format
45	.byte	0x02				// MajorLinkerVersion
46	.byte	0x14				// MinorLinkerVersion
47	.long	_edata - _start			// SizeOfCode
48	.long	0			// SizeOfInitializedData
49	.long	0				// SizeOfUninitializedData
50	.long	_start - ImageBase		// AddressOfEntryPoint
51	.long	_start - ImageBase		// BaseOfCode
52
53extra_header_fields:
54	.quad	0				// ImageBase
55	.long	32				// SectionAlignment
56	.long	8				// FileAlignment
57	.short	0				// MajorOperatingSystemVersion
58	.short	0				// MinorOperatingSystemVersion
59	.short	0				// MajorImageVersion
60	.short	0				// MinorImageVersion
61	.short	0				// MajorSubsystemVersion
62	.short	0				// MinorSubsystemVersion
63	.long	0				// Win32VersionValue
64
65	.long	_edata - ImageBase		// SizeOfImage
66
67	// Everything before the kernel image is considered part of the header
68	.long	_start - ImageBase		// SizeOfHeaders
69	.long	0				// CheckSum
70	.short	10				// Subsystem (EFI)
71	.short	0				// DllCharacteristics
72	.quad	0				// SizeOfStackReserve
73	.quad	0				// SizeOfStackCommit
74	.quad	0				// SizeOfHeapReserve
75	.quad	0				// SizeOfHeapCommit
76	.long	0				// LoaderFlags
77	.long	16				// NumberOfRvaAndSizes
78
79	.quad	0				// ExportTable
80	.quad	0				// ImportTable
81	.quad	0				// ResourceTable
82	.quad	0				// ExceptionTable
83	.quad	0				// CertificationTable
84	.quad	0				// BaseRelocationTable
85	.quad	0				// Debug
86	.quad	0				// Architecture
87	.quad	0				// Global Ptr
88	.quad	0				// TLS Table
89	.quad	0				// Load Config Table
90	.quad	0				// Bound Import
91	.quad	0				// IAT
92	.quad	0				// Delay Import Descriptor
93	.quad	0				// CLR Runtime Header
94	.quad	0				// Reserved
95	// Section table
96section_table:
97
98	/*
99	 * The EFI application loader requires a relocation section
100	 * because EFI applications must be relocatable.  This is a
101	 * dummy section as far as we are concerned.
102	 */
103	.ascii	".reloc"
104	.byte	0
105	.byte	0			// end of 0 padding of section name
106	.long	0
107	.long	0
108	.long	0			// SizeOfRawData
109	.long	0			// PointerToRawData
110	.long	0			// PointerToRelocations
111	.long	0			// PointerToLineNumbers
112	.short	0			// NumberOfRelocations
113	.short	0			// NumberOfLineNumbers
114	.long	0x42100040		// Characteristics (section flags)
115
116	.ascii	".text"
117	.byte	0
118	.byte	0
119	.byte	0
120	.long	_edata - _start		// VirtualSize
121	.long	_start - ImageBase	// VirtualAddress
122	.long	_edata - _start		// SizeOfRawData
123	.long	_start - ImageBase	// PointerToRawData
124
125	.long	0		// PointerToRelocations (0 for executables)
126	.long	0		// PointerToLineNumbers (0 for executables)
127	.short	0		// NumberOfRelocations  (0 for executables)
128	.short	0		// NumberOfLineNumbers  (0 for executables)
129	.long	0xe0500020	// Characteristics (section flags)
130
131	.align		12
132
133	.globl _start
134_start:
135	stp		x29, x30, [sp, #-32]!
136	mov		x29, sp
137
138	stp		x0, x1, [sp, #16]
139	mov		x2, x0
140	mov		x3, x1
141	adr		x0, ImageBase
142	adrp		x1, _DYNAMIC
143	add		x1, x1, #:lo12:_DYNAMIC
144	bl		_relocate
145	cbnz		x0, 0f
146
147	ldp		x0, x1, [sp, #16]
148	bl		efi_main
149
1500:	ldp		x29, x30, [sp], #32
151	ret
152