summaryrefslogtreecommitdiffstats
path: root/tests/lib/spd_cache-test.c
blob: 4bc0a3dcfe93583c88e4d661880683227a9a9f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* SPDX-License-Identifier: GPL-2.0-only */

#include <crc_byte.h>
#include <spd_bin.h>
#include <spd_cache.h>
#include <stdlib.h>
#include <string.h>
#include <tests/test.h>
#include <tests/lib/spd_cache_data.h>

struct region_device flash_rdev_rw;
static char *flash_buffer = NULL;
static size_t flash_buffer_size = 0;

static int setup_spd_cache(void **state)
{
	flash_buffer_size = SC_SPD_TOTAL_LEN + SC_CRC_LEN;
	flash_buffer = malloc(flash_buffer_size);

	if (flash_buffer == NULL) {
		flash_buffer_size = 0;
		return -1;
	}

	rdev_chain_mem_rw(&flash_rdev_rw, flash_buffer, flash_buffer_size);
	return 0;
}

static int setup_spd_cache_test(void **state)
{
	memset(flash_buffer, 0xff, flash_buffer_size);
	return 0;
}

static int teardown_spd_cache(void **state)
{
	rdev_chain_mem_rw(&flash_rdev_rw, NULL, 0);
	free(flash_buffer);
	flash_buffer = NULL;
	flash_buffer_size = 0;
	return 0;
}


int fmap_locate_area_as_rdev(const char *name, struct region_device *area)
{
	return rdev_chain(area, &flash_rdev_rw, 0, flash_buffer_size);
}

/* This test verifies if load_spd_cache() correctly loads spd_cache pointer and size
   from provided region_device. Memory region device is returned by our
   fmap_locate_area_as_rdev() override. */
static void test_load_spd_cache(void **state)
{
	uint8_t *spd_cache;
	size_t spd_cache_sz;

	assert_int_equal(CB_SUCCESS, load_spd_cache(&spd_cache, &spd_cache_sz));
	assert_ptr_equal(flash_buffer, spd_cache);
	assert_int_equal(SC_SPD_TOTAL_LEN + SC_CRC_LEN, spd_cache_sz);
}

static void calc_spd_cache_crc(uint8_t *spd_cache)
{
	*(uint16_t *)(spd_cache + SC_CRC_OFFSET) =
			CRC(spd_cache, SC_SPD_TOTAL_LEN, crc16_byte);
}

__attribute__((unused))
static void fill_spd_cache_ddr3(uint8_t *spd_cache, size_t spd_cache_sz)
{
	assert_true(spd_cache_sz >= (spd_data_ddr3_1_sz + sizeof(uint16_t)));

	memcpy(spd_cache, spd_data_ddr3_1, spd_data_ddr3_1_sz);
	memset(spd_cache + spd_data_ddr3_1_sz, 0, spd_cache_sz - spd_data_ddr3_1_sz);
	calc_spd_cache_crc(spd_cache);
}

__attribute__((unused))
static void fill_spd_cache_ddr4(uint8_t *spd_cache, size_t spd_cache_sz)
{
	assert_true(spd_cache_sz >=
			(spd_data_ddr4_1_sz + spd_data_ddr4_2_sz + sizeof(uint16_t)));

	memcpy(spd_cache, spd_data_ddr4_1, spd_data_ddr4_1_sz);
	memcpy(spd_cache + spd_data_ddr4_1_sz, spd_data_ddr4_2, spd_data_ddr4_2_sz);
	memset(spd_cache + spd_data_ddr4_1_sz + spd_data_ddr4_2_sz, 0,
			spd_cache_sz - (spd_data_ddr4_1_sz + spd_data_ddr4_2_sz));
	calc_spd_cache_crc(spd_cache);
}

