-
Notifications
You must be signed in to change notification settings - Fork 70
/
build_firmwarezip.py
207 lines (185 loc) · 7.37 KB
/
build_firmwarezip.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
197
198
199
200
201
202
203
204
205
206
207
# Create a compressed ZIP file of FujiNet firmware
# for use with the FujiNet-Flasher tool
Import("env")
platform = env.PioPlatform()
import sys, os, configparser, shutil, re, subprocess
import json
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):
# 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("BOOTLOADER not available to pack in firmware zip")
zipit = False
if not os.path.exists(env.subst("$BUILD_DIR/partitions.bin")):
print("PARTITIONS not available to pack in firmware zip")
zipit = False
if not os.path.exists(env.subst("$BUILD_DIR/firmware.bin")):
print("FIRMWARE not available to pack in firmware zip")
zipit = False
if not os.path.exists(env.subst("$BUILD_DIR/littlefs.bin")):
print("LittleFS not available to pack in firmware zip, run \"Build Filesystem Image\" first")
zipit = False
if zipit == True:
# Get the build_board variable
config = configparser.ConfigParser()
config.read(ini_file)
environment = "env:"+config['fujinet']['build_board'].split()[0]
print(f"Creating firmware zip for FujiNet 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")
# Create the 'firmware' output dir if it doesn't exist
firmdir = 'firmware'
if not os.path.exists(firmdir):
os.makedirs(firmdir)
# Filename variables
releasefile = firmdir+"/release.json"
if 'platform_name' in config['fujinet']:
firmwarezip = firmdir+"/fujinet-"+config['fujinet']['platform_name']+"-"+version['FN_VERSION_FULL']+".zip"
else:
firmwarezip = firmdir+"/fujinet-"+config['fujinet']['build_platform'].split("_")[1]+"-"+version['FN_VERSION_FULL']+".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": version['FN_VERSION_FULL'],
"version_date": version['FN_VERSION_DATE'],
"build_date": version['BUILD_DATE'],
"description": version['FN_VERSION_DESC'],
"git_commit": version['FN_VERSION_BUILD'],
"files": []
}
if config[environment]['board'] == "fujinet-v1":
json_contents['files'] += [
{
"filename": "bootloader.bin",
"offset": "0x1000"
},
{
"filename": "partitions.bin",
"offset": "0x8000"
},
{
"filename": "firmware.bin",
"offset": "0x10000"
},
{
"filename": "littlefs.bin",
"offset": "0x910000"
}
]
elif config[environment]['board'] == "fujinet-v1-8mb":
json_contents['files'] += [
{
"filename": "bootloader.bin",
"offset": "0x1000"
},
{
"filename": "partitions.bin",
"offset": "0x8000"
},
{
"filename": "firmware.bin",
"offset": "0x10000"
},
{
"filename": "littlefs.bin",
"offset": "0x600000"
}
]
elif config[environment]['board'] == "fujinet-v1-4mb":
json_contents['files'] += [
{
"filename": "bootloader.bin",
"offset": "0x1000"
},
{
"filename": "partitions.bin",
"offset": "0x8000"
},
{
"filename": "firmware.bin",
"offset": "0x10000"
},
{
"filename": "littlefs.bin",
"offset": "0x250000"
}
]
elif config[environment]['board'] == "fujinet-iec-nugget":
json_contents['files'] += [
{
"filename": "bootloader.bin",
"offset": "0x1000"
},
{
"filename": "partitions.bin",
"offset": "0x8000"
},
{
"filename": "firmware.bin",
"offset": "0x10000"
},
{
"filename": "littlefs.bin",
"offset": "0x910000"
}
]
# Save Release JSON
with open('firmware/release.json', 'w') as f:
f.write(json.dumps(json_contents, indent=4))
# 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(env.subst("$BUILD_DIR/littlefs.bin"), "littlefs.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)