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" |
| 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 */ |
| 28 | static 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. */ |
| 44 | static 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 | |
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); |
| 68 | if (ret != FLASHROM_WP_OK) |
| 69 | return ret; |
| 70 | |
| 71 | if (wps_bit_present && wps) |
| 72 | return FLASHROM_WP_ERR_UNSUPPORTED_STATE; |
| 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); |
| 76 | if (ret != FLASHROM_WP_OK) |
| 77 | return ret; |
| 78 | |
| 79 | ret = read_bit(&bits->sec, &bits->sec_bit_present, flash, bit_map->sec); |
| 80 | if (ret != FLASHROM_WP_OK) |
| 81 | return ret; |
| 82 | |
| 83 | ret = read_bit(&bits->cmp, &bits->cmp_bit_present, flash, bit_map->cmp); |
| 84 | if (ret != FLASHROM_WP_OK) |
| 85 | return ret; |
| 86 | |
| 87 | ret = read_bit(&bits->srp, &bits->srp_bit_present, flash, bit_map->srp); |
| 88 | if (ret != FLASHROM_WP_OK) |
| 89 | return ret; |
| 90 | |
| 91 | ret = read_bit(&bits->srl, &bits->srl_bit_present, flash, bit_map->srl); |
| 92 | if (ret != FLASHROM_WP_OK) |
| 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]); |
| 101 | if (ret != FLASHROM_WP_OK) |
| 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. */ |
| 147 | static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits) |
| 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)) |
| 162 | return FLASHROM_WP_ERR_READ_FAILED; |
| 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 | |
| 172 | if (spi_write_register(flash, reg, value)) |
| 173 | return FLASHROM_WP_ERR_WRITE_FAILED; |
| 174 | } |
| 175 | |
Evan Benn | d81997c | 2022-09-13 17:12:59 +1000 | [diff] [blame] | 176 | enum flashrom_wp_result ret = FLASHROM_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)) |
| 184 | return FLASHROM_WP_ERR_READ_FAILED; |
| 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); |
| 193 | ret = FLASHROM_WP_ERR_VERIFY_FAILED; |
| 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. */ |
| 201 | static enum flashrom_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits) |
| 202 | { |
| 203 | flash->chip->decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash)); |
| 204 | |
| 205 | return FLASHROM_WP_OK; |
| 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 | */ |
| 270 | 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) |
| 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; |
| 320 | enum flashrom_wp_result ret = get_wp_range(&output->range, flash, &bits); |
| 321 | if (ret != FLASHROM_WP_OK) { |
| 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 | |
| 366 | return FLASHROM_WP_OK; |
| 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 | |
| 384 | enum flashrom_wp_result ret = get_ranges_and_wp_bits(flash, *bits, &ranges, &count); |
| 385 | if (ret != FLASHROM_WP_OK) |
| 386 | return ret; |
| 387 | |
| 388 | /* Search for matching range */ |
| 389 | ret = FLASHROM_WP_ERR_RANGE_UNSUPPORTED; |
| 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. */ |
| 405 | static int get_wp_mode(enum flashrom_wp_mode *mode, const struct wp_bits *bits) |
| 406 | { |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 407 | const enum flashrom_wp_mode wp_modes[2][2] = { |
| 408 | { |
| 409 | FLASHROM_WP_MODE_DISABLED, /* srl=0, srp=0 */ |
| 410 | FLASHROM_WP_MODE_HARDWARE, /* srl=0, srp=1 */ |
| 411 | }, { |
| 412 | FLASHROM_WP_MODE_POWER_CYCLE, /* srl=1, srp=0 */ |
| 413 | FLASHROM_WP_MODE_PERMANENT, /* srl=1, srp=1 */ |
| 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 | |
| 419 | return FLASHROM_WP_OK; |
| 420 | } |
| 421 | |
| 422 | /** Modify a wp_bits structure such that it will select a specified protection mode. */ |
| 423 | static int set_wp_mode(struct wp_bits *bits, const enum flashrom_wp_mode mode) |
| 424 | { |
| 425 | switch (mode) { |
| 426 | case FLASHROM_WP_MODE_DISABLED: |
| 427 | bits->srl = 0; |
| 428 | bits->srp = 0; |
| 429 | return FLASHROM_WP_OK; |
| 430 | |
| 431 | case FLASHROM_WP_MODE_HARDWARE: |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 432 | if (!bits->srp_bit_present) |
| 433 | return FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 434 | |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 435 | bits->srl = 0; |
| 436 | bits->srp = 1; |
| 437 | return FLASHROM_WP_OK; |
| 438 | |
| 439 | case FLASHROM_WP_MODE_POWER_CYCLE: |
| 440 | case FLASHROM_WP_MODE_PERMANENT: |
| 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 | */ |
| 448 | return FLASHROM_WP_ERR_MODE_UNSUPPORTED; |
| 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 | |
| 457 | enum flashrom_wp_result wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash) |
| 458 | { |
| 459 | struct wp_bits bits; |
| 460 | enum flashrom_wp_result ret = FLASHROM_WP_OK; |
| 461 | |
| 462 | if (!chip_supported(flash)) |
| 463 | ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 464 | |
| 465 | if (ret == FLASHROM_WP_OK) |
| 466 | ret = read_wp_bits(&bits, flash); |
| 467 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 468 | if (ret == FLASHROM_WP_OK) |
| 469 | ret = get_wp_range(&cfg->range, flash, &bits); |
| 470 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 471 | if (ret == FLASHROM_WP_OK) |
| 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 | |
| 477 | enum flashrom_wp_result wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg) |
| 478 | { |
| 479 | struct wp_bits bits; |
| 480 | enum flashrom_wp_result ret = FLASHROM_WP_OK; |
| 481 | |
| 482 | if (!chip_supported(flash)) |
| 483 | ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 484 | |
| 485 | if (ret == FLASHROM_WP_OK) |
| 486 | ret = read_wp_bits(&bits, flash); |
| 487 | |
| 488 | /* Set protection range */ |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 489 | if (ret == FLASHROM_WP_OK) |
| 490 | ret = set_wp_range(&bits, flash, cfg->range); |
| 491 | if (ret == FLASHROM_WP_OK) |
| 492 | ret = write_wp_bits(flash, bits); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 493 | |
| 494 | /* Set protection mode */ |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 495 | if (ret == FLASHROM_WP_OK) |
| 496 | ret = set_wp_mode(&bits, cfg->mode); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 497 | if (ret == FLASHROM_WP_OK) |
| 498 | ret = write_wp_bits(flash, bits); |
| 499 | |
| 500 | return ret; |
| 501 | } |
| 502 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 503 | enum flashrom_wp_result wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash) |
| 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)) |
| 511 | return FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 512 | |
| 513 | enum flashrom_wp_result ret = read_wp_bits(&bits, flash); |
| 514 | if (ret != FLASHROM_WP_OK) |
| 515 | return ret; |
| 516 | |
| 517 | ret = get_ranges_and_wp_bits(flash, bits, &range_pairs, &count); |
| 518 | if (ret != FLASHROM_WP_OK) |
| 519 | return ret; |
| 520 | |
| 521 | *list = calloc(1, sizeof(struct flashrom_wp_ranges)); |
| 522 | struct wp_range *ranges = calloc(count, sizeof(struct wp_range)); |
| 523 | |
| 524 | if (!(*list) || !ranges) { |
| 525 | free(*list); |
| 526 | free(ranges); |
| 527 | ret = FLASHROM_WP_ERR_OTHER; |
| 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 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 541 | /** @} */ /* end flashrom-wp */ |