summaryrefslogtreecommitdiffstats
path: root/test/lib
diff options
context:
space:
mode:
authorGe Song <ge.song@hxt-semitech.com>2018-08-02 17:29:36 +0000
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>2018-08-07 11:30:36 +1000
commita915889b7fbbc4be11f9a37ea7afb5b3f3d41173 (patch)
treed0d1e8d524e086ded019c19f6c5e54c967f51fe0 /test/lib
parentec07742d2a37f8f96705fc0f20d9b22b583dad57 (diff)
downloadtalos-petitboot-a915889b7fbbc4be11f9a37ea7afb5b3f3d41173.tar.gz
talos-petitboot-a915889b7fbbc4be11f9a37ea7afb5b3f3d41173.zip
lib/efi: Add new routines to access efi variables
Provide methods to load/store petitboot's configuration on efi-based platforms. A test case is also provided. Signed-off-by: Ge Song <ge.song@hxt-semitech.com> [Cleanup file comments, make efivarfs_path static.] Signed-off-by: Geoff Levand <geoff@infradead.org> Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
Diffstat (limited to 'test/lib')
-rw-r--r--test/lib/test-efivar.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/test/lib/test-efivar.c b/test/lib/test-efivar.c
new file mode 100644
index 0000000..8ceb8f5
--- /dev/null
+++ b/test/lib/test-efivar.c
@@ -0,0 +1,127 @@
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Copyright (C) 2018 Huaxintong Semiconductor Technology Co.,Ltd. All rights
+ * reserved.
+ * Author: Ge Song <ge.song@hxt-semitech.com>
+ */
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/statfs.h>
+#include <unistd.h>
+
+#include "efi/efivar.h"
+#include "talloc/talloc.h"
+
+#define DEF_ATTR (EFI_VARIABLE_NON_VOLATILE | \
+ EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
+
+static const char *test_efivar_guid = "c9c07add-256e-4452-b911-f8d0d35a1ac7";
+static const char *test_varname = "efivartest";
+static const char *test_data = "petitboot";
+
+static char* find_efitest_path(void)
+{
+ static char dir[PATH_MAX] = {0};
+ static bool run = false;
+ char *rest_path = "/efivarfs_data/";
+ char *pos = NULL;
+
+ if (run)
+ return dir;
+
+ readlink("/proc/self/exe", dir, PATH_MAX);
+
+ pos = strrchr(dir, '/');
+ *pos = '\0';
+
+ strcat(dir, rest_path);
+ run = true;
+
+ return dir;
+}
+
+static bool probe(void)
+{
+ char *path;
+ int rc;
+
+ path = find_efitest_path();
+
+ rc = access(path, F_OK);
+ if (rc) {
+ if (errno == ENOENT) {
+ rc = mkdir(path, 0755);
+ if(rc)
+ return false;
+ } else {
+ return false;
+ }
+ }
+
+ set_efivarfs_path(path);
+
+ return true;
+}
+
+int main(void)
+{
+ void *ctx = NULL;
+ int rc, errno_value;
+ size_t size;
+ uint8_t *data = NULL;
+ uint32_t attr = DEF_ATTR;
+ char *path = NULL;
+
+ if(!probe())
+ return ENOENT;
+
+ talloc_new(ctx);
+ size = strlen(test_data) + 1;
+ rc = efi_set_variable(ctx, test_efivar_guid, test_varname,
+ (uint8_t *)test_data, size, attr);
+
+ rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
+ &data, &size, &attr);
+
+ assert(data != NULL);
+ rc = strcmp((char *)data, test_data);
+ if (rc) {
+ talloc_free(ctx);
+ assert(0);
+ }
+
+ rc = efi_del_variable(ctx, test_efivar_guid, test_varname);
+
+ rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
+ &data, &size, &attr);
+
+ errno_value = errno;
+ talloc_free(ctx);
+
+ assert(errno_value == ENOENT);
+
+ path = find_efitest_path();
+ rmdir(path);
+
+ return EXIT_SUCCESS;
+}
OpenPOWER on IntegriCloud