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