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 ***************************************************************************/
31 #include "replacements.h"
37 /* convert ELF header field to host endianness */
38 #define field16(elf,field)\
39 ((elf->endianness==ELFDATA2LSB)? \
40 le_to_h_u16((u8*)&field):be_to_h_u16((u8*)&field))
42 #define field32(elf,field)\
43 ((elf->endianness==ELFDATA2LSB)? \
44 le_to_h_u32((u8*)&field):be_to_h_u32((u8*)&field))
46 static int autodetect_image_type(image_t *image, char *url)
53 /* read the first 4 bytes of image */
54 if ((retval = fileio_open(&fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
56 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot open image: %s", fileio.error_str);
57 ERROR(image->error_str);
60 if ((retval = fileio_read(&fileio, 9, buffer, &read_bytes)) != ERROR_OK)
62 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image header: %s", fileio.error_str);
63 ERROR(image->error_str);
64 return ERROR_FILEIO_OPERATION_FAILED;
68 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING, "cannot read image, only partially read");
69 ERROR(image->error_str);
70 return ERROR_FILEIO_OPERATION_FAILED;
72 fileio_close(&fileio);
74 /* check header against known signatures */
75 if (strncmp((char*)buffer,ELFMAG,SELFMAG)==0)
77 DEBUG("ELF image detected.");
78 image->type = IMAGE_ELF;
80 else if ((buffer[0]==':') /* record start byte */
81 &&(isxdigit(buffer[1]))
82 &&(isxdigit(buffer[2]))
83 &&(isxdigit(buffer[3]))
84 &&(isxdigit(buffer[4]))
85 &&(isxdigit(buffer[5]))
86 &&(isxdigit(buffer[6]))
87 &&(buffer[7]=='0') /* record type : 00 -> 05 */
88 &&(buffer[8]>='0')&&(buffer[8]<'6'))
90 DEBUG("IHEX image detected.");
91 image->type = IMAGE_IHEX;
95 image->type = IMAGE_BINARY;
101 int identify_image_type(image_t *image, char *type_string, char *url)
105 if (!strcmp(type_string, "bin"))
107 image->type = IMAGE_BINARY;
109 else if (!strcmp(type_string, "ihex"))
111 image->type = IMAGE_IHEX;
113 else if (!strcmp(type_string, "elf"))
115 image->type = IMAGE_ELF;
117 else if (!strcmp(type_string, "mem"))
119 image->type = IMAGE_MEMORY;
123 return ERROR_IMAGE_TYPE_UNKNOWN;
128 return autodetect_image_type(image, url);
134 int image_ihex_buffer_complete(image_t *image)
136 image_ihex_t *ihex = image->type_private;
137 fileio_t *fileio = &ihex->fileio;
138 u32 raw_bytes_read, raw_bytes;
140 u32 full_address = 0x0;
141 char *buffer = malloc(fileio->size);
145 /* we can't determine the number of sections that we'll have to create ahead of time,
146 * so we locally hold them until parsing is finished */
147 image_section_t section[IMAGE_MAX_SECTIONS];
149 if ((retval = fileio_read(fileio, fileio->size, (u8*)buffer, &raw_bytes_read)) != ERROR_OK)
152 ERROR("failed buffering IHEX file, read failed");
153 return ERROR_FILEIO_OPERATION_FAILED;
156 if (raw_bytes_read != fileio->size)
159 ERROR("failed buffering complete IHEX file, only partially read");
160 return ERROR_FILEIO_OPERATION_FAILED;
163 ihex->buffer = malloc(fileio->size >> 1);
166 image->num_sections = 0;
167 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
168 section[image->num_sections].base_address = 0x0;
169 section[image->num_sections].size = 0x0;
170 section[image->num_sections].flags = 0;
171 while (raw_bytes < raw_bytes_read)
178 if (sscanf(&buffer[raw_bytes], ":%2x%4x%2x", &count, &address, &record_type) != 3)
180 return ERROR_IMAGE_FORMAT_ERROR;
184 if (record_type == 0) /* Data Record */
186 if ((full_address & 0xffff) != address)
188 /* we encountered a nonconsecutive location, create a new section,
189 * unless the current section has zero size, in which case this specifies
190 * the current section's base address
192 if (section[image->num_sections].size != 0)
194 image->num_sections++;
195 section[image->num_sections].size = 0x0;
196 section[image->num_sections].flags = 0;
197 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
199 section[image->num_sections].base_address =
200 (full_address & 0xffff0000) | address;
201 full_address = (full_address & 0xffff0000) | address;
206 sscanf(&buffer[raw_bytes], "%2hhx", &ihex->buffer[cooked_bytes]);
209 section[image->num_sections].size += 1;
213 else if (record_type == 1) /* End of File Record */
215 /* finish the current section */
216 image->num_sections++;
218 /* copy section information */
219 image->sections = malloc(sizeof(image_section_t) * image->num_sections);
220 for (i = 0; i < image->num_sections; i++)
222 image->sections[i].private = section[i].private;
223 image->sections[i].base_address = section[i].base_address +
224 ((image->base_address_set) ? image->base_address : 0);
225 image->sections[i].size = section[i].size;
226 image->sections[i].flags = section[i].flags;
232 else if (record_type == 4) /* Extended Linear Address Record */
236 sscanf(&buffer[raw_bytes], "%4hx", &upper_address);
239 if ((full_address >> 16) != upper_address)
241 /* we encountered a nonconsecutive location, create a new section,
242 * unless the current section has zero size, in which case this specifies
243 * the current section's base address
245 if (section[image->num_sections].size != 0)
247 image->num_sections++;
248 section[image->num_sections].size = 0x0;
249 section[image->num_sections].flags = 0;
250 section[image->num_sections].private = &ihex->buffer[cooked_bytes];
252 section[image->num_sections].base_address =
253 (full_address & 0xffff) | (upper_address << 16);
254 full_address = (full_address & 0xffff) | (upper_address << 16);
257 else if (record_type == 5) /* Start Linear Address Record */
261 sscanf(&buffer[raw_bytes], "%8x", &start_address);
264 image->start_address_set = 1;
265 image->start_address = be_to_h_u32((u8*)&start_address);
270 ERROR("unhandled IHEX record type: %i", record_type);
271 return ERROR_IMAGE_FORMAT_ERROR;
274 sscanf(&buffer[raw_bytes], "%2x", &checksum);
277 /* consume new-line character(s) */
278 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
281 if ((buffer[raw_bytes] == '\n') || (buffer[raw_bytes] == '\r'))
286 ERROR("premature end of IHEX file, no end-of-file record found");
287 return ERROR_IMAGE_FORMAT_ERROR;
290 int image_elf_read_headers(image_t *image)
292 image_elf_t *elf = image->type_private;
297 elf->header = malloc(sizeof(Elf32_Ehdr));
299 if ((retval = fileio_read(&elf->fileio, sizeof(Elf32_Ehdr), (u8*)elf->header, &read_bytes)) != ERROR_OK)
301 ERROR("cannot read ELF file header, read failed");
302 return ERROR_FILEIO_OPERATION_FAILED;
304 if (read_bytes != sizeof(Elf32_Ehdr))
306 ERROR("cannot read ELF file header, only partially read");
307 return ERROR_FILEIO_OPERATION_FAILED;
310 if (strncmp((char*)elf->header->e_ident,ELFMAG,SELFMAG)!=0)
312 ERROR("invalid ELF file, bad magic number");
313 return ERROR_IMAGE_FORMAT_ERROR;
315 if (elf->header->e_ident[EI_CLASS]!=ELFCLASS32)
317 ERROR("invalid ELF file, only 32bits files are supported");
318 return ERROR_IMAGE_FORMAT_ERROR;
322 elf->endianness = elf->header->e_ident[EI_DATA];
323 if ((elf->endianness==ELFDATANONE)
324 ||(elf->endianness>=ELFDATANUM))
326 ERROR("invalid ELF file, unknown endianess setting");
327 return ERROR_IMAGE_FORMAT_ERROR;
330 elf->segment_count = field16(elf,elf->header->e_phnum);
331 if (elf->segment_count==0)
333 ERROR("invalid ELF file, no program headers");
334 return ERROR_IMAGE_FORMAT_ERROR;
337 elf->segments = malloc(elf->segment_count*sizeof(Elf32_Phdr));
339 if ((retval = fileio_read(&elf->fileio, elf->segment_count*sizeof(Elf32_Phdr), (u8*)elf->segments, &read_bytes)) != ERROR_OK)
341 ERROR("cannot read ELF segment headers, read failed");
344 if (read_bytes != elf->segment_count*sizeof(Elf32_Phdr))
346 ERROR("cannot read ELF segment headers, only partially read");
347 return ERROR_FILEIO_OPERATION_FAILED;
350 /* count useful segments (loadable) */
351 image->num_sections = 0;
352 for (i=0;i<elf->segment_count;i++)
353 if (field32(elf,elf->segments[i].p_type) == PT_LOAD)
354 image->num_sections++;
355 /* alloc and fill sections array with loadable segments */
356 image->sections = malloc(image->num_sections * sizeof(image_section_t));
357 for (i=0,j=0;i<elf->segment_count;i++)
359 if (field32(elf,elf->segments[i].p_type) == PT_LOAD)
361 image->sections[j].size = field32(elf,elf->segments[i].p_memsz);
362 image->sections[j].base_address = field32(elf,elf->segments[i].p_vaddr);
363 image->sections[j].private = &elf->segments[i];
364 image->sections[j].flags = field32(elf,elf->segments[i].p_flags);
369 image->start_address_set = 1;
370 image->start_address = field32(elf,elf->header->e_entry);
375 int image_elf_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
377 image_elf_t *elf = image->type_private;
378 Elf32_Phdr *segment = (Elf32_Phdr *)image->sections[section].private;
379 u32 read_size,really_read;
384 DEBUG("load segment %d at 0x%x (sz=0x%x)",section,offset,size);
386 /* read initialized data in current segment if any */
387 if (offset<field32(elf,segment->p_filesz))
389 /* maximal size present in file for the current segment */
390 read_size = MIN(size, field32(elf,segment->p_filesz)-offset);
391 DEBUG("read elf: size = 0x%x at 0x%x",read_size,
392 field32(elf,segment->p_offset)+offset);
393 /* read initialized area of the segment */
394 if ((retval = fileio_seek(&elf->fileio, field32(elf,segment->p_offset)+offset)) != ERROR_OK)
396 ERROR("cannot find ELF segment content, seek failed");
399 if ((retval = fileio_read(&elf->fileio, read_size, buffer, &really_read)) != ERROR_OK)
401 ERROR("cannot read ELF segment content, read failed");
407 *size_read += read_size;
408 /* need more data ? */
412 /* if there is remaining zeroed area in current segment */
413 if (offset<field32(elf,segment->p_memsz))
415 /* fill zeroed part (BSS) of the segment */
416 read_size = MIN(size, field32(elf,segment->p_memsz)-offset);
417 DEBUG("zero fill: size = 0x%x",read_size);
418 memset(buffer,0,read_size);
419 *size_read += read_size;
425 int image_open(image_t *image, char *url, char *type_string)
427 int retval = ERROR_OK;
429 if ((retval = identify_image_type(image, type_string, url)) != ERROR_OK)
434 if (image->type == IMAGE_BINARY)
436 image_binary_t *image_binary;
438 image_binary = image->type_private = malloc(sizeof(image_binary_t));
440 if ((retval = fileio_open(&image_binary->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
442 strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
443 ERROR(image->error_str);
447 image->num_sections = 1;
448 image->sections = malloc(sizeof(image_section_t));
449 image->sections[0].base_address = 0x0;
450 image->sections[0].size = image_binary->fileio.size;
451 image->sections[0].flags = 0;
453 if (image->base_address_set == 1)
454 image->sections[0].base_address = image->base_address;
458 else if (image->type == IMAGE_IHEX)
460 image_ihex_t *image_ihex;
462 image_ihex = image->type_private = malloc(sizeof(image_ihex_t));
464 if ((retval = fileio_open(&image_ihex->fileio, url, FILEIO_READ, FILEIO_TEXT)) != ERROR_OK)
466 strncpy(image->error_str, image_ihex->fileio.error_str, IMAGE_MAX_ERROR_STRING);
467 ERROR(image->error_str);
471 if ((retval = image_ihex_buffer_complete(image)) != ERROR_OK)
473 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
474 "failed buffering IHEX image, check daemon output for additional information");
475 ERROR(image->error_str);
476 fileio_close(&image_ihex->fileio);
480 else if (image->type == IMAGE_ELF)
482 image_elf_t *image_elf;
484 image_elf = image->type_private = malloc(sizeof(image_elf_t));
486 if ((retval = fileio_open(&image_elf->fileio, url, FILEIO_READ, FILEIO_BINARY)) != ERROR_OK)
488 strncpy(image->error_str, image_elf->fileio.error_str, IMAGE_MAX_ERROR_STRING);
489 ERROR(image->error_str);
493 if ((retval = image_elf_read_headers(image)) != ERROR_OK)
495 snprintf(image->error_str, IMAGE_MAX_ERROR_STRING,
496 "failed to read ELF headers, check daemon output for additional information");
497 ERROR(image->error_str);
498 fileio_close(&image_elf->fileio);
502 else if (image->type == IMAGE_MEMORY)
504 image_memory_t *image_memory;
506 image->num_sections = 1;
507 image->sections = malloc(sizeof(image_section_t));
508 image->sections[0].base_address = 0x0;
509 image->sections[0].size = 0xffffffff;
510 image->sections[0].flags = 0;
512 image_memory = image->type_private = malloc(sizeof(image_memory_t));
514 image_memory->target = get_target_by_num(strtoul(url, NULL, 0));;
515 image_memory->cache = NULL;
516 image_memory->cache_address = 0x0;
522 int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read)
526 if (image->type == IMAGE_BINARY)
528 image_binary_t *image_binary = image->type_private;
530 /* only one section in a plain binary */
532 return ERROR_INVALID_ARGUMENTS;
534 if ((offset > image->sections[0].size) || (offset + size > image->sections[0].size))
535 return ERROR_INVALID_ARGUMENTS;
538 if ((retval = fileio_seek(&image_binary->fileio, offset)) != ERROR_OK)
540 strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
544 /* return requested bytes */
545 if ((retval = fileio_read(&image_binary->fileio, size, buffer, size_read)) != ERROR_OK)
547 strncpy(image->error_str, image_binary->fileio.error_str, IMAGE_MAX_ERROR_STRING);
551 else if (image->type == IMAGE_IHEX)
553 memcpy(buffer, (u8*)image->sections[section].private + offset, size);
555 image->error_str[0] = '\0';
559 else if (image->type == IMAGE_ELF)
561 return image_elf_read_section(image, section, offset, size, buffer, size_read);
563 else if (image->type == IMAGE_MEMORY)
565 image_memory_t *image_memory = image->type_private;
566 u32 address = image->sections[section].base_address + offset;
570 while ((size - *size_read) > 0)
574 if (!image_memory->cache
575 || (address < image_memory->cache_address)
576 || (address >= (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE)))
578 if (!image_memory->cache)
579 image_memory->cache = malloc(IMAGE_MEMORY_CACHE_SIZE);
581 if (target_read_buffer(image_memory->target, address & ~(IMAGE_MEMORY_CACHE_SIZE - 1),
582 IMAGE_MEMORY_CACHE_SIZE, image_memory->cache) != ERROR_OK)
584 free(image_memory->cache);
585 return ERROR_IMAGE_TEMPORARILY_UNAVAILABLE;
587 image_memory->cache_address = address & ~(IMAGE_MEMORY_CACHE_SIZE - 1);
590 size_in_cache = (image_memory->cache_address + IMAGE_MEMORY_CACHE_SIZE) - address;
592 memcpy(buffer + *size_read,
593 image_memory->cache + (address - image_memory->cache_address),
594 (size_in_cache > size) ? size : size_in_cache
597 *size_read += (size_in_cache > size) ? size : size_in_cache;
598 address += (size_in_cache > size) ? size : size_in_cache;
605 int image_close(image_t *image)
607 if (image->type == IMAGE_BINARY)
609 image_binary_t *image_binary = image->type_private;
611 fileio_close(&image_binary->fileio);
613 else if (image->type == IMAGE_IHEX)
615 image_ihex_t *image_ihex = image->type_private;
617 fileio_close(&image_ihex->fileio);
619 if (image_ihex->buffer)
620 free(image_ihex->buffer);
622 else if (image->type == IMAGE_ELF)
624 image_elf_t *image_elf = image->type_private;
626 fileio_close(&image_elf->fileio);
628 if (image_elf->header)
629 free(image_elf->header);
631 if (image_elf->segments)
632 free(image_elf->segments);
634 else if (image->type == IMAGE_MEMORY)
636 image_memory_t *image_memory = image->type_private;
638 if (image_memory->cache)
639 free(image_memory->cache);
642 if (image->type_private)
643 free(image->type_private);
646 free(image->sections);