summaryrefslogtreecommitdiff
path: root/zephyr/test/system_safe_mode/src/system_safe_mode.c
blob: 2b861f2e5b28192af312ac94e54cec988b754152 (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
/* 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 "ec_commands.h"
#include "ec_tasks.h"
#include "host_command.h"
#include "panic.h"
#include "system.h"
#include "system_fake.h"
#include "system_safe_mode.h"

#include <zephyr/fff.h>
#include <zephyr/kernel.h>
#include <zephyr/ztest.h>

FAKE_VOID_FUNC(system_reset, int);

static void system_before(void *data)
{
	RESET_FAKE(system_reset);
	set_system_safe_mode(false);
	get_panic_data_write()->flags = 0;
	system_set_shrspi_image_copy(EC_IMAGE_RW);
}

static void enter_safe_mode_cb(struct k_timer *unused)
{
	k_sys_fatal_error_handler(K_ERR_CPU_EXCEPTION, NULL);
}
K_TIMER_DEFINE(enter_safe_mode, enter_safe_mode_cb, NULL);

ZTEST_USER(system_safe_mode, test_safe_mode_from_critical_task)
{
	/**
	 * Timer callback will run in sysworkq, which is a critical
	 * thread, so safe mode should not run.
	 */
	k_timer_start(&enter_safe_mode, K_NO_WAIT, K_NO_WAIT);
	/* Short wait to ensure enter_safe_mode_cb has a chance to run */
	k_msleep(100);
	zassert_false(system_is_in_safe_mode());
	zassert_false(get_panic_data_write()->flags &
		      PANIC_DATA_FLAG_SAFE_MODE_STARTED);
	zassert_true(get_panic_data_write()->flags &
		     PANIC_DATA_FLAG_SAFE_MODE_FAIL_PRECONDITIONS);
	zassert_equal(1, system_reset_fake.call_count);
}

ZTEST_USER(system_safe_mode, test_enter_safe_mode_from_ro)
{
	system_set_shrspi_image_copy(EC_IMAGE_RO);
	k_sys_fatal_error_handler(K_ERR_CPU_EXCEPTION, NULL);
	zassert_false(system_is_in_safe_mode());
	zassert_false(get_panic_data_write()->flags &
		      PANIC_DATA_FLAG_SAFE_MODE_STARTED);
	zassert_true(get_panic_data_write()->flags &
		     PANIC_DATA_FLAG_SAFE_MODE_FAIL_PRECONDITIONS);
	zassert_equal(1, system_reset_fake.call_count);
}

ZTEST_USER(system_safe_mode, test_enter_safe_mode_from_kernel_panic)
{
	system_set_shrspi_image_copy(EC_IMAGE_RO);
	k_sys_fatal_error_handler(K_ERR_KERNEL_PANIC, NULL);
	zassert_false(system_is_in_safe_mode());
	zassert_false(get_panic_data_write()->flags &
		      PANIC_DATA_FLAG_SAFE_MODE_STARTED);
	zassert_true(get_panic_data_write()->flags &
		     PANIC_DATA_FLAG_SAFE_MODE_FAIL_PRECONDITIONS);
	zassert_equal(1, system_reset_fake.call_count);
}

ZTEST_USER(system_safe_mode, test_enter_safe_mode_twice)
{
	zassert_false(system_is_in_safe_mode());

	k_sys_fatal_error_handler(K_ERR_CPU_EXCEPTION, NULL);
	zassert_true(system_is_in_safe_mode());
	zassert_true(get_panic_data_write()->flags &
		     PANIC_DATA_FLAG_SAFE_MODE_STARTED);
	zassert_false(get_panic_data_write()->flags &
		      PANIC_DATA_FLAG_SAFE_MODE_FAIL_PRECONDITIONS);
	zassert_equal(0, system_reset_fake.call_count);

	k_sys_fatal_error_handler(K_ERR_CPU_EXCEPTION, NULL);
	zassert_true(system_is_in_safe_mode());
	zassert_true(get_panic_data_write()->flags &
		     PANIC_DATA_FLAG_SAFE_MODE_STARTED);
	zassert_true(get_panic_data_write()->flags &
		     PANIC_DATA_FLAG_SAFE_MODE_FAIL_PRECONDITIONS);
	zassert_equal(1, system_reset_fake.call_count);
}

ZTEST_USER(system_safe_mode, test_enter_safe_mode)
{
	zassert_false(system_is_in_safe_mode());

	k_sys_fatal_error_handler(K_ERR_CPU_EXCEPTION, NULL);
	zassert_equal(0, system_reset_fake.call_count);
	zassert_true(system_is_in_safe_mode());
	zassert_true(get_panic_data_write()->flags &
		     PANIC_DATA_FLAG_SAFE_MODE_STARTED);
	zassert_false(get_panic_data_write()->flags &
		      PANIC_DATA_FLAG_SAFE_MODE_FAIL_PRECONDITIONS);
}

ZTEST_USER(system_safe_mode, test_safe_mode_reboot)
{
	zassert_false(system_is_in_safe_mode());
	k_sys_fatal_error_handler(0, NULL);
	zassert_true(system_is_in_safe_mode());
	zassert_equal(0, system_reset_fake.call_count);

	/* Wait half of timeout and system hasn't rebooted yet */
	k_msleep(CONFIG_PLATFORM_EC_SYSTEM_SAFE_MODE_TIMEOUT_MSEC / 2);
	zassert_equal(0, system_reset_fake.call_count);

	k_msleep(CONFIG_PLATFORM_EC_SYSTEM_SAFE_MODE_TIMEOUT_MSEC / 2);
	zassert_equal(1, system_reset_fake.call_count);
}

ZTEST_USER(system_safe_mode, test_blocked_command_in_safe_mode)
{
	struct ec_params_gpio_get cmd_params = {
		.name = "wp_l",
	};
	struct ec_response_gpio_get cmd_response;

	struct host_cmd_handler_args args = BUILD_HOST_COMMAND(
		EC_CMD_GPIO_GET, 0, cmd_response, cmd_params);

	zassert_false(system_is_in_safe_mode());
	zassert_ok(host_command_process(&args));

	k_sys_fatal_error_handler(K_ERR_CPU_EXCEPTION, NULL);

	zassert_true(system_is_in_safe_mode());
	zassert_true(host_command_process(&args));
}

ZTEST_SUITE(system_safe_mode, NULL, NULL, system_before, NULL, NULL);