summaryrefslogtreecommitdiffstats
path: root/writeprotect.c
blob: b26c121c54c9f24f857ea8264c61ef4fc08bf6a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
 * This file is part of the flashrom project.
 *
 * Copyright (C) 2010 Google Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "flash.h"
#include "flashchips.h"
#include "chipdrivers.h"
#include "spi.h"
#include "writeprotect.h"

/*
 * The following procedures rely on look-up tables to match the user-specified
 * range with the chip's supported ranges. This turned out to be the most
 * elegant approach since diferent flash chips use different levels of
 * granularity and methods to determine protected ranges. In other words,
 * be stupid and simple since clever arithmetic will not work for many chips.
 */

struct wp_range {
	unsigned int start;	/* starting address */
	unsigned int len;	/* len */
};

enum bit_state {
	OFF	= 0,
	ON	= 1,
	X	= -1	/* don't care. Must be bigger than max # of bp. */
};

/*
 * Generic write-protection schema for 25-series SPI flash chips. This assumes
 * there is a status register that contains one or more consecutive bits which
 * determine which address range is protected.
 */

struct status_register_layout {
	int bp0_pos;	/* position of BP0 */
	int bp_bits;	/* number of block protect bits */
	int srp_pos;	/* position of status register protect enable bit */
};

/*
 * The following ranges and functions are useful for representing the
 * writeprotect schema in which there are typically 5 bits of
 * relevant information stored in status register 1:
 * m.sec: This bit indicates the units (sectors vs. blocks)
 * m.tb: The top-bottom bit indicates if the affected range is at the top of
 *       the flash memory's address space or at the bottom.
 * bp: Bitmask representing the number of affected sectors/blocks.
 */
struct wp_range_descriptor {
	struct modifier_bits m;
	unsigned int bp;		/* block protect bitfield */
	struct wp_range range;
};

struct wp_context {
	struct status_register_layout sr1;	/* status register 1 */
	struct wp_range_descriptor *descrs;

	/*
	 * Some chips store modifier bits in one or more special control
	 * registers instead of the status register like many older SPI NOR
	 * flash chips did. get_modifier_bits() and set_modifier_bits() will do
	 * any chip-specific operations necessary to get/set these bit values.
	 */
	int (*get_modifier_bits)(const struct flashctx *flash,
			struct modifier_bits *m);
	int (*set_modifier_bits)(const struct flashctx *flash,
			struct modifier_bits *m);
};

/*
 * Mask to extract write-protect enable and range bits
 *   Status register 1:
 *     SRP0:           bit 7
 *     range(BP2-BP0): bit 4-2
 *     range(BP3-BP0): bit 5-2 (large chips)
 *   Status register 2:
 *     SRP1:           bit 1
 */
#define MASK_WP_AREA (0x9C)
#define MASK_WP_AREA_LARGE (0x9C)
#define MASK_WP2_AREA (0x01)

static uint8_t do_read_status(const struct flashctx *flash)
{
	if (flash->chip->read_status)
		return flash->chip->read_status(flash);
	else
		return spi_read_status_register(flash);
}

static int do_write_status(const struct flashctx *flash, int status)
{
	if (flash->chip->write_status)
		return flash->chip->write_status(flash, status);
	else
		return spi_write_status_register(flash, status);
}

enum wp_mode get_wp_mode(const char *mode_str)
{
	enum wp_mode wp_mode = WP_MODE_UNKNOWN;

	if (!strcasecmp(mode_str, "hardware"))
		wp_mode = WP_MODE_HARDWARE;
	else if (!strcasecmp(mode_str, "power_cycle"))
		wp_mode = WP_MODE_POWER_CYCLE;
	else if (!strcasecmp(mode_str, "permanent"))
		wp_mode = WP_MODE_PERMANENT;

	return wp_mode;
}

/* Given a flash chip, this function returns its writeprotect info. */
static int generic_range_table(const struct flashctx *flash,
                           struct wp_context **wp,
                           int *num_entries)
{
	*wp = NULL;
	*num_entries = 0;

	switch (flash->chip->manufacture_id) {
	default:
		msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
		         __func__, flash->chip->manufacture_id);
		return -1;
	}

	return 0;
}

static uint8_t generic_get_bp_mask(struct wp_context *wp)
{
	return ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) ^ \
		  ((1 << wp->sr1.bp0_pos) - 1);
}

static uint8_t generic_get_status_check_mask(struct wp_context *wp)
{
	return generic_get_bp_mask(wp) | 1 << wp->sr1.srp_pos;
}

/* Given a [start, len], this function finds a block protect bit combination
 * (if possible) and sets the corresponding bits in "status". Remaining bits
 * are preserved. */
static int generic_range_to_status(const struct flashctx *flash,
                        unsigned int start, unsigned int len,
                        uint8_t *status, uint8_t *check_mask)
{
	struct wp_context *wp;
	struct wp_range_descriptor *r;
	int i, range_found = 0, num_entries;
	uint8_t bp_mask;

	if (generic_range_table(flash, &wp, &num_entries))
		return -1;

	bp_mask = generic_get_bp_mask(wp);

	for (i = 0, r = &wp->descrs[0]; i < num_entries; i++, r++) {
		msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
			  start, len, r->range.start, r->range.len);
		if ((start == r->range.start) && (len == r->range.len)) {
			*status &= ~(bp_mask);
			*status |= r->bp << (wp->sr1.bp0_pos);

			if (wp->set_modifier_bits) {
				if (wp->set_modifier_bits(flash, &r->m) < 0) {
					msg_cerr("error setting modifier bits for range.\n");
					return -1;
				}
			}

			range_found = 1;
			break;
		}
	}

	if (!range_found) {
		msg_cerr("%s: matching range not found\n", __func__);
		return -1;
	}

	*check_mask = generic_get_status_check_mask(wp);
	return 0;
}