static void test_spd_fill_from_cache(void **state)
{
	struct spd_block blk;
	uint8_t *spd_cache;
	size_t spd_cache_sz;
	assert_int_equal(CB_SUCCESS, load_spd_cache(&spd_cache, &spd_cache_sz));

	/* Empty spd cache */
	assert_int_equal(CB_ERR, spd_fill_from_cache(spd_cache, &blk));

#if __TEST_SPD_CACHE_DDR == 3
	fill_spd_cache_ddr3(spd_cache, spd_cache_sz);
#elif __TEST_SPD_CACHE_DDR == 4
	fill_spd_cache_ddr4(spd_cache, spd_cache_sz);
#endif
	assert_int_equal(CB_SUCCESS, spd_fill_from_cache(spd_cache, &blk));
}


static void test_spd_cache_is_valid(void **state)
{
	uint8_t *spd_cache;
	size_t spd_cache_sz;
	assert_int_equal(CB_SUCCESS, load_spd_cache(&spd_cache, &spd_cache_sz));

	/* Empty, incorrect SPD */
	assert_false(spd_cache_is_valid(spd_cache, spd_cache_sz));

#if __TEST_SPD_CACHE_DDR == 3
	fill_spd_cache_ddr3(spd_cache, spd_cache_sz);
#elif __TEST_SPD_CACHE_DDR == 4
	fill_spd_cache_ddr4(spd_cache, spd_cache_sz);
#endif
	assert_true(spd_cache_is_valid(spd_cache, spd_cache_sz));
}


/* Used for setting `sn` parameter value */
static u32 get_spd_sn_ret_sn[SC_SPD_NUMS] = { 0 };
static size_t get_spd_sn_ret_sn_idx = 0;
/* Implementation for testing purposes.  */
enum cb_err get_spd_sn(u8 addr, u32 *sn)
{
	*sn = get_spd_sn_ret_sn[get_spd_sn_ret_sn_idx];
	get_spd_sn_ret_sn_idx = (get_spd_sn_ret_sn_idx + 1) % ARRAY_SIZE(get_spd_sn_ret_sn);

	return mock_type(enum cb_err);
}

static void get_sn_from_spd_cache(uint8_t *spd_cache, u32 arr[])
{
	for (int i = 0; i < SC_SPD_NUMS; ++i)
		arr[i] = *(u32 *)(spd_cache + SC_SPD_OFFSET(i) + DDR4_SPD_SN_OFF);
}

/* check_if_dimm_changed() has is used only with DDR4, so there tests are not used for DDR3 */
__attribute__((unused))
static void test_check_if_dimm_changed_not_changed(void **state)
{
	uint8_t *spd_cache;
	size_t spd_cache_sz;
	struct spd_block blk = { .addr_map = {0}, .spd_array = {0}, .len = 0 };

	assert_int_equal(CB_SUCCESS, load_spd_cache(&spd_cache, &spd_cache_sz));
	fill_spd_cache_ddr4(spd_cache, spd_cache_sz);
	assert_int_equal(CB_SUCCESS, spd_fill_from_cache(spd_cache, &blk));

	get_sn_from_spd_cache(spd_cache, get_spd_sn_ret_sn);
	get_spd_sn_ret_sn_idx = 0;
	will_return_count(get_spd_sn, CB_SUCCESS, SC_SPD_NUMS);
	assert_false(check_if_dimm_changed(spd_cache, &blk));
}

__attribute__((unused))
static void test_check_if_dimm_changed_sn_error(void **state)
{
	uint8_t *spd_cache;
	size_t spd_cache_sz;
	struct spd_block blk = { .addr_map = {0}, .spd_array = {0}, .len = 0 };

	assert_int_equal(CB_SUCCESS, load_spd_cache(&spd_cache, &spd_cache_sz));
	fill_spd_cache_ddr4(spd_cache, spd_cache_sz);
	assert_int_equal(CB_SUCCESS, spd_fill_from_cache(spd_cache, &blk));

	/* Simulate error */
	will_return_count(get_spd_sn, CB_ERR, 1);
	assert_true(check_if_dimm_changed(spd_cache, &blk));
}

