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, const char *id)
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 /* if we don't have an explicit id for this option, fall back to
123 id = opt->option->name;
125 return !strcmp(id, var);
128 /* For non-double-quoted variable expansions, we may need to split the
129 * variable's value into multiple argv items.
131 * This function sets the word->text to the initial set of non-delimiter chars
132 * in the expanded var value. We then skip any delimiter chars, and (if
133 * required), create the new argv item with the remaining text, and
134 * add it to the argv list, after top_word.
136 static void process_split(struct grub2_script *script,
137 struct grub2_word *top_word,
138 struct grub2_word *word)
140 int i, len, delim_start = -1, delim_end = -1;
141 struct grub2_word *new_word;
144 len = strlen(word->text);
146 /* Scan our string for the start of a delim (delim_start), and the
147 * start of any new text (delim_end). */
148 for (i = 0; i < len; i++) {
149 if (is_delim(word->text[i])) {
150 if (delim_start == -1)
152 } else if (delim_start != -1) {
158 /* No delim? nothing to do. The process_expansions loop will
159 * append this word's text to the top word, if necessary
161 if (delim_start == -1)
164 /* Set this word's text value to the text before the delimiter.
165 * this will get appended to the top word
167 word->text[delim_start] = '\0';
169 /* No trailing text? If there are no following word tokens, we're done.
170 * Otherwise, we need to start a new argv item with those tokens */
171 if (delim_end == -1) {
176 remainder = word->text + delim_end;
179 new_word = talloc(script, struct grub2_word);
180 new_word->type = GRUB2_WORD_TEXT;
181 /* if there's no trailing text, we know we don't need to re-split */
182 new_word->split = delim_end != -1;
183 new_word->next = word->next;
184 new_word->last = NULL;
185 new_word->text = talloc_strdup(new_word, remainder);
187 /* stitch it into the argv list before this word */
188 list_insert_after(&top_word->argv_list,
189 &new_word->argv_list);
191 /* terminate this word */
195 /* iterate through the words in an argv_list, looking for GRUB2_WORD_VAR
198 * Once that's done, we may (if split == true) have to split the word to create
201 static void process_expansions(struct grub2_script *script,
202 struct grub2_argv *argv)
204 struct grub2_word *top_word, *word;
209 list_for_each_entry(&argv->words, top_word, argv_list) {
212 /* expand vars and squash the list of words into the head
213 * of the argv word list */
214 for (word = top_word; word; word = word->next) {
216 /* if it's a variable, perform the substitution */
217 if (word->type == GRUB2_WORD_VAR) {
218 expand_var(script, word);
219 word->type = GRUB2_WORD_TEXT;
222 /* split; this will potentially insert argv
223 * entries after top_word. */
225 process_split(script, top_word, word);
227 /* accumulate word text into the top word, so
228 * we end up with a shallow tree of argv data */
229 /* todo: don't do this in process_split */
230 if (word != top_word) {
231 top_word->text = talloc_asprintf_append(
238 top_word->next = NULL;
241 /* convert the list to an argv array, to pass to the function */
242 argv->argv = talloc_array(script, char *, argv->argc);
245 list_for_each_entry(&argv->words, word, argv_list)
246 argv->argv[i++] = word->text;
249 static int statements_execute(struct grub2_script *script,
250 struct grub2_statements *stmts)
252 struct grub2_statement *stmt;
255 list_for_each_entry(&stmts->list, stmt, list) {
257 rc = stmt->exec(script, stmt);
262 int statement_simple_execute(struct grub2_script *script,
263 struct grub2_statement *statement)
265 struct grub2_statement_simple *st = to_stmt_simple(statement);
266 struct grub2_symtab_entry *entry;
273 process_expansions(script, st->argv);
278 /* is this a var=value assignment? */
279 pos = strchr(st->argv->argv[0], '=');
282 name = st->argv->argv[0];
283 name = talloc_strndup(st, name, pos - name);
285 script_env_set(script, name, value);
289 entry = script_lookup_function(script, st->argv->argv[0]);
291 pb_log("grub2: undefined function '%s'\n", st->argv->argv[0]);
295 rc = entry->fn(script, entry->data, st->argv->argc, st->argv->argv);
300 int statement_block_execute(struct grub2_script *script,
301 struct grub2_statement *statement)
303 struct grub2_statement_block *st = to_stmt_block(statement);
304 return statements_execute(script, st->statements);
307 /* returns 0 if the statement was executed, 1 otherwise */
308 static int statement_conditional_execute(struct grub2_script *script,
309 struct grub2_statement *statement, bool *executed)
311 struct grub2_statement_conditional *st = to_stmt_conditional(statement);
314 rc = st->condition->exec(script, st->condition);
317 rc = statements_execute(script, st->statements);
322 int statement_if_execute(struct grub2_script *script,
323 struct grub2_statement *statement)
325 struct grub2_statement_if *st = to_stmt_if(statement);
326 struct grub2_statement *conditional;
330 list_for_each_entry(&st->conditionals->list, conditional, list) {
331 rc = statement_conditional_execute(script,
332 conditional, &executed);
337 if (!executed && st->else_case)
338 rc = statements_execute(script, st->else_case);
343 int statement_menuentry_execute(struct grub2_script *script,
344 struct grub2_statement *statement)
346 struct grub2_statement_menuentry *st = to_stmt_menuentry(statement);
347 struct discover_boot_option *opt;
348 const char *id = NULL;
351 process_expansions(script, st->argv);
353 opt = discover_boot_option_create(script->ctx, script->ctx->device);
355 /* XXX: --options=values need to be parsed properly; this is a simple
356 * implementation to get --id= working.
358 for (i = 1; i < st->argv->argc; ++i) {
359 if (strncmp("--id=", st->argv->argv[i], 5) == 0) {
360 id = st->argv->argv[i] + 5;
364 if (st->argv->argc > 0)
365 opt->option->name = talloc_strdup(opt, st->argv->argv[0]);
367 opt->option->name = talloc_strdup(opt, "(unknown)");
369 opt->option->id = talloc_asprintf(opt->option, "%s#%s",
370 script->ctx->device->device->id,
371 id ? id : opt->option->name);
375 statements_execute(script, st->statements);
377 if (!opt->boot_image)
380 opt->option->is_default = option_is_default(script, opt, id);
382 discover_context_add_boot_option(script->ctx, opt);
389 static int function_invoke(struct grub2_script *script,
390 void *data, int argc, char **argv)
392 struct grub2_statement_function *fn = data;
396 /* set positional parameters */
397 for (i = 0; i < argc; i++) {
398 name = talloc_asprintf(script, "$%d", i);
399 script_env_set(script, name, argv[i]);
402 return statements_execute(script, fn->body);
405 int statement_function_execute(struct grub2_script *script,
406 struct grub2_statement *statement)
408 struct grub2_statement_function *st = to_stmt_function(statement);
411 if (st->name->type == GRUB2_WORD_VAR)
412 expand_var(script, st->name);
414 name = st->name->text;
415 script_register_function(script, name, function_invoke, st);
420 static void init_env(struct grub2_script *script)
422 struct env_entry *env;
425 list_init(&script->environment);
427 /* use location of the parsed config file to determine the prefix */
428 env = talloc(script, struct env_entry);
431 if (script->filename) {
432 sep = strrchr(script->filename, '/');
434 prefix = talloc_strndup(env, script->filename,
435 sep - script->filename);
438 script_env_set(script, "prefix", prefix ? : default_prefix);
442 /* establish feature settings */
443 script_env_set(script, "feature_menuentry_id", "y");
446 void script_register_function(struct grub2_script *script,
447 const char *name, grub2_function fn,
450 struct grub2_symtab_entry *entry;
452 entry = talloc(script, struct grub2_symtab_entry);
456 list_add(&script->symtab, &entry->list);
460 void script_execute(struct grub2_script *script)
463 statements_execute(script, script->statements);
466 struct grub2_script *create_script(struct grub2_parser *parser,
467 struct discover_context *ctx)
469 struct grub2_script *script;
471 script = talloc_zero(parser, struct grub2_script);
475 list_init(&script->symtab);
476 register_builtins(script);