summaryrefslogtreecommitdiff
path: root/zephyr/test/drivers/flash/src/flash.c
blob: 753983cd6c54e172b180851e28bcb4827e6990fb (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
/* 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 "emul/emul_flash.h"
#include "flash.h"
#include "host_command.h"
#include "system.h"
#include "test/drivers/test_state.h"

#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/gpio/gpio_emul.h>
#include <zephyr/kernel.h>
#include <zephyr/shell/shell_dummy.h>
#include <zephyr/ztest.h>

#define WP_L_GPIO_PATH NAMED_GPIOS_GPIO_NODE(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);
}

ZTEST_USER(flash, test_hostcmd_flash_read__overflow)
{
	struct ec_params_flash_read params = {
		.size = 32,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_FLASH_READ, 0, params);

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

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

#define EC_FLASH_REGION_START \
	MIN(CONFIG_EC_PROTECTED_STORAGE_OFF, CONFIG_EC_WRITABLE_STORAGE_OFF)

static void test_region_info(uint32_t region, uint32_t expected_offset,
			     uint32_t expected_size)
{
	struct ec_response_flash_region_info response;
	struct ec_params_flash_region_info params = {
		.region = region,
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND(
		EC_CMD_FLASH_REGION_INFO, 1, response, params);

	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.offset, expected_offset, NULL);
	zassert_equal(response.size, expected_size, NULL);
}

ZTEST_USER(flash, test_hostcmd_flash_region_info_ro)
{
	test_region_info(EC_FLASH_REGION_RO,
			 CONFIG_EC_PROTECTED_STORAGE_OFF +
				 CONFIG_RO_STORAGE_OFF - EC_FLASH_REGION_START,
			 EC_FLASH_REGION_RO_SIZE);
}

ZTEST_USER(flash, test_hostcmd_flash_region_info_active)
{
	test_region_info(EC_FLASH_REGION_ACTIVE,
			 flash_get_rw_offset(system_get_active_copy()) -
				 EC_FLASH_REGION_START,
			 CONFIG_EC_WRITABLE_STORAGE_SIZE);
}

ZTEST_USER(flash, test_hostcmd_flash_region_info_active_wp_ro)
{
	test_region_info(EC_FLASH_REGION_WP_RO,
			 CONFIG_WP_STORAGE_OFF - EC_FLASH_REGION_START,
			 CONFIG_WP_STORAGE_SIZE);
}

ZTEST_USER(flash, test_hostcmd_flash_region_info_active_update)
{
	test_region_info(EC_FLASH_REGION_UPDATE,
			 flash_get_rw_offset(system_get_update_copy()) -
				 EC_FLASH_REGION_START,
			 CONFIG_EC_WRITABLE_STORAGE_SIZE);
}

ZTEST_USER(flash, test_hostcmd_flash_region_info_active_invalid)
{
	struct ec_response_flash_region_info response;
	struct ec_params_flash_region_info params = {
		/* Get an invalid region */
		.region = 10,
	};
	struct host_cmd_handler_args args = BUILD_HOST_COMMAND(
		EC_CMD_FLASH_REGION_INFO, 1, response, params);

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

ZTEST_USER(flash, test_hostcmd_flash_info_1)
{
	struct ec_response_flash_info_1 response;
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND_RESPONSE(EC_CMD_FLASH_INFO, 1, response);

	if (!IS_ENABLED(CONFIG_PLATFORM_EC_USE_ZEPHYR_FLASH_PAGE_LAYOUT)) {
		/* Get the flash info. */
		zassert_ok(host_command_process(&args), NULL);
		zassert_equal(response.flash_size,
			      CONFIG_FLASH_SIZE_BYTES - EC_FLASH_REGION_START,
			      "response.flash_size = %d", response.flash_size);
		zassert_equal(response.flags, 0, "response.flags = %d",
			      response.flags);
		zassert_equal(response.write_block_size,
			      CONFIG_FLASH_WRITE_SIZE,
			      "response.write_block_size = %d",
			      response.write_block_size);
		zassert_equal(response.erase_block_size,
			      CONFIG_FLASH_ERASE_SIZE,
			      "response.erase_block_size = %d",
			      response.erase_block_size);
		zassert_equal(response.protect_block_size,
			      CONFIG_FLASH_BANK_SIZE,
			      "response.protect_block_size = %d",
			      response.protect_block_size);
		zassert_equal(response.write_ideal_size,
			      (args.response_max -
			       sizeof(struct ec_params_flash_write)) &
				      ~(CONFIG_FLASH_WRITE_SIZE - 1),
			      "response.write_ideal_size = %d",
			      response.write_ideal_size);
	} else {
		/*
		 * Flash sector description not supported in FLASH_INFO
		 * version 1 command
		 */
		zassert_equal(host_command_process(&args),
			      EC_RES_INVALID_VERSION, NULL);
	}
}

