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