]> git.ozlabs.org Git - petitboot/blob - discover/grub2/script.c
discover/grub2: Perform word-expansion non-destructively
[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         char                    *name;
27         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 = talloc_strdup(entry, name);
79                 list_add(&script->environment, &entry->list);
80         } else {
81                 talloc_free(entry->value);
82         }
83
84         entry->value = talloc_strdup(entry, value);
85 }
86
87 static char *expand_var(struct grub2_script *script, struct grub2_word *word)
88 {
89         const char *val;
90
91         val = script_env_get(script, word->name);
92         if (!val)
93                 val = "";
94
95         return talloc_strdup(script, val);
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, const char *id)
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         /* if we don't have an explicit id for this option, fall back to
119          * the name */
120         if (!id)
121                 id = opt->option->name;
122
123         return !strcmp(id, 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->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;
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         discover_context_add_boot_option(script->ctx, opt);
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 = 0; 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 static void init_env(struct grub2_script *script)
405 {
406         struct env_entry *env;
407         char *prefix, *sep;
408
409         list_init(&script->environment);
410
411         /* use location of the parsed config file to determine the prefix */
412         env = talloc(script, struct env_entry);
413
414         prefix = NULL;
415         if (script->filename) {
416                 sep = strrchr(script->filename, '/');
417                 if (sep)
418                         prefix = talloc_strndup(env, script->filename,
419                                         sep - script->filename);
420         }
421
422         script_env_set(script, "prefix", prefix ? : default_prefix);
423         if (prefix)
424                 talloc_free(prefix);
425
426         /* establish feature settings */
427         script_env_set(script, "feature_menuentry_id", "y");
428 }
429
430 void script_register_function(struct grub2_script *script,
431                 const char *name, grub2_function fn,
432                 void *data)
433 {
434         struct grub2_symtab_entry *entry;
435
436         entry = talloc(script, struct grub2_symtab_entry);
437         entry->fn = fn;
438         entry->name = name;
439         entry->data = data;
440         list_add(&script->symtab, &entry->list);
441 }
442
443
444 void script_execute(struct grub2_script *script)
445 {
446         init_env(script);
447         statements_execute(script, script->statements);
448 }
449
450 struct grub2_script *create_script(struct grub2_parser *parser,
451                 struct discover_context *ctx)
452 {
453         struct grub2_script *script;
454
455         script = talloc_zero(parser, struct grub2_script);
456
457         script->ctx = ctx;
458
459         list_init(&script->symtab);
460         register_builtins(script);
461
462         return script;
463 }
464