| 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 | #ifdef _WIN32 |
|---|
| 30 | #include <windows.h> |
|---|
| 31 | #undef min |
|---|
| 32 | #undef max |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | #define ERROR_PTR ((void*)-1) |
|---|
| 36 | |
|---|
| 37 | /* Error codes */ |
|---|
| 38 | #define ERROR_OOM -100 |
|---|
| 39 | #define TIMEOUT_ERROR -101 |
|---|
| 40 | |
|---|
| 41 | typedef unsigned long chipaddr; |
|---|
| 42 | |
|---|
| 43 | int register_shutdown(int (*function) (void *data), void *data); |
|---|
| 44 | void *programmer_map_flash_region(const char *descr, unsigned long phys_addr, |
|---|
| 45 | size_t len); |
|---|
| 46 | void programmer_unmap_flash_region(void *virt_addr, size_t len); |
|---|
| 47 | void programmer_delay(int usecs); |
|---|
| 48 | |
|---|
| 49 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
|---|
| 50 | |
|---|
| 51 | enum chipbustype { |
|---|
| 52 | BUS_NONE = 0, |
|---|
| 53 | BUS_PARALLEL = 1 << 0, |
|---|
| 54 | BUS_LPC = 1 << 1, |
|---|
| 55 | BUS_FWH = 1 << 2, |
|---|
| 56 | BUS_SPI = 1 << 3, |
|---|
| 57 | BUS_PROG = 1 << 4, |
|---|
| 58 | BUS_NONSPI = BUS_PARALLEL | BUS_LPC | BUS_FWH, |
|---|
| 59 | }; |
|---|
| 60 | |
|---|
| 61 | /* |
|---|
| 62 | * The following enum defines possible write granularities of flash chips. These tend to reflect the properties |
|---|
| 63 | * of the actual hardware not necesserily the write function(s) defined by the respective struct flashchip. |
|---|
| 64 | * The latter might (and should) be more precisely specified, e.g. they might bail out early if their execution |
|---|
| 65 | * would result in undefined chip contents. |
|---|
| 66 | */ |
|---|
| 67 | enum write_granularity { |
|---|
| 68 | /* We assume 256 byte granularity by default. */ |
|---|
| 69 | write_gran_256bytes = 0,/* If less than 256 bytes are written, the unwritten bytes are undefined. */ |
|---|
| 70 | write_gran_1bit, /* Each bit can be cleared individually. */ |
|---|
| 71 | write_gran_1byte, /* A byte can be written once. Further writes to an already written byte cause |
|---|
| 72 | * its contents to be either undefined or to stay unchanged. */ |
|---|
| 73 | write_gran_264bytes, /* If less than 264 bytes are written, the unwritten bytes are undefined. */ |
|---|
| 74 | write_gran_512bytes, /* If less than 512 bytes are written, the unwritten bytes are undefined. */ |
|---|
| 75 | write_gran_528bytes, /* If less than 528 bytes are written, the unwritten bytes are undefined. */ |
|---|
| 76 | write_gran_1024bytes, /* If less than 1024 bytes are written, the unwritten bytes are undefined. */ |
|---|
| 77 | write_gran_1056bytes, /* If less than 1056 bytes are written, the unwritten bytes are undefined. */ |
|---|
| 78 | }; |
|---|
| 79 | |
|---|
| 80 | /* |
|---|
| 81 | * How many different contiguous runs of erase blocks with one size each do |
|---|
| 82 | * we have for a given erase function? |
|---|
| 83 | */ |
|---|
| 84 | #define NUM_ERASEREGIONS 5 |
|---|
| 85 | |
|---|
| 86 | /* |
|---|
| 87 | * How many different erase functions do we have per chip? |
|---|
| 88 | * Atmel AT25FS010 has 6 different functions. |
|---|
| 89 | */ |
|---|
| 90 | #define NUM_ERASEFUNCTIONS 6 |
|---|
| 91 | |
|---|
| 92 | #define FEATURE_REGISTERMAP (1 << 0) |
|---|
| 93 | #define FEATURE_BYTEWRITES (1 << 1) |
|---|
| 94 | #define FEATURE_LONG_RESET (0 << 4) |
|---|
| 95 | #define FEATURE_SHORT_RESET (1 << 4) |
|---|
| 96 | #define FEATURE_EITHER_RESET FEATURE_LONG_RESET |
|---|
| 97 | #define FEATURE_RESET_MASK (FEATURE_LONG_RESET | FEATURE_SHORT_RESET) |
|---|
| 98 | #define FEATURE_ADDR_FULL (0 << 2) |
|---|
| 99 | #define FEATURE_ADDR_MASK (3 << 2) |
|---|
| 100 | #define FEATURE_ADDR_2AA (1 << 2) |
|---|
| 101 | #define FEATURE_ADDR_AAA (2 << 2) |
|---|
| 102 | #define FEATURE_ADDR_SHIFTED (1 << 5) |
|---|
| 103 | #define FEATURE_WRSR_EWSR (1 << 6) |
|---|
| 104 | #define FEATURE_WRSR_WREN (1 << 7) |
|---|
| 105 | #define FEATURE_OTP (1 << 8) |
|---|
| 106 | #define FEATURE_QPI (1 << 9) |
|---|
| 107 | #define FEATURE_WRSR_EITHER (FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN) |
|---|
| 108 | |
|---|
| 109 | struct flashctx; |
|---|
| 110 | typedef int (erasefunc_t)(struct flashctx *flash, unsigned int addr, unsigned int blocklen); |
|---|
| 111 | |
|---|
| 112 | struct flashchip { |
|---|
| 113 | const char *vendor; |
|---|
| 114 | const char *name; |
|---|
| 115 | |
|---|
| 116 | enum chipbustype bustype; |
|---|
| 117 | |
|---|
| 118 | /* |
|---|
| 119 | * With 32bit manufacture_id and model_id we can cover IDs up to |
|---|
| 120 | * (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's |
|---|
| 121 | * Identification code. |
|---|
| 122 | */ |
|---|
| 123 | uint32_t manufacture_id; |
|---|
| 124 | uint32_t model_id; |
|---|
| 125 | |
|---|
| 126 | /* Total chip size in kilobytes */ |
|---|
| 127 | unsigned int total_size; |
|---|
| 128 | /* Chip page size in bytes */ |
|---|
| 129 | unsigned int page_size; |
|---|
| 130 | int feature_bits; |
|---|
| 131 | |
|---|
| 132 | /* |
|---|
| 133 | * Indicate if flashrom has been tested with this flash chip and if |
|---|
| 134 | * everything worked correctly. |
|---|
| 135 | */ |
|---|
| 136 | uint32_t tested; |
|---|
| 137 | |
|---|
| 138 | int (*probe) (struct flashctx *flash); |
|---|
| 139 | |
|---|
| 140 | /* Delay after "enter/exit ID mode" commands in microseconds. |
|---|
| 141 | * NB: negative values have special meanings, see TIMING_* below. |
|---|
| 142 | */ |
|---|
| 143 | signed int probe_timing; |
|---|
| 144 | |
|---|
| 145 | /* |
|---|
| 146 | * Erase blocks and associated erase function. Any chip erase function |
|---|
| 147 | * is stored as chip-sized virtual block together with said function. |
|---|
| 148 | * The first one that fits will be chosen. There is currently no way to |
|---|
| 149 | * influence that behaviour. For testing just comment out the other |
|---|
| 150 | * elements or set the function pointer to NULL. |
|---|
| 151 | */ |
|---|
| 152 | struct block_eraser { |
|---|
| 153 | struct eraseblock{ |
|---|
| 154 | unsigned int size; /* Eraseblock size in bytes */ |
|---|
| 155 | unsigned int count; /* Number of contiguous blocks with that size */ |
|---|
| 156 | } eraseblocks[NUM_ERASEREGIONS]; |
|---|
| 157 | /* a block_erase function should try to erase one block of size |
|---|
| 158 | * 'blocklen' at address 'blockaddr' and return 0 on success. */ |
|---|
| 159 | int (*block_erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen); |
|---|
| 160 | } block_erasers[NUM_ERASEFUNCTIONS]; |
|---|
| 161 | |
|---|
| 162 | int (*printlock) (struct flashctx *flash); |
|---|
| 163 | int (*unlock) (struct flashctx *flash); |
|---|
| 164 | int (*write) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len); |
|---|
| 165 | int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len); |
|---|
| 166 | struct voltage { |
|---|
| 167 | uint16_t min; |
|---|
| 168 | uint16_t max; |
|---|
| 169 | } voltage; |
|---|
| 170 | enum write_granularity gran; |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | struct flashctx { |
|---|
| 174 | struct flashchip *chip; |
|---|
| 175 | chipaddr virtual_memory; |
|---|
| 176 | /* Some flash devices have an additional register space. */ |
|---|
| 177 | chipaddr virtual_registers; |
|---|
| 178 | struct registered_programmer *pgm; |
|---|
| 179 | }; |
|---|
| 180 | |
|---|
| 181 | #define TEST_UNTESTED 0 |
|---|
| 182 | |
|---|
| 183 | #define TEST_OK_PROBE (1 << 0) |
|---|
| 184 | #define TEST_OK_READ (1 << 1) |
|---|
| 185 | #define TEST_OK_ERASE (1 << 2) |
|---|
| 186 | #define TEST_OK_WRITE (1 << 3) |
|---|
| 187 | #define TEST_OK_PR (TEST_OK_PROBE | TEST_OK_READ) |
|---|
| 188 | #define TEST_OK_PRE (TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE) |
|---|
| 189 | #define TEST_OK_PRW (TEST_OK_PROBE | TEST_OK_READ | TEST_OK_WRITE) |
|---|
| 190 | #define TEST_OK_PREW (TEST_OK_PROBE | TEST_OK_READ | TEST_OK_ERASE | TEST_OK_WRITE) |
|---|
| 191 | #define TEST_OK_MASK 0x0f |
|---|
| 192 | |
|---|
| 193 | #define TEST_BAD_PROBE (1 << 4) |
|---|
| 194 | #define TEST_BAD_READ (1 << 5) |
|---|
| 195 | #define TEST_BAD_ERASE (1 << 6) |
|---|
| 196 | #define TEST_BAD_WRITE (1 << 7) |
|---|
| 197 | #define TEST_BAD_REW (TEST_BAD_READ | TEST_BAD_ERASE | TEST_BAD_WRITE) |
|---|
| 198 | #define TEST_BAD_PREW (TEST_BAD_PROBE | TEST_BAD_READ | TEST_BAD_ERASE | TEST_BAD_WRITE) |
|---|
| 199 | #define TEST_BAD_MASK 0xf0 |
|---|
| 200 | |
|---|
| 201 | /* Timing used in probe routines. ZERO is -2 to differentiate between an unset |
|---|
| 202 | * field and zero delay. |
|---|
| 203 | * |
|---|
| 204 | * SPI devices will always have zero delay and ignore this field. |
|---|
| 205 | */ |
|---|
| 206 | #define TIMING_FIXME -1 |
|---|
| 207 | /* this is intentionally same value as fixme */ |
|---|
| 208 | #define TIMING_IGNORED -1 |
|---|
| 209 | #define TIMING_ZERO -2 |
|---|
| 210 | |
|---|
| 211 | extern const struct flashchip flashchips[]; |
|---|
| 212 | |
|---|
| 213 | void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
|---|
| 214 | void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr); |
|---|
| 215 | void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr); |
|---|
| 216 | void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len); |
|---|
| 217 | uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr); |
|---|
| 218 | uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr); |
|---|
| 219 | uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr); |
|---|
| 220 | void chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len); |
|---|
| 221 | |
|---|
| 222 | /* print.c */ |
|---|
| 223 | char *flashbuses_to_text(enum chipbustype bustype); |
|---|
| 224 | int print_supported(void); |
|---|
| 225 | void print_supported_wiki(void); |
|---|
| 226 | |
|---|
| 227 | /* flashrom.c */ |
|---|
| 228 | extern int verbose_screen; |
|---|
| 229 | extern int verbose_logfile; |
|---|
| 230 | extern const char flashrom_version[]; |
|---|
| 231 | extern const char *chip_to_probe; |
|---|
| 232 | void map_flash_registers(struct flashctx *flash); |
|---|
| 233 | int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len); |
|---|
| 234 | int erase_flash(struct flashctx *flash); |
|---|
| 235 | int probe_flash(struct registered_programmer *pgm, int startchip, struct flashctx *fill_flash, int force); |
|---|
| 236 | int read_flash_to_file(struct flashctx *flash, const char *filename); |
|---|
| 237 | int min(int a, int b); |
|---|
| 238 | int max(int a, int b); |
|---|
| 239 | void tolower_string(char *str); |
|---|
| 240 | char *extract_param(const char *const *haystack, const char *needle, const char *delim); |
|---|
| 241 | int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len); |
|---|
| 242 | int need_erase(uint8_t *have, uint8_t *want, unsigned int len, enum write_granularity gran); |
|---|
| 243 | char *strcat_realloc(char *dest, const char *src); |
|---|
| 244 | void print_version(void); |
|---|
| 245 | void print_buildinfo(void); |
|---|
| 246 | void print_banner(void); |
|---|
| 247 | void list_programmers_linebreak(int startcol, int cols, int paren); |
|---|
| 248 | int selfcheck(void); |
|---|
| 249 | int doit(struct flashctx *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it); |
|---|
| 250 | int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename); |
|---|
| 251 | int write_buf_to_file(unsigned char *buf, unsigned long size, const char *filename); |
|---|
| 252 | |
|---|
| 253 | enum test_state { |
|---|
| 254 | OK = 0, |
|---|
| 255 | NT = 1, /* Not tested */ |
|---|
| 256 | BAD |
|---|
| 257 | }; |
|---|
| 258 | |
|---|
| 259 | /* Something happened that shouldn't happen, but we can go on. */ |
|---|
| 260 | #define ERROR_NONFATAL 0x100 |
|---|
| 261 | |
|---|
| 262 | /* Something happened that shouldn't happen, we'll abort. */ |
|---|
| 263 | #define ERROR_FATAL -0xee |
|---|
| 264 | #define ERROR_FLASHROM_BUG -200 |
|---|
| 265 | /* We reached one of the hardcoded limits of flashrom. This can be fixed by |
|---|
| 266 | * increasing the limit of a compile-time allocation or by switching to dynamic |
|---|
| 267 | * allocation. |
|---|
| 268 | * Note: If this warning is triggered, check first for runaway registrations. |
|---|
| 269 | */ |
|---|
| 270 | #define ERROR_FLASHROM_LIMIT -201 |
|---|
| 271 | |
|---|
| 272 | /* cli_output.c */ |
|---|
| 273 | #ifndef STANDALONE |
|---|
| 274 | int open_logfile(const char * const filename); |
|---|
| 275 | int close_logfile(void); |
|---|
| 276 | void start_logging(void); |
|---|
| 277 | #endif |
|---|
| 278 | enum msglevel { |
|---|
| 279 | MSG_ERROR = 0, |
|---|
| 280 | MSG_WARN = 1, |
|---|
| 281 | MSG_INFO = 2, |
|---|
| 282 | MSG_DEBUG = 3, |
|---|
| 283 | MSG_DEBUG2 = 4, |
|---|
| 284 | MSG_SPEW = 5, |
|---|
| 285 | }; |
|---|
| 286 | /* Let gcc and clang check for correct printf-style format strings. */ |
|---|
| 287 | int print(enum msglevel level, const char *fmt, ...) __attribute__((format(printf, 2, 3))); |
|---|
| 288 | #define msg_gerr(...) print(MSG_ERROR, __VA_ARGS__) /* general errors */ |
|---|
| 289 | #define msg_perr(...) print(MSG_ERROR, __VA_ARGS__) /* programmer errors */ |
|---|
| 290 | #define msg_cerr(...) print(MSG_ERROR, __VA_ARGS__) /* chip errors */ |
|---|
| 291 | #define msg_gwarn(...) print(MSG_WARN, __VA_ARGS__) /* general warnings */ |
|---|
| 292 | #define msg_pwarn(...) print(MSG_WARN, __VA_ARGS__) /* programmer warnings */ |
|---|
| 293 | #define msg_cwarn(...) print(MSG_WARN, __VA_ARGS__) /* chip warnings */ |
|---|
| 294 | #define msg_ginfo(...) print(MSG_INFO, __VA_ARGS__) /* general info */ |
|---|
| 295 | #define msg_pinfo(...) print(MSG_INFO, __VA_ARGS__) /* programmer info */ |
|---|
| 296 | #define msg_cinfo(...) print(MSG_INFO, __VA_ARGS__) /* chip info */ |
|---|
| 297 | #define msg_gdbg(...) print(MSG_DEBUG, __VA_ARGS__) /* general debug */ |
|---|
| 298 | #define msg_pdbg(...) print(MSG_DEBUG, __VA_ARGS__) /* programmer debug */ |
|---|
| 299 | #define msg_cdbg(...) print(MSG_DEBUG, __VA_ARGS__) /* chip debug */ |
|---|
| 300 | #define msg_gdbg2(...) print(MSG_DEBUG2, __VA_ARGS__) /* general debug2 */ |
|---|
| 301 | #define msg_pdbg2(...) print(MSG_DEBUG2, __VA_ARGS__) /* programmer debug2 */ |
|---|
| 302 | #define msg_cdbg2(...) print(MSG_DEBUG2, __VA_ARGS__) /* chip debug2 */ |
|---|
| 303 | #define msg_gspew(...) print(MSG_SPEW, __VA_ARGS__) /* general debug spew */ |
|---|
| 304 | #define msg_pspew(...) print(MSG_SPEW, __VA_ARGS__) /* programmer debug spew */ |
|---|
| 305 | #define msg_cspew(...) print(MSG_SPEW, __VA_ARGS__) /* chip debug spew */ |
|---|
| 306 | |
|---|
| 307 | /* layout.c */ |
|---|
| 308 | int register_include_arg(char *name); |
|---|
| 309 | int process_include_args(void); |
|---|
| 310 | int read_romlayout(char *name); |
|---|
| 311 | int handle_romentries(const struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents); |
|---|
| 312 | |
|---|
| 313 | /* spi.c */ |
|---|
| 314 | struct spi_command { |
|---|
| 315 | unsigned int writecnt; |
|---|
| 316 | unsigned int readcnt; |
|---|
| 317 | const unsigned char *writearr; |
|---|
| 318 | unsigned char *readarr; |
|---|
| 319 | }; |
|---|
| 320 | int spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr); |
|---|
| 321 | int spi_send_multicommand(struct flashctx *flash, struct spi_command *cmds); |
|---|
| 322 | uint32_t spi_get_valid_read_addr(struct flashctx *flash); |
|---|
| 323 | |
|---|
| 324 | enum chipbustype get_buses_supported(void); |
|---|
| 325 | #endif /* !__FLASH_H__ */ |
|---|