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>
36 struct carlfw_file fw;
37 struct carlfw_file hdr;
39 struct list_head desc_list;
40 unsigned int desc_list_entries,
44 #define carlfw_walk_descs(iter, fw) \
45 list_for_each_entry(iter, &fw->desc_list, h.list)
47 struct carlfw_list_entry_head {
48 struct list_head list;
51 struct carlfw_list_entry {
52 struct carlfw_list_entry_head h;
54 struct carl9170fw_desc_head head;
60 static inline struct carlfw_list_entry *carlfw_desc_to_entry(struct carl9170fw_desc_head *head)
62 return container_of(head, struct carlfw_list_entry, head);
65 static inline struct carl9170fw_desc_head *carlfw_entry_to_desc(struct carlfw_list_entry *entry)
70 static void carlfw_entry_unlink(struct carlfw *fw,
71 struct carlfw_list_entry *entry)
73 fw->desc_list_entries--;
74 fw->desc_list_len -= le16_to_cpu(entry->head.length);
75 list_del(&entry->h.list);
78 static void carlfw_entry_del(struct carlfw *fw,
79 struct carlfw_list_entry *entry)
81 carlfw_entry_unlink(fw, entry);
85 static struct carlfw_list_entry *carlfw_find_entry(struct carlfw *fw,
86 const uint8_t descid[4],
88 uint8_t compatible_revision)
90 struct carlfw_list_entry *iter;
92 carlfw_walk_descs(iter, fw) {
93 if (carl9170fw_desc_cmp(&iter->head, descid, len,
101 static struct carlfw_list_entry *__carlfw_entry_add_prepare(struct carlfw *fw,
102 const struct carl9170fw_desc_head *desc)
104 struct carlfw_list_entry *tmp;
107 len = le16_to_cpu(desc->length);
109 if (len < sizeof(struct carl9170fw_desc_head))
110 return ERR_PTR(-EINVAL);
112 tmp = malloc(sizeof(*tmp) + len);
114 return ERR_PTR(-ENOMEM);
116 fw->desc_list_entries++;
117 fw->desc_list_len += len;
119 memcpy(tmp->data, desc, len);
123 static void __carlfw_release(struct carlfw_file *f)
135 void carlfw_release(struct carlfw *fw)
137 struct carlfw_list_entry *entry;
139 if (!IS_ERR_OR_NULL(fw)) {
140 while (!list_empty(&fw->desc_list)) {
141 entry = list_entry(fw->desc_list.next,
142 struct carlfw_list_entry, h.list);
143 carlfw_entry_del(fw, entry);
146 __carlfw_release(&fw->fw);
147 __carlfw_release(&fw->hdr);
152 static int __carlfw_load(struct carlfw_file *file, const char *name, const char *mode)
154 struct stat file_stat;
158 fh = fopen(name, mode);
160 return errno ? -errno : -1;
162 err = fstat(fileno(fh), &file_stat);
164 return errno ? -errno : -1;
166 file->len = file_stat.st_size;
167 file->data = malloc(file->len);
168 if (file->data == NULL)
171 err = fread(file->data, file->len, 1, fh);
175 file->name = strdup(name);
184 static void *__carlfw_find_desc(struct carlfw_file *file,
187 uint8_t compatible_revision)
189 int scan = file->len, found = 0;
190 struct carl9170fw_desc_head *tmp = NULL;
193 if (file->data[scan] == descid[CARL9170FW_MAGIC_SIZE - found - 1])
198 if (found == CARL9170FW_MAGIC_SIZE)
204 if (found == CARL9170FW_MAGIC_SIZE) {
205 tmp = (void *) &file->data[scan];
207 if (!CHECK_HDR_VERSION(tmp, compatible_revision) &&
208 (le16_to_cpu(tmp->length) >= len))
215 void *carlfw_find_desc(struct carlfw *fw,
216 const uint8_t descid[4],
217 const unsigned int len,
218 const uint8_t compatible_revision)
220 struct carlfw_list_entry *tmp;
222 tmp = carlfw_find_entry(fw, descid, len, compatible_revision);
224 return tmp ? carlfw_entry_to_desc(tmp) : NULL;
227 int carlfw_desc_add_tail(struct carlfw *fw,
228 const struct carl9170fw_desc_head *desc)
230 struct carlfw_list_entry *tmp;
232 tmp = __carlfw_entry_add_prepare(fw, desc);
236 list_add_tail(&tmp->h.list, &fw->desc_list);
240 int carlfw_desc_add(struct carlfw *fw,
241 const struct carl9170fw_desc_head *desc,
242 struct carl9170fw_desc_head *prev,
243 struct carl9170fw_desc_head *next)
245 struct carlfw_list_entry *tmp;
247 tmp = __carlfw_entry_add_prepare(fw, desc);
251 list_add(&tmp->h.list, &((carlfw_desc_to_entry(prev))->h.list),
252 &((carlfw_desc_to_entry(next))->h.list));
256 int carlfw_desc_add_before(struct carlfw *fw,
257 const struct carl9170fw_desc_head *desc,
258 struct carl9170fw_desc_head *pos)
260 struct carl9170fw_desc_head *prev;
261 struct carlfw_list_entry *prev_entry;
263 prev_entry = carlfw_desc_to_entry(pos);
265 prev = carlfw_entry_to_desc((struct carlfw_list_entry *) prev_entry->h.list.prev);
267 return carlfw_desc_add(fw, desc, prev, pos);
270 void carlfw_desc_unlink(struct carlfw *fw,
271 struct carl9170fw_desc_head *desc)
273 carlfw_entry_unlink(fw, carlfw_desc_to_entry(desc));
276 void carlfw_desc_del(struct carlfw *fw,
277 struct carl9170fw_desc_head *desc)
279 carlfw_entry_del(fw, carlfw_desc_to_entry(desc));
282 void *carlfw_desc_mod_len(struct carlfw *fw __unused,
283 struct carl9170fw_desc_head *desc, size_t len)
285 struct carlfw_list_entry *obj, tmp;
286 int new_len = le16_to_cpu(desc->length) + len;
288 if (new_len < (int)sizeof(*desc))
289 return ERR_PTR(-EINVAL);
291 if (new_len > CARL9170FW_DESC_MAX_LENGTH)
292 return ERR_PTR(-E2BIG);
294 obj = carlfw_desc_to_entry(desc);
296 memcpy(&tmp, obj, sizeof(tmp));
297 obj = realloc(obj, new_len + sizeof(struct carlfw_list_entry_head));
299 return ERR_PTR(-ENOMEM);
301 list_replace(&tmp.h.list, &obj->h.list);
303 desc = carlfw_entry_to_desc(obj);
304 desc->length = le16_to_cpu(new_len);
305 fw->desc_list_len += len;
310 void *carlfw_desc_next(struct carlfw *fw,
311 struct carl9170fw_desc_head *pos)
313 struct carlfw_list_entry *entry;
316 entry = (struct carlfw_list_entry *) &fw->desc_list;
318 entry = carlfw_desc_to_entry(pos);
320 if (list_at_tail(entry, &fw->desc_list, h.list))
323 entry = (struct carlfw_list_entry *) entry->h.list.next;
325 return carlfw_entry_to_desc(entry);
328 static int carlfw_parse_descs(struct carlfw *fw,
329 struct carl9170fw_otus_desc *otus_desc)
331 const struct carl9170fw_desc_head *iter = NULL;
334 carl9170fw_for_each_hdr(iter, &otus_desc->head) {
335 err = carlfw_desc_add_tail(fw, iter);
339 /* LAST is added automatically by carlfw_store */
344 #if BYTE_ORDER == LITTLE_ENDIAN
345 #define CRCPOLY_LE 0xedb88320
347 /* copied from the linux kernel */
348 static uint32_t crc32_le(uint32_t crc, unsigned char const *p, size_t len)
353 for (i = 0; i < 8; i++)
354 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
359 #error "this tool does not work with a big endian host yet!"
362 static int carlfw_check_crc32s(struct carlfw *fw)
364 struct carl9170fw_chk_desc *chk_desc;
365 struct carlfw_list_entry *iter;
369 chk_desc = carlfw_find_desc(fw, (uint8_t *) CHK_MAGIC,
371 CARL9170FW_CHK_DESC_CUR_VER);
375 crc32 = crc32_le(~0, (void *) fw->fw.data, fw->fw.len);
376 if (crc32 != le32_to_cpu(chk_desc->fw_crc32))
379 carlfw_walk_descs(iter, fw) {
380 elen = le16_to_cpu(iter->head.length);
382 if (carl9170fw_desc_cmp(&iter->head, (uint8_t *) CHK_MAGIC,
384 CARL9170FW_CHK_DESC_CUR_VER))
387 crc32 = crc32_le(crc32, (void *) &iter->head, elen);
390 if (crc32 != le32_to_cpu(chk_desc->hdr_crc32))
396 struct carlfw *carlfw_load(const char *basename)
400 struct carl9170fw_otus_desc *otus_desc;
401 struct carl9170fw_last_desc *last_desc;
402 struct carlfw_file *hdr_file;
403 unsigned long fin, diff, off, rem;
406 fw = calloc(1, sizeof(*fw));
408 return ERR_PTR(-ENOMEM);
410 init_list_head(&fw->desc_list);
412 err = __carlfw_load(&fw->fw, basename, "r");
421 otus_desc = __carlfw_find_desc(hdr_file, (uint8_t *) OTUS_MAGIC,
423 CARL9170FW_OTUS_DESC_CUR_VER);
424 last_desc = __carlfw_find_desc(hdr_file, (uint8_t *) LAST_MAGIC,
426 CARL9170FW_LAST_DESC_CUR_VER);
428 if (!otus_desc || !last_desc ||
429 (unsigned long) otus_desc > (unsigned long) last_desc) {
434 err = carlfw_parse_descs(fw, otus_desc);
438 fin = (unsigned long)last_desc + sizeof(*last_desc);
439 diff = fin - (unsigned long)otus_desc;
440 rem = hdr_file->len - (fin - (unsigned long) hdr_file->data);
443 off = (unsigned long)otus_desc - (unsigned long)hdr_file->data;
444 memmove(&hdr_file->data[off],
445 ((uint8_t *)last_desc) + sizeof(*last_desc), rem);
448 hdr_file->len -= diff;
449 hdr_file->data = realloc(hdr_file->data, hdr_file->len);
450 if (!hdr_file->data && hdr_file->len) {
455 err = carlfw_check_crc32s(fw);
456 if (err && err != -ENODATA)
466 static int carlfw_apply_checksums(struct carlfw *fw)
468 struct carlfw_list_entry *iter;
469 struct carl9170fw_chk_desc tmp = {
470 CARL9170FW_FILL_DESC(CHK_MAGIC, sizeof(tmp),
471 CARL9170FW_CHK_DESC_MIN_VER,
472 CARL9170FW_CHK_DESC_CUR_VER) };
473 struct carl9170fw_chk_desc *chk_desc = NULL;
475 unsigned int len = 0, elen, max_len;
478 chk_desc = carlfw_find_desc(fw, (uint8_t *) CHK_MAGIC,
480 CARL9170FW_CHK_DESC_CUR_VER);
482 carlfw_desc_del(fw, &chk_desc->head);
486 max_len = fw->desc_list_len;
488 crc32 = crc32_le(~0, (void *) fw->fw.data, fw->fw.len);
489 tmp.fw_crc32 = cpu_to_le32(crc32);
494 * The descriptor checksum is seeded with the firmware's crc32.
495 * This neat trick ensures that the driver can check whenever
496 * descriptor actually belongs to the firmware, or not.
499 carlfw_walk_descs(iter, fw) {
500 elen = le16_to_cpu(iter->head.length);
502 if (max_len < len + elen)
505 crc32 = crc32_le(crc32, (void *) &iter->head, elen);
509 tmp.hdr_crc32 = cpu_to_le32(crc32);
511 err = carlfw_desc_add_tail(fw, &tmp.head);
516 int carlfw_store(struct carlfw *fw)
518 struct carl9170fw_last_desc last_desc = {
519 CARL9170FW_FILL_DESC(LAST_MAGIC, sizeof(last_desc),
520 CARL9170FW_LAST_DESC_MIN_VER,
521 CARL9170FW_LAST_DESC_CUR_VER) };
523 struct carlfw_list_entry *iter;
527 err = carlfw_apply_checksums(fw);
531 fh = fopen(fw->fw.name, "w");
535 err = fwrite(fw->fw.data, fw->fw.len, 1, fh);
544 fh = fopen(fw->hdr.name, "w");
547 carlfw_walk_descs(iter, fw) {
548 elen = le16_to_cpu(iter->head.length);
550 if (elen > CARL9170FW_DESC_MAX_LENGTH) {
555 err = fwrite(iter->data, elen, 1, fh);
562 err = fwrite(&last_desc, sizeof(last_desc), 1, fh);
575 void *carlfw_mod_tailroom(struct carlfw *fw, ssize_t len)
580 new_len = fw->fw.len + len;
582 if (!carl9170fw_size_check(new_len))
583 return ERR_PTR(-EINVAL);
585 buf = realloc(fw->fw.data, new_len);
587 return ERR_PTR(-ENOMEM);
589 fw->fw.len = new_len;
591 return &fw->fw.data[new_len - len];
594 void *carlfw_mod_headroom(struct carlfw *fw, ssize_t len)
599 new_len = fw->fw.len + len;
600 if (!carl9170fw_size_check(new_len))
601 return ERR_PTR(-EINVAL);
604 memmove(fw->fw.data, &fw->fw.data[len], new_len);
606 ptr = carlfw_mod_tailroom(fw, len);
607 if (IS_ERR_OR_NULL(ptr))
611 memmove(&fw->fw.data[len], &fw->fw.data[0], new_len - len);
616 void *carlfw_get_fw(struct carlfw *fw, size_t *len)
622 unsigned int carlfw_get_descs_num(struct carlfw *fw)
624 return fw->desc_list_entries;
627 unsigned int carlfw_get_descs_size(struct carlfw *fw)
629 return fw->desc_list_len;