summaryrefslogtreecommitdiffstats
path: root/discover
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2019-11-14 15:06:26 +0800
committerJeremy Kerr <jk@ozlabs.org>2019-11-29 13:54:10 +0800
commit967cfa7e5c1bfb4d2cf78bb3de3dc6d36b78c440 (patch)
tree880bc62b0e2aeacb70105f923a19821fb1bd4291 /discover
parent9711179694bb0e52c5951dc7222f1f79fcba814d (diff)
downloadtalos-petitboot-967cfa7e5c1bfb4d2cf78bb3de3dc6d36b78c440.tar.gz
talos-petitboot-967cfa7e5c1bfb4d2cf78bb3de3dc6d36b78c440.zip
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>
Diffstat (limited to 'discover')
-rw-r--r--discover/grub2/builtins.c51
-rw-r--r--discover/grub2/grub2.h1
2 files changed, 51 insertions, 1 deletions
diff --git a/discover/grub2/builtins.c b/discover/grub2/builtins.c
index c726216..ab1407a 100644
--- a/discover/grub2/builtins.c
+++ b/discover/grub2/builtins.c
@@ -401,6 +401,51 @@ static int builtin_test(struct grub2_script *script,
return rc ? 0 : 1;
}
+static int builtin_source(struct grub2_script *script,
+ void *data __attribute__((unused)),
+ int argc, char *argv[])
+{
+ struct grub2_statements *statements;
+ struct discover_device *dev;
+ const char *filename;
+ char *path, *buf;
+ int rc, len;
+
+ if (argc != 2)
+ return false;
+
+ /* limit script recursion */
+ if (script->include_depth >= 10)
+ return false;
+
+ rc = parse_to_device_path(script, argv[1], &dev, &path);
+ if (rc)
+ return false;
+
+ rc = parser_request_file(script->ctx, dev, path, &buf, &len);
+ if (rc)
+ return false;
+
+ /* save current script state */
+ statements = script->statements;
+ filename = script->filename;
+ script->include_depth++;
+
+ rc = grub2_parser_parse(script->parser, argv[1], buf, len);
+
+ if (!rc)
+ statements_execute(script, script->statements);
+
+ talloc_free(script->statements);
+
+ /* restore state */
+ script->statements = statements;
+ script->filename = filename;
+ script->include_depth--;
+
+ return !rc;
+}
+
static int builtin_true(struct grub2_script *script __attribute__((unused)),
void *data __attribute__((unused)),
int argc __attribute__((unused)),
@@ -491,7 +536,11 @@ static struct {
{
.name = "blscfg",
.fn = builtin_blscfg,
- }
+ },
+ {
+ .name = "source",
+ .fn = builtin_source,
+ },
};
static const char *nops[] = {
diff --git a/discover/grub2/grub2.h b/discover/grub2/grub2.h
index deaf976..75f6aa0 100644
--- a/discover/grub2/grub2.h
+++ b/discover/grub2/grub2.h
@@ -100,6 +100,7 @@ struct grub2_script {
const char *filename;
unsigned int n_options;
struct list options;
+ int include_depth;
};
struct grub2_parser {
OpenPOWER on IntegriCloud