|
| 1 | +import binascii |
1 | 2 | import hashlib
|
2 | 3 | import os
|
3 | 4 | import sys
|
4 | 5 | import tempfile
|
| 6 | +import zlib |
5 | 7 |
|
6 | 8 | import serial.tools.list_ports
|
7 | 9 |
|
8 |
| -from .transport import TransportError, stdout_write_bytes |
| 10 | +from .transport import TransportError, TransportExecError, stdout_write_bytes |
9 | 11 | from .transport_serial import SerialTransport
|
| 12 | +from .romfs import make_romfs |
10 | 13 |
|
11 | 14 |
|
12 | 15 | class CommandError(Exception):
|
@@ -478,3 +481,181 @@ def do_rtc(state, args):
|
478 | 481 | state.transport.exec("machine.RTC().datetime({})".format(timetuple))
|
479 | 482 | else:
|
480 | 483 | print(state.transport.eval("machine.RTC().datetime()"))
|
| 484 | + |
| 485 | + |
| 486 | +def _do_romfs_query(state, args): |
| 487 | + state.ensure_raw_repl() |
| 488 | + state.did_action() |
| 489 | + |
| 490 | + # Detect the romfs and get its associated device. |
| 491 | + state.transport.exec("import vfs") |
| 492 | + if not state.transport.eval("hasattr(vfs,'rom_ioctl')"): |
| 493 | + print("ROMFS is not enabled on this device") |
| 494 | + return |
| 495 | + num_rom_partitions = state.transport.eval("vfs.rom_ioctl(1)") |
| 496 | + if num_rom_partitions <= 0: |
| 497 | + print("No ROMFS partitions available") |
| 498 | + return |
| 499 | + |
| 500 | + for rom_id in range(num_rom_partitions): |
| 501 | + state.transport.exec(f"dev=vfs.rom_ioctl(2,{rom_id})") |
| 502 | + has_object = state.transport.eval("hasattr(dev,'ioctl')") |
| 503 | + if has_object: |
| 504 | + rom_block_count = state.transport.eval("dev.ioctl(4,0)") |
| 505 | + rom_block_size = state.transport.eval("dev.ioctl(5,0)") |
| 506 | + rom_size = rom_block_count * rom_block_size |
| 507 | + print( |
| 508 | + f"ROMFS{rom_id} partition has size {rom_size} bytes ({rom_block_count} blocks of {rom_block_size} bytes each)" |
| 509 | + ) |
| 510 | + else: |
| 511 | + rom_size = state.transport.eval("len(dev)") |
| 512 | + print(f"ROMFS{rom_id} partition has size {rom_size} bytes") |
| 513 | + romfs = state.transport.eval("bytes(memoryview(dev)[:12])") |
| 514 | + print(f" Raw contents: {romfs.hex(':')} ...") |
| 515 | + if not romfs.startswith(b"\xd2\xcd\x31"): |
| 516 | + print(" Not a valid ROMFS") |
| 517 | + else: |
| 518 | + size = 0 |
| 519 | + for value in romfs[3:]: |
| 520 | + size = (size << 7) | (value & 0x7F) |
| 521 | + if not value & 0x80: |
| 522 | + break |
| 523 | + print(f" ROMFS image size: {size}") |
| 524 | + |
| 525 | + |
| 526 | +def _do_romfs_build(state, args): |
| 527 | + state.did_action() |
| 528 | + |
| 529 | + if args.path is None: |
| 530 | + raise CommandError("romfs build: source path not given") |
| 531 | + |
| 532 | + input_directory = args.path |
| 533 | + |
| 534 | + if args.output is None: |
| 535 | + output_file = input_directory + ".romfs" |
| 536 | + else: |
| 537 | + output_file = args.output |
| 538 | + |
| 539 | + romfs = make_romfs(input_directory, mpy_cross=args.mpy) |
| 540 | + |
| 541 | + print(f"Writing {len(romfs)} bytes to output file {output_file}") |
| 542 | + with open(output_file, "wb") as f: |
| 543 | + f.write(romfs) |
| 544 | + |
| 545 | + |
| 546 | +def _do_romfs_deploy(state, args): |
| 547 | + state.ensure_raw_repl() |
| 548 | + state.did_action() |
| 549 | + transport = state.transport |
| 550 | + |
| 551 | + if args.path is None: |
| 552 | + raise CommandError("romfs deploy: source path not given") |
| 553 | + |
| 554 | + rom_id = args.partition |
| 555 | + romfs_filename = args.path |
| 556 | + |
| 557 | + # Read in or create the ROMFS filesystem image. |
| 558 | + if romfs_filename.endswith(".romfs"): |
| 559 | + with open(romfs_filename, "rb") as f: |
| 560 | + romfs = f.read() |
| 561 | + else: |
| 562 | + romfs = make_romfs(romfs_filename, mpy_cross=args.mpy) |
| 563 | + print(f"Image size is {len(romfs)} bytes") |
| 564 | + |
| 565 | + # Detect the ROMFS partition and get its associated device. |
| 566 | + state.transport.exec("import vfs") |
| 567 | + if not state.transport.eval("hasattr(vfs,'rom_ioctl')"): |
| 568 | + raise CommandError("ROMFS is not enabled on this device") |
| 569 | + transport.exec(f"dev=vfs.rom_ioctl(2,{rom_id})") |
| 570 | + if transport.eval("isinstance(dev,int) and dev<0"): |
| 571 | + raise CommandError(f"ROMFS{rom_id} partition not found on device") |
| 572 | + has_object = transport.eval("hasattr(dev,'ioctl')") |
| 573 | + if has_object: |
| 574 | + rom_block_count = transport.eval("dev.ioctl(4,0)") |
| 575 | + rom_block_size = transport.eval("dev.ioctl(5,0)") |
| 576 | + rom_size = rom_block_count * rom_block_size |
| 577 | + print( |
| 578 | + f"ROMFS{rom_id} partition has size {rom_size} bytes ({rom_block_count} blocks of {rom_block_size} bytes each)" |
| 579 | + ) |
| 580 | + else: |
| 581 | + rom_size = transport.eval("len(dev)") |
| 582 | + print(f"ROMFS{rom_id} partition has size {rom_size} bytes") |
| 583 | + |
| 584 | + # Check if ROMFS filesystem image will fit in the target partition. |
| 585 | + if len(romfs) > rom_size: |
| 586 | + print("ROMFS image is too big for the target partition") |
| 587 | + sys.exit(1) |
| 588 | + |
| 589 | + # Prepare ROMFS partition for writing. |
| 590 | + print(f"Preparing ROMFS{rom_id} partition for writing") |
| 591 | + transport.exec("import vfs\ntry:\n vfs.umount('/rom')\nexcept:\n pass") |
| 592 | + chunk_size = 4096 |
| 593 | + if has_object: |
| 594 | + for offset in range(0, len(romfs), rom_block_size): |
| 595 | + transport.exec(f"dev.ioctl(6,{offset // rom_block_size})") |
| 596 | + chunk_size = min(chunk_size, rom_block_size) |
| 597 | + else: |
| 598 | + rom_min_write = transport.eval(f"vfs.rom_ioctl(3,{rom_id},{len(romfs)})") |
| 599 | + chunk_size = max(chunk_size, rom_min_write) |
| 600 | + |
| 601 | + # Detect capabilities of the device to use the fastest method of transfer. |
| 602 | + has_bytes_fromhex = transport.eval("hasattr(bytes,'fromhex')") |
| 603 | + try: |
| 604 | + transport.exec("from binascii import a2b_base64") |
| 605 | + has_a2b_base64 = True |
| 606 | + except TransportExecError: |
| 607 | + has_a2b_base64 = False |
| 608 | + try: |
| 609 | + transport.exec("from io import BytesIO") |
| 610 | + transport.exec("from deflate import DeflateIO,RAW") |
| 611 | + has_deflate_io = True |
| 612 | + except TransportExecError: |
| 613 | + has_deflate_io = False |
| 614 | + |
| 615 | + # Deploy the ROMFS filesystem image to the device. |
| 616 | + for offset in range(0, len(romfs), chunk_size): |
| 617 | + romfs_chunk = romfs[offset : offset + chunk_size] |
| 618 | + romfs_chunk += bytes(chunk_size - len(romfs_chunk)) |
| 619 | + if has_deflate_io: |
| 620 | + # Needs: binascii.a2b_base64, io.BytesIO, deflate.DeflateIO. |
| 621 | + romfs_chunk_compressed = zlib.compress(romfs_chunk, wbits=-9) |
| 622 | + buf = binascii.b2a_base64(romfs_chunk_compressed).strip() |
| 623 | + transport.exec(f"buf=DeflateIO(BytesIO(a2b_base64({buf})),RAW,9).read()") |
| 624 | + elif has_a2b_base64: |
| 625 | + # Needs: binascii.a2b_base64. |
| 626 | + buf = binascii.b2a_base64(romfs_chunk) |
| 627 | + transport.exec(f"buf=a2b_base64({buf})") |
| 628 | + elif has_bytes_fromhex: |
| 629 | + # Needs: bytes.fromhex. |
| 630 | + buf = romfs_chunk.hex() |
| 631 | + transport.exec(f"buf=bytes.fromhex('{buf}')") |
| 632 | + else: |
| 633 | + # Needs nothing special. |
| 634 | + transport.exec("buf=" + repr(romfs_chunk)) |
| 635 | + print(f"\rWriting at offset {offset}", end="") |
| 636 | + if has_object: |
| 637 | + transport.exec( |
| 638 | + f"dev.writeblocks({offset // rom_block_size},buf,{offset % rom_block_size})" |
| 639 | + ) |
| 640 | + else: |
| 641 | + transport.exec(f"vfs.rom_ioctl(4,{rom_id},{offset},buf)") |
| 642 | + |
| 643 | + # Complete writing. |
| 644 | + if not has_object: |
| 645 | + transport.eval(f"vfs.rom_ioctl(5,{rom_id})") |
| 646 | + |
| 647 | + print() |
| 648 | + print("ROMFS image deployed") |
| 649 | + |
| 650 | + |
| 651 | +def do_romfs(state, args): |
| 652 | + if args.command[0] == "query": |
| 653 | + _do_romfs_query(state, args) |
| 654 | + elif args.command[0] == "build": |
| 655 | + _do_romfs_build(state, args) |
| 656 | + elif args.command[0] == "deploy": |
| 657 | + _do_romfs_deploy(state, args) |
| 658 | + else: |
| 659 | + raise CommandError( |
| 660 | + f"romfs: '{args.command[0]}' is not a command; pass romfs --help for a list" |
| 661 | + ) |
0 commit comments