2007-05-29 07:04:20 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* Copyright (C) 2007 by Dominic Rath *
|
|
|
|
* Dominic.Rath@gmx.de *
|
|
|
|
* *
|
2009-07-17 14:54:25 -05:00
|
|
|
* Copyright (C) 2007,2008 Øyvind Harboe *
|
2008-07-25 01:54:17 -05:00
|
|
|
* oyvind.harboe@zylin.com *
|
|
|
|
* *
|
2008-09-20 05:50:53 -05:00
|
|
|
* Copyright (C) 2008 by Spencer Oliver *
|
|
|
|
* spen@spen-soft.co.uk *
|
|
|
|
* *
|
2009-10-28 12:24:55 -05:00
|
|
|
* Copyright (C) 2009 by Franck Hereson *
|
|
|
|
* franck.hereson@secad.fr *
|
|
|
|
* *
|
2007-05-29 07:04:20 -05:00
|
|
|
* 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 *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the *
|
|
|
|
* Free Software Foundation, Inc., *
|
|
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
|
|
|
***************************************************************************/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "image.h"
|
2009-05-10 23:56:37 -05:00
|
|
|
#include "target.h"
|
2009-12-03 06:14:28 -06:00
|
|
|
#include <helper/log.h>
|
2007-05-29 07:04:20 -05:00
|
|
|
|
|
|
|
|
2007-05-31 06:17:54 -05:00
|
|
|
/* convert ELF header field to host endianness */
|
2008-02-29 05:42:37 -06:00
|
|
|
#define field16(elf,field)\
|
2009-06-23 17:42:03 -05:00
|
|
|
((elf->endianness == ELFDATA2LSB)? \
|
2009-06-18 02:04:08 -05:00
|
|
|
le_to_h_u16((uint8_t*)&field):be_to_h_u16((uint8_t*)&field))
|
2007-05-31 06:17:54 -05:00
|
|
|
|
2008-02-29 05:42:37 -06:00
|
|
|
#define field32(elf,field)\
|
2009-06-23 17:42:03 -05:00
|
|
|
((elf->endianness == ELFDATA2LSB)? \
|
2009-06-18 02:04:08 -05:00
|
|
|
le_to_h_u32((uint8_t*)&field):be_to_h_u32((uint8_t*)&field))
|
2007-05-31 06:17:54 -05:00
|
|
|
|
2009-11-13 11:46:21 -06:00
|
|
|
static int autodetect_image_type(struct image *image, const char *url)
|
2007-06-14 04:47:00 -05:00
|
|
|
{
|
|
|
|
int retval;
|
2009-11-13 05:08:29 -06:00
|
|
|
struct fileio fileio;
|
2009-11-13 16:44:53 -06:00
|
|
|
size_t read_bytes;
|
2009-06-18 02:04:08 -05:00
|
|
|
uint8_t buffer[9];
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-14 04:47:00 -05:00
|
|
|
/* read the first 4 bytes of image */
|
|
|
|
if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
2008-04-24 15:40:52 -05:00
|
|
|
retval = fileio_read(&fileio, 9, buffer, &read_bytes);
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-23 17:42:03 -05:00
|
|
|
if (retval == ERROR_OK)
|
2007-06-14 04:47:00 -05:00
|
|
|
{
|
2008-04-24 15:40:52 -05:00
|
|
|
if (read_bytes != 9)
|
|
|
|
{
|
2009-06-23 17:42:54 -05:00
|
|
|
retval = ERROR_FILEIO_OPERATION_FAILED;
|
2008-04-24 15:40:52 -05:00
|
|
|
}
|
2007-06-14 04:47:00 -05:00
|
|
|
}
|
|
|
|
fileio_close(&fileio);
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-23 17:38:12 -05:00
|
|
|
if (retval != ERROR_OK)
|
2008-04-24 15:40:52 -05:00
|
|
|
return retval;
|
2007-06-14 04:47:00 -05:00
|
|
|
|
|
|
|
/* check header against known signatures */
|
2009-06-23 17:42:03 -05:00
|
|
|
if (strncmp((char*)buffer,ELFMAG,SELFMAG) == 0)
|
2007-06-14 04:47:00 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_DEBUG("ELF image detected.");
|
2007-06-14 04:47:00 -05:00
|
|
|
image->type = IMAGE_ELF;
|
|
|
|
}
|
2008-12-13 06:44:39 -06:00
|
|
|
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 */
|
2009-06-23 17:39:47 -05:00
|
|
|
&&(buffer[8]>='0') && (buffer[8]<'6'))
|
2007-06-14 04:47:00 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_DEBUG("IHEX image detected.");
|
2007-06-14 04:47:00 -05:00
|
|
|
image->type = IMAGE_IHEX;
|
|
|
|
}
|
2007-07-31 04:15:59 -05:00
|
|
|
else if ((buffer[0] == 'S') /* record start byte */
|
|
|
|
&&(isxdigit(buffer[1]))
|
|
|
|
&&(isxdigit(buffer[2]))
|
|
|
|
&&(isxdigit(buffer[3]))
|
|
|
|
&&(buffer[1] >= '0') && (buffer[1] < '9'))
|
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_DEBUG("S19 image detected.");
|
2007-07-31 04:15:59 -05:00
|
|
|
image->type = IMAGE_SRECORD;
|
|
|
|
}
|
2007-06-14 04:47:00 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
image->type = IMAGE_BINARY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:46:21 -06:00
|
|
|
static int identify_image_type(struct image *image, const char *type_string, const char *url)
|
2007-06-14 04:47:00 -05:00
|
|
|
{
|
|
|
|
if (type_string)
|
|
|
|
{
|
|
|
|
if (!strcmp(type_string, "bin"))
|
|
|
|
{
|
|
|
|
image->type = IMAGE_BINARY;
|
|
|
|
}
|
|
|
|
else if (!strcmp(type_string, "ihex"))
|
|
|
|
{
|
|
|
|
image->type = IMAGE_IHEX;
|
|
|
|
}
|
|
|
|
else if (!strcmp(type_string, "elf"))
|
|
|
|
{
|
|
|
|
image->type = IMAGE_ELF;
|
|
|
|
}
|
|
|
|
else if (!strcmp(type_string, "mem"))
|
|
|
|
{
|
|
|
|
image->type = IMAGE_MEMORY;
|
|
|
|
}
|
2007-07-31 04:15:59 -05:00
|
|
|
else if (!strcmp(type_string, "s19"))
|
|
|
|
{
|
|
|
|
image->type = IMAGE_SRECORD;
|
|
|
|
}
|
2007-08-10 14:44:06 -05:00
|
|
|
else if (!strcmp(type_string, "build"))
|
|
|
|
{
|
|
|
|
image->type = IMAGE_BUILDER;
|
|
|
|
}
|
2007-06-14 04:47:00 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return ERROR_IMAGE_TYPE_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return autodetect_image_type(image, url);
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-14 04:47:00 -05:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-21 16:25:46 -06:00
|
|
|
static int image_ihex_buffer_complete_inner(struct image *image, char *lpszLine, struct imageection *section)
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
2009-11-13 10:43:09 -06:00
|
|
|
struct image_ihex *ihex = image->type_private;
|
2009-11-13 05:08:29 -06:00
|
|
|
struct fileio *fileio = &ihex->fileio;
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t full_address = 0x0;
|
|
|
|
uint32_t cooked_bytes;
|
2007-05-30 10:47:18 -05:00
|
|
|
int i;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-30 10:47:18 -05:00
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
ihex->buffer = malloc(fileio->size >> 1);
|
|
|
|
cooked_bytes = 0x0;
|
|
|
|
image->num_sections = 0;
|
2007-05-31 06:17:54 -05:00
|
|
|
section[image->num_sections].private = &ihex->buffer[cooked_bytes];
|
2007-05-30 10:47:18 -05:00
|
|
|
section[image->num_sections].base_address = 0x0;
|
|
|
|
section[image->num_sections].size = 0x0;
|
|
|
|
section[image->num_sections].flags = 0;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-10-01 03:31:30 -05:00
|
|
|
while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t count;
|
|
|
|
uint32_t address;
|
|
|
|
uint32_t record_type;
|
|
|
|
uint32_t checksum;
|
2009-06-18 02:04:08 -05:00
|
|
|
uint8_t cal_checksum = 0;
|
2009-11-13 16:44:53 -06:00
|
|
|
size_t bytes_read = 0;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-20 22:16:52 -05:00
|
|
|
if (sscanf(&lpszLine[bytes_read], ":%2" SCNx32 "%4" SCNx32 "%2" SCNx32 , &count, &address, &record_type) != 3)
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 9;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)count;
|
|
|
|
cal_checksum += (uint8_t)(address >> 8);
|
|
|
|
cal_checksum += (uint8_t)address;
|
|
|
|
cal_checksum += (uint8_t)record_type;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-30 10:47:18 -05:00
|
|
|
if (record_type == 0) /* Data Record */
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
|
|
|
if ((full_address & 0xffff) != address)
|
|
|
|
{
|
2007-05-30 10:47:18 -05:00
|
|
|
/* we encountered a nonconsecutive location, create a new section,
|
|
|
|
* unless the current section has zero size, in which case this specifies
|
|
|
|
* the current section's base address
|
|
|
|
*/
|
|
|
|
if (section[image->num_sections].size != 0)
|
|
|
|
{
|
|
|
|
image->num_sections++;
|
2009-10-28 12:24:55 -05:00
|
|
|
if (image->num_sections >= IMAGE_MAX_SECTIONS)
|
|
|
|
{
|
|
|
|
/* too many sections */
|
|
|
|
LOG_ERROR("Too many sections found in IHEX file");
|
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
2007-05-30 10:47:18 -05:00
|
|
|
section[image->num_sections].size = 0x0;
|
|
|
|
section[image->num_sections].flags = 0;
|
2007-05-31 06:17:54 -05:00
|
|
|
section[image->num_sections].private = &ihex->buffer[cooked_bytes];
|
2007-05-30 10:47:18 -05:00
|
|
|
}
|
|
|
|
section[image->num_sections].base_address =
|
|
|
|
(full_address & 0xffff0000) | address;
|
|
|
|
full_address = (full_address & 0xffff0000) | address;
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
while (count-- > 0)
|
|
|
|
{
|
2009-05-06 18:28:38 -05:00
|
|
|
unsigned value;
|
|
|
|
sscanf(&lpszLine[bytes_read], "%2x", &value);
|
2009-06-18 02:04:08 -05:00
|
|
|
ihex->buffer[cooked_bytes] = (uint8_t)value;
|
|
|
|
cal_checksum += (uint8_t)ihex->buffer[cooked_bytes];
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 2;
|
2007-05-29 07:04:20 -05:00
|
|
|
cooked_bytes += 1;
|
2007-05-30 10:47:18 -05:00
|
|
|
section[image->num_sections].size += 1;
|
2007-05-29 07:04:20 -05:00
|
|
|
full_address++;
|
|
|
|
}
|
|
|
|
}
|
2007-05-30 10:47:18 -05:00
|
|
|
else if (record_type == 1) /* End of File Record */
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
2007-05-30 10:47:18 -05:00
|
|
|
/* finish the current section */
|
|
|
|
image->num_sections++;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-30 10:47:18 -05:00
|
|
|
/* copy section information */
|
2009-11-13 11:46:21 -06:00
|
|
|
image->sections = malloc(sizeof(struct imageection) * image->num_sections);
|
2007-05-30 10:47:18 -05:00
|
|
|
for (i = 0; i < image->num_sections; i++)
|
|
|
|
{
|
2007-05-31 06:17:54 -05:00
|
|
|
image->sections[i].private = section[i].private;
|
2007-12-17 14:52:37 -06:00
|
|
|
image->sections[i].base_address = section[i].base_address;
|
2007-05-30 10:47:18 -05:00
|
|
|
image->sections[i].size = section[i].size;
|
|
|
|
image->sections[i].flags = section[i].flags;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2007-10-01 03:31:30 -05:00
|
|
|
else if (record_type == 2) /* Linear Address Record */
|
|
|
|
{
|
2009-06-18 02:07:59 -05:00
|
|
|
uint16_t upper_address;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-10-01 03:31:30 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)(upper_address >> 8);
|
|
|
|
cal_checksum += (uint8_t)upper_address;
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 4;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-10-01 03:31:30 -05:00
|
|
|
if ((full_address >> 4) != upper_address)
|
|
|
|
{
|
|
|
|
/* we encountered a nonconsecutive location, create a new section,
|
|
|
|
* unless the current section has zero size, in which case this specifies
|
|
|
|
* the current section's base address
|
|
|
|
*/
|
|
|
|
if (section[image->num_sections].size != 0)
|
|
|
|
{
|
|
|
|
image->num_sections++;
|
2009-10-28 12:24:55 -05:00
|
|
|
if (image->num_sections >= IMAGE_MAX_SECTIONS)
|
|
|
|
{
|
|
|
|
/* too many sections */
|
|
|
|
LOG_ERROR("Too many sections found in IHEX file");
|
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
2007-10-01 03:31:30 -05:00
|
|
|
section[image->num_sections].size = 0x0;
|
|
|
|
section[image->num_sections].flags = 0;
|
|
|
|
section[image->num_sections].private = &ihex->buffer[cooked_bytes];
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
section[image->num_sections].base_address =
|
2007-10-01 03:31:30 -05:00
|
|
|
(full_address & 0xffff) | (upper_address << 4);
|
|
|
|
full_address = (full_address & 0xffff) | (upper_address << 4);
|
|
|
|
}
|
|
|
|
}
|
2007-12-18 15:29:44 -06:00
|
|
|
else if (record_type == 3) /* Start Segment Address Record */
|
|
|
|
{
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t dummy;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-12-18 15:29:44 -06:00
|
|
|
/* "Start Segment Address Record" will not be supported */
|
|
|
|
/* but we must consume it, and do not create an error. */
|
|
|
|
while (count-- > 0)
|
|
|
|
{
|
2009-06-20 22:16:52 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)dummy;
|
2007-12-18 15:29:44 -06:00
|
|
|
bytes_read += 2;
|
|
|
|
}
|
|
|
|
}
|
2007-05-30 10:47:18 -05:00
|
|
|
else if (record_type == 4) /* Extended Linear Address Record */
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
2009-06-18 02:07:59 -05:00
|
|
|
uint16_t upper_address;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-10-01 03:31:30 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%4hx", &upper_address);
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)(upper_address >> 8);
|
|
|
|
cal_checksum += (uint8_t)upper_address;
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 4;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
if ((full_address >> 16) != upper_address)
|
|
|
|
{
|
2007-05-30 10:47:18 -05:00
|
|
|
/* we encountered a nonconsecutive location, create a new section,
|
|
|
|
* unless the current section has zero size, in which case this specifies
|
|
|
|
* the current section's base address
|
|
|
|
*/
|
|
|
|
if (section[image->num_sections].size != 0)
|
|
|
|
{
|
|
|
|
image->num_sections++;
|
2009-10-28 12:24:55 -05:00
|
|
|
if (image->num_sections >= IMAGE_MAX_SECTIONS)
|
|
|
|
{
|
|
|
|
/* too many sections */
|
|
|
|
LOG_ERROR("Too many sections found in IHEX file");
|
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
2007-05-30 10:47:18 -05:00
|
|
|
section[image->num_sections].size = 0x0;
|
|
|
|
section[image->num_sections].flags = 0;
|
2007-05-31 06:17:54 -05:00
|
|
|
section[image->num_sections].private = &ihex->buffer[cooked_bytes];
|
2007-05-30 10:47:18 -05:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
section[image->num_sections].base_address =
|
2007-05-30 10:47:18 -05:00
|
|
|
(full_address & 0xffff) | (upper_address << 16);
|
|
|
|
full_address = (full_address & 0xffff) | (upper_address << 16);
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
|
|
|
}
|
2007-05-30 10:47:18 -05:00
|
|
|
else if (record_type == 5) /* Start Linear Address Record */
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t start_address;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-20 22:16:52 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%8" SCNx32, &start_address);
|
2009-06-18 02:04:08 -05:00
|
|
|
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;
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 8;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
image->start_address_set = 1;
|
2009-06-18 02:04:08 -05:00
|
|
|
image->start_address = be_to_h_u32((uint8_t*)&start_address);
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-20 22:16:52 -05:00
|
|
|
LOG_ERROR("unhandled IHEX record type: %i", (int)record_type);
|
2007-05-29 07:04:20 -05:00
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-20 22:16:52 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 2;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-18 02:04:08 -05:00
|
|
|
if ((uint8_t)checksum != (uint8_t)(~cal_checksum + 1))
|
2007-07-31 04:15:59 -05:00
|
|
|
{
|
|
|
|
/* checksum failed */
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("incorrect record checksum found in IHEX file");
|
2007-07-31 04:15:59 -05:00
|
|
|
return ERROR_IMAGE_CHECKSUM;
|
|
|
|
}
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("premature end of IHEX file, no end-of-file record found");
|
2007-05-29 07:04:20 -05:00
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-11-21 16:25:46 -06:00
|
|
|
/**
|
|
|
|
* Allocate memory dynamically instead of on the stack. This
|
|
|
|
* is important w/embedded hosts.
|
|
|
|
*/
|
|
|
|
static int image_ihex_buffer_complete(struct image *image)
|
|
|
|
{
|
|
|
|
char *lpszLine = malloc(1023);
|
|
|
|
if (lpszLine == NULL)
|
|
|
|
{
|
|
|
|
LOG_ERROR("Out of memory");
|
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
|
|
|
struct imageection *section = malloc(sizeof(struct imageection) * IMAGE_MAX_SECTIONS);
|
|
|
|
if (section == NULL)
|
|
|
|
{
|
|
|
|
free(lpszLine);
|
|
|
|
LOG_ERROR("Out of memory");
|
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = image_ihex_buffer_complete_inner(image, lpszLine, section);
|
|
|
|
|
|
|
|
free(section);
|
|
|
|
free(lpszLine);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:46:21 -06:00
|
|
|
static int image_elf_read_headers(struct image *image)
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2009-11-13 11:25:36 -06:00
|
|
|
struct image_elf *elf = image->type_private;
|
2009-11-13 16:44:53 -06:00
|
|
|
size_t read_bytes;
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t i,j;
|
2007-05-31 06:17:54 -05:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
elf->header = malloc(sizeof(Elf32_Ehdr));
|
|
|
|
|
2009-06-23 17:35:09 -05:00
|
|
|
if (elf->header == NULL)
|
2008-05-20 05:10:54 -05:00
|
|
|
{
|
|
|
|
LOG_ERROR("insufficient memory to perform operation ");
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
|
|
|
|
2009-06-18 02:04:08 -05:00
|
|
|
if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (uint8_t*)elf->header, &read_bytes)) != ERROR_OK)
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("cannot read ELF file header, read failed");
|
2007-05-31 06:17:54 -05:00
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
|
|
|
if (read_bytes != sizeof(Elf32_Ehdr))
|
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("cannot read ELF file header, only partially read");
|
2007-05-31 06:17:54 -05:00
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
|
|
|
|
2009-06-23 17:38:12 -05:00
|
|
|
if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG) != 0)
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("invalid ELF file, bad magic number");
|
2007-05-31 06:17:54 -05:00
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
2008-02-29 05:42:37 -06:00
|
|
|
if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("invalid ELF file, only 32bits files are supported");
|
2007-05-31 06:17:54 -05:00
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
elf->endianness = elf->header->e_ident[EI_DATA];
|
2009-06-23 17:38:12 -05:00
|
|
|
if ((elf->endianness != ELFDATA2LSB)
|
|
|
|
&&(elf->endianness != ELFDATA2MSB))
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("invalid ELF file, unknown endianess setting");
|
2007-05-31 06:17:54 -05:00
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-02-29 05:42:37 -06:00
|
|
|
elf->segment_count = field16(elf,elf->header->e_phnum);
|
2009-06-23 17:42:03 -05:00
|
|
|
if (elf->segment_count == 0)
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("invalid ELF file, no program headers");
|
2007-05-31 06:17:54 -05:00
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-03-07 10:51:37 -06:00
|
|
|
if ((retval = fileio_seek(&elf->fileio, field32(elf,elf->header->e_phoff))) != ERROR_OK)
|
2008-03-07 10:38:02 -06:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("cannot seek to ELF program header table, read failed");
|
2008-03-07 10:38:02 -06:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-02-29 05:42:37 -06:00
|
|
|
elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
|
2009-06-23 17:35:09 -05:00
|
|
|
if (elf->segments == NULL)
|
2008-05-20 05:10:54 -05:00
|
|
|
{
|
|
|
|
LOG_ERROR("insufficient memory to perform operation ");
|
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
2007-05-31 06:17:54 -05:00
|
|
|
|
2009-06-18 02:04:08 -05:00
|
|
|
if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (uint8_t*)elf->segments, &read_bytes)) != ERROR_OK)
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("cannot read ELF segment headers, read failed");
|
2007-05-31 06:17:54 -05:00
|
|
|
return retval;
|
|
|
|
}
|
2008-02-29 05:42:37 -06:00
|
|
|
if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("cannot read ELF segment headers, only partially read");
|
2007-05-31 06:17:54 -05:00
|
|
|
return ERROR_FILEIO_OPERATION_FAILED;
|
|
|
|
}
|
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
/* count useful segments (loadable), ignore BSS section */
|
2007-05-31 06:17:54 -05:00
|
|
|
image->num_sections = 0;
|
2009-06-23 17:45:47 -05:00
|
|
|
for (i = 0;i < elf->segment_count;i++)
|
2007-07-31 04:15:59 -05:00
|
|
|
if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
|
2007-05-31 06:17:54 -05:00
|
|
|
image->num_sections++;
|
|
|
|
/* alloc and fill sections array with loadable segments */
|
2009-11-13 11:46:21 -06:00
|
|
|
image->sections = malloc(image->num_sections * sizeof(struct imageection));
|
2009-06-23 17:45:47 -05:00
|
|
|
for (i = 0,j = 0;i < elf->segment_count;i++)
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2007-07-31 04:15:59 -05:00
|
|
|
if ((field32(elf, elf->segments[i].p_type) == PT_LOAD) && (field32(elf, elf->segments[i].p_filesz) != 0))
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2008-02-29 05:42:37 -06:00
|
|
|
image->sections[j].size = field32(elf,elf->segments[i].p_filesz);
|
|
|
|
image->sections[j].base_address = field32(elf,elf->segments[i].p_paddr);
|
2007-05-31 06:17:54 -05:00
|
|
|
image->sections[j].private = &elf->segments[i];
|
2008-02-29 05:42:37 -06:00
|
|
|
image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
|
2007-05-31 06:17:54 -05:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-31 06:17:54 -05:00
|
|
|
image->start_address_set = 1;
|
2008-02-29 05:42:37 -06:00
|
|
|
image->start_address = field32(elf,elf->header->e_entry);
|
2007-05-31 06:17:54 -05:00
|
|
|
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 16:44:53 -06:00
|
|
|
static int image_elf_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2009-11-13 11:25:36 -06:00
|
|
|
struct image_elf *elf = image->type_private;
|
2007-05-31 06:17:54 -05:00
|
|
|
Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
|
2009-11-13 16:44:53 -06:00
|
|
|
size_t read_size,really_read;
|
2007-05-31 06:17:54 -05:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
*size_read = 0;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-23 17:42:54 -05:00
|
|
|
LOG_DEBUG("load segment %d at 0x%" PRIx32 " (sz = 0x%" PRIx32 ")",section,offset,size);
|
2007-05-31 06:17:54 -05:00
|
|
|
|
|
|
|
/* read initialized data in current segment if any */
|
2009-06-23 17:45:47 -05:00
|
|
|
if (offset < field32(elf,segment->p_filesz))
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
|
|
|
/* maximal size present in file for the current segment */
|
2008-02-29 05:42:37 -06:00
|
|
|
read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
|
2009-11-13 16:44:53 -06:00
|
|
|
LOG_DEBUG("read elf: size = 0x%zu at 0x%" PRIx32 "", read_size,
|
2009-06-23 17:44:17 -05:00
|
|
|
field32(elf,segment->p_offset) + offset);
|
2007-05-31 06:17:54 -05:00
|
|
|
/* read initialized area of the segment */
|
2009-06-23 17:44:17 -05:00
|
|
|
if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset) + offset)) != ERROR_OK)
|
2007-05-31 06:17:54 -05:00
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("cannot find ELF segment content, seek failed");
|
2007-05-31 06:17:54 -05:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
|
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("cannot read ELF segment content, read failed");
|
2007-05-31 06:17:54 -05:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
buffer += read_size;
|
|
|
|
size -= read_size;
|
|
|
|
offset += read_size;
|
|
|
|
*size_read += read_size;
|
|
|
|
/* need more data ? */
|
|
|
|
if (!size)
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-31 06:17:54 -05:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-21 16:25:46 -06:00
|
|
|
static int image_mot_buffer_complete_inner(struct image *image, char *lpszLine, struct imageection *section)
|
2007-07-31 04:15:59 -05:00
|
|
|
{
|
2009-11-13 10:43:23 -06:00
|
|
|
struct image_mot *mot = image->type_private;
|
2009-11-13 05:08:29 -06:00
|
|
|
struct fileio *fileio = &mot->fileio;
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t full_address = 0x0;
|
|
|
|
uint32_t cooked_bytes;
|
2007-07-31 04:15:59 -05:00
|
|
|
int i;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
/* 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 */
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
mot->buffer = malloc(fileio->size >> 1);
|
|
|
|
cooked_bytes = 0x0;
|
|
|
|
image->num_sections = 0;
|
|
|
|
section[image->num_sections].private = &mot->buffer[cooked_bytes];
|
|
|
|
section[image->num_sections].base_address = 0x0;
|
|
|
|
section[image->num_sections].size = 0x0;
|
|
|
|
section[image->num_sections].flags = 0;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-10-01 03:31:30 -05:00
|
|
|
while (fileio_fgets(fileio, 1023, lpszLine) == ERROR_OK)
|
2007-07-31 04:15:59 -05:00
|
|
|
{
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t count;
|
|
|
|
uint32_t address;
|
|
|
|
uint32_t record_type;
|
|
|
|
uint32_t checksum;
|
2009-06-18 02:04:08 -05:00
|
|
|
uint8_t cal_checksum = 0;
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t bytes_read = 0;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
/* get record type and record length */
|
2009-06-20 22:16:52 -05:00
|
|
|
if (sscanf(&lpszLine[bytes_read], "S%1" SCNx32 "%2" SCNx32 , &record_type, &count) != 2)
|
2007-07-31 04:15:59 -05:00
|
|
|
{
|
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 4;
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)count;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
/* skip checksum byte */
|
|
|
|
count -=1;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
if (record_type == 0)
|
|
|
|
{
|
|
|
|
/* S0 - starting record (optional) */
|
|
|
|
int iValue;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
while (count-- > 0) {
|
2007-10-01 03:31:30 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%2x", &iValue);
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)iValue;
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 2;
|
2007-07-31 04:15:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (record_type >= 1 && record_type <= 3)
|
|
|
|
{
|
2009-06-23 17:47:42 -05:00
|
|
|
switch (record_type)
|
2007-07-31 04:15:59 -05:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
/* S1 - 16 bit address data record */
|
2009-06-20 22:16:52 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%4" SCNx32, &address);
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)(address >> 8);
|
|
|
|
cal_checksum += (uint8_t)address;
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 4;
|
2007-07-31 04:15:59 -05:00
|
|
|
count -=2;
|
|
|
|
break;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
case 2:
|
|
|
|
/* S2 - 24 bit address data record */
|
2009-06-20 22:16:52 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%6" SCNx32 , &address);
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)(address >> 16);
|
|
|
|
cal_checksum += (uint8_t)(address >> 8);
|
|
|
|
cal_checksum += (uint8_t)address;
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 6;
|
2007-07-31 04:15:59 -05:00
|
|
|
count -=3;
|
|
|
|
break;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
case 3:
|
|
|
|
/* S3 - 32 bit address data record */
|
2009-06-20 22:16:52 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%8" SCNx32 , &address);
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)(address >> 24);
|
|
|
|
cal_checksum += (uint8_t)(address >> 16);
|
|
|
|
cal_checksum += (uint8_t)(address >> 8);
|
|
|
|
cal_checksum += (uint8_t)address;
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 8;
|
2007-07-31 04:15:59 -05:00
|
|
|
count -=4;
|
|
|
|
break;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
if (full_address != address)
|
|
|
|
{
|
|
|
|
/* we encountered a nonconsecutive location, create a new section,
|
|
|
|
* unless the current section has zero size, in which case this specifies
|
|
|
|
* the current section's base address
|
|
|
|
*/
|
|
|
|
if (section[image->num_sections].size != 0)
|
|
|
|
{
|
|
|
|
image->num_sections++;
|
|
|
|
section[image->num_sections].size = 0x0;
|
|
|
|
section[image->num_sections].flags = 0;
|
|
|
|
section[image->num_sections].private = &mot->buffer[cooked_bytes];
|
|
|
|
}
|
2007-10-01 03:31:30 -05:00
|
|
|
section[image->num_sections].base_address = address;
|
|
|
|
full_address = address;
|
2007-07-31 04:15:59 -05:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
while (count-- > 0)
|
|
|
|
{
|
2009-05-06 18:28:38 -05:00
|
|
|
unsigned value;
|
|
|
|
sscanf(&lpszLine[bytes_read], "%2x", &value);
|
2009-06-18 02:04:08 -05:00
|
|
|
mot->buffer[cooked_bytes] = (uint8_t)value;
|
|
|
|
cal_checksum += (uint8_t)mot->buffer[cooked_bytes];
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 2;
|
2007-07-31 04:15:59 -05:00
|
|
|
cooked_bytes += 1;
|
|
|
|
section[image->num_sections].size += 1;
|
|
|
|
full_address++;
|
|
|
|
}
|
|
|
|
}
|
2007-12-18 15:29:44 -06:00
|
|
|
else if (record_type == 5)
|
|
|
|
{
|
|
|
|
/* S5 is the data count record, we ignore it */
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t dummy;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-12-18 15:29:44 -06:00
|
|
|
while (count-- > 0)
|
|
|
|
{
|
2009-06-20 22:16:52 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &dummy);
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)dummy;
|
2007-12-18 15:29:44 -06:00
|
|
|
bytes_read += 2;
|
|
|
|
}
|
|
|
|
}
|
2007-07-31 04:15:59 -05:00
|
|
|
else if (record_type >= 7 && record_type <= 9)
|
|
|
|
{
|
|
|
|
/* S7, S8, S9 - ending records for 32, 24 and 16bit */
|
|
|
|
image->num_sections++;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
/* copy section information */
|
2009-11-13 11:46:21 -06:00
|
|
|
image->sections = malloc(sizeof(struct imageection) * image->num_sections);
|
2007-07-31 04:15:59 -05:00
|
|
|
for (i = 0; i < image->num_sections; i++)
|
|
|
|
{
|
|
|
|
image->sections[i].private = section[i].private;
|
2007-12-17 14:52:37 -06:00
|
|
|
image->sections[i].base_address = section[i].base_address;
|
2007-07-31 04:15:59 -05:00
|
|
|
image->sections[i].size = section[i].size;
|
|
|
|
image->sections[i].flags = section[i].flags;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-20 22:16:52 -05:00
|
|
|
LOG_ERROR("unhandled S19 record type: %i", (int)(record_type));
|
2007-07-31 04:15:59 -05:00
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
/* account for checksum, will always be 0xFF */
|
2009-06-20 22:16:52 -05:00
|
|
|
sscanf(&lpszLine[bytes_read], "%2" SCNx32 , &checksum);
|
2009-06-18 02:04:08 -05:00
|
|
|
cal_checksum += (uint8_t)checksum;
|
2007-10-01 03:31:30 -05:00
|
|
|
bytes_read += 2;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-23 17:47:42 -05:00
|
|
|
if (cal_checksum != 0xFF)
|
2007-07-31 04:15:59 -05:00
|
|
|
{
|
|
|
|
/* checksum failed */
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("incorrect record checksum found in S19 file");
|
2007-07-31 04:15:59 -05:00
|
|
|
return ERROR_IMAGE_CHECKSUM;
|
|
|
|
}
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("premature end of S19 file, no end-of-file record found");
|
2007-07-31 04:15:59 -05:00
|
|
|
return ERROR_IMAGE_FORMAT_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-11-21 16:25:46 -06:00
|
|
|
/**
|
|
|
|
* Allocate memory dynamically instead of on the stack. This
|
|
|
|
* is important w/embedded hosts.
|
|
|
|
*/
|
|
|
|
static int image_mot_buffer_complete(struct image *image)
|
|
|
|
{
|
|
|
|
char *lpszLine = malloc(1023);
|
|
|
|
if (lpszLine == NULL)
|
|
|
|
{
|
|
|
|
LOG_ERROR("Out of memory");
|
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
|
|
|
struct imageection *section = malloc(sizeof(struct imageection) * IMAGE_MAX_SECTIONS);
|
|
|
|
if (section == NULL)
|
|
|
|
{
|
|
|
|
free(lpszLine);
|
|
|
|
LOG_ERROR("Out of memory");
|
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = image_mot_buffer_complete_inner(image, lpszLine, section);
|
|
|
|
|
|
|
|
free(section);
|
|
|
|
free(lpszLine);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-13 11:46:21 -06:00
|
|
|
int image_open(struct image *image, const char *url, const char *type_string)
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
|
|
|
int retval = ERROR_OK;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
|
2007-06-14 04:47:00 -05:00
|
|
|
{
|
|
|
|
return retval;
|
2007-06-15 09:10:23 -05:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
if (image->type == IMAGE_BINARY)
|
|
|
|
{
|
2009-11-13 10:43:02 -06:00
|
|
|
struct image_binary *image_binary;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-11-13 10:43:02 -06:00
|
|
|
image_binary = image->type_private = malloc(sizeof(struct image_binary));
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-14 04:47:00 -05:00
|
|
|
if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-30 10:47:18 -05:00
|
|
|
image->num_sections = 1;
|
2009-11-13 11:46:21 -06:00
|
|
|
image->sections = malloc(sizeof(struct imageection));
|
2007-05-30 10:47:18 -05:00
|
|
|
image->sections[0].base_address = 0x0;
|
|
|
|
image->sections[0].size = image_binary->fileio.size;
|
|
|
|
image->sections[0].flags = 0;
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
|
|
|
else if (image->type == IMAGE_IHEX)
|
|
|
|
{
|
2009-11-13 10:43:09 -06:00
|
|
|
struct image_ihex *image_ihex;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-11-13 10:43:09 -06:00
|
|
|
image_ihex = image->type_private = malloc(sizeof(struct image_ihex));
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
|
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("failed buffering IHEX image, check daemon output for additional information");
|
2007-05-29 07:04:20 -05:00
|
|
|
fileio_close(&image_ihex->fileio);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
2007-05-31 06:17:54 -05:00
|
|
|
else if (image->type == IMAGE_ELF)
|
|
|
|
{
|
2009-11-13 11:25:36 -06:00
|
|
|
struct image_elf *image_elf;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-11-13 11:25:36 -06:00
|
|
|
image_elf = image->type_private = malloc(sizeof(struct image_elf));
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-31 06:17:54 -05:00
|
|
|
if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-31 06:17:54 -05:00
|
|
|
if ((retval = image_elf_read_headers(image)) != ERROR_OK)
|
|
|
|
{
|
|
|
|
fileio_close(&image_elf->fileio);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
2007-05-29 07:04:20 -05:00
|
|
|
else if (image->type == IMAGE_MEMORY)
|
|
|
|
{
|
2009-11-13 12:11:13 -06:00
|
|
|
struct target *target = get_target(url);
|
2009-05-17 23:44:28 -05:00
|
|
|
|
|
|
|
if (target == NULL)
|
2008-11-19 01:32:30 -06:00
|
|
|
{
|
2009-05-17 23:44:28 -05:00
|
|
|
LOG_ERROR("target '%s' not defined", url);
|
2008-11-19 01:32:30 -06:00
|
|
|
return ERROR_FAIL;
|
|
|
|
}
|
|
|
|
|
2009-11-13 10:43:14 -06:00
|
|
|
struct image_memory *image_memory;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
image->num_sections = 1;
|
2009-11-13 11:46:21 -06:00
|
|
|
image->sections = malloc(sizeof(struct imageection));
|
2007-06-15 09:10:23 -05:00
|
|
|
image->sections[0].base_address = 0x0;
|
|
|
|
image->sections[0].size = 0xffffffff;
|
|
|
|
image->sections[0].flags = 0;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-11-13 10:43:14 -06:00
|
|
|
image_memory = image->type_private = malloc(sizeof(struct image_memory));
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2008-11-19 01:32:30 -06:00
|
|
|
image_memory->target = target;
|
2007-06-15 09:10:23 -05:00
|
|
|
image_memory->cache = NULL;
|
|
|
|
image_memory->cache_address = 0x0;
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
2007-07-31 04:15:59 -05:00
|
|
|
else if (image->type == IMAGE_SRECORD)
|
|
|
|
{
|
2009-11-13 10:43:23 -06:00
|
|
|
struct image_mot *image_mot;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-11-13 10:43:23 -06:00
|
|
|
image_mot = image->type_private = malloc(sizeof(struct image_mot));
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
if ((retval = fileio_open(&image_mot->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
if ((retval = image_mot_buffer_complete(image)) != ERROR_OK)
|
|
|
|
{
|
2008-03-25 10:45:17 -05:00
|
|
|
LOG_ERROR("failed buffering S19 image, check daemon output for additional information");
|
2007-07-31 04:15:59 -05:00
|
|
|
fileio_close(&image_mot->fileio);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
2007-08-10 14:44:06 -05:00
|
|
|
else if (image->type == IMAGE_BUILDER)
|
|
|
|
{
|
|
|
|
image->num_sections = 0;
|
|
|
|
image->sections = NULL;
|
|
|
|
image->type_private = NULL;
|
|
|
|
}
|
2007-12-17 14:52:37 -06:00
|
|
|
|
|
|
|
if (image->base_address_set)
|
|
|
|
{
|
2007-12-20 10:19:10 -06:00
|
|
|
/* relocate */
|
2007-12-17 14:52:37 -06:00
|
|
|
int section;
|
2009-06-23 17:42:54 -05:00
|
|
|
for (section = 0; section < image->num_sections; section++)
|
2007-12-17 14:52:37 -06:00
|
|
|
{
|
2009-06-23 17:39:18 -05:00
|
|
|
image->sections[section].base_address += image->base_address;
|
2007-12-17 14:52:37 -06:00
|
|
|
}
|
2007-12-20 10:19:10 -06:00
|
|
|
/* we're done relocating. The two statements below are mainly
|
|
|
|
* for documenation purposes: stop anyone from empirically
|
|
|
|
* thinking they should use these values henceforth. */
|
2009-06-23 17:42:54 -05:00
|
|
|
image->base_address = 0;
|
|
|
|
image->base_address_set = 0;
|
2007-12-17 14:52:37 -06:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
return retval;
|
|
|
|
};
|
|
|
|
|
2009-11-13 16:44:53 -06:00
|
|
|
int image_read_section(struct image *image, int section, uint32_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
|
|
|
int retval;
|
2007-08-10 14:44:06 -05:00
|
|
|
|
|
|
|
/* don't read past the end of a section */
|
|
|
|
if (offset + size > image->sections[section].size)
|
|
|
|
{
|
2009-06-20 22:16:52 -05:00
|
|
|
LOG_DEBUG("read past end of section: 0x%8.8" PRIx32 " + 0x%8.8" PRIx32 " > 0x%8.8" PRIx32 "",
|
2007-08-10 14:44:06 -05:00
|
|
|
offset, size, image->sections[section].size);
|
|
|
|
return ERROR_INVALID_ARGUMENTS;
|
|
|
|
}
|
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
if (image->type == IMAGE_BINARY)
|
|
|
|
{
|
2009-11-13 10:43:02 -06:00
|
|
|
struct image_binary *image_binary = image->type_private;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-30 10:47:18 -05:00
|
|
|
/* only one section in a plain binary */
|
|
|
|
if (section != 0)
|
|
|
|
return ERROR_INVALID_ARGUMENTS;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-30 10:47:18 -05:00
|
|
|
/* seek to offset */
|
|
|
|
if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-30 10:47:18 -05:00
|
|
|
/* return requested bytes */
|
2007-05-29 07:04:20 -05:00
|
|
|
if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
|
|
|
|
{
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (image->type == IMAGE_IHEX)
|
|
|
|
{
|
2009-06-18 02:04:08 -05:00
|
|
|
memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
|
2007-05-29 07:04:20 -05:00
|
|
|
*size_read = size;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2007-05-31 06:17:54 -05:00
|
|
|
else if (image->type == IMAGE_ELF)
|
|
|
|
{
|
|
|
|
return image_elf_read_section(image, section, offset, size, buffer, size_read);
|
|
|
|
}
|
2007-05-29 07:04:20 -05:00
|
|
|
else if (image->type == IMAGE_MEMORY)
|
|
|
|
{
|
2009-11-13 10:43:14 -06:00
|
|
|
struct image_memory *image_memory = image->type_private;
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t address = image->sections[section].base_address + offset;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
*size_read = 0;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
while ((size - *size_read) > 0)
|
|
|
|
{
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t size_in_cache;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
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);
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
|
|
|
|
IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
|
|
|
|
{
|
|
|
|
free(image_memory->cache);
|
2007-12-18 15:20:28 -06:00
|
|
|
image_memory->cache = NULL;
|
2007-06-15 09:10:23 -05:00
|
|
|
return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
memcpy(buffer + *size_read,
|
|
|
|
image_memory->cache + (address - image_memory->cache_address),
|
|
|
|
(size_in_cache > size) ? size : size_in_cache
|
2009-06-23 17:47:42 -05:00
|
|
|
);
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
*size_read += (size_in_cache > size) ? size : size_in_cache;
|
|
|
|
address += (size_in_cache > size) ? size : size_in_cache;
|
|
|
|
}
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
2007-07-31 04:15:59 -05:00
|
|
|
else if (image->type == IMAGE_SRECORD)
|
|
|
|
{
|
2009-06-18 02:04:08 -05:00
|
|
|
memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
|
2007-07-31 04:15:59 -05:00
|
|
|
*size_read = size;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2007-08-10 14:44:06 -05:00
|
|
|
else if (image->type == IMAGE_BUILDER)
|
|
|
|
{
|
2009-06-18 02:04:08 -05:00
|
|
|
memcpy(buffer, (uint8_t*)image->sections[section].private + offset, size);
|
2007-08-10 14:44:06 -05:00
|
|
|
*size_read = size;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-08-10 14:44:06 -05:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-08-10 14:44:06 -05:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:46:21 -06:00
|
|
|
int image_add_section(struct image *image, uint32_t base, uint32_t size, int flags, uint8_t *data)
|
2007-08-10 14:44:06 -05:00
|
|
|
{
|
2009-11-13 11:46:21 -06:00
|
|
|
struct imageection *section;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-08-10 14:44:06 -05:00
|
|
|
/* only image builder supports adding sections */
|
|
|
|
if (image->type != IMAGE_BUILDER)
|
|
|
|
return ERROR_INVALID_ARGUMENTS;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-08-14 04:48:54 -05:00
|
|
|
/* see if there's a previous section */
|
|
|
|
if (image->num_sections)
|
2007-08-10 14:44:06 -05:00
|
|
|
{
|
2007-08-14 04:48:54 -05:00
|
|
|
section = &image->sections[image->num_sections - 1];
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-08-14 04:48:54 -05:00
|
|
|
/* 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);
|
2009-06-18 02:04:08 -05:00
|
|
|
memcpy((uint8_t*)section->private + section->size, data, size);
|
2007-08-14 04:48:54 -05:00
|
|
|
section->size += size;
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|
2007-08-10 14:44:06 -05:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-08-10 14:44:06 -05:00
|
|
|
/* allocate new section */
|
|
|
|
image->num_sections++;
|
2009-11-13 11:46:21 -06:00
|
|
|
image->sections = realloc(image->sections, sizeof(struct imageection) * image->num_sections);
|
2007-08-14 04:48:54 -05:00
|
|
|
section = &image->sections[image->num_sections - 1];
|
|
|
|
section->base_address = base;
|
|
|
|
section->size = size;
|
|
|
|
section->flags = flags;
|
2009-06-18 02:04:08 -05:00
|
|
|
section->private = malloc(sizeof(uint8_t) * size);
|
|
|
|
memcpy((uint8_t*)section->private, data, size);
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
return ERROR_OK;
|
|
|
|
}
|
|
|
|
|
2009-11-13 11:46:21 -06:00
|
|
|
void image_close(struct image *image)
|
2007-05-29 07:04:20 -05:00
|
|
|
{
|
|
|
|
if (image->type == IMAGE_BINARY)
|
|
|
|
{
|
2009-11-13 10:43:02 -06:00
|
|
|
struct image_binary *image_binary = image->type_private;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
fileio_close(&image_binary->fileio);
|
|
|
|
}
|
|
|
|
else if (image->type == IMAGE_IHEX)
|
|
|
|
{
|
2009-11-13 10:43:09 -06:00
|
|
|
struct image_ihex *image_ihex = image->type_private;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
fileio_close(&image_ihex->fileio);
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-29 07:04:20 -05:00
|
|
|
if (image_ihex->buffer)
|
2007-12-18 15:20:28 -06:00
|
|
|
{
|
2007-05-29 07:04:20 -05:00
|
|
|
free(image_ihex->buffer);
|
2007-12-18 15:20:28 -06:00
|
|
|
image_ihex->buffer = NULL;
|
|
|
|
}
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
2007-05-31 06:17:54 -05:00
|
|
|
else if (image->type == IMAGE_ELF)
|
|
|
|
{
|
2009-11-13 11:25:36 -06:00
|
|
|
struct image_elf *image_elf = image->type_private;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-31 06:17:54 -05:00
|
|
|
fileio_close(&image_elf->fileio);
|
2008-01-21 13:34:45 -06:00
|
|
|
|
2007-05-31 06:17:54 -05:00
|
|
|
if (image_elf->header)
|
2007-12-18 15:20:28 -06:00
|
|
|
{
|
2007-05-31 06:17:54 -05:00
|
|
|
free(image_elf->header);
|
2007-12-18 15:20:28 -06:00
|
|
|
image_elf->header = NULL;
|
|
|
|
}
|
2007-05-31 06:17:54 -05:00
|
|
|
|
|
|
|
if (image_elf->segments)
|
2007-12-18 15:20:28 -06:00
|
|
|
{
|
2007-05-31 06:17:54 -05:00
|
|
|
free(image_elf->segments);
|
2007-12-18 15:20:28 -06:00
|
|
|
image_elf->segments = NULL;
|
|
|
|
}
|
2007-05-31 06:17:54 -05:00
|
|
|
}
|
2007-05-29 07:04:20 -05:00
|
|
|
else if (image->type == IMAGE_MEMORY)
|
|
|
|
{
|
2009-11-13 10:43:14 -06:00
|
|
|
struct image_memory *image_memory = image->type_private;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-06-15 09:10:23 -05:00
|
|
|
if (image_memory->cache)
|
2007-12-18 15:20:28 -06:00
|
|
|
{
|
2007-06-15 09:10:23 -05:00
|
|
|
free(image_memory->cache);
|
2007-12-18 15:20:28 -06:00
|
|
|
image_memory->cache = NULL;
|
|
|
|
}
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
2007-07-31 04:15:59 -05:00
|
|
|
else if (image->type == IMAGE_SRECORD)
|
|
|
|
{
|
2009-11-13 10:43:23 -06:00
|
|
|
struct image_mot *image_mot = image->type_private;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
fileio_close(&image_mot->fileio);
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-07-31 04:15:59 -05:00
|
|
|
if (image_mot->buffer)
|
2007-12-18 15:20:28 -06:00
|
|
|
{
|
2007-07-31 04:15:59 -05:00
|
|
|
free(image_mot->buffer);
|
2007-12-18 15:20:28 -06:00
|
|
|
image_mot->buffer = NULL;
|
|
|
|
}
|
2007-07-31 04:15:59 -05:00
|
|
|
}
|
2007-08-10 14:44:06 -05:00
|
|
|
else if (image->type == IMAGE_BUILDER)
|
|
|
|
{
|
|
|
|
int i;
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-08-10 14:44:06 -05:00
|
|
|
for (i = 0; i < image->num_sections; i++)
|
|
|
|
{
|
|
|
|
free(image->sections[i].private);
|
2007-12-18 15:20:28 -06:00
|
|
|
image->sections[i].private = NULL;
|
2007-08-10 14:44:06 -05:00
|
|
|
}
|
|
|
|
}
|
2007-05-29 07:04:20 -05:00
|
|
|
|
2007-05-30 10:47:18 -05:00
|
|
|
if (image->type_private)
|
2007-12-18 15:20:28 -06:00
|
|
|
{
|
2007-05-30 10:47:18 -05:00
|
|
|
free(image->type_private);
|
2007-12-18 15:20:28 -06:00
|
|
|
image->type_private = NULL;
|
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2007-05-30 10:47:18 -05:00
|
|
|
if (image->sections)
|
2007-12-18 15:20:28 -06:00
|
|
|
{
|
2007-05-30 10:47:18 -05:00
|
|
|
free(image->sections);
|
2007-12-18 15:20:28 -06:00
|
|
|
image->sections = NULL;
|
|
|
|
}
|
2007-05-29 07:04:20 -05:00
|
|
|
}
|
2007-10-22 03:44:34 -05:00
|
|
|
|
2009-06-18 02:09:35 -05:00
|
|
|
int image_calculate_checksum(uint8_t* buffer, uint32_t nbytes, uint32_t* checksum)
|
2007-10-22 03:44:34 -05:00
|
|
|
{
|
2009-06-18 02:09:35 -05:00
|
|
|
uint32_t crc = 0xffffffff;
|
2008-10-06 06:16:10 -05:00
|
|
|
LOG_DEBUG("Calculating checksum");
|
|
|
|
|
2009-11-21 16:45:36 -06:00
|
|
|
static uint32_t crc32_table[256];
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-11-21 16:45:36 -06:00
|
|
|
static bool first_init = false;
|
|
|
|
if (!first_init)
|
2007-10-22 03:44:34 -05:00
|
|
|
{
|
2009-11-21 16:45:36 -06:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
first_init = true;
|
2007-10-22 03:44:34 -05:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2009-06-23 17:45:47 -05:00
|
|
|
while (nbytes > 0)
|
2007-10-22 03:44:34 -05:00
|
|
|
{
|
2009-06-23 17:42:54 -05:00
|
|
|
int run = nbytes;
|
2009-06-23 17:45:47 -05:00
|
|
|
if (run > 32768)
|
2008-10-03 08:25:33 -05:00
|
|
|
{
|
2009-06-23 17:42:54 -05:00
|
|
|
run = 32768;
|
2008-10-03 08:25:33 -05:00
|
|
|
}
|
2009-06-23 17:37:56 -05:00
|
|
|
nbytes -= run;
|
2008-10-06 06:16:10 -05:00
|
|
|
while (run--)
|
|
|
|
{
|
|
|
|
/* as per gdb */
|
|
|
|
crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buffer++) & 255];
|
|
|
|
}
|
|
|
|
keep_alive();
|
2007-10-22 03:44:34 -05:00
|
|
|
}
|
2008-10-03 08:25:33 -05:00
|
|
|
|
2008-10-06 06:16:10 -05:00
|
|
|
LOG_DEBUG("Calculating checksum done");
|
|
|
|
|
2007-10-22 03:44:34 -05:00
|
|
|
*checksum = crc;
|
|
|
|
return ERROR_OK;
|
|
|
|
}
|