def main():
    bootloader = BootLoader()
    bootloader.start()
    filename = time.strftime("eeprom-backup-%Y%m%d-%H%M%S.bin", time.localtime())
    print("Reading 1K EEPROM data...")
    bootloader.write(b"A\x00\x00")
    bootloader.read(1)
    bootloader.write(b"g\x04\x00E")
    eepromdata = bytearray(bootloader.read(1024))
    print(f'saving 1K EEPROM data to "{filename}"')
    f = open(filename, "wb")
    f.write(eepromdata)
    f.close()
    print("Done")
    bootloader.exit()
    delayed_exit()
Beispiel #2
0
def write_flash(pagenumber, flashdata):
    global verifyAfterWrite
    bootloader = BootLoader()
    bootloader.start()

    # check version
    if bootloader.get_version() < 13:
        print("Bootloader has no flash cart support\nWrite aborted!")
        delayed_exit()

    # detect flash cart
    jedec_id = bootloader.get_jedec_id()
    if jedec_id[0] in manufacturers.keys():
        manufacturer = manufacturers[jedec_id[0]]
    else:
        manufacturer = "unknown"
    capacity = 1 << jedec_id[2]
    print(f"\nFlash cart JEDEC ID    : {jedec_id[0]:02X}{jedec_id[1]:02X}{jedec_id[2]:02X}")
    print(f"Flash cart Manufacturer: {manufacturer}")
    print(f"Flash cart capacity    : {capacity // 1024} Kbyte\n")

    oldtime = time.time()
    # when starting partially in a block, preserve the beginning of old block data
    if pagenumber % PAGES_PER_BLOCK:
        blocklen = pagenumber % PAGES_PER_BLOCK * PAGESIZE
        blockaddr = pagenumber // PAGES_PER_BLOCK * PAGES_PER_BLOCK
        # read partial block data start
        bootloader.write(bytearray([ord("A"), blockaddr >> 8, blockaddr & 0xFF]))
        bootloader.read(1)
        bootloader.write(bytearray([ord("g"), (blocklen >> 8) & 0xFF, blocklen & 0xFF, ord("C")]))
        flashdata = bootloader.read(blocklen) + flashdata
        pagenumber = blockaddr

    # when ending partially in a block, preserve the ending of old block data
    if len(flashdata) % BLOCKSIZE:
        blocklen = BLOCKSIZE - len(flashdata) % BLOCKSIZE
        blockaddr = pagenumber + len(flashdata) // PAGESIZE
        # read partial block data end
        bootloader.write(bytearray([ord("A"), blockaddr >> 8, blockaddr & 0xFF]))
        bootloader.read(1)
        bootloader.write(bytearray([ord("g"), (blocklen >> 8) & 0xFF, blocklen & 0xFF, ord("C")]))
        flashdata += bootloader.read(blocklen)

    # write to flash cart
    blocks = len(flashdata) // BLOCKSIZE
    for block in range(blocks):
        if block & 1:
            bootloader.write(b"x\xC0")  # RGB LED OFF, buttons disabled
        else:
            bootloader.write(b"x\xC2")  # RGB LED RED, buttons disabled
        bootloader.read(1)
        sys.stdout.write(f"\rWriting block {block + 1}/{blocks}")
        blockaddr = pagenumber + block * BLOCKSIZE // PAGESIZE
        blocklen = BLOCKSIZE
        # write block
        bootloader.write(bytearray([ord("A"), blockaddr >> 8, blockaddr & 0xFF]))
        bootloader.read(1)
        bootloader.write(bytearray([ord("B"), (blocklen >> 8) & 0xFF, blocklen & 0xFF, ord("C")]))
        bootloader.write(flashdata[block * BLOCKSIZE: block * BLOCKSIZE + blocklen])
        bootloader.read(1)
        if verifyAfterWrite:
            bootloader.write(bytearray([ord("A"), blockaddr >> 8, blockaddr & 0xFF]))
            bootloader.read(1)
            bootloader.write(bytearray([ord("g"), (blocklen >> 8) & 0xFF, blocklen & 0xFF, ord("C")]))
            if bootloader.read(blocklen) != flashdata[block * BLOCKSIZE: block * BLOCKSIZE + blocklen]:
                print(" verify failed!\n\nWrite aborted.")
                break

    # write complete
    bootloader.write(b"x\x44")  # RGB LED GREEN, buttons enabled
    bootloader.read(1)
    time.sleep(0.5)
    bootloader.exit()
    print(f"\n\nDone in {round(time.time() - oldtime, 2)} seconds")
def main():
    caterina_overwrite = False

    flash_addr = 0
    flash_data = bytearray(chr(0xFF) * 32768)
    flash_page = 1
    flash_page_count = 0
    flash_page_used = [False] * 256

    ################################################################################

    bootloader = BootLoader()
    bootloader.start()

    # Erase
    print("\nErasing sketch startup page")
    bootloader.write(b"A\x00\x00")  # select page 0
    bootloader.read(1)
    bootloader.write(
        b"B\x00\x00F")  # writing 0 length block will erase page only
    bootloader.read(1)
    bootloader.write(b"A\x00\x00")  # select page 0
    bootloader.read(1)
    bootloader.write(b"g\x00\x80F")  # read 128 byte page
    if bytearray(bootloader.read(128)) == bytearray("\xff" * 128):
        print("\nErase successful")
    else:
        print("\nErase failed")
    bootloader.exit()
    delayed_exit()
