GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — esptool/esptool/util.py at master · espressif/esptool Expand file tree/ Copy pathutil.pyMore file actions 301 lines (240 loc) · 9.64 KB / Copy pathutil.pyFile metadata and controls301 lines (240 loc) · 9.64 KB 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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 # SPDX-FileCopyrightText: 2014-2025 Fredrik Ahlberg, Angus Gratton, # Espressif Systems (Shanghai) CO LTD, other contributors as noted. # # SPDX-License-Identifier: GPL-2.0-or-later from __future__ import annotations import os import re import struct from typing import IO, TypeAlias from esp_pylib.errors import FatalError as _PylibFatalError # Define a custom type for the input ImageSource: TypeAlias = str | bytes | IO[bytes] # Chips that implement the Secure Debug Controller (SDC). Append new target # names here as SDC support is added on additional chips. SDC_SUPPORTED_CHIPS = ("ESP32-S31",) def byte(bitstr, index): return bitstr[index] def mask_to_shift(mask): """Return the index of the least significant bit in the mask""" shift = 0 while mask & 0x1 == 0: shift += 1 mask >>= 1 return shift def div_roundup(a, b): """Return a/b rounded up to nearest integer, equivalent result to int(math.ceil(float(int(a)) / float(int(b))), only without possible floating point accuracy errors. """ return (int(a) + int(b) - 1) // int(b) def flash_size_bytes(size): """Given a flash size of the type passed in size (ie 512KB or 1MB) then return the size in bytes. """ if size is None: return None if "MB" in size: return int(size[: size.index("MB")]) * 1024 * 1024 elif "KB" in size: return int(size[: size.index("KB")]) * 1024 else: raise FatalError(f"Unknown size {size}") def hexify(s, uppercase=True): format_str = "%02X" if uppercase else "%02x" return "".join(format_str % c for c in s) def pad_to(data, alignment, pad_character=b"\xff"): """Pad to the next alignment boundary""" pad_mod = len(data) % alignment if pad_mod != 0: data += pad_character * (alignment - pad_mod) return data def expand_chip_name(chip_name): """Change chip name to official form, e.g. `esp32s3beta2` -> `ESP32-S3(beta2)`""" # Put "-" after "esp32" chip_name = re.sub(r"(esp32)(?!$)", r"\1-", chip_name) # Put "()" around "betaN" chip_name = re.sub(r"(beta\d*)", r"(\1)", chip_name) # Uppercase everything before "(betaN)" chip_name = re.sub(r"^[^\(]+", lambda x: x.group(0).upper(), chip_name) return chip_name def strip_chip_name(chip_name): """Strip chip name to normalized form, e.g. `ESP32-S3(beta2)` -> `esp32s3beta2`""" return re.sub(r"[-()]", "", chip_name.lower()) def get_file_size(path_to_file): """Returns the file size in bytes""" file_size = 0 with open(path_to_file, "rb") as f: f.seek(0, os.SEEK_END) file_size = f.tell() return file_size def sanitize_string(byte_string): return byte_string.decode("utf-8").replace("\0", "") def get_bytes(input: ImageSource) -> tuple[bytes, str | None]: """ Normalize the input (file path, bytes, or an opened file-like object) into bytes and provide a name of the source. Args: input: The input file path, bytes, or an opened file-like object. Returns: A tuple containing the normalized bytes and the source of the input. """ if isinstance(input, str): with open(input, "rb") as f: data = f.read() source = input elif isinstance(input, bytes): data = input source = None elif hasattr(input, "read") and hasattr(input, "write") and hasattr(input, "close"): pos = input.tell() data = input.read() input.seek(pos) # Reset the file pointer source = input.name else: raise FatalError(f"Invalid input type {type(input)}") return data, source def get_key_from_value(dict, val): """ Get key from value in dictionary, assumes unique values in dictionary """ for key, value in dict.items(): if value == val: return key return None def check_deprecated_py_suffix(module_name: str) -> None: """Check if called with deprecated .py suffix""" import sys from esptool import log script_name = sys.argv[0] if sys.argv else "" if script_name.endswith(module_name + ".py"): log.warn( f"DEPRECATED: '{module_name}.py' is deprecated. Please use '{module_name}' " "instead. The '.py' suffix will be removed in a future major release." ) class PrintOnce: """ Class for printing messages just once. Can be useful when running in a loop """ def __init__(self, print_callback) -> None: self.already_printed = False self.print_callback = print_callback def __call__(self, text) -> None: if not self.already_printed: self.print_callback(text) self.already_printed = True class FatalError(_PylibFatalError): """ Wrapper class for runtime errors that aren't caused by internal bugs, but by ESP ROM responses or input content. Extends `esp_pylib.errors.FatalError` (itself a ``RuntimeError``) so existing ``except FatalError`` / ``except RuntimeError`` blocks in esptool and downstream tools (espefuse, espsecure) keep working unchanged. """ @staticmethod def WithResult(message, result): """ Return a fatal error object that appends the hex values of 'result' and its meaning as a string formatted argument. """ err_defs = { # ROM error codes 0x100: "Undefined errors", 0x101: "The input parameter is invalid", 0x102: "Failed to malloc memory from system", 0x103: "Failed to send out message", 0x104: "Failed to receive message", 0x105: "The format of the received message is invalid", 0x106: "Message is ok, but the running result is wrong", 0x107: "Checksum error", 0x108: "Flash write error", 0x109: "Flash read error", 0x10A: "Flash read length error", 0x10B: "Deflate failed error", 0x10C: "Deflate Adler32 error", 0x10D: "Deflate parameter error", 0x10E: "Invalid RAM binary size", 0x10F: "Invalid RAM binary address", 0x164: "Invalid parameter", 0x165: "Invalid format", 0x166: "Description too long", 0x167: "Bad encoding description", 0x169: "Insufficient storage", # Flasher stub error codes 0xC000: "Bad data length", 0xC100: "Bad data checksum", 0xC200: "Bad blocksize", 0xC300: "Invalid command", 0xC400: "Failed SPI operation", 0xC500: "Failed SPI unlock", 0xC600: "Not in flash mode", 0xC700: "Inflate error", 0xC800: "Not enough data", 0xC900: "Too much data", 0xCA00: "NAND program failed (P_FAIL)", 0xCB00: "NAND erase failed (E_FAIL)", 0xFF00: "Command not implemented", } err_code = struct.unpack(">H", result[:2]) message += " (result was {}: {})".format( hexify(result), err_defs.get(err_code[0], "Unknown result") ) if err_code[0] == 0xCA00: return NANDProgramFailed(message) if err_code[0] == 0xCB00: return NANDEraseFailed(message) return FatalError(message) class NotImplementedInROMError(FatalError): """ Wrapper class for the error thrown when a particular ESP bootloader function is not implemented in the ROM bootloader. """ def __init__(self, bootloader, func): FatalError.__init__( self, f"{bootloader.CHIP_NAME} ROM does not support function {func.__name__}.", ) class NotSupportedError(FatalError): def __init__(self, esp, function_name): FatalError.__init__( self, f"{function_name} is not supported by {esp.CHIP_NAME}.", ) class NANDProgramFailed(FatalError): """Raised when the stub reports a NAND P_FAIL (program failed, 0xCA00). Indicates the chip set the P_FAIL bit after a page program operation. Per W25N01GV app-note: mark the block bad and retry on the next block. """ def __init__(self, message): FatalError.__init__(self, message) class NANDEraseFailed(FatalError): """Raised when the stub reports a NAND E_FAIL (erase failed, 0xCB00). Indicates the chip set the E_FAIL bit after a block erase operation. Per W25N01GV app-note: mark the block bad and retry on the next block. """ def __init__(self, message): FatalError.__init__(self, message) class UnsupportedCommandError(FatalError): """ Wrapper class for when ROM loader returns an invalid command response. Usually this indicates the loader is running in a restricted download mode (e.g. Secure Download Mode). """ def __init__(self, esp, op): if esp.secure_download_mode: msg = f"This command ({op:#x}) is not supported in Secure Download Mode" elif getattr(esp, "CHIP_NAME", None) in SDC_SUPPORTED_CHIPS: # SDC is only implemented on specific chips, so only mention it there # to avoid confusing users of other chips with an irrelevant hint. msg = ( f"This command ({op:#x}) is not supported in the current download " "mode. If download mode is disabled and the Secure Debug " "Controller (SDC) is enabled, re-open download mode by verifying a " "download-reuse certificate ('esptool verify-sdc-certificate'), " "then reconnect with '--before no-reset'." ) else: msg = ( f"This command ({op:#x}) is not supported in the current download mode" ) super().__init__(msg) |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |