opt->option->is_default = option_is_default(script, opt, id);
- discover_context_add_boot_option(script->ctx, opt);
+ list_add_tail(&script->options, &opt->list);
script->n_options++;
script->opt = NULL;
list_add(&script->symtab, &entry->list);
}
+static void set_fallback_default(struct grub2_script *script)
+{
+ struct discover_boot_option *opt, *first = NULL;
+ bool have_default = false;
+
+ list_for_each_entry(&script->options, opt, list) {
+ if (!first)
+ first = opt;
+ have_default = have_default || opt->option->is_default;
+ }
+
+ if (!have_default && first) {
+ const char *env = script_env_get(script, "default");
+
+ pb_log("grub: no explicit default (env default=%s), "
+ "falling back to first option (%s)\n",
+ env ?: "unset", first->option->name);
+
+ first->option->is_default = true;
+ }
+}
void script_execute(struct grub2_script *script)
{
+ struct discover_boot_option *opt, *tmp;
+
init_env(script);
statements_execute(script, script->statements);
+
+ set_fallback_default(script);
+
+ list_for_each_entry_safe(&script->options, opt, tmp, list)
+ discover_context_add_boot_option(script->ctx, opt);
+
+ /* Our option list will be invalid, as we've added all options to the
+ * discover context */
+ list_init(&script->options);
}
struct grub2_script *create_script(struct grub2_parser *parser,
script->ctx = ctx;
list_init(&script->symtab);
+ list_init(&script->options);
register_builtins(script);
return script;
test/parser/test-grub2-if-formats \
test/parser/test-grub2-default-index \
test/parser/test-grub2-default-multiword \
+ test/parser/test-grub2-implicit-default-unset \
+ test/parser/test-grub2-implicit-default-invalid \
test/parser/test-grub2-multiple-resolve \
test/parser/test-grub2-multiple-id \
test/parser/test-grub2-single-line-if \
--- /dev/null
+
+#include "parser-test.h"
+
+#if 0 /* PARSER_EMBEDDED_CONFIG */
+default=missing
+menuentry 'test.1' {
+ linux /vmlinux
+}
+menuentry 'test.2' {
+ linux /vmlinux
+}
+#endif
+
+void run_test(struct parser_test *test)
+{
+ struct discover_boot_option *opt;
+
+ test_read_conf_embedded(test, "/grub2/grub.cfg");
+ test_run_parser(test, "grub2");
+
+ check_boot_option_count(test->ctx, 2);
+ opt = get_boot_option(test->ctx, 0);
+
+ check_name(opt, "test.1");
+ check_is_default(opt);
+}
--- /dev/null
+
+#include "parser-test.h"
+
+#if 0 /* PARSER_EMBEDDED_CONFIG */
+menuentry 'test.1' {
+ linux /vmlinux
+}
+menuentry 'test.2' {
+ linux /vmlinux
+}
+#endif
+
+void run_test(struct parser_test *test)
+{
+ struct discover_boot_option *opt;
+
+ test_read_conf_embedded(test, "/grub2/grub.cfg");
+ test_run_parser(test, "grub2");
+
+ check_boot_option_count(test->ctx, 2);
+ opt = get_boot_option(test->ctx, 0);
+
+ check_name(opt, "test.1");
+ check_is_default(opt);
+}