+ * Copyright (C) 2007,2008 Øyvind Harboe *
+ * oyvind.harboe@zylin.com *
+ * *
+ * Copyright (C) 2008 by Spencer Oliver *
+ * spen@spen-soft.co.uk *
+ * *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
/* 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;
}
/* read the first 4 bytes of image */
if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
return retval;
}
- if ((retval = fileio_read(&fileio, 9, buffer, &read_bytes)) != ERROR_OK)
- {
- return ERROR_FILEIO_OPERATION_FAILED;
- }
- if (read_bytes != 9)
+ retval = fileio_read(&fileio, 9, buffer, &read_bytes);
+
+ if (retval==ERROR_OK)
/* check header against known signatures */
if (strncmp((char*)buffer,ELFMAG,SELFMAG)==0)
{
/* check header against known signatures */
if (strncmp((char*)buffer,ELFMAG,SELFMAG)==0)
{
/* 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 += (u8)count;
cal_checksum += (u8)(address >> 8);
cal_checksum += (u8)address;
cal_checksum += (u8)record_type;
/* 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++)
sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
cal_checksum += (u8)(upper_address >> 8);
cal_checksum += (u8)upper_address;
bytes_read += 4;
sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
cal_checksum += (u8)(upper_address >> 8);
cal_checksum += (u8)upper_address;
bytes_read += 4;
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);
}
(full_address & 0xffff) | (upper_address << 4);
full_address = (full_address & 0xffff) | (upper_address << 4);
}
/* "Start Segment Address Record" will not be supported */
/* but we must consume it, and do not create an error. */
while (count-- > 0)
/* "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], "%4hx", &upper_address);
cal_checksum += (u8)(upper_address >> 8);
cal_checksum += (u8)upper_address;
bytes_read += 4;
sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
cal_checksum += (u8)(upper_address >> 8);
cal_checksum += (u8)upper_address;
bytes_read += 4;
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);
}
(full_address & 0xffff) | (upper_address << 16);
full_address = (full_address & 0xffff) | (upper_address << 16);
}
sscanf(&lpszLine[bytes_read], "%8x", &start_address);
cal_checksum += (u8)(start_address >> 24);
cal_checksum += (u8)(start_address >> 16);
cal_checksum += (u8)(start_address >> 8);
cal_checksum += (u8)start_address;
bytes_read += 8;
sscanf(&lpszLine[bytes_read], "%8x", &start_address);
cal_checksum += (u8)(start_address >> 24);
cal_checksum += (u8)(start_address >> 16);
cal_checksum += (u8)(start_address >> 8);
cal_checksum += (u8)start_address;
bytes_read += 8;
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), (u8*)elf->header, &read_bytes)) != ERROR_OK)
{
return ERROR_FILEIO_OPERATION_FAILED;
}
if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG)!=0)
{
return ERROR_FILEIO_OPERATION_FAILED;
}
if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG)!=0)
{
return ERROR_IMAGE_FORMAT_ERROR;
}
elf->segment_count = field16(elf,elf->header->e_phnum);
if (elf->segment_count==0)
{
return ERROR_IMAGE_FORMAT_ERROR;
}
elf->segment_count = field16(elf,elf->header->e_phnum);
if (elf->segment_count==0)
{
return ERROR_IMAGE_FORMAT_ERROR;
}
if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
{
return ERROR_IMAGE_FORMAT_ERROR;
}
if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
{
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), (u8*)elf->segments, &read_bytes)) != ERROR_OK)
{
image->start_address_set = 1;
image->start_address = field32(elf,elf->header->e_entry);
image->start_address_set = 1;
image->start_address = field32(elf,elf->header->e_entry);
-
- DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
+
+ LOG_DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
/* read initialized data in current segment if any */
if (offset<field32(elf,segment->p_filesz))
{
/* maximal size present in file for the current segment */
read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
/* read initialized data in current segment if any */
if (offset<field32(elf,segment->p_filesz))
{
/* maximal size present in file for the current segment */
read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
field32(elf,segment->p_offset)+offset);
/* read initialized area of the segment */
if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
{
field32(elf,segment->p_offset)+offset);
/* read initialized area of the segment */
if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
{
return retval;
}
if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
{
return retval;
}
if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
{
/* 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;
}
while (count-- > 0) {
sscanf(&lpszLine[bytes_read], "%2x", &iValue);
cal_checksum += (u8)iValue;
while (count-- > 0) {
sscanf(&lpszLine[bytes_read], "%2x", &iValue);
cal_checksum += (u8)iValue;
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);
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);
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,
{
/* 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++)
/* account for checksum, will always be 0xFF */
sscanf(&lpszLine[bytes_read], "%2x", &checksum);
cal_checksum += (u8)checksum;
bytes_read += 2;
/* account for checksum, will always be 0xFF */
sscanf(&lpszLine[bytes_read], "%2x", &checksum);
cal_checksum += (u8)checksum;
bytes_read += 2;
return ERROR_IMAGE_FORMAT_ERROR;
}
int image_open(image_t *image, char *url, char *type_string)
{
int retval = ERROR_OK;
return ERROR_IMAGE_FORMAT_ERROR;
}
int image_open(image_t *image, char *url, char *type_string)
{
int retval = ERROR_OK;
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 = 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;
}
offset, size, image->sections[section].size);
return ERROR_INVALID_ARGUMENTS;
}
offset, size, image->sections[section].size);
return ERROR_INVALID_ARGUMENTS;
}
/* 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)
{
{
image_memory_t *image_memory = image->type_private;
u32 address = image->sections[section].base_address + offset;
{
image_memory_t *image_memory = image->type_private;
u32 address = image->sections[section].base_address + offset;
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;
}
*size_read += (size_in_cache > size) ? size : size_in_cache;
address += (size_in_cache > size) ? size : size_in_cache;
}
return ERROR_OK;
}
else if (image->type == IMAGE_BUILDER)
{
memcpy(buffer, (u8*)image->sections[section].private + offset, size);
*size_read = size;
return ERROR_OK;
}
else if (image->type == IMAGE_BUILDER)
{
memcpy(buffer, (u8*)image->sections[section].private + offset, size);
*size_read = size;
return ERROR_OK;
}
int image_add_section(image_t *image, u32 base, u32 size, int flags, u8 *data)
{
image_section_t *section;
return ERROR_OK;
}
int image_add_section(image_t *image, u32 base, u32 size, int flags, u8 *data)
{
image_section_t *section;
/* 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))
/* 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))
/* 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->flags = flags;
section->private = malloc(sizeof(u8) * size);
memcpy((u8*)section->private, data, size);
section->flags = flags;
section->private = malloc(sizeof(u8) * size);
memcpy((u8*)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;
+ LOG_DEBUG("Calculating checksum");
+
+ u32 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)