flash.h 14.2 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 <inttypes.h>
28
#include <stdio.h>
29
#include <stdint.h>
30
#include <stddef.h>
31
#include <stdbool.h>
Patrick Georgi's avatar
Patrick Georgi committed
32 33 34 35 36
#ifdef _WIN32
#include <windows.h>
#undef min
#undef max
#endif
37

38 39
#define ERROR_PTR ((void*)-1)

40
/* Error codes */
41
#define ERROR_OOM	-100
42 43
#define TIMEOUT_ERROR	-101

44 45
/* TODO: check using code for correct usage of types */
typedef uintptr_t chipaddr;
46
#define PRIxPTR_WIDTH ((int)(sizeof(uintptr_t)*2))
47

48 49 50
/* Types and macros regarding the maximum flash space size supported by generic code. */
typedef uint32_t chipoff_t; /* Able to store any addressable offset within a supported flash memory. */
typedef uint32_t chipsize_t; /* Able to store the number of bytes of any supported flash memory. */
51 52 53
#define FL_MAX_CHIPOFF_BITS (24)
#define FL_MAX_CHIPOFF ((chipoff_t)(1ULL<<FL_MAX_CHIPOFF_BITS)-1)
#define PRIxCHIPOFF "06"PRIx32
54 55
#define PRIuCHIPSIZE PRIu32

56
int register_shutdown(int (*function) (void *data), void *data);
57
int shutdown_free(void *data);
58
void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len);
59
void programmer_unmap_flash_region(void *virt_addr, size_t len);
Stefan Tauner's avatar
Stefan Tauner committed
60
void programmer_delay(unsigned int usecs);
61

62 63
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

64
enum chipbustype {
65 66 67 68 69
	BUS_NONE	= 0,
	BUS_PARALLEL	= 1 << 0,
	BUS_LPC		= 1 << 1,
	BUS_FWH		= 1 << 2,
	BUS_SPI		= 1 << 3,
70
	BUS_PROG	= 1 << 4,
71
	BUS_NONSPI	= BUS_PARALLEL | BUS_LPC | BUS_FWH,
72 73
};

74
/*
75 76 77 78
 * The following enum defines possible write granularities of flash chips. These tend to reflect the properties
 * of the actual hardware not necesserily the write function(s) defined by the respective struct flashchip.
 * The latter might (and should) be more precisely specified, e.g. they might bail out early if their execution
 * would result in undefined chip contents.
79 80
 */
enum write_granularity {
81 82 83 84 85 86 87 88 89 90
	/* We assume 256 byte granularity by default. */
	write_gran_256bytes = 0,/* If less than 256 bytes are written, the unwritten bytes are undefined. */
	write_gran_1bit,	/* Each bit can be cleared individually. */
	write_gran_1byte,	/* A byte can be written once. Further writes to an already written byte cause
				 * its contents to be either undefined or to stay unchanged. */
	write_gran_264bytes,	/* If less than 264 bytes are written, the unwritten bytes are undefined. */
	write_gran_512bytes,	/* If less than 512 bytes are written, the unwritten bytes are undefined. */
	write_gran_528bytes,	/* If less than 528 bytes are written, the unwritten bytes are undefined. */
	write_gran_1024bytes,	/* If less than 1024 bytes are written, the unwritten bytes are undefined. */
	write_gran_1056bytes,	/* If less than 1056 bytes are written, the unwritten bytes are undefined. */
91
	write_gran_1byte_implicit_erase, /* EEPROMs and other chips with implicit erase and 1-byte writes. */
92 93
};

94 95 96 97 98 99 100 101
/*
 * 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?
102
 * Atmel AT25FS010 has 6 different functions.
103
 */
104
#define NUM_ERASEFUNCTIONS 6
105

