Beispiel #1
0
def main(argv):

    parser = util.ArgumentParser(usage='%(prog)s [options]')
    parser.add_argument("--device", help="greaseweazle device name")
    parser.add_argument("--bootloader",
                        action="store_true",
                        help="display bootloader info (F7 only)")
    parser.description = description
    parser.prog += ' ' + argv[1]
    args = parser.parse_args(argv[2:])

    print_info_line('Host Tools', 'v%d.%d' % (version.major, version.minor))

    print('Greaseweazle:')

    try:
        usb = util.usb_open(args.device, mode_check=False)
    except serial.SerialException:
        print('  Not found')
        sys.exit(0)

    mode_switched = (usb.jumperless_update
                     and usb.update_mode != args.bootloader
                     and not (usb.update_mode and usb.update_jumpered))
    if mode_switched:
        usb = util.usb_reopen(usb, args.bootloader)

    port = usb.port_info

    if port.device:
        print_info_line('Device', port.device, tab=2)

    try:
        model = model_id[usb.hw_model][usb.hw_submodel]
    except KeyError:
        model = 'Unknown (0x%02X%02X)' % (usb.hw_model, usb.hw_submodel)
    print_info_line('Model', model, tab=2)

    fwver = 'v%d.%d' % (usb.major, usb.minor)
    if usb.update_mode:
        fwver += ' (Update Bootloader)'
    print_info_line('Firmware', fwver, tab=2)

    print_info_line('Serial',
                    port.serial_number if port.serial_number else 'Unknown',
                    tab=2)

    try:
        speed = speed_id[usb.usb_speed]
    except KeyError:
        speed = 'Unknown (0x%02X)' % usb.usb_speed
    print_info_line('USB Rate', speed, tab=2)

    if mode_switched:
        usb = util.usb_reopen(usb, not args.bootloader)
Beispiel #2
0
def main(argv):

    parser = util.ArgumentParser(allow_abbrev=False,
                                 usage='%(prog)s [options] [file]')
    parser.add_argument("file", nargs="?", help="update filename")
    parser.add_argument("--device", help="device name (COM/serial port)")
    parser.add_argument("--force",
                        action="store_true",
                        help="force update even if firmware is older")
    parser.add_argument("--bootloader",
                        action="store_true",
                        help="update the bootloader (use with caution!)")
    parser.description = description
    parser.prog += ' ' + argv[1]
    args = parser.parse_args(argv[2:])

    if args.file is None:
        args.file, dat = download_latest()
    else:
        with open(args.file, "rb") as f:
            dat = f.read()

    try:
        usb = util.usb_open(args.device, mode_check=False)
        dat_version, dat = extract_update(usb, dat, args)
        print("Updating %s to v%u.%u..." %
              ("Bootloader" if args.bootloader else "Main Firmware",
               *dat_version))
        if not args.force and (usb.can_mode_switch
                               or args.bootloader == usb.update_mode):
            if args.bootloader != usb.update_mode:
                usb = util.usb_reopen(usb, is_update=args.bootloader)
                error.check(args.bootloader == usb.update_mode,
                            'Device did not mode switch as requested')
            if usb.version >= dat_version:
                if usb.update_mode and usb.can_mode_switch:
                    usb = util.usb_reopen(usb, is_update=False)
                raise error.Fatal('Device is running v%d.%d (>= v%d.%d). '
                                  'Use --force to update anyway.' %
                                  (usb.version + dat_version))
        usb = util.usb_mode_check(usb, is_update=not args.bootloader)
        update_firmware(usb, dat, args)
        if usb.update_mode and usb.can_mode_switch:
            util.usb_reopen(usb, is_update=False)
    except USB.CmdError as err:
        if err.code == USB.Ack.OutOfSRAM and args.bootloader:
            # Special warning for Low-Density F1 devices. The new bootloader
            # cannot be fully buffered in the limited RAM available.
            print("ERROR: Bootloader update unsupported on this device "
                  "(insufficient SRAM)")
        elif err.code == USB.Ack.OutOfFlash and not args.bootloader:
            print("ERROR: New firmware is too large for this device "
                  "(insufficient Flash memory)")
        else:
            print("Command Failed: %s" % err)
Beispiel #3
0
def update_firmware(usb, args):

    filename = args.file
    if filename == "auto":
        # Get the absolute path to the root Greaseweazle folder.
        path = os.path.dirname(os.path.abspath(__file__))
        for _ in range(3):
            path = os.path.join(path, os.pardir)
        path = os.path.normpath(path)
        filename = os.path.join(
            path, "Greaseweazle-v%d.%d.upd" % (version.major, version.minor))

    # Read the entire update catalogue.
    with open(filename, "rb") as f:
        dat = f.read()

    # Search the catalogue for a match on our Weazle's hardware type.
    while dat:
        upd_len, hw_type = struct.unpack("<2H", dat[:4])
        if hw_type == usb.hw_type:
            # Match: Pull out the embedded update file.
            dat = dat[4:upd_len + 4]
            break
        # Skip to the next catalogue entry.
        dat = dat[upd_len + 4:]

    if not dat:
        print("%s: No match for hardware type %x" % (filename, usb.hw_type))
        return

    # Check the matching update file's footer.
    sig, maj, min, hw_type = struct.unpack("<2s2BH", dat[-8:-2])
    if len(dat) & 3 != 0 or sig != b'GW' or hw_type != usb.hw_type:
        print("%s: Bad update file" % (filename))
        return
    crc16 = crcmod.predefined.Crc('crc-ccitt-false')
    crc16.update(dat)
    if crc16.crcValue != 0:
        print("%s: Bad CRC" % (filename))
        return

    # Perform the update.
    print("Updating to v%u.%u..." % (maj, min))
    ack = usb.update_firmware(dat)
    if ack != 0:
        print("** UPDATE FAILED: Please retry!")
        return
    print("Done.")

    if usb.hw_type == 7:
        util.usb_reopen(usb, is_update=False)
    else:
        print("** Disconnect Greaseweazle and remove the Programming Jumper.")
