+int handle_verify_image_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
+{
+ u8 *buffer;
+ u32 buf_cnt;
+ u32 image_size;
+ int i;
+ int retval;
+ u32 checksum = 0;
+ u32 mem_checksum = 0;
+
+ image_t image;
+
+ duration_t duration;
+ char *duration_text;
+
+ target_t *target = get_current_target(cmd_ctx);
+
+ if (argc < 1)
+ {
+ command_print(cmd_ctx, "usage: verify_image <file> [offset] [type]");
+ return ERROR_OK;
+ }
+
+ if (!target)
+ {
+ ERROR("no target selected");
+ return ERROR_OK;
+ }
+
+ duration_start_measure(&duration);
+
+ if (argc >= 2)
+ {
+ image.base_address_set = 1;
+ image.base_address = strtoul(args[1], NULL, 0);
+ }
+ else
+ {
+ image.base_address_set = 0;
+ image.base_address = 0x0;
+ }
+
+ image.start_address_set = 0;
+
+ if (image_open(&image, args[0], (argc == 3) ? args[2] : NULL) != ERROR_OK)
+ {
+ return ERROR_OK;
+ }
+
+ image_size = 0x0;
+ for (i = 0; i < image.num_sections; i++)
+ {
+ buffer = malloc(image.sections[i].size);
+ if (buffer == NULL)
+ {
+ command_print(cmd_ctx, "error allocating buffer for section (%d bytes)", image.sections[i].size);
+ break;
+ }
+ if ((retval = image_read_section(&image, i, 0x0, image.sections[i].size, buffer, &buf_cnt)) != ERROR_OK)
+ {
+ ERROR("image_read_section failed with error code: %i", retval);
+ command_print(cmd_ctx, "image reading failed, verify aborted");
+ free(buffer);
+ image_close(&image);
+ return ERROR_OK;
+ }
+
+ /* calculate checksum of image */
+ image_calculate_checksum( buffer, buf_cnt, &checksum );
+
+ retval = target_checksum_memory(target, image.sections[i].base_address, buf_cnt, &mem_checksum);
+
+ if( retval != ERROR_OK )
+ {
+ command_print(cmd_ctx, "could not calculate checksum, verify aborted");
+ free(buffer);
+ image_close(&image);
+ return ERROR_OK;
+ }
+
+ if( checksum != mem_checksum )
+ {
+ /* failed crc checksum, fall back to a binary compare */
+ u8 *data;
+
+ command_print(cmd_ctx, "checksum mismatch - attempting binary compare");
+
+ data = (u8*)malloc(buf_cnt);
+
+ /* Can we use 32bit word accesses? */
+ int size = 1;
+ int count = buf_cnt;
+ if ((count % 4) == 0)
+ {
+ size *= 4;
+ count /= 4;
+ }
+ retval = target->type->read_memory(target, image.sections[i].base_address, size, count, data);
+
+ if (retval == ERROR_OK)
+ {
+ int t;
+ for (t = 0; t < buf_cnt; t++)
+ {
+ if (data[t] != buffer[t])
+ {
+ command_print(cmd_ctx, "Verify operation failed address 0x%08x. Was 0x%02x instead of 0x%02x\n", t + image.sections[i].base_address, data[t], buffer[t]);
+ free(data);
+ free(buffer);
+ image_close(&image);
+ return ERROR_OK;
+ }
+ }
+ }
+
+ free(data);
+ }
+
+ free(buffer);
+ image_size += buf_cnt;
+ }
+
+ duration_stop_measure(&duration, &duration_text);
+ command_print(cmd_ctx, "verified %u bytes in %s", image_size, duration_text);
+ free(duration_text);
+
+ image_close(&image);
+
+ return ERROR_OK;