"""Raw SD card writing on Windows (needs administrator rights). Safety: only removable USB/SD/MMC disks that are neither the boot nor the system disk are offered. The first MiB (partition table) is written last, so Windows does not mount half-written partitions in the middle of the job - the same trick Raspberry Pi Imager uses. """ import ctypes import hashlib import json import lzma import subprocess import sys import time from pathlib import Path CHUNK = 4 << 20 # write size (multiple of the sector size) HEAD = 1 << 20 # held back and written last MAX_SIZE = 512 << 30 # larger "removable" disks are most likely not an SD card NO_WINDOW = 0x08000000 if sys.platform == "win32" else 0 def is_admin() -> bool: try: return bool(ctypes.windll.shell32.IsUserAnAdmin()) except (AttributeError, OSError): return False def relaunch_as_admin() -> bool: """Restart this program elevated (UAC prompt). True if the elevated copy was started.""" argv = sys.argv[1:] if getattr(sys, "frozen", False) else sys.argv # script: python