blob: 11219e2617368390260254c4d9d6a17b545ad813 [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(
93 uint8_t *reg_values, uint8_t *write_masks,
94 struct reg_bit_info bit, uint8_t value)
95{
96 if (bit.reg != INVALID_REG) {
97 reg_values[bit.reg] |= value << bit.bit_index;
98 write_masks[bit.reg] |= 1 << bit.bit_index;
99 }
100}
101
102/** Write WP configuration bits to the flash's registers. */
103static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits)
104{
105 size_t i;
106 enum flash_reg reg;
107 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
108
109 /* Convert wp_bits to register values and write masks */
110 uint8_t reg_values[MAX_REGISTERS] = {0};
111 uint8_t write_masks[MAX_REGISTERS] = {0};
112
113 for (i = 0; i < bits.bp_bit_count; i++)
114 set_reg_bit(reg_values, write_masks, reg_bits->bp[i], bits.bp[i]);
115
116 set_reg_bit(reg_values, write_masks, reg_bits->tb, bits.tb);
117 set_reg_bit(reg_values, write_masks, reg_bits->sec, bits.sec);
118 set_reg_bit(reg_values, write_masks, reg_bits->cmp, bits.cmp);
119 set_reg_bit(reg_values, write_masks, reg_bits->srp, bits.srp);
120 set_reg_bit(reg_values, write_masks, reg_bits->srl, bits.srl);
Sergii Dmytruk801fcd02021-12-19 18:45:16 +0200121 /* Note: always setting WPS bit to zero until its fully supported. */
122 set_reg_bit(reg_values, write_masks, reg_bits->wps, 0);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100123
124 /* Write each register */
125 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
126 if (!write_masks[reg])
127 continue;
128
129 uint8_t value;
130 if (spi_read_register(flash, reg, &value))
131 return FLASHROM_WP_ERR_READ_FAILED;
132
133 value = (value & ~write_masks[reg]) | (reg_values[reg] & write_masks[reg]);
134
135 if (spi_write_register(flash, reg, value))
136 return FLASHROM_WP_ERR_WRITE_FAILED;
137 }
138
Evan Bennd81997c2022-09-13 17:12:59 +1000139 enum flashrom_wp_result ret = FLASHROM_WP_OK;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100140 /* Verify each register */
141 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
142 if (!write_masks[reg])
143 continue;
144
145 uint8_t value;
146 if (spi_read_register(flash, reg, &value))
147 return FLASHROM_WP_ERR_READ_FAILED;
148
Evan Bennd81997c2022-09-13 17:12:59 +1000149 msg_cdbg2("%s: wp_verify reg:%u value:0x%x\n", __func__, reg, value);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100150 uint8_t actual = value & write_masks[reg];
151 uint8_t expected = reg_values[reg] & write_masks[reg];
152
Evan Bennd81997c2022-09-13 17:12:59 +1000153 if (actual != expected) {
154 msg_cdbg("%s: wp_verify failed: reg:%u actual:0x%x expected:0x%x\n",
155 __func__, reg, actual, expected);
156 ret = FLASHROM_WP_ERR_VERIFY_FAILED;
157 }
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100158 }
159
Evan Bennd81997c2022-09-13 17:12:59 +1000160 return ret;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100161}
162
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100163/** Get the range selected by a WP configuration. */
164static enum flashrom_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits)
165{
166 flash->chip->decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash));
167
168 return FLASHROM_WP_OK;
169}
170
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100171/** Write protect bit values and the range they will activate. */
172struct wp_range_and_bits {
173 struct wp_bits bits;
174 struct wp_range range;
175};
176
177/**
178 * Comparator used for sorting ranges in get_ranges_and_wp_bits().
179 *
180 * Ranges are ordered by these attributes, in decreasing significance:
181 * (range length, range start, cmp bit, sec bit, tb bit, bp bits)
182 */
183static int compare_ranges(const void *aa, const void *bb)
184{
185 const struct wp_range_and_bits
186 *a = (const struct wp_range_and_bits *)aa,
187 *b = (const struct wp_range_and_bits *)bb;
188 int i;
189
190 int ord = 0;
191
192 if (ord == 0)
193 ord = a->range.len - b->range.len;
194
195 if (ord == 0)
196 ord = a->range.start - b->range.start;
197
198 if (ord == 0)
199 ord = a->bits.cmp - b->bits.cmp;
200
201 if (ord == 0)
202 ord = a->bits.sec - b->bits.sec;
203
204 if (ord == 0)
205 ord = a->bits.tb - b->bits.tb;
206
207 for (i = a->bits.bp_bit_count - 1; i >= 0; i--) {
208 if (ord == 0)
209 ord = a->bits.bp[i] - b->bits.bp[i];
210 }
211
212 return ord;
213}
214
215static bool can_write_bit(const struct reg_bit_info bit)
216{
217 /*
218 * TODO: check if the programmer supports writing the register that the
219 * bit is in. For example, some chipsets may only allow SR1 to be
220 * written.
221 */
222
223 return bit.reg != INVALID_REG && bit.writability == RW;
224}
225
226/**
227 * Enumerate all protection ranges that the chip supports and that are able to
228 * be activated, given limitations such as OTP bits or programmer-enforced
229 * restrictions. Returns a list of deduplicated wp_range_and_bits structures.
230 *
231 * Allocates a buffer that must be freed by the caller with free().
232 */
233static 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)
234{
235 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
236 size_t i;
237 /*
238 * Create a list of bits that affect the chip's protection range in
239 * range_bits. Each element is a pointer to a member of the wp_bits
240 * structure that will be modified.
241 *
242 * Some chips have range bits that cannot be changed (e.g. MX25L6473E
243 * has a one-time programmable TB bit). Rather than enumerating all
244 * possible values for unwritable bits, just read their values from the
245 * chip to ensure we only enumerate ranges that are actually available.
246 */
247 uint8_t *range_bits[ARRAY_SIZE(bits.bp) + 1 /* TB */ + 1 /* SEC */ + 1 /* CMP */];
248 size_t bit_count = 0;
249
250 for (i = 0; i < ARRAY_SIZE(bits.bp); i++) {
251 if (can_write_bit(reg_bits->bp[i]))
252 range_bits[bit_count++] = &bits.bp[i];
253 }
254
255 if (can_write_bit(reg_bits->tb))
256 range_bits[bit_count++] = &bits.tb;
257
258 if (can_write_bit(reg_bits->sec))
259 range_bits[bit_count++] = &bits.sec;
260
261 if (can_write_bit(reg_bits->cmp))
262 range_bits[bit_count++] = &bits.cmp;
263
264 /* Allocate output buffer */
265 *count = 1 << bit_count;
266 *ranges = calloc(*count, sizeof(struct wp_range_and_bits));
267
Sergii Dmytruk801fcd02021-12-19 18:45:16 +0200268 /* TODO: take WPS bit into account. */
269
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100270 size_t range_index;
271 for (range_index = 0; range_index < *count; range_index++) {
272 /*
273 * Extract bits from the range index and assign them to members
274 * of the wp_bits structure. The loop bounds ensure that all
275 * bit combinations will be enumerated.
276 */
277 for (i = 0; i < bit_count; i++)
278 *range_bits[i] = (range_index >> i) & 1;
279
280 struct wp_range_and_bits *output = &(*ranges)[range_index];
281
282 output->bits = bits;
283 enum flashrom_wp_result ret = get_wp_range(&output->range, flash, &bits);
284 if (ret != FLASHROM_WP_OK) {
285 free(*ranges);
286 return ret;
287 }
288
289 /* Debug: print range bits and range */
290 msg_gspew("Enumerated range: ");
291 if (bits.cmp_bit_present)
292 msg_gspew("CMP=%u ", bits.cmp);
293 if (bits.sec_bit_present)
294 msg_gspew("SEC=%u ", bits.sec);
295 if (bits.tb_bit_present)
296 msg_gspew("TB=%u ", bits.tb);
297 for (i = 0; i < bits.bp_bit_count; i++) {
298 size_t j = bits.bp_bit_count - i - 1;
299 msg_gspew("BP%zu=%u ", j, bits.bp[j]);
300 }
Nico Huber1e6aabc2022-05-28 16:39:07 +0200301 msg_gspew(" start=0x%08zx length=0x%08zx\n",
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100302 output->range.start, output->range.len);
303 }
304
305 /* Sort ranges. Ensures consistency if there are duplicate ranges. */
306 qsort(*ranges, *count, sizeof(struct wp_range_and_bits), compare_ranges);
307
308 /* Remove duplicates */
309 size_t output_index = 0;
310 struct wp_range *last_range = NULL;
311
312 for (i = 0; i < *count; i++) {
313 bool different_to_last =
314 (last_range == NULL) ||
315 ((*ranges)[i].range.start != last_range->start) ||
316 ((*ranges)[i].range.len != last_range->len);
317
318 if (different_to_last) {
319 /* Move range to the next free position */
320 (*ranges)[output_index] = (*ranges)[i];
321 output_index++;
322 /* Keep track of last non-duplicate range */
323 last_range = &(*ranges)[i].range;
324 }
325 }
326 /* Reduce count to only include non-duplicate ranges */
327 *count = output_index;
328
329 return FLASHROM_WP_OK;
330}
331
Nikolai Artemievb6112a52021-10-21 02:28:23 +1100332static bool ranges_equal(struct wp_range a, struct wp_range b)
333{
334 return (a.start == b.start) && (a.len == b.len);
335}
336
337/*
338 * Modify the range-related bits in a wp_bits structure so they select a given
339 * protection range. Bits that control the protection mode are not changed.
340 */
341static int set_wp_range(struct wp_bits *bits, struct flashctx *flash, const struct wp_range range)
342{
343 struct wp_range_and_bits *ranges = NULL;
344 size_t count;
345 size_t i;
346
347 enum flashrom_wp_result ret = get_ranges_and_wp_bits(flash, *bits, &ranges, &count);
348 if (ret != FLASHROM_WP_OK)
349 return ret;
350
351 /* Search for matching range */
352 ret = FLASHROM_WP_ERR_RANGE_UNSUPPORTED;
353 for (i = 0; i < count; i++) {
354
355 if (ranges_equal(ranges[i].range, range)) {
356 *bits = ranges[i].bits;
357 ret = 0;
358 break;
359 }
360 }
361
362 free(ranges);
363
364 return ret;
365}
366
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100367/** Get the mode selected by a WP configuration. */
368static int get_wp_mode(enum flashrom_wp_mode *mode, const struct wp_bits *bits)
369{
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100370 const enum flashrom_wp_mode wp_modes[2][2] = {
371 {
372 FLASHROM_WP_MODE_DISABLED, /* srl=0, srp=0 */
373 FLASHROM_WP_MODE_HARDWARE, /* srl=0, srp=1 */
374 }, {
375 FLASHROM_WP_MODE_POWER_CYCLE, /* srl=1, srp=0 */
376 FLASHROM_WP_MODE_PERMANENT, /* srl=1, srp=1 */
377 },
378 };
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100379
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100380 *mode = wp_modes[bits->srl][bits->srp];
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100381
382 return FLASHROM_WP_OK;
383}
384
385/** Modify a wp_bits structure such that it will select a specified protection mode. */
386static int set_wp_mode(struct wp_bits *bits, const enum flashrom_wp_mode mode)
387{
388 switch (mode) {
389 case FLASHROM_WP_MODE_DISABLED:
390 bits->srl = 0;
391 bits->srp = 0;
392 return FLASHROM_WP_OK;
393
394 case FLASHROM_WP_MODE_HARDWARE:
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100395 if (!bits->srp_bit_present)
396 return FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
397
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100398 bits->srl = 0;
399 bits->srp = 1;
400 return FLASHROM_WP_OK;
401
402 case FLASHROM_WP_MODE_POWER_CYCLE:
403 case FLASHROM_WP_MODE_PERMANENT:
404 default:
405 /*
406 * Don't try to enable power cycle or permanent protection for
407 * now. Those modes may be possible to activate on some chips,
408 * but they are usually unavailable by default or require special
409 * commands to activate.
410 */
411 return FLASHROM_WP_ERR_MODE_UNSUPPORTED;
412 }
413}
414
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100415static bool chip_supported(struct flashctx *flash)
416{
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100417 return (flash->chip != NULL) && (flash->chip->decode_range != NULL);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100418}
419
420enum flashrom_wp_result wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash)
421{
422 struct wp_bits bits;
423 enum flashrom_wp_result ret = FLASHROM_WP_OK;
424
425 if (!chip_supported(flash))
426 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
427
428 if (ret == FLASHROM_WP_OK)
429 ret = read_wp_bits(&bits, flash);
430
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100431 if (ret == FLASHROM_WP_OK)
432 ret = get_wp_range(&cfg->range, flash, &bits);
433
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100434 if (ret == FLASHROM_WP_OK)
435 ret = get_wp_mode(&cfg->mode, &bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100436
437 return ret;
438}
439
440enum flashrom_wp_result wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg)
441{
442 struct wp_bits bits;
443 enum flashrom_wp_result ret = FLASHROM_WP_OK;
444
445 if (!chip_supported(flash))
446 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
447
448 if (ret == FLASHROM_WP_OK)
449 ret = read_wp_bits(&bits, flash);
450
451 /* Set protection range */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100452 if (ret == FLASHROM_WP_OK)
453 ret = set_wp_range(&bits, flash, cfg->range);
454 if (ret == FLASHROM_WP_OK)
455 ret = write_wp_bits(flash, bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100456
457 /* Set protection mode */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100458 if (ret == FLASHROM_WP_OK)
459 ret = set_wp_mode(&bits, cfg->mode);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100460 if (ret == FLASHROM_WP_OK)
461 ret = write_wp_bits(flash, bits);
462
463 return ret;
464}
465
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100466enum flashrom_wp_result wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash)
467{
468 struct wp_bits bits;
469 struct wp_range_and_bits *range_pairs = NULL;
470 size_t count;
471 size_t i;
472
473 if (!chip_supported(flash))
474 return FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
475
476 enum flashrom_wp_result ret = read_wp_bits(&bits, flash);
477 if (ret != FLASHROM_WP_OK)
478 return ret;
479
480 ret = get_ranges_and_wp_bits(flash, bits, &range_pairs, &count);
481 if (ret != FLASHROM_WP_OK)
482 return ret;
483
484 *list = calloc(1, sizeof(struct flashrom_wp_ranges));
485 struct wp_range *ranges = calloc(count, sizeof(struct wp_range));
486
487 if (!(*list) || !ranges) {
488 free(*list);
489 free(ranges);
490 ret = FLASHROM_WP_ERR_OTHER;
491 goto out;
492 }
493 (*list)->count = count;
494 (*list)->ranges = ranges;
495
496 for (i = 0; i < count; i++)
497 ranges[i] = range_pairs[i].range;
498
499out:
500 free(range_pairs);
501 return ret;
502}
503
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100504/** @} */ /* end flashrom-wp */