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