blob: b91f3e830c11714413cce1fafe7d5b7f4fd376ab [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"
Nico Huberc3b02dc2023-08-12 01:13:45 +020023#include "libflashprog.h"
Nikolai Artemievda1c8342021-10-21 00:58:12 +110024#include "chipdrivers.h"
25#include "writeprotect.h"
26
27/** Read and extract a single bit from the chip's registers */
Nico Huberc3b02dc2023-08-12 01:13:45 +020028static enum flashprog_wp_result read_bit(uint8_t *value, bool *present, struct flashctx *flash, struct reg_bit_info bit)
Nikolai Artemievda1c8342021-10-21 00:58:12 +110029{
30 *present = bit.reg != INVALID_REG;
31 if (*present) {
32 if (spi_read_register(flash, bit.reg, value))
Nico Huberc3b02dc2023-08-12 01:13:45 +020033 return FLASHPROG_WP_ERR_READ_FAILED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +110034 *value = (*value >> bit.bit_index) & 1;
35 } else {
36 /* Zero bit, it may be used by compare_ranges(). */
37 *value = 0;
38 }
39
Nico Huberc3b02dc2023-08-12 01:13:45 +020040 return FLASHPROG_WP_OK;
Nikolai Artemievda1c8342021-10-21 00:58:12 +110041}
42
43/** Read all WP configuration bits from the chip's registers. */
Nico Huberc3b02dc2023-08-12 01:13:45 +020044static enum flashprog_wp_result read_wp_bits(struct wp_bits *bits, struct flashctx *flash)
Nikolai Artemievda1c8342021-10-21 00:58:12 +110045{
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;
Nico Huberc3b02dc2023-08-12 01:13:45 +020054 enum flashprog_wp_result ret;
Nikolai Artemievda1c8342021-10-21 00:58:12 +110055
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);
Nico Huberc3b02dc2023-08-12 01:13:45 +020068 if (ret != FLASHPROG_WP_OK)
Sergii Dmytruk081ffba2022-08-17 18:29:10 +030069 return ret;
70
71 if (wps_bit_present && wps)
Nico Huberc3b02dc2023-08-12 01:13:45 +020072 return FLASHPROG_WP_ERR_UNSUPPORTED_STATE;
Sergii Dmytruk081ffba2022-08-17 18:29:10 +030073 }
74
Nikolai Artemievda1c8342021-10-21 00:58:12 +110075 ret = read_bit(&bits->tb, &bits->tb_bit_present, flash, bit_map->tb);
Nico Huberc3b02dc2023-08-12 01:13:45 +020076 if (ret != FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +110077 return ret;
78
79 ret = read_bit(&bits->sec, &bits->sec_bit_present, flash, bit_map->sec);
Nico Huberc3b02dc2023-08-12 01:13:45 +020080 if (ret != FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +110081 return ret;
82
83 ret = read_bit(&bits->cmp, &bits->cmp_bit_present, flash, bit_map->cmp);
Nico Huberc3b02dc2023-08-12 01:13:45 +020084 if (ret != FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +110085 return ret;
86
87 ret = read_bit(&bits->srp, &bits->srp_bit_present, flash, bit_map->srp);
Nico Huberc3b02dc2023-08-12 01:13:45 +020088 if (ret != FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +110089 return ret;
90
91 ret = read_bit(&bits->srl, &bits->srl_bit_present, flash, bit_map->srl);
Nico Huberc3b02dc2023-08-12 01:13:45 +020092 if (ret != FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +110093 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]);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200101 if (ret != FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100102 return ret;
103 }
104
105 return ret;
106}
107
Nikolai Artemiev09dd6ba2022-11-21 19:10:54 +1100108/** Helper function for get_wp_bits_reg_values(). */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100109static 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
Nikolai Artemiev09dd6ba2022-11-21 19:10:54 +1100123/** Convert wp_bits to register values and write masks */
124static void get_wp_bits_reg_values(
125 uint8_t *reg_values, uint8_t *bit_masks, uint8_t *write_masks,
126 const struct reg_bit_map *reg_bits, struct wp_bits bits)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100127{
128 size_t i;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100129
Nikolai Artemiev09dd6ba2022-11-21 19:10:54 +1100130 memset(reg_values, 0, sizeof(uint8_t) * MAX_REGISTERS);
131 memset(bit_masks, 0, sizeof(uint8_t) * MAX_REGISTERS);
132 memset(write_masks, 0, sizeof(uint8_t) * MAX_REGISTERS);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100133
134 for (i = 0; i < bits.bp_bit_count; i++)
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300135 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->bp[i], bits.bp[i]);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100136
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300137 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->tb, bits.tb);
138 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->sec, bits.sec);
139 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->cmp, bits.cmp);
140 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->srp, bits.srp);
141 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->srl, bits.srl);
Sergii Dmytruk801fcd02021-12-19 18:45:16 +0200142 /* Note: always setting WPS bit to zero until its fully supported. */
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300143 set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->wps, 0);
Nikolai Artemiev09dd6ba2022-11-21 19:10:54 +1100144}
145
146/** Write WP configuration bits to the flash's registers. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200147static enum flashprog_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits)
Nikolai Artemiev09dd6ba2022-11-21 19:10:54 +1100148{
149 enum flash_reg reg;
150 uint8_t reg_values[MAX_REGISTERS];
151 uint8_t bit_masks[MAX_REGISTERS]; /* masks of valid bits */
152 uint8_t write_masks[MAX_REGISTERS]; /* masks of written bits */
153 get_wp_bits_reg_values(reg_values, bit_masks, write_masks, &flash->chip->reg_bits, bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100154
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300155 /* Write each register whose value was updated */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100156 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
157 if (!write_masks[reg])
158 continue;
159
160 uint8_t value;
161 if (spi_read_register(flash, reg, &value))
Nico Huberc3b02dc2023-08-12 01:13:45 +0200162 return FLASHPROG_WP_ERR_READ_FAILED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100163
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300164 /* Skip unnecessary register writes */
165 uint8_t actual = value & write_masks[reg];
166 uint8_t expected = reg_values[reg] & write_masks[reg];
167 if (actual == expected)
168 continue;
169
170 value = (value & ~write_masks[reg]) | expected;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100171
172 if (spi_write_register(flash, reg, value))
Nico Huberc3b02dc2023-08-12 01:13:45 +0200173 return FLASHPROG_WP_ERR_WRITE_FAILED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100174 }
175
Nico Huberc3b02dc2023-08-12 01:13:45 +0200176 enum flashprog_wp_result ret = FLASHPROG_WP_OK;
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300177 /* Verify each register even if write to it was skipped */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100178 for (reg = STATUS1; reg < MAX_REGISTERS; reg++) {
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300179 if (!bit_masks[reg])
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100180 continue;
181
182 uint8_t value;
183 if (spi_read_register(flash, reg, &value))
Nico Huberc3b02dc2023-08-12 01:13:45 +0200184 return FLASHPROG_WP_ERR_READ_FAILED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100185
Evan Bennd81997c2022-09-13 17:12:59 +1000186 msg_cdbg2("%s: wp_verify reg:%u value:0x%x\n", __func__, reg, value);
Sergii Dmytruk0b5a7072022-08-14 16:51:46 +0300187 uint8_t actual = value & bit_masks[reg];
188 uint8_t expected = reg_values[reg] & bit_masks[reg];
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100189
Evan Bennd81997c2022-09-13 17:12:59 +1000190 if (actual != expected) {
191 msg_cdbg("%s: wp_verify failed: reg:%u actual:0x%x expected:0x%x\n",
192 __func__, reg, actual, expected);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200193 ret = FLASHPROG_WP_ERR_VERIFY_FAILED;
Evan Bennd81997c2022-09-13 17:12:59 +1000194 }
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100195 }
196
Evan Bennd81997c2022-09-13 17:12:59 +1000197 return ret;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100198}
199
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100200/** Get the range selected by a WP configuration. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200201static enum flashprog_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits)
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100202{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200203 flash->chip->decode_range(&range->start, &range->len, bits, flashprog_flash_getsize(flash));
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100204
Nico Huberc3b02dc2023-08-12 01:13:45 +0200205 return FLASHPROG_WP_OK;
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100206}
207
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100208/** Write protect bit values and the range they will activate. */
209struct wp_range_and_bits {
210 struct wp_bits bits;
211 struct wp_range range;
212};
213
214/**
215 * Comparator used for sorting ranges in get_ranges_and_wp_bits().
216 *
217 * Ranges are ordered by these attributes, in decreasing significance:
218 * (range length, range start, cmp bit, sec bit, tb bit, bp bits)
219 */
220static int compare_ranges(const void *aa, const void *bb)
221{
222 const struct wp_range_and_bits
223 *a = (const struct wp_range_and_bits *)aa,
224 *b = (const struct wp_range_and_bits *)bb;
225 int i;
226
227 int ord = 0;
228
229 if (ord == 0)
230 ord = a->range.len - b->range.len;
231
232 if (ord == 0)
233 ord = a->range.start - b->range.start;
234
235 if (ord == 0)
236 ord = a->bits.cmp - b->bits.cmp;
237
238 if (ord == 0)
239 ord = a->bits.sec - b->bits.sec;
240
241 if (ord == 0)
242 ord = a->bits.tb - b->bits.tb;
243
244 for (i = a->bits.bp_bit_count - 1; i >= 0; i--) {
245 if (ord == 0)
246 ord = a->bits.bp[i] - b->bits.bp[i];
247 }
248
249 return ord;
250}
251
252static bool can_write_bit(const struct reg_bit_info bit)
253{
254 /*
255 * TODO: check if the programmer supports writing the register that the
256 * bit is in. For example, some chipsets may only allow SR1 to be
257 * written.
258 */
259
260 return bit.reg != INVALID_REG && bit.writability == RW;
261}
262
263/**
264 * Enumerate all protection ranges that the chip supports and that are able to
265 * be activated, given limitations such as OTP bits or programmer-enforced
266 * restrictions. Returns a list of deduplicated wp_range_and_bits structures.
267 *
268 * Allocates a buffer that must be freed by the caller with free().
269 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200270static enum flashprog_wp_result get_ranges_and_wp_bits(struct flashctx *flash, struct wp_bits bits, struct wp_range_and_bits **ranges, size_t *count)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100271{
272 const struct reg_bit_map *reg_bits = &flash->chip->reg_bits;
273 size_t i;
274 /*
275 * Create a list of bits that affect the chip's protection range in
276 * range_bits. Each element is a pointer to a member of the wp_bits
277 * structure that will be modified.
278 *
279 * Some chips have range bits that cannot be changed (e.g. MX25L6473E
280 * has a one-time programmable TB bit). Rather than enumerating all
281 * possible values for unwritable bits, just read their values from the
282 * chip to ensure we only enumerate ranges that are actually available.
283 */
284 uint8_t *range_bits[ARRAY_SIZE(bits.bp) + 1 /* TB */ + 1 /* SEC */ + 1 /* CMP */];
285 size_t bit_count = 0;
286
287 for (i = 0; i < ARRAY_SIZE(bits.bp); i++) {
288 if (can_write_bit(reg_bits->bp[i]))
289 range_bits[bit_count++] = &bits.bp[i];
290 }
291
292 if (can_write_bit(reg_bits->tb))
293 range_bits[bit_count++] = &bits.tb;
294
295 if (can_write_bit(reg_bits->sec))
296 range_bits[bit_count++] = &bits.sec;
297
298 if (can_write_bit(reg_bits->cmp))
299 range_bits[bit_count++] = &bits.cmp;
300
301 /* Allocate output buffer */
302 *count = 1 << bit_count;
303 *ranges = calloc(*count, sizeof(struct wp_range_and_bits));
304
Sergii Dmytruk801fcd02021-12-19 18:45:16 +0200305 /* TODO: take WPS bit into account. */
306
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100307 size_t range_index;
308 for (range_index = 0; range_index < *count; range_index++) {
309 /*
310 * Extract bits from the range index and assign them to members
311 * of the wp_bits structure. The loop bounds ensure that all
312 * bit combinations will be enumerated.
313 */
314 for (i = 0; i < bit_count; i++)
315 *range_bits[i] = (range_index >> i) & 1;
316
317 struct wp_range_and_bits *output = &(*ranges)[range_index];
318
319 output->bits = bits;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200320 enum flashprog_wp_result ret = get_wp_range(&output->range, flash, &bits);
321 if (ret != FLASHPROG_WP_OK) {
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100322 free(*ranges);
323 return ret;
324 }
325
326 /* Debug: print range bits and range */
327 msg_gspew("Enumerated range: ");
328 if (bits.cmp_bit_present)
329 msg_gspew("CMP=%u ", bits.cmp);
330 if (bits.sec_bit_present)
331 msg_gspew("SEC=%u ", bits.sec);
332 if (bits.tb_bit_present)
333 msg_gspew("TB=%u ", bits.tb);
334 for (i = 0; i < bits.bp_bit_count; i++) {
335 size_t j = bits.bp_bit_count - i - 1;
336 msg_gspew("BP%zu=%u ", j, bits.bp[j]);
337 }
Nico Huber1e6aabc2022-05-28 16:39:07 +0200338 msg_gspew(" start=0x%08zx length=0x%08zx\n",
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100339 output->range.start, output->range.len);
340 }
341
342 /* Sort ranges. Ensures consistency if there are duplicate ranges. */
343 qsort(*ranges, *count, sizeof(struct wp_range_and_bits), compare_ranges);
344
345 /* Remove duplicates */
346 size_t output_index = 0;
347 struct wp_range *last_range = NULL;
348
349 for (i = 0; i < *count; i++) {
350 bool different_to_last =
351 (last_range == NULL) ||
352 ((*ranges)[i].range.start != last_range->start) ||
353 ((*ranges)[i].range.len != last_range->len);
354
355 if (different_to_last) {
356 /* Move range to the next free position */
357 (*ranges)[output_index] = (*ranges)[i];
358 output_index++;
359 /* Keep track of last non-duplicate range */
360 last_range = &(*ranges)[i].range;
361 }
362 }
363 /* Reduce count to only include non-duplicate ranges */
364 *count = output_index;
365
Nico Huberc3b02dc2023-08-12 01:13:45 +0200366 return FLASHPROG_WP_OK;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100367}
368
Nikolai Artemievb6112a52021-10-21 02:28:23 +1100369static bool ranges_equal(struct wp_range a, struct wp_range b)
370{
371 return (a.start == b.start) && (a.len == b.len);
372}
373
374/*
375 * Modify the range-related bits in a wp_bits structure so they select a given
376 * protection range. Bits that control the protection mode are not changed.
377 */
378static int set_wp_range(struct wp_bits *bits, struct flashctx *flash, const struct wp_range range)
379{
380 struct wp_range_and_bits *ranges = NULL;
381 size_t count;
382 size_t i;
383
Nico Huberc3b02dc2023-08-12 01:13:45 +0200384 enum flashprog_wp_result ret = get_ranges_and_wp_bits(flash, *bits, &ranges, &count);
385 if (ret != FLASHPROG_WP_OK)
Nikolai Artemievb6112a52021-10-21 02:28:23 +1100386 return ret;
387
388 /* Search for matching range */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200389 ret = FLASHPROG_WP_ERR_RANGE_UNSUPPORTED;
Nikolai Artemievb6112a52021-10-21 02:28:23 +1100390 for (i = 0; i < count; i++) {
391
392 if (ranges_equal(ranges[i].range, range)) {
393 *bits = ranges[i].bits;
394 ret = 0;
395 break;
396 }
397 }
398
399 free(ranges);
400
401 return ret;
402}
403
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100404/** Get the mode selected by a WP configuration. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200405static int get_wp_mode(enum flashprog_wp_mode *mode, const struct wp_bits *bits)
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100406{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200407 const enum flashprog_wp_mode wp_modes[2][2] = {
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100408 {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200409 FLASHPROG_WP_MODE_DISABLED, /* srl=0, srp=0 */
410 FLASHPROG_WP_MODE_HARDWARE, /* srl=0, srp=1 */
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100411 }, {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200412 FLASHPROG_WP_MODE_POWER_CYCLE, /* srl=1, srp=0 */
413 FLASHPROG_WP_MODE_PERMANENT, /* srl=1, srp=1 */
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100414 },
415 };
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100416
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100417 *mode = wp_modes[bits->srl][bits->srp];
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100418
Nico Huberc3b02dc2023-08-12 01:13:45 +0200419 return FLASHPROG_WP_OK;
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100420}
421
422/** Modify a wp_bits structure such that it will select a specified protection mode. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200423static int set_wp_mode(struct wp_bits *bits, const enum flashprog_wp_mode mode)
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100424{
425 switch (mode) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200426 case FLASHPROG_WP_MODE_DISABLED:
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100427 bits->srl = 0;
428 bits->srp = 0;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200429 return FLASHPROG_WP_OK;
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100430
Nico Huberc3b02dc2023-08-12 01:13:45 +0200431 case FLASHPROG_WP_MODE_HARDWARE:
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100432 if (!bits->srp_bit_present)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200433 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievccae68a2022-03-08 01:07:01 +1100434
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100435 bits->srl = 0;
436 bits->srp = 1;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200437 return FLASHPROG_WP_OK;
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100438
Nico Huberc3b02dc2023-08-12 01:13:45 +0200439 case FLASHPROG_WP_MODE_POWER_CYCLE:
440 case FLASHPROG_WP_MODE_PERMANENT:
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100441 default:
442 /*
443 * Don't try to enable power cycle or permanent protection for
444 * now. Those modes may be possible to activate on some chips,
445 * but they are usually unavailable by default or require special
446 * commands to activate.
447 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200448 return FLASHPROG_WP_ERR_MODE_UNSUPPORTED;
Nikolai Artemiev9e1afb72021-10-21 02:29:22 +1100449 }
450}
451
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100452static bool chip_supported(struct flashctx *flash)
453{
Nikolai Artemiev1234d112021-10-21 02:28:23 +1100454 return (flash->chip != NULL) && (flash->chip->decode_range != NULL);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100455}
456
Nico Huberc3b02dc2023-08-12 01:13:45 +0200457enum flashprog_wp_result wp_read_cfg(struct flashprog_wp_cfg *cfg, struct flashctx *flash)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100458{
459 struct wp_bits bits;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200460 enum flashprog_wp_result ret = FLASHPROG_WP_OK;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100461
462 if (!chip_supported(flash))
Nico Huberc3b02dc2023-08-12 01:13:45 +0200463 ret = FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100464
Nico Huberc3b02dc2023-08-12 01:13:45 +0200465 if (ret == FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100466 ret = read_wp_bits(&bits, flash);
467
Nico Huberc3b02dc2023-08-12 01:13:45 +0200468 if (ret == FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100469 ret = get_wp_range(&cfg->range, flash, &bits);
470
Nico Huberc3b02dc2023-08-12 01:13:45 +0200471 if (ret == FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100472 ret = get_wp_mode(&cfg->mode, &bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100473
474 return ret;
475}
476
Nico Huberc3b02dc2023-08-12 01:13:45 +0200477enum flashprog_wp_result wp_write_cfg(struct flashctx *flash, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100478{
479 struct wp_bits bits;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200480 enum flashprog_wp_result ret = FLASHPROG_WP_OK;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100481
482 if (!chip_supported(flash))
Nico Huberc3b02dc2023-08-12 01:13:45 +0200483 ret = FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100484
Nico Huberc3b02dc2023-08-12 01:13:45 +0200485 if (ret == FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100486 ret = read_wp_bits(&bits, flash);
487
488 /* Set protection range */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200489 if (ret == FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100490 ret = set_wp_range(&bits, flash, cfg->range);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200491 if (ret == FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100492 ret = write_wp_bits(flash, bits);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100493
494 /* Set protection mode */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200495 if (ret == FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100496 ret = set_wp_mode(&bits, cfg->mode);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200497 if (ret == FLASHPROG_WP_OK)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100498 ret = write_wp_bits(flash, bits);
499
500 return ret;
501}
502
Nico Huberc3b02dc2023-08-12 01:13:45 +0200503enum flashprog_wp_result wp_get_available_ranges(struct flashprog_wp_ranges **list, struct flashprog_flashctx *flash)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100504{
505 struct wp_bits bits;
506 struct wp_range_and_bits *range_pairs = NULL;
507 size_t count;
508 size_t i;
509
510 if (!chip_supported(flash))
Nico Huberc3b02dc2023-08-12 01:13:45 +0200511 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100512
Nico Huberc3b02dc2023-08-12 01:13:45 +0200513 enum flashprog_wp_result ret = read_wp_bits(&bits, flash);
514 if (ret != FLASHPROG_WP_OK)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100515 return ret;
516
517 ret = get_ranges_and_wp_bits(flash, bits, &range_pairs, &count);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200518 if (ret != FLASHPROG_WP_OK)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100519 return ret;
520
Nico Huberc3b02dc2023-08-12 01:13:45 +0200521 *list = calloc(1, sizeof(struct flashprog_wp_ranges));
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100522 struct wp_range *ranges = calloc(count, sizeof(struct wp_range));
523
524 if (!(*list) || !ranges) {
525 free(*list);
526 free(ranges);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200527 ret = FLASHPROG_WP_ERR_OTHER;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100528 goto out;
529 }
530 (*list)->count = count;
531 (*list)->ranges = ranges;
532
533 for (i = 0; i < count; i++)
534 ranges[i] = range_pairs[i].range;
535
536out:
537 free(range_pairs);
538 return ret;
539}
540
Nico Huberc3b02dc2023-08-12 01:13:45 +0200541/** @} */ /* end flashprog-wp */