-
Notifications
You must be signed in to change notification settings - Fork 14
/
esp_bin2elf.py
58 lines (41 loc) · 1.92 KB
/
esp_bin2elf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# esp-bin2elf written by Joel Sandin <[email protected]>
#
# MIT licence
#
# based on the excellent reversing / writeup from Richard Burton:
# http://richard.burtons.org/2015/05/17/esp8266-boot-process/
from esp_rom import EspRom
from esp_elf import XtensaElf, ElfSection, default_section_settings
from esp_bootrom import get_bootrom_contents, symbols
def parse_rom(rom_name, rom_filename, flash_layout):
with open(rom_filename) as f:
rom = EspRom(rom_name, f, flash_layout)
return rom
def name_sections(rom):
addr_to_section_name_mapping = {}
print "select a unique name for each section in the rom."
print "sensible defaults are available for the following common names:"
print " ".join(default_section_settings.keys())
print "if defaults are unavailable for a name, generic values will be used."
for section in rom.sections:
name = raw_input("enter a name for 0x%04x> " % (section.address))
addr_to_section_name_mapping[section.address] = name
return addr_to_section_name_mapping
def convert_rom_to_elf(esp_rom, addr_to_section_name_mapping, filename_to_write=None):
elf = XtensaElf(esp_rom.name + '.elf', esp_rom.header.entry_addr)
bootrom_bytes = get_bootrom_contents()
bootrom_section = ElfSection('.bootrom.text', 0x40000000, bootrom_bytes)
elf.add_section(bootrom_section, True)
for section in esp_rom.sections:
if section.address not in addr_to_section_name_mapping:
print "generation failed: no name for 0x%04x." % (section.address)
return None
name = addr_to_section_name_mapping[section.address]
elf_section = ElfSection(name, section.address, section.contents)
elf.add_section(elf_section, True)
for name, addr in symbols.iteritems():
elf.add_symbol(name, addr, '.bootrom.text')
elf.generate_elf()
if filename_to_write:
elf.write_to_file(filename_to_write)
return elf