xref: /DragonBoot/src/arch/riscv64/crt0-efi-riscv64.S (revision 1971aeeee1b8252821527c319e1327d93b88ffc4)
1abdb84b8SLoGin/*-
2abdb84b8SLoGin * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3abdb84b8SLoGin *
4abdb84b8SLoGin * Copyright (c) 2020 Mitchell Horne <mhorne@FreeBSD.org>
5abdb84b8SLoGin *
6abdb84b8SLoGin * Redistribution and use in source and binary forms, with or without
7abdb84b8SLoGin * modification, are permitted provided that the following conditions
8abdb84b8SLoGin * are met:
9abdb84b8SLoGin * 1. Redistributions of source code must retain the above copyright
10abdb84b8SLoGin *    notice, this list of conditions and the following disclaimer.
11abdb84b8SLoGin * 2. Redistributions in binary form must reproduce the above copyright
12abdb84b8SLoGin *    notice, this list of conditions and the following disclaimer in the
13abdb84b8SLoGin *    documentation and/or other materials provided with the distribution.
14abdb84b8SLoGin *
15abdb84b8SLoGin * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16abdb84b8SLoGin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17abdb84b8SLoGin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18abdb84b8SLoGin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19abdb84b8SLoGin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20abdb84b8SLoGin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21abdb84b8SLoGin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22abdb84b8SLoGin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23abdb84b8SLoGin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24abdb84b8SLoGin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25abdb84b8SLoGin * SUCH DAMAGE.
26abdb84b8SLoGin *
27abdb84b8SLoGin * $FreeBSD$
28abdb84b8SLoGin */
29abdb84b8SLoGin
30abdb84b8SLoGin	.section	.text.head
31abdb84b8SLoGin
32abdb84b8SLoGin	/*
33abdb84b8SLoGin	 * Magic "MZ" signature for PE/COFF
34abdb84b8SLoGin	 */
35abdb84b8SLoGin	.globl	ImageBase
36abdb84b8SLoGinImageBase:
37abdb84b8SLoGin	.ascii	"MZ"
38abdb84b8SLoGin	.skip	58				// 'MZ' + pad + offset == 64
39abdb84b8SLoGin	.long	pe_header - ImageBase		// Offset to the PE header.
40abdb84b8SLoGinpe_header:
41abdb84b8SLoGin	.ascii	"PE"
42abdb84b8SLoGin	.short 	0
43abdb84b8SLoGincoff_header:
44abdb84b8SLoGin	.short	0x5064			// RISCV64
45abdb84b8SLoGin	.short	2				// nr_sections
46abdb84b8SLoGin	.long	0 				// TimeDateStamp
47abdb84b8SLoGin	.long	0				// PointerToSymbolTable
48abdb84b8SLoGin	.long	0				// NumberOfSymbols
49abdb84b8SLoGin	.short	section_table - optional_header	// SizeOfOptionalHeader
50abdb84b8SLoGin	.short	0x20e			// Characteristics.
51abdb84b8SLoGin							// IMAGE_FILE_DEBUG_STRIPPED |
52abdb84b8SLoGin							// IMAGE_FILE_EXECUTABLE_IMAGE |
53abdb84b8SLoGin							// IMAGE_FILE_LOCAL_SYMS_STRIPPED |
54abdb84b8SLoGin							// IMAGE_FILE_LINE_NUMS_STRIPPED
55abdb84b8SLoGinoptional_header:
56abdb84b8SLoGin	.short	0x20b				// PE32+ format
57abdb84b8SLoGin	.byte	0x02				// MajorLinkerVersion
58abdb84b8SLoGin	.byte	0x14				// MinorLinkerVersion
59abdb84b8SLoGin	.long	_edata - _start			// SizeOfCode
60abdb84b8SLoGin	.long	0				// SizeOfInitializedData
61abdb84b8SLoGin	.long	0				// SizeOfUninitializedData
62abdb84b8SLoGin	.long	_start - ImageBase		// AddressOfEntryPoint
63abdb84b8SLoGin	.long	_start - ImageBase		// BaseOfCode
64abdb84b8SLoGin
65abdb84b8SLoGinextra_header_fields:
66abdb84b8SLoGin	.quad	0				// ImageBase
67abdb84b8SLoGin	.long	32				// SectionAlignment
68abdb84b8SLoGin	.long	8				// FileAlignment
69abdb84b8SLoGin
70abdb84b8SLoGin	.short	0				// MajorOperatingSystemVersion
71abdb84b8SLoGin	.short	0				// MinorOperatingSystemVersion
72abdb84b8SLoGin	.short	0				// MajorImageVersion
73abdb84b8SLoGin	.short	0				// MinorImageVersion
74abdb84b8SLoGin	.short	0				// MajorSubsystemVersion
75abdb84b8SLoGin	.short	0				// MinorSubsystemVersion
76abdb84b8SLoGin	.long	0				// Win32VersionValue
77abdb84b8SLoGin
78abdb84b8SLoGin	.long	_edata - ImageBase		// SizeOfImage
79abdb84b8SLoGin
80abdb84b8SLoGin	// Everything before the kernel image is considered part of the header
81abdb84b8SLoGin	.long	_start - ImageBase		// SizeOfHeaders
82abdb84b8SLoGin	.long	0				// CheckSum
83abdb84b8SLoGin	.short	10				// Subsystem (EFI)
84abdb84b8SLoGin	.short	0				// DllCharacteristics
85abdb84b8SLoGin	.quad	0				// SizeOfStackReserve
86abdb84b8SLoGin	.quad	0				// SizeOfStackCommit
87abdb84b8SLoGin	.quad	0				// SizeOfHeapReserve
88abdb84b8SLoGin	.quad	0				// SizeOfHeapCommit
89abdb84b8SLoGin	.long	0				// LoaderFlags
90abdb84b8SLoGin	.long	16				// NumberOfRvaAndSizes
91abdb84b8SLoGin
92abdb84b8SLoGin	.quad	0				// ExportTable
93abdb84b8SLoGin	.quad	0				// ImportTable
94abdb84b8SLoGin	.quad	0				// ResourceTable
95abdb84b8SLoGin	.quad	0				// ExceptionTable
96abdb84b8SLoGin	.quad	0				// CertificationTable
97abdb84b8SLoGin	.quad	0				// BaseRelocationTable
98abdb84b8SLoGin	.quad	0				// Debug
99abdb84b8SLoGin	.quad	0				// Architecture
100abdb84b8SLoGin	.quad	0				// Global Ptr
101abdb84b8SLoGin	.quad	0				// TLS Table
102abdb84b8SLoGin	.quad	0				// Load Config Table
103abdb84b8SLoGin	.quad	0				// Bound Import
104abdb84b8SLoGin	.quad	0				// IAT
105abdb84b8SLoGin	.quad	0				// Delay Import Descriptor
106abdb84b8SLoGin	.quad	0				// CLR Runtime Header
107abdb84b8SLoGin	.quad	0				// Reserved
108abdb84b8SLoGin
109abdb84b8SLoGin	// Section table
110abdb84b8SLoGinsection_table:
111abdb84b8SLoGin
112abdb84b8SLoGin	/*
113abdb84b8SLoGin	 * The EFI application loader requires a relocation section
114abdb84b8SLoGin	 * because EFI applications must be relocatable.  This is a
115abdb84b8SLoGin	 * dummy section as far as we are concerned.
116abdb84b8SLoGin	 */
117abdb84b8SLoGin	.ascii	".reloc"
118abdb84b8SLoGin	.byte	0
119abdb84b8SLoGin	.byte	0			// end of 0 padding of section name
120abdb84b8SLoGin	.long	0
121abdb84b8SLoGin	.long	0
122abdb84b8SLoGin	.long	0			// SizeOfRawData
123abdb84b8SLoGin	.long	0			// PointerToRawData
124abdb84b8SLoGin	.long	0			// PointerToRelocations
125abdb84b8SLoGin	.long	0			// PointerToLineNumbers
126abdb84b8SLoGin	.short	0			// NumberOfRelocations
127abdb84b8SLoGin	.short	0			// NumberOfLineNumbers
128abdb84b8SLoGin	.long	0x42100040		// Characteristics (section flags)
129abdb84b8SLoGin
130abdb84b8SLoGin
131abdb84b8SLoGin	.ascii	".text"
132abdb84b8SLoGin	.byte	0
133abdb84b8SLoGin	.byte	0
134abdb84b8SLoGin	.byte	0        		// end of 0 padding of section name
135abdb84b8SLoGin	.long	_edata - _start		// VirtualSize
136abdb84b8SLoGin	.long	_start - ImageBase	// VirtualAddress
137abdb84b8SLoGin	.long	_edata - _start		// SizeOfRawData
138abdb84b8SLoGin	.long	_start - ImageBase	// PointerToRawData
139abdb84b8SLoGin
140abdb84b8SLoGin	.long	0		// PointerToRelocations (0 for executables)
141abdb84b8SLoGin	.long	0		// PointerToLineNumbers (0 for executables)
142abdb84b8SLoGin	.short	0		// NumberOfRelocations  (0 for executables)
143abdb84b8SLoGin	.short	0		// NumberOfLineNumbers  (0 for executables)
144abdb84b8SLoGin	.long	0xe0500020	// Characteristics (section flags)
145abdb84b8SLoGin
146abdb84b8SLoGin	.globl _start
147abdb84b8SLoGin_start:
148abdb84b8SLoGin	/* Save boot parameters to the stack */
149abdb84b8SLoGin	addi		sp, sp, -24
150abdb84b8SLoGin	sd			a0, 0(sp)
151abdb84b8SLoGin	sd			a1, 8(sp)
152abdb84b8SLoGin	sd			ra, 16(sp)
153abdb84b8SLoGin
154abdb84b8SLoGin	/* Run relocation */
155*1971aeeeSLoGin	lla			a0, ImageBase
156*1971aeeeSLoGin	lla			a1, _DYNAMIC
157*1971aeeeSLoGin	call		efi_relocate
158*1971aeeeSLoGin	bne			a0, zero, 0f
159abdb84b8SLoGin
160abdb84b8SLoGin	/* Call EFI code */
161abdb84b8SLoGin	ld			a1, 8(sp)
162abdb84b8SLoGin	ld			a0, 0(sp)
163abdb84b8SLoGin	call	    efi_main
164abdb84b8SLoGin
165abdb84b8SLoGin	ld			ra, 16(sp)
166abdb84b8SLoGin
167abdb84b8SLoGin0:	addi		sp, sp, 24
168abdb84b8SLoGin	ret
169*1971aeeeSLoGin
170*1971aeeeSLoGin_DYNAMIC:
171*1971aeeeSLoGin    .long 0
172