+int target_checksum_memory(struct target_s *target, u32 address, u32 size, u32* crc)
+{
+ u8 *buffer;
+ int retval;
+ int i;
+ u32 checksum = 0;
+
+ if ((retval = target->type->checksum_memory(target, address,
+ size, &checksum)) == ERROR_TARGET_RESOURCE_NOT_AVAILABLE)
+ {
+ buffer = malloc(size);
+ if (buffer == NULL)
+ {
+ ERROR("error allocating buffer for section (%d bytes)", size);
+ return ERROR_INVALID_ARGUMENTS;
+ }
+ retval = target_read_buffer(target, address, size, buffer);
+ if (retval != ERROR_OK)
+ {
+ free(buffer);
+ return retval;
+ }
+
+ /* convert to target endianess */
+ for (i = 0; i < (size/sizeof(u32)); i++)
+ {
+ u32 target_data;
+ target_data = target_buffer_get_u32(target, &buffer[i*sizeof(u32)]);
+ target_buffer_set_u32(target, &buffer[i*sizeof(u32)], target_data);
+ }
+
+ retval = image_calculate_checksum( buffer, size, &checksum );
+ free(buffer);
+ }
+
+ *crc = checksum;
+
+ return retval;
+}
+
+int target_read_u32(struct target_s *target, u32 address, u32 *value)
+{
+ u8 value_buf[4];
+
+ int retval = target->type->read_memory(target, address, 4, 1, value_buf);
+
+ if (retval == ERROR_OK)
+ {
+ *value = target_buffer_get_u32(target, value_buf);
+ DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, *value);
+ }
+ else
+ {
+ *value = 0x0;
+ DEBUG("address: 0x%8.8x failed", address);
+ }
+
+ return retval;
+}
+
+int target_read_u16(struct target_s *target, u32 address, u16 *value)
+{
+ u8 value_buf[2];
+
+ int retval = target->type->read_memory(target, address, 2, 1, value_buf);
+
+ if (retval == ERROR_OK)
+ {
+ *value = target_buffer_get_u16(target, value_buf);
+ DEBUG("address: 0x%8.8x, value: 0x%4.4x", address, *value);
+ }
+ else
+ {
+ *value = 0x0;
+ DEBUG("address: 0x%8.8x failed", address);
+ }
+
+ return retval;
+}
+
+int target_read_u8(struct target_s *target, u32 address, u8 *value)
+{
+ int retval = target->type->read_memory(target, address, 1, 1, value);
+
+ if (retval == ERROR_OK)
+ {
+ DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, *value);
+ }
+ else
+ {
+ *value = 0x0;
+ DEBUG("address: 0x%8.8x failed", address);
+ }
+
+ return retval;
+}
+
+int target_write_u32(struct target_s *target, u32 address, u32 value)
+{
+ int retval;
+ u8 value_buf[4];
+
+ DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
+
+ target_buffer_set_u32(target, value_buf, value);
+ if ((retval = target->type->write_memory(target, address, 4, 1, value_buf)) != ERROR_OK)
+ {
+ DEBUG("failed: %i", retval);
+ }
+
+ return retval;
+}
+
+int target_write_u16(struct target_s *target, u32 address, u16 value)
+{
+ int retval;
+ u8 value_buf[2];
+
+ DEBUG("address: 0x%8.8x, value: 0x%8.8x", address, value);
+
+ target_buffer_set_u16(target, value_buf, value);
+ if ((retval = target->type->write_memory(target, address, 2, 1, value_buf)) != ERROR_OK)
+ {
+ DEBUG("failed: %i", retval);
+ }
+
+ return retval;
+}
+
+int target_write_u8(struct target_s *target, u32 address, u8 value)
+{
+ int retval;
+
+ DEBUG("address: 0x%8.8x, value: 0x%2.2x", address, value);
+
+ if ((retval = target->type->read_memory(target, address, 1, 1, &value)) != ERROR_OK)
+ {
+ DEBUG("failed: %i", retval);
+ }
+
+ return retval;
+}
+