106
/* Feature bits used for non-SPI only */
107 108
#define FEATURE_REGISTERMAP	(1 << 0)
#define FEATURE_BYTEWRITES	(1 << 1)
109 110 111
#define FEATURE_LONG_RESET	(0 << 4)
#define FEATURE_SHORT_RESET	(1 << 4)
#define FEATURE_EITHER_RESET	FEATURE_LONG_RESET
112
#define FEATURE_RESET_MASK	(FEATURE_LONG_RESET | FEATURE_SHORT_RESET)
113 114
#define FEATURE_ADDR_FULL	(0 << 2)
#define FEATURE_ADDR_MASK	(3 << 2)
115 116
#define FEATURE_ADDR_2AA	(1 << 2)
#define FEATURE_ADDR_AAA	(2 << 2)
Michael Karcher's avatar
Michael Karcher committed
117
#define FEATURE_ADDR_SHIFTED	(1 << 5)
118
/* Feature bits used for SPI only */
119 120
#define FEATURE_WRSR_EWSR	(1 << 6)
#define FEATURE_WRSR_WREN	(1 << 7)
121
#define FEATURE_WRSR_EITHER	(FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN)
122
#define FEATURE_OTP		(1 << 8)
123
#define FEATURE_QPI		(1 << 9)
124

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
enum test_state {
	OK = 0,
	NT = 1,	/* Not tested */
	BAD,	/* Known to not work */
	DEP,	/* Support depends on configuration (e.g. Intel flash descriptor) */
	NA,	/* Not applicable (e.g. write support on ROM chips) */
};

#define TEST_UNTESTED	(struct tested){ .probe = NT, .read = NT, .erase = NT, .write = NT }

#define TEST_OK_PROBE	(struct tested){ .probe = OK, .read = NT, .erase = NT, .write = NT }
#define TEST_OK_PR	(struct tested){ .probe = OK, .read = OK, .erase = NT, .write = NT }
#define TEST_OK_PRE	(struct tested){ .probe = OK, .read = OK, .erase = OK, .write = NT }
#define TEST_OK_PREW	(struct tested){ .probe = OK, .read = OK, .erase = OK, .write = OK }

#define TEST_BAD_PROBE	(struct tested){ .probe = BAD, .read = NT, .erase = NT, .write = NT }
#define TEST_BAD_PR	(struct tested){ .probe = BAD, .read = BAD, .erase = NT, .write = NT }
#define TEST_BAD_PRE	(struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = NT }
#define TEST_BAD_PREW	(struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD }

145
struct flashctx;
146
typedef int (erasefunc_t)(struct flashctx *flash, unsigned int addr, unsigned int blocklen);
147

148
struct flashchip {
149
	const char *vendor;
150
	const char *name;
151 152 153

	enum chipbustype bustype;

154 155
	/*
	 * With 32bit manufacture_id and model_id we can cover IDs up to
156 157 158 159 160
	 * (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's
	 * Identification code.
	 */
	uint32_t manufacture_id;
	uint32_t model_id;
161

162
	/* Total chip size in kilobytes */
163
	unsigned int total_size;
164
	/* Chip page size in bytes */
165
	unsigned int page_size;
166
	int feature_bits;
167

168 169 170 171 172 173 174
	/* Indicate how well flashrom supports different operations of this flash chip. */
	struct tested {
		enum test_state probe;
		enum test_state read;
		enum test_state erase;
		enum test_state write;
	} tested;
175

176
	int (*probe) (struct flashctx *flash);
177

178 179 180 181
	/* Delay after "enter/exit ID mode" commands in microseconds.
	 * NB: negative values have special meanings, see TIMING_* below.
	 */
	signed int probe_timing;
182 183

