def copy_and_verify_image(image, dest, bmap, image_chksum, image_size): """ Copy image 'image' using bmap file 'bmap' to the destination file 'dest' and verify the resulting image checksum. """ f_image = TransRead.TransRead(image) f_dest = open(dest, "w+") if (bmap): f_bmap = open(bmap, "r") else: f_bmap = None writer = BmapCopy.BmapCopy(f_image, f_dest, f_bmap, image_size) # Randomly decide whether we want the progress bar or not if bool(random.getrandbits(1)): writer.set_progress_indicator(sys.stdout, None) writer.copy(bool(random.getrandbits(1)), bool(random.getrandbits(1))) # Compare the original file and the copy are identical assert calculate_chksum(dest) == image_chksum if f_bmap: f_bmap.close() f_dest.close() f_image.close()
def _do_test_older_bmapcopy(self, bmap_path, modref): """ Test an older version of BmapCopy class, referenced to by the 'modref' argument. The 'bmap_path' argument is the bmap file path to test with. """ # Get a reference to the older BmapCopy class object to test with old_bmapcopy_class = getattr(modref, "BmapCopy") supported_ver = getattr(modref, "SUPPORTED_BMAP_VERSION") f_bmap = open(bmap_path, "r") # Find the version of the bmap file. The easiest is to simply use the # latest BmapCopy. bmapcopy = BmapCopy.BmapCopy(self._f_image, self._f_copy, f_bmap) bmap_version = bmapcopy.bmap_version bmap_version_major = bmapcopy.bmap_version_major try: if supported_ver >= bmap_version: writer = old_bmapcopy_class(self._f_image, self._f_copy, f_bmap) writer.copy(True, True) except: # pylint: disable=W0702 if supported_ver >= bmap_version_major: # The BmapCopy which we are testing is supposed to support this # version of bmap file format. However, bmap format version 1.4 # was a screw-up, because it actually had incompatible changes, # so old versions of BmapCopy are supposed to fail. if not (supported_ver == 1 and bmap_version == "1.4"): print("Module \"%s\" failed to handle \"%s\"" % (modref.__name__, bmap_path)) raise f_bmap.close()
def copy_command(args): """Copy an image to a block device or a regular file using bmap.""" if args.nobmap and args.bmap: error_out("--nobmap and --bmap cannot be used together") if args.bmap_sig and args.no_sig_verify: error_out("--bmap-sig and --no-sig-verify cannot be used together") image_obj, dest_obj, bmap_obj, bmap_path, image_size, dest_is_blkdev = \ open_files(args) if args.bmap_sig and not bmap_obj: error_out("the bmap signature file was specified, but bmap file was " "not found") f_obj = verify_bmap_signature(args, bmap_obj, bmap_path) if f_obj: bmap_obj.close() bmap_obj = f_obj if bmap_obj: bmap_obj = NamedFile(bmap_obj, bmap_path) try: if dest_is_blkdev: dest_str = "block device '%s'" % args.dest # For block devices, use the specialized class writer = BmapCopy.BmapBdevCopy(image_obj, dest_obj, bmap_obj, image_size) else: dest_str = "file '%s'" % os.path.basename(args.dest) writer = BmapCopy.BmapCopy(image_obj, dest_obj, bmap_obj, image_size) except BmapCopy.Error as err: error_out(err) # Print the progress indicator while copying if not args.quiet and not args.debug and \ sys.stderr.isatty() and sys.stdout.isatty(): writer.set_progress_indicator(sys.stderr, "bmaptool: info: %d%% copied") start_time = time.time() if not bmap_obj: if args.nobmap: log.info("no bmap given, copy entire image to '%s'" % args.dest) else: error_out("bmap file not found, please, use --nobmap option to " "flash without bmap") else: log.info("block map format version %s" % writer.bmap_version) log.info("%d blocks of size %d (%s), mapped %d blocks (%s or %.1f%%)" % (writer.blocks_cnt, writer.block_size, writer.image_size_human, writer.mapped_cnt, writer.mapped_size_human, writer.mapped_percent)) log.info("copying image '%s' to %s using bmap file '%s'" % (os.path.basename(args.image), dest_str, os.path.basename(bmap_path))) try: try: writer.copy(False, not args.no_verify) except (BmapCopy.Error, TransRead.Error) as err: error_out(err) # Synchronize the block device log.info("synchronizing '%s'" % args.dest) try: writer.sync() except BmapCopy.Error as err: error_out(err) except KeyboardInterrupt: error_out("interrupted, exiting") copying_time = time.time() - start_time copying_speed = writer.mapped_size // copying_time log.info("copying time: %s, copying speed %s/sec" % (BmapHelpers.human_time(copying_time), BmapHelpers.human_size(copying_speed))) dest_obj.close() if bmap_obj: bmap_obj.close() image_obj.close()