flash.h 11.7 KB
Newer Older
1
/*
2
 * This file is part of the flashrom project.
3
 *
4 5
 * Copyright (C) 2000 Silicon Integrated System Corporation
 * Copyright (C) 2000 Ronald G. Minnich <rminnich@gmail.com>
6
 * Copyright (C) 2005-2009 coresystems GmbH
7
 * Copyright (C) 2006-2009 Carl-Daniel Hailfinger
8
 *
9 10 11 12
 * 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.
13
 *
14 15 16 17
 * 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.
18
 *
19 20 21
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
22 23
 */

Ronald G. Minnich's avatar
Ronald G. Minnich committed
24 25 26
#ifndef __FLASH_H__
#define __FLASH_H__ 1

27
#include <stdint.h>
28
#include <stddef.h>
Patrick Georgi's avatar
Patrick Georgi committed
29 30 31 32 33
#ifdef _WIN32
#include <windows.h>
#undef min
#undef max
#endif
34

35 36
#define ERROR_PTR ((void*)-1)

37
/* Error codes */
38
#define ERROR_OOM	-100
39 40
#define TIMEOUT_ERROR	-101

41 42
typedef unsigned long chipaddr;

43
int register_shutdown(int (*function) (void *data), void *data);
44 45 46
void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
				  size_t len);
void programmer_unmap_flash_region(void *virt_addr, size_t len);
47
void programmer_delay(int usecs);
48

49 50
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

51
enum chipbustype {
52 53 54 55 56
	BUS_NONE	= 0,
	BUS_PARALLEL	= 1 << 0,
	BUS_LPC		= 1 << 1,
	BUS_FWH		= 1 << 2,
	BUS_SPI		= 1 << 3,
57
	BUS_PROG	= 1 << 4,
58
	BUS_NONSPI	= BUS_PARALLEL | BUS_LPC | BUS_FWH,
59 60
};

61 62 63 64 65 66 67 68 69 70
/*
 * The following write granularities are known:
 * - 1 bit: Each bit can be cleared individually.
 * - 1 byte: A byte can be written once. Further writes to an already written byte cause its contents to be
 *   either undefined or to stay unchanged.
 * - 128 bytes: If less than 128 bytes are written, the rest will be erased. Each write to a 128-byte region
 *   will trigger an automatic erase before anything is written. Very uncommon behaviour.
 * - 256 bytes: If less than 256 bytes are written, the contents of the unwritten bytes are undefined.
 */
enum write_granularity {
71
	write_gran_256bytes = 0, /* We assume 256 byte granularity by default. */
72 73 74 75
	write_gran_1bit,
	write_gran_1byte,
};

76 77 78 79 80 81 82 83
/*
 * How many different contiguous runs of erase blocks with one size each do
 * we have for a given erase function?
 */
#define NUM_ERASEREGIONS 5

/*
 * How many different erase functions do we have per chip?
84
 * Atmel AT25FS010 has 6 different functions.
85
 */
86
#define NUM_ERASEFUNCTIONS 6
87

88 89
#define FEATURE_REGISTERMAP	(1 << 0)
#define FEATURE_BYTEWRITES	(1 << 1)
90 91 92
#define FEATURE_LONG_RESET	(0 << 4)
#define FEATURE_SHORT_RESET	(1 << 4)
#define FEATURE_EITHER_RESET	FEATURE_LONG_RESET
93
#define FEATURE_RESET_MASK	(FEATURE_LONG_RESET | FEATURE_SHORT_RESET)
94 95
#define FEATURE_ADDR_FULL	(0 << 2)
#define FEATURE_ADDR_MASK	(3 << 2)
96 97
#define FEATURE_ADDR_2AA	(1 << 2)
#define FEATURE_ADDR_AAA	(2 << 2)
Michael Karcher's avatar
Michael Karcher committed
98
#define FEATURE_ADDR_SHIFTED	(1 << 5)
99 100
#define FEATURE_WRSR_EWSR	(1 << 6)
#define FEATURE_WRSR_WREN	(1 << 7)
101
#define FEATURE_OTP		(1 << 8)
102
#define FEATURE_QPI		(1 << 9)
103
#define FEATURE_WRSR_EITHER	(FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN)
104

105
struct flashctx;
106
typedef int (erasefunc_t)(struct flashctx *flash, unsigned int addr, unsigned int blocklen);
107

108
struct flashchip {
109
	const char *vendor;
110
	const char *name;
111 112 113

	enum chipbustype bustype;

114 115
	/*
	 * With 32bit manufacture_id and model_id we can cover IDs up to
116 117 118 119 120
	 * (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's
	 * Identification code.
	 */
	uint32_t manufacture_id;
	uint32_t model_id;
121

122
	/* Total chip size in kilobytes */
123
	unsigned int total_size;
124
	/* Chip page size in bytes */
125
	unsigned int page_size;
126
	int feature_bits;
127

128 129
	/*
	 * Indicate if flashrom has been tested with this flash chip and if
130 131 132 133
	 * everything worked correctly.
	 */
	uint32_t tested;

134
	int (*probe) (struct flashctx *flash);
135

136 137 138 139
	/* Delay after "enter/exit ID mode" commands in microseconds.
	 * NB: negative values have special meanings, see TIMING_* below.
	 */
	signed int probe_timing;
140 141