static int generic_status_to_range(const struct flashctx *flash,
		const uint8_t sr1, unsigned int *start, unsigned int *len)
{
	struct wp_context *wp;
	struct wp_range_descriptor *r;
	int num_entries, i, status_found = 0;
	uint8_t sr1_bp;
	struct modifier_bits m;

	if (generic_range_table(flash, &wp, &num_entries))
		return -1;

	/* modifier bits may be compared more than once, so get them here */
	if (wp->get_modifier_bits && wp->get_modifier_bits(flash, &m) < 0)
			return -1;

	sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);

	for (i = 0, r = &wp->descrs[0]; i < num_entries; i++, r++) {
		if (wp->get_modifier_bits) {
			if (memcmp(&m, &r->m, sizeof(m)))
				continue;
		}
		msg_cspew("comparing  0x%02x 0x%02x\n", sr1_bp, r->bp);
		if (sr1_bp == r->bp) {
			*start = r->range.start;
			*len = r->range.len;
			status_found = 1;
			break;
		}
	}

	if (!status_found) {
		msg_cerr("matching status not found\n");
		return -1;
	}

	return 0;
}

/* Given a [start, len], this function calls generic_range_to_status() to
 * convert it to flash-chip-specific range bits, then sets into status register.
 */
static int generic_set_range(const struct flashctx *flash,
                         unsigned int start, unsigned int len)
{
	uint8_t status, expected, check_mask;

	status = do_read_status(flash);
	msg_cdbg("%s: old status: 0x%02x\n", __func__, status);

	expected = status;	/* preserve non-bp bits */
	if (generic_range_to_status(flash, start, len, &expected, &check_mask))
		return -1;

	do_write_status(flash, expected);

	status = do_read_status(flash);
	msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
	if ((status & check_mask) != (expected & check_mask)) {
		msg_cerr("expected=0x%02x, but actual=0x%02x. check mask=0x%02x\n",
		          expected, status, check_mask);
		return 1;
	}
	return 0;
}

/* Set/clear the status regsiter write protect bit in SR1. */
static int generic_set_srp0(const struct flashctx *flash, int enable)
{
	uint8_t status, expected, check_mask;
	struct wp_context *wp;
	int num_entries;

	if (generic_range_table(flash, &wp, &num_entries))
		return -1;

	expected = do_read_status(flash);
	msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);

	if (enable)
		expected |= 1 << wp->sr1.srp_pos;
	else
		expected &= ~(1 << wp->sr1.srp_pos);

	do_write_status(flash, expected);

	status = do_read_status(flash);
	msg_cdbg("%s: new status: 0x%02x\n", __func__, status);

	check_mask = generic_get_status_check_mask(wp);
	msg_cdbg("%s: check mask: 0x%02x\n", __func__, check_mask);
	if ((status & check_mask) != (expected & check_mask)) {
		msg_cerr("expected=0x%02x, but actual=0x%02x. check mask=0x%02x\n",
		          expected, status, check_mask);
		return -1;
	}

	return 0;
}

static int generic_enable_writeprotect(const struct flashctx *flash,
		enum wp_mode wp_mode)
{
	int ret;

	if (wp_mode != WP_MODE_HARDWARE) {
		msg_cerr("%s(): unsupported write-protect mode\n", __func__);
		return 1;
	}

	ret = generic_set_srp0(flash, 1);
	if (ret)
		msg_cerr("%s(): error=%d.\n", __func__, ret);

	return ret;
}

static int generic_disable_writeprotect(const struct flashctx *flash)
{
	int ret;

	ret = generic_set_srp0(flash, 0);
	if (ret)
		msg_cerr("%s(): error=%d.\n", __func__, ret);

	return ret;
}

static int generic_list_ranges(const struct flashctx *flash)
{
	struct wp_context *wp;
	struct wp_range_descriptor *r;
	int i, num_entries;

	if (generic_range_table(flash, &wp, &num_entries))
		return -1;

	r = &wp->descrs[0];
	for (i = 0; i < num_entries; i++) {
		msg_cinfo("start: 0x%06x, length: 0x%06x\n",
		          r->range.start, r->range.len);
		r++;
	}

	return 0;
}

static int wp_context_status(const struct flashctx *flash)
{
	uint8_t sr1;
	unsigned int start, len;
	int ret = 0;
	struct wp_context *wp;
	int num_entries, wp_en;

	if (generic_range_table(flash, &wp, &num_entries))
		return -1;

	sr1 = do_read_status(flash);
	wp_en = (sr1 >> wp->sr1.srp_pos) & 1;

	msg_cinfo("WP: status: 0x%04x\n", sr1);
	msg_cinfo("WP: status.srp0: %x\n", wp_en);
	/* FIXME: SRP1 is not really generic, but we probably should print
	 * it anyway to have consistent output. #legacycruft */
	msg_cinfo("WP: status.srp1: %x\n", 0);
	msg_cinfo("WP: write protect is %s.\n",
		          wp_en ? "enabled" : "disabled");

	msg_cinfo("WP: write protect range: ");
	if (generic_status_to_range(flash, sr1, &start, &len)) {
		msg_cinfo("(cannot resolve the range)\n");
		ret = -1;
	} else {
		msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
	}

	return ret;
}

struct wp wp_generic = {
	.list_ranges	= generic_list_ranges,
	.set_range	= generic_set_range,
	.enable		= generic_enable_writeprotect,
	.disable	= generic_disable_writeprotect,
	.wp_status	= wp_context_status,
};