blob: 28c7cfa2fbd25371f9ffc5b75149738d1acbdb52 [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
ollie6a600992005-11-26 21:55:36 +000023#include <stdint.h>
hailfingerd43a4e32010-06-03 00:49:50 +000024#include <stddef.h>
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +100025#include <stdbool.h>
hailfinger088dc812009-12-14 03:32:24 +000026#include "hwaccess.h"
oxygene3ad3b332010-01-06 22:14:39 +000027#ifdef _WIN32
28#include <windows.h>
29#undef min
30#undef max
31#endif
hailfingere1f062f2008-05-22 13:22:45 +000032
Stefan Reinauere64faaf2011-05-03 18:03:25 -070033/* Are timers broken? */
34extern int broken_timer;
35
Souvik Ghoshd75cd672016-06-17 14:21:39 -070036struct flashctx; /* forward declare */
hailfingerf294fa22010-09-25 22:53:44 +000037#define ERROR_PTR ((void*)-1)
38
hailfingeree9ee132010-10-08 00:37:55 +000039/* Error codes */
40#define TIMEOUT_ERROR -101
41
Edward O'Callaghan9b520dd2019-05-01 21:47:21 -040042#define PRIxPTR_WIDTH ((int)(sizeof(uintptr_t)*2))
43
Edward O'Callaghan1a3fd132019-06-04 14:18:55 +100044/* for verify_it variable in flashrom.c and cli_classic.c */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +080045enum {
46 VERIFY_OFF = 0,
47 VERIFY_FULL,
48 VERIFY_PARTIAL,
49};
50
hailfinger82719632009-05-16 21:22:56 +000051typedef unsigned long chipaddr;
52
David Hendricks93784b42016-08-09 17:00:38 -070053int register_shutdown(int (*function) (void *data), void *data);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070054#define CHIP_RESTORE_CALLBACK int (*func) (struct flashctx *flash, uint8_t status)
David Hendricksbf36f092010-11-02 23:39:29 -070055
Souvik Ghoshd75cd672016-06-17 14:21:39 -070056int register_chip_restore(CHIP_RESTORE_CALLBACK, struct flashctx *flash, uint8_t status);
uweabe92a52009-05-16 22:36:00 +000057void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
58 size_t len);
59void programmer_unmap_flash_region(void *virt_addr, size_t len);
hailfingere5829f62009-06-05 17:48:08 +000060void programmer_delay(int usecs);
hailfingerba3761a2009-03-05 19:24:22 +000061
uwe16f99092008-03-12 11:54:51 +000062#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
63
hailfinger40167462009-05-31 17:57:34 +000064enum chipbustype {
hailfingere1e41ea2011-07-27 07:13:06 +000065 BUS_NONE = 0,
66 BUS_PARALLEL = 1 << 0,
67 BUS_LPC = 1 << 1,
68 BUS_FWH = 1 << 2,
69 BUS_SPI = 1 << 3,
hailfingerfe7cd9e2011-11-04 21:35:26 +000070 BUS_PROG = 1 << 4,
hailfingere1e41ea2011-07-27 07:13:06 +000071 BUS_NONSPI = BUS_PARALLEL | BUS_LPC | BUS_FWH,
hailfinger40167462009-05-31 17:57:34 +000072};
73
David Hendricks80f62d22010-10-08 11:09:35 -070074/* used to select bus which target chip resides */
75extern enum chipbustype target_bus;
76
hailfinger7df21362009-09-05 02:30:58 +000077/*
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +100078 * The following enum defines possible write granularities of flash chips. These tend to reflect the properties
79 * of the actual hardware not necesserily the write function(s) defined by the respective struct flashchip.
80 * The latter might (and should) be more precisely specified, e.g. they might bail out early if their execution
81 * would result in undefined chip contents.
82 */
83enum write_granularity {
84 /* We assume 256 byte granularity by default. */
85 write_gran_256bytes = 0,/* If less than 256 bytes are written, the unwritten bytes are undefined. */
86 write_gran_1bit, /* Each bit can be cleared individually. */
87 write_gran_1byte, /* A byte can be written once. Further writes to an already written byte cause
88 * its contents to be either undefined or to stay unchanged. */
89 write_gran_128bytes, /* If less than 128 bytes are written, the unwritten bytes are undefined. */
90 write_gran_264bytes, /* If less than 264 bytes are written, the unwritten bytes are undefined. */
91 write_gran_512bytes, /* If less than 512 bytes are written, the unwritten bytes are undefined. */
92 write_gran_528bytes, /* If less than 528 bytes are written, the unwritten bytes are undefined. */
93 write_gran_1024bytes, /* If less than 1024 bytes are written, the unwritten bytes are undefined. */
94 write_gran_1056bytes, /* If less than 1056 bytes are written, the unwritten bytes are undefined. */
95 write_gran_1byte_implicit_erase, /* EEPROMs and other chips with implicit erase and 1-byte writes. */
96};
97
98/*
hailfinger7df21362009-09-05 02:30:58 +000099 * How many different contiguous runs of erase blocks with one size each do
100 * we have for a given erase function?
101 */
102#define NUM_ERASEREGIONS 5
103
104/*
105 * How many different erase functions do we have per chip?
hailfingerc33d4732010-07-29 13:09:18 +0000106 * Atmel AT25FS010 has 6 different functions.
hailfinger7df21362009-09-05 02:30:58 +0000107 */
hailfingerc33d4732010-07-29 13:09:18 +0000108#define NUM_ERASEFUNCTIONS 6
hailfinger7df21362009-09-05 02:30:58 +0000109
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000110/* Feature bits used for non-SPI only */
hailfinger80dea312010-01-09 03:15:50 +0000111#define FEATURE_REGISTERMAP (1 << 0)
112#define FEATURE_BYTEWRITES (1 << 1)
snelsonc6855342010-01-28 23:55:12 +0000113#define FEATURE_LONG_RESET (0 << 4)
114#define FEATURE_SHORT_RESET (1 << 4)
115#define FEATURE_EITHER_RESET FEATURE_LONG_RESET
hailfingerb07dc972010-10-20 21:13:19 +0000116#define FEATURE_RESET_MASK (FEATURE_LONG_RESET | FEATURE_SHORT_RESET)
hailfinger80dea312010-01-09 03:15:50 +0000117#define FEATURE_ADDR_FULL (0 << 2)
118#define FEATURE_ADDR_MASK (3 << 2)
snelsonc6855342010-01-28 23:55:12 +0000119#define FEATURE_ADDR_2AA (1 << 2)
120#define FEATURE_ADDR_AAA (2 << 2)
mkarcher9ded5fe2010-04-03 10:27:08 +0000121#define FEATURE_ADDR_SHIFTED (1 << 5)
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000122/* Feature bits used for SPI only */
hailfingerc33d4732010-07-29 13:09:18 +0000123#define FEATURE_WRSR_EWSR (1 << 6)
124#define FEATURE_WRSR_WREN (1 << 7)
125#define FEATURE_WRSR_EITHER (FEATURE_WRSR_EWSR | FEATURE_WRSR_WREN)
David Hendricksff55cf62016-08-30 11:22:31 -0700126#define FEATURE_OTP (1 << 8)
127#define FEATURE_ERASE_TO_ZERO (1 << 9)
Duncan Laurie06ffd522015-10-26 12:56:08 -0700128#define FEATURE_UNBOUND_READ (1 << 10)
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700129#define FEATURE_NO_ERASE (1 << 11)
Edward O'Callaghan3d0cbd42019-06-24 15:37:01 +1000130#define FEATURE_4BA_ENTER (1 << 12)
131#define FEATURE_4BA_ENTER_WREN (1 << 13) /**< Can enter/exit 4BA mode with instructions 0xb7/0xe9 after WREN */
132#define FEATURE_4BA_EXT_ADDR (1 << 14) /**< Regular 3-byte operations can be used by writing the most
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000133 significant address byte into an extended address register. */
Edward O'Callaghan3d0cbd42019-06-24 15:37:01 +1000134#define FEATURE_4BA_READ (1 << 15) /**< Native 4BA read instruction (0x13) is supported. */
135#define FEATURE_4BA_FAST_READ (1 << 16) /**< Native 4BA fast read instruction (0x0c) is supported. */
136#define FEATURE_4BA_WRITE (1 << 17) /**< Native 4BA byte program (0x12) is supported. */
137/* 4BA Shorthands */
138#define FEATURE_4BA_NATIVE (FEATURE_4BA_READ | FEATURE_4BA_FAST_READ | FEATURE_4BA_WRITE)
139#define FEATURE_4BA (FEATURE_4BA_ENTER | FEATURE_4BA_EXT_ADDR | FEATURE_4BA_NATIVE)
140#define FEATURE_4BA_WREN (FEATURE_4BA_ENTER_WREN | FEATURE_4BA_EXT_ADDR | FEATURE_4BA_NATIVE)
Simon Glass4c214132013-07-16 10:09:28 -0600141
David Hendricks8c084212015-11-17 22:29:36 -0800142struct voltage_range {
143 uint16_t min, max;
144};
145
Patrick Georgiac3423f2017-02-03 20:58:06 +0100146enum test_state {
147 OK = 0,
148 NT = 1, /* Not tested */
149 BAD, /* Known to not work */
150 DEP, /* Support depends on configuration (e.g. Intel flash descriptor) */
151 NA, /* Not applicable (e.g. write support on ROM chips) */
152};
153
154#define TEST_UNTESTED (struct tested){ .probe = NT, .read = NT, .erase = NT, .write = NT, .uread = NT }
155
156#define TEST_OK_PROBE (struct tested){ .probe = OK, .read = NT, .erase = NT, .write = NT, .uread = NT }
157#define TEST_OK_PR (struct tested){ .probe = OK, .read = OK, .erase = NT, .write = NT, .uread = NT }
158#define TEST_OK_PRE (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = NT, .uread = NT }
159#define TEST_OK_PRU (struct tested){ .probe = OK, .read = OK, .erase = NT, .write = NT, .uread = OK }
160#define TEST_OK_PREU (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = NT, .uread = OK }
161#define TEST_OK_PREW (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = OK, .uread = NT }
162#define TEST_OK_PREWU (struct tested){ .probe = OK, .read = OK, .erase = OK, .write = OK, .uread = OK }
163
164#define TEST_BAD_PROBE (struct tested){ .probe = BAD, .read = NT, .erase = NT, .write = NT, .uread = NT }
165#define TEST_BAD_PR (struct tested){ .probe = BAD, .read = BAD, .erase = NT, .write = NT, .uread = NT }
166#define TEST_BAD_PRE (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = NT, .uread = NT }
167#define TEST_BAD_PREW (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD, .uread = NT }
168#define TEST_BAD_PREWU (struct tested){ .probe = BAD, .read = BAD, .erase = BAD, .write = BAD, .uread = BAD }
169
rminnich8d3ff912003-10-25 17:01:29 +0000170struct flashchip {
uwedfcd15f2008-03-14 23:55:58 +0000171 const char *vendor;
uwe6ed6d952007-12-04 21:49:06 +0000172 const char *name;
hailfinger40167462009-05-31 17:57:34 +0000173
174 enum chipbustype bustype;
175
uwefa98ca12008-10-18 21:14:13 +0000176 /*
177 * With 32bit manufacture_id and model_id we can cover IDs up to
hailfinger428f2012007-12-31 01:49:00 +0000178 * (including) the 4th bank of JEDEC JEP106W Standard Manufacturer's
179 * Identification code.
180 */
181 uint32_t manufacture_id;
182 uint32_t model_id;
rminnich8d3ff912003-10-25 17:01:29 +0000183
stefanct707f13b2011-05-19 02:58:17 +0000184 /* Total chip size in kilobytes */
stefanctc5eb8a92011-11-23 09:13:48 +0000185 unsigned int total_size;
stefanct707f13b2011-05-19 02:58:17 +0000186 /* Chip page size in bytes */
stefanctc5eb8a92011-11-23 09:13:48 +0000187 unsigned int page_size;
snelson63133f92010-01-04 17:15:23 +0000188 int feature_bits;
rminnich8d3ff912003-10-25 17:01:29 +0000189
Patrick Georgiac3423f2017-02-03 20:58:06 +0100190 /* Indicate how well flashrom supports different operations of this flash chip. */
191 struct tested {
192 enum test_state probe;
193 enum test_state read;
194 enum test_state erase;
195 enum test_state write;
196 enum test_state uread;
197 } tested;
stuge9cd64bd2008-05-03 04:34:37 +0000198
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +1100199 /*
200 * Group chips that have common command sets. This should ensure that
201 * no chip gets confused by a probing command for a very different class
202 * of chips.
203 */
204 enum {
205 /* SPI25 is very common. Keep it at zero so we don't have
206 to specify it for each and every chip in the database.*/
207 SPI25 = 0,
208 } spi_cmd_set;
209
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700210 int (*probe) (struct flashctx *flash);
hailfingerd5b35922009-06-03 14:46:22 +0000211
stefanctc5eb8a92011-11-23 09:13:48 +0000212 /* Delay after "enter/exit ID mode" commands in microseconds.
213 * NB: negative values have special meanings, see TIMING_* below.
214 */
215 signed int probe_timing;
hailfinger7df21362009-09-05 02:30:58 +0000216
217 /*
hailfingerc4fac582009-12-22 13:04:53 +0000218 * Erase blocks and associated erase function. Any chip erase function
219 * is stored as chip-sized virtual block together with said function.
stefanct707f13b2011-05-19 02:58:17 +0000220 * The first one that fits will be chosen. There is currently no way to
221 * influence that behaviour. For testing just comment out the other
222 * elements or set the function pointer to NULL.
hailfinger7df21362009-09-05 02:30:58 +0000223 */
224 struct block_eraser {
Patrick Georgiac3423f2017-02-03 20:58:06 +0100225 struct eraseblock {
stefanct312d9ff2011-06-12 19:47:55 +0000226 unsigned int size; /* Eraseblock size in bytes */
hailfinger7df21362009-09-05 02:30:58 +0000227 unsigned int count; /* Number of contiguous blocks with that size */
228 } eraseblocks[NUM_ERASEREGIONS];
stefanct9e6b98a2011-05-28 02:37:14 +0000229 /* a block_erase function should try to erase one block of size
230 * 'blocklen' at address 'blockaddr' and return 0 on success. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700231 int (*block_erase) (struct flashctx *flash, unsigned int blockaddr, unsigned int blocklen);
hailfinger7df21362009-09-05 02:30:58 +0000232 } block_erasers[NUM_ERASEFUNCTIONS];
233
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700234 int (*printlock) (struct flashctx *flash);
235 int (*unlock) (struct flashctx *flash);
Patrick Georgiab8353e2017-02-03 18:32:01 +0100236 int (*write) (struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700237 int (*read) (struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +1000238 int (*set_4ba) (struct flashctx *flash);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700239 uint8_t (*read_status) (const struct flashctx *flash);
240 int (*write_status) (const struct flashctx *flash, int status);
Duncan Laurie25a4ca22019-04-25 12:08:52 -0700241 int (*check_access) (const struct flashctx *flash, unsigned int start, unsigned int len, int read);
David Hendricks8c084212015-11-17 22:29:36 -0800242 struct voltage_range voltage;
Edward O'Callaghan10e63d92019-06-17 14:12:52 +1000243 enum write_granularity gran;
Edward O'Callaghan2d001292019-06-26 14:35:03 +1000244
245 /* SPI specific options (TODO: Make it a union in case other bustypes get specific options.) */
246 uint8_t wrea_override; /**< override opcode for write extended address register */
247
David Hendricksf7924d12010-06-10 21:26:44 -0700248 struct wp *wp;
rminnich8d3ff912003-10-25 17:01:29 +0000249};
250
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700251/* struct flashctx must always contain struct flashchip at the beginning. */
252struct flashctx {
Patrick Georgif3fa2992017-02-02 16:24:44 +0100253 struct flashchip *chip;
254
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700255 chipaddr virtual_memory;
256 /* Some flash devices have an additional register space. */
257 chipaddr virtual_registers;
Edward O'Callaghan20596a82019-06-13 14:47:03 +1000258 struct registered_master *mst;
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +1000259
260 /* We cache the state of the extended address register (highest byte
261 * of a 4BA for 3BA instructions) and the state of the 4BA mode here.
262 * If possible, we enter 4BA mode early. If that fails, we make use
263 * of the extended address register.
264 */
265 int address_high_byte;
266 bool in_4ba_mode;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700267};
268
269
Simon Glass4c214132013-07-16 10:09:28 -0600270/* This is the byte value we expect to see in erased regions of the flash */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700271int flash_erase_value(struct flashctx *flash);
Simon Glass4c214132013-07-16 10:09:28 -0600272
273/* This is a byte value that indicates that the region is not erased */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700274int flash_unerased_value(struct flashctx *flash);
Simon Glass4c214132013-07-16 10:09:28 -0600275
David Hendricks40df5b52016-12-22 15:36:28 -0800276/* Given RDID info, return pointer to entry in flashchips[] */
277const struct flashchip *flash_id_to_entry(uint32_t mfg_id, uint32_t model_id);
278
hailfingerd5b35922009-06-03 14:46:22 +0000279/* Timing used in probe routines. ZERO is -2 to differentiate between an unset
280 * field and zero delay.
Simon Glass8dc82732013-07-16 10:13:51 -0600281 *
hailfingerd5b35922009-06-03 14:46:22 +0000282 * SPI devices will always have zero delay and ignore this field.
283 */
284#define TIMING_FIXME -1
285/* this is intentionally same value as fixme */
286#define TIMING_IGNORED -1
287#define TIMING_ZERO -2
288
hailfinger48ed3e22011-05-04 00:39:50 +0000289extern const struct flashchip flashchips[];
Edward O'Callaghan6240c852019-07-02 15:49:58 +1000290extern const unsigned int flashchips_size;
291
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530292extern const struct flashchip flashchips_hwseq[];
ollie6a600992005-11-26 21:55:36 +0000293
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700294void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr);
295void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr);
296void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr);
297void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len);
298uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr);
299uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr);
300uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr);
301void chip_readn(const struct flashctx *flash, uint8_t *buf, const chipaddr addr, size_t len);
302
uwe884cc8b2009-06-17 12:07:12 +0000303/* print.c */
hailfingera50d60e2009-11-17 09:57:34 +0000304void print_supported(void);
hailfingera50d60e2009-11-17 09:57:34 +0000305void print_supported_wiki(void);
uwea3a82c92009-05-15 17:02:34 +0000306
Edward O'Callaghan8dd57922019-03-15 16:21:34 +1100307/* helpers.c */
308uint32_t address_to_bits(uint32_t addr);
309int bitcount(unsigned long a);
Edward O'Callaghan8dd57922019-03-15 16:21:34 +1100310int max(int a, int b);
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000311int min(int a, int b);
Edward O'Callaghan8dd57922019-03-15 16:21:34 +1100312char *strcat_realloc(char *dest, const char *src);
313void tolower_string(char *str);
314
uwe4529d202007-08-23 13:34:59 +0000315/* flashrom.c */
krause2eb76212011-01-17 07:50:42 +0000316extern const char flashrom_version[];
hailfinger92cd8e32010-01-07 03:24:05 +0000317extern char *chip_to_probe;
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000318char *flashbuses_to_text(enum chipbustype bustype);
319extern enum chipbustype buses_supported;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700320void map_flash_registers(struct flashctx *flash);
321int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
322int erase_flash(struct flashctx *flash);
Edward O'Callaghan20596a82019-06-13 14:47:03 +1000323int probe_flash(struct registered_master *master, int startchip, struct flashctx *fill_flash, int force);
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000324int read_flash(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700325int read_flash_to_file(struct flashctx *flash, const char *filename);
stefanct52700282011-06-26 17:38:17 +0000326char *extract_param(char **haystack, const char *needle, const char *delim);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700327int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len, const char *message);
hailfinger92cd8e32010-01-07 03:24:05 +0000328void print_version(void);
Souvik Ghosh3c963a42016-07-19 18:48:15 -0700329void print_buildinfo(void);
hailfinger74819ad2010-05-15 15:04:37 +0000330void print_banner(void);
hailfingerf79d1712010-10-06 23:48:34 +0000331void list_programmers_linebreak(int startcol, int cols, int paren);
hailfinger92cd8e32010-01-07 03:24:05 +0000332int selfcheck(void);
Edward O'Callaghanf78fffc2019-06-17 12:40:12 +1000333int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename);
334int write_buf_to_file(unsigned char *buf, unsigned long size, const char *filename);
Vadim Bendebury2f346a32018-05-21 10:24:18 -0700335
336/*
337 *
338 * The main processing function of flashrom utility; it is invoked once
339 * command line parameters are processed and verified, and the type of the
340 * flash chip the programmer operates on has been determined.
341 *
342 * @flash pointer to the flash context matching the chip detected
343 * during initialization.
344 * @force when set proceed even if the chip is not known to work
345 * @filename pointer to the name of the file to read from or write to
346 * @read_it when true, flash contents are read into 'filename'
347 * @write_it when true, flash is programmed with 'filename' contents
348 * @erase_it when true, flash chip is erased
349 * @verify_it depending on the value verify the full chip, only changed
350 * areas, or none
351 * @extract_it extract all known flash chip regions into separate files
352 * @diff_file when deciding what areas to program, use this file's
353 * contents instead of reading the current chip contents
354 * @do_diff when true - compare result of the operation with either the
355 * original chip contents for 'diff_file' contents, is present.
356 * When false - do not diff, consider the chip erased before
357 * operation starts.
358 *
359 * Only one of 'read_it', 'write_it', and 'erase_it' is expected to be set,
360 * but this is not enforced.
361 *
362 * 'do_diff' must be set if 'diff_file' is set. If 'do_diff' is set, but
363 * 'diff_file' is not - comparison is done against the pre-operation chip
364 * contents.
365 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700366int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +0900367 int write_it, int erase_it, int verify_it, int extract_it,
Vadim Bendebury2f346a32018-05-21 10:24:18 -0700368 const char *diff_file, int do_diff);
uwe884cc8b2009-06-17 12:07:12 +0000369
370#define OK 0
371#define NT 1 /* Not tested */
uwe4529d202007-08-23 13:34:59 +0000372
David Hendricks1ed1d352011-11-23 17:54:37 -0800373/* what to do in case of an error */
374enum error_action {
375 error_fail, /* fail immediately */
376 error_ignore, /* non-fatal error; continue */
377};
378
uwe97e8e272011-09-03 17:15:00 +0000379/* Something happened that shouldn't happen, but we can go on. */
mkarcher74d30132010-07-22 18:04:15 +0000380#define ERROR_NONFATAL 0x100
381
uwe97e8e272011-09-03 17:15:00 +0000382/* Something happened that shouldn't happen, we'll abort. */
383#define ERROR_FATAL -0xee
384
Edward O'Callaghan20596a82019-06-13 14:47:03 +1000385#define ERROR_FLASHROM_BUG -200
386/* We reached one of the hardcoded limits of flashrom. This can be fixed by
387 * increasing the limit of a compile-time allocation or by switching to dynamic
388 * allocation.
389 * Note: If this warning is triggered, check first for runaway registrations.
390 */
391#define ERROR_FLASHROM_LIMIT -201
392
David Hendricks1ed1d352011-11-23 17:54:37 -0800393/* Operation failed due to access restriction set in programmer or flash chip */
394#define ACCESS_DENIED -7
395extern enum error_action access_denied_action;
396
397/* convenience function for checking return codes */
398extern int ignore_error(int x);
399
Edward O'Callaghan83c77002019-06-04 15:56:19 +1000400/* cli_common.c */
Edward O'Callaghan71e30b42019-06-04 16:16:13 +1000401void print_chip_support_status(const struct flashchip *chip);
Edward O'Callaghan83c77002019-06-04 15:56:19 +1000402
snelson9cba3c62010-01-07 20:09:33 +0000403/* cli_output.c */
Edward O'Callaghan83c77002019-06-04 15:56:19 +1000404extern enum flashrom_log_level verbose_screen;
405extern enum flashrom_log_level verbose_logfile;
Souvik Ghosh3c963a42016-07-19 18:48:15 -0700406#ifndef STANDALONE
407int open_logfile(const char * const filename);
408int close_logfile(void);
409void start_logging(void);
410#endif
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +1100411enum flashrom_log_level {
412 FLASHROM_MSG_ERROR = 0,
413 FLASHROM_MSG_WARN = 1,
414 FLASHROM_MSG_INFO = 2,
415 FLASHROM_MSG_DEBUG = 3,
416 FLASHROM_MSG_DEBUG2 = 4,
417 FLASHROM_MSG_SPEW = 5,
Patrick Georgidbde2f12017-02-03 18:07:45 +0100418};
hailfinger63932d42010-06-04 23:20:21 +0000419/* Let gcc and clang check for correct printf-style format strings. */
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +1100420int print(enum flashrom_log_level level, const char *fmt, ...)
Patrick Georgidbde2f12017-02-03 18:07:45 +0100421#ifdef __MINGW32__
422__attribute__((format(gnu_printf, 2, 3)));
423#else
424__attribute__((format(printf, 2, 3)));
425#endif
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +1100426#define msg_gerr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* general errors */
427#define msg_perr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* programmer errors */
428#define msg_cerr(...) print(FLASHROM_MSG_ERROR, __VA_ARGS__) /* chip errors */
429#define msg_gwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* general warnings */
430#define msg_pwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* programmer warnings */
431#define msg_cwarn(...) print(FLASHROM_MSG_WARN, __VA_ARGS__) /* chip warnings */
432#define msg_ginfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* general info */
433#define msg_pinfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* programmer info */
434#define msg_cinfo(...) print(FLASHROM_MSG_INFO, __VA_ARGS__) /* chip info */
435#define msg_gdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* general debug */
436#define msg_pdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* programmer debug */
437#define msg_cdbg(...) print(FLASHROM_MSG_DEBUG, __VA_ARGS__) /* chip debug */
438#define msg_gdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* general debug2 */
439#define msg_pdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* programmer debug2 */
440#define msg_cdbg2(...) print(FLASHROM_MSG_DEBUG2, __VA_ARGS__) /* chip debug2 */
441#define msg_gspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* general debug spew */
442#define msg_pspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* programmer debug spew */
443#define msg_cspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* chip debug spew */
snelson9cba3c62010-01-07 20:09:33 +0000444
stepan745615e2007-10-15 21:44:47 +0000445/* spi.c */
hailfinger68002c22009-07-10 21:08:55 +0000446struct spi_command {
447 unsigned int writecnt;
448 unsigned int readcnt;
449 const unsigned char *writearr;
450 unsigned char *readarr;
451};
Nico Huber4c8a9562017-10-15 11:20:58 +0200452#define NULL_SPI_CMD { 0, 0, NULL, NULL, }
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700453int spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
uwefa98ca12008-10-18 21:14:13 +0000454 const unsigned char *writearr, unsigned char *readarr);
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700455int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds);
456uint32_t spi_get_valid_read_addr(struct flashctx *flash);
uweaf9b4df2008-09-26 13:19:02 +0000457
David Hendricks8c084212015-11-17 22:29:36 -0800458#define NUM_VOLTAGE_RANGES 16
459extern struct voltage_range voltage_ranges[];
460/* returns number of unique voltage ranges, or <0 to indicate failure */
461extern int flash_supported_voltage_ranges(enum chipbustype bus);
462
ollie5b621572004-03-20 16:46:10 +0000463#endif /* !__FLASH_H__ */