-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopy_fw_files.py
95 lines (79 loc) · 3.64 KB
/
copy_fw_files.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
Import("env")
import os, zipfile, shutil
from pathlib import Path
# Get the version number from the build environment.
firmware_version = os.environ.get('VERSION', "")
if firmware_version == "":
firmware_version = "0.0.1"
firmware_version = firmware_version.lstrip("v")
firmware_version = firmware_version.strip(".")
# Get the ZIP filename from the build environment.
community_project = env.GetProjectOption('custom_community_project', "")
# Get the custom folder from the build environment.
custom_source_folder = env.GetProjectOption('custom_source_folder', "")
# Get the foldername inside the zip file from the build environment.
community_folder = env.GetProjectOption('custom_community_folder', "")
platform = env.BoardConfig().get("platform", {})
def copy_fw_files (source, target, env):
fw_file_name=str(target[0])
if os.path.exists("./_build/" + custom_source_folder) == False:
os.makedirs("./_build/" + custom_source_folder + "/Community/firmware")
shutil.copytree(custom_source_folder + "/Community", "./_build/" + custom_source_folder + "/Community", dirs_exist_ok=True)
print("Creating /_build folder")
if platform == "raspberrypi":
fw_file_name=fw_file_name[0:-3] + "uf2"
if platform == "espressif32":
merge_bin(source, target, env)
old_name = fw_file_name[0:-4] + "_merged.bin"
fw_file_name = fw_file_name[0:-9] + "merged_" + firmware_version.replace(".", "_") + ".bin"
os.replace(old_name, fw_file_name)
print("Copying community folder")
shutil.copy(fw_file_name, "./_build/" + custom_source_folder + "/Community/firmware")
original_folder_path = "./_build/" + custom_source_folder + "/Community"
zip_file_path = './_dist/' + community_project + '_' + firmware_version + '.zip'
new_folder_in_zip = community_folder
print("Creating zip file")
createZIP(original_folder_path, zip_file_path, new_folder_in_zip)
def createZIP(original_folder_path, zip_file_path, new_folder_name):
if os.path.exists("./_dist") == False:
os.mkdir("./_dist")
with zipfile.ZipFile(zip_file_path, 'w') as zipf:
for root, dirs, files in os.walk(original_folder_path):
for file in files:
# Create a new path in the ZIP file
new_path = os.path.join(new_folder_name, os.path.relpath(os.path.join(root, file), original_folder_path))
# Add the file to the ZIP file
zipf.write(os.path.join(root, file), new_path)
APP_BIN = "$BUILD_DIR/${PROGNAME}.bin"
MERGED_BIN = "$BUILD_DIR/${PROGNAME}_merged.bin"
BOARD_CONFIG = env.BoardConfig()
def merge_bin(source, target, env):
# The list contains all extra images (bootloader, partitions, eboot) and
# the final application binary
flash_images = env.Flatten(env.get("FLASH_EXTRA_IMAGES", [])) + ["$ESP32_APP_OFFSET", APP_BIN]
# Run esptool to merge images into a single binary
env.Execute(
" ".join(
[
"$PYTHONEXE",
"$OBJCOPY",
"--chip",
BOARD_CONFIG.get("build.mcu", "esp32"),
"merge_bin",
"--fill-flash-size",
BOARD_CONFIG.get("upload.flash_size", "4MB"),
"-o",
MERGED_BIN,
]
+ flash_images
)
)
# Patch the upload command to flash the merged binary at address 0x0
#env.Replace(
# UPLOADERFLAGS=[
# ]
# + ["write_flash", "0x0", MERGED_BIN],
# UPLOADCMD='"$PYTHONEXE" "$UPLOADER" $UPLOADERFLAGS',
#)
env.AddPostAction("$BUILD_DIR/${PROGNAME}.hex", copy_fw_files)
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", copy_fw_files)