summaryrefslogtreecommitdiff
path: root/zephyr/test/system_shim/src/test_system.c
blob: 56031a4b672c748bdef5d1cdb302032b36d2a93a (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
294
295
296
297
298
299
/* Copyright 2020 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "drivers/cros_system.h"
#include "fakes.h"
#include "system.h"

#include <setjmp.h>

#include <zephyr/device.h>
#include <zephyr/drivers/bbram.h>
#include <zephyr/fff.h>
#include <zephyr/logging/log.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/ztest_assert.h>
#include <zephyr/ztest_test_new.h>

LOG_MODULE_REGISTER(test);

#define BBRAM_REGION_OFF(name) \
	DT_PROP(DT_PATH(named_bbram_regions, name), offset)
#define BBRAM_REGION_SIZE(name) \
	DT_PROP(DT_PATH(named_bbram_regions, name), size)

static char mock_data[64] =
	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@";

int system_preinitialize(const struct device *unused);

ZTEST(system, test_invalid_bbram_index)
{
	zassert_equal(EC_ERROR_INVAL,
		      system_get_bbram(SYSTEM_BBRAM_IDX_TRY_SLOT + 1, NULL));
}

ZTEST(system, test_bbram_get)
{
	const struct device *const bbram_dev =
		DEVICE_DT_GET(DT_CHOSEN(cros_ec_bbram));
	uint8_t output[10];
	int rc;

	/* Write expected data to read back */
	rc = bbram_write(bbram_dev, 0, ARRAY_SIZE(mock_data), mock_data);
	zassert_ok(rc);

	rc = system_get_bbram(SYSTEM_BBRAM_IDX_PD0, output);
	zassert_ok(rc);
	zassert_mem_equal(output, mock_data + BBRAM_REGION_OFF(pd0),
			  BBRAM_REGION_SIZE(pd0), NULL);

	rc = system_get_bbram(SYSTEM_BBRAM_IDX_PD1, output);
	zassert_ok(rc);
	zassert_mem_equal(output, mock_data + BBRAM_REGION_OFF(pd1),
			  BBRAM_REGION_SIZE(pd1), NULL);

	rc = system_get_bbram(SYSTEM_BBRAM_IDX_PD2, output);
	zassert_ok(rc);
	zassert_mem_equal(output, mock_data + BBRAM_REGION_OFF(pd2),
			  BBRAM_REGION_SIZE(pd2), NULL);

	rc = system_get_bbram(SYSTEM_BBRAM_IDX_TRY_SLOT, output);
	zassert_ok(rc);
	zassert_mem_equal(output, mock_data + BBRAM_REGION_OFF(try_slot),
			  BBRAM_REGION_SIZE(try_slot), NULL);
}

ZTEST(system, test_save_read_chip_reset_flags)
{
	uint32_t arbitrary_flags = 0x1234;

	chip_save_reset_flags(0);
	chip_save_reset_flags(arbitrary_flags);
	zassert_equal(chip_read_reset_flags(), arbitrary_flags);
}

ZTEST(system, test_system_set_get_scratchpad)
{
	/* Arbitrary values */
	uint32_t scratch_set = 0x1234;
	uint32_t scratch_read;

	system_set_scratchpad(scratch_set);
	system_get_scratchpad(&scratch_read);
	zassert_equal(scratch_read, scratch_set);
}

ZTEST(system, test_system_get_scratchpad_fail)
{
	const struct device *bbram_dev =
		DEVICE_DT_GET(DT_CHOSEN(cros_ec_bbram));

	zassert_ok(bbram_emul_set_invalid(bbram_dev, true));
	zassert_equal(-EC_ERROR_INVAL, system_get_scratchpad(NULL));
}

static jmp_buf jmp_hibernate;

static int _test_cros_system_native_posix_hibernate(const struct device *dev,
						    uint32_t seconds,
						    uint32_t microseconds)
{
	longjmp(jmp_hibernate, 1);

	return 0;
}

ZTEST(system, test_system_hibernate)
{
	/*
	 * Due to setjmp usage, this test provides no coverage, but does
	 * actually cover the code. This is due to a bug in LCOV.
	 */
	const struct device *sys_dev = device_get_binding("CROS_SYSTEM");
	int ret = setjmp(jmp_hibernate);
	/* Validate 0th and last bit preserved*/
	uint32_t secs = BIT(31) + 1;
	uint32_t msecs = BIT(31) + 3;

	zassert_not_null(sys_dev);

	cros_system_native_posix_hibernate_fake.custom_fake =
		_test_cros_system_native_posix_hibernate;

	if (ret == 0) {
		system_hibernate(secs, msecs);
	}

	zassert_not_equal(ret, 0);

	zassert_equal(cros_system_native_posix_hibernate_fake.call_count, 1);
	zassert_equal(cros_system_native_posix_hibernate_fake.arg0_val,
		      sys_dev);
	zassert_equal(cros_system_native_posix_hibernate_fake.arg1_val, secs);
	zassert_equal(cros_system_native_posix_hibernate_fake.arg2_val, msecs);
	zassert_equal(board_hibernate_fake.call_count, 1);
}

ZTEST(system, test_system_hibernate__failure)
{
	const struct device *sys_dev = device_get_binding("CROS_SYSTEM");
	/* Validate 0th and last bit preserved*/
	uint32_t secs = BIT(31) + 1;
	uint32_t msecs = BIT(31) + 3;

	zassert_not_null(sys_dev);

	cros_system_native_posix_hibernate_fake.return_val = -1;

	system_hibernate(secs, msecs);

	zassert_equal(cros_system_native_posix_hibernate_fake.call_count, 1);
	zassert_equal(cros_system_native_posix_hibernate_fake.arg0_val,
		      sys_dev);
	zassert_equal(cros_system_native_posix_hibernate_fake.arg1_val, secs);
	zassert_equal(cros_system_native_posix_hibernate_fake.arg2_val, msecs);
}

