fix esp8266 fs download and extract (#20554)

This commit is contained in:
Jason2866 2024-01-21 13:16:05 +01:00 committed by GitHub
parent de91133414
commit 4a4fe27cee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 49 deletions

View File

@ -27,7 +27,6 @@ mcu = board.get("build.mcu", "esp32")
class FSType(Enum):
SPIFFS="spiffs"
LITTLEFS="littlefs"
FATFS="fatfs"
@ -44,26 +43,15 @@ class FSInfo:
def get_extract_cmd(self, input_file, output_dir):
raise NotImplementedError()
class LittleFSInfo(FSInfo):
class FS_Info(FSInfo):
def __init__(self, start, length, page_size, block_size):
self.tool = env["MKFSTOOL"]
self.tool = join(platform.get_package_dir("tool-mklittlefs"), self.tool)
super().__init__(FSType.LITTLEFS, start, length, page_size, block_size)
def __repr__(self):
return f"{self.fs_type} Start {hex(self.start)} Len {self.length} Page size {self.page_size} Block size {self.block_size}"
return f"{self.fs_type} Start {hex(self.start)} Len {hex(self.length)} Page size {hex(self.page_size)} Block size {hex(self.block_size)}"
def get_extract_cmd(self, input_file, output_dir):
return [self.tool, "-b", str(self.block_size), "-s", str(self.length), "-p", str(self.page_size), "--unpack", output_dir, input_file]
class SPIFFSInfo(FSInfo):
def __init__(self, start, length, page_size, block_size):
self.tool = env["MKFSTOOL"]
self.tool = join(platform.get_package_dir("tool-mklittlefs"), self.tool)
super().__init__(FSType.SPIFFS, start, length, page_size, block_size)
def __repr__(self):
return f"{self.fs_type} Start {hex(self.start)} Len {self.length} Page size {self.page_size} Block size {self.block_size}"
def get_extract_cmd(self, input_file, output_dir):
return f'"{self.tool}" -b {self.block_size} -p {self.page_size} --unpack "{output_dir}" "{input_file}"'
return f'"{self.tool}" -b {self.block_size} -s {self.length} -p {self.page_size} --unpack "{output_dir}" "{input_file}"'
# SPIFFS helpers copied from ESP32, https://github.com/platformio/platform-espressif32/blob/develop/builder/main.py
# Copyright 2014-present PlatformIO <contact@platformio.org>
@ -89,8 +77,6 @@ def _parse_size(value):
def _parse_ld_sizes(ldscript_path):
assert ldscript_path
result = {}
# get flash size from board's manifest
result['flash_size'] = int(env.BoardConfig().get("upload.maximum_size", 0))
# get flash size from LD script path
match = re.search(r"\.flash\.(\d+[mk]).*\.ld", ldscript_path)
if match:
@ -144,18 +130,6 @@ def esp8266_fetch_fs_size(env):
env[k] = _value
def esp8266_get_esptoolpy_reset_flags(resetmethod):
# no dtr, no_sync
resets = ("no_reset_no_sync", "soft_reset")
if resetmethod == "nodemcu":
# dtr
resets = ("default_reset", "hard_reset")
elif resetmethod == "ck":
# no dtr
resets = ("no_reset", "soft_reset")
return ["--before", resets[0], "--after", resets[1]]
## Script interface functions
def parse_partition_table(content):
entries = [e for e in content.split(b'\xaaP') if len(e) > 0]
@ -168,10 +142,10 @@ def parse_partition_table(content):
#print("type:",hex(type))
#print("address:",hex(offset))
#print("size:",hex(size))
env["SPIFFS_START"] = offset
env["SPIFFS_SIZE"] = size
env["SPIFFS_PAGE"] = int("0x100", 16)
env["SPIFFS_BLOCK"] = int("0x1000", 16)
env["FS_START"] = offset
env["FS_SIZE"] = size
env["FS_PAGE"] = int("0x100", 16)
env["FS_BLOCK"] = int("0x1000", 16)
def get_partition_table():
esptoolpy = join(platform.get_package_dir("tool-esptoolpy") or "", "esptool.py")
@ -193,8 +167,6 @@ def get_partition_table():
fs_file
]
esptoolpy_cmd = [env["PYTHONEXE"], esptoolpy] + esptoolpy_flags
#print("Executing flash download command to read partition table.")
#print(esptoolpy_cmd)
try:
returncode = subprocess.call(esptoolpy_cmd, shell=False)
except subprocess.CalledProcessError as exc:
@ -207,10 +179,8 @@ def get_fs_type_start_and_length():
platform = env["PIOPLATFORM"]
if platform == "espressif32":
print(f"Retrieving filesystem info for {mcu}.")
#print("Partition file: " + str(env.subst("$PARTITIONS_TABLE_CSV")))
# esp32_fetch_spiffs_size(env)
get_partition_table()
return SPIFFSInfo(env["SPIFFS_START"], env["SPIFFS_SIZE"], env["SPIFFS_PAGE"], env["SPIFFS_BLOCK"])
return FS_Info(env["FS_START"], env["FS_SIZE"], env["FS_PAGE"], env["FS_BLOCK"])
elif platform == "espressif8266":
print("Retrieving filesystem info for ESP8266.")
filesystem = board.get("build.filesystem", "littlefs")
@ -219,13 +189,13 @@ def get_fs_type_start_and_length():
env.Exit(1)
# fetching sizes is the same for all filesystems
esp8266_fetch_fs_size(env)
print("FS_START: " + hex(env["FS_START"]))
print("FS_END: " + hex(env["FS_END"]))
print("FS_PAGE: " + hex(env["FS_PAGE"]))
print("FS_BLOCK: " + hex(env["FS_BLOCK"]))
#print("FS_START: " + hex(env["FS_START"]))
#print("FS_SIZE: " + hex(env["FS_END"] - env["FS_START"]))
#print("FS_PAGE: " + hex(env["FS_PAGE"]))
#print("FS_BLOCK: " + hex(env["FS_BLOCK"]))
if filesystem == "littlefs":
print("Recognized LittleFS filesystem.")
return LittleFSInfo(env["FS_START"], env["FS_END"] - env["FS_START"], env["FS_PAGE"], env["FS_BLOCK"])
return FS_Info(env["FS_START"], env["FS_END"] - env["FS_START"], env["FS_PAGE"], env["FS_BLOCK"])
else:
print("Unrecongized configuration.")
pass
@ -252,10 +222,8 @@ def download_fs(fs_info: FSInfo):
]
esptoolpy_cmd = [env["PYTHONEXE"], esptoolpy] + esptoolpy_flags
print("Download filesystem image")
#print(esptoolpy_cmd)
try:
returncode = subprocess.call(esptoolpy_cmd, shell=False)
# print("Launched download of filesystem binary.")
return (True, fs_file)
except subprocess.CalledProcessError as exc:
print("Downloading failed with " + str(exc))
@ -265,7 +233,6 @@ def unpack_fs(fs_info: FSInfo, downloaded_file: str):
# by writing custom_unpack_dir = some_dir in the platformio.ini, one can
# control the unpack directory
unpack_dir = env.GetProjectOption("custom_unpack_dir", "unpacked_fs")
#unpack_dir = "unpacked_fs"
if not os.path.exists(downloaded_file):
print(f"ERROR: {downloaded_file} with filesystem not found, maybe download failed due to download_speed setting being too high.")
assert(0)
@ -279,7 +246,6 @@ def unpack_fs(fs_info: FSInfo, downloaded_file: str):
cmd = fs_info.get_extract_cmd(downloaded_file, unpack_dir)
print("Unpack files from filesystem image")
#print("Extraction command: " + str(cmd))
try:
returncode = subprocess.call(cmd, shell=True)
return (True, unpack_dir)
@ -294,10 +260,8 @@ def display_fs(extracted_dir):
print("Extracted " + str(file_count) + " file(s) from filesystem.")
def command_download_fs(*args, **kwargs):
get_partition_table()
info = get_fs_type_start_and_length()
download_ok, downloaded_file = download_fs(info)
# print("Download was okay: " + str(download_ok) + ". File at: "+ str(downloaded_file)) # this is wrong
unpack_ok, unpacked_dir = unpack_fs(info, downloaded_file)
if unpack_ok is True:
display_fs(unpacked_dir)