Skip to content

Commit

Permalink
Allow specifying a fallback task
Browse files Browse the repository at this point in the history
In very specific cases, it might be desirable to fallback to another
task when the target task fails. Most commonly this will be falling
back to the `complete` task when the `upgrade` fails due to the firmware
not having a previous firmware.

The fallback will only occur if the failure is withing the tasks functions
so checks before and after the task do not trigger a fallback (i.e. issues
with archive or config file)

This is inspired by nerves-project/nerves#682
  • Loading branch information
jjcarstens committed Jan 17, 2022
1 parent 3dde4b2 commit afe5645
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/fwup.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ static void print_usage()
printf(" --sparse-check <path> Check if the OS and file system supports sparse files at path\n");
printf(" --sparse-check-size <bytes> Hole size to check for --sparse-check\n");
printf(" -t, --task <task> Task to apply within the firmware update\n");
printf(" -T, --fallback-task <task> Fallback task to apply within the firmware update if the first task fails\n");
printf(" -u, --unmount Unmount all partitions on device first\n");
printf(" -U, --no-unmount Do not try to unmount partitions on device\n");
printf(" --unsafe Allow unsafe commands (consider applying only signed archives)\n");
Expand Down Expand Up @@ -207,6 +208,7 @@ static struct option long_options[] = {
{"sparse-check-size", required_argument, 0, OPTION_SPARSE_CHECK_SIZE},
{"sign", no_argument, 0, 'S'},
{"task", required_argument, 0, 't'},
{"fallback-task", required_argument, 0, 'T'},
{"unmount", no_argument, 0, 'u'},
{"no-unmount", no_argument, 0, 'U'},
{"unsafe", no_argument, 0, OPTION_UNSAFE},
Expand Down Expand Up @@ -376,6 +378,7 @@ int main(int argc, char **argv)
const char *input_filename = NULL;
const char *output_filename = NULL;
const char *task = NULL;
const char *fallback_task = NULL;
#ifndef FWUP_MINIMAL
const char *configfile = "fwupdate.conf";
const char *sparse_check = NULL;
Expand Down Expand Up @@ -413,7 +416,7 @@ int main(int argc, char **argv)
atexit(mmc_finalize);

int opt;
while ((opt = getopt_long(argc, argv, "acd:DEf:Fghi:lmno:p:qSs:t:VvUuyZz123456789", long_options, NULL)) != -1) {
while ((opt = getopt_long(argc, argv, "acd:DEf:Fghi:lmno:p:qSs:t:T:VvUuyZz123456789", long_options, NULL)) != -1) {
switch (opt) {
case 'a': // --apply
command = CMD_APPLY;
Expand Down Expand Up @@ -520,6 +523,9 @@ int main(int argc, char **argv)
case 't': // --task
task = optarg;
break;
case 'T': // --fallback-task
fallback_task = optarg;
break;
case 'v': // --verbose
fwup_verbose = true;
break;
Expand Down Expand Up @@ -713,6 +719,7 @@ int main(int argc, char **argv)

if (fwup_apply(input_filename,
task,
fallback_task,
output_fd,
end_offset,
&progress,
Expand Down
30 changes: 25 additions & 5 deletions src/fwup_apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ static int run_task(struct fun_context *fctx, struct fwup_apply_data *pd)

int fwup_apply(const char *fw_filename,
const char *task_prefix,
const char *fb_task_prefix,
int output_fd,
off_t end_offset,
struct fwup_progress *progress,
Expand Down Expand Up @@ -512,14 +513,28 @@ int fwup_apply(const char *fw_filename,

// Go through all of the tasks and find a matcher
fctx.task = find_task(&fctx, task_prefix);
if (fctx.task == 0)
cfg_t *fallback_task = 0;
if (fb_task_prefix != NULL) {
fallback_task = find_task(&fctx, fb_task_prefix);
}

if (fctx.task == 0 && fb_task_prefix == NULL)
ERR_CLEANUP_MSG("Couldn't find applicable task '%s'. If task is available, the task's requirements may not be met.", task_prefix);

// Compute the total progress units
OK_OR_CLEANUP(compute_progress(&fctx));
if (fctx.task == 0 && fallback_task == 0)
ERR_CLEANUP_MSG("Couldn't find applicable task '%s' or fallback task '%s'. If either task is available, the requirements may not be met.", task_prefix, fb_task_prefix);

if (fctx.task == 0) {
fctx.task = fallback_task;
fwup_warnx("'%s' task not found. Using fallback task '%s'", task_prefix, fb_task_prefix);
}

// Run
OK_OR_CLEANUP(run_task(&fctx, &pd));
fallback:
// Compute the total progress units
OK_OR_CLEANUP(compute_progress(&fctx));

// Run
OK_OR_CLEANUP(run_task(&fctx, &pd));

// Flush everything
fatfs_closefs();
Expand All @@ -535,6 +550,11 @@ int fwup_apply(const char *fw_filename,
progress_report_complete(fctx.progress);

cleanup:
if (fallback_task != 0 && fctx.task != fallback_task) {
fctx.task = fallback_task;
goto fallback;
}

// Close the output
if (fctx.output) {
// In the error case, flush in case the on-error
Expand Down
1 change: 1 addition & 0 deletions src/fwup_apply.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct fwup_progress;

int fwup_apply(const char *fw_filename,
const char *task,
const char *fb_task,
int output_fd,
off_t end_offset,
struct fwup_progress *progress,
Expand Down
42 changes: 42 additions & 0 deletions tests/192_fallback_task.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/sh

#
# Test the fallback task ability when initial task fails
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

cat >$CONFIG <<EOF
file-resource TEST {
host-path = "${TESTFILE_1K}"
}
task complete {
on-resource TEST { raw_write(0) }
}
EOF

cat >$EXPECTED_META_CONF <<EOF
file-resource "TEST" {
length=1024
blake2b-256="b25c2dfe31707f5572d9a3670d0dcfe5d59ccb010e6aba3b81aad133eb5e378b"
}
task "complete" {
on-resource "TEST" {
funlist = {"2", "raw_write", "0"}
}
}
EOF

$FWUP_CREATE -c -f $CONFIG -o $FWFILE

# Check that the zip file was created as expected
check_meta_conf
cmp $TESTFILE_1K $UNZIPDIR/data/TEST

# Check that applying the firmware does the expected thing
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t upgrade -T complete --verify-writes
cmp_bytes 1024 $IMGFILE $TESTFILE_1K

# Check that the verify logic works on this file
$FWUP_VERIFY -V -i $FWFILE
3 changes: 2 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ TESTS = 001_simple_fw.test \
188_uboot_redundant_bad_param.test \
189_uboot_redundant_recover.test \
190_one_metadata_cmdline.test \
191_disk_crypto_via_env.test
191_disk_crypto_via_env.test \
192_fallback_test.test

EXTRA_DIST = $(TESTS) common.sh 1K.bin 1K-corrupt.bin 150K.bin

0 comments on commit afe5645

Please sign in to comment.