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