Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 1 | /* |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 2 | * Copyright (C) 2013-2017 Linaro Ltd |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 3 | * Authors: Roy Franz <roy.franz@linaro.org> |
| 4 | * Ard Biesheuvel <ard.biesheuvel@linaro.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 11 | #include <linux/pe.h> |
| 12 | #include <linux/sizes.h> |
| 13 | |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 14 | .macro __nop |
| 15 | #ifdef CONFIG_EFI_STUB |
| 16 | @ This is almost but not quite a NOP, since it does clobber the |
| 17 | @ condition flags. But it is the best we can do for EFI, since |
| 18 | @ PE/COFF expects the magic string "MZ" at offset 0, while the |
| 19 | @ ARM/Linux boot protocol expects an executable instruction |
| 20 | @ there. |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 21 | .inst MZ_MAGIC | (0x1310 << 16) @ tstne r0, #0x4d000 |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 22 | #else |
Ard Biesheuvel | 60ce285 | 2017-06-09 10:14:53 +0100 | [diff] [blame] | 23 | AR_CLASS( mov r0, r0 ) |
| 24 | M_CLASS( nop.w ) |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 25 | #endif |
| 26 | .endm |
| 27 | |
| 28 | .macro __EFI_HEADER |
| 29 | #ifdef CONFIG_EFI_STUB |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 30 | .set start_offset, __efi_start - start |
| 31 | .org start + 0x3c |
| 32 | @ |
| 33 | @ The PE header can be anywhere in the file, but for |
| 34 | @ simplicity we keep it together with the MSDOS header |
| 35 | @ The offset to the PE/COFF header needs to be at offset |
| 36 | @ 0x3C in the MSDOS header. |
| 37 | @ The only 2 fields of the MSDOS header that are used are this |
| 38 | @ PE/COFF offset, and the "MZ" bytes at offset 0x0. |
| 39 | @ |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 40 | .long pe_header - start @ Offset to the PE header. |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 41 | |
| 42 | pe_header: |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 43 | .long PE_MAGIC |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 44 | |
| 45 | coff_header: |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 46 | .short IMAGE_FILE_MACHINE_THUMB @ Machine |
| 47 | .short section_count @ NumberOfSections |
| 48 | .long 0 @ TimeDateStamp |
| 49 | .long 0 @ PointerToSymbolTable |
| 50 | .long 0 @ NumberOfSymbols |
| 51 | .short section_table - optional_header @ SizeOfOptionalHeader |
| 52 | .short IMAGE_FILE_32BIT_MACHINE | \ |
| 53 | IMAGE_FILE_DEBUG_STRIPPED | \ |
| 54 | IMAGE_FILE_EXECUTABLE_IMAGE | \ |
| 55 | IMAGE_FILE_LINE_NUMS_STRIPPED @ Characteristics |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 56 | |
Ard Biesheuvel | e4bae4d | 2017-08-18 20:49:44 +0100 | [diff] [blame^] | 57 | #define __pecoff_code_size (__pecoff_data_start - __efi_start) |
| 58 | |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 59 | optional_header: |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 60 | .short PE_OPT_MAGIC_PE32 @ PE32 format |
| 61 | .byte 0x02 @ MajorLinkerVersion |
| 62 | .byte 0x14 @ MinorLinkerVersion |
Ard Biesheuvel | e4bae4d | 2017-08-18 20:49:44 +0100 | [diff] [blame^] | 63 | .long __pecoff_code_size @ SizeOfCode |
| 64 | .long __pecoff_data_size @ SizeOfInitializedData |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 65 | .long 0 @ SizeOfUninitializedData |
| 66 | .long efi_stub_entry - start @ AddressOfEntryPoint |
| 67 | .long start_offset @ BaseOfCode |
Ard Biesheuvel | e4bae4d | 2017-08-18 20:49:44 +0100 | [diff] [blame^] | 68 | .long __pecoff_data_start - start @ BaseOfData |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 69 | |
| 70 | extra_header_fields: |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 71 | .long 0 @ ImageBase |
Ard Biesheuvel | e4bae4d | 2017-08-18 20:49:44 +0100 | [diff] [blame^] | 72 | .long SZ_4K @ SectionAlignment |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 73 | .long SZ_512 @ FileAlignment |
| 74 | .short 0 @ MajorOsVersion |
| 75 | .short 0 @ MinorOsVersion |
| 76 | .short 0 @ MajorImageVersion |
| 77 | .short 0 @ MinorImageVersion |
| 78 | .short 0 @ MajorSubsystemVersion |
| 79 | .short 0 @ MinorSubsystemVersion |
| 80 | .long 0 @ Win32VersionValue |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 81 | |
Ard Biesheuvel | e4bae4d | 2017-08-18 20:49:44 +0100 | [diff] [blame^] | 82 | .long __pecoff_end - start @ SizeOfImage |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 83 | .long start_offset @ SizeOfHeaders |
| 84 | .long 0 @ CheckSum |
| 85 | .short IMAGE_SUBSYSTEM_EFI_APPLICATION @ Subsystem |
| 86 | .short 0 @ DllCharacteristics |
| 87 | .long 0 @ SizeOfStackReserve |
| 88 | .long 0 @ SizeOfStackCommit |
| 89 | .long 0 @ SizeOfHeapReserve |
| 90 | .long 0 @ SizeOfHeapCommit |
| 91 | .long 0 @ LoaderFlags |
| 92 | .long (section_table - .) / 8 @ NumberOfRvaAndSizes |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 93 | |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 94 | .quad 0 @ ExportTable |
| 95 | .quad 0 @ ImportTable |
| 96 | .quad 0 @ ResourceTable |
| 97 | .quad 0 @ ExceptionTable |
| 98 | .quad 0 @ CertificationTable |
| 99 | .quad 0 @ BaseRelocationTable |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 100 | |
| 101 | section_table: |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 102 | .ascii ".text\0\0\0" |
Ard Biesheuvel | e4bae4d | 2017-08-18 20:49:44 +0100 | [diff] [blame^] | 103 | .long __pecoff_code_size @ VirtualSize |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 104 | .long __efi_start @ VirtualAddress |
Ard Biesheuvel | e4bae4d | 2017-08-18 20:49:44 +0100 | [diff] [blame^] | 105 | .long __pecoff_code_size @ SizeOfRawData |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 106 | .long __efi_start @ PointerToRawData |
| 107 | .long 0 @ PointerToRelocations |
| 108 | .long 0 @ PointerToLineNumbers |
| 109 | .short 0 @ NumberOfRelocations |
| 110 | .short 0 @ NumberOfLineNumbers |
| 111 | .long IMAGE_SCN_CNT_CODE | \ |
| 112 | IMAGE_SCN_MEM_READ | \ |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 113 | IMAGE_SCN_MEM_EXECUTE @ Characteristics |
| 114 | |
Ard Biesheuvel | e4bae4d | 2017-08-18 20:49:44 +0100 | [diff] [blame^] | 115 | .ascii ".data\0\0\0" |
| 116 | .long __pecoff_data_size @ VirtualSize |
| 117 | .long __pecoff_data_start - start @ VirtualAddress |
| 118 | .long __pecoff_data_rawsize @ SizeOfRawData |
| 119 | .long __pecoff_data_start - start @ PointerToRawData |
| 120 | .long 0 @ PointerToRelocations |
| 121 | .long 0 @ PointerToLineNumbers |
| 122 | .short 0 @ NumberOfRelocations |
| 123 | .short 0 @ NumberOfLineNumbers |
| 124 | .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ |
| 125 | IMAGE_SCN_MEM_READ | \ |
| 126 | IMAGE_SCN_MEM_WRITE @ Characteristics |
| 127 | |
Ard Biesheuvel | 609eaf0 | 2017-08-18 20:49:43 +0100 | [diff] [blame] | 128 | .set section_count, (. - section_table) / 40 |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 129 | |
Ard Biesheuvel | e4bae4d | 2017-08-18 20:49:44 +0100 | [diff] [blame^] | 130 | .align 12 |
Roy Franz | 81a0bc3 | 2015-09-23 20:17:54 -0700 | [diff] [blame] | 131 | __efi_start: |
| 132 | #endif |
| 133 | .endm |