summaryrefslogtreecommitdiffstats
path: root/carl9170fw/tools/lib/carlfw.c
blob: ce61afb244913f1c892a878d5f6b90ac60bc287a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/*
 * Copyright 2010-2011 Christian Lamparter <chunkeey@googlemail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <stdlib.h>
#include <stdio.h>
#include <error.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include "carlfw.h"

struct carlfw_file {
	char *name;
	size_t len;
	char *data;
};

struct carlfw {
	struct carlfw_file fw;
	struct carlfw_file hdr;

	struct list_head desc_list;
	unsigned int desc_list_entries,
		     desc_list_len;
};

#define carlfw_walk_descs(iter, fw)					\
	list_for_each_entry(iter, &fw->desc_list, h.list)

struct carlfw_list_entry_head {
	struct list_head list;
};

struct carlfw_list_entry {
	struct carlfw_list_entry_head h;
	union {
		struct carl9170fw_desc_head head;
		uint32_t data[0];
		char text[0];
	};
};

static inline struct carlfw_list_entry *carlfw_desc_to_entry(struct carl9170fw_desc_head *head)
{
	return container_of(head, struct carlfw_list_entry, head);
}

static inline struct carl9170fw_desc_head *carlfw_entry_to_desc(struct carlfw_list_entry *entry)
{
	return &entry->head;
}

static void carlfw_entry_unlink(struct carlfw *fw,
	struct carlfw_list_entry *entry)
{
	fw->desc_list_entries--;
	fw->desc_list_len -= le16_to_cpu(entry->head.length);
	list_del(&entry->h.list);
}

static void carlfw_entry_del(struct carlfw *fw,
	struct carlfw_list_entry *entry)
{
	carlfw_entry_unlink(fw, entry);
	free(entry);
}

static struct carlfw_list_entry *carlfw_find_entry(struct carlfw *fw,
						   const uint8_t descid[4],
						   unsigned int len,
						   uint8_t compatible_revision)
{
	struct carlfw_list_entry *iter;

	carlfw_walk_descs(iter, fw) {
		if (carl9170fw_desc_cmp(&iter->head, descid, len,
					 compatible_revision))
			return (void *)iter;
	}

	return NULL;
}

static struct carlfw_list_entry *__carlfw_entry_add_prepare(struct carlfw *fw,
	const struct carl9170fw_desc_head *desc)
{
	struct carlfw_list_entry *tmp;
	unsigned int len;

	len = le16_to_cpu(desc->length);

	if (len < sizeof(struct carl9170fw_desc_head))
		return ERR_PTR(-EINVAL);

	tmp = malloc(sizeof(*tmp) + len);
	if (!tmp)
		return ERR_PTR(-ENOMEM);

	fw->desc_list_entries++;
	fw->desc_list_len += len;

	memcpy(tmp->data, desc, len);
	return tmp;
}

static void __carlfw_release(struct carlfw_file *f)
{
	f->len = 0;
	if (f->name)
		free(f->name);
	f->name = NULL;

	if (f->data)
		free(f->data);
	f->data = NULL;
}

void carlfw_release(struct carlfw *fw)
{
	struct carlfw_list_entry *entry;

	if (!IS_ERR_OR_NULL(fw)) {
		while (!list_empty(&fw->desc_list)) {
			entry = list_entry(fw->desc_list.next,
					   struct carlfw_list_entry, h.list);
			carlfw_entry_del(fw, entry);
		}

		__carlfw_release(&fw->fw);
		__carlfw_release(&fw->hdr);
		free(fw);
	}
}

static int __carlfw_load(struct carlfw_file *file, const char *name, const char *mode)
{
	struct stat file_stat;
	FILE *fh;
	int err;

	fh = fopen(name, mode);
	if (fh == NULL)
		return errno ? -errno : -1;

	err = fstat(fileno(fh), &file_stat);
	if (err)
		return errno ? -errno : -1;

	file->len = file_stat.st_size;
	file->data = malloc(file->len);
	if (file->data == NULL)
		return -ENOMEM;

	err = fread(file->data, file->len, 1, fh);
	if (err != 1)
		return -ferror(fh);

	file->name = strdup(name);
	fclose(fh);

	if (!file->name)
		return -ENOMEM;

	return 0;
}

static void *__carlfw_find_desc(struct carlfw_file *file,
				uint8_t descid[4],
				unsigned int len,
				uint8_t compatible_revision)
{
	int scan = file->len, found = 0;
	struct carl9170fw_desc_head *tmp = NULL;

	while (scan >= 0) {
		if (file->data[scan] == descid[CARL9170FW_MAGIC_SIZE - found - 1])
			found++;
		else
			found = 0;

		if (found == CARL9170FW_MAGIC_SIZE)
			break;

		scan--;
	}

	if (found == CARL9170FW_MAGIC_SIZE) {
		tmp = (void *) &file->data[scan];

		if (!CHECK_HDR_VERSION(tmp, compatible_revision) &&
		    (le16_to_cpu(tmp->length) >= len))
			return tmp;
	}

	return NULL;
}

void *carlfw_find_desc(struct carlfw *fw,
		       const uint8_t descid[4],
		       const unsigned int len,
		       const uint8_t compatible_revision)
{
	struct carlfw_list_entry *tmp;

	tmp = carlfw_find_entry(fw, descid, len, compatible_revision);

	return tmp ? carlfw_entry_to_desc(tmp) : NULL;
}

int carlfw_desc_add_tail(struct carlfw *fw,
	const struct carl9170fw_desc_head *desc)
{
	struct carlfw_list_entry *tmp;

	tmp = __carlfw_entry_add_prepare(fw, desc);
	if (IS_ERR(tmp))
		return PTR_ERR(tmp);

	list_add_tail(&tmp->h.list, &fw->desc_list);
	return 0;
}

int carlfw_desc_add(struct carlfw *fw,
		    const struct carl9170fw_desc_head *desc,
		    struct carl9170fw_desc_head *prev,
		    struct carl9170fw_desc_head *next)
{
	struct carlfw_list_entry *tmp;

	tmp = __carlfw_entry_add_prepare(fw, desc);
	if (IS_ERR(tmp))
		return PTR_ERR(tmp);

	list_add(&tmp->h.list, &((carlfw_desc_to_entry(prev))->h.list),
		 &((carlfw_desc_to_entry(next))->h.list));
	return 0;
}

int carlfw_desc_add_before(struct carlfw *fw,
			   const struct carl9170fw_desc_head *desc,
			   struct carl9170fw_desc_head *pos)
{
	struct carl9170fw_desc_head *prev;
	struct carlfw_list_entry *prev_entry;

	prev_entry = carlfw_desc_to_entry(pos);

	prev = carlfw_entry_to_desc((struct carlfw_list_entry *) prev_entry->h.list.prev);

	return carlfw_desc_add(fw, desc, prev, pos);
}

void carlfw_desc_unlink(struct carlfw *fw,
	struct carl9170fw_desc_head *desc)
{
	carlfw_entry_unlink(fw, carlfw_desc_to_entry(desc));
}

void carlfw_desc_del(struct carlfw *fw,
	struct carl9170fw_desc_head *desc)
{
	carlfw_entry_del(fw, carlfw_desc_to_entry(desc));
}

void *carlfw_desc_mod_len(struct carlfw *fw __unused,
	struct carl9170fw_desc_head *desc, size_t len)
{
	struct carlfw_list_entry *obj, tmp;
	int new_len = le16_to_cpu(desc->length) + len;

	if (new_len < (int)sizeof(*desc))
		return ERR_PTR(-EINVAL);

	if (new_len > CARL9170FW_DESC_MAX_LENGTH)
		return ERR_PTR(-E2BIG);

	obj = carlfw_desc_to_entry(desc);

	memcpy(&tmp, obj, sizeof(tmp));
	obj = realloc(obj, new_len + sizeof(struct carlfw_list_entry_head));
	if (obj == NULL)
		return ERR_PTR(-ENOMEM);

	list_replace(&tmp.h.list, &obj->h.list);

	desc = carlfw_entry_to_desc(obj);
	desc->length = le16_to_cpu(new_len);
	fw->desc_list_len += len;

	return desc;
}

void *carlfw_desc_next(struct carlfw *fw,
		       struct carl9170fw_desc_head *pos)
{
	struct carlfw_list_entry *entry;

	if (!pos)
		entry = (struct carlfw_list_entry *) &fw->desc_list;
	else
		entry = carlfw_desc_to_entry(pos);

	if (list_at_tail(entry, &fw->desc_list, h.list))
		return NULL;

	entry = (struct carlfw_list_entry *) entry->h.list.next;

	return carlfw_entry_to_desc(entry);
}

static int carlfw_parse_descs(struct carlfw *fw,
			      struct carl9170fw_otus_desc *otus_desc)
{
	const struct carl9170fw_desc_head *iter = NULL;
	int err;

	carl9170fw_for_each_hdr(iter, &otus_desc->head) {
		err = carlfw_desc_add_tail(fw, iter);
		if (err)
			return err;
	}
	/* LAST is added automatically by carlfw_store */

	return err;
}

