blob: e3a1410f0ebbbe3decc8421defea0a8d0270a516 [file] [log] [blame]
Nikolai Artemievda1c8342021-10-21 00:58:12 +11001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Google Inc.
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
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "flash.h"
23#include "libflashrom.h"
24#include "chipdrivers.h"
25#include "writeprotect.h"
26
27/** Read and extract a single bit from the chip's registers */
28static enum flashrom_wp_result read_bit(uint8_t *value, bool *present, struct flashctx *flash, struct reg_bit_info bit)
29{
30 *present = bit.reg != INVALID_REG;
31 if (*present) {
32 if (spi_read_register(flash, bit.reg, value))
33 return FLASHROM_WP_ERR_READ_FAILED;
34 *value = (*value >> bit.bit_index) & 1;
35 } else {
36 /* Zero bit, it may be used by compare_ranges(). */
37 *value = 0;
38 }
39
40 return FLASHROM_WP_OK;
41}
42
43/** Read all WP configuration bits from the chip's registers. */
44static enum flashrom_wp_result read_wp_bits(struct wp_bits *bits, struct flashctx *flash)
45{
46 /*
47 * For each WP bit that is included in the chip's register layout, read
48 * the register that contains it, extracts the bit's value, and assign
49 * it to the appropriate field in the wp_bits structure.
50 */
51 const struct reg_bit_map *bit_map = &flash->chip->reg_bits;
52 bool ignored;
53 size_t i;
54 enum flashrom_wp_result ret;
55
Sergii Dmytruk081ffba2022-08-17 18:29:10 +030056 /*
57 * Write protection select bit (WPS) controls kind of write protection
58 * that is used by the chip. When set, BP bits are ignored and each
59 * block/sector has its own WP bit managed by special commands. When
60 * the bit is set and we can't change it, just bail out until
61 * implementation is extended to handle this kind of WP.
62 */
63 if (bit_map->wps.reg != INVALID_REG && bit_map->wps.writability != RW) {
64 bool wps_bit_present;
65 uint8_t wps;
66
67 ret = read_bit(&wps, &wps_bit_present, flash, bit_map->wps);
68 if (ret != FLASHROM_WP_OK)
69 return ret;
70
71 if (wps_bit_present && wps)
72 return FLASHROM_WP_ERR_UNSUPPORTED_STATE;
73 }
74
Nikolai Artemievda1c8342021-10-21 00:58:12 +110075 ret = read_bit(&bits->tb, &bits->tb_bit_present, flash, bit_map->tb);
76 if (ret != FLASHROM_WP_OK)
77 return ret;
78
79 ret = read_bit(&bits->sec, &bits->sec_bit_present, flash, bit_map->sec);
80 if (ret != FLASHROM_WP_OK)
81 return ret;
82
83 ret = read_bit(&bits->cmp, &bits->cmp_bit_present, flash, bit_map->cmp);
84 if (ret != FLASHROM_WP_OK)
85 return ret;
86
87 ret = read_bit(&bits->srp, &bits->srp_bit_present, flash, bit_map->srp);
88 if (ret != FLASHROM_WP_OK)
89 return ret;
90
91 ret = read_bit(&bits->srl, &bits->srl_bit_present, flash, bit_map->srl);
92 if (ret != FLASHROM_WP_OK)
93 return ret;
94
95 for (i = 0; i < ARRAY_SIZE(bits->bp); i++) {
96 if (bit_map->bp[i].reg == INVALID_REG)
97 break;
98
99 bits->bp_bit_count = i + 1;
100 ret = read_bit(&bits->bp[i], &ignored, flash, bit_map->bp[i]);
101 if (ret != FLASHROM_WP_OK)
102 return ret;
103 }
104
105 return ret;
106}
107
108/** Helper function for write_wp_bits(). */
109static void set_reg_bit(
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300110 uint8_t *reg_values, uint8_t *bit_masks, uint8_t *write_masks,
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100111 struct reg_bit_info bit, uint8_t value)
112{
113 if (bit.reg != INVALID_REG) {
114 reg_values[bit.reg] |= value << bit.bit_index;
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300115 bit_masks[bit.reg] |= 1 << bit.bit_index;
116
117 /* Avoid RO and OTP bits causing a register update */
118 if (bit.writability == RW)
119 write_masks[bit.reg] |= 1 << bit.bit_index;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100120 }
121}
122
123/** Write WP configuration bits to the flash's registers. */
124static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits)
125{
126 size_t i;
127 enum flash_reg reg;
128 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
129
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300130 /* Convert wp_bits to register values and masks */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100131 uint8_t reg_values[MAX_REGISTERS] = {0};
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300132 uint8_t bit_masks[MAX_REGISTERS] = {0}; /* masks of valid bits */
133 uint8_t write_masks[MAX_REGISTERS] = {0}; /* masks of written bits */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100134
135 for (i = 0; i < bits.bp_bit_count; i++)
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300136 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->bp[i], bits.bp[i]);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100137
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300138 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->tb, bits.tb);
139 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->sec, bits.sec);
140 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->cmp, bits.cmp);
141 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->srp, bits.srp);
142 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->srl, bits.srl);
Sergii Dmytruk801fcd02021-12-19 18:45:16 +0200143 /* Note: always setting WPS bit to zero until its fully supported. */
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300144 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->wps, 0);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100145
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300146 /* Write each register whose value was updated */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100147 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
148 if (!write_masks[reg])
149 continue;
150
151 uint8_t value;
152 if (spi_read_register(flash, reg, &value))
153 return FLASHROM_WP_ERR_READ_FAILED;
154
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300155 /* Skip unnecessary register writes */
156 uint8_t actual = value & write_masks[reg];
157 uint8_t expected = reg_values[reg] & write_masks[reg];
158 if (actual == expected)
159 continue;
160
161 value = (value & ~write_masks[reg]) | expected;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100162
163 if (spi_write_register(flash, reg, value))
164 return FLASHROM_WP_ERR_WRITE_FAILED;
165 }
166
Evan Bennd81997c2022-09-13 17:12:59 +1000167 enum flashrom_wp_result ret = FLASHROM_WP_OK;
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300168 /* Verify each register even if write to it was skipped */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100169 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300170 if (!bit_masks[reg])
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100171 continue;
172
173 uint8_t value;
174 if (spi_read_register(flash, reg, &value))
175 return FLASHROM_WP_ERR_READ_FAILED;
176
Evan Bennd81997c2022-09-13 17:12:59 +1000177 msg_cdbg2("%s: wp_verify reg:%u value:0x%x\n", __func__, reg, value);
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300178 uint8_t actual = value & bit_masks[reg];
179 uint8_t expected = reg_values[reg] & bit_masks[reg];
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100180
Evan Bennd81997c2022-09-13 17:12:59 +1000181 if (actual != expected) {
182 msg_cdbg("%s: wp_verify failed: reg:%u actual:0x%x expected:0x%x\n",
183 __func__, reg, actual, expected);
184 ret = FLASHROM_WP_ERR_VERIFY_FAILED;
185 }
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100186 }
187
Evan Bennd81997c2022-09-13 17:12:59 +1000188 return ret;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100189}
190
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100191/** Get the range selected by a WP configuration. */
192static enum flashrom_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits)
193{
194 flash->chip->decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash));
195
196 return FLASHROM_WP_OK;
197}
198
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100199/** Write protect bit values and the range they will activate. */
200struct wp_range_and_bits {
201 struct wp_bits bits;
202 struct wp_range range;
203};
204
205/**
206 * Comparator used for sorting ranges in get_ranges_and_wp_bits().
207 *
208 * Ranges are ordered by these attributes, in decreasing significance:
209 * (range length, range start, cmp bit, sec bit, tb bit, bp bits)
210 */
211static int compare_ranges(const void *aa, const void *bb)
212{
213 const struct wp_range_and_bits
214 *a = (const struct wp_range_and_bits *)aa,
215 *b = (const struct wp_range_and_bits *)bb;
216 int i;
217
218 int ord = 0;
219
220 if (ord == 0)
221 ord = a->range.len - b->range.len;
222
223 if (ord == 0)
224 ord = a->range.start - b->range.start;
225
226 if (ord == 0)
227 ord = a->bits.cmp - b->bits.cmp;
228
229 if (ord == 0)
230 ord = a->bits.sec - b->bits.sec;
231
232 if (ord == 0)
233 ord = a->bits.tb - b->bits.tb;
234
235 for (i = a->bits.bp_bit_count - 1; i >= 0; i--) {
236 if (ord == 0)
237 ord = a->bits.bp[i] - b->bits.bp[i];
238 }
239
240 return ord;
241}
242
243static bool can_write_bit(const struct reg_bit_info bit)
244{
245 /*
246 * TODO: check if the programmer supports writing the register that the
247 * bit is in. For example, some chipsets may only allow SR1 to be
248 * written.
249 */
250
251 return bit.reg != INVALID_REG && bit.writability == RW;
252}
253
254/**
255 * Enumerate all protection ranges that the chip supports and that are able to
256 * be activated, given limitations such as OTP bits or programmer-enforced
257 * restrictions. Returns a list of deduplicated wp_range_and_bits structures.
258 *
259 * Allocates a buffer that must be freed by the caller with free().
260 */
261static enum flashrom_wp_result get_ranges_and_wp_bits(struct flashctx *flash, struct wp_bits bits, struct wp_range_and_bits **ranges, size_t *count)
262{
263 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
264 size_t i;
265 /*
266 * Create a list of bits that affect the chip's protection range in
267 * range_bits. Each element is a pointer to a member of the wp_bits
268 * structure that will be modified.
269 *
270 * Some chips have range bits that cannot be changed (e.g. MX25L6473E
271 * has a one-time programmable TB bit). Rather than enumerating all
272 * possible values for unwritable bits, just read their values from the
273 * chip to ensure we only enumerate ranges that are actually available.
274 */
275 uint8_t *range_bits[ARRAY_SIZE(bits.bp) + 1 /* TB */ + 1 /* SEC */ + 1 /* CMP */];
276 size_t bit_count = 0;
277
278 for (i = 0; i < ARRAY_SIZE(bits.bp); i++) {
279 if (can_write_bit(reg_bits->bp[i]))
280 range_bits[bit_count++] = &bits.bp[i];
281 }
282
283 if (can_write_bit(reg_bits->tb))
284 range_bits[bit_count++] = &bits.tb;
285
286 if (can_write_bit(reg_bits->sec))
287 range_bits[bit_count++] = &bits.sec;
288
289 if (can_write_bit(reg_bits->cmp))
290 range_bits[bit_count++] = &bits.cmp;
291
292 /* Allocate output buffer */
293 *count = 1 << bit_count;
294 *ranges = calloc(*count, sizeof(struct wp_range_and_bits));
295
Sergii Dmytruk801fcd02021-12-19 18:45:16 +0200296 /* TODO: take WPS bit into account. */
297
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100298 size_t range_index;
299 for (range_index = 0; range_index < *count; range_index++) {
300 /*
301 * Extract bits from the range index and assign them to members
302 * of the wp_bits structure. The loop bounds ensure that all
303 * bit combinations will be enumerated.
304 */
305 for (i = 0; i < bit_count; i++)
306 *range_bits[i] = (range_index >> i) & 1;
307
308 struct wp_range_and_bits *output = &(*ranges)[range_index];
309
310 output->bits = bits;
311 enum flashrom_wp_result ret = get_wp_range(&output->range, flash, &bits);
312 if (ret != FLASHROM_WP_OK) {
313 free(*ranges);
314 return ret;
315 }
316
317 /* Debug: print range bits and range */
318 msg_gspew("Enumerated range: ");
319 if (bits.cmp_bit_present)
320 msg_gspew("CMP=%u ", bits.cmp);
321 if (bits.sec_bit_present)
322 msg_gspew("SEC=%u ", bits.sec);
323 if (bits.tb_bit_present)
324 msg_gspew("TB=%u ", bits.tb);
325 for (i = 0; i < bits.bp_bit_count; i++) {
326 size_t j = bits.bp_bit_count - i - 1;
327 msg_gspew("BP%zu=%u ", j, bits.bp[j]);
328 }
Nico Huber1e6aabc2022-05-28 16:39:07 +0200329 msg_gspew(" start=0x%08zx length=0x%08zx\n",
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100330 output->range.start, output->range.len);
331 }
332
333 /* Sort ranges. Ensures consistency if there are duplicate ranges. */
334 qsort(*ranges, *count, sizeof(struct wp_range_and_bits), compare_ranges);
335
336 /* Remove duplicates */
337 size_t output_index = 0;
338 struct wp_range *last_range = NULL;
339
340 for (i = 0; i < *count; i++) {
341 bool different_to_last =
342 (last_range == NULL) ||
343 ((*ranges)[i].range.start != last_range->start) ||
344 ((*ranges)[i].range.len != last_range->len);
345
346 if (different_to_last) {
347 /* Move range to the next free position */
348 (*ranges)[output_index] = (*ranges)[i];
349 output_index++;
350 /* Keep track of last non-duplicate range */
351 last_range = &(*ranges)[i].range;
352 }
353 }
354 /* Reduce count to only include non-duplicate ranges */
355 *count = output_index;
356
357 return FLASHROM_WP_OK;
358}
359
Nikolai Artemievb6112a52021-10-21 02:28:23 +1100360static bool ranges_equal(struct wp_range a, struct wp_range b)
361{
362 return (a.start == b.start) && (a.len == b.len);
363}
364
365/*
366 * Modify the range-related bits in a wp_bits structure so they select a given
367 * protection range. Bits that control the protection mode are not changed.
368 */
369static int set_wp_range(struct wp_bits *bits, struct flashctx *flash, const struct wp_range range)
370{
371 struct wp_range_and_bits *ranges = NULL;
372 size_t count;
373 size_t i;
374
375 enum flashrom_wp_result ret = get_ranges_and_wp_bits(flash, *bits, &ranges, &count);
376 if (ret != FLASHROM_WP_OK)
377 return ret;
378
379 /* Search for matching range */
380 ret = FLASHROM_WP_ERR_RANGE_UNSUPPORTED;
381 for (i = 0; i < count; i++) {
382
383 if (ranges_equal(ranges[i].range, range)) {
384 *bits = ranges[i].bits;
385 ret = 0;
386 break;
387 }
388 }
389
390 free(ranges);
391
392 return ret;
393}
394
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100395/** Get the mode selected by a WP configuration. */
396static int get_wp_mode(enum flashrom_wp_mode *mode, const struct wp_bits *bits)
397{
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100398 const enum flashrom_wp_mode wp_modes[2][2] = {
399 {
400 FLASHROM_WP_MODE_DISABLED, /* srl=0, srp=0 */
401 FLASHROM_WP_MODE_HARDWARE, /* srl=0, srp=1 */
402 }, {
403 FLASHROM_WP_MODE_POWER_CYCLE, /* srl=1, srp=0 */
404 FLASHROM_WP_MODE_PERMANENT, /* srl=1, srp=1 */
405 },
406 };
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100407
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100408 *mode = wp_modes[bits->srl][bits->srp];
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100409
410 return FLASHROM_WP_OK;
411}
412
413/** Modify a wp_bits structure such that it will select a specified protection mode. */
414static int set_wp_mode(struct wp_bits *bits, const enum flashrom_wp_mode mode)
415{
416 switch (mode) {
417 case FLASHROM_WP_MODE_DISABLED:
418 bits->srl = 0;
419 bits->srp = 0;
420 return FLASHROM_WP_OK;
421
422 case FLASHROM_WP_MODE_HARDWARE:
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100423 if (!bits->srp_bit_present)
424 return FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
425
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100426 bits->srl = 0;
427 bits->srp = 1;
428 return FLASHROM_WP_OK;
429
430 case FLASHROM_WP_MODE_POWER_CYCLE:
431 case FLASHROM_WP_MODE_PERMANENT:
432 default:
433 /*
434 * Don't try to enable power cycle or permanent protection for
435 * now. Those modes may be possible to activate on some chips,
436 * but they are usually unavailable by default or require special
437 * commands to activate.
438 */
439 return FLASHROM_WP_ERR_MODE_UNSUPPORTED;
440 }
441}
442
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100443static bool chip_supported(struct flashctx *flash)
444{
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100445 return (flash->chip != NULL) && (flash->chip->decode_range != NULL);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100446}
447
448enum flashrom_wp_result wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash)
449{
450 struct wp_bits bits;
451 enum flashrom_wp_result ret = FLASHROM_WP_OK;
452
453 if (!chip_supported(flash))
454 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
455
456 if (ret == FLASHROM_WP_OK)
457 ret = read_wp_bits(&bits, flash);
458
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100459 if (ret == FLASHROM_WP_OK)
460 ret = get_wp_range(&cfg->range, flash, &bits);
461
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100462 if (ret == FLASHROM_WP_OK)
463 ret = get_wp_mode(&cfg->mode, &bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100464
465 return ret;
466}
467
468enum flashrom_wp_result wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg)
469{
470 struct wp_bits bits;
471 enum flashrom_wp_result ret = FLASHROM_WP_OK;
472
473 if (!chip_supported(flash))
474 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
475
476 if (ret == FLASHROM_WP_OK)
477 ret = read_wp_bits(&bits, flash);
478
479 /* Set protection range */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100480 if (ret == FLASHROM_WP_OK)
481 ret = set_wp_range(&bits, flash, cfg->range);
482 if (ret == FLASHROM_WP_OK)
483 ret = write_wp_bits(flash, bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100484
485 /* Set protection mode */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100486 if (ret == FLASHROM_WP_OK)
487 ret = set_wp_mode(&bits, cfg->mode);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100488 if (ret == FLASHROM_WP_OK)
489 ret = write_wp_bits(flash, bits);
490
491 return ret;
492}
493
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100494enum flashrom_wp_result wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash)
495{
496 struct wp_bits bits;
497 struct wp_range_and_bits *range_pairs = NULL;
498 size_t count;
499 size_t i;
500
501 if (!chip_supported(flash))
502 return FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
503
504 enum flashrom_wp_result ret = read_wp_bits(&bits, flash);
505 if (ret != FLASHROM_WP_OK)
506 return ret;
507
508 ret = get_ranges_and_wp_bits(flash, bits, &range_pairs, &count);
509 if (ret != FLASHROM_WP_OK)
510 return ret;
511
512 *list = calloc(1, sizeof(struct flashrom_wp_ranges));
513 struct wp_range *ranges = calloc(count, sizeof(struct wp_range));
514
515 if (!(*list) || !ranges) {
516 free(*list);
517 free(ranges);
518 ret = FLASHROM_WP_ERR_OTHER;
519 goto out;
520 }
521 (*list)->count = count;
522 (*list)->ranges = ranges;
523
524 for (i = 0; i < count; i++)
525 ranges[i] = range_pairs[i].range;
526
527out:
528 free(range_pairs);
529 return ret;
530}
531
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100532/** @} */ /* end flashrom-wp */