-#define field16(elf, field)\
- ((elf->endianness == ELFDATA2LSB)? \
- le_to_h_u16((u8*)&field) : be_to_h_u16((u8*)&field))
+#define field16(elf,field)\
+ ((elf->endianness==ELFDATA2LSB)? \
+ le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field))
-#define field32(elf, field)\
- ((elf->endianness == ELFDATA2LSB)? \
- le_to_h_u32((u8*)&field) : be_to_h_u32((u8*)&field))
+#define field32(elf,field)\
+ ((elf->endianness==ELFDATA2LSB)? \
+ le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field))
/* read the first 4 bytes of image */
if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
/* 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 retval;
}
if ((retval = fileio_read(&fileio, 9, buffer, &read_bytes)) != 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;
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;
ERROR("unhandled IHEX record type: %i", record_type);
return ERROR_IMAGE_FORMAT_ERROR;
}
ERROR("unhandled IHEX record type: %i", record_type);
return ERROR_IMAGE_FORMAT_ERROR;
}
ERROR("premature end of IHEX file, no end-of-file record found");
return ERROR_IMAGE_FORMAT_ERROR;
}
ERROR("premature end of IHEX file, no end-of-file record found");
return ERROR_IMAGE_FORMAT_ERROR;
}
{
ERROR("invalid ELF file, bad magic number");
return ERROR_IMAGE_FORMAT_ERROR;
}
{
ERROR("invalid ELF file, bad magic number");
return ERROR_IMAGE_FORMAT_ERROR;
}
{
ERROR("invalid ELF file, only 32bits files are supported");
return ERROR_IMAGE_FORMAT_ERROR;
{
ERROR("invalid ELF file, only 32bits files are supported");
return ERROR_IMAGE_FORMAT_ERROR;
{
ERROR("invalid ELF file, unknown endianess setting");
return ERROR_IMAGE_FORMAT_ERROR;
}
{
ERROR("invalid ELF file, unknown endianess setting");
return ERROR_IMAGE_FORMAT_ERROR;
}
- elf->segment_count = field16(elf, elf->header->e_phnum);
- if (elf->segment_count == 0)
+ elf->segment_count = field16(elf,elf->header->e_phnum);
+ if (elf->segment_count==0)
{
ERROR("invalid ELF file, no program headers");
return ERROR_IMAGE_FORMAT_ERROR;
}
{
ERROR("invalid ELF file, no program headers");
return ERROR_IMAGE_FORMAT_ERROR;
}
- 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)
{
ERROR("cannot read ELF segment headers, only partially read");
return ERROR_FILEIO_OPERATION_FAILED;
{
ERROR("cannot read ELF segment headers, only partially read");
return ERROR_FILEIO_OPERATION_FAILED;
if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
image->num_sections++;
/* alloc and fill sections array with loadable segments */
image->sections = malloc(image->num_sections * sizeof(image_section_t));
if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
image->num_sections++;
/* alloc and fill sections array with loadable segments */
image->sections = malloc(image->num_sections * sizeof(image_section_t));
{
if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
{
{
if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
{
- image->sections[j].size = field32(elf, elf->segments[i].p_memsz);
- image->sections[j].base_address = field32(elf, elf->segments[i].p_paddr);
+ image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
+ image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
{
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;
-
- DEBUG("load segment %d at 0x%x (sz = 0x%x)", section, offset, size);
+
+ DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
- read_size = MIN(size, field32(elf, segment->p_filesz) - offset);
- DEBUG("read elf: size = 0x%x at 0x%x", read_size,
- field32(elf, segment->p_offset) + offset);
+ read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
+ DEBUG("read elf: size = 0x%x at 0x%x",read_size,
+ field32(elf,segment->p_offset)+offset);
/* 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++)
ERROR("unhandled S19 record type: %i", record_type);
return ERROR_IMAGE_FORMAT_ERROR;
}
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);
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;
ERROR("premature end of S19 file, no end-of-file record found");
return ERROR_IMAGE_FORMAT_ERROR;
}
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)
{
if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
{
if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
{
- snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
- "failed buffering IHEX image, check daemon output for additional information");
- ERROR(image->error_str);
+ 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)
{
if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
{
- snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
- "failed to read ELF headers, check daemon output for additional information");
- ERROR(image->error_str);
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->target = get_target_by_num(strtoul(url, NULL, 0));;
image_memory->cache = NULL;
image_memory->cache_address = 0x0;
image_memory->target = get_target_by_num(strtoul(url, NULL, 0));;
image_memory->cache = NULL;
image_memory->cache_address = 0x0;
if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
{
if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
{
- snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
- "failed buffering S19 image, check daemon output for additional information");
- ERROR(image->error_str);
+ ERROR("failed buffering S19 image, check daemon output for additional information");
}
/* we're done relocating. The two statements below are mainly
* for documenation purposes: stop anyone from empirically
* thinking they should use these values henceforth. */
}
/* we're done relocating. The two statements below are mainly
* for documenation purposes: stop anyone from empirically
* thinking they should use these values henceforth. */
/* 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;