forked from Tasssadar/Team-Win-Recovery-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimg2img.c
314 lines (262 loc) · 7.29 KB
/
simg2img.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (C) 2010 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.
*/
#include "ext4_utils.h"
#include "sparse_format.h"
#include "sparse_crc32.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#define COPY_BUF_SIZE (1024*1024)
u8 *copybuf;
/* This will be malloc'ed with the size of blk_sz from the sparse file header */
u8* zerobuf;
#define SPARSE_HEADER_MAJOR_VER 1
#define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
void usage()
{
fprintf(stderr, "Usage: simg2img <sparse_image_file> <raw_image_file>\n");
}
static int read_all(int fd, void *buf, size_t len)
{
size_t total = 0;
int ret;
char *ptr = buf;
while (total < len) {
ret = read(fd, ptr, len - total);
if (ret < 0)
return ret;
if (ret == 0)
return total;
ptr += ret;
total += ret;
}
return total;
}
static int write_all(int fd, void *buf, size_t len)
{
size_t total = 0;
int ret;
char *ptr = buf;
while (total < len) {
ret = write(fd, ptr, len - total);
if (ret < 0)
return ret;
if (ret == 0)
return total;
ptr += ret;
total += ret;
}
return total;
}
int process_raw_chunk(int in, int out, u32 blocks, u32 blk_sz, u32 *crc32)
{
u64 len = (u64)blocks * blk_sz;
int ret;
int chunk;
while (len) {
chunk = (len > COPY_BUF_SIZE) ? COPY_BUF_SIZE : len;
ret = read_all(in, copybuf, chunk);
if (ret != chunk) {
fprintf(stderr, "read returned an error copying a raw chunk: %d %d\n",
ret, chunk);
exit(-1);
}
*crc32 = sparse_crc32(*crc32, copybuf, chunk);
ret = write_all(out, copybuf, chunk);
if (ret != chunk) {
fprintf(stderr, "write returned an error copying a raw chunk\n");
exit(-1);
}
len -= chunk;
}
return blocks;
}
int process_fill_chunk(int in, int out, u32 blocks, u32 blk_sz, u32 *crc32)
{
u64 len = (u64)blocks * blk_sz;
int ret;
int chunk;
u32 fill_val;
u32 *fillbuf;
unsigned int i;
/* Fill copy_buf with the fill value */
ret = read_all(in, &fill_val, sizeof(fill_val));
fillbuf = (u32 *)copybuf;
for (i = 0; i < (COPY_BUF_SIZE / sizeof(fill_val)); i++) {
fillbuf[i] = fill_val;
}
while (len) {
chunk = (len > COPY_BUF_SIZE) ? COPY_BUF_SIZE : len;
*crc32 = sparse_crc32(*crc32, copybuf, chunk);
ret = write_all(out, copybuf, chunk);
if (ret != chunk) {
fprintf(stderr, "write returned an error copying a raw chunk\n");
exit(-1);
}
len -= chunk;
}
return blocks;
}
int process_skip_chunk(int out, u32 blocks, u32 blk_sz, u32 *crc32)
{
/* len needs to be 64 bits, as the sparse file specifies the skip amount
* as a 32 bit value of blocks.
*/
u64 len = (u64)blocks * blk_sz;
lseek64(out, len, SEEK_CUR);
return blocks;
}
int process_crc32_chunk(int in, u32 crc32)
{
u32 file_crc32;
int ret;
ret = read_all(in, &file_crc32, 4);
if (ret != 4) {
fprintf(stderr, "read returned an error copying a crc32 chunk\n");
exit(-1);
}
if (file_crc32 != crc32) {
fprintf(stderr, "computed crc32 of 0x%8.8x, expected 0x%8.8x\n",
crc32, file_crc32);
exit(-1);
}
return 0;
}
int main(int argc, char *argv[])
{
int in;
int out;
unsigned int i;
sparse_header_t sparse_header;
chunk_header_t chunk_header;
u32 crc32 = 0;
u32 total_blocks = 0;
int ret;
if (argc != 3) {
usage();
exit(-1);
}
if ( (copybuf = malloc(COPY_BUF_SIZE)) == 0) {
fprintf(stderr, "Cannot malloc copy buf\n");
exit(-1);
}
if (strcmp(argv[1], "-") == 0) {
in = STDIN_FILENO;
} else {
if ((in = open(argv[1], O_RDONLY)) == 0) {
fprintf(stderr, "Cannot open input file %s\n", argv[1]);
exit(-1);
}
}
if (strcmp(argv[2], "-") == 0) {
out = STDOUT_FILENO;
} else {
if ((out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666)) == 0) {
fprintf(stderr, "Cannot open output file %s\n", argv[2]);
exit(-1);
}
}
ret = read_all(in, &sparse_header, sizeof(sparse_header));
if (ret != sizeof(sparse_header)) {
fprintf(stderr, "Error reading sparse file header\n");
exit(-1);
}
if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
fprintf(stderr, "Bad magic\n");
exit(-1);
}
if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
fprintf(stderr, "Unknown major version number\n");
exit(-1);
}
if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) {
/* Skip the remaining bytes in a header that is longer than
* we expected.
*/
lseek64(in, sparse_header.file_hdr_sz - SPARSE_HEADER_LEN, SEEK_CUR);
}
if ( (zerobuf = malloc(sparse_header.blk_sz)) == 0) {
fprintf(stderr, "Cannot malloc zero buf\n");
exit(-1);
}
for (i=0; i<sparse_header.total_chunks; i++) {
ret = read_all(in, &chunk_header, sizeof(chunk_header));
if (ret != sizeof(chunk_header)) {
fprintf(stderr, "Error reading chunk header\n");
exit(-1);
}
if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) {
/* Skip the remaining bytes in a header that is longer than
* we expected.
*/
lseek64(in, sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN, SEEK_CUR);
}
switch (chunk_header.chunk_type) {
case CHUNK_TYPE_RAW:
if (chunk_header.total_sz != (sparse_header.chunk_hdr_sz +
(chunk_header.chunk_sz * sparse_header.blk_sz)) ) {
fprintf(stderr, "Bogus chunk size for chunk %d, type Raw\n", i);
exit(-1);
}
total_blocks += process_raw_chunk(in, out,
chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
break;
case CHUNK_TYPE_FILL:
if (chunk_header.total_sz != (sparse_header.chunk_hdr_sz + sizeof(u32)) ) {
fprintf(stderr, "Bogus chunk size for chunk %d, type Fill\n", i);
exit(-1);
}
total_blocks += process_fill_chunk(in, out,
chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
break;
case CHUNK_TYPE_DONT_CARE:
if (chunk_header.total_sz != sparse_header.chunk_hdr_sz) {
fprintf(stderr, "Bogus chunk size for chunk %d, type Dont Care\n", i);
exit(-1);
}
total_blocks += process_skip_chunk(out,
chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
break;
case CHUNK_TYPE_CRC32:
process_crc32_chunk(in, crc32);
break;
default:
fprintf(stderr, "Unknown chunk type 0x%4.4x\n", chunk_header.chunk_type);
}
}
/* If the last chunk was a skip, then the code just did a seek, but
* no write, and the file won't actually be the correct size. This
* will make the file the correct size. Make sure the offset is
* computed in 64 bits, and the function called can handle 64 bits.
*/
if (ftruncate64(out, (u64)total_blocks * sparse_header.blk_sz)) {
fprintf(stderr, "Error calling ftruncate() to set the image size\n");
exit(-1);
}
close(in);
close(out);
if (sparse_header.total_blks != total_blocks) {
fprintf(stderr, "Wrote %d blocks, expected to write %d blocks\n",
total_blocks, sparse_header.total_blks);
exit(-1);
}
exit(0);
}