summaryrefslogtreecommitdiffstats
path: root/discover/grub2
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2019-11-11 19:30:41 +0800
committerJeremy Kerr <jk@ozlabs.org>2019-11-29 13:54:10 +0800
commitb22445748f44ed6965f31f45b39abb690d24ec47 (patch)
treecb9f942dac9ff19cc1786ea23f9f5a3509deec48 /discover/grub2
parent9fc2ac627df17ddc8e7c2735053aeb9e1252ff6e (diff)
downloadtalos-petitboot-b22445748f44ed6965f31f45b39abb690d24ec47.tar.gz
talos-petitboot-b22445748f44ed6965f31f45b39abb690d24ec47.zip
discover/grub2: Allow (device)/path references in general script usage
Currently, we have support for grub2 (device)/path syntax for boot resources. This change allows this syntax for general paths in grub2 scripts (for example, -f tests). This involves exposing grub2_lookup_device, to allow the script execution code to resolve pathnames. Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Diffstat (limited to 'discover/grub2')
-rw-r--r--discover/grub2/builtins.c50
-rw-r--r--discover/grub2/grub2.c4
-rw-r--r--discover/grub2/grub2.h2
3 files changed, 47 insertions, 9 deletions
diff --git a/discover/grub2/builtins.c b/discover/grub2/builtins.c
index 765c4d7..c726216 100644
--- a/discover/grub2/builtins.c
+++ b/discover/grub2/builtins.c
@@ -197,6 +197,32 @@ static int builtin_search(struct grub2_script *script,
return 0;
}
+static int parse_to_device_path(struct grub2_script *script,
+ const char *desc, struct discover_device **devp,
+ char **pathp)
+{
+ struct discover_device *dev;
+ struct grub2_file *file;
+
+ file = grub2_parse_file(script, desc);
+ if (!file)
+ return -1;
+
+ dev = script->ctx->device;
+ if (file->dev)
+ dev = grub2_lookup_device(script->ctx->handler, file->dev);
+
+ if (!dev)
+ return -1;
+
+ *devp = dev;
+ *pathp = talloc_strdup(script, file->path);
+
+ talloc_free(file);
+
+ return 0;
+}
+
/* Note that GRUB does not follow symlinks in evaluating all file
* tests but -s, unlike below. However, it seems like a bad idea to
* emulate GRUB's behavior (e.g., it would take extra work), so we
@@ -204,12 +230,17 @@ static int builtin_search(struct grub2_script *script,
static bool builtin_test_op_file(struct grub2_script *script, char op,
const char *file)
{
+ struct discover_device *dev;
+ struct stat statbuf;
bool result;
+ char *path;
int rc;
- struct stat statbuf;
- rc = parser_stat_path(script->ctx, script->ctx->device,
- file, &statbuf);
+ rc = parse_to_device_path(script, file, &dev, &path);
+ if (rc)
+ return false;
+
+ rc = parser_stat_path(script->ctx, dev, path, &statbuf);
if (rc)
return false;
@@ -237,16 +268,21 @@ static bool builtin_test_op_file(struct grub2_script *script, char op,
static bool builtin_test_op_dir(struct grub2_script *script, char op,
const char *dir)
{
- int rc;
+ struct discover_device *dev;
struct stat statbuf;
+ char *path;
+ int rc;
if (op != 'd')
return false;
- rc = parser_stat_path(script->ctx, script->ctx->device, dir, &statbuf);
- if (rc) {
+ rc = parse_to_device_path(script, dir, &dev, &path);
+ if (rc)
+ return false;
+
+ rc = parser_stat_path(script->ctx, dev, path, &statbuf);
+ if (rc)
return false;
- }
return S_ISDIR(statbuf.st_mode);
}
diff --git a/discover/grub2/grub2.c b/discover/grub2/grub2.c
index a08a320..a8d115f 100644
--- a/discover/grub2/grub2.c
+++ b/discover/grub2/grub2.c
@@ -33,8 +33,8 @@ static const char *const grub2_conf_files[] = {
NULL
};
-static struct discover_device *grub2_lookup_device(
- struct device_handler *handler, const char *desc)
+struct discover_device *grub2_lookup_device(struct device_handler *handler,
+ const char *desc)
{
struct discover_device *dev;
diff --git a/discover/grub2/grub2.h b/discover/grub2/grub2.h
index ef32d4b..eabd6d6 100644
--- a/discover/grub2/grub2.h
+++ b/discover/grub2/grub2.h
@@ -200,6 +200,8 @@ bool resolve_grub2_resource(struct device_handler *handler,
/* grub-style device+path parsing */
struct grub2_file *grub2_parse_file(struct grub2_script *script,
const char *str);
+struct discover_device *grub2_lookup_device(struct device_handler *handler,
+ const char *desc);
/* external parser api */
struct grub2_parser *grub2_parser_create(struct discover_context *ctx);
OpenPOWER on IntegriCloud