Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 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 "flash.h" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 23 | #include "libflashprog.h" |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 24 | #include "chipdrivers.h" |
| 25 | #include "writeprotect.h" |
| 26 | |
| 27 | /** Read and extract a single bit from the chip's registers */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 28 | static enum flashprog_wp_result read_bit(uint8_t *value, bool *present, struct flashctx *flash, struct reg_bit_info bit) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 29 | { |
| 30 | *present = bit.reg != INVALID_REG; |
| 31 | if (*present) { |
| 32 | if (spi_read_register(flash, bit.reg, value)) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 33 | return FLASHPROG_WP_ERR_READ_FAILED; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 34 | *value = (*value >> bit.bit_index) & 1; |
| 35 | } else { |
| 36 | /* Zero bit, it may be used by compare_ranges(). */ |
| 37 | *value = 0; |
| 38 | } |
| 39 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 40 | return FLASHPROG_WP_OK; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** Read all WP configuration bits from the chip's registers. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 44 | static enum flashprog_wp_result read_wp_bits(struct wp_bits *bits, struct flashctx *flash) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 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; |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 54 | enum flashprog_wp_result ret; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 55 | |
Sergii Dmytruk | 081ffba | 2022-08-17 18:29:10 +0300 | [diff] [blame] | 56 | /* |
| 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 68 | if (ret != FLASHPROG_WP_OK) |
Sergii Dmytruk | 081ffba | 2022-08-17 18:29:10 +0300 | [diff] [blame] | 69 | return ret; |
| 70 | |
| 71 | if (wps_bit_present && wps) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 72 | return FLASHPROG_WP_ERR_UNSUPPORTED_STATE; |
Sergii Dmytruk | 081ffba | 2022-08-17 18:29:10 +0300 | [diff] [blame] | 73 | } |
| 74 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 75 | ret = read_bit(&bits->tb, &bits->tb_bit_present, flash, bit_map->tb); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 76 | if (ret != FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 77 | return ret; |
| 78 | |
| 79 | ret = read_bit(&bits->sec, &bits->sec_bit_present, flash, bit_map->sec); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 80 | if (ret != FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 81 | return ret; |
| 82 | |
| 83 | ret = read_bit(&bits->cmp, &bits->cmp_bit_present, flash, bit_map->cmp); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 84 | if (ret != FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 85 | return ret; |
| 86 | |
| 87 | ret = read_bit(&bits->srp, &bits->srp_bit_present, flash, bit_map->srp); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 88 | if (ret != FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 89 | return ret; |
| 90 | |
| 91 | ret = read_bit(&bits->srl, &bits->srl_bit_present, flash, bit_map->srl); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 92 | if (ret != FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 93 | 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 101 | if (ret != FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | return ret; |
| 106 | } |
| 107 | |
Nikolai Artemiev | 09dd6ba | 2022-11-21 19:10:54 +1100 | [diff] [blame] | 108 | /** Helper function for get_wp_bits_reg_values(). */ |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 109 | static void set_reg_bit( |
Sergii Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 110 | uint8_t *reg_values, uint8_t *bit_masks, uint8_t *write_masks, |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 111 | 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 Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 115 | 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 Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Nikolai Artemiev | 09dd6ba | 2022-11-21 19:10:54 +1100 | [diff] [blame] | 123 | /** Convert wp_bits to register values and write masks */ |
| 124 | static 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 Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 127 | { |
| 128 | size_t i; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 129 | |
Nikolai Artemiev | 09dd6ba | 2022-11-21 19:10:54 +1100 | [diff] [blame] | 130 | 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 Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 133 | |
| 134 | for (i = 0; i < bits.bp_bit_count; i++) |
Sergii Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 135 | set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->bp[i], bits.bp[i]); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 136 | |
Sergii Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 137 | 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 Dmytruk | 801fcd0 | 2021-12-19 18:45:16 +0200 | [diff] [blame] | 142 | /* Note: always setting WPS bit to zero until its fully supported. */ |
Sergii Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 143 | set_reg_bit(reg_values, bit_masks, write_masks, reg_bits->wps, 0); |
Nikolai Artemiev | 09dd6ba | 2022-11-21 19:10:54 +1100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** Write WP configuration bits to the flash's registers. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 147 | static enum flashprog_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits) |
Nikolai Artemiev | 09dd6ba | 2022-11-21 19:10:54 +1100 | [diff] [blame] | 148 | { |
| 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 Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 154 | |
Sergii Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 155 | /* Write each register whose value was updated */ |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 156 | 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 162 | return FLASHPROG_WP_ERR_READ_FAILED; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 163 | |
Sergii Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 164 | /* 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 Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 171 | |
Nico Huber | 4ac536b | 2024-07-21 00:22:29 +0200 | [diff] [blame] | 172 | if (spi_write_register(flash, reg, value, WRSR_EITHER)) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 173 | return FLASHPROG_WP_ERR_WRITE_FAILED; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 174 | } |
| 175 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 176 | enum flashprog_wp_result ret = FLASHPROG_WP_OK; |
Sergii Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 177 | /* Verify each register even if write to it was skipped */ |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 178 | for (reg = STATUS1; reg < MAX_REGISTERS; reg++) { |
Sergii Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 179 | if (!bit_masks[reg]) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 180 | continue; |
| 181 | |
| 182 | uint8_t value; |
| 183 | if (spi_read_register(flash, reg, &value)) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 184 | return FLASHPROG_WP_ERR_READ_FAILED; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 185 | |
Evan Benn | d81997c | 2022-09-13 17:12:59 +1000 | [diff] [blame] | 186 | msg_cdbg2("%s: wp_verify reg:%u value:0x%x\n", __func__, reg, value); |
Sergii Dmytruk | 0b5a707 | 2022-08-14 16:51:46 +0300 | [diff] [blame] | 187 | uint8_t actual = value & bit_masks[reg]; |
| 188 | uint8_t expected = reg_values[reg] & bit_masks[reg]; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 189 | |
Evan Benn | d81997c | 2022-09-13 17:12:59 +1000 | [diff] [blame] | 190 | 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 193 | ret = FLASHPROG_WP_ERR_VERIFY_FAILED; |
Evan Benn | d81997c | 2022-09-13 17:12:59 +1000 | [diff] [blame] | 194 | } |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 195 | } |
| 196 | |
Evan Benn | d81997c | 2022-09-13 17:12:59 +1000 | [diff] [blame] | 197 | return ret; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 198 | } |
| 199 | |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 200 | /** Get the range selected by a WP configuration. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 201 | static enum flashprog_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits) |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 202 | { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 203 | flash->chip->decode_range(&range->start, &range->len, bits, flashprog_flash_getsize(flash)); |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 204 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 205 | return FLASHPROG_WP_OK; |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 206 | } |
| 207 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 208 | /** Write protect bit values and the range they will activate. */ |
| 209 | struct 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 | */ |
| 220 | static 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 | |
| 252 | static 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 270 | static 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 Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 271 | { |
| 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 Dmytruk | 801fcd0 | 2021-12-19 18:45:16 +0200 | [diff] [blame] | 305 | /* TODO: take WPS bit into account. */ |
| 306 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 307 | 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 320 | enum flashprog_wp_result ret = get_wp_range(&output->range, flash, &bits); |
| 321 | if (ret != FLASHPROG_WP_OK) { |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 322 | 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 Huber | 1e6aabc | 2022-05-28 16:39:07 +0200 | [diff] [blame] | 338 | msg_gspew(" start=0x%08zx length=0x%08zx\n", |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 339 | 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 366 | return FLASHPROG_WP_OK; |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 367 | } |
| 368 | |
Nikolai Artemiev | b6112a5 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 369 | static 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 | */ |
| 378 | static 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 384 | enum flashprog_wp_result ret = get_ranges_and_wp_bits(flash, *bits, &ranges, &count); |
| 385 | if (ret != FLASHPROG_WP_OK) |
Nikolai Artemiev | b6112a5 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 386 | return ret; |
| 387 | |
| 388 | /* Search for matching range */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 389 | ret = FLASHPROG_WP_ERR_RANGE_UNSUPPORTED; |
Nikolai Artemiev | b6112a5 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 390 | 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 Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 404 | /** Get the mode selected by a WP configuration. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 405 | static int get_wp_mode(enum flashprog_wp_mode *mode, const struct wp_bits *bits) |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 406 | { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 407 | const enum flashprog_wp_mode wp_modes[2][2] = { |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 408 | { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 409 | FLASHPROG_WP_MODE_DISABLED, /* srl=0, srp=0 */ |
| 410 | FLASHPROG_WP_MODE_HARDWARE, /* srl=0, srp=1 */ |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 411 | }, { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 412 | FLASHPROG_WP_MODE_POWER_CYCLE, /* srl=1, srp=0 */ |
| 413 | FLASHPROG_WP_MODE_PERMANENT, /* srl=1, srp=1 */ |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 414 | }, |
| 415 | }; |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 416 | |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 417 | *mode = wp_modes[bits->srl][bits->srp]; |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 418 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 419 | return FLASHPROG_WP_OK; |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | /** Modify a wp_bits structure such that it will select a specified protection mode. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 423 | static int set_wp_mode(struct wp_bits *bits, const enum flashprog_wp_mode mode) |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 424 | { |
| 425 | switch (mode) { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 426 | case FLASHPROG_WP_MODE_DISABLED: |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 427 | bits->srl = 0; |
| 428 | bits->srp = 0; |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 429 | return FLASHPROG_WP_OK; |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 430 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 431 | case FLASHPROG_WP_MODE_HARDWARE: |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 432 | if (!bits->srp_bit_present) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 433 | return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED; |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 434 | |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 435 | bits->srl = 0; |
| 436 | bits->srp = 1; |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 437 | return FLASHPROG_WP_OK; |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 438 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 439 | case FLASHPROG_WP_MODE_POWER_CYCLE: |
| 440 | case FLASHPROG_WP_MODE_PERMANENT: |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 441 | 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 448 | return FLASHPROG_WP_ERR_MODE_UNSUPPORTED; |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 452 | static bool chip_supported(struct flashctx *flash) |
| 453 | { |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 454 | return (flash->chip != NULL) && (flash->chip->decode_range != NULL); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 455 | } |
| 456 | |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 457 | enum flashprog_wp_result spi_wp_read_cfg(struct flashprog_wp_cfg *cfg, struct flashctx *flash) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 458 | { |
| 459 | struct wp_bits bits; |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 460 | enum flashprog_wp_result ret = FLASHPROG_WP_OK; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 461 | |
| 462 | if (!chip_supported(flash)) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 463 | ret = FLASHPROG_WP_ERR_CHIP_UNSUPPORTED; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 464 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 465 | if (ret == FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 466 | ret = read_wp_bits(&bits, flash); |
| 467 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 468 | if (ret == FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 469 | ret = get_wp_range(&cfg->range, flash, &bits); |
| 470 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 471 | if (ret == FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 472 | ret = get_wp_mode(&cfg->mode, &bits); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 473 | |
| 474 | return ret; |
| 475 | } |
| 476 | |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 477 | enum flashprog_wp_result spi_wp_write_cfg(struct flashctx *flash, const struct flashprog_wp_cfg *cfg) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 478 | { |
| 479 | struct wp_bits bits; |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 480 | enum flashprog_wp_result ret = FLASHPROG_WP_OK; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 481 | |
| 482 | if (!chip_supported(flash)) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 483 | ret = FLASHPROG_WP_ERR_CHIP_UNSUPPORTED; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 484 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 485 | if (ret == FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 486 | ret = read_wp_bits(&bits, flash); |
| 487 | |
| 488 | /* Set protection range */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 489 | if (ret == FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 490 | ret = set_wp_range(&bits, flash, cfg->range); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 491 | if (ret == FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 492 | ret = write_wp_bits(flash, bits); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 493 | |
| 494 | /* Set protection mode */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 495 | if (ret == FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 496 | ret = set_wp_mode(&bits, cfg->mode); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 497 | if (ret == FLASHPROG_WP_OK) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 498 | ret = write_wp_bits(flash, bits); |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 503 | enum flashprog_wp_result spi_wp_get_available_ranges(struct flashprog_wp_ranges **list, struct flashprog_flashctx *flash) |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 504 | { |
| 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 511 | return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED; |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 512 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 513 | enum flashprog_wp_result ret = read_wp_bits(&bits, flash); |
| 514 | if (ret != FLASHPROG_WP_OK) |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 515 | return ret; |
| 516 | |
| 517 | ret = get_ranges_and_wp_bits(flash, bits, &range_pairs, &count); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 518 | if (ret != FLASHPROG_WP_OK) |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 519 | return ret; |
| 520 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 521 | *list = calloc(1, sizeof(struct flashprog_wp_ranges)); |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 522 | struct wp_range *ranges = calloc(count, sizeof(struct wp_range)); |
| 523 | |
| 524 | if (!(*list) || !ranges) { |
| 525 | free(*list); |
| 526 | free(ranges); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 527 | ret = FLASHPROG_WP_ERR_OTHER; |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 528 | 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 | |
| 536 | out: |
| 537 | free(range_pairs); |
| 538 | return ret; |
| 539 | } |
| 540 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 541 | /** @} */ /* end flashprog-wp */ |