soc/intel/xeon_sp/util: Enhance lock_pam0123
[coreboot2.git] / payloads / libpayload / tests / libc / fmap_locate_area-test.c
blobce7c36b373c0c3abba3592a1e22c8256485b277b
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include "../libc/fmap.c"
5 #include <libpayload.h>
6 #include <tests/test.h>
9 /* Mocks */
10 struct sysinfo_t lib_sysinfo;
11 unsigned long virtual_offset = 0;
13 static void reset_fmap_cache(void)
15 _fmap_cache = NULL;
18 static int setup_fmap_test(void **state)
20 reset_fmap_cache();
21 lib_sysinfo.fmap_cache = 0;
22 return 0;
25 static void test_fmap_locate_area_no_fmap_available(void **state)
27 size_t offset = 0;
28 size_t size = 0;
30 assert_int_equal(-1, fmap_locate_area("COREBOOT", &offset, &size));
33 static void test_fmap_locate_area_incorrect_signature(void **state)
35 size_t offset = 0;
36 size_t size = 0;
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)
47 size_t offset = 0;
48 size_t size = 0;
49 struct fmap mock_fmap = {
50 .signature = FMAP_SIGNATURE,
51 .ver_major = 1,
52 .ver_minor = 1,
53 .base = 0xAABB,
54 .size = 0x10000,
55 .nareas = 3,
57 struct fmap_area area_1 = {
58 .size = 0x1100,
59 .offset = 0x11,
60 .name = {'F', 'I', 'R', 'S', 'T', '_', 'A', 'R', 'E', 'A', 0},
61 .flags = 0,
63 struct fmap_area area_2 = {
64 .size = 0x2200,
65 .offset = 0x1111,
66 .name = {'S', 'E', 'C', 'O', 'N', 'D', '_', 'A', 'R', 'E', 'A', 0},
67 .flags = 0,
69 struct fmap_area area_3 = {
70 .size = 0x100,
71 .offset = 0x3311,
72 .name = {'T', 'H', 'I', 'R', 'D', '_', 'A', 'R', 'E', 'A', 0},
73 .flags = 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,
80 sizeof(area_3));
82 /* Cache only */
83 reset_fmap_cache();
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);
98 reset_fmap_cache();
101 #define FMAP_LOCATE_AREA_TEST(fn) cmocka_unit_test_setup(fn, setup_fmap_test)
103 int main(void)
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);