]> git.ozlabs.org Git - petitboot/blob - discover/grub2/script.c
discover/grub2: Add menuentry execution
[petitboot] / discover / grub2 / script.c
1
2 #include <sys/types.h>
3 #include <string.h>
4
5 #include <talloc/talloc.h>
6
7 #include "grub2.h"
8
9 #define to_stmt_simple(stmt) \
10         container_of(stmt, struct grub2_statement_simple, st)
11 #define to_stmt_if(stmt) \
12         container_of(stmt, struct grub2_statement_if, st)
13 #define to_stmt_menuentry(stmt) \
14         container_of(stmt, struct grub2_statement_menuentry, st)
15
16 struct env_entry {
17         const char              *name;
18         const char              *value;
19         struct list_item        list;
20 };
21
22 static const char *env_lookup(struct grub2_script *script,
23                 const char *name)
24 {
25         struct env_entry *entry;
26
27         list_for_each_entry(&script->environment, entry, list)
28                 if (!strcmp(entry->name, name))
29                         return entry->value;
30
31         return NULL;
32 }
33
34 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
35 {
36         const char *val;
37
38         val = env_lookup(script, word->var.name);
39         if (!val)
40                 val = "";
41
42         word->type = GRUB2_WORD_TEXT;
43         word->text = talloc_strdup(script, val);
44
45         return true;
46 }
47
48 /* iterate through the words in an argv, looking for GRUB2_WORD_VAR expansions.
49  *
50  * Once that's done, we may (if split == true) have to split the word to create
51  * new argv items
52  */
53 static void process_expansions(struct grub2_script *script,
54                 struct grub2_argv *argv)
55 {
56         struct grub2_word *top_word, *word;
57
58         list_for_each_entry(&argv->words, top_word, argv_list) {
59                 /* expand vars and squash the list of words into the top struct.
60                  * todo: splitting
61                  */
62                 for (word = top_word; word; word = word->next) {
63                         if (word->type == GRUB2_WORD_VAR)
64                                 expand_var(script, word);
65
66                         if (word == top_word)
67                                 continue;
68
69                         top_word->text = talloc_asprintf_append(top_word->text,
70                                         "%s", word->text);
71                 }
72                 top_word->next = NULL;
73         }
74 }
75
76 int statements_execute(struct grub2_script *script,
77                 struct grub2_statements *stmts)
78 {
79         struct grub2_statement *stmt;
80         int rc = 0;
81
82         list_for_each_entry(&stmts->list, stmt, list) {
83                 if (stmt->exec)
84                         rc = stmt->exec(script, stmt);
85         }
86         return rc;
87 }
88
89 int statement_simple_execute(struct grub2_script *script,
90                 struct grub2_statement *statement)
91 {
92         struct grub2_statement_simple *st = to_stmt_simple(statement);
93
94         if (!st->argv)
95                 return 0;
96
97         process_expansions(script, st->argv);
98
99         return 0;
100 }
101
102 int statement_if_execute(struct grub2_script *script,
103                 struct grub2_statement *statement)
104 {
105         struct grub2_statement_if *st = to_stmt_if(statement);
106         struct grub2_statements *case_stmts;
107         int rc;
108
109         rc = st->condition->exec(script, st->condition);
110
111         if (rc == 0)
112                 case_stmts = st->true_case;
113         else
114                 case_stmts = st->false_case;
115
116         if (case_stmts)
117                 statements_execute(script, case_stmts);
118         else
119                 rc = 0;
120
121         return rc;
122 }
123
124 int statement_menuentry_execute(struct grub2_script *script,
125                 struct grub2_statement *statement)
126 {
127         struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
128
129         process_expansions(script, st->argv);
130         statements_execute(script, st->statements);
131
132         return 0;
133 }
134
135 static void init_env(struct grub2_script *script)
136 {
137         struct env_entry *env;
138
139         list_init(&script->environment);
140
141         env = talloc(script, struct env_entry);
142         env->name = talloc_strdup(env, "prefix");
143         env->value = talloc_strdup(env, "/");
144
145         list_add(&script->environment, &env->list);
146 }
147
148
149 void script_execute(struct grub2_script *script)
150 {
151         statements_execute(script, script->statements);
152 }
153
154 struct grub2_script *create_script(void *ctx)
155 {
156         struct grub2_script *script;
157
158         script = talloc(ctx, struct grub2_script);
159
160         init_env(script);
161
162         return script;
163 }
164