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