- command_print(cmd_ctx, "image_open error: %s", image.error_str);
- return retval;
- }
-
- failed = malloc(sizeof(int) * image.num_sections);
-
- error_str = NULL;
-
- retval = flash_write(target, &image, &written, &error_str, failed, auto_erase);
-
- if (retval != ERROR_OK)
- {
- if (error_str)
- {
- command_print(cmd_ctx, "failed writing image %s: %s", args[0], error_str);
- free(error_str);
- }
- image_close(&image);
- free(failed);
- return retval;
- }
-
- for (i = 0; i < image.num_sections; i++)
- {
- if (failed[i])
- {
- command_print(cmd_ctx, "didn't write section at 0x%8.8x, size 0x%8.8x",
- image.sections[i].base_address, image.sections[i].size);
- }
- }
-
- duration_stop_measure(&duration, &duration_text);
- command_print(cmd_ctx, "wrote %u byte from file %s in %s (%f kb/s)",
- written, args[0], duration_text,
- (float)written / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
- free(duration_text);
- free(failed);
-
- image_close(&image);
-
- return ERROR_OK;
-}
-
-int handle_flash_write_binary_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
-{
- u32 offset;
- u8 *buffer;
- u32 buf_cnt;
-
- fileio_t fileio;
-
- duration_t duration;
- char *duration_text;
-
- int retval;
- flash_bank_t *p;
-
- if (argc < 3)
- {
- command_print(cmd_ctx, "usage: flash write_binary <bank> <file> <offset>");
- return ERROR_OK;
- }
-
- duration_start_measure(&duration);
-
- offset = strtoul(args[2], NULL, 0);
- p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
- if (!p)
- {
- command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
- return ERROR_OK;
- }
-
- if (fileio_open(&fileio, args[1], FILEIO_READ, FILEIO_BINARY) != ERROR_OK)
- {
- command_print(cmd_ctx, "flash write_binary error: %s", fileio.error_str);
- return ERROR_OK;
- }
-
- buffer = malloc(fileio.size);
- if (fileio_read(&fileio, fileio.size, buffer, &buf_cnt) != ERROR_OK)
- {
- command_print(cmd_ctx, "flash write_binary error: %s", fileio.error_str);
- return ERROR_OK;
- }
-
- if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
- {
- command_print(cmd_ctx, "failed writing file %s to flash bank %i at offset 0x%8.8x",
- args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
-
- switch (retval)
- {
- case ERROR_TARGET_NOT_HALTED:
- command_print(cmd_ctx, "can't work with this flash while target is running");
- break;
- case ERROR_INVALID_ARGUMENTS:
- command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
- break;
- case ERROR_FLASH_BANK_INVALID:
- command_print(cmd_ctx, "no '%s' flash found at 0x%8.8x", p->driver->name, p->base);
- break;
- case ERROR_FLASH_OPERATION_FAILED:
- command_print(cmd_ctx, "flash program error");
- break;
- case ERROR_FLASH_DST_BREAKS_ALIGNMENT:
- command_print(cmd_ctx, "offset breaks required alignment");
- break;
- case ERROR_FLASH_DST_OUT_OF_BANK:
- command_print(cmd_ctx, "destination is out of flash bank (offset and/or file too large)");
- break;
- case ERROR_FLASH_SECTOR_NOT_ERASED:
- command_print(cmd_ctx, "destination sector(s) not erased");
- break;
- default:
- command_print(cmd_ctx, "unknown error");
- }
- }
-
- free(buffer);
-
- duration_stop_measure(&duration, &duration_text);
- command_print(cmd_ctx, "wrote %"PRIi64" byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
- fileio.size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
- (float)fileio.size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
- free(duration_text);
-
- fileio_close(&fileio);
-
- return ERROR_OK;
-}
-
-void flash_set_dirty(void)
-{
- flash_bank_t *c;
- int i;
-
- /* set all flash to require erasing */
- for (c = flash_banks; c; c = c->next)
- {
- for (i = 0; i < c->num_sectors; i++)
- {
- c->sectors[i].is_erased = 0;
- }