#! /usr/bin/env python # Copyright (C) 2012 The Android Open Source Project # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from __future__ import print_function import getopt, posixpath, signal, struct, sys def usage(argv0): print(""" Usage: %s [-v] sparse_image_file ... -v verbose output """ % ( argv0 )) sys.exit(2) def main(): signal.signal(signal.SIGPIPE, signal.SIG_DFL) me = posixpath.basename(sys.argv[0]) # Parse the command line verbose = 0 # -v try: opts, args = getopt.getopt(sys.argv[1:], "v", ["verbose"]) except getopt.GetoptError, e: print(e) usage(me) for o, a in opts: if o in ("-v", "--verbose"): verbose += 1 else: print("Unrecognized option \"%s\"" % (o)) usage(me) if len(args) == 0: print("No sparse_image_file specified") usage(me) for path in args: FH = open(path, 'rb') header_bin = FH.read(28) header = struct.unpack(" 1: header = struct.unpack("<12B", header_bin) print(" (%02X%02X %02X%02X %02X%02X%02X%02X %02X%02X%02X%02X)" % (header[0], header[1], header[2], header[3], header[4], header[5], header[6], header[7], header[8], header[9], header[10], header[11])) else: print() offset += chunk_sz print(" %10u %7u End" % (FH.tell(), offset)) if total_blks != offset: print("The header said we should have %u output blocks, but we saw %u" % (total_blks, offset)) junk_len = len(FH.read()) if junk_len: print("There were %u bytes of extra data at the end of the file." % (junk_len)) sys.exit(0) if __name__ == "__main__": main()