]> git.ozlabs.org Git - petitboot/blobdiff - discover/grub2/script.c
discover/grub2: Populate $prefix from config file location
[petitboot] / discover / grub2 / script.c
index d0b824aebcb9965e63d2ec0d0cb631da0a2dfc5d..a58f1a0f58eccb51abee06f9127ca0ac30450091 100644 (file)
@@ -23,8 +23,8 @@
        container_of(stmt, struct grub2_statement_conditional, st)
 
 struct env_entry {
-       const char              *name;
-       const char              *value;
+       char                    *name;
+       char                    *value;
        struct list_item        list;
 };
 
@@ -35,6 +35,8 @@ struct grub2_symtab_entry {
        struct list_item        list;
 };
 
+static const char *default_prefix = "/boot/grub";
+
 static struct grub2_symtab_entry *script_lookup_function(
                struct grub2_script *script, const char *name)
 {
@@ -73,11 +75,13 @@ void script_env_set(struct grub2_script *script,
 
        if (!entry) {
                entry = talloc(script, struct env_entry);
-               entry->name = name;
+               entry->name = talloc_strdup(entry, name);
                list_add(&script->environment, &entry->list);
+       } else {
+               talloc_free(entry->value);
        }
 
-       entry->value = value;
+       entry->value = talloc_strdup(entry, value);
 }
 
 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
@@ -316,7 +320,7 @@ int statement_if_execute(struct grub2_script *script,
        struct grub2_statement_if *st = to_stmt_if(statement);
        struct grub2_statement *conditional;
        bool executed;
-       int rc;
+       int rc = 0;
 
        list_for_each_entry(&st->conditionals->list, conditional, list) {
                rc = statement_conditional_execute(script,
@@ -396,12 +400,26 @@ int statement_function_execute(struct grub2_script *script,
 static void init_env(struct grub2_script *script)
 {
        struct env_entry *env;
+       char *prefix, *sep;
 
        list_init(&script->environment);
 
+       /* use location of the parsed config file to determine the prefix */
        env = talloc(script, struct env_entry);
+
+       prefix = NULL;
+       if (script->filename) {
+               sep = strrchr(script->filename, '/');
+               if (sep)
+                       prefix = talloc_strndup(env, script->filename,
+                                       sep - script->filename);
+       }
+
        env->name = talloc_strdup(env, "prefix");
-       env->value = talloc_strdup(env, "/");
+       if (prefix)
+               env->value = prefix;
+       else
+               env->value = talloc_strdup(env, default_prefix);
 
        list_add(&script->environment, &env->list);
 }
@@ -422,6 +440,7 @@ void script_register_function(struct grub2_script *script,
 
 void script_execute(struct grub2_script *script)
 {
+       init_env(script);
        statements_execute(script, script->statements);
 }
 
@@ -432,7 +451,6 @@ struct grub2_script *create_script(struct grub2_parser *parser,
 
        script = talloc_zero(parser, struct grub2_script);
 
-       init_env(script);
        script->ctx = ctx;
 
        list_init(&script->symtab);