forked from jsandin/esp-bin2elf
-
Notifications
You must be signed in to change notification settings - Fork 4
/
esp_bin2elf.py
59 lines (41 loc) · 1.95 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
59
# 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, offset=0):
with open(rom_filename, 'rb') as f:
rom = EspRom(rom_name, f, flash_layout, offset=offset)
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(list(default_section_settings.keys())))
print("If defaults are unavailable for a name, generic values will be used.")
for section in rom.sections:
name = 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.items():
elf.add_symbol(name, addr, '.bootrom.text')
elf.generate_elf()
if filename_to_write:
elf.write_to_file(filename_to_write)
return elf