]> git.ozlabs.org Git - petitboot/blobdiff - discover/grub2/blscfg.c
discover/grub: Allow to set a default index for BLS entries
[petitboot] / discover / grub2 / blscfg.c
index 0f69f7e291060550df492785cf402e83b6ead78f..a1076b9db27b5002e2ba984779e881ae092170ad 100644 (file)
@@ -20,6 +20,7 @@
 struct bls_state {
        struct discover_boot_option *opt;
        struct grub2_script *script;
 struct bls_state {
        struct discover_boot_option *opt;
        struct grub2_script *script;
+       unsigned int idx;
        const char *filename;
        const char *title;
        const char *version;
        const char *filename;
        const char *title;
        const char *version;
@@ -81,19 +82,25 @@ static void bls_process_pair(struct conf_context *conf, const char *name,
        }
 }
 
        }
 }
 
-static bool option_is_default(struct grub2_script *script,
+static bool option_is_default(struct bls_state *state,
                              struct boot_option *option)
 {
                              struct boot_option *option)
 {
+       unsigned int idx;
        const char *var;
        const char *var;
+       char *end;
 
 
-       var = script_env_get(script, "default");
+       var = script_env_get(state->script, "default");
        if (!var)
                return false;
 
        if (!strcmp(var, option->id))
                return true;
 
        if (!var)
                return false;
 
        if (!strcmp(var, option->id))
                return true;
 
-       return !strcmp(var, option->name);
+       if (!strcmp(var, option->name))
+               return true;
+
+       idx = strtoul(var, &end, 10);
+       return end != var && *end == '\0' && idx == state->idx;
 }
 
 static void bls_finish(struct conf_context *conf)
 }
 
 static void bls_finish(struct conf_context *conf)
@@ -141,9 +148,10 @@ static void bls_finish(struct conf_context *conf)
                opt->dtb = create_grub2_resource(opt, conf->dc->device,
                                                 root, state->dtb);
 
                opt->dtb = create_grub2_resource(opt, conf->dc->device,
                                                 root, state->dtb);
 
-       option->is_default = option_is_default(state->script, option);
+       option->is_default = option_is_default(state, option);
 
 
-       discover_context_add_boot_option(dc, opt);
+       list_add_tail(&state->script->options, &opt->list);
+       state->script->n_options++;
 
        device_handler_status_dev_info(dc->handler, dc->device,
                                       _("Created menu entry from BLS file %s"),
 
        device_handler_status_dev_info(dc->handler, dc->device,
                                       _("Created menu entry from BLS file %s"),
@@ -162,7 +170,7 @@ static int bls_filter(const struct dirent *ent)
 
 static int bls_sort(const struct dirent **ent_a, const struct dirent **ent_b)
 {
 
 static int bls_sort(const struct dirent **ent_a, const struct dirent **ent_b)
 {
-       return strverscmp((*ent_b)->d_name, (*ent_a)->d_name);
+       return strverscmp((*ent_a)->d_name, (*ent_b)->d_name);
 }
 
 int builtin_blscfg(struct grub2_script *script,
 }
 
 int builtin_blscfg(struct grub2_script *script,
@@ -175,11 +183,13 @@ int builtin_blscfg(struct grub2_script *script,
                int argc __attribute__((unused)),
                char *argv[] __attribute__((unused)))
 {
                int argc __attribute__((unused)),
                char *argv[] __attribute__((unused)))
 {
+       unsigned int current_idx = script->n_options;
        struct discover_context *dc = script->ctx;
        struct dirent **bls_entries;
        struct conf_context *conf;
        struct bls_state *state;
        char *buf, *filename;
        struct discover_context *dc = script->ctx;
        struct dirent **bls_entries;
        struct conf_context *conf;
        struct bls_state *state;
        char *buf, *filename;
+       const char *blsdir;
        int n, len, rc = -1;
 
        conf = talloc_zero(dc, struct conf_context);
        int n, len, rc = -1;
 
        conf = talloc_zero(dc, struct conf_context);
@@ -191,12 +201,16 @@ int builtin_blscfg(struct grub2_script *script,
        conf->process_pair = bls_process_pair;
        conf->finish = bls_finish;
 
        conf->process_pair = bls_process_pair;
        conf->finish = bls_finish;
 
-       n = parser_scandir(dc, BLS_DIR, &bls_entries, bls_filter, bls_sort);
+       blsdir = script_env_get(script, "blsdir");
+       if (!blsdir)
+               blsdir = BLS_DIR;
+
+       n = parser_scandir(dc, blsdir, &bls_entries, bls_filter, bls_sort);
        if (n <= 0)
                goto err;
 
        while (n--) {
        if (n <= 0)
                goto err;
 
        while (n--) {
-               filename = talloc_asprintf(dc, BLS_DIR"/%s",
+               filename = talloc_asprintf(dc, "%s/%s", blsdir,
                                           bls_entries[n]->d_name);
                if (!filename)
                        break;
                                           bls_entries[n]->d_name);
                if (!filename)
                        break;
@@ -211,6 +225,7 @@ int builtin_blscfg(struct grub2_script *script,
 
                state->script = script;
                state->filename = filename;
 
                state->script = script;
                state->filename = filename;
+               state->idx = current_idx++;
                conf->parser_info = state;
 
                rc = parser_request_file(dc, dc->device, filename, &buf, &len);
                conf->parser_info = state;
 
                rc = parser_request_file(dc, dc->device, filename, &buf, &len);