]> git.ozlabs.org Git - petitboot/blob - discover/grub2/script.c
e29d43702b1e0099435ab76b2f3966ad559cee90
[petitboot] / discover / grub2 / script.c
1
2 #include <sys/types.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6 #include <log/log.h>
7 #include <types/types.h>
8 #include <talloc/talloc.h>
9
10 #include "grub2.h"
11
12 #define to_stmt_simple(stmt) \
13         container_of(stmt, struct grub2_statement_simple, st)
14 #define to_stmt_block(stmt) \
15         container_of(stmt, struct grub2_statement_block, st)
16 #define to_stmt_if(stmt) \
17         container_of(stmt, struct grub2_statement_if, st)
18 #define to_stmt_menuentry(stmt) \
19         container_of(stmt, struct grub2_statement_menuentry, st)
20 #define to_stmt_function(stmt) \
21         container_of(stmt, struct grub2_statement_function, st)
22 #define to_stmt_conditional(stmt) \
23         container_of(stmt, struct grub2_statement_conditional, st)
24
25 struct env_entry {
26         char                    *name;
27         char                    *value;
28         struct list_item        list;
29 };
30
31 struct grub2_symtab_entry {
32         const char              *name;
33         grub2_function          fn;
34         void                    *data;
35         struct list_item        list;
36 };
37
38 static const char *default_prefix = "/boot/grub";
39
40 static struct grub2_symtab_entry *script_lookup_function(
41                 struct grub2_script *script, const char *name)
42 {
43         struct grub2_symtab_entry *entry;
44
45         list_for_each_entry(&script->symtab, entry, list) {
46                 if (!strcmp(entry->name, name))
47                         return entry;
48         }
49
50         return NULL;
51 }
52
53 const char *script_env_get(struct grub2_script *script, const char *name)
54 {
55         struct env_entry *entry;
56
57         list_for_each_entry(&script->environment, entry, list)
58                 if (!strcmp(entry->name, name))
59                         return entry->value;
60
61         return NULL;
62 }
63
64 void script_env_set(struct grub2_script *script,
65                 const char *name, const char *value)
66 {
67         struct env_entry *e, *entry = NULL;
68
69         list_for_each_entry(&script->environment, e, list) {
70                 if (!strcmp(e->name, name)) {
71                         entry = e;
72                         break;
73                 }
74         }
75
76         if (!entry) {
77                 entry = talloc(script, struct env_entry);
78                 entry->name = talloc_strdup(entry, name);
79                 list_add(&script->environment, &entry->list);
80         } else {
81                 talloc_free(entry->value);
82         }
83
84         entry->value = talloc_strdup(entry, value);
85 }
86
87 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
88 {
89         const char *val;
90
91         val = script_env_get(script, word->name);
92         if (!val)
93                 val = "";
94
95         word->text = talloc_strdup(script, val);
96
97         return true;
98 }
99
100 static bool is_delim(char c)
101 {
102         return c == ' ' || c == '\t';
103 }
104
105 static bool option_is_default(struct grub2_script *script,
106                 struct discover_boot_option *opt)
107 {
108         unsigned int default_idx;
109         const char *var;
110         char *end;
111
112         var = script_env_get(script, "default");
113         if (!var)
114                 return false;
115
116         default_idx = strtoul(var, &end, 10);
117         if (end != var && *end == '\0')
118                 return default_idx == script->n_options;
119
120         return !strcmp(opt->option->name, var);
121 }
122
123 /* For non-double-quoted variable expansions, we may need to split the
124  * variable's value into multiple argv items.
125  *
126  * This function sets the word->text to the initial set of non-delimiter chars
127  * in the expanded var value. We then skip any delimiter chars, and (if
128  * required), create the new argv item with the remaining text, and
129  * add it to the argv list, after top_word.
130  */
131 static void process_split(struct grub2_script *script,
132                 struct grub2_word *top_word,
133                 struct grub2_word *word)
134 {
135         int i, len, delim_start = -1, delim_end = -1;
136         struct grub2_word *new_word;
137         char *remainder;
138
139         len = strlen(word->text);
140
141         /* Scan our string for the start of a delim (delim_start), and the
142          * start of any new text (delim_end). */
143         for (i = 0; i < len; i++) {
144                 if (is_delim(word->text[i])) {
145                         if (delim_start == -1)
146                                 delim_start = i;
147                 } else if (delim_start != -1) {
148                         delim_end = i;
149                         break;
150                 }
151         }
152
153         /* No delim? nothing to do. The process_expansions loop will
154          * append this word's text to the top word, if necessary
155          */
156         if (delim_start == -1)
157                 return;
158
159         /* Set this word's text value to the text before the delimiter.
160          * this will get appended to the top word
161          */
162         word->text[delim_start] = '\0';
163
164         /* No trailing text? If there are no following word tokens, we're done.
165          * Otherwise, we need to start a new argv item with those tokens */
166         if (delim_end == -1) {
167                 if (!word->next)
168                         return;
169                 remainder = "";
170         } else {
171                 remainder = word->text + delim_end;
172         }
173
174         new_word = talloc(script, struct grub2_word);
175         new_word->type = GRUB2_WORD_TEXT;
176         /* if there's no trailing text, we know we don't need to re-split */
177         new_word->split = delim_end != -1;
178         new_word->next = word->next;
179         new_word->last = NULL;
180         new_word->text = talloc_strdup(new_word, remainder);
181
182         /* stitch it into the argv list before this word */
183         list_insert_after(&top_word->argv_list,
184                            &new_word->argv_list);
185
186         /* terminate this word */
187         word->next = NULL;
188 }
189
190 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
191  * expansions.
192  *
193  * Once that's done, we may (if split == true) have to split the word to create
194  * new argv items
195  */
196 static void process_expansions(struct grub2_script *script,
197                 struct grub2_argv *argv)
198 {
199         struct grub2_word *top_word, *word;
200         int i;
201
202         argv->argc = 0;
203
204         list_for_each_entry(&argv->words, top_word, argv_list) {
205                 argv->argc++;
206
207                 /* expand vars and squash the list of words into the head
208                  * of the argv word list */
209                 for (word = top_word; word; word = word->next) {
210
211                         /* if it's a variable, perform the substitution */
212                         if (word->type == GRUB2_WORD_VAR) {
213                                 expand_var(script, word);
214                                 word->type = GRUB2_WORD_TEXT;
215                         }
216
217                         /* split; this will potentially insert argv
218                          * entries after top_word. */
219                         if (word->split)
220                                 process_split(script, top_word, word);
221
222                         /* accumulate word text into the top word, so
223                          * we end up with a shallow tree of argv data */
224                         /* todo: don't do this in process_split */
225                         if (word != top_word) {
226                                 top_word->text = talloc_asprintf_append(
227                                                         top_word->text,
228                                                         "%s", word->text);
229                         }
230
231
232                 }
233                 top_word->next = NULL;
234         }
235
236         /* convert the list to an argv array, to pass to the function */
237         argv->argv = talloc_array(script, char *, argv->argc);
238         i = 0;
239
240         list_for_each_entry(&argv->words, word, argv_list)
241                 argv->argv[i++] = word->text;
242 }
243
244 static int statements_execute(struct grub2_script *script,
245                 struct grub2_statements *stmts)
246 {
247         struct grub2_statement *stmt;
248         int rc = 0;
249
250         list_for_each_entry(&stmts->list, stmt, list) {
251                 if (stmt->exec)
252                         rc = stmt->exec(script, stmt);
253         }
254         return rc;
255 }
256
257 int statement_simple_execute(struct grub2_script *script,
258                 struct grub2_statement *statement)
259 {
260         struct grub2_statement_simple *st = to_stmt_simple(statement);
261         struct grub2_symtab_entry *entry;
262         char *pos;
263         int rc;
264
265         if (!st->argv)
266                 return 0;
267
268         process_expansions(script, st->argv);
269
270         if (!st->argv->argc)
271                 return 0;
272
273         /* is this a var=value assignment? */
274         pos = strchr(st->argv->argv[0], '=');
275         if (pos) {
276                 char *name, *value;
277                 name = st->argv->argv[0];
278                 name = talloc_strndup(st, name, pos - name);
279                 value = pos + 1;
280                 script_env_set(script, name, value);
281                 return 0;
282         }
283
284         entry = script_lookup_function(script, st->argv->argv[0]);
285         if (!entry) {
286                 pb_log("grub2: undefined function '%s'\n", st->argv->argv[0]);
287                 return 1;
288         }
289
290         rc = entry->fn(script, entry->data, st->argv->argc, st->argv->argv);
291
292         return rc;
293 }
294
295 int statement_block_execute(struct grub2_script *script,
296                 struct grub2_statement *statement)
297 {
298         struct grub2_statement_block *st = to_stmt_block(statement);
299         return statements_execute(script, st->statements);
300 }
301
302 /* returns 0 if the statement was executed, 1 otherwise */
303 static int statement_conditional_execute(struct grub2_script *script,
304                 struct grub2_statement *statement, bool *executed)
305 {
306         struct grub2_statement_conditional *st = to_stmt_conditional(statement);
307         int rc;
308
309         rc = st->condition->exec(script, st->condition);
310         *executed = (!rc);
311         if (*executed)
312                 rc = statements_execute(script, st->statements);
313
314         return rc;
315 }
316
317 int statement_if_execute(struct grub2_script *script,
318                 struct grub2_statement *statement)
319 {
320         struct grub2_statement_if *st = to_stmt_if(statement);
321         struct grub2_statement *conditional;
322         bool executed;
323         int rc = 0;
324
325         list_for_each_entry(&st->conditionals->list, conditional, list) {
326                 rc = statement_conditional_execute(script,
327                                 conditional, &executed);
328                 if (executed)
329                         break;
330         }
331
332         if (!executed && st->else_case)
333                 rc = statements_execute(script, st->else_case);
334
335         return rc;
336 }
337
338 int statement_menuentry_execute(struct grub2_script *script,
339                 struct grub2_statement *statement)
340 {
341         struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
342         struct discover_boot_option *opt;
343
344         process_expansions(script, st->argv);
345
346         opt = discover_boot_option_create(script->ctx, script->ctx->device);
347         if (st->argv->argc > 0) {
348                 opt->option->name = talloc_strdup(opt, st->argv->argv[0]);
349         } else {
350                 opt->option->name = talloc_strdup(opt, "(unknown)");
351         }
352         opt->option->id = talloc_asprintf(opt->option, "%s#%s",
353                         script->ctx->device->device->id,
354                         opt->option->name);
355
356         script->opt = opt;
357
358         statements_execute(script, st->statements);
359
360         opt->option->is_default = option_is_default(script, opt);
361
362         discover_context_add_boot_option(script->ctx, opt);
363         script->n_options++;
364         script->opt = NULL;
365
366         return 0;
367 }
368
369 static int function_invoke(struct grub2_script *script,
370                 void *data, int argc, char **argv)
371 {
372         struct grub2_statement_function *fn = data;
373         char *name;
374         int i;
375
376         /* set positional parameters */
377         for (i = 0; i < argc; i++) {
378                 name = talloc_asprintf(script, "$%d", i);
379                 script_env_set(script, name, argv[i]);
380         }
381
382         return statements_execute(script, fn->body);
383 }
384
385 int statement_function_execute(struct grub2_script *script,
386                 struct grub2_statement *statement)
387 {
388         struct grub2_statement_function *st = to_stmt_function(statement);
389         const char *name;
390
391         if (st->name->type == GRUB2_WORD_VAR)
392                 expand_var(script, st->name);
393
394         name = st->name->text;
395         script_register_function(script, name, function_invoke, st);
396
397         return 0;
398 }
399
400 static void init_env(struct grub2_script *script)
401 {
402         struct env_entry *env;
403
404         list_init(&script->environment);
405
406         env = talloc(script, struct env_entry);
407         env->name = talloc_strdup(env, "prefix");
408         env->value = talloc_strdup(env, default_prefix);
409
410         list_add(&script->environment, &env->list);
411 }
412
413 void script_register_function(struct grub2_script *script,
414                 const char *name, grub2_function fn,
415                 void *data)
416 {
417         struct grub2_symtab_entry *entry;
418
419         entry = talloc(script, struct grub2_symtab_entry);
420         entry->fn = fn;
421         entry->name = name;
422         entry->data = data;
423         list_add(&script->symtab, &entry->list);
424 }
425
426
427 void script_execute(struct grub2_script *script)
428 {
429         statements_execute(script, script->statements);
430 }
431
432 struct grub2_script *create_script(struct grub2_parser *parser,
433                 struct discover_context *ctx)
434 {
435         struct grub2_script *script;
436
437         script = talloc_zero(parser, struct grub2_script);
438
439         init_env(script);
440         script->ctx = ctx;
441
442         list_init(&script->symtab);
443         register_builtins(script);
444
445         return script;
446 }
447