summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/default/src/flash.c
blob: b0cf9df449a9e575e94c0570f3cd31c87c1fa704 (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
/* 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 <zephyr/drivers/emul.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/gpio/gpio_emul.h>
#include <zephyr/zephyr.h>
#include <zephyr/ztest.h>

#include "ec_commands.h"
#include "emul/emul_flash.h"
#include "flash.h"
#include "host_command.h"
#include "test/drivers/test_state.h"

#define WP_L_GPIO_PATH DT_PATH(named_gpios, wp_l)

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);
}

ZTEST_USER(flash, test_hostcmd_flash_protect_wp_asserted)
{
	struct ec_response_flash_protect response;
	struct ec_params_flash_protect params = {
		.mask = 0,
		.flags = 0,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_FLASH_PROTECT, 0, response, params);
	/* The original flags not 0 as GPIO WP_L asserted */
	uint32_t expected_flags = EC_FLASH_PROTECT_GPIO_ASSERTED;

	/* Get the flash protect */
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);

	/* Enable RO_AT_BOOT */
	params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
	params.flags = EC_FLASH_PROTECT_RO_AT_BOOT;
	expected_flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW;
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);

	/* Disable RO_AT_BOOT; should change nothing as GPIO WP_L is asserted */
	params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
	params.flags = 0;
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);

	/* Enable ALL_NOW */
	params.mask = EC_FLASH_PROTECT_ALL_NOW;
	params.flags = EC_FLASH_PROTECT_ALL_NOW;
	expected_flags |= EC_FLASH_PROTECT_ALL_NOW;
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);

	/* Disable ALL_NOW; should change nothing as GPIO WP_L is asserted */
	params.mask = EC_FLASH_PROTECT_ALL_NOW;
	params.flags = 0;
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);

	/* Disable RO_AT_BOOT; should change nothing as GPIO WP_L is asserted */
	params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
	params.flags = 0;
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);
}

ZTEST_USER(flash, test_hostcmd_flash_protect_wp_deasserted)
{
	struct ec_response_flash_protect response;
	struct ec_params_flash_protect params = {
		.mask = 0,
		.flags = 0,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_FLASH_PROTECT, 0, response, params);
	/* The original flags 0 as GPIO WP_L deasserted */
	uint32_t expected_flags = 0;

	zassert_ok(gpio_wp_l_set(1), NULL);

	/* Get the flash protect */
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);

	/* Enable RO_AT_BOOT */
	params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
	params.flags = EC_FLASH_PROTECT_RO_AT_BOOT;
	expected_flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW;
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);

	/* Disable RO_AT_BOOT */
	params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
	params.flags = 0;
	expected_flags &=
		~(EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW);
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);

	/* Enable RO_AT_BOOT */
	params.mask = EC_FLASH_PROTECT_RO_AT_BOOT;
	params.flags = EC_FLASH_PROTECT_RO_AT_BOOT;
	expected_flags |= EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW;
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);

	/* Enable ALL_NOW; should change nothing as GPIO WP_L is deasserted */
	params.mask = EC_FLASH_PROTECT_ALL_NOW;
	params.flags = EC_FLASH_PROTECT_ALL_NOW;
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flags, expected_flags, "response.flags = %d",
		      response.flags);
}

#define TEST_BUF_SIZE 0x100

ZTEST_USER(flash, test_hostcmd_flash_write_and_erase)
{
	uint8_t in_buf[TEST_BUF_SIZE];
	uint8_t out_buf[sizeof(struct ec_params_flash_write) + TEST_BUF_SIZE];

	struct ec_params_flash_read read_params = {
		.offset = 0x10000,
		.size = TEST_BUF_SIZE,
	};
	struct host_cmd_handler_args read_args =
		BUILD_HOST_COMMAND(EC_CMD_FLASH_READ, 0, in_buf, read_params);

	struct ec_params_flash_erase erase_params = {
		.offset = 0x10000,
		.size = 0x10000,
	};
	struct host_cmd_handler_args erase_args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_FLASH_ERASE, 0, erase_params);

	/* The write host command structs need to be filled run-time */
	struct ec_params_flash_write *write_params =
		(struct ec_params_flash_write *)out_buf;
	struct host_cmd_handler_args write_args =
		BUILD_HOST_COMMAND_SIMPLE(EC_CMD_FLASH_WRITE, 0);

	write_params->offset = 0x10000;
	write_params->size = TEST_BUF_SIZE;
	write_args.params = write_params;
	write_args.params_size = sizeof(*write_params) + TEST_BUF_SIZE;

	/* Flash write to all 0xec */
	memset(write_params + 1, 0xec, TEST_BUF_SIZE);
	zassert_ok(host_command_process(&write_args), NULL);

	/* Flash read and compare the readback data */
	zassert_ok(host_command_process(&read_args), NULL);
	zassert_equal(read_args.response_size, TEST_BUF_SIZE, NULL);
	zassert_equal(in_buf[0], 0xec, "readback data not expected: 0x%x",
		      in_buf[0]);
	zassert_equal(in_buf[TEST_BUF_SIZE - 1], 0xec,
		      "readback data not expected: 0x%x", in_buf[0]);

	/* Flash erase */
	zassert_ok(host_command_process(&erase_args), NULL);

	/* Flash read and compare the readback data */
	zassert_ok(host_command_process(&read_args), NULL);
	zassert_equal(in_buf[0], 0xff, "readback data not expected: 0x%x",
		      in_buf[0]);
	zassert_equal(in_buf[TEST_BUF_SIZE - 1], 0xff,
		      "readback data not expected: 0x%x", in_buf[0]);
}

static void flash_reset(void)
{
	/* Set the GPIO WP_L to default */
	gpio_wp_l_set(0);

	/* Reset the protection flags */
	cros_flash_emul_protect_reset();
}

static void flash_before(void *data)
{
	ARG_UNUSED(data);
	flash_reset();
}

static void flash_after(void *data)
{
	ARG_UNUSED(data);
	flash_reset();

	/* The test modifies this bank. Erase it in case of failure. */
	crec_flash_erase(0x10000, 0x10000);
}

ZTEST_SUITE(flash, drivers_predicate_post_main, NULL, flash_before, flash_after,
	    NULL);