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 if (firmware->frequency)
42 avr->frequency = firmware->frequency;
44 avr->vcc = firmware->vcc;
46 avr->avcc = firmware->avcc;
48 avr->aref = firmware->aref;
49 #if CONFIG_SIMAVR_TRACE
50 avr->trace_data->codeline = firmware->codeline;
53 avr_loadcode(avr, firmware->flash, firmware->flashsize, firmware->flashbase);
54 avr->codeend = firmware->flashsize + firmware->flashbase - firmware->datasize;
55 if (firmware->eeprom && firmware->eesize) {
56 avr_eeprom_desc_t d = { .ee = firmware->eeprom, .offset = 0, .size = firmware->eesize };
57 avr_ioctl(avr, AVR_IOCTL_EEPROM_SET, &d);
60 avr_set_command_register(avr, firmware->command_register_addr);
61 avr_set_console_register(avr, firmware->console_register_addr);
63 // rest is initialization of the VCD file
65 if (firmware->tracecount == 0)
67 avr->vcd = malloc(sizeof(*avr->vcd));
68 memset(avr->vcd, 0, sizeof(*avr->vcd));
70 firmware->tracename[0] ? firmware->tracename: "gtkwave_trace.vcd",
72 firmware->traceperiod >= 1000 ? firmware->traceperiod : 1000);
74 printf("Creating VCD trace file '%s'\n", avr->vcd->filename);
75 for (int ti = 0; ti < firmware->tracecount; ti++) {
76 if (firmware->trace[ti].mask == 0xff || firmware->trace[ti].mask == 0) {
78 avr_irq_t * all = avr_iomem_getirq(avr, firmware->trace[ti].addr, AVR_IOMEM_IRQ_ALL);
80 printf("%s: unable to attach trace to address %04x\n",
81 __FUNCTION__, firmware->trace[ti].addr);
83 avr_vcd_add_signal(avr->vcd, all, 8, firmware->trace[ti].name);
87 for (int bi = 0; bi < 8; bi++)
88 if (firmware->trace[ti].mask & (1 << bi))
90 for (int bi = 0; bi < 8; bi++)
91 if (firmware->trace[ti].mask & (1 << bi)) {
92 avr_irq_t * bit = avr_iomem_getirq(avr, firmware->trace[ti].addr, bi);
94 printf("%s: unable to attach trace to address %04x\n",
95 __FUNCTION__, firmware->trace[ti].addr);
100 avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);
104 sprintf(comp, "%s.%d", firmware->trace[ti].name, bi);
105 avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);
109 // if the firmware has specified a command register, do NOT start the trace here
110 // the firmware probably knows best when to start/stop it
111 if (!firmware->command_register_addr)
112 avr_vcd_start(avr->vcd);
115 static void elf_parse_mmcu_section(elf_firmware_t * firmware, uint8_t * src, uint32_t size)
118 uint8_t tag = *src++;
120 int next = size > 2 + ts ? 2 + ts : size;
121 // printf("elf_parse_mmcu_section %d, %d / %d\n", tag, ts, size);
123 case AVR_MMCU_TAG_FREQUENCY:
124 firmware->frequency =
125 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
127 case AVR_MMCU_TAG_NAME:
128 strcpy(firmware->mmcu, (char*)src);
130 case AVR_MMCU_TAG_VCC:
132 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
134 case AVR_MMCU_TAG_AVCC:
136 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
138 case AVR_MMCU_TAG_AREF:
140 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
142 case AVR_MMCU_TAG_VCD_TRACE: {
143 uint8_t mask = src[0];
144 uint16_t addr = src[1] | (src[2] << 8);
145 char * name = (char*)src + 3;
146 printf("AVR_MMCU_TAG_VCD_TRACE %04x:%02x - %s\n", addr, mask, name);
147 firmware->trace[firmware->tracecount].mask = mask;
148 firmware->trace[firmware->tracecount].addr = addr;
149 strncpy(firmware->trace[firmware->tracecount].name, name,
150 sizeof(firmware->trace[firmware->tracecount].name));
151 firmware->tracecount++;
153 case AVR_MMCU_TAG_VCD_FILENAME: {
154 strcpy(firmware->tracename, (char*)src);
156 case AVR_MMCU_TAG_VCD_PERIOD: {
157 firmware->traceperiod =
158 src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
160 case AVR_MMCU_TAG_SIMAVR_COMMAND: {
161 firmware->command_register_addr = src[0] | (src[1] << 8);
163 case AVR_MMCU_TAG_SIMAVR_CONSOLE: {
164 firmware->console_register_addr = src[0] | (src[1] << 8);
168 src += next - 2; // already incremented
172 int elf_read_firmware(const char * file, elf_firmware_t * firmware)
174 Elf32_Ehdr elf_header; /* ELF header */
175 Elf *elf = NULL; /* Our Elf pointer for libelf */
176 int fd; // File Descriptor
178 if ((fd = open(file, O_RDONLY)) == -1 ||
179 (read(fd, &elf_header, sizeof(elf_header))) < sizeof(elf_header)) {
180 printf("could not read %s\n", file);
186 Elf_Data *data_data = NULL,
188 *data_ee = NULL; /* Data Descriptor */
190 memset(firmware, 0, sizeof(*firmware));
192 //int bitesize = ((avr->flashend+1) >> 1) * sizeof(avr_symbol_t);
193 firmware->codesize = 32768;
194 int bitesize = firmware->codesize * sizeof(avr_symbol_t);
195 firmware->codeline = malloc(bitesize);
196 memset(firmware->codeline,0, bitesize);
199 /* this is actually mandatory !! otherwise elf_begin() fails */
200 if (elf_version(EV_CURRENT) == EV_NONE) {
201 /* library out of date - recover from error */
203 // Iterate through section headers again this time well stop when we find symbols
204 elf = elf_begin(fd, ELF_C_READ, NULL);
205 //printf("Loading elf %s : %p\n", file, elf);
207 Elf_Scn *scn = NULL; /* Section Descriptor */
209 while ((scn = elf_nextscn(elf, scn)) != NULL) {
210 GElf_Shdr shdr; /* Section Header */
211 gelf_getshdr(scn, &shdr);
212 char * name = elf_strptr(elf, elf_header.e_shstrndx, shdr.sh_name);
213 // printf("Walking elf section '%s'\n", name);
215 if (!strcmp(name, ".text"))
216 data_text = elf_getdata(scn, NULL);
217 else if (!strcmp(name, ".data"))
218 data_data = elf_getdata(scn, NULL);
219 else if (!strcmp(name, ".eeprom"))
220 data_ee = elf_getdata(scn, NULL);
221 else if (!strcmp(name, ".bss")) {
222 Elf_Data *s = elf_getdata(scn, NULL);
223 firmware->bsssize = s->d_size;
224 } else if (!strcmp(name, ".mmcu")) {
225 Elf_Data *s = elf_getdata(scn, NULL);
226 elf_parse_mmcu_section(firmware, s->d_buf, s->d_size);
227 //printf("%s: avr_mcu_t size %ld / read %ld\n", __FUNCTION__, sizeof(struct avr_mcu_t), s->d_size);
228 // avr->frequency = f_cpu;
231 // When we find a section header marked SHT_SYMTAB stop and get symbols
232 if (shdr.sh_type == SHT_SYMTAB) {
233 // edata points to our symbol table
234 Elf_Data *edata = elf_getdata(scn, NULL);
236 // how many symbols are there? this number comes from the size of
237 // the section divided by the entry size
238 int symbol_count = shdr.sh_size / shdr.sh_entsize;
240 // loop through to grab all symbols
241 for (int i = 0; i < symbol_count; i++) {
242 GElf_Sym sym; /* Symbol */
243 // libelf grabs the symbol data using gelf_getsym()
244 gelf_getsym(edata, i, &sym);
246 // print out the value and size
247 // printf("%08x %08d ", sym.st_value, sym.st_size);
248 if (ELF32_ST_BIND(sym.st_info) == STB_GLOBAL ||
249 ELF32_ST_TYPE(sym.st_info) == STT_FUNC ||
250 ELF32_ST_TYPE(sym.st_info) == STT_OBJECT) {
251 const char * name = elf_strptr(elf, shdr.sh_link, sym.st_name);
254 if (sym.st_value & 0xfff00000) {
258 if (firmware->codeline[sym.st_value >> 1] == NULL) {
259 avr_symbol_t * s = firmware->codeline[sym.st_value >> 1] = malloc(sizeof(avr_symbol_t));
260 s->symbol = strdup(name);
261 s->addr = sym.st_value;
270 avr_symbol_t * last = NULL;
271 for (int i = 0; i < firmware->codesize; i++) {
272 if (!firmware->codeline[i])
273 firmware->codeline[i] = last;
275 last = firmware->codeline[i];
279 firmware->flashsize =
280 (data_text ? data_text->d_size : 0) +
281 (data_data ? data_data->d_size : 0);
282 firmware->flash = malloc(firmware->flashsize);
284 // hdump("code", data_text->d_buf, data_text->d_size);
285 memcpy(firmware->flash + offset, data_text->d_buf, data_text->d_size);
286 offset += data_text->d_size;
287 printf("Loaded %zu .text\n", data_text->d_size);
290 // hdump("data", data_data->d_buf, data_data->d_size);
291 memcpy(firmware->flash + offset, data_data->d_buf, data_data->d_size);
292 printf("Loaded %zu .data\n", data_data->d_size);
293 offset += data_data->d_size;
294 firmware->datasize = data_data->d_size;
297 // hdump("eeprom", data_ee->d_buf, data_ee->d_size);
298 firmware->eeprom = malloc(data_ee->d_size);
299 memcpy(firmware->eeprom, data_ee->d_buf, data_ee->d_size);
300 printf("Loaded %zu .eeprom\n", data_ee->d_size);
301 firmware->eesize = data_ee->d_size;
303 // hdump("flash", avr->flash, offset);