summaryrefslogtreecommitdiff
path: root/include/test.h
diff options
context:
space:
mode:
authorPeter Jones <pjones@redhat.com>2021-07-22 15:45:23 -0400
committerPeter Jones <pjones@redhat.com>2021-09-07 17:05:04 -0400
commit2c9eebcf6ddd198c5ba49d784c4536d05023c28b (patch)
tree2d719e8b49b3d5d0d2ac0e73cae6fa4eafb60dbb /include/test.h
parent5ed27307d4854e67cf999fd7785b48f5cf488a51 (diff)
downloadefi-boot-shim-2c9eebcf6ddd198c5ba49d784c4536d05023c28b.tar.gz
efi-boot-shim-2c9eebcf6ddd198c5ba49d784c4536d05023c28b.zip
tests: add a mock implementation of {Get,Set}Variable and tests for it
Some tests will need variables, and so we need a mock implementation of the various calls relating to them. This patch adds implementations for the EFI Runtime Services calls GetVariable(), SetVariable(), GetNextVariableName(), and QueryVariableInfo(). Additionally, it enforces tunable limits on storage for variables, and (with only a little work) the limits can be different for SetVariable() vs what is returned by QueryVariableInfo(). That is, it can lie to you like real systems do. Signed-off-by: Peter Jones <pjones@redhat.com>
Diffstat (limited to 'include/test.h')
-rw-r--r--include/test.h124
1 files changed, 116 insertions, 8 deletions
diff --git a/include/test.h b/include/test.h
index fcaa1cdf..5261dbd9 100644
--- a/include/test.h
+++ b/include/test.h
@@ -43,21 +43,27 @@
#include <stdlib.h>
-#define ZeroMem(buf, sz) memset(buf, 0, sz)
-#define SetMem(buf, sz, value) memset(buf, value, sz)
-#define CopyMem(dest, src, len) memcpy(dest, src, len)
-#define CompareMem(dest, src, len) memcmp(dest, src, len)
+#include <assert.h>
+
+#include <efiui.h>
+#include <efilib.h>
+
+#include <efivar/efivar.h>
#include <assert.h>
-#define AllocateZeroPool(x) calloc(1, (x))
-#define AllocatePool(x) malloc(x)
-#define FreePool(x) free(x)
-#define ReallocatePool(old, oldsz, newsz) realloc(old, newsz)
+#include "list.h"
+INTN StrCmp(IN CONST CHAR16 *s1,
+ IN CONST CHAR16 *s2);
INTN StrnCmp(IN CONST CHAR16 *s1,
IN CONST CHAR16 *s2,
IN UINTN len);
+VOID StrCpy(IN CHAR16 *Dest,
+ IN CONST CHAR16 *Src);
+VOID StrnCpy(IN CHAR16 *Dest,
+ IN CONST CHAR16 *Src,
+ IN UINTN Len);
CHAR16 *StrDuplicate(IN CONST CHAR16 *Src);
UINTN StrLen(IN CONST CHAR16 *s1);
UINTN StrSize(IN CONST CHAR16 *s1);
@@ -298,6 +304,108 @@ efi_strerror(EFI_STATUS status)
return out;
}
+static inline char *
+Str2str(CHAR16 *in)
+{
+ static char buf0[1024];
+ static char buf1[1024];
+ char *out;
+ static int n;
+
+ out = n++ % 2 ? buf0 : buf1;
+ if (n > 1)
+ n -= 2;
+ SetMem(out, 1024, 0);
+ for (UINTN i = 0; i < 1023 && in[i]; i++)
+ out[i] = in[i];
+ return out;
+}
+
+static inline char *
+format_var_attrs(UINT32 attr)
+{
+ static char buf0[1024];
+ static char buf1[1024];
+ static int n;
+ int pos = 0;
+ bool found = false;
+ char *buf, *bufp;
+
+ buf = n++ % 2 ? buf0 : buf1;
+ if (n > 1)
+ n -= 2;
+ SetMem(buf, sizeof(buf0), 0);
+ bufp = &buf[0];
+ for (UINT32 i = 0; i < 8; i++) {
+ switch((1ul << i) & attr) {
+ case EFI_VARIABLE_NON_VOLATILE:
+ if (found)
+ bufp = stpcpy(bufp, "|");
+ bufp = stpcpy(bufp, "NV");
+ attr &= ~EFI_VARIABLE_NON_VOLATILE;
+ found = true;
+ break;
+ case EFI_VARIABLE_BOOTSERVICE_ACCESS:
+ if (found)
+ bufp = stpcpy(bufp, "|");
+ bufp = stpcpy(bufp, "BS");
+ attr &= ~EFI_VARIABLE_BOOTSERVICE_ACCESS;
+ found = true;
+ break;
+ case EFI_VARIABLE_RUNTIME_ACCESS:
+ if (found)
+ bufp = stpcpy(bufp, "|");
+ bufp = stpcpy(bufp, "RT");
+ attr &= ~EFI_VARIABLE_RUNTIME_ACCESS;
+ found = true;
+ break;
+ case EFI_VARIABLE_HARDWARE_ERROR_RECORD:
+ if (found)
+ bufp = stpcpy(bufp, "|");
+ bufp = stpcpy(bufp, "HER");
+ attr &= ~EFI_VARIABLE_HARDWARE_ERROR_RECORD;
+ found = true;
+ break;
+ case EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS:
+ if (found)
+ bufp = stpcpy(bufp, "|");
+ bufp = stpcpy(bufp, "AUTH");
+ attr &= ~EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS;
+ found = true;
+ break;
+ case EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS:
+ if (found)
+ bufp = stpcpy(bufp, "|");
+ bufp = stpcpy(bufp, "TIMEAUTH");
+ attr &= ~EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
+ found = true;
+ break;
+ case EFI_VARIABLE_APPEND_WRITE:
+ if (found)
+ bufp = stpcpy(bufp, "|");
+ bufp = stpcpy(bufp, "APPEND");
+ attr &= ~EFI_VARIABLE_APPEND_WRITE;
+ found = true;
+ break;
+ case EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS:
+ if (found)
+ bufp = stpcpy(bufp, "|");
+ bufp = stpcpy(bufp, "ENHANCED_AUTH");
+ attr &= ~EFI_VARIABLE_ENHANCED_AUTHENTICATED_ACCESS;
+ found = true;
+ break;
+ default:
+ break;
+ }
+ }
+ if (attr) {
+ if (found)
+ bufp = stpcpy(bufp, "|");
+ snprintf(bufp, bufp - buf - 1, "0x%x", attr);
+ }
+ return &buf[0];
+}
+
extern int debug;
#ifdef dprint
#undef dprint