blob: 28d692d0c2cf92be685fe70f9c1f582e4de48926 [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
76 for (i = 0; i < ARRAY_SIZE(bits->bp); i++) {
77 if (bit_map->bp[i].reg == INVALID_REG)
78 break;
79
80 bits->bp_bit_count = i + 1;
81 ret = read_bit(&bits->bp[i], &ignored, flash, bit_map->bp[i]);
82 if (ret != FLASHROM_WP_OK)
83 return ret;
84 }
85
86 return ret;
87}
88
89/** Helper function for write_wp_bits(). */
90static void set_reg_bit(
91 uint8_t *reg_values, uint8_t *write_masks,
92 struct reg_bit_info bit, uint8_t value)
93{
94 if (bit.reg != INVALID_REG) {
95 reg_values[bit.reg] |= value << bit.bit_index;
96 write_masks[bit.reg] |= 1 << bit.bit_index;
97 }
98}
99
100/** Write WP configuration bits to the flash's registers. */
101static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits)
102{
103 size_t i;
104 enum flash_reg reg;
105 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
106
107 /* Convert wp_bits to register values and write masks */
108 uint8_t reg_values[MAX_REGISTERS] = {0};
109 uint8_t write_masks[MAX_REGISTERS] = {0};
110
111 for (i = 0; i < bits.bp_bit_count; i++)
112 set_reg_bit(reg_values, write_masks, reg_bits->bp[i], bits.bp[i]);
113
114 set_reg_bit(reg_values, write_masks, reg_bits->tb, bits.tb);
115 set_reg_bit(reg_values, write_masks, reg_bits->sec, bits.sec);
116 set_reg_bit(reg_values, write_masks, reg_bits->cmp, bits.cmp);
117 set_reg_bit(reg_values, write_masks, reg_bits->srp, bits.srp);
118 set_reg_bit(reg_values, write_masks, reg_bits->srl, bits.srl);
119
120 /* Write each register */
121 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
122 if (!write_masks[reg])
123 continue;
124
125 uint8_t value;
126 if (spi_read_register(flash, reg, &value))
127 return FLASHROM_WP_ERR_READ_FAILED;
128
129 value = (value & ~write_masks[reg]) | (reg_values[reg] & write_masks[reg]);
130
131 if (spi_write_register(flash, reg, value))
132 return FLASHROM_WP_ERR_WRITE_FAILED;
133 }
134
135 /* Verify each register */
136 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
137 if (!write_masks[reg])
138 continue;
139
140 uint8_t value;
141 if (spi_read_register(flash, reg, &value))
142 return FLASHROM_WP_ERR_READ_FAILED;
143
144 uint8_t actual = value & write_masks[reg];
145 uint8_t expected = reg_values[reg] & write_masks[reg];
146
147 if (actual != expected)
148 return FLASHROM_WP_ERR_VERIFY_FAILED;
149 }
150
151 return FLASHROM_WP_OK;
152}
153
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100154/** Get the range selected by a WP configuration. */
155static enum flashrom_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits)
156{
157 flash->chip->decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash));
158
159 return FLASHROM_WP_OK;
160}
161
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100162/** Write protect bit values and the range they will activate. */
163struct wp_range_and_bits {
164 struct wp_bits bits;
165 struct wp_range range;
166};
167
168/**
169 * Comparator used for sorting ranges in get_ranges_and_wp_bits().
170 *
171 * Ranges are ordered by these attributes, in decreasing significance:
172 * (range length, range start, cmp bit, sec bit, tb bit, bp bits)
173 */
174static int compare_ranges(const void *aa, const void *bb)
175{
176 const struct wp_range_and_bits
177 *a = (const struct wp_range_and_bits *)aa,
178 *b = (const struct wp_range_and_bits *)bb;
179 int i;
180
181 int ord = 0;
182
183 if (ord == 0)
184 ord = a->range.len - b->range.len;
185
186 if (ord == 0)
187 ord = a->range.start - b->range.start;
188
189 if (ord == 0)
190 ord = a->bits.cmp - b->bits.cmp;
191
192 if (ord == 0)
193 ord = a->bits.sec - b->bits.sec;
194
195 if (ord == 0)
196 ord = a->bits.tb - b->bits.tb;
197
198 for (i = a->bits.bp_bit_count - 1; i >= 0; i--) {
199 if (ord == 0)
200 ord = a->bits.bp[i] - b->bits.bp[i];
201 }
202
203 return ord;
204}
205
206static bool can_write_bit(const struct reg_bit_info bit)
207{
208 /*
209 * TODO: check if the programmer supports writing the register that the
210 * bit is in. For example, some chipsets may only allow SR1 to be
211 * written.
212 */
213
214 return bit.reg != INVALID_REG && bit.writability == RW;
215}
216
217/**
218 * Enumerate all protection ranges that the chip supports and that are able to
219 * be activated, given limitations such as OTP bits or programmer-enforced
220 * restrictions. Returns a list of deduplicated wp_range_and_bits structures.
221 *
222 * Allocates a buffer that must be freed by the caller with free().
223 */
224static 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)
225{
226 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
227 size_t i;
228 /*
229 * Create a list of bits that affect the chip's protection range in
230 * range_bits. Each element is a pointer to a member of the wp_bits
231 * structure that will be modified.
232 *
233 * Some chips have range bits that cannot be changed (e.g. MX25L6473E
234 * has a one-time programmable TB bit). Rather than enumerating all
235 * possible values for unwritable bits, just read their values from the
236 * chip to ensure we only enumerate ranges that are actually available.
237 */
238 uint8_t *range_bits[ARRAY_SIZE(bits.bp) + 1 /* TB */ + 1 /* SEC */ + 1 /* CMP */];
239 size_t bit_count = 0;
240
241 for (i = 0; i < ARRAY_SIZE(bits.bp); i++) {
242 if (can_write_bit(reg_bits->bp[i]))
243 range_bits[bit_count++] = &bits.bp[i];
244 }
245
246 if (can_write_bit(reg_bits->tb))
247 range_bits[bit_count++] = &bits.tb;
248
249 if (can_write_bit(reg_bits->sec))
250 range_bits[bit_count++] = &bits.sec;
251
252 if (can_write_bit(reg_bits->cmp))
253 range_bits[bit_count++] = &bits.cmp;
254
255 /* Allocate output buffer */
256 *count = 1 << bit_count;
257 *ranges = calloc(*count, sizeof(struct wp_range_and_bits));
258
259 size_t range_index;
260 for (range_index = 0; range_index < *count; range_index++) {
261 /*
262 * Extract bits from the range index and assign them to members
263 * of the wp_bits structure. The loop bounds ensure that all
264 * bit combinations will be enumerated.
265 */
266 for (i = 0; i < bit_count; i++)
267 *range_bits[i] = (range_index >> i) & 1;
268
269 struct wp_range_and_bits *output = &(*ranges)[range_index];
270
271 output->bits = bits;
272 enum flashrom_wp_result ret = get_wp_range(&output->range, flash, &bits);
273 if (ret != FLASHROM_WP_OK) {
274 free(*ranges);
275 return ret;
276 }
277
278 /* Debug: print range bits and range */
279 msg_gspew("Enumerated range: ");
280 if (bits.cmp_bit_present)
281 msg_gspew("CMP=%u ", bits.cmp);
282 if (bits.sec_bit_present)
283 msg_gspew("SEC=%u ", bits.sec);
284 if (bits.tb_bit_present)
285 msg_gspew("TB=%u ", bits.tb);
286 for (i = 0; i < bits.bp_bit_count; i++) {
287 size_t j = bits.bp_bit_count - i - 1;
288 msg_gspew("BP%zu=%u ", j, bits.bp[j]);
289 }
290 msg_gspew(" start=0x%08zx length=0x%08zx ",
291 output->range.start, output->range.len);
292 }
293
294 /* Sort ranges. Ensures consistency if there are duplicate ranges. */
295 qsort(*ranges, *count, sizeof(struct wp_range_and_bits), compare_ranges);
296
297 /* Remove duplicates */
298 size_t output_index = 0;
299 struct wp_range *last_range = NULL;
300
301 for (i = 0; i < *count; i++) {
302 bool different_to_last =
303 (last_range == NULL) ||
304 ((*ranges)[i].range.start != last_range->start) ||
305 ((*ranges)[i].range.len != last_range->len);
306
307 if (different_to_last) {
308 /* Move range to the next free position */
309 (*ranges)[output_index] = (*ranges)[i];
310 output_index++;
311 /* Keep track of last non-duplicate range */
312 last_range = &(*ranges)[i].range;
313 }
314 }
315 /* Reduce count to only include non-duplicate ranges */
316 *count = output_index;
317
318 return FLASHROM_WP_OK;
319}
320
Nikolai Artemievb6112a52021-10-21 02:28:23 +1100321static bool ranges_equal(struct wp_range a, struct wp_range b)
322{
323 return (a.start == b.start) && (a.len == b.len);
324}
325
326/*
327 * Modify the range-related bits in a wp_bits structure so they select a given
328 * protection range. Bits that control the protection mode are not changed.
329 */
330static int set_wp_range(struct wp_bits *bits, struct flashctx *flash, const struct wp_range range)
331{
332 struct wp_range_and_bits *ranges = NULL;
333 size_t count;
334 size_t i;
335
336 enum flashrom_wp_result ret = get_ranges_and_wp_bits(flash, *bits, &ranges, &count);
337 if (ret != FLASHROM_WP_OK)
338 return ret;
339
340 /* Search for matching range */
341 ret = FLASHROM_WP_ERR_RANGE_UNSUPPORTED;
342 for (i = 0; i < count; i++) {
343
344 if (ranges_equal(ranges[i].range, range)) {
345 *bits = ranges[i].bits;
346 ret = 0;
347 break;
348 }
349 }
350
351 free(ranges);
352
353 return ret;
354}
355
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100356static bool chip_supported(struct flashctx *flash)
357{
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100358 return (flash->chip != NULL) && (flash->chip->decode_range != NULL);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100359}
360
361enum flashrom_wp_result wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash)
362{
363 struct wp_bits bits;
364 enum flashrom_wp_result ret = FLASHROM_WP_OK;
365
366 if (!chip_supported(flash))
367 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
368
369 if (ret == FLASHROM_WP_OK)
370 ret = read_wp_bits(&bits, flash);
371
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100372 if (ret == FLASHROM_WP_OK)
373 ret = get_wp_range(&cfg->range, flash, &bits);
374
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100375 /* TODO: implement and get_wp_mode() and call it */
376 /*
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100377 if (ret == FLASHROM_WP_OK)
378 ret = get_wp_mode(&cfg->mode, &bits);
379 */
380
381 return ret;
382}
383
384enum flashrom_wp_result wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg)
385{
386 struct wp_bits bits;
387 enum flashrom_wp_result ret = FLASHROM_WP_OK;
388
389 if (!chip_supported(flash))
390 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
391
392 if (ret == FLASHROM_WP_OK)
393 ret = read_wp_bits(&bits, flash);
394
395 /* Set protection range */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100396 if (ret == FLASHROM_WP_OK)
397 ret = set_wp_range(&bits, flash, cfg->range);
398 if (ret == FLASHROM_WP_OK)
399 ret = write_wp_bits(flash, bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100400
401 /* Set protection mode */
402 /* TODO: implement set_wp_mode() and use it */
403 /*
404 if (ret == FLASHROM_WP_OK)
405 ret = set_wp_mode(&bits, cfg->mode);
406 */
407 if (ret == FLASHROM_WP_OK)
408 ret = write_wp_bits(flash, bits);
409
410 return ret;
411}
412
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100413enum flashrom_wp_result wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash)
414{
415 struct wp_bits bits;
416 struct wp_range_and_bits *range_pairs = NULL;
417 size_t count;
418 size_t i;
419
420 if (!chip_supported(flash))
421 return FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
422
423 enum flashrom_wp_result ret = read_wp_bits(&bits, flash);
424 if (ret != FLASHROM_WP_OK)
425 return ret;
426
427 ret = get_ranges_and_wp_bits(flash, bits, &range_pairs, &count);
428 if (ret != FLASHROM_WP_OK)
429 return ret;
430
431 *list = calloc(1, sizeof(struct flashrom_wp_ranges));
432 struct wp_range *ranges = calloc(count, sizeof(struct wp_range));
433
434 if (!(*list) || !ranges) {
435 free(*list);
436 free(ranges);
437 ret = FLASHROM_WP_ERR_OTHER;
438 goto out;
439 }
440 (*list)->count = count;
441 (*list)->ranges = ranges;
442
443 for (i = 0; i < count; i++)
444 ranges[i] = range_pairs[i].range;
445
446out:
447 free(range_pairs);
448 return ret;
449}
450
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100451/** @} */ /* end flashrom-wp */