Nikolai Artemiev | c9feb1b | 2021-10-21 01:35:13 +1100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright 2021 Google LLC |
| 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 | #include "writeprotect.h" |
| 18 | #include "chipdrivers.h" |
| 19 | |
Sergii Dmytruk | 39687ac | 2022-07-25 00:23:25 +0300 | [diff] [blame] | 20 | static void decode_range_generic(size_t *start, size_t *len, const struct wp_bits *bits, size_t chip_len, |
| 21 | bool fixed_block_len, bool apply_cmp_to_bp, int coeff_offset) |
Nikolai Artemiev | c9feb1b | 2021-10-21 01:35:13 +1100 | [diff] [blame] | 22 | { |
Sergii Dmytruk | 39687ac | 2022-07-25 00:23:25 +0300 | [diff] [blame] | 23 | const bool cmp = bits->cmp_bit_present && bits->cmp == 1; |
| 24 | |
Nikolai Artemiev | c9feb1b | 2021-10-21 01:35:13 +1100 | [diff] [blame] | 25 | /* Interpret BP bits as an integer */ |
| 26 | size_t i; |
| 27 | size_t bp = 0; |
| 28 | size_t bp_max = 0; |
| 29 | |
| 30 | for (i = 0; i < bits->bp_bit_count; i++) { |
| 31 | bp |= bits->bp[i] << i; |
| 32 | bp_max |= 1 << i; |
| 33 | } |
| 34 | |
Sergii Dmytruk | 39687ac | 2022-07-25 00:23:25 +0300 | [diff] [blame] | 35 | /* |
| 36 | * Most chips: the CMP bit only negates the range. |
| 37 | * |
| 38 | * Some MX chips: the CMP bit negates the BP bits and the range. |
| 39 | * (CMP bit is often the MSB BP bit in such chips.) |
| 40 | */ |
| 41 | if (cmp && apply_cmp_to_bp) |
| 42 | bp ^= bp_max; |
| 43 | |
Nikolai Artemiev | c9feb1b | 2021-10-21 01:35:13 +1100 | [diff] [blame] | 44 | if (bp == 0) { |
| 45 | /* Special case: all BP bits are 0 => no write protection */ |
| 46 | *len = 0; |
| 47 | } else if (bp == bp_max) { |
| 48 | /* Special case: all BP bits are 1 => full write protection */ |
| 49 | *len = chip_len; |
| 50 | } else { |
| 51 | /* |
| 52 | * Usual case: the BP bits encode a coefficient in the form |
Sergii Dmytruk | 39687ac | 2022-07-25 00:23:25 +0300 | [diff] [blame] | 53 | * `coeff = 2 ** (bp - offset)` where `offset == 1`. |
Nikolai Artemiev | c9feb1b | 2021-10-21 01:35:13 +1100 | [diff] [blame] | 54 | * |
| 55 | * The range's length is given by multiplying the coefficient |
| 56 | * by a base unit, usually a 4K sector or a 64K block. |
| 57 | */ |
| 58 | |
Sergii Dmytruk | 39687ac | 2022-07-25 00:23:25 +0300 | [diff] [blame] | 59 | size_t coeff = 1 << (bp - coeff_offset); |
| 60 | size_t max_coeff = 1 << (bp_max - coeff_offset - 1); |
Nikolai Artemiev | c9feb1b | 2021-10-21 01:35:13 +1100 | [diff] [blame] | 61 | |
| 62 | size_t sector_len = 4 * KiB; |
| 63 | size_t default_block_len = 64 * KiB; |
| 64 | |
| 65 | if (bits->sec_bit_present && bits->sec == 1) { |
| 66 | /* |
| 67 | * SEC=1, protect 4K sectors. Flash chips clamp the |
| 68 | * protection length at 32K, probably to avoid overlap |
| 69 | * with the SEC=0 case. |
| 70 | */ |
| 71 | *len = min(sector_len * coeff, default_block_len / 2); |
| 72 | } else { |
| 73 | /* |
| 74 | * SEC=0 or is not present, protect blocks. |
Sergii Dmytruk | 39687ac | 2022-07-25 00:23:25 +0300 | [diff] [blame] | 75 | */ |
| 76 | size_t block_len = default_block_len; |
| 77 | |
| 78 | /* |
Nikolai Artemiev | c9feb1b | 2021-10-21 01:35:13 +1100 | [diff] [blame] | 79 | * With very large chips, the 'block' size can be |
| 80 | * larger than 64K. This occurs when a larger block |
| 81 | * size is needed so that half the chip can be |
| 82 | * protected by the maximum possible coefficient. |
| 83 | */ |
Sergii Dmytruk | 39687ac | 2022-07-25 00:23:25 +0300 | [diff] [blame] | 84 | if (!fixed_block_len) { |
| 85 | size_t min_block_len = chip_len / 2 / max_coeff; |
| 86 | block_len = max(min_block_len, default_block_len); |
| 87 | } |
Nikolai Artemiev | c9feb1b | 2021-10-21 01:35:13 +1100 | [diff] [blame] | 88 | |
| 89 | *len = min(block_len * coeff, chip_len); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* Apply TB bit */ |
| 94 | bool protect_top = bits->tb_bit_present ? (bits->tb == 0) : 1; |
| 95 | |
| 96 | /* Apply CMP bit */ |
Sergii Dmytruk | 39687ac | 2022-07-25 00:23:25 +0300 | [diff] [blame] | 97 | if (cmp) { |
Nikolai Artemiev | c9feb1b | 2021-10-21 01:35:13 +1100 | [diff] [blame] | 98 | *len = chip_len - *len; |
| 99 | protect_top = !protect_top; |
| 100 | } |
| 101 | |
| 102 | /* Calculate start address, ensuring that empty ranges start at 0 */ |
| 103 | if (protect_top && *len > 0) |
| 104 | *start = chip_len - *len; |
| 105 | else |
| 106 | *start = 0; |
| 107 | } |
Sergii Dmytruk | 39687ac | 2022-07-25 00:23:25 +0300 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * Protection range calculation that works with many common SPI flash chips. |
| 111 | */ |
| 112 | void decode_range_spi25(size_t *start, size_t *len, const struct wp_bits *bits, size_t chip_len) |
| 113 | { |
| 114 | decode_range_generic(start, len, bits, chip_len, |
| 115 | /*fixed_block_len=*/false, /*apply_cmp_to_bp=*/false, /*coeff_offset=*/1); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Do not adjust block size to be able to fill half of the chip. |
| 120 | */ |
| 121 | void decode_range_spi25_64k_block(size_t *start, size_t *len, const struct wp_bits *bits, size_t chip_len) |
| 122 | { |
| 123 | decode_range_generic(start, len, bits, chip_len, |
| 124 | /*fixed_block_len=*/true, /*apply_cmp_to_bp=*/false, /*coeff_offset=*/1); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Inverts BP bits when CMP is set and treats all ones in BP bits as a request to protect whole chip regardless |
| 129 | * of the CMP bit. |
| 130 | */ |
| 131 | void decode_range_spi25_bit_cmp(size_t *start, size_t *len, const struct wp_bits *bits, size_t chip_len) |
| 132 | { |
| 133 | decode_range_generic(start, len, bits, chip_len, |
| 134 | /*fixed_block_len=*/false, /*apply_cmp_to_bp=*/true, /*coeff_offset=*/1); |
| 135 | } |
| 136 | |
| 137 | /* |
| 138 | * This multiplies coefficient by 2. To be used with chips which have more BP bits than needed, such that the |
| 139 | * most significant BP bit effectively acts as "protect whole chip" flag. |
| 140 | */ |
| 141 | void decode_range_spi25_2x_block(size_t *start, size_t *len, const struct wp_bits *bits, size_t chip_len) |
| 142 | { |
| 143 | decode_range_generic(start, len, bits, chip_len, |
| 144 | /*fixed_block_len=*/false, /*apply_cmp_to_bp=*/false, /*coeff_offset=*/0); |
| 145 | } |