1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Copyright (C) 2013-2017 Linaro Ltd
4 * Authors: Roy Franz <roy.franz@linaro.org>
5 *          Ard Biesheuvel <ard.biesheuvel@linaro.org>
6 */
7
8#include <linux/pe.h>
9#include <linux/sizes.h>
10
11		.macro	__nop
12 AR_CLASS(	mov	r0, r0		)
13  M_CLASS(	nop.w			)
14		.endm
15
16		.macro __initial_nops
17#ifdef CONFIG_EFI_STUB
18		@ This is a two-instruction NOP, which happens to bear the
19		@ PE/COFF signature "MZ" in the first two bytes, so the kernel
20		@ is accepted as an EFI binary. Booting via the UEFI stub
21		@ will not execute those instructions, but the ARM/Linux
22		@ boot protocol does, so we need some NOPs here.
23		.inst	MZ_MAGIC | (0xe225 << 16)	@ eor r5, r5, 0x4d000
24		eor	r5, r5, 0x4d000			@ undo previous insn
25#else
26		__nop
27		__nop
28#endif
29		.endm
30
31		.macro	__EFI_HEADER
32#ifdef CONFIG_EFI_STUB
33		.set	start_offset, __efi_start - start
34		.org	start + 0x3c
35		@
36		@ The PE header can be anywhere in the file, but for
37		@ simplicity we keep it together with the MSDOS header
38		@ The offset to the PE/COFF header needs to be at offset
39		@ 0x3C in the MSDOS header.
40		@ The only 2 fields of the MSDOS header that are used are this
41		@ PE/COFF offset, and the "MZ" bytes at offset 0x0.
42		@
43		.long	pe_header - start		@ Offset to the PE header.
44
45pe_header:
46		.long	PE_MAGIC
47
48coff_header:
49		.short	IMAGE_FILE_MACHINE_THUMB	@ Machine
50		.short	section_count			@ NumberOfSections
51		.long	0 				@ TimeDateStamp
52		.long	0				@ PointerToSymbolTable
53		.long	0				@ NumberOfSymbols
54		.short	section_table - optional_header	@ SizeOfOptionalHeader
55		.short	IMAGE_FILE_32BIT_MACHINE | \
56			IMAGE_FILE_DEBUG_STRIPPED | \
57			IMAGE_FILE_EXECUTABLE_IMAGE | \
58			IMAGE_FILE_LINE_NUMS_STRIPPED	@ Characteristics
59
60#define __pecoff_code_size (__pecoff_data_start - __efi_start)
61
62optional_header:
63		.short	PE_OPT_MAGIC_PE32		@ PE32 format
64		.byte	0x02				@ MajorLinkerVersion
65		.byte	0x14				@ MinorLinkerVersion
66		.long	__pecoff_code_size		@ SizeOfCode
67		.long	__pecoff_data_size		@ SizeOfInitializedData
68		.long	0				@ SizeOfUninitializedData
69		.long	efi_pe_entry - start		@ AddressOfEntryPoint
70		.long	start_offset			@ BaseOfCode
71		.long	__pecoff_data_start - start	@ BaseOfData
72
73extra_header_fields:
74		.long	0				@ ImageBase
75		.long	SZ_4K				@ SectionAlignment
76		.long	SZ_512				@ FileAlignment
77		.short	0				@ MajorOsVersion
78		.short	0				@ MinorOsVersion
79		.short	LINUX_EFISTUB_MAJOR_VERSION	@ MajorImageVersion
80		.short	LINUX_EFISTUB_MINOR_VERSION	@ MinorImageVersion
81		.short	0				@ MajorSubsystemVersion
82		.short	0				@ MinorSubsystemVersion
83		.long	0				@ Win32VersionValue
84
85		.long	__pecoff_end - start		@ SizeOfImage
86		.long	start_offset			@ SizeOfHeaders
87		.long	0				@ CheckSum
88		.short	IMAGE_SUBSYSTEM_EFI_APPLICATION	@ Subsystem
89		.short	0				@ DllCharacteristics
90		.long	0				@ SizeOfStackReserve
91		.long	0				@ SizeOfStackCommit
92		.long	0				@ SizeOfHeapReserve
93		.long	0				@ SizeOfHeapCommit
94		.long	0				@ LoaderFlags
95		.long	(section_table - .) / 8		@ NumberOfRvaAndSizes
96
97		.quad	0				@ ExportTable
98		.quad	0				@ ImportTable
99		.quad	0				@ ResourceTable
100		.quad	0				@ ExceptionTable
101		.quad	0				@ CertificationTable
102		.quad	0				@ BaseRelocationTable
103
104section_table:
105		.ascii	".text\0\0\0"
106		.long	__pecoff_code_size		@ VirtualSize
107		.long	__efi_start			@ VirtualAddress
108		.long	__pecoff_code_size		@ SizeOfRawData
109		.long	__efi_start			@ PointerToRawData
110		.long	0				@ PointerToRelocations
111		.long	0				@ PointerToLineNumbers
112		.short	0				@ NumberOfRelocations
113		.short	0				@ NumberOfLineNumbers
114		.long	IMAGE_SCN_CNT_CODE | \
115			IMAGE_SCN_MEM_READ | \
116			IMAGE_SCN_MEM_EXECUTE		@ Characteristics
117
118		.ascii	".data\0\0\0"
119		.long	__pecoff_data_size		@ VirtualSize
120		.long	__pecoff_data_start - start	@ VirtualAddress
121		.long	__pecoff_data_rawsize		@ SizeOfRawData
122		.long	__pecoff_data_start - start	@ PointerToRawData
123		.long	0				@ PointerToRelocations
124		.long	0				@ PointerToLineNumbers
125		.short	0				@ NumberOfRelocations
126		.short	0				@ NumberOfLineNumbers
127		.long	IMAGE_SCN_CNT_INITIALIZED_DATA | \
128			IMAGE_SCN_MEM_READ | \
129			IMAGE_SCN_MEM_WRITE		@ Characteristics
130
131		.set	section_count, (. - section_table) / 40
132
133		.align	12
134__efi_start:
135#endif
136		.endm
137