diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2019-11-11 19:30:41 +0800 |
---|---|---|
committer | Jeremy Kerr <jk@ozlabs.org> | 2019-11-29 13:54:10 +0800 |
commit | b22445748f44ed6965f31f45b39abb690d24ec47 (patch) | |
tree | cb9f942dac9ff19cc1786ea23f9f5a3509deec48 /discover/grub2/builtins.c | |
parent | 9fc2ac627df17ddc8e7c2735053aeb9e1252ff6e (diff) | |
download | talos-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/builtins.c')
-rw-r--r-- | discover/grub2/builtins.c | 50 |
1 files changed, 43 insertions, 7 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); } |