2 * Copyright 2010, 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;
51 struct carl9170fw_usb_desc *usb_desc = NULL;
59 fprintf(stderr, "Failed to open file %s (%d).\n",
65 err = fstat(fileno(m), &file_stat);
67 fprintf(stderr, "Failed to query file infos from "
68 "\"%s\" (%d).\n", mini, errno);
72 extra = file_stat.st_size;
74 otus_desc = carlfw_find_desc(fw, (uint8_t *) OTUS_MAGIC,
76 CARL9170FW_OTUS_DESC_CUR_VER);
78 fprintf(stderr, "No OTUS descriptor found\n");
82 usb_desc = carlfw_find_desc(fw, (uint8_t *) USB_MAGIC,
84 CARL9170FW_USB_DESC_CUR_VER);
86 fprintf(stderr, "Firmware is not for USB devices.\n");
90 if (carl9170fw_supports(usb_desc->usb_feature_set, CARL9170FW_USB_MINIBOOT)) {
91 fprintf(stderr, "Firmware has already a miniboot image.\n");
95 usb_desc->usb_feature_set |= cpu_to_le32(BIT(CARL9170FW_USB_MINIBOOT));
96 usb_desc->miniboot_size = cpu_to_le16(extra);
98 buf = carlfw_mod_headroom(fw, extra);
99 if (IS_ERR_OR_NULL(buf)) {
100 fprintf(stderr, "Unable to add miniboot image.\n");
104 err = fread(buf, extra, 1, m);
106 fprintf(stderr, "Unable to load miniboot.\n");
122 static int del_mini(struct carlfw *fw)
124 struct carl9170fw_usb_desc *usb_desc = NULL;
128 usb_desc = carlfw_find_desc(fw, (uint8_t *) USB_MAGIC,
130 CARL9170FW_USB_DESC_CUR_VER);
132 fprintf(stderr, "Firmware is not for USB devices.\n");
136 if (!carl9170fw_supports(usb_desc->usb_feature_set, CARL9170FW_USB_MINIBOOT)) {
137 fprintf(stderr, "Firmware has no miniboot image.\n");
141 cut = le16_to_cpu(usb_desc->miniboot_size);
143 buf = carlfw_mod_headroom(fw, -cut);
144 if (IS_ERR_OR_NULL(buf)) {
145 fprintf(stderr, "Unable to remove miniboot.\n");
149 usb_desc->usb_feature_set &= cpu_to_le32(~BIT(CARL9170FW_USB_MINIBOOT));
150 usb_desc->miniboot_size = cpu_to_le16(0);
156 int main(int argc, char *args[])
158 struct carlfw *fw = NULL;
161 if (argc < 3 || argc > 4) {
166 switch (args[1][0]) {
171 fw = carlfw_load(args[2]);
172 if (IS_ERR_OR_NULL(fw)) {
177 err = add_mini(fw, args[3]);
183 fw = carlfw_load(args[2]);
184 if (IS_ERR_OR_NULL(fw)) {
202 fprintf(stderr, "miniboot action failed (%d).\n", err);