/* convert ELF header field to host endianness */
#define field16(elf,field)\
((elf->endianness==ELFDATA2LSB)? \
/* convert ELF header field to host endianness */
#define field16(elf,field)\
((elf->endianness==ELFDATA2LSB)? \
#define field32(elf,field)\
((elf->endianness==ELFDATA2LSB)? \
#define field32(elf,field)\
((elf->endianness==ELFDATA2LSB)? \
/* read the first 4 bytes of image */
if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
return retval;
}
retval = fileio_read(&fileio, 9, buffer, &read_bytes);
/* read the first 4 bytes of image */
if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
return retval;
}
retval = fileio_read(&fileio, 9, buffer, &read_bytes);
- else if ((buffer[0]==':') /* record start byte */
- &&(isxdigit(buffer[1]))
- &&(isxdigit(buffer[2]))
- &&(isxdigit(buffer[3]))
- &&(isxdigit(buffer[4]))
- &&(isxdigit(buffer[5]))
- &&(isxdigit(buffer[6]))
- &&(buffer[7]=='0') /* record type : 00 -> 05 */
- &&(buffer[8]>='0')&&(buffer[8]<'6'))
+ else if ((buffer[0]==':') /* record start byte */
+ &&(isxdigit(buffer[1]))
+ &&(isxdigit(buffer[2]))
+ &&(isxdigit(buffer[3]))
+ &&(isxdigit(buffer[4]))
+ &&(isxdigit(buffer[5]))
+ &&(isxdigit(buffer[6]))
+ &&(buffer[7]=='0') /* record type : 00 -> 05 */
+ &&(buffer[8]>='0')&&(buffer[8]<'6'))
/* we can't determine the number of sections that we'll have to create ahead of time,
* so we locally hold them until parsing is finished */
image_section_t section[IMAGE_MAX_SECTIONS];
/* we can't determine the number of sections that we'll have to create ahead of time,
* so we locally hold them until parsing is finished */
image_section_t section[IMAGE_MAX_SECTIONS];
section[image->num_sections].base_address = 0x0;
section[image->num_sections].size = 0x0;
section[image->num_sections].flags = 0;
section[image->num_sections].base_address = 0x0;
section[image->num_sections].size = 0x0;
section[image->num_sections].flags = 0;
if (sscanf(&lpszLine[bytes_read], ":%2x%4x%2x", &count, &address, &record_type) != 3)
{
return ERROR_IMAGE_FORMAT_ERROR;
}
bytes_read += 9;
if (sscanf(&lpszLine[bytes_read], ":%2x%4x%2x", &count, &address, &record_type) != 3)
{
return ERROR_IMAGE_FORMAT_ERROR;
}
bytes_read += 9;
-
- cal_checksum += (u8)count;
- cal_checksum += (u8)(address >> 8);
- cal_checksum += (u8)address;
- cal_checksum += (u8)record_type;
-
+
+ cal_checksum += (uint8_t)count;
+ cal_checksum += (uint8_t)(address >> 8);
+ cal_checksum += (uint8_t)address;
+ cal_checksum += (uint8_t)record_type;
+
- sscanf(&lpszLine[bytes_read], "%2x", (u32*)&ihex->buffer[cooked_bytes]);
- cal_checksum += (u8)ihex->buffer[cooked_bytes];
+ unsigned value;
+ sscanf(&lpszLine[bytes_read], "%2x", &value);
+ ihex->buffer[cooked_bytes] = (uint8_t)value;
+ cal_checksum += (uint8_t)ihex->buffer[cooked_bytes];
/* copy section information */
image->sections = malloc(sizeof(image_section_t) * image->num_sections);
for (i = 0; i < image->num_sections; i++)
/* copy section information */
image->sections = malloc(sizeof(image_section_t) * image->num_sections);
for (i = 0; i < image->num_sections; i++)
if ((full_address >> 4) != upper_address)
{
/* we encountered a nonconsecutive location, create a new section,
if ((full_address >> 4) != upper_address)
{
/* we encountered a nonconsecutive location, create a new section,
section[image->num_sections].flags = 0;
section[image->num_sections].private = &ihex->buffer[cooked_bytes];
}
section[image->num_sections].flags = 0;
section[image->num_sections].private = &ihex->buffer[cooked_bytes];
}
(full_address & 0xffff) | (upper_address << 4);
full_address = (full_address & 0xffff) | (upper_address << 4);
}
}
else if (record_type == 3) /* Start Segment Address Record */
{
(full_address & 0xffff) | (upper_address << 4);
full_address = (full_address & 0xffff) | (upper_address << 4);
}
}
else if (record_type == 3) /* Start Segment Address Record */
{
/* "Start Segment Address Record" will not be supported */
/* but we must consume it, and do not create an error. */
while (count-- > 0)
{
sscanf(&lpszLine[bytes_read], "%2x", &dummy);
/* "Start Segment Address Record" will not be supported */
/* but we must consume it, and do not create an error. */
while (count-- > 0)
{
sscanf(&lpszLine[bytes_read], "%2x", &dummy);
if ((full_address >> 16) != upper_address)
{
/* we encountered a nonconsecutive location, create a new section,
if ((full_address >> 16) != upper_address)
{
/* we encountered a nonconsecutive location, create a new section,
section[image->num_sections].flags = 0;
section[image->num_sections].private = &ihex->buffer[cooked_bytes];
}
section[image->num_sections].flags = 0;
section[image->num_sections].private = &ihex->buffer[cooked_bytes];
}
(full_address & 0xffff) | (upper_address << 16);
full_address = (full_address & 0xffff) | (upper_address << 16);
}
}
else if (record_type == 5) /* Start Linear Address Record */
{
(full_address & 0xffff) | (upper_address << 16);
full_address = (full_address & 0xffff) | (upper_address << 16);
}
}
else if (record_type == 5) /* Start Linear Address Record */
{
- cal_checksum += (u8)(start_address >> 24);
- cal_checksum += (u8)(start_address >> 16);
- cal_checksum += (u8)(start_address >> 8);
- cal_checksum += (u8)start_address;
+ cal_checksum += (uint8_t)(start_address >> 24);
+ cal_checksum += (uint8_t)(start_address >> 16);
+ cal_checksum += (uint8_t)(start_address >> 8);
+ cal_checksum += (uint8_t)start_address;
}
else
{
LOG_ERROR("unhandled IHEX record type: %i", record_type);
return ERROR_IMAGE_FORMAT_ERROR;
}
}
else
{
LOG_ERROR("unhandled IHEX record type: %i", record_type);
return ERROR_IMAGE_FORMAT_ERROR;
}
LOG_ERROR("premature end of IHEX file, no end-of-file record found");
return ERROR_IMAGE_FORMAT_ERROR;
}
LOG_ERROR("premature end of IHEX file, no end-of-file record found");
return ERROR_IMAGE_FORMAT_ERROR;
}
- if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (u8*)elf->header, &read_bytes)) != ERROR_OK)
+ if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (uint8_t*)elf->header, &read_bytes)) != ERROR_OK)
{
LOG_ERROR("cannot read ELF file header, read failed");
return ERROR_FILEIO_OPERATION_FAILED;
{
LOG_ERROR("cannot read ELF file header, read failed");
return ERROR_FILEIO_OPERATION_FAILED;
elf->endianness = elf->header->e_ident[EI_DATA];
if ((elf->endianness!=ELFDATA2LSB)
&&(elf->endianness!=ELFDATA2MSB))
elf->endianness = elf->header->e_ident[EI_DATA];
if ((elf->endianness!=ELFDATA2LSB)
&&(elf->endianness!=ELFDATA2MSB))
- if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (u8*)elf->segments, &read_bytes)) != ERROR_OK)
+ if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (uint8_t*)elf->segments, &read_bytes)) != ERROR_OK)
image->start_address_set = 1;
image->start_address = field32(elf,elf->header->e_entry);
return ERROR_OK;
}
image->start_address_set = 1;
image->start_address = field32(elf,elf->header->e_entry);
return ERROR_OK;
}
-int image_elf_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
+static int image_elf_read_section(image_t *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, uint32_t *size_read)
{
image_elf_t *elf = image->type_private;
Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
{
image_elf_t *elf = image->type_private;
Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
LOG_DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
/* read initialized data in current segment if any */
LOG_DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
/* read initialized data in current segment if any */
/* we can't determine the number of sections that we'll have to create ahead of time,
* so we locally hold them until parsing is finished */
image_section_t section[IMAGE_MAX_SECTIONS];
/* we can't determine the number of sections that we'll have to create ahead of time,
* so we locally hold them until parsing is finished */
image_section_t section[IMAGE_MAX_SECTIONS];
section[image->num_sections].base_address = 0x0;
section[image->num_sections].size = 0x0;
section[image->num_sections].flags = 0;
section[image->num_sections].base_address = 0x0;
section[image->num_sections].size = 0x0;
section[image->num_sections].flags = 0;
/* get record type and record length */
if (sscanf(&lpszLine[bytes_read], "S%1x%2x", &record_type, &count) != 2)
{
return ERROR_IMAGE_FORMAT_ERROR;
}
/* get record type and record length */
if (sscanf(&lpszLine[bytes_read], "S%1x%2x", &record_type, &count) != 2)
{
return ERROR_IMAGE_FORMAT_ERROR;
}
case 1:
/* S1 - 16 bit address data record */
sscanf(&lpszLine[bytes_read], "%4x", &address);
case 1:
/* S1 - 16 bit address data record */
sscanf(&lpszLine[bytes_read], "%4x", &address);
case 2:
/* S2 - 24 bit address data record */
sscanf(&lpszLine[bytes_read], "%6x", &address);
case 2:
/* S2 - 24 bit address data record */
sscanf(&lpszLine[bytes_read], "%6x", &address);
- cal_checksum += (u8)(address >> 16);
- cal_checksum += (u8)(address >> 8);
- cal_checksum += (u8)address;
+ cal_checksum += (uint8_t)(address >> 16);
+ cal_checksum += (uint8_t)(address >> 8);
+ cal_checksum += (uint8_t)address;
case 3:
/* S3 - 32 bit address data record */
sscanf(&lpszLine[bytes_read], "%8x", &address);
case 3:
/* S3 - 32 bit address data record */
sscanf(&lpszLine[bytes_read], "%8x", &address);
- cal_checksum += (u8)(address >> 24);
- cal_checksum += (u8)(address >> 16);
- cal_checksum += (u8)(address >> 8);
- cal_checksum += (u8)address;
+ cal_checksum += (uint8_t)(address >> 24);
+ cal_checksum += (uint8_t)(address >> 16);
+ cal_checksum += (uint8_t)(address >> 8);
+ cal_checksum += (uint8_t)address;
if (full_address != address)
{
/* we encountered a nonconsecutive location, create a new section,
if (full_address != address)
{
/* we encountered a nonconsecutive location, create a new section,
- sscanf(&lpszLine[bytes_read], "%2x", (u32*)&mot->buffer[cooked_bytes]);
- cal_checksum += (u8)mot->buffer[cooked_bytes];
+ unsigned value;
+ sscanf(&lpszLine[bytes_read], "%2x", &value);
+ mot->buffer[cooked_bytes] = (uint8_t)value;
+ cal_checksum += (uint8_t)mot->buffer[cooked_bytes];
else if (record_type == 5)
{
/* S5 is the data count record, we ignore it */
else if (record_type == 5)
{
/* S5 is the data count record, we ignore it */
{
/* S7, S8, S9 - ending records for 32, 24 and 16bit */
image->num_sections++;
{
/* S7, S8, S9 - ending records for 32, 24 and 16bit */
image->num_sections++;
/* copy section information */
image->sections = malloc(sizeof(image_section_t) * image->num_sections);
for (i = 0; i < image->num_sections; i++)
/* copy section information */
image->sections = malloc(sizeof(image_section_t) * image->num_sections);
for (i = 0; i < image->num_sections; i++)
LOG_ERROR("unhandled S19 record type: %i", record_type);
return ERROR_IMAGE_FORMAT_ERROR;
}
LOG_ERROR("unhandled S19 record type: %i", record_type);
return ERROR_IMAGE_FORMAT_ERROR;
}
/* account for checksum, will always be 0xFF */
sscanf(&lpszLine[bytes_read], "%2x", &checksum);
/* account for checksum, will always be 0xFF */
sscanf(&lpszLine[bytes_read], "%2x", &checksum);
LOG_ERROR("premature end of S19 file, no end-of-file record found");
return ERROR_IMAGE_FORMAT_ERROR;
}
LOG_ERROR("premature end of S19 file, no end-of-file record found");
return ERROR_IMAGE_FORMAT_ERROR;
}
if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
return retval;
}
if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
return retval;
}
if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
{
return retval;
}
if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
{
return retval;
}
if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
{
LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
{
LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
return retval;
}
if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
return retval;
}
image->num_sections = 1;
image->sections = malloc(sizeof(image_section_t));
image->sections[0].base_address = 0x0;
image->sections[0].size = 0xffffffff;
image->sections[0].flags = 0;
image->num_sections = 1;
image->sections = malloc(sizeof(image_section_t));
image->sections[0].base_address = 0x0;
image->sections[0].size = 0xffffffff;
image->sections[0].flags = 0;
image_memory->cache = NULL;
image_memory->cache_address = 0x0;
}
else if (image->type == IMAGE_SRECORD)
{
image_mot_t *image_mot;
image_memory->cache = NULL;
image_memory->cache_address = 0x0;
}
else if (image->type == IMAGE_SRECORD)
{
image_mot_t *image_mot;
if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
{
return retval;
}
if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
{
return retval;
}
if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
{
LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
{
LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
-int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
+int image_read_section(image_t *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, uint32_t *size_read)
/* return requested bytes */
if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
{
/* return requested bytes */
if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
{
if (!image_memory->cache
|| (address < image_memory->cache_address)
|| (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
{
if (!image_memory->cache)
image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
if (!image_memory->cache
|| (address < image_memory->cache_address)
|| (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
{
if (!image_memory->cache)
image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
{
if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
{
memcpy(buffer + *size_read,
image_memory->cache + (address - image_memory->cache_address),
(size_in_cache > size) ? size : size_in_cache
);
memcpy(buffer + *size_read,
image_memory->cache + (address - image_memory->cache_address),
(size_in_cache > size) ? size : size_in_cache
);
*size_read += (size_in_cache > size) ? size : size_in_cache;
address += (size_in_cache > size) ? size : size_in_cache;
}
}
else if (image->type == IMAGE_SRECORD)
{
*size_read += (size_in_cache > size) ? size : size_in_cache;
address += (size_in_cache > size) ? size : size_in_cache;
}
}
else if (image->type == IMAGE_SRECORD)
{
-int image_add_section(image_t *image, u32 base, u32 size, int flags, u8 *data)
+int image_add_section(image_t *image, uint32_t base, uint32_t size, int flags, uint8_t *data)
/* only image builder supports adding sections */
if (image->type != IMAGE_BUILDER)
return ERROR_INVALID_ARGUMENTS;
/* only image builder supports adding sections */
if (image->type != IMAGE_BUILDER)
return ERROR_INVALID_ARGUMENTS;
/* see if it's enough to extend the last section,
* adding data to previous sections or merging is not supported */
if (((section->base_address + section->size) == base) && (section->flags == flags))
{
section->private = realloc(section->private, section->size + size);
/* see if it's enough to extend the last section,
* adding data to previous sections or merging is not supported */
if (((section->base_address + section->size) == base) && (section->flags == flags))
{
section->private = realloc(section->private, section->size + size);
/* allocate new section */
image->num_sections++;
image->sections = realloc(image->sections, sizeof(image_section_t) * image->num_sections);
/* allocate new section */
image->num_sections++;
image->sections = realloc(image->sections, sizeof(image_section_t) * image->num_sections);
- section->private = malloc(sizeof(u8) * size);
- memcpy((u8*)section->private, data, size);
-
+ section->private = malloc(sizeof(uint8_t) * size);
+ memcpy((uint8_t*)section->private, data, size);
+
fileio_close(&image_binary->fileio);
}
else if (image->type == IMAGE_IHEX)
{
image_ihex_t *image_ihex = image->type_private;
fileio_close(&image_binary->fileio);
}
else if (image->type == IMAGE_IHEX)
{
image_ihex_t *image_ihex = image->type_private;
-static u32 crc32_table[256] = {0, 0};
-
-int image_calculate_checksum(u8* buffer, u32 nbytes, u32* checksum)
+int image_calculate_checksum(uint8_t* buffer, uint32_t nbytes, uint32_t* checksum)
- u32 crc = 0xffffffff;
-
- if (!crc32_table[1])
+ uint32_t crc = 0xffffffff;
+ LOG_DEBUG("Calculating checksum");
+
+ uint32_t crc32_table[256];
+
+ /* Initialize the CRC table and the decoding table. */
+ int i, j;
+ unsigned int c;
+ for (i = 0; i < 256; i++)
+ {
+ /* as per gdb */
+ for (c = i << 24, j = 8; j > 0; --j)
+ c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
+ crc32_table[i] = c;
+ }
+
+ while (nbytes>0)