ZTEST_USER(flash, test_hostcmd_flash_info_2_zero_bank)
{
	struct ec_response_flash_info_2 response = {};
	struct ec_params_flash_info_2 params = {
		.num_banks_desc = 0,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_FLASH_INFO, 2, response, params);

	/* Get the flash info. */
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response.flash_size,
		      CONFIG_FLASH_SIZE_BYTES - EC_FLASH_REGION_START, "got %d",
		      response.flash_size);
	zassert_equal(response.flags, 0, "got %d", response.flags);
	zassert_equal(
		response.write_ideal_size,
		(args.response_max - sizeof(struct ec_params_flash_write)) &
			~(CONFIG_FLASH_WRITE_SIZE - 1),
		"got %d", response.write_ideal_size);
	zassert_equal(response.num_banks_total, 1, "got %d",
		      response.num_banks_total);
	zassert_equal(response.num_banks_desc, 0, "got %d",
		      response.num_banks_desc);
}

ZTEST_USER(flash, test_hostcmd_flash_info_2)
{
	uint8_t response_buffer[sizeof(struct ec_response_flash_info_2) +
				sizeof(struct ec_flash_bank)];
	struct ec_response_flash_info_2 *response =
		(struct ec_response_flash_info_2 *)response_buffer;
	struct ec_params_flash_info_2 params = {
		.num_banks_desc = 1,
	};
	struct host_cmd_handler_args args =
		BUILD_HOST_COMMAND(EC_CMD_FLASH_INFO, 2, *response, params);

	/* Get the flash info. */
	zassert_ok(host_command_process(&args), NULL);
	zassert_equal(response->flash_size,
		      CONFIG_FLASH_SIZE_BYTES - EC_FLASH_REGION_START, "got %d",
		      response->flash_size);
	zassert_equal(response->flags, 0, "got %d", response->flags);
	zassert_equal(
		response->write_ideal_size,
		(args.response_max - sizeof(struct ec_params_flash_write)) &
			~(CONFIG_FLASH_WRITE_SIZE - 1),
		"got %d", response->write_ideal_size);
	zassert_equal(response->num_banks_total, 1, "got %d",
		      response->num_banks_total);
	zassert_equal(response->num_banks_desc, 1, "got %d",
		      response->num_banks_desc);
	zassert_equal(response->banks[0].count,
		      CONFIG_FLASH_SIZE_BYTES / CONFIG_FLASH_BANK_SIZE,
		      "got %d", response->banks[0].count);
	zassert_equal(response->banks[0].size_exp,
		      __fls(CONFIG_FLASH_BANK_SIZE), "got %d",
		      response->banks[0].size_exp);
	zassert_equal(response->banks[0].write_size_exp,
		      __fls(CONFIG_FLASH_WRITE_SIZE), "got %d",
		      response->banks[0].write_size_exp);
	zassert_equal(response->banks[0].erase_size_exp,
		      __fls(CONFIG_FLASH_ERASE_SIZE), "got %d",
		      response->banks[0].erase_size_exp);
	zassert_equal(response->banks[0].protect_size_exp,
		      __fls(CONFIG_FLASH_BANK_SIZE), "got %d",
		      response->banks[0].protect_size_exp);
}

ZTEST_USER(flash, test_console_cmd_flash_info)
{
	const struct shell *shell_zephyr = get_ec_shell();
	const char *outbuffer;
	size_t buffer_size;
	/* Arbitrary array size for sprintf should not need this amount */
	char format_buffer[100];

	shell_backend_dummy_clear_output(shell_zephyr);

	zassert_ok(shell_execute_cmd(shell_zephyr, "flashinfo"));
	outbuffer = shell_backend_dummy_get_output(shell_zephyr, &buffer_size);

	zassert_true(buffer_size > 0, NULL);

	sprintf(format_buffer, "Usable:  %4d KB",
		CONFIG_FLASH_SIZE_BYTES / 1024);
	zassert_not_null(strstr(outbuffer, format_buffer));

	sprintf(format_buffer, "Write:   %4d B (ideal %d B)",
		CONFIG_FLASH_WRITE_SIZE, CONFIG_FLASH_WRITE_IDEAL_SIZE);
	zassert_not_null(strstr(outbuffer, format_buffer));

	if (IS_ENABLED(CONFIG_PLATFORM_EC_USE_ZEPHYR_FLASH_PAGE_LAYOUT)) {
		sprintf(format_buffer, "%d regions", crec_flash_total_banks());
		zassert_not_null(strstr(outbuffer, format_buffer));
	}

	sprintf(format_buffer, "Erase:   %4d B", CONFIG_FLASH_ERASE_SIZE);
	zassert_not_null(strstr(outbuffer, format_buffer));

	sprintf(format_buffer, "Protect: %4d B", CONFIG_FLASH_BANK_SIZE);
	zassert_not_null(strstr(outbuffer, format_buffer));

	zassert_not_null(strstr(outbuffer, "wp_gpio_asserted: ON"));
	zassert_not_null(strstr(outbuffer, "ro_at_boot: OFF"));
	zassert_not_null(strstr(outbuffer, "all_at_boot: OFF"));
	zassert_not_null(strstr(outbuffer, "ro_now: OFF"));
	zassert_not_null(strstr(outbuffer, "all_now: OFF"));
	zassert_not_null(strstr(outbuffer, "STUCK: OFF"));
	zassert_not_null(strstr(outbuffer, "INCONSISTENT: OFF"));
	zassert_not_null(strstr(outbuffer, "UNKNOWN_ERROR: OFF"));
	zassert_not_null(strstr(outbuffer, "Protected now"));
}

