ath79: add support for TP-Link Archer C6 v2
[oweals/openwrt.git] / tools / firmware-utils / src / buffalo-lib.h
1 /*
2  *  Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify it
5  *  under the terms of the GNU General Public License version 2 as published
6  *  by the Free Software Foundation.
7  *
8  */
9
10 #ifndef _BUFFALO_LIB_H
11 #define _BUFFALO_LIB_H
12
13 #include <stdint.h>
14
15 #define ARRAY_SIZE(_a)  (sizeof((_a)) / sizeof((_a)[0]))
16 #define BIT(_x)         (1UL << (_x))
17
18 #define TAG_BRAND_LEN           32
19 #define TAG_PRODUCT_LEN         32
20 #define TAG_VERSION_LEN         8
21 #define TAG_REGION_LEN          2
22 #define TAG_LANGUAGE_LEN        8
23 #define TAG_PLATFORM_LEN        8
24 #define TAG_HWVER_LEN           4
25 #define TAG_HWVER_VAL_LEN       4
26
27 struct buffalo_tag {
28         unsigned char   product[TAG_PRODUCT_LEN];
29         unsigned char   brand[TAG_BRAND_LEN];
30         unsigned char   ver_major[TAG_VERSION_LEN];
31         unsigned char   ver_minor[TAG_VERSION_LEN];
32         unsigned char   region_code[2];
33         uint32_t        region_mask;
34         unsigned char   unknown0[2];
35         unsigned char   language[TAG_LANGUAGE_LEN];
36         unsigned char   platform[TAG_PLATFORM_LEN];
37         unsigned char   hwv[TAG_HWVER_LEN];
38         unsigned char   hwv_val[TAG_HWVER_VAL_LEN];
39         uint8_t         unknown1[24];
40
41         uint32_t        len;
42         uint32_t        crc;
43         uint32_t        base1;
44         uint32_t        base2;
45         uint32_t        data_len;
46         uint8_t         flag;
47         uint8_t         unknown2[3];
48 } __attribute ((packed));
49
50 struct buffalo_tag2 {
51         unsigned char   product[TAG_PRODUCT_LEN];
52         unsigned char   brand[TAG_BRAND_LEN];
53         unsigned char   ver_major[TAG_VERSION_LEN];
54         unsigned char   ver_minor[TAG_VERSION_LEN];
55         unsigned char   region_code[2];
56         uint32_t        region_mask;
57         unsigned char   unknown0[2];
58         unsigned char   language[TAG_LANGUAGE_LEN];
59         unsigned char   platform[TAG_PLATFORM_LEN];
60         unsigned char   hwv[TAG_HWVER_LEN];
61         unsigned char   hwv_val[TAG_HWVER_VAL_LEN];
62         uint8_t         unknown1[24];
63
64         uint32_t        total_len;
65         uint32_t        crc;
66         uint32_t        len1;
67         uint32_t        len2;
68         uint8_t         flag;
69         uint8_t         unknown2[3];
70 } __attribute ((packed));
71
72 struct buffalo_tag3 {
73         unsigned char   product[TAG_PRODUCT_LEN];
74         unsigned char   brand[TAG_BRAND_LEN];
75         unsigned char   ver_major[TAG_VERSION_LEN];
76         unsigned char   ver_minor[TAG_VERSION_LEN];
77         unsigned char   region_code[2];
78         uint32_t        region_mask;
79         unsigned char   unknown0[2];
80         unsigned char   language[TAG_LANGUAGE_LEN];
81         unsigned char   platform[TAG_PLATFORM_LEN];
82         unsigned char   hwv[TAG_HWVER_LEN];
83         unsigned char   hwv_val[TAG_HWVER_VAL_LEN];
84         uint8_t         unknown1[24];
85
86         uint32_t        total_len;
87         uint32_t        crc;
88         uint32_t        len1;
89         uint32_t        base2;
90 } __attribute ((packed));
91
92 #define ENC_PRODUCT_LEN         32
93 #define ENC_VERSION_LEN         8
94 #define ENC_MAGIC_LEN           6
95
96 unsigned long enc_compute_header_len(char *product, char *version);
97 unsigned long enc_compute_buf_len(char *product, char *version,
98                                   unsigned long datalen);
99
100 struct enc_param {
101         unsigned char *key;
102         unsigned char magic[ENC_MAGIC_LEN];
103         unsigned char product[ENC_PRODUCT_LEN];
104         unsigned char version[ENC_VERSION_LEN];
105         unsigned char seed;
106         int longstate;
107         unsigned datalen;
108         uint32_t csum;
109 };
110
111 int encrypt_buf(struct enc_param *ep, unsigned char *hdr,
112                 unsigned char *data);
113 int decrypt_buf(struct enc_param *ep, unsigned char *data,
114                 unsigned long datalen);
115
116 #define BCRYPT_DEFAULT_STATE_LEN        256
117 #define BCRYPT_MAX_KEYLEN               254
118
119 struct bcrypt_ctx {
120         unsigned long i;
121         unsigned long j;
122         unsigned char *state;
123         unsigned long state_len;
124 };
125
126 int bcrypt_init(struct bcrypt_ctx *ctx, void *key, int keylen,
127                 unsigned long state_len);
128 int bcrypt_process(struct bcrypt_ctx *ctx, unsigned char *src,
129                    unsigned char *dst, unsigned long len);
130 void bcrypt_finish(struct bcrypt_ctx *ctx);
131 int bcrypt_buf(unsigned char seed, unsigned char *key, unsigned char *src,
132                unsigned char *dst, unsigned long len, int longstate);
133
134 uint32_t buffalo_csum(uint32_t csum, void *buf, unsigned long len);
135 uint32_t buffalo_crc(void *buf, unsigned long len);
136
137 ssize_t get_file_size(char *name);
138 int read_file_to_buf(char *name, void *buf, ssize_t buflen);
139 int write_buf_to_file(char *name, void *buf, ssize_t buflen);
140
141 #endif /* _BUFFALO_LIB_H */