# Writes the project name (custom_fw_name) and version (custom_version_file/custom_version_define) into the # esp_app_desc_t of firmware.bin, which the prebuilt Arduino libraries leave empty. The application can then # identify the image with esp_ota_get_partition_description(), or from the first bytes of an upload. # Must run before rename_fw.py so the renamed copy and its MD5 include the change. import hashlib import re import struct from pathlib import Path Import("env") IMAGE_HEADER_LEN = 24 SEGMENT_HEADER_LEN = 8 APP_DESC_OFFSET = IMAGE_HEADER_LEN + SEGMENT_HEADER_LEN APP_DESC_MAGIC = 0xABCD5432 HASH_APPENDED_OFFSET = 23 def read_version(env): version_file = Path(env.GetProjectOption("custom_version_file", "./src/emsesp_version.h")) version_define = env.GetProjectOption("custom_version_define", "EMSESP_APP_VERSION") pattern = re.compile(rf'^#define {version_define}\s+"(\S+)"') for line in version_file.read_text().splitlines(): match = pattern.match(line) if match: return match.group(1) raise ValueError(f"{version_define} not found in {version_file}") def put_string(image, offset, value): encoded = value.encode()[:31] image[offset:offset + 32] = encoded + b"\0" * (32 - len(encoded)) def set_app_desc(source, target, env): path = Path(target[0].get_abspath()) image = bytearray(path.read_bytes()) (magic,) = struct.unpack_from("