-//#ifdef FILEIO_BUFFER_COMPLETE_IHEX
-int fileio_ihex_buffer_complete(fileio_t *fileio)
-{
- fileio_image_t *image = fileio->pri_type_private;
- fileio_ihex_t *ihex = fileio->sec_type_private;
- u32 raw_bytes_read, raw_bytes;
- int retval;
- u32 full_address = image->base_address;
- char *buffer = malloc(ihex->raw_size);
- u32 cooked_bytes = 0x0;
-
- ihex->raw_size = fileio->size;
- ihex->buffer = malloc(ihex->raw_size >> 1);
-
- if ((retval = fileio_dispatch_read(fileio, ihex->raw_size, (u8*)buffer, &raw_bytes_read)) != ERROR_OK)
- {
- free(buffer);
- ERROR("failed buffering IHEX file, read failed");
- return ERROR_FILEIO_OPERATION_FAILED;
- }
-
- if (raw_bytes_read != ihex->raw_size)
- {
- free(buffer);
- ERROR("failed buffering complete IHEX file, only partially read");
- return ERROR_FILEIO_OPERATION_FAILED;
- }
-
- raw_bytes = 0x0;
- while (raw_bytes < raw_bytes_read)
- {
- u32 count;
- u32 address;
- u32 record_type;
- u32 checksum;
-
- if (sscanf(&buffer[raw_bytes], ":%2x%4x%2x", &count, &address, &record_type) != 3)
- {
- snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "invalid IHEX record");
- return ERROR_FILEIO_OPERATION_FAILED;
- }
- raw_bytes += 9;
-
- if (record_type == 0)
- {
- if ((full_address & 0xffff) != address)
- {
- free(buffer);
- ERROR("can't handle non-linear IHEX file");
- snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "can't handle non-linear IHEX file");
- return ERROR_FILEIO_OPERATION_FAILED;
- }
-
- while (count-- > 0)
- {
- sscanf(&buffer[raw_bytes], "%2hhx", &ihex->buffer[cooked_bytes]);
- raw_bytes += 2;
- cooked_bytes += 1;
- full_address++;
- }
- }
- else if (record_type == 1)
- {
- free(buffer);
- fileio->size = cooked_bytes;
- return ERROR_OK;
- }
- else if (record_type == 4)
- {
- u16 upper_address;
-
- sscanf(&buffer[raw_bytes], "%4hx", &upper_address);
- raw_bytes += 4;
-
- if ((full_address >> 16) != upper_address)
- {
- free(buffer);
- ERROR("can't handle non-linear IHEX file");
- snprintf(fileio->error_str, FILEIO_MAX_ERROR_STRING, "can't handle non-linear IHEX file");
- return ERROR_FILEIO_OPERATION_FAILED;
- }
- }
- else if (record_type == 5)
- {
- u32 start_address;
-
- sscanf(&buffer[raw_bytes], "%8x", &start_address);
- raw_bytes += 8;
-
- image->has_start_address = 1;
- image->start_address = be_to_h_u32((u8*)&start_address);
- }
- else