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