carl9170 firmware: add radar pattern generator
[carl9170fw.git] / tools / src / fwprepare.c
1 /*
2  * Copyright 2012 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
27 #include "carlfw.h"
28
29 #include "compiler.h"
30 #include "pattern.h"
31
32 static void checksum_help(void)
33 {
34         fprintf(stderr, "Usage:\n");
35         fprintf(stderr, "\tfwprepare FW-FILE\n");
36
37         fprintf(stderr, "\nDescription:\n");
38         fprintf(stderr, "\tThis simple utility prepares the firmware "
39                         "for release.\n");
40
41         fprintf(stderr, "\nParameteres:\n");
42         fprintf(stderr, "\t 'FW-FILE'   = firmware name\n");
43         fprintf(stderr, "\n");
44 }
45
46 static int add_patterns(struct carlfw *fw) {
47         const struct carl9170fw_otus_desc *otus_desc = NULL;
48         struct carl9170fw_pattern_desc *pattern_desc = NULL;
49         int to_add;
50
51         otus_desc = carlfw_find_desc(fw, (uint8_t *) OTUS_MAGIC,
52                                      sizeof(*otus_desc),
53                                      CARL9170FW_OTUS_DESC_CUR_VER);
54         if (!otus_desc) {
55                 fprintf(stderr, "No OTUS descriptor found\n");
56                 return -1;
57         }
58
59         if (!carl9170fw_supports(otus_desc->feature_set, CARL9170FW_PATTERN_GENERATOR)) {
60                 return 0;
61         }
62
63         pattern_desc = carlfw_find_desc(fw, (uint8_t *) PATTERN_MAGIC,
64                                       sizeof(*pattern_desc),
65                                       CARL9170FW_PATTERN_DESC_CUR_VER);
66
67         if (!pattern_desc) {
68                 fprintf(stderr, "Firmware has the pattern generator feature set, but "
69                         "can't find a valid pattern descriptor\n");
70                 return 0;
71         }
72
73         to_add = pattern_desc->num_patterns -
74                  ((pattern_desc->head.length - sizeof(*pattern_desc)) /
75                  sizeof(struct carl9170fw_pattern_map_entry));
76         if (to_add == 0) {
77                 /* been there, done that */
78                 return 0;
79         }
80
81         if (to_add == __CARL9170FW_NUM_PATTERNS) {
82                 struct carl9170fw_pattern_desc *tmp;
83                 unsigned int len, map_len;
84
85                 map_len = sizeof(struct carl9170fw_pattern_map_entry) * to_add;
86                 len = sizeof(*tmp) + map_len;
87                 tmp = malloc(len);
88                 if (!tmp)
89                         return -ENOMEM;
90
91                 pattern_desc = carlfw_desc_mod_len(fw, &pattern_desc->head, map_len);
92                 if (IS_ERR_OR_NULL(pattern_desc))
93                         return (int) PTR_ERR(pattern_desc);
94
95                 memcpy(&pattern_desc->patterns, pattern_names, map_len);
96                 return 0;
97         } else {
98                 fprintf(stderr, "No idea, what you just did. But congrats: you broke it!");
99                 return -EINVAL;
100         }
101 }
102
103 static int add_checksums(struct carlfw __unused *fw)
104 {
105         /*
106          * No magic here, The checksum descriptor is added/update
107          * automatically in a subroutine of carlfw_store().
108          */
109         return 0;
110 }
111
112 int main(int argc, char *args[])
113 {
114         struct carlfw *fw = NULL;
115         int err = 0;
116
117         if (argc != 2) {
118                 err = -EINVAL;
119                 goto out;
120         }
121
122         fw = carlfw_load(args[1]);
123         if (IS_ERR_OR_NULL(fw)) {
124                 err = PTR_ERR(fw);
125                 fprintf(stderr, "Failed to open file \"%s\" (%d).\n",
126                         args[1], err);
127                 goto out;
128         }
129
130         err = add_patterns(fw);
131         if (err)
132                 goto out;
133
134         err = add_checksums(fw);
135         if (err)
136                 goto out;
137
138         err = carlfw_store(fw);
139         if (err) {
140                 fprintf(stderr, "Failed to apply checksum (%d).\n", err);
141                 goto out;
142         }
143
144 out:
145         switch (err) {
146         case 0:
147                 fprintf(stdout, "firmware was prepared successfully.\n");
148                 break;
149         case -EINVAL:
150                 checksum_help();
151                 break;
152         default:
153                 break;
154         }
155
156         carlfw_release(fw);
157         return err ? EXIT_FAILURE : EXIT_SUCCESS;
158 }