Skip to content

Commit 8e63b00

Browse files
image-android-sparse: add option to fill holes with zeros
By default 'holes' are converted into "don't care" areas. This adds an option to fill the area with zeros instead. Signed-off-by: Michael Olbrich <[email protected]>
1 parent 9b9aa24 commit 8e63b00

File tree

6 files changed

+28
-5
lines changed

6 files changed

+28
-5
lines changed

Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ EXTRA_DIST += \
146146
test/rauc.config \
147147
test/sharness.sh \
148148
test/sparse.config \
149+
test/sparse-fill.config \
149150
test/squashfs.config \
150151
test/tar.config \
151152
test/test.raucb.info.1 \

README.rst

+2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ Options:
251251
:block-size: The granularity that the sparse image uses to
252252
find "don't care" or "fill" blocks. The supported
253253
block sizes depend on the user. The default is 4k.
254+
:fill-holes: If enabled, 'holes' are filled with zero instead of
255+
"don't care". Disabled by default.
254256

255257
cpio
256258
****

genimage.h

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ struct extent {
175175
};
176176

177177
int open_file(struct image *image, const char *filename, int extra_flags);
178+
int whole_file_exent(size_t size, struct extent **extents,
179+
size_t *extent_count);
178180
int map_file_extents(struct image *image, const char *filename, int fd,
179181
size_t size, struct extent **extents, size_t *extent_count);
180182
int is_block_device(const char *filename);

image-android-sparse.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
struct sparse {
3030
uint32_t block_size;
31+
cfg_bool_t fill_holes;
3132
};
3233

3334
struct sparse_header {
@@ -147,9 +148,13 @@ static int android_sparse_generate(struct image *image)
147148
block_count = (s.st_size - 1 + sparse->block_size) / sparse->block_size;
148149
header.output_blocks = block_count;
149150

150-
ret = map_file_extents(inimage, infile, in_fd, s.st_size, &extents, &extent_count);
151-
if (ret < 0)
152-
goto out;
151+
if (sparse->fill_holes)
152+
whole_file_exent(s.st_size, &extents, &extent_count);
153+
else {
154+
ret = map_file_extents(inimage, infile, in_fd, s.st_size, &extents, &extent_count);
155+
if (ret < 0)
156+
goto out;
157+
}
153158

154159
/* The extents may have a different granularity than the chosen block size.
155160
So all start and end of all extents must be aligned accordingly. The
@@ -391,6 +396,7 @@ static int android_sparse_setup(struct image *image, cfg_t *cfg)
391396
sparse->block_size);
392397
return -EINVAL;
393398
}
399+
sparse->fill_holes = cfg_getbool(cfg, "fill-holes");
394400

395401
image->handler_priv = sparse;
396402
return 0;
@@ -399,6 +405,7 @@ static int android_sparse_setup(struct image *image, cfg_t *cfg)
399405
static cfg_opt_t android_sparse_opts[] = {
400406
CFG_STR("image", NULL, CFGF_NONE),
401407
CFG_STR("block-size", "4k", CFGF_NONE),
408+
CFG_BOOL("fill-holes", cfg_false, CFGF_NONE),
402409
CFG_END()
403410
};
404411

test/misc.test

+11
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ test_expect_success simg2img "android-sparse" "
9393
simg2img images/test.sparse images/test.hdimage &&
9494
simg2img images/interleaved.sparse input/interleaved &&
9595
simg2img images/not-aligned.sparse input/not-aligned &&
96+
md5sum -c md5sum &&
97+
98+
run_genimage sparse-fill.config &&
99+
# simg2img will expand the partial block
100+
truncate --size=12k input/not-aligned
101+
md5sum images/test.hdimage input/interleaved input/not-aligned > md5sum &&
102+
rm images/test.hdimage input/interleaved input/not-aligned &&
103+
check_size_range images/interleaved.sparse 9732464 9732636 &&
104+
simg2img images/test.sparse images/test.hdimage &&
105+
simg2img images/interleaved.sparse input/interleaved &&
106+
simg2img images/not-aligned.sparse input/not-aligned &&
96107
md5sum -c md5sum
97108
"
98109

util.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,8 @@ int open_file(struct image *image, const char *filename, int extra_flags)
398398
}
399399

400400
/* Build a file extent covering the whole file */
401-
static int whole_file_exent(size_t size, struct extent **extents,
402-
size_t *extent_count)
401+
int whole_file_exent(size_t size, struct extent **extents,
402+
size_t *extent_count)
403403
{
404404
*extents = xzalloc(sizeof(struct extent));
405405
(*extents)[0].start = 0;

0 commit comments

Comments
 (0)