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);
78 entry->name = talloc_strdup(entry, name);
79 list_add(&script->environment, &entry->list);
81 talloc_free(entry->value);
84 entry->value = talloc_strdup(entry, value);
87 static bool expand_var(struct grub2_script *script, struct grub2_word *word)
91 val = script_env_get(script, word->name);
95 word->text = talloc_strdup(script, val);
100 static bool is_delim(char c)
102 return c == ' ' || c == '\t';
105 static bool option_is_default(struct grub2_script *script,
106 struct discover_boot_option *opt)
108 unsigned int default_idx;
112 var = script_env_get(script, "default");
116 default_idx = strtoul(var, &end, 10);
117 if (end != var && *end == '\0')
118 return default_idx == script->n_options;
120 return !strcmp(opt->option->name, var);
123 /* For non-double-quoted variable expansions, we may need to split the
124 * variable's value into multiple argv items.
126 * This function sets the word->text to the initial set of non-delimiter chars
127 * in the expanded var value. We then skip any delimiter chars, and (if
128 * required), create the new argv item with the remaining text, and
129 * add it to the argv list, after top_word.
131 static void process_split(struct grub2_script *script,
132 struct grub2_word *top_word,
133 struct grub2_word *word)
135 int i, len, delim_start = -1, delim_end = -1;
136 struct grub2_word *new_word;
139 len = strlen(word->text);
141 /* Scan our string for the start of a delim (delim_start), and the
142 * start of any new text (delim_end). */
143 for (i = 0; i < len; i++) {
144 if (is_delim(word->text[i])) {
145 if (delim_start == -1)
147 } else if (delim_start != -1) {
153 /* No delim? nothing to do. The process_expansions loop will
154 * append this word's text to the top word, if necessary
156 if (delim_start == -1)
159 /* Set this word's text value to the text before the delimiter.
160 * this will get appended to the top word
162 word->text[delim_start] = '\0';
164 /* No trailing text? If there are no following word tokens, we're done.
165 * Otherwise, we need to start a new argv item with those tokens */
166 if (delim_end == -1) {
171 remainder = word->text + delim_end;
174 new_word = talloc(script, struct grub2_word);
175 new_word->type = GRUB2_WORD_TEXT;
176 /* if there's no trailing text, we know we don't need to re-split */
177 new_word->split = delim_end != -1;
178 new_word->next = word->next;
179 new_word->last = NULL;
180 new_word->text = talloc_strdup(new_word, remainder);
182 /* stitch it into the argv list before this word */
183 list_insert_after(&top_word->argv_list,
184 &new_word->argv_list);
186 /* terminate this word */
190 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
193 * Once that's done, we may (if split == true) have to split the word to create
196 static void process_expansions(struct grub2_script *script,
197 struct grub2_argv *argv)
199 struct grub2_word *top_word, *word;
204 list_for_each_entry(&argv->words, top_word, argv_list) {
207 /* expand vars and squash the list of words into the head
208 * of the argv word list */
209 for (word = top_word; word; word = word->next) {
211 /* if it's a variable, perform the substitution */
212 if (word->type == GRUB2_WORD_VAR) {
213 expand_var(script, word);
214 word->type = GRUB2_WORD_TEXT;
217 /* split; this will potentially insert argv
218 * entries after top_word. */
220 process_split(script, top_word, word);
222 /* accumulate word text into the top word, so
223 * we end up with a shallow tree of argv data */
224 /* todo: don't do this in process_split */
225 if (word != top_word) {
226 top_word->text = talloc_asprintf_append(
233 top_word->next = NULL;
236 /* convert the list to an argv array, to pass to the function */
237 argv->argv = talloc_array(script, char *, argv->argc);
240 list_for_each_entry(&argv->words, word, argv_list)
241 argv->argv[i++] = word->text;
244 static int statements_execute(struct grub2_script *script,
245 struct grub2_statements *stmts)
247 struct grub2_statement *stmt;
250 list_for_each_entry(&stmts->list, stmt, list) {
252 rc = stmt->exec(script, stmt);
257 int statement_simple_execute(struct grub2_script *script,
258 struct grub2_statement *statement)
260 struct grub2_statement_simple *st = to_stmt_simple(statement);
261 struct grub2_symtab_entry *entry;
268 process_expansions(script, st->argv);
273 /* is this a var=value assignment? */
274 pos = strchr(st->argv->argv[0], '=');
277 name = st->argv->argv[0];
278 name = talloc_strndup(st, name, pos - name);
280 script_env_set(script, name, value);
284 entry = script_lookup_function(script, st->argv->argv[0]);
286 pb_log("grub2: undefined function '%s'\n", st->argv->argv[0]);
290 rc = entry->fn(script, entry->data, st->argv->argc, st->argv->argv);
295 int statement_block_execute(struct grub2_script *script,
296 struct grub2_statement *statement)
298 struct grub2_statement_block *st = to_stmt_block(statement);
299 return statements_execute(script, st->statements);
302 /* returns 0 if the statement was executed, 1 otherwise */
303 static int statement_conditional_execute(struct grub2_script *script,
304 struct grub2_statement *statement, bool *executed)
306 struct grub2_statement_conditional *st = to_stmt_conditional(statement);
309 rc = st->condition->exec(script, st->condition);
312 rc = statements_execute(script, st->statements);
317 int statement_if_execute(struct grub2_script *script,
318 struct grub2_statement *statement)
320 struct grub2_statement_if *st = to_stmt_if(statement);
321 struct grub2_statement *conditional;
325 list_for_each_entry(&st->conditionals->list, conditional, list) {
326 rc = statement_conditional_execute(script,
327 conditional, &executed);
332 if (!executed && st->else_case)
333 rc = statements_execute(script, st->else_case);
338 int statement_menuentry_execute(struct grub2_script *script,
339 struct grub2_statement *statement)
341 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
342 struct discover_boot_option *opt;
344 process_expansions(script, st->argv);
346 opt = discover_boot_option_create(script->ctx, script->ctx->device);
347 if (st->argv->argc > 0) {
348 opt->option->name = talloc_strdup(opt, st->argv->argv[0]);
350 opt->option->name = talloc_strdup(opt, "(unknown)");
352 opt->option->id = talloc_asprintf(opt->option, "%s#%s",
353 script->ctx->device->device->id,
358 statements_execute(script, st->statements);
360 opt->option->is_default = option_is_default(script, opt);
362 discover_context_add_boot_option(script->ctx, opt);
369 static int function_invoke(struct grub2_script *script,
370 void *data, int argc, char **argv)
372 struct grub2_statement_function *fn = data;
376 /* set positional parameters */
377 for (i = 0; i < argc; i++) {
378 name = talloc_asprintf(script, "$%d", i);
379 script_env_set(script, name, argv[i]);
382 return statements_execute(script, fn->body);
385 int statement_function_execute(struct grub2_script *script,
386 struct grub2_statement *statement)
388 struct grub2_statement_function *st = to_stmt_function(statement);
391 if (st->name->type == GRUB2_WORD_VAR)
392 expand_var(script, st->name);
394 name = st->name->text;
395 script_register_function(script, name, function_invoke, st);
400 static void init_env(struct grub2_script *script)
402 struct env_entry *env;
404 list_init(&script->environment);
406 env = talloc(script, struct env_entry);
407 env->name = talloc_strdup(env, "prefix");
408 env->value = talloc_strdup(env, default_prefix);
410 list_add(&script->environment, &env->list);
413 void script_register_function(struct grub2_script *script,
414 const char *name, grub2_function fn,
417 struct grub2_symtab_entry *entry;
419 entry = talloc(script, struct grub2_symtab_entry);
423 list_add(&script->symtab, &entry->list);
427 void script_execute(struct grub2_script *script)
429 statements_execute(script, script->statements);
432 struct grub2_script *create_script(struct grub2_parser *parser,
433 struct discover_context *ctx)
435 struct grub2_script *script;
437 script = talloc_zero(parser, struct grub2_script);
442 list_init(&script->symtab);
443 register_builtins(script);