Debian Codegraph @

tests/chip_wp.c

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