18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2013-2017 Linaro Ltd 48c2ecf20Sopenharmony_ci * Authors: Roy Franz <roy.franz@linaro.org> 58c2ecf20Sopenharmony_ci * Ard Biesheuvel <ard.biesheuvel@linaro.org> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/pe.h> 98c2ecf20Sopenharmony_ci#include <linux/sizes.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci .macro __nop 128c2ecf20Sopenharmony_ci AR_CLASS( mov r0, r0 ) 138c2ecf20Sopenharmony_ci M_CLASS( nop.w ) 148c2ecf20Sopenharmony_ci .endm 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci .macro __initial_nops 178c2ecf20Sopenharmony_ci#ifdef CONFIG_EFI_STUB 188c2ecf20Sopenharmony_ci @ This is a two-instruction NOP, which happens to bear the 198c2ecf20Sopenharmony_ci @ PE/COFF signature "MZ" in the first two bytes, so the kernel 208c2ecf20Sopenharmony_ci @ is accepted as an EFI binary. Booting via the UEFI stub 218c2ecf20Sopenharmony_ci @ will not execute those instructions, but the ARM/Linux 228c2ecf20Sopenharmony_ci @ boot protocol does, so we need some NOPs here. 238c2ecf20Sopenharmony_ci .inst MZ_MAGIC | (0xe225 << 16) @ eor r5, r5, 0x4d000 248c2ecf20Sopenharmony_ci eor r5, r5, 0x4d000 @ undo previous insn 258c2ecf20Sopenharmony_ci#else 268c2ecf20Sopenharmony_ci __nop 278c2ecf20Sopenharmony_ci __nop 288c2ecf20Sopenharmony_ci#endif 298c2ecf20Sopenharmony_ci .endm 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci .macro __EFI_HEADER 328c2ecf20Sopenharmony_ci#ifdef CONFIG_EFI_STUB 338c2ecf20Sopenharmony_ci .set start_offset, __efi_start - start 348c2ecf20Sopenharmony_ci .org start + 0x3c 358c2ecf20Sopenharmony_ci @ 368c2ecf20Sopenharmony_ci @ The PE header can be anywhere in the file, but for 378c2ecf20Sopenharmony_ci @ simplicity we keep it together with the MSDOS header 388c2ecf20Sopenharmony_ci @ The offset to the PE/COFF header needs to be at offset 398c2ecf20Sopenharmony_ci @ 0x3C in the MSDOS header. 408c2ecf20Sopenharmony_ci @ The only 2 fields of the MSDOS header that are used are this 418c2ecf20Sopenharmony_ci @ PE/COFF offset, and the "MZ" bytes at offset 0x0. 428c2ecf20Sopenharmony_ci @ 438c2ecf20Sopenharmony_ci .long pe_header - start @ Offset to the PE header. 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_cipe_header: 468c2ecf20Sopenharmony_ci .long PE_MAGIC 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cicoff_header: 498c2ecf20Sopenharmony_ci .short IMAGE_FILE_MACHINE_THUMB @ Machine 508c2ecf20Sopenharmony_ci .short section_count @ NumberOfSections 518c2ecf20Sopenharmony_ci .long 0 @ TimeDateStamp 528c2ecf20Sopenharmony_ci .long 0 @ PointerToSymbolTable 538c2ecf20Sopenharmony_ci .long 0 @ NumberOfSymbols 548c2ecf20Sopenharmony_ci .short section_table - optional_header @ SizeOfOptionalHeader 558c2ecf20Sopenharmony_ci .short IMAGE_FILE_32BIT_MACHINE | \ 568c2ecf20Sopenharmony_ci IMAGE_FILE_DEBUG_STRIPPED | \ 578c2ecf20Sopenharmony_ci IMAGE_FILE_EXECUTABLE_IMAGE | \ 588c2ecf20Sopenharmony_ci IMAGE_FILE_LINE_NUMS_STRIPPED @ Characteristics 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#define __pecoff_code_size (__pecoff_data_start - __efi_start) 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_cioptional_header: 638c2ecf20Sopenharmony_ci .short PE_OPT_MAGIC_PE32 @ PE32 format 648c2ecf20Sopenharmony_ci .byte 0x02 @ MajorLinkerVersion 658c2ecf20Sopenharmony_ci .byte 0x14 @ MinorLinkerVersion 668c2ecf20Sopenharmony_ci .long __pecoff_code_size @ SizeOfCode 678c2ecf20Sopenharmony_ci .long __pecoff_data_size @ SizeOfInitializedData 688c2ecf20Sopenharmony_ci .long 0 @ SizeOfUninitializedData 698c2ecf20Sopenharmony_ci .long efi_pe_entry - start @ AddressOfEntryPoint 708c2ecf20Sopenharmony_ci .long start_offset @ BaseOfCode 718c2ecf20Sopenharmony_ci .long __pecoff_data_start - start @ BaseOfData 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ciextra_header_fields: 748c2ecf20Sopenharmony_ci .long 0 @ ImageBase 758c2ecf20Sopenharmony_ci .long SZ_4K @ SectionAlignment 768c2ecf20Sopenharmony_ci .long SZ_512 @ FileAlignment 778c2ecf20Sopenharmony_ci .short 0 @ MajorOsVersion 788c2ecf20Sopenharmony_ci .short 0 @ MinorOsVersion 798c2ecf20Sopenharmony_ci .short LINUX_EFISTUB_MAJOR_VERSION @ MajorImageVersion 808c2ecf20Sopenharmony_ci .short LINUX_EFISTUB_MINOR_VERSION @ MinorImageVersion 818c2ecf20Sopenharmony_ci .short 0 @ MajorSubsystemVersion 828c2ecf20Sopenharmony_ci .short 0 @ MinorSubsystemVersion 838c2ecf20Sopenharmony_ci .long 0 @ Win32VersionValue 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci .long __pecoff_end - start @ SizeOfImage 868c2ecf20Sopenharmony_ci .long start_offset @ SizeOfHeaders 878c2ecf20Sopenharmony_ci .long 0 @ CheckSum 888c2ecf20Sopenharmony_ci .short IMAGE_SUBSYSTEM_EFI_APPLICATION @ Subsystem 898c2ecf20Sopenharmony_ci .short 0 @ DllCharacteristics 908c2ecf20Sopenharmony_ci .long 0 @ SizeOfStackReserve 918c2ecf20Sopenharmony_ci .long 0 @ SizeOfStackCommit 928c2ecf20Sopenharmony_ci .long 0 @ SizeOfHeapReserve 938c2ecf20Sopenharmony_ci .long 0 @ SizeOfHeapCommit 948c2ecf20Sopenharmony_ci .long 0 @ LoaderFlags 958c2ecf20Sopenharmony_ci .long (section_table - .) / 8 @ NumberOfRvaAndSizes 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci .quad 0 @ ExportTable 988c2ecf20Sopenharmony_ci .quad 0 @ ImportTable 998c2ecf20Sopenharmony_ci .quad 0 @ ResourceTable 1008c2ecf20Sopenharmony_ci .quad 0 @ ExceptionTable 1018c2ecf20Sopenharmony_ci .quad 0 @ CertificationTable 1028c2ecf20Sopenharmony_ci .quad 0 @ BaseRelocationTable 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cisection_table: 1058c2ecf20Sopenharmony_ci .ascii ".text\0\0\0" 1068c2ecf20Sopenharmony_ci .long __pecoff_code_size @ VirtualSize 1078c2ecf20Sopenharmony_ci .long __efi_start @ VirtualAddress 1088c2ecf20Sopenharmony_ci .long __pecoff_code_size @ SizeOfRawData 1098c2ecf20Sopenharmony_ci .long __efi_start @ PointerToRawData 1108c2ecf20Sopenharmony_ci .long 0 @ PointerToRelocations 1118c2ecf20Sopenharmony_ci .long 0 @ PointerToLineNumbers 1128c2ecf20Sopenharmony_ci .short 0 @ NumberOfRelocations 1138c2ecf20Sopenharmony_ci .short 0 @ NumberOfLineNumbers 1148c2ecf20Sopenharmony_ci .long IMAGE_SCN_CNT_CODE | \ 1158c2ecf20Sopenharmony_ci IMAGE_SCN_MEM_READ | \ 1168c2ecf20Sopenharmony_ci IMAGE_SCN_MEM_EXECUTE @ Characteristics 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci .ascii ".data\0\0\0" 1198c2ecf20Sopenharmony_ci .long __pecoff_data_size @ VirtualSize 1208c2ecf20Sopenharmony_ci .long __pecoff_data_start - start @ VirtualAddress 1218c2ecf20Sopenharmony_ci .long __pecoff_data_rawsize @ SizeOfRawData 1228c2ecf20Sopenharmony_ci .long __pecoff_data_start - start @ PointerToRawData 1238c2ecf20Sopenharmony_ci .long 0 @ PointerToRelocations 1248c2ecf20Sopenharmony_ci .long 0 @ PointerToLineNumbers 1258c2ecf20Sopenharmony_ci .short 0 @ NumberOfRelocations 1268c2ecf20Sopenharmony_ci .short 0 @ NumberOfLineNumbers 1278c2ecf20Sopenharmony_ci .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ 1288c2ecf20Sopenharmony_ci IMAGE_SCN_MEM_READ | \ 1298c2ecf20Sopenharmony_ci IMAGE_SCN_MEM_WRITE @ Characteristics 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci .set section_count, (. - section_table) / 40 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci .align 12 1348c2ecf20Sopenharmony_ci__efi_start: 1358c2ecf20Sopenharmony_ci#endif 1368c2ecf20Sopenharmony_ci .endm 137