	/*
184 185
	 * Erase blocks and associated erase function. Any chip erase function
	 * is stored as chip-sized virtual block together with said function.
186 187 188
	 * 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.
189 190
	 */
	struct block_eraser {
191
		struct eraseblock {
192
			unsigned int size; /* Eraseblock size in bytes */
193 194
			unsigned int count; /* Number of contiguous blocks with that size */
		} eraseblocks[NUM_ERASEREGIONS];
Stefan Tauner's avatar
Stefan Tauner committed
195 196
		/* a block_erase function should try to erase one block of size
		 * 'blocklen' at address 'blockaddr' and return 0 on success. */
197
		int (*block_erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen);
198 199
	} block_erasers[NUM_ERASEFUNCTIONS];

200 201
	int (*printlock) (struct flashctx *flash);
	int (*unlock) (struct flashctx *flash);
202
	int (*write) (struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
203 204
	int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
	struct voltage {
205 206 207
		uint16_t min;
		uint16_t max;
	} voltage;
208
	enum write_granularity gran;
209
};
Ronald G. Minnich's avatar
Ronald G. Minnich committed
210

211
struct flashctx {
212
	struct flashchip *chip;
213 214 215 216 217
	/* FIXME: The memory mappings should be saved in a more structured way. */
	/* The physical_* fields store the respective addresses in the physical address space of the CPU. */
	uintptr_t physical_memory;
	/* The virtual_* fields store where the respective physical address is mapped into flashrom's address
	 * space. A value equivalent to (chipaddr)ERROR_PTR indicates an invalid mapping (or none at all). */
218
	chipaddr virtual_memory;
219 220
	/* Some flash devices have an additional register space; semantics are like above. */
	uintptr_t physical_registers;
221
	chipaddr virtual_registers;
222
	struct registered_master *mst;
223 224
};

225 226 227 228 229 230 231 232 233 234
/* 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

235
extern const struct flashchip flashchips[];
236
extern const unsigned int flashchips_size;
237

238 239 240
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);
241
void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
242 243 244 245 246
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);

247
/* print.c */
248
int print_supported(void);
249
void print_supported_wiki(void);
250

Stefan Tauner's avatar
Stefan Tauner committed
251 252 253 254 255 256 257 258
/* helpers.c */
uint32_t address_to_bits(uint32_t addr);
int bitcount(unsigned long a);
int max(int a, int b);
int min(int a, int b);
char *strcat_realloc(char *dest, const char *src);
void tolower_string(char *str);

259
/* flashrom.c */
260
extern const char flashrom_version[];
Nico Huber's avatar
Nico Huber committed
261
extern const char *chip_to_probe;
262 263
int map_flash(struct flashctx *flash);
void unmap_flash(struct flashctx *flash);
264 265
int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
int erase_flash(struct flashctx *flash);
266
int probe_flash(struct registered_master *mst, int startchip, struct flashctx *fill_flash, int force);
267
int read_flash_to_file(struct flashctx *flash, const char *filename);
Nico Huber's avatar
Nico Huber committed
268
char *extract_param(const char *const *haystack, const char *needle, const char *delim);
269 270
int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len);
int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, enum write_granularity gran);
271
void print_version(void);
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
272
void print_buildinfo(void);
273
void print_banner(void);
274
void list_programmers_linebreak(int startcol, int cols, int paren);
275
int selfcheck(void);
276
int doit(struct flashctx *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it);
277
int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename);
278
int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename);
279

280
/* Something happened that shouldn't happen, but we can go on. */
281 282
#define ERROR_NONFATAL 0x100

283 284
/* Something happened that shouldn't happen, we'll abort. */
#define ERROR_FATAL -0xee
285 286 287 288 289 290 291
#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
292

293 294 295 296
/* cli_common.c */
char *flashbuses_to_text(enum chipbustype bustype);
void print_chip_support_status(const struct flashchip *chip);

297
/* cli_output.c */
298 299
extern int verbose_screen;
extern int verbose_logfile;
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
300 301 302 303 304
#ifndef STANDALONE
int open_logfile(const char * const filename);
int close_logfile(void);
void start_logging(void);
#endif
305 306
enum msglevel {
	MSG_ERROR	= 0,
Stefan Tauner's avatar
Stefan Tauner committed
307 308 309 310 311
	MSG_WARN	= 1,
	MSG_INFO	= 2,
	MSG_DEBUG	= 3,
	MSG_DEBUG2	= 4,
	MSG_SPEW	= 5,
312
};
313
/* Let gcc and clang check for correct printf-style format strings. */
314 315 316 317 318 319
int print(enum msglevel level, const char *fmt, ...)
#ifdef __MINGW32__
__attribute__((format(gnu_printf, 2, 3)));
#else
__attribute__((format(printf, 2, 3)));
#endif
320 321 322
#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
323 324 325
#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 */
326 327 328 329 330 331
#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
332 333 334
#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 */
335 336 337
#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  */
338

339
/* layout.c */
340 341
int register_include_arg(char *name);
int process_include_args(void);
342
int read_romlayout(const char *name);
343 344
int normalize_romentries(const struct flashctx *flash);
int build_new_image(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents);
345
void layout_cleanup(void);
346

347
/* spi.c */
348 349 350 351 352 353
struct spi_command {
	unsigned int writecnt;
	unsigned int readcnt;
	const unsigned char *writearr;
	unsigned char *readarr;
};
354 355 356
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);
357

358
enum chipbustype get_buses_supported(void);
359
#endif				/* !__FLASH_H__ */