Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
discover/grub2: implement 'source' command
This change add support for the grub2 'source' command, executing a referenced script in the current parse context. We impose a limit of 10 (concurrent) source commands, to prevent infinite recursion. Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
- Loading branch information
Showing
7 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
/* check that we can source other scripts, and functions can be defined | ||
* and called across sourced scripts */ | ||
|
||
#include "parser-test.h" | ||
|
||
#if 0 /* PARSER_EMBEDDED_CONFIG */ | ||
|
||
function f1 { | ||
menuentry "f1$1" { linux $2 } | ||
} | ||
|
||
source /grub/2.cfg | ||
|
||
f2 a /vmlinux | ||
|
||
#endif | ||
|
||
void run_test(struct parser_test *test) | ||
{ | ||
struct discover_boot_option *opt; | ||
struct discover_context *ctx; | ||
struct discover_device *dev; | ||
|
||
ctx = test->ctx; | ||
dev = ctx->device; | ||
|
||
test_read_conf_embedded(test, "/grub/grub.cfg"); | ||
|
||
test_add_file_string(test, dev, | ||
"/grub/2.cfg", | ||
"function f2 { menuentry \"f2$1\" { linux $2 } }\n" | ||
"f1 a /vmlinux\n"); | ||
|
||
test_run_parser(test, "grub2"); | ||
|
||
check_boot_option_count(ctx, 2); | ||
|
||
opt = get_boot_option(ctx, 0); | ||
check_name(opt, "f1a"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/vmlinux"); | ||
|
||
opt = get_boot_option(ctx, 1); | ||
check_name(opt, "f2a"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/vmlinux"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
/* check that have a maximum source recursion limit */ | ||
|
||
#include "parser-test.h" | ||
|
||
#if 0 /* PARSER_EMBEDDED_CONFIG */ | ||
|
||
name=a$name | ||
|
||
menuentry $name { | ||
linux /a | ||
} | ||
|
||
source /grub/grub.cfg | ||
|
||
#endif | ||
|
||
void run_test(struct parser_test *test) | ||
{ | ||
struct discover_boot_option *opt; | ||
struct discover_context *ctx; | ||
struct discover_device *dev; | ||
|
||
ctx = test->ctx; | ||
dev = ctx->device; | ||
|
||
test_read_conf_embedded(test, "/grub/grub.cfg"); | ||
|
||
test_run_parser(test, "grub2"); | ||
|
||
/* we error out after 10 levels, but we should still have | ||
* parse results up to that point | ||
*/ | ||
check_boot_option_count(ctx, 11); | ||
|
||
opt = get_boot_option(ctx, 0); | ||
check_name(opt, "a"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/a"); | ||
|
||
opt = get_boot_option(ctx,10); | ||
check_name(opt, "aaaaaaaaaaa"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/a"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* check that we can source other files recursively */ | ||
|
||
#include "parser-test.h" | ||
|
||
#if 0 /* PARSER_EMBEDDED_CONFIG */ | ||
|
||
menuentry a { | ||
linux /a | ||
} | ||
|
||
source /grub/2.cfg | ||
|
||
menuentry c { | ||
linux /c | ||
} | ||
|
||
#endif | ||
|
||
void run_test(struct parser_test *test) | ||
{ | ||
struct discover_boot_option *opt; | ||
struct discover_context *ctx; | ||
struct discover_device *dev; | ||
|
||
ctx = test->ctx; | ||
dev = ctx->device; | ||
|
||
test_read_conf_embedded(test, "/grub/grub.cfg"); | ||
|
||
/* four levels of config files, the last defining a boot option */ | ||
test_add_file_string(test, dev, | ||
"/grub/2.cfg", | ||
"source /grub/3.cfg\n"); | ||
|
||
test_add_file_string(test, dev, | ||
"/grub/3.cfg", | ||
"source /grub/4.cfg\n"); | ||
|
||
test_add_file_string(test, dev, | ||
"/grub/4.cfg", | ||
"menuentry b { linux /b }\n"); | ||
|
||
test_run_parser(test, "grub2"); | ||
|
||
check_boot_option_count(ctx, 3); | ||
|
||
opt = get_boot_option(ctx, 0); | ||
check_name(opt, "a"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/a"); | ||
|
||
opt = get_boot_option(ctx, 1); | ||
check_name(opt, "b"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/b"); | ||
|
||
opt = get_boot_option(ctx, 2); | ||
check_name(opt, "c"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/c"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
/* check that we can source other scripts, and variables get passed | ||
* in to and out of sourced scripts */ | ||
|
||
#include "parser-test.h" | ||
|
||
#if 0 /* PARSER_EMBEDDED_CONFIG */ | ||
|
||
menuentry a { | ||
linux /a | ||
} | ||
|
||
# var: outer -> inner -> outer | ||
v=b | ||
|
||
source /grub/2.cfg | ||
|
||
menuentry $v { | ||
linux /c | ||
} | ||
|
||
#endif | ||
|
||
void run_test(struct parser_test *test) | ||
{ | ||
struct discover_boot_option *opt; | ||
struct discover_context *ctx; | ||
struct discover_device *dev; | ||
|
||
ctx = test->ctx; | ||
dev = ctx->device; | ||
|
||
test_read_conf_embedded(test, "/grub/grub.cfg"); | ||
|
||
test_add_file_string(test, dev, | ||
"/grub/2.cfg", | ||
"menuentry $v { linux /b }\nv=c\n"); | ||
|
||
test_run_parser(test, "grub2"); | ||
|
||
check_boot_option_count(ctx, 3); | ||
|
||
opt = get_boot_option(ctx, 0); | ||
check_name(opt, "a"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/a"); | ||
|
||
opt = get_boot_option(ctx, 1); | ||
check_name(opt, "b"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/b"); | ||
|
||
opt = get_boot_option(ctx, 2); | ||
check_name(opt, "c"); | ||
check_resolved_local_resource(opt->boot_image, dev, "/c"); | ||
} |