blob: 662e6f83204a38fd8f04482759cad9a04eed3e32 [file] [log] [blame]
Damien George075d5972014-11-27 20:30:33 +00001import sys
Paul Sokolovskya6213332016-08-04 00:19:09 +03002import struct
Paul Sokolovskybf47b712016-08-04 00:21:05 +03003import hashlib
Damien George075d5972014-11-27 20:30:33 +00004
Damien George497ca992020-04-04 16:30:39 +11005# This region at the start of flash contains a small header and then segments
6# containing .text, .data and .rodata, and so must be large enough to hold all
7# of this. This data is loaded to the appropriate places in RAM by the ROM
8# bootloader at boot. After this in flash comes .irom0.text, which must begin
9# on a flash erase-page boundary.
Paul Sokolovsky8d2bcaf2016-04-03 15:40:53 +030010SEGS_MAX_SIZE = 0x9000
11
Damien George075d5972014-11-27 20:30:33 +000012assert len(sys.argv) == 4
13
Paul Sokolovskybf47b712016-08-04 00:21:05 +030014md5 = hashlib.md5()
15
Damien George69661f32020-02-27 15:36:53 +110016with open(sys.argv[3], "wb") as fout:
Damien George69661f32020-02-27 15:36:53 +110017 with open(sys.argv[1], "rb") as f:
Damien George075d5972014-11-27 20:30:33 +000018 data_flash = f.read()
19 fout.write(data_flash)
Paul Sokolovskybf47b712016-08-04 00:21:05 +030020 # First 4 bytes include flash size, etc. which may be changed
21 # by esptool.py, etc.
22 md5.update(data_flash[4:])
Damien George69661f32020-02-27 15:36:53 +110023 print("flash ", len(data_flash))
Damien George075d5972014-11-27 20:30:33 +000024
Damien George497ca992020-04-04 16:30:39 +110025 # Print info about segments in this first part of flash
26 num_segs = struct.unpack_from("<BBBBI", data_flash, 0)[1]
27 offset = 8
28 for seg_num in range(num_segs):
29 seg_name = [".text", ".data", ".rodata"][seg_num]
30 seg_offset, seg_size = struct.unpack_from("<II", data_flash, offset)
31 print(" {:7} {} at 0x{:x}".format(seg_name, seg_size, seg_offset))
32 offset += 8 + seg_size
33
Damien George69661f32020-02-27 15:36:53 +110034 with open(sys.argv[2], "rb") as f:
Damien George075d5972014-11-27 20:30:33 +000035 data_rom = f.read()
Paul Sokolovskya6213332016-08-04 00:19:09 +030036
Damien George69661f32020-02-27 15:36:53 +110037 pad = b"\xff" * (SEGS_MAX_SIZE - len(data_flash))
Paul Sokolovskya6213332016-08-04 00:19:09 +030038 assert len(pad) >= 4
39 fout.write(pad[:-4])
Paul Sokolovskybf47b712016-08-04 00:21:05 +030040 md5.update(pad[:-4])
41 len_data = struct.pack("I", SEGS_MAX_SIZE + len(data_rom))
42 fout.write(len_data)
43 md5.update(len_data)
Damien George69661f32020-02-27 15:36:53 +110044 print("padding ", len(pad))
Paul Sokolovskya6213332016-08-04 00:19:09 +030045
46 fout.write(data_rom)
Paul Sokolovskybf47b712016-08-04 00:21:05 +030047 md5.update(data_rom)
Damien George69661f32020-02-27 15:36:53 +110048 print("irom0text", len(data_rom))
Damien George075d5972014-11-27 20:30:33 +000049
Paul Sokolovskybf47b712016-08-04 00:21:05 +030050 fout.write(md5.digest())
51
Damien George69661f32020-02-27 15:36:53 +110052 print("total ", SEGS_MAX_SIZE + len(data_rom))
53 print("md5 ", md5.hexdigest())