Загрузка данных


#!/usr/bin/env python3
import os
import struct
import sys

def main():
    # Use path from argument if provided, otherwise fallback to standard Zephyr build directory
    if len(sys.argv) > 1:
        bin_path = sys.argv[1]
    else:
        bin_path = "build_rt1024/build_pjsip/zephyr/zephyr.bin"

    if not os.path.exists(bin_path):
        # Local folder fallback check
        bin_path = "zephyr.bin"

    if not os.path.exists(bin_path):
        print(f"[Boot Patcher] Error: Target binary file not found at path: {bin_path}")
        print("Please provide the correct path as an argument. Example: python3 patch_bin.py path/to/zephyr.bin")
        sys.exit(1)

    # 1. Retrieve the exact byte size of the raw binary payload
    file_size = os.path.getsize(bin_path)
    
    # 2. Align the calculated size up to a strict 4KB (0x1000) sector boundary
    # This prevents the internal NXP FlexSPI Flash Controller from throwing stability faults
    aligned_size = (file_size + 0xFFF) & ~0xFFF

    print(f"[Boot Patcher] Found binary: '{bin_path}'")
    print(f"  -> Raw file size: {file_size} bytes ({hex(file_size)})")
    print(f"  -> MPU-Safe aligned size: {aligned_size} bytes ({hex(aligned_size)})")

    try:
        with open(bin_path, "r+b") as f:
            # Move the internal file pointer to the NXP Boot Data 'Length' field.
            # According to the NXP i.MX RT1024 Reference Manual, this field is strictly located at offset 0x24.
            f.seek(0x24)
            
            # Pack and write the aligned size as a 32-bit unsigned little-endian integer (<I)
            f.write(struct.pack("<I", aligned_size))
            
        print(f"[Boot Patcher] Success: Successfully patched Boot Data Length header to {hex(aligned_size)}")
    except Exception as e:
        print(f"[Boot Patcher] Critical Error: Could not patch binary header. Reason: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()