forked from data-respons-solutions/nvram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvram_interface_efi.c
176 lines (145 loc) · 2.79 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
#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_device {
char *path;
};
int nvram_interface_init(struct nvram_device** dev, const char* section)
{
struct nvram_device *pbuf = malloc(sizeof(struct nvram_device));
if (!pbuf) {
return -ENOMEM;
}
pbuf->path = (char*) section;
*dev = pbuf;
return 0;
}
void nvram_interface_destroy(struct nvram_device** dev)
{
if (*dev) {
free(*dev);
*dev = NULL;
}
}
int nvram_interface_size(struct nvram_device* dev, size_t* size)
{
struct stat sb;
if (stat(dev->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;
}
int nvram_interface_read(struct nvram_device* dev, uint8_t* buf, size_t size)
{
if (!buf) {
return -EINVAL;
}
int r = 0;
int fd = open(dev->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;
}
int nvram_interface_write(struct nvram_device* dev, const uint8_t* buf, size_t size)
{
if (!buf) {
return -EINVAL;
}
uint8_t* pbuf = NULL;
int r = 0;
r = set_immutable(dev->path, false);
if (r) {
return r;
}
int fd = open(dev->path, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
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(dev->path, true);
free(pbuf);
close(fd);
return r;
}
const char* nvram_interface_section(const struct nvram_device* dev)
{
return dev->path;
}