^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) * Copyright (C) 2013, 2014 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/pe.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) .macro __EFI_PE_HEADER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) .long PE_MAGIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) coff_header:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) .short IMAGE_FILE_MACHINE_ARM64 // Machine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) .short section_count // NumberOfSections
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) .long 0 // TimeDateStamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) .long 0 // PointerToSymbolTable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) .long 0 // NumberOfSymbols
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) .short section_table - optional_header // SizeOfOptionalHeader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) .short IMAGE_FILE_DEBUG_STRIPPED | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) IMAGE_FILE_EXECUTABLE_IMAGE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) IMAGE_FILE_LINE_NUMS_STRIPPED // Characteristics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) optional_header:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .short PE_OPT_MAGIC_PE32PLUS // PE32+ format
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) .byte 0x02 // MajorLinkerVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .byte 0x14 // MinorLinkerVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) .long __initdata_begin - efi_header_end // SizeOfCode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) .long __pecoff_data_size // SizeOfInitializedData
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) .long 0 // SizeOfUninitializedData
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .long __efistub_efi_pe_entry - _head // AddressOfEntryPoint
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .long efi_header_end - _head // BaseOfCode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) extra_header_fields:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .quad 0 // ImageBase
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) .long SEGMENT_ALIGN // SectionAlignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) .long PECOFF_FILE_ALIGNMENT // FileAlignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .short 0 // MajorOperatingSystemVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .short 0 // MinorOperatingSystemVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .short LINUX_EFISTUB_MAJOR_VERSION // MajorImageVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .short LINUX_EFISTUB_MINOR_VERSION // MinorImageVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .short 0 // MajorSubsystemVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) .short 0 // MinorSubsystemVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) .long 0 // Win32VersionValue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) .long _end - _head // SizeOfImage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) // Everything before the kernel image is considered part of the header
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) .long efi_header_end - _head // SizeOfHeaders
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) .long 0 // CheckSum
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) .short IMAGE_SUBSYSTEM_EFI_APPLICATION // Subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) .short 0 // DllCharacteristics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .quad 0 // SizeOfStackReserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .quad 0 // SizeOfStackCommit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .quad 0 // SizeOfHeapReserve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .quad 0 // SizeOfHeapCommit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .long 0 // LoaderFlags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .long (section_table - .) / 8 // NumberOfRvaAndSizes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) .quad 0 // ExportTable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) .quad 0 // ImportTable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) .quad 0 // ResourceTable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .quad 0 // ExceptionTable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .quad 0 // CertificationTable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .quad 0 // BaseRelocationTable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #ifdef CONFIG_DEBUG_EFI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .long efi_debug_table - _head // DebugTable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .long efi_debug_table_size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) // Section table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) section_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .ascii ".text\0\0\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .long __initdata_begin - efi_header_end // VirtualSize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .long efi_header_end - _head // VirtualAddress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) .long __initdata_begin - efi_header_end // SizeOfRawData
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) .long efi_header_end - _head // PointerToRawData
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) .long 0 // PointerToRelocations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) .long 0 // PointerToLineNumbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) .short 0 // NumberOfRelocations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .short 0 // NumberOfLineNumbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .long IMAGE_SCN_CNT_CODE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) IMAGE_SCN_MEM_READ | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) IMAGE_SCN_MEM_EXECUTE // Characteristics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) .ascii ".data\0\0\0"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) .long __pecoff_data_size // VirtualSize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .long __initdata_begin - _head // VirtualAddress
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .long __pecoff_data_rawsize // SizeOfRawData
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .long __initdata_begin - _head // PointerToRawData
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .long 0 // PointerToRelocations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .long 0 // PointerToLineNumbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) .short 0 // NumberOfRelocations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) .short 0 // NumberOfLineNumbers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) .long IMAGE_SCN_CNT_INITIALIZED_DATA | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) IMAGE_SCN_MEM_READ | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) IMAGE_SCN_MEM_WRITE // Characteristics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) .set section_count, (. - section_table) / 40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #ifdef CONFIG_DEBUG_EFI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * The debug table is referenced via its Relative Virtual Address (RVA),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * which is only defined for those parts of the image that are covered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * by a section declaration. Since this header is not covered by any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * section, the debug table must be emitted elsewhere. So stick it in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * the .init.rodata section instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * Note that the EFI debug entry itself may legally have a zero RVA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * which means we can simply put it right after the section headers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) __INITRODATA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) .align 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) efi_debug_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) // EFI_IMAGE_DEBUG_DIRECTORY_ENTRY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .long 0 // Characteristics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .long 0 // TimeDateStamp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .short 0 // MajorVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .short 0 // MinorVersion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .long IMAGE_DEBUG_TYPE_CODEVIEW // Type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .long efi_debug_entry_size // SizeOfData
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) .long 0 // RVA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) .long efi_debug_entry - _head // FileOffset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) .set efi_debug_table_size, . - efi_debug_table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) .previous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) efi_debug_entry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) // EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) .ascii "NB10" // Signature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) .long 0 // Unknown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) .long 0 // Unknown2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) .long 0 // Unknown3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) .asciz VMLINUX_PATH
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) .set efi_debug_entry_size, . - efi_debug_entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * EFI will load .text onwards at the 4k section alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * described in the PE/COFF header. To ensure that instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * sequences using an adrp and a :lo12: immediate will function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * correctly at this alignment, we must ensure that .text is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * placed at a 4k boundary in the Image to begin with.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) .balign SEGMENT_ALIGN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) efi_header_end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) .endm