xref: /freebsd/stand/efi/loader/arch/arm64/start.S (revision d9a42747950146bf03cda7f6e25d219253f8a57a)
1/*-
2 * Copyright (c) 2014 Andrew Turner
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD$
27 */
28
29/*
30 * We need to be a PE32+ file for EFI. On some architectures we can use
31 * objcopy to create the correct file, however on arm64 we need to do
32 * it ourselves.
33 */
34
35#define	IMAGE_FILE_MACHINE_ARM64	0xaa64
36
37#define	IMAGE_FILE_EXECUTABLE		0x0002
38
39#define	IMAGE_SCN_CNT_CODE		0x00000020
40#define	IMAGE_SCN_CNT_INITIALIZED_DATA	0x00000040
41#define	IMAGE_SCN_MEM_DISCARDABLE	0x02000000
42#define	IMAGE_SCN_MEM_EXECUTE		0x20000000
43#define	IMAGE_SCN_MEM_READ		0x40000000
44#define	IMAGE_SCN_MEM_WRITE		0x80000000
45
46	.section .peheader,"a"
47efi_start:
48	/* The MS-DOS Stub, only used to get the offset of the COFF header */
49	.ascii	"MZ"
50	.short	0
51	.space	0x38
52	.long	pe_sig - efi_start
53
54	/* The PE32 Signature. Needs to be 8-byte aligned */
55	.align	3
56pe_sig:
57	.ascii	"PE"
58	.short	0
59coff_head:
60	.short	IMAGE_FILE_MACHINE_ARM64	/* AArch64 file */
61	.short	2				/* 2 Sections */
62	.long	0				/* Timestamp */
63	.long	0				/* No symbol table */
64	.long	0				/* No symbols */
65	.short	section_table - optional_header	/* Optional header size */
66	.short	IMAGE_FILE_EXECUTABLE		/* Characteristics */
67
68optional_header:
69	.short	0x020b				/* PE32+ (64-bit addressing) */
70	.byte	0				/* Major linker version */
71	.byte	0				/* Minor linker version */
72	.long	_etext - _end_header		/* Code size */
73	.long	__data_size			/* Initialized data size */
74	.long	0				/* No uninitialized data */
75	.long	_start - efi_start		/* Entry point */
76	.long	_end_header - efi_start		/* Start of code */
77
78optional_windows_header:
79	.quad	0				/* Image base */
80	.long	4096				/* Section Alignment */
81	.long	512				/* File alignment */
82	.short	0				/* Major OS version */
83	.short	0				/* Minor OS version */
84	.short	0				/* Major image version */
85	.short	0				/* Minor image version */
86	.short	0				/* Major subsystem version */
87	.short	0				/* Minor subsystem version */
88	.long	0				/* Win32 version */
89	.long	_edata - efi_start		/* Image size */
90	.long	_end_header - efi_start		/* Header size */
91	.long	0				/* Checksum */
92	.short	0xa				/* Subsystem (EFI app) */
93	.short	0				/* DLL Characteristics */
94	.quad	0				/* Stack reserve */
95	.quad	0				/* Stack commit */
96	.quad	0				/* Heap reserve */
97	.quad	0				/* Heap commit */
98	.long	0				/* Loader flags */
99	.long	6				/* Number of RVAs */
100
101	/* RVAs: */
102	.quad	0
103	.quad	0
104	.quad	0
105	.quad	0
106	.quad	0
107	.quad	0
108
109section_table:
110	.ascii	".text"
111	.byte	0
112	.byte	0
113	.byte	0				/* Pad to 8 bytes */
114	.long	_etext - _end_header		/* Virtual size */
115	.long	_end_header - efi_start		/* Virtual address */
116	.long	_etext - _end_header		/* Size of raw data */
117	.long	_end_header - efi_start		/* Pointer to raw data */
118	.long	0				/* Pointer to relocations */
119	.long	0				/* Pointer to line numbers */
120	.short	0				/* Number of relocations */
121	.short	0				/* Number of line numbers */
122	.long	(IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | \
123		 IMAGE_SCN_MEM_READ)		/* Characteristics */
124
125	.ascii	".data"
126	.byte	0
127	.byte	0
128	.byte	0				/* Pad to 8 bytes */
129	.long	__data_size			/* Virtual size */
130	.long	__data_start - efi_start	/* Virtual address */
131	.long	__data_size			/* Size of raw data */
132	.long	__data_start - efi_start	/* Pointer to raw data */
133	.long	0				/* Pointer to relocations */
134	.long	0				/* Pointer to line numbers */
135	.short	0				/* Number of relocations */
136	.short	0				/* Number of line numbers */
137	.long	(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | \
138		 IMAGE_SCN_MEM_WRITE)		/* Characteristics */
139
140	.align	12
141_end_header:
142
143	.text
144	.globl	_start
145_start:
146	/* Save the boot params to the stack */
147	stp	x0, x1, [sp, #-16]!
148
149	adrp	x0, __bss_start
150	add	x0, x0, :lo12:__bss_start
151	adrp	x1, __bss_end
152	add	x1, x1, :lo12:__bss_end
153
154	b 2f
155
1561:
157	stp	xzr, xzr, [x0], #16
1582:
159	cmp	x0, x1
160	b.lo	1b
161
162	adrp	x0, ImageBase
163	add	x0, x0, :lo12:ImageBase
164	adrp	x1, _DYNAMIC
165	add	x1, x1, :lo12:_DYNAMIC
166
167	bl	self_reloc
168
169	ldp	x0, x1, [sp], #16
170
171#ifndef EFI_BOOT1
172	/*
173	 * Load the stack to use. The default stack may be too small for
174	 * the lua loader.
175	 */
176	adrp	x2, initstack_end
177	add	x2, x2, :lo12:initstack_end
178	mov	sp, x2
179#endif
180
181	bl	efi_main
182
1831:	b	1b
184
185#ifndef EFI_BOOT1
186.bss
187	.align	4
188initstack:
189	.space	(64 * 1024)
190initstack_end:
191#endif
192