blob: afef7977bc6d34f7d00dcf19666ea633ef245d77 [file] [log] [blame]
stepan5c3f1382007-02-06 19:47:50 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
stepan5c3f1382007-02-06 19:47:50 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2000 Ronald G. Minnich <rminnich@gmail.com>
stepan6d42c0f2009-08-12 09:27:45 +00006 * Copyright (C) 2005-2009 coresystems GmbH
hailfinger77c5d932009-06-15 12:10:57 +00007 * Copyright (C) 2006-2009 Carl-Daniel Hailfinger
stepan5c3f1382007-02-06 19:47:50 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * 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.
stepan5c3f1382007-02-06 19:47:50 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * 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.
stepan5c3f1382007-02-06 19:47:50 +000018 */
19
rminnich8d3ff912003-10-25 17:01:29 +000020#ifndef __FLASH_H__
21#define __FLASH_H__ 1
22
Edward O'Callaghan407a2ef2020-12-03 16:24:54 +110023#include "platform.h"
24
Edward O'Callaghan7b188712019-09-09 15:07:14 +100025#include <inttypes.h>
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +100026#include <stdio.h>
ollie6a600992005-11-26 21:55:36 +000027#include <stdint.h>
hailfingerd43a4e32010-06-03 00:49:50 +000028#include <stddef.h>
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +100029#include <stdarg.h>
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +100030#include <stdbool.h>
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +100031#if IS_WINDOWS
oxygene3ad3b332010-01-06 22:14:39 +000032#include <windows.h>
33#undef min
34#undef max
35#endif
hailfingere1f062f2008-05-22 13:22:45 +000036
Edward O'Callaghan53ff4ad2020-12-16 20:36:28 +110037#include "layout.h"
38
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +100039#define KiB (1024)
40#define MiB (1024 * KiB)
41
42/* Assumes `n` and `a` are at most 64-bit wide (to avoid typeof() operator). */
43#define ALIGN_DOWN(n, a) ((n) & ~((uint64_t)(a) - 1))
44
Edward O'Callaghan8bc8b092020-12-04 15:31:56 +110045struct flashrom_flashctx;
46#define flashctx flashrom_flashctx /* TODO: Agree on a name and convert all occurences. */
hailfingerf294fa22010-09-25 22:53:44 +000047#define ERROR_PTR ((void*)-1)
48
hailfingeree9ee132010-10-08 00:37:55 +000049/* Error codes */
Nikolai Artemiev1e0291b2020-04-15 10:29:26 +100050#define ERROR_OOM -100
hailfingeree9ee132010-10-08 00:37:55 +000051#define TIMEOUT_ERROR -101
52
Edward O'Callaghan1a3fd132019-06-04 14:18:55 +100053/* for verify_it variable in flashrom.c and cli_classic.c */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +080054enum {
55 VERIFY_OFF = 0,
56 VERIFY_FULL,
57 VERIFY_PARTIAL,
58};
59
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +100060/* TODO: check using code for correct usage of types */
Kangheui Won4974cc12019-10-18 12:59:01 +110061typedef uintptr_t chipaddr;
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +100062#define PRIxPTR_WIDTH ((int)(sizeof(uintptr_t)*2))
Pavol Markoef4c6e82019-09-09 12:43:44 +000063
David Hendricks93784b42016-08-09 17:00:38 -070064int register_shutdown(int (*function) (void *data), void *data);
Edward O'Callaghana5cfb4d2020-09-07 16:26:42 +100065void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len);
uweabe92a52009-05-16 22:36:00 +000066void programmer_unmap_flash_region(void *virt_addr, size_t len);
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +100067void programmer_delay(unsigned int usecs);
hailfingerba3761a2009-03-05 19:24:22 +000068
uwe16f99092008-03-12 11:54:51 +000069#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
70
hailfinger40167462009-05-31 17:57:34 +000071enum chipbustype {
hailfingere1e41ea2011-07-27 07:13:06 +000072 BUS_NONE = 0,
73 BUS_PARALLEL = 1 << 0,
74 BUS_LPC = 1 << 1,
75 BUS_FWH = 1 << 2,
76 BUS_SPI = 1 << 3,
hailfingerfe7cd9e2011-11-04 21:35:26 +000077 BUS_PROG = 1 << 4,
hailfingere1e41ea2011-07-27 07:13:06 +000078 BUS_NONSPI = BUS_PARALLEL | BUS_LPC | BUS_FWH,
hailfinger40167462009-05-31 17:57:34 +000079};
80
hailfinger7df21362009-09-05 02:30:58 +000081/*
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +100082 * The following enum defines possible write granularities of flash chips. These tend to reflect the properties
83 * of the actual hardware not necesserily the write function(s) defined by the respective struct flashchip.
84 * The latter might (and should) be more precisely specified, e.g. they might bail out early if their execution
85 * would result in undefined chip contents.
86 */
87enum write_granularity {
88 /* We assume 256 byte granularity by default. */
89 write_gran_256bytes = 0,/* If less than 256 bytes are written, the unwritten bytes are undefined. */
90 write_gran_1bit, /* Each bit can be cleared individually. */
91 write_gran_1byte, /* A byte can be written once. Further writes to an already written byte cause
92 * its contents to be either undefined or to stay unchanged. */
93 write_gran_128bytes, /* If less than 128 bytes are written, the unwritten bytes are undefined. */
94 write_gran_264bytes, /* If less than 264 bytes are written, the unwritten bytes are undefined. */
95 write_gran_512bytes, /* If less than 512 bytes are written, the unwritten bytes are undefined. */
96 write_gran_528bytes, /* If less than 528 bytes are written, the unwritten bytes are undefined. */
97 write_gran_1024bytes, /* If less than 1024 bytes are written, the unwritten bytes are undefined. */
98 write_gran_1056bytes, /* If less than 1056 bytes are written, the unwritten bytes are undefined. */
99 write_gran_1byte_implicit_erase, /* EEPROMs and other chips with implicit erase and 1-byte writes. */
100};
101
102/*
hailfinger7df21362009-09-05 02:30:58 +0000103 * How many different contiguous runs of erase blocks with one size each do
104 * we have for a given erase function?
105 */
106#define NUM_ERASEREGIONS 5
107
108/*
109 * How many different erase functions do we have per chip?
Edward O'Callaghanfadf15b2019-10-10 13:46:39 +1100110 * Macronix MX25L25635F has 8 different functions.
hailfinger7df21362009-09-05 02:30:58 +0000111 */
Edward O'Callaghanfadf15b2019-10-10 13:46:39 +1100112#define NUM_ERASEFUNCTIONS 8
hailfinger7df21362009-09-05 02:30:58 +0000113
Nikolai Artemiev55f7a332020-11-05 13:54:27 +1100114#define MAX_CHIP_RESTORE_FUNCTIONS 4
115
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000116/* Feature bits used for non-SPI only */
hailfinger80dea312010-01-09 03:15:50 +0000117#define FEATURE_REGISTERMAP (1 << 0)
snelsonc6855342010-01-28 23:55:12 +0000118#define FEATURE_LONG_RESET (0 << 4)
119#define FEATURE_SHORT_RESET (1 << 4)
120#define FEATURE_EITHER_RESET FEATURE_LONG_RESET
hailfingerb07dc972010-10-20 21:13:19 +0000121#define FEATURE_RESET_MASK (FEATURE_LONG_RESET | FEATURE_SHORT_RESET)
hailfinger80dea312010-01-09 03:15:50 +0000122#define FEATURE_ADDR_FULL (0 << 2)
123#define FEATURE_ADDR_MASK (3 << 2)
snelsonc6855342010-01-28 23:55:12 +0000124#define FEATURE_ADDR_2AA (1 << 2)
125#define FEATURE_ADDR_AAA (2 << 2)
mkarcher9ded5fe2010-04-03 10:27:08 +0000126#define FEATURE_ADDR_SHIFTED (1 << 5)
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000127/* Feature bits used for SPI only */
hailfingerc33d4732010-07-29 13:09:18 +0000128#define FEATURE_WRSR_EWSR (1 << 6)
129#define FEATURE_WRSR_WREN (1 << 7)
130#define FEATURE_WRSR_EITHER (FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN)
David Hendricksff55cf62016-08-30 11:22:31 -0700131#define FEATURE_OTP (1 << 8)
Edward O'Callaghan96a4f542020-06-29 16:51:24 +1000132#define FEATURE_QPI (1 << 9)
133#define FEATURE_4BA_ENTER (1 << 10) /**< Can enter/exit 4BA mode with instructions 0xb7/0xe9 w/o WREN */
134#define FEATURE_4BA_ENTER_WREN (1 << 11) /**< Can enter/exit 4BA mode with instructions 0xb7/0xe9 after WREN */
135#define FEATURE_4BA_ENTER_EAR7 (1 << 12) /**< Can enter/exit 4BA mode by setting bit7 of the ext addr reg */
Edward O'Callaghan27486212019-07-26 21:59:55 +1000136#define FEATURE_4BA_EXT_ADDR (1 << 13) /**< Regular 3-byte operations can be used by writing the most
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000137 significant address byte into an extended address register. */
Edward O'Callaghan27486212019-07-26 21:59:55 +1000138#define FEATURE_4BA_READ (1 << 14) /**< Native 4BA read instruction (0x13) is supported. */
139#define FEATURE_4BA_FAST_READ (1 << 15) /**< Native 4BA fast read instruction (0x0c) is supported. */
140#define FEATURE_4BA_WRITE (1 << 16) /**< Native 4BA byte program (0x12) is supported. */
Edward O'Callaghan3d0cbd42019-06-24 15:37:01 +1000141/* 4BA Shorthands */
142#define FEATURE_4BA_NATIVE (FEATURE_4BA_READ | FEATURE_4BA_FAST_READ | FEATURE_4BA_WRITE)
143#define FEATURE_4BA (FEATURE_4BA_ENTER | FEATURE_4BA_EXT_ADDR | FEATURE_4BA_NATIVE)
144#define FEATURE_4BA_WREN (FEATURE_4BA_ENTER_WREN | FEATURE_4BA_EXT_ADDR | FEATURE_4BA_NATIVE)
Edward O'Callaghan96a4f542020-06-29 16:51:24 +1000145#define FEATURE_4BA_EAR7 (FEATURE_4BA_ENTER_EAR7 | FEATURE_4BA_EXT_ADDR | 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 << 17)
151#define FEATURE_NO_ERASE (1 << 18)
Simon Glass4c214132013-07-16 10:09:28 -0600152
Edward O'Callaghanef783e32020-08-10 19:54:27 +1000153#define ERASED_VALUE(flash) (((flash)->chip->feature_bits & FEATURE_ERASED_ZERO) ? 0x00 : 0xff)
154#define UNERASED_VALUE(flash) (((flash)->chip->feature_bits & FEATURE_ERASED_ZERO) ? 0xff : 0x00)
155
David Hendricks8c084212015-11-17 22:29:36 -0800156struct voltage_range {
157 uint16_t min, max;
158};
159
Patrick Georgiac3423f2017-02-03 20:58:06 +0100160enum test_state {
161 OK = 0,
162 NT = 1, /* Not tested */
163 BAD, /* Known to not work */
164 DEP, /* Support depends on configuration (e.g. Intel flash descriptor) */
165 NA, /* Not applicable (e.g. write support on ROM chips) */
166};
167
Alan Green5447a452019-07-30 13:57:52 +1000168#define TEST_UNTESTED (struct tested){ .probe = NT, .read = NT, .erase = NT, .write = NT }
Patrick Georgiac3423f2017-02-03 20:58:06 +0100169
Alan Green5447a452019-07-30 13:57:52 +1000170#define TEST_OK_PROBE (struct tested){ .probe = OK, .read = NT, .erase = NT, .write = NT }
171#define TEST_OK_PR (struct tested){ .probe = OK, .read = OK, .erase = NT, .write = NT }
172#define TEST_OK_PRE (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = NT }
173#define TEST_OK_PREW (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = OK }
Patrick Georgiac3423f2017-02-03 20:58:06 +0100174
Alan Green5447a452019-07-30 13:57:52 +1000175#define TEST_BAD_PROBE (struct tested){ .probe = BAD, .read = NT, .erase = NT, .write = NT }
176#define TEST_BAD_PR (struct tested){ .probe = BAD, .read = BAD, .erase = NT, .write = NT }
177#define TEST_BAD_PRE (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = NT }
178#define TEST_BAD_PREW (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD }
Patrick Georgiac3423f2017-02-03 20:58:06 +0100179
Nikolai Artemieva66b6cd2020-08-31 18:07:13 +1000180typedef int (erasefunc_t)(struct flashctx *flash, unsigned int addr, unsigned int blocklen);
181
rminnich8d3ff912003-10-25 17:01:29 +0000182struct flashchip {
uwedfcd15f2008-03-14 23:55:58 +0000183 const char *vendor;
uwe6ed6d952007-12-04 21:49:06 +0000184 const char *name;
hailfinger40167462009-05-31 17:57:34 +0000185
186 enum chipbustype bustype;
187
uwefa98ca12008-10-18 21:14:13 +0000188 /*
189 * With 32bit manufacture_id and model_id we can cover IDs up to
hailfinger428f2012007-12-31 01:49:00 +0000190 * (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's
191 * Identification code.
192 */
193 uint32_t manufacture_id;
194 uint32_t model_id;
rminnich8d3ff912003-10-25 17:01:29 +0000195
stefanct707f13b2011-05-19 02:58:17 +0000196 /* Total chip size in kilobytes */
stefanctc5eb8a92011-11-23 09:13:48 +0000197 unsigned int total_size;
stefanct707f13b2011-05-19 02:58:17 +0000198 /* Chip page size in bytes */
stefanctc5eb8a92011-11-23 09:13:48 +0000199 unsigned int page_size;
snelson63133f92010-01-04 17:15:23 +0000200 int feature_bits;
rminnich8d3ff912003-10-25 17:01:29 +0000201
Patrick Georgiac3423f2017-02-03 20:58:06 +0100202 /* Indicate how well flashrom supports different operations of this flash chip. */
203 struct tested {
204 enum test_state probe;
205 enum test_state read;
206 enum test_state erase;
207 enum test_state write;
Patrick Georgiac3423f2017-02-03 20:58:06 +0100208 } tested;
stuge9cd64bd2008-05-03 04:34:37 +0000209
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +1100210 /*
211 * Group chips that have common command sets. This should ensure that
212 * no chip gets confused by a probing command for a very different class
213 * of chips.
214 */
215 enum {
216 /* SPI25 is very common. Keep it at zero so we don't have
217 to specify it for each and every chip in the database.*/
218 SPI25 = 0,
Edward O'Callaghana9c81002019-02-24 15:54:40 +1100219 SPI_EDI = 1,
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +1100220 } spi_cmd_set;
221
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700222 int (*probe) (struct flashctx *flash);
hailfingerd5b35922009-06-03 14:46:22 +0000223
stefanctc5eb8a92011-11-23 09:13:48 +0000224 /* Delay after "enter/exit ID mode" commands in microseconds.
225 * NB: negative values have special meanings, see TIMING_* below.
226 */
227 signed int probe_timing;
hailfinger7df21362009-09-05 02:30:58 +0000228
229 /*
hailfingerc4fac582009-12-22 13:04:53 +0000230 * Erase blocks and associated erase function. Any chip erase function
231 * is stored as chip-sized virtual block together with said function.
stefanct707f13b2011-05-19 02:58:17 +0000232 * The first one that fits will be chosen. There is currently no way to
233 * influence that behaviour. For testing just comment out the other
234 * elements or set the function pointer to NULL.
hailfinger7df21362009-09-05 02:30:58 +0000235 */
236 struct block_eraser {
Patrick Georgiac3423f2017-02-03 20:58:06 +0100237 struct eraseblock {
stefanct312d9ff2011-06-12 19:47:55 +0000238 unsigned int size; /* Eraseblock size in bytes */
hailfinger7df21362009-09-05 02:30:58 +0000239 unsigned int count; /* Number of contiguous blocks with that size */
240 } eraseblocks[NUM_ERASEREGIONS];
stefanct9e6b98a2011-05-28 02:37:14 +0000241 /* a block_erase function should try to erase one block of size
242 * 'blocklen' at address 'blockaddr' and return 0 on success. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700243 int (*block_erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen);
hailfinger7df21362009-09-05 02:30:58 +0000244 } block_erasers[NUM_ERASEFUNCTIONS];
245
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700246 int (*printlock) (struct flashctx *flash);
247 int (*unlock) (struct flashctx *flash);
Patrick Georgiab8353e2017-02-03 18:32:01 +0100248 int (*write) (struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700249 int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
250 uint8_t (*read_status) (const struct flashctx *flash);
251 int (*write_status) (const struct flashctx *flash, int status);
Duncan Laurie25a4ca22019-04-25 12:08:52 -0700252 int (*check_access) (const struct flashctx *flash, unsigned int start, unsigned int len, int read);
David Hendricks8c084212015-11-17 22:29:36 -0800253 struct voltage_range voltage;
Edward O'Callaghan10e63d92019-06-17 14:12:52 +1000254 enum write_granularity gran;
Edward O'Callaghan2d001292019-06-26 14:35:03 +1000255
256 /* SPI specific options (TODO: Make it a union in case other bustypes get specific options.) */
257 uint8_t wrea_override; /**< override opcode for write extended address register */
258
David Hendricksf7924d12010-06-10 21:26:44 -0700259 struct wp *wp;
rminnich8d3ff912003-10-25 17:01:29 +0000260};
261
Nikolai Artemiev55f7a332020-11-05 13:54:27 +1100262typedef int (*chip_restore_fn_cb_t)(struct flashctx *flash, uint8_t status);
263
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700264/* struct flashctx must always contain struct flashchip at the beginning. */
Edward O'Callaghan8bc8b092020-12-04 15:31:56 +1100265struct flashrom_flashctx {
Patrick Georgif3fa2992017-02-02 16:24:44 +0100266 struct flashchip *chip;
Edward O'Callaghan79357b32020-08-02 01:24:58 +1000267 /* FIXME: The memory mappings should be saved in a more structured way. */
268 /* The physical_* fields store the respective addresses in the physical address space of the CPU. */
269 uintptr_t physical_memory;
270 /* The virtual_* fields store where the respective physical address is mapped into flashrom's address
271 * space. A value equivalent to (chipaddr)ERROR_PTR indicates an invalid mapping (or none at all). */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700272 chipaddr virtual_memory;
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000273 /* Some flash devices have an additional register space; semantics are like above. */
Edward O'Callaghan79357b32020-08-02 01:24:58 +1000274 uintptr_t physical_registers;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700275 chipaddr virtual_registers;
Edward O'Callaghan20596a82019-06-13 14:47:03 +1000276 struct registered_master *mst;
Edward O'Callaghan53ff4ad2020-12-16 20:36:28 +1100277 const struct flashrom_layout *layout;
278 struct single_layout fallback_layout;
Edward O'Callaghan2c679272020-09-23 22:41:01 +1000279 struct {
280 bool force;
281 bool force_boardmismatch;
282 bool verify_after_write;
283 bool verify_whole_chip;
Edward O'Callaghane8118582020-12-03 12:45:59 +1100284 bool do_diff;
Edward O'Callaghan2c679272020-09-23 22:41:01 +1000285 } flags;
Edward O'Callaghan6e573be2020-12-18 10:38:06 +1100286 char *diff_file; /* HACK to keep do_*() fn signatures the same. */
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000287 /* We cache the state of the extended address register (highest byte
288 * of a 4BA for 3BA instructions) and the state of the 4BA mode here.
289 * If possible, we enter 4BA mode early. If that fails, we make use
290 * of the extended address register.
291 */
292 int address_high_byte;
293 bool in_4ba_mode;
Nikolai Artemiev55f7a332020-11-05 13:54:27 +1100294
295 int chip_restore_fn_count;
296 struct chip_restore_func_data {
297 chip_restore_fn_cb_t func;
298 uint8_t status;
299 } chip_restore_fn[MAX_CHIP_RESTORE_FUNCTIONS];
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700300};
301
hailfingerd5b35922009-06-03 14:46:22 +0000302/* Timing used in probe routines. ZERO is -2 to differentiate between an unset
303 * field and zero delay.
Simon Glass8dc82732013-07-16 10:13:51 -0600304 *
hailfingerd5b35922009-06-03 14:46:22 +0000305 * SPI devices will always have zero delay and ignore this field.
306 */
307#define TIMING_FIXME -1
308/* this is intentionally same value as fixme */
309#define TIMING_IGNORED -1
310#define TIMING_ZERO -2
311
hailfinger48ed3e22011-05-04 00:39:50 +0000312extern const struct flashchip flashchips[];
Edward O'Callaghan6240c852019-07-02 15:49:58 +1000313extern const unsigned int flashchips_size;
314
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700315void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
316void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
317void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
Stuart langleyc98e43f2020-03-26 20:27:36 +1100318void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700319uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr);
320uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr);
321uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr);
322void chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
323
uwe884cc8b2009-06-17 12:07:12 +0000324/* print.c */
Edward O'Callaghanab4993a2019-11-09 21:36:17 +1100325int print_supported(void);
hailfingera50d60e2009-11-17 09:57:34 +0000326void print_supported_wiki(void);
uwea3a82c92009-05-15 17:02:34 +0000327
Edward O'Callaghan8dd57922019-03-15 16:21:34 +1100328/* helpers.c */
329uint32_t address_to_bits(uint32_t addr);
Edward O'Callaghan2fc166e2019-09-09 00:51:20 +1000330unsigned int bitcount(unsigned long a);
Edward O'Callaghand2799ab2019-09-09 16:30:31 +1000331#undef MIN
332#define MIN(a, b) ((a) < (b) ? (a) : (b))
333#undef MAX
334#define MAX(a, b) ((a) > (b) ? (a) : (b))
Edward O'Callaghan8dd57922019-03-15 16:21:34 +1100335int max(int a, int b);
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000336int min(int a, int b);
Edward O'Callaghan8dd57922019-03-15 16:21:34 +1100337char *strcat_realloc(char *dest, const char *src);
338void tolower_string(char *str);
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000339uint8_t reverse_byte(uint8_t x);
340void reverse_bytes(uint8_t *dst, const uint8_t *src, size_t length);
341#ifdef __MINGW32__
342char* strtok_r(char *str, const char *delim, char **nextp);
343char *strndup(const char *str, size_t size);
344#endif
345#if defined(__DJGPP__) || (!defined(__LIBPAYLOAD__) && !defined(HAVE_STRNLEN))
346size_t strnlen(const char *str, size_t n);
347#endif
Edward O'Callaghan8dd57922019-03-15 16:21:34 +1100348
uwe4529d202007-08-23 13:34:59 +0000349/* flashrom.c */
krause2eb76212011-01-17 07:50:42 +0000350extern const char flashrom_version[];
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000351extern const char *chip_to_probe;
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000352char *flashbuses_to_text(enum chipbustype bustype);
Edward O'Callaghan79357b32020-08-02 01:24:58 +1000353int map_flash(struct flashctx *flash);
354void unmap_flash(struct flashctx *flash);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700355int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
356int erase_flash(struct flashctx *flash);
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000357int probe_flash(struct registered_master *mst, int startchip, struct flashctx *fill_flash, int force);
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000358int read_flash(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700359int read_flash_to_file(struct flashctx *flash, const char *filename);
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000360char *extract_param(const char *const *haystack, const char *needle, const char *delim);
Edward O'Callaghan445b48b2020-08-13 12:25:17 +1000361int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len);
hailfinger92cd8e32010-01-07 03:24:05 +0000362void print_version(void);
Souvik Ghosh3c963a42016-07-19 18:48:15 -0700363void print_buildinfo(void);
hailfinger74819ad2010-05-15 15:04:37 +0000364void print_banner(void);
hailfingerf79d1712010-10-06 23:48:34 +0000365void list_programmers_linebreak(int startcol, int cols, int paren);
hailfinger92cd8e32010-01-07 03:24:05 +0000366int selfcheck(void);
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000367int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename);
Edward O'Callaghanb2257cc2020-07-25 22:19:47 +1000368int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename);
Edward O'Callaghana0176ff2020-08-18 15:49:23 +1000369int prepare_flash_access(struct flashctx *, bool read_it, bool write_it, bool erase_it, bool verify_it);
370void finalize_flash_access(struct flashctx *);
Vadim Bendebury2f346a32018-05-21 10:24:18 -0700371
Edward O'Callaghan020dfa12020-09-23 23:12:55 +1000372int do_read(struct flashctx *, const char *filename);
Edward O'Callaghan6e573be2020-12-18 10:38:06 +1100373int do_erase(struct flashctx *);
374int do_write(struct flashctx *, const char *const filename, const char *const referencefile);
375int do_verify(struct flashctx *, const char *const filename);
Edward O'Callaghan020dfa12020-09-23 23:12:55 +1000376int do_extract_it(struct flashctx *);
uwe884cc8b2009-06-17 12:07:12 +0000377
David Hendricks1ed1d352011-11-23 17:54:37 -0800378/* what to do in case of an error */
379enum error_action {
380 error_fail, /* fail immediately */
381 error_ignore, /* non-fatal error; continue */
382};
Nikolai Artemiev55f7a332020-11-05 13:54:27 +1100383int register_chip_restore(chip_restore_fn_cb_t func, struct flashctx *flash, uint8_t status);
David Hendricks1ed1d352011-11-23 17:54:37 -0800384
uwe97e8e272011-09-03 17:15:00 +0000385/* Something happened that shouldn't happen, but we can go on. */
mkarcher74d30132010-07-22 18:04:15 +0000386#define ERROR_NONFATAL 0x100
387
uwe97e8e272011-09-03 17:15:00 +0000388/* Something happened that shouldn't happen, we'll abort. */
389#define ERROR_FATAL -0xee
Edward O'Callaghan20596a82019-06-13 14:47:03 +1000390#define ERROR_FLASHROM_BUG -200
391/* We reached one of the hardcoded limits of flashrom. This can be fixed by
392 * increasing the limit of a compile-time allocation or by switching to dynamic
393 * allocation.
394 * Note: If this warning is triggered, check first for runaway registrations.
395 */
396#define ERROR_FLASHROM_LIMIT -201
397
David Hendricks1ed1d352011-11-23 17:54:37 -0800398/* convenience function for checking return codes */
399extern int ignore_error(int x);
400
Edward O'Callaghan83c77002019-06-04 15:56:19 +1000401/* cli_common.c */
Edward O'Callaghan71e30b42019-06-04 16:16:13 +1000402void print_chip_support_status(const struct flashchip *chip);
Edward O'Callaghan83c77002019-06-04 15:56:19 +1000403
snelson9cba3c62010-01-07 20:09:33 +0000404/* cli_output.c */
Edward O'Callaghan83c77002019-06-04 15:56:19 +1000405extern enum flashrom_log_level verbose_screen;
406extern enum flashrom_log_level verbose_logfile;
Souvik Ghosh3c963a42016-07-19 18:48:15 -0700407#ifndef STANDALONE
408int open_logfile(const char * const filename);
409int close_logfile(void);
410void start_logging(void);
411#endif
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +1100412enum flashrom_log_level {
413 FLASHROM_MSG_ERROR = 0,
414 FLASHROM_MSG_WARN = 1,
415 FLASHROM_MSG_INFO = 2,
416 FLASHROM_MSG_DEBUG = 3,
417 FLASHROM_MSG_DEBUG2 = 4,
418 FLASHROM_MSG_SPEW = 5,
Patrick Georgidbde2f12017-02-03 18:07:45 +0100419};
hailfinger63932d42010-06-04 23:20:21 +0000420/* Let gcc and clang check for correct printf-style format strings. */
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +1100421int print(enum flashrom_log_level level, const char *fmt, ...)
Patrick Georgidbde2f12017-02-03 18:07:45 +0100422#ifdef __MINGW32__
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000423# ifndef __MINGW_PRINTF_FORMAT
424# define __MINGW_PRINTF_FORMAT gnu_printf
425# endif
426__attribute__((format(__MINGW_PRINTF_FORMAT, 2, 3)));
Patrick Georgidbde2f12017-02-03 18:07:45 +0100427#else
428__attribute__((format(printf, 2, 3)));
429#endif
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +1100430#define msg_gerr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* general errors */
431#define msg_perr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* programmer errors */
432#define msg_cerr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* chip errors */
433#define msg_gwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* general warnings */
434#define msg_pwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* programmer warnings */
435#define msg_cwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* chip warnings */
436#define msg_ginfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* general info */
437#define msg_pinfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* programmer info */
438#define msg_cinfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* chip info */
439#define msg_gdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* general debug */
440#define msg_pdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* programmer debug */
441#define msg_cdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* chip debug */
442#define msg_gdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* general debug2 */
443#define msg_pdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* programmer debug2 */
444#define msg_cdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* chip debug2 */
445#define msg_gspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* general debug spew */
446#define msg_pspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* programmer debug spew */
447#define msg_cspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* chip debug spew */
snelson9cba3c62010-01-07 20:09:33 +0000448
Edward O'Callaghan1bbd6bf2020-12-16 20:25:35 +1100449/* layout.c */
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100450int register_include_arg(struct layout_include_args **args, char *name);
Edward O'Callaghan1bbd6bf2020-12-16 20:25:35 +1100451int read_romlayout(const char *name);
452int normalize_romentries(const struct flashctx *flash);
Edward O'Callaghan10bb9ae2020-12-17 13:06:10 +1100453void layout_cleanup(struct layout_include_args **args);
Edward O'Callaghan1bbd6bf2020-12-16 20:25:35 +1100454
stepan745615e2007-10-15 21:44:47 +0000455/* spi.c */
hailfinger68002c22009-07-10 21:08:55 +0000456struct spi_command {
457 unsigned int writecnt;
458 unsigned int readcnt;
459 const unsigned char *writearr;
460 unsigned char *readarr;
461};
Nico Huber4c8a9562017-10-15 11:20:58 +0200462#define NULL_SPI_CMD { 0, 0, NULL, NULL, }
Edward O'Callaghancda796c2020-12-01 20:57:49 +1100463int spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700464int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds);
uweaf9b4df2008-09-26 13:19:02 +0000465
Edward O'Callaghan4b940572019-08-02 01:44:47 +1000466enum chipbustype get_buses_supported(void);
ollie5b621572004-03-20 16:46:10 +0000467#endif /* !__FLASH_H__ */