-
Notifications
You must be signed in to change notification settings - Fork 111
/
image-android-sparse.c
416 lines (360 loc) · 10.9 KB
/
image-android-sparse.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Copyright (c) 2021 Michael Olbrich <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <confuse.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "genimage.h"
struct sparse {
uint32_t block_size;
};
struct sparse_header {
uint32_t magic;
uint16_t major_version;
uint16_t minor_version;
uint16_t header_size;
uint16_t chunk_header_size;
uint32_t block_size;
uint32_t output_blocks;
uint32_t input_chunks;
uint32_t crc32;
} __attribute__((packed));
#define SPARSE_RAW htole16(0xCAC1)
#define SPARSE_FILL htole16(0xCAC2)
#define SPARSE_DONT_CARE htole16(0xCAC3)
#define SPARSE_CRC32 htole16(0xCAC4)
struct sparse_chunk_header {
uint16_t chunk_type;
uint16_t reserved;
uint32_t blocks;
uint32_t size;
} __attribute__((packed));
static int write_data(struct image *image, int fd, const void *data, size_t size)
{
int ret = 0;
ssize_t written = write(fd, data, size);
if (written < 0) {
ret = -errno;
image_error(image, "write %s: %s\n", imageoutfile(image), strerror(errno));
}
else if ((size_t)written != size) {
image_error(image, "only %llu bytes written instead of %llu\n",
(unsigned long long)written, (unsigned long long)size);
ret = -EINVAL;
}
return ret;
}
static int flush_header(struct image *image, int fd, struct sparse_chunk_header *header,
ssize_t offset)
{
int ret;
if (header->chunk_type == 0)
return 0;
if (offset >= 0) {
if (lseek(fd, offset, SEEK_SET) < 0) {
ret = -errno;
image_error(image, "seek %s: %s\n", imageoutfile(image),
strerror(errno));
return ret;
}
}
ret = write_data(image, fd, header, sizeof(*header));
if (ret < 0)
return ret;
if (offset >= 0) {
if (lseek(fd, 0, SEEK_END) < 0) {
ret = -errno;
image_error(image, "seek %s: %s\n", imageoutfile(image),
strerror(errno));
return ret;
}
}
if (header->blocks > 0 || header->chunk_type == SPARSE_CRC32)
image_debug(image, "chunk(0x%04x): blocks =%7u size =%10u bytes\n",
header->chunk_type, header->blocks, header->size);
return 0;
}
static int android_sparse_generate(struct image *image)
{
struct sparse *sparse = image->handler_priv;
struct image *inimage;
const char *infile;
struct sparse_header header;
struct sparse_chunk_header chunk_header = {};
struct extent *extents = NULL;
size_t extent_count, extent, block_count, block;
int in_fd = -1, out_fd = -1, ret;
off_t offset;
unsigned int i;
uint32_t *buf, *zeros, crc32 = 0;
struct stat s;
memset(&header, 0, sizeof(header));
header.magic = htole32(0xed26ff3a);
header.major_version = htole16(0x1);
header.minor_version = htole16(0x0);
header.header_size = htole16(sizeof(struct sparse_header));
header.chunk_header_size = htole16(sizeof(struct sparse_chunk_header));
header.block_size = sparse->block_size;
inimage = image_get(list_first_entry(&image->partitions, struct partition, list)->image);
infile = imageoutfile(inimage);
in_fd = open(infile, O_RDONLY);
if (in_fd < 0) {
ret = -errno;
image_error(image, "open %s: %s\n", infile, strerror(errno));
return ret;
}
ret = fstat(in_fd, &s);
if (ret) {
ret = -errno;
image_error(image, "stat %s: %s\n", infile, strerror(errno));
goto out;
}
block_count = (s.st_size - 1 + sparse->block_size) / sparse->block_size;
header.output_blocks = block_count;
ret = map_file_extents(inimage, infile, in_fd, s.st_size, &extents, &extent_count);
if (ret < 0)
goto out;
/* The extents may have a different granularity than the chosen block size.
So all start and end of all extents must be aligned accordingly. The
extents may overlap now, so merge them if necessary. */
for (extent = 0; extent < extent_count; ++extent) {
size_t size, max;
int j;
extents[extent].start = extents[extent].start / sparse->block_size *
sparse->block_size;
extents[extent].end = (extents[extent].end - 1 + sparse->block_size) /
sparse->block_size * sparse->block_size;
extents[extent].end = min(extents[extent].end, s.st_size);
for (j = extent - 1; j > 0; --j)
if (extents[j].end != 0)
break;
if (j >= 0 && extents[extent].start <= extents[j].end) {
extents[j].end = extents[extent].end;
extents[extent].start = 0;
extents[extent].end = 0;
}
/* TODO: split extents that are too big */
max = (~(uint32_t)0) - sizeof(struct sparse_chunk_header);
size = extents[extent].end - extents[extent].start;
if (size > max) {
image_error(image, "extents size %llu larger and supported maximum %llu.\n",
(unsigned long long)size, (unsigned long long)max);
ret = -EINVAL;
goto out;
}
}
out_fd = open_file(image, imageoutfile(image), O_TRUNC);
if (out_fd < 0) {
ret = out_fd;
goto out;
}
ret = write_data(image, out_fd, &header, sizeof(header));
if (ret < 0)
goto out;
block = 0;
buf = xzalloc(sparse->block_size);
zeros = xzalloc(sparse->block_size);
memset(zeros, 0, sparse->block_size);
for (extent = 0; extent < extent_count; ++extent) {
uint32_t start_block = extents[extent].start / sparse->block_size;
size_t size = extents[extent].end - extents[extent].start;
uint32_t fill_value = 0;
size_t pos;
/* skip removed extents */
if (size == 0)
continue;
if (block < start_block) {
header.input_chunks++;
chunk_header.chunk_type = SPARSE_DONT_CARE;
chunk_header.blocks = start_block - block;
chunk_header.size = sizeof(chunk_header);
ret = flush_header(image, out_fd, &chunk_header, -1);
if (ret < 0)
return ret;
block = start_block;
for (i = 0; i < chunk_header.blocks; ++i)
crc32 = crc32_next(zeros, sparse->block_size, crc32);
}
offset = lseek(in_fd, extents[extent].start, SEEK_SET);
if (offset < 0) {
ret = -errno;
image_error(image, "seek %s: %s\n", infile, strerror(errno));
goto out;
}
chunk_header.chunk_type = 0;
chunk_header.blocks = 0;
pos = lseek(out_fd, 0, SEEK_CUR);
while (size > 0) {
ssize_t now = min(size, sparse->block_size);
int fill = 1;
ssize_t r = read(in_fd, buf, now);
if (r < 0) {
ret = -errno;
image_error(image, "read %s: %s\n", infile, strerror(errno));
goto out;
}
else if (r != now) {
ret = -EINVAL;
image_error(image, "short read %s %lld != %lld\n", infile,
(long long)r, (long long)now);
goto out;
}
/* The sparse format only allows image sizes that are a multiple of
the block size. Pad the last block as needed. */
if ((uint32_t)r < sparse->block_size) {
memset(((char*)buf)+ r, 0, sparse->block_size - r);
now = sparse->block_size;
}
crc32 = crc32_next(buf, sparse->block_size, crc32);
for (i = 1; i < sparse->block_size/4; ++i) {
if (buf[0] != buf[i]) {
fill = 0;
break;
}
}
if (fill) {
if (chunk_header.chunk_type != SPARSE_FILL ||
fill_value != buf[0]) {
header.input_chunks++;
ret = flush_header(image, out_fd, &chunk_header, pos);
if (ret < 0)
return ret;
pos = lseek(out_fd, 0, SEEK_CUR);
chunk_header.chunk_type = SPARSE_FILL;
chunk_header.size = sizeof(chunk_header) + sizeof(buf[0]);
chunk_header.blocks = 0;
fill_value = buf[0];
ret = flush_header(image, out_fd, &chunk_header, -1);
if (ret < 0)
return ret;
ret = write_data(image, out_fd, buf, sizeof(buf[0]));
if (ret < 0)
goto out;
}
chunk_header.blocks++;
}
else {
if (chunk_header.chunk_type != SPARSE_RAW) {
header.input_chunks++;
ret = flush_header(image, out_fd, &chunk_header, pos);
if (ret < 0)
return ret;
pos = lseek(out_fd, 0, SEEK_CUR);
chunk_header.chunk_type = SPARSE_RAW;
chunk_header.size = sizeof(chunk_header);
chunk_header.blocks = 0;
ret = flush_header(image, out_fd, &chunk_header, -1);
if (ret < 0)
return ret;
}
chunk_header.blocks++;
chunk_header.size += now;
ret = write_data(image, out_fd, buf, now);
if (ret < 0)
goto out;
}
size -= r;
}
ret = flush_header(image, out_fd, &chunk_header, pos);
if (ret < 0)
return ret;
block = (extents[extent].end - 1 + sparse->block_size) / sparse->block_size;
}
if (block < block_count) {
header.input_chunks++;
chunk_header.chunk_type = SPARSE_DONT_CARE;
chunk_header.blocks = block_count - block;
chunk_header.size = sizeof(chunk_header);
ret = flush_header(image, out_fd, &chunk_header, -1);
if (ret < 0)
return ret;
for (i = 0; i < chunk_header.blocks; ++i)
crc32 = crc32_next(zeros, sparse->block_size, crc32);
}
header.input_chunks++;
chunk_header.chunk_type = SPARSE_CRC32;
chunk_header.blocks = 0;
chunk_header.size = sizeof(chunk_header) + sizeof(crc32);
ret = flush_header(image, out_fd, &chunk_header, -1);
if (ret < 0)
return ret;
ret = write_data(image, out_fd, &crc32, sizeof(crc32));
if (ret < 0)
goto out;
offset = lseek(out_fd, 0, SEEK_SET);
if (offset < 0) {
ret = -errno;
image_error(image, "seek %s: %s\n", infile, strerror(errno));
goto out;
}
ret = write_data(image, out_fd, &header, sizeof(header));
if (ret < 0)
goto out;
image_info(image, "sparse image with %u chunks and %u blocks\n",
header.input_chunks, header.output_blocks);
out:
close(in_fd);
if (out_fd >= 0)
close(out_fd);
if (extents)
free(extents);
return ret;
}
static int android_sparse_parse(struct image *image, cfg_t *cfg)
{
struct partition *part;
char *src;
src = cfg_getstr(image->imagesec, "image");
if (!src) {
image_error(image, "Mandatory 'image' option is missing!\n");
return -EINVAL;
}
image_info(image, "input image: %s\n", src);
part = xzalloc(sizeof *part);
part->image = src;
list_add_tail(&part->list, &image->partitions);
return 0;
}
static int android_sparse_setup(struct image *image, cfg_t *cfg)
{
struct sparse *sparse = xzalloc(sizeof(*sparse));
sparse->block_size = cfg_getint_suffix(cfg, "block-size");
if (sparse->block_size % 512) {
image_error(image, "block-size %u invalid. It must be a multiple of 512!\n",
sparse->block_size);
return -EINVAL;
}
image->handler_priv = sparse;
return 0;
}
static cfg_opt_t android_sparse_opts[] = {
CFG_STR("image", NULL, CFGF_NONE),
CFG_STR("block-size", "4k", CFGF_NONE),
CFG_END()
};
struct image_handler android_sparse_handler = {
.type = "android-sparse",
.no_rootpath = cfg_true,
.generate = android_sparse_generate,
.parse = android_sparse_parse,
.setup = android_sparse_setup,
.opts = android_sparse_opts,
};