]> git.ozlabs.org Git - petitboot/blob - discover/grub2/script.c
discover/grub2: Allow using title for default even if id was defined
[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], 5) == 0) {
343                         id = st->argv->argv[i] + 5;
344                         break;
345                 }
346         }
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                         id ? id : opt->option->name);
355
356         script->opt = opt;
357
358         statements_execute(script, st->statements);
359
360         if (!opt->boot_image)
361                 return -1;
362
363         opt->option->is_default = option_is_default(script, opt, id);
364
365         list_add_tail(&script->options, &opt->list);
366         script->n_options++;
367         script->opt = NULL;
368
369         return 0;
370 }
371
372 static int function_invoke(struct grub2_script *script,
373                 void *data, int argc, char **argv)
374 {
375         struct grub2_statement_function *fn = data;
376         char *name;
377         int i;
378
379         /* set positional parameters */
380         for (i = 1; i < argc; i++) {
381                 name = talloc_asprintf(script, "%d", i);
382                 script_env_set(script, name, argv[i]);
383         }
384
385         return statements_execute(script, fn->body);
386 }
387
388 int statement_function_execute(struct grub2_script *script,
389                 struct grub2_statement *statement)
390 {
391         struct grub2_statement_function *st = to_stmt_function(statement);
392         const char *name;
393
394         if (st->name->type == GRUB2_WORD_VAR)
395                 name = expand_var(script, st->name);
396         else
397                 name = st->name->text;
398
399         script_register_function(script, name, function_invoke, st);
400
401         return 0;
402 }
403
404 int statement_for_execute(struct grub2_script *script,
405                 struct grub2_statement *statement)
406 {
407         struct grub2_statement_for *st = to_stmt_for(statement);
408         const char *varname;
409         int i, rc = 0;
410
411         if (st->var->type == GRUB2_WORD_VAR)
412                 expand_var(script, st->var);
413         varname = st->var->text;
414
415         process_expansions(script, st->list);
416
417         for (i = 0; i < st->list->argc; ++i) {
418                 script_env_set(script, varname, st->list->argv[i]);
419                 rc = statements_execute(script, st->body);
420         }
421
422         return rc;
423 }
424
425 static void init_env(struct grub2_script *script)
426 {
427         struct env_entry *env;
428         char *prefix, *sep;
429
430         list_init(&script->environment);
431
432         /* use location of the parsed config file to determine the prefix */
433         env = talloc(script, struct env_entry);
434
435         prefix = NULL;
436         if (script->filename) {
437                 sep = strrchr(script->filename, '/');
438                 if (sep)
439                         prefix = talloc_strndup(env, script->filename,
440                                         sep - script->filename);
441         }
442
443         script_env_set(script, "prefix", prefix ? : default_prefix);
444         if (prefix)
445                 talloc_free(prefix);
446
447         /* establish feature settings */
448         script_env_set(script, "feature_menuentry_id", "y");
449 }
450
451 void script_register_function(struct grub2_script *script,
452                 const char *name, grub2_function fn,
453                 void *data)
454 {
455         struct grub2_symtab_entry *entry;
456
457         entry = talloc(script, struct grub2_symtab_entry);
458         entry->fn = fn;
459         entry->name = name;
460         entry->data = data;
461         list_add(&script->symtab, &entry->list);
462 }
463
464 static void set_fallback_default(struct grub2_script *script)
465 {
466         struct discover_boot_option *opt, *first = NULL;
467         bool have_default = false;
468
469         list_for_each_entry(&script->options, opt, list) {
470                 if (!first)
471                         first = opt;
472                 have_default = have_default || opt->option->is_default;
473         }
474
475         if (!have_default && first) {
476                 const char *env = script_env_get(script, "default");
477
478                 pb_log("grub: no explicit default (env default=%s), "
479                                 "falling back to first option (%s)\n",
480                                 env ?: "unset", first->option->name);
481
482                 first->option->is_default = true;
483         }
484 }
485
486 void script_execute(struct grub2_script *script)
487 {
488         struct discover_boot_option *opt, *tmp;
489
490         if (!script)
491                 return;
492
493         init_env(script);
494         statements_execute(script, script->statements);
495
496         set_fallback_default(script);
497
498         list_for_each_entry_safe(&script->options, opt, tmp, list)
499                 discover_context_add_boot_option(script->ctx, opt);
500
501         /* Our option list will be invalid, as we've added all options to the
502          * discover context */
503         list_init(&script->options);
504 }
505
506 struct grub2_script *create_script(struct grub2_parser *parser,
507                 struct discover_context *ctx)
508 {
509         struct grub2_script *script;
510
511         script = talloc_zero(parser, struct grub2_script);
512
513         script->ctx = ctx;
514
515         list_init(&script->symtab);
516         list_init(&script->options);
517         register_builtins(script);
518
519         return script;
520 }
521