summaryrefslogtreecommitdiff
path: root/test/cbi.c
blob: 688063f153e877289d8494f5d17fdc5667ae01bd (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Test CBI
 */

#include "common.h"
#include "console.h"
#include "cros_board_info.h"
#include "ec_commands.h"
#include "gpio.h"
#include "i2c.h"
#include "test_util.h"
#include "util.h"

static void test_setup(void)
{
	/* Make sure that write protect is disabled */
#ifdef CONFIG_WP_ACTIVE_HIGH
	gpio_set_level(GPIO_WP, 0);
#else
	gpio_set_level(GPIO_WP_L, 1);
#endif /* CONFIG_WP_ACTIVE_HIGH */

	cbi_create();
	cbi_write();
}

static void test_teardown(void)
{
}

DECLARE_EC_TEST(test_uint8)
{
	uint8_t d8;
	uint32_t d32;
	uint8_t size;
	const int tag = 0xff;

	/* Set & get uint8_t */
	d8 = 0xa5;
	zassert_equal(cbi_set_board_info(tag, &d8, sizeof(d8)), EC_SUCCESS,
		      NULL);
	size = 1;
	zassert_equal(cbi_get_board_info(tag, &d8, &size), EC_SUCCESS, NULL);
	zassert_equal(d8, 0xa5, "0x%x, 0x%x", d8, 0xa5);
	zassert_equal(size, 1, "%x, %x", size, 1);

	/* Size-up */
	d32 = 0x1234abcd;
	zassert_equal(cbi_set_board_info(tag, (void *)&d32, sizeof(d32)),
		      EC_SUCCESS, NULL);
	size = 4;
	zassert_equal(cbi_get_board_info(tag, (void *)&d32, &size), EC_SUCCESS,
		      NULL);
	zassert_equal(d32, 0x1234abcd, "0x%x, 0x%x", d32, 0x1234abcd);
	zassert_equal(size, 4, "%u, %u", size, 4);

	return EC_SUCCESS;
}

DECLARE_EC_TEST(test_uint32)
{
	uint8_t d8;
	uint32_t d32;
	uint8_t size;
	const int tag = 0xff;

	/* Set & get uint32_t */
	d32 = 0x1234abcd;
	zassert_equal(cbi_set_board_info(tag, (void *)&d32, sizeof(d32)),
		      EC_SUCCESS, NULL);
	size = 4;
	zassert_equal(cbi_get_board_info(tag, (void *)&d32, &size), EC_SUCCESS,
		      NULL);
	zassert_equal(d32, 0x1234abcd, "0x%x, 0x%x", d32, 0x1234abcd);
	zassert_equal(size, 4, "%u, %u", size, 4);

	/* Size-down */
	d8 = 0xa5;
	zassert_equal(cbi_set_board_info(tag, &d8, sizeof(d8)), EC_SUCCESS,
		      NULL);
	size = 1;
	zassert_equal(cbi_get_board_info(tag, &d8, &size), EC_SUCCESS, NULL);
	zassert_equal(d8, 0xa5, "0x%x, 0x%x", d8, 0xa5);
	zassert_equal(size, 1, "%u, %u", size, 1);

	return EC_SUCCESS;
}

DECLARE_EC_TEST(test_string)
{
	const uint8_t string[] = "abcdefghijklmn";
	uint8_t buf[32];
	uint8_t size;
	const int tag = 0xff;

	/* Set & get string */
	zassert_equal(cbi_set_board_info(tag, string, sizeof(string)),
		      EC_SUCCESS, NULL);
	size = sizeof(buf);
	zassert_equal(cbi_get_board_info(tag, buf, &size), EC_SUCCESS, NULL);
	zassert_equal(strncmp(buf, string, sizeof(string)), 0, NULL);
	/* Size contains null byte */
	/* This should be zassert_equal, but for EC test fmt is always "0x%x"
	 * which will generate compilation error.
	 */
	zassert_true((size_t)size - 1 == strlen(buf), "%zu, %zu",
		     (size_t)size - 1, strlen(buf));

	/* Read buffer too small */
	size = 4;
	zassert_equal(cbi_get_board_info(tag, buf, &size), EC_ERROR_INVAL,
		      NULL);

	return EC_SUCCESS;
}

DECLARE_EC_TEST(test_not_found)
{
	uint8_t d8;
	const int tag = 0xff;
	uint8_t size;

	size = 1;
	zassert_equal(cbi_get_board_info(tag, &d8, &size), EC_ERROR_UNKNOWN,
		      NULL);

	return EC_SUCCESS;
}

DECLARE_EC_TEST(test_too_large)
{
	uint8_t buf[CBI_IMAGE_SIZE-1];
	const int tag = 0xff;

	/* Data too large */
	memset(buf, 0xa5, sizeof(buf));
	zassert_equal(cbi_set_board_info(tag, buf, sizeof(buf)),
		      EC_ERROR_OVERFLOW, NULL);

	return EC_SUCCESS;
}

DECLARE_EC_TEST(test_all_tags)
{
	uint8_t d8;
	uint32_t d32;
	uint64_t d64;
	const char string[] = "abc";
	uint8_t buf[32];
	uint8_t size;
	int count = 0;

	/* Populate all data and read out */
	d8 = 0x12;
	zassert_equal(cbi_set_board_info(CBI_TAG_BOARD_VERSION, &d8,
					 sizeof(d8)),
		      EC_SUCCESS, NULL);
	count++;
	zassert_equal(cbi_set_board_info(CBI_TAG_OEM_ID, &d8, sizeof(d8)),
		      EC_SUCCESS, NULL);
	count++;
	zassert_equal(cbi_set_board_info(CBI_TAG_SKU_ID, &d8, sizeof(d8)),
		      EC_SUCCESS, NULL);
	count++;
	zassert_equal(cbi_set_board_info(CBI_TAG_DRAM_PART_NUM,
					 string, sizeof(string)),
		      EC_SUCCESS, NULL);
	count++;
	zassert_equal(cbi_set_board_info(CBI_TAG_OEM_NAME,
					 string, sizeof(string)),
		      EC_SUCCESS, NULL);
	count++;
	zassert_equal(cbi_set_board_info(CBI_TAG_MODEL_ID, &d8, sizeof(d8)),
		      EC_SUCCESS, NULL);
	count++;
	zassert_equal(cbi_set_board_info(CBI_TAG_FW_CONFIG, &d8, sizeof(d8)),
		      EC_SUCCESS, NULL);
	count++;
	zassert_equal(cbi_set_board_info(CBI_TAG_PCB_SUPPLIER, &d8,
					 sizeof(d8)),
		      EC_SUCCESS, NULL);
	count++;
	zassert_equal(cbi_set_board_info(CBI_TAG_SSFC, &d8, sizeof(d8)),
		      EC_SUCCESS, NULL);
	count++;
	zassert_equal(cbi_set_board_info(CBI_TAG_REWORK_ID, &d8, sizeof(d8)),
		      EC_SUCCESS, NULL);
	count++;

	/* Read out all */
	zassert_equal(cbi_get_board_version(&d32), EC_SUCCESS, NULL);
	zassert_equal(d32, d8, "0x%x, 0x%x", d32, d8);
	zassert_equal(cbi_get_oem_id(&d32), EC_SUCCESS, NULL);
	zassert_equal(d32, d8, "0x%x, 0x%x", d32, d8);
	zassert_equal(cbi_get_sku_id(&d32), EC_SUCCESS, NULL);
	zassert_equal(d32, d8, "0x%x, 0x%x", d32, d8);
	size = sizeof(buf);
	zassert_equal(cbi_get_board_info(CBI_TAG_DRAM_PART_NUM, buf, &size),
		      EC_SUCCESS, NULL);
	zassert_equal(strncmp(buf, string, sizeof(string)), 0, NULL);
	/* This should be zassert_equal, but for EC test fmt is always "0x%x"
	 * which will generate compilation error.
	 */
	zassert_true((size_t)size - 1 == strlen(buf), "%zu, %zu",
		     (size_t)size - 1, strlen(buf));
	size = sizeof(buf);
	zassert_equal(cbi_get_board_info(CBI_TAG_OEM_NAME, buf, &size),
		      EC_SUCCESS, NULL);
	zassert_equal(strncmp(buf, string, sizeof(string)), 0, NULL);
	/* This should be zassert_equal, but for EC test fmt is always "0x%x"
	 * which will generate compilation error.
	 */
	zassert_true((size_t)size - 1 == strlen(buf), "%zu, %zu",
		     (size_t)size - 1, strlen(buf));
	zassert_equal(cbi_get_model_id(&d32), EC_SUCCESS, NULL);
	zassert_equal(d32, d8, "0x%x, 0x%x", d32, d8);
	zassert_equal(cbi_get_fw_config(&d32), EC_SUCCESS, NULL);
	zassert_equal(d32, d8, "0x%x, 0x%x", d32, d8);
	zassert_equal(cbi_get_pcb_supplier(&d32), EC_SUCCESS, NULL);
	zassert_equal(d32, d8, "0x%x, 0x%x", d32, d8);
	zassert_equal(cbi_get_ssfc(&d32), EC_SUCCESS, NULL);
	zassert_equal(d32, d8, "0x%x, 0x%x", d32, d8);
	zassert_equal(cbi_get_rework_id(&d64), EC_SUCCESS, NULL);
	/* This should be zassert_equal, but for EC test fmt is always "0x%x"
	 * which will generate compilation error.
	 */
	zassert_true((unsigned long long)d64 == (unsigned long long)d8,
		     "0x%llx, 0x%llx", (unsigned long long)d64,
		     (unsigned long long)d8);

	/* Fail if a (new) tag is missing from the unit test. */
	zassert_equal(count, CBI_TAG_COUNT, "%d, %d", count, CBI_TAG_COUNT);

	/* Write protect */
#ifdef CONFIG_WP_ACTIVE_HIGH
	gpio_set_level(GPIO_WP, 1);
#else
	gpio_set_level(GPIO_WP_L, 0);
#endif /* CONFIG_WP_ACTIVE_HIGH */
	zassert_equal(cbi_write(), EC_ERROR_ACCESS_DENIED, NULL);

	return EC_SUCCESS;
}

DECLARE_EC_TEST(test_bad_crc)
{
	uint8_t d8;
	const int tag = 0xff;
	uint8_t size;
	int crc;

	/* Bad CRC */
	d8 = 0xa5;
	zassert_equal(cbi_set_board_info(tag, &d8, sizeof(d8)), EC_SUCCESS,
		      NULL);
	i2c_read8(I2C_PORT_EEPROM, I2C_ADDR_EEPROM_FLAGS,
		   offsetof(struct cbi_header, crc), &crc);
	i2c_write8(I2C_PORT_EEPROM, I2C_ADDR_EEPROM_FLAGS,
		   offsetof(struct cbi_header, crc), ++crc);
	cbi_invalidate_cache();
	size = sizeof(d8);
	zassert_equal(cbi_get_board_info(tag, &d8, &size), EC_ERROR_UNKNOWN,
		      NULL);

	return EC_SUCCESS;
}

TEST_SUITE(test_suite_cbi)
{
	ztest_test_suite(test_cbi,
			 ztest_unit_test_setup_teardown(test_uint8, test_setup,
							test_teardown),
			 ztest_unit_test_setup_teardown(test_uint32, test_setup,
							test_teardown),
			 ztest_unit_test_setup_teardown(test_string, test_setup,
							test_teardown),
			 ztest_unit_test_setup_teardown(test_not_found,
							test_setup,
							test_teardown),
			 ztest_unit_test_setup_teardown(test_too_large,
							test_setup,
							test_teardown),
			 ztest_unit_test_setup_teardown(test_all_tags,
							test_setup,
							test_teardown),
			 ztest_unit_test_setup_teardown(test_bad_crc,
							test_setup,
							test_teardown));
	ztest_run_test_suite(test_cbi);
}