From: Andreas Reichel <[email protected]> Add --with-num-config-parts=XX to define number of config partitions at configuration.
Signed-off-by: Andreas Reichel <[email protected]> --- configure.ac | 15 +++++++++++++++ docs/TODO.md | 4 ---- env/env_api_fat.c | 22 +++++++++++----------- env/fatvars.c | 16 +++++++--------- include/envdata.h | 1 - swupdate-adapter/ebgenv.c | 4 ++-- tools/bg_setenv.c | 2 +- tools/tests/test_environment.c | 4 ++-- tools/tests/test_partitions.c | 38 +++++++++++--------------------------- 9 files changed, 49 insertions(+), 57 deletions(-) diff --git a/configure.ac b/configure.ac index fdff617..b12d74b 100644 --- a/configure.ac +++ b/configure.ac @@ -133,6 +133,20 @@ AC_ARG_WITH([env-backend], [ ENV_API_FILE="env_api_fat" ]) AC_SUBST([env_api_file], [${ENV_API_FILE}]) + +AC_ARG_WITH([num-config-parts], + AS_HELP_STRING([--with-num-config-parts=INT], + [specify the number of used config partitions, defaults to 2]), + [ + ENV_NUM_CONFIG_PARTS=${withval:-0} + AS_IF([test "${ENV_NUM_CONFIG_PARTS}" -lt "1"], + [ + AC_MSG_ERROR([Invalid number of config partitions.]) + ]) + ], + [ ENV_NUM_CONFIG_PARTS=2 ]) + +AC_DEFINE_UNQUOTED([ENV_NUM_CONFIG_PARTS], [${ENV_NUM_CONFIG_PARTS}], [Number of config partitions]) # ------------------------------------------------------------------------------ AC_CONFIG_FILES([ Makefile @@ -154,4 +168,5 @@ AC_MSG_RESULT([ efi libs: ${GNUEFI_LIB_DIR} environment backend: ${ENV_API_FILE}.c + number of config parts: ${ENV_NUM_CONFIG_PARTS} ]) diff --git a/docs/TODO.md b/docs/TODO.md index 832f76b..36c5dfb 100644 --- a/docs/TODO.md +++ b/docs/TODO.md @@ -29,8 +29,4 @@ needed then. * Function / Datatype / Variable names remind of Parted and should be renamed if code developes independent of libparted. - * The number of valid config partitions expected by the bootloader and - the tools is currently fixed to the number defined by - `CONFIG_PARTITION_COUNT` in `include/envdata.h`. This value should be - made configurable by a config flag. diff --git a/env/env_api_fat.c b/env/env_api_fat.c index ee8c5b3..7398355 100644 --- a/env/env_api_fat.c +++ b/env/env_api_fat.c @@ -256,11 +256,11 @@ bool probe_config_partitions(CONFIG_PART *cfgpart) strlen(devpath) + 1); if (probe_config_file(&cfgpart[count])) { printf_debug("%s", "Environment file found.\n"); - if (count >= CONFIG_PARTITION_COUNT) { + if (count >= ENV_NUM_CONFIG_PARTS) { VERBOSE(stderr, "Error, there are " "more than %d config " "partitions.\n", - CONFIG_PARTITION_COUNT); + ENV_NUM_CONFIG_PARTS); return false; } count++; @@ -268,10 +268,10 @@ bool probe_config_partitions(CONFIG_PART *cfgpart) part = ped_disk_next_partition(pd, part); } } - if (count < CONFIG_PARTITION_COUNT) { + if (count < ENV_NUM_CONFIG_PARTS) { VERBOSE(stderr, "Error, less than %d config partitions exist.\n", - CONFIG_PARTITION_COUNT); + ENV_NUM_CONFIG_PARTS); return false; } return true; @@ -350,21 +350,21 @@ bool write_env(CONFIG_PART *part, BG_ENVDATA *env) return result; } -CONFIG_PART config_parts[CONFIG_PARTITION_COUNT]; -BG_ENVDATA oldenvs[CONFIG_PARTITION_COUNT]; +CONFIG_PART config_parts[ENV_NUM_CONFIG_PARTS]; +BG_ENVDATA oldenvs[ENV_NUM_CONFIG_PARTS]; bool bgenv_init(BGENVTYPE type) { switch (type) { case BGENVTYPE_FAT: memset((void *)&config_parts, 0, - sizeof(CONFIG_PART) * CONFIG_PARTITION_COUNT); + sizeof(CONFIG_PART) * ENV_NUM_CONFIG_PARTS); /* enumerate all config partitions */ if (!probe_config_partitions(config_parts)) { VERBOSE(stderr, "Error finding config partitions.\n"); return false; } - for (int i = 0; i < CONFIG_PARTITION_COUNT; i++) { + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { read_env(&config_parts[i], &oldenvs[i]); uint32_t sum = crc32(0, (Bytef *)&oldenvs[i], sizeof(BG_ENVDATA) - sizeof(oldenvs[i].crc32)); @@ -385,7 +385,7 @@ BGENV *bgenv_get_by_index(BGENVTYPE type, uint32_t index) switch (type) { case BGENVTYPE_FAT: /* get config partition by index and allocate handle */ - if (index >= CONFIG_PARTITION_COUNT) { + if (index >= ENV_NUM_CONFIG_PARTS) { return NULL; } if (!(handle = calloc(1, sizeof(BGENV)))) { @@ -406,7 +406,7 @@ BGENV *bgenv_get_oldest(BGENVTYPE type) switch (type) { case BGENVTYPE_FAT: - for (int i = 0; i < CONFIG_PARTITION_COUNT; i++) { + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { if (oldenvs[i].revision < minrev) { minrev = oldenvs[i].revision; min_idx = i; @@ -424,7 +424,7 @@ BGENV *bgenv_get_latest(BGENVTYPE type) switch (type) { case BGENVTYPE_FAT: - for (int i = 0; i < CONFIG_PARTITION_COUNT; i++) { + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { if (oldenvs[i].revision > maxrev) { maxrev = oldenvs[i].revision; max_idx = i; diff --git a/env/fatvars.c b/env/fatvars.c index 9d63091..406bdf9 100644 --- a/env/fatvars.c +++ b/env/fatvars.c @@ -15,10 +15,8 @@ #include <syspart.h> #include <envdata.h> -#define CONFIG_PARTITION_COUNT 2 - static int current_partition = 0; -static BG_ENVDATA env[CONFIG_PARTITION_COUNT]; +static BG_ENVDATA env[ENV_NUM_CONFIG_PARTS]; BG_STATUS save_current_config(void) { @@ -41,10 +39,10 @@ BG_STATUS save_current_config(void) return BG_CONFIG_ERROR; } - if (numHandles != CONFIG_PARTITION_COUNT) { + if (numHandles != ENV_NUM_CONFIG_PARTS) { Print(L"Error, unexpected number of config partitions: found " L"%d, but expected %d.\n", - numHandles, CONFIG_PARTITION_COUNT); + numHandles, ENV_NUM_CONFIG_PARTS); /* In case of saving, this must be treated as error, to not * overwrite another partition's config file. */ mfree(roots); @@ -90,7 +88,7 @@ BG_STATUS load_config(BG_LOADER_PARAMS *bglp) UINTN numHandles = CONFIG_PARTITION_MAXCOUNT; EFI_FILE_HANDLE *roots; UINTN i; - int env_invalid[CONFIG_PARTITION_COUNT] = {0}; + int env_invalid[ENV_NUM_CONFIG_PARTS] = {0}; roots = (EFI_FILE_HANDLE *)mmalloc(sizeof(EFI_FILE_HANDLE) * CONFIG_PARTITION_MAXCOUNT); @@ -106,10 +104,10 @@ BG_STATUS load_config(BG_LOADER_PARAMS *bglp) return BG_CONFIG_ERROR; } - if (numHandles != CONFIG_PARTITION_COUNT) { + if (numHandles != ENV_NUM_CONFIG_PARTS) { Print(L"Warning, unexpected number of config partitions: found " L"%d, but expected %d.\n", - numHandles, CONFIG_PARTITION_COUNT); + numHandles, ENV_NUM_CONFIG_PARTS); /* Don't treat this as error because we may still be able to * find a * valid config */ @@ -174,7 +172,7 @@ BG_STATUS load_config(BG_LOADER_PARAMS *bglp) * configuration. */ UINTN latest_rev = 0, latest_idx = 0; UINTN pre_latest_rev = 0, pre_latest_idx = 0; - for (i = 0; i < CONFIG_PARTITION_COUNT; i++) { + for (i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { if (!env_invalid[i]) { if (env[i].revision > latest_rev) { pre_latest_rev = latest_rev; diff --git a/include/envdata.h b/include/envdata.h index f62ce35..f5b8070 100644 --- a/include/envdata.h +++ b/include/envdata.h @@ -16,7 +16,6 @@ #define FAT_ENV_FILENAME "BGENV.DAT" #define ENV_STRING_LENGTH 255 -#define CONFIG_PARTITION_COUNT 2 #define CONFIG_PARTITION_MAXCOUNT 64 #pragma pack(push) diff --git a/swupdate-adapter/ebgenv.c b/swupdate-adapter/ebgenv.c index 9ac42fc..d5f7080 100644 --- a/swupdate-adapter/ebgenv.c +++ b/swupdate-adapter/ebgenv.c @@ -327,7 +327,7 @@ int ebg_env_set(char *key, char *value) bool ebg_env_isupdatesuccessful(void) { /* find all environments with revision 0 */ - for (int i = 0; i < CONFIG_PARTITION_COUNT; i++) { + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { BGENV *env = bgenv_get_by_index(BGENVTYPE_FAT, i); if (!env) { @@ -348,7 +348,7 @@ bool ebg_env_isupdatesuccessful(void) int ebg_env_clearerrorstate(void) { - for (int i = 0; i < CONFIG_PARTITION_COUNT; i++) { + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { BGENV *env = bgenv_get_by_index(BGENVTYPE_FAT, i); if (!env) { diff --git a/tools/bg_setenv.c b/tools/bg_setenv.c index 570ae6d..542653c 100644 --- a/tools/bg_setenv.c +++ b/tools/bg_setenv.c @@ -264,7 +264,7 @@ int main(int argc, char **argv) "Error initializing FAT environment.\n"); return 1; } - for (int i = 0; i < CONFIG_PARTITION_COUNT; i++) { + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { if (verbosity) { printf("\n----------------------------\n"); printf(" Config Partition #%d ", i); diff --git a/tools/tests/test_environment.c b/tools/tests/test_environment.c index 56395b7..61fd417 100644 --- a/tools/tests/test_environment.c +++ b/tools/tests/test_environment.c @@ -22,8 +22,8 @@ /* Mock functions from libparted */ -CONFIG_PART config_parts[CONFIG_PARTITION_COUNT]; -BG_ENVDATA oldenvs[CONFIG_PARTITION_COUNT]; +CONFIG_PART config_parts[ENV_NUM_CONFIG_PARTS]; +BG_ENVDATA oldenvs[ENV_NUM_CONFIG_PARTS]; FILE test_file; diff --git a/tools/tests/test_partitions.c b/tools/tests/test_partitions.c index e292200..62f9cc1 100644 --- a/tools/tests/test_partitions.c +++ b/tools/tests/test_partitions.c @@ -101,37 +101,22 @@ static void test_partition_count(void **state) bool ret; num_simulated_devices = 1; - num_simulated_partitions_per_disk = CONFIG_PARTITION_COUNT; - for (int i = 0; i < CONFIG_PARTITION_COUNT; i++) { + num_simulated_partitions_per_disk = ENV_NUM_CONFIG_PARTS; + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { will_return(probe_config_file, true); } ret = probe_config_partitions(cfgparts); assert_true(ret); - num_simulated_devices = CONFIG_PARTITION_COUNT; + num_simulated_devices = ENV_NUM_CONFIG_PARTS; num_simulated_partitions_per_disk = 1; - for (int i = 0; i < CONFIG_PARTITION_COUNT; i++) { + for (int i = 0; i < ENV_NUM_CONFIG_PARTS - 1; i++) { will_return(probe_config_file, true); } + will_return(probe_config_file, true); ret = probe_config_partitions(cfgparts); assert_true(ret); - num_simulated_devices = 1; - num_simulated_partitions_per_disk = CONFIG_PARTITION_COUNT - 1; - for (int i = 0; i < CONFIG_PARTITION_COUNT - 1; i++) { - will_return(probe_config_file, true); - } - ret = probe_config_partitions(cfgparts); - assert_false(ret); - - num_simulated_devices = 1; - num_simulated_partitions_per_disk = CONFIG_PARTITION_COUNT + 1; - for (int i = 0; i < CONFIG_PARTITION_COUNT + 1; i++) { - will_return(probe_config_file, true); - } - ret = probe_config_partitions(cfgparts); - assert_false(ret); - (void)state; } @@ -141,19 +126,18 @@ static void test_config_file_existence(void **state) bool ret; num_simulated_devices = 1; - num_simulated_partitions_per_disk = CONFIG_PARTITION_COUNT; - for (int i = 0; i < CONFIG_PARTITION_COUNT; i++) { + num_simulated_partitions_per_disk = ENV_NUM_CONFIG_PARTS; + for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) { will_return(probe_config_file, false); } ret = probe_config_partitions(cfgparts); assert_false(ret); - if (CONFIG_PARTITION_COUNT > 1) { - for (int i = 0; i < CONFIG_PARTITION_COUNT - 1; i++) { - will_return(probe_config_file, true); - } - will_return(probe_config_file, false); + for (int i = 0; i < ENV_NUM_CONFIG_PARTS - 1; i++) { + will_return(probe_config_file, true); } + will_return(probe_config_file, false); + ret = probe_config_partitions(cfgparts); assert_false(ret); -- 2.14.1 -- You received this message because you are subscribed to the Google Groups "EFI Boot Guard" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/efibootguard-dev/20170926115638.2006-6-andreas.reichel.ext%40siemens.com. For more options, visit https://groups.google.com/d/optout.
