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