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