]> git.ozlabs.org Git - petitboot/blob - test/lib/test-efivar.c
lib/efi: Add new routines to access efi variables
[petitboot] / test / lib / test-efivar.c
1 /*
2  *  This program is free software; you can redistribute it and/or modify
3  *  it under the terms of the GNU General Public License as published by
4  *  the Free Software Foundation; version 2 of the License.
5  *
6  *  This program is distributed in the hope that it will be useful,
7  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9  *  GNU General Public License for more details.
10  *
11  *  You should have received a copy of the GNU General Public License
12  *  along with this program; if not, write to the Free Software
13  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
14  *
15  *  Copyright (C) 2018 Huaxintong Semiconductor Technology Co.,Ltd. All rights
16  *  reserved.
17  *  Author: Ge Song <ge.song@hxt-semitech.com>
18  */
19 #include <assert.h>
20 #include <dirent.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <linux/limits.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/stat.h>
29 #include <sys/statfs.h>
30 #include <unistd.h>
31
32 #include "efi/efivar.h"
33 #include "talloc/talloc.h"
34
35 #define DEF_ATTR        (EFI_VARIABLE_NON_VOLATILE | \
36         EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS)
37
38 static const char *test_efivar_guid = "c9c07add-256e-4452-b911-f8d0d35a1ac7";
39 static const char *test_varname = "efivartest";
40 static const char *test_data = "petitboot";
41
42 static char* find_efitest_path(void)
43 {
44         static char dir[PATH_MAX] = {0};
45         static bool run = false;
46         char *rest_path = "/efivarfs_data/";
47         char *pos = NULL;
48
49         if (run)
50                 return dir;
51
52         readlink("/proc/self/exe", dir, PATH_MAX);
53
54         pos = strrchr(dir, '/');
55         *pos = '\0';
56
57         strcat(dir, rest_path);
58         run = true;
59
60         return dir;
61 }
62
63 static bool probe(void)
64 {
65         char *path;
66         int rc;
67
68         path = find_efitest_path();
69
70         rc = access(path, F_OK);
71         if (rc) {
72                 if (errno == ENOENT) {
73                         rc = mkdir(path, 0755);
74                         if(rc)
75                                 return false;
76                 } else {
77                         return false;
78                 }
79         }
80
81         set_efivarfs_path(path);
82
83         return true;
84 }
85
86 int main(void)
87 {
88         void *ctx = NULL;
89         int rc, errno_value;
90         size_t size;
91         uint8_t *data = NULL;
92         uint32_t attr = DEF_ATTR;
93         char *path = NULL;
94
95         if(!probe())
96                 return ENOENT;
97
98         talloc_new(ctx);
99         size = strlen(test_data) + 1;
100         rc = efi_set_variable(ctx, test_efivar_guid, test_varname,
101                                 (uint8_t *)test_data, size, attr);
102
103         rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
104                                 &data, &size, &attr);
105
106         assert(data != NULL);
107         rc = strcmp((char *)data, test_data);
108         if (rc) {
109                 talloc_free(ctx);
110                 assert(0);
111         }
112
113         rc = efi_del_variable(ctx, test_efivar_guid, test_varname);
114
115         rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
116                                 &data, &size, &attr);
117
118         errno_value = errno;
119         talloc_free(ctx);
120
121         assert(errno_value == ENOENT);
122
123         path = find_efitest_path();
124         rmdir(path);
125
126         return EXIT_SUCCESS;
127 }