1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2020 Mitchell Horne <mhorne@FreeBSD.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, 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$
28 */
29
30	.section	.text.head
31
32	/*
33	 * Magic "MZ" signature for PE/COFF
34	 */
35	.globl	ImageBase
36ImageBase:
37	.ascii	"MZ"
38	.skip	58				// 'MZ' + pad + offset == 64
39	.long	pe_header - ImageBase		// Offset to the PE header.
40pe_header:
41	.ascii	"PE"
42	.short 	0
43coff_header:
44	.short	0x5064			// RISCV64
45	.short	2				// nr_sections
46	.long	0 				// TimeDateStamp
47	.long	0				// PointerToSymbolTable
48	.long	0				// NumberOfSymbols
49	.short	section_table - optional_header	// SizeOfOptionalHeader
50	.short	0x20e			// Characteristics.
51							// IMAGE_FILE_DEBUG_STRIPPED |
52							// IMAGE_FILE_EXECUTABLE_IMAGE |
53							// IMAGE_FILE_LOCAL_SYMS_STRIPPED |
54							// IMAGE_FILE_LINE_NUMS_STRIPPED
55optional_header:
56	.short	0x20b				// PE32+ format
57	.byte	0x02				// MajorLinkerVersion
58	.byte	0x14				// MinorLinkerVersion
59	.long	_edata - _start			// SizeOfCode
60	.long	0				// SizeOfInitializedData
61	.long	0				// SizeOfUninitializedData
62	.long	_start - ImageBase		// AddressOfEntryPoint
63	.long	_start - ImageBase		// BaseOfCode
64
65extra_header_fields:
66	.quad	0				// ImageBase
67	.long	32				// SectionAlignment
68	.long	8				// FileAlignment
69
70	.short	0				// MajorOperatingSystemVersion
71	.short	0				// MinorOperatingSystemVersion
72	.short	0				// MajorImageVersion
73	.short	0				// MinorImageVersion
74	.short	0				// MajorSubsystemVersion
75	.short	0				// MinorSubsystemVersion
76	.long	0				// Win32VersionValue
77
78	.long	_edata - ImageBase		// SizeOfImage
79
80	// Everything before the kernel image is considered part of the header
81	.long	_start - ImageBase		// SizeOfHeaders
82	.long	0				// CheckSum
83	.short	10				// Subsystem (EFI)
84	.short	0				// DllCharacteristics
85	.quad	0				// SizeOfStackReserve
86	.quad	0				// SizeOfStackCommit
87	.quad	0				// SizeOfHeapReserve
88	.quad	0				// SizeOfHeapCommit
89	.long	0				// LoaderFlags
90	.long	16				// NumberOfRvaAndSizes
91
92	.quad	0				// ExportTable
93	.quad	0				// ImportTable
94	.quad	0				// ResourceTable
95	.quad	0				// ExceptionTable
96	.quad	0				// CertificationTable
97	.quad	0				// BaseRelocationTable
98	.quad	0				// Debug
99	.quad	0				// Architecture
100	.quad	0				// Global Ptr
101	.quad	0				// TLS Table
102	.quad	0				// Load Config Table
103	.quad	0				// Bound Import
104	.quad	0				// IAT
105	.quad	0				// Delay Import Descriptor
106	.quad	0				// CLR Runtime Header
107	.quad	0				// Reserved
108
109	// Section table
110section_table:
111
112	/*
113	 * The EFI application loader requires a relocation section
114	 * because EFI applications must be relocatable.  This is a
115	 * dummy section as far as we are concerned.
116	 */
117	.ascii	".reloc"
118	.byte	0
119	.byte	0			// end of 0 padding of section name
120	.long	0
121	.long	0
122	.long	0			// SizeOfRawData
123	.long	0			// PointerToRawData
124	.long	0			// PointerToRelocations
125	.long	0			// PointerToLineNumbers
126	.short	0			// NumberOfRelocations
127	.short	0			// NumberOfLineNumbers
128	.long	0x42100040		// Characteristics (section flags)
129
130
131	.ascii	".text"
132	.byte	0
133	.byte	0
134	.byte	0        		// end of 0 padding of section name
135	.long	_edata - _start		// VirtualSize
136	.long	_start - ImageBase	// VirtualAddress
137	.long	_edata - _start		// SizeOfRawData
138	.long	_start - ImageBase	// PointerToRawData
139
140	.long	0		// PointerToRelocations (0 for executables)
141	.long	0		// PointerToLineNumbers (0 for executables)
142	.short	0		// NumberOfRelocations  (0 for executables)
143	.short	0		// NumberOfLineNumbers  (0 for executables)
144	.long	0xe0500020	// Characteristics (section flags)
145
146	.globl _start
147_start:
148	/* Save boot parameters to the stack */
149	addi		sp, sp, -24
150	sd			a0, 0(sp)
151	sd			a1, 8(sp)
152	sd			ra, 16(sp)
153
154	/* Run relocation */
155	lla			a0, ImageBase
156	lla			a1, _DYNAMIC
157	call		_relocate
158	bne			a0, zero, 0f
159
160	/* Call EFI code */
161	ld			a1, 8(sp)
162	ld			a0, 0(sp)
163	call		efi_main
164
165	ld			ra, 16(sp)
166
1670:	addi		sp, sp, 24
168	ret
169