ar71xx: wpj588: add missing usb support
[openwrt.git] / target / linux / ar71xx / files / arch / mips / ath79 / routerboot.c
1 /*
2  *  RouterBoot helper routines
3  *
4  *  Copyright (C) 2012 Gabor Juhos <juhosg@openwrt.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify it
7  *  under the terms of the GNU General Public License version 2 as published
8  *  by the Free Software Foundation.
9  */
10
11 #define pr_fmt(fmt) "rb: " fmt
12
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/errno.h>
16 #include <linux/routerboot.h>
17 #include <linux/rle.h>
18
19 #include "routerboot.h"
20
21 #define RB_BLOCK_SIZE           0x1000
22 #define RB_ART_SIZE             0x10000
23
24 static struct rb_info rb_info;
25
26 static u32 get_u32(void *buf)
27 {
28         u8 *p = buf;
29
30         return ((u32) p[3] + ((u32) p[2] << 8) + ((u32) p[1] << 16) +
31                ((u32) p[0] << 24));
32 }
33
34 static u16 get_u16(void *buf)
35 {
36         u8 *p = buf;
37
38         return (u16) p[1] + ((u16) p[0] << 8);
39 }
40
41 __init int
42 routerboot_find_magic(u8 *buf, unsigned int buflen, u32 *offset, bool hard)
43 {
44         u32 magic_ref = hard ? RB_MAGIC_HARD : RB_MAGIC_SOFT;
45         u32 magic;
46         u32 cur = *offset;
47
48         while (cur < buflen) {
49                 magic = get_u32(buf + cur);
50                 if (magic == magic_ref) {
51                         *offset = cur;
52                         return 0;
53                 }
54
55                 cur += 0x1000;
56         }
57
58         return -ENOENT;
59 }
60
61 __init int
62 routerboot_find_tag(u8 *buf, unsigned int buflen, u16 tag_id,
63                     u8 **tag_data, u16 *tag_len)
64 {
65         uint32_t magic;
66         int ret;
67
68         if (buflen < 4)
69                 return -EINVAL;
70
71         magic = get_u32(buf);
72         switch (magic) {
73         case RB_MAGIC_HARD:
74                 /* skip magic value */
75                 buf += 4;
76                 buflen -= 4;
77                 break;
78
79         case RB_MAGIC_SOFT:
80                 if (buflen < 8)
81                         return -EINVAL;
82
83                 /* skip magic and CRC value */
84                 buf += 8;
85                 buflen -= 8;
86
87                 break;
88
89         default:
90                 return -EINVAL;
91         }
92
93         ret = -ENOENT;
94         while (buflen > 2) {
95                 u16 id;
96                 u16 len;
97
98                 len = get_u16(buf);
99                 buf += 2;
100                 buflen -= 2;
101
102                 if (buflen < 2)
103                         break;
104
105                 id = get_u16(buf);
106                 buf += 2;
107                 buflen -= 2;
108
109                 if (id == RB_ID_TERMINATOR)
110                         break;
111
112                 if (buflen < len)
113                         break;
114
115                 if (id == tag_id) {
116                         if (tag_len)
117                                 *tag_len = len;
118                         if (tag_data)
119                                 *tag_data = buf;
120                         ret = 0;
121                         break;
122                 }
123
124                 buf += len;
125                 buflen -= len;
126         }
127
128         return ret;
129 }
130
131 static inline int
132 rb_find_hard_cfg_tag(u16 tag_id, u8 **tag_data, u16 *tag_len)
133 {
134         if (!rb_info.hard_cfg_data ||
135             !rb_info.hard_cfg_size)
136                 return -ENOENT;
137
138         return routerboot_find_tag(rb_info.hard_cfg_data,
139                                    rb_info.hard_cfg_size,
140                                    tag_id, tag_data, tag_len);
141 }
142
143 __init const char *
144 rb_get_board_name(void)
145 {
146         u16 tag_len;
147         u8 *tag;
148         int err;
149
150         err = rb_find_hard_cfg_tag(RB_ID_BOARD_NAME, &tag, &tag_len);
151         if (err)
152                 return NULL;
153
154         return tag;
155 }
156
157 __init u32
158 rb_get_hw_options(void)
159 {
160         u16 tag_len;
161         u8 *tag;
162         int err;
163
164         err = rb_find_hard_cfg_tag(RB_ID_HW_OPTIONS, &tag, &tag_len);
165         if (err)
166                 return 0;
167
168         return get_u32(tag);
169 }
170
171 __init void *
172 rb_get_wlan_data(void)
173 {
174         u16 tag_len;
175         u8 *tag;
176         void *buf;
177         int err;
178
179         err = rb_find_hard_cfg_tag(RB_ID_WLAN_DATA, &tag, &tag_len);
180         if (err) {
181                 pr_err("no calibration data found\n");
182                 goto err;
183         }
184
185         buf = kmalloc(RB_ART_SIZE, GFP_KERNEL);
186         if (buf == NULL) {
187                 pr_err("no memory for calibration data\n");
188                 goto err;
189         }
190
191         err = rle_decode((char *) tag, tag_len, buf, RB_ART_SIZE,
192                          NULL, NULL);
193         if (err) {
194                 pr_err("unable to decode calibration data\n");
195                 goto err_free;
196         }
197
198         return buf;
199
200 err_free:
201         kfree(buf);
202 err:
203         return NULL;
204 }
205
206 __init const struct rb_info *
207 rb_init_info(void *data, unsigned int size)
208 {
209         unsigned int offset;
210
211         if (size == 0 || (size % RB_BLOCK_SIZE) != 0)
212                 return NULL;
213
214         for (offset = 0; offset < size; offset += RB_BLOCK_SIZE) {
215                 u32 magic;
216
217                 magic = get_u32(data + offset);
218                 switch (magic) {
219                 case RB_MAGIC_HARD:
220                         rb_info.hard_cfg_offs = offset;
221                         break;
222
223                 case RB_MAGIC_SOFT:
224                         rb_info.soft_cfg_offs = offset;
225                         break;
226                 }
227         }
228
229         if (!rb_info.hard_cfg_offs) {
230                 pr_err("could not find a valid RouterBOOT hard config\n");
231                 return NULL;
232         }
233
234         if (!rb_info.soft_cfg_offs) {
235                 pr_err("could not find a valid RouterBOOT soft config\n");
236                 return NULL;
237         }
238
239         rb_info.hard_cfg_size = RB_BLOCK_SIZE;
240         rb_info.hard_cfg_data = kmemdup(data + rb_info.hard_cfg_offs,
241                                         RB_BLOCK_SIZE, GFP_KERNEL);
242         if (!rb_info.hard_cfg_data)
243                 return NULL;
244
245         rb_info.board_name = rb_get_board_name();
246         rb_info.hw_options = rb_get_hw_options();
247
248         return &rb_info;
249 }