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"
43 void avr_load_firmware(avr_t * avr, elf_firmware_t * firmware)
45 if (firmware->frequency)
46 avr->frequency = firmware->frequency;
48 avr->vcc = firmware->vcc;
50 avr->avcc = firmware->avcc;
52 avr->aref = firmware->aref;
53 #if CONFIG_SIMAVR_TRACE
54 avr->trace_data->codeline = firmware->codeline;
57 avr_loadcode(avr, firmware->flash, firmware->flashsize, firmware->flashbase);
58 avr->codeend = firmware->flashsize + firmware->flashbase - firmware->datasize;
59 if (firmware->eeprom && firmware->eesize) {
60 avr_eeprom_desc_t d = { .ee = firmware->eeprom, .offset = 0, .size = firmware->eesize };
61 avr_ioctl(avr, AVR_IOCTL_EEPROM_SET, &d);
64 avr_set_command_register(avr, firmware->command_register_addr);
65 avr_set_console_register(avr, firmware->console_register_addr);
67 // rest is initialization of the VCD file
69 if (firmware->tracecount == 0)
71 avr->vcd = malloc(sizeof(*avr->vcd));
72 memset(avr->vcd, 0, sizeof(*avr->vcd));
74 firmware->tracename[0] ? firmware->tracename: "gtkwave_trace.vcd",
76 firmware->traceperiod >= 1000 ? firmware->traceperiod : 1000);
78 printf("Creating VCD trace file '%s'\n", avr->vcd->filename);
79 for (int ti = 0; ti < firmware->tracecount; ti++) {
80 if (firmware->trace[ti].mask == 0xff || firmware->trace[ti].mask == 0) {
82 avr_irq_t * all = avr_iomem_getirq(avr, firmware->trace[ti].addr, AVR_IOMEM_IRQ_ALL);
84 AVR_LOG(avr, LOG_ERROR, "ELF: %s: unable to attach trace to address %04x\n",
85 __FUNCTION__, firmware->trace[ti].addr);
87 avr_vcd_add_signal(avr->vcd, all, 8, firmware->trace[ti].name);
91 for (int bi = 0; bi < 8; bi++)
92 if (firmware->trace[ti].mask & (1 << bi))
94 for (int bi = 0; bi < 8; bi++)
95 if (firmware->trace[ti].mask & (1 << bi)) {
96 avr_irq_t * bit = avr_iomem_getirq(avr, firmware->trace[ti].addr, bi);
98 AVR_LOG(avr, LOG_ERROR, "ELF: %s: unable to attach trace to address %04x\n",
99 __FUNCTION__, firmware->trace[ti].addr);
104 avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);
108 sprintf(comp, "%s.%d", firmware->trace[ti].name, bi);
109 avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);
113 // if the firmware has specified a command register, do NOT start the trace here
114 // the firmware probably knows best when to start/stop it
115 if (!firmware->command_register_addr)
116 avr_vcd_start(avr->vcd);
119 static void elf_parse_mmcu_section(elf_firmware_t * firmware, uint8_t * src, uint32_t size)
122 uint8_t tag = *src++;
124 int next = size > 2 + ts ? 2 + ts : size;
125 // printf("elf_parse_mmcu_section %d, %d / %d\n", tag, ts, size);
127 case AVR_MMCU_TAG_FREQUENCY:
128 firmware->frequency =
129 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
131 case AVR_MMCU_TAG_NAME:
132 strcpy(firmware->mmcu, (char*)src);
134 case AVR_MMCU_TAG_VCC:
136 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
138 case AVR_MMCU_TAG_AVCC:
140 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
142 case AVR_MMCU_TAG_AREF:
144 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
146 case AVR_MMCU_TAG_VCD_TRACE: {
147 uint8_t mask = src[0];
148 uint16_t addr = src[1] | (src[2] << 8);
149 char * name = (char*)src + 3;
150 printf("AVR_MMCU_TAG_VCD_TRACE %04x:%02x - %s\n", addr, mask, name);
151 firmware->trace[firmware->tracecount].mask = mask;
152 firmware->trace[firmware->tracecount].addr = addr;
153 strncpy(firmware->trace[firmware->tracecount].name, name,
154 sizeof(firmware->trace[firmware->tracecount].name));
155 firmware->tracecount++;
157 case AVR_MMCU_TAG_VCD_FILENAME: {
158 strcpy(firmware->tracename, (char*)src);
160 case AVR_MMCU_TAG_VCD_PERIOD: {
161 firmware->traceperiod =
162 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
164 case AVR_MMCU_TAG_SIMAVR_COMMAND: {
165 firmware->command_register_addr = src[0] | (src[1] << 8);
167 case AVR_MMCU_TAG_SIMAVR_CONSOLE: {
168 firmware->console_register_addr = src[0] | (src[1] << 8);
172 src += next - 2; // already incremented
176 int elf_read_firmware(const char * file, elf_firmware_t * firmware)
178 Elf32_Ehdr elf_header; /* ELF header */
179 Elf *elf = NULL; /* Our Elf pointer for libelf */
180 int fd; // File Descriptor
182 if ((fd = open(file, O_RDONLY | O_BINARY)) == -1 ||
183 (read(fd, &elf_header, sizeof(elf_header))) < sizeof(elf_header)) {
184 printf("could not read %s\n", file);
190 Elf_Data *data_data = NULL,
192 *data_ee = NULL; /* Data Descriptor */
194 memset(firmware, 0, sizeof(*firmware));
196 //int bitesize = ((avr->flashend+1) >> 1) * sizeof(avr_symbol_t);
197 firmware->codesize = 32768;
198 int bitesize = firmware->codesize * sizeof(avr_symbol_t);
199 firmware->codeline = malloc(bitesize);
200 memset(firmware->codeline,0, bitesize);
203 /* this is actually mandatory !! otherwise elf_begin() fails */
204 if (elf_version(EV_CURRENT) == EV_NONE) {
205 /* library out of date - recover from error */
207 // Iterate through section headers again this time well stop when we find symbols
208 elf = elf_begin(fd, ELF_C_READ, NULL);
209 //printf("Loading elf %s : %p\n", file, elf);
211 Elf_Scn *scn = NULL; /* Section Descriptor */
213 while ((scn = elf_nextscn(elf, scn)) != NULL) {
214 GElf_Shdr shdr; /* Section Header */
215 gelf_getshdr(scn, &shdr);
216 char * name = elf_strptr(elf, elf_header.e_shstrndx, shdr.sh_name);
217 // printf("Walking elf section '%s'\n", name);
219 if (!strcmp(name, ".text"))
220 data_text = elf_getdata(scn, NULL);
221 else if (!strcmp(name, ".data"))
222 data_data = elf_getdata(scn, NULL);
223 else if (!strcmp(name, ".eeprom"))
224 data_ee = elf_getdata(scn, NULL);
225 else if (!strcmp(name, ".bss")) {
226 Elf_Data *s = elf_getdata(scn, NULL);
227 firmware->bsssize = s->d_size;
228 } else if (!strcmp(name, ".mmcu")) {
229 Elf_Data *s = elf_getdata(scn, NULL);
230 elf_parse_mmcu_section(firmware, s->d_buf, s->d_size);
231 //printf("%s: avr_mcu_t size %ld / read %ld\n", __FUNCTION__, sizeof(struct avr_mcu_t), s->d_size);
232 // avr->frequency = f_cpu;
235 // When we find a section header marked SHT_SYMTAB stop and get symbols
236 if (shdr.sh_type == SHT_SYMTAB) {
237 // edata points to our symbol table
238 Elf_Data *edata = elf_getdata(scn, NULL);
240 // how many symbols are there? this number comes from the size of
241 // the section divided by the entry size
242 int symbol_count = shdr.sh_size / shdr.sh_entsize;
244 // loop through to grab all symbols
245 for (int i = 0; i < symbol_count; i++) {
246 GElf_Sym sym; /* Symbol */
247 // libelf grabs the symbol data using gelf_getsym()
248 gelf_getsym(edata, i, &sym);
250 // print out the value and size
251 // printf("%08x %08d ", sym.st_value, sym.st_size);
252 if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL ||
253 ELF32_ST_TYPE(sym.st_info) == STT_FUNC ||
254 ELF32_ST_TYPE(sym.st_info) == STT_OBJECT) {
255 const char * name = elf_strptr(elf, shdr.sh_link, sym.st_name);
258 if (sym.st_value & 0xfff00000) {
262 if (firmware->codeline[sym.st_value >> 1] == NULL) {
263 avr_symbol_t * s = firmware->codeline[sym.st_value >> 1] = malloc(sizeof(avr_symbol_t));
264 s->symbol = strdup(name);
265 s->addr = sym.st_value;
274 avr_symbol_t * last = NULL;
275 for (int i = 0; i < firmware->codesize; i++) {
276 if (!firmware->codeline[i])
277 firmware->codeline[i] = last;
279 last = firmware->codeline[i];
283 firmware->flashsize =
284 (data_text ? data_text->d_size : 0) +
285 (data_data ? data_data->d_size : 0);
286 firmware->flash = malloc(firmware->flashsize);
288 // using unsigned int for output, since there is no AVR with 4GB
290 // hdump("code", data_text->d_buf, data_text->d_size);
291 memcpy(firmware->flash + offset, data_text->d_buf, data_text->d_size);
292 offset += data_text->d_size;
293 printf("Loaded %u .text\n", (unsigned int)data_text->d_size);
296 // hdump("data", data_data->d_buf, data_data->d_size);
297 memcpy(firmware->flash + offset, data_data->d_buf, data_data->d_size);
298 printf("Loaded %u .data\n", (unsigned int)data_data->d_size);
299 offset += data_data->d_size;
300 firmware->datasize = data_data->d_size;
303 // hdump("eeprom", data_ee->d_buf, data_ee->d_size);
304 firmware->eeprom = malloc(data_ee->d_size);
305 memcpy(firmware->eeprom, data_ee->d_buf, data_ee->d_size);
306 printf("Loaded %u .eeprom\n", (unsigned int)data_ee->d_size);
307 firmware->eesize = data_ee->d_size;
309 // hdump("flash", avr->flash, offset);