#if BYTE_ORDER == LITTLE_ENDIAN
#define CRCPOLY_LE 0xedb88320

/* copied from the linux kernel  */
static uint32_t crc32_le(uint32_t crc, unsigned char const *p, size_t len)
{
	int i;
	while (len--) {
		crc ^= *p++;
		for (i = 0; i < 8; i++)
			crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
	}
	return crc;
}
#else
#error "this tool does not work with a big endian host yet!"
#endif

static int carlfw_check_crc32s(struct carlfw *fw)
{
	struct carl9170fw_chk_desc *chk_desc;
	struct carlfw_list_entry *iter;
	unsigned int elen;
	uint32_t crc32;

	chk_desc = carlfw_find_desc(fw, (uint8_t *) CHK_MAGIC,
				    sizeof(*chk_desc),
				    CARL9170FW_CHK_DESC_CUR_VER);
	if (!chk_desc)
		return -ENODATA;

	crc32 = crc32_le(~0, (void *) fw->fw.data, fw->fw.len);
	if (crc32 != le32_to_cpu(chk_desc->fw_crc32))
		return -EINVAL;

	carlfw_walk_descs(iter, fw) {
		elen = le16_to_cpu(iter->head.length);

		if (carl9170fw_desc_cmp(&iter->head, (uint8_t *) CHK_MAGIC,
					sizeof(*chk_desc),
					CARL9170FW_CHK_DESC_CUR_VER))
			continue;

		crc32 = crc32_le(crc32, (void *) &iter->head, elen);
	}

	if (crc32 != le32_to_cpu(chk_desc->hdr_crc32))
		return -EINVAL;

	return 0;
}

