xref: /freebsd/stand/efi/loader/arch/arm/start.S (revision a64729f5077d77e13b9497cb33ecb3c82e606ee8)
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
27#include <machine/asm.h>
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 arm we need to do
32 * it ourselves.
33 */
34
35#define	IMAGE_FILE_MACHINE_ARM		0x01c2
36
37#define	IMAGE_SCN_CNT_CODE		0x00000020
38#define	IMAGE_SCN_CNT_INITIALIZED_DATA	0x00000040
39#define	IMAGE_SCN_MEM_DISCARDABLE	0x02000000
40#define	IMAGE_SCN_MEM_EXECUTE		0x20000000
41#define	IMAGE_SCN_MEM_READ		0x40000000
42
43	.section .peheader,"a"
44efi_start:
45	/* The MS-DOS Stub, only used to get the offset of the COFF header */
46	.ascii	"MZ"
47	.short	0
48	.space	0x38
49	.long	pe_sig - efi_start
50
51	/* The PE32 Signature. Needs to be 8-byte aligned */
52	.align	3
53pe_sig:
54	.ascii	"PE"
55	.short	0
56coff_head:
57	.short	IMAGE_FILE_MACHINE_ARM		/* ARM file */
58	.short	2				/* 2 Sections */
59	.long	0				/* Timestamp */
60	.long	0				/* No symbol table */
61	.long	0				/* No symbols */
62	.short	section_table - optional_header	/* Optional header size */
63	.short	0	/* Characteristics TODO: Fill in */
64
65optional_header:
66	.short	0x010b				/* PE32 (32-bit addressing) */
67	.byte	0				/* Major linker version */
68	.byte	0				/* Minor linker version */
69	.long	_edata - _end_header		/* Code size */
70	.long	0				/* No initialized data */
71	.long	0				/* No uninitialized data */
72	.long	_start - efi_start		/* Entry point */
73	.long	_end_header - efi_start		/* Start of code */
74	.long	0				/* Start of data */
75
76optional_windows_header:
77	.long	0				/* Image base */
78	.long	32				/* Section Alignment */
79	.long	8				/* File alignment */
80	.short	0				/* Major OS version */
81	.short	0				/* Minor OS version */
82	.short	0				/* Major image version */
83	.short	0				/* Minor image version */
84	.short	0				/* Major subsystem version */
85	.short	0				/* Minor subsystem version */
86	.long	0				/* Win32 version */
87	.long	_edata - efi_start		/* Image size */
88	.long	_end_header - efi_start		/* Header size */
89	.long	0				/* Checksum */
90	.short	0xa				/* Subsystem (EFI app) */
91	.short	0				/* DLL Characteristics */
92	.long	0				/* Stack reserve */
93	.long	0				/* Stack commit */
94	.long	0				/* Heap reserve */
95	.long	0				/* Heap commit */
96	.long	0				/* Loader flags */
97	.long	6				/* Number of RVAs */
98
99	/* RVAs: */
100	.quad	0
101	.quad	0
102	.quad	0
103	.quad	0
104	.quad	0
105	.quad	0
106
107section_table:
108	/* We need a .reloc section for EFI */
109	.ascii	".reloc"
110	.byte	0
111	.byte	0				/* Pad to 8 bytes */
112	.long	0				/* Virtual size */
113	.long	0				/* Virtual address */
114	.long	0				/* Size of raw data */
115	.long	0				/* Pointer to raw data */
116	.long	0				/* Pointer to relocations */
117	.long	0				/* Pointer to line numbers */
118	.short	0				/* Number of relocations */
119	.short	0				/* Number of line numbers */
120	.long	(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | \
121		 IMAGE_SCN_MEM_DISCARDABLE)	/* Characteristics */
122
123	/* The contents of the loader */
124	.ascii	".text"
125	.byte	0
126	.byte	0
127	.byte	0				/* Pad to 8 bytes */
128	.long	_edata - _end_header		/* Virtual size */
129	.long	_end_header - efi_start		/* Virtual address */
130	.long	_edata - _end_header		/* Size of raw data */
131	.long	_end_header - efi_start		/* Pointer to raw data */
132	.long	0				/* Pointer to relocations */
133	.long	0				/* Pointer to line numbers */
134	.short	0				/* Number of relocations */
135	.short	0				/* Number of line numbers */
136	.long	(IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | \
137		 IMAGE_SCN_MEM_READ)		/* Characteristics */
138_end_header:
139
140	.text
141	.globl	_start
142_start:
143	/* Save the boot params to the stack */
144	push	{r0, r1}
145
146	adr	r0, .Lbase
147	ldr	r1, [r0]
148	sub	r5, r0, r1
149
150	ldr	r0, .Limagebase
151	add	r0, r0, r5
152	ldr	r1, .Ldynamic
153	add	r1, r1, r5
154
155	bl	_C_LABEL(self_reloc)
156
157	/* Zero the BSS, _reloc fixed the values for us */
158	ldr	r0, .Lbss
159	ldr	r1, .Lbssend
160	mov	r2, #0
161
1621:	cmp	r0, r1
163	bge	2f
164	str	r2, [r0], #4
165	b	1b
1662:
167
168	pop	{r0, r1}
169	bl	_C_LABEL(efi_main)
170
1711:	b	1b
172
173.Lbase:
174	.word	.
175.Limagebase:
176	.word	ImageBase
177.Ldynamic:
178	.word	_DYNAMIC
179.Lbss:
180	.word	__bss_start
181.Lbssend:
182	.word	__bss_end
183
184.align	3
185stack:
186	.space 512
187stack_end:
188
189