|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# This file is part of GreatFET |
| 4 | +# |
| 5 | + |
| 6 | +from __future__ import print_function |
| 7 | + |
| 8 | +import os |
| 9 | +import sys |
| 10 | +import errno |
| 11 | +import argparse |
| 12 | + |
| 13 | +from tqdm import tqdm |
| 14 | + |
| 15 | +from greatfet import GreatFET |
| 16 | +from greatfet.errors import DeviceNotFoundError |
| 17 | +from greatfet.utils import from_eng_notation, human_readable_size, GreatFETArgumentParser |
| 18 | +from pygreat.comms import CommandFailureError |
| 19 | + |
| 20 | +# Import all of our known JTAG targets. |
| 21 | +from ..targets.jtag import * |
| 22 | +from ..interfaces.jtag import JTAGPatternError |
| 23 | + |
| 24 | + |
| 25 | +def print_chain_info(jtag, log_function, log_error, args): |
| 26 | + """ Command that prints information about devices connected to the scan chain to the console. """ |
| 27 | + |
| 28 | + log_function("Scanning for connected devices...") |
| 29 | + detected_devices = jtag.enumerate() |
| 30 | + |
| 31 | + # If devices exist on the scan chain, print their information. |
| 32 | + supported_commands = {} |
| 33 | + if detected_devices: |
| 34 | + log_function("{} device{} detected on the scan chain:\n".format( |
| 35 | + len(detected_devices), 's' if len(detected_devices) > 1 else '')) |
| 36 | + |
| 37 | + for device in detected_devices: |
| 38 | + log_function(" {:08x} -- {}".format(device.idcode(), device.description())) |
| 39 | + commands = device.supported_console_commands() |
| 40 | + |
| 41 | + # If the device has any associated console commands, add them to our list. |
| 42 | + if commands: |
| 43 | + supported_commands[device.idcode()] = commands |
| 44 | + |
| 45 | + log_function('') |
| 46 | + |
| 47 | + if supported_commands: |
| 48 | + log_function("The following commands are provided for working with these devices:") |
| 49 | + for idcode, commands in supported_commands.items(): |
| 50 | + log_function(" {:08x} -- {}".format(idcode, ', '.join(commands))) |
| 51 | + else: |
| 52 | + log_function("No console commands exist for these devices; perhaps 'jtag svf' would be useful?") |
| 53 | + |
| 54 | + log_function('') |
| 55 | + |
| 56 | + else: |
| 57 | + log_function("No devices found.\n") |
| 58 | + |
| 59 | + |
| 60 | +def play_svf_file(jtag, log_function, log_error, args): |
| 61 | + """ Command that prints the relevant flash chip's information to the console. """ |
| 62 | + |
| 63 | + if not args.filename: |
| 64 | + log_error("You must provide an SVF filename to play!\n") |
| 65 | + sys.exit(-1) |
| 66 | + |
| 67 | + try: |
| 68 | + jtag.play_svf_file(args.filename, log_function=log_function, error_log_function=log_error) |
| 69 | + except JTAGPatternError as e: |
| 70 | + # Our SVF player has already logged the error to stderr. |
| 71 | + log_error("") |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + |
| 76 | + commands = { |
| 77 | + 'scan': print_chain_info, |
| 78 | + 'svf': play_svf_file |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + # Set up a simple argument parser. |
| 83 | + parser = GreatFETArgumentParser(description="Utility for working with JTAG devices") |
| 84 | + parser.add_argument('command', choices=commands, help='the operation to complete') |
| 85 | + parser.add_argument('filename', metavar="[filename]", nargs='?', |
| 86 | + help='the filename to read from, for SVF playback') |
| 87 | + |
| 88 | + args = parser.parse_args() |
| 89 | + device = parser.find_specified_device() |
| 90 | + |
| 91 | + if args.command == 'scan': |
| 92 | + args.verbose = True |
| 93 | + elif args.filename == "-": |
| 94 | + args.verbose = False |
| 95 | + |
| 96 | + # Grab our log functions. |
| 97 | + log_function, log_error = parser.get_log_functions() |
| 98 | + |
| 99 | + # Execute the relevant command. |
| 100 | + command = commands[args.command] |
| 101 | + command(device.jtag, log_function, log_error, args) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == '__main__': |
| 105 | + main() |
0 commit comments