From: Andreas Reichel <andreas.reichel....@siemens.com>

For better usability, `bg_setenv`'s parameter -f is changed from
`--file` to `--filepath` and now accepts a path, where `BGENV.DAT`
is created so that it can directly be applied to the real environ-
ment location.

Signed-off-by: Andreas Reichel <andreas.reichel....@siemens.com>
---
 docs/TODO.md      |  1 -
 docs/USAGE.md     | 12 ++++++------
 tools/bg_setenv.c | 20 ++++++++++++++------
 3 files changed, 20 insertions(+), 13 deletions(-)

diff --git a/docs/TODO.md b/docs/TODO.md
index 40d5ba8..832f76b 100644
--- a/docs/TODO.md
+++ b/docs/TODO.md
@@ -6,7 +6,6 @@
          the current working environment to the (latest-1) environment, so
          that if the current environment breaks, there is a backup with the
          latest values.
-       * Make `bg_setenv -f` take a path to where the `BGENV.DAT` is stored.
 
 * Application specific variables
        * applications may need to store their own variables into the
diff --git a/docs/USAGE.md b/docs/USAGE.md
index 7dd89ce..97a25cb 100644
--- a/docs/USAGE.md
+++ b/docs/USAGE.md
@@ -98,13 +98,13 @@ This step first creates a custom label contained in 
`EFILABEL`, which is later
 used to specify the kernel location.
 
 ```
-# mount /dev/sdX2 /mnt && cd /mnt
-# echo -n "KERNEL1" | iconv -f ascii -t UTF-16LE > EFILABEL
-# bg_setenv -f -r 1 --kernel="C:KERNEL1:vmlinuz-linux" --args="root=/dev/sdX4 
noinitrd"
+# mount /dev/sdX2 /mnt
+# echo -n "KERNEL1" | iconv -f ascii -t UTF-16LE > /mnt/EFILABEL
+# bg_setenv -f /mnt -r 1 --kernel="C:KERNEL1:vmlinuz-linux" 
--args="root=/dev/sdX4 noinitrd"
 # umount /mnt
-# mount /dev/sdX3 /mnt && cd /mnt
-# echo -n "KERNEL2" | iconv -f ascii -t UTF-16LE > EFILABEL
-# bg_setenv -f -r 2 --kernel="C:KERNEL2:vmlinuz-linux" --args="root=/dev/sdX5 
noinitrd"
+# mount /dev/sdX3 /mnt
+# echo -n "KERNEL2" | iconv -f ascii -t UTF-16LE > /mnt/EFILABEL
+# bg_setenv -f /mnt -r 2 --kernel="C:KERNEL2:vmlinuz-linux" 
--args="root=/dev/sdX5 noinitrd"
 # umount /mnt
 ```
 
diff --git a/tools/bg_setenv.c b/tools/bg_setenv.c
index 5c313cf..5cc6403 100644
--- a/tools/bg_setenv.c
+++ b/tools/bg_setenv.c
@@ -24,7 +24,9 @@ static struct argp_option options_setenv[] = {
                                 "above zero is updated."},
     {"revision", 'r', "REVISION", 0, "Set revision value"},
     {"testing", 't', "TESTING", 0, "Set test mode for environment"},
-    {"file", 'f', 0, 0, "Output environment to file"},
+    {"filepath", 'f', "ENVFILE_DIR", 0, "Output environment to file. Expects "
+                                       "an output path where the file name "
+                                       "is automatically appended."},
     {"watchdog", 'w', "WATCHDOG_TIMEOUT", 0, "Watchdog timeout in seconds"},
     {"confirm", 'c', 0, 0, "Confirm working environment"},
     {"update", 'u', 0, 0, "Automatically update oldest revision"},
@@ -58,6 +60,8 @@ static bool part_specified = false;
 
 static bool verbosity = false;
 
+static char *envfilepath = NULL;
+
 static error_t parse_opt(int key, char *arg, struct argp_state *state)
 {
        struct arguments *arguments = state->input;
@@ -138,6 +142,7 @@ static error_t parse_opt(int key, char *arg, struct 
argp_state *state)
                break;
        case 'f':
                arguments->output_to_file = true;
+               asprintf(&envfilepath, "%s/%s", arg, FAT_ENV_FILENAME);
                break;
        case 'c':
                VERBOSE(stdout,
@@ -363,22 +368,25 @@ int main(int argc, char **argv)
                if (verbosity) {
                        dump_env(&data);
                }
-               FILE *of = fopen(FAT_ENV_FILENAME, "wb");
+               FILE *of = fopen(envfilepath, "wb");
                if (of) {
                        if (fwrite(&data, sizeof(BG_ENVDATA), 1, of) != 1) {
                                fprintf(stderr,
-                                       "Error writing to output file.\n");
-                               result = 1;
+                                       "Error writing to output file: %s\n",
+                                       strerror(errno));
+                               result = errno;
                        }
                        if (fclose(of)) {
                                fprintf(stderr, "Error closing output file.\n");
                                result = 1;
                        };
-                       printf("Output written to %s.\n", FAT_ENV_FILENAME);
+                       printf("Output written to %s.\n", envfilepath);
                } else {
-                       fprintf(stderr, "Error opening output file.\n");
+                       fprintf(stderr, "Error opening output file %s (%s).\n",
+                               envfilepath, strerror(errno));
                        result = 1;
                }
+               free(envfilepath);
        }
 
        return result;
-- 
2.14.1

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to efibootguard-dev+unsubscr...@googlegroups.com.
To post to this group, send email to efibootguard-dev@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20170912105511.11060-4-andreas.reichel.ext%40siemens.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to