xref: /netbsd-src/sys/external/bsd/gnu-efi/dist/gnuefi/crt0-efi-riscv64.S (revision 3587d6f89c746bbb4f886219ddacd41ace480ecf)
1/*	$NetBSD: crt0-efi-riscv64.S,v 1.1.1.1 2021/09/30 18:50:09 jmcneill Exp $	*/
2
3/* SPDX-License-Identifier: GPL-2.0+ */
4/*
5 * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
6 * Copright (C) 2018 Alexander Graf <agraf@suse.de>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice and this list of conditions, without modification.
13 * 2. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * Alternatively, this software may be distributed under the terms of the
17 * GNU General Public License as published by the Free Software Foundation;
18 * either version 2 of the License, or (at your option) any later version.
19 */
20
21#ifndef EFI_SUBSYSTEM
22#define EFI_SUBSYSTEM 10
23#endif
24
25	.section	.text.head
26
27	/*
28	 * Magic "MZ" signature for PE/COFF
29	 */
30	.globl	ImageBase
31ImageBase:
32	.ascii	"MZ"
33	.skip	58				// 'MZ' + pad + offset == 64
34	.long	pe_header - ImageBase		// Offset to the PE header.
35pe_header:
36	.ascii	"PE"
37	.short 	0
38coff_header:
39	.short	0x5064				// riscv64
40	.short	2				// nr_sections
41	.long	0 				// TimeDateStamp
42	.long	0				// PointerToSymbolTable
43	.long	0				// NumberOfSymbols
44	.short	section_table - optional_header	// SizeOfOptionalHeader
45	.short	0x206				// Characteristics.
46						// IMAGE_FILE_DEBUG_STRIPPED |
47						// IMAGE_FILE_EXECUTABLE_IMAGE |
48						// IMAGE_FILE_LINE_NUMS_STRIPPED
49optional_header:
50	.short	0x20b				// PE32+ format
51	.byte	0x02				// MajorLinkerVersion
52	.byte	0x14				// MinorLinkerVersion
53	.long	_data - _start			// SizeOfCode
54	.long	_data_size			// SizeOfInitializedData
55	.long	0				// SizeOfUninitializedData
56	.long	_start - ImageBase		// AddressOfEntryPoint
57	.long	_start - ImageBase		// BaseOfCode
58
59extra_header_fields:
60	.quad	0				// ImageBase
61	.long	0x1000				// SectionAlignment
62	.long	0x200				// FileAlignment
63	.short	0				// MajorOperatingSystemVersion
64	.short	0				// MinorOperatingSystemVersion
65	.short	0				// MajorImageVersion
66	.short	0				// MinorImageVersion
67	.short	0				// MajorSubsystemVersion
68	.short	0				// MinorSubsystemVersion
69	.long	0				// Win32VersionValue
70
71	.long	_edata - ImageBase		// SizeOfImage
72
73	// Everything before the kernel image is considered part of the header
74	.long	_start - ImageBase		// SizeOfHeaders
75	.long	0				// CheckSum
76	.short	EFI_SUBSYSTEM			// Subsystem
77	.short	0				// DllCharacteristics
78	.quad	0				// SizeOfStackReserve
79	.quad	0				// SizeOfStackCommit
80	.quad	0				// SizeOfHeapReserve
81	.quad	0				// SizeOfHeapCommit
82	.long	0				// LoaderFlags
83	.long	0x6				// NumberOfRvaAndSizes
84
85	.quad	0				// ExportTable
86	.quad	0				// ImportTable
87	.quad	0				// ResourceTable
88	.quad	0				// ExceptionTable
89	.quad	0				// CertificationTable
90	.quad	0				// BaseRelocationTable
91
92	// Section table
93section_table:
94	/*
95	 * The EFI application loader requires a relocation section
96	 * because EFI applications must be relocatable.  This is a
97	 * dummy section as far as we are concerned.
98	 */
99	.ascii	".reloc\0\0"
100	.long	0
101	.long	0
102	.long	0				// SizeOfRawData
103	.long	0				// PointerToRawData
104	.long	0				// PointerToRelocations
105	.long	0				// PointerToLineNumbers
106	.short	0				// NumberOfRelocations
107	.short	0				// NumberOfLineNumbers
108	.long	0x42100040			// Characteristics (section flags)
109
110	.ascii	".text\0\0\0"
111	.long	_edata - _start			// VirtualSize
112	.long	_start - ImageBase		// VirtualAddress
113	.long	_edata - _start			// SizeOfRawData
114	.long	_start - ImageBase		// PointerToRawData
115
116	.long	0				// PointerToRelocations (0 for executables)
117	.long	0				// PointerToLineNumbers (0 for executables)
118	.short	0				// NumberOfRelocations  (0 for executables)
119	.short	0				// NumberOfLineNumbers  (0 for executables)
120	.long	0xe0500020			// Characteristics (section flags)
121
122	.align	12
123	.globl _start
124_start:
125	addi		sp, sp, -24
126	sd		a0, 0(sp)
127	sd		a1, 8(sp)
128	sd		ra, 16(sp)
129	lla		a0, ImageBase
130	lla		a1, _DYNAMIC
131	call		_relocate
132	bne		a0, zero, 0f
133	ld		a1, 8(sp)
134	ld		a0, 0(sp)
135	call		efi_main
136	ld		ra, 16(sp)
1370:	addi		sp, sp, 24
138	ret
139