efi_loader: use predefined constants in crt0_*_efi.S
[oweals/u-boot.git] / arch / riscv / lib / crt0_riscv_efi.S
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * crt0-efi-riscv.S - PE/COFF header for RISC-V EFI applications
4  *
5  * Copright (C) 2014 Linaro Ltd. <ard.biesheuvel@linaro.org>
6  * Copright (C) 2018 Alexander Graf <agraf@suse.de>
7  *
8  * This file is inspired by arch/arm/lib/crt0_aarch64_efi.S
9  */
10
11 #include <asm-generic/pe.h>
12
13 #if __riscv_xlen == 64
14 #define SIZE_LONG       8
15 #define SAVE_LONG(reg, idx)     sd      reg, (idx*SIZE_LONG)(sp)
16 #define LOAD_LONG(reg, idx)     ld      reg, (idx*SIZE_LONG)(sp)
17 #define PE_MACHINE      IMAGE_FILE_MACHINE_RISCV64
18 #else
19 #define SIZE_LONG       4
20 #define SAVE_LONG(reg, idx)     sw      reg, (idx*SIZE_LONG)(sp)
21 #define LOAD_LONG(reg, idx)     lw      reg, (idx*SIZE_LONG)(sp)
22 #define PE_MACHINE      IMAGE_FILE_MACHINE_RISCV32
23 #endif
24
25
26         .section        .text.head
27
28         /*
29          * Magic "MZ" signature for PE/COFF
30          */
31         .globl  ImageBase
32 ImageBase:
33         .short  IMAGE_DOS_SIGNATURE             /* 'MZ' */
34         .skip   58                              /* 'MZ' + pad + offset == 64 */
35         .long   pe_header - ImageBase           /* Offset to the PE header */
36 pe_header:
37         .long   IMAGE_NT_SIGNATURE              /* 'PE' */
38 coff_header:
39         .short  PE_MACHINE                      /* RISC-V 64/32-bit */
40         .short  2                               /* nr_sections */
41         .long   0                               /* TimeDateStamp */
42         .long   0                               /* PointerToSymbolTable */
43         .long   0                               /* NumberOfSymbols */
44         .short  section_table - optional_header /* SizeOfOptionalHeader */
45         /* Characteristics */
46         .short  (IMAGE_FILE_EXECUTABLE_IMAGE | \
47                  IMAGE_FILE_LINE_NUMS_STRIPPED | \
48                  IMAGE_FILE_LOCAL_SYMS_STRIPPED | \
49                  IMAGE_FILE_DEBUG_STRIPPED)
50 optional_header:
51         .short  IMAGE_NT_OPTIONAL_HDR64_MAGIC   /* PE32+ format */
52         .byte   0x02                            /* MajorLinkerVersion */
53         .byte   0x14                            /* MinorLinkerVersion */
54         .long   _edata - _start                 /* SizeOfCode */
55         .long   0                               /* SizeOfInitializedData */
56         .long   0                               /* SizeOfUninitializedData */
57         .long   _start - ImageBase              /* AddressOfEntryPoint */
58         .long   _start - ImageBase              /* BaseOfCode */
59
60 extra_header_fields:
61         .quad   0                               /* ImageBase */
62         .long   0x20                            /* SectionAlignment */
63         .long   0x8                             /* FileAlignment */
64         .short  0                               /* MajorOperatingSystemVersion */
65         .short  0                               /* MinorOperatingSystemVersion */
66         .short  0                               /* MajorImageVersion */
67         .short  0                               /* MinorImageVersion */
68         .short  0                               /* MajorSubsystemVersion */
69         .short  0                               /* MinorSubsystemVersion */
70         .long   0                               /* Win32VersionValue */
71
72         .long   _edata - ImageBase              /* SizeOfImage */
73
74         /*
75          * Everything before the kernel image is considered part of the header
76          */
77         .long   _start - ImageBase              /* SizeOfHeaders */
78         .long   0                               /* CheckSum */
79         .short  IMAGE_SUBSYSTEM_EFI_APPLICATION /* Subsystem */
80         .short  0                               /* DllCharacteristics */
81         .quad   0                               /* SizeOfStackReserve */
82         .quad   0                               /* SizeOfStackCommit */
83         .quad   0                               /* SizeOfHeapReserve */
84         .quad   0                               /* SizeOfHeapCommit */
85         .long   0                               /* LoaderFlags */
86         .long   0x6                             /* NumberOfRvaAndSizes */
87
88         .quad   0                               /* ExportTable */
89         .quad   0                               /* ImportTable */
90         .quad   0                               /* ResourceTable */
91         .quad   0                               /* ExceptionTable */
92         .quad   0                               /* CertificationTable */
93         .quad   0                               /* BaseRelocationTable */
94
95         /* Section table */
96 section_table:
97
98         /*
99          * The EFI application loader requires a relocation section
100          * because EFI applications must be relocatable.  This is a
101          * dummy section as far as we are concerned.
102          */
103         .ascii  ".reloc"
104         .byte   0
105         .byte   0                       /* end of 0 padding of section name */
106         .long   0
107         .long   0
108         .long   0                       /* SizeOfRawData */
109         .long   0                       /* PointerToRawData */
110         .long   0                       /* PointerToRelocations */
111         .long   0                       /* PointerToLineNumbers */
112         .short  0                       /* NumberOfRelocations */
113         .short  0                       /* NumberOfLineNumbers */
114         .long   0x42100040              /* Characteristics (section flags) */
115
116
117         .ascii  ".text"
118         .byte   0
119         .byte   0
120         .byte   0                       /* end of 0 padding of section name */
121         .long   _edata - _start         /* VirtualSize */
122         .long   _start - ImageBase      /* VirtualAddress */
123         .long   _edata - _start         /* SizeOfRawData */
124         .long   _start - ImageBase      /* PointerToRawData */
125
126         .long   0               /* PointerToRelocations (0 for executables) */
127         .long   0               /* PointerToLineNumbers (0 for executables) */
128         .short  0               /* NumberOfRelocations  (0 for executables) */
129         .short  0               /* NumberOfLineNumbers  (0 for executables) */
130         .long   0xe0500020      /* Characteristics (section flags) */
131
132 _start:
133         addi            sp, sp, -(SIZE_LONG * 3)
134         SAVE_LONG(a0, 0)
135         SAVE_LONG(a1, 1)
136         SAVE_LONG(ra, 2)
137
138         lla             a0, ImageBase
139         lla             a1, _DYNAMIC
140         call            _relocate
141         bne             a0, zero, 0f
142
143         LOAD_LONG(a1, 1)
144         LOAD_LONG(a0, 0)
145         call            efi_main
146
147         LOAD_LONG(ra, 2)
148
149 0:      addi            sp, sp, (SIZE_LONG * 3)
150         ret