diff mbox series

[libubootenv] uboot_env: Add parsing of .flag variable also when env is read from a default env file

Message ID 2710c0a6-1ffb-4182-943e-33cb7fd41c50@googlegroups.com
State New
Headers show
Series [libubootenv] uboot_env: Add parsing of .flag variable also when env is read from a default env file | expand

Commit Message

Uwe Schuster Aug. 2, 2019, 11:36 a.m. UTC
This patch fixes the default env flag parsing issue reported in 
https://groups.google.com/d/msg/swupdate/ShdD10dl7Pc/ULUYSpFDCAAJ:

3. When reading the variables from the default env file (like after an 
> erased env flash area or with -f command line flag) and if the .flags 
> variable is part of the default env file, it is not parsed, as when read 
> from normal u-boot env 
>

---
 src/uboot_env.c | 150 +++++++++++++++++++++++++++---------------------
 1 file changed, 85 insertions(+), 65 deletions(-)

 
@@ -981,10 +989,22 @@ int libuboot_load_file(struct uboot_ctx *ctx, const 
char *filename)
 
         name = buf;
 
-        libuboot_set_env(ctx, name, value);
+            if (!strcmp(name, ".flags"))
+                flagsvar = strdup(value);
+        else
+                libuboot_set_env(ctx, name, value);
     }
-    fclose(fp);
+
+    /*
+     * Parse .flags and set the attributes for a variable
+     */
+    if (flagsvar) {
+        libuboot_parseflags(ctx, flagsvar);
+    }
+
+    free(flagsvar);
     free(buf);
+    fclose(fp);
 
     return 0;
 }

Comments

Uwe Schuster March 11, 2020, 2:35 p.m. UTC | #1
Hi Stefano,

can this patch be applied ? We are using it in our products since a while, 
but would like to have it part of the community version. Or is there 
something else you need to be prepared?

Uwe

