-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnvram_interface_efi.c
187 lines (155 loc) · 3.01 KB
/
nvram_interface_efi.c
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
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
#include <ext2fs/ext2_fs.h>
#include <e2p/e2p.h>
#include "nvram_interface.h"
struct efi_header {
uint32_t attr;
};
static const struct efi_header EFI_HEADER = {0x7};
struct nvram_priv {
char *path;
};
static int efi_init(struct nvram_priv** priv, const char* section)
{
struct nvram_priv *pbuf = malloc(sizeof(struct nvram_priv));
if (!pbuf) {
return -ENOMEM;
}
pbuf->path = (char*) section;
*priv = pbuf;
return 0;
}
static void efi_destroy(struct nvram_priv** priv)
{
if (*priv) {
free(*priv);
*priv = NULL;
}
}
static int efi_size(const struct nvram_priv* priv, size_t* size)
{
struct stat sb;
if (stat(priv->path, &sb)) {
switch (errno) {
case ENOENT:
*size = 0;
return 0;
default:
return -errno;
}
}
if (sb.st_size < (off_t) sizeof(EFI_HEADER)) {
return -EBADF;
}
*size = sb.st_size - sizeof(EFI_HEADER);
return 0;
}
static int efi_read(struct nvram_priv* priv, uint8_t* buf, size_t size)
{
if (!buf) {
return -EINVAL;
}
int r = 0;
int fd = open(priv->path, O_RDONLY);
if (fd < 0) {
return -errno;
}
uint8_t* pbuf = (uint8_t*) malloc(size + sizeof(EFI_HEADER));
if (!pbuf) {
return -ENOMEM;
}
ssize_t bytes = read(fd, pbuf, size + sizeof(EFI_HEADER));
if (bytes < 0) {
r = -errno;
goto exit;
}
else
if ((size_t) bytes != size + sizeof(EFI_HEADER)) {
r = -EIO;
goto exit;
}
memcpy(buf, pbuf + sizeof(EFI_HEADER), size);
r = 0;
exit:
free(pbuf);
close(fd);
return r;
}
static int set_immutable(const char* path, bool value)
{
unsigned long flags = 0LU;
if (fgetflags(path, &flags) != 0) {
return -errno;
}
if (value) {
flags |= EXT2_IMMUTABLE_FL;
}
else {
flags &= ~EXT2_IMMUTABLE_FL;
}
if (fsetflags(path, flags) != 0) {
return -errno;
}
return 0;
}
static int efi_write(struct nvram_priv* priv, const uint8_t* buf, size_t size)
{
if (!buf) {
return -EINVAL;
}
uint8_t* pbuf = NULL;
int r = 0;
r = set_immutable(priv->path, false);
if (r != 0 && r != -ENOENT) {
return r;
}
int fd = open(priv->path, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
r = -errno;
goto exit;
}
pbuf = (uint8_t*) malloc(size + sizeof(EFI_HEADER));
if (!pbuf) {
r = -ENOMEM;
goto exit;
}
memcpy(pbuf, &EFI_HEADER, sizeof(EFI_HEADER));
memcpy(pbuf + sizeof(EFI_HEADER), buf, size);
ssize_t bytes = write(fd, pbuf, size + sizeof(EFI_HEADER));
if (bytes < 0) {
r = -errno;
goto exit;
}
else
if ((size_t) bytes != size + sizeof(EFI_HEADER)) {
r = -EIO;
goto exit;
}
r = 0;
exit:
set_immutable(priv->path, true);
free(pbuf);
close(fd);
return r;
}
static const char* efi_section(const struct nvram_priv* priv)
{
return priv->path;
}
/* Exposed by nvram_interface.c */
struct nvram_interface nvram_efi_interface =
{
.init = efi_init,
.destroy = efi_destroy,
.size = efi_size,
.read = efi_read,
.write = efi_write,
.section = efi_section,
};