- add support for ELF images (thanks to Vincent Palatin for this patch)
[fw/openocd] / src / target / image.h
1 /***************************************************************************
2  *   Copyright (C) 2007 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20 #ifndef IMAGE_H
21 #define IMAGE_H
22
23 #include <elf.h>
24 #include "fileio.h"
25 #include "target.h"
26
27 #define IMAGE_MAX_ERROR_STRING          (128)
28 #define IMAGE_MAX_SECTIONS                      (128)
29
30 typedef enum image_type
31 {
32     IMAGE_BINARY,       /* plain binary */
33     IMAGE_IHEX,         /* intel hex-record format */
34     IMAGE_MEMORY,       /* target-memory pseudo-image */
35     IMAGE_ELF,          /* ELF binary */
36 /*
37  * Possible future enhancements:
38  * IMAGE_SRECORD,
39  */
40 } image_type_t;
41
42 typedef struct image_section_s
43 {
44         u32 base_address;
45         u32 size;
46         int flags;
47         void *private;          /* private data */
48 } image_section_t;
49
50 typedef struct image_s
51 {
52         image_type_t type;              /* image type (plain, ihex, ...) */
53         void *type_private;             /* type private data */
54         int num_sections;               /* number of sections contained in the image */
55         image_section_t *sections;      /* array of sections */
56         int base_address_set;   /* whether the image has a base address set (for relocation purposes) */
57         int base_address;               /* base address, if one is set */
58         int start_address_set;  /* whether the image has a start address (entry point) associated */
59         u32 start_address;              /* start address, if one is set */
60         char error_str[IMAGE_MAX_ERROR_STRING];
61 } image_t;
62
63 typedef struct image_binary_s
64 {
65         fileio_t fileio;
66 } image_binary_t;
67
68 typedef struct image_ihex_s
69 {
70         fileio_t fileio;
71         u8 *buffer;
72         u8 **section_pointer;
73 } image_ihex_t;
74
75 typedef struct image_memory_s
76 {
77         target_t *target;
78 } image_memory_t;
79
80 typedef struct fileio_elf_s
81 {
82         fileio_t fileio;
83         Elf32_Ehdr *header;
84         Elf32_Phdr *segments;
85         u32 segment_count;
86         u8 endianness;
87 } image_elf_t;
88
89 extern int image_open(image_t *image, void *source, enum fileio_access access);
90 extern int image_read_section(image_t *image, int section, u32 offset, u32 size, u8 *buffer, u32 *size_read);
91 extern int image_close(image_t *image);
92 extern int identify_image_type(image_type_t *type, char *type_string);
93
94 #define ERROR_IMAGE_FORMAT_ERROR        (-1400)
95 #define ERROR_IMAGE_TYPE_UNKNOWN        (-1401)
96
97 #endif /* IMAGE_H */