Elliott Hughes | 726a6a9 | 2021-08-17 15:02:00 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 2 | |
| 3 | # Copyright (C) 2012 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 17 | import csv |
| 18 | import getopt |
| 19 | import hashlib |
| 20 | import posixpath |
| 21 | import signal |
| 22 | import struct |
| 23 | import sys |
| 24 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 25 | |
| 26 | def usage(argv0): |
| 27 | print(""" |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 28 | Usage: %s [-v] [-s] [-c <filename>] sparse_image_file ... |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 29 | -v verbose output |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 30 | -s show sha1sum of data blocks |
| 31 | -c <filename> save .csv file of blocks |
| 32 | """ % (argv0)) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 33 | sys.exit(2) |
| 34 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 35 | |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 36 | def main(): |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 37 | signal.signal(signal.SIGPIPE, signal.SIG_DFL) |
| 38 | |
| 39 | me = posixpath.basename(sys.argv[0]) |
| 40 | |
| 41 | # Parse the command line |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 42 | verbose = 0 # -v |
| 43 | showhash = 0 # -s |
| 44 | csvfilename = None # -c |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 45 | try: |
| 46 | opts, args = getopt.getopt(sys.argv[1:], |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 47 | "vsc:", |
| 48 | ["verbose", "showhash", "csvfile"]) |
Elliott Hughes | 726a6a9 | 2021-08-17 15:02:00 -0700 | [diff] [blame] | 49 | except getopt.GetoptError as e: |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 50 | print(e) |
| 51 | usage(me) |
| 52 | for o, a in opts: |
| 53 | if o in ("-v", "--verbose"): |
| 54 | verbose += 1 |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 55 | elif o in ("-s", "--showhash"): |
| 56 | showhash = True |
| 57 | elif o in ("-c", "--csvfile"): |
| 58 | csvfilename = a |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 59 | else: |
| 60 | print("Unrecognized option \"%s\"" % (o)) |
| 61 | usage(me) |
| 62 | |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 63 | if not args: |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 64 | print("No sparse_image_file specified") |
| 65 | usage(me) |
| 66 | |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 67 | if csvfilename: |
| 68 | csvfile = open(csvfilename, "wb") |
| 69 | csvwriter = csv.writer(csvfile) |
| 70 | |
| 71 | output = verbose or csvfilename or showhash |
| 72 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 73 | for path in args: |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 74 | FH = open(path, "rb") |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 75 | header_bin = FH.read(28) |
| 76 | header = struct.unpack("<I4H4I", header_bin) |
| 77 | |
| 78 | magic = header[0] |
| 79 | major_version = header[1] |
| 80 | minor_version = header[2] |
| 81 | file_hdr_sz = header[3] |
| 82 | chunk_hdr_sz = header[4] |
| 83 | blk_sz = header[5] |
| 84 | total_blks = header[6] |
| 85 | total_chunks = header[7] |
| 86 | image_checksum = header[8] |
| 87 | |
| 88 | if magic != 0xED26FF3A: |
| 89 | print("%s: %s: Magic should be 0xED26FF3A but is 0x%08X" |
| 90 | % (me, path, magic)) |
| 91 | continue |
| 92 | if major_version != 1 or minor_version != 0: |
| 93 | print("%s: %s: I only know about version 1.0, but this is version %u.%u" |
| 94 | % (me, path, major_version, minor_version)) |
| 95 | continue |
| 96 | if file_hdr_sz != 28: |
| 97 | print("%s: %s: The file header size was expected to be 28, but is %u." |
| 98 | % (me, path, file_hdr_sz)) |
| 99 | continue |
| 100 | if chunk_hdr_sz != 12: |
| 101 | print("%s: %s: The chunk header size was expected to be 12, but is %u." |
| 102 | % (me, path, chunk_hdr_sz)) |
| 103 | continue |
| 104 | |
| 105 | print("%s: Total of %u %u-byte output blocks in %u input chunks." |
| 106 | % (path, total_blks, blk_sz, total_chunks)) |
| 107 | |
| 108 | if image_checksum != 0: |
| 109 | print("checksum=0x%08X" % (image_checksum)) |
| 110 | |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 111 | if not output: |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 112 | continue |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 113 | |
| 114 | if verbose > 0: |
| 115 | print(" input_bytes output_blocks") |
| 116 | print("chunk offset number offset number") |
| 117 | |
| 118 | if csvfilename: |
| 119 | csvwriter.writerow(["chunk", "input offset", "input bytes", |
| 120 | "output offset", "output blocks", "type", "hash"]) |
| 121 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 122 | offset = 0 |
Elliott Hughes | f75518e | 2022-01-18 17:26:56 -0800 | [diff] [blame] | 123 | for i in range(1, total_chunks + 1): |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 124 | header_bin = FH.read(12) |
| 125 | header = struct.unpack("<2H2I", header_bin) |
| 126 | chunk_type = header[0] |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 127 | chunk_sz = header[2] |
| 128 | total_sz = header[3] |
| 129 | data_sz = total_sz - 12 |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 130 | curhash = "" |
| 131 | curtype = "" |
| 132 | curpos = FH.tell() |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 133 | |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 134 | if verbose > 0: |
| 135 | print("%4u %10u %10u %7u %7u" % (i, curpos, data_sz, offset, chunk_sz), |
| 136 | end=" ") |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 137 | |
| 138 | if chunk_type == 0xCAC1: |
| 139 | if data_sz != (chunk_sz * blk_sz): |
| 140 | print("Raw chunk input size (%u) does not match output size (%u)" |
| 141 | % (data_sz, chunk_sz * blk_sz)) |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 142 | break |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 143 | else: |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 144 | curtype = "Raw data" |
| 145 | data = FH.read(data_sz) |
| 146 | if showhash: |
| 147 | h = hashlib.sha1() |
| 148 | h.update(data) |
| 149 | curhash = h.hexdigest() |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 150 | elif chunk_type == 0xCAC2: |
| 151 | if data_sz != 4: |
| 152 | print("Fill chunk should have 4 bytes of fill, but this has %u" |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 153 | % (data_sz)) |
| 154 | break |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 155 | else: |
| 156 | fill_bin = FH.read(4) |
| 157 | fill = struct.unpack("<I", fill_bin) |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 158 | curtype = format("Fill with 0x%08X" % (fill)) |
| 159 | if showhash: |
| 160 | h = hashlib.sha1() |
Bowgo Tsai | dbf62d9 | 2022-10-03 16:15:21 +0800 | [diff] [blame] | 161 | data = fill_bin * (blk_sz // 4); |
Elliott Hughes | f75518e | 2022-01-18 17:26:56 -0800 | [diff] [blame] | 162 | for block in range(chunk_sz): |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 163 | h.update(data) |
| 164 | curhash = h.hexdigest() |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 165 | elif chunk_type == 0xCAC3: |
| 166 | if data_sz != 0: |
| 167 | print("Don't care chunk input size is non-zero (%u)" % (data_sz)) |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 168 | break |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 169 | else: |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 170 | curtype = "Don't care" |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 171 | elif chunk_type == 0xCAC4: |
| 172 | if data_sz != 4: |
| 173 | print("CRC32 chunk should have 4 bytes of CRC, but this has %u" |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 174 | % (data_sz)) |
| 175 | break |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 176 | else: |
| 177 | crc_bin = FH.read(4) |
Eric Miao | 4cc3978 | 2015-04-12 16:31:46 -0700 | [diff] [blame] | 178 | crc = struct.unpack("<I", crc_bin) |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 179 | curtype = format("Unverified CRC32 0x%08X" % (crc)) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 180 | else: |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 181 | print("Unknown chunk type 0x%04X" % (chunk_type)) |
| 182 | break |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 183 | |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 184 | if verbose > 0: |
| 185 | print("%-18s" % (curtype), end=" ") |
| 186 | |
| 187 | if verbose > 1: |
| 188 | header = struct.unpack("<12B", header_bin) |
| 189 | print(" (%02X%02X %02X%02X %02X%02X%02X%02X %02X%02X%02X%02X)" |
| 190 | % (header[0], header[1], header[2], header[3], |
| 191 | header[4], header[5], header[6], header[7], |
| 192 | header[8], header[9], header[10], header[11]), end=" ") |
| 193 | |
| 194 | print(curhash) |
| 195 | |
| 196 | if csvfilename: |
| 197 | csvwriter.writerow([i, curpos, data_sz, offset, chunk_sz, curtype, |
| 198 | curhash]) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 199 | |
| 200 | offset += chunk_sz |
| 201 | |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 202 | if verbose > 0: |
| 203 | print(" %10u %7u End" % (FH.tell(), offset)) |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 204 | |
| 205 | if total_blks != offset: |
| 206 | print("The header said we should have %u output blocks, but we saw %u" |
| 207 | % (total_blks, offset)) |
| 208 | |
| 209 | junk_len = len(FH.read()) |
| 210 | if junk_len: |
| 211 | print("There were %u bytes of extra data at the end of the file." |
| 212 | % (junk_len)) |
| 213 | |
Patrick Tjin | c38720a | 2016-10-03 13:03:23 -0700 | [diff] [blame] | 214 | if csvfilename: |
| 215 | csvfile.close() |
| 216 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 217 | sys.exit(0) |
| 218 | |
| 219 | if __name__ == "__main__": |
| 220 | main() |