PageRenderTime 50ms CodeModel.GetById 34ms app.highlight 13ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/ath/carl9170/fw.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t
C | 435 lines | 320 code | 93 blank | 22 comment | 65 complexity | b76e0b9dcb8f1a7d987b503547216902 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1/*
  2 * Atheros CARL9170 driver
  3 *
  4 * firmware parser
  5 *
  6 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
  7 *
  8 * This program is free software; you can redistribute it and/or modify
  9 * it under the terms of the GNU General Public License as published by
 10 * the Free Software Foundation; either version 2 of the License, or
 11 * (at your option) any later version.
 12 *
 13 * This program is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 * GNU General Public License for more details.
 17 *
 18 * You should have received a copy of the GNU General Public License
 19 * along with this program; see the file COPYING.  If not, see
 20 * http://www.gnu.org/licenses/.
 21 */
 22
 23#include <linux/kernel.h>
 24#include <linux/firmware.h>
 25#include <linux/crc32.h>
 26#include "carl9170.h"
 27#include "fwcmd.h"
 28#include "version.h"
 29
 30#define MAKE_STR(symbol) #symbol
 31#define TO_STR(symbol) MAKE_STR(symbol)
 32#define CARL9170FW_API_VER_STR TO_STR(CARL9170FW_API_MAX_VER)
 33MODULE_VERSION(CARL9170FW_API_VER_STR ":" CARL9170FW_VERSION_GIT);
 34
 35static const u8 otus_magic[4] = { OTUS_MAGIC };
 36
 37static const void *carl9170_fw_find_desc(struct ar9170 *ar, const u8 descid[4],
 38	const unsigned int len, const u8 compatible_revision)
 39{
 40	const struct carl9170fw_desc_head *iter;
 41
 42	carl9170fw_for_each_hdr(iter, ar->fw.desc) {
 43		if (carl9170fw_desc_cmp(iter, descid, len,
 44					compatible_revision))
 45			return (void *)iter;
 46	}
 47
 48	/* needed to find the LAST desc */
 49	if (carl9170fw_desc_cmp(iter, descid, len,
 50				compatible_revision))
 51		return (void *)iter;
 52
 53	return NULL;
 54}
 55
 56static int carl9170_fw_verify_descs(struct ar9170 *ar,
 57	const struct carl9170fw_desc_head *head, unsigned int max_len)
 58{
 59	const struct carl9170fw_desc_head *pos;
 60	unsigned long pos_addr, end_addr;
 61	unsigned int pos_length;
 62
 63	if (max_len < sizeof(*pos))
 64		return -ENODATA;
 65
 66	max_len = min_t(unsigned int, CARL9170FW_DESC_MAX_LENGTH, max_len);
 67
 68	pos = head;
 69	pos_addr = (unsigned long) pos;
 70	end_addr = pos_addr + max_len;
 71
 72	while (pos_addr < end_addr) {
 73		if (pos_addr + sizeof(*head) > end_addr)
 74			return -E2BIG;
 75
 76		pos_length = le16_to_cpu(pos->length);
 77
 78		if (pos_length < sizeof(*head))
 79			return -EBADMSG;
 80
 81		if (pos_length > max_len)
 82			return -EOVERFLOW;
 83
 84		if (pos_addr + pos_length > end_addr)
 85			return -EMSGSIZE;
 86
 87		if (carl9170fw_desc_cmp(pos, LAST_MAGIC,
 88					CARL9170FW_LAST_DESC_SIZE,
 89					CARL9170FW_LAST_DESC_CUR_VER))
 90			return 0;
 91
 92		pos_addr += pos_length;
 93		pos = (void *)pos_addr;
 94		max_len -= pos_length;
 95	}
 96	return -EINVAL;
 97}
 98
 99static void carl9170_fw_info(struct ar9170 *ar)
