From: Andreas Reichel <[email protected]>

For additional config file checks like fail-safe mode, define one
common open/read and close function to simplify code.

Signed-off-by: Andreas Reichel <[email protected]>
---
 env/fatvars.c     | 21 +++++++--------------
 env/syspart.c     | 29 +++++++++++++++++++++++------
 include/syspart.h |  4 ++++
 3 files changed, 34 insertions(+), 20 deletions(-)

diff --git a/env/fatvars.c b/env/fatvars.c
index bbff777..ab1b5c4 100644
--- a/env/fatvars.c
+++ b/env/fatvars.c
@@ -51,10 +51,8 @@ BG_STATUS save_current_config(void)
        }
 
        EFI_FILE_HANDLE fh = NULL;
-       efistatus = uefi_call_wrapper(
-           roots[current_partition]->Open, 5, roots[current_partition], &fh,
-           ENV_FILE_NAME, EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ,
-           EFI_FILE_ARCHIVE | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM);
+       efistatus = open_cfg_file(roots[current_partition], &fh,
+                                 EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ);
        if (EFI_ERROR(efistatus)) {
                Print(L"Error, could not open environment file on system "
                      L"partition %d: %r\n",
@@ -75,7 +73,7 @@ BG_STATUS save_current_config(void)
                result = BG_CONFIG_ERROR;
        }
 
-       if (EFI_ERROR(uefi_call_wrapper(fh->Close, 1, fh))) {
+       if (EFI_ERROR(close_cfg_file(roots[current_partition], fh))) {
                Print(L"Error, could not close environment config file.\n");
                result = BG_CONFIG_ERROR;
        }
@@ -123,11 +121,8 @@ BG_STATUS load_config(BG_LOADER_PARAMS *bglp)
        /* Load all config data */
        for (i = 0; i < numHandles; i++) {
                EFI_FILE_HANDLE fh = NULL;
-               if (EFI_ERROR(uefi_call_wrapper(
-                       roots[i]->Open, 5, roots[i], &fh, ENV_FILE_NAME,
-                       EFI_FILE_MODE_READ,
-                       EFI_FILE_READ_ONLY | EFI_FILE_ARCHIVE |
-                           EFI_FILE_HIDDEN | EFI_FILE_SYSTEM))) {
+               if (EFI_ERROR(open_cfg_file(roots[i], &fh,
+                                           EFI_FILE_MODE_READ))) {
                        Print(L"Warning, could not open environment file on "
                              L"config partition %d\n",
                              i);
@@ -135,14 +130,12 @@ BG_STATUS load_config(BG_LOADER_PARAMS *bglp)
                        continue;
                }
 
-               UINTN readlen = sizeof(BG_ENVDATA);
-               if (EFI_ERROR(uefi_call_wrapper(fh->Read, 3, fh, &readlen,
-                                               (VOID *)&env[i]))) {
+               if (EFI_ERROR(read_cfg_file(fh, (VOID *)&env[i]))) {
                        Print(L"Error reading environment from config "
                              L"partition %d.\n",
                              i);
                        env_invalid[i] = 1;
-                       if (EFI_ERROR(uefi_call_wrapper(fh->Close, 1, fh))) {
+                       if (EFI_ERROR(close_cfg_file(roots[i], fh))) {
                                Print(L"Error, could not close environment "
                                      L"config file.\n");
                        }
diff --git a/env/syspart.c b/env/syspart.c
index 7bdf595..aa848d0 100644
--- a/env/syspart.c
+++ b/env/syspart.c
@@ -14,9 +14,29 @@
 
 #include <syspart.h>
 #include <utils.h>
+#include <envdata.h>
 
 #define MAX_INFO_SIZE 1024
 
+EFI_STATUS open_cfg_file(EFI_FILE_HANDLE root, EFI_FILE_HANDLE *fh,
+                        UINT64 mode)
+{
+       return uefi_call_wrapper(root->Open, 5, root, fh, ENV_FILE_NAME, mode,
+                                EFI_FILE_ARCHIVE | EFI_FILE_HIDDEN |
+                                EFI_FILE_SYSTEM);
+}
+
+EFI_STATUS close_cfg_file(EFI_FILE_HANDLE root, EFI_FILE_HANDLE fh)
+{
+       return uefi_call_wrapper(root->Close, 1, fh);
+}
+
+EFI_STATUS read_cfg_file(EFI_FILE_HANDLE fh, VOID *buffer)
+{
+       UINTN readlen = sizeof(BG_ENVDATA);
+       return uefi_call_wrapper(fh->Read, 3, fh, &readlen, buffer);
+}
+
 EFI_STATUS enumerate_cfg_parts(EFI_FILE_HANDLE *roots, UINTN *numHandles)
 {
        EFI_STATUS status;
@@ -32,16 +52,13 @@ EFI_STATUS enumerate_cfg_parts(EFI_FILE_HANDLE *roots, 
UINTN *numHandles)
                if (!volumes[index].root) {
                        continue;
                }
-               status = uefi_call_wrapper(
-                   volumes[index].root->Open, 5, volumes[index].root, &fh,
-                   ENV_FILE_NAME, EFI_FILE_MODE_WRITE | EFI_FILE_MODE_READ,
-                   EFI_FILE_ARCHIVE | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM);
+               status = open_cfg_file(volumes[index].root, &fh,
+                                      EFI_FILE_MODE_READ);
                if (status == EFI_SUCCESS) {
                        Print(L"Config file found on volume %d.\n", index);
                        roots[rootCount] = volumes[index].root;
                        rootCount++;
-                       status = uefi_call_wrapper(volumes[index].root->Close,
-                                                  1, fh);
+                       status = close_cfg_file(volumes[index].root, fh);
                        if (EFI_ERROR(status)) {
                                Print(L"Could not close config file on "
                                      L"partition %d.\n",
diff --git a/include/syspart.h b/include/syspart.h
index 2b70ed5..27caa93 100644
--- a/include/syspart.h
+++ b/include/syspart.h
@@ -21,6 +21,10 @@
 #include <efipciio.h>
 #include "bootguard.h"
 
+EFI_STATUS open_cfg_file(EFI_FILE_HANDLE root, EFI_FILE_HANDLE *fh,
+                        UINT64 mode);
+EFI_STATUS close_cfg_file(EFI_FILE_HANDLE root, EFI_FILE_HANDLE fh);
+EFI_STATUS read_cfg_file(EFI_FILE_HANDLE fh, VOID *buffer);
 EFI_STATUS enumerate_cfg_parts(EFI_FILE_HANDLE *roots, UINTN *maxHandles);
 
 #endif // __H_SYSPART__
-- 
2.17.0

-- 
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20180525120108.31055-9-andreas.reichel.ext%40siemens.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to