Skip to content

Commit 7edc34c

Browse files
martinezjaviersammj
authored andcommitted
discover/grub2: Allow to separate the --id argument using a space char
The GRUB menuentry command allows to separate the arguments for options using either a '=' or a ' '. The latter is the convention used when the menu entries are defined in the GRUB config file, but this is currently not supported by Petitboot. Add tests to cover both using '--id=foo' and '--id foo' as options. Signed-off-by: Javier Martinez Canillas <[email protected]> Signed-off-by: Samuel Mendoza-Jonas <[email protected]>
1 parent 53e0f3e commit 7edc34c

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

discover/grub2/script.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,16 @@ int statement_menuentry_execute(struct grub2_script *script,
339339
* implementation to get --id= working.
340340
*/
341341
for (i = 1; i < st->argv->argc; ++i) {
342-
if (strncmp("--id=", st->argv->argv[i], 5) == 0) {
343-
id = st->argv->argv[i] + 5;
344-
break;
342+
if (strncmp("--id", st->argv->argv[i], strlen("--id")) == 0) {
343+
if (strlen(st->argv->argv[i]) > strlen("--id=")) {
344+
id = st->argv->argv[i] + strlen("--id=");
345+
break;
346+
}
347+
348+
if (i + 1 < st->argv->argc) {
349+
id = st->argv->argv[i + 1];
350+
break;
351+
}
345352
}
346353
}
347354
if (st->argv->argc > 0)

test/parser/Makefile.am

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ parser_TESTS = \
1919
test/parser/test-grub2-noeol \
2020
test/parser/test-grub2-menuentry-formats \
2121
test/parser/test-grub2-if-formats \
22+
test/parser/test-grub2-default-id \
23+
test/parser/test-grub2-default-id-space \
2224
test/parser/test-grub2-default-index \
2325
test/parser/test-grub2-default-multiword \
2426
test/parser/test-grub2-implicit-default-unset \
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
#include "parser-test.h"
3+
4+
#if 0 /* PARSER_EMBEDDED_CONFIG */
5+
set default=option1
6+
menuentry 'test-option-0' --id option0 {
7+
linux /vmlinux.0
8+
}
9+
menuentry 'test-option-1' --id option1 {
10+
linux /vmlinux.1
11+
}
12+
menuentry 'test-option-2' --id option2 {
13+
linux /vmlinux.2
14+
}
15+
#endif
16+
17+
void run_test(struct parser_test *test)
18+
{
19+
struct discover_boot_option *opt;
20+
struct discover_context *ctx;
21+
22+
test_read_conf_embedded(test, "/boot/grub2/grub.cfg");
23+
test_run_parser(test, "grub2");
24+
25+
ctx = test->ctx;
26+
27+
check_boot_option_count(ctx, 3);
28+
opt = get_boot_option(ctx, 1);
29+
30+
check_name(opt, "test-option-1");
31+
check_resolved_local_resource(opt->boot_image, ctx->device,
32+
"/vmlinux.1");
33+
check_is_default(opt);
34+
}

test/parser/test-grub2-default-id.c

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
#include "parser-test.h"
3+
4+
#if 0 /* PARSER_EMBEDDED_CONFIG */
5+
set default=option1
6+
menuentry 'test-option-0' --id=option0 {
7+
linux /vmlinux.0
8+
}
9+
menuentry 'test-option-1' --id=option1 {
10+
linux /vmlinux.1
11+
}
12+
menuentry 'test-option-2' --id=option2 {
13+
linux /vmlinux.2
14+
}
15+
#endif
16+
17+
void run_test(struct parser_test *test)
18+
{
19+
struct discover_boot_option *opt;
20+
struct discover_context *ctx;
21+
22+
test_read_conf_embedded(test, "/boot/grub2/grub.cfg");
23+
test_run_parser(test, "grub2");
24+
25+
ctx = test->ctx;
26+
27+
check_boot_option_count(ctx, 3);
28+
opt = get_boot_option(ctx, 1);
29+
30+
check_name(opt, "test-option-1");
31+
check_resolved_local_resource(opt->boot_image, ctx->device,
32+
"/vmlinux.1");
33+
check_is_default(opt);
34+
}

0 commit comments

Comments
 (0)