-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.go
224 lines (209 loc) · 6.52 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package main
import (
"archive/zip"
"bytes"
"compress/bzip2"
"encoding/binary"
"fmt"
"io"
"log"
"os"
"github.com/golang/protobuf/proto"
"github.com/xi2/xz"
)
const (
payloadFilename = "payload.bin"
zipMagic = "PK"
payloadMagic = "CrAU"
brilloMajorPayloadVersion = 2
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("Usage: %s <input> [(optional) file to extract...]\n", os.Args[0])
os.Exit(1)
}
filename := os.Args[1]
extractFiles := os.Args[2:]
f, err := os.Open(filename)
if err != nil {
log.Fatalf("Failed to open file: %s", err)
}
if isZip(f) {
// Extract payload.bin from the zip first
_ = f.Close()
log.Printf("Input is a zip file, searching for %s ...\n", payloadFilename)
zr, err := zip.OpenReader(filename)
if err != nil {
log.Fatalf("Failed to open the zip file: %s\n", err.Error())
}
zf, err := findPayload(zr)
if err != nil {
log.Fatalf("Failed to read from the zip file: %s\n", err.Error())
}
if zf == nil {
log.Fatalf("%s not found in the zip file\n", payloadFilename)
}
log.Printf("Extracting %s ...\n", payloadFilename)
f, err = os.Create(payloadFilename)
if err != nil {
log.Fatalf("Failed to create the extraction file: %s\n", err.Error())
}
_, err = io.Copy(f, zf)
if err != nil {
log.Fatalf("Failed to extract: %s\n", err.Error())
}
_ = zf.Close()
_ = zr.Close()
_, _ = f.Seek(0, 0)
}
parsePayload(f, extractFiles)
}
func isZip(f *os.File) bool {
header := make([]byte, len(zipMagic))
_, err := f.Read(header)
_, _ = f.Seek(0, 0)
return err == nil && string(header) == zipMagic
}
func findPayload(zr *zip.ReadCloser) (io.ReadCloser, error) {
for _, f := range zr.File {
if f.Name == payloadFilename {
return f.Open()
}
}
return nil, nil
}
func parsePayload(r io.ReadSeeker, extractFiles []string) {
log.Println("Parsing payload...")
// magic
magic := make([]byte, len(payloadMagic))
_, err := r.Read(magic)
if err != nil || string(magic) != payloadMagic {
log.Fatalf("Incorrect magic (%s)\n", string(magic))
}
// version & lengths
var version, manifestLen uint64
var metadataSigLen uint32
err = binary.Read(r, binary.BigEndian, &version)
if err != nil || version != brilloMajorPayloadVersion {
log.Fatalf("Unsupported payload version (%d). This tool only supports version %d\n",
version, brilloMajorPayloadVersion)
}
err = binary.Read(r, binary.BigEndian, &manifestLen)
if err != nil || !(manifestLen > 0) {
log.Fatalf("Incorrect manifest length (%d)\n", manifestLen)
}
err = binary.Read(r, binary.BigEndian, &metadataSigLen)
if err != nil || !(metadataSigLen > 0) {
log.Fatalf("Incorrect metadata signature length (%d)\n", metadataSigLen)
}
// manifest
manifestRaw := make([]byte, manifestLen)
n, err := r.Read(manifestRaw)
if err != nil || uint64(n) != manifestLen {
log.Fatalf("Failed to read the manifest (%d)\n", manifestLen)
}
var manifest DeltaArchiveManifest
err = proto.Unmarshal(manifestRaw, &manifest)
if err != nil {
log.Fatalf("Failed to parse the manifest: %s\n", err.Error())
}
// only support full payloads!
if *manifest.MinorVersion != 0 {
log.Fatalf("Delta payloads are not supported, please use a full payload file\n")
}
// print manifest info
log.Printf("Block size: %d, Partition count: %d\n",
*manifest.BlockSize, len(manifest.Partitions))
// extract partitions
extractPartitions(&manifest, r, 24+manifestLen+uint64(metadataSigLen), extractFiles)
// done
log.Println("Done!")
}
func extractPartitions(manifest *DeltaArchiveManifest, r io.ReadSeeker, baseOffset uint64, extractFiles []string) {
for _, p := range manifest.Partitions {
if p.PartitionName == nil || (len(extractFiles) > 0 && !contains(extractFiles, *p.PartitionName)) {
continue
}
log.Printf("Extracting %s (%d ops) ...", *p.PartitionName, len(p.Operations))
outFilename := fmt.Sprintf("%s.img", *p.PartitionName)
extractPartition(p, outFilename, r, baseOffset, *manifest.BlockSize)
}
}
func extractPartition(p *PartitionUpdate, outFilename string, r io.ReadSeeker, baseOffset uint64, blockSize uint32) {
outFile, err := os.Create(outFilename)
if err != nil {
log.Fatalf("Failed to create the output file: %s\n", err.Error())
}
for _, op := range p.Operations {
data, dataPos := make([]byte, *op.DataLength), int64(baseOffset+*op.DataOffset)
_, err = r.Seek(dataPos, 0)
if err != nil {
_ = outFile.Close()
log.Fatalf("Failed to seek to %d in partition %s: %s\n", dataPos, outFilename, err.Error())
}
n, err := r.Read(data)
if err != nil || uint64(n) != *op.DataLength {
_ = outFile.Close()
log.Fatalf("Failed to read enough data from partition %s: %s\n", outFilename, err.Error())
}
outSeekPos := int64(*op.DstExtents[0].StartBlock * uint64(blockSize))
_, err = outFile.Seek(outSeekPos, 0)
if err != nil {
_ = outFile.Close()
log.Fatalf("Failed to seek to %d in partition %s: %s\n", outSeekPos, outFilename, err.Error())
}
switch *op.Type {
case InstallOperation_REPLACE:
_, err = outFile.Write(data)
if err != nil {
_ = outFile.Close()
log.Fatalf("Failed to write output to %s: %s\n", outFilename, err.Error())
}
case InstallOperation_REPLACE_BZ:
bzr := bzip2.NewReader(bytes.NewReader(data))
_, err = io.Copy(outFile, bzr)
if err != nil {
_ = outFile.Close()
log.Fatalf("Failed to write output to %s: %s\n", outFilename, err.Error())
}
case InstallOperation_REPLACE_XZ:
xzr, err := xz.NewReader(bytes.NewReader(data), 0)
if err != nil {
_ = outFile.Close()
log.Fatalf("Bad xz data in partition %s: %s\n", *p.PartitionName, err.Error())
}
_, err = io.Copy(outFile, xzr)
if err != nil {
_ = outFile.Close()
log.Fatalf("Failed to write output to %s: %s\n", outFilename, err.Error())
}
case InstallOperation_ZERO:
for _, ext := range op.DstExtents {
outSeekPos = int64(*ext.StartBlock * uint64(blockSize))
_, err = outFile.Seek(outSeekPos, 0)
if err != nil {
_ = outFile.Close()
log.Fatalf("Failed to seek to %d in partition %s: %s\n", outSeekPos, outFilename, err.Error())
}
// write zeros
_, err = io.Copy(outFile, bytes.NewReader(make([]byte, *ext.NumBlocks*uint64(blockSize))))
if err != nil {
_ = outFile.Close()
log.Fatalf("Failed to write output to %s: %s\n", outFilename, err.Error())
}
}
default:
_ = outFile.Close()
log.Fatalf("Unsupported operation type: %d (%s), please report a bug\n",
*op.Type, InstallOperation_Type_name[int32(*op.Type)])
}
}
}
func contains(ss []string, s string) bool {
for _, v := range ss {
if v == s {
return true
}
}
return false
}