2 * Copyright 2012 Christian Lamparter <chunkeey@googlemail.com>
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.
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.
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.
23 #include <sys/types.h>
32 static void checksum_help(void)
34 fprintf(stderr, "Usage:\n");
35 fprintf(stderr, "\tfwprepare FW-FILE\n");
37 fprintf(stderr, "\nDescription:\n");
38 fprintf(stderr, "\tThis simple utility prepares the firmware "
41 fprintf(stderr, "\nParameteres:\n");
42 fprintf(stderr, "\t 'FW-FILE' = firmware name\n");
43 fprintf(stderr, "\n");
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;
51 otus_desc = carlfw_find_desc(fw, (uint8_t *) OTUS_MAGIC,
53 CARL9170FW_OTUS_DESC_CUR_VER);
55 fprintf(stderr, "No OTUS descriptor found\n");
59 if (!carl9170fw_supports(otus_desc->feature_set, CARL9170FW_PATTERN_GENERATOR)) {
63 pattern_desc = carlfw_find_desc(fw, (uint8_t *) PATTERN_MAGIC,
64 sizeof(*pattern_desc),
65 CARL9170FW_PATTERN_DESC_CUR_VER);
68 fprintf(stderr, "Firmware has the pattern generator feature set, but "
69 "can't find a valid pattern descriptor\n");
73 to_add = pattern_desc->num_patterns -
74 ((pattern_desc->head.length - sizeof(*pattern_desc)) /
75 sizeof(struct carl9170fw_pattern_map_entry));
77 /* been there, done that */
81 if (to_add == __CARL9170FW_NUM_PATTERNS) {
82 struct carl9170fw_pattern_desc *tmp;
83 unsigned int len, map_len;
85 map_len = sizeof(struct carl9170fw_pattern_map_entry) * to_add;
86 len = sizeof(*tmp) + map_len;
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);
95 memcpy(&pattern_desc->patterns, pattern_names, map_len);
98 fprintf(stderr, "No idea, what you just did. But congrats: you broke it!");
103 static int add_checksums(struct carlfw __unused *fw)
106 * No magic here, The checksum descriptor is added/update
107 * automatically in a subroutine of carlfw_store().
112 int main(int argc, char *args[])
114 struct carlfw *fw = NULL;
122 fw = carlfw_load(args[1]);
123 if (IS_ERR_OR_NULL(fw)) {
125 fprintf(stderr, "Failed to open file \"%s\" (%d).\n",
130 err = add_patterns(fw);
134 err = add_checksums(fw);
138 err = carlfw_store(fw);
140 fprintf(stderr, "Failed to apply checksum (%d).\n", err);
147 fprintf(stdout, "firmware was prepared successfully.\n");
157 return err ? EXIT_FAILURE : EXIT_SUCCESS;