Prepare firmware files for mounting into Docker container. Args: firmware_path: Path to firmware.bin or build directory Returns: Path to the prepared firmware directory
(self, firmware_path: Path)
| 427 | raise e |
| 428 | |
| 429 | def prepare_firmware(self, firmware_path: Path) -> Path: |
| 430 | """Prepare firmware files for mounting into Docker container. |
| 431 | |
| 432 | Args: |
| 433 | firmware_path: Path to firmware.bin or build directory |
| 434 | |
| 435 | Returns: |
| 436 | Path to the prepared firmware directory |
| 437 | """ |
| 438 | # Create temporary directory for firmware files |
| 439 | temp_dir = Path(tempfile.mkdtemp(prefix="qemu_firmware_")) |
| 440 | |
| 441 | try: |
| 442 | if firmware_path.is_file() and firmware_path.suffix == ".bin": |
| 443 | # Copy single firmware file |
| 444 | shutil.copy2(firmware_path, temp_dir / "firmware.bin") |
| 445 | # Create proper 4MB flash image for QEMU |
| 446 | self._create_flash_image(firmware_path, temp_dir / "flash.bin") |
| 447 | else: |
| 448 | # Copy entire build directory |
| 449 | if firmware_path.is_dir(): |
| 450 | # Copy relevant files from build directory |
| 451 | patterns = ["*.bin", "*.elf", "partitions.csv", "flash_args"] |
| 452 | for pattern in patterns: |
| 453 | for file in firmware_path.glob(pattern): |
| 454 | shutil.copy2(file, temp_dir) |
| 455 | |
| 456 | # Also check for PlatformIO build structure |
| 457 | pio_build = firmware_path / ".pio" / "build" / "esp32dev" |
| 458 | if pio_build.exists(): |
| 459 | for pattern in patterns: |
| 460 | for file in pio_build.glob(pattern): |
| 461 | shutil.copy2(file, temp_dir) |
| 462 | |
| 463 | # Create proper flash.bin file from firmware.bin |
| 464 | firmware_bin = temp_dir / "firmware.bin" |
| 465 | if firmware_bin.exists(): |
| 466 | # Create proper 4MB flash image for QEMU |
| 467 | self._create_flash_image(firmware_bin, temp_dir / "flash.bin") |
| 468 | else: |
| 469 | # Create a minimal 4MB flash.bin for testing |
| 470 | flash_bin = temp_dir / "flash.bin" |
| 471 | flash_bin.write_bytes( |
| 472 | b"\xff" * (4 * 1024 * 1024) |
| 473 | ) # 4MB of 0xFF |
| 474 | else: |
| 475 | raise ValueError(f"Invalid firmware path: {firmware_path}") |
| 476 | |
| 477 | return temp_dir |
| 478 | except KeyboardInterrupt as ki: |
| 479 | handle_keyboard_interrupt(ki) |
| 480 | raise |
| 481 | except Exception as e: |
| 482 | # Clean up on error |
| 483 | shutil.rmtree(temp_dir, ignore_errors=True) |
| 484 | raise e |
| 485 | |
| 486 | def _create_flash_image( |
no test coverage detected