__attribute__((unused))
static void test_check_if_dimm_changed_sodimm_lost(void **state)
{
	uint8_t *spd_cache;
	size_t spd_cache_sz;
	struct spd_block blk = { .addr_map = {0}, .spd_array = {0}, .len = 0 };

	assert_int_equal(CB_SUCCESS, load_spd_cache(&spd_cache, &spd_cache_sz));
	fill_spd_cache_ddr4(spd_cache, spd_cache_sz);
	assert_int_equal(CB_SUCCESS, spd_fill_from_cache(spd_cache, &blk));
	get_sn_from_spd_cache(spd_cache, get_spd_sn_ret_sn);
	memset(spd_cache + spd_data_ddr4_1_sz, 0xff, spd_data_ddr4_2_sz);

	get_spd_sn_ret_sn_idx = 0;
	will_return_always(get_spd_sn, CB_SUCCESS);
	assert_true(check_if_dimm_changed(spd_cache, &blk));
}

__attribute__((unused))
static void test_check_if_dimm_changed_new_sodimm(void **state)
{
	uint8_t *spd_cache;
	size_t spd_cache_sz;
	struct spd_block blk = { .addr_map = {0}, .spd_array = {0}, .len = 0 };

	assert_int_equal(CB_SUCCESS, load_spd_cache(&spd_cache, &spd_cache_sz));
	fill_spd_cache_ddr4(spd_cache, spd_cache_sz);
	assert_int_equal(CB_SUCCESS, spd_fill_from_cache(spd_cache, &blk));
	get_sn_from_spd_cache(spd_cache, get_spd_sn_ret_sn);
	memcpy(spd_cache + spd_data_ddr4_1_sz + spd_data_ddr4_2_sz,
			spd_data_ddr4_2, spd_data_ddr4_2_sz);

	get_spd_sn_ret_sn_idx = 0;
	will_return_always(get_spd_sn, CB_SUCCESS);
	assert_true(check_if_dimm_changed(spd_cache, &blk));
}

__attribute__((unused))
static void test_check_if_dimm_changed_sn_changed(void **state)
{
	uint8_t *spd_cache;
	size_t spd_cache_sz;
	struct spd_block blk = { .addr_map = {0}, .spd_array = {0}, .len = 0 };

	assert_int_equal(CB_SUCCESS, load_spd_cache(&spd_cache, &spd_cache_sz));
	fill_spd_cache_ddr4(spd_cache, spd_cache_sz);
	assert_int_equal(CB_SUCCESS, spd_fill_from_cache(spd_cache, &blk));
	get_sn_from_spd_cache(spd_cache, get_spd_sn_ret_sn);
	*(u32 *)(spd_cache + SC_SPD_OFFSET(0) + DDR4_SPD_SN_OFF) = 0x43211234;

	get_spd_sn_ret_sn_idx = 0;
	will_return_always(get_spd_sn, CB_SUCCESS);
	assert_true(check_if_dimm_changed(spd_cache, &blk));
}

int main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test_setup(test_load_spd_cache, setup_spd_cache_test),
		cmocka_unit_test_setup(test_spd_fill_from_cache, setup_spd_cache_test),
		cmocka_unit_test_setup(test_spd_cache_is_valid, setup_spd_cache_test),
#if __TEST_SPD_CACHE_DDR == 4
		cmocka_unit_test_setup(test_check_if_dimm_changed_not_changed,
					setup_spd_cache_test),
		cmocka_unit_test_setup(test_check_if_dimm_changed_sn_error,
					setup_spd_cache_test),
		cmocka_unit_test_setup(test_check_if_dimm_changed_sodimm_lost,
					setup_spd_cache_test),
		cmocka_unit_test_setup(test_check_if_dimm_changed_new_sodimm,
					setup_spd_cache_test),
		cmocka_unit_test_setup(test_check_if_dimm_changed_sn_changed,
					setup_spd_cache_test),
#endif
	};

	return cmocka_run_group_tests(tests, setup_spd_cache, teardown_spd_cache);
}