X-Git-Url: https://review.openocd.org/gitweb?a=blobdiff_plain;f=src%2Ftarget%2Fimage.c;h=9a9c9553b87f1b66bdb71da7ebf684fb1bdb8a4f;hb=94f5ed90f1832e81803713f4364de586a69247d4;hp=ea9f3d343f005fe0d954ac2c3794e3d4f2aa6896;hpb=f90d8fa45f2d4c9d4b7990f198b232ee55cbb4e1;p=openocd.git diff --git a/src/target/image.c b/src/target/image.c index ea9f3d343f..9a9c9553b8 100644 --- a/src/target/image.c +++ b/src/target/image.c @@ -2,12 +2,15 @@ * Copyright (C) 2007 by Dominic Rath * * Dominic.Rath@gmx.de * * * - * Copyright (C) 2007,2008 Øyvind Harboe * + * Copyright (C) 2007,2008 Øyvind Harboe * * oyvind.harboe@zylin.com * * * * Copyright (C) 2008 by Spencer Oliver * * spen@spen-soft.co.uk * * * + * Copyright (C) 2009 by Franck Hereson * + * franck.hereson@secad.fr * + * * * 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 * @@ -41,10 +44,10 @@ ((elf->endianness == ELFDATA2LSB)? \ le_to_h_u32((uint8_t*)&field):be_to_h_u32((uint8_t*)&field)) -static int autodetect_image_type(image_t *image, char *url) +static int autodetect_image_type(image_t *image, const char *url) { int retval; - fileio_t fileio; + struct fileio fileio; uint32_t read_bytes; uint8_t buffer[9]; @@ -103,7 +106,7 @@ static int autodetect_image_type(image_t *image, char *url) return ERROR_OK; } -static int identify_image_type(image_t *image, char *type_string, char *url) +static int identify_image_type(image_t *image, const char *type_string, const char *url) { if (type_string) { @@ -147,7 +150,7 @@ static int identify_image_type(image_t *image, char *type_string, char *url) static int image_ihex_buffer_complete(image_t *image) { image_ihex_t *ihex = image->type_private; - fileio_t *fileio = &ihex->fileio; + struct fileio *fileio = &ihex->fileio; uint32_t full_address = 0x0; uint32_t cooked_bytes; int i; @@ -196,6 +199,12 @@ static int image_ihex_buffer_complete(image_t *image) if (section[image->num_sections].size != 0) { image->num_sections++; + if (image->num_sections >= IMAGE_MAX_SECTIONS) + { + /* too many sections */ + LOG_ERROR("Too many sections found in IHEX file"); + return ERROR_IMAGE_FORMAT_ERROR; + } section[image->num_sections].size = 0x0; section[image->num_sections].flags = 0; section[image->num_sections].private = &ihex->buffer[cooked_bytes]; @@ -252,6 +261,12 @@ static int image_ihex_buffer_complete(image_t *image) if (section[image->num_sections].size != 0) { image->num_sections++; + if (image->num_sections >= IMAGE_MAX_SECTIONS) + { + /* too many sections */ + LOG_ERROR("Too many sections found in IHEX file"); + return ERROR_IMAGE_FORMAT_ERROR; + } section[image->num_sections].size = 0x0; section[image->num_sections].flags = 0; section[image->num_sections].private = &ihex->buffer[cooked_bytes]; @@ -292,6 +307,12 @@ static int image_ihex_buffer_complete(image_t *image) if (section[image->num_sections].size != 0) { image->num_sections++; + if (image->num_sections >= IMAGE_MAX_SECTIONS) + { + /* too many sections */ + LOG_ERROR("Too many sections found in IHEX file"); + return ERROR_IMAGE_FORMAT_ERROR; + } section[image->num_sections].size = 0x0; section[image->num_sections].flags = 0; section[image->num_sections].private = &ihex->buffer[cooked_bytes]; @@ -481,7 +502,7 @@ static int image_elf_read_section(image_t *image, int section, uint32_t offset, static int image_mot_buffer_complete(image_t *image) { image_mot_t *mot = image->type_private; - fileio_t *fileio = &mot->fileio; + struct fileio *fileio = &mot->fileio; uint32_t full_address = 0x0; uint32_t cooked_bytes; int i; @@ -533,7 +554,7 @@ static int image_mot_buffer_complete(image_t *image) } else if (record_type >= 1 && record_type <= 3) { - switch (record_type ) + switch (record_type) { case 1: /* S1 - 16 bit address data record */ @@ -636,7 +657,7 @@ static int image_mot_buffer_complete(image_t *image) cal_checksum += (uint8_t)checksum; bytes_read += 2; - if (cal_checksum != 0xFF ) + if (cal_checksum != 0xFF) { /* checksum failed */ LOG_ERROR("incorrect record checksum found in S19 file"); @@ -648,7 +669,7 @@ static int image_mot_buffer_complete(image_t *image) return ERROR_IMAGE_FORMAT_ERROR; } -int image_open(image_t *image, char *url, char *type_string) +int image_open(image_t *image, const char *url, const char *type_string) { int retval = ERROR_OK; @@ -852,7 +873,7 @@ int image_read_section(image_t *image, int section, uint32_t offset, uint32_t si 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;