diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2013-11-27 19:51:49 +0800 |
---|---|---|
committer | Jeremy Kerr <jk@ozlabs.org> | 2013-11-27 20:02:30 +0800 |
commit | 9e5b77f258b8e993f480763d8e5269bd8c87b508 (patch) | |
tree | 6e802cf451fe385f67e51e89d423fdb6cf2a1116 | |
parent | 77e0df22dd3fb1cf0dcdd1124a5aafa2ec872a1f (diff) | |
download | talos-petitboot-9e5b77f258b8e993f480763d8e5269bd8c87b508.tar.gz talos-petitboot-9e5b77f258b8e993f480763d8e5269bd8c87b508.zip |
discover/grub2: Populate $prefix from config file location
Rather than always using the default prefix, we should determine it from
the location of the grub2 config file.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
-rw-r--r-- | discover/grub2/grub2.c | 2 | ||||
-rw-r--r-- | discover/grub2/grub2.h | 4 | ||||
-rw-r--r-- | discover/grub2/parser.y | 5 | ||||
-rw-r--r-- | discover/grub2/script.c | 18 | ||||
-rw-r--r-- | test/parser/Makefile.am | 1 | ||||
-rw-r--r-- | test/parser/test-grub2-nondefault-prefix.c | 29 |
6 files changed, 54 insertions, 5 deletions
diff --git a/discover/grub2/grub2.c b/discover/grub2/grub2.c index ffb6ece..7f63c35 100644 --- a/discover/grub2/grub2.c +++ b/discover/grub2/grub2.c @@ -103,7 +103,7 @@ static int grub2_parse(struct discover_context *dc) continue; parser = grub2_parser_create(dc); - grub2_parser_parse(parser, buf, len); + grub2_parser_parse(parser, *filename, buf, len); talloc_free(buf); talloc_free(parser); break; diff --git a/discover/grub2/grub2.h b/discover/grub2/grub2.h index 1515d69..6166289 100644 --- a/discover/grub2/grub2.h +++ b/discover/grub2/grub2.h @@ -89,6 +89,7 @@ struct grub2_script { struct list symtab; struct discover_context *ctx; struct discover_boot_option *opt; + const char *filename; unsigned int n_options; }; @@ -176,6 +177,7 @@ bool resolve_grub2_resource(struct device_handler *handler, /* external parser api */ struct grub2_parser *grub2_parser_create(struct discover_context *ctx); -void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len); +void grub2_parser_parse(struct grub2_parser *parser, const char *filename, + char *buf, int len); #endif /* GRUB2_H */ diff --git a/discover/grub2/parser.y b/discover/grub2/parser.y index a3473ca..5a4d4f8 100644 --- a/discover/grub2/parser.y +++ b/discover/grub2/parser.y @@ -303,11 +303,14 @@ struct grub2_parser *grub2_parser_create(struct discover_context *ctx) return parser; } -void grub2_parser_parse(struct grub2_parser *parser, char *buf, int len) +void grub2_parser_parse(struct grub2_parser *parser, const char *filename, + char *buf, int len) { YY_BUFFER_STATE bufstate; int rc; + parser->script->filename = filename; + bufstate = yy_scan_bytes(buf, len - 1, parser->scanner); yyset_lineno(1, parser->scanner); diff --git a/discover/grub2/script.c b/discover/grub2/script.c index e29d437..a58f1a0 100644 --- a/discover/grub2/script.c +++ b/discover/grub2/script.c @@ -400,12 +400,26 @@ int statement_function_execute(struct grub2_script *script, static void init_env(struct grub2_script *script) { struct env_entry *env; + char *prefix, *sep; list_init(&script->environment); + /* use location of the parsed config file to determine the prefix */ env = talloc(script, struct env_entry); + + prefix = NULL; + if (script->filename) { + sep = strrchr(script->filename, '/'); + if (sep) + prefix = talloc_strndup(env, script->filename, + sep - script->filename); + } + env->name = talloc_strdup(env, "prefix"); - env->value = talloc_strdup(env, default_prefix); + if (prefix) + env->value = prefix; + else + env->value = talloc_strdup(env, default_prefix); list_add(&script->environment, &env->list); } @@ -426,6 +440,7 @@ void script_register_function(struct grub2_script *script, void script_execute(struct grub2_script *script) { + init_env(script); statements_execute(script, script->statements); } @@ -436,7 +451,6 @@ struct grub2_script *create_script(struct grub2_parser *parser, script = talloc_zero(parser, struct grub2_script); - init_env(script); script->ctx = ctx; list_init(&script->symtab); diff --git a/test/parser/Makefile.am b/test/parser/Makefile.am index e355af3..f2ee67e 100644 --- a/test/parser/Makefile.am +++ b/test/parser/Makefile.am @@ -36,6 +36,7 @@ TESTS = \ test-grub2-load-env \ test-grub2-save-env \ test-grub2-saved-default \ + test-grub2-nondefault-prefix \ test-grub2-f18-ppc64 \ test-grub2-ubuntu-13_04-x86 \ test-grub2-lexer-error \ diff --git a/test/parser/test-grub2-nondefault-prefix.c b/test/parser/test-grub2-nondefault-prefix.c new file mode 100644 index 0000000..420cf76 --- /dev/null +++ b/test/parser/test-grub2-nondefault-prefix.c @@ -0,0 +1,29 @@ + +#include "parser-test.h" + +#if 0 /* PARSER_EMBEDDED_CONFIG */ +menuentry 'test option' { + linux ${prefix}/vmlinux +} +#endif + + + +void run_test(struct parser_test *test) +{ + struct discover_boot_option *opt; + struct discover_context *ctx; + + test_read_conf_embedded(test, "/grub/grub.cfg"); + + test_run_parser(test, "grub2"); + + ctx = test->ctx; + + check_boot_option_count(ctx, 1); + opt = get_boot_option(ctx, 0); + + check_name(opt, "test option"); + check_resolved_local_resource(opt->boot_image, ctx->device, + "/grub/vmlinux"); +} |