summaryrefslogtreecommitdiffstats
path: root/rtkmib.c
blob: f49bae82bba6600e37aa75f7eda8a0f5ef5cad56 (plain)
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
#include <getopt.h>

#include "rtkmib.h"

#define NAME		"rtkmib"
#define VERSION		"0.0.1"


static const char *opt_string = ":i:o:h";
static struct option long_options[] = {
	{ "input", required_argument, NULL, 'i' },
	{ "output", required_argument, NULL, 'o' },
	{ "help", no_argument, NULL, 'h' },
	{ 0, 0, 0, 0 },
};

void usage ( char *pname ) {
	char **dp;
	char *optdoc[] = {
		"\n",
		"   Options:\n",
		"   -i, --input            input file name\n",
		"   -o, --output           output file name\n",
		"   -h, --help             print this help message\n",
		"\n",
		"If you find bugs, cockroaches or other nasty insects don't\n",
		"send them to roman@advem.lv - just kill 'em! ;)\n",
		"\n",
		0
	};
	printf( "\n%s v%s\n", NAME, VERSION );
	printf( "Usage: %s [OPTIONS]\n", pname );
	for (dp = optdoc; *dp; dp++) {
		printf( "%s", *dp );
	}
}

static int flash_read( char *mtd, int offset, int len, char *buf )
{
	if ( !buf || !mtd || len < 0 )
		return -1;

	int err = 0;
	int fd = open( mtd, O_RDONLY );

	if ( fd < 0 ) {
		printf( "Flash read error: %m\n" );
		return fd;
	}

	if ( offset > 0 )
		lseek( fd, offset, SEEK_SET );

	if ( read( fd, buf, len ) != len )
		err = -1;

	close( fd );

	return err;
}

inline uint16_t swap16( uint16_t x )
{
	uint16_t b = x;
	b = (x >> 8) & 0xff;
	return b | (x << 8);
}

inline uint32_t swap32( uint32_t x )
{
	return x = (x >> 24) |
		((x << 8) & 0x00ff0000) |
		((x >> 8) & 0x0000ff00) |
		(x << 24);
}

#define RING_SIZE       4096    /* size of ring buffer, must be power of 2 */
#define UL_MATCH        18      /* upper limit for match_length */
#define THRESHOLD       2       /* encode string into position and length
                                 * if match_length is greater than this */

static int mib_decode( unsigned char *in, uint32_t len, unsigned char **out )
{
	if ( !in || !out || len < 1 )
		return -1;

	int  i, j, k, c;
	int r = RING_SIZE - UL_MATCH;
	unsigned int flags = 0;
	unsigned int pos = 0;
	unsigned int explen = 0;

	unsigned char *text_buf =
			(unsigned char *)malloc( RING_SIZE + UL_MATCH - 1 );
	if ( !text_buf )
		return -1;

	*out = (unsigned char *)malloc( len );
	if ( !*out ) {
		free(text_buf);
		return -1;
	}

	/* original code initializes text_buf with spaces */
	memset( text_buf, ' ', r );
	memset( *out, 0, len );

	while (1) {
		if ( ((flags >>= 1) & 0x100) == 0 ) {
			if ( pos++ > len )
				break;
			c = *in++;		/* get flags for frame */
			flags = c | 0xff00;	/* uses higher byte cleverly */
		}				/* to count eight */
		/* test next flag bit */
		if ( flags & 1 ) {
			/* flag bit of 1 means unencoded byte */
			if ( pos++ > len )
				break;
			c = *in++;
			if ( explen + 1 > len )
				*out = (unsigned char *)realloc( *out, explen + 1 );
			(*out)[ explen ] = c;	/* copy to output */
			//printf("%i: %x\n", explen, c); fflush(stdout);
			explen++;
			text_buf[ r++ ] = c;	/* and to text_buf */
			r &= (RING_SIZE - 1);
		} else {
			/* 0 means encoded info */
			if ( pos++ > len )
				break;
			i = *in++;		/* get position */
			if ( pos++ > len )
				break;
			j = *in++;		/* get length of run */

			i |= ((j & 0xf0) << 4);	    /* i is now offset of run */
			j = (j & 0x0f) + THRESHOLD; /* j is the length */

			for ( k = 0; k <= j; k++ ) {
				c = text_buf[ (i + k) & (RING_SIZE - 1) ];
				if ( explen + 1 > len )
					*out = (unsigned char *)realloc( (void *)*out, explen + 1 );
				(*out)[ explen ] = c;
				//printf("c%i: %x\n", explen, c); fflush(stdout);
				explen++;
				text_buf[ r++ ] = c;
				r &= (RING_SIZE - 1);
			}
		}
	}

	free(text_buf);
	return explen;
}