	/*
142 143
	 * Erase blocks and associated erase function. Any chip erase function
	 * is stored as chip-sized virtual block together with said function.
144 145 146
	 * The first one that fits will be chosen. There is currently no way to
	 * influence that behaviour. For testing just comment out the other
	 * elements or set the function pointer to NULL.
147 148 149
	 */
	struct block_eraser {
		struct eraseblock{
150
			unsigned int size; /* Eraseblock size in bytes */
151 152
			unsigned int count; /* Number of contiguous blocks with that size */
		} eraseblocks[NUM_ERASEREGIONS];
Stefan Tauner's avatar
Stefan Tauner committed
153 154
		/* a block_erase function should try to erase one block of size
		 * 'blocklen' at address 'blockaddr' and return 0 on success. */
155
		int (*block_erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen);
156 157
	} block_erasers[NUM_ERASEFUNCTIONS];

158 159 160 161 162
	int (*printlock) (struct flashctx *flash);
	int (*unlock) (struct flashctx *flash);
	int (*write) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
	int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
	struct voltage {
163 164 165
		uint16_t min;
		uint16_t max;
	} voltage;
166
	enum write_granularity gran;
167
};
Ronald G. Minnich's avatar
Ronald G. Minnich committed
168

169
struct flashctx {
170
	struct flashchip *chip;
171
	chipaddr virtual_memory;
172
	/* Some flash devices have an additional register space. */
173
	chipaddr virtual_registers;
174
	struct registered_programmer *pgm;
175 176
};

177 178
#define TEST_UNTESTED	0

Uwe Hermann's avatar
Uwe Hermann committed
179 180 181 182 183 184
#define TEST_OK_PROBE	(1 << 0)
#define TEST_OK_READ	(1 << 1)
#define TEST_OK_ERASE	(1 << 2)
#define TEST_OK_WRITE	(1 << 3)
#define TEST_OK_PR	(TEST_OK_PROBE | TEST_OK_READ)
#define TEST_OK_PRE	(TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE)
185
#define TEST_OK_PRW	(TEST_OK_PROBE | TEST_OK_READ | TEST_OK_WRITE)
Uwe Hermann's avatar
Uwe Hermann committed
186
#define TEST_OK_PREW	(TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE | TEST_OK_WRITE)
187 188
#define TEST_OK_MASK	0x0f

Uwe Hermann's avatar
Uwe Hermann committed
189 190 191 192
#define TEST_BAD_PROBE	(1 << 4)
#define TEST_BAD_READ	(1 << 5)
#define TEST_BAD_ERASE	(1 << 6)
#define TEST_BAD_WRITE	(1 << 7)
193
#define TEST_BAD_REW	(TEST_BAD_READ | TEST_BAD_ERASE | TEST_BAD_WRITE)
Uwe Hermann's avatar
Uwe Hermann committed
194
#define TEST_BAD_PREW	(TEST_BAD_PROBE | TEST_BAD_READ | TEST_BAD_ERASE | TEST_BAD_WRITE)
195 196
#define TEST_BAD_MASK	0xf0

197 198 199 200 201 202 203 204 205 206
/* Timing used in probe routines. ZERO is -2 to differentiate between an unset
 * field and zero delay.
 * 
 * SPI devices will always have zero delay and ignore this field.
 */
#define TIMING_FIXME	-1
/* this is intentionally same value as fixme */
#define TIMING_IGNORED	-1
#define TIMING_ZERO	-2

207
extern const struct flashchip flashchips[];
208

209 210 211 212 213 214 215 216 217
void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len);
uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr);
uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr);
uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr);
void chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);

218 219
/* print.c */
char *flashbuses_to_text(enum chipbustype bustype);
220
int print_supported(void);
221
void print_supported_wiki(void);
222

223
/* flashrom.c */
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
224 225
extern int verbose_screen;
extern int verbose_logfile;
226
extern const char flashrom_version[];
Nico Huber's avatar
Nico Huber committed
227
extern const char *chip_to_probe;
228 229 230
void map_flash_registers(struct flashctx *flash);
int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
int erase_flash(struct flashctx *flash);
231
int probe_flash(struct registered_programmer *pgm, int startchip, struct flashctx *fill_flash, int force);
232
int read_flash_to_file(struct flashctx *flash, const char *filename);
233
int min(int a, int b);
234
int max(int a, int b);
235
void tolower_string(char *str);
Nico Huber's avatar
Nico Huber committed
236
char *extract_param(const char *const *haystack, const char *needle, const char *delim);
237
int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len);
238
int need_erase(uint8_t *have, uint8_t *want, unsigned int len, enum write_granularity gran);
239
char *strcat_realloc(char *dest, const char *src);
240
void print_version(void);
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
241
void print_buildinfo(void);
242
void print_banner(void);
243
void list_programmers_linebreak(int startcol, int cols, int paren);
244
int selfcheck(void);
245
int doit(struct flashctx *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it);
246 247
int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename);
int write_buf_to_file(unsigned char *buf, unsigned long size, const char *filename);
248

