tools/firmware-utils: add buffalo specific fw tools
[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 #define ENC_PRODUCT_LEN         32
51 #define ENC_VERSION_LEN         8
52 #define ENC_MAGIC_LEN           6
53
54 unsigned long enc_compute_header_len(char *product, char *version);
55 unsigned long enc_compute_buf_len(char *product, char *version,
56                                   unsigned long datalen);
57
58 struct enc_param {
59         unsigned char *key;
60         unsigned char magic[ENC_MAGIC_LEN];
61         unsigned char product[ENC_PRODUCT_LEN];
62         unsigned char version[ENC_VERSION_LEN];
63         unsigned char seed;
64         int longstate;
65         unsigned datalen;
66         uint32_t csum;
67 };
68
69 int encrypt_buf(struct enc_param *ep, unsigned char *hdr,
70                 unsigned char *data);
71 int decrypt_buf(struct enc_param *ep, unsigned char *data,
72                 unsigned long datalen);
73
74 #define BCRYPT_DEFAULT_STATE_LEN        256
75 #define BCRYPT_MAX_KEYLEN               254
76
77 struct bcrypt_ctx {
78         unsigned long i;
79         unsigned long j;
80         unsigned char *state;
81         unsigned long state_len;
82 };
83
84 int bcrypt_init(struct bcrypt_ctx *ctx, void *key, int keylen,
85                 unsigned long state_len);
86 int bcrypt_process(struct bcrypt_ctx *ctx, unsigned char *src,
87                    unsigned char *dst, unsigned long len);
88 void bcrypt_finish(struct bcrypt_ctx *ctx);
89 int bcrypt_buf(unsigned char seed, unsigned char *key, unsigned char *src,
90                unsigned char *dst, unsigned long len, int longstate);
91
92 uint32_t buffalo_csum(uint32_t csum, void *buf, unsigned long len);
93 uint32_t buffalo_crc(void *buf, unsigned long len);
94
95 ssize_t get_file_size(char *name);
96 int read_file_to_buf(char *name, void *buf, ssize_t buflen);
97 int write_buf_to_file(char *name, void *buf, ssize_t buflen);
98
99 #endif /* _BUFFALO_LIB_H */