summaryrefslogtreecommitdiffstats
path: root/lib/efi/efivar.c
blob: 1ac69908d1f62f87fb770f0cc547c606f8537cfc (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 *  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 <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

#include <linux/fs.h>
#include <sys/ioctl.h>

#include "efivar.h"
#include "log/log.h"
#include "talloc/talloc.h"

static const char *efivarfs_path;

inline void set_efivarfs_path(const char *path)
{
	efivarfs_path = path;
}

inline const char *get_efivarfs_path(void)
{

	return efivarfs_path;
}

int efi_del_variable(void *ctx, const char *guidstr,
		const char *name)
{
	int fd, flag, errno_value;
	int rc = -1;
	const char *dir;
	char *path;

	dir = get_efivarfs_path();
	if (!dir)
		return -1;

	path = talloc_asprintf(ctx, "%s%s-%s", dir, name, guidstr);
	if (!path)
		return -1;

	fd = open(path, O_RDONLY|O_NONBLOCK);
	if (fd == -1)
		goto err;

	rc = ioctl(fd, FS_IOC_GETFLAGS, &flag);
	if (rc == -1)
		goto err;

	flag &= ~FS_IMMUTABLE_FL;
	rc = ioctl(fd, FS_IOC_SETFLAGS, &flag);
	if (rc == -1)
		goto err;

	close(fd);
	rc = unlink(path);

err:
	errno_value = errno;
	if (fd > 0)
		close(fd);

	errno = errno_value;
	return rc;
}

int efi_get_variable(void *ctx, const char *guidstr, const char *name,
		uint8_t **data, size_t *data_size, uint32_t *attributes)
{
	int fd, errno_value;
	int rc = -1;
	void *p, *buf;
	size_t bufsize = 4096;
	size_t filesize = 0;
	ssize_t sz;
	const char *dir;
	char *path;

	dir = get_efivarfs_path();
	if (!dir)
		return EFAULT;

	path = talloc_asprintf(ctx, "%s%s-%s", dir, name, guidstr);
	if (!path)
		return ENOMEM;

	fd = open(path, O_RDONLY|O_NONBLOCK);
	if (fd < 0)
		goto err;

	buf = talloc_size(ctx, bufsize);
	if (!buf)
		goto err;

	do {
		p = buf + filesize;
		sz = read(fd, p, bufsize);
		if (sz < 0 && errno == EAGAIN) {
			continue;
		} else if (sz == 0) {
			break;
		}
		filesize += sz;
	} while (1);

	*attributes = *(uint32_t *)buf;
	*data = (uint8_t *)(buf + sizeof(uint32_t));
	*data_size = strlen(buf + sizeof(uint32_t));
	rc = 0;

err:
	errno_value = errno;
	if (fd > 0)
		close(fd);

	errno = errno_value;
	return rc;
}

int efi_set_variable(void *ctx, const char *guidstr, const char *name,
		uint8_t *data, size_t data_size, uint32_t attributes)
{
	int rc = -1, errno_value;
	int fd = -1;
	ssize_t len;
	const char *dir;
	char *path;
	void *buf;
	size_t bufsize;
	mode_t mask = 0644;

	dir = get_efivarfs_path();
	if (!dir)
		return EFAULT;

	path = talloc_asprintf(ctx, "%s%s-%s", dir, name, guidstr);
	if (!path)
		return ENOMEM;

	if (!access(path, F_OK)) {
		rc = efi_del_variable(ctx, guidstr, name);
		if (rc < 0) {
			goto err;
		}
	}

	fd = open(path, O_CREAT|O_WRONLY, mask);
	if (fd < 0)
		goto err;

	bufsize = sizeof(uint32_t) + data_size;
	buf = talloc_size(ctx, bufsize);
	if (!buf)
		goto err;

	*(uint32_t *)buf = attributes;
	memcpy(buf + sizeof(uint32_t), data, data_size);

	len = write(fd, buf, bufsize);
	if ((size_t)len != bufsize)
		goto err;
	else
		rc = 0;

err:
	errno_value = errno;
	if (fd > 0)
		close(fd);

	errno = errno_value;
	return rc;
}
OpenPOWER on IntegriCloud