Skip to content

Commit

Permalink
Support multiple tasks in --task
Browse files Browse the repository at this point in the history
Based on the discussion in fwup-home#185

The idea is that you can specify a list of tasks with `-t` and fwup
will try each one until the first success supporting a "fallback"
behavior
  • Loading branch information
jjcarstens committed Sep 17, 2022
1 parent c9918f7 commit e9619b8
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ struct fun_context {
// Task configuration
cfg_t *task;

// Lists of task configurations
cfg_t *tasks[16];

// When processing events (on-init, on-resource, on-finish, etc.) this is that configuration
cfg_t *on_event;

Expand Down
38 changes: 35 additions & 3 deletions src/fwup_apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ static cfg_t *find_task(struct fun_context *fctx, const char *task_prefix)
return 0;
}

static int find_tasks(struct fun_context *fctx, const char *tasks_prefix)
{
int rc = -1;
int i = 0;
char *tasks_copy = strdup(tasks_prefix);
for (char *task_prefix = strtok(tasks_copy, ":"); task_prefix != NULL; task_prefix = strtok(NULL, ";")) {
fctx->tasks[i] = find_task(fctx, task_prefix);
// First task found sets return code as successful
if (rc < 0 && fctx->tasks[i] != 0)
rc = 0;
i++;
}
free(tasks_copy);
return rc;
}

static int apply_event(struct fun_context *fctx, cfg_t *task, const char *event_type, const char *event_parameter, int (*fun)(struct fun_context *fctx))
{
if (event_parameter)
Expand Down Expand Up @@ -452,6 +468,20 @@ static int run_task(struct fun_context *fctx, struct fwup_apply_data *pd)
return rc;
}

static int run_tasks(struct fun_context *fctx, struct fwup_apply_data *pd)
{
int rc = 0;
for (int i = 0; fctx->tasks[i] != 0; i++) {
fctx->task = fctx->tasks[i];
rc = run_task(fctx, pd);
if (rc == 0)
return 0;
// Maybe we should report this task failed and moving on to the next one?
}
// No task was successful
return -1;
}

int fwup_apply(const char *fw_filename,
const char *task_prefix,
int output_fd,
Expand Down Expand Up @@ -511,15 +541,17 @@ int fwup_apply(const char *fw_filename,
OK_OR_CLEANUP(block_cache_init(fctx.output, output_fd, end_offset, enable_trim, verify_writes));

// Go through all of the tasks and find a matcher
fctx.task = find_task(&fctx, task_prefix);
if (fctx.task == 0)
// fctx.task = find_task(&fctx, task_prefix);
int task_rc = find_tasks(&fctx, task_prefix);
if (task_rc < 0)
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));

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

// Flush everything
fatfs_closefs();
Expand Down
42 changes: 42 additions & 0 deletions tests/192_multiple_tasks.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#/bin/sh

#
# Test that multiple tasks are attempted until success with a task list
#

. "$(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

# The upgrade task does not exist, so it should fallback to the complete task
$FWUP_APPLY -a -d $IMGFILE -i $FWFILE -t upgrade:complete
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_multiple_tasks.test

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

0 comments on commit e9619b8

Please sign in to comment.