summaryrefslogtreecommitdiff
path: root/test/cbi.c
blob: 936dc204b88738b9f77b163a4632b844576e5a42 (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
/* 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 "gpio.h"
#include "i2c.h"
#include "test_util.h"
#include "util.h"

void before_test(void)
{
	cbi_create();
	cbi_write();
}

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

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

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

	return EC_SUCCESS;
}

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

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

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

	return EC_SUCCESS;
}

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

	/* Set & get string */
	TEST_ASSERT(cbi_set_board_info(tag, string, sizeof(string))
		    == EC_SUCCESS);
	size = sizeof(buf);
	TEST_ASSERT(cbi_get_board_info(tag, buf, &size) == EC_SUCCESS);
	TEST_ASSERT(strncmp(buf, string, sizeof(string)) == 0);
	/* Size contains null byte */
	TEST_EQ((size_t)size - 1, strlen(buf), "%zu");

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

	return EC_SUCCESS;
}

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

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

	return EC_SUCCESS;
}

static int test_too_large(void)
{
	uint8_t buf[CBI_EEPROM_SIZE-1];
	const int tag = 0xff;

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

	return EC_SUCCESS;
}

static int test_all_tags(void)
{
	uint8_t d8;
	uint32_t d32;

	/* Populate all data and read out */
	d8 = 0x12;
	TEST_ASSERT(cbi_set_board_info(CBI_TAG_BOARD_VERSION, &d8, sizeof(d8))
		    == EC_SUCCESS);
	TEST_ASSERT(cbi_set_board_info(CBI_TAG_OEM_ID, &d8, sizeof(d8))
		    == EC_SUCCESS);
	TEST_ASSERT(cbi_set_board_info(CBI_TAG_SKU_ID, &d8, sizeof(d8))
		    == EC_SUCCESS);
	TEST_ASSERT(cbi_set_board_info(CBI_TAG_MODEL_ID, &d8, sizeof(d8))
		    == EC_SUCCESS);
	TEST_ASSERT(cbi_set_board_info(CBI_TAG_FW_CONFIG, &d8, sizeof(d8))
		    == EC_SUCCESS);
	TEST_ASSERT(cbi_set_board_info(CBI_TAG_PCB_SUPPLIER, &d8, sizeof(d8))
		    == EC_SUCCESS);
	TEST_ASSERT(cbi_set_board_info(CBI_TAG_SSFC, &d8, sizeof(d8))
		    == EC_SUCCESS);
	TEST_ASSERT(cbi_get_board_version(&d32) == EC_SUCCESS);
	TEST_EQ(d32, d8, "0x%x");
	TEST_ASSERT(cbi_get_oem_id(&d32) == EC_SUCCESS);
	TEST_EQ(d32, d8, "0x%x");
	TEST_ASSERT(cbi_get_sku_id(&d32) == EC_SUCCESS);
	TEST_EQ(d32, d8, "0x%x");
	TEST_ASSERT(cbi_get_model_id(&d32) == EC_SUCCESS);
	TEST_EQ(d32, d8, "0x%x");
	TEST_ASSERT(cbi_get_fw_config(&d32) == EC_SUCCESS);
	TEST_EQ(d32, d8, "0x%x");
	TEST_ASSERT(cbi_get_pcb_supplier(&d32) == EC_SUCCESS);
	TEST_EQ(d32, d8, "0x%x");
	TEST_ASSERT(cbi_get_ssfc(&d32) == EC_SUCCESS);
	TEST_EQ(d32, d8, "0x%x");

	/* Write protect */
	gpio_set_level(GPIO_WP, 1);
	TEST_ASSERT(cbi_write() == EC_ERROR_ACCESS_DENIED);

	return EC_SUCCESS;
}

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

	/* Bad CRC */
	d8 = 0xa5;
	TEST_ASSERT(cbi_set_board_info(tag, &d8, sizeof(d8)) == EC_SUCCESS);
	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);
	TEST_ASSERT(cbi_get_board_info(tag, &d8, &size) == EC_ERROR_UNKNOWN);

	return EC_SUCCESS;
}

void run_test(int argc, char **argv)
{
	RUN_TEST(test_uint8);
	RUN_TEST(test_uint32);
	RUN_TEST(test_string);
	RUN_TEST(test_not_found);
	RUN_TEST(test_too_large);
	RUN_TEST(test_all_tags);
	RUN_TEST(test_bad_crc);

	test_print_result();
}