forked from firmadyne/libnvram
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnvram.c
More file actions
1112 lines (944 loc) · 30.2 KB
/
nvram.c
File metadata and controls
1112 lines (944 loc) · 30.2 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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#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 <sys/file.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <mntent.h>
#include <fcntl.h>
#include "nvram.h"
#include "config.h"
#include "strings.h"
// 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)
/* Global variables */
static int init = 0;
static volatile int logging_enabled = 0;
#define FIRMAE_NVRAM 1
static int _libinject_flock_asm(int fd, int op) {
// File lock with SYS_flock. We do this in assembly
// for portability - libc may not be available / match versions
// with the library we're building
int retval;
#if defined(__mips64__)
asm volatile(
"daddiu $a0, %1, 0\n" // Move fd to $a0
"daddiu $a1, %2, 0\n" // Move op to $a1
"li $v0, %3\n" // Load SYS_flock (the system call number) into $v0
"syscall\n" // Make the system call
"move %0, $v0\n" // Move the result from $v0 to retval
: "=r" (retval) // Output
: "r" (fd), "r" (op), "i" (SYS_flock) // Inputs
: "v0", "a0", "a1" // Clobber list
);
#elif defined(__mips__)
asm volatile(
"move $a0, %1\n" // Correctly move fd (from C variable) to $a0
"move $a1, %2\n" // Correctly move op (from C variable) to $a1
"li $v0, %3\n" // Load the syscall number for flock into $v0
"syscall\n" // Perform the syscall
"move %0, $v0" // Move the result from $v0 to retval
: "=r" (retval) // Output
: "r" (fd), "r" (op), "i" (SYS_flock) // Inputs; "i" for immediate syscall number
: "v0", "a0", "a1" // Clobber list
);
#elif defined(__arm__)
asm volatile(
"mov r0, %1\n" // Move fd to r0, the first argument for the system call
"mov r1, %2\n" // Move op to r1, the second argument for the system call
"mov r7, %3\n" // Move SYS_flock (the system call number) to r7
"svc 0x00000000\n" // Make the system call
"mov %[result], r0" // Move the result from r0 to retval
: [result]"=r" (retval) // Output
: "r"(fd), "r"(op), "i"(SYS_flock) // Inputs
: "r0", "r1", "r7" // Clobber list
);
#elif defined(__aarch64__) // AArch64
// XXX: using %w registers for 32-bit movs. This made the compiler
// happy but I'm not sure why we can't be operating on 64-bit ints
asm volatile(
"mov w0, %w1\n" // Move fd to w0, the first argument for the system call
"mov w1, %w2\n" // Move op to w1, the second argument for the system call
"mov x8, %3\n" // Move SYS_flock (the system call number) to x8
"svc 0\n" // Make the system call (Supervisor Call)
"mov %w0, w0\n" // Move the result from w0 to retval
: "=r" (retval) // Output
: "r" (fd), "r" (op), "i" (SYS_flock) // Inputs
: "x0", "x1", "x8" // Clobber list
);
#elif defined(__x86_64__) // x86_64
// XXX: movl's for 32-bit movs. This made the compiler
// happy but I'm not sure why we can't be operating on 64-bit ints
// I think it should be fine though
asm volatile(
"movl %1, %%edi\n" // Move fd to rdi (1st argument)
"movl %2, %%esi\n" // Move op to rsi (2nd argument)
"movl %3, %%eax\n" // Move SYS_flock to rax (syscall number)
"syscall\n" // Make the syscall
"movl %%eax, %0\n" // Move the result from rax to retval
: "=r" (retval) // Output
: "r" (fd), "r" (op), "i" (SYS_flock) // Inputs
: "rax", "rdi", "rsi" // Clobber list
);
#elif defined(__i386__) // x86 32-bit
asm volatile(
"movl %1, %%ebx\n" // Move fd to ebx
"movl %2, %%ecx\n" // Move op to ecx
"movl %3, %%eax\n" // Move SYS_flock to eax
"int $0x80\n" // Make the syscall
"movl %%eax, %0\n" // Move the result from eax to retval
: "=r" (retval) // Output
: "r" (fd), "r" (op), "i" (SYS_flock) // Inputs
: "eax", "ebx", "ecx" // Clobber list
);
#elif defined(__powerpc__) || defined(__powerpc64__)
asm volatile(
"mr 3, %1\n" // Move fd to r3 (1st argument)
"mr 4, %2\n" // Move op to r4 (2nd argument)
"li 0, %3\n" // Load SYS_flock (the system call number) into r0
"sc\n" // Make the system call
"mr %0, 3\n" // Move the result from r3 to retval
: "=r" (retval) // Output
: "r" (fd), "r" (op), "i" (SYS_flock) // Inputs
: "r0", "r3", "r4" // Clobber list
);
#elif defined(__riscv)
asm volatile(
"mv a0, %1\n" // Move fd to a0 (1st argument)
"mv a1, %2\n" // Move op to a1 (2nd argument)
"li a7, %3\n" // Load SYS_flock (the system call number) into a7
"ecall\n" // Make the system call
"mv %0, a0\n" // Move the result from a0 to retval
: "=r" (retval) // Output
: "r" (fd), "r" (op), "i" (SYS_flock) // Inputs
: "a0", "a1", "a7" // Clobber list
);
#elif defined(__loongarch64)
asm volatile(
"move $a0, %1\n" // Move fd to $a0 (1st argument)
"move $a1, %2\n" // Move op to $a1 (2nd argument)
"addi.d $a7, $zero, %3\n" // Load SYS_flock (the system call number) into $a7
"syscall 0\n" // Make the system call
"move %0, $a0\n" // Move the result from $a0 to retval
: "=r" (retval) // Output
: "r" (fd), "r" (op), "i" (SYS_flock) // Inputs
: "a0", "a1", "a7" // Clobber list
);
#else
#error "Unsupported architecture"
#endif
return retval;
}
static int _libinject_dir_lock() {
int dirfd;
// If not initialized, check for existing mount before triggering NVRAM init
if (!init) {
PRINT_MSG("%s\n", "Triggering NVRAM initialization!");
libinject_nvram_init();
}
dirfd = open(MOUNT_POINT, O_DIRECTORY | O_RDONLY);
if(dirfd < 0) {
PRINT_MSG("Couldn't open %s\n", MOUNT_POINT);
}
if(_libinject_flock_asm(dirfd,LOCK_EX) < 0) {
PRINT_MSG("Couldn't lock %s\n", MOUNT_POINT);
}
return dirfd;
}
static void _libinject_dir_unlock(int dirfd) {
if(_libinject_flock_asm(dirfd,LOCK_UN) < 0) {
PRINT_MSG("Couldn't unlock %s\n", MOUNT_POINT);
}
close(dirfd);
return;
}
int libinject_ret_1() {
return E_SUCCESS; // 1
}
int libinject_ret_0() {
return E_FAILURE; // 0
}
int libinject_ret_1_arg(char* a1) {
return E_SUCCESS; //1
}
int libinject_ret_0_arg(char* a1) {
return E_FAILURE; // 0
}
int libinject_nvram_init(void) {
if (!init) {
// If we haven't initialized yet, check if we should be logging events
logging_enabled = igloo_hypercall2(111, 0, 0);
}
init = 1;
return E_SUCCESS;
}
int libinject_nvram_reset(void) {
PRINT_MSG("%s\n", "Reseting NVRAM...");
if (libinject_nvram_clear() != E_SUCCESS) {
PRINT_MSG("%s\n", "Unable to clear NVRAM!");
return E_FAILURE;
}
return E_SUCCESS;
}
int libinject_nvram_clear(void) {
char path[PATH_MAX] = MOUNT_POINT;
struct dirent *entry;
int ret = E_SUCCESS;
DIR *dir;
int dirfd;
int rv;
PRINT_MSG("%s\n", "Clearing NVRAM...");
dirfd = _libinject_dir_lock();
if (!(dir = opendir(MOUNT_POINT))) {
_libinject_dir_unlock(dirfd);
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;
}
// Clear is really a bunch of unsets
if (logging_enabled) {
rv = igloo_hypercall2(110, (unsigned long)path, strlen(path));
while (rv == 1) {
PAGE_IN(path);
rv = igloo_hypercall2(110, (unsigned long)path, strlen(path));
}
}
}
closedir(dir);
_libinject_dir_unlock(dirfd);
return ret;
}
int libinject_nvram_close(void) {
PRINT_MSG("%s\n", "Closing NVRAM...");
return E_SUCCESS;
}
int libinject_nvram_list_add(const char *key, const char *val) {
char *temp = malloc(BUFFER_SIZE);
if (!temp) return E_FAILURE;
memset(temp, 0, BUFFER_SIZE);
char *pos;
PRINT_MSG("%s = %s + %s\n", val, temp, key);
if (libinject_nvram_get_buf(key, temp, BUFFER_SIZE) != E_SUCCESS) {
free(temp);
return libinject_nvram_set(key, val);
}
if (!key || !val) {
free(temp);
return E_FAILURE;
}
if (strlen(temp) + 1 + strlen(val) + 1 > BUFFER_SIZE) {
free(temp);
return E_FAILURE;
}
// This will overwrite the temp buffer, but it is OK
if (libinject_nvram_list_exist(key, val, LIST_MAGIC) != NULL) {
free(temp);
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) {
free(temp);
return E_FAILURE;
}
int ret = libinject_nvram_set(key, temp);
free(temp);
return ret;
}
char *libinject_nvram_list_exist(const char *key, const char *val, int magic) {
char *temp = malloc(BUFFER_SIZE);
if (!temp) return (magic == LIST_MAGIC) ? NULL : (char *)E_FAILURE;
memset(temp, 0, BUFFER_SIZE);
char *pos = NULL;
if (libinject_nvram_get_buf(key, temp, BUFFER_SIZE) != E_SUCCESS) {
free(temp);
return (magic == LIST_MAGIC) ? NULL : (char *)E_FAILURE;
}
PRINT_MSG("%s ?in %s (%s)\n", val, key, temp);
if (!val) {
free(temp);
return (magic == LIST_MAGIC) ? NULL : (char *) E_FAILURE;
}
while ((pos = strtok(!pos ? temp : NULL, LIST_SEP))) {
if (!strcmp(pos + 1, val)) {
char *result = (magic == LIST_MAGIC) ? pos + 1 : (char *) E_SUCCESS;
free(temp);
return result;
}
}
free(temp);
return (magic == LIST_MAGIC) ? NULL : (char *) E_FAILURE;
}
int libinject_nvram_list_del(const char *key, const char *val) {
char *temp = malloc(BUFFER_SIZE);
if (!temp) return E_FAILURE;
memset(temp, 0, BUFFER_SIZE);
char *pos;
if (libinject_nvram_get_buf(key, temp, BUFFER_SIZE) != E_SUCCESS) {
free(temp);
return E_SUCCESS;
}
PRINT_MSG("%s = %s - %s\n", key, temp, val);
if (!val) {
free(temp);
return E_FAILURE;
}
// This will overwrite the temp buffer, but it is OK.
if ((pos = libinject_nvram_list_exist(key, val, LIST_MAGIC))) {
while (*pos && *pos != LIST_SEP[0]) {
*pos++ = LIST_SEP[0];
}
}
int ret = libinject_nvram_set(key, temp);
free(temp);
return ret;
}
char *libinject_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
char *temp = malloc(BUFFER_SIZE);
if (!temp) return NULL;
memset(temp, 0, BUFFER_SIZE);
char *result = (libinject_nvram_get_buf(key, temp, BUFFER_SIZE) == E_SUCCESS) ? strndup(temp, BUFFER_SIZE) : NULL;
free(temp);
return result;
}
char *libinject_nvram_safe_get(const char *key) {
char* ret = libinject_nvram_get(key);
return ret ? ret : strdup("");
}
char *libinject_nvram_default_get(const char *key, const char *val) {
char *ret = libinject_nvram_get(key);
PRINT_MSG("%s = %s || %s\n", key, ret, val);
if (ret) {
return ret;
}
if (val && libinject_nvram_set(key, val)) {
return libinject_nvram_get(key);
}
return NULL;
}
int libinject_nvram_get_buf(const char *key, char *buf, size_t sz) {
memset(buf, 0, sz);
char *path = malloc(PATH_MAX);
if (!path) return E_FAILURE;
strncpy(path, MOUNT_POINT, PATH_MAX - 1);
path[PATH_MAX - 1] = '\0';
FILE *f;
int dirfd;
int rv;
if (!buf) {
PRINT_MSG("NULL output buffer, key: %s!\n", key);
free(path);
return E_FAILURE;
}
if (!key) {
PRINT_MSG("NULL input key, buffer: %s!\n", buf);
free(path);
#ifdef FIRMAE_NVRAM
return E_SUCCESS;
#else
return E_FAILURE;
#endif
}
PRINT_MSG("%s\n", key);
strncat(path, key, PATH_MAX - strlen(path) - 1);
// Before taking the lock, check if the key exists, if not bail
if (access(path, F_OK) != 0) {
if (logging_enabled) {
rv = igloo_hypercall2(107, (unsigned long)path, strlen(path));
}
free(path);
#ifdef FIRMAE_NVRAM
// Key doesn't exist, set default empty value
buf[0] = '\0';
return E_SUCCESS;
#else
return E_FAILURE;
#endif
}
dirfd = _libinject_dir_lock();
if ((f = fopen(path, "rb")) == NULL) {
// We just checked without the lock, but it's empty after we took the lock
// Someone must have just deleted it. Slow path but not wrong.
_libinject_dir_unlock(dirfd);
PRINT_MSG("Unable to open key: %s! Set default value to \"\"\n", path);
if (logging_enabled) {
rv = igloo_hypercall2(107, (unsigned long)path, strlen(path));
while (rv == 1) {
PAGE_IN(path);
rv = igloo_hypercall2(107, (unsigned long)path, strlen(path));
}
}
free(path);
#ifdef FIRMAE_NVRAM
//If key value is not found, make the default value to ""
//if (!strcmp(key, "noinitrc")) // Weird magic constant from FirmAE
// return E_FAILURE;
buf[0] = '\0';
return E_SUCCESS;
#else
return E_FAILURE;
#endif
}
else
{
PRINT_MSG("\n\n[NVRAM] %d %s\n\n", (int)strlen(key), key);
// success
if (logging_enabled) {
rv = igloo_hypercall2(108, (unsigned long)path, strlen(path));
while (rv == 1) {
PAGE_IN(path);
rv = igloo_hypercall2(108, (unsigned long)path, strlen(path));
}
}
}
buf[0] = '\0';
char tmp[sz];
while(fgets(tmp, sz, f)) {
strncat (buf, tmp, sz);
}
fclose(f);
_libinject_dir_unlock(dirfd);
free(path);
PRINT_MSG("= \"%s\"\n", buf);
return E_SUCCESS;
}
int libinject_nvram_get_int(const char *key) {
char *path = malloc(PATH_MAX);
if (!path) return E_FAILURE;
strncpy(path, MOUNT_POINT, PATH_MAX - 1);
path[PATH_MAX - 1] = '\0';
FILE *f;
char buf[32]; // Buffer to store ASCII representation of the integer
int ret = 0;
int dirfd;
if (!key) {
PRINT_MSG("%s\n", "NULL key!");
free(path);
return E_FAILURE;
}
PRINT_MSG("%s\n", key);
strncat(path, key, PATH_MAX - strlen(path) - 1);
// Before taking the lock, check if the key exists, if not bail
if (access(path, F_OK) != 0) {
free(path);
return E_FAILURE;
}
dirfd = _libinject_dir_lock();
// Try to open the file
if ((f = fopen(path, "rb")) == NULL) {
PRINT_MSG("Unable to open key: %s!\n", path);
free(path);
_libinject_dir_unlock(dirfd);
return E_FAILURE;
}
// Attempt to read the ASCII representation of the integer
if (fgets(buf, sizeof(buf), f) != NULL) {
// Attempt to convert the read string to an integer
char *endptr;
long val = strtol(buf, &endptr, 10);
// Check for conversion errors (no digits found or not all string parsed)
if ((endptr != buf && *endptr == '\n') || *endptr == '\0') {
ret = (int)val; // Successfully converted ASCII to integer
} else {
// Reset file pointer and try reading as binary integer
fseek(f, 0, SEEK_SET);
if (fread(&ret, sizeof(ret), 1, f) != 1) {
PRINT_MSG("Unable to read key as binary int: %s!\n", path);
fclose(f);
_libinject_dir_unlock(dirfd);
free(path);
return E_FAILURE;
}
}
} else {
fclose(f);
_libinject_dir_unlock(dirfd);
PRINT_MSG("Unable to read key: %s!\n", path);
free(path);
return E_FAILURE;
}
fclose(f);
_libinject_dir_unlock(dirfd);
free(path);
PRINT_MSG("= %d\n", ret);
return ret;
}
int libinject_nvram_getall(char *buf, size_t len) {
char *path = malloc(PATH_MAX);
if (!path) return E_FAILURE;
strncpy(path, MOUNT_POINT, PATH_MAX - 1);
path[PATH_MAX - 1] = '\0';
struct dirent *entry;
size_t pos = 0, ret;
DIR *dir;
FILE *f;
int dirfd;
if (!buf || !len) {
PRINT_MSG("%s\n", "NULL buffer or zero length!");
free(path);
return E_FAILURE;
}
dirfd = _libinject_dir_lock();
if (!(dir = opendir(MOUNT_POINT))) {
_libinject_dir_unlock(dirfd);
PRINT_MSG("Unable to open directory %s!\n", MOUNT_POINT);
free(path);
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, PATH_MAX - strlen(MOUNT_POINT) - 1);
path[PATH_MAX - 1] = '\0';
if ((ret = snprintf(buf + pos, len - pos, "%s=", entry->d_name)) != strlen(entry->d_name) + 1) {
closedir(dir);
_libinject_dir_unlock(dirfd);
PRINT_MSG("Unable to append key %s!\n", buf + pos);
free(path);
return E_FAILURE;
}
pos += ret;
if ((f = fopen(path, "rb")) == NULL) {
PRINT_MSG("Unable to open key: %s!\n", path);
closedir(dir);
_libinject_dir_unlock(dirfd);
free(path);
return E_FAILURE;
}
// Determine file size
fseek(f, 0, SEEK_END);
long filesize = ftell(f);
rewind(f);
if (filesize < 0) filesize = 0;
char *temp = malloc(filesize + 1);
if (!temp) {
PRINT_MSG("Unable to allocate buffer for key: %s!\n", path);
fclose(f);
closedir(dir);
_libinject_dir_unlock(dirfd);
free(path);
return E_FAILURE;
}
ret = fread(temp, 1, filesize, f);
if (ferror(f)) {
PRINT_MSG("Unable to read key: %s!\n", path);
free(temp);
fclose(f);
closedir(dir);
_libinject_dir_unlock(dirfd);
free(path);
return E_FAILURE;
}
memcpy(buf + pos, temp, ret);
buf[pos + ret] = '\0';
pos += ret + 1;
free(temp);
fclose(f);
}
closedir(dir);
_libinject_dir_unlock(dirfd);
free(path);
return E_SUCCESS;
}
int libinject_nvram_set(const char *key, const char *val) {
char *path = malloc(PATH_MAX);
if (!path) return E_FAILURE;
strncpy(path, MOUNT_POINT, PATH_MAX - 1);
path[PATH_MAX - 1] = '\0';
FILE *f;
int dirfd;
int rv;
if (!key || !val) {
PRINT_MSG("%s\n", "NULL key or value!");
free(path);
return E_FAILURE;
}
PRINT_MSG("%s = \"%s\"\n", key, val);
strncat(path, key, PATH_MAX - strlen(path) - 1);
if (logging_enabled) {
rv = igloo_hypercall2(109, (unsigned long)path, (unsigned long)val);
while (rv == 1) {
PAGE_IN(path);
rv = igloo_hypercall2(109, (unsigned long)path, (unsigned long)val);
}
}
dirfd = _libinject_dir_lock();
if ((f = fopen(path, "wb")) == NULL) {
_libinject_dir_unlock(dirfd);
PRINT_MSG("Unable to open key: %s!\n", path);
free(path);
return E_FAILURE;
}
if (fwrite(val, sizeof(*val), strlen(val), f) != strlen(val)) {
fclose(f);
_libinject_dir_unlock(dirfd);
PRINT_MSG("Unable to write value: %s to key: %s!\n", val, path);
free(path);
return E_FAILURE;
}
PRINT_MSG("Wrote value: %s to key: %s!\n", val, path);
fclose(f);
_libinject_dir_unlock(dirfd);
free(path);
return E_SUCCESS;
}
int libinject_nvram_set_int(const char *key, const int val) {
size_t path_len = strlen(MOUNT_POINT) + strlen(key) + 1;
char *path = malloc(path_len);
FILE *f;
int dirfd;
if (!key) {
PRINT_MSG("%s\n", "NULL key!");
free(path);
return E_FAILURE;
}
// Truncate key if too long
size_t max_key_len = PATH_MAX - strlen(MOUNT_POINT) - 1;
char truncated_key[PATH_MAX];
strncpy(truncated_key, key, max_key_len);
truncated_key[max_key_len] = '\0';
PRINT_MSG("%s = %d\n", truncated_key, val);
snprintf(path, path_len, "%s%s", MOUNT_POINT, truncated_key);
dirfd = _libinject_dir_lock();
if ((f = fopen(path, "wb")) == NULL) {
_libinject_dir_unlock(dirfd);
PRINT_MSG("Unable to open key: %s!\n", path);
free(path);
return E_FAILURE;
}
if (fwrite(&val, sizeof(val), 1, f) != 1) {
fclose(f);
_libinject_dir_unlock(dirfd);
PRINT_MSG("Unable to write value: %d to key: %s!\n", val, path);
free(path);
return E_FAILURE;
}
fclose(f);
_libinject_dir_unlock(dirfd);
free(path);
return E_SUCCESS;
}
int libinject_nvram_unset(const char *key) {
size_t path_len = strlen(MOUNT_POINT) + strlen(key) + 1;
char *path = malloc(path_len);
int dirfd;
int rv;
if (!key) {
PRINT_MSG("%s\n", "NULL key!");
free(path);
return E_FAILURE;
}
// Truncate key if too long
size_t max_key_len = PATH_MAX - strlen(MOUNT_POINT) - 1;
char truncated_key[PATH_MAX];
strncpy(truncated_key, key, max_key_len);
truncated_key[max_key_len] = '\0';
PRINT_MSG("%s\n", truncated_key);
snprintf(path, path_len, "%s%s", MOUNT_POINT, truncated_key);
if (logging_enabled) {
rv = igloo_hypercall2(110, (unsigned long)path, strlen(path));
while (rv == 1) {
PAGE_IN(path);
rv = igloo_hypercall2(110, (unsigned long)path, strlen(path));
}
}
dirfd = _libinject_dir_lock();
if (unlink(path) == -1 && errno != ENOENT) {
_libinject_dir_unlock(dirfd);
PRINT_MSG("Unable to unlink %s!\n", path);
free(path);
return E_FAILURE;
}
_libinject_dir_unlock(dirfd);
free(path);
return E_SUCCESS;
}
int libinject_nvram_safe_unset(const char *key) {
char *temp = malloc(BUFFER_SIZE);
if (!temp) return E_SUCCESS;
memset(temp, 0, BUFFER_SIZE);
if (libinject_nvram_get_buf(key, temp, BUFFER_SIZE) == E_SUCCESS) {
libinject_nvram_unset(key);
}
free(temp);
return E_SUCCESS;
}
int libinject_nvram_match(const char *key, const char *val) {
char *temp = malloc(BUFFER_SIZE);
if (!temp) return E_FAILURE;
memset(temp, 0, BUFFER_SIZE);
if (!key) {
free(temp);
PRINT_MSG("%s\n", "NULL key!");
return E_FAILURE;
}
if (libinject_nvram_get_buf(key, temp, BUFFER_SIZE) != E_SUCCESS) {
free(temp);
return !val ? E_SUCCESS : E_FAILURE;
}
PRINT_MSG("%s (%s) ?= \"%s\"\n", key, temp, val);
int cmp = strncmp(temp, val, BUFFER_SIZE);
free(temp);
if (cmp) {
PRINT_MSG("%s\n", "false");
return E_FAILURE;
}
PRINT_MSG("%s\n", "true");
return E_SUCCESS;
}
int libinject_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 !libinject_nvram_match(key, val);
}
int libinject_nvram_commit(void) {
int dirfd;
dirfd = _libinject_dir_lock();
sync();
_libinject_dir_unlock(dirfd);
return E_SUCCESS;
}
int libinject_parse_nvram_from_file(const char *file)
{
FILE *f;
char *buffer;
int fileLen=0;
if((f = fopen(file, "rb")) == NULL){
PRINT_MSG("Unable to open file: %s!\n", file);
return E_FAILURE;
}
/* Get file length */
fseek(f, 0, SEEK_END);
fileLen = ftell(f);
rewind(f);
/* Allocate memory */
buffer = (char*)malloc(sizeof(char) *fileLen);
int rv = fread(buffer, 1, fileLen, f);
if (rv != fileLen) {
PRINT_MSG("Unable to read file: %s: %d!\n", file, rv);
free(buffer);
fclose(f);
return E_FAILURE;
}
fclose(f);
/* split the buffer including null byte */
#define LEN 1024
int i=0,j=0,k=0; int left = 1;
char *key="", *val="";
char larr[LEN]="", rarr[LEN]="";
for(i=0; i < fileLen; i++)
{
char tmp[4];
sprintf(tmp, "%c", *(buffer+i));
if (left==1 && j<LEN)
larr[j++] = tmp[0];
else if(left==0 && k<LEN)
rarr[k++] = tmp[0];
if(!memcmp(tmp,"=",1)){
left=0;
larr[j-1]='\0';
}
if (!memcmp(tmp,"\x00",1)){
key = larr; val = rarr;
libinject_nvram_set(key, val);
j=0; k=0; left=1;
memset(larr, 0, LEN); memset(rarr, 0, LEN);
}
}
return E_SUCCESS;
}
/* Atheros/Broadcom NVRAM */
int libinject_nvram_get_nvramspace(void) {
return NVRAM_SIZE;
}
char *libinject_nvram_nget(const char *fmt, ...) {
va_list va;
char *temp = malloc(BUFFER_SIZE);
if (!temp) return NULL;
va_start(va, fmt);
vsnprintf(temp, BUFFER_SIZE, fmt, va);
va_end(va);
char *result = libinject_nvram_get(temp);
free(temp);
return result;
}
int libinject_nvram_nset(const char *val, const char *fmt, ...) {
va_list va;
char *temp = malloc(BUFFER_SIZE);
if (!temp) return E_FAILURE;
memset(temp, 0, BUFFER_SIZE);
va_start(va, fmt);
vsnprintf(temp, BUFFER_SIZE, fmt, va);
va_end(va);
int ret = libinject_nvram_set(temp, val);
free(temp);
return ret;
}
int libinject_nvram_nset_int(const int val, const char *fmt, ...) {
va_list va;
char *temp = malloc(BUFFER_SIZE);
if (!temp) return E_FAILURE;
memset(temp, 0, BUFFER_SIZE);
va_start(va, fmt);
vsnprintf(temp, BUFFER_SIZE, fmt, va);
va_end(va);
int ret = libinject_nvram_set_int(temp, val);
free(temp);
return ret;
}
int libinject_nvram_nmatch(const char *val, const char *fmt, ...) {
va_list va;
char *temp = malloc(BUFFER_SIZE);
if (!temp) return E_FAILURE;
memset(temp, 0, BUFFER_SIZE);
va_start(va, fmt);
vsnprintf(temp, BUFFER_SIZE, fmt, va);
va_end(va);
int ret = libinject_nvram_match(temp, val);
free(temp);
return ret;
}
/* Realtek */
int libinject_apmib_get(const int key, void *buf) {
char *temp = malloc(BUFFER_SIZE);
if (!temp) return 0;
memset(temp, 0, BUFFER_SIZE);
int res;
snprintf(temp, BUFFER_SIZE, "%d", key);
if ((res = libinject_nvram_get_int(temp))) {
(*(int32_t *) buf) = res;
}
free(temp);
return res;
}
int libinject_apmib_set(const int key, void *buf) {
char *temp = malloc(BUFFER_SIZE);
if (!temp) return E_FAILURE;
memset(temp, 0, BUFFER_SIZE);
snprintf(temp, BUFFER_SIZE, "%d", key);
int ret = libinject_nvram_set_int(temp, ((int32_t *) buf)[0]);
free(temp);
return ret;