aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArd Biesheuvel <ard.biesheuvel@linaro.org>2015-01-08 12:51:47 -0500
committerMatt Fleming <matt.fleming@intel.com>2015-01-20 17:13:33 -0500
commitd1a8d66b9177105e898e73716f97eb61842c457a (patch)
tree743fa621077ce787e58a98d0b9a9b803df25f2b3
parent86d68a58d00db3770735b5919ef2c6b12d7f06f3 (diff)
efi/libstub: Call get_memory_map() to obtain map and desc sizes
This fixes two minor issues in the implementation of get_memory_map(): - Currently, it assumes that sizeof(efi_memory_desc_t) == desc_size, which is usually true, but not mandated by the spec. (This was added intentionally to allow future additions to the definition of efi_memory_desc_t). The way the loop is implemented currently, the added slack space may be insufficient if desc_size is larger, which in some corner cases could result in the loop never terminating. - It allocates 32 efi_memory_desc_t entries first (again, using the size of the struct instead of desc_size), and frees and reallocates if it turns out to be insufficient. Few implementations of UEFI have such small memory maps, which results in a unnecessary allocate/free pair on each invocation. Fix this by calling the get_memory_map() boot service first with a '0' input value for map size to retrieve the map size and desc size from the firmware and only then perform the allocation, using desc_size rather than sizeof(efi_memory_desc_t). Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Matt Fleming <matt.fleming@intel.com>
-rw-r--r--drivers/firmware/efi/libstub/efi-stub-helper.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c
index 9bd9fbb5bea8..d073e3946383 100644
--- a/drivers/firmware/efi/libstub/efi-stub-helper.c
+++ b/drivers/firmware/efi/libstub/efi-stub-helper.c
@@ -66,25 +66,29 @@ efi_status_t efi_get_memory_map(efi_system_table_t *sys_table_arg,
66 unsigned long key; 66 unsigned long key;
67 u32 desc_version; 67 u32 desc_version;
68 68
69 *map_size = sizeof(*m) * 32; 69 *map_size = 0;
70again: 70 *desc_size = 0;
71 key = 0;
72 status = efi_call_early(get_memory_map, map_size, NULL,
73 &key, desc_size, &desc_version);
74 if (status != EFI_BUFFER_TOO_SMALL)
75 return EFI_LOAD_ERROR;
76
71 /* 77 /*
72 * Add an additional efi_memory_desc_t because we're doing an 78 * Add an additional efi_memory_desc_t because we're doing an
73 * allocation which may be in a new descriptor region. 79 * allocation which may be in a new descriptor region.
74 */ 80 */
75 *map_size += sizeof(*m); 81 *map_size += *desc_size;
76 status = efi_call_early(allocate_pool, EFI_LOADER_DATA, 82 status = efi_call_early(allocate_pool, EFI_LOADER_DATA,
77 *map_size, (void **)&m); 83 *map_size, (void **)&m);
78 if (status != EFI_SUCCESS) 84 if (status != EFI_SUCCESS)
79 goto fail; 85 goto fail;
80 86
81 *desc_size = 0;
82 key = 0;
83 status = efi_call_early(get_memory_map, map_size, m, 87 status = efi_call_early(get_memory_map, map_size, m,
84 &key, desc_size, &desc_version); 88 &key, desc_size, &desc_version);
85 if (status == EFI_BUFFER_TOO_SMALL) { 89 if (status == EFI_BUFFER_TOO_SMALL) {
86 efi_call_early(free_pool, m); 90 efi_call_early(free_pool, m);
87 goto again; 91 return EFI_LOAD_ERROR;
88 } 92 }
89 93
90 if (status != EFI_SUCCESS) 94 if (status != EFI_SUCCESS)