+ image.start_address_set = 0;
+
+ if (image_open(&image, args[0], (argc == 3) ? args[2] : NULL) != ERROR_OK)
+ {
+ command_print(cmd_ctx, "verify_image error: %s", image.error_str);
+ 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;