def main(): args = parser.parse_args() logging.basicConfig( format='%(asctime)s %(name)s: %(levelname)s: %(message)s', level=logging.DEBUG if args.debug else logging.INFO) try: os.makedirs(args.outdir) except OSError: pass comm = lglaf.autodetect_device() with closing(comm): lglaf.try_hello(comm) _logger.debug("Using Protocol version: 0x%x" % comm.protocol_version) with partitions.laf_open_disk(comm) as disk_fd: _logger.debug("Opened fd %d for disk", disk_fd) dump_partitions(comm, disk_fd, args.outdir, args.max_size * 1024, args.batch) if args.batch: print("#All finished") else: _logger.info("All finished!")
def main(): args = parser.parse_args() logging.basicConfig( format='%(asctime)s %(name)s: %(levelname)s: %(message)s', level=logging.DEBUG if args.debug else logging.INFO) comm = lglaf.autodetect_device() with closing(comm): lglaf.try_hello(comm) # Be careful: a too large read size will result in a hang while LAF # tries to read more data, requiring a reset. if args.size: offset = args.offset size = args.size else: offset = args.offset size = get_file_size(comm, args.file) if offset > size: _logger.warning( "Offset %d is larger than the detected size %d", offset, size) size -= offset if size > 0: _logger.debug("File size is %d", size) with laf_open_ro(comm, args.file) as file_fd: _logger.debug("Opened fd %d for file %s", file_fd, args.file) dump_file(comm, file_fd, args.output_file, size, offset) else: _logger.warning("Not a file or zero size, not writing file")
def main(): args = parser.parse_args() logging.basicConfig(format='%(name)s: %(levelname)s: %(message)s', level=logging.DEBUG if args.debug else logging.INFO) comm = lglaf.autodetect_device() with closing(comm): do_challenge_response(comm) if not args.skip_hello: lglaf.try_hello(comm)
def main(): args = parser.parse_args() logging.basicConfig(format='%(asctime)s %(name)s: %(levelname)s: %(message)s', level=logging.DEBUG if args.debug else logging.INFO) try: os.makedirs(args.outdir) except OSError: pass comm = lglaf.autodetect_device() with closing(comm): lglaf.try_hello(comm) with partitions.laf_open_disk(comm) as disk_fd: _logger.debug("Opened fd %d for disk", disk_fd) dump_partitions(comm, disk_fd, args.outdir, args.max_size * 1024)
def main(): args = parser.parse_args() logging.basicConfig(format='%(asctime)s %(name)s: %(levelname)s: %(message)s', level=logging.DEBUG if args.debug else logging.INFO) try: os.makedirs(args.outdir) except OSError: pass comm = lglaf.autodetect_device() with closing(comm): if not args.skip_hello: lglaf.try_hello(comm) with partitions.laf_open_disk(comm) as disk_fd: _logger.debug("Opened fd %d for disk", disk_fd) dump_partitions(comm, disk_fd, args.outdir, args.max_size * 1024)
def main(): args = parser.parse_args() logging.basicConfig(format='%(asctime)s %(name)s: %(levelname)s: %(message)s', level=logging.DEBUG if args.debug else logging.INFO) comm = lglaf.autodetect_device() with closing(comm): lglaf.try_hello(comm) # Be careful: a too large read size will result in a hang while LAF # tries to read more data, requiring a reset. size = get_file_size(comm, args.file) if size > 0: _logger.debug("File size is %d", size) with laf_open_ro(comm, args.file) as file_fd: _logger.debug("Opened fd %d for file %s", file_fd, args.file) dump_file(comm, file_fd, args.output_file, size) else: _logger.warning("Not a file or zero size, not writing file")
def laf_read(comm, fd_num, offset, size): """Read size bytes at the given block offset.""" read_cmd = lglaf.make_request(b'READ', args=[fd_num, offset, size]) for attempt in range(3): try: header, response = comm.call(read_cmd) break except usb.core.USBError as e: if e.strerror == 'Overflow': _logger.debug("Overflow on READ %d %d %d", fd_num, offset, size) for attempt in range(3): try: comm.reset() comm._read(-1) # clear line break except usb.core.USBError: pass continue elif e.strerror == 'Operation timed out': _logger.debug("Timeout on READ %d %d %d", fd_num, offset, size) comm.close() time.sleep(3) comm.__init__() try: lglaf.try_hello(comm) except usb.core.USBError: pass close_cmd = lglaf.make_request(b'CLSE', args=[fd_num]) comm.call(close_cmd) open_cmd = lglaf.make_request(b'OPEN', body=b'\0') open_header = comm.call(open_cmd)[0] fd_num = read_uint32(open_header, 4) read_cmd = lglaf.make_request(b'READ', args=[fd_num, offset, size]) continue else: raise # rethrow # Ensure that response fd, offset and length are sane (match the request) assert read_cmd[4:4 + 12] == header[4:4 + 12], "Unexpected read response" assert len(response) == size return response, fd_num
def main(): args = parser.parse_args() logging.basicConfig( format='%(asctime)s %(name)s: %(levelname)s: %(message)s', level=logging.DEBUG if args.debug else logging.INFO) actions = (args.list, args.dump, args.restore, args.wipe) if sum(1 if x else 0 for x in actions) != 1: parser.error("Please specify one action from" " --list / --dump / --restore / --wipe") if not args.partition and (args.dump or args.restore or args.wipe): parser.error("Please specify a partition") comm = lglaf.autodetect_device() with closing(comm): lglaf.try_hello(comm) if args.list: list_partitions(comm, args.partition) return partitions = get_partitions(comm) try: part_label, part_name = find_partition(partitions, args.partition) except ValueError as e: parser.error(e) part_offset, part_size = partition_info(comm, part_name) _logger.debug("Partition %s (%s) at offset %d (%#x) size %d (%#x)", part_label, part_name, part_offset, part_offset, part_size, part_size) with laf_open_disk(comm) as disk_fd: _logger.debug("Opened fd %d for disk", disk_fd) if args.dump: dump_partition(comm, disk_fd, args.dump, part_offset, part_size) elif args.restore: write_partition(comm, disk_fd, args.restore, part_offset, part_size) elif args.wipe: wipe_partition(comm, disk_fd, part_offset, part_size)
def laf_read(comm, fd_num, offset, size): """Read size bytes at the given block offset.""" read_cmd = lglaf.make_request(b'READ', args=[fd_num, offset, size]) for attempt in range(3): try: header, response = comm.call(read_cmd) break except usb.core.USBError as e: if e.strerror == 'Overflow': _logger.debug("Overflow on READ %d %d %d", fd_num, offset, size) for attempt in range(3): try: comm.reset() comm._read(-1) # clear line break except usb.core.USBError: pass continue elif e.strerror == 'Operation timed out': _logger.debug("Timeout on READ %d %d %d", fd_num, offset, size) comm.close() time.sleep(3) comm.__init__() try: lglaf.try_hello(comm) except usb.core.USBError: pass close_cmd = lglaf.make_request(b'CLSE', args=[fd_num]) comm.call(close_cmd) open_cmd = lglaf.make_request(b'OPEN', body=b'\0') open_header = comm.call(open_cmd)[0] fd_num = read_uint32(open_header, 4) read_cmd = lglaf.make_request(b'READ', args=[fd_num, offset, size]) continue else: raise # rethrow # Ensure that response fd, offset and length are sane (match the request) assert read_cmd[4:4+12] == header[4:4+12], "Unexpected read response" assert len(response) == size return response, fd_num
def main(): args = parser.parse_args() logging.basicConfig(format='%(asctime)s %(name)s: %(levelname)s: %(message)s', level=logging.DEBUG if args.debug else logging.INFO) actions = (args.list, args.dump, args.restore, args.wipe) if sum(1 if x else 0 for x in actions) != 1: parser.error("Please specify one action from" " --list / --dump / --restore / --wipe") if not args.partition and (args.dump or args.restore or args.wipe): parser.error("Please specify a partition") comm = lglaf.autodetect_device() with closing(comm): lglaf.try_hello(comm) if args.list: list_partitions(comm, args.partition) return partitions = get_partitions(comm) try: part_label, part_name = find_partition(partitions, args.partition) except ValueError as e: parser.error(e) part_offset, part_size = partition_info(comm, part_name) _logger.debug("Partition %s (%s) at offset %d (%#x) size %d (%#x)", part_label, part_name, part_offset, part_offset, part_size, part_size) with laf_open_disk(comm) as disk_fd: _logger.debug("Opened fd %d for disk", disk_fd) if args.dump: dump_partition(comm, disk_fd, args.dump, part_offset, part_size) elif args.restore: write_partition(comm, disk_fd, args.restore, part_offset, part_size) elif args.wipe: wipe_partition(comm, disk_fd, part_offset, part_size)
def main(): args = parser.parse_args() logging.basicConfig(format='%(asctime)s %(name)s: %(levelname)s: %(message)s', level=logging.DEBUG if args.debug else logging.INFO) actions = (args.list, args.dump, args.restore, args.restoremisc, args.wipe) if sum(1 if x else 0 for x in actions) != 1: parser.error("Please specify one action from" " --list / --dump / --restore /--restoremisc / --wipe") if not args.partition and (args.dump or args.restore or args.wipe): parser.error("Please specify a partition") if args.partition and args.partition.isdigit(): args.partition = int(args.partition) comm = lglaf.autodetect_device() with closing(comm): lglaf.try_hello(comm) _logger.debug("Using Protocol version: 0x%x" % comm.protocol_version) with laf_open_disk(comm) as disk_fd: if args.list: if args.batch: list_partitions(comm, disk_fd, args.partition, True) else: list_partitions(comm, disk_fd, args.partition, False) return diskinfo = get_partitions(comm, disk_fd) try: part = find_partition(diskinfo, args.partition) except ValueError as e: parser.error(e) info = get_partition_info_string(part) _logger.debug("%s", info) part_offset = part.first_lba * BLOCK_SIZE part_size = (part.last_lba - (part.first_lba - 1)) * BLOCK_SIZE _logger.debug("%s", info) _logger.debug("Opened fd %d for disk", disk_fd) if args.dump: if not args.batch: dump_partition(comm, disk_fd, args.dump, part_offset, part_size, False) else: dump_partition(comm, disk_fd, args.dump, part_offset, part_size, True) elif args.restore: if not args.batch: write_partition(comm, disk_fd, args.restore, part_offset, part_size, False) else: write_partition(comm, disk_fd, args.restore, part_offset, part_size, True) elif args.restoremisc: if not args.batch: write_misc_partition(comm, disk_fd, args.restoremisc, part_offset, part_size, False) else: write_misc_partition(comm, disk_fd, args.restoremisc, part_offset, part_size, True) elif args.wipe: if not args.batch: wipe_partition(comm, disk_fd, part_offset, part_size, False) else: wipe_partition(comm, disk_fd, part_offset, part_size, True)