summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/common_cbi/src/test_common_cbi.c
blob: bf8da93a9e1d793b4080452f4198ffb212d22b65 (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
/* Copyright 2022 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "cros_board_info.h"
#include "host_command.h"
#include "test/drivers/test_mocks.h"
#include "test/drivers/test_state.h"

#include <zephyr/drivers/eeprom.h>
#include <zephyr/drivers/gpio/gpio_emul.h>
#include <zephyr/ztest.h>

#define WP_L_GPIO_PATH DT_PATH(named_gpios, wp_l)
#define CBI_EEPROM_DEV DEVICE_DT_GET(DT_NODELABEL(cbi_eeprom))

FAKE_VALUE_FUNC(int, eeprom_load, uint8_t, uint8_t *, int);

static int gpio_wp_l_set(int value)
{
	const struct device *wp_l_gpio_dev =
		DEVICE_DT_GET(DT_GPIO_CTLR(WP_L_GPIO_PATH, gpios));

	return gpio_emul_input_set(wp_l_gpio_dev,
				   DT_GPIO_PIN(WP_L_GPIO_PATH, gpios), value);
}

static int __test_eeprom_load_default_impl(uint8_t offset, uint8_t *data,
					   int len)
{
	int ret = eeprom_read(CBI_EEPROM_DEV, offset, data, len);

	return ret;
}

ZTEST(common_cbi, test_cbi_latch_eeprom_wp)
{
	const struct gpio_dt_spec *wp = GPIO_DT_FROM_ALIAS(gpio_cbi_wp);

	zassert_equal(gpio_emul_output_get(wp->port, wp->pin), 0);

	cbi_latch_eeprom_wp();

	zassert_equal(gpio_emul_output_get(wp->port, wp->pin), 1);
}

ZTEST(common_cbi, test_do_cbi_read__cant_load_head)
{
	enum cbi_data_tag arbitrary_unused_tag = CBI_TAG_SKU_ID;
	uint8_t arbitrary_unused_byte_buffer[100];
	uint8_t unused_data_size;

	/* Force a do_cbi_read() to eeprom */
	cbi_invalidate_cache();

	/* Return arbitrary nonzero value */
	eeprom_load_fake.return_val = 1;
	eeprom_load_fake.custom_fake = NULL;

	zassert_equal(cbi_get_board_info(arbitrary_unused_tag,
					 arbitrary_unused_byte_buffer,
					 &unused_data_size),
		      EC_ERROR_UNKNOWN);
}

ZTEST(common_cbi, test_cbi_set_string__null_str)
{
	struct cbi_data data = { 0 };
	struct cbi_data unused_data = { 0 };
	enum cbi_data_tag arbitrary_valid_tag = CBI_TAG_BOARD_VERSION;

	zassert_equal(cbi_set_string((uint8_t *)&data, arbitrary_valid_tag,
				     NULL),
		      (uint8_t *)&data);

	/* Validate no writes happened */
	zassert_mem_equal(&data, &unused_data, sizeof(data));
}

ZTEST(common_cbi, test_cbi_set_string)
{
	const char arbitrary_str[] = "hello cbi";
	enum cbi_data_tag arbitrary_valid_tag = CBI_TAG_SKU_ID;

	struct cbi_data_wrapper {
		struct cbi_data data;
		uint8_t value_arr[ARRAY_SIZE(arbitrary_str)];
	};
	struct cbi_data_wrapper cbi_data = { 0 };

	/* Set some provided memory then check values */
	uint8_t *addr_byte_after_store = cbi_set_string(
		(uint8_t *)&cbi_data, arbitrary_valid_tag, arbitrary_str);

	zassert_equal(cbi_data.data.tag, arbitrary_valid_tag);
	zassert_equal(cbi_data.data.size, ARRAY_SIZE(arbitrary_str));
	zassert_mem_equal(cbi_data.data.value, arbitrary_str,
			  cbi_data.data.size);

	uint32_t expected_added_memory =
		(ARRAY_SIZE(arbitrary_str) + sizeof(cbi_data.data));

	/* Validate that next address for write was set appropriately */
	zassert_equal_ptr(addr_byte_after_store - expected_added_memory,
			  &cbi_data.data);
}

