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);