ZTEST(system, test_system_get_chip_values)
{
	const struct device *sys_dev = device_get_binding("CROS_SYSTEM");

	zassert_not_null(sys_dev);

	/* Vendor */
	cros_system_native_posix_get_chip_vendor_fake.return_val = "a";
	zassert_mem_equal(system_get_chip_vendor(), "a", sizeof("a"));
	zassert_equal(cros_system_native_posix_get_chip_vendor_fake.call_count,
		      1);
	zassert_equal(cros_system_native_posix_get_chip_vendor_fake.arg0_val,
		      sys_dev);

	/* Name */
	cros_system_native_posix_get_chip_name_fake.return_val = "b";
	zassert_mem_equal(system_get_chip_name(), "b", sizeof("b"));
	zassert_equal(cros_system_native_posix_get_chip_name_fake.call_count,
		      1);
	zassert_equal(cros_system_native_posix_get_chip_name_fake.arg0_val,
		      sys_dev);

	/* Revision */
	cros_system_native_posix_get_chip_revision_fake.return_val = "c";
	zassert_mem_equal(system_get_chip_revision(), "c", sizeof("c"));
	zassert_equal(
		cros_system_native_posix_get_chip_revision_fake.call_count, 1);
	zassert_equal(cros_system_native_posix_get_chip_revision_fake.arg0_val,
		      sys_dev);
}

static int _test_cros_system_native_posix_soc_reset(const struct device *dev)
{
	printf("called from soc reset");
	longjmp(jmp_hibernate, 1);

	return 0;
}

ZTEST(system, test_system_reset)
{
	/*
	 * Despite using setjmp this test consistently covers the code under
	 * test. Context: https://github.com/llvm/llvm-project/issues/50119
	 */
	const struct device *sys_dev = device_get_binding("CROS_SYSTEM");
	int ret = setjmp(jmp_hibernate);
	uint32_t arbitrary_flags_w_reset_wait_ext = 0x1234 |
						    SYSTEM_RESET_WAIT_EXT;
	uint32_t encoded_arbitrary_flags_w_reset_wait_ext;

	system_encode_save_flags(arbitrary_flags_w_reset_wait_ext,
				 &encoded_arbitrary_flags_w_reset_wait_ext);

	zassert_not_null(sys_dev);

	cros_system_native_posix_soc_reset_fake.custom_fake =
		_test_cros_system_native_posix_soc_reset;

	if (ret == 0) {
		system_reset(arbitrary_flags_w_reset_wait_ext);
	}

	zassert_not_null(sys_dev);

	zassert_equal(chip_read_reset_flags(),
		      encoded_arbitrary_flags_w_reset_wait_ext);

	zassert_equal(watchdog_reload_fake.call_count, 1000);
	zassert_equal(cros_system_native_posix_soc_reset_fake.call_count, 1);
	zassert_equal(cros_system_native_posix_soc_reset_fake.arg0_val,
		      sys_dev);
}

ZTEST_USER(system, test_system_console_cmd__idlestats)
{
	const struct device *sys_dev = device_get_binding("CROS_SYSTEM");
	const struct shell *shell_zephyr = get_ec_shell();
	const char *outbuffer;
	size_t buffer_size;

	zassert_not_null(sys_dev);

	shell_backend_dummy_clear_output(shell_zephyr);

	k_sleep(K_SECONDS(1));
	zassert_ok(shell_execute_cmd(shell_zephyr, "idlestats"), NULL);

	/* Weakly verify contents */
	outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size);
	zassert_not_equal(buffer_size, 0);
	zassert_not_null(strstr(outbuffer, "Time spent in deep-sleep:"));
	zassert_not_null(strstr(outbuffer, "Total time on:"));

	zassert_equal(cros_system_native_posix_deep_sleep_ticks_fake.call_count,
		      1);
}

ZTEST(system, test_init_invalid_reset_cause)
{
	cros_system_native_posix_get_reset_cause_fake.return_val = -1;
	zassert_equal(-1, system_preinitialize(NULL));
}

ZTEST(system, test_init_cause_vcc1_rst_pin)
{
	cros_system_native_posix_get_reset_cause_fake.return_val = VCC1_RST_PIN;
	chip_save_reset_flags(0);
	system_clear_reset_flags(0xffffffff);

	zassert_ok(system_preinitialize(NULL));
	zassert_equal(EC_RESET_FLAG_RESET_PIN, system_get_reset_flags());

	chip_save_reset_flags(EC_RESET_FLAG_INITIAL_PWR);
	zassert_ok(system_preinitialize(NULL));
	zassert_equal(EC_RESET_FLAG_RESET_PIN | EC_RESET_FLAG_POWER_ON |
			      EC_RESET_FLAG_POWER_ON,
		      system_get_reset_flags());
}

ZTEST(system, test_init_cause_debug_rst)
{
	cros_system_native_posix_get_reset_cause_fake.return_val = DEBUG_RST;
	chip_save_reset_flags(0);
	system_clear_reset_flags(0xffffffff);

	zassert_ok(system_preinitialize(NULL));
	zassert_equal(EC_RESET_FLAG_SOFT, system_get_reset_flags());
}

ZTEST(system, test_init_cause_watchdog_rst)
{
	cros_system_native_posix_get_reset_cause_fake.return_val = WATCHDOG_RST;
	chip_save_reset_flags(0);
	system_clear_reset_flags(0xffffffff);

	zassert_ok(system_preinitialize(NULL));
	zassert_equal(EC_RESET_FLAG_WATCHDOG, system_get_reset_flags());
}