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 | |
| 154 | static bool chip_supported(struct flashctx *flash) |
| 155 | { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | enum flashrom_wp_result wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash) |
| 160 | { |
| 161 | struct wp_bits bits; |
| 162 | enum flashrom_wp_result ret = FLASHROM_WP_OK; |
| 163 | |
| 164 | if (!chip_supported(flash)) |
| 165 | ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 166 | |
| 167 | if (ret == FLASHROM_WP_OK) |
| 168 | ret = read_wp_bits(&bits, flash); |
| 169 | |
| 170 | /* TODO: implement get_wp_range() and get_wp_mode() and call them */ |
| 171 | /* |
| 172 | if (ret == FLASHROM_WP_OK) |
| 173 | ret = get_wp_range(&cfg->range, flash, &bits); |
| 174 | |
| 175 | if (ret == FLASHROM_WP_OK) |
| 176 | ret = get_wp_mode(&cfg->mode, &bits); |
| 177 | */ |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
| 182 | enum flashrom_wp_result wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg) |
| 183 | { |
| 184 | struct wp_bits bits; |
| 185 | enum flashrom_wp_result ret = FLASHROM_WP_OK; |
| 186 | |
| 187 | if (!chip_supported(flash)) |
| 188 | ret = FLASHROM_WP_ERR_CHIP_UNSUPPORTED; |
| 189 | |
| 190 | if (ret == FLASHROM_WP_OK) |
| 191 | ret = read_wp_bits(&bits, flash); |
| 192 | |
| 193 | /* Set protection range */ |
| 194 | /* TODO: implement set_wp_range() and use it */ |
| 195 | /* |
| 196 | if (ret == FLASHROM_WP_OK) |
| 197 | ret = set_wp_range(&bits, flash, cfg->range); |
| 198 | if (ret == FLASHROM_WP_OK) |
| 199 | ret = write_wp_bits(flash, bits); |
| 200 | */ |
| 201 | |
| 202 | /* Set protection mode */ |
| 203 | /* TODO: implement set_wp_mode() and use it */ |
| 204 | /* |
| 205 | if (ret == FLASHROM_WP_OK) |
| 206 | ret = set_wp_mode(&bits, cfg->mode); |
| 207 | */ |
| 208 | if (ret == FLASHROM_WP_OK) |
| 209 | ret = write_wp_bits(flash, bits); |
| 210 | |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | /** @} */ /* end flashrom-wp */ |