Skip to content

Commit bba6d3c

Browse files
committed
improving readability
1 parent e578a78 commit bba6d3c

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

esp32_image_parser.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def image2elf(filename, output_file, verbose=False):
3939
image = LoadFirmwareImage('esp32', filename)
4040

4141
# parse image name
42+
# e.g. 'image.bin' turns to 'image'
4243
image_name = image_base_name(filename)
4344

4445
elf = ELF(e_machine=EM.EM_XTENSA, e_data=ELFDATA.ELFDATA2LSB)
@@ -50,29 +51,32 @@ def image2elf(filename, output_file, verbose=False):
5051
section_map = {
5152
'DROM' : '.flash.rodata',
5253
'BYTE_ACCESSIBLE, DRAM, DMA': '.dram0.data',
53-
'RTC_IRAM' : '.rtc.text', # TODO double-check
54-
'IROM' : '.flash.text'
54+
'IROM' : '.flash.text',
55+
#'RTC_IRAM' : '.rtc.text' TODO
5556
}
5657

57-
section_ids = {}
58-
section_data = {}
59-
60-
# map to hold pre-defined ELF section attributes
58+
# map to hold pre-defined ELF section header attributes
59+
# http://man7.org/linux/man-pages/man5/elf.5.html
60+
# ES : sh_entsize
61+
# Flg : sh_flags
62+
# Lk : sh_link
63+
# Inf : sh_info
64+
# Al : sh_addralign
6165
sect_attr_map = {
6266
'.flash.rodata' : {'ES':0x00, 'Flg':'WA', 'Lk':0, 'Inf':0, 'Al':16},
6367
'.dram0.data' : {'ES':0x00, 'Flg':'WA', 'Lk':0, 'Inf':0, 'Al':16},
6468
'.iram0.vectors': {'ES':0x00, 'Flg':'AX', 'Lk':0, 'Inf':0, 'Al':4},
65-
'.iram0.text' : {'ES':0x00, 'Flg':'WAX', 'Lk':0, 'Inf':0, 'Al':4}, # TODO WAX? or just AX?
69+
'.iram0.text' : {'ES':0x00, 'Flg':'AX', 'Lk':0, 'Inf':0, 'Al':4},
6670
'.flash.text' : {'ES':0x00, 'Flg':'AX', 'Lk':0, 'Inf':0, 'Al':4}
6771
}
6872
# TODO rtc not accounted for
6973

70-
idx = 0
74+
section_data = {}
75+
7176
##### build out the section data #####
7277
######################################
7378
iram_seen = False
7479
for seg in sorted(image.segments, key=lambda s:s.addr):
75-
idx += 1
7680

7781
# name from image
7882
segment_name = ", ".join([seg_range[2] for seg_range in image.ROM_LOADER.MEMORY_MAP if seg_range[0] <= seg.addr < seg_range[1]])
@@ -113,27 +117,29 @@ def image2elf(filename, output_file, verbose=False):
113117
if name in sect_attr_map:
114118
sect = sect_attr_map[name]
115119
flg = calcShFlg(sect['Flg'])
116-
section_ids[name] = elf._append_section(name, data, addr,SHT.SHT_PROGBITS, flg, sect['Lk'], sect['Inf'], sect['Al'], sect['ES'])
120+
elf._append_section(name, data, addr,SHT.SHT_PROGBITS, flg, sect['Lk'], sect['Inf'], sect['Al'], sect['ES'])
117121
else:
118-
section_ids[name] = elf.append_section(name, data, addr)
122+
elf.append_section(name, data, addr)
119123

120124
elf.append_special_section('.strtab')
121125
elf.append_special_section('.symtab')
122126
add_elf_symbols(elf)
123127

124128
# segment flags
125-
# TODO double check this stuff
129+
# TODO rtc
126130
segments = {
127131
'.flash.rodata' : 'rw',
128132
'.dram0.data' : 'rw',
129-
'.iram0.vectors': 'rwx',
133+
'.iram0.vectors': 'rx',
130134
'.flash.text' : 'rx'
131135
}
132136

133137
# there is an initial program header that we don't want...
134138
elf.Elf.Phdr_table.pop()
135139

136140
bytes(elf) # kind of a hack, but __bytes__() calculates offsets in elf object
141+
142+
# TODO this logic might change as we add support for rtc
137143
size_of_phdrs = len(Elf32_Phdr()) * len(segments) # to pre-calculate program header offsets
138144

139145
##### add the segments ####
@@ -158,8 +164,7 @@ def image2elf(filename, output_file, verbose=False):
158164
# build program header
159165
Phdr = Elf32_Phdr(PT.PT_LOAD, p_offset=offset, p_vaddr=addr,
160166
p_paddr=addr, p_filesz=size, p_memsz=size,
161-
p_flags=p_flags, p_align=0x1000, little=elf.little)
162-
167+
p_flags=p_flags, p_align=align, little=elf.little)
163168

164169
print_verbose(verbose, name + ": " + str(Phdr))
165170
elf.Elf.Phdr_table.append(Phdr)

0 commit comments

Comments
 (0)