]> git.ozlabs.org Git - petitboot/blob - discover/grub2/script.c
discover/grub2: Add support for for-loops
[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_for(stmt) \
23         container_of(stmt, struct grub2_statement_for, st)
24 #define to_stmt_conditional(stmt) \
25         container_of(stmt, struct grub2_statement_conditional, st)
26
27 struct env_entry {
28         char                    *name;
29         char                    *value;
30         struct list_item        list;
31 };
32
33 struct grub2_symtab_entry {
34         const char              *name;
35         grub2_function          fn;
36         void                    *data;
37         struct list_item        list;
38 };
39
40 static const char *default_prefix = "/boot/grub";
41
42 static struct grub2_symtab_entry *script_lookup_function(
43                 struct grub2_script *script, const char *name)
44 {
45         struct grub2_symtab_entry *entry;
46
47         list_for_each_entry(&script->symtab, entry, list) {
48                 if (!strcmp(entry->name, name))
49                         return entry;
50         }
51
52         return NULL;
53 }
54
55 const char *script_env_get(struct grub2_script *script, const char *name)
56 {
57         struct env_entry *entry;
58
59         list_for_each_entry(&script->environment, entry, list)
60                 if (!strcmp(entry->name, name))
61                         return entry->value;
62
63         return NULL;
64 }
65
66 void script_env_set(struct grub2_script *script,
67                 const char *name, const char *value)
68 {
69         struct env_entry *e, *entry = NULL;
70
71         list_for_each_entry(&script->environment, e, list) {
72                 if (!strcmp(e->name, name)) {
73                         entry = e;
74                         break;
75                 }
76         }
77
78         if (!entry) {
79                 entry = talloc(script, struct env_entry);
80                 entry->name = talloc_strdup(entry, name);
81                 list_add(&script->environment, &entry->list);
82         } else {
83                 talloc_free(entry->value);
84         }
85
86         entry->value = talloc_strdup(entry, value);
87 }
88
89 static char *expand_var(struct grub2_script *script, struct grub2_word *word)
90 {
91         const char *val;
92
93         val = script_env_get(script, word->name);
94         if (!val)
95                 val = "";
96
97         return talloc_strdup(script, val);
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, const char *id)
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         /* if we don't have an explicit id for this option, fall back to
121          * the name */
122         if (!id)
123                 id = opt->option->name;
124
125         return !strcmp(id, var);
126 }
127
128 static void append_text_to_current_arg(struct grub2_argv *argv,
129                 const char *text, int len)
130 {
131         char *cur = argv->argv[argv->argc - 1];
132
133         if (cur) {
134                 int curlen = strlen(cur);
135                 cur = talloc_realloc(argv->argv, cur, char, len + curlen + 1);
136                 memcpy(cur + curlen, text, len);
137                 cur[len + curlen] = '\0';
138
139         } else {
140                 cur = talloc_strndup(argv->argv, text, len);
141         }
142
143         argv->argv[argv->argc-1] = cur;
144 }
145
146 /* Add a word to the argv array. Depending on the word type, and presence of
147  * delimiter characters, we may add multiple items to the array.
148  */
149 static void append_word_to_argv(struct grub2_script *script,
150                 struct grub2_argv *argv, struct grub2_word *word)
151 {
152         const char *text, *pos;
153         int i, len;
154
155         /* If it's a variable, perform substitution */
156         if (word->type == GRUB2_WORD_VAR)
157                 text = expand_var(script, word);
158         else
159                 text = word->text;
160
161         len = strlen(text);
162
163         /* If we have no text, we leave the current word as-is. The caller
164          * has allocated an empty string for the case where this is the
165          * first text token */
166         if (!len)
167                 return;
168
169         /* If we're not splitting, we just add the entire block to the
170          * current argv item */
171         if (!word->split) {
172                 append_text_to_current_arg(argv, text, len);
173                 return;
174         }
175
176         /* Scan for delimiters. If we find a word-end boundary, add the word
177          * to the argv array, and start a new argv item */
178         pos = !is_delim(text[0]) ? text : NULL;
179         for (i = 0; i < len; i++) {
180
181                 /* first delimiter after a word: add the accumulated word to
182                  * argv */
183                 if (pos && is_delim(text[i])) {
184                         append_text_to_current_arg(argv, pos,
185                                         text + i - pos);
186                         pos = NULL;
187
188                 /* first non-delimeter after a delimiter: record the starting
189                  * position, and create another argv item */
190                 } else if (!pos && !is_delim(text[i])) {
191                         pos = text + i;
192                         argv->argc++;
193                         argv->argv = talloc_realloc(argv, argv->argv, char *,
194                                         argv->argc);
195                         argv->argv[argv->argc - 1] = NULL;
196                 }
197         }
198
199         /* add remaining word characters */
200         if (pos)
201                 append_text_to_current_arg(argv, pos, text + len - pos);
202 }
203
204 /* Transform an argv word-token list (returned from the parser) into an
205  * expanded argv array (as used by the script execution code). We do this by
206  * iterating through the words in an argv_list, looking for GRUB2_WORD_VAR
207  * expansions.
208  */
209 static void process_expansions(struct grub2_script *script,
210                 struct grub2_argv *argv)
211 {
212         struct grub2_word *top_word, *word;
213
214         argv->argc = 0;
215         argv->argv = NULL;
216
217         list_for_each_entry(&argv->words, top_word, argv_list) {
218                 argv->argc++;
219                 argv->argv = talloc_realloc(argv, argv->argv, char *,
220                                 argv->argc);
221                 /* because we've parsed a separate word here, we know that
222                  * we need at least an empty string */
223                 argv->argv[argv->argc - 1] = talloc_strdup(argv->argv, "");
224
225                 for (word = top_word; word; word = word->next)
226                         append_word_to_argv(script, argv, word);
227         }
228
229         /* we may have allocated an extra argv element but not populated it */
230         if (!argv->argv[argv->argc - 1])
231                 argv->argc--;
232 }
233
234 static int statements_execute(struct grub2_script *script,
235                 struct grub2_statements *stmts)
236 {
237         struct grub2_statement *stmt;
238         int rc = 0;
239
240         list_for_each_entry(&stmts->list, stmt, list) {
241                 if (stmt->exec)
242                         rc = stmt->exec(script, stmt);
243         }
244         return rc;
245 }
246
247 int statement_simple_execute(struct grub2_script *script,
248                 struct grub2_statement *statement)
249 {
250         struct grub2_statement_simple *st = to_stmt_simple(statement);
251         struct grub2_symtab_entry *entry;
252         char *pos;
253         int rc;
254
255         if (!st->argv)
256                 return 0;
257
258         process_expansions(script, st->argv);
259
260         if (!st->argv->argc)
261                 return 0;
262
263         /* is this a var=value assignment? */
264         pos = strchr(st->argv->argv[0], '=');
265         if (pos) {
266                 char *name, *value;
267                 name = st->argv->argv[0];
268                 name = talloc_strndup(st, name, pos - name);
269                 value = pos + 1;
270                 script_env_set(script, name, value);
271                 return 0;
272         }
273
274         entry = script_lookup_function(script, st->argv->argv[0]);
275         if (!entry) {
276                 pb_log("grub2: undefined function '%s'\n", st->argv->argv[0]);
277                 return 1;
278         }
279
280         rc = entry->fn(script, entry->data, st->argv->argc, st->argv->argv);
281
282         return rc;
283 }
284
285 int statement_block_execute(struct grub2_script *script,
286                 struct grub2_statement *statement)
287 {
288         struct grub2_statement_block *st = to_stmt_block(statement);
289         return statements_execute(script, st->statements);
290 }
291
292 /* returns 0 if the statement was executed, 1 otherwise */
293 static int statement_conditional_execute(struct grub2_script *script,
294                 struct grub2_statement *statement, bool *executed)
295 {
296         struct grub2_statement_conditional *st = to_stmt_conditional(statement);
297         int rc;
298
299         rc = st->condition->exec(script, st->condition);
300         *executed = (!rc);
301         if (*executed)
302                 rc = statements_execute(script, st->statements);
303
304         return rc;
305 }
306
307 int statement_if_execute(struct grub2_script *script,
308                 struct grub2_statement *statement)
309 {
310         struct grub2_statement_if *st = to_stmt_if(statement);
311         struct grub2_statement *conditional;
312         bool executed;
313         int rc = 0;
314
315         list_for_each_entry(&st->conditionals->list, conditional, list) {
316                 rc = statement_conditional_execute(script,
317                                 conditional, &executed);
318                 if (executed)
319                         break;
320         }
321
322         if (!executed && st->else_case)
323                 rc = statements_execute(script, st->else_case);
324
325         return rc;
326 }
327
328 int statement_menuentry_execute(struct grub2_script *script,
329                 struct grub2_statement *statement)
330 {
331         struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
332         struct discover_boot_option *opt;
333         const char *id = NULL;
334         int i;
335
336         process_expansions(script, st->argv);
337
338         opt = discover_boot_option_create(script->ctx, script->ctx->device);
339
340         /* XXX: --options=values need to be parsed properly; this is a simple
341          * implementation to get --id= working.
342          */
343         for (i = 1; i < st->argv->argc; ++i) {
344                 if (strncmp("--id=", st->argv->argv[i], 5) == 0) {
345                         id = st->argv->argv[i] + 5;
346                         break;
347                 }
348         }
349         if (st->argv->argc > 0)
350                 opt->option->name = talloc_strdup(opt, st->argv->argv[0]);
351         else
352                 opt->option->name = talloc_strdup(opt, "(unknown)");
353
354         opt->option->id = talloc_asprintf(opt->option, "%s#%s",
355                         script->ctx->device->device->id,
356                         id ? id : opt->option->name);
357
358         script->opt = opt;
359
360         statements_execute(script, st->statements);
361
362         if (!opt->boot_image)
363                 return -1;
364
365         opt->option->is_default = option_is_default(script, opt, id);
366
367         discover_context_add_boot_option(script->ctx, opt);
368         script->n_options++;
369         script->opt = NULL;
370
371         return 0;
372 }
373
374 static int function_invoke(struct grub2_script *script,
375                 void *data, int argc, char **argv)
376 {
377         struct grub2_statement_function *fn = data;
378         char *name;
379         int i;
380
381         /* set positional parameters */
382         for (i = 0; i < argc; i++) {
383                 name = talloc_asprintf(script, "$%d", i);
384                 script_env_set(script, name, argv[i]);
385         }
386
387         return statements_execute(script, fn->body);
388 }
389
390 int statement_function_execute(struct grub2_script *script,
391                 struct grub2_statement *statement)
392 {
393         struct grub2_statement_function *st = to_stmt_function(statement);
394         const char *name;
395
396         if (st->name->type == GRUB2_WORD_VAR)
397                 name = expand_var(script, st->name);
398         else
399                 name = st->name->text;
400
401         script_register_function(script, name, function_invoke, st);
402
403         return 0;
404 }
405
406 int statement_for_execute(struct grub2_script *script,
407                 struct grub2_statement *statement)
408 {
409         struct grub2_statement_for *st = to_stmt_for(statement);
410         const char *varname;
411         int i, rc = 0;
412
413         if (st->var->type == GRUB2_WORD_VAR)
414                 expand_var(script, st->var);
415         varname = st->var->text;
416
417         process_expansions(script, st->list);
418
419         for (i = 0; i < st->list->argc; ++i) {
420                 script_env_set(script, varname, st->list->argv[i]);
421                 rc = statements_execute(script, st->body);
422         }
423
424         return rc;
425 }
426
427 static void init_env(struct grub2_script *script)
428 {
429         struct env_entry *env;
430         char *prefix, *sep;
431
432         list_init(&script->environment);
433
434         /* use location of the parsed config file to determine the prefix */
435         env = talloc(script, struct env_entry);
436
437         prefix = NULL;
438         if (script->filename) {
439                 sep = strrchr(script->filename, '/');
440                 if (sep)
441                         prefix = talloc_strndup(env, script->filename,
442                                         sep - script->filename);
443         }
444
445         script_env_set(script, "prefix", prefix ? : default_prefix);
446         if (prefix)
447                 talloc_free(prefix);
448
449         /* establish feature settings */
450         script_env_set(script, "feature_menuentry_id", "y");
451 }
452
453 void script_register_function(struct grub2_script *script,
454                 const char *name, grub2_function fn,
455                 void *data)
456 {
457         struct grub2_symtab_entry *entry;
458
459         entry = talloc(script, struct grub2_symtab_entry);
460         entry->fn = fn;
461         entry->name = name;
462         entry->data = data;
463         list_add(&script->symtab, &entry->list);
464 }
465
466
467 void script_execute(struct grub2_script *script)
468 {
469         init_env(script);
470         statements_execute(script, script->statements);
471 }
472
473 struct grub2_script *create_script(struct grub2_parser *parser,
474                 struct discover_context *ctx)
475 {
476         struct grub2_script *script;
477
478         script = talloc_zero(parser, struct grub2_script);
479
480         script->ctx = ctx;
481
482         list_init(&script->symtab);
483         register_builtins(script);
484
485         return script;
486 }
487