-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnvram_format_v2.c
245 lines (215 loc) · 6.61 KB
/
nvram_format_v2.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/types.h>
#include "log.h"
#include "nvram_format.h"
#include "nvram_interface.h"
#include "libnvram/libnvram.h"
struct nvram {
struct nvram_interface* interface;
struct libnvram_transaction trans;
struct nvram_priv* priv_a;
struct nvram_priv* priv_b;
};
static void v2_close(struct nvram** nvram)
{
if (nvram && *nvram) {
struct nvram *pnvram = *nvram;
if (pnvram->priv_a)
pnvram->interface->destroy(&pnvram->priv_a);
if (pnvram->priv_b)
pnvram->interface->destroy(&pnvram->priv_b);
free(*nvram);
*nvram = NULL;
}
}
static const char* nvram_active_str(enum libnvram_active active)
{
switch (active) {
case LIBNVRAM_ACTIVE_A:
return "A";
case LIBNVRAM_ACTIVE_B:
return "B";
default:
return "NONE";
}
}
// return 1 for valid header, 0 for invalid, negative for error
static int read_header(struct nvram_interface* interface, struct nvram_priv* priv, struct libnvram_header* header)
{
const uint32_t size = libnvram_header_len();
uint8_t* buf = malloc(size);
if (buf == NULL)
return -ENOMEM;
int r = interface->read(priv, buf, size);
if (r == 0)
r = libnvram_validate_header(buf, size, header) == 0;
free(buf);
return r;
}
static int read_section(struct nvram_interface* interface, struct nvram_priv* priv, uint8_t** data, size_t* len)
{
size_t total_size = 0;
size_t data_size = 0;
uint8_t *buf = NULL;
int r = interface->size(priv, &total_size);
if (r) {
pr_err("%s: failed checking size [%d]: %s\n", interface->section(priv), -r, strerror(-r));
goto error_exit;
}
if (total_size >= libnvram_header_len()) {
struct libnvram_header hdr;
memset(&hdr, 0, sizeof(hdr));
r = read_header(interface, priv, &hdr);
switch (r) {
case 1: /* valid */
data_size = libnvram_header_len() + hdr.len;
buf = malloc(data_size);
if (buf == NULL) {
r = -ENOMEM;
pr_err("%s: failed allocating %zu byte read buffer\n", interface->section(priv), data_size);
goto error_exit;
}
r = interface->read(priv, buf, data_size);
if (r != 0) {
pr_err("%s: failed reading %zu bytes [%d]: %s\n", interface->section(priv), data_size, -r, strerror(-r));
goto error_exit;
}
case 0: /* invalid */
break;
default: /* error */
pr_err("%s: failed reading and validating header: [%d]: %s\n", interface->section(priv), -r, strerror(-r));
goto error_exit;
}
}
*data = buf;
*len = data_size;
return 0;
error_exit:
if (buf) {
free(buf);
}
return r;
}
static int init_and_read(struct nvram_interface* interface, struct nvram_priv** priv, const char* section, enum libnvram_active name, uint8_t** buf, size_t* size)
{
pr_dbg("%s: initializing: %s\n", nvram_active_str(name), section);
int r = interface->init(priv, section);
if (r) {
pr_err("%s: failed init [%d]: %s\n", section, -r, strerror(-r));
return r;
}
r = read_section(interface, *priv, buf, size);
if (r) {
return r;
}
pr_dbg("%s: size: %zu b\n", nvram_active_str(name), *size);
return 0;
}
static int v2_init(struct nvram** nvram, struct nvram_interface* interface, struct libnvram_list** list, const char* section_a, const char* section_b)
{
uint8_t *buf_a = NULL;
size_t size_a = 0;
uint8_t *buf_b = NULL;
size_t size_b = 0;
struct nvram *pnvram = (struct nvram*) malloc(sizeof(struct nvram));
if (!pnvram)
return -ENOMEM;
memset(pnvram, 0, sizeof(struct nvram));
pnvram->interface = interface;
int r = 0;
if (section_a && strlen(section_a) > 0) {
r = init_and_read(pnvram->interface, &pnvram->priv_a, section_a, LIBNVRAM_ACTIVE_A, &buf_a, &size_a);
if (r)
goto exit;
}
if (section_b && strlen(section_b) > 0) {
r = init_and_read(pnvram->interface, &pnvram->priv_b, section_b, LIBNVRAM_ACTIVE_B, &buf_b, &size_b);
if (r)
goto exit;
}
libnvram_init_transaction(&pnvram->trans, buf_a, size_a, buf_b, size_b);
pr_dbg("A: %s\n", pnvram->trans.section_a.state == LIBNVRAM_STATE_ALL_VERIFIED ? "valid" : "invalid");
pr_dbg("B: %s\n", pnvram->trans.section_b.state == LIBNVRAM_STATE_ALL_VERIFIED ? "valid" : "invalid");
pr_dbg("%s: active\n", nvram_active_str(pnvram->trans.active));
r = 0;
if ((pnvram->trans.active & LIBNVRAM_ACTIVE_A) == LIBNVRAM_ACTIVE_A)
r = libnvram_deserialize(list, buf_a + libnvram_header_len(), size_a - libnvram_header_len(), &pnvram->trans.section_a.hdr);
else if ((pnvram->trans.active & LIBNVRAM_ACTIVE_B) == LIBNVRAM_ACTIVE_B)
r = libnvram_deserialize(list, buf_b + libnvram_header_len(), size_b - libnvram_header_len(), &pnvram->trans.section_b.hdr);
if (r) {
pr_err("failed deserializing data [%d]: %s\n", -r, strerror(-r));
goto exit;
}
*nvram = pnvram;
exit:
if (r)
v2_close(&pnvram);
if (buf_a)
free(buf_a);
if (buf_b)
free(buf_b);
return r;
}
static int write_buf(struct nvram_interface* interface, struct nvram_priv* priv, const uint8_t* buf, uint32_t size)
{
pr_dbg("%s: write: %" PRIu32 " b\n", interface->section(priv), size);
int r = interface->write(priv, buf, size);
if (r)
pr_err("%s: failed writing %" PRIu32 " b [%d]: %s\n", interface->section(priv), size, -r, strerror(-r));
return r;
}
static int v2_commit(struct nvram* nvram, const struct libnvram_list* list)
{
uint8_t *buf = NULL;
int r = 0;
uint32_t size = libnvram_serialize_size(list, LIBNVRAM_TYPE_LIST);
buf = (uint8_t*) malloc(size);
if (!buf) {
pr_err("failed allocating %" PRIu32 " byte write buffer\n", size);
r = -ENOMEM;
goto exit;
}
struct libnvram_header hdr;
hdr.type = LIBNVRAM_TYPE_LIST;
enum libnvram_operation op = libnvram_next_transaction(&nvram->trans, &hdr);
uint32_t bytes = libnvram_serialize(list, buf, size, &hdr);
if (!bytes) {
pr_err("failed serializing nvram data\n");
goto exit;
}
if (!nvram->priv_a || !nvram->priv_b) {
// Transactional write disabled
r = write_buf(nvram->interface, nvram->priv_a ? nvram->priv_a : nvram->priv_b, buf, size);
}
else {
const int is_write_a = (op & LIBNVRAM_OPERATION_WRITE_A) == LIBNVRAM_OPERATION_WRITE_A;
const int is_counter_reset = (op & LIBNVRAM_OPERATION_COUNTER_RESET) == LIBNVRAM_OPERATION_COUNTER_RESET;
// first write
r = write_buf(nvram->interface, is_write_a ? nvram->priv_a : nvram->priv_b, buf, size);
if (!r && is_counter_reset) {
// second write, if requested
r = write_buf(nvram->interface, is_write_a ? nvram->priv_b : nvram->priv_a, buf, size);
}
}
if (r)
goto exit;
libnvram_update_transaction(&nvram->trans, op, &hdr);
pr_dbg("%s: active\n", nvram_active_str(nvram->trans.active));
r = 0;
exit:
if (buf) {
free(buf);
}
return r;
}
/* Exposed by nvram_format.c */
struct nvram_format nvram_v2_format =
{
.init = v2_init,
.commit = v2_commit,
.close = v2_close,
};