From: Andreas Reichel <[email protected]>

If the crc was found to be wrong, the environment was used
normally despite, which caused problems in user tools.
Now, if an environment with an invalid crc is found, its working
copy is cleared. This way it can be automatically overwritten
by updates.

Also, the variable name 'oldenvs' is misleading, as it is an
array containing all current environments.

Signed-off-by: Andreas Reichel <[email protected]>
---
 env/env_api_fat.c | 25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/env/env_api_fat.c b/env/env_api_fat.c
index 4376b8a..10fe37b 100644
--- a/env/env_api_fat.c
+++ b/env/env_api_fat.c
@@ -330,7 +330,7 @@ bool write_env(CONFIG_PART *part, BG_ENVDATA *env)
 }
 
 static CONFIG_PART config_parts[ENV_NUM_CONFIG_PARTS];
-static BG_ENVDATA oldenvs[ENV_NUM_CONFIG_PARTS];
+static BG_ENVDATA envdata[ENV_NUM_CONFIG_PARTS];
 
 bool bgenv_init()
 {
@@ -342,12 +342,15 @@ bool bgenv_init()
                return false;
        }
        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));
-               if (oldenvs[i].crc32 != sum) {
+               read_env(&config_parts[i], &envdata[i]);
+               uint32_t sum = crc32(0, (Bytef *)&envdata[i],
+                   sizeof(BG_ENVDATA) - sizeof(envdata[i].crc32));
+               if (envdata[i].crc32 != sum) {
                        VERBOSE(stderr, "Invalid CRC32!\n");
-                       continue;
+                       /* clear invalid environment */
+                       memset(&envdata[i], 0, sizeof(BG_ENVDATA));
+                       envdata[i].crc32 = crc32(0, (Bytef *)&envdata[i],
+                           sizeof(BG_ENVDATA) - sizeof(envdata[i].crc32));
                }
        }
        return true;
@@ -365,7 +368,7 @@ BGENV *bgenv_open_by_index(uint32_t index)
                return NULL;
        }
        handle->desc = (void *)&config_parts[index];
-       handle->data = &oldenvs[index];
+       handle->data = &envdata[index];
        return handle;
 }
 
@@ -375,8 +378,8 @@ BGENV *bgenv_open_oldest()
        uint32_t min_idx = 0;
 
        for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) {
-               if (oldenvs[i].revision < minrev) {
-                       minrev = oldenvs[i].revision;
+               if (envdata[i].revision < minrev) {
+                       minrev = envdata[i].revision;
                        min_idx = i;
                }
        }
@@ -389,8 +392,8 @@ BGENV *bgenv_open_latest()
        uint32_t max_idx = 0;
 
        for (int i = 0; i < ENV_NUM_CONFIG_PARTS; i++) {
-               if (oldenvs[i].revision > maxrev) {
-                       maxrev = oldenvs[i].revision;
+               if (envdata[i].revision > maxrev) {
+                       maxrev = envdata[i].revision;
                        max_idx = i;
                }
        }
-- 
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/20170921132111.29035-11-andreas.reichel.ext%40siemens.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to