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 | |
| 56 | ret = read_bit(&bits->tb, &bits->tb_bit_present, flash, bit_map->tb); |
| 57 | if (ret != FLASHROM_WP_OK) |
| 58 | return ret; |
| 59 | |
| 60 | ret = read_bit(&bits->sec, &bits->sec_bit_present, flash, bit_map->sec); |
| 61 | if (ret != FLASHROM_WP_OK) |
| 62 | return ret; |
| 63 | |
| 64 | ret = read_bit(&bits->cmp, &bits->cmp_bit_present, flash, bit_map->cmp); |
| 65 | if (ret != FLASHROM_WP_OK) |
| 66 | return ret; |
| 67 | |
| 68 | ret = read_bit(&bits->srp, &bits->srp_bit_present, flash, bit_map->srp); |
| 69 | if (ret != FLASHROM_WP_OK) |
| 70 | return ret; |
| 71 | |
| 72 | ret = read_bit(&bits->srl, &bits->srl_bit_present, flash, bit_map->srl); |
| 73 | if (ret != FLASHROM_WP_OK) |
| 74 | return ret; |
| 75 | |
Sergii Dmytruk | 801fcd0 | 2021-12-19 18:45:16 +0200 | [diff] [blame^] | 76 | /* Note: WPS bit isn't read here, because it's not part of any range. */ |
| 77 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 78 | for (i = 0; i < ARRAY_SIZE(bits->bp); i++) { |
| 79 | if (bit_map->bp[i].reg == INVALID_REG) |
| 80 | break; |
| 81 | |
| 82 | bits->bp_bit_count = i + 1; |
| 83 | ret = read_bit(&bits->bp[i], &ignored, flash, bit_map->bp[i]); |
| 84 | if (ret != FLASHROM_WP_OK) |
| 85 | return ret; |
| 86 | } |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
| 91 | /** Helper function for write_wp_bits(). */ |
| 92 | static void set_reg_bit( |
| 93 | uint8_t *reg_values, uint8_t *write_masks, |
| 94 | struct reg_bit_info bit, uint8_t value) |
| 95 | { |
| 96 | if (bit.reg != INVALID_REG) { |
| 97 | reg_values[bit.reg] |= value << bit.bit_index; |
| 98 | write_masks[bit.reg] |= 1 << bit.bit_index; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** Write WP configuration bits to the flash's registers. */ |
| 103 | static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits) |
| 104 | { |
| 105 | size_t i; |
| 106 | enum flash_reg reg; |
| 107 | const struct reg_bit_map *reg_bits = &flash->chip->reg_bits; |
| 108 | |
| 109 | /* Convert wp_bits to register values and write masks */ |
| 110 | uint8_t reg_values[MAX_REGISTERS] = {0}; |
| 111 | uint8_t write_masks[MAX_REGISTERS] = {0}; |
| 112 | |
| 113 | for (i = 0; i < bits.bp_bit_count; i++) |
| 114 | set_reg_bit(reg_values, write_masks, reg_bits->bp[i], bits.bp[i]); |
| 115 | |
| 116 | set_reg_bit(reg_values, write_masks, reg_bits->tb, bits.tb); |
| 117 | set_reg_bit(reg_values, write_masks, reg_bits->sec, bits.sec); |
| 118 | set_reg_bit(reg_values, write_masks, reg_bits->cmp, bits.cmp); |
| 119 | set_reg_bit(reg_values, write_masks, reg_bits->srp, bits.srp); |
| 120 | set_reg_bit(reg_values, write_masks, reg_bits->srl, bits.srl); |
Sergii Dmytruk | 801fcd0 | 2021-12-19 18:45:16 +0200 | [diff] [blame^] | 121 | /* Note: always setting WPS bit to zero until its fully supported. */ |
| 122 | set_reg_bit(reg_values, write_masks, reg_bits->wps, 0); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 123 | |
| 124 | /* Write each register */ |
| 125 | for (reg = STATUS1; reg < MAX_REGISTERS; reg++) { |
| 126 | if (!write_masks[reg]) |
| 127 | continue; |
| 128 | |
| 129 | uint8_t value; |
| 130 | if (spi_read_register(flash, reg, &value)) |
| 131 | return FLASHROM_WP_ERR_READ_FAILED; |
| 132 | |
| 133 | value = (value & ~write_masks[reg]) | (reg_values[reg] & write_masks[reg]); |
| 134 | |
| 135 | if (spi_write_register(flash, reg, value)) |
| 136 | return FLASHROM_WP_ERR_WRITE_FAILED; |
| 137 | } |
| 138 | |
| 139 | /* Verify each register */ |
| 140 | for (reg = STATUS1; reg < MAX_REGISTERS; reg++) { |
| 141 | if (!write_masks[reg]) |
| 142 | continue; |
| 143 | |
| 144 | uint8_t value; |
| 145 | if (spi_read_register(flash, reg, &value)) |
| 146 | return FLASHROM_WP_ERR_READ_FAILED; |
| 147 | |
| 148 | uint8_t actual = value & write_masks[reg]; |
| 149 | uint8_t expected = reg_values[reg] & write_masks[reg]; |
| 150 | |
| 151 | if (actual != expected) |
| 152 | return FLASHROM_WP_ERR_VERIFY_FAILED; |
| 153 | } |
| 154 | |
| 155 | return FLASHROM_WP_OK; |
| 156 | } |
| 157 | |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 158 | /** Get the range selected by a WP configuration. */ |
| 159 | static enum flashrom_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits) |
| 160 | { |
| 161 | flash->chip->decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash)); |
| 162 | |
| 163 | return FLASHROM_WP_OK; |
| 164 | } |
| 165 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 166 | /** Write protect bit values and the range they will activate. */ |
| 167 | struct wp_range_and_bits { |
| 168 | struct wp_bits bits; |
| 169 | struct wp_range range; |
| 170 | }; |
| 171 | |
| 172 | /** |
| 173 | * Comparator used for sorting ranges in get_ranges_and_wp_bits(). |
| 174 | * |
| 175 | * Ranges are ordered by these attributes, in decreasing significance: |
| 176 | * (range length, range start, cmp bit, sec bit, tb bit, bp bits) |
| 177 | */ |
| 178 | static int compare_ranges(const void *aa, const void *bb) |
| 179 | { |
| 180 | const struct wp_range_and_bits |
| 181 | *a = (const struct wp_range_and_bits *)aa, |
| 182 | *b = (const struct wp_range_and_bits *)bb; |
| 183 | int i; |
| 184 | |
| 185 | int ord = 0; |
| 186 | |
| 187 | if (ord == 0) |
| 188 | ord = a->range.len - b->range.len; |
| 189 | |
| 190 | if (ord == 0) |
| 191 | ord = a->range.start - b->range.start; |
| 192 | |
| 193 | if (ord == 0) |
| 194 | ord = a->bits.cmp - b->bits.cmp; |
| 195 | |
| 196 | if (ord == 0) |
| 197 | ord = a->bits.sec - b->bits.sec; |
| 198 | |
| 199 | if (ord == 0) |
| 200 | ord = a->bits.tb - b->bits.tb; |
| 201 | |
| 202 | for (i = a->bits.bp_bit_count - 1; i >= 0; i--) { |
| 203 | if (ord == 0) |
| 204 | ord = a->bits.bp[i] - b->bits.bp[i]; |
| 205 | } |
| 206 | |
| 207 | return ord; |
| 208 | } |
| 209 | |
| 210 | static bool can_write_bit(const struct reg_bit_info bit) |
| 211 | { |
| 212 | /* |
| 213 | * TODO: check if the programmer supports writing the register that the |
| 214 | * bit is in. For example, some chipsets may only allow SR1 to be |
| 215 | * written. |
| 216 | */ |
| 217 | |
| 218 | return bit.reg != INVALID_REG && bit.writability == RW; |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Enumerate all protection ranges that the chip supports and that are able to |
| 223 | * be activated, given limitations such as OTP bits or programmer-enforced |
| 224 | * restrictions. Returns a list of deduplicated wp_range_and_bits structures. |
| 225 | * |
| 226 | * Allocates a buffer that must be freed by the caller with free(). |
| 227 | */ |
| 228 | 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) |
| 229 | { |
| 230 | const struct reg_bit_map *reg_bits = &flash->chip->reg_bits; |
| 231 | size_t i; |
| 232 | /* |
| 233 | * Create a list of bits that affect the chip's protection range in |
| 234 | * range_bits. Each element is a pointer to a member of the wp_bits |
| 235 | * structure that will be modified. |
| 236 | * |
| 237 | * Some chips have range bits that cannot be changed (e.g. MX25L6473E |
| 238 | * has a one-time programmable TB bit). Rather than enumerating all |
| 239 | * possible values for unwritable bits, just read their values from the |
| 240 | * chip to ensure we only enumerate ranges that are actually available. |
| 241 | */ |
| 242 | uint8_t *range_bits[ARRAY_SIZE(bits.bp) + 1 /* TB */ + 1 /* SEC */ + 1 /* CMP */]; |
| 243 | size_t bit_count = 0; |
| 244 | |
| 245 | for (i = 0; i < ARRAY_SIZE(bits.bp); i++) { |
| 246 | if (can_write_bit(reg_bits->bp[i])) |
| 247 | range_bits[bit_count++] = &bits.bp[i]; |
| 248 | } |
| 249 | |
| 250 | if (can_write_bit(reg_bits->tb)) |
| 251 | range_bits[bit_count++] = &bits.tb; |
| 252 | |
| 253 | if (can_write_bit(reg_bits->sec)) |
| 254 | range_bits[bit_count++] = &bits.sec; |
| 255 | |
| 256 | if (can_write_bit(reg_bits->cmp)) |
| 257 | range_bits[bit_count++] = &bits.cmp; |
| 258 | |
| 259 | /* Allocate output buffer */ |
| 260 | *count = 1 << bit_count; |
| 261 | *ranges = calloc(*count, sizeof(struct wp_range_and_bits)); |
| 262 | |
Sergii Dmytruk | 801fcd0 | 2021-12-19 18:45:16 +0200 | [diff] [blame^] | 263 | /* TODO: take WPS bit into account. */ |
| 264 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 265 | size_t range_index; |
| 266 | for (range_index = 0; range_index < *count; range_index++) { |
| 267 | /* |
| 268 | * Extract bits from the range index and assign them to members |
| 269 | * of the wp_bits structure. The loop bounds ensure that all |
| 270 | * bit combinations will be enumerated. |
| 271 | */ |
| 272 | for (i = 0; i < bit_count; i++) |
| 273 | *range_bits[i] = (range_index >> i) & 1; |
| 274 | |
| 275 | struct wp_range_and_bits *output = &(*ranges)[range_index]; |
| 276 | |
| 277 | output->bits = bits; |
| 278 | enum flashrom_wp_result ret = get_wp_range(&output->range, flash, &bits); |
| 279 | if (ret != FLASHROM_WP_OK) { |
| 280 | free(*ranges); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | /* Debug: print range bits and range */ |
| 285 | msg_gspew("Enumerated range: "); |
| 286 | if (bits.cmp_bit_present) |
| 287 | msg_gspew("CMP=%u ", bits.cmp); |
| 288 | if (bits.sec_bit_present) |
| 289 | msg_gspew("SEC=%u ", bits.sec); |
| 290 | if (bits.tb_bit_present) |
| 291 | msg_gspew("TB=%u ", bits.tb); |
| 292 | for (i = 0; i < bits.bp_bit_count; i++) { |
| 293 | size_t j = bits.bp_bit_count - i - 1; |
| 294 | msg_gspew("BP%zu=%u ", j, bits.bp[j]); |
| 295 | } |
| 296 | msg_gspew(" start=0x%08zx length=0x%08zx ", |
| 297 | output->range.start, output->range.len); |
| 298 | } |
| 299 | |
| 300 | /* Sort ranges. Ensures consistency if there are duplicate ranges. */ |
| 301 | qsort(*ranges, *count, sizeof(struct wp_range_and_bits), compare_ranges); |
| 302 | |
| 303 | /* Remove duplicates */ |
| 304 | size_t output_index = 0; |
| 305 | struct wp_range *last_range = NULL; |
| 306 | |
| 307 | for (i = 0; i < *count; i++) { |
| 308 | bool different_to_last = |
| 309 | (last_range == NULL) || |
| 310 | ((*ranges)[i].range.start != last_range->start) || |
| 311 | ((*ranges)[i].range.len != last_range->len); |
| 312 | |
| 313 | if (different_to_last) { |
| 314 | /* Move range to the next free position */ |
| 315 | (*ranges)[output_index] = (*ranges)[i]; |
| 316 | output_index++; |
| 317 | /* Keep track of last non-duplicate range */ |
| 318 | last_range = &(*ranges)[i].range; |
| 319 | } |
| 320 | } |
| 321 | /* Reduce count to only include non-duplicate ranges */ |
| 322 | *count = output_index; |
| 323 | |
| 324 | return FLASHROM_WP_OK; |
| 325 | } |
| 326 | |
Nikolai Artemiev | b6112a5 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 327 | static bool ranges_equal(struct wp_range a, struct wp_range b) |
| 328 | { |
| 329 | return (a.start == b.start) && (a.len == b.len); |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Modify the range-related bits in a wp_bits structure so they select a given |
| 334 | * protection range. Bits that control the protection mode are not changed. |
| 335 | */ |
| 336 | static int set_wp_range(struct wp_bits *bits, struct flashctx *flash, const struct wp_range range) |
| 337 | { |
| 338 | struct wp_range_and_bits *ranges = NULL; |
| 339 | size_t count; |
| 340 | size_t i; |
| 341 | |
| 342 | enum flashrom_wp_result ret = get_ranges_and_wp_bits(flash, *bits, &ranges, &count); |
| 343 | if (ret != FLASHROM_WP_OK) |
| 344 | return ret; |
| 345 | |
| 346 | /* Search for matching range */ |
| 347 | ret = FLASHROM_WP_ERR_RANGE_UNSUPPORTED; |
| 348 | for (i = 0; i < count; i++) { |
| 349 | |
| 350 | if (ranges_equal(ranges[i].range, range)) { |
| 351 | *bits = ranges[i].bits; |
| 352 | ret = 0; |
| 353 | break; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | free(ranges); |
| 358 | |
| 359 | return ret; |
| 360 | } |
| 361 | |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 362 | /** Get the mode selected by a WP configuration. */ |
| 363 | static int get_wp_mode(enum flashrom_wp_mode *mode, const struct wp_bits *bits) |
| 364 | { |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 365 | const enum flashrom_wp_mode wp_modes[2][2] = { |
| 366 | { |
| 367 | FLASHROM_WP_MODE_DISABLED, /* srl=0, srp=0 */ |
| 368 | FLASHROM_WP_MODE_HARDWARE, /* srl=0, srp=1 */ |
| 369 | }, { |
| 370 | FLASHROM_WP_MODE_POWER_CYCLE, /* srl=1, srp=0 */ |
| 371 | FLASHROM_WP_MODE_PERMANENT, /* srl=1, srp=1 */ |
| 372 | }, |
| 373 | }; |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 374 | |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 375 | *mode = wp_modes[bits->srl][bits->srp]; |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 376 | |
| 377 | return FLASHROM_WP_OK; |
| 378 | } |
| 379 | |
| 380 | /** Modify a wp_bits structure such that it will select a specified protection mode. */ |
| 381 | static int set_wp_mode(struct wp_bits *bits, const enum flashrom_wp_mode mode) |
| 382 | { |
| 383 | switch (mode) { |
| 384 | case FLASHROM_WP_MODE_DISABLED: |
| 385 | bits->srl = 0; |
| 386 | bits->srp = 0; |
| 387 | return FLASHROM_WP_OK; |
| 388 | |
| 389 | case FLASHROM_WP_MODE_HARDWARE: |
Nikolai Artemiev | ccae68a | 2022-03-08 01:07:01 +1100 | [diff] [blame] | 390 | if (!bits->srp_bit_present) |
| 391 | return FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 392 | |
Nikolai Artemiev | 9e1afb7 | 2021-10-21 02:29:22 +1100 | [diff] [blame] | 393 | bits->srl = 0; |
| 394 | bits->srp = 1; |
| 395 | return FLASHROM_WP_OK; |
| 396 | |
| 397 | case FLASHROM_WP_MODE_POWER_CYCLE: |
| 398 | case FLASHROM_WP_MODE_PERMANENT: |
| 399 | default: |
| 400 | /* |
| 401 | * Don't try to enable power cycle or permanent protection for |
| 402 | * now. Those modes may be possible to activate on some chips, |
| 403 | * but they are usually unavailable by default or require special |
| 404 | * commands to activate. |
| 405 | */ |
| 406 | return FLASHROM_WP_ERR_MODE_UNSUPPORTED; |
| 407 | } |
| 408 | } |
| 409 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 410 | static bool chip_supported(struct flashctx *flash) |
| 411 | { |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame] | 412 | return (flash->chip != NULL) && (flash->chip->decode_range != NULL); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | enum flashrom_wp_result wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash) |
| 416 | { |
| 417 | struct wp_bits bits; |
| 418 | enum flashrom_wp_result ret = FLASHROM_WP_OK; |
| 419 | |
| 420 | if (!chip_supported(flash)) |
| 421 | ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 422 | |
| 423 | if (ret == FLASHROM_WP_OK) |
| 424 | ret = read_wp_bits(&bits, flash); |
| 425 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 426 | if (ret == FLASHROM_WP_OK) |
| 427 | ret = get_wp_range(&cfg->range, flash, &bits); |
| 428 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 429 | if (ret == FLASHROM_WP_OK) |
| 430 | ret = get_wp_mode(&cfg->mode, &bits); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | enum flashrom_wp_result wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg) |
| 436 | { |
| 437 | struct wp_bits bits; |
| 438 | enum flashrom_wp_result ret = FLASHROM_WP_OK; |
| 439 | |
| 440 | if (!chip_supported(flash)) |
| 441 | ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 442 | |
| 443 | if (ret == FLASHROM_WP_OK) |
| 444 | ret = read_wp_bits(&bits, flash); |
| 445 | |
| 446 | /* Set protection range */ |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 447 | if (ret == FLASHROM_WP_OK) |
| 448 | ret = set_wp_range(&bits, flash, cfg->range); |
| 449 | if (ret == FLASHROM_WP_OK) |
| 450 | ret = write_wp_bits(flash, bits); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 451 | |
| 452 | /* Set protection mode */ |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 453 | if (ret == FLASHROM_WP_OK) |
| 454 | ret = set_wp_mode(&bits, cfg->mode); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 455 | if (ret == FLASHROM_WP_OK) |
| 456 | ret = write_wp_bits(flash, bits); |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 461 | enum flashrom_wp_result wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash) |
| 462 | { |
| 463 | struct wp_bits bits; |
| 464 | struct wp_range_and_bits *range_pairs = NULL; |
| 465 | size_t count; |
| 466 | size_t i; |
| 467 | |
| 468 | if (!chip_supported(flash)) |
| 469 | return FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 470 | |
| 471 | enum flashrom_wp_result ret = read_wp_bits(&bits, flash); |
| 472 | if (ret != FLASHROM_WP_OK) |
| 473 | return ret; |
| 474 | |
| 475 | ret = get_ranges_and_wp_bits(flash, bits, &range_pairs, &count); |
| 476 | if (ret != FLASHROM_WP_OK) |
| 477 | return ret; |
| 478 | |
| 479 | *list = calloc(1, sizeof(struct flashrom_wp_ranges)); |
| 480 | struct wp_range *ranges = calloc(count, sizeof(struct wp_range)); |
| 481 | |
| 482 | if (!(*list) || !ranges) { |
| 483 | free(*list); |
| 484 | free(ranges); |
| 485 | ret = FLASHROM_WP_ERR_OTHER; |
| 486 | goto out; |
| 487 | } |
| 488 | (*list)->count = count; |
| 489 | (*list)->ranges = ranges; |
| 490 | |
| 491 | for (i = 0; i < count; i++) |
| 492 | ranges[i] = range_pairs[i].range; |
| 493 | |
| 494 | out: |
| 495 | free(range_pairs); |
| 496 | return ret; |
| 497 | } |
| 498 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 499 | /** @} */ /* end flashrom-wp */ |