summaryrefslogtreecommitdiffstats
path: root/test/lib/test-efivar.c
blob: a85b73ccb2c6db603418a423985be507a30313ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 *  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;
	uint32_t attr = DEF_ATTR;
	char *path = NULL;
	struct efi_data *efi_data;

	if(!probe())
		return ENOENT;

	talloc_new(ctx);

	efi_data = talloc_zero(ctx, struct efi_data);
	efi_data->attributes = attr;
	efi_data->data = talloc_strdup(efi_data, test_data);
	efi_data->data_size = strlen(test_data) + 1;

	rc = efi_set_variable(test_efivar_guid, test_varname,
				efi_data);

	talloc_free(efi_data);
	rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
				&efi_data);

	assert(efi_data->data != NULL);
	rc = strcmp((char *)efi_data->data, test_data);
	if (rc) {
		talloc_free(ctx);
		assert(0);
	}

	rc = efi_del_variable(test_efivar_guid, test_varname);

	rc = efi_get_variable(ctx, test_efivar_guid, test_varname,
				&efi_data);

	errno_value = errno;
	talloc_free(ctx);

	assert(errno_value == ENOENT);

	path = find_efitest_path();
	rmdir(path);

	return EXIT_SUCCESS;
}
OpenPOWER on IntegriCloud