1 /***************************************************************************
2 * Copyright (C) 2007 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
30 #include "replacements.h"
36 int image_ihex_buffer_complete(image_t *image)
38 image_ihex_t *ihex = image->type_private;
39 fileio_t *fileio = &ihex->fileio;
40 u32 raw_bytes_read, raw_bytes;
42 u32 full_address = image->base_address;
43 char *buffer = malloc(ihex->raw_size);
44 u32 cooked_bytes = 0x0;
46 ihex->raw_size = fileio->size;
47 ihex->buffer = malloc(ihex->raw_size >> 1);
49 if ((retval = fileio_read(fileio, ihex->raw_size, (u8*)buffer, &raw_bytes_read)) != ERROR_OK)
52 ERROR("failed buffering IHEX file, read failed");
53 return ERROR_FILEIO_OPERATION_FAILED;
56 if (raw_bytes_read != ihex->raw_size)
59 ERROR("failed buffering complete IHEX file, only partially read");
60 return ERROR_FILEIO_OPERATION_FAILED;
65 while (raw_bytes < raw_bytes_read)
72 if (sscanf(&buffer[raw_bytes], ":%2x%4x%2x", &count, &address, &record_type) != 3)
74 return ERROR_IMAGE_FORMAT_ERROR;
80 if ((full_address & 0xffff) != address)
83 ERROR("can't handle non-linear IHEX file");
84 return ERROR_IMAGE_FORMAT_ERROR;
89 sscanf(&buffer[raw_bytes], "%2hhx", &ihex->buffer[cooked_bytes]);
95 else if (record_type == 1)
98 image->size = cooked_bytes;
101 else if (record_type == 4)
105 sscanf(&buffer[raw_bytes], "%4hx", &upper_address);
108 if ((full_address >> 16) != upper_address)
111 ERROR("can't handle non-linear IHEX file");
112 return ERROR_IMAGE_FORMAT_ERROR;
115 else if (record_type == 5)
119 sscanf(&buffer[raw_bytes], "%8x", &start_address);
122 image->start_address_set = 1;
123 image->start_address = be_to_h_u32((u8*)&start_address);
128 ERROR("unhandled IHEX record type: %i", record_type);
129 return ERROR_IMAGE_FORMAT_ERROR;
132 sscanf(&buffer[raw_bytes], "%2x", &checksum);
135 /* consume new-line character(s) */
136 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
139 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
144 ERROR("premature end of IHEX file, no end-of-file record found");
145 return ERROR_IMAGE_FORMAT_ERROR;
148 int image_open(image_t *image, void *source, enum fileio_access access)
150 int retval = ERROR_OK;
152 if (image->type == IMAGE_BINARY)
154 image_binary_t *image_binary;
157 image_binary = image->type_private = malloc(sizeof(image_binary_t));
159 if ((retval = fileio_open(&image_binary->fileio, url, access, FILEIO_BINARY)) != ERROR_OK)
161 strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
162 ERROR(image->error_str);
166 if (access == FILEIO_WRITE)
169 image->size = image_binary->fileio.size;
173 else if (image->type == IMAGE_IHEX)
175 image_ihex_t *image_ihex;
178 if (access != FILEIO_READ)
180 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
181 "can't open IHEX file for writing");
182 ERROR(image->error_str);
183 return ERROR_FILEIO_ACCESS_NOT_SUPPORTED;
186 image_ihex = image->type_private = malloc(sizeof(image_ihex_t));
188 if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
190 strncpy(image->error_str, image_ihex->fileio.error_str, IMAGE_MAX_ERROR_STRING);
191 ERROR(image->error_str);
195 image_ihex->position = 0;
196 image_ihex->raw_size = image_ihex->fileio.size;
198 if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
200 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
201 "failed buffering IHEX image, check daemon output for additional information");
202 ERROR(image->error_str);
203 fileio_close(&image_ihex->fileio);
207 else if (image->type == IMAGE_MEMORY)
209 image_memory_t *image_memory;
210 target_t *target = source;
212 image_memory = image->type_private = malloc(sizeof(image_memory_t));
214 image_memory->target = target;
220 int image_read(image_t *image, u32 size, u8 *buffer, u32 *size_read)
224 if (image->type == IMAGE_BINARY)
226 image_binary_t *image_binary = image->type_private;
228 if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
230 strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
234 else if (image->type == IMAGE_IHEX)
236 image_ihex_t *image_ihex = image->type_private;
238 if ((image_ihex->position + size) > image->size)
240 /* don't read past the end of the file */
241 size = (image->size - image_ihex->position);
244 memcpy(buffer, image_ihex->buffer + image_ihex->position, size);
245 image_ihex->position += size;
247 image->error_str[0] = '\0';
251 else if (image->type == IMAGE_MEMORY)
253 /* TODO: handle target memory pseudo image */
259 int image_write(image_t *image, u32 size, u8 *buffer, u32 *size_written)
261 int retval = ERROR_FILEIO_OPERATION_NOT_SUPPORTED;
263 if (image->type == IMAGE_BINARY)
265 image_binary_t *image_binary = image->type_private;
267 if ((retval = fileio_write(&image_binary->fileio, size, buffer, size_written)) != ERROR_OK)
269 strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
273 else if (image->type == IMAGE_IHEX)
275 return ERROR_FILEIO_OPERATION_NOT_SUPPORTED;
277 else if (image->type == IMAGE_MEMORY)
279 /* TODO: handle target memory pseudo image */
282 if (retval != ERROR_OK)
290 int image_close(image_t *image)
292 if (image->type == IMAGE_BINARY)
294 image_binary_t *image_binary = image->type_private;
296 fileio_close(&image_binary->fileio);
298 else if (image->type == IMAGE_IHEX)
300 image_ihex_t *image_ihex = image->type_private;
302 fileio_close(&image_ihex->fileio);
304 if (image_ihex->buffer)
305 free(image_ihex->buffer);
307 else if (image->type == IMAGE_MEMORY)
309 /* do nothing for now */
312 free(image->type_private);
317 int identify_image_type(image_type_t *type, char *type_string)
321 if (!strcmp(type_string, "bin"))
323 *type = IMAGE_BINARY;
325 else if (!strcmp(type_string, "ihex"))
331 return ERROR_IMAGE_TYPE_UNKNOWN;
336 *type = IMAGE_BINARY;