]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
test: unit test for efi_alloc_aligned_pages()
authorHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Sun, 8 Jun 2025 07:54:28 +0000 (09:54 +0200)
committerTom Rini <trini@konsulko.com>
Thu, 26 Jun 2025 19:45:33 +0000 (13:45 -0600)
Provide unit tests for efi_alloc_aligned_pages() and
efi_allocate_pages().

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
test/lib/Makefile
test/lib/efi_memory.c [new file with mode: 0644]

index ff4ff63270df5a7470a50884c16e7373f76bc2ce..35b40b584c46a8b8f9e583cfa22d24d3a008e3e9 100644 (file)
@@ -8,7 +8,7 @@ obj-$(CONFIG_$(PHASE_)UT_COMPRESSION) += compression.o
 ifeq ($(CONFIG_XPL_BUILD),)
 obj-y += abuf.o
 obj-y += alist.o
-obj-$(CONFIG_EFI_LOADER) += efi_device_path.o
+obj-$(CONFIG_EFI_LOADER) += efi_device_path.o efi_memory.o
 obj-$(CONFIG_EFI_SECURE_BOOT) += efi_image_region.o
 obj-y += hexdump.o
 obj-$(CONFIG_SANDBOX) += kconfig.o
diff --git a/test/lib/efi_memory.c b/test/lib/efi_memory.c
new file mode 100644 (file)
index 0000000..d2e1ab6
--- /dev/null
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Test memory functions
+ *
+ * Copyright (c) 2025 Heinrich Schuchardt <xypron.glpk@gmx.de>
+ */
+
+#include <efi_loader.h>
+#include <test/lib.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static int lib_test_efi_alloc_aligned_pages(struct unit_test_state *uts)
+{
+       efi_status_t ret;
+
+       void *addr;
+       unsigned long align = 0x400000;
+
+       addr = efi_alloc_aligned_pages(4096, EFI_PERSISTENT_MEMORY_TYPE,
+                                      EFI_PAGE_SIZE);
+       ut_asserteq_ptr(NULL, addr);
+
+       addr = efi_alloc_aligned_pages(4096, 0x6FFFFFFF, EFI_PAGE_SIZE);
+       ut_asserteq_ptr(NULL, addr);
+
+       align = 0x200;
+       addr = efi_alloc_aligned_pages(4096, EFI_ACPI_RECLAIM_MEMORY, align);
+       ut_assertnonnull(addr);
+       ut_asserteq_64(0, (uintptr_t)addr & (align - 1));
+
+       ret = efi_free_pages((uintptr_t) addr, 1);
+       ut_asserteq_64(ret, EFI_SUCCESS);
+
+       align = 0x400000;
+       addr = efi_alloc_aligned_pages(4096, EFI_ACPI_RECLAIM_MEMORY, align);
+       ut_assertnonnull(addr);
+       ut_asserteq_64(0, (uintptr_t)addr & (align - 1));
+
+       ret = efi_free_pages((uintptr_t) addr, 1);
+       ut_asserteq_64(ret, EFI_SUCCESS);
+
+       return 0;
+}
+LIB_TEST(lib_test_efi_alloc_aligned_pages, 0);
+
+static int lib_test_efi_allocate_pages(struct unit_test_state *uts)
+{
+       efi_status_t ret;
+       u64 memory;
+
+       ret = efi_allocate_pages(EFI_ALLOCATE_ANY_PAGES,
+                                EFI_ACPI_RECLAIM_MEMORY,
+                                1, &memory);
+       ut_asserteq_64(ret, EFI_SUCCESS);
+       ut_asserteq_64(0, memory & EFI_PAGE_MASK);
+
+       ret = efi_free_pages(memory, 1);
+       ut_asserteq_64(ret, EFI_SUCCESS);
+
+       return 0;
+}
+LIB_TEST(lib_test_efi_allocate_pages, 0);