[U-Boot] [PATCH 3/6] efi_loader: Track size of pool allocations to allow freeing

allocate_pool has to return a buffer which is 8-byte aligned. Shift the region returned by allocate_pages by 8 byte and store the size in the headroom. The 8 byte overhead is neglegible, but provides the required size when freeing the allocation later.
Signed-off-by: Stefan Brüns stefan.bruens@rwth-aachen.de --- lib/efi_loader/efi_boottime.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c index be6f5e8..3e526eb 100644 --- a/lib/efi_loader/efi_boottime.c +++ b/lib/efi_loader/efi_boottime.c @@ -134,18 +134,35 @@ static efi_status_t EFIAPI efi_allocate_pool(int pool_type, unsigned long size, void **buffer) { efi_status_t r; + u64 num_pages = (size + 8 + 0xfff) >> 12;
EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer); - r = efi_allocate_pages(0, pool_type, (size + 0xfff) >> 12, (void*)buffer); + + if (size == 0) { + *buffer = NULL; + return EFI_EXIT(EFI_SUCCESS); + } + + r = efi_allocate_pages(0, pool_type, num_pages, (void *)buffer); + if (r == EFI_SUCCESS) { + *(u64 *)(*buffer) = num_pages; + *buffer = (char *)(*buffer) + 8; + } + return EFI_EXIT(r); }
static efi_status_t EFIAPI efi_free_pool(void *buffer) { efi_status_t r; + u64 num_pages;
EFI_ENTRY("%p", buffer); - r = efi_free_pages((ulong)buffer, 0); + + buffer = (char *)(buffer) - 8; + num_pages = *(u64 *)buffer; + + r = efi_free_pages((ulong)buffer, num_pages); return EFI_EXIT(r); }

On 30.09.16 02:03, Stefan Brüns wrote:
allocate_pool has to return a buffer which is 8-byte aligned. Shift the region returned by allocate_pages by 8 byte and store the size in the headroom. The 8 byte overhead is neglegible, but provides the required size when freeing the allocation later.
Signed-off-by: Stefan Brüns stefan.bruens@rwth-aachen.de
lib/efi_loader/efi_boottime.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c index be6f5e8..3e526eb 100644 --- a/lib/efi_loader/efi_boottime.c +++ b/lib/efi_loader/efi_boottime.c @@ -134,18 +134,35 @@ static efi_status_t EFIAPI efi_allocate_pool(int pool_type, unsigned long size, void **buffer) { efi_status_t r;
u64 num_pages = (size + 8 + 0xfff) >> 12;
EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
- r = efi_allocate_pages(0, pool_type, (size + 0xfff) >> 12, (void*)buffer);
Please base on top of this patch to avoid conflicts:
http://lists.denx.de/pipermail/u-boot/2016-September/266580.html
- if (size == 0) {
*buffer = NULL;
return EFI_EXIT(EFI_SUCCESS);
- }
- r = efi_allocate_pages(0, pool_type, num_pages, (void *)buffer);
- if (r == EFI_SUCCESS) {
*(u64 *)(*buffer) = num_pages;
*buffer = (char *)(*buffer) + 8;
- }
- return EFI_EXIT(r);
}
static efi_status_t EFIAPI efi_free_pool(void *buffer) { efi_status_t r;
u64 num_pages;
EFI_ENTRY("%p", buffer);
- r = efi_free_pages((ulong)buffer, 0);
- buffer = (char *)(buffer) - 8;
Please add a sanity assert here that verifies that (ulong)buffer & EFI_PAGE_MASK == 8.
Alex
- num_pages = *(u64 *)buffer;
- r = efi_free_pages((ulong)buffer, num_pages); return EFI_EXIT(r);
}
participants (2)
-
Alexander Graf
-
Stefan Brüns