-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathnvram.c
More file actions
706 lines (550 loc) · 16.5 KB
/
nvram.c
File metadata and controls
706 lines (550 loc) · 16.5 KB
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <mntent.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ipc.h>
#include <sys/mount.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "alias.h"
#include "nvram.h"
#include "config.h"
/* Generate variable declarations for external NVRAM data. */
#define NATIVE(a, b)
#define PATH(a)
#define TABLE(a) \
extern const char *a[] __attribute__((weak));
NVRAM_DEFAULTS_PATH
#undef TABLE
#undef PATH
#undef NATIVE
// https://lkml.org/lkml/2007/3/9/10
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + sizeof(typeof(int[1 - 2 * !!__builtin_types_compatible_p(typeof(arr), typeof(&arr[0]))])) * 0)
#define PRINT_MSG(fmt, ...) do { if (DEBUG) { fprintf(stderr, "%s: "fmt, __FUNCTION__, __VA_ARGS__); } } while (0)
/* Weak symbol definitions for library functions that may not be present */
__typeof__(ftok) __attribute__((weak)) ftok;
/* Global variables */
static int init = 0;
static char temp[BUFFER_SIZE];
static int sem_get() {
int key, semid = 0;
unsigned int timeout = 0;
struct semid_ds seminfo;
union semun {
int val;
struct semid_ds *buf;
unsigned short *array;
struct seminfo *__buf;
} semun;
struct sembuf sembuf = {
.sem_num = 0,
.sem_op = 1,
.sem_flg = 0,
};
// Generate key for semaphore based on the mount point
if (!ftok || (key = ftok(MOUNT_POINT, IPC_KEY)) == -1) {
PRINT_MSG("%s\n", "Unable to get semaphore key!");
return -1;
}
PRINT_MSG("Key: %x\n", key);
// Get the semaphore using the key
if ((semid = semget(key, 1, IPC_CREAT | IPC_EXCL | 0666)) >= 0) {
semun.val = 1;
// Unlock the semaphore and set the sem_otime field
if (semop(semid, &sembuf, 1) == -1) {
PRINT_MSG("%s\n", "Unable to initialize semaphore!");
// Clean up semaphore
semctl(semid, 0, IPC_RMID);
semid = -1;
}
} else if (errno == EEXIST) {
// Get the semaphore in non-exclusive mode
if ((semid = semget(key, 1, 0)) < 0) {
PRINT_MSG("%s\n", "Unable to get semaphore non-exclusively!");
return semid;
}
semun.buf = &seminfo;
// Wait for the semaphore to be initialized
while (timeout++ < IPC_TIMEOUT) {
semctl(semid, 0, IPC_STAT, semun);
if (semun.buf && semun.buf->sem_otime != 0) {
break;
}
if (!(timeout % 100)) {
PRINT_MSG("Waiting for semaphore initialization (Key: %x, Semaphore: %x)...\n", key, semid);
}
}
}
return (timeout < IPC_TIMEOUT) ? semid : -1;
}
static void sem_lock() {
int semid;
struct sembuf sembuf = {
.sem_num = 0,
.sem_op = -1,
.sem_flg = SEM_UNDO,
};
struct mntent entry, *ent;
FILE *mnt = NULL;
// If not initialized, check for existing mount before triggering NVRAM init
if (!init) {
if ((mnt = setmntent("/proc/mounts", "r"))) {
while ((ent = getmntent_r(mnt, &entry, temp, BUFFER_SIZE))) {
if (!strncmp(ent->mnt_dir, MOUNT_POINT, sizeof(MOUNT_POINT) - 2)) {
init = 1;
PRINT_MSG("%s\n", "Already initialized!");
endmntent(mnt);
goto cont;
}
}
endmntent(mnt);
}
PRINT_MSG("%s\n", "Triggering NVRAM initialization!");
nvram_init();
}
cont:
// Must get sempahore after NVRAM initialization, mounting will change ID
if ((semid = sem_get()) == -1) {
PRINT_MSG("%s\n", "Unable to get semaphore!");
return;
}
// PRINT_MSG("%s\n", "Locking semaphore...");
if (semop(semid, &sembuf, 1) == -1) {
PRINT_MSG("%s\n", "Unable to lock semaphore!");
}
return;
}
static void sem_unlock() {
int semid;
struct sembuf sembuf = {
.sem_num = 0,
.sem_op = 1,
.sem_flg = SEM_UNDO,
};
if ((semid = sem_get(NULL)) == -1) {
PRINT_MSG("%s\n", "Unable to get semaphore!");
return;
}
// PRINT_MSG("%s\n", "Unlocking semaphore...");
if (semop(semid, &sembuf, 1) == -1) {
PRINT_MSG("%s\n", "Unable to unlock semaphore!");
}
return;
}
int nvram_init(void) {
FILE *f;
PRINT_MSG("%s\n", "Initializing NVRAM...");
if (init) {
PRINT_MSG("%s\n", "Early termination!");
return E_SUCCESS;
}
init = 1;
sem_lock();
if (mount("tmpfs", MOUNT_POINT, "tmpfs", MS_NOEXEC | MS_NOSUID | MS_SYNCHRONOUS, "") == -1) {
sem_unlock();
PRINT_MSG("Unable to mount tmpfs on mount point %s!\n", MOUNT_POINT);
return E_FAILURE;
}
// Checked by certain Ralink routers
if ((f = fopen("/var/run/nvramd.pid", "w+")) == NULL) {
PRINT_MSG("Unable to touch Ralink PID file: %s!\n", "/var/run/nvramd.pid");
}
else {
fclose(f);
}
sem_unlock();
return nvram_set_default();
}
int nvram_reset(void) {
PRINT_MSG("%s\n", "Reseting NVRAM...");
if (nvram_clear() != E_SUCCESS) {
PRINT_MSG("%s\n", "Unable to clear NVRAM!");
return E_FAILURE;
}
return nvram_set_default();
}
int nvram_clear(void) {
char path[PATH_MAX] = MOUNT_POINT;
struct dirent *entry;
int ret = E_SUCCESS;
DIR *dir;
PRINT_MSG("%s\n", "Clearing NVRAM...");
sem_lock();
if (!(dir = opendir(MOUNT_POINT))) {
sem_unlock();
PRINT_MSG("Unable to open directory %s!\n", MOUNT_POINT);
return E_FAILURE;
}
while ((entry = readdir(dir))) {
if (!strncmp(entry->d_name, ".", 1) || !strcmp(entry->d_name, "..")) {
PRINT_MSG("Skipping %s\n", entry->d_name);
continue;
}
strncpy(path + strlen(MOUNT_POINT), entry->d_name, ARRAY_SIZE(path) - ARRAY_SIZE(MOUNT_POINT) - 1);
path[PATH_MAX - 1] = '\0';
PRINT_MSG("%s\n", path);
if (unlink(path) == -1 && errno != ENOENT) {
PRINT_MSG("Unable to unlink %s!\n", path);
ret = E_FAILURE;
}
}
closedir(dir);
sem_unlock();
return ret;
}
int nvram_close(void) {
PRINT_MSG("%s\n", "Closing NVRAM...");
return E_SUCCESS;
}
int nvram_list_add(const char *key, const char *val) {
char *pos;
PRINT_MSG("%s = %s + %s\n", val, temp, key);
if (nvram_get_buf(key, temp, BUFFER_SIZE) != E_SUCCESS) {
return nvram_set(key, val);
}
if (!key || !val) {
return E_FAILURE;
}
if (strlen(temp) + 1 + strlen(val) + 1 > BUFFER_SIZE) {
return E_FAILURE;
}
// This will overwrite the temp buffer, but it is OK
if (nvram_list_exist(key, val, LIST_MAGIC) != NULL) {
return E_SUCCESS;
}
// Replace terminating NULL of list with LIST_SEP
pos = temp + strlen(temp);
if (pos != temp) {
*pos++ = LIST_SEP[0];
}
if (strcpy(pos, val) != pos) {
return E_FAILURE;
}
return nvram_set(key, temp);
}
char *nvram_list_exist(const char *key, const char *val, int magic) {
char *pos = NULL;
if (nvram_get_buf(key, temp, BUFFER_SIZE) != E_SUCCESS) {
return E_FAILURE;
}
PRINT_MSG("%s ?in %s (%s)\n", val, key, temp);
if (!val) {
return (magic == LIST_MAGIC) ? NULL : (char *) E_FAILURE;
}
while ((pos = strtok(!pos ? temp : NULL, LIST_SEP))) {
if (!strcmp(pos + 1, val)) {
return (magic == LIST_MAGIC) ? pos + 1 : (char *) E_SUCCESS;
}
}
return (magic == LIST_MAGIC) ? NULL : (char *) E_FAILURE;
}
int nvram_list_del(const char *key, const char *val) {
char *pos;
if (nvram_get_buf(key, temp, BUFFER_SIZE) != E_SUCCESS) {
return E_SUCCESS;
}
PRINT_MSG("%s = %s - %s\n", key, temp, val);
if (!val) {
return E_FAILURE;
}
// This will overwrite the temp buffer, but it is OK.
if ((pos = nvram_list_exist(key, val, LIST_MAGIC))) {
while (*pos && *pos != LIST_SEP[0]) {
*pos++ = LIST_SEP[0];
}
}
return nvram_set(key, temp);
}
char *nvram_get(const char *key) {
// Some routers pass the key as the second argument, instead of the first.
// We attempt to fix this directly in assembly for MIPS if the key is NULL.
#if defined(mips)
if (!key) {
asm ("move %0, $a1" :"=r"(key));
}
#endif
return (nvram_get_buf(key, temp, BUFFER_SIZE) == E_SUCCESS) ? strndup(temp, BUFFER_SIZE) : NULL;
}
char *nvram_safe_get(const char *key) {
char* ret = nvram_get(key);
return ret ? ret : strdup("");
}
char *nvram_default_get(const char *key, const char *val) {
char *ret = nvram_get(key);
PRINT_MSG("%s = %s || %s\n", key, ret, val);
if (ret) {
return ret;
}
if (val && nvram_set(key, val)) {
return nvram_get(key);
}
return NULL;
}
int nvram_get_buf(const char *key, char *buf, size_t sz) {
char path[PATH_MAX] = MOUNT_POINT;
FILE *f;
if (!buf) {
PRINT_MSG("NULL output buffer, key: %s!\n", key);
return E_FAILURE;
}
if (!key) {
PRINT_MSG("NULL input key, buffer: %s!\n", buf);
return E_FAILURE;
}
PRINT_MSG("%s\n", key);
strncat(path, key, ARRAY_SIZE(path) - ARRAY_SIZE(MOUNT_POINT) - 1);
sem_lock();
if ((f = fopen(path, "rb")) == NULL) {
sem_unlock();
PRINT_MSG("Unable to open key: %s!\n", path);
return E_FAILURE;
}
buf[0] = '\0';
char tmp[sz];
while(fgets(tmp, sz, f)) {
strncat (buf, tmp, sz);
}
fclose(f);
sem_unlock();
PRINT_MSG("= \"%s\"\n", buf);
return E_SUCCESS;
}
int nvram_get_int(const char *key) {
char path[PATH_MAX] = MOUNT_POINT;
FILE *f;
int ret;
if (!key) {
PRINT_MSG("%s\n", "NULL key!");
return E_FAILURE;
}
PRINT_MSG("%s\n", key);
strncat(path, key, ARRAY_SIZE(path) - ARRAY_SIZE(MOUNT_POINT) - 1);
sem_lock();
if ((f = fopen(path, "rb")) == NULL) {
sem_unlock();
PRINT_MSG("Unable to open key: %s!\n", path);
return E_FAILURE;
}
if (fread(&ret, sizeof(ret), 1, f) != 1) {
fclose(f);
sem_unlock();
PRINT_MSG("Unable to read key: %s!\n", path);
return E_FAILURE;
}
fclose(f);
sem_unlock();
PRINT_MSG("= %d\n", ret);
return ret;
}
int nvram_getall(char *buf, size_t len) {
char path[PATH_MAX] = MOUNT_POINT;
struct stat path_stat;
struct dirent *entry;
size_t pos = 0, ret;
DIR *dir;
FILE *f;
if (!buf || !len) {
PRINT_MSG("%s\n", "NULL buffer or zero length!");
return E_FAILURE;
}
sem_lock();
if (!(dir = opendir(MOUNT_POINT))) {
sem_unlock();
PRINT_MSG("Unable to open directory %s!\n", MOUNT_POINT);
return E_FAILURE;
}
while ((entry = readdir(dir))) {
if (!strncmp(entry->d_name, ".", 1) || !strcmp(entry->d_name, "..")) {
continue;
}
strncpy(path + strlen(MOUNT_POINT), entry->d_name, ARRAY_SIZE(path) - ARRAY_SIZE(MOUNT_POINT) - 1);
path[PATH_MAX - 1] = '\0';
stat(path, &path_stat);
if (!S_ISREG(path_stat.st_mode)) {
continue;
}
if ((ret = snprintf(buf + pos, len - pos, "%s=", entry->d_name)) != strlen(entry->d_name) + 1) {
closedir(dir);
sem_unlock();
PRINT_MSG("Unable to append key %s!\n", buf + pos);
return E_FAILURE;
}
pos += ret;
if ((f = fopen(path, "rb")) == NULL) {
closedir(dir);
sem_unlock();
PRINT_MSG("Unable to open key: %s!\n", path);
return E_FAILURE;
}
ret = fread(temp, sizeof(*temp), BUFFER_SIZE, f);
if (ferror(f)) {
fclose(f);
closedir(dir);
sem_unlock();
PRINT_MSG("Unable to read key: %s!\n", path);
return E_FAILURE;
}
memcpy(buf + pos, temp, ret);
buf[pos + ret] = '\0';
pos += ret + 1;
fclose(f);
}
closedir(dir);
sem_unlock();
return E_SUCCESS;
}
int nvram_set(const char *key, const char *val) {
char path[PATH_MAX] = MOUNT_POINT;
FILE *f;
if (!key || !val) {
PRINT_MSG("%s\n", "NULL key or value!");
return E_FAILURE;
}
PRINT_MSG("%s = \"%s\"\n", key, val);
strncat(path, key, ARRAY_SIZE(path) - ARRAY_SIZE(MOUNT_POINT) - 1);
sem_lock();
if ((f = fopen(path, "wb")) == NULL) {
sem_unlock();
PRINT_MSG("Unable to open key: %s!\n", path);
return E_FAILURE;
}
if (fwrite(val, sizeof(*val), strlen(val), f) != strlen(val)) {
fclose(f);
sem_unlock();
PRINT_MSG("Unable to write value: %s to key: %s!\n", val, path);
return E_FAILURE;
}
fclose(f);
sem_unlock();
return E_SUCCESS;
}
int nvram_set_int(const char *key, const int val) {
char path[PATH_MAX] = MOUNT_POINT;
FILE *f;
if (!key) {
PRINT_MSG("%s\n", "NULL key!");
return E_FAILURE;
}
PRINT_MSG("%s = %d\n", key, val);
strncat(path, key, ARRAY_SIZE(path) - ARRAY_SIZE(MOUNT_POINT) - 1);
sem_lock();
if ((f = fopen(path, "wb")) == NULL) {
sem_unlock();
PRINT_MSG("Unable to open key: %s!\n", path);
return E_FAILURE;
}
if (fwrite(&val, sizeof(val), 1, f) != 1) {
fclose(f);
sem_unlock();
PRINT_MSG("Unable to write value: %d to key: %s!\n", val, path);
return E_FAILURE;
}
fclose(f);
sem_unlock();
return E_SUCCESS;
}
int nvram_set_default(void) {
int ret = nvram_set_default_builtin();
PRINT_MSG("Loading built-in default values = %d!\n", ret);
#define NATIVE(a, b) \
if (!system(a)) { \
PRINT_MSG("Executing native call to built-in function: %s (%p) = %d!\n", #b, b, b); \
}
#define TABLE(a) \
PRINT_MSG("Checking for symbol \"%s\"...\n", #a); \
if (a) { \
PRINT_MSG("Loading from native built-in table: %s (%p) = %d!\n", #a, a, nvram_set_default_table(a)); \
}
#define PATH(a) \
if (!access(a, R_OK)) { \
PRINT_MSG("Loading from default configuration file: %s = %d!\n", a, foreach_nvram_from(a, (void (*)(const char *, const char *, void *)) nvram_set, NULL)); \
}
NVRAM_DEFAULTS_PATH
#undef PATH
#undef NATIVE
#undef TABLE
return nvram_set_default_image();
}
static int nvram_set_default_builtin(void) {
int ret = E_SUCCESS;
PRINT_MSG("%s\n", "Setting built-in default values!");
#define ENTRY(a, b, c) \
if (b(a, c) != E_SUCCESS) { \
PRINT_MSG("Unable to initialize built-in NVRAM value %s!\n", a); \
ret = E_FAILURE; \
}
NVRAM_DEFAULTS
#undef ENTRY
return ret;
}
static int nvram_set_default_image(void) {
PRINT_MSG("%s\n", "Copying overrides from defaults folder!");
sem_lock();
system("/bin/cp "OVERRIDE_POINT"* "MOUNT_POINT);
sem_unlock();
return E_SUCCESS;
}
static int nvram_set_default_table(const char *tbl[]) {
size_t i = 0;
while (tbl[i]) {
nvram_set(tbl[i], tbl[i + 1]);
i += (tbl[i + 2] != 0 && tbl[i + 2] != (char *) 1) ? 2 : 3;
}
return E_SUCCESS;
}
int nvram_unset(const char *key) {
char path[PATH_MAX] = MOUNT_POINT;
if (!key) {
PRINT_MSG("%s\n", "NULL key!");
return E_FAILURE;
}
PRINT_MSG("%s\n", key);
strncat(path, key, ARRAY_SIZE(path) - ARRAY_SIZE(MOUNT_POINT) - 1);
sem_lock();
if (unlink(path) == -1 && errno != ENOENT) {
sem_unlock();
PRINT_MSG("Unable to unlink %s!\n", path);
return E_FAILURE;
}
sem_unlock();
return E_SUCCESS;
}
int nvram_match(const char *key, const char *val) {
if (!key) {
PRINT_MSG("%s\n", "NULL key!");
return E_FAILURE;
}
if (nvram_get_buf(key, temp, BUFFER_SIZE) != E_SUCCESS) {
return !val ? E_SUCCESS : E_FAILURE;
}
PRINT_MSG("%s (%s) ?= \"%s\"\n", key, temp, val);
if (strncmp(temp, val, BUFFER_SIZE)) {
PRINT_MSG("%s\n", "false");
return E_FAILURE;
}
PRINT_MSG("%s\n", "true");
return E_SUCCESS;
}
int nvram_invmatch(const char *key, const char *val) {
if (!key) {
PRINT_MSG("%s\n", "NULL key!");
return E_FAILURE;
}
PRINT_MSG("%s ~?= \"%s\"\n", key, val);
return !nvram_match(key, val);
}
int nvram_commit(void) {
sem_lock();
sync();
sem_unlock();
return E_SUCCESS;
}
// Hack to use static variables in shared library
#include "alias.c"