summaryrefslogtreecommitdiffstats
path: root/lib/efi/efivar.c
blob: 21c5d344f582b212eec8af2bcd53d93a11040b80 (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
192
/*
 *  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 <sys/stat.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;
}

static int efi_open(const char *name, const char *guidstr, int flags,
	mode_t mode, char **path)
{
	int fd;

	*path = NULL;

	if (!get_efivarfs_path())
		return -1;

	*path = talloc_asprintf(NULL, "%s%s-%s", get_efivarfs_path(), name, guidstr);
	if (!*path)
		return -1;

	flags = flags ? flags : O_RDONLY | O_NONBLOCK;

	fd = open(*path, flags, mode);

	if (fd < 0) {
		pb_log("%s: open failed %s: %s\n", __func__, *path,
			strerror(errno));
		talloc_free(*path);
		*path = NULL;
		return -1;
	}

	return fd;
}

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

	fd = efi_open(name, guidstr, 0, 0, &path);
	if (fd < 0)
		return -1;

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

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

	close(fd);
	fd = 0;
	rc = unlink(path);
exit:
	talloc_free(path);
	close(fd);
	return rc;
}

int efi_get_variable(void *ctx, const char *guidstr, const char *name,
		struct efi_data **efi_data)
{
	int fd;
	int rc = -1;
	char *p;
	char buf[4096];
	ssize_t total;
	ssize_t count;
	char *path;

	*efi_data = NULL;

	fd = efi_open(name, guidstr, 0, 0, &path);
	if (fd < 0)
		return -1;

	for (p = buf, total = 0; ; p = buf + count) {
		count = read(fd, p, sizeof(buf) - total);
		if (count < 0) {
			if (errno == EAGAIN || errno == EWOULDBLOCK)
				continue;

			pb_log("%s: read failed %s: (%ld) %s\n", __func__, path,
				count, strerror(errno));
			goto exit;
		}
		if (p >= (buf + sizeof(buf))) {
			pb_log("%s: buffer full %s: (%ld)\n", __func__, path,
				sizeof(buf));
			goto exit;
		}
		if (count == 0)
			break;
		total += count;
	};

	*efi_data = (void*)talloc_zero_array(ctx, char,
		sizeof (struct efi_data) + total);

	(*efi_data)->attributes = *(uint32_t *)buf;
	(*efi_data)->data_size = total;
	(*efi_data)->data = (*efi_data)->fill;
	memcpy((*efi_data)->data, buf + sizeof (uint32_t), total);

	rc = 0;
exit:
	talloc_free(path);
	close(fd);
	return rc;
}

int efi_set_variable(const char *guidstr, const char *name,
		const struct efi_data *efi_data)
{
	int rc = -1;
	int fd;
	ssize_t count;
	void *buf;
	size_t bufsize;
	char *path;

	efi_del_variable(guidstr, name);

	fd = efi_open(name, guidstr, O_CREAT | O_WRONLY,
		S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH, &path);
	if (fd < 0)
		return -1;

	bufsize = sizeof(uint32_t) + efi_data->data_size;
	buf = talloc_size(path, bufsize);
	if (!buf)
		goto exit;

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

	count = write(fd, buf, bufsize);
	if ((size_t)count != bufsize) {
		pb_log("%s: write failed %s: (%ld) %s\n", __func__, name,
			count, strerror(errno));
		goto exit;
	}
	rc = 0;

exit:
	talloc_free(path);
	close(fd);
	return rc;
}
OpenPOWER on IntegriCloud