console: Implement debug console register
[simavr] / simavr / sim / sim_elf.c
1 /*
2         sim_elf.c
3
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.
7
8         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
9
10         This file is part of simavr.
11
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.
16
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.
21
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/>.
24  */
25
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <libelf.h>
33 #include <gelf.h>
34
35 #include "sim_elf.h"
36 #include "sim_vcd_file.h"
37 #include "avr_eeprom.h"
38
39 void avr_load_firmware(avr_t * avr, elf_firmware_t * firmware)
40 {
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;
47 #endif
48
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);
54         }
55
56         avr_set_command_register(avr, firmware->command_register_addr);
57         avr_set_console_register(avr, firmware->console_register_addr);
58
59         // rest is initialization of the VCD file
60
61         if (firmware->tracecount == 0)
62                 return;
63         avr->vcd = malloc(sizeof(*avr->vcd));
64         memset(avr->vcd, 0, sizeof(*avr->vcd));
65         avr_vcd_init(avr, 
66                 firmware->tracename[0] ? firmware->tracename: "gtkwave_trace.vcd",
67                 avr->vcd,
68                 firmware->traceperiod >= 1000 ? firmware->traceperiod : 1000);
69         
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) {
73                         // easy one
74                         avr_irq_t * all = avr_iomem_getirq(avr, firmware->trace[ti].addr, AVR_IOMEM_IRQ_ALL);
75                         if (!all) {
76                                 printf("%s: unable to attach trace to address %04x\n",
77                                         __FUNCTION__, firmware->trace[ti].addr);
78                         } else {
79                                 avr_vcd_add_signal(avr->vcd, all, 8, firmware->trace[ti].name);
80                         }
81                 } else {
82                         int count = 0;
83                         for (int bi = 0; bi < 8; bi++)
84                                 if (firmware->trace[ti].mask & (1 << bi))
85                                         count++;
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);
89                                         if (!bit) {
90                                                 printf("%s: unable to attach trace to address %04x\n",
91                                                         __FUNCTION__, firmware->trace[ti].addr);
92                                                 break;
93                                         }
94                                         
95                                         if (count == 1) {
96                                                 avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);
97                                                 break;
98                                         }
99                                         char comp[128];
100                                         sprintf(comp, "%s.%d", firmware->trace[ti].name, bi);
101                                         avr_vcd_add_signal(avr->vcd, bit, 1, firmware->trace[ti].name);                                 
102                                 }
103                 }
104         }
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);
109 }
110
111 static void elf_parse_mmcu_section(elf_firmware_t * firmware, uint8_t * src, uint32_t size)
112 {
113         while (size) {
114                 uint8_t tag = *src++;
115                 uint8_t ts = *src++;
116                 int next = size > 2 + ts ? 2 + ts : size;
117         //      printf("elf_parse_mmcu_section %d, %d / %d\n", tag, ts, size);
118                 switch (tag) {
119                         case AVR_MMCU_TAG_FREQUENCY:
120                                 firmware->frequency =
121                                         src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
122                                 break;
123                         case AVR_MMCU_TAG_NAME:
124                                 strcpy(firmware->mmcu, (char*)src);
125                                 break;          
126                         case AVR_MMCU_TAG_VCC:
127                                 firmware->vcc =
128                                         src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
129                                 break;
130                         case AVR_MMCU_TAG_AVCC:
131                                 firmware->avcc =
132                                         src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
133                                 break;
134                         case AVR_MMCU_TAG_AREF:
135                                 firmware->aref =
136                                         src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
137                                 break;
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++;
148                         }       break;
149                         case AVR_MMCU_TAG_VCD_FILENAME: {
150                                 strcpy(firmware->tracename, (char*)src);
151                         }       break;
152                         case AVR_MMCU_TAG_VCD_PERIOD: {
153                                 firmware->traceperiod =
154                                         src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
155                         }       break;
156                         case AVR_MMCU_TAG_SIMAVR_COMMAND: {
157                                 firmware->command_register_addr = src[0] | (src[1] << 8);
158                         }       break;
159                         case AVR_MMCU_TAG_SIMAVR_CONSOLE: {
160                                 firmware->console_register_addr = src[0] | (src[1] << 8);
161                         }       break;
162                 }
163                 size -= next;
164                 src += next - 2; // already incremented
165         }
166 }
167
168 int elf_read_firmware(const char * file, elf_firmware_t * firmware)
169 {
170         Elf32_Ehdr elf_header;                  /* ELF header */
171         Elf *elf = NULL;                       /* Our Elf pointer for libelf */
172         int fd; // File Descriptor
173
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);
177                 perror(file);
178                 close(fd);
179                 return -1;
180         }
181
182         Elf_Data *data_data = NULL, 
183                 *data_text = NULL,
184                 *data_ee = NULL;                /* Data Descriptor */
185
186         memset(firmware, 0, sizeof(*firmware));
187 #if ELF_SYMBOLS
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);
193 #endif
194
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 */
198         }
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);
202
203         Elf_Scn *scn = NULL;                   /* Section Descriptor */
204
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);
210
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;
225                 }
226 #if ELF_SYMBOLS
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);
231
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;
235
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);
241
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);
248
249                                         // type of symbol
250                                         if (sym.st_value & 0xfff00000) {
251
252                                         } else {
253                                                 // code
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;
258                                                 }
259                                         }
260                                 }
261                         }
262                 }
263 #endif
264         }
265 #if ELF_SYMBOLS
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;
270                 else
271                         last = firmware->codeline[i];
272         }
273 #endif
274         uint32_t offset = 0;
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);
279         if (data_text) {
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);
284         }
285         if (data_data) {
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;
291         }
292         if (data_ee) {
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;
298         }
299 //      hdump("flash", avr->flash, offset);
300         elf_end(elf);
301         close(fd);
302         return 0;
303 }
304