flash.h 8.63 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>
29
#include "hwaccess.h"
Patrick Georgi's avatar
Patrick Georgi committed
30 31 32 33 34
#ifdef _WIN32
#include <windows.h>
#undef min
#undef max
#endif
35

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

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

41 42
typedef unsigned long chipaddr;

43
int register_shutdown(void (*function) (void *data), void *data);
44 45 46 47 48 49
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);
void chip_writeb(uint8_t val, chipaddr addr);
void chip_writew(uint16_t val, chipaddr addr);
void chip_writel(uint32_t val, chipaddr addr);
50
void chip_writen(uint8_t *buf, chipaddr addr, size_t len);
51 52 53
uint8_t chip_readb(const chipaddr addr);
uint16_t chip_readw(const chipaddr addr);
uint32_t chip_readl(const chipaddr addr);
54
void chip_readn(uint8_t *buf, const chipaddr addr, size_t len);
55
void programmer_delay(int usecs);
56

57 58
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

59
enum chipbustype {
60
	CHIP_BUSTYPE_NONE	= 0,
61 62 63 64 65 66 67 68
	CHIP_BUSTYPE_PARALLEL	= 1 << 0,
	CHIP_BUSTYPE_LPC	= 1 << 1,
	CHIP_BUSTYPE_FWH	= 1 << 2,
	CHIP_BUSTYPE_SPI	= 1 << 3,
	CHIP_BUSTYPE_NONSPI	= CHIP_BUSTYPE_PARALLEL | CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH,
	CHIP_BUSTYPE_UNKNOWN	= CHIP_BUSTYPE_PARALLEL | CHIP_BUSTYPE_LPC | CHIP_BUSTYPE_FWH | CHIP_BUSTYPE_SPI,
};

69 70 71 72 73 74 75 76
/*
 * 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?
77
 * Atmel AT25FS010 has 6 different functions.
78
 */
79
#define NUM_ERASEFUNCTIONS 6
80

81 82
#define FEATURE_REGISTERMAP	(1 << 0)
#define FEATURE_BYTEWRITES	(1 << 1)
83 84 85
#define FEATURE_LONG_RESET	(0 << 4)
#define FEATURE_SHORT_RESET	(1 << 4)
#define FEATURE_EITHER_RESET	FEATURE_LONG_RESET
86 87
#define FEATURE_ADDR_FULL	(0 << 2)
#define FEATURE_ADDR_MASK	(3 << 2)
88 89
#define FEATURE_ADDR_2AA	(1 << 2)
#define FEATURE_ADDR_AAA	(2 << 2)
Michael Karcher's avatar
Michael Karcher committed
90
#define FEATURE_ADDR_SHIFTED	(1 << 5)
91 92 93
#define FEATURE_WRSR_EWSR	(1 << 6)
#define FEATURE_WRSR_WREN	(1 << 7)
#define FEATURE_WRSR_EITHER	(FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN)
94

95
struct flashchip {
96
	const char *vendor;
97
	const char *name;
98 99 100

	enum chipbustype bustype;

101 102
	/*
	 * With 32bit manufacture_id and model_id we can cover IDs up to
103 104 105 106 107
	 * (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's
	 * Identification code.
	 */
	uint32_t manufacture_id;
	uint32_t model_id;
108 109 110

	int total_size;
	int page_size;
111
	int feature_bits;
112

113 114
	/*
	 * Indicate if flashrom has been tested with this flash chip and if
115 116 117 118
	 * everything worked correctly.
	 */
	uint32_t tested;

Uwe Hermann's avatar
Uwe Hermann committed
119
	int (*probe) (struct flashchip *flash);
120 121 122

	/* Delay after "enter/exit ID mode" commands in microseconds. */
	int probe_timing;
123 124

	/*
125 126
	 * Erase blocks and associated erase function. Any chip erase function
	 * is stored as chip-sized virtual block together with said function.
127 128 129 130 131 132 133 134 135
	 */
	struct block_eraser {
		struct eraseblock{
			unsigned int size; /* Eraseblock size */
			unsigned int count; /* Number of contiguous blocks with that size */
		} eraseblocks[NUM_ERASEREGIONS];
		int (*block_erase) (struct flashchip *flash, unsigned int blockaddr, unsigned int blocklen);
	} block_erasers[NUM_ERASEFUNCTIONS];

136 137
	int (*printlock) (struct flashchip *flash);
	int (*unlock) (struct flashchip *flash);
138
	int (*write) (struct flashchip *flash, uint8_t *buf, int start, int len);
139
	int (*read) (struct flashchip *flash, uint8_t *buf, int start, int len);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
140

141
	/* Some flash devices have an additional register space. */
142 143
	chipaddr virtual_memory;
	chipaddr virtual_registers;
144 145
};

