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