Merge branch 'master' into radar
[carl9170fw.git] / tools / src / fwinfo.c
1 /*
2  * Copyright 2010-2011 Christian Lamparter <chunkeey@googlemail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <error.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <ctype.h>
27
28 #include "carlfw.h"
29
30 #include "fwcmd.h"
31 #include "compiler.h"
32
33 struct feature_list {
34         unsigned int id;
35         char name[64];
36         void (*func)(const struct carl9170fw_desc_head *, struct carlfw *fw);
37 };
38
39 #define CHECK_FOR_FEATURE(feature_enum)                                 \
40         { .id = feature_enum, .name = #feature_enum, .func = NULL }
41
42 #define CHECK_FOR_FEATURE_FUNC(feature_enum, _func)                     \
43         { .id = feature_enum, .name = #feature_enum, .func = _func }
44
45 static void show_miniboot_info(const struct carl9170fw_desc_head *head,
46                                struct carlfw *fw __unused)
47 {
48         const struct carl9170fw_otus_desc *otus = (const void *) head;
49
50         fprintf(stdout, "\t\t\tminiboot size: %d Bytes\n", otus->miniboot_size);
51 }
52
53 static const struct feature_list known_otus_features_v1[] = {
54         CHECK_FOR_FEATURE(CARL9170FW_DUMMY_FEATURE),
55         CHECK_FOR_FEATURE_FUNC(CARL9170FW_MINIBOOT, show_miniboot_info),
56         CHECK_FOR_FEATURE(CARL9170FW_USB_INIT_FIRMWARE),
57         CHECK_FOR_FEATURE(CARL9170FW_USB_RESP_EP2),
58         CHECK_FOR_FEATURE(CARL9170FW_USB_DOWN_STREAM),
59         CHECK_FOR_FEATURE(CARL9170FW_USB_UP_STREAM),
60         CHECK_FOR_FEATURE(CARL9170FW_UNUSABLE),
61         CHECK_FOR_FEATURE(CARL9170FW_COMMAND_PHY),
62         CHECK_FOR_FEATURE(CARL9170FW_COMMAND_CAM),
63         CHECK_FOR_FEATURE(CARL9170FW_WLANTX_CAB),
64         CHECK_FOR_FEATURE(CARL9170FW_HANDLE_BACK_REQ),
65         CHECK_FOR_FEATURE(CARL9170FW_GPIO_INTERRUPT),
66         CHECK_FOR_FEATURE(CARL9170FW_PSM),
67         CHECK_FOR_FEATURE(CARL9170FW_RX_FILTER),
68         CHECK_FOR_FEATURE(CARL9170FW_WOL),
69         CHECK_FOR_FEATURE(CARL9170FW_FIXED_5GHZ_PSM),
70         CHECK_FOR_FEATURE(CARL9170FW_HW_COUNTERS),
71         CHECK_FOR_FEATURE(CARL9170FW_RX_BA_FILTER),
72         CHECK_FOR_FEATURE(CARL9170FW_HAS_WREGB_CMD),
73         CHECK_FOR_FEATURE(CARL9170FW_PATTERN_GENERATOR),
74 };
75
76 static void check_feature_list(const struct carl9170fw_desc_head *head,
77                                const __le32 bitmap,
78                                const struct feature_list *list,
79                                const unsigned int entries,
80                                struct carlfw *fw)
81 {
82         unsigned int i;
83
84         for (i = 0; i < entries; i++) {
85                 if (!carl9170fw_supports(bitmap, list[i].id))
86                         continue;
87
88                 fprintf(stdout, "\t\t%2d = %s\n", list[i].id, list[i].name);
89                 if (list[i].func)
90                         list[i].func(head, fw);
91         }
92 }
93
94 static void show_otus_desc(const struct carl9170fw_desc_head *head,
95                            struct carlfw *fw)
96 {
97         const struct carl9170fw_otus_desc *otus = (const void *) head;
98
99         BUILD_BUG_ON(ARRAY_SIZE(known_otus_features_v1) != __CARL9170FW_FEATURE_NUM);
100
101         fprintf(stdout, "\tFirmware upload pointer: 0x%x\n",
102                 otus->fw_address);
103         fprintf(stdout, "\tBeacon Address: %x, (reserved:%d Bytes)\n",
104                 le32_to_cpu(otus->bcn_addr), le16_to_cpu(otus->bcn_len));
105         fprintf(stdout, "\tTX DMA chunk size:%d Bytes, TX DMA chunks:%d\n",
106                 otus->tx_frag_len, otus->tx_descs);
107         fprintf(stdout, "\t=> %d Bytes are reserved for the TX queues\n",
108                 otus->tx_frag_len * otus->tx_descs);
109         fprintf(stdout, "\tCommand response buffers:%d\n", otus->cmd_bufs);
110         fprintf(stdout, "\tMax. RX stream block size:%d Bytes\n",
111                 otus->rx_max_frame_len);
112         fprintf(stdout, "\tSupported Firmware Interfaces: %d\n", otus->vif_num);
113         fprintf(stdout, "\tFirmware API Version: %d\n", otus->api_ver);
114         fprintf(stdout, "\tSupported Features: (raw:%.08x)\n",
115                 le32_to_cpu(otus->feature_set));
116
117         check_feature_list(head, otus->feature_set, known_otus_features_v1,
118                            ARRAY_SIZE(known_otus_features_v1), fw);
119 }
120
121 static void show_motd_desc(const struct carl9170fw_desc_head *head,
122                            struct carlfw *fw __unused)
123 {
124         const struct carl9170fw_motd_desc *motd = (const void *) head;
125         char buf[CARL9170FW_MOTD_STRING_LEN];
126         unsigned int fw_date;
127
128         fw_date = motd->fw_year_month_day;
129         fprintf(stdout, "\tFirmware Build Date (YYYY-MM-DD): 2%03d-%02d-%02d\n",
130                 CARL9170FW_GET_YEAR(fw_date), CARL9170FW_GET_MONTH(fw_date),
131                 CARL9170FW_GET_DAY(fw_date));
132
133         strncpy(buf, motd->desc, CARL9170FW_MOTD_STRING_LEN);
134         fprintf(stdout, "\tFirmware Text:\"%s\"\n", buf);
135
136         strncpy(buf, motd->release, CARL9170FW_MOTD_STRING_LEN);
137         fprintf(stdout, "\tFirmware Release:\"%s\"\n", buf);
138 }
139
140 static void show_fix_desc(const struct carl9170fw_desc_head *head,
141                           struct carlfw *fw __unused)
142 {
143         const struct carl9170fw_fix_desc *fix = (const void *) head;
144         const struct carl9170fw_fix_entry *iter;
145         unsigned int i;
146
147         for (i = 0; i < (head->length - sizeof(*head)) / sizeof(*iter); i++) {
148                 iter = &fix->data[i];
149                 fprintf(stdout, "\t\t%d: 0x%.8x := 0x%.8x (0x%.8x)\n", i,
150                         le32_to_cpu(iter->address), le32_to_cpu(iter->value),
151                         le32_to_cpu(iter->mask));
152         }
153 }
154
155 static void show_dbg_desc(const struct carl9170fw_desc_head *head,
156                             struct carlfw *fw __unused)
157 {
158         const struct carl9170fw_dbg_desc *dbg = (const void *) head;
159
160 #define DBG_ADDR(_name, _reg) do {                                      \
161         unsigned int __tmp = le32_to_cpu(dbg->_reg);                    \
162         if (__tmp)                                                      \
163                 fprintf(stdout, "\t\t" _name " = 0x%.8x\n", __tmp);     \
164         } while (0);
165
166         fprintf(stdout, "\tFirmware Debug Registers/Counters\n");
167         DBG_ADDR("bogoclock    ", bogoclock_addr);
168         DBG_ADDR("counter      ", counter_addr);
169         DBG_ADDR("rx total     ", rx_total_addr);
170         DBG_ADDR("rx overrun   ", rx_overrun_addr);
171         DBG_ADDR("rx filer     ", rx_filter);
172 }
173
174 static void show_txsq_desc(const struct carl9170fw_desc_head *head,
175                            struct carlfw *fw __unused)
176 {
177         const struct carl9170fw_txsq_desc *txsq = (const void *) head;
178
179         fprintf(stdout, "\t\ttx-seq table addr: 0x%x\n",
180                 le32_to_cpu(txsq->seq_table_addr));
181 }
182
183
184 static const struct feature_list wol_triggers_v1[] = {
185         CHECK_FOR_FEATURE(CARL9170_WOL_DISCONNECT),
186         CHECK_FOR_FEATURE(CARL9170_WOL_MAGIC_PKT),
187 };
188
189 static void show_wol_desc(const struct carl9170fw_desc_head *head,
190                           struct carlfw *fw __unused)
191 {
192         const struct carl9170fw_wol_desc *wol = (const void *) head;
193
194         fprintf(stdout, "\tSupported WOWLAN triggers: (raw:%.08x)\n",
195                 le32_to_cpu(wol->supported_triggers));
196
197         check_feature_list(head, wol->supported_triggers, wol_triggers_v1,
198                            ARRAY_SIZE(wol_triggers_v1), fw);
199 }
200
201 static void show_chk_desc(const struct carl9170fw_desc_head *head,
202                           struct carlfw *fw __unused)
203 {
204         const struct carl9170fw_chk_desc *chk = (const void *) head;
205
206         fprintf(stdout, "\tFirmware Descriptor CRC32: %08x\n",
207                 le32_to_cpu(chk->hdr_crc32));
208         fprintf(stdout, "\tFirmware Image CRC32: %08x\n",
209                 le32_to_cpu(chk->fw_crc32));
210 }
211
212 static void show_pattern_desc(const struct carl9170fw_desc_head *head,
213                           struct carlfw *fw __unused)
214 {
215         const struct carl9170fw_pattern_desc *pattern = (const void *) head;
216         const struct carl9170fw_pattern_map_entry *map = pattern->patterns;
217         int map_entries = (head->length - sizeof(*pattern)) / sizeof(*map);
218         int i;
219
220         fprintf(stdout, "\tPattern index register: %08x\n",
221                 le32_to_cpu(pattern->soft_pattern));
222         fprintf(stdout, "\tNumber of supported patterns: %08x\n",
223                 le32_to_cpu(pattern->num_patterns));
224
225         for (i = 0; i < map_entries; i++) {
226                 fprintf(stdout, "\t\tindex:0x%x, description:%s\n",
227                         map[i].index, map[i].name);
228         }
229 }
230
231 static void show_last_desc(const struct carl9170fw_desc_head *head,
232                            struct carlfw *fw __unused)
233
234 {
235         const struct carl9170fw_last_desc *last __unused = (const void *) head;
236
237         /* Nothing here */
238 }
239
240 #define ADD_HANDLER(_magic, _func)                              \
241         {                                                       \
242           .magic = _magic##_MAGIC,                              \
243           .min_ver = CARL9170FW_## _magic##_DESC_CUR_VER,       \
244           .func = _func,                                        \
245           .size = CARL9170FW_## _magic##_DESC_SIZE,             \
246         }
247
248 static const struct {
249         uint8_t magic[4];
250         uint8_t min_ver;
251         void (*func)(const struct carl9170fw_desc_head *, struct carlfw *);
252         uint16_t size;
253 } known_magics[] = {
254         ADD_HANDLER(OTUS, show_otus_desc),
255         ADD_HANDLER(TXSQ, show_txsq_desc),
256         ADD_HANDLER(MOTD, show_motd_desc),
257         ADD_HANDLER(DBG, show_dbg_desc),
258         ADD_HANDLER(FIX, show_fix_desc),
259         ADD_HANDLER(CHK, show_chk_desc),
260         ADD_HANDLER(WOL, show_wol_desc),
261         ADD_HANDLER(PATTERN, show_pattern_desc),
262         ADD_HANDLER(LAST, show_last_desc),
263 };
264
265 static const uint8_t otus_magic[4] = { OTUS_MAGIC };
266
267 static void show_desc_head(struct carl9170fw_desc_head *head)
268 {
269 #define P(c) (isprint(c) ? c :  ' ')
270
271         fprintf(stdout, ">\t%c%c%c%c Descriptor: size:%d, compatible:%d, "
272                         "version:%d\n",
273                 P(head->magic[0]), P(head->magic[1]), P(head->magic[2]),
274                 P(head->magic[3]), le16_to_cpu(head->length), head->min_ver,
275                 head->cur_ver);
276 }
277
278 static void fwinfo_info(void)
279 {
280         fprintf(stderr, "Usage:\n");
281         fprintf(stderr, "\tfwinfo FW-FILE\n");
282
283         fprintf(stderr, "\nDescription:\n");
284         fprintf(stderr, "\tDisplay firmware descriptors information in "
285                         "a human readable form.\n");
286
287         fprintf(stderr, "\nParameteres:\n");
288         fprintf(stderr, "\t 'FW-FILE'   = firmware file/base-name\n\n");
289 }
290
291 int main(int argc, char *args[])
292 {
293         struct carlfw *fw = NULL;
294         struct carl9170fw_desc_head *fw_desc;
295         unsigned int i;
296         int err = 0;
297         size_t len;
298
299         if (argc != 2) {
300                 err = -EINVAL;
301                 goto out;
302         }
303
304         fw = carlfw_load(args[1]);
305         if (IS_ERR_OR_NULL(fw)) {
306                 err = PTR_ERR(fw);
307                 fprintf(stderr, "Failed to open firmware \"%s\" (%d).\n",
308                         args[1], err);
309                 goto out;
310         }
311
312         carlfw_get_fw(fw, &len);
313         fprintf(stdout, "General Firmware Statistics:\n");
314         fprintf(stdout, "\tFirmware file size: %u Bytes\n", (unsigned int)len);
315         fprintf(stdout, "\t%d Descriptors in %d Bytes\n",
316                 carlfw_get_descs_num(fw), carlfw_get_descs_size(fw));
317
318         fw_desc = NULL;
319         fprintf(stdout, "\nDetailed Descriptor Description:\n");
320         while ((fw_desc = carlfw_desc_next(fw, fw_desc))) {
321                 show_desc_head(fw_desc);
322
323                 for (i = 0; i < ARRAY_SIZE(known_magics); i++) {
324                         if (carl9170fw_desc_cmp(fw_desc, known_magics[i].magic,
325                             known_magics[i].size, known_magics[i].min_ver)) {
326                                 known_magics[i].func(fw_desc, fw);
327                                 break;
328                         }
329                 }
330
331                 if (i == ARRAY_SIZE(known_magics))
332                         fprintf(stderr, "Unknown Descriptor.\n");
333
334                 fprintf(stdout, "\n");
335         }
336
337 out:
338         switch (err) {
339         case 0:
340                 break;
341
342         case -EINVAL:
343                 fwinfo_info();
344                 break;
345
346         default:
347                 fprintf(stderr, "%s\n", strerror(-err));
348                 break;
349         }
350
351         carlfw_release(fw);
352         return err ? EXIT_FAILURE : EXIT_SUCCESS;
353 }