| 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 <[email protected]> |
| 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 |
|
| 20 |
#ifndef __FLASH_H__ |
| 21 |
#define __FLASH_H__ 1 |
| 22 |
|
| 23 |
#include <inttypes.h> |
| 24 |
#include <stdio.h> |
| 25 |
#include <stdint.h> |
| 26 |
#include <stddef.h> |
| 27 |
#include <stdarg.h> |
| 28 |
#include <stdbool.h> |
| 29 |
#if IS_WINDOWS |
| 30 |
#include <windows.h> |
| 31 |
#undef min |
| 32 |
#undef max |
| 33 |
#endif |
| 34 |
|
| 35 |
#include "libflashrom.h" |
| 36 |
#include "layout.h" |
| 37 |
#include "writeprotect.h" |
| 38 |
|
| 39 |
#define KiB (1024) |
| 40 |
#define MiB (1024 * KiB) |
| 41 |
|
| 42 |
#define BIT(x) (1<<(x)) |
| 43 |
|
| 44 |
/* Assumes `n` and `a` are at most 64-bit wide (to avoid typeof() operator). */ |
| 45 |
#define ALIGN_DOWN(n, a) ((n) & ~((uint64_t)(a) - 1)) |
| 46 |
|
| 47 |
#define ERROR_PTR ((void*)-1) |
| 48 |
|
| 49 |
/* Error codes */ |
| 50 |
#define ERROR_OOM -100 |
| 51 |
#define TIMEOUT_ERROR -101 |
| 52 |
|
| 53 |
/* TODO: check using code for correct usage of types */ |
| 54 |
typedef uintptr_t chipaddr; |
| 55 |
#define PRIxPTR_WIDTH ((int)(sizeof(uintptr_t)*2)) |
| 56 |
|
| 57 |
int register_shutdown(int (*function) (void *data), void *data); |
| 58 |
struct registered_master; |
| 59 |
void *master_map_flash_region(const struct registered_master *mast, |
| 60 |
const char *descr, uintptr_t phys_addr, size_t len); |
| 61 |
void master_unmap_flash_region(const struct registered_master *mast, |
| 62 |
void *virt_addr, size_t len); |
| 63 |
/* NOTE: flashctx is not used in default_delay. In this case, a context should be NULL. */ |
| 64 |
void programmer_delay(const struct flashrom_flashctx *flash, unsigned int usecs); |
| 65 |
|
| 66 |
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 67 |
|
| 68 |
enum chipbustype { |
| 69 |
BUS_NONE = 0, |
| 70 |
BUS_PARALLEL = 1 << 0, |
| 71 |
BUS_LPC = 1 << 1, |
| 72 |
BUS_FWH = 1 << 2, |
| 73 |
BUS_SPI = 1 << 3, |
| 74 |
BUS_PROG = 1 << 4, |
| 75 |
BUS_NONSPI = BUS_PARALLEL | BUS_LPC | BUS_FWH, |
| 76 |
}; |
| 77 |
|
| 78 |
/* |
| 79 |
* The following enum defines possible write granularities of flash chips. These tend to reflect the properties |
| 80 |
* of the actual hardware not necessarily the write function(s) defined by the respective struct flashchip. |
| 81 |
* The latter might (and should) be more precisely specified, e.g. they might bail out early if their execution |
| 82 |
* would result in undefined chip contents. |
| 83 |
*/ |
| 84 |
enum write_granularity { |
| 85 |
/* We assume 256 byte granularity by default. */ |
| 86 |
WRITE_GRAN_256BYTES = 0,/* If less than 256 bytes are written, the unwritten bytes are undefined. */ |
| 87 |
WRITE_GRAN_1BIT, /* Each bit can be cleared individually. */ |
| 88 |
WRITE_GRAN_1BYTE, /* A byte can be written once. Further writes to an already written byte cause |
| 89 |
* its contents to be either undefined or to stay unchanged. */ |
| 90 |
WRITE_GRAN_128BYTES, /* If less than 128 bytes are written, the unwritten bytes are undefined. */ |
| 91 |
WRITE_GRAN_264BYTES, /* If less than 264 bytes are written, the unwritten bytes are undefined. */ |
| 92 |
WRITE_GRAN_512BYTES, /* If less than 512 bytes are written, the unwritten bytes are undefined. */ |
| 93 |
WRITE_GRAN_528BYTES, /* If less than 528 bytes are written, the unwritten bytes are undefined. */ |
| 94 |
WRITE_GRAN_1024BYTES, /* If less than 1024 bytes are written, the unwritten bytes are undefined. */ |
| 95 |
WRITE_GRAN_1056BYTES, /* If less than 1056 bytes are written, the unwritten bytes are undefined. */ |
| 96 |
WRITE_GRAN_1BYTE_IMPLICIT_ERASE, /* EEPROMs and other chips with implicit erase and 1-byte writes. */ |
| 97 |
}; |
| 98 |
|
| 99 |
/* |
| 100 |
* How many different contiguous runs of erase blocks with one size each do |
| 101 |
* we have for a given erase function? |
| 102 |
*/ |
| 103 |
#define NUM_ERASEREGIONS 5 |
| 104 |
|
| 105 |
/* |
| 106 |
* How many different erase functions do we have per chip? |
| 107 |
* Macronix MX25L25635F has 8 different functions. |
| 108 |
*/ |
| 109 |
#define NUM_ERASEFUNCTIONS 8 |
| 110 |
|
| 111 |
#define MAX_CHIP_RESTORE_FUNCTIONS 4 |
| 112 |
|
| 113 |
/* Feature bits used for non-SPI only */ |
| 114 |
#define FEATURE_REGISTERMAP (1 << 0) |
| 115 |
#define FEATURE_LONG_RESET (0 << 4) |
| 116 |
#define FEATURE_SHORT_RESET (1 << 4) |
| 117 |
#define FEATURE_EITHER_RESET FEATURE_LONG_RESET |
| 118 |
#define FEATURE_RESET_MASK (FEATURE_LONG_RESET | FEATURE_SHORT_RESET) |
| 119 |
#define FEATURE_ADDR_FULL (0 << 2) |
| 120 |
#define FEATURE_ADDR_MASK (3 << 2) |
| 121 |
#define FEATURE_ADDR_2AA (1 << 2) |
| 122 |
#define FEATURE_ADDR_AAA (2 << 2) |
| 123 |
#define FEATURE_ADDR_SHIFTED (1 << 5) |
| 124 |
/* Feature bits used for SPI only */ |
| 125 |
#define FEATURE_WRSR_EWSR (1 << 6) |
| 126 |
#define FEATURE_WRSR_WREN (1 << 7) |
| 127 |
#define FEATURE_WRSR_EITHER (FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN) |
| 128 |
#define FEATURE_OTP (1 << 8) |
| 129 |
#define FEATURE_QPI (1 << 9) |
| 130 |
#define FEATURE_4BA_ENTER (1 << 10) /**< Can enter/exit 4BA mode with instructions 0xb7/0xe9 w/o WREN */ |
| 131 |
#define FEATURE_4BA_ENTER_WREN (1 << 11) /**< Can enter/exit 4BA mode with instructions 0xb7/0xe9 after WREN */ |
| 132 |
#define FEATURE_4BA_ENTER_EAR7 (1 << 12) /**< Can enter/exit 4BA mode by setting bit7 of the ext addr reg */ |
| 133 |
#define FEATURE_4BA_EAR_C5C8 (1 << 13) /**< Regular 3-byte operations can be used by writing the most |
| 134 |
significant address byte into an extended address register |
| 135 |
(using 0xc5/0xc8 instructions). */ |
| 136 |
#define FEATURE_4BA_EAR_1716 (1 << 14) /**< Like FEATURE_4BA_EAR_C5C8 but with 0x17/0x16 instructions. */ |
| 137 |
#define FEATURE_4BA_READ (1 << 15) /**< Native 4BA read instruction (0x13) is supported. */ |
| 138 |
#define FEATURE_4BA_FAST_READ (1 << 16) /**< Native 4BA fast read instruction (0x0c) is supported. */ |
| 139 |
#define FEATURE_4BA_WRITE (1 << 17) /**< Native 4BA byte program (0x12) is supported. */ |
| 140 |
/* 4BA Shorthands */ |
| 141 |
#define FEATURE_4BA_EAR_ANY (FEATURE_4BA_EAR_C5C8 | FEATURE_4BA_EAR_1716) |
| 142 |
#define FEATURE_4BA_NATIVE (FEATURE_4BA_READ | FEATURE_4BA_FAST_READ | FEATURE_4BA_WRITE) |
| 143 |
#define FEATURE_4BA (FEATURE_4BA_ENTER | FEATURE_4BA_EAR_C5C8 | FEATURE_4BA_NATIVE) |
| 144 |
#define FEATURE_4BA_WREN (FEATURE_4BA_ENTER_WREN | FEATURE_4BA_EAR_C5C8 | FEATURE_4BA_NATIVE) |
| 145 |
#define FEATURE_4BA_EAR7 (FEATURE_4BA_ENTER_EAR7 | FEATURE_4BA_EAR_C5C8 | FEATURE_4BA_NATIVE) |
| 146 |
/* |
| 147 |
* Most flash chips are erased to ones and programmed to zeros. However, some |
| 148 |
* other flash chips, such as the ENE KB9012 internal flash, work the opposite way. |
| 149 |
*/ |
| 150 |
#define FEATURE_ERASED_ZERO (1 << 18) |
| 151 |
/* |
| 152 |
* Feature indicates that the chip does not require erase before writing: |
| 153 |
* write operations can set any bit to any value without first doing an erase, |
| 154 |
* but bulk erase operations may still be supported. |
| 155 |
* |
| 156 |
* EEPROMs usually behave this way (compare to Flash, which requires erase), |
| 157 |
* for example the ST M95M02. |
| 158 |
*/ |
| 159 |
#define FEATURE_NO_ERASE (1 << 19) |
| 160 |
|
| 161 |
#define FEATURE_WRSR_EXT2 (1 << 20) |
| 162 |
#define FEATURE_WRSR2 (1 << 21) |
| 163 |
#define FEATURE_WRSR_EXT3 ((1 << 22) | FEATURE_WRSR_EXT2) |
| 164 |
#define FEATURE_WRSR3 (1 << 23) |
| 165 |
|
| 166 |
/* |
| 167 |
* Whether chip has security register (RDSCUR/WRSCUR commands). |
| 168 |
* Not to be confused with "secure registers" of OTP. |
| 169 |
*/ |
| 170 |
#define FEATURE_SCUR (1 << 24) |
| 171 |
|
| 172 |
/* Whether chip has configuration register (RDCR/WRSR_EXT2 commands) */ |
| 173 |
#define FEATURE_CFGR (1 << 25) |
| 174 |
|
| 175 |
/* |
| 176 |
* Whether the chip supports serial flash hardening specified in JESD260 |
| 177 |
*/ |
| 178 |
#define FEATURE_FLASH_HARDENING (1 << 26) |
| 179 |
|
| 180 |
#define ERASED_VALUE(flash) (((flash)->chip->feature_bits & FEATURE_ERASED_ZERO) ? 0x00 : 0xff) |
| 181 |
#define UNERASED_VALUE(flash) (((flash)->chip->feature_bits & FEATURE_ERASED_ZERO) ? 0xff : 0x00) |
| 182 |
|
| 183 |
enum test_state { |
| 184 |
OK = 0, |
| 185 |
NT = 1, /* Not tested */ |
| 186 |
BAD, /* Known to not work */ |
| 187 |
DEP, /* Support depends on configuration (e.g. Intel flash descriptor) */ |
| 188 |
NA, /* Not applicable (e.g. write support on ROM chips) */ |
| 189 |
}; |
| 190 |
|
| 191 |
#define TEST_UNTESTED (struct tested){ .probe = NT, .read = NT, .erase = NT, .write = NT, .wp = NT } |
| 192 |
|
| 193 |
#define TEST_OK_PROBE (struct tested){ .probe = OK, .read = NT, .erase = NT, .write = NT, .wp = NT } |
| 194 |
#define TEST_OK_PR (struct tested){ .probe = OK, .read = OK, .erase = NT, .write = NT, .wp = NT } |
| 195 |
#define TEST_OK_PRE (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = NT, .wp = NT } |
| 196 |
#define TEST_OK_PREW (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = OK, .wp = NT } |
| 197 |
#define TEST_OK_PREWB (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = OK, .wp = OK } |
| 198 |
|
| 199 |
#define TEST_BAD_PROBE (struct tested){ .probe = BAD, .read = NT, .erase = NT, .write = NT, .wp = NT } |
| 200 |
#define TEST_BAD_PR (struct tested){ .probe = BAD, .read = BAD, .erase = NT, .write = NT, .wp = NT } |
| 201 |
#define TEST_BAD_PRE (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = NT, .wp = NT } |
| 202 |
#define TEST_BAD_PREW (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD, .wp = NT } |
| 203 |
#define TEST_BAD_PREWB (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD, .wp = BAD } |
| 204 |
|
| 205 |
struct flashrom_flashctx; |
| 206 |
#define flashctx flashrom_flashctx /* TODO: Agree on a name and convert all occurrences. */ |
| 207 |
typedef int (erasefunc_t)(struct flashctx *flash, unsigned int addr, unsigned int blocklen); |
| 208 |
|
| 209 |
enum flash_reg { |
| 210 |
INVALID_REG = 0, |
| 211 |
STATUS1, |
| 212 |
STATUS2, |
| 213 |
STATUS3, |
| 214 |
SECURITY, |
| 215 |
CONFIG, |
| 216 |
MAX_REGISTERS |
| 217 |
}; |
| 218 |
|
| 219 |
struct reg_bit_info { |
| 220 |
/* Register containing the bit */ |
| 221 |
enum flash_reg reg; |
| 222 |
|
| 223 |
/* Bit index within register */ |
| 224 |
uint8_t bit_index; |
| 225 |
|
| 226 |
/* |
| 227 |
* Writability of the bit. RW does not guarantee the bit will be |
| 228 |
* writable, for example if status register protection is enabled. |
| 229 |
*/ |
| 230 |
enum { |
| 231 |
RO, /* Read only */ |
| 232 |
RW, /* Readable and writable */ |
| 233 |
OTP /* One-time programmable */ |
| 234 |
} writability; |
| 235 |
}; |
| 236 |
|
| 237 |
struct wp_bits; |
| 238 |
|
| 239 |
enum decode_range_func { |
| 240 |
NO_DECODE_RANGE_FUNC = 0, /* 0 indicates no range decode function is set. */ |
| 241 |
DECODE_RANGE_SPI25 = 1, |
| 242 |
DECODE_RANGE_SPI25_64K_BLOCK = 2, |
| 243 |
DECODE_RANGE_SPI25_BIT_CMP = 3, |
| 244 |
DECODE_RANGE_SPI25_2X_BLOCK = 4, |
| 245 |
}; |
| 246 |
typedef void (decode_range_func_t)(size_t *start, size_t *len, const struct wp_bits *, size_t chip_len); |
| 247 |
|
| 248 |
enum probe_func { |
| 249 |
NO_PROBE_FUNC = 0, /* 0 indicates no probe function set. */ |
| 250 |
PROBE_JEDEC = 1, |
| 251 |
PROBE_JEDEC_29GL, |
| 252 |
PROBE_OPAQUE, |
| 253 |
PROBE_EDI_KB9012, |
| 254 |
PROBE_AT82802AB, |
| 255 |
PROBE_W29EE011, |
| 256 |
PROBE_EN29LV640B, |
| 257 |
PROBE_SPI_AT25F, |
| 258 |
PROBE_SPI_AT45DB, |
| 259 |
PROBE_SPI_BIG_SPANSION, |
| 260 |
PROBE_SPI_RDID, |
| 261 |
PROBE_SPI_RDID4, |
| 262 |
PROBE_SPI_REMS, |
| 263 |
PROBE_SPI_RES1, |
| 264 |
PROBE_SPI_RES2, |
| 265 |
PROBE_SPI_SFDP, |
| 266 |
PROBE_SPI_ST95, |
| 267 |
}; |
| 268 |
|
| 269 |
enum write_func { |
| 270 |
NO_WRITE_FUNC = 0, /* 0 indicates no write function set. */ |
| 271 |
WRITE_JEDEC = 1, |
| 272 |
WRITE_JEDEC1, |
| 273 |
WRITE_OPAQUE, |
| 274 |
SPI_CHIP_WRITE1, |
| 275 |
SPI_CHIP_WRITE256, |
| 276 |
SPI_WRITE_AAI, |
| 277 |
SPI_WRITE_AT45DB, |
| 278 |
WRITE_28SF040, |
| 279 |
WRITE_82802AB, |
| 280 |
WRITE_EN29LV640B, |
| 281 |
EDI_CHIP_WRITE, |
| 282 |
#ifdef FLASHROM_TEST |
| 283 |
TEST_WRITE_INJECTOR, /* special case must come last. */ |
| 284 |
#endif |
| 285 |
}; |
| 286 |
typedef int (write_func_t)(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len); |
| 287 |
|
| 288 |
#ifdef FLASHROM_TEST |
| 289 |
extern write_func_t *g_test_write_injector; |
| 290 |
#endif |
| 291 |
|
| 292 |
enum read_func { |
| 293 |
NO_READ_FUNC = 0, /* 0 indicates no read function set. */ |
| 294 |
SPI_CHIP_READ = 1, |
| 295 |
READ_OPAQUE, |
| 296 |
READ_MEMMAPPED, |
| 297 |
EDI_CHIP_READ, |
| 298 |
SPI_READ_AT45DB, |
| 299 |
SPI_READ_AT45DB_E8, |
| 300 |
#ifdef FLASHROM_TEST |
| 301 |
TEST_READ_INJECTOR, /* special case must come last. */ |
| 302 |
#endif |
| 303 |
}; |
| 304 |
typedef int (read_func_t)(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len); |
| 305 |
int read_flash(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len); |
| 306 |
|
| 307 |
#ifdef FLASHROM_TEST |
| 308 |
extern read_func_t *g_test_read_injector; |
| 309 |
#endif |
| 310 |
|
| 311 |
enum block_erase_func { |
| 312 |
NO_BLOCK_ERASE_FUNC = 0, /* 0 indicates no block erase function set. */ |
| 313 |
SPI_BLOCK_ERASE_EMULATION = 1, |
| 314 |
SPI_BLOCK_ERASE_20, |
| 315 |
SPI_BLOCK_ERASE_21, |
| 316 |
SPI_BLOCK_ERASE_40, |
| 317 |
SPI_BLOCK_ERASE_50, |
| 318 |
SPI_BLOCK_ERASE_52, |
| 319 |
SPI_BLOCK_ERASE_53, |
| 320 |
SPI_BLOCK_ERASE_5C, |
| 321 |
SPI_BLOCK_ERASE_60, |
| 322 |
SPI_BLOCK_ERASE_62, |
| 323 |
SPI_BLOCK_ERASE_81, |
| 324 |
SPI_BLOCK_ERASE_C4, |
| 325 |
SPI_BLOCK_ERASE_C7, |
| 326 |
SPI_BLOCK_ERASE_D7, |
| 327 |
SPI_BLOCK_ERASE_D8, |
| 328 |
SPI_BLOCK_ERASE_DB, |
| 329 |
SPI_BLOCK_ERASE_DC, |
| 330 |
S25FL_BLOCK_ERASE, |
| 331 |
S25FS_BLOCK_ERASE_D8, |
| 332 |
JEDEC_SECTOR_ERASE, |
| 333 |
JEDEC_BLOCK_ERASE, |
| 334 |
JEDEC_CHIP_BLOCK_ERASE, |
| 335 |
OPAQUE_ERASE, |
| 336 |
SPI_ERASE_AT45CS_SECTOR, |
| 337 |
SPI_ERASE_AT45DB_BLOCK, |
| 338 |
SPI_ERASE_AT45DB_CHIP, |
| 339 |
SPI_ERASE_AT45DB_PAGE, |
| 340 |
SPI_ERASE_AT45DB_SECTOR, |
| 341 |
ERASE_CHIP_28SF040, |
| 342 |
ERASE_SECTOR_28SF040, |
| 343 |
ERASE_BLOCK_82802AB, |
| 344 |
ERASE_SECTOR_49LFXXXC, |
| 345 |
STM50_SECTOR_ERASE, |
| 346 |
EDI_CHIP_BLOCK_ERASE, |
| 347 |
#ifdef FLASHROM_TEST |
| 348 |
/* special cases must come last. */ |
| 349 |
TEST_ERASE_INJECTOR_1, |
| 350 |
TEST_ERASE_INJECTOR_2, |
| 351 |
TEST_ERASE_INJECTOR_3, |
| 352 |
TEST_ERASE_INJECTOR_4, |
| 353 |
TEST_ERASE_INJECTOR_5, |
| 354 |
#endif |
| 355 |
}; |
| 356 |
|
| 357 |
#ifdef FLASHROM_TEST |
| 358 |
#define NUM_TEST_ERASE_INJECTORS 5 |
| 359 |
extern erasefunc_t *g_test_erase_injector[NUM_TEST_ERASE_INJECTORS]; |
| 360 |
#endif |
| 361 |
|
| 362 |
enum blockprotect_func { |
| 363 |
NO_BLOCKPROTECT_FUNC = 0, /* 0 indicates no unlock function set. */ |
| 364 |
SPI_DISABLE_BLOCKPROTECT, |
| 365 |
SPI_DISABLE_BLOCKPROTECT_BP2_EP_SRWD, |
| 366 |
SPI_DISABLE_BLOCKPROTECT_BP1_SRWD, |
| 367 |
SPI_DISABLE_BLOCKPROTECT_BP2_SRWD, |
| 368 |
SPI_DISABLE_BLOCKPROTECT_BP3_SRWD, |
| 369 |
SPI_DISABLE_BLOCKPROTECT_BP4_SRWD, |
| 370 |
SPI_DISABLE_BLOCKPROTECT_AT45DB, |
| 371 |
SPI_DISABLE_BLOCKPROTECT_AT25F, |
| 372 |
SPI_DISABLE_BLOCKPROTECT_AT25FS010, |
| 373 |
SPI_DISABLE_BLOCKPROTECT_AT25FS040, |
| 374 |
SPI_DISABLE_BLOCKPROTECT_AT25F512A, |
| 375 |
SPI_DISABLE_BLOCKPROTECT_AT25F512B, |
| 376 |
SPI_DISABLE_BLOCKPROTECT_AT2X_GLOBAL_UNPROTECT, |
| 377 |
SPI_DISABLE_BLOCKPROTECT_AT2X_GLOBAL_UNPROTECT_SEC, |
| 378 |
SPI_DISABLE_BLOCKPROTECT_SST26_GLOBAL_UNPROTECT, |
| 379 |
SPI_DISABLE_BLOCKPROTECT_N25Q, |
| 380 |
UNLOCK_REGSPACE2_BLOCK_ERASER_0, |
| 381 |
UNLOCK_REGSPACE2_BLOCK_ERASER_1, |
| 382 |
UNLOCK_REGSPACE2_UNIFORM_32K, |
| 383 |
UNLOCK_REGSPACE2_UNIFORM_64K, |
| 384 |
UNLOCK_28F004S5, |
| 385 |
UNLOCK_LH28F008BJT, |
| 386 |
UNLOCK_SST_FWHUB, |
| 387 |
UNPROTECT_28SF040, |
| 388 |
}; |
| 389 |
|
| 390 |
enum printlock_func { |
| 391 |
NO_PRINTLOCK_FUNC, |
| 392 |
PRINTLOCK_AT49F, |
| 393 |
PRINTLOCK_REGSPACE2_BLOCK_ERASER_0, |
| 394 |
PRINTLOCK_REGSPACE2_BLOCK_ERASER_1, |
| 395 |
PRINTLOCK_SST_FWHUB, |
| 396 |
PRINTLOCK_W39F010, |
| 397 |
PRINTLOCK_W39L010, |
| 398 |
PRINTLOCK_W39L020, |
| 399 |
PRINTLOCK_W39L040, |
| 400 |
PRINTLOCK_W39V040A, |
| 401 |
PRINTLOCK_W39V040B, |
| 402 |
PRINTLOCK_W39V040C, |
| 403 |
PRINTLOCK_W39V040FA, |
| 404 |
PRINTLOCK_W39V040FB, |
| 405 |
PRINTLOCK_W39V040FC, |
| 406 |
PRINTLOCK_W39V080A, |
| 407 |
PRINTLOCK_W39V080FA, |
| 408 |
PRINTLOCK_W39V080FA_DUAL, |
| 409 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT25DF, |
| 410 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT25DF_SEC, |
| 411 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT25F, |
| 412 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT25F4096, |
| 413 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT25F512A, |
| 414 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT25F512B, |
| 415 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT25FS010, |
| 416 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT25FS040, |
| 417 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT26DF081A, |
| 418 |
SPI_PRETTYPRINT_STATUS_REGISTER_AT45DB, |
| 419 |
SPI_PRETTYPRINT_STATUS_REGISTER_BP1_SRWD, |
| 420 |
SPI_PRETTYPRINT_STATUS_REGISTER_BP2_BPL, |
| 421 |
SPI_PRETTYPRINT_STATUS_REGISTER_BP2_EP_SRWD, |
| 422 |
SPI_PRETTYPRINT_STATUS_REGISTER_BP2_SRWD, |
| 423 |
SPI_PRETTYPRINT_STATUS_REGISTER_BP2_TB_BPL, |
| 424 |
SPI_PRETTYPRINT_STATUS_REGISTER_SRWD_SEC_TB_BP2_WELWIP, |
| 425 |
SPI_PRETTYPRINT_STATUS_REGISTER_BP3_SRWD, |
| 426 |
SPI_PRETTYPRINT_STATUS_REGISTER_BP4_SRWD, |
| 427 |
SPI_PRETTYPRINT_STATUS_REGISTER_DEFAULT_WELWIP, |
| 428 |
SPI_PRETTYPRINT_STATUS_REGISTER_EN25S_WP, |
| 429 |
SPI_PRETTYPRINT_STATUS_REGISTER_N25Q, |
| 430 |
SPI_PRETTYPRINT_STATUS_REGISTER_PLAIN, |
| 431 |
SPI_PRETTYPRINT_STATUS_REGISTER_SST25, |
| 432 |
SPI_PRETTYPRINT_STATUS_REGISTER_SST25VF016, |
| 433 |
SPI_PRETTYPRINT_STATUS_REGISTER_SST25VF040B, |
| 434 |
}; |
| 435 |
typedef int (printlockfunc_t)(struct flashctx *flash); |
| 436 |
printlockfunc_t *lookup_printlock_func_ptr(struct flashctx *flash); |
| 437 |
|
| 438 |
struct flashchip { |
| 439 |
const char *vendor; |
| 440 |
const char *name; |
| 441 |
|
| 442 |
enum chipbustype bustype; |
| 443 |
|
| 444 |
/* |
| 445 |
* With 32bit manufacture_id and model_id we can cover IDs up to |
| 446 |
* (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's |
| 447 |
* Identification code. |
| 448 |
*/ |
| 449 |
uint32_t manufacture_id; |
| 450 |
uint32_t model_id; |
| 451 |
|
| 452 |
/* Total chip size in kilobytes */ |
| 453 |
unsigned int total_size; |
| 454 |
/* Chip page size in bytes */ |
| 455 |
unsigned int page_size; |
| 456 |
int feature_bits; |
| 457 |
|
| 458 |
/* Indicate how well flashrom supports different operations of this flash chip. */ |
| 459 |
struct tested { |
| 460 |
enum test_state probe; |
| 461 |
enum test_state read; |
| 462 |
enum test_state erase; |
| 463 |
enum test_state write; |
| 464 |
enum test_state wp; |
| 465 |
} tested; |
| 466 |
|
| 467 |
/* |
| 468 |
* Group chips that have common command sets. This should ensure that |
| 469 |
* no chip gets confused by a probing command for a very different class |
| 470 |
* of chips. |
| 471 |
*/ |
| 472 |
enum { |
| 473 |
/* SPI25 is very common. Keep it at zero so we don't have |
| 474 |
to specify it for each and every chip in the database.*/ |
| 475 |
SPI25 = 0, |
| 476 |
SPI_EDI = 1, |
| 477 |
} spi_cmd_set; |
| 478 |
|
| 479 |
enum probe_func probe; |
| 480 |
|
| 481 |
/* Delay after "enter/exit ID mode" commands in microseconds. |
| 482 |
* NB: negative values have special meanings, see TIMING_* below. |
| 483 |
*/ |
| 484 |
signed int probe_timing; |
| 485 |
|
| 486 |
/* |
| 487 |
* Erase blocks and associated erase function. Any chip erase function |
| 488 |
* is stored as chip-sized virtual block together with said function. |
| 489 |
* The logic for how to optimally select erase functions is in erasure_layout.c |
| 490 |
*/ |
| 491 |
struct block_eraser { |
| 492 |
struct eraseblock { |
| 493 |
unsigned int size; /* Eraseblock size in bytes */ |
| 494 |
unsigned int count; /* Number of contiguous blocks with that size */ |
| 495 |
} eraseblocks[NUM_ERASEREGIONS]; |
| 496 |
/* a block_erase function should try to erase one block of size |
| 497 |
* 'blocklen' at address 'blockaddr' and return 0 on success. */ |
| 498 |
enum block_erase_func block_erase; |
| 499 |
} block_erasers[NUM_ERASEFUNCTIONS]; |
| 500 |
|
| 501 |
enum printlock_func printlock; |
| 502 |
enum blockprotect_func unlock; |
| 503 |
enum write_func write; |
| 504 |
enum read_func read; |
| 505 |
struct voltage { |
| 506 |
uint16_t min; |
| 507 |
uint16_t max; |
| 508 |
} voltage; |
| 509 |
enum write_granularity gran; |
| 510 |
|
| 511 |
struct reg_bit_map { |
| 512 |
/* Status register protection bit (SRP) */ |
| 513 |
struct reg_bit_info srp; |
| 514 |
|
| 515 |
/* Status register lock bit (SRP) */ |
| 516 |
struct reg_bit_info srl; |
| 517 |
|
| 518 |
/* |
| 519 |
* Note: some datasheets refer to configuration bits that |
| 520 |
* function like TB/SEC/CMP bits as BP bits (e.g. BP3 for a bit |
| 521 |
* that functions like TB). |
| 522 |
* |
| 523 |
* As a convention, any config bit that functions like a |
| 524 |
* TB/SEC/CMP bit should be assigned to the respective |
| 525 |
* tb/sec/cmp field in this structure, even if the datasheet |
| 526 |
* uses a different name. |
| 527 |
*/ |
| 528 |
|
| 529 |
/* Block protection bits (BP) */ |
| 530 |
/* Extra element for terminator */ |
| 531 |
struct reg_bit_info bp[MAX_BP_BITS + 1]; |
| 532 |
|
| 533 |
/* Top/bottom protection bit (TB) */ |
| 534 |
struct reg_bit_info tb; |
| 535 |
|
| 536 |
/* Sector/block protection bit (SEC) */ |
| 537 |
struct reg_bit_info sec; |
| 538 |
|
| 539 |
/* Complement bit (CMP) */ |
| 540 |
struct reg_bit_info cmp; |
| 541 |
|
| 542 |
/* Write Protect Selection (per sector protection when set) */ |
| 543 |
struct reg_bit_info wps; |
| 544 |
} reg_bits; |
| 545 |
|
| 546 |
/* |
| 547 |
* Function that takes a set of WP config bits (e.g. BP, SEC, TB, etc) |
| 548 |
* and determines what protection range they select. |
| 549 |
*/ |
| 550 |
enum decode_range_func decode_range; |
| 551 |
|
| 552 |
struct rpmc_config { |
| 553 |
uint8_t op1_opcode; |
| 554 |
uint8_t op2_opcode; |
| 555 |
|
| 556 |
unsigned int num_counters; |
| 557 |
|
| 558 |
/* |
| 559 |
* Busy Polling Method : |
| 560 |
* ‘0’: Poll for OP1 busy using OP2 Extended Status[0]. |
| 561 |
* No OP1 Suspended State Support. |
| 562 |
* ‘1’: Poll for OP1 busy using Read Status (05H). |
| 563 |
* Suspended State is supported. |
| 564 |
*/ |
| 565 |
enum busy_polling_methods { |
| 566 |
POLL_OP2_EXTENDED_STATUS = 0, |
| 567 |
POLL_READ_STATUS = 1 |
| 568 |
} busy_polling_method; |
| 569 |
|
| 570 |
unsigned int update_rate; |
| 571 |
|
| 572 |
/* All times in microsecond (us) */ |
| 573 |
unsigned int polling_delay_read_counter_us; |
| 574 |
unsigned int polling_short_delay_write_counter_us; |
| 575 |
unsigned int polling_long_delay_write_counter_us; |
| 576 |
} rpmc_ctx; |
| 577 |
}; |
| 578 |
|
| 579 |
typedef int (*chip_restore_fn_cb_t)(struct flashctx *flash, void *data); |
| 580 |
typedef int (blockprotect_func_t)(struct flashctx *flash); |
| 581 |
blockprotect_func_t *lookup_blockprotect_func_ptr(const struct flashchip *const chip); |
| 582 |
|
| 583 |
struct stage_progress { |
| 584 |
size_t current; |
| 585 |
size_t total; |
| 586 |
}; |
| 587 |
|
| 588 |
struct flashrom_flashctx { |
| 589 |
struct flashchip *chip; |
| 590 |
/* FIXME: The memory mappings should be saved in a more structured way. */ |
| 591 |
/* The physical_* fields store the respective addresses in the physical address space of the CPU. */ |
| 592 |
uintptr_t physical_memory; |
| 593 |
/* The virtual_* fields store where the respective physical address is mapped into flashrom's address |
| 594 |
* space. A value equivalent to (chipaddr)ERROR_PTR indicates an invalid mapping (or none at all). */ |
| 595 |
chipaddr virtual_memory; |
| 596 |
/* Some flash devices have an additional register space; semantics are like above. */ |
| 597 |
uintptr_t physical_registers; |
| 598 |
chipaddr virtual_registers; |
| 599 |
struct registered_master *mst; |
| 600 |
const struct flashrom_layout *layout; |
| 601 |
struct flashrom_layout *default_layout; |
| 602 |
struct { |
| 603 |
bool force; |
| 604 |
bool force_boardmismatch; |
| 605 |
bool verify_after_write; |
| 606 |
bool verify_whole_chip; |
| 607 |
bool skip_unreadable_regions; |
| 608 |
bool skip_unwritable_regions; |
| 609 |
} flags; |
| 610 |
/* We cache the state of the extended address register (highest byte |
| 611 |
* of a 4BA for 3BA instructions) and the state of the 4BA mode here. |
| 612 |
* If possible, we enter 4BA mode early. If that fails, we make use |
| 613 |
* of the extended address register. |
| 614 |
*/ |
| 615 |
int address_high_byte; |
| 616 |
bool in_4ba_mode; |
| 617 |
|
| 618 |
int chip_restore_fn_count; |
| 619 |
struct chip_restore_func_data { |
| 620 |
chip_restore_fn_cb_t func; |
| 621 |
void *data; |
| 622 |
} chip_restore_fn[MAX_CHIP_RESTORE_FUNCTIONS]; |
| 623 |
/* Progress reporting */ |
| 624 |
flashrom_progress_callback_v2 *progress_callback; |
| 625 |
struct flashrom_progress progress_state; |
| 626 |
struct stage_progress stage_progress[FLASHROM_PROGRESS_NR]; |
| 627 |
/* deprecated, do not use */ |
| 628 |
flashrom_progress_callback *deprecated_progress_callback; |
| 629 |
struct flashrom_progress *deprecated_progress_state; |
| 630 |
|
| 631 |
/* Maximum allowed % of redundant erase */ |
| 632 |
int sacrifice_ratio; |
| 633 |
}; |
| 634 |
|
| 635 |
/* Timing used in probe routines. ZERO is -2 to differentiate between an unset |
| 636 |
* field and zero delay. |
| 637 |
* |
| 638 |
* SPI devices will always have zero delay and ignore this field. |
| 639 |
*/ |
| 640 |
#define TIMING_FIXME -1 |
| 641 |
/* this is intentionally same value as fixme */ |
| 642 |
#define TIMING_IGNORED -1 |
| 643 |
#define TIMING_ZERO -2 |
| 644 |
|
| 645 |
extern const struct flashchip flashchips[]; |
| 646 |
extern const unsigned int flashchips_size; |
| 647 |
|
| 648 |
/* parallel.c */ |
| 649 |
void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
| 650 |
void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr); |
| 651 |
void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr); |
| 652 |
void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len); |
| 653 |
uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr); |
| 654 |
uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr); |
| 655 |
uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr); |
| 656 |
void chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len); |
| 657 |
|
| 658 |
/* print.c */ |
| 659 |
int print_supported(void); |
| 660 |
|
| 661 |
/* helpers.c */ |
| 662 |
uint32_t address_to_bits(uint32_t addr); |
| 663 |
unsigned int bitcount(unsigned long a); |
| 664 |
#undef MIN |
| 665 |
#define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 666 |
#undef MAX |
| 667 |
#define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 668 |
int max(int a, int b); |
| 669 |
int min(int a, int b); |
| 670 |
char *strcat_realloc(char *dest, const char *src); |
| 671 |
void tolower_string(char *str); |
| 672 |
uint8_t reverse_byte(uint8_t x); |
| 673 |
void reverse_bytes(uint8_t *dst, const uint8_t *src, size_t length); |
| 674 |
#ifdef __MINGW32__ |
| 675 |
char* strtok_r(char *str, const char *delim, char **nextp); |
| 676 |
char *strndup(const char *str, size_t size); |
| 677 |
#endif |
| 678 |
#if defined(__DJGPP__) || (!defined(__LIBPAYLOAD__) && !defined(HAVE_STRNLEN)) |
| 679 |
size_t strnlen(const char *str, size_t n); |
| 680 |
#endif |
| 681 |
|
| 682 |
/* flashrom.c */ |
| 683 |
extern const char flashrom_version[]; |
| 684 |
char *flashbuses_to_text(enum chipbustype bustype); |
| 685 |
int map_flash(struct flashctx *flash); |
| 686 |
void unmap_flash(struct flashctx *flash); |
| 687 |
int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len); |
| 688 |
int erase_flash(struct flashctx *flash); |
| 689 |
int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force, const char *const chip_to_probe); |
| 690 |
int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len); |
| 691 |
void emergency_help_message(void); |
| 692 |
void print_version(void); |
| 693 |
void print_buildinfo(void); |
| 694 |
void print_banner(void); |
| 695 |
void list_programmers_linebreak(int startcol, int cols, int paren); |
| 696 |
int selfcheck(void); |
| 697 |
int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename); |
| 698 |
int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename); |
| 699 |
int prepare_flash_access(struct flashctx *, bool read_it, bool write_it, bool erase_it, bool verify_it); |
| 700 |
void finalize_flash_access(struct flashctx *); |
| 701 |
int register_chip_restore(chip_restore_fn_cb_t func, struct flashctx *flash, void *data); |
| 702 |
int check_block_eraser(const struct flashctx *flash, int k, int log); |
| 703 |
unsigned int count_usable_erasers(const struct flashctx *flash); |
| 704 |
int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, enum write_granularity gran, const uint8_t erased_value); |
| 705 |
erasefunc_t *lookup_erase_func_ptr(const struct block_eraser *const eraser); |
| 706 |
int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len); |
| 707 |
unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len, unsigned int *first_start, enum write_granularity gran); |
| 708 |
int write_flash(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len); |
| 709 |
|
| 710 |
/* Something happened that shouldn't happen, but we can go on. */ |
| 711 |
#define ERROR_FLASHROM_NONFATAL 0x100 |
| 712 |
|
| 713 |
/* Something happened that shouldn't happen, we'll abort. */ |
| 714 |
#define ERROR_FLASHROM_FATAL -0xee |
| 715 |
#define ERROR_FLASHROM_BUG -200 |
| 716 |
/* We reached one of the hardcoded limits of flashrom. This can be fixed by |
| 717 |
* increasing the limit of a compile-time allocation or by switching to dynamic |
| 718 |
* allocation. |
| 719 |
* Note: If this warning is triggered, check first for runaway registrations. |
| 720 |
*/ |
| 721 |
#define ERROR_FLASHROM_LIMIT -201 |
| 722 |
|
| 723 |
#define ERROR_FLASHROM_PROBE_NO_CHIPS_FOUND -1 |
| 724 |
#define ERROR_FLASHROM_PROBE_INTERNAL_ERROR -2 |
| 725 |
|
| 726 |
struct cli_progress { |
| 727 |
unsigned int stage_pc[FLASHROM_PROGRESS_NR]; |
| 728 |
unsigned int visible_stages; /* Bitmask of stages with non-zero progress. */ |
| 729 |
bool stage_setup; /* Flag to know when to reset progress data. */ |
| 730 |
}; |
| 731 |
|
| 732 |
/* cli_common.c */ |
| 733 |
void print_chip_support_status(const struct flashchip *chip); |
| 734 |
|
| 735 |
/* libflashrom.c */ |
| 736 |
/* |
| 737 |
* Let gcc and clang check for correct printf-style format strings. |
| 738 |
* Avoid using formatted messages longer than 256 characters. |
| 739 |
* The new public logging API formats messages and passes a ready-to-print |
| 740 |
* string to the user callback for performance reasons. |
| 741 |
* Therefore, any message exceeding 256 characters will be truncated, |
| 742 |
* and an ERANGE error code will be returned. |
| 743 |
*/ |
| 744 |
int print(enum flashrom_log_level level, const char *fmt, ...) |
| 745 |
#ifdef __MINGW32__ |
| 746 |
# ifndef __MINGW_PRINTF_FORMAT |
| 747 |
# define __MINGW_PRINTF_FORMAT gnu_printf |
| 748 |
# endif |
| 749 |
__attribute__((format(__MINGW_PRINTF_FORMAT, 2, 3))); |
| 750 |
#else |
| 751 |
__attribute__((format(printf, 2, 3))); |
| 752 |
#endif |
| 753 |
#define msg_gerr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* general errors */ |
| 754 |
#define msg_perr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* programmer errors */ |
| 755 |
#define msg_cerr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* chip errors */ |
| 756 |
#define msg_gwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* general warnings */ |
| 757 |
#define msg_pwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* programmer warnings */ |
| 758 |
#define msg_cwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* chip warnings */ |
| 759 |
#define msg_ginfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* general info */ |
| 760 |
#define msg_pinfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* programmer info */ |
| 761 |
#define msg_cinfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* chip info */ |
| 762 |
#define msg_gdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* general debug */ |
| 763 |
#define msg_pdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* programmer debug */ |
| 764 |
#define msg_cdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* chip debug */ |
| 765 |
#define msg_gdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* general debug2 */ |
| 766 |
#define msg_pdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* programmer debug2 */ |
| 767 |
#define msg_cdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* chip debug2 */ |
| 768 |
#define msg_gspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* general debug spew */ |
| 769 |
#define msg_pspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* programmer debug spew */ |
| 770 |
#define msg_cspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* chip debug spew */ |
| 771 |
void init_progress(struct flashctx *flash, enum flashrom_progress_stage stage, size_t total); |
| 772 |
void update_progress(struct flashctx *flash, enum flashrom_progress_stage stage, size_t increment); |
| 773 |
|
| 774 |
#endif /* !__FLASH_H__ */ |