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 | |
| 76 | for (i = 0; i < ARRAY_SIZE(bits->bp); i++) { |
| 77 | if (bit_map->bp[i].reg == INVALID_REG) |
| 78 | break; |
| 79 | |
| 80 | bits->bp_bit_count = i + 1; |
| 81 | ret = read_bit(&bits->bp[i], &ignored, flash, bit_map->bp[i]); |
| 82 | if (ret != FLASHROM_WP_OK) |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | /** Helper function for write_wp_bits(). */ |
| 90 | static void set_reg_bit( |
| 91 | uint8_t *reg_values, uint8_t *write_masks, |
| 92 | struct reg_bit_info bit, uint8_t value) |
| 93 | { |
| 94 | if (bit.reg != INVALID_REG) { |
| 95 | reg_values[bit.reg] |= value << bit.bit_index; |
| 96 | write_masks[bit.reg] |= 1 << bit.bit_index; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** Write WP configuration bits to the flash's registers. */ |
| 101 | static enum flashrom_wp_result write_wp_bits(struct flashctx *flash, struct wp_bits bits) |
| 102 | { |
| 103 | size_t i; |
| 104 | enum flash_reg reg; |
| 105 | const struct reg_bit_map *reg_bits = &flash->chip->reg_bits; |
| 106 | |
| 107 | /* Convert wp_bits to register values and write masks */ |
| 108 | uint8_t reg_values[MAX_REGISTERS] = {0}; |
| 109 | uint8_t write_masks[MAX_REGISTERS] = {0}; |
| 110 | |
| 111 | for (i = 0; i < bits.bp_bit_count; i++) |
| 112 | set_reg_bit(reg_values, write_masks, reg_bits->bp[i], bits.bp[i]); |
| 113 | |
| 114 | set_reg_bit(reg_values, write_masks, reg_bits->tb, bits.tb); |
| 115 | set_reg_bit(reg_values, write_masks, reg_bits->sec, bits.sec); |
| 116 | set_reg_bit(reg_values, write_masks, reg_bits->cmp, bits.cmp); |
| 117 | set_reg_bit(reg_values, write_masks, reg_bits->srp, bits.srp); |
| 118 | set_reg_bit(reg_values, write_masks, reg_bits->srl, bits.srl); |
| 119 | |
| 120 | /* Write each register */ |
| 121 | for (reg = STATUS1; reg < MAX_REGISTERS; reg++) { |
| 122 | if (!write_masks[reg]) |
| 123 | continue; |
| 124 | |
| 125 | uint8_t value; |
| 126 | if (spi_read_register(flash, reg, &value)) |
| 127 | return FLASHROM_WP_ERR_READ_FAILED; |
| 128 | |
| 129 | value = (value & ~write_masks[reg]) | (reg_values[reg] & write_masks[reg]); |
| 130 | |
| 131 | if (spi_write_register(flash, reg, value)) |
| 132 | return FLASHROM_WP_ERR_WRITE_FAILED; |
| 133 | } |
| 134 | |
| 135 | /* Verify each register */ |
| 136 | for (reg = STATUS1; reg < MAX_REGISTERS; reg++) { |
| 137 | if (!write_masks[reg]) |
| 138 | continue; |
| 139 | |
| 140 | uint8_t value; |
| 141 | if (spi_read_register(flash, reg, &value)) |
| 142 | return FLASHROM_WP_ERR_READ_FAILED; |
| 143 | |
| 144 | uint8_t actual = value & write_masks[reg]; |
| 145 | uint8_t expected = reg_values[reg] & write_masks[reg]; |
| 146 | |
| 147 | if (actual != expected) |
| 148 | return FLASHROM_WP_ERR_VERIFY_FAILED; |
| 149 | } |
| 150 | |
| 151 | return FLASHROM_WP_OK; |
| 152 | } |
| 153 | |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame^] | 154 | /** Get the range selected by a WP configuration. */ |
| 155 | static enum flashrom_wp_result get_wp_range(struct wp_range *range, struct flashctx *flash, const struct wp_bits *bits) |
| 156 | { |
| 157 | flash->chip->decode_range(&range->start, &range->len, bits, flashrom_flash_getsize(flash)); |
| 158 | |
| 159 | return FLASHROM_WP_OK; |
| 160 | } |
| 161 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 162 | static bool chip_supported(struct flashctx *flash) |
| 163 | { |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame^] | 164 | return (flash->chip != NULL) && (flash->chip->decode_range != NULL); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | enum flashrom_wp_result wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash) |
| 168 | { |
| 169 | struct wp_bits bits; |
| 170 | enum flashrom_wp_result ret = FLASHROM_WP_OK; |
| 171 | |
| 172 | if (!chip_supported(flash)) |
| 173 | ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 174 | |
| 175 | if (ret == FLASHROM_WP_OK) |
| 176 | ret = read_wp_bits(&bits, flash); |
| 177 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 178 | if (ret == FLASHROM_WP_OK) |
| 179 | ret = get_wp_range(&cfg->range, flash, &bits); |
| 180 | |
Nikolai Artemiev | 1234d11 | 2021-10-21 02:28:23 +1100 | [diff] [blame^] | 181 | /* TODO: implement and get_wp_mode() and call it */ |
| 182 | /* |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 183 | if (ret == FLASHROM_WP_OK) |
| 184 | ret = get_wp_mode(&cfg->mode, &bits); |
| 185 | */ |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | enum flashrom_wp_result wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg) |
| 191 | { |
| 192 | struct wp_bits bits; |
| 193 | enum flashrom_wp_result ret = FLASHROM_WP_OK; |
| 194 | |
| 195 | if (!chip_supported(flash)) |
| 196 | ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 197 | |
| 198 | if (ret == FLASHROM_WP_OK) |
| 199 | ret = read_wp_bits(&bits, flash); |
| 200 | |
| 201 | /* Set protection range */ |
| 202 | /* TODO: implement set_wp_range() and use it */ |
| 203 | /* |
| 204 | if (ret == FLASHROM_WP_OK) |
| 205 | ret = set_wp_range(&bits, flash, cfg->range); |
| 206 | if (ret == FLASHROM_WP_OK) |
| 207 | ret = write_wp_bits(flash, bits); |
| 208 | */ |
| 209 | |
| 210 | /* Set protection mode */ |
| 211 | /* TODO: implement set_wp_mode() and use it */ |
| 212 | /* |
| 213 | if (ret == FLASHROM_WP_OK) |
| 214 | ret = set_wp_mode(&bits, cfg->mode); |
| 215 | */ |
| 216 | if (ret == FLASHROM_WP_OK) |
| 217 | ret = write_wp_bits(flash, bits); |
| 218 | |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | /** @} */ /* end flashrom-wp */ |