100{
101	const struct carl9170fw_motd_desc *motd_desc;
102	unsigned int str_ver_len;
103	u32 fw_date;
104
105	dev_info(&ar->udev->dev, "driver   API: %s 2%03d-%02d-%02d [%d-%d]\n",
106		CARL9170FW_VERSION_GIT, CARL9170FW_VERSION_YEAR,
107		CARL9170FW_VERSION_MONTH, CARL9170FW_VERSION_DAY,
108		CARL9170FW_API_MIN_VER, CARL9170FW_API_MAX_VER);
109
110	motd_desc = carl9170_fw_find_desc(ar, MOTD_MAGIC,
111		sizeof(*motd_desc), CARL9170FW_MOTD_DESC_CUR_VER);
112
113	if (motd_desc) {
114		str_ver_len = strnlen(motd_desc->release,
115			CARL9170FW_MOTD_RELEASE_LEN);
116
117		fw_date = le32_to_cpu(motd_desc->fw_year_month_day);
118
119		dev_info(&ar->udev->dev, "firmware API: %.*s 2%03d-%02d-%02d\n",
120			 str_ver_len, motd_desc->release,
121			 CARL9170FW_GET_YEAR(fw_date),
122			 CARL9170FW_GET_MONTH(fw_date),
123			 CARL9170FW_GET_DAY(fw_date));
124
125		strlcpy(ar->hw->wiphy->fw_version, motd_desc->release,
126			sizeof(ar->hw->wiphy->fw_version));
127	}
128}
129
130static bool valid_dma_addr(const u32 address)
131{
132	if (address >= AR9170_SRAM_OFFSET &&
133	    address < (AR9170_SRAM_OFFSET + AR9170_SRAM_SIZE))
134		return true;
135
136	return false;
137}
138
139static bool valid_cpu_addr(const u32 address)
140{
141	if (valid_dma_addr(address) || (address >= AR9170_PRAM_OFFSET &&
142	    address < (AR9170_PRAM_OFFSET + AR9170_PRAM_SIZE)))
143		return true;
144
145	return false;
146}
147
148static int carl9170_fw(struct ar9170 *ar, const __u8 *data, size_t len)
149{
150	const struct carl9170fw_otus_desc *otus_desc;
151	const struct carl9170fw_chk_desc *chk_desc;
152	const struct carl9170fw_last_desc *last_desc;
153	const struct carl9170fw_txsq_desc *txsq_desc;
154	u16 if_comb_types;
155
156	last_desc = carl9170_fw_find_desc(ar, LAST_MAGIC,
157		sizeof(*last_desc), CARL9170FW_LAST_DESC_CUR_VER);
158	if (!last_desc)
159		return -EINVAL;
160
161	otus_desc = carl9170_fw_find_desc(ar, OTUS_MAGIC,
162		sizeof(*otus_desc), CARL9170FW_OTUS_DESC_CUR_VER);
163	if (!otus_desc) {
164		dev_err(&ar->udev->dev, "failed to find compatible firmware "
165			"descriptor.\n");
166		return -ENODATA;
167	}
168
169	chk_desc = carl9170_fw_find_desc(ar, CHK_MAGIC,
170		sizeof(*chk_desc), CARL9170FW_CHK_DESC_CUR_VER);
171
172	if (chk_desc) {
173		unsigned long fin, diff;
174		unsigned int dsc_len;
175		u32 crc32;
176
177		dsc_len = min_t(unsigned int, len,
178			(unsigned long)chk_desc - (unsigned long)otus_desc);
179
180		fin = (unsigned long) last_desc + sizeof(*last_desc);
181		diff = fin - (unsigned long) otus_desc;
182
183		if (diff < len)
184			len -= diff;
185
186		if (len < 256)
187			return -EIO;
188
189		crc32 = crc32_le(~0, data, len);
190		if (cpu_to_le32(crc32) != chk_desc->fw_crc32) {
191			dev_err(&ar->udev->dev, "fw checksum test failed.\n");
192			return -ENOEXEC;
193		}
194
195		crc32 = crc32_le(crc32, (void *)otus_desc, dsc_len);
196		if (cpu_to_le32(crc32) != chk_desc->hdr_crc32) {
197			dev_err(&ar->udev->dev, "descriptor check failed.\n");
198			return -EINVAL;
199		}
200	} else {
201		dev_warn(&ar->udev->dev, "Unprotected firmware image.\n");
202	}
203
204#define SUPP(feat)						\
205	(carl9170fw_supports(otus_desc->feature_set, feat))
206
207	if (!SUPP(CARL9170FW_DUMMY_FEATURE)) {
208		dev_err(&ar->udev->dev, "invalid firmware descriptor "
209			"format detected.\n");
210		return -EINVAL;
211	}
212
213	ar->fw.api_version = otus_desc->api_ver;
214
215	if (ar->fw.api_version < CARL9170FW_API_MIN_VER ||
216	    ar->fw.api_version > CARL9170FW_API_MAX_VER) {
217		dev_err(&ar->udev->dev, "unsupported firmware api version.\n");
218		return -EINVAL;
219	}
220
221	if (!SUPP(CARL9170FW_COMMAND_PHY) || SUPP(CARL9170FW_UNUSABLE) ||
222	    !SUPP(CARL9170FW_HANDLE_BACK_REQ)) {
223		dev_err(&ar->udev->dev, "firmware does support "
224			"mandatory features.\n");
225		return -ECANCELED;
226	}
227
228	if (ilog2(le32_to_cpu(otus_desc->feature_set)) >=
229		__CARL9170FW_FEATURE_NUM) {
230		dev_warn(&ar->udev->dev, "driver does not support all "
231			 "firmware features.\n");
232	}
233
234	if (!SUPP(CARL9170FW_COMMAND_CAM)) {
235		dev_info(&ar->udev->dev, "crypto offloading is disabled "
236			 "by firmware.\n");
237		ar->disable_offload = true;
238	}
239
240	if (SUPP(CARL9170FW_PSM) && SUPP(CARL9170FW_FIXED_5GHZ_PSM))
241		ar->hw->flags |= IEEE80211_HW_SUPPORTS_PS;
242
243	if (!SUPP(CARL9170FW_USB_INIT_FIRMWARE)) {
244		dev_err(&ar->udev->dev, "firmware does not provide "
245			"mandatory interfaces.\n");
246		return -EINVAL;
247	}
248
249	if (SUPP(CARL9170FW_MINIBOOT))
250		ar->fw.offset = le16_to_cpu(otus_desc->miniboot_size);
251	else
252		ar->fw.offset = 0;
253
254	if (SUPP(CARL9170FW_USB_DOWN_STREAM)) {
255		ar->hw->extra_tx_headroom += sizeof(struct ar9170_stream);
256		ar->fw.tx_stream = true;
257	}
258
259	if (SUPP(CARL9170FW_USB_UP_STREAM))
260		ar->fw.rx_stream = true;
261
262	if (SUPP(CARL9170FW_RX_FILTER)) {
263		ar->fw.rx_filter = true;
264		ar->rx_filter_caps = FIF_FCSFAIL | FIF_PLCPFAIL |
265			FIF_CONTROL | FIF_PSPOLL | FIF_OTHER_BSS |
266			FIF_PROMISC_IN_BSS;
267	}
268
269	if (SUPP(CARL9170FW_WOL))
270		device_set_wakeup_enable(&ar->udev->dev, true);
271
272	if_comb_types = BIT(NL80211_IFTYPE_STATION) |
273			BIT(NL80211_IFTYPE_P2P_CLIENT);
274
275	ar->fw.vif_num = otus_desc->vif_num;
276	ar->fw.cmd_bufs = otus_desc->cmd_bufs;
277	ar->fw.address = le32_to_cpu(otus_desc->fw_address);
278	ar->fw.rx_size = le16_to_cpu(otus_desc->rx_max_frame_len);
279	ar->fw.mem_blocks = min_t(unsigned int, otus_desc->tx_descs, 0xfe);
280	atomic_set(&ar->mem_free_blocks, ar->fw.mem_blocks);
281	ar->fw.mem_block_size = le16_to_cpu(otus_desc->tx_frag_len);
282
283	if (ar->fw.vif_num >= AR9170_MAX_VIRTUAL_MAC || !ar->fw.vif_num ||
284	    ar->fw.mem_blocks < 16 || !ar->fw.cmd_bufs ||
285	    ar->fw.mem_block_size < 64 || ar->fw.mem_block_size > 512 ||
286	    ar->fw.rx_size > 32768 || ar->fw.rx_size < 4096 ||
287	    !valid_cpu_addr(ar->fw.address)) {
288		dev_err(&ar->udev->dev, "firmware shows obvious signs of "
289			"malicious tampering.\n");
290		return -EINVAL;
291	}
292
293	ar->fw.beacon_addr = le32_to_cpu(otus_desc->bcn_addr);
294	ar->fw.beacon_max_len = le16_to_cpu(otus_desc->bcn_len);
295
296	if (valid_dma_addr(ar->fw.beacon_addr) && ar->fw.beacon_max_len >=
297	    AR9170_MAC_BCN_LENGTH_MAX) {
298		ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
299
300		if (SUPP(CARL9170FW_WLANTX_CAB)) {
301			if_comb_types |=
302				BIT(NL80211_IFTYPE_AP) |
303				BIT(NL80211_IFTYPE_P2P_GO);
304		}
305	}
306
307	ar->if_comb_limits[0].max = ar->fw.vif_num;
308	ar->if_comb_limits[0].types = if_comb_types;
309
310	ar->if_combs[0].num_different_channels = 1;
311	ar->if_combs[0].max_interfaces = ar->fw.vif_num;
312	ar->if_combs[0].limits = ar->if_comb_limits;
313	ar->if_combs[0].n_limits = ARRAY_SIZE(ar->if_comb_limits);
314
315	ar->hw->wiphy->iface_combinations = ar->if_combs;
316	ar->hw->wiphy->n_iface_combinations = ARRAY_SIZE(ar->if_combs);
317
318	ar->hw->wiphy->interface_modes |= if_comb_types;
319
320	txsq_desc = carl9170_fw_find_desc(ar, TXSQ_MAGIC,
321		sizeof(*txsq_desc), CARL9170FW_TXSQ_DESC_CUR_VER);
322
323	if (txsq_desc) {
324		ar->fw.tx_seq_table = le32_to_cpu(txsq_desc->seq_table_addr);
325		if (!valid_cpu_addr(ar->fw.tx_seq_table))
326			return -EINVAL;
327	} else {
328		ar->fw.tx_seq_table = 0;
329	}
330
331#undef SUPPORTED
332	return 0;
333}
334
335static struct carl9170fw_desc_head *
336carl9170_find_fw_desc(struct ar9170 *ar, const __u8 *fw_data, const size_t len)
337
338{
339	int scan = 0, found = 0;
340
341	if (!carl9170fw_size_check(len)) {
342		dev_err(&ar->udev->dev, "firmware size is out of bound.\n");
343		return NULL;
344	}
345
346	while (scan < len - sizeof(struct carl9170fw_desc_head)) {
347		if (fw_data[scan++] == otus_magic[found])
348			found++;
349		else
350			found = 0;
351
352		if (scan >= len)
353			break;
354
355		if (found == sizeof(otus_magic))
356			break;
357	}
358
359	if (found != sizeof(otus_magic))
360		return NULL;
361
362	return (void *)&fw_data[scan - found];
363}
364
365int carl9170_fw_fix_eeprom(struct ar9170 *ar)
366{
367	const struct carl9170fw_fix_desc *fix_desc = NULL;
368	unsigned int i, n, off;
369	u32 *data = (void *)&ar->eeprom;
370
371	fix_desc = carl9170_fw_find_desc(ar, FIX_MAGIC,
372		sizeof(*fix_desc), CARL9170FW_FIX_DESC_CUR_VER);
373
374	if (!fix_desc)
375		return 0;
376
377	n = (le16_to_cpu(fix_desc->head.length) - sizeof(*fix_desc)) /
378	    sizeof(struct carl9170fw_fix_entry);
379
380	for (i = 0; i < n; i++) {
381		off = le32_to_cpu(fix_desc->data[i].address) -
382		      AR9170_EEPROM_START;
383
384		if (off >= sizeof(struct ar9170_eeprom) || (off & 3)) {
385			dev_err(&ar->udev->dev, "Skip invalid entry %d\n", i);
386			continue;
387		}
388
389		data[off / sizeof(*data)] &=
390			le32_to_cpu(fix_desc->data[i].mask);
391		data[off / sizeof(*data)] |=
392			le32_to_cpu(fix_desc->data[i].value);
393	}
394
395	return 0;
396}
397
398int carl9170_parse_firmware(struct ar9170 *ar)
399{
400	const struct carl9170fw_desc_head *fw_desc = NULL;
401	const struct firmware *fw = ar->fw.fw;
402	unsigned long header_offset = 0;
403	int err;
404
405	if (WARN_ON(!fw))
406		return -EINVAL;
407
408	fw_desc = carl9170_find_fw_desc(ar, fw->data, fw->size);
409
410	if (!fw_desc) {
411		dev_err(&ar->udev->dev, "unsupported firmware.\n");
412		return -ENODATA;
413	}
414
415	header_offset = (unsigned long)fw_desc - (unsigned long)fw->data;
416
417	err = carl9170_fw_verify_descs(ar, fw_desc, fw->size - header_offset);
418	if (err) {
419		dev_err(&ar->udev->dev, "damaged firmware (%d).\n", err);
420		return err;
421	}
422
423	ar->fw.desc = fw_desc;
424
425	carl9170_fw_info(ar);
426
427	err = carl9170_fw(ar, fw->data, fw->size);
428	if (err) {
429		dev_err(&ar->udev->dev, "failed to parse firmware (%d).\n",
430			err);
431		return err;
432	}
433
434	return 0;
435}