-
Notifications
You must be signed in to change notification settings - Fork 139
/
nvram.c
373 lines (307 loc) · 8.3 KB
/
nvram.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* nvram.c
*
* Copyright 2010 iDroid Project
*
* This file is part of iDroid. An android distribution for Apple products.
* For more information, please visit http://www.idroidproject.org/.
*
* 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; either version 3
* of the License, or (at your option) any later version.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "openiboot.h"
#include "commands.h"
#include "nvram.h"
#include "util.h"
#include "mtd.h"
static uint8_t* bank1Data;
static uint8_t* bank2Data;
static NVRamAtom* bank1Atoms;
static NVRamAtom* bank2Atoms;
static uint32_t oldestBank;
static NVRamAtom* newestBank;
static uint8_t* newestBankData;
static EnvironmentVar* variables;
static mtd_t *nvram_dev = NULL;
static mtd_t *nvram_device()
{
if(!nvram_dev)
{
mtd_t *ptr = NULL;
while((ptr = mtd_find(ptr)))
{
if(ptr->usage == mtd_boot_images)
{
nvram_dev = ptr;
break;
}
}
}
return nvram_dev;
}
static uint8_t checkNVRamInfo(NVRamInfo* info) {
uint32_t c = info->ckByteSeed;
uint8_t* data = (uint8_t*) info;
int i;
for(i = 0x2; i < 0x10; i++) {
c = (c + data[i]) & 0xffff;
}
while(c > 0xff) {
c = (c >> 8) + (c & 0xff);
}
return c;
}
static void releaseAtoms(NVRamAtom* atoms) {
NVRamAtom* toRelease;
while(atoms != NULL) {
toRelease = atoms;
atoms = atoms->next;
free(toRelease);
}
}
static NVRamAtom* findAtom(NVRamAtom* atoms, const char* type) {
while(atoms != NULL) {
if(strcmp(atoms->info->type, type) == 0)
return atoms;
atoms = atoms->next;
}
return NULL;
}
static NVRamAtom* readAtoms(uint8_t* bankData) {
NVRamAtom* lastAtom = NULL;
NVRamAtom* firstAtom = NULL;
size_t position = 0;
while(position < NVRAM_SIZE) {
if(lastAtom == NULL) {
firstAtom = (NVRamAtom*) malloc(sizeof(NVRamAtom));
lastAtom = firstAtom;
} else {
lastAtom->next = (NVRamAtom*) malloc(sizeof(NVRamAtom));
lastAtom = lastAtom->next;
}
lastAtom->info = (NVRamInfo*) &bankData[position];
lastAtom->size = lastAtom->info->size << 4;
lastAtom->data = &bankData[position + sizeof(NVRamInfo)];
lastAtom->next = NULL;
position += lastAtom->size;
if(checkNVRamInfo(lastAtom->info) != lastAtom->info->ckByte) {
releaseAtoms(firstAtom);
return NULL;
}
}
NVRamAtom* ckDataAtom = findAtom(firstAtom, "nvram");
if(ckDataAtom == NULL) {
releaseAtoms(firstAtom);
return NULL;
}
NVRamData* ckData = (NVRamData*) ckDataAtom->data;
if(ckData->adler != adler32(bankData + 0x14, NVRAM_SIZE - 0x14)) {
releaseAtoms(firstAtom);
return NULL;
}
return firstAtom;
}
static void releaseEnvironment(EnvironmentVar* vars) {
EnvironmentVar* toRelease;
while(vars != NULL) {
toRelease = vars;
vars = vars->next;
free(toRelease);
}
}
void nvram_save()
{
mtd_t *dev = nvram_device();
if(!dev)
return;
mtd_prepare(dev);
NVRamAtom* commonAtom = findAtom(newestBank, "common");
EnvironmentVar* var = variables;
memset(commonAtom->data, 0, commonAtom->size - sizeof(NVRamInfo));
char* pos = (char*) commonAtom->data;
while(var != NULL) {
if(var->name[0] == '\0')
break;
sprintf(pos, "%s=%s", var->name, var->value);
pos += strlen(pos) + 1;
var = var->next;
}
NVRamAtom* ckDataAtom = findAtom(newestBank, "nvram");
NVRamData* ckData = (NVRamData*) ckDataAtom->data;
ckData->epoch++;
ckData->adler = adler32(newestBankData + 0x14, NVRAM_SIZE - 0x14);
NVRamAtom* atom = newestBank;
uint32_t offset = oldestBank;
while(atom != NULL) {
mtd_write(dev, atom->info, offset, sizeof(NVRamInfo));
offset += sizeof(NVRamInfo);
mtd_write(dev, atom->data, offset, atom->size - sizeof(NVRamInfo));
offset += atom->size - sizeof(NVRamInfo);
atom = atom->next;
}
releaseAtoms(bank1Atoms);
releaseAtoms(bank2Atoms);
releaseEnvironment(variables);
free(bank1Data);
free(bank2Data);
mtd_finish(dev);
nvram_setup();
}
const char* nvram_getvar(const char* name) {
EnvironmentVar* var = variables;
while(var != NULL) {
if(strcmp(var->name, name) == 0) {
return var->value;
}
var = var->next;
}
return NULL;
}
void nvram_setvar(const char* name, const char* value) {
EnvironmentVar* var = variables;
while(var != NULL) {
if(strcmp(var->name, name) == 0) {
free(var->value);
var->value = strdup(value);
return;
}
var = var->next;
}
var = (EnvironmentVar*) malloc(sizeof(EnvironmentVar));
var->next = variables;
var->name = strdup(name);
var->value = strdup(value);
variables = var;
}
void nvram_listvars() {
EnvironmentVar* var = variables;
while(var != NULL) {
bufferPrintf("%s = %s\r\n", var->name, var->value);
var = var->next;
}
}
static EnvironmentVar* loadEnvironment(NVRamAtom* atoms) {
EnvironmentVar* toRet = NULL;
EnvironmentVar* curVar = NULL;
NVRamAtom* commonAtom = findAtom(atoms, "common");
char* loc = (char*) commonAtom->data;
char* name = loc;
char* value = NULL;
size_t nameLen = 0;
while(TRUE) {
if(*loc == '\0') {
if(curVar == NULL) {
toRet = curVar = (EnvironmentVar*) malloc(sizeof(EnvironmentVar));
} else {
curVar->next = (EnvironmentVar*) malloc(sizeof(EnvironmentVar));
curVar = curVar->next;
}
curVar->next = NULL;
curVar->name = (char*) malloc(nameLen + 1);
memcpy(curVar->name, name, nameLen);
curVar->name[nameLen] = '\0';
if(value) {
size_t valueLen = strlen(value);
curVar->value = (char*) malloc(valueLen + 1);
memcpy(curVar->value, value, valueLen);
curVar->value[valueLen] = '\0';
} else {
curVar->value = (char*) malloc(1);
curVar->value[0] = '\0';
}
if(nameLen == 0) {
break;
}
name = loc + 1;
nameLen = 0;
value = NULL;
} else if(*loc == '=') {
value = loc + 1;
} else if(value == NULL) {
nameLen++;
}
loc++;
}
return toRet;
}
error_t nvram_setup()
{
mtd_t *dev = nvram_device();
if(!dev)
return ENOENT;
mtd_prepare(dev);
bank1Data = (uint8_t*) malloc(NVRAM_SIZE);
bank2Data = (uint8_t*) malloc(NVRAM_SIZE);
mtd_read(dev, bank1Data, NVRAM_START, NVRAM_SIZE);
mtd_read(dev, bank2Data, NVRAM_START + NVRAM_SIZE, NVRAM_SIZE);
mtd_finish(dev);
bank1Atoms = readAtoms(bank1Data);
bank2Atoms = readAtoms(bank2Data);
if(bank1Atoms) {
bufferPrintf("Successfully loaded bank1 nvram\r\n");
}
if(bank2Atoms) {
bufferPrintf("Successfully loaded bank2 nvram\r\n");
}
if(!bank1Atoms && !bank2Atoms) {
bufferPrintf("Could not load either bank1 nor bank2 nvram!\r\n");
return EIO;
}
NVRamAtom* ckDataAtom1 = findAtom(bank1Atoms, "nvram");
NVRamAtom* ckDataAtom2 = findAtom(bank2Atoms, "nvram");
if(ckDataAtom1 == NULL) {
oldestBank = NVRAM_START + NVRAM_SIZE;
newestBank = bank2Atoms;
newestBankData = bank2Data;
} else if(ckDataAtom2 == NULL) {
oldestBank = NVRAM_START;
newestBank = bank1Atoms;
newestBankData = bank1Data;
} else if(((NVRamData*)(ckDataAtom1->data))->epoch < ((NVRamData*)(ckDataAtom2->data))->epoch) {
oldestBank = NVRAM_START;
newestBank = bank2Atoms;
newestBankData = bank2Data;
} else {
oldestBank = NVRAM_START + NVRAM_SIZE;
newestBank = bank1Atoms;
newestBankData = bank1Data;
}
variables = loadEnvironment(newestBank);
return SUCCESS;
}
static error_t cmd_printenv(int argc, char** argv)
{
nvram_listvars();
return SUCCESS;
}
COMMAND("printenv", "list the environment variables in nvram", cmd_printenv);
static error_t cmd_setenv(int argc, char** argv)
{
if(argc < 3) {
bufferPrintf("Usage: %s <name> <value>\r\n", argv[0]);
return EINVAL;
}
nvram_setvar(argv[1], argv[2]);
bufferPrintf("Set %s = %s\r\n", argv[1], argv[2]);
return SUCCESS;
}
COMMAND("setenv", "sets an environment variable", cmd_setenv);
static error_t cmd_saveenv(int argc, char** argv)
{
bufferPrintf("Saving environment, this may take awhile...\r\n");
nvram_save();
bufferPrintf("Environment saved\r\n");
return SUCCESS;
}
COMMAND("saveenv", "saves the environment variables in nvram", cmd_saveenv);