Skip to content

Commit

Permalink
image-rauc: allow to specify an offset to skip input bytes
Browse files Browse the repository at this point in the history
This is useful for barebox images on (at least) i.MX8 to be written to
eMMC. There the first 32K of the image must be skipped.

Signed-off-by: Uwe Kleine-König <[email protected]>
  • Loading branch information
Uwe Kleine-König committed Mar 22, 2024
1 parent fbad79c commit f335bcd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
1 change: 1 addition & 0 deletions genimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct partition {
cfg_bool_t no_automount;
cfg_bool_t fill;
const char *image;
off_t imageoffset;
struct list_head list;
int autoresize;
int in_partition_table;
Expand Down
36 changes: 32 additions & 4 deletions image-rauc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

#include "genimage.h"

Expand Down Expand Up @@ -68,6 +69,7 @@ static int rauc_generate(struct image *image)
struct image *child = image_get(part->image);
const char *file = imageoutfile(child);
const char *target = part->name;
char *tmptarget;
char *path, *tmp;

if (part->partition_type == RAUC_CERT)
Expand Down Expand Up @@ -105,10 +107,34 @@ static int rauc_generate(struct image *image)
goto out;
}

image_info(image, "adding file '%s' as '%s' ...\n",
child->file, target);
ret = systemp(image, "cp --remove-destination '%s' '%s/%s'",
file, tmpdir, target);
xasprintf(&tmptarget, "%s/%s", tmpdir, target);

image_info(image, "adding file '%s' as '%s' (offset=%lld)...\n",
child->file, target, (long long)part->imageoffset);

if (part->imageoffset) {
unlink(tmptarget);

/*
* Starting with coreutils 9.1 you can use a 'B' suffix for
* skip=N instead of iflag=skip_bytes to have N count bytes, not
* (input) blocks.
*
* Note that dd doesn't behave as optimal as cp in the
* else branch below because it doesn't preserve holes.
* To improve here insert_image() should be extended to
* support part->imageoffset != 0 and then it can
* replace both commands.
*/
ret = systemp(image, "dd if='%s' of='%s' iflag=skip_bytes skip=%lld",
file, tmptarget, (long long)part->imageoffset);

} else {
ret = systemp(image, "cp --remove-destination '%s' '%s'",
file, tmptarget);
}

free(tmptarget);
if (ret)
goto out;
}
Expand Down Expand Up @@ -193,6 +219,7 @@ static int rauc_parse(struct image *image, cfg_t *cfg)
part = xzalloc(sizeof *part);
part->name = cfg_title(filesec);
part->image = cfg_getstr(filesec, "image");
part->imageoffset = cfg_getint_suffix(filesec, "offset");
part->partition_type = RAUC_CONTENT;
list_add_tail(&part->list, &image->partitions);
}
Expand All @@ -219,6 +246,7 @@ static int rauc_setup(struct image *image, cfg_t *cfg)

static cfg_opt_t file_opts[] = {
CFG_STR("image", NULL, CFGF_NONE),
CFG_STR("offset", "0", CFGF_NONE),
CFG_END()
};

Expand Down
6 changes: 3 additions & 3 deletions test/misc.test
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ setup_rauc() {
mkdir input &&
cp -r "${testdir}"/rauc-openssl-ca input/ &&
echo "test" > input/rauc.content &&
echo "test2" > input/rauc2.content
echo "xtest2" > input/rauc2.content
}

exec_test_set_prereq rauc
Expand All @@ -41,7 +41,7 @@ test_expect_success rauc "rauc" "
rauc info \
--keyring input/rauc-openssl-ca/ca.cert.pem \
--cert input/rauc-openssl-ca/ca.cert.pem \
images/test.raucb | grep -v cms_get_enveloped_type \
images/test.raucb | grep -vE '(cms_get_enveloped_type|Manifest Hash)' \
| sed -e 's;O = Test Org, CN = ;/O=Test Org/CN=;' \
-e '/Bundle Format:[ \t]*plain$/d' \
> test.raucb.info &&
Expand All @@ -53,7 +53,7 @@ test_expect_success rauc "rauc" "
rauc info \
--keyring input/rauc-openssl-ca/ca.cert.pem \
--cert input/rauc-openssl-ca/ca.cert.pem \
images/test2.raucb | grep -v cms_get_enveloped_type \
images/test2.raucb | grep -vE '(cms_get_enveloped_type|Manifest Hash)' \
| sed -e 's;O = Test Org, CN = ;/O=Test Org/CN=;' \
-e '/Bundle Format:[ \t]*plain$/d' \
> test2.raucb.info &&
Expand Down
5 changes: 4 additions & 1 deletion test/rauc.config
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ image test.raucb {
}
image test2.raucb {
rauc {
file data { image = "rauc2.content" }
file data {
image = "rauc2.content"
offset = 1
}
manifest = "
[update]
compatible=genimage-test
Expand Down

0 comments on commit f335bcd

Please sign in to comment.