2 * Copyright 2010-2011 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>
31 static void mini_help(void)
33 fprintf(stderr, "Usage:\n");
34 fprintf(stderr, "\tminiboot ACTION FW-FILE [MB-FILE]\n");
36 fprintf(stderr, "\nDescription:\n");
37 fprintf(stderr, "\tFirmware concatenation utility.\n");
39 fprintf(stderr, "\nParameteres:\n");
40 fprintf(stderr, "\t'ACTION' = [a|d]\n");
41 fprintf(stderr, "\t | 'a' = Add miniboot firmware.\n");
42 fprintf(stderr, "\t * 'd' = remove miniboot firmware.\n");
43 fprintf(stderr, "\t'FW-FILE' = destination for the package.\n");
44 fprintf(stderr, "\t'MB-FILE' = extra firmware image.\n");
47 static int add_mini(struct carlfw *fw, const char *mini)
49 struct stat file_stat;
50 struct carl9170fw_otus_desc *otus_desc = NULL;
58 fprintf(stderr, "Failed to open file %s (%d).\n",
64 err = fstat(fileno(m), &file_stat);
66 fprintf(stderr, "Failed to query file infos from "
67 "\"%s\" (%d).\n", mini, errno);
71 extra = file_stat.st_size;
73 otus_desc = carlfw_find_desc(fw, (uint8_t *) OTUS_MAGIC,
75 CARL9170FW_OTUS_DESC_CUR_VER);
77 fprintf(stderr, "No OTUS descriptor found\n");
81 if (carl9170fw_supports(otus_desc->feature_set, CARL9170FW_MINIBOOT)) {
82 fprintf(stderr, "Firmware has already a miniboot image.\n");
86 otus_desc->feature_set |= cpu_to_le32(BIT(CARL9170FW_MINIBOOT));
87 otus_desc->miniboot_size = cpu_to_le16(extra);
89 buf = carlfw_mod_headroom(fw, extra);
90 if (IS_ERR_OR_NULL(buf)) {
91 fprintf(stderr, "Unable to add miniboot image.\n");
95 err = fread(buf, extra, 1, m);
97 fprintf(stderr, "Unable to load miniboot.\n");
113 static int del_mini(struct carlfw *fw)
115 struct carl9170fw_otus_desc *otus_desc = NULL;
119 otus_desc = carlfw_find_desc(fw, (uint8_t *) OTUS_MAGIC,
121 CARL9170FW_OTUS_DESC_CUR_VER);
123 fprintf(stderr, "Firmware is not for USB devices.\n");
127 if (!carl9170fw_supports(otus_desc->feature_set, CARL9170FW_MINIBOOT)) {
128 fprintf(stderr, "Firmware has no miniboot image.\n");
132 cut = le16_to_cpu(otus_desc->miniboot_size);
134 buf = carlfw_mod_headroom(fw, -cut);
135 if (IS_ERR_OR_NULL(buf)) {
136 fprintf(stderr, "Unable to remove miniboot.\n");
140 otus_desc->feature_set &= cpu_to_le32(~BIT(CARL9170FW_MINIBOOT));
141 otus_desc->miniboot_size = cpu_to_le16(0);
147 int main(int argc, char *args[])
149 struct carlfw *fw = NULL;
152 if (argc < 3 || argc > 4) {
157 switch (args[1][0]) {
162 fw = carlfw_load(args[2]);
163 if (IS_ERR_OR_NULL(fw)) {
168 err = add_mini(fw, args[3]);
174 fw = carlfw_load(args[2]);
175 if (IS_ERR_OR_NULL(fw)) {
193 fprintf(stderr, "miniboot action failed (%d).\n", err);