struct carlfw *carlfw_load(const char *basename)
{
	char filename[256];
	struct carlfw *fw;
	struct carl9170fw_otus_desc *otus_desc;
	struct carl9170fw_last_desc *last_desc;
	struct carlfw_file *hdr_file;
	unsigned long fin, diff, off, rem;
	int err;

	fw = calloc(1, sizeof(*fw));
	if (!fw)
		return ERR_PTR(-ENOMEM);

	init_list_head(&fw->desc_list);

	err = __carlfw_load(&fw->fw, basename, "r");
	if (err)
		goto err_out;

	if (fw->hdr.name)
		hdr_file = &fw->hdr;
	else
		hdr_file = &fw->fw;

	otus_desc = __carlfw_find_desc(hdr_file, (uint8_t *) OTUS_MAGIC,
				       sizeof(*otus_desc),
				       CARL9170FW_OTUS_DESC_CUR_VER);
	last_desc = __carlfw_find_desc(hdr_file, (uint8_t *) LAST_MAGIC,
				       sizeof(*last_desc),
				       CARL9170FW_LAST_DESC_CUR_VER);

	if (!otus_desc || !last_desc ||
	    (unsigned long) otus_desc > (unsigned long) last_desc) {
		err = -ENODATA;
		goto err_out;
	}

	err = carlfw_parse_descs(fw, otus_desc);
	if (err)
		goto err_out;

	fin = (unsigned long)last_desc + sizeof(*last_desc);
	diff = fin - (unsigned long)otus_desc;
	rem = hdr_file->len - (fin - (unsigned long) hdr_file->data);

	if (rem) {
		off = (unsigned long)otus_desc - (unsigned long)hdr_file->data;
		memmove(&hdr_file->data[off],
			((uint8_t *)last_desc) + sizeof(*last_desc), rem);
	}

	hdr_file->len -= diff;
	hdr_file->data = realloc(hdr_file->data, hdr_file->len);
	if (!hdr_file->data && hdr_file->len) {
		err = -ENOMEM;
		goto err_out;
	}

	err = carlfw_check_crc32s(fw);
	if (err && err != -ENODATA)
		goto err_out;

	return fw;

err_out:
	carlfw_release(fw);
	return ERR_PTR(err);
}

