From: Andreas Reichel <andreas.reichel....@siemens.com>

Add --with-num-config-parts=XX to define number of config partitions
at configuration.

Signed-off-by: Andreas Reichel <andreas.reichel....@siemens.com>
---
 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 e235341..aa9e314 100644
--- a/configure.ac
+++ b/configure.ac
@@ -142,6 +142,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
@@ -163,5 +177,6 @@ AC_MSG_RESULT([
        efi lds:                 ${GNUEFI_LDS_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 93b85a1..bed74d9 100644
--- a/env/env_api_fat.c
+++ b/env/env_api_fat.c
@@ -254,11 +254,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++;
@@ -266,10 +266,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;
@@ -348,21 +348,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));
@@ -383,7 +383,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)))) {
@@ -404,7 +404,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;
@@ -422,7 +422,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 63df653..7402121 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 4387ecd..237b01e 100644
--- a/tools/tests/test_partitions.c
+++ b/tools/tests/test_partitions.c
@@ -96,37 +96,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;
 }
 
@@ -136,19 +121,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 efibootguard-dev+unsubscr...@googlegroups.com.
To post to this group, send email to efibootguard-dev@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20170912105511.11060-7-andreas.reichel.ext%40siemens.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to