aboutsummaryrefslogtreecommitdiffstats
path: root/tools/firmware-utils/src/buffalo-lib.h
blob: 7eb9bf5398759e4215aec437a1d5ddff4a8e2504 (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
/*
 *  Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
 *
 *  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.
 *
 */

#ifndef _BUFFALO_LIB_H
#define _BUFFALO_LIB_H

#include <stdint.h>

#define ARRAY_SIZE(_a)	(sizeof((_a)) / sizeof((_a)[0]))
#define BIT(_x)		(1UL << (_x))

#define TAG_BRAND_LEN		32
#define TAG_PRODUCT_LEN		32
#define TAG_VERSION_LEN		8
#define TAG_REGION_LEN		2
#define TAG_LANGUAGE_LEN	8
#define TAG_PLATFORM_LEN	8
#define TAG_HWVER_LEN		4
#define TAG_HWVER_VAL_LEN	4

struct buffalo_tag {
	unsigned char	product[TAG_PRODUCT_LEN];
	unsigned char	brand[TAG_BRAND_LEN];
	unsigned char	ver_major[TAG_VERSION_LEN];
	unsigned char	ver_minor[TAG_VERSION_LEN];
	unsigned char	region_code[2];
	uint32_t	region_mask;
	unsigned char	unknown0[2];
	unsigned char	language[TAG_LANGUAGE_LEN];
	unsigned char	platform[TAG_PLATFORM_LEN];
	unsigned char	hwv[TAG_HWVER_LEN];
	unsigned char	hwv_val[TAG_HWVER_VAL_LEN];
	uint8_t		unknown1[24];

	uint32_t	len;
	uint32_t	crc;
	uint32_t	base1;
	uint32_t	base2;
	uint32_t	data_len;
	uint8_t		flag;
	uint8_t		unknown2[3];
} __attribute ((packed));

struct buffalo_tag2 {
	unsigned char	product[TAG_PRODUCT_LEN];
	unsigned char	brand[TAG_BRAND_LEN];
	unsigned char	ver_major[TAG_VERSION_LEN];
	unsigned char	ver_minor[TAG_VERSION_LEN];
	unsigned char	region_code[2];
	uint32_t	region_mask;
	unsigned char	unknown0[2];
	unsigned char	language[TAG_LANGUAGE_LEN];
	unsigned char	platform[TAG_PLATFORM_LEN];
	unsigned char	hwv[TAG_HWVER_LEN];
	unsigned char	hwv_val[TAG_HWVER_VAL_LEN];
	uint8_t		unknown1[24];

	uint32_t	total_len;
	uint32_t	crc;
	uint32_t	len1;
	uint32_t	len2;
	uint8_t		flag;
	uint8_t		unknown2[3];
} __attribute ((packed));

struct buffalo_tag3 {
	unsigned char	product[TAG_PRODUCT_LEN];
	unsigned char	brand[TAG_BRAND_LEN];
	unsigned char	ver_major[TAG_VERSION_LEN];
	unsigned char	ver_minor[TAG_VERSION_LEN];
	unsigned char	region_code[2];
	uint32_t	region_mask;
	unsigned char	unknown0[2];
	unsigned char	language[TAG_LANGUAGE_LEN];
	unsigned char	platform[TAG_PLATFORM_LEN];
	unsigned char	hwv[TAG_HWVER_LEN];
	unsigned char	hwv_val[TAG_HWVER_VAL_LEN];
	uint8_t		unknown1[24];

	uint32_t	total_len;
	uint32_t	crc;
	uint32_t	len1;
	uint32_t	base2;
} __attribute ((packed));

#define ENC_PRODUCT_LEN		32
#define ENC_VERSION_LEN		8
#define ENC_MAGIC_LEN		6

unsigned long enc_compute_header_len(char *product, char *version);
unsigned long enc_compute_buf_len(char *product, char *version,
				  unsigned long datalen);

struct enc_param {
	unsigned char *key;
	unsigned char magic[ENC_MAGIC_LEN];
	unsigned char product[ENC_PRODUCT_LEN];
	unsigned char version[ENC_VERSION_LEN];
	unsigned char seed;
	int longstate;
	unsigned datalen;
	uint32_t csum;
};

int encrypt_buf(struct enc_param *ep, unsigned char *hdr,
	        unsigned char *data);
int decrypt_buf(struct enc_param *ep, unsigned char *data,
		unsigned long datalen);

#define BCRYPT_DEFAULT_STATE_LEN	256
#define BCRYPT_MAX_KEYLEN		254

struct bcrypt_ctx {
	unsigned long i;
	unsigned long j;
	unsigned char *state;
	unsigned long state_len;
};

int bcrypt_init(struct bcrypt_ctx *ctx, void *key, int keylen,
		unsigned long state_len);
int bcrypt_process(struct bcrypt_ctx *ctx, unsigned char *src,
		   unsigned char *dst, unsigned long len);
void bcrypt_finish(struct bcrypt_ctx *ctx);
int bcrypt_buf(unsigned char seed, unsigned char *key, unsigned char *src,
	       unsigned char *dst, unsigned long len, int longstate);

uint32_t buffalo_csum(uint32_t csum, void *buf, unsigned long len);
uint32_t buffalo_crc(void *buf, unsigned long len);

ssize_t get_file_size(char *name);
int read_file_to_buf(char *name, void *buf, ssize_t buflen);
int write_buf_to_file(char *name, void *buf, ssize_t buflen);

#endif /* _BUFFALO_LIB_H */