1 /*
2  *  EFI image loader
3  *
4  *  based partly on wine code
5  *
6  *  Copyright (c) 2016 Alexander Graf
7  *
8  *  SPDX-License-Identifier:     GPL-2.0+
9  */
10 
11 #include <common.h>
12 #include <efi_loader.h>
13 #include <pe.h>
14 #include <asm/global_data.h>
15 
16 DECLARE_GLOBAL_DATA_PTR;
17 
18 const efi_guid_t efi_guid_device_path = DEVICE_PATH_GUID;
19 const efi_guid_t efi_guid_loaded_image = LOADED_IMAGE_GUID;
20 
21 static efi_status_t efi_loader_relocate(const IMAGE_BASE_RELOCATION *rel,
22 			unsigned long rel_size, void *efi_reloc)
23 {
24 	const IMAGE_BASE_RELOCATION *end;
25 	int i;
26 
27 	end = (const IMAGE_BASE_RELOCATION *)((const char *)rel + rel_size);
28 	while (rel < end - 1 && rel->SizeOfBlock) {
29 		const uint16_t *relocs = (const uint16_t *)(rel + 1);
30 		i = (rel->SizeOfBlock - sizeof(*rel)) / sizeof(uint16_t);
31 		while (i--) {
32 			uint32_t offset = (uint32_t)(*relocs & 0xfff) +
33 					  rel->VirtualAddress;
34 			int type = *relocs >> EFI_PAGE_SHIFT;
35 			unsigned long delta = (unsigned long)efi_reloc;
36 			uint64_t *x64 = efi_reloc + offset;
37 			uint32_t *x32 = efi_reloc + offset;
38 			uint16_t *x16 = efi_reloc + offset;
39 
40 			switch (type) {
41 			case IMAGE_REL_BASED_ABSOLUTE:
42 				break;
43 			case IMAGE_REL_BASED_HIGH:
44 				*x16 += ((uint32_t)delta) >> 16;
45 				break;
46 			case IMAGE_REL_BASED_LOW:
47 				*x16 += (uint16_t)delta;
48 				break;
49 			case IMAGE_REL_BASED_HIGHLOW:
50 				*x32 += (uint32_t)delta;
51 				break;
52 			case IMAGE_REL_BASED_DIR64:
53 				*x64 += (uint64_t)delta;
54 				break;
55 			default:
56 				printf("Unknown Relocation off %x type %x\n",
57 				       offset, type);
58 				return EFI_LOAD_ERROR;
59 			}
60 			relocs++;
61 		}
62 		rel = (const IMAGE_BASE_RELOCATION *)relocs;
63 	}
64 	return EFI_SUCCESS;
65 }
66 
67 void __weak invalidate_icache_all(void)
68 {
69 	/* If the system doesn't support icache_all flush, cross our fingers */
70 }
71 
72 /*
73  * This function loads all sections from a PE binary into a newly reserved
74  * piece of memory. On successful load it then returns the entry point for
75  * the binary. Otherwise NULL.
76  */
77 void *efi_load_pe(void *efi, struct efi_loaded_image *loaded_image_info)
78 {
79 	IMAGE_NT_HEADERS32 *nt;
80 	IMAGE_DOS_HEADER *dos;
81 	IMAGE_SECTION_HEADER *sections;
82 	int num_sections;
83 	void *efi_reloc;
84 	int i;
85 	const IMAGE_BASE_RELOCATION *rel;
86 	unsigned long rel_size;
87 	int rel_idx = IMAGE_DIRECTORY_ENTRY_BASERELOC;
88 	void *entry;
89 	uint64_t image_size;
90 	unsigned long virt_size = 0;
91 	bool can_run_nt64 = true;
92 	bool can_run_nt32 = true;
93 
94 #if defined(CONFIG_ARM64)
95 	can_run_nt32 = false;
96 #elif defined(CONFIG_ARM)
97 	can_run_nt64 = false;
98 #endif
99 
100 	dos = efi;
101 	if (dos->e_magic != IMAGE_DOS_SIGNATURE) {
102 		printf("%s: Invalid DOS Signature\n", __func__);
103 		return NULL;
104 	}
105 
106 	nt = (void *) ((char *)efi + dos->e_lfanew);
107 	if (nt->Signature != IMAGE_NT_SIGNATURE) {
108 		printf("%s: Invalid NT Signature\n", __func__);
109 		return NULL;
110 	}
111 
112 	/* Calculate upper virtual address boundary */
113 	num_sections = nt->FileHeader.NumberOfSections;
114 	sections = (void *)&nt->OptionalHeader +
115 			    nt->FileHeader.SizeOfOptionalHeader;
116 
117 	for (i = num_sections - 1; i >= 0; i--) {
118 		IMAGE_SECTION_HEADER *sec = &sections[i];
119 		virt_size = max_t(unsigned long, virt_size,
120 				  sec->VirtualAddress + sec->Misc.VirtualSize);
121 	}
122 
123 	/* Read 32/64bit specific header bits */
124 	if (can_run_nt64 &&
125 	    (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)) {
126 		IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
127 		IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
128 		image_size = opt->SizeOfImage;
129 		efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
130 		if (!efi_reloc) {
131 			printf("%s: Could not allocate %ld bytes\n",
132 				__func__, virt_size);
133 			return NULL;
134 		}
135 		entry = efi_reloc + opt->AddressOfEntryPoint;
136 		rel_size = opt->DataDirectory[rel_idx].Size;
137 		rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
138 	} else if (can_run_nt32 &&
139 		   (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
140 		IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
141 		image_size = opt->SizeOfImage;
142 		efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
143 		if (!efi_reloc) {
144 			printf("%s: Could not allocate %ld bytes\n",
145 				__func__, virt_size);
146 			return NULL;
147 		}
148 		entry = efi_reloc + opt->AddressOfEntryPoint;
149 		rel_size = opt->DataDirectory[rel_idx].Size;
150 		rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
151 	} else {
152 		printf("%s: Invalid optional header magic %x\n", __func__,
153 		       nt->OptionalHeader.Magic);
154 		return NULL;
155 	}
156 
157 	/* Load sections into RAM */
158 	for (i = num_sections - 1; i >= 0; i--) {
159 		IMAGE_SECTION_HEADER *sec = &sections[i];
160 		memset(efi_reloc + sec->VirtualAddress, 0,
161 		       sec->Misc.VirtualSize);
162 		memcpy(efi_reloc + sec->VirtualAddress,
163 		       efi + sec->PointerToRawData,
164 		       sec->SizeOfRawData);
165 	}
166 
167 	/* Run through relocations */
168 	if (efi_loader_relocate(rel, rel_size, efi_reloc) != EFI_SUCCESS) {
169 		efi_free_pages((uintptr_t) efi_reloc,
170 			       (virt_size + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT);
171 		return NULL;
172 	}
173 
174 	/* Flush cache */
175 	flush_cache((ulong)efi_reloc,
176 		    ALIGN(virt_size, CONFIG_SYS_CACHELINE_SIZE));
177 	invalidate_icache_all();
178 
179 	/* Populate the loaded image interface bits */
180 	loaded_image_info->image_base = efi;
181 	loaded_image_info->image_size = image_size;
182 
183 	return entry;
184 }
185