-
Notifications
You must be signed in to change notification settings - Fork 32
/
ar9300_eeprom.c
236 lines (210 loc) · 6.53 KB
/
ar9300_eeprom.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
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <string.h>
#include "ar9300_eeprom.h"
#include "wdr4300.h"
const static struct ar9300_detect *ar9300_detects[] = {
&wdr4300_detect,
NULL
};
const static struct ar9300_layout layout0 = {
{ 0x1000, 0x5000 }
};
const static struct ar9300_layout *layouts[] = {
&layout0,
NULL
};
static char buffer[64*1024];
static struct ar9300_eeprom eeproms[AR9300_MAX_EEPROMS];
int main(int argc, char *argv[])
{
int i;
int j;
int l;
int offset;
int len = 0;
int ret = -1;
bool err = false;
bool dump = false;
char *option = "";
bool found = false;
bool update = false;
int use_layout = -1;
char *outname = NULL;
char *filename = NULL;
const struct ar9300_layout *layout;
const struct ar9300_detect *detect;
const struct ar9300_revision *revision;
for (i = 1; !err && i < argc; i++)
{
if (argv[i][0] == '-')
{
option = &argv[i][1];
if (!dump && !strcmp(option, "d"))
{
dump = true;
continue;
}
if (!update && !strcmp(option, "u"))
{
update = true;
continue;
}
if (use_layout < 0 && !strcmp(option, "y0"))
{
use_layout = 0;
continue;
}
err = true;
printf("\nInvalid option: '%s'.\n", argv[i]);
}
else
{
if (outname == NULL && !strcmp(option, "u"))
{
outname = argv[i];
continue;
}
if (filename == NULL)
{
filename = argv[i];
continue;
}
err = true;
printf("\nInvalid parameter: '%s'.\n", argv[i]);
}
}
if (!err && filename == NULL)
{
err = true;
puts("\nBinary image file of 'art' partition not specified.");
}
if (!err && update && outname == NULL)
{
err = true;
puts("\nOutput binary image file 'art' not specified.");
}
if (!err && dump && update)
{
err = true;
puts("\nDump and update can not be specified together.");
}
if (err)
goto print;
l = (use_layout >= 0) ? use_layout : 0;
while (!found && (layout = layouts[l++]) != NULL)
{
if ((ret = read_eeproms(eeproms, layout, filename)) < 0)
goto print;
if (ret < AR9300_MAX_EEPROMS)
{
if (use_layout < 0)
continue;
printf("\nSmall file: '%s'.\n", filename);
goto print;
}
i = 0;
while (!found && (detect = ar9300_detects[i++]) != NULL)
{
if (memcmp(&detect->layout, layout, sizeof(struct ar9300_layout)))
continue;
j = 0;
while (!found && (revision = detect->revisions[j++]) != NULL)
found = revision->detect(eeproms, revision->data);
}
if (use_layout >= 0)
break;
}
if (!found)
{
if (ret < AR9300_MAX_EEPROMS)
{
printf("\nSmall file: '%s'.\n", filename);
goto print;
}
puts("Board not detected.");
if (use_layout < 0)
return 1;
}
else
printf("Hardware: %s Model: %s ver. %s Board: %s rev. %s\n",
detect->name, revision->name, revision->version, revision->number, revision->revision);
for (i = 0; i < AR9300_MAX_EEPROMS && (offset = layout->offsets[i]) >= 0; i++)
{
struct ar9300_eeprom *eeprom = &eeproms[i];
if (dump)
{
len += sprintf(buffer + len, "\nEEPROM 0x%4.4X:\n", offset);
len = dump_eeprom(eeprom, buffer, len);
}
if (update)
{
bool allow2G = !!(eeprom->baseEepHeader.opCapFlags.opFlags & AR5416_OPFLAGS_11G);
bool allow5G = !!(eeprom->baseEepHeader.opCapFlags.opFlags & AR5416_OPFLAGS_11A);
printf("\nUpdate EEPROM 0x%4.4X:\n", offset);
if (found && revision->fixup != NULL)
{
const struct ar9300_fixup *fixup = revision->fixup;
if (allow2G && fixup->allow2G)
{
puts("\nFix up 2.4GHz.");
memcpy(eeprom->calFreqPier2G, fixup->calFreqPier2G, sizeof(eeprom->calFreqPier2G));
memcpy(eeprom->calPierData2G, fixup->calPierData2G, sizeof(eeprom->calPierData2G));
}
if (allow5G && fixup->allow5G)
{
puts("\nFix up 5GHz.");
memcpy(eeprom->calFreqPier5G, fixup->calFreqPier5G, sizeof(eeprom->calFreqPier5G));
memcpy(eeprom->calPierData5G, fixup->calPierData5G, sizeof(eeprom->calPierData5G));
}
}
if (allow2G)
{
int c;
puts("\nUpdate regulatory 2.4GHz.");
for (c = 0; c < AR9300_NUM_CTLS_2G; c++)
{
int e;
for (e = 0; e < AR9300_NUM_BAND_EDGES_2G; e++)
eeprom->ctlPowerData_2G[c].ctlEdges[e] = CTL(60, CTL_EDGE_FLAGS(eeprom->ctlPowerData_2G[c].ctlEdges[e]));
}
}
if (allow5G)
{
int c;
puts("\nUpdate regulatory 5GHz.");
for (c = 0; c < AR9300_NUM_CTLS_5G; c++)
{
int e;
for (e = 0; e < AR9300_NUM_BAND_EDGES_5G; e++)
eeprom->ctlPowerData_5G[c].ctlEdges[e] = CTL(60, CTL_EDGE_FLAGS(eeprom->ctlPowerData_5G[c].ctlEdges[e]));
}
}
}
}
if (found && revision->fixup != NULL)
{
if (dump)
{
len += sprintf(buffer + len, "\n%s\n", revision->fixup->info);
len = dump_fixup((struct ar9300_fixup *)revision->fixup, buffer, len);
}
else
printf("\n%s\n", revision->fixup->info);
}
if (dump)
puts(buffer);
if (update)
write_eeproms(eeproms, layout, filename, outname);
return 0;
print:
puts("\nUsage: ar9300_eeprom [-d|-y0] ART_FILE [-u OUT_FILE]\n\n\t\
-d: dump eeprom.\n\t\
-u: update eeprom.\n\t\
-y0: use layout 0 - eeprom in 0x1000 and eeprom in 0x5000.\n\n\t\
ART_FILE: 64KB binary image file of 'art' partition.\n\t\
OUT_FILE: updated binary image file 'art'.\n");
return 1;
}