Am Freitag, 2. August 2019 13:36:53 UTC+2 schrieb Uwe Schuster:
>
> This patch fixes the default env flag parsing issue reported in 
> https://groups.google.com/d/msg/swupdate/ShdD10dl7Pc/ULUYSpFDCAAJ:
>
> 3. When reading the variables from the default env file (like after an 
>> erased env flash area or with -f command line flag) and if the .flags 
>> variable is part of the default env file, it is not parsed, as when read 
>> from normal u-boot env 
>>
>
> ---
>  src/uboot_env.c | 150 +++++++++++++++++++++++++++---------------------
>  1 file changed, 85 insertions(+), 65 deletions(-)
>
> diff --git a/src/uboot_env.c b/src/uboot_env.c
> index 726c433..91f94e7 100644
> --- a/src/uboot_env.c
> +++ b/src/uboot_env.c
> @@ -745,6 +745,73 @@ int libuboot_env_store(struct uboot_ctx *ctx)
>      return ret;
>  }
>  
> +static void libuboot_parseflags(struct uboot_ctx *ctx, char* flagsvar)
> +{
> +    char *pvar;
> +    char *pval;
> +    struct var_entry *entry;
> +
> +#if !defined(NDEBUG)
> +    fprintf(stdout, "Environment FLAGS %s\n", flagsvar);
> +#endif
> +    pvar = flagsvar;
> +
> +    while (*pvar && (pvar - flagsvar) < strlen(flagsvar)) {
> +        char *pnext;
> +        pval = strchr(pvar, ':');
> +        if (!pval)
> +            break;
> +
> +        *pval++ = '\0';
> +        pnext = strchr(pval, ',');
> +        if (!pnext)
> +            pnext = flagsvar + strlen(flagsvar);
> +        else
> +            *pnext++ = '\0';
> +
> +        entry = __libuboot_get_env(&ctx->varlist, pvar);
> +        if (entry) {
> +            for (int i = 0; i < strlen(pval); i++) {
> +                switch (pval[i]) {
> +                case 's':
> +                    entry->type = TYPE_ATTR_STRING;
> +                    break;
> +                case 'd':
> +                    entry->type = TYPE_ATTR_DECIMAL;
> +                    break;
> +                case 'x':
> +                    entry->type = TYPE_ATTR_HEX;
> +                    break;
> +                case 'b':
> +                    entry->type = TYPE_ATTR_BOOL;
> +                    break;
> +                case 'i':
> +                    entry->type = TYPE_ATTR_IP;
> +                    break;
> +                case 'm':
> +                    entry->type = TYPE_ATTR_MAC;
> +                    break;
> +                case 'a':
> +                    entry->access = ACCESS_ATTR_ANY;
> +                    break;
> +                case 'r':
> +                    entry->access = ACCESS_ATTR_READ_ONLY;
> +                    break;
> +                case 'o':
> +                    entry->access = ACCESS_ATTR_WRITE_ONCE;
> +                    break;
> +                case 'c':
> +                    entry->access = ACCESS_ATTR_CHANGE_DEFAULT;
> +                    break;
> +                default: /* ignore it */
> +                    break;
> +                }
> +            }
> +        }
> +        pvar = pnext;
> +    }
> +}
> +
>  static int libuboot_load(struct uboot_ctx *ctx)
>  {
>      int ret, i;
> @@ -871,72 +938,11 @@ static int libuboot_load(struct uboot_ctx *ctx)
>      /*
>       * Parse .flags and set the attributes for a variable
>       */
> -    char *pvar;
> -    char *pval;
>      if (flagsvar) {
> -#if !defined(NDEBUG)
> -    fprintf(stdout, "Environment FLAGS %s\n", flagsvar);
> -#endif
> -        pvar = flagsvar;
> -
> -        while (*pvar && (pvar - flagsvar) < strlen(flagsvar)) {
> -            char *pnext;
> -            pval = strchr(pvar, ':');
> -            if (!pval)
> -                break;
> -
> -            *pval++ = '\0';
> -            pnext = strchr(pval, ',');
> -            if (!pnext)
> -                pnext = flagsvar + strlen(flagsvar);
> -            else
> -                *pnext++ = '\0';
> -
> -            entry = __libuboot_get_env(&ctx->varlist, pvar);
> -            if (entry) {
> -                for (int i = 0; i < strlen(pval); i++) {
> -                    switch (pval[i]) {
> -                    case 's':
> -                        entry->type = TYPE_ATTR_STRING;
> -                        break;
> -                    case 'd':
> -                        entry->type = TYPE_ATTR_DECIMAL;
> -                        break;
> -                    case 'x':
> -                        entry->type = TYPE_ATTR_HEX;
> -                        break;
> -                    case 'b':
> -                        entry->type = TYPE_ATTR_BOOL;
> -                        break;
> -                    case 'i':
> -                        entry->type = TYPE_ATTR_IP;
> -                        break;
> -                    case 'm':
> -                        entry->type = TYPE_ATTR_MAC;
> -                        break;
> -                    case 'a':
> -                        entry->access = ACCESS_ATTR_ANY;
> -                        break;
> -                    case 'r':
> -                        entry->access = ACCESS_ATTR_READ_ONLY;
> -                        break;
> -                    case 'o':
> -                        entry->access = ACCESS_ATTR_WRITE_ONCE;
> -                        break;
> -                    case 'c':
> -                        entry->access = ACCESS_ATTR_CHANGE_DEFAULT;
> -                        break;
> -                    default: /* ignore it */
> -                        break;
> -                    }
> -                }
> -            }
> -
> -            pvar = pnext;
> -        }
> +        libuboot_parseflags(ctx, flagsvar);
>      }
> -    free(flagsvar);
>  
> +    free(flagsvar);
>      free(buf[0]);
>  
>      return ctx->valid ? 0 : -ENODATA;
> @@ -963,6 +969,8 @@ int libuboot_load_file(struct uboot_ctx *ctx, const 
> char *filename)
>          return -ENOMEM;
>      }
>  
> +    char *flagsvar = NULL;
> +
>      while (fgets(buf, LINE_LENGTH, fp)) {
>          int len = strlen(buf);
>  
> @@ -981,10 +989,22 @@ int libuboot_load_file(struct uboot_ctx *ctx, const 
> char *filename)
>  
>          name = buf;
>  
> -        libuboot_set_env(ctx, name, value);
> +            if (!strcmp(name, ".flags"))
> +                flagsvar = strdup(value);
> +        else
> +                libuboot_set_env(ctx, name, value);
>      }
> -    fclose(fp);
> +
> +    /*
> +     * Parse .flags and set the attributes for a variable
> +     */
> +    if (flagsvar) {
> +        libuboot_parseflags(ctx, flagsvar);
> +    }
> +
> +    free(flagsvar);
>      free(buf);
> +    fclose(fp);
>  
>      return 0;
>  }
> -- 
> 2.17.1
>
>
>
diff mbox series

Patch

diff --git a/src/uboot_env.c b/src/uboot_env.c
index 726c433..91f94e7 100644
--- a/src/uboot_env.c
+++ b/src/uboot_env.c
@@ -745,6 +745,73 @@  int libuboot_env_store(struct uboot_ctx *ctx)
     return ret;
 }
 