static int mib_read( char *mtd, unsigned int offset,
			unsigned char **mib, uint32_t *size )
{
	*mib = NULL;
	mib_hdr_t header;
	mib_hdr_compr_t header_compr;
	unsigned int len = 0;
	int compression = 0;
	unsigned char *sig = NULL;

	if ( flash_read( mtd, offset,
			 sizeof(mib_hdr_t), (char *)&header ) )
	{
		syslog( LOG_ERR, "probe header failed\n" );
		return MIB_ERR_GENERIC;
	}

	if ( !memcmp( MIB_HEADER_COMPR_TAG,
		      header.sig,
		      sizeof(header.sig) ) )
	{
		printf( "MIB is compressed!\n" );
		if ( flash_read( mtd, offset, sizeof(mib_hdr_compr_t),
						(char *)&header_compr ) )
		{
			syslog( LOG_ERR, "read header failed\n" );
			return MIB_ERR_GENERIC;
		}
		sig = header_compr.sig;
		len = swap32(header_compr.len);
		compression = swap16(header_compr.factor);
	} else {
		sig = header.sig;
		len = swap16(header.len);
	}

	printf( "Header info:\n" );
	printf( "  signature: '%s'\n", sig );
	printf( "  data size: 0x%x\n", len );

	*mib = (unsigned char *)malloc(len);

	if ( compression ) {
		printf( "  compression factor: 0x%x\n", compression );
		*size = len;
		if ( flash_read( mtd, offset + sizeof(mib_hdr_compr_t),
				 len, (char *)*mib ) )
		{
			syslog( LOG_ERR, "MIB read failed\n" );
			return MIB_ERR_GENERIC;
		}
		return MIB_ERR_COMPRESSED;
	}

	if ( flash_read( mtd, offset + sizeof(mib_hdr_t),
			 len, (char *)*mib ) )
	{
		syslog( LOG_ERR, "MIB read failed\n" );
		return MIB_ERR_GENERIC;
	}

	return len;
}

static int hex_to_string( unsigned char *hex, char *str, int len )
{
	int i;
	unsigned char *d, *s;
	const static char hexdig[] = "0123456789abcdef";

	if ( hex == NULL || str == NULL )
		return -1;

	d = (unsigned char *)str;
	s = hex;

	for ( i = 0; i < len; i++,s++ ) {
		*d++ = hexdig[(*s >> 4) & 0xf];
		*d++ = hexdig[*s & 0xf];
	}

	*d = 0;
	return 0;
}

#ifdef HAVE_RTK_AC_SUPPORT

#define B1_G1	40
#define B1_G2	48

#define B2_G1	56
#define B2_G2	64

#define B3_G1	104
#define B3_G2	112
#define B3_G3	120
#define B3_G4	128
#define B3_G5	136
#define B3_G6	144

#define B4_G1	153
#define B4_G2	161
#define B4_G3	169
#define B4_G4	177

void assign_diff_AC(unsigned char* pMib, unsigned char* pVal)
{
	int x=0, y=0;

	memset((pMib+35), pVal[0], (B1_G1-35));
	memset((pMib+B1_G1), pVal[1], (B1_G2-B1_G1));
	memset((pMib+B1_G2), pVal[2], (B2_G1-B1_G2));
	memset((pMib+B2_G1), pVal[3], (B2_G2-B2_G1));
	memset((pMib+B2_G2), pVal[4], (B3_G1-B2_G2));
	memset((pMib+B3_G1), pVal[5], (B3_G2-B3_G1));
	memset((pMib+B3_G2), pVal[6], (B3_G3-B3_G2));
	memset((pMib+B3_G3), pVal[7], (B3_G4-B3_G3));
	memset((pMib+B3_G4), pVal[8], (B3_G5-B3_G4));
	memset((pMib+B3_G5), pVal[9], (B3_G6-B3_G5));
	memset((pMib+B3_G6), pVal[10], (B4_G1-B3_G6));
	memset((pMib+B4_G1), pVal[11], (B4_G2-B4_G1));
	memset((pMib+B4_G2), pVal[12], (B4_G3-B4_G2));
	memset((pMib+B4_G3), pVal[13], (B4_G4-B4_G3));

}

