-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
pack_firmware.py
196 lines (173 loc) · 6.46 KB
/
pack_firmware.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Create a compressed ZIP file of meatloaf firmware
# for use with the meatloaf-Flasher tool
Import("env")
platform = env.PioPlatform()
import sys, os, configparser, shutil, re, subprocess
from os.path import join
from datetime import datetime
from zipfile import ZipFile
print("Build firmware ZIP enabled")
ini_file = 'platformio.ini'
# this is specified with "-c /path/to/your.ini" when running pio
if env["PROJECT_CONFIG"] is not None:
ini_file = env["PROJECT_CONFIG"]
print(f"Reading from config file {ini_file}")
def makezip(source, target, env):
# Create the 'firmware' output dir if it doesn't exist
firmdir = 'firmware'
if not os.path.exists(firmdir):
os.makedirs(firmdir)
# Make sure all the files are built and ready to zip
zipit = True
if not os.path.exists(env.subst("$BUILD_DIR/bootloader.bin")):
print("\033[1;31mBOOTLOADER not available to pack in firmware zip\033[1;37m")
zipit = False
if not os.path.exists(env.subst("$BUILD_DIR/partitions.bin")):
print("\033[1;31mPARTITIONS not available to pack in firmware zip\033[1;37m")
zipit = False
if not os.path.exists(env.subst("$BUILD_DIR/firmware.bin")):
print("\033[1;31mFIRMWARE not available to pack in firmware zip\033[1;37m")
zipit = False
if not os.path.exists(env.subst("$BUILD_DIR/littlefs.bin")):
print("\033[1;31mLittleFS not available to pack in firmware zip, building...\033[1;37m")
os.system("pio run -t buildfs")
zipit = False
if zipit == True:
# Get the build_board variable
config = configparser.ConfigParser()
config.read(ini_file)
environment = "env:"+config['meatloaf']['environment'].split()[0]
print(f"Creating firmware zip for Meatloaf ESP32 Board: {config[environment]['board']}")
# Get version information
with open("include/version.h", "r") as file:
version_content = file.read()
defines = re.findall(r'#define\s+(\w+)\s+"?([^"\n]+)"?\n', version_content)
version = {}
for define in defines:
name = define[0]
value = define[1]
version[name] = value
# Get and clean the current commit message
try:
version_desc = subprocess.getoutput("git log -1 --pretty=%B | tr '\n' ' '")
except subprocess.CalledProcessError as e:
# Revert to full version if no commit msg or error
version_desc = version['FN_VERSION_FULL']
try:
version_build = subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], universal_newlines=True).strip()
except subprocess.CalledProcessError as e:
version_build = "NOGIT"
version['FN_VERSION_DESC'] = version_desc
version['FN_VERSION_BUILD'] = version_build
version['BUILD_DATE'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Copy filesystem image to firmware folder
try:
shutil.copy(env.subst("$BUILD_DIR/littlefs.bin"), firmdir+"/filesystem.bin")
except: pass
# Filename variables
environment_name = config['meatloaf']['environment'].split()[0]
firmware_date = datetime.utcnow().strftime("%Y%m%d.%H")
releasefile = firmdir+"/release.json"
firmwarezip = firmdir+"/meatloaf."+environment_name+"."+firmware_date+".zip"
# Clean the firmware output dir
try:
if os.path.isfile(releasefile):
os.unlink(releasefile)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (releasefile, e))
try:
if os.path.isfile(firmwarezip):
os.unlink(firmwarezip)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (firmwarezip, e))
# Create release JSON
json_contents = """{
"version": "%s",
"version_date": "%s",
"build_date": "%s",
"description": "%s",
"git_commit": "%s",
"files":
[
""" % (version['FN_VERSION_FULL'], version['FN_VERSION_DATE'], version['BUILD_DATE'], version['FN_VERSION_DESC'], version['FN_VERSION_BUILD'])
if config[environment]['board'] == "esp32-4mb":
json_contents += """ {
"filename": "bootloader.bin",
"offset": "0x1000"
},
{
"filename": "partitions.bin",
"offset": "0x8000"
},
{
"filename": "firmware.bin",
"offset": "0x10000"
},
{
"filename": "filesystem.bin",
"offset": "0x250000"
}
]
}
"""
elif config[environment]['board'] == "esp32-8mb":
json_contents += """ {
"filename": "bootloader.bin",
"offset": "0x1000"
},
{
"filename": "partitions.bin",
"offset": "0x8000"
},
{
"filename": "firmware.bin",
"offset": "0x10000"
},
{
"filename": "filesystem.bin",
"offset": "0x600000"
}
]
}
"""
elif config[environment]['board'] == "esp32-16mb":
json_contents += """ {
"filename": "bootloader.bin",
"offset": "0x1000"
},
{
"filename": "partitions.bin",
"offset": "0x8000"
},
{
"filename": "firmware.bin",
"offset": "0x10000"
},
{
"filename": "filesystem.bin",
"offset": "0x910000"
}
]
}
"""
# Save Release JSON
with open('firmware/release.json', 'w') as f:
f.write(json_contents)
# Create the ZIP File
try:
with ZipFile(firmwarezip, 'w') as zip_object:
zip_object.write(env.subst("$BUILD_DIR/bootloader.bin"), "bootloader.bin")
zip_object.write(env.subst("$BUILD_DIR/partitions.bin"), "partitions.bin")
zip_object.write(env.subst("$BUILD_DIR/firmware.bin"), "firmware.bin")
zip_object.write(firmdir+"/filesystem.bin", "filesystem.bin")
zip_object.write("firmware/release.json", "release.json")
finally:
print("*" * 80)
print("*")
print("* FIRMWARE ZIP CREATED AT: " + firmwarezip)
print("*")
print("*" * 80)
# else:
# print("Skipping making firmware ZIP due to error")
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", makezip)
env.AddPostAction("buildfs", makezip)