forked from 1technophile/OpenMQTTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add script to compress firmware before ota upload (1technophile#1065)
* add script to compress firmware before ota upload * add examples for OTA flashing/updating Sonoff RF Bridge - use compress script to save space uploading firmware - use "board_build.ldscript = eagle.flash.1m64.ld" to free some additional space for OTA flashing.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import gzip | ||
import shutil | ||
import os | ||
Import("env") | ||
|
||
|
||
def compressFirmware(source, target, env): | ||
""" Compress firmware using gzip for 'compressed OTA upload' """ | ||
SOURCE_FILE = env.subst("$BUILD_DIR") + os.sep + env.subst("$PROGNAME") + ".bin" | ||
SOURCE_BAK = SOURCE_FILE + '.bak' | ||
do_compress = True | ||
if os.path.exists(SOURCE_FILE) and os.path.exists(SOURCE_BAK): | ||
src_mtime = os.stat(SOURCE_FILE).st_mtime | ||
bak_mtime = os.stat(SOURCE_BAK).st_mtime | ||
""" Recompress if .bin file is newer than .bak file """ | ||
do_compress = (src_mtime > bak_mtime) | ||
|
||
if do_compress: | ||
print("Compressing firmware for upload...") | ||
shutil.move(SOURCE_FILE, SOURCE_BAK) | ||
with open(SOURCE_BAK, 'rb') as f_in: | ||
with gzip.open(SOURCE_FILE, 'wb', compresslevel = 9) as f_out: | ||
shutil.copyfileobj(f_in, f_out) | ||
""" Set modification time on compressed file so incremental build works """ | ||
shutil.copystat(SOURCE_BAK, SOURCE_FILE) | ||
|
||
if os.path.exists(SOURCE_BAK): | ||
ORG_FIRMWARE_SIZE = os.stat(SOURCE_BAK).st_size | ||
GZ_FIRMWARE_SIZE = os.stat(SOURCE_FILE).st_size | ||
|
||
print("Compression reduced firmware size to {:.0f}% (was {} bytes, now {} bytes)".format((GZ_FIRMWARE_SIZE / ORG_FIRMWARE_SIZE) * 100, ORG_FIRMWARE_SIZE, GZ_FIRMWARE_SIZE)) | ||
|
||
env.AddPostAction("$BUILD_DIR/firmware.bin", [compressFirmware]) |