start.S revision 329100
1/*-
2 * Copyright (c) 2014, 2015 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: stable/11/sys/boot/efi/loader/arch/arm/start.S 329100 2018-02-10 04:56:07Z kevans $
27 */
28
29#include <machine/asm.h>
30
31/*
32 * We need to be a PE32 file for EFI. On some architectures we can use
33 * objcopy to create the correct file, however on arm we need to do
34 * it ourselves.
35 */
36
37#define	IMAGE_FILE_MACHINE_ARM		0x01c2
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
45	.section .peheader,"a"
46efi_start:
47	/* The MS-DOS Stub, only used to get the offset of the COFF header */
48	.ascii	"MZ"
49	.short	0
50	.space	0x38
51	.long	pe_sig - efi_start
52
53	/* The PE32 Signature. Needs to be 8-byte aligned */
54	.align	3
55pe_sig:
56	.ascii	"PE"
57	.short	0
58coff_head:
59	.short	IMAGE_FILE_MACHINE_ARM		/* ARM file */
60	.short	2				/* 2 Sections */
61	.long	0				/* Timestamp */
62	.long	0				/* No symbol table */
63	.long	0				/* No symbols */
64	.short	section_table - optional_header	/* Optional header size */
65	.short	0	/* Characteristics TODO: Fill in */
66
67optional_header:
68	.short	0x010b				/* PE32 (32-bit addressing) */
69	.byte	0				/* Major linker version */
70	.byte	0				/* Minor linker version */
71	.long	_edata - _end_header		/* Code size */
72	.long	0				/* No initialized data */
73	.long	0				/* No uninitialized data */
74	.long	_start - efi_start		/* Entry point */
75	.long	_end_header - efi_start		/* Start of code */
76	.long	0				/* Start of data */
77
78optional_windows_header:
79	.long	0				/* Image base */
80	.long	32				/* Section Alignment */
81	.long	8				/* 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	.long	0				/* Stack reserve */
95	.long	0				/* Stack commit */
96	.long	0				/* Heap reserve */
97	.long	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	/* We need a .reloc section for EFI */
111	.ascii	".reloc"
112	.byte	0
113	.byte	0				/* Pad to 8 bytes */
114	.long	0				/* Virtual size */
115	.long	0				/* Virtual address */
116	.long	0				/* Size of raw data */
117	.long	0				/* 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_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | \
123		 IMAGE_SCN_MEM_DISCARDABLE)	/* Characteristics */
124
125	/* The contents of the loader */
126	.ascii	".text"
127	.byte	0
128	.byte	0
129	.byte	0				/* Pad to 8 bytes */
130	.long	_edata - _end_header		/* Virtual size */
131	.long	_end_header - efi_start		/* Virtual address */
132	.long	_edata - _end_header		/* Size of raw data */
133	.long	_end_header - efi_start		/* Pointer to raw data */
134	.long	0				/* Pointer to relocations */
135	.long	0				/* Pointer to line numbers */
136	.short	0				/* Number of relocations */
137	.short	0				/* Number of line numbers */
138	.long	(IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | \
139		 IMAGE_SCN_MEM_READ)		/* Characteristics */
140_end_header:
141
142	.text
143_start:
144	/* Save the boot params to the stack */
145	push	{r0, r1}
146
147	adr	r0, .Lbase
148	ldr	r1, [r0]
149	sub	r5, r0, r1
150
151	ldr	r0, .Limagebase
152	add	r0, r0, r5
153	ldr	r1, .Ldynamic
154	add	r1, r1, r5
155
156	bl	_C_LABEL(self_reloc)
157
158	/* Zero the BSS, _reloc fixed the values for us */
159	ldr	r0, .Lbss
160	ldr	r1, .Lbssend
161	mov	r2, #0
162
1631:	cmp	r0, r1
164	bge	2f
165	str	r2, [r0], #4
166	b	1b
1672:
168
169	pop	{r0, r1}
170	bl	_C_LABEL(efi_main)
171
1721:	b	1b
173
174.Lbase:
175	.word	.
176.Limagebase:
177	.word	ImageBase
178.Ldynamic:
179	.word	_DYNAMIC
180.Lbss:
181	.word	__bss_start
182.Lbssend:
183	.word	__bss_end
184
185.align	3
186stack:
187	.space 512
188stack_end:
189
190