7 #include <types/types.h>
8 #include <talloc/talloc.h>
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)
28 struct list_item list;
31 struct grub2_symtab_entry {
35 struct list_item list;
38 static const char *default_prefix = "/boot/grub";
40 static struct grub2_symtab_entry *script_lookup_function(
41 struct grub2_script *script, const char *name)
43 struct grub2_symtab_entry *entry;
45 list_for_each_entry(&script->symtab, entry, list) {
46 if (!strcmp(entry->name, name))
53 const char *script_env_get(struct grub2_script *script, const char *name)
55 struct env_entry *entry;
57 list_for_each_entry(&script->environment, entry, list)
58 if (!strcmp(entry->name, name))
64 void script_env_set(struct grub2_script *script,
65 const char *name, const char *value)
67 struct env_entry *e, *entry = NULL;
69 list_for_each_entry(&script->environment, e, list) {
70 if (!strcmp(e->name, name)) {
77 entry = talloc(script, struct env_entry);
79 list_add(&script->environment, &entry->list);
85 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
89 val = script_env_get(script, word->name);
93 word->text = talloc_strdup(script, val);
98 static bool is_delim(char c)
100 return c == ' ' || c == '\t';
103 static bool option_is_default(struct grub2_script *script,
104 struct discover_boot_option *opt)
106 unsigned int default_idx;
110 var = script_env_get(script, "default");
114 default_idx = strtoul(var, &end, 10);
115 if (end != var && *end == '\0')
116 return default_idx == script->n_options;
118 return !strcmp(opt->option->name, var);
121 /* For non-double-quoted variable expansions, we may need to split the
122 * variable's value into multiple argv items.
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.
129 static void process_split(struct grub2_script *script,
130 struct grub2_word *top_word,
131 struct grub2_word *word)
133 int i, len, delim_start = -1, delim_end = -1;
134 struct grub2_word *new_word;
137 len = strlen(word->text);
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)
145 } else if (delim_start != -1) {
151 /* No delim? nothing to do. The process_expansions loop will
152 * append this word's text to the top word, if necessary
154 if (delim_start == -1)
157 /* Set this word's text value to the text before the delimiter.
158 * this will get appended to the top word
160 word->text[delim_start] = '\0';
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) {
169 remainder = word->text + delim_end;
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);
180 /* stitch it into the argv list before this word */
181 list_insert_after(&top_word->argv_list,
182 &new_word->argv_list);
184 /* terminate this word */
188 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
191 * Once that's done, we may (if split == true) have to split the word to create
194 static void process_expansions(struct grub2_script *script,
195 struct grub2_argv *argv)
197 struct grub2_word *top_word, *word;
202 list_for_each_entry(&argv->words, top_word, argv_list) {
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) {
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;
215 /* split; this will potentially insert argv
216 * entries after top_word. */
218 process_split(script, top_word, word);
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(
231 top_word->next = NULL;
234 /* convert the list to an argv array, to pass to the function */
235 argv->argv = talloc_array(script, char *, argv->argc);
238 list_for_each_entry(&argv->words, word, argv_list)
239 argv->argv[i++] = word->text;
242 static int statements_execute(struct grub2_script *script,
243 struct grub2_statements *stmts)
245 struct grub2_statement *stmt;
248 list_for_each_entry(&stmts->list, stmt, list) {
250 rc = stmt->exec(script, stmt);
255 int statement_simple_execute(struct grub2_script *script,
256 struct grub2_statement *statement)
258 struct grub2_statement_simple *st = to_stmt_simple(statement);
259 struct grub2_symtab_entry *entry;
266 process_expansions(script, st->argv);
271 /* is this a var=value assignment? */
272 pos = strchr(st->argv->argv[0], '=');
275 name = st->argv->argv[0];
276 name = talloc_strndup(st, name, pos - name);
278 script_env_set(script, name, value);
282 entry = script_lookup_function(script, st->argv->argv[0]);
284 pb_log("grub2: undefined function '%s'\n", st->argv->argv[0]);
288 rc = entry->fn(script, entry->data, st->argv->argc, st->argv->argv);
293 int statement_block_execute(struct grub2_script *script,
294 struct grub2_statement *statement)
296 struct grub2_statement_block *st = to_stmt_block(statement);
297 return statements_execute(script, st->statements);
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)
304 struct grub2_statement_conditional *st = to_stmt_conditional(statement);
307 rc = st->condition->exec(script, st->condition);
310 rc = statements_execute(script, st->statements);
315 int statement_if_execute(struct grub2_script *script,
316 struct grub2_statement *statement)
318 struct grub2_statement_if *st = to_stmt_if(statement);
319 struct grub2_statement *conditional;
323 list_for_each_entry(&st->conditionals->list, conditional, list) {
324 rc = statement_conditional_execute(script,
325 conditional, &executed);
330 if (!executed && st->else_case)
331 rc = statements_execute(script, st->else_case);
336 int statement_menuentry_execute(struct grub2_script *script,
337 struct grub2_statement *statement)
339 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
340 struct discover_boot_option *opt;
342 process_expansions(script, st->argv);
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]);
348 opt->option->name = talloc_strdup(opt, "(unknown)");
350 opt->option->id = talloc_asprintf(opt->option, "%s#%s",
351 script->ctx->device->device->id,
356 statements_execute(script, st->statements);
358 opt->option->is_default = option_is_default(script, opt);
360 discover_context_add_boot_option(script->ctx, opt);
367 static int function_invoke(struct grub2_script *script,
368 void *data, int argc, char **argv)
370 struct grub2_statement_function *fn = data;
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]);
380 return statements_execute(script, fn->body);
383 int statement_function_execute(struct grub2_script *script,
384 struct grub2_statement *statement)
386 struct grub2_statement_function *st = to_stmt_function(statement);
389 if (st->name->type == GRUB2_WORD_VAR)
390 expand_var(script, st->name);
392 name = st->name->text;
393 script_register_function(script, name, function_invoke, st);
398 static void init_env(struct grub2_script *script)
400 struct env_entry *env;
402 list_init(&script->environment);
404 env = talloc(script, struct env_entry);
405 env->name = talloc_strdup(env, "prefix");
406 env->value = talloc_strdup(env, default_prefix);
408 list_add(&script->environment, &env->list);
411 void script_register_function(struct grub2_script *script,
412 const char *name, grub2_function fn,
415 struct grub2_symtab_entry *entry;
417 entry = talloc(script, struct grub2_symtab_entry);
421 list_add(&script->symtab, &entry->list);
425 void script_execute(struct grub2_script *script)
427 statements_execute(script, script->statements);
430 struct grub2_script *create_script(struct grub2_parser *parser,
431 struct discover_context *ctx)
433 struct grub2_script *script;
435 script = talloc_zero(parser, struct grub2_script);
440 list_init(&script->symtab);
441 register_builtins(script);