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