blob: 48c5ae6d0e66ccff0991ec52bb14722d89c4cec9 [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
56 ret = read_bit(&bits->tb, &bits->tb_bit_present, flash, bit_map->tb);
57 if (ret != FLASHROM_WP_OK)
58 return ret;
59
60 ret = read_bit(&bits->sec, &bits->sec_bit_present, flash, bit_map->sec);
61 if (ret != FLASHROM_WP_OK)
62 return ret;
63
64 ret = read_bit(&bits->cmp, &bits->cmp_bit_present, flash, bit_map->cmp);
65 if (ret != FLASHROM_WP_OK)
66 return ret;
67
68 ret = read_bit(&bits->srp, &bits->srp_bit_present, flash, bit_map->srp);
69 if (ret != FLASHROM_WP_OK)
70 return ret;
71
72 ret = read_bit(&bits->srl, &bits->srl_bit_present, flash, bit_map->srl);
73 if (ret != FLASHROM_WP_OK)
74 return ret;
75
Sergii Dmytruk801fcd02021-12-19 18:45:16 +020076 /* Note: WPS bit isn't read here, because it's not part of any range. */
77
Nikolai Artemievda1c8342021-10-21 00:58:12 +110078 for (i = 0; i < ARRAY_SIZE(bits->bp); i++) {
79 if (bit_map->bp[i].reg == INVALID_REG)
80 break;
81
82 bits->bp_bit_count = i + 1;
83 ret = read_bit(&bits->bp[i], &ignored, flash, bit_map->bp[i]);
84 if (ret != FLASHROM_WP_OK)
85 return ret;
86 }
87
88 return ret;
89}
90
91/** Helper function for write_wp_bits(). */
92static void set_reg_bit(
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +030093 uint8_t *reg_values, uint8_t *bit_masks, uint8_t *write_masks,
Nikolai Artemievda1c8342021-10-21 00:58:12 +110094 struct reg_bit_info bit, uint8_t value)
95{
96 if (bit.reg != INVALID_REG) {
97 reg_values[bit.reg] |= value << bit.bit_index;
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +030098 bit_masks[bit.reg] |= 1 << bit.bit_index;
99
100 /* Avoid RO and OTP bits causing a register update */
101 if (bit.writability == RW)
102 write_masks[bit.reg] |= 1 << bit.bit_index;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100103 }
104}
105
106/** Write WP configuration bits to the flash's registers. */
107static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits)
108{
109 size_t i;
110 enum flash_reg reg;
111 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
112
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300113 /* Convert wp_bits to register values and masks */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100114 uint8_t reg_values[MAX_REGISTERS] = {0};
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300115 uint8_t bit_masks[MAX_REGISTERS] = {0}; /* masks of valid bits */
116 uint8_t write_masks[MAX_REGISTERS] = {0}; /* masks of written bits */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100117
118 for (i = 0; i < bits.bp_bit_count; i++)
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300119 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->bp[i], bits.bp[i]);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100120
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300121 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->tb, bits.tb);
122 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->sec, bits.sec);
123 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->cmp, bits.cmp);
124 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->srp, bits.srp);
125 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->srl, bits.srl);
Sergii Dmytruk801fcd02021-12-19 18:45:16 +0200126 /* Note: always setting WPS bit to zero until its fully supported. */
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300127 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->wps, 0);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100128
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300129 /* Write each register whose value was updated */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100130 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
131 if (!write_masks[reg])
132 continue;
133
134 uint8_t value;
135 if (spi_read_register(flash, reg, &value))
136 return FLASHROM_WP_ERR_READ_FAILED;
137
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300138 /* Skip unnecessary register writes */
139 uint8_t actual = value & write_masks[reg];
140 uint8_t expected = reg_values[reg] & write_masks[reg];
141 if (actual == expected)
142 continue;
143
144 value = (value & ~write_masks[reg]) | expected;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100145
146 if (spi_write_register(flash, reg, value))
147 return FLASHROM_WP_ERR_WRITE_FAILED;
148 }
149
Evan Bennd81997c2022-09-13 17:12:59 +1000150 enum flashrom_wp_result ret = FLASHROM_WP_OK;
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300151 /* Verify each register even if write to it was skipped */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100152 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300153 if (!bit_masks[reg])
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100154 continue;
155
156 uint8_t value;
157 if (spi_read_register(flash, reg, &value))
158 return FLASHROM_WP_ERR_READ_FAILED;
159
Evan Bennd81997c2022-09-13 17:12:59 +1000160 msg_cdbg2("%s: wp_verify reg:%u value:0x%x\n", __func__, reg, value);
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300161 uint8_t actual = value & bit_masks[reg];
162 uint8_t expected = reg_values[reg] & bit_masks[reg];
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100163
Evan Bennd81997c2022-09-13 17:12:59 +1000164 if (actual != expected) {
165 msg_cdbg("%s: wp_verify failed: reg:%u actual:0x%x expected:0x%x\n",
166 __func__, reg, actual, expected);
167 ret = FLASHROM_WP_ERR_VERIFY_FAILED;
168 }
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100169 }
170
Evan Bennd81997c2022-09-13 17:12:59 +1000171 return ret;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100172}
173
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100174/** Get the range selected by a WP configuration. */
175static enum flashrom_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits)
176{
177 flash->chip->decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash));
178
179 return FLASHROM_WP_OK;
180}
181
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100182/** Write protect bit values and the range they will activate. */
183struct wp_range_and_bits {
184 struct wp_bits bits;
185 struct wp_range range;
186};
187
188/**
189 * Comparator used for sorting ranges in get_ranges_and_wp_bits().
190 *
191 * Ranges are ordered by these attributes, in decreasing significance:
192 * (range length, range start, cmp bit, sec bit, tb bit, bp bits)
193 */
194static int compare_ranges(const void *aa, const void *bb)
195{
196 const struct wp_range_and_bits
197 *a = (const struct wp_range_and_bits *)aa,
198 *b = (const struct wp_range_and_bits *)bb;
199 int i;
200
201 int ord = 0;
202
203 if (ord == 0)
204 ord = a->range.len - b->range.len;
205
206 if (ord == 0)
207 ord = a->range.start - b->range.start;
208
209 if (ord == 0)
210 ord = a->bits.cmp - b->bits.cmp;
211
212 if (ord == 0)
213 ord = a->bits.sec - b->bits.sec;
214
215 if (ord == 0)
216 ord = a->bits.tb - b->bits.tb;
217
218 for (i = a->bits.bp_bit_count - 1; i >= 0; i--) {
219 if (ord == 0)
220 ord = a->bits.bp[i] - b->bits.bp[i];
221 }
222
223 return ord;
224}
225
226static bool can_write_bit(const struct reg_bit_info bit)
227{
228 /*
229 * TODO: check if the programmer supports writing the register that the
230 * bit is in. For example, some chipsets may only allow SR1 to be
231 * written.
232 */
233
234 return bit.reg != INVALID_REG && bit.writability == RW;
235}
236
237/**
238 * Enumerate all protection ranges that the chip supports and that are able to
239 * be activated, given limitations such as OTP bits or programmer-enforced
240 * restrictions. Returns a list of deduplicated wp_range_and_bits structures.
241 *
242 * Allocates a buffer that must be freed by the caller with free().
243 */
244static 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)
245{
246 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
247 size_t i;
248 /*
249 * Create a list of bits that affect the chip's protection range in
250 * range_bits. Each element is a pointer to a member of the wp_bits
251 * structure that will be modified.
252 *
253 * Some chips have range bits that cannot be changed (e.g. MX25L6473E
254 * has a one-time programmable TB bit). Rather than enumerating all
255 * possible values for unwritable bits, just read their values from the
256 * chip to ensure we only enumerate ranges that are actually available.
257 */
258 uint8_t *range_bits[ARRAY_SIZE(bits.bp) + 1 /* TB */ + 1 /* SEC */ + 1 /* CMP */];
259 size_t bit_count = 0;
260
261 for (i = 0; i < ARRAY_SIZE(bits.bp); i++) {
262 if (can_write_bit(reg_bits->bp[i]))
263 range_bits[bit_count++] = &bits.bp[i];
264 }
265
266 if (can_write_bit(reg_bits->tb))
267 range_bits[bit_count++] = &bits.tb;
268
269 if (can_write_bit(reg_bits->sec))
270 range_bits[bit_count++] = &bits.sec;
271
272 if (can_write_bit(reg_bits->cmp))
273 range_bits[bit_count++] = &bits.cmp;
274
275 /* Allocate output buffer */
276 *count = 1 << bit_count;
277 *ranges = calloc(*count, sizeof(struct wp_range_and_bits));
278
Sergii Dmytruk801fcd02021-12-19 18:45:16 +0200279 /* TODO: take WPS bit into account. */
280
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100281 size_t range_index;
282 for (range_index = 0; range_index < *count; range_index++) {
283 /*
284 * Extract bits from the range index and assign them to members
285 * of the wp_bits structure. The loop bounds ensure that all
286 * bit combinations will be enumerated.
287 */
288 for (i = 0; i < bit_count; i++)
289 *range_bits[i] = (range_index >> i) & 1;
290
291 struct wp_range_and_bits *output = &(*ranges)[range_index];
292
293 output->bits = bits;
294 enum flashrom_wp_result ret = get_wp_range(&output->range, flash, &bits);
295 if (ret != FLASHROM_WP_OK) {
296 free(*ranges);
297 return ret;
298 }
299
300 /* Debug: print range bits and range */
301 msg_gspew("Enumerated range: ");
302 if (bits.cmp_bit_present)
303 msg_gspew("CMP=%u ", bits.cmp);
304 if (bits.sec_bit_present)
305 msg_gspew("SEC=%u ", bits.sec);
306 if (bits.tb_bit_present)
307 msg_gspew("TB=%u ", bits.tb);
308 for (i = 0; i < bits.bp_bit_count; i++) {
309 size_t j = bits.bp_bit_count - i - 1;
310 msg_gspew("BP%zu=%u ", j, bits.bp[j]);
311 }
Nico Huber1e6aabc2022-05-28 16:39:07 +0200312 msg_gspew(" start=0x%08zx length=0x%08zx\n",
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100313 output->range.start, output->range.len);
314 }
315
316 /* Sort ranges. Ensures consistency if there are duplicate ranges. */
317 qsort(*ranges, *count, sizeof(struct wp_range_and_bits), compare_ranges);
318
319 /* Remove duplicates */
320 size_t output_index = 0;
321 struct wp_range *last_range = NULL;
322
323 for (i = 0; i < *count; i++) {
324 bool different_to_last =
325 (last_range == NULL) ||
326 ((*ranges)[i].range.start != last_range->start) ||
327 ((*ranges)[i].range.len != last_range->len);
328
329 if (different_to_last) {
330 /* Move range to the next free position */
331 (*ranges)[output_index] = (*ranges)[i];
332 output_index++;
333 /* Keep track of last non-duplicate range */
334 last_range = &(*ranges)[i].range;
335 }
336 }
337 /* Reduce count to only include non-duplicate ranges */
338 *count = output_index;
339
340 return FLASHROM_WP_OK;
341}
342
Nikolai Artemievb6112a52021-10-21 02:28:23 +1100343static bool ranges_equal(struct wp_range a, struct wp_range b)
344{
345 return (a.start == b.start) && (a.len == b.len);
346}
347
348/*
349 * Modify the range-related bits in a wp_bits structure so they select a given
350 * protection range. Bits that control the protection mode are not changed.
351 */
352static int set_wp_range(struct wp_bits *bits, struct flashctx *flash, const struct wp_range range)
353{
354 struct wp_range_and_bits *ranges = NULL;
355 size_t count;
356 size_t i;
357
358 enum flashrom_wp_result ret = get_ranges_and_wp_bits(flash, *bits, &ranges, &count);
359 if (ret != FLASHROM_WP_OK)
360 return ret;
361
362 /* Search for matching range */
363 ret = FLASHROM_WP_ERR_RANGE_UNSUPPORTED;
364 for (i = 0; i < count; i++) {
365
366 if (ranges_equal(ranges[i].range, range)) {
367 *bits = ranges[i].bits;
368 ret = 0;
369 break;
370 }
371 }
372
373 free(ranges);
374
375 return ret;
376}
377
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100378/** Get the mode selected by a WP configuration. */
379static int get_wp_mode(enum flashrom_wp_mode *mode, const struct wp_bits *bits)
380{
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100381 const enum flashrom_wp_mode wp_modes[2][2] = {
382 {
383 FLASHROM_WP_MODE_DISABLED, /* srl=0, srp=0 */
384 FLASHROM_WP_MODE_HARDWARE, /* srl=0, srp=1 */
385 }, {
386 FLASHROM_WP_MODE_POWER_CYCLE, /* srl=1, srp=0 */
387 FLASHROM_WP_MODE_PERMANENT, /* srl=1, srp=1 */
388 },
389 };
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100390
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100391 *mode = wp_modes[bits->srl][bits->srp];
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100392
393 return FLASHROM_WP_OK;
394}
395
396/** Modify a wp_bits structure such that it will select a specified protection mode. */
397static int set_wp_mode(struct wp_bits *bits, const enum flashrom_wp_mode mode)
398{
399 switch (mode) {
400 case FLASHROM_WP_MODE_DISABLED:
401 bits->srl = 0;
402 bits->srp = 0;
403 return FLASHROM_WP_OK;
404
405 case FLASHROM_WP_MODE_HARDWARE:
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100406 if (!bits->srp_bit_present)
407 return FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
408
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100409 bits->srl = 0;
410 bits->srp = 1;
411 return FLASHROM_WP_OK;
412
413 case FLASHROM_WP_MODE_POWER_CYCLE:
414 case FLASHROM_WP_MODE_PERMANENT:
415 default:
416 /*
417 * Don't try to enable power cycle or permanent protection for
418 * now. Those modes may be possible to activate on some chips,
419 * but they are usually unavailable by default or require special
420 * commands to activate.
421 */
422 return FLASHROM_WP_ERR_MODE_UNSUPPORTED;
423 }
424}
425
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100426static bool chip_supported(struct flashctx *flash)
427{
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100428 return (flash->chip != NULL) && (flash->chip->decode_range != NULL);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100429}
430
431enum flashrom_wp_result wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash)
432{
433 struct wp_bits bits;
434 enum flashrom_wp_result ret = FLASHROM_WP_OK;
435
436 if (!chip_supported(flash))
437 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
438
439 if (ret == FLASHROM_WP_OK)
440 ret = read_wp_bits(&bits, flash);
441
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100442 if (ret == FLASHROM_WP_OK)
443 ret = get_wp_range(&cfg->range, flash, &bits);
444
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100445 if (ret == FLASHROM_WP_OK)
446 ret = get_wp_mode(&cfg->mode, &bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100447
448 return ret;
449}
450
451enum flashrom_wp_result wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg)
452{
453 struct wp_bits bits;
454 enum flashrom_wp_result ret = FLASHROM_WP_OK;
455
456 if (!chip_supported(flash))
457 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
458
459 if (ret == FLASHROM_WP_OK)
460 ret = read_wp_bits(&bits, flash);
461
462 /* Set protection range */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100463 if (ret == FLASHROM_WP_OK)
464 ret = set_wp_range(&bits, flash, cfg->range);
465 if (ret == FLASHROM_WP_OK)
466 ret = write_wp_bits(flash, bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100467
468 /* Set protection mode */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100469 if (ret == FLASHROM_WP_OK)
470 ret = set_wp_mode(&bits, cfg->mode);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100471 if (ret == FLASHROM_WP_OK)
472 ret = write_wp_bits(flash, bits);
473
474 return ret;
475}
476
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100477enum flashrom_wp_result wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash)
478{
479 struct wp_bits bits;
480 struct wp_range_and_bits *range_pairs = NULL;
481 size_t count;
482 size_t i;
483
484 if (!chip_supported(flash))
485 return FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
486
487 enum flashrom_wp_result ret = read_wp_bits(&bits, flash);
488 if (ret != FLASHROM_WP_OK)
489 return ret;
490
491 ret = get_ranges_and_wp_bits(flash, bits, &range_pairs, &count);
492 if (ret != FLASHROM_WP_OK)
493 return ret;
494
495 *list = calloc(1, sizeof(struct flashrom_wp_ranges));
496 struct wp_range *ranges = calloc(count, sizeof(struct wp_range));
497
498 if (!(*list) || !ranges) {
499 free(*list);
500 free(ranges);
501 ret = FLASHROM_WP_ERR_OTHER;
502 goto out;
503 }
504 (*list)->count = count;
505 (*list)->ranges = ranges;
506
507 for (i = 0; i < count; i++)
508 ranges[i] = range_pairs[i].range;
509
510out:
511 free(range_pairs);
512 return ret;
513}
514
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100515/** @} */ /* end flashrom-wp */