ZTEST_USER(common_cbi, test_hc_cbi_set_then_get)
{
	const uint8_t data[] = "I love test coverage! <3";

	struct actual_set_params {
		struct ec_params_set_cbi params;
		uint8_t actual_data[ARRAY_SIZE(data)];
	};

	struct actual_set_params hc_set_params = {
		.params = {
		.tag = CBI_TAG_SKU_ID,
		/* Force a reload */
		.flag = CBI_SET_INIT,
		.size = ARRAY_SIZE(data),
		},
	};
	struct host_cmd_handler_args set_args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_SET_CROS_BOARD_INFO, 0, hc_set_params);

	memcpy(hc_set_params.params.data, data, ARRAY_SIZE(data));

	/* Turn off write-protect so we can actually write */
	gpio_wp_l_set(1);

	zassert_ok(host_command_process(&set_args));

	/* Now verify our write by invoking a get host command */

	struct ec_params_get_cbi hc_get_params = {
		.flag = CBI_GET_RELOAD,
		.tag = hc_set_params.params.tag,
	};

	struct test_ec_params_get_cbi_response {
		uint8_t data[ARRAY_SIZE(data)];
	};
	struct test_ec_params_get_cbi_response hc_get_response;
	struct host_cmd_handler_args get_args = BUILD_HOST_COMMAND(
		EC_CMD_GET_CROS_BOARD_INFO, 0, hc_get_response, hc_get_params);

	zassert_ok(host_command_process(&get_args));
	zassert_mem_equal(hc_get_response.data, hc_set_params.actual_data,
			  hc_set_params.params.size);
}

ZTEST_USER(common_cbi, test_hc_cbi_set__bad_size)
{
	const char data[] = "hello";

	struct actual_set_params {
		struct ec_params_set_cbi params;
		/* We want less data than we need for our size */
		uint8_t actual_data[0];
	};
	struct actual_set_params hc_set_params = {
		.params = {
		.tag = CBI_TAG_SKU_ID,
		/* Force a reload */
		.flag = CBI_SET_INIT,
		.size = ARRAY_SIZE(data),
		},
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_SET_CROS_BOARD_INFO, 0, hc_set_params);

	zassert_equal(host_command_process(&args), EC_RES_INVALID_PARAM);
}

ZTEST_USER(common_cbi, test_hc_cbi_set_then_get__with_too_small_response)
{
	const uint8_t data[] = "I'm way too big of a payload for you!";

	struct actual_set_params {
		struct ec_params_set_cbi params;
		uint8_t actual_data[ARRAY_SIZE(data)];
	};

	struct actual_set_params hc_set_params = {
		.params = {
		.tag = CBI_TAG_SKU_ID,
		/* Force a reload */
		.flag = CBI_SET_INIT,
		.size = ARRAY_SIZE(data),
		},
	};
	struct host_cmd_handler_args set_args = BUILD_HOST_COMMAND_PARAMS(
		EC_CMD_SET_CROS_BOARD_INFO, 0, hc_set_params);

	memcpy(hc_set_params.params.data, data, ARRAY_SIZE(data));

	/* Turn off write-protect so we can actually write */
	gpio_wp_l_set(1);

	zassert_ok(host_command_process(&set_args));

	/* Now verify our write by invoking a get host command */

	struct ec_params_get_cbi hc_get_params = {
		.flag = CBI_GET_RELOAD,
		.tag = hc_set_params.params.tag,
	};

	struct test_ec_params_get_cbi_response {
		/*
		 * Want want less space than we need to retrieve cbi data, by
		 * allocating an array of size zero, we're implicitly setting
		 * the response_max value of the host command to be zero. So the
		 * host command will fail because it the EC knows it doesn't
		 * have enough response space to actually fetch the data for the
		 * host.
		 */
		uint8_t data[0];
	};
	struct test_ec_params_get_cbi_response hc_get_response;
	struct host_cmd_handler_args get_args = BUILD_HOST_COMMAND(
		EC_CMD_GET_CROS_BOARD_INFO, 0, hc_get_response, hc_get_params);

	zassert_equal(host_command_process(&get_args), EC_RES_INVALID_PARAM);
}

static void test_common_cbi_before_after(void *test_data)
{
	RESET_FAKE(eeprom_load);
	eeprom_load_fake.custom_fake = __test_eeprom_load_default_impl;

	cbi_create();
}

ZTEST_SUITE(common_cbi, drivers_predicate_post_main, NULL,
	    test_common_cbi_before_after, test_common_cbi_before_after, NULL);