void assign_diff_AC_hex_to_string(unsigned char* pmib,char* str,int len)
{
	char mib_buf[MAX_5G_CHANNEL_NUM_MIB];
	memset(mib_buf,0,sizeof(mib_buf));
	assign_diff_AC(mib_buf, pmib);
	hex_to_string(mib_buf,str,MAX_5G_CHANNEL_NUM_MIB);
}
#endif /* HAVE_RTK_AC_SUPPORT */

int set_tx_calibration( mib_wlan_t *phw, char *interface )
{
	char tmpbuff[1024], p[ MAX_5G_CHANNEL_NUM_MIB * 2 + 1 ];
	if(!phw)
		return -1;

	hex_to_string(phw->pwrlevelCCK_A,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrlevelCCK_A=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrlevelCCK_B,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrlevelCCK_B=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrlevelHT40_1S_A,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrlevelHT40_1S_A=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrlevelHT40_1S_B,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrlevelHT40_1S_B=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrdiffHT40_2S,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiffHT40_2S=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrdiffHT20,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiffHT20=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrdiffOFDM,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiffOFDM=%s",interface,p);
//	system(tmpbuff);

	hex_to_string(phw->pwrlevel5GHT40_1S_A,p,MAX_5G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrlevel5GHT40_1S_A=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrlevel5GHT40_1S_B,p,MAX_5G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrlevel5GHT40_1S_B=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

#ifdef HAVE_RTK_92D_SUPPORT
	hex_to_string(phw->pwrdiff5GHT40_2S,p,MAX_5G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff5GHT40_2S=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrdiff5GHT20,p,MAX_5G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff5GHT20=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrdiff5GOFDM,p,MAX_5G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff5GOFDM=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);
#endif /* HAVE_RTK_92D_SUPPORT */

#ifdef HAVE_RTK_AC_SUPPORT /* 8812 */
	hex_to_string(phw->pwrdiff_20BW1S_OFDM1T_A,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_20BW1S_OFDM1T_A=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrdiff_40BW2S_20BW2S_A,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_40BW2S_20BW2S_A=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	assign_diff_AC_hex_to_string(phw->pwrdiff_5G_20BW1S_OFDM1T_A,p,MAX_5G_DIFF_NUM);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_5G_20BW1S_OFDM1T_A=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	assign_diff_AC_hex_to_string(phw->pwrdiff_5G_40BW2S_20BW2S_A,p,MAX_5G_DIFF_NUM);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_5G_40BW2S_20BW2S_A=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	assign_diff_AC_hex_to_string(phw->pwrdiff_5G_80BW1S_160BW1S_A,p,MAX_5G_DIFF_NUM);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_5G_80BW1S_160BW1S_A=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	assign_diff_AC_hex_to_string(phw->pwrdiff_5G_80BW2S_160BW2S_A,p,MAX_5G_DIFF_NUM);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_5G_80BW2S_160BW2S_A=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrdiff_20BW1S_OFDM1T_B,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_20BW1S_OFDM1T_B=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	hex_to_string(phw->pwrdiff_40BW2S_20BW2S_B,p,MAX_2G_CHANNEL_NUM_MIB);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_40BW2S_20BW2S_B=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	assign_diff_AC_hex_to_string(phw->pwrdiff_5G_20BW1S_OFDM1T_B,p,MAX_5G_DIFF_NUM);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_5G_20BW1S_OFDM1T_B=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	assign_diff_AC_hex_to_string(phw->pwrdiff_5G_40BW2S_20BW2S_B,p,MAX_5G_DIFF_NUM);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_5G_40BW2S_20BW2S_B=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	assign_diff_AC_hex_to_string(phw->pwrdiff_5G_80BW1S_160BW1S_B,p,MAX_5G_DIFF_NUM);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_5G_80BW1S_160BW1S_B=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);

	assign_diff_AC_hex_to_string(phw->pwrdiff_5G_80BW2S_160BW2S_B,p,MAX_5G_DIFF_NUM);
	sprintf(tmpbuff,"iwpriv %s set_mib pwrdiff_5G_80BW2S_160BW2S_B=%s",interface,p);
//	system(tmpbuff);
	printf("%s\n",tmpbuff);
#endif /* HAVE_RTK_AC_SUPPORT */

	return 0;
}

int main( int argc, char **argv )
{
	int efuse = 0; /* HAVE_RTK_EFUSE */

	char infile[ 255 ] = "";
	char outfile[ 255 ] = "";
	unsigned int mib_offset = MIB_OFFSET;

	int opt;
	int option_index = 0;
	while ( (opt = getopt_long( argc, argv,
					opt_string, long_options,
					&option_index )) != -1 )
	{
		switch( opt ) {
		case 'i':
			snprintf( infile, sizeof infile, "%s", optarg );
			break;
		case 'o':
			mib_offset = (unsigned int)atoi(optarg);
			break;
		case 'O':
			snprintf( outfile, sizeof outfile, "%s", optarg );
			break;
		case 'h':
			usage(argv[0]);
			exit(EXIT_SUCCESS);
		case '?':
		default:
			printf( "%s: option -%c is invalid\n", argv[0], optopt );
			exit(EXIT_FAILURE);
		}
	}

	if ( strlen(infile) < 1 )
		snprintf( infile, sizeof infile, "%s", FLASH_DEVICE_NAME );

	unsigned char *buf = NULL;
	unsigned char *mib = NULL;
	unsigned char *tmp = NULL;
	int mib_len = 0;
	uint32_t compr_size = 0;
	mib_wlan_t *mib_wlan;

	if ( efuse ) {
		syslog( LOG_WARNING,"Efuse enabled. Nothing to do!\n" );
		exit(EXIT_SUCCESS);
	}

	mib_len = mib_read( infile, mib_offset,	&buf, &compr_size );

	if ( mib_len == MIB_ERR_GENERIC )
		goto exit;

	if ( mib_len == MIB_ERR_COMPRESSED ) {
		printf("Compressed size: %i\n", compr_size);
		mib_len = mib_decode( buf, compr_size, &tmp );

		mib_hdr_t *header = (mib_hdr_t *)tmp;
		printf("Header signature: '%s' (%x)\n", header->sig, ((char *)header)[0]);
		printf("Length from header: 0x%x\n", swap16(header->len));
		printf("Decoded length: 0x%x\n", mib_len);

		mib = tmp + sizeof(mib_hdr_t);
	} else {
		mib = buf;
	}

	//printf( "MIB len: %i\n", mib_len );
	printf( "Expected mininum len: 0x%x\n", (int)sizeof(mib_t) );

	if ( mib_len < (int)sizeof(mib_t) ) {
		syslog( LOG_ERR, "MIB length invalid!\n" );
		goto exit;
	}


	printf("board version: 0x%02x\n", mib[0]);
	printf("nic0: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[1], mib[2], mib[3],
				mib[4], mib[5], mib[6] );
	printf("nic1: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[7], mib[8], mib[9],
				mib[10], mib[11], mib[12] );
	printf("wlan0: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[13], mib[14], mib[15],
				mib[16], mib[17], mib[18] );
	printf("wlan1: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[19], mib[20], mib[21],
				mib[22], mib[23], mib[24] );
	printf("wlan2: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[25], mib[26], mib[27],
				mib[28], mib[29], mib[30] );
	printf("wlan3: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[31], mib[32], mib[33],
				mib[34], mib[35], mib[36] );
	printf("wlan4: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[37], mib[38], mib[39],
				mib[40], mib[41], mib[42] );
	printf("wlan5: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[43], mib[44], mib[45],
				mib[46], mib[47], mib[48] );
	printf("wlan6: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[49], mib[50], mib[51],
				mib[52], mib[53], mib[54] );
	printf("wlan7: %02x:%02x:%02x:%02x:%02x:%02x\n",
				mib[55], mib[56], mib[57],
				mib[58], mib[59], mib[60] );

	mib_wlan = (mib_wlan_t *)( mib + MIB_WLAN_OFFSET );
	set_tx_calibration( mib_wlan, "wlan0" );

#ifdef HAVE_RTK_DUAL_BAND_SUPPORT
	mib_wlan = (mib_wlan_t *)( mib + MIB_WLAN_OFFSET + sizeof(mib_wlan_t) );
	set_tx_calibration( mib_wlan, "wlan1" );
#endif
exit:
	free(buf);
	if ( mib != buf )
		free(tmp);

	exit(EXIT_SUCCESS);
}