Beispiel #4
0
def update_firmware(usb, args):

    # Read the entire update catalogue.
    with open(args.file, "rb") as f:
        dat = f.read()

    # Search the catalogue for a match on our Weazle's hardware type.
    while dat:
        upd_len, hw_type = struct.unpack("<2H", dat[:4])
        if hw_type == usb.hw_type:
            # Match: Pull out the embedded update file.
            dat = dat[4:upd_len + 4]
            break
        # Skip to the next catalogue entry.
        dat = dat[upd_len + 4:]

    if not dat:
        print("%s: No match for hardware type %x" % (args.file, usb.hw_type))
        return

    # Check the matching update file's footer.
    sig, maj, min, hw_type = struct.unpack("<2s2BH", dat[-8:-2])
    if len(dat) & 3 != 0 or sig != b'GW' or hw_type != usb.hw_type:
        print("%s: Bad update file" % (args.file))
        return
    crc16 = crcmod.predefined.Crc('crc-ccitt-false')
    crc16.update(dat)
    if crc16.crcValue != 0:
        print("%s: Bad CRC" % (args.file))
        return

    # Perform the update.
    print("Updating to v%u.%u..." % (maj, min))
    ack = usb.update_firmware(dat)
    if ack != 0:
        print("** UPDATE FAILED: Please retry!")
        return
    print("Done.")

    if usb.hw_type == 7:
        util.usb_reopen(usb, is_update=False)
    else:
        print("** Disconnect Greaseweazle and remove the Programming Jumper.")
Beispiel #5
0
def update_firmware(usb, args):

    req_type = b'BL' if args.bootloader else b'GW'

    filename = args.file
    if filename is None:
        # Get the absolute path to the root Greaseweazle folder.
        path = os.path.dirname(os.path.abspath(__file__))
        for _ in range(3):
            path = os.path.join(path, os.pardir)
        path = os.path.normpath(path)
        filename = os.path.join(
            path, "Greaseweazle-v%d.%d.upd" % (version.major, version.minor))

    # Read and verify the entire update catalogue.
    with open(filename, "rb") as f:
        dat = f.read()
    if struct.unpack('4s', dat[:4])[0] != b'GWUP':
        print('%s: Not a valid UPD file' % (filename))
        return
    crc32 = crcmod.predefined.Crc('crc-32-mpeg')
    crc32.update(dat)
    if crc32.crcValue != 0:
        print('%s: UPD file is corrupt' % (filename))
        return
    dat = dat[4:-4]

    # Search the catalogue for a match on our Weazle's hardware type.
    while dat:
        upd_len, hw_model = struct.unpack("<2H", dat[:4])
        upd_type, major, minor = struct.unpack("2s2B",
                                               dat[upd_len - 4:upd_len])
        if ((hw_model, upd_type, major,
             minor) == (usb.hw_model, req_type, version.major, version.minor)):
            # Match: Pull out the embedded update file.
            dat = dat[4:upd_len + 4]
            break
        # Skip to the next catalogue entry.
        dat = dat[upd_len + 4:]

    if not dat:
        print("%s: F%u v%u.%u %s update not found" %
              (filename, usb.hw_model, version.major, version.minor,
               'bootloader' if args.bootloader else 'firmware'))
        return

    # Check the matching update file's footer.
    sig, maj, min, hw_model = struct.unpack("<2s2BH", dat[-8:-2])
    if len(dat) & 3 != 0 or sig != req_type or hw_model != usb.hw_model:
        print("%s: Bad update file" % (filename))
        return
    crc16 = crcmod.predefined.Crc('crc-ccitt-false')
    crc16.update(dat)
    if crc16.crcValue != 0:
        print("%s: Bad CRC" % (filename))
        return

    # Perform the update.
    print("Updating %s to v%u.%u..." %
          ("Bootloader" if args.bootloader else "Main Firmware", maj, min))
    if args.bootloader:
        ack = usb.update_bootloader(dat)
        if ack != 0:
            print("""\
** UPDATE FAILED: Please retry immediately or your Weazle may need
        full reflashing via a suitable programming adapter!""")
            return
        print("Done.")
    else:
        ack = usb.update_firmware(dat)
        if ack != 0:
            print("** UPDATE FAILED: Please retry!")
            return
        print("Done.")

        if usb.jumperless_update:
            util.usb_reopen(usb, is_update=False)
        else:
            print("** Disconnect Greaseweazle and remove the Update Jumper")