ZTEST_USER(flash, test_console_cmd_flashwp__invalid)
{
	/* Command requires a 2nd CLI arg */
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "flashwp"), NULL);
}

ZTEST_USER(flash, test_console_cmd_flashwp__now)
{
	uint32_t current;

	zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp true"), NULL);

	current = crec_flash_get_protect();
	zassert_true(EC_FLASH_PROTECT_GPIO_ASSERTED & current, "current = %08x",
		     current);
	zassert_true(EC_FLASH_PROTECT_RO_AT_BOOT & current, "current = %08x",
		     current);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp now"), NULL);

	current = crec_flash_get_protect();
	zassert_true(current & EC_FLASH_PROTECT_ALL_NOW, "current = %08x",
		     current);
}

ZTEST_USER(flash, test_console_cmd_flashwp__all)
{
	uint32_t current;

	zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp true"), NULL);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp all"), NULL);

	current = crec_flash_get_protect();
	zassert_true(EC_FLASH_PROTECT_ALL_NOW & current, "current = %08x",
		     current);
}

ZTEST_USER(flash, test_console_cmd_flashwp__bool_false)
{
	uint32_t current;

	/* Set RO_AT_BOOT and verify */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp true"), NULL);

	current = crec_flash_get_protect();
	zassert_true(current & EC_FLASH_PROTECT_RO_AT_BOOT, "current = %08x",
		     current);

	gpio_wp_l_set(1);

	/* Now clear it */
	zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp false"), NULL);

	current = crec_flash_get_protect();
	zassert_false(current & EC_FLASH_PROTECT_RO_AT_BOOT, "current = %08x",
		      current);
}

ZTEST_USER(flash, test_console_cmd_flashwp__bool_true)
{
	uint32_t current;

	gpio_wp_l_set(1);

	zassert_ok(shell_execute_cmd(get_ec_shell(), "flashwp true"), NULL);

	current = crec_flash_get_protect();
	zassert_equal(EC_FLASH_PROTECT_RO_AT_BOOT | EC_FLASH_PROTECT_RO_NOW,
		      current, "current = %08x", current);
}

ZTEST_USER(flash, test_console_cmd_flashwp__bad_param)
{
	zassert_ok(!shell_execute_cmd(get_ec_shell(), "flashwp xyz"), NULL);
}

/**
 * @brief Prepare a region of flash for the test_crec_flash_is_erased* tests
 *
 * @param offset Offset to write bytes at.
 * @param size Number of bytes to erase.
 * @param make_write If true, write an arbitrary byte after erase so the region
 *                   is no longer fully erased.
 */
static void setup_flash_region_helper(uint32_t offset, uint32_t size,
				      bool make_write)
{
	struct ec_params_flash_erase erase_params = {
		.offset = offset,
		.size = size,
	};
	struct host_cmd_handler_args erase_args =
		BUILD_HOST_COMMAND_PARAMS(EC_CMD_FLASH_ERASE, 0, erase_params);
	int rv;

	rv = host_command_process(&erase_args);
	zassert_ok(rv, "Got %d", rv);

	if (make_write) {
		/* Sized for flash_write header plus one byte of data */
		uint8_t out_buf[sizeof(struct ec_params_flash_write) +
				sizeof(uint8_t)];

		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 = offset;
		write_params->size = 1;
		write_args.params = write_params;
		write_args.params_size = sizeof(out_buf);

		/* Write one byte at start of region */
		out_buf[sizeof(*write_params)] = 0xec;

		zassert_ok(host_command_process(&write_args), NULL);
	}
}

ZTEST_USER(flash, test_crec_flash_is_erased__happy)
{
	uint32_t offset = 0x10000;

	setup_flash_region_helper(offset, CONFIG_FLASH_ERASE_SIZE, false);

	zassert_true(crec_flash_is_erased(offset, CONFIG_FLASH_ERASE_SIZE),
		     NULL);
}

ZTEST_USER(flash, test_crec_flash_is_erased__not_erased)
{
	uint32_t offset = 0x10000;

	setup_flash_region_helper(offset, CONFIG_FLASH_ERASE_SIZE, true);

	zassert_true(!crec_flash_is_erased(offset, CONFIG_FLASH_ERASE_SIZE),
		     NULL);
}

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