static int carlfw_apply_checksums(struct carlfw *fw)
{
	struct carlfw_list_entry *iter;
	struct carl9170fw_chk_desc tmp = {
		CARL9170FW_FILL_DESC(CHK_MAGIC, sizeof(tmp),
				      CARL9170FW_CHK_DESC_MIN_VER,
				      CARL9170FW_CHK_DESC_CUR_VER) };
	struct carl9170fw_chk_desc *chk_desc = NULL;
	int err = 0;
	unsigned int len = 0, elen, max_len;
	uint32_t crc32;

	chk_desc = carlfw_find_desc(fw, (uint8_t *) CHK_MAGIC,
				    sizeof(*chk_desc),
				    CARL9170FW_CHK_DESC_CUR_VER);
	if (chk_desc) {
		carlfw_desc_del(fw, &chk_desc->head);
		chk_desc = NULL;
	}

	max_len = fw->desc_list_len;

	crc32 = crc32_le(~0, (void *) fw->fw.data, fw->fw.len);
	tmp.fw_crc32 = cpu_to_le32(crc32);

	/*
	 * NOTE:
	 *
	 * The descriptor checksum is seeded with the firmware's crc32.
	 * This neat trick ensures that the driver can check whenever
	 * descriptor actually belongs to the firmware, or not.
	 */

	carlfw_walk_descs(iter, fw) {
		elen = le16_to_cpu(iter->head.length);

		if (max_len < len + elen)
			return -EMSGSIZE;

		crc32 = crc32_le(crc32, (void *) &iter->head, elen);
		len += elen;
	}

	tmp.hdr_crc32 = cpu_to_le32(crc32);

	err = carlfw_desc_add_tail(fw, &tmp.head);

	return err;
}

int carlfw_store(struct carlfw *fw)
{
	struct carl9170fw_last_desc last_desc = {
		CARL9170FW_FILL_DESC(LAST_MAGIC, sizeof(last_desc),
			CARL9170FW_LAST_DESC_MIN_VER,
			CARL9170FW_LAST_DESC_CUR_VER) };

	struct carlfw_list_entry *iter;
	FILE *fh;
	int err, elen;

	err = carlfw_apply_checksums(fw);
	if (err)
		return err;

	fh = fopen(fw->fw.name, "w");
	if (!fh)
		return -errno;

	err = fwrite(fw->fw.data, fw->fw.len, 1, fh);
	if (err != 1) {
		err = -errno;
		goto close_out;
	}

	if (fw->hdr.name) {
		fclose(fh);

		fh = fopen(fw->hdr.name, "w");
	}

	carlfw_walk_descs(iter, fw) {
		elen = le16_to_cpu(iter->head.length);

		if (elen > CARL9170FW_DESC_MAX_LENGTH) {
			err = -E2BIG;
			goto close_out;
		}

		err = fwrite(iter->data, elen, 1, fh);
		if (err != 1) {
			err = -ferror(fh);
			goto close_out;
		}
	}

	err = fwrite(&last_desc, sizeof(last_desc), 1, fh);
	if (err != 1) {
		err = -ferror(fh);
		goto close_out;
	}

	err = 0;

close_out:
	fclose(fh);
	return err;
}

void *carlfw_mod_tailroom(struct carlfw *fw, ssize_t len)
{
	size_t new_len;
	void *buf;

	new_len = fw->fw.len + len;

	if (!carl9170fw_size_check(new_len))
		return ERR_PTR(-EINVAL);

	buf = realloc(fw->fw.data, new_len);
	if (buf == NULL)
		return ERR_PTR(-ENOMEM);

	fw->fw.len = new_len;
	fw->fw.data = buf;
	return &fw->fw.data[new_len - len];
}

void *carlfw_mod_headroom(struct carlfw *fw, ssize_t len)
{
	size_t new_len;
	void *ptr;

	new_len = fw->fw.len + len;
	if (!carl9170fw_size_check(new_len))
		return ERR_PTR(-EINVAL);

	if (len < 0)
		memmove(fw->fw.data, &fw->fw.data[len], new_len);

	ptr = carlfw_mod_tailroom(fw, len);
	if (IS_ERR_OR_NULL(ptr))
		return ptr;

	if (len > 0)
		memmove(&fw->fw.data[len], &fw->fw.data[0], new_len - len);

	return fw->fw.data;
}

void *carlfw_get_fw(struct carlfw *fw, size_t *len)
{
	*len = fw->fw.len;
	return fw->fw.data;
}

unsigned int carlfw_get_descs_num(struct carlfw *fw)
{
	return fw->desc_list_entries;
}

unsigned int carlfw_get_descs_size(struct carlfw *fw)
{
	return fw->desc_list_len;
}