Stefan Tauner's avatar
Stefan Tauner committed
249 250 251 252 253
enum test_state {
	OK = 0,
	NT = 1,	/* Not tested */
	BAD
};
254

255
/* Something happened that shouldn't happen, but we can go on. */
256 257
#define ERROR_NONFATAL 0x100

258 259
/* Something happened that shouldn't happen, we'll abort. */
#define ERROR_FATAL -0xee
260 261 262 263 264 265 266
#define ERROR_FLASHROM_BUG -200
/* We reached one of the hardcoded limits of flashrom. This can be fixed by
 * increasing the limit of a compile-time allocation or by switching to dynamic
 * allocation.
 * Note: If this warning is triggered, check first for runaway registrations.
 */
#define ERROR_FLASHROM_LIMIT -201
267

268
/* cli_output.c */
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
269 270 271 272 273
#ifndef STANDALONE
int open_logfile(const char * const filename);
int close_logfile(void);
void start_logging(void);
#endif
274 275
enum msglevel {
	MSG_ERROR	= 0,
Stefan Tauner's avatar
Stefan Tauner committed
276 277 278 279 280
	MSG_WARN	= 1,
	MSG_INFO	= 2,
	MSG_DEBUG	= 3,
	MSG_DEBUG2	= 4,
	MSG_SPEW	= 5,
281
};
282
/* Let gcc and clang check for correct printf-style format strings. */
283
int print(enum msglevel level, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
284 285 286
#define msg_gerr(...)	print(MSG_ERROR, __VA_ARGS__)	/* general errors */
#define msg_perr(...)	print(MSG_ERROR, __VA_ARGS__)	/* programmer errors */
#define msg_cerr(...)	print(MSG_ERROR, __VA_ARGS__)	/* chip errors */
Stefan Tauner's avatar
Stefan Tauner committed
287 288 289
#define msg_gwarn(...)	print(MSG_WARN, __VA_ARGS__)	/* general warnings */
#define msg_pwarn(...)	print(MSG_WARN, __VA_ARGS__)	/* programmer warnings */
#define msg_cwarn(...)	print(MSG_WARN, __VA_ARGS__)	/* chip warnings */
290 291 292 293 294 295
#define msg_ginfo(...)	print(MSG_INFO, __VA_ARGS__)	/* general info */
#define msg_pinfo(...)	print(MSG_INFO, __VA_ARGS__)	/* programmer info */
#define msg_cinfo(...)	print(MSG_INFO, __VA_ARGS__)	/* chip info */
#define msg_gdbg(...)	print(MSG_DEBUG, __VA_ARGS__)	/* general debug */
#define msg_pdbg(...)	print(MSG_DEBUG, __VA_ARGS__)	/* programmer debug */
#define msg_cdbg(...)	print(MSG_DEBUG, __VA_ARGS__)	/* chip debug */
Stefan Tauner's avatar
Stefan Tauner committed
296 297 298
#define msg_gdbg2(...)	print(MSG_DEBUG2, __VA_ARGS__)	/* general debug2 */
#define msg_pdbg2(...)	print(MSG_DEBUG2, __VA_ARGS__)	/* programmer debug2 */
#define msg_cdbg2(...)	print(MSG_DEBUG2, __VA_ARGS__)	/* chip debug2 */
299 300 301
#define msg_gspew(...)	print(MSG_SPEW, __VA_ARGS__)	/* general debug spew  */
#define msg_pspew(...)	print(MSG_SPEW, __VA_ARGS__)	/* programmer debug spew  */
#define msg_cspew(...)	print(MSG_SPEW, __VA_ARGS__)	/* chip debug spew  */
302

303
/* layout.c */
304 305
int register_include_arg(char *name);
int process_include_args(void);
306
int read_romlayout(char *name);
307
int handle_romentries(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents);
308

309
/* spi.c */
310 311 312 313 314 315
struct spi_command {
	unsigned int writecnt;
	unsigned int readcnt;
	const unsigned char *writearr;
	unsigned char *readarr;
};
316 317 318
int spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr);
int spi_send_multicommand(struct flashctx *flash, struct spi_command *cmds);
uint32_t spi_get_valid_read_addr(struct flashctx *flash);
319

320
enum chipbustype get_buses_supported(void);
321
#endif				/* !__FLASH_H__ */