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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h> //mark_file
#include "inband_if.h"
#define HOST_NETIF ("wlan0")
#define SLAVE_MAC ("001234567899")
#define ETH_P_RTK 0x8899 // Realtek Remote Control Protocol (RRCP)
int hcd_inband_chan=0;
// command ID
#define id_ioctl 0x01
#define id_systemcall 0x02
#define id_firm_upgrade 0x03
#define id_hostcmd_only 0xff //only for new hostcmd implement ,if the cmd is not real "inband cmd"
// Command Table
struct inband_cmd_table_t {
char cmd_id;
const char *cmd; // Input command string
int (*func)(int index,int argc, char *argv[]);
void (*print_func)(char *para,char *data,int len);
const char *msg; // Help message
};
/*firmware define*/
#define __PACK__ __attribute__ ((packed))
#define SIGNATURE_LEN 4
#define FW_HEADER_WITH_ROOT ((char *)"cr6c")
#define FW_HEADER ((char *)"cs6c")
#define ROOT_HEADER ((char *)"r6cr")
#define WEB_HEADER ((char *)"w6cg") //no ap sign now //mark_issue
typedef struct img_header {
unsigned char signature[SIGNATURE_LEN] __PACK__;
unsigned int startAddr __PACK__;
unsigned int burnAddr __PACK__;
unsigned int len __PACK__;
} IMG_HEADER_T, *IMG_HEADER_Tp;
unsigned int host_inband_seq=0; //use to do requset <-> response check
int cmd_systemcall(int index,int argc, char *argv[]);
int cmd_firmware_upgrade(int index,int argc, char *argv[]);
void print_systemcall(char *para,char *data,int len);
struct inband_cmd_table_t inband_cmd_table[]=
{
//
{id_systemcall,"systemcall", cmd_systemcall, print_systemcall, "inband systemcall \'cmd_string\'"},
{id_firm_upgrade,"firm_upgrade", cmd_firmware_upgrade, NULL,"inband firm_upgrade /var/linux.bin"},
//{id_hostcmd_only,"send_conf", cmd_sendconf, NULL,"inband send_conf /etc/xxx.conf"},
{NULL, NULL, NULL},
};
unsigned int get_random(int max)
{
struct timeval tod;
gettimeofday(&tod , NULL);
srand(tod.tv_usec);
return rand()%max;
}
void print_command_list(void)
{
int i;
printf("\n==========commands for debugging============\n");
i = 0;
while (inband_cmd_table[i].cmd != NULL) {
printf("%s\n", inband_cmd_table[i].msg);
i++;
}
}
void print_systemcall(char *para,char *data,int len)
{
int i=0;
unsigned char *value;
data[len] = '\0';
printf("cmd_cfgread \n");
printf("%s",data);
}
static int host_inband_write(char cmd_id,char *data,int data_len)
{
int ret;
host_inband_seq = get_random(65536);
ret = inband_write(hcd_inband_chan,host_inband_seq,cmd_id,data,data_len,0); //send request
return ret;
}
static inline int CHECKSUM_OK(unsigned char *data, int len)
{
int i;
unsigned char sum=0;
for (i=0; i<len; i++)
sum += data[i];
if (sum == 0)
return 1;
else
return 0;
}
static int fwChecksumOk(char *data, int len)
{
unsigned short sum=0;
int i;
for (i=0; i<len; i+=2) {
#ifdef _LITTLE_ENDIAN_
sum += WORD_SWAP( *((unsigned short *)&data[i]) );
#else
sum += *((unsigned short *)&data[i]);
#endif
}
return( (sum==0) ? 1 : 0);
}
static int host_firmware_write(char *data,int len)
{
unsigned int tx_seq,rx_seq;
int ret=0,count;
char rx_cmd_type;
char *buf_p;
ret = host_inband_write(id_firm_upgrade,data,len); //send request
tx_seq = host_inband_seq;
if(ret < 0)
return -1;
ret = inband_rcv_data_and_seq(hcd_inband_chan,&rx_seq,&rx_cmd_type,&buf_p,20000); //return data length
if(ret < 0)
{
ret=-1;
goto out;
}
if( (rx_cmd_type == id_firm_upgrade) && (tx_seq == rx_seq))
printf("host_firmware_write ok!\n");
else
{
ret=-1;
printf("host_firmware_write fail!\n");
}
out:
inband_free_buf(buf_p, count);
return ret;
}
static int rtk_firmware_update(char *upload_data, int upload_len)
{
int head_offset=0 ;
int isIncludeRoot=0;
int len;
int numLeft;
int numWrite;
IMG_HEADER_Tp pHeader;
int fh;
int fwSizeLimit = 0x200000;
char buffer[100];
int flag=0;
while(head_offset < upload_len) {
pHeader = (IMG_HEADER_Tp) &upload_data[head_offset];
len = pHeader->len;
#ifdef _LITTLE_ENDIAN_
len = DWORD_SWAP(len);
#endif
numLeft = len + sizeof(IMG_HEADER_T) ;
// check header and checksum
if (!memcmp(&upload_data[head_offset], FW_HEADER, SIGNATURE_LEN) ||
!memcmp(&upload_data[head_offset], FW_HEADER_WITH_ROOT, SIGNATURE_LEN))
flag = 1;
else if (!memcmp(&upload_data[head_offset], WEB_HEADER, SIGNATURE_LEN))
flag = 2;
else if (!memcmp(&upload_data[head_offset], ROOT_HEADER, SIGNATURE_LEN)){
flag = 3;
isIncludeRoot = 1;
}
else {
strcpy(buffer, "Invalid file format!");
goto ret_upload;
}
if(len > fwSizeLimit){ //len check by sc_yang
sprintf(buffer, "Image len exceed max size 0x%x ! len=0x%x",fwSizeLimit, len);
goto ret_upload;
}
if ( (flag == 1) || (flag == 3)) {
if ( !fwChecksumOk(&upload_data[sizeof(IMG_HEADER_T)+head_offset], len)) {
sprintf(buffer, "Image checksum mismatched! len=0x%x, checksum=0x%x", len,
*((unsigned short *)&upload_data[len-2]) );
goto ret_upload;
}
}
else {
char *ptr = &upload_data[sizeof(IMG_HEADER_T)+head_offset];
if ( !CHECKSUM_OK(ptr, len) ) {
sprintf(buffer, "Image checksum mismatched! len=0x%x", len);
goto ret_upload;
}
}
//numWrite = write(fh, &(upload_data[locWrite+head_offset]), numLeft);
//inband write , and check ack
if(host_firmware_write(&(upload_data[head_offset]),numLeft) < 0 )
{
sprintf(buffer, "host_firmware_write! numLeft=0x%x", numLeft);
goto ret_upload;
}
head_offset += len + sizeof(IMG_HEADER_T) ;
}
return 0;
ret_upload:
fprintf(stderr, "%s\n", buffer);
return -1;
} //while //sc_yang
int cmd_firmware_upgrade(int index,int argc, char *argv[])
{
int len,ret,count;
//char data[1480];
char filename[50];
char *data;
int fd,rc;
struct stat ffstat;
int flen=0;
//sprintf(data,"%s",argv[2]);
sprintf(filename,"%s",argv[2]);
fd = open(filename, O_RDONLY);
if (fd < 0) {
printf("Cannot Open file %s!\n", filename);
return -1;
}
fstat(fd, &ffstat);
flen = ffstat.st_size;
printf("flen = %d \n",flen);
if((data = (char *)malloc(flen)) == NULL)
{
printf("data buffer allocation failed!\n");
return -1;
}
// rc = read(fd, data, 1480);
rc = read(fd, data, flen);
//if (rc != 1480) {
if (rc != flen) {
printf("Reading error\n");
free(data); //need free before return!!!!!
return -1;
}
close(fd);
ret = rtk_firmware_update(data, flen);
if(ret < 0)
printf("rtk_firmware_update fail \n");
else
printf("rtk_firmware_update ok \n");
free(data); //need free before return!!!!!
return ret;
}
#if 0
int cmd_sendconf(int index,int argc, char *argv[])
{
int len,ret,count;
//char data[1480];
char cmd_id,rx_cmd_type;
char *buf_p;
unsigned int tx_seq,rx_seq;
char filename[50];
char *data;
int fd,rc;
struct stat ffstat;
int flen=0;
cmd_id = id_set_mib;
//sprintf(data,"%s",argv[2]);
sprintf(filename,"%s",argv[2]);
fd = open(filename, O_RDONLY);
if (fd < 0) {
printf("Cannot Open file %s!\n", filename);
return -1;
}
fstat(fd, &ffstat);
flen = ffstat.st_size;
printf("flen = %d \n",flen);
if((data = (char *)malloc(flen)) == NULL)
{
printf("data buffer allocation failed!\n");
return -1;
}
// rc = read(fd, data, 1480);
rc = read(fd, data, flen);
//if (rc != 1480) {
if (rc != flen) {
printf("Reading error\n");
free(data); //need free before return!!!!!
return -1;
}
close(fd);
//len = 1480;
len = flen;
tx_seq = host_inband_seq;
ret = host_inband_write(cmd_id,data,len); //send request
free(data); //need free before return!!!!!
if(ret < 0)
return -1;
//count = inband_rcv_data(&rx_cmd_type,&buf_p,1000); //return data length
count = inband_rcv_data_and_seq(hcd_inband_chan,&rx_seq,&rx_cmd_type,&buf_p,3000); //return data length
if(count < 0)
{
ret=-1;
goto out;
}
if( (rx_cmd_type == cmd_id) && (tx_seq == rx_seq))
printf("remote write ok!\n");
else
{
ret=-1;
printf("remote write fail!\n");
}
out:
inband_free_buf(buf_p, count);
return ret;
}
#endif
int cmd_systemcall(int index,int argc, char *argv[])
{
int len,ret,count;
char data[1480];
char cmd_id,rx_cmd_type;
char *buf_p;
unsigned int tx_seq,rx_seq;
cmd_id = inband_cmd_table[index].cmd_id;
if(argc > 3)
sprintf(data,"%s %s",argv[2],argv[3]);
else
sprintf(data,"%s",argv[2]);
len = strlen(data);
ret = host_inband_write(cmd_id,data,len); //send request
tx_seq = host_inband_seq;
if(ret < 0)
return -1;
//count = inband_rcv_data(&rx_cmd_type,&buf_p,1000); //return data length
count = inband_rcv_data_and_seq(hcd_inband_chan,&rx_seq,&rx_cmd_type,&buf_p,1000); //return data length
if(count < 0)
return -1;
//printf("count=%d rx_cmd_type=%x,cmd_id=%x,tx_seq=%d,rx_seq=%d",count,rx_cmd_type,cmd_id,tx_seq,rx_seq);
if((rx_cmd_type == cmd_id) && (tx_seq == rx_seq))
{
if(inband_cmd_table[index].print_func)
inband_cmd_table[index].print_func(data,buf_p,count);
}
else
return -1;
return 0;
}
int main(int argc, char *argv[])
{
//int fdflags;
//unsigned int arg;
int i, ret;
int chan;
if (argc < 2) {
print_command_list();
return 0;
}
//open inband
chan = inband_open(HOST_NETIF,SLAVE_MAC,ETH_P_RTK,0);
if (chan < 0) {
printf("open inband failed!\n");
return -1;
}
hcd_inband_chan = chan;
i = 0;
while (inband_cmd_table[i].cmd != NULL) {
if (0 == strcmp(argv[1], inband_cmd_table[i].cmd)) {
if (inband_cmd_table[i].func) {
ret = inband_cmd_table[i].func(i,argc, argv);
if (ret > 0)
printf("OK\n");
else if (ret < 0)
printf("FAIL\n");
}
break;
}
i++;
}
inband_close(hcd_inband_chan);
return 0;
}
|