stepan | 5c3f138 | 2007-02-06 19:47:50 +0000 | [diff] [blame] | 1 | /* |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
stepan | 5c3f138 | 2007-02-06 19:47:50 +0000 | [diff] [blame] | 3 | * |
uwe | 555dd97 | 2007-09-09 20:21:05 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
| 5 | * Copyright (C) 2000 Ronald G. Minnich <rminnich@gmail.com> |
stepan | 6d42c0f | 2009-08-12 09:27:45 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005-2009 coresystems GmbH |
hailfinger | 77c5d93 | 2009-06-15 12:10:57 +0000 | [diff] [blame] | 7 | * Copyright (C) 2006-2009 Carl-Daniel Hailfinger |
stepan | 5c3f138 | 2007-02-06 19:47:50 +0000 | [diff] [blame] | 8 | * |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 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. |
stepan | 5c3f138 | 2007-02-06 19:47:50 +0000 | [diff] [blame] | 13 | * |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 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. |
stepan | 5c3f138 | 2007-02-06 19:47:50 +0000 | [diff] [blame] | 18 | * |
| 19 | */ |
| 20 | |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 21 | #ifndef __FLASH_H__ |
| 22 | #define __FLASH_H__ 1 |
| 23 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 24 | #include <stdint.h> |
hailfinger | d43a4e3 | 2010-06-03 00:49:50 +0000 | [diff] [blame] | 25 | #include <stddef.h> |
hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 26 | #include "hwaccess.h" |
oxygene | 3ad3b33 | 2010-01-06 22:14:39 +0000 | [diff] [blame] | 27 | #ifdef _WIN32 |
| 28 | #include <windows.h> |
| 29 | #undef min |
| 30 | #undef max |
| 31 | #endif |
hailfinger | e1f062f | 2008-05-22 13:22:45 +0000 | [diff] [blame] | 32 | |
Stefan Reinauer | e64faaf | 2011-05-03 18:03:25 -0700 | [diff] [blame] | 33 | /* Are timers broken? */ |
| 34 | extern int broken_timer; |
| 35 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 36 | struct flashctx; /* forward declare */ |
hailfinger | f294fa2 | 2010-09-25 22:53:44 +0000 | [diff] [blame] | 37 | #define ERROR_PTR ((void*)-1) |
| 38 | |
hailfinger | ee9ee13 | 2010-10-08 00:37:55 +0000 | [diff] [blame] | 39 | /* Error codes */ |
| 40 | #define TIMEOUT_ERROR -101 |
| 41 | |
Edward O'Callaghan | 9b520dd | 2019-05-01 21:47:21 -0400 | [diff] [blame] | 42 | #define PRIxPTR_WIDTH ((int)(sizeof(uintptr_t)*2)) |
| 43 | |
Edward O'Callaghan | 1a3fd13 | 2019-06-04 14:18:55 +1000 | [diff] [blame] | 44 | /* for verify_it variable in flashrom.c and cli_classic.c */ |
Louis Yung-Chieh Lo | 5d95f04 | 2011-09-01 17:33:06 +0800 | [diff] [blame] | 45 | enum { |
| 46 | VERIFY_OFF = 0, |
| 47 | VERIFY_FULL, |
| 48 | VERIFY_PARTIAL, |
| 49 | }; |
| 50 | |
hailfinger | 8271963 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 51 | typedef unsigned long chipaddr; |
| 52 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 53 | int register_shutdown(int (*function) (void *data), void *data); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 54 | #define CHIP_RESTORE_CALLBACK int (*func) (struct flashctx *flash, uint8_t status) |
David Hendricks | bf36f09 | 2010-11-02 23:39:29 -0700 | [diff] [blame] | 55 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 56 | int register_chip_restore(CHIP_RESTORE_CALLBACK, struct flashctx *flash, uint8_t status); |
uwe | abe92a5 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 57 | void *programmer_map_flash_region(const char *descr, unsigned long phys_addr, |
| 58 | size_t len); |
| 59 | void programmer_unmap_flash_region(void *virt_addr, size_t len); |
hailfinger | e5829f6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 60 | void programmer_delay(int usecs); |
hailfinger | ba3761a | 2009-03-05 19:24:22 +0000 | [diff] [blame] | 61 | |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 62 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 63 | |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 64 | enum chipbustype { |
hailfinger | e1e41ea | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 65 | BUS_NONE = 0, |
| 66 | BUS_PARALLEL = 1 << 0, |
| 67 | BUS_LPC = 1 << 1, |
| 68 | BUS_FWH = 1 << 2, |
| 69 | BUS_SPI = 1 << 3, |
hailfinger | fe7cd9e | 2011-11-04 21:35:26 +0000 | [diff] [blame] | 70 | BUS_PROG = 1 << 4, |
hailfinger | e1e41ea | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 71 | BUS_NONSPI = BUS_PARALLEL | BUS_LPC | BUS_FWH, |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
David Hendricks | 80f62d2 | 2010-10-08 11:09:35 -0700 | [diff] [blame] | 74 | /* used to select bus which target chip resides */ |
| 75 | extern enum chipbustype target_bus; |
| 76 | |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 77 | /* |
| 78 | * How many different contiguous runs of erase blocks with one size each do |
| 79 | * we have for a given erase function? |
| 80 | */ |
| 81 | #define NUM_ERASEREGIONS 5 |
| 82 | |
| 83 | /* |
| 84 | * How many different erase functions do we have per chip? |
hailfinger | c33d473 | 2010-07-29 13:09:18 +0000 | [diff] [blame] | 85 | * Atmel AT25FS010 has 6 different functions. |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 86 | */ |
hailfinger | c33d473 | 2010-07-29 13:09:18 +0000 | [diff] [blame] | 87 | #define NUM_ERASEFUNCTIONS 6 |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 88 | |
hailfinger | 80dea31 | 2010-01-09 03:15:50 +0000 | [diff] [blame] | 89 | #define FEATURE_REGISTERMAP (1 << 0) |
| 90 | #define FEATURE_BYTEWRITES (1 << 1) |
snelson | c685534 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 91 | #define FEATURE_LONG_RESET (0 << 4) |
| 92 | #define FEATURE_SHORT_RESET (1 << 4) |
| 93 | #define FEATURE_EITHER_RESET FEATURE_LONG_RESET |
hailfinger | b07dc97 | 2010-10-20 21:13:19 +0000 | [diff] [blame] | 94 | #define FEATURE_RESET_MASK (FEATURE_LONG_RESET | FEATURE_SHORT_RESET) |
hailfinger | 80dea31 | 2010-01-09 03:15:50 +0000 | [diff] [blame] | 95 | #define FEATURE_ADDR_FULL (0 << 2) |
| 96 | #define FEATURE_ADDR_MASK (3 << 2) |
snelson | c685534 | 2010-01-28 23:55:12 +0000 | [diff] [blame] | 97 | #define FEATURE_ADDR_2AA (1 << 2) |
| 98 | #define FEATURE_ADDR_AAA (2 << 2) |
mkarcher | 9ded5fe | 2010-04-03 10:27:08 +0000 | [diff] [blame] | 99 | #define FEATURE_ADDR_SHIFTED (1 << 5) |
hailfinger | c33d473 | 2010-07-29 13:09:18 +0000 | [diff] [blame] | 100 | #define FEATURE_WRSR_EWSR (1 << 6) |
| 101 | #define FEATURE_WRSR_WREN (1 << 7) |
| 102 | #define FEATURE_WRSR_EITHER (FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN) |
David Hendricks | ff55cf6 | 2016-08-30 11:22:31 -0700 | [diff] [blame] | 103 | #define FEATURE_OTP (1 << 8) |
| 104 | #define FEATURE_ERASE_TO_ZERO (1 << 9) |
Duncan Laurie | 06ffd52 | 2015-10-26 12:56:08 -0700 | [diff] [blame] | 105 | #define FEATURE_UNBOUND_READ (1 << 10) |
William A. Kennington III | f15c2fa | 2017-04-07 17:38:42 -0700 | [diff] [blame] | 106 | #define FEATURE_NO_ERASE (1 << 11) |
Boris Baykov | 64d00c7 | 2016-06-11 18:28:59 +0200 | [diff] [blame] | 107 | #define FEATURE_4BA_SUPPORT (1 << 12) |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 108 | |
David Hendricks | 8c08421 | 2015-11-17 22:29:36 -0800 | [diff] [blame] | 109 | struct voltage_range { |
| 110 | uint16_t min, max; |
| 111 | }; |
| 112 | |
Patrick Georgi | ac3423f | 2017-02-03 20:58:06 +0100 | [diff] [blame] | 113 | enum test_state { |
| 114 | OK = 0, |
| 115 | NT = 1, /* Not tested */ |
| 116 | BAD, /* Known to not work */ |
| 117 | DEP, /* Support depends on configuration (e.g. Intel flash descriptor) */ |
| 118 | NA, /* Not applicable (e.g. write support on ROM chips) */ |
| 119 | }; |
| 120 | |
| 121 | #define TEST_UNTESTED (struct tested){ .probe = NT, .read = NT, .erase = NT, .write = NT, .uread = NT } |
| 122 | |
| 123 | #define TEST_OK_PROBE (struct tested){ .probe = OK, .read = NT, .erase = NT, .write = NT, .uread = NT } |
| 124 | #define TEST_OK_PR (struct tested){ .probe = OK, .read = OK, .erase = NT, .write = NT, .uread = NT } |
| 125 | #define TEST_OK_PRE (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = NT, .uread = NT } |
| 126 | #define TEST_OK_PRU (struct tested){ .probe = OK, .read = OK, .erase = NT, .write = NT, .uread = OK } |
| 127 | #define TEST_OK_PREU (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = NT, .uread = OK } |
| 128 | #define TEST_OK_PREW (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = OK, .uread = NT } |
| 129 | #define TEST_OK_PREWU (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = OK, .uread = OK } |
| 130 | |
| 131 | #define TEST_BAD_PROBE (struct tested){ .probe = BAD, .read = NT, .erase = NT, .write = NT, .uread = NT } |
| 132 | #define TEST_BAD_PR (struct tested){ .probe = BAD, .read = BAD, .erase = NT, .write = NT, .uread = NT } |
| 133 | #define TEST_BAD_PRE (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = NT, .uread = NT } |
| 134 | #define TEST_BAD_PREW (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD, .uread = NT } |
| 135 | #define TEST_BAD_PREWU (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD, .uread = BAD } |
| 136 | |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 137 | struct flashchip { |
uwe | dfcd15f | 2008-03-14 23:55:58 +0000 | [diff] [blame] | 138 | const char *vendor; |
uwe | 6ed6d95 | 2007-12-04 21:49:06 +0000 | [diff] [blame] | 139 | const char *name; |
hailfinger | 4016746 | 2009-05-31 17:57:34 +0000 | [diff] [blame] | 140 | |
| 141 | enum chipbustype bustype; |
| 142 | |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 143 | /* |
| 144 | * With 32bit manufacture_id and model_id we can cover IDs up to |
hailfinger | 428f201 | 2007-12-31 01:49:00 +0000 | [diff] [blame] | 145 | * (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's |
| 146 | * Identification code. |
| 147 | */ |
| 148 | uint32_t manufacture_id; |
| 149 | uint32_t model_id; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 150 | |
stefanct | 707f13b | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 151 | /* Total chip size in kilobytes */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 152 | unsigned int total_size; |
stefanct | 707f13b | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 153 | /* Chip page size in bytes */ |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 154 | unsigned int page_size; |
snelson | 63133f9 | 2010-01-04 17:15:23 +0000 | [diff] [blame] | 155 | int feature_bits; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 156 | |
Boris Baykov | 1a2f532 | 2016-06-11 18:29:00 +0200 | [diff] [blame] | 157 | /* set of function pointers to use in 4-bytes addressing mode */ |
| 158 | struct four_bytes_addr_funcs_set { |
Ed Swierk | 28cf799 | 2017-07-03 13:17:18 -0700 | [diff] [blame] | 159 | int (*set_4ba) (struct flashctx *flash); |
Boris Baykov | 1a2f532 | 2016-06-11 18:29:00 +0200 | [diff] [blame] | 160 | int (*read_nbyte) (struct flashctx *flash, unsigned int addr, uint8_t *bytes, unsigned int len); |
| 161 | int (*program_byte) (struct flashctx *flash, unsigned int addr, const uint8_t databyte); |
| 162 | int (*program_nbyte) (struct flashctx *flash, unsigned int addr, const uint8_t *bytes, unsigned int len); |
| 163 | } four_bytes_addr_funcs; |
| 164 | |
Patrick Georgi | ac3423f | 2017-02-03 20:58:06 +0100 | [diff] [blame] | 165 | /* Indicate how well flashrom supports different operations of this flash chip. */ |
| 166 | struct tested { |
| 167 | enum test_state probe; |
| 168 | enum test_state read; |
| 169 | enum test_state erase; |
| 170 | enum test_state write; |
| 171 | enum test_state uread; |
| 172 | } tested; |
stuge | 9cd64bd | 2008-05-03 04:34:37 +0000 | [diff] [blame] | 173 | |
Edward O'Callaghan | cc1d0c9 | 2019-02-24 15:35:07 +1100 | [diff] [blame] | 174 | /* |
| 175 | * Group chips that have common command sets. This should ensure that |
| 176 | * no chip gets confused by a probing command for a very different class |
| 177 | * of chips. |
| 178 | */ |
| 179 | enum { |
| 180 | /* SPI25 is very common. Keep it at zero so we don't have |
| 181 | to specify it for each and every chip in the database.*/ |
| 182 | SPI25 = 0, |
| 183 | } spi_cmd_set; |
| 184 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 185 | int (*probe) (struct flashctx *flash); |
hailfinger | d5b3592 | 2009-06-03 14:46:22 +0000 | [diff] [blame] | 186 | |
stefanct | c5eb8a9 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 187 | /* Delay after "enter/exit ID mode" commands in microseconds. |
| 188 | * NB: negative values have special meanings, see TIMING_* below. |
| 189 | */ |
| 190 | signed int probe_timing; |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 191 | |
| 192 | /* |
hailfinger | c4fac58 | 2009-12-22 13:04:53 +0000 | [diff] [blame] | 193 | * Erase blocks and associated erase function. Any chip erase function |
| 194 | * is stored as chip-sized virtual block together with said function. |
stefanct | 707f13b | 2011-05-19 02:58:17 +0000 | [diff] [blame] | 195 | * The first one that fits will be chosen. There is currently no way to |
| 196 | * influence that behaviour. For testing just comment out the other |
| 197 | * elements or set the function pointer to NULL. |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 198 | */ |
| 199 | struct block_eraser { |
Patrick Georgi | ac3423f | 2017-02-03 20:58:06 +0100 | [diff] [blame] | 200 | struct eraseblock { |
stefanct | 312d9ff | 2011-06-12 19:47:55 +0000 | [diff] [blame] | 201 | unsigned int size; /* Eraseblock size in bytes */ |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 202 | unsigned int count; /* Number of contiguous blocks with that size */ |
| 203 | } eraseblocks[NUM_ERASEREGIONS]; |
stefanct | 9e6b98a | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 204 | /* a block_erase function should try to erase one block of size |
| 205 | * 'blocklen' at address 'blockaddr' and return 0 on success. */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 206 | int (*block_erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen); |
hailfinger | 7df2136 | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 207 | } block_erasers[NUM_ERASEFUNCTIONS]; |
| 208 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 209 | int (*printlock) (struct flashctx *flash); |
| 210 | int (*unlock) (struct flashctx *flash); |
Patrick Georgi | ab8353e | 2017-02-03 18:32:01 +0100 | [diff] [blame] | 211 | int (*write) (struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 212 | int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len); |
| 213 | uint8_t (*read_status) (const struct flashctx *flash); |
| 214 | int (*write_status) (const struct flashctx *flash, int status); |
Duncan Laurie | 25a4ca2 | 2019-04-25 12:08:52 -0700 | [diff] [blame] | 215 | int (*check_access) (const struct flashctx *flash, unsigned int start, unsigned int len, int read); |
David Hendricks | 8c08421 | 2015-11-17 22:29:36 -0800 | [diff] [blame] | 216 | struct voltage_range voltage; |
David Hendricks | f7924d1 | 2010-06-10 21:26:44 -0700 | [diff] [blame] | 217 | struct wp *wp; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 218 | }; |
| 219 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 220 | /* struct flashctx must always contain struct flashchip at the beginning. */ |
| 221 | struct flashctx { |
Patrick Georgi | f3fa299 | 2017-02-02 16:24:44 +0100 | [diff] [blame] | 222 | struct flashchip *chip; |
| 223 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 224 | chipaddr virtual_memory; |
| 225 | /* Some flash devices have an additional register space. */ |
| 226 | chipaddr virtual_registers; |
Edward O'Callaghan | 20596a8 | 2019-06-13 14:47:03 +1000 | [diff] [blame^] | 227 | struct registered_master *mst; |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 231 | /* This is the byte value we expect to see in erased regions of the flash */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 232 | int flash_erase_value(struct flashctx *flash); |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 233 | |
| 234 | /* This is a byte value that indicates that the region is not erased */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 235 | int flash_unerased_value(struct flashctx *flash); |
Simon Glass | 4c21413 | 2013-07-16 10:09:28 -0600 | [diff] [blame] | 236 | |
David Hendricks | 40df5b5 | 2016-12-22 15:36:28 -0800 | [diff] [blame] | 237 | /* Given RDID info, return pointer to entry in flashchips[] */ |
| 238 | const struct flashchip *flash_id_to_entry(uint32_t mfg_id, uint32_t model_id); |
| 239 | |
hailfinger | d5b3592 | 2009-06-03 14:46:22 +0000 | [diff] [blame] | 240 | /* Timing used in probe routines. ZERO is -2 to differentiate between an unset |
| 241 | * field and zero delay. |
Simon Glass | 8dc8273 | 2013-07-16 10:13:51 -0600 | [diff] [blame] | 242 | * |
hailfinger | d5b3592 | 2009-06-03 14:46:22 +0000 | [diff] [blame] | 243 | * SPI devices will always have zero delay and ignore this field. |
| 244 | */ |
| 245 | #define TIMING_FIXME -1 |
| 246 | /* this is intentionally same value as fixme */ |
| 247 | #define TIMING_IGNORED -1 |
| 248 | #define TIMING_ZERO -2 |
| 249 | |
hailfinger | 48ed3e2 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 250 | extern const struct flashchip flashchips[]; |
Ramya Vijaykumar | e6a7ca8 | 2015-05-12 14:27:29 +0530 | [diff] [blame] | 251 | extern const struct flashchip flashchips_hwseq[]; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 252 | |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 253 | void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr); |
| 254 | void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr); |
| 255 | void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr); |
| 256 | void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len); |
| 257 | uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr); |
| 258 | uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr); |
| 259 | uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr); |
| 260 | void chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len); |
| 261 | |
uwe | 884cc8b | 2009-06-17 12:07:12 +0000 | [diff] [blame] | 262 | /* print.c */ |
hailfinger | a50d60e | 2009-11-17 09:57:34 +0000 | [diff] [blame] | 263 | void print_supported(void); |
hailfinger | a50d60e | 2009-11-17 09:57:34 +0000 | [diff] [blame] | 264 | void print_supported_wiki(void); |
uwe | a3a82c9 | 2009-05-15 17:02:34 +0000 | [diff] [blame] | 265 | |
Edward O'Callaghan | 8dd5792 | 2019-03-15 16:21:34 +1100 | [diff] [blame] | 266 | /* helpers.c */ |
| 267 | uint32_t address_to_bits(uint32_t addr); |
| 268 | int bitcount(unsigned long a); |
| 269 | int min(int a, int b); |
| 270 | int max(int a, int b); |
| 271 | char *strcat_realloc(char *dest, const char *src); |
| 272 | void tolower_string(char *str); |
| 273 | |
uwe | 4529d20 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 274 | /* flashrom.c */ |
Edward O'Callaghan | d8eca56 | 2019-02-24 21:10:33 +1100 | [diff] [blame] | 275 | /* |
| 276 | * The following enum defines possible write granularities of flash chips. These tend to reflect the properties |
| 277 | * of the actual hardware not necesserily the write function(s) defined by the respective struct flashchip. |
| 278 | * The latter might (and should) be more precisely specified, e.g. they might bail out early if their execution |
| 279 | * would result in undefined chip contents. |
| 280 | */ |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 281 | enum write_granularity { |
Edward O'Callaghan | d8eca56 | 2019-02-24 21:10:33 +1100 | [diff] [blame] | 282 | /* We assume 256 byte granularity by default. */ |
| 283 | write_gran_256bytes = 0,/* If less than 256 bytes are written, the unwritten bytes are undefined. */ |
| 284 | write_gran_1bit, /* Each bit can be cleared individually. */ |
| 285 | write_gran_1byte, /* A byte can be written once. Further writes to an already written byte cause |
| 286 | * its contents to be either undefined or to stay unchanged. */ |
| 287 | write_gran_128bytes, /* If less than 128 bytes are written, the unwritten bytes are undefined. */ |
| 288 | write_gran_264bytes, /* If less than 264 bytes are written, the unwritten bytes are undefined. */ |
| 289 | write_gran_512bytes, /* If less than 512 bytes are written, the unwritten bytes are undefined. */ |
| 290 | write_gran_528bytes, /* If less than 528 bytes are written, the unwritten bytes are undefined. */ |
| 291 | write_gran_1024bytes, /* If less than 1024 bytes are written, the unwritten bytes are undefined. */ |
| 292 | write_gran_1056bytes, /* If less than 1056 bytes are written, the unwritten bytes are undefined. */ |
| 293 | write_gran_1byte_implicit_erase, /* EEPROMs and other chips with implicit erase and 1-byte writes. */ |
hailfinger | b247c7a | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 294 | }; |
Edward O'Callaghan | d8eca56 | 2019-02-24 21:10:33 +1100 | [diff] [blame] | 295 | |
hailfinger | 80422e2 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 296 | extern enum chipbustype buses_supported; |
krause | 2eb7621 | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 297 | extern const char flashrom_version[]; |
hailfinger | 92cd8e3 | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 298 | extern char *chip_to_probe; |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 299 | void map_flash_registers(struct flashctx *flash); |
| 300 | int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len); |
| 301 | int erase_flash(struct flashctx *flash); |
Edward O'Callaghan | 20596a8 | 2019-06-13 14:47:03 +1000 | [diff] [blame^] | 302 | int probe_flash(struct registered_master *master, int startchip, struct flashctx *fill_flash, int force); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 303 | int read_flash(struct flashctx *flash, uint8_t *buf, |
David Hendricks | e345194 | 2013-03-21 17:23:29 -0700 | [diff] [blame] | 304 | unsigned int start, unsigned int len); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 305 | int read_flash_to_file(struct flashctx *flash, const char *filename); |
stefanct | 5270028 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 306 | char *extract_param(char **haystack, const char *needle, const char *delim); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 307 | int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len, const char *message); |
hailfinger | 92cd8e3 | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 308 | void print_version(void); |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 309 | void print_buildinfo(void); |
hailfinger | 74819ad | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 310 | void print_banner(void); |
hailfinger | f79d171 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 311 | void list_programmers_linebreak(int startcol, int cols, int paren); |
hailfinger | 92cd8e3 | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 312 | int selfcheck(void); |
Vadim Bendebury | 2f346a3 | 2018-05-21 10:24:18 -0700 | [diff] [blame] | 313 | |
| 314 | /* |
| 315 | * |
| 316 | * The main processing function of flashrom utility; it is invoked once |
| 317 | * command line parameters are processed and verified, and the type of the |
| 318 | * flash chip the programmer operates on has been determined. |
| 319 | * |
| 320 | * @flash pointer to the flash context matching the chip detected |
| 321 | * during initialization. |
| 322 | * @force when set proceed even if the chip is not known to work |
| 323 | * @filename pointer to the name of the file to read from or write to |
| 324 | * @read_it when true, flash contents are read into 'filename' |
| 325 | * @write_it when true, flash is programmed with 'filename' contents |
| 326 | * @erase_it when true, flash chip is erased |
| 327 | * @verify_it depending on the value verify the full chip, only changed |
| 328 | * areas, or none |
| 329 | * @extract_it extract all known flash chip regions into separate files |
| 330 | * @diff_file when deciding what areas to program, use this file's |
| 331 | * contents instead of reading the current chip contents |
| 332 | * @do_diff when true - compare result of the operation with either the |
| 333 | * original chip contents for 'diff_file' contents, is present. |
| 334 | * When false - do not diff, consider the chip erased before |
| 335 | * operation starts. |
| 336 | * |
| 337 | * Only one of 'read_it', 'write_it', and 'erase_it' is expected to be set, |
| 338 | * but this is not enforced. |
| 339 | * |
| 340 | * 'do_diff' must be set if 'diff_file' is set. If 'do_diff' is set, but |
| 341 | * 'diff_file' is not - comparison is done against the pre-operation chip |
| 342 | * contents. |
| 343 | */ |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 344 | int doit(struct flashctx *flash, int force, const char *filename, int read_it, |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 345 | int write_it, int erase_it, int verify_it, int extract_it, |
Vadim Bendebury | 2f346a3 | 2018-05-21 10:24:18 -0700 | [diff] [blame] | 346 | const char *diff_file, int do_diff); |
stefanct | 5270028 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 347 | int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename); |
| 348 | int write_buf_to_file(unsigned char *buf, unsigned long size, const char *filename); |
uwe | 884cc8b | 2009-06-17 12:07:12 +0000 | [diff] [blame] | 349 | |
| 350 | #define OK 0 |
| 351 | #define NT 1 /* Not tested */ |
uwe | 4529d20 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 352 | |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 353 | /* what to do in case of an error */ |
| 354 | enum error_action { |
| 355 | error_fail, /* fail immediately */ |
| 356 | error_ignore, /* non-fatal error; continue */ |
| 357 | }; |
| 358 | |
uwe | 97e8e27 | 2011-09-03 17:15:00 +0000 | [diff] [blame] | 359 | /* Something happened that shouldn't happen, but we can go on. */ |
mkarcher | 74d3013 | 2010-07-22 18:04:15 +0000 | [diff] [blame] | 360 | #define ERROR_NONFATAL 0x100 |
| 361 | |
uwe | 97e8e27 | 2011-09-03 17:15:00 +0000 | [diff] [blame] | 362 | /* Something happened that shouldn't happen, we'll abort. */ |
| 363 | #define ERROR_FATAL -0xee |
| 364 | |
Edward O'Callaghan | 20596a8 | 2019-06-13 14:47:03 +1000 | [diff] [blame^] | 365 | #define ERROR_FLASHROM_BUG -200 |
| 366 | /* We reached one of the hardcoded limits of flashrom. This can be fixed by |
| 367 | * increasing the limit of a compile-time allocation or by switching to dynamic |
| 368 | * allocation. |
| 369 | * Note: If this warning is triggered, check first for runaway registrations. |
| 370 | */ |
| 371 | #define ERROR_FLASHROM_LIMIT -201 |
| 372 | |
David Hendricks | 1ed1d35 | 2011-11-23 17:54:37 -0800 | [diff] [blame] | 373 | /* Operation failed due to access restriction set in programmer or flash chip */ |
| 374 | #define ACCESS_DENIED -7 |
| 375 | extern enum error_action access_denied_action; |
| 376 | |
| 377 | /* convenience function for checking return codes */ |
| 378 | extern int ignore_error(int x); |
| 379 | |
Edward O'Callaghan | 83c7700 | 2019-06-04 15:56:19 +1000 | [diff] [blame] | 380 | /* cli_common.c */ |
| 381 | char *flashbuses_to_text(enum chipbustype bustype); |
Edward O'Callaghan | 71e30b4 | 2019-06-04 16:16:13 +1000 | [diff] [blame] | 382 | void print_chip_support_status(const struct flashchip *chip); |
Edward O'Callaghan | 83c7700 | 2019-06-04 15:56:19 +1000 | [diff] [blame] | 383 | |
snelson | 9cba3c6 | 2010-01-07 20:09:33 +0000 | [diff] [blame] | 384 | /* cli_output.c */ |
Edward O'Callaghan | 83c7700 | 2019-06-04 15:56:19 +1000 | [diff] [blame] | 385 | extern enum flashrom_log_level verbose_screen; |
| 386 | extern enum flashrom_log_level verbose_logfile; |
Souvik Ghosh | 3c963a4 | 2016-07-19 18:48:15 -0700 | [diff] [blame] | 387 | #ifndef STANDALONE |
| 388 | int open_logfile(const char * const filename); |
| 389 | int close_logfile(void); |
| 390 | void start_logging(void); |
| 391 | #endif |
Edward O'Callaghan | 8d8d397 | 2019-02-24 20:40:10 +1100 | [diff] [blame] | 392 | enum flashrom_log_level { |
| 393 | FLASHROM_MSG_ERROR = 0, |
| 394 | FLASHROM_MSG_WARN = 1, |
| 395 | FLASHROM_MSG_INFO = 2, |
| 396 | FLASHROM_MSG_DEBUG = 3, |
| 397 | FLASHROM_MSG_DEBUG2 = 4, |
| 398 | FLASHROM_MSG_SPEW = 5, |
Patrick Georgi | dbde2f1 | 2017-02-03 18:07:45 +0100 | [diff] [blame] | 399 | }; |
hailfinger | 63932d4 | 2010-06-04 23:20:21 +0000 | [diff] [blame] | 400 | /* Let gcc and clang check for correct printf-style format strings. */ |
Edward O'Callaghan | 8d8d397 | 2019-02-24 20:40:10 +1100 | [diff] [blame] | 401 | int print(enum flashrom_log_level level, const char *fmt, ...) |
Patrick Georgi | dbde2f1 | 2017-02-03 18:07:45 +0100 | [diff] [blame] | 402 | #ifdef __MINGW32__ |
| 403 | __attribute__((format(gnu_printf, 2, 3))); |
| 404 | #else |
| 405 | __attribute__((format(printf, 2, 3))); |
| 406 | #endif |
Edward O'Callaghan | 8d8d397 | 2019-02-24 20:40:10 +1100 | [diff] [blame] | 407 | #define msg_gerr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* general errors */ |
| 408 | #define msg_perr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* programmer errors */ |
| 409 | #define msg_cerr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* chip errors */ |
| 410 | #define msg_gwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* general warnings */ |
| 411 | #define msg_pwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* programmer warnings */ |
| 412 | #define msg_cwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* chip warnings */ |
| 413 | #define msg_ginfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* general info */ |
| 414 | #define msg_pinfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* programmer info */ |
| 415 | #define msg_cinfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* chip info */ |
| 416 | #define msg_gdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* general debug */ |
| 417 | #define msg_pdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* programmer debug */ |
| 418 | #define msg_cdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* chip debug */ |
| 419 | #define msg_gdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* general debug2 */ |
| 420 | #define msg_pdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* programmer debug2 */ |
| 421 | #define msg_cdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* chip debug2 */ |
| 422 | #define msg_gspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* general debug spew */ |
| 423 | #define msg_pspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* programmer debug spew */ |
| 424 | #define msg_cspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* chip debug spew */ |
snelson | 9cba3c6 | 2010-01-07 20:09:33 +0000 | [diff] [blame] | 425 | |
stepan | 745615e | 2007-10-15 21:44:47 +0000 | [diff] [blame] | 426 | /* spi.c */ |
hailfinger | 68002c2 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 427 | struct spi_command { |
| 428 | unsigned int writecnt; |
| 429 | unsigned int readcnt; |
| 430 | const unsigned char *writearr; |
| 431 | unsigned char *readarr; |
| 432 | }; |
Nico Huber | 4c8a956 | 2017-10-15 11:20:58 +0200 | [diff] [blame] | 433 | #define NULL_SPI_CMD { 0, 0, NULL, NULL, } |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 434 | int spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 435 | const unsigned char *writearr, unsigned char *readarr); |
Souvik Ghosh | d75cd67 | 2016-06-17 14:21:39 -0700 | [diff] [blame] | 436 | int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds); |
| 437 | uint32_t spi_get_valid_read_addr(struct flashctx *flash); |
uwe | af9b4df | 2008-09-26 13:19:02 +0000 | [diff] [blame] | 438 | |
David Hendricks | 8c08421 | 2015-11-17 22:29:36 -0800 | [diff] [blame] | 439 | #define NUM_VOLTAGE_RANGES 16 |
| 440 | extern struct voltage_range voltage_ranges[]; |
| 441 | /* returns number of unique voltage ranges, or <0 to indicate failure */ |
| 442 | extern int flash_supported_voltage_ranges(enum chipbustype bus); |
| 443 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 444 | #endif /* !__FLASH_H__ */ |