#include "flash.h"
#include "command.h"
-#include "log.h"
#include "target.h"
#include "time_support.h"
#include <fileio.h>
#include <image.h>
+#include "log.h"
/* command handlers */
int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
extern flash_driver_t at91sam7_flash;
extern flash_driver_t str7x_flash;
extern flash_driver_t str9x_flash;
+extern flash_driver_t stellaris_flash;
+extern flash_driver_t str9xpec_flash;
flash_driver_t *flash_drivers[] =
{
&at91sam7_flash,
&str7x_flash,
&str9x_flash,
+ &stellaris_flash,
+ &str9xpec_flash,
NULL,
};
int last = strtoul(args[2], NULL, 0);
int retval;
flash_bank_t *p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
- struct timeval start, end, duration;
-
- gettimeofday(&start, NULL);
+ duration_t duration;
+ char *duration_text;
+
+ duration_start_measure(&duration);
if (!p)
{
}
else
{
- gettimeofday(&end, NULL);
- timeval_subtract(&duration, &end, &start);
-
- command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %is %ius", first, last, strtoul(args[0], 0, 0), duration.tv_sec, duration.tv_usec);
+ duration_stop_measure(&duration, &duration_text);
+
+ command_print(cmd_ctx, "erased sectors %i through %i on flash bank %i in %s", first, last, strtoul(args[0], 0, 0), duration_text);
+ free(duration_text);
}
}
else
int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
u32 offset;
- u32 binary_size;
u8 *buffer;
u32 buf_cnt;
+ u32 image_size;
+ int i;
image_t image;
duration_start_measure(&duration);
- identify_image_type(&image.type, (argc == 4) ? args[3] : NULL);
-
image.base_address_set = 1;
image.base_address = strtoul(args[1], NULL, 0);
return ERROR_OK;
}
- if (image_open(&image, args[1], FILEIO_READ) != ERROR_OK)
+ if (image_open(&image, args[1], (argc == 4) ? args[3] : NULL) != ERROR_OK)
{
command_print(cmd_ctx, "flash write error: %s", image.error_str);
return ERROR_OK;
}
- binary_size = image.size;
- buffer = malloc(binary_size);
-
- image_read(&image, binary_size, buffer, &buf_cnt);
-
- if ((retval = p->driver->write(p, buffer, offset, buf_cnt)) != ERROR_OK)
+ image_size = 0x0;
+ for (i = 0; i < image.num_sections; i++)
{
- 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)
+ buffer = malloc(image.sections[i].size);
+ if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
{
- 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");
+ ERROR("image_read_section failed with error code: %i", retval);
+ command_print(cmd_ctx, "image reading failed, flash write aborted");
+ free(buffer);
+ image_close(&image);
+ 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");
+ }
+ }
+ image_size += buf_cnt;
+
+ free(buffer);
}
- else
- {
- duration_stop_measure(&duration, &duration_text);
- command_print(cmd_ctx, "wrote file %s to flash bank %i at offset 0x%8.8x in %s",
- args[1], strtoul(args[0], NULL, 0), offset, duration_text);
- free(duration_text);
- }
+
- free(buffer);
+ duration_stop_measure(&duration, &duration_text);
+ command_print(cmd_ctx, "wrote %u byte from file %s to flash bank %i at offset 0x%8.8x in %s (%f kb/s)",
+ image_size, args[1], strtoul(args[0], NULL, 0), offset, duration_text,
+ (float)image_size / 1024.0 / ((float)duration.duration.tv_sec + ((float)duration.duration.tv_usec / 1000000.0)));
+ free(duration_text);
+
image_close(&image);
return ERROR_OK;