Beispiel #4
0
def main():
    bootloader = BootLoader()
    bootloader.start()

    # check version
    if bootloader.get_version() < 13:
        print("Bootloader has no flash cart support\nWrite aborted!")
        delayed_exit()

    # detect flash cart
    jedec_id = bootloader.get_jedec_id()
    if jedec_id[0] in manufacturers.keys():
        manufacturer = manufacturers[jedec_id[0]]
    else:
        manufacturer = "unknown"
    capacity = 1 << jedec_id[2]
    print(
        f"\nFlash cart JEDEC ID    : {jedec_id[0]:02X}{jedec_id[1]:02X}{jedec_id[2]:02X}"
    )
    print(f"Flash cart Manufacturer: {manufacturer}")
    print(f"Flash cart capacity    : {capacity // 1024} Kbyte\n")

    filename = time.strftime("flashcart-backup-image-%Y%m%d-%H%M%S.bin",
                             time.localtime())
    print(f'Writing flash image to file: "{filename}"\n')

    oldtime = time.time()
    blocks = capacity // BLOCKSIZE
    with open(filename, "wb") as binfile:
        for block in range(0, blocks):
            if block & 1:
                bootloader.write(b"x\xC0")  # RGB BLUE OFF, buttons disabled
            else:
                bootloader.write(b"x\xC1")  # RGB BLUE RED, buttons disabled
            bootloader.read(1)
            sys.stdout.write(f"\rReading block {block + 1}/{blocks}")

            blockaddr = block * BLOCKSIZE // PAGESIZE

            bootloader.write(b"A")
            bootloader.write(bytearray([blockaddr >> 8, blockaddr & 0xFF]))
            bootloader.read(1)

            blocklen = BLOCKSIZE

            bootloader.write(b"g")
            bootloader.write(
                bytearray([(blocklen >> 8) & 0xFF, blocklen & 0xFF]))

            bootloader.write(b"C")
            contents = bootloader.read(blocklen)
            binfile.write(contents)

    bootloader.write(b"x\x44")  # RGB LED GREEN, buttons enabled
    bootloader.read(1)
    time.sleep(0.5)
    bootloader.exit()
    print(f"\n\nDone in {round(time.time() - oldtime, 2)} seconds")
    delayed_exit()
                i:i +
                4] == b'\x81\xef\x85\xb9':  # Arduboy core init RXLED port
            flash_data[i] = 0x80
        elif flash_data[
                i:i +
                4] == b'\x84\xe2\x8b\xb9':  # Arduboy core init TXLED port
            flash_data[i + 1] = 0xE0

# check  for data in catarina bootloader area
for i in range(256):
    if flash_page_used[i]:
        flash_page_count += 1
        if i >= 224:
            caterina_overwrite = True

bootloader = BootLoader()
bootloader.start()
# test if bootloader can and will be overwritten by hex file
bootloader.write(b"V")  # get bootloader software version
if bootloader.read(2) == b"10":  # original caterina 1.0 bootloader
    bootloader.write(b"r")  # read lock bits
    if (ord(bootloader.read(1)) & 0x10 != 0) and caterina_overwrite:
        print(
            "\nThis upload will most likely corrupt the bootloader. Upload aborted."
        )
        bootloader.exit()
        delayed_exit()

# Flash
print(
    f"\nFlashing {flash_page_count * 128} bytes. ({flash_page_count} flash pages)"
def main():
    if len(sys.argv) != 2:
        print(f"\nUsage: {os.path.basename(sys.argv[0])} eepromfile.bin\n")
        delayed_exit()

    filename = sys.argv[1]
    if not os.path.isfile(filename):
        print(f"File not found. [{filename}]")
        delayed_exit()

    print(f'Reading EEPROM data from file "{filename}"')
    f = open(filename, "rb")
    eepromdata = bytearray(f.read())
    f.close()

    if len(eepromdata) != 1024:
        print("File does not contain 1K (1024 bytes) of EEPROM data\nRestore aborted")
        delayed_exit()

    # restore
    bootloader = BootLoader()
    bootloader.start()
    print("Restoring EEPROM data...")
    bootloader.write(b"A\x00\x00")
    bootloader.read(1)
    bootloader.write(b"B\x04\x00E")
    bootloader.write(eepromdata)
    bootloader.read(1)
    bootloader.exit()
    print("Done")
    delayed_exit()
Beispiel #7
0
def main():
    bootloader = BootLoader()
    bootloader.start()
    filename = time.strftime("sketch-backup-%Y%m%d-%H%M%S.bin",
                             time.localtime())
    print("Reading sketch...")
    bootloader.write(b"A\x00\x00")
    bootloader.read(1)
    bootloader.write(b"g\x70\x00F")
    backupdata = bytearray(bootloader.read(0x7000))
    print(f'saving sketch to "{filename}"')
    f = open(filename, "wb")
    f.write(backupdata)
    f.close()
    print("Done")
    bootloader.exit()
    delayed_exit()
def main():
    bootloader = BootLoader()
    bootloader.start()
    print("Erasing EEPROM data...")
    bootloader.write(b"A\x00\x00")
    bootloader.read(1)
    bootloader.write(b"B\x04\x00E")
    bootloader.write(bytearray("\xFF" * 1024))
    bootloader.read(1)
    bootloader.exit()
    print("Erase complete.")
    delayed_exit()