1 /*
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 "spi.h"
23 #include "flash.h"
24 #include "libflashrom.h"
25 #include "chipdrivers.h"
26 #include "writeprotect.h"
27 #include "programmer.h"
28
29 /*
30 * Allow specialisation in opaque masters, such as ichspi hwseq, to r/w to status registers.
31 */
wp_write_register(const struct flashctx * flash,enum flash_reg reg,uint8_t value)32 static int wp_write_register(const struct flashctx *flash, enum flash_reg reg, uint8_t value)
33 {
34 int ret;
35 if ((flash->mst->buses_supported & BUS_PROG) && flash->mst->opaque.write_register) {
36 ret = flash->mst->opaque.write_register(flash, reg, value);
37 } else {
38 ret = spi_write_register(flash, reg, value);
39 }
40
41 /* Writing SR1 should always be supported, ignore errors for other registers. */
42 if (ret == SPI_INVALID_OPCODE && reg != STATUS1) {
43 msg_pdbg("%s: write to register %d not supported by programmer, ignoring.\n", __func__, reg);
44 ret = 0;
45 }
46 return ret;
47 }
48
wp_read_register(const struct flashctx * flash,enum flash_reg reg,uint8_t * value)49 static int wp_read_register(const struct flashctx *flash, enum flash_reg reg, uint8_t *value)
50 {
51 int ret;
52 if ((flash->mst->buses_supported & BUS_PROG) && flash->mst->opaque.read_register) {
53 ret = flash->mst->opaque.read_register(flash, reg, value);
54 } else {
55 ret = spi_read_register(flash, reg, value);
56 }
57
58 /* Reading SR1 should always be supported, ignore errors for other registers. */
59 if (ret == SPI_INVALID_OPCODE && reg != STATUS1) {
60 msg_pdbg("%s: read from register %d not is supported by programmer, "
61 "writeprotect operations will assume it contains 0x00.\n", __func__, reg);
62 *value = 0;
63 ret = 0;
64 }
65 return ret;
66 }
67
68 /** Read and extract a single bit from the chip's registers */
read_bit(uint8_t * value,bool * present,struct flashctx * flash,struct reg_bit_info bit)69 static enum flashrom_wp_result read_bit(uint8_t *value, bool *present, struct flashctx *flash, struct reg_bit_info bit)
70 {
71 *present = bit.reg != INVALID_REG;
72 if (*present) {
73 if (wp_read_register(flash, bit.reg, value))
74 return FLASHROM_WP_ERR_READ_FAILED;
75 *value = (*value >> bit.bit_index) & 1;
76 } else {
77 /* Zero bit, it may be used by compare_ranges(). */
78 *value = 0;
79 }
80
81 return FLASHROM_WP_OK;
82 }
83
84 /** Read all WP configuration bits from the chip's registers. */
read_wp_bits(struct wp_bits * bits,struct flashctx * flash)85 static enum flashrom_wp_result read_wp_bits(struct wp_bits *bits, struct flashctx *flash)
86 {
87 /*
88 * For each WP bit that is included in the chip's register layout, read
89 * the register that contains it, extracts the bit's value, and assign
90 * it to the appropriate field in the wp_bits structure.
91 */
92 const struct reg_bit_map *bit_map = &flash->chip->reg_bits;
93 bool ignored;
94 size_t i;
95 enum flashrom_wp_result ret;
96
97 /*
98 * Write protection select bit (WPS) controls kind of write protection
99 * that is used by the chip. When set, BP bits are ignored and each
100 * block/sector has its own WP bit managed by special commands. When
101 * the bit is set and we can't change it, just bail out until
102 * implementation is extended to handle this kind of WP.
103 */
104 if (bit_map->wps.reg != INVALID_REG && bit_map->wps.writability != RW) {
105 bool wps_bit_present;
106 uint8_t wps;
107
108 ret = read_bit(&wps, &wps_bit_present, flash, bit_map->wps);
109 if (ret != FLASHROM_WP_OK)
110 return ret;
111
112 if (wps_bit_present && wps)
113 return FLASHROM_WP_ERR_UNSUPPORTED_STATE;
114 }
115
116 ret = read_bit(&bits->tb, &bits->tb_bit_present, flash, bit_map->tb);
117 if (ret != FLASHROM_WP_OK)
118 return ret;
119
120 ret = read_bit(&bits->sec, &bits->sec_bit_present, flash, bit_map->sec);
121 if (ret != FLASHROM_WP_OK)
122 return ret;
123
124 ret = read_bit(&bits->cmp, &bits->cmp_bit_present, flash, bit_map->cmp);
125 if (ret != FLASHROM_WP_OK)
126 return ret;
127
128 ret = read_bit(&bits->srp, &bits->srp_bit_present, flash, bit_map->srp);
129 if (ret != FLASHROM_WP_OK)
130 return ret;
131
132 ret = read_bit(&bits->srl, &bits->srl_bit_present, flash, bit_map->srl);
133 if (ret != FLASHROM_WP_OK)
134 return ret;
135
136 for (i = 0; i < ARRAY_SIZE(bits->bp); i++) {
137 if (bit_map->bp[i].reg == INVALID_REG)
138 break;
139
140 bits->bp_bit_count = i + 1;
141 ret = read_bit(&bits->bp[i], &ignored, flash, bit_map->bp[i]);
142 if (ret != FLASHROM_WP_OK)
143 return ret;
144 }
145
146 return ret;
147 }
148
149 /** Helper function for get_wp_bits_reg_values(). */
set_reg_bit(uint8_t * reg_values,uint8_t * bit_masks,uint8_t * write_masks,struct reg_bit_info bit,uint8_t value)150 static void set_reg_bit(
151 uint8_t *reg_values, uint8_t *bit_masks, uint8_t *write_masks,
152 struct reg_bit_info bit, uint8_t value)
153 {
154 if (bit.reg != INVALID_REG) {
155 reg_values[bit.reg] |= value << bit.bit_index;
156 bit_masks[bit.reg] |= 1 << bit.bit_index;
157
158 /* Avoid RO and OTP bits causing a register update */
159 if (bit.writability == RW)
160 write_masks[bit.reg] |= 1 << bit.bit_index;
161 }
162 }
163
164 /** Convert wp_bits to register values and write masks */
get_wp_bits_reg_values(uint8_t * reg_values,uint8_t * bit_masks,uint8_t * write_masks,const struct reg_bit_map * reg_bits,struct wp_bits bits)165 static void get_wp_bits_reg_values(
166 uint8_t *reg_values, uint8_t *bit_masks, uint8_t *write_masks,
167 const struct reg_bit_map *reg_bits, struct wp_bits bits)
168 {
169 memset(reg_values, 0, sizeof(uint8_t) * MAX_REGISTERS);
170 memset(bit_masks, 0, sizeof(uint8_t) * MAX_REGISTERS);
171 memset(write_masks, 0, sizeof(uint8_t) * MAX_REGISTERS);
172
173 for (size_t i = 0; i < bits.bp_bit_count; i++)
174 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->bp[i], bits.bp[i]);
175
176 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->tb, bits.tb);
177 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->sec, bits.sec);
178 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->cmp, bits.cmp);
179 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->srp, bits.srp);
180 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->srl, bits.srl);
181 /* Note: always setting WPS bit to zero until its fully supported. */
182 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->wps, 0);
183 }
184
185 /** Write WP configuration bits to the flash's registers. */
write_wp_bits(struct flashctx * flash,struct wp_bits bits)186 static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits)
187 {
188 uint8_t reg_values[MAX_REGISTERS];
189 uint8_t bit_masks[MAX_REGISTERS]; /* masks of valid bits */
190 uint8_t write_masks[MAX_REGISTERS]; /* masks of written bits */
191 get_wp_bits_reg_values(reg_values, bit_masks, write_masks, &flash->chip->reg_bits, bits);
192
193 /* Write each register whose value was updated */
194 for (enum flash_reg reg = STATUS1; reg < MAX_REGISTERS; reg++) {
195 if (!write_masks[reg])
196 continue;
197
198 uint8_t value;
199 if (wp_read_register(flash, reg, &value))
200 return FLASHROM_WP_ERR_READ_FAILED;
201
202 /* Skip unnecessary register writes */
203 uint8_t actual = value & write_masks[reg];
204 uint8_t expected = reg_values[reg] & write_masks[reg];
205 if (actual == expected)
206 continue;
207
208 value = (value & ~write_masks[reg]) | expected;
209
210 if (wp_write_register(flash, reg, value))
211 return FLASHROM_WP_ERR_WRITE_FAILED;
212 }
213
214 enum flashrom_wp_result ret = FLASHROM_WP_OK;
215 /* Verify each register even if write to it was skipped */
216 for (enum flash_reg reg = STATUS1; reg < MAX_REGISTERS; reg++) {
217 if (!bit_masks[reg])
218 continue;
219
220 uint8_t value;
221 if (wp_read_register(flash, reg, &value))
222 return FLASHROM_WP_ERR_READ_FAILED;
223
224 msg_cdbg2("%s: wp_verify reg:%u value:0x%x\n", __func__, reg, value);
225 uint8_t actual = value & bit_masks[reg];
226 uint8_t expected = reg_values[reg] & bit_masks[reg];
227
228 if (actual != expected) {
229 msg_cdbg("%s: wp_verify failed: reg:%u actual:0x%x expected:0x%x\n",
230 __func__, reg, actual, expected);
231 ret = FLASHROM_WP_ERR_VERIFY_FAILED;
232 }
233 }
234
235 return ret;
236 }
237
lookup_decode_range_func_ptr(const struct flashchip * chip)238 static decode_range_func_t *lookup_decode_range_func_ptr(const struct flashchip *chip)
239 {
240 switch (chip->decode_range) {
241 case DECODE_RANGE_SPI25: return &decode_range_spi25;
242 case DECODE_RANGE_SPI25_64K_BLOCK: return &decode_range_spi25_64k_block;
243 case DECODE_RANGE_SPI25_BIT_CMP: return &decode_range_spi25_bit_cmp;
244 case DECODE_RANGE_SPI25_2X_BLOCK: return &decode_range_spi25_2x_block;
245 /* default: total function, 0 indicates no decode range function set. */
246 case NO_DECODE_RANGE_FUNC: return NULL;
247 };
248
249 return NULL;
250 }
251
252
253 /** Get the range selected by a WP configuration. */
get_wp_range(struct wp_range * range,struct flashctx * flash,const struct wp_bits * bits)254 static enum flashrom_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits)
255 {
256 decode_range_func_t *decode_range = lookup_decode_range_func_ptr(flash->chip);
257 if (decode_range == NULL)
258 return FLASHROM_WP_ERR_OTHER;
259
260 decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash));
261 return FLASHROM_WP_OK;
262 }
263
264 /** Write protect bit values and the range they will activate. */
265 struct wp_range_and_bits {
266 struct wp_bits bits;
267 struct wp_range range;
268 };
269
270 /**
271 * Comparator used for sorting ranges in get_ranges_and_wp_bits().
272 *
273 * Ranges are ordered by these attributes, in decreasing significance:
274 * (range length, range start, cmp bit, sec bit, tb bit, bp bits)
275 */
compare_ranges(const void * aa,const void * bb)276 static int compare_ranges(const void *aa, const void *bb)
277 {
278 const struct wp_range_and_bits
279 *a = (const struct wp_range_and_bits *)aa,
280 *b = (const struct wp_range_and_bits *)bb;
281
282 int ord = 0;
283
284 if (ord == 0)
285 ord = a->range.len - b->range.len;
286
287 if (ord == 0)
288 ord = a->range.start - b->range.start;
289
290 if (ord == 0)
291 ord = a->bits.cmp - b->bits.cmp;
292
293 if (ord == 0)
294 ord = a->bits.sec - b->bits.sec;
295
296 if (ord == 0)
297 ord = a->bits.tb - b->bits.tb;
298
299 for (int i = a->bits.bp_bit_count - 1; i >= 0; i--) {
300 if (ord == 0)
301 ord = a->bits.bp[i] - b->bits.bp[i];
302 }
303
304 return ord;
305 }
306
can_write_bit(const struct reg_bit_info bit)307 static bool can_write_bit(const struct reg_bit_info bit)
308 {
309 /*
310 * TODO: check if the programmer supports writing the register that the
311 * bit is in. For example, some chipsets may only allow SR1 to be
312 * written.
313 */
314
315 return bit.reg != INVALID_REG && bit.writability == RW;
316 }
317
318 /**
319 * Enumerate all protection ranges that the chip supports and that are able to
320 * be activated, given limitations such as OTP bits or programmer-enforced
321 * restrictions. Returns a list of deduplicated wp_range_and_bits structures.
322 *
323 * Allocates a buffer that must be freed by the caller with free().
324 */
get_ranges_and_wp_bits(struct flashctx * flash,struct wp_bits bits,struct wp_range_and_bits ** ranges,size_t * count)325 static 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)
326 {
327 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
328 /*
329 * Create a list of bits that affect the chip's protection range in
330 * range_bits. Each element is a pointer to a member of the wp_bits
331 * structure that will be modified.
332 *
333 * Some chips have range bits that cannot be changed (e.g. MX25L6473E
334 * has a one-time programmable TB bit). Rather than enumerating all
335 * possible values for unwritable bits, just read their values from the
336 * chip to ensure we only enumerate ranges that are actually available.
337 */
338 uint8_t *range_bits[ARRAY_SIZE(bits.bp) + 1 /* TB */ + 1 /* SEC */ + 1 /* CMP */];
339 size_t bit_count = 0;
340
341 for (size_t i = 0; i < ARRAY_SIZE(bits.bp); i++) {
342 if (can_write_bit(reg_bits->bp[i]))
343 range_bits[bit_count++] = &bits.bp[i];
344 }
345
346 if (can_write_bit(reg_bits->tb))
347 range_bits[bit_count++] = &bits.tb;
348
349 if (can_write_bit(reg_bits->sec))
350 range_bits[bit_count++] = &bits.sec;
351
352 if (can_write_bit(reg_bits->cmp))
353 range_bits[bit_count++] = &bits.cmp;
354
355 /* Allocate output buffer */
356 *count = 1 << bit_count;
357 *ranges = calloc(*count, sizeof(struct wp_range_and_bits));
358
359 /* TODO: take WPS bit into account. */
360
361 for (size_t range_index = 0; range_index < *count; range_index++) {
362 /*
363 * Extract bits from the range index and assign them to members
364 * of the wp_bits structure. The loop bounds ensure that all
365 * bit combinations will be enumerated.
366 */
367 for (size_t i = 0; i < bit_count; i++)
368 *range_bits[i] = (range_index >> i) & 1;
369
370 struct wp_range_and_bits *output = &(*ranges)[range_index];
371
372 output->bits = bits;
373 enum flashrom_wp_result ret = get_wp_range(&output->range, flash, &bits);
374 if (ret != FLASHROM_WP_OK) {
375 free(*ranges);
376 return ret;
377 }
378
379 /* Debug: print range bits and range */
380 msg_gspew("Enumerated range: ");
381 if (bits.cmp_bit_present)
382 msg_gspew("CMP=%u ", bits.cmp);
383 if (bits.sec_bit_present)
384 msg_gspew("SEC=%u ", bits.sec);
385 if (bits.tb_bit_present)
386 msg_gspew("TB=%u ", bits.tb);
387 for (size_t i = 0; i < bits.bp_bit_count; i++) {
388 size_t j = bits.bp_bit_count - i - 1;
389 msg_gspew("BP%zu=%u ", j, bits.bp[j]);
390 }
391 msg_gspew(" start=0x%08zx length=0x%08zx\n",
392 output->range.start, output->range.len);
393 }
394
395 /* Sort ranges. Ensures consistency if there are duplicate ranges. */
396 qsort(*ranges, *count, sizeof(struct wp_range_and_bits), compare_ranges);
397
398 /* Remove duplicates */
399 size_t output_index = 0;
400 struct wp_range *last_range = NULL;
401
402 for (size_t i = 0; i < *count; i++) {
403 bool different_to_last =
404 (last_range == NULL) ||
405 ((*ranges)[i].range.start != last_range->start) ||
406 ((*ranges)[i].range.len != last_range->len);
407
408 if (different_to_last) {
409 /* Move range to the next free position */
410 (*ranges)[output_index] = (*ranges)[i];
411 output_index++;
412 /* Keep track of last non-duplicate range */
413 last_range = &(*ranges)[i].range;
414 }
415 }
416 /* Reduce count to only include non-duplicate ranges */
417 *count = output_index;
418
419 return FLASHROM_WP_OK;
420 }
421
ranges_equal(struct wp_range a,struct wp_range b)422 static bool ranges_equal(struct wp_range a, struct wp_range b)
423 {
424 return (a.start == b.start) && (a.len == b.len);
425 }
426
427 /*
428 * Modify the range-related bits in a wp_bits structure so they select a given
429 * protection range. Bits that control the protection mode are not changed.
430 */
set_wp_range(struct wp_bits * bits,struct flashctx * flash,const struct wp_range range)431 static int set_wp_range(struct wp_bits *bits, struct flashctx *flash, const struct wp_range range)
432 {
433 struct wp_range_and_bits *ranges = NULL;
434 size_t count;
435
436 enum flashrom_wp_result ret = get_ranges_and_wp_bits(flash, *bits, &ranges, &count);
437 if (ret != FLASHROM_WP_OK)
438 return ret;
439
440 /* Search for matching range */
441 ret = FLASHROM_WP_ERR_RANGE_UNSUPPORTED;
442 for (size_t i = 0; i < count; i++) {
443
444 if (ranges_equal(ranges[i].range, range)) {
445 *bits = ranges[i].bits;
446 ret = 0;
447 break;
448 }
449 }
450
451 free(ranges);
452
453 return ret;
454 }
455
456 /** Get the mode selected by a WP configuration. */
get_wp_mode(enum flashrom_wp_mode * mode,const struct wp_bits * bits)457 static int get_wp_mode(enum flashrom_wp_mode *mode, const struct wp_bits *bits)
458 {
459 const enum flashrom_wp_mode wp_modes[2][2] = {
460 {
461 FLASHROM_WP_MODE_DISABLED, /* srl=0, srp=0 */
462 FLASHROM_WP_MODE_HARDWARE, /* srl=0, srp=1 */
463 }, {
464 FLASHROM_WP_MODE_POWER_CYCLE, /* srl=1, srp=0 */
465 FLASHROM_WP_MODE_PERMANENT, /* srl=1, srp=1 */
466 },
467 };
468
469 *mode = wp_modes[bits->srl][bits->srp];
470
471 return FLASHROM_WP_OK;
472 }
473
474 /** Modify a wp_bits structure such that it will select a specified protection mode. */
set_wp_mode(struct wp_bits * bits,const enum flashrom_wp_mode mode)475 static int set_wp_mode(struct wp_bits *bits, const enum flashrom_wp_mode mode)
476 {
477 switch (mode) {
478 case FLASHROM_WP_MODE_DISABLED:
479 bits->srl = 0;
480 bits->srp = 0;
481 return FLASHROM_WP_OK;
482
483 case FLASHROM_WP_MODE_HARDWARE:
484 if (!bits->srp_bit_present)
485 return FLASHROM_WP_ERR_MODE_UNSUPPORTED;
486
487 bits->srl = 0;
488 bits->srp = 1;
489 return FLASHROM_WP_OK;
490
491 case FLASHROM_WP_MODE_POWER_CYCLE:
492 case FLASHROM_WP_MODE_PERMANENT:
493 default:
494 /*
495 * Don't try to enable power cycle or permanent protection for
496 * now. Those modes may be possible to activate on some chips,
497 * but they are usually unavailable by default or require special
498 * commands to activate.
499 */
500 return FLASHROM_WP_ERR_MODE_UNSUPPORTED;
501 }
502 }
503
chip_supported(struct flashctx * flash)504 static bool chip_supported(struct flashctx *flash)
505 {
506 return (flash->chip != NULL) && (flash->chip->decode_range != NO_DECODE_RANGE_FUNC);
507 }
508
509
wp_operations_available(struct flashrom_flashctx * flash)510 bool wp_operations_available(struct flashrom_flashctx *flash)
511 {
512 return (flash->mst->buses_supported & BUS_SPI) ||
513 ((flash->mst->buses_supported & BUS_PROG) &&
514 flash->mst->opaque.read_register &&
515 flash->mst->opaque.write_register);
516 }
517
wp_read_cfg(struct flashrom_wp_cfg * cfg,struct flashctx * flash)518 enum flashrom_wp_result wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash)
519 {
520 struct wp_bits bits;
521 enum flashrom_wp_result ret = FLASHROM_WP_OK;
522
523 if (!chip_supported(flash))
524 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
525
526 if (ret == FLASHROM_WP_OK)
527 ret = read_wp_bits(&bits, flash);
528
529 if (ret == FLASHROM_WP_OK)
530 ret = get_wp_range(&cfg->range, flash, &bits);
531
532 if (ret == FLASHROM_WP_OK)
533 ret = get_wp_mode(&cfg->mode, &bits);
534
535 return ret;
536 }
537
wp_write_cfg(struct flashctx * flash,const struct flashrom_wp_cfg * cfg)538 enum flashrom_wp_result wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg)
539 {
540 struct wp_bits bits;
541 enum flashrom_wp_result ret = FLASHROM_WP_OK;
542
543 if (!chip_supported(flash))
544 ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
545
546 if (ret == FLASHROM_WP_OK)
547 ret = read_wp_bits(&bits, flash);
548
549 /* Set protection range */
550 if (ret == FLASHROM_WP_OK)
551 ret = set_wp_range(&bits, flash, cfg->range);
552 if (ret == FLASHROM_WP_OK)
553 ret = write_wp_bits(flash, bits);
554
555 /* Set protection mode */
556 if (ret == FLASHROM_WP_OK)
557 ret = set_wp_mode(&bits, cfg->mode);
558 if (ret == FLASHROM_WP_OK)
559 ret = write_wp_bits(flash, bits);
560
561 return ret;
562 }
563
wp_get_available_ranges(struct flashrom_wp_ranges ** list,struct flashrom_flashctx * flash)564 enum flashrom_wp_result wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash)
565 {
566 struct wp_bits bits;
567 struct wp_range_and_bits *range_pairs = NULL;
568 size_t count;
569
570 if (!chip_supported(flash))
571 return FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
572
573 enum flashrom_wp_result ret = read_wp_bits(&bits, flash);
574 if (ret != FLASHROM_WP_OK)
575 return ret;
576
577 ret = get_ranges_and_wp_bits(flash, bits, &range_pairs, &count);
578 if (ret != FLASHROM_WP_OK)
579 return ret;
580
581 *list = calloc(1, sizeof(struct flashrom_wp_ranges));
582 struct wp_range *ranges = calloc(count, sizeof(struct wp_range));
583
584 if (!(*list) || !ranges) {
585 free(*list);
586 free(ranges);
587 ret = FLASHROM_WP_ERR_OTHER;
588 goto out;
589 }
590 (*list)->count = count;
591 (*list)->ranges = ranges;
592
593 for (size_t i = 0; i < count; i++)
594 ranges[i] = range_pairs[i].range;
595
596 out:
597 free(range_pairs);
598 return ret;
599 }
600
wp_cfg_to_reg_values(uint8_t * reg_values,uint8_t * bit_masks,uint8_t * write_masks,struct flashctx * flash,const struct flashrom_wp_cfg * cfg)601 enum flashrom_wp_result wp_cfg_to_reg_values(
602 uint8_t *reg_values, uint8_t *bit_masks, uint8_t *write_masks,
603 struct flashctx *flash, const struct flashrom_wp_cfg *cfg)
604 {
605 struct wp_bits bits;
606
607 if (!chip_supported(flash))
608 return FLASHROM_WP_ERR_CHIP_UNSUPPORTED;
609
610 enum flashrom_wp_result ret = read_wp_bits(&bits, flash);
611 if (ret != FLASHROM_WP_OK)
612 return ret;
613
614 /* Set protection range */
615 ret = set_wp_range(&bits, flash, cfg->range);
616 if (ret != FLASHROM_WP_OK)
617 return ret;
618
619 /* Set protection mode */
620 ret = set_wp_mode(&bits, cfg->mode);
621 if (ret != FLASHROM_WP_OK)
622 return ret;
623
624 get_wp_bits_reg_values(reg_values, bit_masks, write_masks, &flash->chip->reg_bits, bits);
625
626 return FLASHROM_WP_OK;
627 }
628