firmware-utils/mktplinkfw: allow to specify firmware version
[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 #define ENC_PRODUCT_LEN         32
73 #define ENC_VERSION_LEN         8
74 #define ENC_MAGIC_LEN           6
75
76 unsigned long enc_compute_header_len(char *product, char *version);
77 unsigned long enc_compute_buf_len(char *product, char *version,
78                                   unsigned long datalen);
79
80 struct enc_param {
81         unsigned char *key;
82         unsigned char magic[ENC_MAGIC_LEN];
83         unsigned char product[ENC_PRODUCT_LEN];
84         unsigned char version[ENC_VERSION_LEN];
85         unsigned char seed;
86         int longstate;
87         unsigned datalen;
88         uint32_t csum;
89 };
90
91 int encrypt_buf(struct enc_param *ep, unsigned char *hdr,
92                 unsigned char *data);
93 int decrypt_buf(struct enc_param *ep, unsigned char *data,
94                 unsigned long datalen);
95
96 #define BCRYPT_DEFAULT_STATE_LEN        256
97 #define BCRYPT_MAX_KEYLEN               254
98
99 struct bcrypt_ctx {
100         unsigned long i;
101         unsigned long j;
102         unsigned char *state;
103         unsigned long state_len;
104 };
105
106 int bcrypt_init(struct bcrypt_ctx *ctx, void *key, int keylen,
107                 unsigned long state_len);
108 int bcrypt_process(struct bcrypt_ctx *ctx, unsigned char *src,
109                    unsigned char *dst, unsigned long len);
110 void bcrypt_finish(struct bcrypt_ctx *ctx);
111 int bcrypt_buf(unsigned char seed, unsigned char *key, unsigned char *src,
112                unsigned char *dst, unsigned long len, int longstate);
113
114 uint32_t buffalo_csum(uint32_t csum, void *buf, unsigned long len);
115 uint32_t buffalo_crc(void *buf, unsigned long len);
116
117 ssize_t get_file_size(char *name);
118 int read_file_to_buf(char *name, void *buf, ssize_t buflen);
119 int write_buf_to_file(char *name, void *buf, ssize_t buflen);
120
121 #endif /* _BUFFALO_LIB_H */