1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include "../libc/fmap.c"
5 #include <libpayload.h>
6 #include <tests/test.h>
10 struct sysinfo_t lib_sysinfo
;
11 unsigned long virtual_offset
= 0;
13 static void reset_fmap_cache(void)
18 static int setup_fmap_test(void **state
)
21 lib_sysinfo
.fmap_cache
= 0;
25 static void test_fmap_locate_area_no_fmap_available(void **state
)
30 assert_int_equal(-1, fmap_locate_area("COREBOOT", &offset
, &size
));
33 static void test_fmap_locate_area_incorrect_signature(void **state
)
37 struct fmap mock_fmap
= {
38 .signature
= "NOT_MAP",
40 lib_sysinfo
.fmap_cache
= (uintptr_t)&mock_fmap
;
42 assert_int_equal(-1, fmap_locate_area("COREBOOT", &offset
, &size
));
45 static void test_fmap_locate_area_success(void **state
)
49 struct fmap mock_fmap
= {
50 .signature
= FMAP_SIGNATURE
,
57 struct fmap_area area_1
= {
60 .name
= {'F', 'I', 'R', 'S', 'T', '_', 'A', 'R', 'E', 'A', 0},
63 struct fmap_area area_2
= {
66 .name
= {'S', 'E', 'C', 'O', 'N', 'D', '_', 'A', 'R', 'E', 'A', 0},
69 struct fmap_area area_3
= {
72 .name
= {'T', 'H', 'I', 'R', 'D', '_', 'A', 'R', 'E', 'A', 0},
75 u8 fmap_buffer
[sizeof(struct fmap
) + 3 * sizeof(struct fmap_area
)];
76 memcpy(fmap_buffer
, &mock_fmap
, sizeof(mock_fmap
));
77 memcpy(&fmap_buffer
[sizeof(mock_fmap
)], &area_1
, sizeof(area_1
));
78 memcpy(&fmap_buffer
[sizeof(mock_fmap
) + sizeof(area_1
)], &area_2
, sizeof(area_2
));
79 memcpy(&fmap_buffer
[sizeof(mock_fmap
) + sizeof(area_1
) + sizeof(area_2
)], &area_3
,
84 lib_sysinfo
.fmap_cache
= (uintptr_t)fmap_buffer
;
86 assert_int_equal(0, fmap_locate_area("FIRST_AREA", &offset
, &size
));
87 assert_int_equal(area_1
.offset
, offset
);
88 assert_int_equal(area_1
.size
, size
);
90 assert_int_equal(0, fmap_locate_area("THIRD_AREA", &offset
, &size
));
91 assert_int_equal(area_3
.offset
, offset
);
92 assert_int_equal(area_3
.size
, size
);
94 assert_int_equal(0, fmap_locate_area("SECOND_AREA", &offset
, &size
));
95 assert_int_equal(area_2
.offset
, offset
);
96 assert_int_equal(area_2
.size
, size
);
101 #define FMAP_LOCATE_AREA_TEST(fn) cmocka_unit_test_setup(fn, setup_fmap_test)
105 const struct CMUnitTest tests
[] = {
106 FMAP_LOCATE_AREA_TEST(test_fmap_locate_area_no_fmap_available
),
107 FMAP_LOCATE_AREA_TEST(test_fmap_locate_area_incorrect_signature
),
108 FMAP_LOCATE_AREA_TEST(test_fmap_locate_area_success
),
111 return lp_run_group_tests(tests
, NULL
, NULL
);