Modern Apps git

MAOS.git

Public
git clone https://git.maproj.org/MAOS.git
main ▾

MAOS.git / installer / sparse_split.py

sparse_split.py124 lines · 4.1 KiBRaw
1#!/usr/bin/env python3
2"""
3sparse_split.py — split a raw partition image into multiple Android *sparse* images,
4each small enough for one fastboot `download` (i.e. under the device's max-download-size).
5
6Why: partitions like `super` are multiple GB, but a device only accepts max-download-size
7bytes per download (often a few hundred MB). fastboot normally re-chunks large images into
8several sparse files and flashes each to the same partition; the MAOS web installer flashes
9pre-split chunks instead of implementing sparse in the browser. Each output file is a full
10sparse image for the WHOLE partition, but contains real data only for its slice and marks
11the rest DONT_CARE, so flashing them in order reconstructs the partition.
12
13Sparse format ref: AOSP system/core/libsparse (sparse_format.h).
14
15USAGE:
16 sparse_split.py <raw-image> <out-prefix> [--max-bytes N] [--block-size 4096]
17
18Emits <out-prefix>.0.simg, <out-prefix>.1.simg, ... and prints each filename on stdout.
19
20NOTE: authored against the documented format; validate a real flash on a device before
21relying on it.
22"""
23import argparse
24import os
25import struct
26import sys
27
28SPARSE_MAGIC = 0xED26FF3A
29CHUNK_RAW = 0xCAC1
30CHUNK_FILL = 0xCAC2
31CHUNK_DONT_CARE = 0xCAC3
32
33FILE_HDR_SZ = 28
34CHUNK_HDR_SZ = 12
35
36
37def write_header(f, block_size, total_blocks, total_chunks):
38 f.write(
39 struct.pack(
40 "<IHHHHIIII",
41 SPARSE_MAGIC,
42 1, # major
43 0, # minor
44 FILE_HDR_SZ,
45 CHUNK_HDR_SZ,
46 block_size,
47 total_blocks,
48 total_chunks,
49 0, # image checksum (unused)
50 )
51 )
52
53
54def write_dont_care(f, blocks):
55 f.write(struct.pack("<HHII", CHUNK_DONT_CARE, 0, blocks, CHUNK_HDR_SZ))
56
57
58def write_raw(f, data, block_size):
59 assert len(data) % block_size == 0
60 blocks = len(data) // block_size
61 f.write(struct.pack("<HHII", CHUNK_RAW, 0, blocks, CHUNK_HDR_SZ + len(data)))
62 f.write(data)
63
64
65def main():
66 ap = argparse.ArgumentParser()
67 ap.add_argument("raw")
68 ap.add_argument("out_prefix")
69 ap.add_argument("--max-bytes", type=int, default=100 * 1024 * 1024,
70 help="max real data bytes per output file (default 100 MiB)")
71 ap.add_argument("--block-size", type=int, default=4096)
72 args = ap.parse_args()
73
74 bs = args.block_size
75 size = os.path.getsize(args.raw)
76 total_blocks = (size + bs - 1) // bs
77 # Real-data blocks per output file (keep each file's payload under --max-bytes).
78 blocks_per_file = max(1, args.max_bytes // bs)
79
80 outputs = []
81 with open(args.raw, "rb") as src:
82 idx = 0
83 start = 0 # starting block for this output file
84 while start < total_blocks:
85 end = min(start + blocks_per_file, total_blocks)
86 out_path = f"{args.out_prefix}.{idx}.simg"
87 with open(out_path, "wb") as out:
88 # chunks: [DONT_CARE prefix?] [RAW slice] [DONT_CARE suffix?]
89 chunks = 0
90 chunks += 1 if start > 0 else 0
91 chunks += 1 # the RAW slice
92 chunks += 1 if end < total_blocks else 0
93 write_header(out, bs, total_blocks, chunks)
94
95 if start > 0:
96 write_dont_care(out, start)
97
98 src.seek(start * bs)
99 to_read = (end - start) * bs
100 data = src.read(min(to_read, size - start * bs))
101 # Zero-pad the final block if the image isn't block-aligned.
102 if len(data) % bs != 0:
103 data += b"\x00" * (bs - (len(data) % bs))
104 # If this slice runs past EOF (last file), pad remaining blocks with zeros.
105 expected = (end - start) * bs
106 if len(data) < expected:
107 data += b"\x00" * (expected - len(data))
108 write_raw(out, data, bs)
109
110 if end < total_blocks:
111 write_dont_care(out, total_blocks - end)
112
113 outputs.append(out_path)
114 print(out_path)
115 idx += 1
116 start = end
117
118 if not outputs:
119 print("no output produced", file=sys.stderr)
120 sys.exit(1)
121
122
123if __name__ == "__main__":
124 main()