xref: /aosp_15_r20/external/flashrom/tests/chip_wp.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2021 3mdeb Embedded Systems Consulting
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  */
16 
17 #include <include/test.h>
18 #include <stdio.h>
19 #include <string.h>
20 
21 #include "chipdrivers.h"
22 #include "flash.h"
23 #include "libflashrom.h"
24 #include "programmer.h"
25 #include "tests.h"
26 
unittest_print_cb(enum flashrom_log_level level,const char * fmt,va_list ap)27 static int unittest_print_cb(enum flashrom_log_level level, const char *fmt, va_list ap)
28 {
29 	if (level > FLASHROM_MSG_INFO) return 0;
30 	return vfprintf(stderr, fmt, ap);
31 }
32 
33 /*
34  * Tests in this file do not use any mocking, because using write-protect
35  * emulation in dummyflasher programmer is sufficient
36  */
37 
38 #define LAYOUT_TAIL_REGION_START 0x1000
39 
setup_chip(struct flashrom_flashctx * flash,struct flashrom_layout ** layout,struct flashchip * chip,const char * programmer_param)40 static void setup_chip(struct flashrom_flashctx *flash, struct flashrom_layout **layout,
41 		       struct flashchip *chip, const char *programmer_param)
42 {
43 	flash->chip = chip;
44 
45 	if (layout) {
46 		const size_t tail_start = LAYOUT_TAIL_REGION_START;
47 		const size_t tail_len = chip->total_size * KiB - 1;
48 
49 		assert_int_equal(0, flashrom_layout_new(layout));
50 		assert_int_equal(0, flashrom_layout_add_region(*layout, 0, tail_start - 1, "head"));
51 		assert_int_equal(0, flashrom_layout_add_region(*layout, tail_start, tail_len, "tail"));
52 
53 		flashrom_layout_set(flash, *layout);
54 	}
55 
56 	flashrom_set_log_callback((flashrom_log_callback *)&unittest_print_cb);
57 
58 	assert_int_equal(0, programmer_init(&programmer_dummy, programmer_param));
59 	/* Assignment below normally happens while probing, but this test is not probing. */
60 	flash->mst = &registered_masters[0];
61 }
62 
teardown(struct flashrom_layout ** layout)63 static void teardown(struct flashrom_layout **layout)
64 {
65 	assert_int_equal(0, programmer_shutdown());
66 	if (layout)
67 		flashrom_layout_release(*layout);
68 }
69 
70 /* Setup the struct for W25Q128.V, all values come from flashchips.c */
71 static const struct flashchip chip_W25Q128_V = {
72 	.vendor		= "aklm&dummyflasher",
73 	.total_size	= 16 * 1024,
74 	.page_size	= 1024,
75 	.tested		= TEST_OK_PREW,
76 	.read		= SPI_CHIP_READ,
77 	.write		= SPI_CHIP_WRITE256,
78 	.unlock         = SPI_DISABLE_BLOCKPROTECT,
79 	.feature_bits	= FEATURE_WRSR_WREN | FEATURE_OTP | FEATURE_WRSR_EXT2 | FEATURE_WRSR2 | FEATURE_WRSR3,
80 	.block_erasers  =
81 	{
82 		{
83 			.eraseblocks = { {4 * 1024, 4096} },
84 			.block_erase = SPI_BLOCK_ERASE_20,
85 		}, {
86 			.eraseblocks = { {32 * 1024, 512} },
87 			.block_erase = SPI_BLOCK_ERASE_52,
88 		}, {
89 			.eraseblocks = { {64 * 1024, 256} },
90 			.block_erase = SPI_BLOCK_ERASE_D8,
91 		}, {
92 			.eraseblocks = { {16 * 1024 * 1024, 1} },
93 			.block_erase = SPI_BLOCK_ERASE_60,
94 		}, {
95 			.eraseblocks = { {16 * 1024 * 1024, 1} },
96 			.block_erase = SPI_BLOCK_ERASE_C7,
97 		}
98 	},
99 	.reg_bits	=
100 	{
101 		.srp    = {STATUS1, 7, RW},
102 		.srl    = {STATUS2, 0, RW},
103 		.bp     = {{STATUS1, 2, RW}, {STATUS1, 3, RW}, {STATUS1, 4, RW}},
104 		.tb     = {STATUS1, 5, RW},
105 		.sec    = {STATUS1, 6, RW},
106 		.cmp    = {STATUS2, 6, RW},
107 		.wps    = {STATUS3, 2, RW},
108 	},
109 	.decode_range	= DECODE_RANGE_SPI25,
110 };
111 
112 /* Trying to set an unsupported WP range fails */
invalid_wp_range_dummyflasher_test_success(void ** state)113 void invalid_wp_range_dummyflasher_test_success(void **state)
114 {
115 	(void) state; /* unused */
116 
117 	const char *param_dup = "bus=spi,emulate=W25Q128FV,hwwp=no";
118 
119 	struct flashrom_flashctx flash = { 0 };
120 	struct flashchip mock_chip = chip_W25Q128_V;
121 	struct flashrom_wp_cfg *wp_cfg;
122 
123 	setup_chip(&flash, NULL, &mock_chip, param_dup);
124 
125 	assert_int_equal(0, flashrom_wp_cfg_new(&wp_cfg));
126 	flashrom_wp_set_mode(wp_cfg, FLASHROM_WP_MODE_HARDWARE);
127 	flashrom_wp_set_range(wp_cfg, 0x1000, 0x1000);
128 
129 	assert_int_equal(FLASHROM_WP_ERR_RANGE_UNSUPPORTED, flashrom_wp_write_cfg(&flash, wp_cfg));
130 
131 	teardown(NULL);
132 
133 	flashrom_wp_cfg_release(wp_cfg);
134 }
135 
136 /* Enabling hardware WP with a valid range succeeds */
set_wp_range_dummyflasher_test_success(void ** state)137 void set_wp_range_dummyflasher_test_success(void **state)
138 {
139 	(void) state; /* unused */
140 
141 	const char *param_dup = "bus=spi,emulate=W25Q128FV,hwwp=no";
142 
143 	struct flashrom_flashctx flash = { 0 };
144 	struct flashchip mock_chip = chip_W25Q128_V;
145 	struct flashrom_wp_cfg *wp_cfg;
146 
147 	size_t start;
148 	size_t len;
149 
150 	setup_chip(&flash, NULL, &mock_chip, param_dup);
151 
152 	/* Use last 4 KiB for a range. */
153 	assert_int_equal(0, flashrom_wp_cfg_new(&wp_cfg));
154 	flashrom_wp_set_mode(wp_cfg, FLASHROM_WP_MODE_HARDWARE);
155 	flashrom_wp_set_range(wp_cfg, mock_chip.total_size * KiB - 4 * KiB, 4 * KiB);
156 
157 	assert_int_equal(0, flashrom_wp_write_cfg(&flash, wp_cfg));
158 
159 	/* Check that range was set correctly. */
160 	assert_int_equal(0, flashrom_wp_read_cfg(wp_cfg, &flash));
161 	flashrom_wp_get_range(&start, &len, wp_cfg);
162 	assert_int_equal(16 * MiB - 4 * KiB, start);
163 	assert_int_equal(4 * KiB, len);
164 
165 	teardown(NULL);
166 
167 	flashrom_wp_cfg_release(wp_cfg);
168 }
169 
170 /* Enable hardware WP and verify that it can not be unset */
switch_wp_mode_dummyflasher_test_success(void ** state)171 void switch_wp_mode_dummyflasher_test_success(void **state)
172 {
173 	(void) state; /* unused */
174 
175 	const char *param_dup = "bus=spi,emulate=W25Q128FV,hwwp=yes";
176 
177 	struct flashrom_flashctx flash = { 0 };
178 	struct flashchip mock_chip = chip_W25Q128_V;
179 	struct flashrom_wp_cfg *wp_cfg;
180 
181 	setup_chip(&flash, NULL, &mock_chip, param_dup);
182 
183 	assert_int_equal(0, flashrom_wp_cfg_new(&wp_cfg));
184 
185 	/* Check initial mode. */
186 	assert_int_equal(0, flashrom_wp_read_cfg(wp_cfg, &flash));
187 	assert_int_equal(FLASHROM_WP_MODE_DISABLED, flashrom_wp_get_mode(wp_cfg));
188 
189 	/* Enable hardware protection, which can't be unset because simulated
190 	   HW WP pin is in active state. */
191 	flashrom_wp_set_mode(wp_cfg, FLASHROM_WP_MODE_HARDWARE);
192 	assert_int_equal(0, flashrom_wp_write_cfg(&flash, wp_cfg));
193 	assert_int_equal(0, flashrom_wp_read_cfg(wp_cfg, &flash));
194 	assert_int_equal(FLASHROM_WP_MODE_HARDWARE, flashrom_wp_get_mode(wp_cfg));
195 
196 	/* Check that write-protection mode can't be unset. */
197 	flashrom_wp_set_mode(wp_cfg, FLASHROM_WP_MODE_DISABLED);
198 	assert_int_equal(FLASHROM_WP_ERR_VERIFY_FAILED, flashrom_wp_write_cfg(&flash, wp_cfg));
199 
200 	/* Final mode should be "hardware". */
201 	assert_int_equal(0, flashrom_wp_read_cfg(wp_cfg, &flash));
202 	assert_int_equal(FLASHROM_WP_MODE_HARDWARE, flashrom_wp_get_mode(wp_cfg));
203 
204 	teardown(NULL);
205 
206 	flashrom_wp_cfg_release(wp_cfg);
207 }
208 
209 /* WP state is decoded correctly from status registers */
wp_init_from_status_dummyflasher_test_success(void ** state)210 void wp_init_from_status_dummyflasher_test_success(void **state)
211 {
212 	(void) state; /* unused */
213 
214 	/*
215 	 * CMP  (S14) = 1 (range complement)
216 	 * SRP1 (S8)  = 1
217 	 * SRP0 (S7)  = 1 (`SRP1 == 1 && SRP0 == 1` is permanent mode)
218 	 * SEC  (S6)  = 1 (base unit is a 4 KiB sector)
219 	 * TB   (S5)  = 1 (bottom up range)
220 	 * BP2  (S4)  = 0
221 	 * BP1  (S3)  = 1
222 	 * BP0  (S2)  = 1 (bp: BP2-0 == 0b011 == 3)
223 	 *
224 	 * Range coefficient is `2 ** (bp - 1)`, which is 4 in this case.
225 	 * Multiplaying that by base unit gives 16 KiB protected region at the
226 	 * bottom (start of the chip), which is then complemented.
227 	 */
228 	const char *param_dup = "bus=spi,emulate=W25Q128FV,spi_status=0x41ec";
229 
230 	struct flashrom_flashctx flash = { 0 };
231 	struct flashchip mock_chip = chip_W25Q128_V;
232 	struct flashrom_wp_cfg *wp_cfg;
233 
234 	size_t start;
235 	size_t len;
236 
237 	setup_chip(&flash, NULL, &mock_chip, param_dup);
238 
239 	assert_int_equal(0, flashrom_wp_cfg_new(&wp_cfg));
240 
241 	/* Verify that WP mode reflects SPI status */
242 	assert_int_equal(0, flashrom_wp_read_cfg(wp_cfg, &flash));
243 	assert_int_equal(FLASHROM_WP_MODE_PERMANENT, flashrom_wp_get_mode(wp_cfg));
244 	flashrom_wp_get_range(&start, &len, wp_cfg);
245 	assert_int_equal(0x004000, start);
246 	assert_int_equal(0xffc000, len);
247 
248 	teardown(NULL);
249 
250 	flashrom_wp_cfg_release(wp_cfg);
251 }
252 
253 /* Enabled WP makes full chip erasure fail */
full_chip_erase_with_wp_dummyflasher_test_success(void ** state)254 void full_chip_erase_with_wp_dummyflasher_test_success(void **state)
255 {
256 	(void) state; /* unused */
257 
258 	struct flashrom_flashctx flash = { 0 };
259 	struct flashrom_layout *layout;
260 	struct flashchip mock_chip = chip_W25Q128_V;
261 	struct flashrom_wp_cfg *wp_cfg;
262 
263 	const char *param_dup = "bus=spi,emulate=W25Q128FV,hwwp=yes";
264 
265 	setup_chip(&flash, &layout, &mock_chip, param_dup);
266 	/* Layout regions are created by setup_chip(). */
267 	assert_int_equal(0, flashrom_layout_include_region(layout, "head"));
268 	assert_int_equal(0, flashrom_layout_include_region(layout, "tail"));
269 
270 	assert_int_equal(0, flashrom_wp_cfg_new(&wp_cfg));
271 
272 	/* Write protection takes effect only after changing SRP values, so at
273 	   this stage WP is not enabled and erase completes successfully. */
274 	assert_int_equal(0, flashrom_flash_erase(&flash));
275 
276 	/* Write non-erased value to entire chip so that erase operations cannot
277 	 * be optimized away. */
278 	unsigned long size = flashrom_flash_getsize(&flash);
279 	uint8_t *const contents = malloc(size);
280 	assert_non_null(contents);
281 	memset(contents, UNERASED_VALUE(&flash), size);
282 	assert_int_equal(0, flashrom_image_write(&flash, contents, size, NULL));
283 	free(contents);
284 
285 	assert_int_equal(0, flashrom_wp_read_cfg(wp_cfg, &flash));
286 
287 	/* Hardware-protect first 4 KiB. */
288 	flashrom_wp_set_range(wp_cfg, 0, 4 * KiB);
289 	flashrom_wp_set_mode(wp_cfg, FLASHROM_WP_MODE_HARDWARE);
290 
291 	assert_int_equal(0, flashrom_wp_write_cfg(&flash, wp_cfg));
292 
293 	/* Try erasing the chip again. Now that WP is active, the first 4 KiB is
294 	   protected and we're trying to erase the whole chip, erase should
295 	   fail. */
296 	assert_int_equal(1, flashrom_flash_erase(&flash));
297 
298 	teardown(&layout);
299 
300 	flashrom_wp_cfg_release(wp_cfg);
301 }
302 
303 /* Enabled WP does not block erasing unprotected parts of the chip */
partial_chip_erase_with_wp_dummyflasher_test_success(void ** state)304 void partial_chip_erase_with_wp_dummyflasher_test_success(void **state)
305 {
306 	(void) state; /* unused */
307 
308 	struct flashrom_flashctx flash = { 0 };
309 	struct flashrom_layout *layout;
310 	struct flashchip mock_chip = chip_W25Q128_V;
311 	struct flashrom_wp_cfg *wp_cfg;
312 
313 	const char *param_dup = "bus=spi,emulate=W25Q128FV,hwwp=yes";
314 
315 	setup_chip(&flash, &layout, &mock_chip, param_dup);
316 	/* Layout region is created by setup_chip(). */
317 	assert_int_equal(0, flashrom_layout_include_region(layout, "tail"));
318 
319 	assert_int_equal(0, flashrom_wp_cfg_new(&wp_cfg));
320 
321 	assert_int_equal(0, flashrom_wp_read_cfg(wp_cfg, &flash));
322 
323 	/* Hardware-protect head region. */
324 	flashrom_wp_set_mode(wp_cfg, FLASHROM_WP_MODE_HARDWARE);
325 	flashrom_wp_set_range(wp_cfg, 0, LAYOUT_TAIL_REGION_START);
326 
327 	assert_int_equal(0, flashrom_wp_write_cfg(&flash, wp_cfg));
328 
329 	/* First 4 KiB is the only protected part of the chip and the region
330 	   we included covers only unprotected part, so erase operation should
331 	   succeed. */
332 	assert_int_equal(0, flashrom_flash_erase(&flash));
333 
334 	teardown(&layout);
335 
336 	flashrom_wp_cfg_release(wp_cfg);
337 }
338 
339 /* Chip register values & masks are calculated correctly by WP */
wp_get_register_values_and_masks(void ** state)340 void wp_get_register_values_and_masks(void **state)
341 {
342 	(void) state; /* unused */
343 
344 	/*
345 	 * Test with range: start = 0x004000, lengh = 0xffc000
346 	 *
347 	 * WP should use these bit values:
348 	 * WPS  (S17) = 0 (write protect scheme)
349 	 * CMP  (S14) = 1 (range complement)
350 	 * SRP1 (S8)  = 0
351 	 * SRP0 (S7)  = 1 (`SRP1 == 1 && SRP0 == 1` is permanent mode)
352 	 * SEC  (S6)  = 1 (base unit is a 4 KiB sector)
353 	 * TB   (S5)  = 1 (bottom up range)
354 	 * BP2  (S4)  = 0
355 	 * BP1  (S3)  = 1
356 	 * BP0  (S2)  = 1 (bp: BP2-0 == 0b011 == 3)
357 	 *
358 	 * Register values:
359 	 * SR1 = 0b11101100 = 0xec
360 	 * SR2 = 0b01000000 = 0x40
361 	 * SR3 = 0b00000000 = 0x00
362 	 *
363 	 * Masks for WP bits in registers:
364 	 * SR1: 0b11111100 = 0xfc
365 	 * SR2: 0b01000000 = 0x41
366 	 * SR3: 0b00000100 = 0x04
367 	 *
368 	 * All WP bits are RW so write masks should be the same as the bit masks.
369 	 *
370 	 */
371 	struct flashrom_flashctx flash = { 0 };
372 	struct flashchip mock_chip = chip_W25Q128_V;
373 	struct flashrom_wp_cfg *wp_cfg;
374 
375 	uint8_t reg_values[MAX_REGISTERS];
376 	uint8_t bit_masks[MAX_REGISTERS];
377 	uint8_t write_masks[MAX_REGISTERS];
378 
379 	setup_chip(&flash, NULL, &mock_chip, "bus=spi,emulate=W25Q128FV");
380 
381 	assert_int_equal(0, flashrom_wp_cfg_new(&wp_cfg));
382 	flashrom_wp_set_mode(wp_cfg, FLASHROM_WP_MODE_HARDWARE);
383 	flashrom_wp_set_range(wp_cfg, 0x004000, 0xffc000);
384 
385 	assert_int_equal(0, wp_cfg_to_reg_values(reg_values, bit_masks, write_masks, &flash, wp_cfg));
386 
387 	assert_int_equal(0xec, reg_values[STATUS1]);
388 	assert_int_equal(0x40, reg_values[STATUS2]);
389 	assert_int_equal(0x00, reg_values[STATUS3]);
390 
391 	assert_int_equal(0xfc, bit_masks[STATUS1]);
392 	assert_int_equal(0x41, bit_masks[STATUS2]);
393 	assert_int_equal(0x04, bit_masks[STATUS3]);
394 
395 	assert_int_equal(0xfc, write_masks[STATUS1]);
396 	assert_int_equal(0x41, write_masks[STATUS2]);
397 	assert_int_equal(0x04, write_masks[STATUS3]);
398 
399 	teardown(NULL);
400 
401 	flashrom_wp_cfg_release(wp_cfg);
402 }
403