146 147
#define TEST_UNTESTED	0

Uwe Hermann's avatar
Uwe Hermann committed
148 149 150 151 152 153
#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)
154
#define TEST_OK_PRW	(TEST_OK_PROBE | TEST_OK_READ | TEST_OK_WRITE)
Uwe Hermann's avatar
Uwe Hermann committed
155
#define TEST_OK_PREW	(TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE | TEST_OK_WRITE)
156 157
#define TEST_OK_MASK	0x0f

Uwe Hermann's avatar
Uwe Hermann committed
158 159 160 161 162
#define TEST_BAD_PROBE	(1 << 4)
#define TEST_BAD_READ	(1 << 5)
#define TEST_BAD_ERASE	(1 << 6)
#define TEST_BAD_WRITE	(1 << 7)
#define TEST_BAD_PREW	(TEST_BAD_PROBE | TEST_BAD_READ | TEST_BAD_ERASE | TEST_BAD_WRITE)
163 164
#define TEST_BAD_MASK	0xf0

165 166 167 168 169 170 171 172 173 174
/* 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

175 176
extern struct flashchip flashchips[];

177 178
/* print.c */
char *flashbuses_to_text(enum chipbustype bustype);
179 180
void print_supported(void);
void print_supported_wiki(void);
181

182
/* flashrom.c */
183 184 185 186 187
enum write_granularity {
	write_gran_1bit,
	write_gran_1byte,
	write_gran_256bytes,
};
188
extern enum chipbustype buses_supported;
Uwe Hermann's avatar
Uwe Hermann committed
189
extern int verbose;
190
extern const char * const flashrom_version;
191
extern char *chip_to_probe;
192
void map_flash_registers(struct flashchip *flash);
193
int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len);
194
int erase_flash(struct flashchip *flash);
195
struct flashchip *probe_flash(struct flashchip *first_flash, int force);
196
int read_flash_to_file(struct flashchip *flash, char *filename);
197
int min(int a, int b);
198
int max(int a, int b);
199
char *extract_param(char **haystack, char *needle, char *delim);
200 201
int check_erased_range(struct flashchip *flash, int start, int len);
int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message);
202
int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran);
203
char *strcat_realloc(char *dest, const char *src);
204
void print_version(void);
205
void print_banner(void);
206
void list_programmers_linebreak(int startcol, int cols, int paren);
207
int selfcheck(void);
208
int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it);
209 210 211

#define OK 0
#define NT 1    /* Not tested */
212

213 214 215
/* Something happened that shouldn't happen, but we can go on */
#define ERROR_NONFATAL 0x100

216
/* cli_output.c */
217 218
/* Let gcc and clang check for correct printf-style format strings. */
int print(int type, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#define MSG_ERROR	0
#define MSG_INFO	1
#define MSG_DEBUG	2
#define MSG_BARF	3
#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 */
#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 */
#define msg_gspew(...)	print(MSG_BARF, __VA_ARGS__)	/* general debug barf  */
#define msg_pspew(...)	print(MSG_BARF, __VA_ARGS__)	/* programmer debug barf  */
#define msg_cspew(...)	print(MSG_BARF, __VA_ARGS__)	/* chip debug barf  */
235

236 237 238
/* cli_classic.c */
int cli_classic(int argc, char *argv[]);

239 240 241
/* layout.c */
int read_romlayout(char *name);
int find_romentry(char *name);
242
int handle_romentries(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents);
243

244
/* spi.c */
245 246 247 248 249 250 251
struct spi_command {
	unsigned int writecnt;
	unsigned int readcnt;
	const unsigned char *writearr;
	unsigned char *readarr;
};
int spi_send_command(unsigned int writecnt, unsigned int readcnt,
252
		const unsigned char *writearr, unsigned char *readarr);
253
int spi_send_multicommand(struct spi_command *cmds);
254
uint32_t spi_get_valid_read_addr(void);
255

256
#endif				/* !__FLASH_H__ */