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

Stefan Brüns stefan.bruens at rwth-aachen.de
Sat Oct 1 19:31:51 CEST 2016


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 at rwth-aachen.de>
---
 lib/efi_loader/efi_boottime.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
index 784891b..c413ecb 100644
--- a/lib/efi_loader/efi_boottime.c
+++ b/lib/efi_loader/efi_boottime.c
@@ -135,19 +135,37 @@ static efi_status_t EFIAPI efi_allocate_pool(int pool_type, unsigned long size,
 {
 	efi_status_t r;
 	efi_physical_addr_t t;
+	u64 num_pages = (size + 8 + EFI_PAGE_MASK) >> EFI_PAGE_SHIFT;
 
 	EFI_ENTRY("%d, %ld, %p", pool_type, size, buffer);
-	r = efi_allocate_pages(0, pool_type, (size + 0xfff) >> 12, &t);
-	*buffer = (void *)(uintptr_t)t;
+
+	if (size == 0) {
+		*buffer = NULL;
+		return EFI_EXIT(EFI_SUCCESS);
+	}
+
+	r = efi_allocate_pages(0, pool_type, num_pages, &t);
+
+	if (r == EFI_SUCCESS) {
+		*(u64 *)(uintptr_t)t = num_pages;
+		*buffer = (void *)(uintptr_t)(t + 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;
+	assert(((ulong)buffer & EFI_PAGE_MASK) == 0);
+	num_pages = *(u64 *)buffer;
+
+	r = efi_free_pages((ulong)buffer, num_pages);
 	return EFI_EXIT(r);
 }
 
-- 
2.10.0



More information about the U-Boot mailing list