Example #1
0
def log_summary(log_function, spi_flash):
    """ Print a quick summary of the detected flash; for use in non-info commands. """

    if spi_flash.manufacturer_id in SPIFlash.JEDEC_MANUFACTURERS:
        log_function("Detected a {} {} ({}).".format(
            spi_flash.manufacturer, spi_flash.part,
            human_readable_size(spi_flash.maximum_address + 1)))
    else:
        log_function("Detected an unknown flash chip.")
Example #2
0
def print_flash_info(spi_flash, log_function, log_error, args):
    """ Function that prints the relevant flash chip's information to the console. """

    capacity = spi_flash.maximum_address + 1

    if args.bypass_jedec:
        log_function("Taking it on faith that an SPI flash is there.")
    else:
        log_function("SPI flash detected:")

    log_function("\tManufacturer: {} (0x{:02x})".format(
        spi_flash.manufacturer, spi_flash.manufacturer_id))
    log_function("\tPart number: {} (0x{:02x})".format(spi_flash.part,
                                                       spi_flash.part_id))
    log_function("\tCapacity: {} ({})".format(
        human_readable_size(capacity),
        human_readable_size(capacity * 8, unit='b')))
    log_function("\tPage size: {} B".format(spi_flash.page_size))
    log_function('')
Example #3
0
def dump_chip(spi_flash, log_function, log_error, args):
    """ Dump the contents of the provided chip. """

    log_summary(log_function, spi_flash)

    # Figure out how much we're going to read.
    length_to_read = args.length if args.length else (
        spi_flash.maximum_address + 1)

    # Truncate the read size to the flash's length, if necessary.
    flash_size = spi_flash.maximum_address + 1
    if (args.address + length_to_read) > flash_size:
        length_to_read = flash_size - args.address

        log_error(
            "This operation would read past the end of flash -- truncating to {}."
            .format(human_readable_size(length_to_read)))

    if args.filename is None:
        log_error(
            "You must provide a filename to write the captured data to!\n")
        sys.exit(-1)

    # Finally, run the flash operation.
    try:
        log_function("Reading {}.\n".format(
            human_readable_size(length_to_read)))
        run_flash_operation(spi_flash.dump,
                            spi_flash.page_size,
                            length_to_read,
                            args,
                            args.filename,
                            args.address,
                            length_to_read,
                            auto_truncate=args.truncate)
        log_function("Read complete.\n")
    except FileNotFoundError:
        log_error("Cannot open {} for writing.\n".format(args.filename))
Example #4
0
def program_chip(spi_flash, log_function, log_error, args):
    """ Programs data to the given chip. """

    log_summary(log_function, spi_flash)
    be_cautious(args, log_error, operation="erase")

    if args.filename is None:
        log_error(
            "You must provide a filename that will provided the data to write!\n"
        )
        sys.exit(-1)

    try:
        # Grab the size of the file to read.
        if args.length is None:
            length_to_write = None if args.filename == '-' else os.path.getsize(
                args.filename)

        # Check that we can write the relevant file into flash.
        flash_size = spi_flash.maximum_address + 1
        if length_to_write and ((length_to_write + args.address) > flash_size):
            log_error(
                "This operation would write past the end of flash. If you'd like to trim your file, try --length.\n"
            )
            sys.exit(-3)

        log_function("Writing {}.\n".format(
            human_readable_size(length_to_write)))
        run_flash_operation(spi_flash.upload,
                            spi_flash.page_size,
                            length_to_write,
                            args,
                            args.filename,
                            args.address,
                            length_to_write,
                            erase_first=args.autoerase)
        log_function("Write complete.\n")

    except FileNotFoundError:
        log_error("Cannot open {} for reading.\n".format(args.filename))