-
Notifications
You must be signed in to change notification settings - Fork 2
/
nvram.c
628 lines (524 loc) · 12.6 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
#include <assert.h>
#include "nvram.h"
size_t nvram_erase_size = 0;
/* -- Helper functions -- */
/**
* \brief Caculate the hash value of specified string
* \return The hash value
* \param[in] s The specified string.
*/
uint32_t hash(const char *s)
{
assert(s!=NULL);
uint32_t hash = 0;
while (*s)
hash = 31 * hash + *s++;
return hash;
}
/**
* \brief Free all tuples of NVRAM handler.
* \param[in] h The specified NVRAM handler.
*/
void _nvram_free(nvram_handle_t *h)
{
assert(h != NULL);
uint32_t i;
nvram_tuple_t *t, *next;
/* Free hash table */
for (i = 0; i < NVRAM_ARRAYSIZE(h->nvram_hash); i++) {
for (t = h->nvram_hash[i]; t; t = next) {
next = t->next;
free(t->value);
free(t);
}
h->nvram_hash[i] = NULL;
}
/* Free dead table */
for (t = h->nvram_dead; t; t = next) {
next = t->next;
free(t->value);
free(t);
}
h->nvram_dead = NULL;
}
/**
*\brief (Re)allocate NVRAM tuples.
*\return The (Re)allocated tuple.
*\param[in] h The NVRAM handler//FIXME USELESS
*\param[in] t The tuples need to be (re)allocated
*\param[in] name The current name
*\param[in] value The current value
*\remark If t is NULL, we allocate t with t's curr name. \
If t is not NULL, we compare the curr value with its original, reallocate and copy curr value if diff exists.
*/
nvram_tuple_t * _nvram_realloc( nvram_handle_t *h, nvram_tuple_t *t,
const char *name, const char *value )
{
assert(h != NULL);
//FIXME
if ((strlen(value) + 1) > NVRAM_SPACE)
return NULL;
if (!t) {
if (!(t = malloc(sizeof(nvram_tuple_t) + strlen(name) + 1)))
return NULL;
/* Copy name */
t->name = (char *) &t[1];
strcpy(t->name, name);
t->value = NULL;
}
/* Copy value */
if (!t->value || strcmp(t->value, value))
{
if(!(t->value = (char *) realloc(t->value, strlen(value)+1)))
return NULL;
strcpy(t->value, value);
t->value[strlen(value)] = '\0';
}
return t;
}
/**
* \brief (Re)initialize the hash table.
* \return Return 0 on success
* \param[in] h The NVRAM handler*/
int _nvram_rehash(nvram_handle_t *h)
{
assert(h != NULL);
nvram_header_t *header = _nvram_header(h);
char *name, *value, *eq;
/* (Re)initialize hash table */
_nvram_free(h);
/* Parse and set "name=value\0 ... \0\0" */
name = (char *) &header[1];
for (; *name; name = value + strlen(value) + 1) {
if (!(eq = strchr(name, '=')))
break;
*eq = '\0';
value = eq + 1;
_nvram_set(h, name, value);
*eq = '=';
}
return 0;
}
/**
*\brief Copy NVRAM flash block data to staging file.
*\return Return zero on success.
*/
int nvram_to_staging(void)
{
int fdmtd, fdstg, stat;
char *mtd = nvram_find_mtd();
char buf[nvram_erase_size];
stat = -1;
if( (mtd != NULL) && (nvram_erase_size > 0) )
{
if( (fdmtd = open(mtd, O_RDONLY)) > -1 )
{
if( read(fdmtd, buf, sizeof(buf)) == sizeof(buf) )
{
if((fdstg = open(NVRAM_STAGING, O_WRONLY | O_CREAT, 0600)) > -1)
{
write(fdstg, buf, sizeof(buf));
fsync(fdstg);
close(fdstg);
stat = 0;
}
}
close(fdmtd);
}
}
free(mtd);
return stat;
}
/**
*\brief Copy staging file to NVRAM flash bock.
*\return Return zero on success.
*/
int staging_to_nvram(void)
{
int fdmtd, fdstg, stat;
char *mtd = nvram_find_mtd();
char buf[nvram_erase_size];
stat = -1;
if( (mtd != NULL) && (nvram_erase_size > 0) )
{
if( (fdstg = open(NVRAM_STAGING, O_RDONLY)) > -1 )
{
if( read(fdstg, buf, sizeof(buf)) == sizeof(buf) )
{
if( (fdmtd = open(mtd, O_WRONLY | O_SYNC)) > -1 )
{
write(fdmtd, buf, sizeof(buf));
fsync(fdmtd);
close(fdmtd);
stat = 0;
}
}
close(fdstg);
if( !stat )
stat = unlink(NVRAM_STAGING) ? 1 : 0;
}
}
free(mtd);
return stat;
}
/* -- inner functions -- */
/**
*\brief Get NVRAM header from its handler.
*\return The request NVRAM header
*\param[in] h The specified NVRAM handler
*/
nvram_header_t * _nvram_header(nvram_handle_t *h)
{
return (nvram_header_t *) &h->mmap[h->offset];
}
/**
*\brief Determine NVRAM device node.
*\return the path-to-file of flash block
*/
char * nvram_find_mtd(void)
{
FILE *fp;
int i, esz;
char dev[PATH_MAX];
char *path = NULL;
struct stat s;
int supported = 1;
if( supported && (fp = fopen("/proc/mtd", "r")) )
{
while( fgets(dev, sizeof(dev), fp) )
{
if( strstr(dev, NVRAM_MTD_NAME) && sscanf(dev, "mtd%d: %08x", &i, &esz) )
{
nvram_erase_size = esz;
sprintf(dev, "/dev/mtdblock/%d", i);
if( stat(dev, &s) > -1 && (s.st_mode & S_IFBLK) )
{
if( (path = (char *) malloc(strlen(dev)+1)) != NULL )
{
strncpy(path, dev, strlen(dev)+1);
break;
}
}
else
{
sprintf(dev, "/dev/mtdblock%d", i);
if( stat(dev, &s) > -1 && (s.st_mode & S_IFBLK) )
{
if( (path = (char *) malloc(strlen(dev)+1)) != NULL )
{
strncpy(path, dev, strlen(dev)+1);
break;
}
}
}
}
}
fclose(fp);
}
return path;
}
/**
*\brief Check NVRAM staging file.
*\return Return the path-to-file of staging file
or NULL if staging file does not exist
*/
char * nvram_find_staging(void)
{
struct stat s;
if( (stat(NVRAM_STAGING, &s) > -1) && (s.st_mode & S_IFREG) )
{
return NVRAM_STAGING;
}
return NULL;
}
/**
*\brief Open NVRAM and obtain a handle.
*\return The NVRAM handler
*\param[in] file File to be opened. Either staging file or flash block device name.
*\param[in] access Either NVRAM_RO or NVRAM_RW
*/
nvram_handle_t * _nvram_open(const char *file, int access)
{
int i;
int fd;
char *mtd = NULL;
nvram_handle_t *h;
nvram_header_t *header;
int offset = -1;
/* If erase size or file are undefined then try to define them */
if( (nvram_erase_size == 0) || (file == NULL) )
{
/* Finding the mtd will set the appropriate erase size */
if( (mtd = nvram_find_mtd()) == NULL || nvram_erase_size == 0 )
{
free(mtd);
return NULL;
}
}
if( (fd = open(file ? file : mtd, O_RDWR)) > -1 )
{
char *mmap_area = (char *) mmap(
NULL, nvram_erase_size, PROT_READ | PROT_WRITE,
(( access == NVRAM_RO ) ? MAP_PRIVATE : MAP_SHARED) | MAP_LOCKED, fd, 0);
if( mmap_area != MAP_FAILED )
{
for( i = 0; i <= ((nvram_erase_size - NVRAM_SPACE) / sizeof(uint32_t)); i++ )
{
if( ((uint32_t *)mmap_area)[i] == NVRAM_MAGIC )
{
offset = i * sizeof(uint32_t);
break;
}
}
if( offset < 0 )
{
free(mtd);
return NULL;
}
else if( (h = malloc(sizeof(nvram_handle_t))) != NULL )
{
memset(h, 0, sizeof(nvram_handle_t));
h->fd = fd;
h->mmap = mmap_area;
h->length = nvram_erase_size;
h->offset = offset;
h->access = access;
header = _nvram_header(h);
if( header->magic == NVRAM_MAGIC )
{
_nvram_rehash(h);
free(mtd);
return h;
}
else
{
munmap(h->mmap, h->length);
free(mtd);
free(h);
}
}
}
}
free(mtd);
return NULL;
}
/**
*\brief Invoke NVRAM handle for read.
*\return The NVRAM handler
*/
nvram_handle_t * _nvram_open_rdonly(void)
{
const char *file = nvram_find_staging();
if( file == NULL )
file = nvram_find_mtd();
if( file != NULL )
return _nvram_open(file, NVRAM_RO);
return NULL;
}
/**
*\brief Invoke NVRAM handle for read & write.
*\return The NVRAM handler
**/
nvram_handle_t * _nvram_open_staging(void)
{
if( nvram_find_staging() != NULL || nvram_to_staging() == 0 )
return _nvram_open(NVRAM_STAGING, NVRAM_RW);
return NULL;
}
/**
*\brief Close NVRAM and free memory.
*\return Always return 0
**/
int _nvram_close(nvram_handle_t *h)
{
if (NULL == h) {
fprintf(stderr,
"Could not open nvram! Possible reasons are:\n"
" - No \'nvram\' block found in /proc/mtd\n"
" - Unable to open mtd device\n"
" - Insufficient memory to complete operation\n"
" - Memory mapping failed or not supported\n"
" - Mtd block not initialized. Run \'nvram init\' first.\n"
);
return -1;
}
_nvram_free(h);
munmap(h->mmap, h->length);
close(h->fd);
free(h);
return 0;
}
/**
*\brief Get the value of an NVRAM variable.
*\return Return the value of the name, or NULL if name does not exist
*\param[in] h NVRAM handler
*\param[in] name The specified name
*\deprecated Invoked in inner functions only.
**/
char * _nvram_get(nvram_handle_t *h, const char *name)
{
assert(h != NULL);
uint32_t i;
nvram_tuple_t *t;
char *value;
if (!name)
return NULL;
/* Hash the name */
i = hash(name) % NVRAM_ARRAYSIZE(h->nvram_hash);
/* Find the associated tuple in the hash table */
for (t = h->nvram_hash[i]; t && strcmp(t->name, name); t = t->next)
{
}
value = t ? t->value : NULL;
return value;
}
/**
*\brief Get all NVRAM variables.
*\return The iterator of all NVRAM settings.
*\param[in] h The NVRAM handler
*\deprecated Invoked in inner functions only.
**/
nvram_tuple_t * _nvram_getall(nvram_handle_t *h)
{
assert(h != NULL);
int i;
nvram_tuple_t *t, *l, *x;
l = NULL;
for (i = 0; i < NVRAM_ARRAYSIZE(h->nvram_hash); i++) {
for (t = h->nvram_hash[i]; t; t = t->next) {
if( (x = (nvram_tuple_t *) malloc(sizeof(nvram_tuple_t))) != NULL )
{
x->name = t->name;
x->value = t->value;
x->next = l;
l = x;
}
else
{
break;
}
}
}
return l;
}
/**
* \brief Set the value of an NVRAM variable.
* \return Return 0 on success, errno on fail
* \param[in] h The NVRAM handler
* \param[in] name The specified name
* \param[in] value The specified value
* \deprecated Invoked in inner functions only.
**/
int _nvram_set(nvram_handle_t *h, const char *name, const char *value)
{
assert(h != NULL);
uint32_t i;
nvram_tuple_t *t, *u, **prev;
/* Hash the name */
i = hash(name) % NVRAM_ARRAYSIZE(h->nvram_hash);
/* Find the associated tuple in the hash table */
for (prev = &h->nvram_hash[i], t = *prev;
t && strcmp(t->name, name);
prev = &t->next, t = *prev);
/* (Re)allocate tuple */
if (!(u = _nvram_realloc(h, t, name, value)))
return -12; /* -ENOMEM */
/* Value reallocated */
if (t && t == u)
return 0;
/* Move old tuple to the dead table */
if (t) {
*prev = t->next;
t->next = h->nvram_dead;
h->nvram_dead = t;
}
/* Add new tuple to the hash table */
u->next = h->nvram_hash[i];
h->nvram_hash[i] = u;
return 0;
}
/**
*\brief Unset the value of an NVRAM variable.
*\return Return 0 on success //FIXME
*\param[in] h The NVRAM handler
*\param[in] name The specifed name
*\deprecated Invoked in inner functions only.
*/
int _nvram_unset(nvram_handle_t *h, const char *name)
{
assert(h != NULL);
uint32_t i;
nvram_tuple_t *t, **prev;
//FIXME
if (!name)
return EACCES;
/* Hash the name */
i = hash(name) % NVRAM_ARRAYSIZE(h->nvram_hash);
/* Find the associated tuple in the hash table */
for (prev = &h->nvram_hash[i], t = *prev;
t && strcmp(t->name, name); prev = &t->next, t = *prev);
/* Move it to the dead table */
if (t) {
*prev = t->next;
t->next = h->nvram_dead;
h->nvram_dead = t;
}
return 0;
}
/**
* \brief Regenerate NVRAM.
* \return Return 0 on success
* \param[in] h The NVRAM handler
* \deprecated Invoked in inner functions only.
**/
int _nvram_commit(nvram_handle_t *h)
{
assert(h != NULL);
nvram_header_t *header = _nvram_header(h);
char *ptr, *end;
int i;
nvram_tuple_t *t;
nvram_header_t tmp;
uint8_t crc;
/* Regenerate header */
header->magic = NVRAM_MAGIC;
header->crc_ver_init = (NVRAM_VERSION << 8);
/* Clear data area */
ptr = (char *) header + sizeof(nvram_header_t);
memset(ptr, 0xFF, NVRAM_SPACE - sizeof(nvram_header_t));
memset(&tmp, 0, sizeof(nvram_header_t));
/* Leave space for a double NUL at the end */
end = (char *) header + NVRAM_SPACE - 2;
/* Write out all tuples */
for (i = 0; i < NVRAM_ARRAYSIZE(h->nvram_hash); i++) {
for (t = h->nvram_hash[i]; t; t = t->next) {
if ((ptr + strlen(t->name) + 1 + strlen(t->value) + 1) > end)
break;
ptr += sprintf(ptr, "%s=%s", t->name, t->value) + 1;
}
}
/* End with a double NULL and pad to 4 bytes */
*ptr = '\0';
ptr++;
if( (int)ptr % 4 )
memset(ptr, 0, 4 - ((int)ptr % 4));
ptr++;
/* Set new length */
header->len = NVRAM_ROUNDUP(ptr - (char *) header, 4);
/* Little-endian CRC8 over the last 11 bytes of the header */
tmp.crc_ver_init = header->crc_ver_init;
tmp.config_refresh = header->config_refresh;
tmp.config_ncdl = header->config_ncdl;
crc = hndcrc8((unsigned char *) &tmp + NVRAM_CRC_START_POSITION,
sizeof(nvram_header_t) - NVRAM_CRC_START_POSITION, 0xff);
/* Continue CRC8 over data bytes */
crc = hndcrc8((unsigned char *) &header[0] + sizeof(nvram_header_t),
header->len - sizeof(nvram_header_t), crc);
/* Set new CRC8 */
header->crc_ver_init |= crc;
/* Write out */
msync(h->mmap, h->length, MS_SYNC);
fsync(h->fd);
/* Reinitialize hash table */
return _nvram_rehash(h);
}