| 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 | ␊ |
| 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 | * 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_RESET␉FEATURE_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 | ␊ |
| 88 | struct flashctx;␊ |
| 89 | ␊ |
| 90 | struct flashchip {␊ |
| 91 | ␉const char *vendor;␊ |
| 92 | ␉const char *name;␊ |
| 93 | ␊ |
| 94 | ␉enum 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 | ␉ */␊ |
| 101 | ␉uint32_t manufacture_id;␊ |
| 102 | ␉uint32_t model_id;␊ |
| 103 | ␊ |
| 104 | ␉/* Total chip size in kilobytes */␊ |
| 105 | ␉unsigned int total_size;␊ |
| 106 | ␉/* Chip page size in bytes */␊ |
| 107 | ␉unsigned int page_size;␊ |
| 108 | ␉int feature_bits;␊ |
| 109 | ␊ |
| 110 | ␉/*␊ |
| 111 | ␉ * Indicate if flashrom has been tested with this flash chip and if␊ |
| 112 | ␉ * everything worked correctly.␊ |
| 113 | ␉ */␊ |
| 114 | ␉uint32_t tested;␊ |
| 115 | ␊ |
| 116 | ␉int (*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 | ␉ */␊ |
| 121 | ␉signed 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 | ␉ */␊ |
| 130 | ␉struct block_eraser {␊ |
| 131 | ␉␉struct eraseblock{␊ |
| 132 | ␉␉␉unsigned int size; /* Eraseblock size in bytes */␊ |
| 133 | ␉␉␉unsigned 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. */␊ |
| 137 | ␉␉int (*block_erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen);␊ |
| 138 | ␉} block_erasers[NUM_ERASEFUNCTIONS];␊ |
| 139 | ␊ |
| 140 | ␉int (*printlock) (struct flashctx *flash);␊ |
| 141 | ␉int (*unlock) (struct flashctx *flash);␊ |
| 142 | ␉int (*write) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);␊ |
| 143 | ␉int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);␊ |
| 144 | ␉struct voltage {␊ |
| 145 | ␉␉uint16_t min;␊ |
| 146 | ␉␉uint16_t max;␊ |
| 147 | ␉} voltage;␊ |
| 148 | };␊ |
| 149 | ␊ |
| 150 | /* struct flashctx must always contain struct flashchip at the beginning. */␊ |
| 151 | struct flashctx {␊ |
| 152 | ␉const char *vendor;␊ |
| 153 | ␉const char *name;␊ |
| 154 | ␉enum chipbustype bustype;␊ |
| 155 | ␉uint32_t manufacture_id;␊ |
| 156 | ␉uint32_t model_id;␊ |
| 157 | ␉int total_size;␊ |
| 158 | ␉int page_size;␊ |
| 159 | ␉int feature_bits;␊ |
| 160 | ␉uint32_t tested;␊ |
| 161 | ␉int (*probe) (struct flashctx *flash);␊ |
| 162 | ␉int probe_timing;␊ |
| 163 | ␉struct block_eraser block_erasers[NUM_ERASEFUNCTIONS];␊ |
| 164 | ␉int (*printlock) (struct flashctx *flash);␊ |
| 165 | ␉int (*unlock) (struct flashctx *flash);␊ |
| 166 | ␉int (*write) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);␊ |
| 167 | ␉int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);␊ |
| 168 | ␉struct voltage voltage;␊ |
| 169 | ␉/* struct flashchip ends here. */␊ |
| 170 | ␉␊ |
| 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 | #define TEST_UNTESTED␉0␊ |
| 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_MASK␉0x0f␊ |
| 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_MASK␉0xf0␊ |
| 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 | ␊ |
| 206 | extern const struct flashchip flashchips[];␊ |
| 207 | ␊ |
| 208 | void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);␊ |
| 209 | void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);␊ |
| 210 | void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);␊ |
| 211 | void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len);␊ |
| 212 | uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr);␊ |
| 213 | uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr);␊ |
| 214 | uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr);␊ |
| 215 | void chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);␊ |
| 216 | ␊ |
| 217 | /* print.c */␊ |
| 218 | char *flashbuses_to_text(enum chipbustype bustype);␊ |
| 219 | void print_supported(void);␊ |
| 220 | void print_supported_wiki(void);␊ |
| 221 | ␊ |
| 222 | /* flashrom.c */␊ |
| 223 | enum write_granularity {␊ |
| 224 | ␉write_gran_1bit,␊ |
| 225 | ␉write_gran_1byte,␊ |
| 226 | ␉write_gran_256bytes,␊ |
| 227 | };␊ |
| 228 | extern int verbose;␊ |
| 229 | extern const char flashrom_version[];␊ |
| 230 | extern char *chip_to_probe;␊ |
| 231 | void map_flash_registers(struct flashctx *flash);␊ |
| 232 | int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);␊ |
| 233 | int erase_flash(struct flashctx *flash);␊ |
| 234 | int probe_flash(struct registered_programmer *pgm, int startchip, struct flashctx *fill_flash, int force);␊ |
| 235 | int read_flash_to_file(struct flashctx *flash, const char *filename);␊ |
| 236 | int min(int a, int b);␊ |
| 237 | int max(int a, int b);␊ |
| 238 | void tolower_string(char *str);␊ |
| 239 | char *extract_param(char **haystack, const char *needle, const char *delim);␊ |
| 240 | int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len, const char *message);␊ |
| 241 | int need_erase(uint8_t *have, uint8_t *want, unsigned int len, enum write_granularity gran);␊ |
| 242 | char *strcat_realloc(char *dest, const char *src);␊ |
| 243 | void print_version(void);␊ |
| 244 | void print_banner(void);␊ |
| 245 | void list_programmers_linebreak(int startcol, int cols, int paren);␊ |
| 246 | int selfcheck(void);␊ |
| 247 | int doit(struct flashctx *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it);␊ |
| 248 | int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename);␊ |
| 249 | int 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. */␊ |
| 269 | int print(int type, const char *fmt, ...) __attribute__((format(printf, 2, 3)));␊ |
| 270 | #define MSG_ERROR␉0␊ |
| 271 | #define MSG_INFO␉1␊ |
| 272 | #define MSG_DEBUG␉2␊ |
| 273 | #define MSG_DEBUG2␉3␊ |
| 274 | #define MSG_BARF␉4␊ |
| 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 */␊ |
| 292 | int register_include_arg(char *name);␊ |
| 293 | int process_include_args(void);␊ |
| 294 | int read_romlayout(char *name);␊ |
| 295 | int handle_romentries(struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents);␊ |
| 296 | ␊ |
| 297 | /* spi.c */␊ |
| 298 | struct spi_command {␊ |
| 299 | ␉unsigned int writecnt;␊ |
| 300 | ␉unsigned int readcnt;␊ |
| 301 | ␉const unsigned char *writearr;␊ |
| 302 | ␉unsigned char *readarr;␊ |
| 303 | };␊ |
| 304 | int spi_send_command(struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr);␊ |
| 305 | int spi_send_multicommand(struct flashctx *flash, struct spi_command *cmds);␊ |
| 306 | uint32_t spi_get_valid_read_addr(struct flashctx *flash);␊ |
| 307 | ␊ |
| 308 | enum chipbustype get_buses_supported(void);␊ |
| 309 | #endif␉␉␉␉/* !__FLASH_H__ */␊ |
| 310 | |