blob: b26c121c54c9f24f857ea8264c61ef4fc08bf6a1 [file] [log] [blame]
Edward O'Callaghand3b6acf2020-09-21 17:10:21 +10001/*
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 <stdlib.h>
19#include <string.h>
20#include <strings.h>
21
22#include "flash.h"
23#include "flashchips.h"
24#include "chipdrivers.h"
25#include "spi.h"
26#include "writeprotect.h"
27
28/*
29 * The following procedures rely on look-up tables to match the user-specified
30 * range with the chip's supported ranges. This turned out to be the most
31 * elegant approach since diferent flash chips use different levels of
32 * granularity and methods to determine protected ranges. In other words,
33 * be stupid and simple since clever arithmetic will not work for many chips.
34 */
35
36struct wp_range {
37 unsigned int start; /* starting address */
38 unsigned int len; /* len */
39};
40
41enum bit_state {
42 OFF = 0,
43 ON = 1,
44 X = -1 /* don't care. Must be bigger than max # of bp. */
45};
46
47/*
48 * Generic write-protection schema for 25-series SPI flash chips. This assumes
49 * there is a status register that contains one or more consecutive bits which
50 * determine which address range is protected.
51 */
52
53struct status_register_layout {
54 int bp0_pos; /* position of BP0 */
55 int bp_bits; /* number of block protect bits */
56 int srp_pos; /* position of status register protect enable bit */
57};
58
59/*
60 * The following ranges and functions are useful for representing the
61 * writeprotect schema in which there are typically 5 bits of
62 * relevant information stored in status register 1:
63 * m.sec: This bit indicates the units (sectors vs. blocks)
64 * m.tb: The top-bottom bit indicates if the affected range is at the top of
65 * the flash memory's address space or at the bottom.
66 * bp: Bitmask representing the number of affected sectors/blocks.
67 */
68struct wp_range_descriptor {
69 struct modifier_bits m;
70 unsigned int bp; /* block protect bitfield */
71 struct wp_range range;
72};
73
74struct wp_context {
75 struct status_register_layout sr1; /* status register 1 */
76 struct wp_range_descriptor *descrs;
77
78 /*
79 * Some chips store modifier bits in one or more special control
80 * registers instead of the status register like many older SPI NOR
81 * flash chips did. get_modifier_bits() and set_modifier_bits() will do
82 * any chip-specific operations necessary to get/set these bit values.
83 */
84 int (*get_modifier_bits)(const struct flashctx *flash,
85 struct modifier_bits *m);
86 int (*set_modifier_bits)(const struct flashctx *flash,
87 struct modifier_bits *m);
88};
89
90/*
91 * Mask to extract write-protect enable and range bits
92 * Status register 1:
93 * SRP0: bit 7
94 * range(BP2-BP0): bit 4-2
95 * range(BP3-BP0): bit 5-2 (large chips)
96 * Status register 2:
97 * SRP1: bit 1
98 */
99#define MASK_WP_AREA (0x9C)
100#define MASK_WP_AREA_LARGE (0x9C)
101#define MASK_WP2_AREA (0x01)
102
103static uint8_t do_read_status(const struct flashctx *flash)
104{
105 if (flash->chip->read_status)
106 return flash->chip->read_status(flash);
107 else
108 return spi_read_status_register(flash);
109}
110
111static int do_write_status(const struct flashctx *flash, int status)
112{
113 if (flash->chip->write_status)
114 return flash->chip->write_status(flash, status);
115 else
116 return spi_write_status_register(flash, status);
117}
118
119enum wp_mode get_wp_mode(const char *mode_str)
120{
121 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
122
123 if (!strcasecmp(mode_str, "hardware"))
124 wp_mode = WP_MODE_HARDWARE;
125 else if (!strcasecmp(mode_str, "power_cycle"))
126 wp_mode = WP_MODE_POWER_CYCLE;
127 else if (!strcasecmp(mode_str, "permanent"))
128 wp_mode = WP_MODE_PERMANENT;
129
130 return wp_mode;
131}
132
133/* Given a flash chip, this function returns its writeprotect info. */
134static int generic_range_table(const struct flashctx *flash,
135 struct wp_context **wp,
136 int *num_entries)
137{
138 *wp = NULL;
139 *num_entries = 0;
140
141 switch (flash->chip->manufacture_id) {
142 default:
143 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
144 __func__, flash->chip->manufacture_id);
145 return -1;
146 }
147
148 return 0;
149}
150
151static uint8_t generic_get_bp_mask(struct wp_context *wp)
152{
153 return ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) ^ \
154 ((1 << wp->sr1.bp0_pos) - 1);
155}
156
157static uint8_t generic_get_status_check_mask(struct wp_context *wp)
158{
159 return generic_get_bp_mask(wp) | 1 << wp->sr1.srp_pos;
160}
161
162/* Given a [start, len], this function finds a block protect bit combination
163 * (if possible) and sets the corresponding bits in "status". Remaining bits
164 * are preserved. */
165static int generic_range_to_status(const struct flashctx *flash,
166 unsigned int start, unsigned int len,
167 uint8_t *status, uint8_t *check_mask)
168{
169 struct wp_context *wp;
170 struct wp_range_descriptor *r;
171 int i, range_found = 0, num_entries;
172 uint8_t bp_mask;
173
174 if (generic_range_table(flash, &wp, &num_entries))
175 return -1;
176
177 bp_mask = generic_get_bp_mask(wp);
178
179 for (i = 0, r = &wp->descrs[0]; i < num_entries; i++, r++) {
180 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
181 start, len, r->range.start, r->range.len);
182 if ((start == r->range.start) && (len == r->range.len)) {
183 *status &= ~(bp_mask);
184 *status |= r->bp << (wp->sr1.bp0_pos);
185
186 if (wp->set_modifier_bits) {
187 if (wp->set_modifier_bits(flash, &r->m) < 0) {
188 msg_cerr("error setting modifier bits for range.\n");
189 return -1;
190 }
191 }
192
193 range_found = 1;
194 break;
195 }
196 }
197
198 if (!range_found) {
199 msg_cerr("%s: matching range not found\n", __func__);
200 return -1;
201 }
202
203 *check_mask = generic_get_status_check_mask(wp);
204 return 0;
205}
206
207static int generic_status_to_range(const struct flashctx *flash,
208 const uint8_t sr1, unsigned int *start, unsigned int *len)
209{
210 struct wp_context *wp;
211 struct wp_range_descriptor *r;
212 int num_entries, i, status_found = 0;
213 uint8_t sr1_bp;
214 struct modifier_bits m;
215
216 if (generic_range_table(flash, &wp, &num_entries))
217 return -1;
218
219 /* modifier bits may be compared more than once, so get them here */
220 if (wp->get_modifier_bits && wp->get_modifier_bits(flash, &m) < 0)
221 return -1;
222
223 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
224
225 for (i = 0, r = &wp->descrs[0]; i < num_entries; i++, r++) {
226 if (wp->get_modifier_bits) {
227 if (memcmp(&m, &r->m, sizeof(m)))
228 continue;
229 }
230 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
231 if (sr1_bp == r->bp) {
232 *start = r->range.start;
233 *len = r->range.len;
234 status_found = 1;
235 break;
236 }
237 }
238
239 if (!status_found) {
240 msg_cerr("matching status not found\n");
241 return -1;
242 }
243
244 return 0;
245}
246
247/* Given a [start, len], this function calls generic_range_to_status() to
248 * convert it to flash-chip-specific range bits, then sets into status register.
249 */
250static int generic_set_range(const struct flashctx *flash,
251 unsigned int start, unsigned int len)
252{
253 uint8_t status, expected, check_mask;
254
255 status = do_read_status(flash);
256 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
257
258 expected = status; /* preserve non-bp bits */
259 if (generic_range_to_status(flash, start, len, &expected, &check_mask))
260 return -1;
261
262 do_write_status(flash, expected);
263
264 status = do_read_status(flash);
265 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
266 if ((status & check_mask) != (expected & check_mask)) {
267 msg_cerr("expected=0x%02x, but actual=0x%02x. check mask=0x%02x\n",
268 expected, status, check_mask);
269 return 1;
270 }
271 return 0;
272}
273
274/* Set/clear the status regsiter write protect bit in SR1. */
275static int generic_set_srp0(const struct flashctx *flash, int enable)
276{
277 uint8_t status, expected, check_mask;
278 struct wp_context *wp;
279 int num_entries;
280
281 if (generic_range_table(flash, &wp, &num_entries))
282 return -1;
283
284 expected = do_read_status(flash);
285 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
286
287 if (enable)
288 expected |= 1 << wp->sr1.srp_pos;
289 else
290 expected &= ~(1 << wp->sr1.srp_pos);
291
292 do_write_status(flash, expected);
293
294 status = do_read_status(flash);
295 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
296
297 check_mask = generic_get_status_check_mask(wp);
298 msg_cdbg("%s: check mask: 0x%02x\n", __func__, check_mask);
299 if ((status & check_mask) != (expected & check_mask)) {
300 msg_cerr("expected=0x%02x, but actual=0x%02x. check mask=0x%02x\n",
301 expected, status, check_mask);
302 return -1;
303 }
304
305 return 0;
306}
307
308static int generic_enable_writeprotect(const struct flashctx *flash,
309 enum wp_mode wp_mode)
310{
311 int ret;
312
313 if (wp_mode != WP_MODE_HARDWARE) {
314 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
315 return 1;
316 }
317
318 ret = generic_set_srp0(flash, 1);
319 if (ret)
320 msg_cerr("%s(): error=%d.\n", __func__, ret);
321
322 return ret;
323}
324
325static int generic_disable_writeprotect(const struct flashctx *flash)
326{
327 int ret;
328
329 ret = generic_set_srp0(flash, 0);
330 if (ret)
331 msg_cerr("%s(): error=%d.\n", __func__, ret);
332
333 return ret;
334}
335
336static int generic_list_ranges(const struct flashctx *flash)
337{
338 struct wp_context *wp;
339 struct wp_range_descriptor *r;
340 int i, num_entries;
341
342 if (generic_range_table(flash, &wp, &num_entries))
343 return -1;
344
345 r = &wp->descrs[0];
346 for (i = 0; i < num_entries; i++) {
347 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
348 r->range.start, r->range.len);
349 r++;
350 }
351
352 return 0;
353}
354
355static int wp_context_status(const struct flashctx *flash)
356{
357 uint8_t sr1;
358 unsigned int start, len;
359 int ret = 0;
360 struct wp_context *wp;
361 int num_entries, wp_en;
362
363 if (generic_range_table(flash, &wp, &num_entries))
364 return -1;
365
366 sr1 = do_read_status(flash);
367 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
368
369 msg_cinfo("WP: status: 0x%04x\n", sr1);
370 msg_cinfo("WP: status.srp0: %x\n", wp_en);
371 /* FIXME: SRP1 is not really generic, but we probably should print
372 * it anyway to have consistent output. #legacycruft */
373 msg_cinfo("WP: status.srp1: %x\n", 0);
374 msg_cinfo("WP: write protect is %s.\n",
375 wp_en ? "enabled" : "disabled");
376
377 msg_cinfo("WP: write protect range: ");
378 if (generic_status_to_range(flash, sr1, &start, &len)) {
379 msg_cinfo("(cannot resolve the range)\n");
380 ret = -1;
381 } else {
382 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
383 }
384
385 return ret;
386}
387
388struct wp wp_generic = {
389 .list_ranges = generic_list_ranges,
390 .set_range = generic_set_range,
391 .enable = generic_enable_writeprotect,
392 .disable = generic_disable_writeprotect,
393 .wp_status = wp_context_status,
394};