+static void libuboot_parseflags(struct uboot_ctx *ctx, char* flagsvar)
+{
+    char *pvar;
+    char *pval;
+    struct var_entry *entry;
+
+#if !defined(NDEBUG)
+    fprintf(stdout, "Environment FLAGS %s\n", flagsvar);
+#endif
+    pvar = flagsvar;
+
+    while (*pvar && (pvar - flagsvar) < strlen(flagsvar)) {
+        char *pnext;
+        pval = strchr(pvar, ':');
+        if (!pval)
+            break;
+
+        *pval++ = '\0';
+        pnext = strchr(pval, ',');
+        if (!pnext)
+            pnext = flagsvar + strlen(flagsvar);
+        else
+            *pnext++ = '\0';
+
+        entry = __libuboot_get_env(&ctx->varlist, pvar);
+        if (entry) {
+            for (int i = 0; i < strlen(pval); i++) {
+                switch (pval[i]) {
+                case 's':
+                    entry->type = TYPE_ATTR_STRING;
+                    break;
+                case 'd':
+                    entry->type = TYPE_ATTR_DECIMAL;
+                    break;
+                case 'x':
+                    entry->type = TYPE_ATTR_HEX;
+                    break;
+                case 'b':
+                    entry->type = TYPE_ATTR_BOOL;
+                    break;
+                case 'i':
+                    entry->type = TYPE_ATTR_IP;
+                    break;
+                case 'm':
+                    entry->type = TYPE_ATTR_MAC;
+                    break;
+                case 'a':
+                    entry->access = ACCESS_ATTR_ANY;
+                    break;
+                case 'r':
+                    entry->access = ACCESS_ATTR_READ_ONLY;
+                    break;
+                case 'o':
+                    entry->access = ACCESS_ATTR_WRITE_ONCE;
+                    break;
+                case 'c':
+                    entry->access = ACCESS_ATTR_CHANGE_DEFAULT;
+                    break;
+                default: /* ignore it */
+                    break;
+                }
+            }
+        }
+        pvar = pnext;
+    }
+}
+
 static int libuboot_load(struct uboot_ctx *ctx)
 {
     int ret, i;
@@ -871,72 +938,11 @@  static int libuboot_load(struct uboot_ctx *ctx)
     /*
      * Parse .flags and set the attributes for a variable
      */
-    char *pvar;
-    char *pval;
     if (flagsvar) {
-#if !defined(NDEBUG)
-    fprintf(stdout, "Environment FLAGS %s\n", flagsvar);
-#endif
-        pvar = flagsvar;
-
-        while (*pvar && (pvar - flagsvar) < strlen(flagsvar)) {
-            char *pnext;
-            pval = strchr(pvar, ':');
-            if (!pval)
-                break;
-
-            *pval++ = '\0';
-            pnext = strchr(pval, ',');
-            if (!pnext)
-                pnext = flagsvar + strlen(flagsvar);
-            else
-                *pnext++ = '\0';
-
-            entry = __libuboot_get_env(&ctx->varlist, pvar);
-            if (entry) {
-                for (int i = 0; i < strlen(pval); i++) {
-                    switch (pval[i]) {
-                    case 's':
-                        entry->type = TYPE_ATTR_STRING;
-                        break;
-                    case 'd':
-                        entry->type = TYPE_ATTR_DECIMAL;
-                        break;
-                    case 'x':
-                        entry->type = TYPE_ATTR_HEX;
-                        break;
-                    case 'b':
-                        entry->type = TYPE_ATTR_BOOL;
-                        break;
-                    case 'i':
-                        entry->type = TYPE_ATTR_IP;
-                        break;
-                    case 'm':
-                        entry->type = TYPE_ATTR_MAC;
-                        break;
-                    case 'a':
-                        entry->access = ACCESS_ATTR_ANY;
-                        break;
-                    case 'r':
-                        entry->access = ACCESS_ATTR_READ_ONLY;
-                        break;
-                    case 'o':
-                        entry->access = ACCESS_ATTR_WRITE_ONCE;
-                        break;
-                    case 'c':
-                        entry->access = ACCESS_ATTR_CHANGE_DEFAULT;
-                        break;
-                    default: /* ignore it */
-                        break;
-                    }
-                }
-            }
-
-            pvar = pnext;
-        }
+        libuboot_parseflags(ctx, flagsvar);
     }
-    free(flagsvar);
 
+    free(flagsvar);
     free(buf[0]);
 
     return ctx->valid ? 0 : -ENODATA;
@@ -963,6 +969,8 @@  int libuboot_load_file(struct uboot_ctx *ctx, const 
char *filename)
         return -ENOMEM;
     }
 
+    char *flagsvar = NULL;
+
     while (fgets(buf, LINE_LENGTH, fp)) {
         int len = strlen(buf);