forked from iH8sn0w/iDove
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvram.c
executable file
·183 lines (154 loc) · 4.1 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
/**
* GreenPois0n Cynanide - nvram.c
* Copyright (C) 2010 Chronic-Dev Team
* Copyright (C) 2010 Joshua Hill
*
* 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, see <http://www.gnu.org/licenses/>.
**/
#include <stdio.h>
#include <string.h>
#include "lock.h"
#include "nvram.h"
#include "common.h"
LinkedList* gNvramList = NULL;
void* find_nvram_list() {
unsigned int ref1 = find_string(TARGET_BASEADDR, TARGET_BASEADDR, 0x40000, "build-style");
unsigned int* ref2 = find_string(ref1+4, ref1+4, 0x40000, "build-style");
ref2 -= 5;
return *ref2;
}
int nvram_init() {
gNvramList = find_nvram_list();
if(gNvramList == NULL) {
puts("Unable to find gNvramList\n");
} else {
//printf("Found gNvramList at 0x%x\n", gNvramList);
cmd_add("nvram", &nvram_cmd, "view and modify nvram variables");
}
return 0;
}
int nvram_cmd(int argc, CmdArg* argv) {
char* var = NULL;
char* value = NULL;
char* action = NULL;
if(argc < 2) {
puts("usage: nvram <action> [options]\n");
puts(" list \t\tdisplay list of nvram variables\n");
puts(" remove <var> \t\tdeletes entry from nvram variables\n");
puts(" get <var> \t\tshow contents of nvram variable\n");
puts(" set <var> <value> \t\tset contents of nvram variable\n\n");
return 0;
}
action = argv[1].string;
if(argc == 2) {
if(!strcmp(action, "list")) {
nvram_display_list();
return 0;
}
} else if(argc == 3) {
if(!strcmp(action, "get")) {
var = argv[2].string;
return nvram_get_var(var);
} else if(!strcmp(action, "remove")) {
var = argv[2].string;
return nvram_remove_var(var);
}
} else if(argc == 4) {
if(!strcmp(action, "set")) {
var = argv[2].string;
value = argv[3].string;
return nvram_set_var(var, value);
}
}
return 0;
}
void nvram_display_list() {
NvramVar* var = (NvramVar*) gNvramList->next;
enter_critical_section();
while(var != (void*) gNvramList) {
printf("0x%08x: %s = %s\n", var, var->name, var->string);
var = var->next;
}
printf("\n");
exit_critical_section();
}
int nvram_remove_var(const char* name) {
NvramVar* var = nvram_find_var(name);
if(var == NULL) {
return -1;
}
NvramVar* next = var->next;
NvramVar* prev = var->prev;
next->prev = prev;
prev->next = next;
var->next = 0;
var->prev = 0;
free(var->string);
free(var);
return 0;
}
NvramVar* nvram_find_var(const char* name) {
NvramVar* var = (NvramVar*) gNvramList->next;
while(var != (void*) gNvramList) {
if(!strcmp(name, var->name)) {
return var;
}
var = (NvramVar*) var->next;
}
return NULL;
}
int nvram_get_var(const char* name) {
enter_critical_section();
NvramVar* var = nvram_find_var(name);
if(var == NULL) {
puts("unable to find nvram entry\n\n");
exit_critical_section();
return -1;
}
printf("%s = %s\n", var->name, var->string);
exit_critical_section();
return 0;
}
int nvram_set_var(const char* name, const char* value) {
enter_critical_section();
NvramVar* var = NULL;
var = nvram_find_var(name);
if(var != NULL) {
nvram_remove_var(name);
var = NULL;
}
NvramVar* next = (NvramVar*) gNvramList;
NvramVar* prev = (NvramVar*) gNvramList->prev;
var = malloc(sizeof(NvramVar));
if(var == NULL) {
exit_critical_section();
return -1;
}
strncpy(var->name, name, 0x40);
var->string = malloc(0x200);
if(var->string == NULL) {
free(var);
exit_critical_section();
return -1;
}
strncpy(var->string, value, 0x200);
var->integer = strtoul(var->string, NULL, 0);
var->save = 1;
var->next = next;
var->prev = prev;
next->prev = var;
prev->next = var;
exit_critical_section();
return 0;
}