Beispiel #6
0
def main(argv):

    parser = util.ArgumentParser(usage='%(prog)s [options]')
    parser.add_argument("--device", help="device name (COM/serial port)")
    parser.add_argument("--bootloader", action="store_true",
                        help="display bootloader info (F7 only)")
    parser.description = description
    parser.prog += ' ' + argv[1]
    args = parser.parse_args(argv[2:])

    print_info_line('Host Tools', 'v%d.%d' % (version.major, version.minor))

    print('Device:')

    try:
        usb = util.usb_open(args.device, mode_check=False)
    except serial.SerialException:
        print('  Not found')
        sys.exit(0)

    mode_switched = usb.can_mode_switch and usb.update_mode != args.bootloader
    if mode_switched:
        usb = util.usb_reopen(usb, args.bootloader)
        
    port = usb.port_info

    if port.device:
        print_info_line('Port', port.device, tab=2)

    try:
        model = model_id[usb.hw_model][usb.hw_submodel]
        if usb.hw_model != 8:
            model = 'Greaseweazle ' + model
    except KeyError:
        model = 'Unknown (0x%02X%02X)' % (usb.hw_model, usb.hw_submodel)
    print_info_line('Model', model, tab=2)

    fwver = 'v%d.%d' % (usb.major, usb.minor)
    if usb.update_mode:
        fwver += ' (Update Bootloader)'
    print_info_line('Firmware', fwver, tab=2)

    print_info_line('Serial', port.serial_number if port.serial_number
                    else 'Unknown', tab=2)

    try:
        speed = speed_id[usb.usb_speed]
    except KeyError:
        speed = 'Unknown (0x%02X)' % usb.usb_speed
    print_info_line('USB Rate', speed, tab=2)

    usb_update_mode, usb_version = usb.update_mode, usb.version

    if mode_switched:
        usb = util.usb_reopen(usb, not args.bootloader)

    if not usb_update_mode:
        latest_version = latest_firmware()
        if latest_version > usb_version:
            print('\n*** New firmware v%d.%d is available' % latest_version)
            util.print_update_instructions(usb)
Beispiel #7
0
def main(argv):

    epilog = """\
Examples:
  gw update
  gw update --force v1.0
  gw update greaseweazle-firmware-v1.0.upd"""

    parser = util.ArgumentParser(allow_abbrev=False,
                                 usage='%(prog)s [options]',
                                 epilog=epilog)
    parser.add_argument("--file", help="use specified update file")
    parser.add_argument("--tag", help="use specified GitHub release tag")
    parser.add_argument("--device", help="device name (COM/serial port)")
    parser.add_argument("--force",
                        action="store_true",
                        help="force update even if firmware is older")
    parser.add_argument("--bootloader",
                        action="store_true",
                        help="update the bootloader (use with caution!)")
    parser.description = description
    parser.prog += ' ' + argv[1]
    args = parser.parse_args(argv[2:])

    error.check(args.tag is None or args.file is None,
                "File and tag both specified. Only one is allowed.")

    if args.tag is not None:
        args.file, dat = download_by_tag(args.tag)
    elif args.file is None:
        args.file, dat = download_latest()
    else:
        with open(args.file, "rb") as f:
            dat = f.read()

    try:
        usb = util.usb_open(args.device, mode_check=False)
        dat_version, dat = extract_update(usb, dat, args)
        print("Updating %s to version %u.%u..." %
              ("Bootloader" if args.bootloader else "Main Firmware",
               *dat_version))
        if not args.force and (usb.can_mode_switch
                               or args.bootloader == usb.update_mode):
            if args.bootloader != usb.update_mode:
                usb = util.usb_reopen(usb, is_update=args.bootloader)
                error.check(args.bootloader == usb.update_mode,
                            'Device did not mode switch as requested')
            if usb.version >= dat_version:
                if usb.update_mode and usb.can_mode_switch:
                    usb = util.usb_reopen(usb, is_update=False)
                raise SkipUpdate('''\
                    Device is already running version %d.%d.
                    Use --force to update anyway.''' % usb.version)
        usb = util.usb_mode_check(usb, is_update=not args.bootloader)
        update_firmware(usb, dat, args)
        if usb.update_mode and usb.can_mode_switch:
            util.usb_reopen(usb, is_update=False)
    except USB.CmdError as err:
        if err.code == USB.Ack.OutOfSRAM and args.bootloader:
            # Special warning for Low-Density F1 devices. The new bootloader
            # cannot be fully buffered in the limited RAM available.
            print("ERROR: Bootloader update unsupported on this device "
                  "(insufficient SRAM)")
        elif err.code == USB.Ack.OutOfFlash and not args.bootloader:
            print("ERROR: New firmware is too large for this device "
                  "(insufficient Flash memory)")
        else:
            print("Command Failed: %s" % err)
    except SkipUpdate as exc:
        print("** SKIPPING UPDATE:")
        print(textwrap.dedent(str(exc)))