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 ***************************************************************************/
33 #include "replacements.h"
39 /* convert ELF header field to host endianness */
40 #define field16(elf,field)\
41 ((elf->endianness==ELFDATA2LSB)? \
42 le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field))
44 #define field32(elf,field)\
45 ((elf->endianness==ELFDATA2LSB)? \
46 le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field))
48 static int autodetect_image_type(image_t *image, char *url)
55 /* read the first 4 bytes of image */
56 if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
58 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot open image: %s", fileio.error_str);
59 ERROR(image->error_str);
62 if ((retval = fileio_read(&fileio, 9, buffer, &read_bytes)) != ERROR_OK)
64 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image header: %s", fileio.error_str);
65 ERROR(image->error_str);
66 return ERROR_FILEIO_OPERATION_FAILED;
70 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image, only partially read");
71 ERROR(image->error_str);
72 return ERROR_FILEIO_OPERATION_FAILED;
74 fileio_close(&fileio);
76 /* check header against known signatures */
77 if (strncmp((char*)buffer,ELFMAG,SELFMAG)==0)
79 DEBUG("ELF image detected.");
80 image->type = IMAGE_ELF;
82 else if ((buffer[0]==':') /* record start byte */
83 &&(isxdigit(buffer[1]))
84 &&(isxdigit(buffer[2]))
85 &&(isxdigit(buffer[3]))
86 &&(isxdigit(buffer[4]))
87 &&(isxdigit(buffer[5]))
88 &&(isxdigit(buffer[6]))
89 &&(buffer[7]=='0') /* record type : 00 -> 05 */
90 &&(buffer[8]>='0')&&(buffer[8]<'6'))
92 DEBUG("IHEX image detected.");
93 image->type = IMAGE_IHEX;
97 image->type = IMAGE_BINARY;
103 int identify_image_type(image_t *image, char *type_string, char *url)
107 if (!strcmp(type_string, "bin"))
109 image->type = IMAGE_BINARY;
111 else if (!strcmp(type_string, "ihex"))
113 image->type = IMAGE_IHEX;
115 else if (!strcmp(type_string, "elf"))
117 image->type = IMAGE_ELF;
119 else if (!strcmp(type_string, "mem"))
121 image->type = IMAGE_MEMORY;
125 return ERROR_IMAGE_TYPE_UNKNOWN;
130 return autodetect_image_type(image, url);
136 int image_ihex_buffer_complete(image_t *image)
138 image_ihex_t *ihex = image->type_private;
139 fileio_t *fileio = &ihex->fileio;
140 u32 raw_bytes_read, raw_bytes;
142 u32 full_address = 0x0;
143 char *buffer = malloc(fileio->size);
147 /* we can't determine the number of sections that we'll have to create ahead of time,
148 * so we locally hold them until parsing is finished */
149 image_section_t section[IMAGE_MAX_SECTIONS];
151 if ((retval = fileio_read(fileio, fileio->size, (u8*)buffer, &raw_bytes_read)) != ERROR_OK)
154 ERROR("failed buffering IHEX file, read failed");
155 return ERROR_FILEIO_OPERATION_FAILED;
158 if (raw_bytes_read != fileio->size)
161 ERROR("failed buffering complete IHEX file, only partially read");
162 return ERROR_FILEIO_OPERATION_FAILED;
165 ihex->buffer = malloc(fileio->size >> 1);
168 image->num_sections = 0;
169 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
170 section[image->num_sections].base_address = 0x0;
171 section[image->num_sections].size = 0x0;
172 section[image->num_sections].flags = 0;
173 while (raw_bytes < raw_bytes_read)
180 if (sscanf(&buffer[raw_bytes], ":%2x%4x%2x", &count, &address, &record_type) != 3)
182 return ERROR_IMAGE_FORMAT_ERROR;
186 if (record_type == 0) /* Data Record */
188 if ((full_address & 0xffff) != address)
190 /* we encountered a nonconsecutive location, create a new section,
191 * unless the current section has zero size, in which case this specifies
192 * the current section's base address
194 if (section[image->num_sections].size != 0)
196 image->num_sections++;
197 section[image->num_sections].size = 0x0;
198 section[image->num_sections].flags = 0;
199 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
201 section[image->num_sections].base_address =
202 (full_address & 0xffff0000) | address;
203 full_address = (full_address & 0xffff0000) | address;
208 sscanf(&buffer[raw_bytes], "%2hhx", &ihex->buffer[cooked_bytes]);
211 section[image->num_sections].size += 1;
215 else if (record_type == 1) /* End of File Record */
217 /* finish the current section */
218 image->num_sections++;
220 /* copy section information */
221 image->sections = malloc(sizeof(image_section_t) * image->num_sections);
222 for (i = 0; i < image->num_sections; i++)
224 image->sections[i].private = section[i].private;
225 image->sections[i].base_address = section[i].base_address +
226 ((image->base_address_set) ? image->base_address : 0);
227 image->sections[i].size = section[i].size;
228 image->sections[i].flags = section[i].flags;
234 else if (record_type == 4) /* Extended Linear Address Record */
238 sscanf(&buffer[raw_bytes], "%4hx", &upper_address);
241 if ((full_address >> 16) != upper_address)
243 /* we encountered a nonconsecutive location, create a new section,
244 * unless the current section has zero size, in which case this specifies
245 * the current section's base address
247 if (section[image->num_sections].size != 0)
249 image->num_sections++;
250 section[image->num_sections].size = 0x0;
251 section[image->num_sections].flags = 0;
252 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
254 section[image->num_sections].base_address =
255 (full_address & 0xffff) | (upper_address << 16);
256 full_address = (full_address & 0xffff) | (upper_address << 16);
259 else if (record_type == 5) /* Start Linear Address Record */
263 sscanf(&buffer[raw_bytes], "%8x", &start_address);
266 image->start_address_set = 1;
267 image->start_address = be_to_h_u32((u8*)&start_address);
272 ERROR("unhandled IHEX record type: %i", record_type);
273 return ERROR_IMAGE_FORMAT_ERROR;
276 sscanf(&buffer[raw_bytes], "%2x", &checksum);
279 /* consume new-line character(s) */
280 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
283 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
288 ERROR("premature end of IHEX file, no end-of-file record found");
289 return ERROR_IMAGE_FORMAT_ERROR;
292 int image_elf_read_headers(image_t *image)
294 image_elf_t *elf = image->type_private;
299 elf->header = malloc(sizeof(Elf32_Ehdr));
301 if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (u8*)elf->header, &read_bytes)) != ERROR_OK)
303 ERROR("cannot read ELF file header, read failed");
304 return ERROR_FILEIO_OPERATION_FAILED;
306 if (read_bytes != sizeof(Elf32_Ehdr))
308 ERROR("cannot read ELF file header, only partially read");
309 return ERROR_FILEIO_OPERATION_FAILED;
312 if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG)!=0)
314 ERROR("invalid ELF file, bad magic number");
315 return ERROR_IMAGE_FORMAT_ERROR;
317 if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
319 ERROR("invalid ELF file, only 32bits files are supported");
320 return ERROR_IMAGE_FORMAT_ERROR;
324 elf->endianness = elf->header->e_ident[EI_DATA];
325 if ((elf->endianness!=ELFDATA2LSB)
326 &&(elf->endianness!=ELFDATA2MSB))
328 ERROR("invalid ELF file, unknown endianess setting");
329 return ERROR_IMAGE_FORMAT_ERROR;
332 elf->segment_count = field16(elf,elf->header->e_phnum);
333 if (elf->segment_count==0)
335 ERROR("invalid ELF file, no program headers");
336 return ERROR_IMAGE_FORMAT_ERROR;
339 elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
341 if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (u8*)elf->segments, &read_bytes)) != ERROR_OK)
343 ERROR("cannot read ELF segment headers, read failed");
346 if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
348 ERROR("cannot read ELF segment headers, only partially read");
349 return ERROR_FILEIO_OPERATION_FAILED;
352 /* count useful segments (loadable) */
353 image->num_sections = 0;
354 for (i=0;i<elf->segment_count;i++)
355 if (field32(elf,elf->segments[i].p_type) == PT_LOAD)
356 image->num_sections++;
357 /* alloc and fill sections array with loadable segments */
358 image->sections = malloc(image->num_sections * sizeof(image_section_t));
359 for (i=0,j=0;i<elf->segment_count;i++)
361 if (field32(elf,elf->segments[i].p_type) == PT_LOAD)
363 image->sections[j].size = field32(elf,elf->segments[i].p_memsz);
364 image->sections[j].base_address = field32(elf,elf->segments[i].p_vaddr);
365 image->sections[j].private = &elf->segments[i];
366 image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
371 image->start_address_set = 1;
372 image->start_address = field32(elf,elf->header->e_entry);
377 int image_elf_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
379 image_elf_t *elf = image->type_private;
380 Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
381 u32 read_size,really_read;
386 DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
388 /* read initialized data in current segment if any */
389 if (offset<field32(elf,segment->p_filesz))
391 /* maximal size present in file for the current segment */
392 read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
393 DEBUG("read elf: size = 0x%x at 0x%x",read_size,
394 field32(elf,segment->p_offset)+offset);
395 /* read initialized area of the segment */
396 if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
398 ERROR("cannot find ELF segment content, seek failed");
401 if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
403 ERROR("cannot read ELF segment content, read failed");
409 *size_read += read_size;
410 /* need more data ? */
414 /* if there is remaining zeroed area in current segment */
415 if (offset<field32(elf,segment->p_memsz))
417 /* fill zeroed part (BSS) of the segment */
418 read_size = MIN(size, field32(elf,segment->p_memsz)-offset);
419 DEBUG("zero fill: size = 0x%x",read_size);
420 memset(buffer,0,read_size);
421 *size_read += read_size;
427 int image_open(image_t *image, char *url, char *type_string)
429 int retval = ERROR_OK;
431 if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
436 if (image->type == IMAGE_BINARY)
438 image_binary_t *image_binary;
440 image_binary = image->type_private = malloc(sizeof(image_binary_t));
442 if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
444 strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
445 ERROR(image->error_str);
449 image->num_sections = 1;
450 image->sections = malloc(sizeof(image_section_t));
451 image->sections[0].base_address = 0x0;
452 image->sections[0].size = image_binary->fileio.size;
453 image->sections[0].flags = 0;
455 if (image->base_address_set == 1)
456 image->sections[0].base_address = image->base_address;
460 else if (image->type == IMAGE_IHEX)
462 image_ihex_t *image_ihex;
464 image_ihex = image->type_private = malloc(sizeof(image_ihex_t));
466 if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
468 strncpy(image->error_str, image_ihex->fileio.error_str, IMAGE_MAX_ERROR_STRING);
469 ERROR(image->error_str);
473 if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
475 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
476 "failed buffering IHEX image, check daemon output for additional information");
477 ERROR(image->error_str);
478 fileio_close(&image_ihex->fileio);
482 else if (image->type == IMAGE_ELF)
484 image_elf_t *image_elf;
486 image_elf = image->type_private = malloc(sizeof(image_elf_t));
488 if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
490 strncpy(image->error_str, image_elf->fileio.error_str, IMAGE_MAX_ERROR_STRING);
491 ERROR(image->error_str);
495 if ((retval = image_elf_read_headers(image)) != ERROR_OK)
497 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
498 "failed to read ELF headers, check daemon output for additional information");
499 ERROR(image->error_str);
500 fileio_close(&image_elf->fileio);
504 else if (image->type == IMAGE_MEMORY)
506 image_memory_t *image_memory;
508 image->num_sections = 1;
509 image->sections = malloc(sizeof(image_section_t));
510 image->sections[0].base_address = 0x0;
511 image->sections[0].size = 0xffffffff;
512 image->sections[0].flags = 0;
514 image_memory = image->type_private = malloc(sizeof(image_memory_t));
516 image_memory->target = get_target_by_num(strtoul(url, NULL, 0));;
517 image_memory->cache = NULL;
518 image_memory->cache_address = 0x0;
524 int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
528 if (image->type == IMAGE_BINARY)
530 image_binary_t *image_binary = image->type_private;
532 /* only one section in a plain binary */
534 return ERROR_INVALID_ARGUMENTS;
536 if ((offset > image->sections[0].size) || (offset + size > image->sections[0].size))
537 return ERROR_INVALID_ARGUMENTS;
540 if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
542 strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
546 /* return requested bytes */
547 if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
549 strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
553 else if (image->type == IMAGE_IHEX)
555 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
557 image->error_str[0] = '\0';
561 else if (image->type == IMAGE_ELF)
563 return image_elf_read_section(image, section, offset, size, buffer, size_read);
565 else if (image->type == IMAGE_MEMORY)
567 image_memory_t *image_memory = image->type_private;
568 u32 address = image->sections[section].base_address + offset;
572 while ((size - *size_read) > 0)
576 if (!image_memory->cache
577 || (address < image_memory->cache_address)
578 || (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
580 if (!image_memory->cache)
581 image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
583 if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
584 IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
586 free(image_memory->cache);
587 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
589 image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
592 size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
594 memcpy(buffer + *size_read,
595 image_memory->cache + (address - image_memory->cache_address),
596 (size_in_cache > size) ? size : size_in_cache
599 *size_read += (size_in_cache > size) ? size : size_in_cache;
600 address += (size_in_cache > size) ? size : size_in_cache;
607 int image_close(image_t *image)
609 if (image->type == IMAGE_BINARY)
611 image_binary_t *image_binary = image->type_private;
613 fileio_close(&image_binary->fileio);
615 else if (image->type == IMAGE_IHEX)
617 image_ihex_t *image_ihex = image->type_private;
619 fileio_close(&image_ihex->fileio);
621 if (image_ihex->buffer)
622 free(image_ihex->buffer);
624 else if (image->type == IMAGE_ELF)
626 image_elf_t *image_elf = image->type_private;
628 fileio_close(&image_elf->fileio);
630 if (image_elf->header)
631 free(image_elf->header);
633 if (image_elf->segments)
634 free(image_elf->segments);
636 else if (image->type == IMAGE_MEMORY)
638 image_memory_t *image_memory = image->type_private;
640 if (image_memory->cache)
641 free(image_memory->cache);
644 if (image->type_private)
645 free(image->type_private);
648 free(image->sections);