22 COMMAND_HELPER(flash_command_get_bank_probe_optional,
unsigned int name_index,
39 unsigned int bank_num;
55 name_index,
bank,
true);
63 bool show_sectors =
false;
64 bool prot_block_available;
66 if (CMD_ARGC < 1 || CMD_ARGC > 2)
70 if (strcmp(
"sectors",
CMD_ARGV[1]) == 0)
100 LOG_INFO(
"Flash protection check is not implemented.");
104 ", buswidth %u, chipwidth %u",
113 if (!show_sectors && prot_block_available) {
121 for (j = 0; j < num_blocks; j++) {
122 char *protect_state =
"";
125 protect_state =
"not protected";
127 protect_state =
"protected";
128 else if (!show_sectors || !prot_block_available)
129 protect_state =
"protection state unknown";
132 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIu32
"kB) %s",
136 block_array[j].
size >> 10,
145 LOG_ERROR(
"error retrieving flash info");
199 "checking erase state of flash bank #%s at "
211 for (
unsigned int j = 0; j < p->
num_sectors; j++) {
215 erase_state =
"not erased";
219 erase_state =
"erase state unknown";
223 "\t#%3i: 0x%8.8" PRIx32
" (0x%" PRIx32
" %" PRIu32
"kB) %s",
246 bool do_unlock =
false;
254 if (strcmp(
"pad",
CMD_ARGV[0]) == 0)
256 else if (strcmp(
"unlock",
CMD_ARGV[0]) == 0)
325 LOG_INFO(
"bank has no sectors, ignoring specified sectors");
330 if (strcmp(
CMD_ARGV[2],
"last") == 0)
335 if (!(first <= last)) {
337 "first sector must be <= last");
343 "last sector must be <= %u",
364 "through %" PRIu32
" on flash bank %u "
393 if (strcmp(
CMD_ARGV[2],
"last") == 0)
394 last = num_blocks - 1;
401 if (!(first <= last)) {
403 "first %s must be <= last",
408 if (!(last <= (uint32_t)(num_blocks - 1))) {
410 "last %s must be <= %d",
423 " through %" PRIu32
" on flash bank %d",
424 set ?
"set" :
"cleared",
442 bool auto_unlock =
false;
445 if (strcmp(
CMD_ARGV[0],
"erase") == 0) {
450 }
else if (strcmp(
CMD_ARGV[0],
"unlock") == 0) {
485 auto_unlock,
true,
false);
493 "in %fs (%0.3f KiB/s)", written,
CMD_ARGV[0],
545 "in %fs (%0.3f KiB/s)", verified,
CMD_ARGV[0],
593 if ((wordsize <
sizeof(
pattern)) && (
pattern >> (8 * wordsize) != 0)) {
602 LOG_ERROR(
"Cannot cross flash bank borders");
606 uint32_t size_bytes =
count * wordsize;
610 uint32_t aligned_size = aligned_end + 1 - aligned_start;
611 uint32_t padding_at_start =
address - aligned_start;
612 uint32_t padding_at_end = aligned_end - end_addr;
614 uint8_t *
buffer = malloc(aligned_size);
618 if (padding_at_start) {
619 memset(
buffer,
bank->default_padded_value, padding_at_start);
621 " breaks the required alignment of flash bank %s",
624 padding_at_start, aligned_start);
627 uint8_t *ptr =
buffer + padding_at_start;
631 for (i = 0; i <
count; i++, ptr += wordsize)
635 for (i = 0; i <
count; i++, ptr += wordsize)
639 for (i = 0; i <
count; i++, ptr += wordsize)
652 if (padding_at_end) {
653 memset(ptr,
bank->default_padded_value, padding_at_end);
655 " bytes (bank write end alignment)",
656 end_addr + 1, padding_at_end);
671 uint64_t readback = 0;
690 ", read back 0x%02" PRIx64
", expected 0x%02" PRIx64,
700 " in %fs (%0.3f KiB/s)", size_bytes,
address,
714 if (CMD_ARGC < 1 || CMD_ARGC > 2)
724 unsigned int wordsize;
749 uint32_t sizebytes =
count * wordsize;
787 unsigned int width_bits;
794 switch (width_bits) {
810 const unsigned int width = width_bits / 8;
826 uint32_t sizebytes =
count * width_bits;
840 char *separator =
"";
843 const size_t chunk_len = MIN(
count, max_chunk_len);
849 addr, width_bits, chunk_len);
859 for (
size_t i = 0; i < chunk_len ; i++) {
897 if (CMD_ARGC < 2 || CMD_ARGC > 3)
914 LOG_ERROR(
"Offset 0x%8.8" PRIx32
" is out of range of the flash bank",
932 LOG_INFO(
"Nothing to write to flash bank");
938 LOG_INFO(
"File content exceeds flash bank size. Only writing the "
939 "first %zu bytes of the file",
length);
945 uint32_t aligned_size = aligned_end + 1 - aligned_start;
946 uint32_t padding_at_start = start_addr - aligned_start;
947 uint32_t padding_at_end = aligned_end - end_addr;
949 buffer = malloc(aligned_size);
956 if (padding_at_start) {
957 memset(
buffer,
bank->default_padded_value, padding_at_start);
958 LOG_INFO(
"Start offset 0x%08" PRIx32
959 " breaks the required alignment of flash bank %s",
962 padding_at_start, aligned_start);
965 uint8_t *ptr =
buffer + padding_at_start;
982 if (padding_at_end) {
983 memset(ptr,
bank->default_padded_value, padding_at_end);
985 " bytes (bank write end alignment)",
986 end_addr + 1, padding_at_end);
995 " at offset 0x%8.8" PRIx32
" in %fs (%0.3f KiB/s)",
1013 if (CMD_ARGC < 2 || CMD_ARGC > 4)
1031 LOG_ERROR(
"Offset 0x%8.8" PRIx32
" is out of range of the flash bank",
1042 LOG_ERROR(
"Length of %" PRIu32
" bytes with offset 0x%8.8" PRIx32
1077 " at offset 0x%8.8" PRIx32
" in %fs (%0.3f KiB/s)",
1088 uint8_t *buffer_file, *buffer_flash;
1095 if (CMD_ARGC < 2 || CMD_ARGC > 3)
1112 LOG_ERROR(
"Offset 0x%8.8" PRIx32
" is out of range of the flash bank",
1132 LOG_INFO(
"Nothing to compare with flash bank");
1138 LOG_INFO(
"File content exceeds flash bank size. Only comparing the "
1139 "first %zu bytes of the file",
length);
1141 buffer_file = malloc(
length);
1156 if (read_cnt !=
length) {
1162 buffer_flash = malloc(
length);
1163 if (!buffer_flash) {
1179 " at offset 0x%8.8" PRIx32
" in %fs (%0.3f KiB/s)",
1183 differ = memcmp(buffer_file, buffer_flash,
length);
1188 for (t = 0; t <
length; t++) {
1189 if (buffer_flash[t] == buffer_file[t])
1191 command_print(
CMD,
"diff %d address 0x%08" PRIx32
". Was 0x%02x instead of 0x%02x",
1192 diffs, t +
offset, buffer_flash[t], buffer_file[t]);
1193 if (diffs++ >= 127) {
1229 command_print(
CMD,
"Default padded value set to 0x%" PRIx8
" for flash bank %u",
1238 .handler = handle_flash_probe_command,
1240 .usage =
"(bank_name|num)",
1241 .help =
"Identify a flash bank.",
1245 .handler = handle_flash_info_command,
1247 .usage =
"(bank_name|num) ['sectors']",
1248 .help =
"Print information about a flash bank.",
1251 .name =
"erase_check",
1252 .handler = handle_flash_erase_check_command,
1254 .usage =
"(bank_name|num)",
1255 .help =
"Check erase state of all blocks in a "
1259 .name =
"erase_sector",
1260 .handler = handle_flash_erase_command,
1262 .usage =
"(bank_name|num) first_sector_num (last_sector_num|'last')",
1263 .help =
"Erase a range of sectors in a flash bank.",
1266 .name =
"erase_address",
1267 .handler = handle_flash_erase_address_command,
1269 .usage =
"['pad'] ['unlock'] address length",
1270 .help =
"Erase flash sectors starting at address and "
1271 "continuing for length bytes. If 'pad' is specified, "
1272 "data outside that range may also be erased: the start "
1273 "address may be decreased, and length increased, so "
1274 "that all of the first and last sectors are erased. "
1275 "If 'unlock' is specified, then the flash is unprotected "
1281 .handler = handle_flash_fill_command,
1283 .usage =
"address value n",
1284 .help =
"Fill n double-words with 64-bit value, starting at "
1285 "word address. (No autoerase.)",
1289 .handler = handle_flash_fill_command,
1291 .usage =
"address value n",
1292 .help =
"Fill n words with 32-bit value, starting at "
1293 "word address. (No autoerase.)",
1297 .handler = handle_flash_fill_command,
1299 .usage =
"address value n",
1300 .help =
"Fill n halfwords with 16-bit value, starting at "
1301 "word address. (No autoerase.)",
1305 .handler = handle_flash_fill_command,
1307 .usage =
"address value n",
1308 .help =
"Fill n bytes with 8-bit value, starting at "
1309 "word address. (No autoerase.)",
1313 .handler = handle_flash_md_command,
1315 .usage =
"address [count]",
1316 .help =
"Display bytes from flash.",
1320 .handler = handle_flash_md_command,
1322 .usage =
"address [count]",
1323 .help =
"Display half-words from flash.",
1327 .handler = handle_flash_md_command,
1329 .usage =
"address [count]",
1330 .help =
"Display words from flash.",
1333 .name =
"read_memory",
1335 .handler = handle_flash_read_memory_command,
1336 .help =
"Read Tcl list of 8/16/32/64 bit numbers from flash memory",
1337 .usage =
"address width count",
1340 .name =
"write_bank",
1341 .handler = handle_flash_write_bank_command,
1343 .usage =
"bank_id filename [offset]",
1344 .help =
"Write binary data from file to flash bank. Allow optional "
1345 "offset from beginning of the bank (defaults to zero).",
1348 .name =
"write_image",
1349 .handler = handle_flash_write_image_command,
1351 .usage =
"[erase] [unlock] filename [offset [file_type]]",
1352 .help =
"Write an image to flash. Optionally first unprotect "
1353 "and/or erase the region to be used. Allow optional "
1354 "offset from beginning of bank (defaults to zero)",
1357 .name =
"verify_image",
1358 .handler = handle_flash_verify_image_command,
1360 .usage =
"filename [offset [file_type]]",
1361 .help =
"Verify an image against flash. Allow optional "
1362 "offset from beginning of bank (defaults to zero)",
1365 .name =
"read_bank",
1366 .handler = handle_flash_read_bank_command,
1368 .usage =
"(bank_name|num) filename [offset [length]]",
1369 .help =
"Read binary data from flash bank to file. Allow optional "
1370 "offset from beginning of the bank (defaults to zero).",
1373 .name =
"verify_bank",
1374 .handler = handle_flash_verify_bank_command,
1376 .usage =
"(bank_name|num) filename [offset]",
1377 .help =
"Compare the contents of a file with the contents of the "
1378 "flash bank. Allow optional offset from beginning of the bank "
1379 "(defaults to zero).",
1383 .handler = handle_flash_protect_command,
1385 .usage =
"(bank_name|num) first_block [last_block|'last'] "
1387 .help =
"Turn protection on or off for a range of protection "
1388 "blocks or sectors in a given flash bank. "
1389 "See 'flash info' output for a list of blocks.",
1392 .name =
"padded_value",
1393 .handler = handle_flash_padded_value_command,
1395 .usage =
"(bank_name|num) value",
1396 .help =
"Set default flash padded value",
1412 LOG_ERROR(
"usage: flash bank <name> <driver> "
1413 "<base> <size> <chip_width> <bus_width> <target>");
1417 const char *bank_name = *
CMD_ARGV++;
1426 const char *driver_name =
CMD_ARGV[0];
1430 LOG_ERROR(
"flash driver '%s' not found", driver_name);
1437 LOG_ERROR(
"flash bank name '%s' already exists", bank_name);
1445 LOG_ERROR(
"couldn't register '%s' commands",
1451 struct flash_bank *c = calloc(1,
sizeof(*c));
1452 c->
name = strdup(bank_name);
1473 LOG_DEBUG(
"'%s' driver usage field missing", driver_name);
1487 "buswidth %u, chipwidth %u", p->bank_number,
1488 p->name, p->driver->name, p->base, p->size,
1489 p->bus_width, p->chip_width);
1505 " size 0x%" PRIx32
"\n"
1510 p->name, p->driver->name, p->base, p->size, p->bus_width, p->chip_width,
1522 static bool flash_initialized;
1523 if (flash_initialized) {
1524 LOG_INFO(
"'flash init' has already been called");
1527 flash_initialized =
true;
1529 LOG_DEBUG(
"Initializing flash devices...");
1536 .handler = handle_flash_bank_command,
1538 .usage =
"bank_name driver_name base_address size_bytes "
1539 "chip_width_bytes bus_width_bytes target "
1540 "[driver_options ...]",
1541 .help =
"Define a new bank with the given name, "
1542 "using the specified NOR flash driver.",
1547 .handler = handle_flash_init_command,
1548 .help =
"Initialize flash devices.",
1554 .handler = handle_flash_banks_command,
1555 .help =
"Display table with information about flash banks.",
1561 .handler = handle_flash_list,
1562 .help =
"Returns a list of details about the flash banks.",
1571 .help =
"NOR flash command group",
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
void command_print(struct command_invocation *cmd, const char *format,...)
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
#define CMD_NAME
Use this macro to access the name of the command being handled, rather than accessing the variable di...
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
#define COMMAND_PARSE_ADDRESS(in, out)
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
#define ERROR_COMMAND_SYNTAX_ERROR
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_COMMAND_ARGUMENT_INVALID
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
uint8_t pattern
Fill pattern.
uint64_t buffer
Pointer to data buffer to send over SPI.
uint32_t size
Size of dw_spi_transaction::buffer.
uint32_t buffer_size
Size of dw_spi_program::buffer.
uint32_t address
Starting address. Sector aligned.
#define ERROR_FLASH_OPER_UNSUPPORTED
target_addr_t flash_write_align_start(struct flash_bank *bank, target_addr_t addr)
Get aligned start address of a flash write region.
int flash_driver_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
struct flash_bank * get_flash_bank_by_name_noprobe(const char *name)
Returns the flash bank specified by name, which matches the driver name and a suffix (option) specify...
target_addr_t flash_write_align_end(struct flash_bank *bank, target_addr_t addr)
Get aligned end address of a flash write region.
int flash_driver_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
int flash_driver_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
void flash_bank_add(struct flash_bank *bank)
Adds a new NOR bank to the global list of banks.
int flash_unlock_address_range(struct target *target, target_addr_t addr, uint32_t length)
struct flash_bank * get_flash_bank_by_num_noprobe(unsigned int num)
Returns the flash bank like get_flash_bank_by_num(), without probing.
int get_flash_bank_by_addr(struct target *target, target_addr_t addr, bool check, struct flash_bank **result_bank)
Returns the flash bank located at a specified address.
int flash_driver_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
int get_flash_bank_by_name(const char *name, struct flash_bank **bank_result)
Returns the flash bank specified by name, which matches the driver name and a suffix (option) specify...
struct flash_bank * flash_bank_list(void)
int get_flash_bank_by_num(unsigned int num, struct flash_bank **bank)
Returns the flash bank like get_flash_bank_by_name(), without probing.
int flash_erase_address_range(struct target *target, bool pad, target_addr_t addr, uint32_t length, bool skip_banks_without_erase)
Erases length bytes in the target flash, starting at addr.
int flash_write_unlock_verify(struct target *target, struct image *image, uint32_t *written, bool erase, bool unlock, bool write, bool verify)
static const struct command_registration flash_config_command_handlers[]
static const struct command_registration flash_command_handlers[]
void flash_set_dirty(void)
Forces targets to re-examine their erase/protection state.
COMMAND_HELPER(flash_command_get_bank_probe_optional, unsigned int name_index, struct flash_bank **bank, bool do_probe)
Retrieves bank from a command argument, reporting errors parsing the bank identifier or retrieving th...
static const struct command_registration flash_exec_command_handlers[]
static int flash_init_drivers(struct command_context *cmd_ctx)
COMMAND_HANDLER(handle_flash_info_command)
int flash_register_commands(struct command_context *cmd_ctx)
Registers the 'flash' subsystem commands.
int fileio_write(struct fileio *fileio, size_t size, const void *buffer, size_t *size_written)
int fileio_read(struct fileio *fileio, size_t size, void *buffer, size_t *size_read)
int fileio_close(struct fileio *fileio)
int fileio_size(struct fileio *fileio, size_t *size)
FIX!!!!
int fileio_open(struct fileio **fileio, const char *url, enum fileio_access access_type, enum fileio_type type)
void image_close(struct image *image)
int image_open(struct image *image, const char *url, const char *type_string)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
#define FLASH_WRITE_GAP_SECTOR
const struct flash_driver * flash_driver_find_by_name(const char *name)
Find a NOR flash driver by its name.
target_addr_t addr
Start address to search for the control block.
const char * usage
a string listing the options and arguments, required or optional
Provides details of a flash bank, available either on-chip or through a major interface.
unsigned int num_prot_blocks
The number of protection blocks in this bank.
struct flash_sector * sectors
Array of sectors, allocated and initialized by the flash driver.
uint8_t default_padded_value
Default padded value used, normally this matches the flash erased value.
const struct flash_driver * driver
Driver for this bank.
unsigned int chip_width
Width of the chip in bytes (1,2,4 bytes)
target_addr_t base
The base address of this bank.
uint32_t size
The size of this chip bank, in bytes.
unsigned int num_sectors
The number of sectors on this chip.
struct flash_sector * prot_blocks
Array of protection blocks, allocated and initialized by the flash driver.
unsigned int bus_width
Maximum bus width, in bytes (1,2,4 bytes)
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
struct flash_bank * next
The next flash bank on this chip.
struct target * target
Target to which this bank belongs.
uint32_t minimal_write_gap
Minimal gap between sections to discontinue flash write Default FLASH_WRITE_GAP_SECTOR splits the wri...
uint8_t erased_value
Erased value.
unsigned int bank_number
The 'bank' (or chip number) of this instance.
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
const struct command_registration * commands
An array of driver-specific commands to register.
int(* auto_probe)(struct flash_bank *bank)
A more gentle flavor of flash_driver::probe, performing setup with less noise.
const char * usage
Gives a human-readable description of arguments.
int(* erase_check)(struct flash_bank *bank)
Check the erasure status of a flash bank.
int(* info)(struct flash_bank *bank, struct command_invocation *cmd)
Display human-readable information about the flash bank.
int(* probe)(struct flash_bank *bank)
Probe to determine what kind of flash is present.
int(* protect_check)(struct flash_bank *bank)
Determine if the specific bank is "protected" or not.
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Describes the geometry and status of a single flash sector within a flash bank.
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
uint32_t offset
Bus offset from start of the flash chip (in bytes).
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
uint32_t size
Number of bytes in this flash sector.
uint64_t target_buffer_get_u64(struct target *target, const uint8_t *buffer)
struct target * get_target(const char *id)
void target_buffer_set_u16(struct target *target, uint8_t *buffer, uint16_t value)
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
void target_buffer_set_u64(struct target *target, uint8_t *buffer, uint64_t value)
uint16_t target_buffer_get_u16(struct target *target, const uint8_t *buffer)
struct target * get_current_target(struct command_context *cmd_ctx)
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
void target_handle_md_output(struct command_invocation *cmd, struct target *target, target_addr_t address, unsigned int size, unsigned int count, const uint8_t *buffer, bool include_address)
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
float duration_elapsed(const struct duration *duration)
int duration_measure(struct duration *duration)
Update the duration->elapsed field to finish the duration measurement.
int duration_start(struct duration *duration)
Update the duration->start field to start the duration measurement.
float duration_kbps(const struct duration *duration, size_t count)