diff options
author | Matt Fleming <matt.fleming@linux.intel.com> | 2011-06-17 11:43:12 +0100 |
---|---|---|
committer | Matt Fleming <matt.fleming@linux.intel.com> | 2011-07-22 12:59:57 +0100 |
commit | 692986e9a44ffc427b100bac83a0a08151c9954b (patch) | |
tree | 995bf533d114cb318c87d817c30b69d01e739187 | |
parent | 4acff69413c58401983d5b8c0d11e1d7c5ad5fda (diff) | |
download | efilinux-692986e9a44ffc427b100bac83a0a08151c9954b.tar.gz |
efilinux: Extract code for getting memory map
In the future there will be multiple call sites for this code,
e.g. whenever someone calls print_memory_map() and also when we call
exit_boot_services().
Signed-off-by: Matt Fleming <matt.fleming@linux.intel.com>
-rw-r--r-- | entry.c | 91 |
1 files changed, 60 insertions, 31 deletions
@@ -40,6 +40,63 @@ EFI_SYSTEM_TABLE *sys_table; EFI_BOOT_SERVICES *boot; EFI_RUNTIME_SERVICES *runtime; +/** + * memory_map - Allocate and fill out an array of memory descriptors + * @map_buf: buffer containing the memory map + * @map_size: size of the buffer containing the memory map + * @map_key: key for the current memory map + * @desc_size: size of the desc + * @desc_version: memory descriptor version + * + * On success, @map_size contains the size of the memory map pointed + * to by @map_buf and @map_key, @desc_size and @desc_version are + * updated. + */ +static EFI_STATUS +memory_map(EFI_MEMORY_DESCRIPTOR **map_buf, UINTN *map_size, + UINTN *map_key, UINTN *desc_size, UINTN *desc_version) +{ + EFI_STATUS err; + + *map_size = sizeof(**map_buf) * 31; +get_map: + + /* + * Because we're about to allocate memory, we may + * potentially create a new memory descriptor, thereby + * increasing the size of the memory map. So increase + * the buffer size by the size of one memory + * descriptor, just in case. + */ + *map_size += sizeof(**map_buf); + + err = allocate_pool(EfiLoaderData, *map_size, + (void **)map_buf); + if (err != EFI_SUCCESS) { + Print(L"Failed to allocate pool for memory map"); + goto failed; + } + + err = get_memory_map(map_size, *map_buf, map_key, + desc_size, desc_version); + if (err != EFI_SUCCESS) { + if (err == EFI_BUFFER_TOO_SMALL) { + /* + * 'map_size' has been updated to reflect the + * required size of a map buffer. + */ + free_pool((void *)*map_buf); + goto get_map; + } + + Print(L"Failed to get memory map"); + goto failed; + } + +failed: + return err; +} + static void print_memory_map(EFI_MEMORY_DESCRIPTOR *buf, UINTN size, UINTN key, UINTN desc_size, UINTN desc_version) @@ -103,39 +160,11 @@ efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *_table) Print(L"efilinux loader\n"); - size = sizeof(*map_buf) * 31; -get_map: - /* - * Because we're about to allocate memory, we may potentially - * create a new memory descriptor, thereby increasing the size - * of the memory map. So increase the buffer size by the size - * of one memory descriptor, just in case. - */ - size += sizeof(*map_buf); - - err = allocate_pool(EfiLoaderData, size, (void **)&map_buf); - if (err != EFI_SUCCESS) { - Print(L"Failed to allocate pool for memory map"); + err = memory_map(&map_buf, &size, &map_key, + &desc_size, &desc_version); + if (err != EFI_SUCCESS) goto failed; - } - - err = get_memory_map(&size, map_buf, &map_key, - &desc_size, &desc_version); - if (err != EFI_SUCCESS) { - if (err == EFI_BUFFER_TOO_SMALL) { - /* - * 'size' has been updated to reflect the - * required size of a map buffer. - */ - free_pool((void *)map_buf); - Print(L"Failed to get map, retry size=%d\n", size); - goto get_map; - } - - Print(L"Failed to get memory map"); - goto failed; - } print_memory_map(map_buf, size, map_key, desc_size, desc_version); |