Flashrom

Flashrom Svn Source Tree

Root/trunk/flash.h

  • Property svn:keywords set to Author Date Id Revision
  • Property svn:eol-style set to native
1/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * 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 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#ifndef __FLASH_H__
25#define __FLASH_H__ 1
26
27#include <stdint.h>
28#include <stddef.h>
29#include "hwaccess.h"
30#ifdef _WIN32
31#include <windows.h>
32#undef min
33#undef max
34#endif
35
36#define ERROR_PTR ((void*)-1)
37
38/* Error codes */
39#define TIMEOUT_ERROR-101
40
41typedef unsigned long chipaddr;
42
43int register_shutdown(int (*function) (void *data), void *data);
44void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
45 size_t len);
46void programmer_unmap_flash_region(void *virt_addr, size_t len);
47void programmer_delay(int usecs);
48
49#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
50
51enum chipbustype {
52BUS_NONE= 0,
53BUS_PARALLEL= 1 << 0,
54BUS_LPC= 1 << 1,
55BUS_FWH= 1 << 2,
56BUS_SPI= 1 << 3,
57BUS_PROG= 1 << 4,
58BUS_NONSPI= BUS_PARALLEL | BUS_LPC | BUS_FWH,
59};
60
61/*
62 * How many different contiguous runs of erase blocks with one size each do
63 * we have for a given erase function?
64 */
65#define NUM_ERASEREGIONS 5
66
67/*
68 * How many different erase functions do we have per chip?
69 * Atmel AT25FS010 has 6 different functions.
70 */
71#define NUM_ERASEFUNCTIONS 6
72
73#define FEATURE_REGISTERMAP(1 << 0)
74#define FEATURE_BYTEWRITES(1 << 1)
75#define FEATURE_LONG_RESET(0 << 4)
76#define FEATURE_SHORT_RESET(1 << 4)
77#define FEATURE_EITHER_RESETFEATURE_LONG_RESET
78#define FEATURE_RESET_MASK(FEATURE_LONG_RESET | FEATURE_SHORT_RESET)
79#define FEATURE_ADDR_FULL(0 << 2)
80#define FEATURE_ADDR_MASK(3 << 2)
81#define FEATURE_ADDR_2AA(1 << 2)
82#define FEATURE_ADDR_AAA(2 << 2)
83#define FEATURE_ADDR_SHIFTED(1 << 5)
84#define FEATURE_WRSR_EWSR(1 << 6)
85#define FEATURE_WRSR_WREN(1 << 7)
86#define FEATURE_WRSR_EITHER(FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN)
87
88struct flashctx;
89
90struct flashchip {
91const char *vendor;
92const char *name;
93
94enum chipbustype bustype;
95
96/*
97 * With 32bit manufacture_id and model_id we can cover IDs up to
98 * (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's
99 * Identification code.
100 */
101uint32_t manufacture_id;
102uint32_t model_id;
103
104/* Total chip size in kilobytes */
105unsigned int total_size;
106/* Chip page size in bytes */
107unsigned int page_size;
108int feature_bits;
109
110/*
111 * Indicate if flashrom has been tested with this flash chip and if
112 * everything worked correctly.
113 */
114uint32_t tested;
115
116int (*probe) (struct flashctx *flash);
117
118/* Delay after "enter/exit ID mode" commands in microseconds.
119 * NB: negative values have special meanings, see TIMING_* below.
120 */
121signed int probe_timing;
122
123/*
124 * Erase blocks and associated erase function. Any chip erase function
125 * is stored as chip-sized virtual block together with said function.
126 * The first one that fits will be chosen. There is currently no way to
127 * influence that behaviour. For testing just comment out the other
128 * elements or set the function pointer to NULL.
129 */
130struct block_eraser {
131struct eraseblock{
132unsigned int size; /* Eraseblock size in bytes */
133unsigned int count; /* Number of contiguous blocks with that size */
134} eraseblocks[NUM_ERASEREGIONS];
135/* a block_erase function should try to erase one block of size
136 * 'blocklen' at address 'blockaddr' and return 0 on success. */
137int (*block_erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen);
138} block_erasers[NUM_ERASEFUNCTIONS];
139
140int (*printlock) (struct flashctx *flash);
141int (*unlock) (struct flashctx *flash);
142int (*write) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
143int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
144struct voltage {
145uint16_t min;
146uint16_t max;
147} voltage;
148};
149
150/* struct flashctx must always contain struct flashchip at the beginning. */
151struct flashctx {
152const char *vendor;
153const char *name;
154enum chipbustype bustype;
155uint32_t manufacture_id;
156uint32_t model_id;
157int total_size;
158int page_size;
159int feature_bits;
160uint32_t tested;
161int (*probe) (struct flashctx *flash);
162int probe_timing;
163struct block_eraser block_erasers[NUM_ERASEFUNCTIONS];
164int (*printlock) (struct flashctx *flash);
165int (*unlock) (struct flashctx *flash);
166int (*write) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
167int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
168struct voltage voltage;
169/* struct flashchip ends here. */
170
171chipaddr virtual_memory;
172/* Some flash devices have an additional register space. */
173chipaddr virtual_registers;
174struct registered_programmer *pgm;
175};
176
177#define TEST_UNTESTED0
178
179#define TEST_OK_PROBE(1 << 0)
180#define TEST_OK_READ(1 << 1)
181#define TEST_OK_ERASE(1 << 2)
182#define TEST_OK_WRITE(1 << 3)
183#define TEST_OK_PR(TEST_OK_PROBE | TEST_OK_READ)
184#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)
186#define TEST_OK_PREW(TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE | TEST_OK_WRITE)
187#define TEST_OK_MASK0x0f
188
189#define TEST_BAD_PROBE(1 << 4)
190#define TEST_BAD_READ(1 << 5)
191#define TEST_BAD_ERASE(1 << 6)
192#define TEST_BAD_WRITE(1 << 7)
193#define TEST_BAD_PREW(TEST_BAD_PROBE | TEST_BAD_READ | TEST_BAD_ERASE | TEST_BAD_WRITE)
194#define TEST_BAD_MASK0xf0
195
196/* Timing used in probe routines. ZERO is -2 to differentiate between an unset
197 * field and zero delay.
198 *
199 * SPI devices will always have zero delay and ignore this field.
200 */
201#define TIMING_FIXME-1
202/* this is intentionally same value as fixme */
203#define TIMING_IGNORED-1
204#define TIMING_ZERO-2
205
206extern const struct flashchip flashchips[];
207
208void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
209void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
210void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
211void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len);
212uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr);
213uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr);
214uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr);
215void chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
216
217/* print.c */
218char *flashbuses_to_text(enum chipbustype bustype);
219void print_supported(void);
220void print_supported_wiki(void);
221
222/* flashrom.c */
223enum write_granularity {
224write_gran_1bit,
225write_gran_1byte,
226write_gran_256bytes,
227};
228extern int verbose;
229extern const char flashrom_version[];
230extern char *chip_to_probe;
231void map_flash_registers(struct flashctx *flash);
232int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
233int erase_flash(struct flashctx *flash);
234int probe_flash(struct registered_programmer *pgm, int startchip, struct flashctx *fill_flash, int force);
235int read_flash_to_file(struct flashctx *flash, const char *filename);
236int min(int a, int b);
237int max(int a, int b);
238void tolower_string(char *str);
239char *extract_param(char **haystack, const char *needle, const char *delim);
240int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len, const char *message);
241int need_erase(uint8_t *have, uint8_t *want, unsigned int len, enum write_granularity gran);
242char *strcat_realloc(char *dest, const char *src);
243void print_version(void);
244void print_banner(void);
245void list_programmers_linebreak(int startcol, int cols, int paren);
246int selfcheck(void);
247int doit(struct flashctx *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it);
248int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename);
249int write_buf_to_file(unsigned char *buf, unsigned long size, const char *filename);
250
251#define OK 0
252#define NT 1 /* Not tested */
253
254/* Something happened that shouldn't happen, but we can go on. */
255#define ERROR_NONFATAL 0x100
256
257/* Something happened that shouldn't happen, we'll abort. */
258#define ERROR_FATAL -0xee
259#define ERROR_FLASHROM_BUG -200
260/* We reached one of the hardcoded limits of flashrom. This can be fixed by
261 * increasing the limit of a compile-time allocation or by switching to dynamic
262 * allocation.
263 * Note: If this warning is triggered, check first for runaway registrations.
264 */
265#define ERROR_FLASHROM_LIMIT -201
266
267/* cli_output.c */
268/* Let gcc and clang check for correct printf-style format strings. */
269int print(int type, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
270#define MSG_ERROR0
271#define MSG_INFO1
272#define MSG_DEBUG2
273#define MSG_DEBUG23
274#define MSG_BARF4
275#define msg_gerr(...)print(MSG_ERROR, __VA_ARGS__)/* general errors */
276#define msg_perr(...)print(MSG_ERROR, __VA_ARGS__)/* programmer errors */
277#define msg_cerr(...)print(MSG_ERROR, __VA_ARGS__)/* chip errors */
278#define msg_ginfo(...)print(MSG_INFO, __VA_ARGS__)/* general info */
279#define msg_pinfo(...)print(MSG_INFO, __VA_ARGS__)/* programmer info */
280#define msg_cinfo(...)print(MSG_INFO, __VA_ARGS__)/* chip info */
281#define msg_gdbg(...)print(MSG_DEBUG, __VA_ARGS__)/* general debug */
282#define msg_pdbg(...)print(MSG_DEBUG, __VA_ARGS__)/* programmer debug */
283#define msg_cdbg(...)print(MSG_DEBUG, __VA_ARGS__)/* chip debug */
284#define msg_gdbg2(...)print(MSG_DEBUG2, __VA_ARGS__)/* general debug2 */
285#define msg_pdbg2(...)print(MSG_DEBUG2, __VA_ARGS__)/* programmer debug2 */
286#define msg_cdbg2(...)print(MSG_DEBUG2, __VA_ARGS__)/* chip debug2 */
287#define msg_gspew(...)print(MSG_BARF, __VA_ARGS__)/* general debug barf */
288#define msg_pspew(...)print(MSG_BARF, __VA_ARGS__)/* programmer debug barf */
289#define msg_cspew(...)print(MSG_BARF, __VA_ARGS__)/* chip debug barf */
290
291/* layout.c */
292int register_include_arg(char *name);
293int process_include_args(void);
294int read_romlayout(char *name);
295int handle_romentries(struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents);
296
297/* spi.c */
298struct spi_command {
299unsigned int writecnt;
300unsigned int readcnt;
301const unsigned char *writearr;
302unsigned char *readarr;
303};
304int spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr);
305int spi_send_multicommand(struct flashctx *flash, struct spi_command *cmds);
306uint32_t spi_get_valid_read_addr(struct flashctx *flash);
307
308enum chipbustype get_buses_supported(void);
309#endif/* !__FLASH_H__ */
310

Archive Download this file

Revision: HEAD