aboutsummaryrefslogtreecommitdiffstats
path: root/package/utils/rbextract/src/rbextract.c
blob: 0985497f3ffcc264003704dcfb2ffc450353dc83 (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
/*
 *  RouterBoot helper routines
 *
 *  Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
 *  Copyright (C) 2018 Chris Schimp <silverchris@gmail.com>
 *  Copyright (C) 2019 Robert Marko <robimarko@gmail.com>
 *  
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 */

#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <endian.h>
#include <arpa/inet.h>

#include <lzo/lzo1x.h>

#include "rle.h"
#include "routerboot.h"

inline uint32_t
get_u32(const void *buf)
{
#if __BYTE_ORDER == __LITTLE_ENDIAN
	return *(uint32_t *)buf;
#elif __BYTE_ORDER == __BIG_ENDIAN
	const uint8_t *p = buf;
	return ((uint32_t) p[3] + ((uint32_t) p[2] << 8) +
	       ((uint32_t) p[1] << 16) + ((uint32_t) p[0] << 24));
#else
#error "Unknown byte order!"
#endif
}

int
routerboot_find_tag(uint8_t *buf, unsigned int buflen, uint16_t tag_id,
		    uint8_t **tag_data, uint16_t *tag_len)
{
	uint16_t id;
	uint16_t len;
	uint32_t magic;
	bool align = false;
	int ret;

	if (buflen < 4)
		return 1;

	magic = get_u32(buf);

	switch (magic) {
	case RB_MAGIC_LZOR:
		buf += 4;
		buflen -= 4;
		break;
	case RB_MAGIC_ERD:
		align = true;
		/* fall trough */
	case RB_MAGIC_HARD:
		/* skip magic value */
		buf += 4;
		buflen -= 4;
		break;

	case RB_MAGIC_SOFT:
		if (buflen < 8)
			return 1;

		/* skip magic and CRC value */
		buf += 8;
		buflen -= 8;

		break;

	default:
		return 1;
	}

	ret = 1;
	while (buflen > 4){
		uint32_t id_and_len = get_u32(buf);
		buf += 4;
		buflen -= 4;
		id = id_and_len & 0xFFFF;
		len = id_and_len >> 16;

		if (align)
			len += (4 - len % 4) % 4;

		if (id == RB_ID_TERMINATOR) {
			break;
		}

		if (buflen < len)
			break;

		if (id == tag_id) {
			*tag_len = len;
			*tag_data = buf;
			ret = 0;
			break;
		}

		buf += len;
		buflen -= len;
	}

	return ret;
}

inline int
rb_find_hard_cfg_tag(uint16_t tag_id, uint8_t **tag_data, uint16_t *tag_len)
{
	if (!rb_hardconfig ||
	    !rb_hardconfig_len)
		return 1;

	return routerboot_find_tag(rb_hardconfig,
				   rb_hardconfig_len,
				   tag_id, tag_data, tag_len);
}

const uint8_t *
rb_get_board_product_code(void)
{
	uint16_t tag_len;
	uint8_t *tag;
	int err;

	err = rb_find_hard_cfg_tag(RB_ID_BOARD_PRODUCT_CODE, &tag, &tag_len);
	if (err)
		return NULL;

	return tag;
}

uint32_t
rb_get_board_mac(void)
{
	uint16_t tag_len;
	uint8_t *tag;
	int err;

	err = rb_find_hard_cfg_tag(RB_ID_MAC_ADDRESS_PACK, &tag, &tag_len);
	if (err)
		return 0;

	return htonl(get_u32(tag));
}

const uint8_t *
rb_get_board_serial(void)
{
	uint16_t tag_len;
	uint8_t *tag;
	int err;

	err = rb_find_hard_cfg_tag(RB_ID_SERIAL_NUMBER, &tag, &tag_len);
	if (err)
		return NULL;

	return tag;
}

const uint8_t *
rb_get_board_identifier(void)
{
	uint16_t tag_len;
	uint8_t *tag;
	int err;

	err = rb_find_hard_cfg_tag(RB_ID_BOARD_IDENTIFIER, &tag, &tag_len);
	if (err)
		return NULL;

	return tag;
}

const uint8_t *
rb_get_board_name(void)
{
	uint16_t tag_len;
	uint8_t *tag;
	int err;

	err = rb_find_hard_cfg_tag(RB_ID_BOARD_NAME, &tag, &tag_len);
	if (err)
		return NULL;

	return tag;
}

const uint8_t *
rb_get_factory_booter_version(void)
{
	uint16_t tag_len;
	uint8_t *tag;
	int err;

	err = rb_find_hard_cfg_tag(RB_ID_BIOS_VERSION, &tag, &tag_len);
	if (err)
		return NULL;

	return tag;
}

uint32_t
rb_get_flash_info(void)
{
	uint16_t tag_len;
	uint8_t *tag;
	int err;

	err = rb_find_hard_cfg_tag(RB_ID_FLASH_INFO, &tag, &tag_len);
	if (err)
		return 0;

	return htonl(get_u32(tag));
}

uint32_t
rb_get_hw_options(void)
{
	uint16_t tag_len;
	uint8_t *tag;
	int err;

	err = rb_find_hard_cfg_tag(RB_ID_HW_OPTIONS, &tag, &tag_len);
	if (err)
		return 0;

	return htonl(get_u32(tag));
}

uint8_t * 
__rb_get_wlan_data(void)
{
	uint16_t tag_len;
	uint8_t *tag;
	uint16_t erd_tag_len;
	uint8_t *erd_tag;
	uint8_t *buf_lzo_in;
	uint8_t *buf_lzo_out;
	uint8_t *buf_rle_out;
	int err;
	uint32_t magic;
	uint32_t erd_magic;
	uint32_t erd_offset;
	size_t lzo_out_len;

	err = rb_find_hard_cfg_tag(RB_ID_WLAN_DATA, &tag, &tag_len);
	if (err) {
		printf("no calibration data found\n");
		goto err;
	}

	buf_lzo_in = malloc(RB_ART_SIZE);
	if (buf_lzo_in == NULL) {
		printf("no memory for calibration data\n");
		goto err;
	}

	buf_rle_out = malloc(RB_ART_SIZE);
	if (buf_rle_out == NULL) {
		printf("no memory for calibration data\n");
		goto err_free_lzo_out;
	}

	buf_lzo_out = malloc(RB_ART_SIZE);
	if (buf_lzo_out == NULL) {
		printf("no memory for calibration data\n");
		goto err_free_lzo_in;
	}

	magic = get_u32(tag);
	if (magic == RB_MAGIC_LZOR) {
		tag += 4;
		tag_len -= 4;
		if (tag_len + sizeof(lzo_prefix) > RB_ART_SIZE) {
			printf("Calibration data too large\n");
			goto err_free_lzo_in;
		}
		printf("Copying fixed LZO prefix (size: %d)\n", sizeof(lzo_prefix));
		memcpy(buf_lzo_in, lzo_prefix, sizeof(lzo_prefix));

		printf("Copying input data (size: %d)\n", tag_len);
		memcpy(buf_lzo_in + sizeof(lzo_prefix), tag, tag_len);

		printf("Decompressing with LZO\n");
		lzo_out_len = RB_ART_SIZE;
		err = lzo1x_decompress_safe(buf_lzo_in, tag_len + sizeof(lzo_prefix),
					    buf_lzo_out, &lzo_out_len, NULL);
		/* For some reason, I get this "input not consumed" error
		 * even though the output is correct, so ignore it. */
		if (err && err != LZO_E_INPUT_NOT_CONSUMED) {
			printf("unable to decompress calibration data: %d\n",
			       err);
			goto err_free_lzo_out;
		}

		printf("Looking for ERD data in decompressed output\n");
		erd_magic = 0;
		for (erd_offset = 0; erd_offset < lzo_out_len; erd_offset++) {
			erd_magic = get_u32(buf_lzo_out + erd_offset);
			if (erd_magic == RB_MAGIC_ERD)
				break;
		}
		if (erd_magic != RB_MAGIC_ERD) {
			printf("no ERD data found\n");
			goto err_free_lzo_out;
		}
		printf("Found ERD magic at offset %d\n", erd_offset);

		err = routerboot_find_tag(buf_lzo_out + erd_offset,
					  lzo_out_len - erd_offset,
					  0x1, &erd_tag, &erd_tag_len);
		if (err) {
			printf("No ERD chunk found\n");
			goto err_free_lzo_out;
		}

		printf("Decompress ERD data with RLE\n");
		err = rle_decode(erd_tag, erd_tag_len, buf_rle_out, RB_ART_SIZE,
				 NULL, NULL);
		if (err) {
			printf("unable to decode ERD data\n");
			goto err_free_rle_out;
		}
	}

	return buf_rle_out;

err_free_rle_out:
	free(buf_rle_out);
err_free_lzo_out:
	free(buf_lzo_out);
err_free_lzo_in:
	free(buf_lzo_in);
err:
	return NULL;
}

int
main(int argc, char **argv)
{
	FILE    *infile;
	FILE	*outfile;
	uint8_t *buf;
	uint32_t magic;
	uint32_t i;
	
	if(argc < 2){
		printf("Not enough arguments\n");
		printf("Use -h for help\n");
		exit(1);
	}

	if(strcmp(argv[1], "-h") == 0){
		printf("This program can extract various data from MikroTik devices hard_config partition\n");
		printf("Usage: rbextract <options> <hard_config_location> <output_file> (Optional)\n");
		printf("Options:\n");
		printf("-a Prints all possible info\n");
		printf("-n Prints board name\n");
		printf("-p Prints board product code\n");
		printf("-i Prints board identifier\n");
		printf("-s Prints board serial number\n");
		printf("-m Prints board MAC\n");
		printf("-o Prints board HW options\n");
		printf("-r Prints board RouterBoot factory version\n");
		printf("-f Prints board flash identifier\n");
		printf("-e Extract board radio calibration\n");
		printf("hard_config_location: Path to hard_config partiton\n");
		printf("output_file: Path to where caldata will be output\n");
	} else {
		infile = fopen(argv[2], "r");

		if(infile == NULL){
			printf("Cant open given path\n");
    		return 1;
    	}

		fseek(infile, 0L, SEEK_END);
		rb_hardconfig_len = ftell(infile);
	
		fseek(infile, 0L, SEEK_SET);
	
		rb_hardconfig = (uint8_t*)calloc(rb_hardconfig_len, sizeof(uint8_t));	
		if(rb_hardconfig == NULL)
			return 1;

		fread(rb_hardconfig, sizeof(uint8_t), rb_hardconfig_len, infile);
		fclose(infile);

		magic = get_u32(rb_hardconfig);
		if(magic != RB_MAGIC_HARD){
			printf("Routerboot Hard Config not found\n");
			exit(1);
		}

		if(strcmp(argv[1], "-a") == 0){
			printf("Board name: %s\n", rb_get_board_name());
			printf("Board product code: %s\n", rb_get_board_product_code());
			printf("Board identifier: %s\n", rb_get_board_identifier());
			printf("Board serial: %s\n", rb_get_board_serial());
			printf("Board MAC: %X\n", rb_get_board_mac());
			printf("HW Options %x\n", rb_get_hw_options());
			printf("Factory RouterBoot version: %s\n", rb_get_factory_booter_version());
			printf("Flash identifier: %x\n", rb_get_flash_info());
		} else if(strcmp(argv[1], "-n") == 0){
			printf("%s\n", rb_get_board_name());
		} else if(strcmp(argv[1], "-p") == 0){
			printf("%s\n", rb_get_board_product_code());
		} else if(strcmp(argv[1], "-i") == 0){
			printf("%s\n", rb_get_board_identifier());
		} else if(strcmp(argv[1], "-s") == 0){
			printf("%s\n", rb_get_board_serial());
		} else if(strcmp(argv[1], "-m") == 0){
			printf("%x\n", rb_get_board_mac());
		} else if(strcmp(argv[1], "-o") == 0){
			printf("%x\n", rb_get_hw_options());
		} else if(strcmp(argv[1], "-r") == 0){
			printf("%s\n", rb_get_factory_booter_version());
		} else if(strcmp(argv[1], "-f") == 0){
			printf("%x\n", rb_get_flash_info());
		} else if(strcmp(argv[1], "-e") == 0){
			buf = __rb_get_wlan_data();
			if (buf == NULL) {
				printf("Could not extract calibration data\n");
				return 1;
			}

			if(argv[3] == NULL){
				printf("Missing output file argument\n");
				return 1;
			}
	
			outfile = fopen(argv[3], "wb");
			if(outfile == NULL){
				printf("Cant open given path\n");
    			return 1;
    		}

			/* Write 65536 bytes of caldata */
			for(i = 0; i<RB_ART_SIZE; i++){
				fwrite(&buf[i], sizeof(uint8_t), sizeof(uint8_t), outfile);
			}
			fclose(outfile);
		}
	}
	exit(0);
}