Skip to content

Commit 04b0c27

Browse files
authored
Added GZipped OTA support in elf2bin and PlatformIO (#7727)
1 parent 8c7fd6a commit 04b0c27

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

tools/elf2bin.py

+29
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,31 @@ def add_crc(out):
141141
with open(out, "wb") as binfile:
142142
binfile.write(raw)
143143

144+
def gzip_bin(mode, out):
145+
import gzip
146+
147+
firmware_path = out
148+
gzip_path = firmware_path + '.gz'
149+
orig_path = firmware_path + '.orig'
150+
if os.path.exists(gzip_path):
151+
os.remove(gzip_path)
152+
print('GZipping firmware ' + firmware_path)
153+
with open(firmware_path, 'rb') as firmware_file, \
154+
gzip.open(gzip_path, 'wb') as dest:
155+
data = firmware_file.read()
156+
dest.write(data)
157+
orig_size = os.stat(firmware_path).st_size
158+
gzip_size = os.stat(gzip_path).st_size
159+
print("New FW size {:d} bytes vs old {:d} bytes".format(
160+
gzip_size, orig_size))
161+
162+
if mode == "PIO":
163+
if os.path.exists(orig_path):
164+
os.remove(orig_path)
165+
print('Moving original firmware to ' + orig_path)
166+
os.rename(firmware_path, orig_path)
167+
os.rename(gzip_path, firmware_path)
168+
144169
def main():
145170
parser = argparse.ArgumentParser(description='Create a BIN file from eboot.elf and Arduino sketch.elf for upload by esptool.py')
146171
parser.add_argument('-e', '--eboot', action='store', required=True, help='Path to the Arduino eboot.elf bootloader')
@@ -150,6 +175,7 @@ def main():
150175
parser.add_argument('-s', '--flash_size', action='store', required=True, choices=['256K', '512K', '1M', '2M', '4M', '8M', '16M'], help='SPI flash size')
151176
parser.add_argument('-o', '--out', action='store', required=True, help='Output BIN filename')
152177
parser.add_argument('-p', '--path', action='store', required=True, help='Path to Xtensa toolchain binaries')
178+
parser.add_argument('-g', '--gzip', choices=['PIO', 'Arduino'], help='PIO - generate gzipped BIN file, Arduino - generate BIN and BIN.gz')
153179

154180
args = parser.parse_args()
155181

@@ -175,6 +201,9 @@ def wrapper(**kwargs):
175201
# Because the CRC includes both eboot and app, can only calculate it after the entire BIN generated
176202
add_crc(args.out)
177203

204+
if args.gzip:
205+
gzip_bin(args.gzip, args.out)
206+
178207
return 0
179208

180209

tools/platformio-build.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ def scons_patched_match_splitext(path, suffixes=None):
4545

4646
env = DefaultEnvironment()
4747
platform = env.PioPlatform()
48+
board = env.BoardConfig()
49+
gzip_fw = board.get("build.gzip_fw", False)
50+
gzip_switch = []
4851

4952
FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoespressif8266")
5053
assert isdir(FRAMEWORK_DIR)
5154

55+
if gzip_fw:
56+
gzip_switch = ["--gzip", "PIO"]
5257

5358
env.Append(
5459
ASFLAGS=["-x", "assembler-with-cpp"],
@@ -145,7 +150,7 @@ def scons_patched_match_splitext(path, suffixes=None):
145150
"--path", '"%s"' % join(
146151
platform.get_package_dir("toolchain-xtensa"), "bin"),
147152
"--out", "$TARGET"
148-
]), "Building $TARGET"),
153+
] + gzip_switch), "Building $TARGET"),
149154
suffix=".bin"
150155
)
151156
)

0 commit comments

Comments
 (0)