2014-11-27 20:30:33 +00:00
|
|
|
import sys
|
2016-08-03 22:19:09 +01:00
|
|
|
import struct
|
2016-08-03 22:21:05 +01:00
|
|
|
import hashlib
|
2014-11-27 20:30:33 +00:00
|
|
|
|
2020-04-04 06:30:39 +01:00
|
|
|
# This region at the start of flash contains a small header and then segments
|
|
|
|
# containing .text, .data and .rodata, and so must be large enough to hold all
|
|
|
|
# of this. This data is loaded to the appropriate places in RAM by the ROM
|
|
|
|
# bootloader at boot. After this in flash comes .irom0.text, which must begin
|
|
|
|
# on a flash erase-page boundary.
|
2016-04-03 13:40:53 +01:00
|
|
|
SEGS_MAX_SIZE = 0x9000
|
|
|
|
|
2014-11-27 20:30:33 +00:00
|
|
|
assert len(sys.argv) == 4
|
|
|
|
|
2016-08-03 22:21:05 +01:00
|
|
|
md5 = hashlib.md5()
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
with open(sys.argv[3], "wb") as fout:
|
2014-11-27 20:30:33 +00:00
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
with open(sys.argv[1], "rb") as f:
|
2014-11-27 20:30:33 +00:00
|
|
|
data_flash = f.read()
|
|
|
|
fout.write(data_flash)
|
2016-08-03 22:21:05 +01:00
|
|
|
# First 4 bytes include flash size, etc. which may be changed
|
|
|
|
# by esptool.py, etc.
|
|
|
|
md5.update(data_flash[4:])
|
2020-02-27 04:36:53 +00:00
|
|
|
print("flash ", len(data_flash))
|
2014-11-27 20:30:33 +00:00
|
|
|
|
2020-04-04 06:30:39 +01:00
|
|
|
# Print info about segments in this first part of flash
|
|
|
|
num_segs = struct.unpack_from("<BBBBI", data_flash, 0)[1]
|
|
|
|
offset = 8
|
|
|
|
for seg_num in range(num_segs):
|
|
|
|
seg_name = [".text", ".data", ".rodata"][seg_num]
|
|
|
|
seg_offset, seg_size = struct.unpack_from("<II", data_flash, offset)
|
|
|
|
print(" {:7} {} at 0x{:x}".format(seg_name, seg_size, seg_offset))
|
|
|
|
offset += 8 + seg_size
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
with open(sys.argv[2], "rb") as f:
|
2016-08-03 22:19:09 +01:00
|
|
|
data_rom = f.read()
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
pad = b"\xff" * (SEGS_MAX_SIZE - len(data_flash))
|
2016-08-03 22:19:09 +01:00
|
|
|
assert len(pad) >= 4
|
|
|
|
fout.write(pad[:-4])
|
2016-08-03 22:21:05 +01:00
|
|
|
md5.update(pad[:-4])
|
|
|
|
len_data = struct.pack("I", SEGS_MAX_SIZE + len(data_rom))
|
|
|
|
fout.write(len_data)
|
|
|
|
md5.update(len_data)
|
2020-02-27 04:36:53 +00:00
|
|
|
print("padding ", len(pad))
|
2014-11-27 20:30:33 +00:00
|
|
|
|
2016-08-03 22:19:09 +01:00
|
|
|
fout.write(data_rom)
|
2016-08-03 22:21:05 +01:00
|
|
|
md5.update(data_rom)
|
2020-02-27 04:36:53 +00:00
|
|
|
print("irom0text", len(data_rom))
|
2014-11-27 20:30:33 +00:00
|
|
|
|
2016-08-03 22:21:05 +01:00
|
|
|
fout.write(md5.digest())
|
|
|
|
|
2020-02-27 04:36:53 +00:00
|
|
|
print("total ", SEGS_MAX_SIZE + len(data_rom))
|
|
|
|
print("md5 ", md5.hexdigest())
|