Doug Zongker | 5fad203 | 2014-02-24 08:13:45 -0800 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 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 | |
| 17 | from __future__ import print_function |
| 18 | import getopt, posixpath, signal, struct, sys |
| 19 | |
| 20 | def main(): |
| 21 | if len(sys.argv) == 4: |
| 22 | print("No sparse_image_file specified") |
| 23 | usage(me) |
| 24 | |
| 25 | sparse_fn = sys.argv[1] |
| 26 | unsparse_fn = sys.argv[2] |
| 27 | map_file = sys.argv[3] |
| 28 | mapped_unsparse_fn = sys.argv[4] |
| 29 | |
| 30 | return ComputeMap(sparse_fn, unsparse_fn, map_file, mapped_unsparse_fn) |
| 31 | |
| 32 | |
| 33 | def ComputeMap(sparse_fn, unsparse_fn, map_file, mapped_unsparse_fn): |
| 34 | care_map = [] |
| 35 | |
| 36 | with open(sparse_fn, "rb") as FH: |
| 37 | header_bin = FH.read(28) |
| 38 | header = struct.unpack("<I4H4I", header_bin) |
| 39 | |
| 40 | magic = header[0] |
| 41 | major_version = header[1] |
| 42 | minor_version = header[2] |
| 43 | file_hdr_sz = header[3] |
| 44 | chunk_hdr_sz = header[4] |
| 45 | blk_sz = header[5] |
| 46 | total_blks = header[6] |
| 47 | total_chunks = header[7] |
| 48 | image_checksum = header[8] |
| 49 | |
| 50 | if magic != 0xED26FF3A: |
| 51 | print("%s: %s: Magic should be 0xED26FF3A but is 0x%08X" |
| 52 | % (me, path, magic)) |
| 53 | return 1 |
| 54 | if major_version != 1 or minor_version != 0: |
| 55 | print("%s: %s: I only know about version 1.0, but this is version %u.%u" |
| 56 | % (me, path, major_version, minor_version)) |
| 57 | return 1 |
| 58 | if file_hdr_sz != 28: |
| 59 | print("%s: %s: The file header size was expected to be 28, but is %u." |
| 60 | % (me, path, file_hdr_sz)) |
| 61 | return 1 |
| 62 | if chunk_hdr_sz != 12: |
| 63 | print("%s: %s: The chunk header size was expected to be 12, but is %u." |
| 64 | % (me, path, chunk_hdr_sz)) |
| 65 | return 1 |
| 66 | |
| 67 | print("%s: Total of %u %u-byte output blocks in %u input chunks." |
| 68 | % (sparse_fn, total_blks, blk_sz, total_chunks)) |
| 69 | |
| 70 | offset = 0 |
| 71 | for i in range(total_chunks): |
| 72 | header_bin = FH.read(12) |
| 73 | header = struct.unpack("<2H2I", header_bin) |
| 74 | chunk_type = header[0] |
| 75 | reserved1 = header[1] |
| 76 | chunk_sz = header[2] |
| 77 | total_sz = header[3] |
| 78 | data_sz = total_sz - 12 |
| 79 | |
| 80 | if chunk_type == 0xCAC1: |
| 81 | if data_sz != (chunk_sz * blk_sz): |
| 82 | print("Raw chunk input size (%u) does not match output size (%u)" |
| 83 | % (data_sz, chunk_sz * blk_sz)) |
| 84 | return 1 |
| 85 | else: |
| 86 | care_map.append((1, chunk_sz)) |
| 87 | FH.seek(data_sz, 1) |
| 88 | |
| 89 | elif chunk_type == 0xCAC2: |
| 90 | print("Fill chunks are not supported") |
| 91 | return 1 |
| 92 | |
| 93 | elif chunk_type == 0xCAC3: |
| 94 | if data_sz != 0: |
| 95 | print("Don't care chunk input size is non-zero (%u)" % (data_sz)) |
| 96 | return 1 |
| 97 | else: |
| 98 | care_map.append((0, chunk_sz)) |
| 99 | |
| 100 | elif chunk_type == 0xCAC4: |
| 101 | print("CRC32 chunks are not supported") |
| 102 | |
| 103 | else: |
| 104 | print("Unknown chunk type 0x%04X not supported" % (chunk_type,)) |
| 105 | return 1 |
| 106 | |
| 107 | offset += chunk_sz |
| 108 | |
| 109 | if total_blks != offset: |
| 110 | print("The header said we should have %u output blocks, but we saw %u" |
| 111 | % (total_blks, offset)) |
| 112 | |
| 113 | junk_len = len(FH.read()) |
| 114 | if junk_len: |
| 115 | print("There were %u bytes of extra data at the end of the file." |
| 116 | % (junk_len)) |
| 117 | return 1 |
| 118 | |
| 119 | last_kind = None |
| 120 | new_care_map = [] |
| 121 | for kind, size in care_map: |
| 122 | if kind != last_kind: |
| 123 | new_care_map.append((kind, size)) |
| 124 | last_kind = kind |
| 125 | else: |
| 126 | new_care_map[-1] = (kind, new_care_map[-1][1] + size) |
| 127 | |
| 128 | if new_care_map[0][0] == 0: |
| 129 | new_care_map.insert(0, (1, 0)) |
| 130 | if len(new_care_map) % 2: |
| 131 | new_care_map.append((0, 0)) |
| 132 | |
| 133 | with open(map_file, "w") as fmap: |
| 134 | fmap.write("%d\n%d\n" % (blk_sz, len(new_care_map))) |
| 135 | for _, sz in new_care_map: |
| 136 | fmap.write("%d\n" % sz) |
| 137 | |
| 138 | with open(unsparse_fn, "rb") as fin: |
| 139 | with open(mapped_unsparse_fn, "wb") as fout: |
| 140 | for k, sz in care_map: |
| 141 | data = fin.read(sz * blk_sz) |
| 142 | if k: |
| 143 | fout.write(data) |
| 144 | else: |
| 145 | assert data == "\x00" * len(data) |
| 146 | |
| 147 | if __name__ == "__main__": |
| 148 | sys.exit(main()) |