* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#include "flash.h"
#include "command.h"
#include "log.h"
#include "target.h"
+#include "time_support.h"
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
+#include <fileio.h>
+#include <image.h>
+
/* command handlers */
int handle_flash_bank_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
int handle_flash_banks_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
extern flash_driver_t cfi_flash;
extern flash_driver_t at91sam7_flash;
extern flash_driver_t str7x_flash;
+extern flash_driver_t str9x_flash;
flash_driver_t *flash_drivers[] =
{
&cfi_flash,
&at91sam7_flash,
&str7x_flash,
+ &str9x_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);
+
if (!p)
{
command_print(cmd_ctx, "flash bank '#%s' is out of bounds", args[0]);
command_print(cmd_ctx, "unknown error");
}
}
+ 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);
+ }
}
else
{
command_print(cmd_ctx, "unknown error");
}
}
+ else
+ {
+ command_print(cmd_ctx, "%s protection for sectors %i through %i on flash bank %i", (set) ? "set" : "cleared", first, last, strtoul(args[0], 0, 0));
+ }
}
else
{
int handle_flash_write_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
{
- FILE *binary;
u32 offset;
- struct stat binary_stat;
u32 binary_size;
u8 *buffer;
u32 buf_cnt;
+
+ image_t image;
+
+ duration_t duration;
+ char *duration_text;
+
int retval;
flash_bank_t *p;
if (argc < 3)
{
- command_print(cmd_ctx, "usage: flash write <bank> <file> <offset>");
+ command_print(cmd_ctx, "usage: flash write <bank> <file> <offset> [type]");
return ERROR_OK;
}
+ 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);
+
+ image.start_address_set = 0;
+
offset = strtoul(args[2], NULL, 0);
p = get_flash_bank_by_num(strtoul(args[0], NULL, 0));
if (!p)
return ERROR_OK;
}
- if (stat(args[1], &binary_stat) == -1)
- {
- ERROR("couldn't stat() %s: %s", args[1], strerror(errno));
- return ERROR_OK;
- }
-
- if (S_ISDIR(binary_stat.st_mode))
- {
- ERROR("%s is a directory", args[1]);
- command_print(cmd_ctx,"%s is a directory", args[1]);
- return ERROR_OK;
- }
-
- if (binary_stat.st_size == 0){
- ERROR("Empty file %s", args[1]);
- command_print(cmd_ctx,"Empty file %s", args[1]);
- return ERROR_OK;
- }
-
- if (!(binary = fopen(args[1], "r")))
+ if (image_open(&image, args[1], FILEIO_READ) != ERROR_OK)
{
- ERROR("couldn't open %s: %s", args[1], strerror(errno));
- command_print(cmd_ctx, "couldn't open %s", args[1]);
+ command_print(cmd_ctx, "flash write error: %s", image.error_str);
return ERROR_OK;
}
-
- binary_size = binary_stat.st_size;
+
+ binary_size = image.size;
buffer = malloc(binary_size);
- buf_cnt = fread(buffer, 1, binary_size, binary);
+ image_read(&image, binary_size, buffer, &buf_cnt);
+
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, "unknown error");
}
}
- free(buffer);
- fclose(binary);
+ 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);
+ }
- command_print(cmd_ctx, "wrote file %s to flash bank %i at offset 0x%8.8x", args[1], strtoul(args[0], NULL, 0), strtoul(args[2], NULL, 0));
+ free(buffer);
+ image_close(&image);
return ERROR_OK;
-
}