Commit 36b41a3cedd6626d8c8277b119d4516865da4738

Authored by Heinrich Schuchardt
Committed by Alexander Graf
1 parent 476ed96e01

efi_loader: allocate correct memory type for EFI image

The category of memory allocated for an EFI image should depend on
its type (application, bootime service driver, runtime service driver).

Our helloworld.efi built on arm64 has an illegal image type. Treat it
like an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

Showing 1 changed file with 40 additions and 24 deletions Side-by-side Diff

lib/efi_loader/efi_image_loader.c
... ... @@ -74,6 +74,40 @@
74 74 }
75 75  
76 76 /*
  77 + * Determine the memory types to be used for code and data.
  78 + *
  79 + * @loaded_image_info image descriptor
  80 + * @image_type field Subsystem of the optional header for
  81 + * Windows specific field
  82 + */
  83 +static void efi_set_code_and_data_type(
  84 + struct efi_loaded_image *loaded_image_info,
  85 + uint16_t image_type)
  86 +{
  87 + switch (image_type) {
  88 + case IMAGE_SUBSYSTEM_EFI_APPLICATION:
  89 + loaded_image_info->image_code_type = EFI_LOADER_CODE;
  90 + loaded_image_info->image_data_type = EFI_LOADER_DATA;
  91 + break;
  92 + case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
  93 + loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
  94 + loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
  95 + break;
  96 + case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
  97 + case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
  98 + loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
  99 + loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
  100 + break;
  101 + default:
  102 + printf("%s: invalid image type: %u\n", __func__, image_type);
  103 + /* Let's assume it is an application */
  104 + loaded_image_info->image_code_type = EFI_LOADER_CODE;
  105 + loaded_image_info->image_data_type = EFI_LOADER_DATA;
  106 + break;
  107 + }
  108 +}
  109 +
  110 +/*
77 111 * This function loads all sections from a PE binary into a newly reserved
78 112 * piece of memory. On successful load it then returns the entry point for
79 113 * the binary. Otherwise NULL.
... ... @@ -94,7 +128,6 @@
94 128 unsigned long virt_size = 0;
95 129 bool can_run_nt64 = true;
96 130 bool can_run_nt32 = true;
97   - uint16_t image_type;
98 131  
99 132 #if defined(CONFIG_ARM64)
100 133 can_run_nt32 = false;
... ... @@ -131,7 +164,9 @@
131 164 IMAGE_NT_HEADERS64 *nt64 = (void *)nt;
132 165 IMAGE_OPTIONAL_HEADER64 *opt = &nt64->OptionalHeader;
133 166 image_size = opt->SizeOfImage;
134   - efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
  167 + efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  168 + efi_reloc = efi_alloc(virt_size,
  169 + loaded_image_info->image_code_type);
135 170 if (!efi_reloc) {
136 171 printf("%s: Could not allocate %lu bytes\n",
137 172 __func__, virt_size);
138 173  
... ... @@ -140,12 +175,13 @@
140 175 entry = efi_reloc + opt->AddressOfEntryPoint;
141 176 rel_size = opt->DataDirectory[rel_idx].Size;
142 177 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
143   - image_type = opt->Subsystem;
144 178 } else if (can_run_nt32 &&
145 179 (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)) {
146 180 IMAGE_OPTIONAL_HEADER32 *opt = &nt->OptionalHeader;
147 181 image_size = opt->SizeOfImage;
148   - efi_reloc = efi_alloc(virt_size, EFI_LOADER_DATA);
  182 + efi_set_code_and_data_type(loaded_image_info, opt->Subsystem);
  183 + efi_reloc = efi_alloc(virt_size,
  184 + loaded_image_info->image_code_type);
149 185 if (!efi_reloc) {
150 186 printf("%s: Could not allocate %lu bytes\n",
151 187 __func__, virt_size);
152 188  
... ... @@ -154,30 +190,10 @@
154 190 entry = efi_reloc + opt->AddressOfEntryPoint;
155 191 rel_size = opt->DataDirectory[rel_idx].Size;
156 192 rel = efi_reloc + opt->DataDirectory[rel_idx].VirtualAddress;
157   - image_type = opt->Subsystem;
158 193 } else {
159 194 printf("%s: Invalid optional header magic %x\n", __func__,
160 195 nt->OptionalHeader.Magic);
161 196 return NULL;
162   - }
163   -
164   - switch (image_type) {
165   - case IMAGE_SUBSYSTEM_EFI_APPLICATION:
166   - loaded_image_info->image_code_type = EFI_LOADER_CODE;
167   - loaded_image_info->image_data_type = EFI_LOADER_DATA;
168   - break;
169   - case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER:
170   - loaded_image_info->image_code_type = EFI_BOOT_SERVICES_CODE;
171   - loaded_image_info->image_data_type = EFI_BOOT_SERVICES_DATA;
172   - break;
173   - case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER:
174   - case IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER:
175   - loaded_image_info->image_code_type = EFI_RUNTIME_SERVICES_CODE;
176   - loaded_image_info->image_data_type = EFI_RUNTIME_SERVICES_DATA;
177   - break;
178   - default:
179   - printf("%s: invalid image type: %u\n", __func__, image_type);
180   - break;
181 197 }
182 198  
183 199 /* Load sections into RAM */