Hi,

On 08/22/2017 06:15 PM, [ext] Reichel Andreas wrote:
From: Andreas Reichel <[email protected]>

From: Reichel Andreas <[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                   | 21 +++++++++++++++++++++
  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, 55 insertions(+), 57 deletions(-)

diff --git a/configure.ac b/configure.ac
index 403ad4b..1974fd8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -168,6 +168,26 @@ AC_ARG_WITH([env-type],
  AC_SUBST(ENV_STORAGE)
  AC_SUBST([env_api_file], [${ENV_API_FILE}])
  AC_DEFINE_UNQUOTED(ENV_STORAGE, [${ENV_STORAGE}], [The storage backend for 
environment data])
+
+AC_ARG_WITH([num-config-parts],
+           AS_HELP_STRING([--with-num-config-parts=INT],
+                          [specify the number of used config partitions, 
defaults to 2]),
+           [
+               AS_IF([test "${ENV_STORAGE}" -ne "1"],
+                     [
+                       AC_MSG_ERROR([Config partitions only usable for FAT 
storage.])
+                     ])
+               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
+           ])

For me that is a bit too much logic in the configure script, especially since it uses intrinsic knowledge about environment storage plugins etc. I would rather prefer this to be done in the precompiler step. This way it would be possible that each env storage plugin can handle their checks next to the code.

I could imagine a future plugin that don't uses config partitions at all, handling all cases here is a bit misplaced IMO.

Claudius

+
+AC_DEFINE_UNQUOTED([ENV_NUM_CONFIG_PARTS], [${ENV_NUM_CONFIG_PARTS}], [Number 
of config partitions])
  # 
------------------------------------------------------------------------------
  AC_CONFIG_FILES([
          Makefile
@@ -189,6 +209,7 @@ AC_MSG_RESULT([
          efi lds:                 ${GNUEFI_LDS_DIR}
environment storage: $ENV_STORAGE_TYPE
+       number of config parts:  $ENV_NUM_CONFIG_PARTS
build and run tests: $enable_tests
  ])
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 f282a3f..de82b8c 100644
--- a/env/env_api_fat.c
+++ b/env/env_api_fat.c
@@ -253,11 +253,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++;
@@ -265,10 +265,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;
@@ -347,21 +347,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));
@@ -382,7 +382,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)))) {
@@ -403,7 +403,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;
@@ -421,7 +421,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 946ce92..525a96e 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 c6865e4..f49b35d 100644
--- a/tools/tests/test_partitions.c
+++ b/tools/tests/test_partitions.c
@@ -95,37 +95,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;
  }
@@ -135,19 +120,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);

--
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/57ac1410-2277-e242-499c-163f2cbb93c1%40siemens.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to