summaryrefslogtreecommitdiff
path: root/payloads/libpayload/tests/libc
diff options
context:
space:
mode:
authorJakub Czapiga <jacz@semihalf.com>2021-11-12 13:45:29 +0000
committerFelix Held <felix-coreboot@felixheld.de>2021-12-23 14:37:42 +0000
commit8fac662f308cdfbeec3f71d4728f71ad79c06925 (patch)
treebc2e73ab891c1643ccbf3bde29b36b7f95383735 /payloads/libpayload/tests/libc
parente7006fb414d20aa49b1aa7d6e2e5a979f5395a6d (diff)
libpayload/libc/fmap: Implement new FlashMap API
This patch introduces new FlashMap API, the fmap_locate_area(). It works on cached FlashMap provided in lib_sysinfo.fmap_cache. Change-Id: Idbf9016ce73aa58e17f3ee19920ab83dc6c25abb Signed-off-by: Jakub Czapiga <jacz@semihalf.com> Reviewed-on: https://review.coreboot.org/c/coreboot/+/59494 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Julius Werner <jwerner@chromium.org>
Diffstat (limited to 'payloads/libpayload/tests/libc')
-rw-r--r--payloads/libpayload/tests/libc/Makefile.inc3
-rw-r--r--payloads/libpayload/tests/libc/fmap_locate_area-test.c112
2 files changed, 115 insertions, 0 deletions
diff --git a/payloads/libpayload/tests/libc/Makefile.inc b/payloads/libpayload/tests/libc/Makefile.inc
new file mode 100644
index 0000000000..5f92bdf0a8
--- /dev/null
+++ b/payloads/libpayload/tests/libc/Makefile.inc
@@ -0,0 +1,3 @@
+tests-y += fmap_locate_area-test
+
+fmap_locate_area-test-srcs += tests/libc/fmap_locate_area-test.c
diff --git a/payloads/libpayload/tests/libc/fmap_locate_area-test.c b/payloads/libpayload/tests/libc/fmap_locate_area-test.c
new file mode 100644
index 0000000000..ce7c36b373
--- /dev/null
+++ b/payloads/libpayload/tests/libc/fmap_locate_area-test.c
@@ -0,0 +1,112 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include "../libc/fmap.c"
+
+#include <libpayload.h>
+#include <tests/test.h>
+
+
+/* Mocks */
+struct sysinfo_t lib_sysinfo;
+unsigned long virtual_offset = 0;
+
+static void reset_fmap_cache(void)
+{
+ _fmap_cache = NULL;
+}
+
+static int setup_fmap_test(void **state)
+{
+ reset_fmap_cache();
+ lib_sysinfo.fmap_cache = 0;
+ return 0;
+}
+
+static void test_fmap_locate_area_no_fmap_available(void **state)
+{
+ size_t offset = 0;
+ size_t size = 0;
+
+ assert_int_equal(-1, fmap_locate_area("COREBOOT", &offset, &size));
+}
+
+static void test_fmap_locate_area_incorrect_signature(void **state)
+{
+ size_t offset = 0;
+ size_t size = 0;
+ struct fmap mock_fmap = {
+ .signature = "NOT_MAP",
+ };
+ lib_sysinfo.fmap_cache = (uintptr_t)&mock_fmap;
+
+ assert_int_equal(-1, fmap_locate_area("COREBOOT", &offset, &size));
+}
+
+static void test_fmap_locate_area_success(void **state)
+{
+ size_t offset = 0;
+ size_t size = 0;
+ struct fmap mock_fmap = {
+ .signature = FMAP_SIGNATURE,
+ .ver_major = 1,
+ .ver_minor = 1,
+ .base = 0xAABB,
+ .size = 0x10000,
+ .nareas = 3,
+ };
+ struct fmap_area area_1 = {
+ .size = 0x1100,
+ .offset = 0x11,
+ .name = {'F', 'I', 'R', 'S', 'T', '_', 'A', 'R', 'E', 'A', 0},
+ .flags = 0,
+ };
+ struct fmap_area area_2 = {
+ .size = 0x2200,
+ .offset = 0x1111,
+ .name = {'S', 'E', 'C', 'O', 'N', 'D', '_', 'A', 'R', 'E', 'A', 0},
+ .flags = 0,
+ };
+ struct fmap_area area_3 = {
+ .size = 0x100,
+ .offset = 0x3311,
+ .name = {'T', 'H', 'I', 'R', 'D', '_', 'A', 'R', 'E', 'A', 0},
+ .flags = 0,
+ };
+ u8 fmap_buffer[sizeof(struct fmap) + 3 * sizeof(struct fmap_area)];
+ memcpy(fmap_buffer, &mock_fmap, sizeof(mock_fmap));
+ memcpy(&fmap_buffer[sizeof(mock_fmap)], &area_1, sizeof(area_1));
+ memcpy(&fmap_buffer[sizeof(mock_fmap) + sizeof(area_1)], &area_2, sizeof(area_2));
+ memcpy(&fmap_buffer[sizeof(mock_fmap) + sizeof(area_1) + sizeof(area_2)], &area_3,
+ sizeof(area_3));
+
+ /* Cache only */
+ reset_fmap_cache();
+ lib_sysinfo.fmap_cache = (uintptr_t)fmap_buffer;
+
+ assert_int_equal(0, fmap_locate_area("FIRST_AREA", &offset, &size));
+ assert_int_equal(area_1.offset, offset);
+ assert_int_equal(area_1.size, size);
+
+ assert_int_equal(0, fmap_locate_area("THIRD_AREA", &offset, &size));
+ assert_int_equal(area_3.offset, offset);
+ assert_int_equal(area_3.size, size);
+
+ assert_int_equal(0, fmap_locate_area("SECOND_AREA", &offset, &size));
+ assert_int_equal(area_2.offset, offset);
+ assert_int_equal(area_2.size, size);
+
+ reset_fmap_cache();
+}
+
+#define FMAP_LOCATE_AREA_TEST(fn) cmocka_unit_test_setup(fn, setup_fmap_test)
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ FMAP_LOCATE_AREA_TEST(test_fmap_locate_area_no_fmap_available),
+ FMAP_LOCATE_AREA_TEST(test_fmap_locate_area_incorrect_signature),
+ FMAP_LOCATE_AREA_TEST(test_fmap_locate_area_success),
+ };
+
+ return lp_run_group_tests(tests, NULL, NULL);
+}