1/*	$NetBSD: crt0-efi-mips64el.S,v 1.1.1.2 2021/09/30 18:50:09 jmcneill Exp $	*/
2
3/*
4 * crt0-efi-mips64el.S - PE/COFF header for MIPS64 EFI applications
5 *
6 * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
7 * Copright (C) 2017 Heiher <r@hev.cc>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice and this list of conditions, without modification.
14 * 2. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * Alternatively, this software may be distributed under the terms of the
18 * GNU General Public License as published by the Free Software Foundation;
19 * either version 2 of the License, or (at your option) any later version.
20 */
21
22	.section	.text.head
23
24	/*
25	 * Magic "MZ" signature for PE/COFF
26	 */
27	.globl	ImageBase
28ImageBase:
29	.ascii	"MZ"
30	.skip	58				// 'MZ' + pad + offset == 64
31	.long	pe_header - ImageBase		// Offset to the PE header.
32pe_header:
33	.ascii	"PE"
34	.short 	0
35coff_header:
36	.short	0x166				// MIPS little endian
37	.short	2				// nr_sections
38	.long	0 				// TimeDateStamp
39	.long	0				// PointerToSymbolTable
40	.long	0				// NumberOfSymbols
41	.short	section_table - optional_header	// SizeOfOptionalHeader
42	.short	0x206				// Characteristics.
43						// IMAGE_FILE_DEBUG_STRIPPED |
44						// IMAGE_FILE_EXECUTABLE_IMAGE |
45						// IMAGE_FILE_LINE_NUMS_STRIPPED
46optional_header:
47	.short	0x20b				// PE32+ format
48	.byte	0x02				// MajorLinkerVersion
49	.byte	0x14				// MinorLinkerVersion
50	.long	_edata - _start			// SizeOfCode
51	.long	0				// SizeOfInitializedData
52	.long	0				// SizeOfUninitializedData
53	.long	_start - ImageBase		// AddressOfEntryPoint
54	.long	_start - ImageBase		// BaseOfCode
55
56extra_header_fields:
57	.quad	0				// ImageBase
58	.long	0x20				// SectionAlignment
59	.long	0x8				// FileAlignment
60	.short	0				// MajorOperatingSystemVersion
61	.short	0				// MinorOperatingSystemVersion
62	.short	0				// MajorImageVersion
63	.short	0				// MinorImageVersion
64	.short	0				// MajorSubsystemVersion
65	.short	0				// MinorSubsystemVersion
66	.long	0				// Win32VersionValue
67
68	.long	_edata - ImageBase		// SizeOfImage
69
70	// Everything before the kernel image is considered part of the header
71	.long	_start - ImageBase		// SizeOfHeaders
72	.long	0				// CheckSum
73	.short	EFI_SUBSYSTEM			// Subsystem
74	.short	0				// DllCharacteristics
75	.quad	0				// SizeOfStackReserve
76	.quad	0				// SizeOfStackCommit
77	.quad	0				// SizeOfHeapReserve
78	.quad	0				// SizeOfHeapCommit
79	.long	0				// LoaderFlags
80	.long	0x6				// NumberOfRvaAndSizes
81
82	.quad	0				// ExportTable
83	.quad	0				// ImportTable
84	.quad	0				// ResourceTable
85	.quad	0				// ExceptionTable
86	.quad	0				// CertificationTable
87	.quad	0				// BaseRelocationTable
88
89	// Section table
90section_table:
91
92	/*
93	 * The EFI application loader requires a relocation section
94	 * because EFI applications must be relocatable.  This is a
95	 * dummy section as far as we are concerned.
96	 */
97	.ascii	".reloc"
98	.byte	0
99	.byte	0			// end of 0 padding of section name
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
111	.ascii	".text"
112	.byte	0
113	.byte	0
114	.byte	0        		// end of 0 padding of section name
115	.long	_edata - _start		// VirtualSize
116	.long	_start - ImageBase	// VirtualAddress
117	.long	_edata - _start		// SizeOfRawData
118	.long	_start - ImageBase	// PointerToRawData
119
120	.long	0		// PointerToRelocations (0 for executables)
121	.long	0		// PointerToLineNumbers (0 for executables)
122	.short	0		// NumberOfRelocations  (0 for executables)
123	.short	0		// NumberOfLineNumbers  (0 for executables)
124	.long	0xe0500020	// Characteristics (section flags)
125
126	.set		push
127	.set		noreorder
128	.align		4
129
130	.globl	_start
131	.ent	_start
132	.type	_start, @function
133_start:
134	daddiu		$sp, -32
135	sd		$ra, ($sp)
136
137	// Get pc & gp
138	.align		3
139	bal		1f
140	sd		$gp, 8($sp)
141_pc:
142	.dword		_gp
143	.dword		_DYNAMIC
144	.dword		_relocate
1451:
146	// pc in ra
147	ld		$gp, ($ra)
148	dli		$t0, _pc
149	dsubu		$gp, $t0
150	daddu		$gp, $ra
151
152	sd		$a0, 16($sp)
153	sd		$a1, 24($sp)
154
155	// a2: ImageHandle
156	move		$a2, $a0
157	// a3: SystemTable
158	move		$a3, $a1
159	// a0: ImageBase
160	dli		$t1, ImageBase - _pc
161	daddu		$a0, $ra, $t1
162	// a1: DynamicSection
163	ld		$t1, 8($ra)
164	dsubu		$t1, $t0
165	daddu		$a1, $ra, $t1
166	// call _relocate
167	ld		$t1, 16($ra)
168	dsubu		$t1, $t0
169	daddu		$t9, $ra, $t1
170	jalr		$t9
171	nop
172	bnez		$v0, 1b
173	nop
174
175	// a0: ImageHandle
176	ld		$a0, 16($sp)
177	// call efi_main
178	dla		$t9, efi_main
179	jalr		$t9
180	// a1: SystemTable
181	ld		$a1, 24($sp)
182
1831:
184	ld		$gp, 8($sp)
185	ld		$ra, ($sp)
186	jr		$ra
187	daddiu		$sp, 32
188	.end		_start
189
190	.set		pop
191