4 Loads a .elf file, extract the code, the data, the eeprom and
5 the "mcu" specification section, also load usable code symbols
6 to be able to print meaningful trace information.
8 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
10 This file is part of simavr.
12 simavr is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 simavr is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with simavr. If not, see <http://www.gnu.org/licenses/>.
36 #include "sim_vcd_file.h"
37 #include "avr_eeprom.h"
39 void avr_load_firmware(avr_t * avr, elf_firmware_t * firmware)
41 avr->frequency = firmware->frequency;
42 avr->vcc = firmware->vcc;
43 avr->avcc = firmware->avcc;
44 avr->aref = firmware->aref;
45 #if CONFIG_SIMAVR_TRACE
46 avr->codeline = firmware->codeline;
49 avr_loadcode(avr, firmware->flash, firmware->flashsize, firmware->flashbase);
50 avr->codeend = firmware->flashsize + firmware->flashbase - firmware->datasize;
51 if (firmware->eeprom && firmware->eesize) {
52 avr_eeprom_desc_t d = { .ee = firmware->eeprom, .offset = 0, .size = firmware->eesize };
53 avr_ioctl(avr, AVR_IOCTL_EEPROM_SET, &d);
56 avr_set_command_register(avr, firmware->command_register_addr);
57 avr_set_console_register(avr, firmware->console_register_addr);
59 // rest is initialization of the VCD file
61 if (firmware->tracecount == 0)
63 avr->vcd = malloc(sizeof(*avr->vcd));
64 memset(avr->vcd, 0, sizeof(*avr->vcd));
66 firmware->tracename[0] ? firmware->tracename: "gtkwave_trace.vcd",
68 firmware->traceperiod >= 1000 ? firmware->traceperiod : 1000);
70 printf("Creating VCD trace file '%s'\n", avr->vcd->filename);
71 for (int ti = 0; ti < firmware->tracecount; ti++) {
72 if (firmware->trace[ti].mask == 0xff || firmware->trace[ti].mask == 0) {
74 avr_irq_t * all = avr_iomem_getirq(avr, firmware->trace[ti].addr, AVR_IOMEM_IRQ_ALL);
76 printf("%s: unable to attach trace to address %04x\n",
77 __FUNCTION__, firmware->trace[ti].addr);
79 avr_vcd_add_signal(avr->vcd, all, 8, firmware->trace[ti].name);
83 for (int bi = 0; bi < 8; bi++)
84 if (firmware->trace[ti].mask & (1 << bi))
86 for (int bi = 0; bi < 8; bi++)
87 if (firmware->trace[ti].mask & (1 << bi)) {
88 avr_irq_t * bit = avr_iomem_getirq(avr, firmware->trace[ti].addr, bi);
90 printf("%s: unable to attach trace to address %04x\n",
91 __FUNCTION__, firmware->trace[ti].addr);
96 avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);
100 sprintf(comp, "%s.%d", firmware->trace[ti].name, bi);
101 avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);
105 // if the firmware has specified a command register, do NOT start the trace here
106 // the firmware probably knows best when to start/stop it
107 if (!firmware->command_register_addr)
108 avr_vcd_start(avr->vcd);
111 static void elf_parse_mmcu_section(elf_firmware_t * firmware, uint8_t * src, uint32_t size)
114 uint8_t tag = *src++;
116 int next = size > 2 + ts ? 2 + ts : size;
117 // printf("elf_parse_mmcu_section %d, %d / %d\n", tag, ts, size);
119 case AVR_MMCU_TAG_FREQUENCY:
120 firmware->frequency =
121 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
123 case AVR_MMCU_TAG_NAME:
124 strcpy(firmware->mmcu, (char*)src);
126 case AVR_MMCU_TAG_VCC:
128 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
130 case AVR_MMCU_TAG_AVCC:
132 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
134 case AVR_MMCU_TAG_AREF:
136 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
138 case AVR_MMCU_TAG_VCD_TRACE: {
139 uint8_t mask = src[0];
140 uint16_t addr = src[1] | (src[2] << 8);
141 char * name = (char*)src + 3;
142 printf("AVR_MMCU_TAG_VCD_TRACE %04x:%02x - %s\n", addr, mask, name);
143 firmware->trace[firmware->tracecount].mask = mask;
144 firmware->trace[firmware->tracecount].addr = addr;
145 strncpy(firmware->trace[firmware->tracecount].name, name,
146 sizeof(firmware->trace[firmware->tracecount].name));
147 firmware->tracecount++;
149 case AVR_MMCU_TAG_VCD_FILENAME: {
150 strcpy(firmware->tracename, (char*)src);
152 case AVR_MMCU_TAG_VCD_PERIOD: {
153 firmware->traceperiod =
154 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
156 case AVR_MMCU_TAG_SIMAVR_COMMAND: {
157 firmware->command_register_addr = src[0] | (src[1] << 8);
159 case AVR_MMCU_TAG_SIMAVR_CONSOLE: {
160 firmware->console_register_addr = src[0] | (src[1] << 8);
164 src += next - 2; // already incremented
168 int elf_read_firmware(const char * file, elf_firmware_t * firmware)
170 Elf32_Ehdr elf_header; /* ELF header */
171 Elf *elf = NULL; /* Our Elf pointer for libelf */
172 int fd; // File Descriptor
174 if ((fd = open(file, O_RDONLY)) == -1 ||
175 (read(fd, &elf_header, sizeof(elf_header))) < sizeof(elf_header)) {
176 printf("could not read %s\n", file);
182 Elf_Data *data_data = NULL,
184 *data_ee = NULL; /* Data Descriptor */
186 memset(firmware, 0, sizeof(*firmware));
188 //int bitesize = ((avr->flashend+1) >> 1) * sizeof(avr_symbol_t);
189 firmware->codesize = 32768;
190 int bitesize = firmware->codesize * sizeof(avr_symbol_t);
191 firmware->codeline = malloc(bitesize);
192 memset(firmware->codeline,0, bitesize);
195 /* this is actualy mandatory !! otherwise elf_begin() fails */
196 if (elf_version(EV_CURRENT) == EV_NONE) {
197 /* library out of date - recover from error */
199 // Iterate through section headers again this time well stop when we find symbols
200 elf = elf_begin(fd, ELF_C_READ, NULL);
201 //printf("Loading elf %s : %p\n", file, elf);
203 Elf_Scn *scn = NULL; /* Section Descriptor */
205 while ((scn = elf_nextscn(elf, scn)) != NULL) {
206 GElf_Shdr shdr; /* Section Header */
207 gelf_getshdr(scn, &shdr);
208 char * name = elf_strptr(elf, elf_header.e_shstrndx, shdr.sh_name);
209 // printf("Walking elf section '%s'\n", name);
211 if (!strcmp(name, ".text"))
212 data_text = elf_getdata(scn, NULL);
213 else if (!strcmp(name, ".data"))
214 data_data = elf_getdata(scn, NULL);
215 else if (!strcmp(name, ".eeprom"))
216 data_ee = elf_getdata(scn, NULL);
217 else if (!strcmp(name, ".bss")) {
218 Elf_Data *s = elf_getdata(scn, NULL);
219 firmware->bsssize = s->d_size;
220 } else if (!strcmp(name, ".mmcu")) {
221 Elf_Data *s = elf_getdata(scn, NULL);
222 elf_parse_mmcu_section(firmware, s->d_buf, s->d_size);
223 //printf("%s: avr_mcu_t size %ld / read %ld\n", __FUNCTION__, sizeof(struct avr_mcu_t), s->d_size);
224 // avr->frequency = f_cpu;
227 // When we find a section header marked SHT_SYMTAB stop and get symbols
228 if (shdr.sh_type == SHT_SYMTAB) {
229 // edata points to our symbol table
230 Elf_Data *edata = elf_getdata(scn, NULL);
232 // how many symbols are there? this number comes from the size of
233 // the section divided by the entry size
234 int symbol_count = shdr.sh_size / shdr.sh_entsize;
236 // loop through to grab all symbols
237 for (int i = 0; i < symbol_count; i++) {
238 GElf_Sym sym; /* Symbol */
239 // libelf grabs the symbol data using gelf_getsym()
240 gelf_getsym(edata, i, &sym);
242 // print out the value and size
243 // printf("%08x %08d ", sym.st_value, sym.st_size);
244 if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL ||
245 ELF32_ST_TYPE(sym.st_info) == STT_FUNC ||
246 ELF32_ST_TYPE(sym.st_info) == STT_OBJECT) {
247 const char * name = elf_strptr(elf, shdr.sh_link, sym.st_name);
250 if (sym.st_value & 0xfff00000) {
254 if (firmware->codeline[sym.st_value >> 1] == NULL) {
255 avr_symbol_t * s = firmware->codeline[sym.st_value >> 1] = malloc(sizeof(avr_symbol_t));
256 s->symbol = strdup(name);
257 s->addr = sym.st_value;
266 avr_symbol_t * last = NULL;
267 for (int i = 0; i < firmware->codesize; i++) {
268 if (!firmware->codeline[i])
269 firmware->codeline[i] = last;
271 last = firmware->codeline[i];
275 firmware->flashsize =
276 (data_text ? data_text->d_size : 0) +
277 (data_data ? data_data->d_size : 0);
278 firmware->flash = malloc(firmware->flashsize);
280 // hdump("code", data_text->d_buf, data_text->d_size);
281 memcpy(firmware->flash + offset, data_text->d_buf, data_text->d_size);
282 offset += data_text->d_size;
283 printf("Loaded %zu .text\n", data_text->d_size);
286 // hdump("data", data_data->d_buf, data_data->d_size);
287 memcpy(firmware->flash + offset, data_data->d_buf, data_data->d_size);
288 printf("Loaded %zu .data\n", data_data->d_size);
289 offset += data_data->d_size;
290 firmware->datasize = data_data->d_size;
293 // hdump("eeprom", data_ee->d_buf, data_ee->d_size);
294 firmware->eeprom = malloc(data_ee->d_size);
295 memcpy(firmware->eeprom, data_ee->d_buf, data_ee->d_size);
296 printf("Loaded %zu .eeprom\n", data_ee->d_size);
297 firmware->eesize = data_ee->d_size;
299 // hdump("flash", avr->flash, offset);