prism2fw.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. /* from src/prism2/download/prism2dl.c
  2. *
  3. * utility for downloading prism2 images moved into kernelspace
  4. *
  5. * Copyright (C) 1999 AbsoluteValue Systems, Inc. All Rights Reserved.
  6. * --------------------------------------------------------------------
  7. *
  8. * linux-wlan
  9. *
  10. * The contents of this file are subject to the Mozilla Public
  11. * License Version 1.1 (the "License"); you may not use this file
  12. * except in compliance with the License. You may obtain a copy of
  13. * the License at http://www.mozilla.org/MPL/
  14. *
  15. * Software distributed under the License is distributed on an "AS
  16. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  17. * implied. See the License for the specific language governing
  18. * rights and limitations under the License.
  19. *
  20. * Alternatively, the contents of this file may be used under the
  21. * terms of the GNU Public License version 2 (the "GPL"), in which
  22. * case the provisions of the GPL are applicable instead of the
  23. * above. If you wish to allow the use of your version of this file
  24. * only under the terms of the GPL and not to allow others to use
  25. * your version of this file under the MPL, indicate your decision
  26. * by deleting the provisions above and replace them with the notice
  27. * and other provisions required by the GPL. If you do not delete
  28. * the provisions above, a recipient may use your version of this
  29. * file under either the MPL or the GPL.
  30. *
  31. * --------------------------------------------------------------------
  32. *
  33. * Inquiries regarding the linux-wlan Open Source project can be
  34. * made directly to:
  35. *
  36. * AbsoluteValue Systems Inc.
  37. * info@linux-wlan.com
  38. * http://www.linux-wlan.com
  39. *
  40. * --------------------------------------------------------------------
  41. *
  42. * Portions of the development of this software were funded by
  43. * Intersil Corporation as part of PRISM(R) chipset product development.
  44. *
  45. * --------------------------------------------------------------------
  46. */
  47. /*================================================================*/
  48. /* System Includes */
  49. #include <linux/ihex.h>
  50. #include <linux/slab.h>
  51. /*================================================================*/
  52. /* Local Constants */
  53. #define PRISM2_USB_FWFILE "prism2_ru.fw"
  54. MODULE_FIRMWARE(PRISM2_USB_FWFILE);
  55. #define S3DATA_MAX 5000
  56. #define S3PLUG_MAX 200
  57. #define S3CRC_MAX 200
  58. #define S3INFO_MAX 50
  59. #define S3ADDR_PLUG (0xff000000UL)
  60. #define S3ADDR_CRC (0xff100000UL)
  61. #define S3ADDR_INFO (0xff200000UL)
  62. #define S3ADDR_START (0xff400000UL)
  63. #define CHUNKS_MAX 100
  64. #define WRITESIZE_MAX 4096
  65. /*================================================================*/
  66. /* Local Types */
  67. struct s3datarec {
  68. u32 len;
  69. u32 addr;
  70. u8 checksum;
  71. u8 *data;
  72. };
  73. struct s3plugrec {
  74. u32 itemcode;
  75. u32 addr;
  76. u32 len;
  77. };
  78. struct s3crcrec {
  79. u32 addr;
  80. u32 len;
  81. unsigned int dowrite;
  82. };
  83. struct s3inforec {
  84. u16 len;
  85. u16 type;
  86. union {
  87. hfa384x_compident_t version;
  88. hfa384x_caplevel_t compat;
  89. u16 buildseq;
  90. hfa384x_compident_t platform;
  91. } info;
  92. };
  93. struct pda {
  94. u8 buf[HFA384x_PDA_LEN_MAX];
  95. hfa384x_pdrec_t *rec[HFA384x_PDA_RECS_MAX];
  96. unsigned int nrec;
  97. };
  98. struct imgchunk {
  99. u32 addr; /* start address */
  100. u32 len; /* in bytes */
  101. u16 crc; /* CRC value (if it falls at a chunk boundary) */
  102. u8 *data;
  103. };
  104. /*================================================================*/
  105. /* Local Static Definitions */
  106. /*----------------------------------------------------------------*/
  107. /* s-record image processing */
  108. /* Data records */
  109. static unsigned int ns3data;
  110. static struct s3datarec s3data[S3DATA_MAX];
  111. /* Plug records */
  112. static unsigned int ns3plug;
  113. static struct s3plugrec s3plug[S3PLUG_MAX];
  114. /* CRC records */
  115. static unsigned int ns3crc;
  116. static struct s3crcrec s3crc[S3CRC_MAX];
  117. /* Info records */
  118. static unsigned int ns3info;
  119. static struct s3inforec s3info[S3INFO_MAX];
  120. /* S7 record (there _better_ be only one) */
  121. static u32 startaddr;
  122. /* Load image chunks */
  123. static unsigned int nfchunks;
  124. static struct imgchunk fchunk[CHUNKS_MAX];
  125. /* Note that for the following pdrec_t arrays, the len and code */
  126. /* fields are stored in HOST byte order. The mkpdrlist() function */
  127. /* does the conversion. */
  128. /*----------------------------------------------------------------*/
  129. /* PDA, built from [card|newfile]+[addfile1+addfile2...] */
  130. static struct pda pda;
  131. static hfa384x_compident_t nicid;
  132. static hfa384x_caplevel_t rfid;
  133. static hfa384x_caplevel_t macid;
  134. static hfa384x_caplevel_t priid;
  135. /*================================================================*/
  136. /* Local Function Declarations */
  137. static int prism2_fwapply(const struct ihex_binrec *rfptr,
  138. wlandevice_t *wlandev);
  139. static int read_fwfile(const struct ihex_binrec *rfptr);
  140. static int mkimage(struct imgchunk *clist, unsigned int *ccnt);
  141. static int read_cardpda(struct pda *pda, wlandevice_t *wlandev);
  142. static int mkpdrlist(struct pda *pda);
  143. static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
  144. struct s3plugrec *s3plug, unsigned int ns3plug, struct pda *pda);
  145. static int crcimage(struct imgchunk *fchunk, unsigned int nfchunks,
  146. struct s3crcrec *s3crc, unsigned int ns3crc);
  147. static int writeimage(wlandevice_t *wlandev, struct imgchunk *fchunk,
  148. unsigned int nfchunks);
  149. static void free_chunks(struct imgchunk *fchunk, unsigned int *nfchunks);
  150. static void free_srecs(void);
  151. static int validate_identity(void);
  152. /*================================================================*/
  153. /* Function Definitions */
  154. /*----------------------------------------------------------------
  155. * prism2_fwtry
  156. *
  157. * Try and get firmware into memory
  158. *
  159. * Arguments:
  160. * udev usb device structure
  161. * wlandev wlan device structure
  162. *
  163. * Returns:
  164. * 0 - success
  165. * ~0 - failure
  166. ----------------------------------------------------------------*/
  167. static int prism2_fwtry(struct usb_device *udev, wlandevice_t *wlandev)
  168. {
  169. const struct firmware *fw_entry = NULL;
  170. netdev_info(wlandev->netdev, "prism2_usb: Checking for firmware %s\n",
  171. PRISM2_USB_FWFILE);
  172. if (request_ihex_firmware(&fw_entry,
  173. PRISM2_USB_FWFILE, &udev->dev) != 0) {
  174. netdev_info(wlandev->netdev,
  175. "prism2_usb: Firmware not available, but not essential\n");
  176. netdev_info(wlandev->netdev,
  177. "prism2_usb: can continue to use card anyway.\n");
  178. return 1;
  179. }
  180. netdev_info(wlandev->netdev,
  181. "prism2_usb: %s will be processed, size %zu\n",
  182. PRISM2_USB_FWFILE, fw_entry->size);
  183. prism2_fwapply((const struct ihex_binrec *)fw_entry->data, wlandev);
  184. release_firmware(fw_entry);
  185. return 0;
  186. }
  187. /*----------------------------------------------------------------
  188. * prism2_fwapply
  189. *
  190. * Apply the firmware loaded into memory
  191. *
  192. * Arguments:
  193. * rfptr firmware image in kernel memory
  194. * wlandev device
  195. *
  196. * Returns:
  197. * 0 - success
  198. * ~0 - failure
  199. ----------------------------------------------------------------*/
  200. static int prism2_fwapply(const struct ihex_binrec *rfptr, wlandevice_t *wlandev)
  201. {
  202. signed int result = 0;
  203. struct p80211msg_dot11req_mibget getmsg;
  204. p80211itemd_t *item;
  205. u32 *data;
  206. /* Initialize the data structures */
  207. ns3data = 0;
  208. memset(s3data, 0, sizeof(s3data));
  209. ns3plug = 0;
  210. memset(s3plug, 0, sizeof(s3plug));
  211. ns3crc = 0;
  212. memset(s3crc, 0, sizeof(s3crc));
  213. ns3info = 0;
  214. memset(s3info, 0, sizeof(s3info));
  215. startaddr = 0;
  216. nfchunks = 0;
  217. memset(fchunk, 0, sizeof(fchunk));
  218. memset(&nicid, 0, sizeof(nicid));
  219. memset(&rfid, 0, sizeof(rfid));
  220. memset(&macid, 0, sizeof(macid));
  221. memset(&priid, 0, sizeof(priid));
  222. /* clear the pda and add an initial END record */
  223. memset(&pda, 0, sizeof(pda));
  224. pda.rec[0] = (hfa384x_pdrec_t *) pda.buf;
  225. pda.rec[0]->len = cpu_to_le16(2); /* len in words */
  226. pda.rec[0]->code = cpu_to_le16(HFA384x_PDR_END_OF_PDA);
  227. pda.nrec = 1;
  228. /*-----------------------------------------------------*/
  229. /* Put card into fwload state */
  230. prism2sta_ifstate(wlandev, P80211ENUM_ifstate_fwload);
  231. /* Build the PDA we're going to use. */
  232. if (read_cardpda(&pda, wlandev)) {
  233. netdev_err(wlandev->netdev, "load_cardpda failed, exiting.\n");
  234. return 1;
  235. }
  236. /* read the card's PRI-SUP */
  237. memset(&getmsg, 0, sizeof(getmsg));
  238. getmsg.msgcode = DIDmsg_dot11req_mibget;
  239. getmsg.msglen = sizeof(getmsg);
  240. strcpy(getmsg.devname, wlandev->name);
  241. getmsg.mibattribute.did = DIDmsg_dot11req_mibget_mibattribute;
  242. getmsg.mibattribute.status = P80211ENUM_msgitem_status_data_ok;
  243. getmsg.resultcode.did = DIDmsg_dot11req_mibget_resultcode;
  244. getmsg.resultcode.status = P80211ENUM_msgitem_status_no_value;
  245. item = (p80211itemd_t *) getmsg.mibattribute.data;
  246. item->did = DIDmib_p2_p2NIC_p2PRISupRange;
  247. item->status = P80211ENUM_msgitem_status_no_value;
  248. data = (u32 *) item->data;
  249. /* DIDmsg_dot11req_mibget */
  250. prism2mgmt_mibset_mibget(wlandev, &getmsg);
  251. if (getmsg.resultcode.data != P80211ENUM_resultcode_success)
  252. netdev_err(wlandev->netdev, "Couldn't fetch PRI-SUP info\n");
  253. /* Already in host order */
  254. priid.role = *data++;
  255. priid.id = *data++;
  256. priid.variant = *data++;
  257. priid.bottom = *data++;
  258. priid.top = *data++;
  259. /* Read the S3 file */
  260. result = read_fwfile(rfptr);
  261. if (result) {
  262. netdev_err(wlandev->netdev,
  263. "Failed to read the data exiting.\n");
  264. return 1;
  265. }
  266. result = validate_identity();
  267. if (result) {
  268. netdev_err(wlandev->netdev, "Incompatible firmware image.\n");
  269. return 1;
  270. }
  271. if (startaddr == 0x00000000) {
  272. netdev_err(wlandev->netdev,
  273. "Can't RAM download a Flash image!\n");
  274. return 1;
  275. }
  276. /* Make the image chunks */
  277. result = mkimage(fchunk, &nfchunks);
  278. /* Do any plugging */
  279. result = plugimage(fchunk, nfchunks, s3plug, ns3plug, &pda);
  280. if (result) {
  281. netdev_err(wlandev->netdev, "Failed to plug data.\n");
  282. return 1;
  283. }
  284. /* Insert any CRCs */
  285. if (crcimage(fchunk, nfchunks, s3crc, ns3crc)) {
  286. netdev_err(wlandev->netdev, "Failed to insert all CRCs\n");
  287. return 1;
  288. }
  289. /* Write the image */
  290. result = writeimage(wlandev, fchunk, nfchunks);
  291. if (result) {
  292. netdev_err(wlandev->netdev, "Failed to ramwrite image data.\n");
  293. return 1;
  294. }
  295. /* clear any allocated memory */
  296. free_chunks(fchunk, &nfchunks);
  297. free_srecs();
  298. netdev_info(wlandev->netdev, "prism2_usb: firmware loading finished.\n");
  299. return result;
  300. }
  301. /*----------------------------------------------------------------
  302. * crcimage
  303. *
  304. * Adds a CRC16 in the two bytes prior to each block identified by
  305. * an S3 CRC record. Currently, we don't actually do a CRC we just
  306. * insert the value 0xC0DE in hfa384x order.
  307. *
  308. * Arguments:
  309. * fchunk Array of image chunks
  310. * nfchunks Number of image chunks
  311. * s3crc Array of crc records
  312. * ns3crc Number of crc records
  313. *
  314. * Returns:
  315. * 0 success
  316. * ~0 failure
  317. ----------------------------------------------------------------*/
  318. static int crcimage(struct imgchunk *fchunk, unsigned int nfchunks,
  319. struct s3crcrec *s3crc, unsigned int ns3crc)
  320. {
  321. int result = 0;
  322. int i;
  323. int c;
  324. u32 crcstart;
  325. u32 crcend;
  326. u32 cstart = 0;
  327. u32 cend;
  328. u8 *dest;
  329. u32 chunkoff;
  330. for (i = 0; i < ns3crc; i++) {
  331. if (!s3crc[i].dowrite)
  332. continue;
  333. crcstart = s3crc[i].addr;
  334. crcend = s3crc[i].addr + s3crc[i].len;
  335. /* Find chunk */
  336. for (c = 0; c < nfchunks; c++) {
  337. cstart = fchunk[c].addr;
  338. cend = fchunk[c].addr + fchunk[c].len;
  339. /* the line below does an address & len match search */
  340. /* unfortunately, I've found that the len fields of */
  341. /* some crc records don't match with the length of */
  342. /* the actual data, so we're not checking right now */
  343. /* if (crcstart-2 >= cstart && crcend <= cend) break; */
  344. /* note the -2 below, it's to make sure the chunk has */
  345. /* space for the CRC value */
  346. if (crcstart - 2 >= cstart && crcstart < cend)
  347. break;
  348. }
  349. if (c >= nfchunks) {
  350. pr_err("Failed to find chunk for crcrec[%d], addr=0x%06x len=%d , aborting crc.\n",
  351. i, s3crc[i].addr, s3crc[i].len);
  352. return 1;
  353. }
  354. /* Insert crc */
  355. pr_debug("Adding crc @ 0x%06x\n", s3crc[i].addr - 2);
  356. chunkoff = crcstart - cstart - 2;
  357. dest = fchunk[c].data + chunkoff;
  358. *dest = 0xde;
  359. *(dest + 1) = 0xc0;
  360. }
  361. return result;
  362. }
  363. /*----------------------------------------------------------------
  364. * free_chunks
  365. *
  366. * Clears the chunklist data structures in preparation for a new file.
  367. *
  368. * Arguments:
  369. * none
  370. *
  371. * Returns:
  372. * nothing
  373. ----------------------------------------------------------------*/
  374. static void free_chunks(struct imgchunk *fchunk, unsigned int *nfchunks)
  375. {
  376. int i;
  377. for (i = 0; i < *nfchunks; i++)
  378. kfree(fchunk[i].data);
  379. *nfchunks = 0;
  380. memset(fchunk, 0, sizeof(*fchunk));
  381. }
  382. /*----------------------------------------------------------------
  383. * free_srecs
  384. *
  385. * Clears the srec data structures in preparation for a new file.
  386. *
  387. * Arguments:
  388. * none
  389. *
  390. * Returns:
  391. * nothing
  392. ----------------------------------------------------------------*/
  393. static void free_srecs(void)
  394. {
  395. ns3data = 0;
  396. memset(s3data, 0, sizeof(s3data));
  397. ns3plug = 0;
  398. memset(s3plug, 0, sizeof(s3plug));
  399. ns3crc = 0;
  400. memset(s3crc, 0, sizeof(s3crc));
  401. ns3info = 0;
  402. memset(s3info, 0, sizeof(s3info));
  403. startaddr = 0;
  404. }
  405. /*----------------------------------------------------------------
  406. * mkimage
  407. *
  408. * Scans the currently loaded set of S records for data residing
  409. * in contiguous memory regions. Each contiguous region is then
  410. * made into a 'chunk'. This function assumes that we're building
  411. * a new chunk list. Assumes the s3data items are in sorted order.
  412. *
  413. * Arguments: none
  414. *
  415. * Returns:
  416. * 0 - success
  417. * ~0 - failure (probably an errno)
  418. ----------------------------------------------------------------*/
  419. static int mkimage(struct imgchunk *clist, unsigned int *ccnt)
  420. {
  421. int result = 0;
  422. int i;
  423. int j;
  424. int currchunk = 0;
  425. u32 nextaddr = 0;
  426. u32 s3start;
  427. u32 s3end;
  428. u32 cstart = 0;
  429. u32 cend;
  430. u32 coffset;
  431. /* There may already be data in the chunklist */
  432. *ccnt = 0;
  433. /* Establish the location and size of each chunk */
  434. for (i = 0; i < ns3data; i++) {
  435. if (s3data[i].addr == nextaddr) {
  436. /* existing chunk, grow it */
  437. clist[currchunk].len += s3data[i].len;
  438. nextaddr += s3data[i].len;
  439. } else {
  440. /* New chunk */
  441. (*ccnt)++;
  442. currchunk = *ccnt - 1;
  443. clist[currchunk].addr = s3data[i].addr;
  444. clist[currchunk].len = s3data[i].len;
  445. nextaddr = s3data[i].addr + s3data[i].len;
  446. /* Expand the chunk if there is a CRC record at */
  447. /* their beginning bound */
  448. for (j = 0; j < ns3crc; j++) {
  449. if (s3crc[j].dowrite &&
  450. s3crc[j].addr == clist[currchunk].addr) {
  451. clist[currchunk].addr -= 2;
  452. clist[currchunk].len += 2;
  453. }
  454. }
  455. }
  456. }
  457. /* We're currently assuming there aren't any overlapping chunks */
  458. /* if this proves false, we'll need to add code to coalesce. */
  459. /* Allocate buffer space for chunks */
  460. for (i = 0; i < *ccnt; i++) {
  461. clist[i].data = kzalloc(clist[i].len, GFP_KERNEL);
  462. if (clist[i].data == NULL) {
  463. pr_err("failed to allocate image space, exitting.\n");
  464. return 1;
  465. }
  466. pr_debug("chunk[%d]: addr=0x%06x len=%d\n",
  467. i, clist[i].addr, clist[i].len);
  468. }
  469. /* Copy srec data to chunks */
  470. for (i = 0; i < ns3data; i++) {
  471. s3start = s3data[i].addr;
  472. s3end = s3start + s3data[i].len - 1;
  473. for (j = 0; j < *ccnt; j++) {
  474. cstart = clist[j].addr;
  475. cend = cstart + clist[j].len - 1;
  476. if (s3start >= cstart && s3end <= cend)
  477. break;
  478. }
  479. if (((unsigned int)j) >= (*ccnt)) {
  480. pr_err("s3rec(a=0x%06x,l=%d), no chunk match, exiting.\n",
  481. s3start, s3data[i].len);
  482. return 1;
  483. }
  484. coffset = s3start - cstart;
  485. memcpy(clist[j].data + coffset, s3data[i].data, s3data[i].len);
  486. }
  487. return result;
  488. }
  489. /*----------------------------------------------------------------
  490. * mkpdrlist
  491. *
  492. * Reads a raw PDA and builds an array of pdrec_t structures.
  493. *
  494. * Arguments:
  495. * pda buffer containing raw PDA bytes
  496. * pdrec ptr to an array of pdrec_t's. Will be filled on exit.
  497. * nrec ptr to a variable that will contain the count of PDRs
  498. *
  499. * Returns:
  500. * 0 - success
  501. * ~0 - failure (probably an errno)
  502. ----------------------------------------------------------------*/
  503. static int mkpdrlist(struct pda *pda)
  504. {
  505. int result = 0;
  506. u16 *pda16 = (u16 *) pda->buf;
  507. int curroff; /* in 'words' */
  508. pda->nrec = 0;
  509. curroff = 0;
  510. while (curroff < (HFA384x_PDA_LEN_MAX / 2) &&
  511. le16_to_cpu(pda16[curroff + 1]) != HFA384x_PDR_END_OF_PDA) {
  512. pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
  513. if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
  514. HFA384x_PDR_NICID) {
  515. memcpy(&nicid, &pda->rec[pda->nrec]->data.nicid,
  516. sizeof(nicid));
  517. nicid.id = le16_to_cpu(nicid.id);
  518. nicid.variant = le16_to_cpu(nicid.variant);
  519. nicid.major = le16_to_cpu(nicid.major);
  520. nicid.minor = le16_to_cpu(nicid.minor);
  521. }
  522. if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
  523. HFA384x_PDR_MFISUPRANGE) {
  524. memcpy(&rfid, &pda->rec[pda->nrec]->data.mfisuprange,
  525. sizeof(rfid));
  526. rfid.id = le16_to_cpu(rfid.id);
  527. rfid.variant = le16_to_cpu(rfid.variant);
  528. rfid.bottom = le16_to_cpu(rfid.bottom);
  529. rfid.top = le16_to_cpu(rfid.top);
  530. }
  531. if (le16_to_cpu(pda->rec[pda->nrec]->code) ==
  532. HFA384x_PDR_CFISUPRANGE) {
  533. memcpy(&macid, &pda->rec[pda->nrec]->data.cfisuprange,
  534. sizeof(macid));
  535. macid.id = le16_to_cpu(macid.id);
  536. macid.variant = le16_to_cpu(macid.variant);
  537. macid.bottom = le16_to_cpu(macid.bottom);
  538. macid.top = le16_to_cpu(macid.top);
  539. }
  540. (pda->nrec)++;
  541. curroff += le16_to_cpu(pda16[curroff]) + 1;
  542. }
  543. if (curroff >= (HFA384x_PDA_LEN_MAX / 2)) {
  544. pr_err("no end record found or invalid lengths in PDR data, exiting. %x %d\n",
  545. curroff, pda->nrec);
  546. return 1;
  547. }
  548. if (le16_to_cpu(pda16[curroff + 1]) == HFA384x_PDR_END_OF_PDA) {
  549. pda->rec[pda->nrec] = (hfa384x_pdrec_t *) &(pda16[curroff]);
  550. (pda->nrec)++;
  551. }
  552. return result;
  553. }
  554. /*----------------------------------------------------------------
  555. * plugimage
  556. *
  557. * Plugs the given image using the given plug records from the given
  558. * PDA and filename.
  559. *
  560. * Arguments:
  561. * fchunk Array of image chunks
  562. * nfchunks Number of image chunks
  563. * s3plug Array of plug records
  564. * ns3plug Number of plug records
  565. * pda Current pda data
  566. *
  567. * Returns:
  568. * 0 success
  569. * ~0 failure
  570. ----------------------------------------------------------------*/
  571. static int plugimage(struct imgchunk *fchunk, unsigned int nfchunks,
  572. struct s3plugrec *s3plug, unsigned int ns3plug, struct pda *pda)
  573. {
  574. int result = 0;
  575. int i; /* plug index */
  576. int j; /* index of PDR or -1 if fname plug */
  577. int c; /* chunk index */
  578. u32 pstart;
  579. u32 pend;
  580. u32 cstart = 0;
  581. u32 cend;
  582. u32 chunkoff;
  583. u8 *dest;
  584. /* for each plug record */
  585. for (i = 0; i < ns3plug; i++) {
  586. pstart = s3plug[i].addr;
  587. pend = s3plug[i].addr + s3plug[i].len;
  588. /* find the matching PDR (or filename) */
  589. if (s3plug[i].itemcode != 0xffffffffUL) { /* not filename */
  590. for (j = 0; j < pda->nrec; j++) {
  591. if (s3plug[i].itemcode ==
  592. le16_to_cpu(pda->rec[j]->code))
  593. break;
  594. }
  595. } else {
  596. j = -1;
  597. }
  598. if (j >= pda->nrec && j != -1) { /* if no matching PDR, fail */
  599. pr_warn("warning: Failed to find PDR for plugrec 0x%04x.\n",
  600. s3plug[i].itemcode);
  601. continue; /* and move on to the next PDR */
  602. #if 0
  603. /* MSM: They swear that unless it's the MAC address,
  604. * the serial number, or the TX calibration records,
  605. * then there's reasonable defaults in the f/w
  606. * image. Therefore, missing PDRs in the card
  607. * should only be a warning, not fatal.
  608. * TODO: add fatals for the PDRs mentioned above.
  609. */
  610. result = 1;
  611. continue;
  612. #endif
  613. }
  614. /* Validate plug len against PDR len */
  615. if (j != -1 && s3plug[i].len < le16_to_cpu(pda->rec[j]->len)) {
  616. pr_err("error: Plug vs. PDR len mismatch for plugrec 0x%04x, abort plugging.\n",
  617. s3plug[i].itemcode);
  618. result = 1;
  619. continue;
  620. }
  621. /* Validate plug address against chunk data and identify chunk */
  622. for (c = 0; c < nfchunks; c++) {
  623. cstart = fchunk[c].addr;
  624. cend = fchunk[c].addr + fchunk[c].len;
  625. if (pstart >= cstart && pend <= cend)
  626. break;
  627. }
  628. if (c >= nfchunks) {
  629. pr_err("error: Failed to find image chunk for plugrec 0x%04x.\n",
  630. s3plug[i].itemcode);
  631. result = 1;
  632. continue;
  633. }
  634. /* Plug data */
  635. chunkoff = pstart - cstart;
  636. dest = fchunk[c].data + chunkoff;
  637. pr_debug("Plugging item 0x%04x @ 0x%06x, len=%d, cnum=%d coff=0x%06x\n",
  638. s3plug[i].itemcode, pstart, s3plug[i].len,
  639. c, chunkoff);
  640. if (j == -1) { /* plug the filename */
  641. memset(dest, 0, s3plug[i].len);
  642. strncpy(dest, PRISM2_USB_FWFILE, s3plug[i].len - 1);
  643. } else { /* plug a PDR */
  644. memcpy(dest, &(pda->rec[j]->data), s3plug[i].len);
  645. }
  646. }
  647. return result;
  648. }
  649. /*----------------------------------------------------------------
  650. * read_cardpda
  651. *
  652. * Sends the command for the driver to read the pda from the card
  653. * named in the device variable. Upon success, the card pda is
  654. * stored in the "cardpda" variables. Note that the pda structure
  655. * is considered 'well formed' after this function. That means
  656. * that the nrecs is valid, the rec array has been set up, and there's
  657. * a valid PDAEND record in the raw PDA data.
  658. *
  659. * Arguments:
  660. * pda pda structure
  661. * wlandev device
  662. *
  663. * Returns:
  664. * 0 - success
  665. * ~0 - failure (probably an errno)
  666. ----------------------------------------------------------------*/
  667. static int read_cardpda(struct pda *pda, wlandevice_t *wlandev)
  668. {
  669. int result = 0;
  670. struct p80211msg_p2req_readpda *msg;
  671. msg = kzalloc(sizeof(*msg), GFP_KERNEL);
  672. if (!msg)
  673. return -ENOMEM;
  674. /* set up the msg */
  675. msg->msgcode = DIDmsg_p2req_readpda;
  676. msg->msglen = sizeof(msg);
  677. strcpy(msg->devname, wlandev->name);
  678. msg->pda.did = DIDmsg_p2req_readpda_pda;
  679. msg->pda.len = HFA384x_PDA_LEN_MAX;
  680. msg->pda.status = P80211ENUM_msgitem_status_no_value;
  681. msg->resultcode.did = DIDmsg_p2req_readpda_resultcode;
  682. msg->resultcode.len = sizeof(u32);
  683. msg->resultcode.status = P80211ENUM_msgitem_status_no_value;
  684. if (prism2mgmt_readpda(wlandev, msg) != 0) {
  685. /* prism2mgmt_readpda prints an errno if appropriate */
  686. result = -1;
  687. } else if (msg->resultcode.data == P80211ENUM_resultcode_success) {
  688. memcpy(pda->buf, msg->pda.data, HFA384x_PDA_LEN_MAX);
  689. result = mkpdrlist(pda);
  690. } else {
  691. /* resultcode must've been something other than success */
  692. result = -1;
  693. }
  694. kfree(msg);
  695. return result;
  696. }
  697. /*----------------------------------------------------------------
  698. * read_fwfile
  699. *
  700. * Reads the given fw file which should have been compiled from an srec
  701. * file. Each record in the fw file will either be a plain data record,
  702. * a start address record, or other records used for plugging.
  703. *
  704. * Note that data records are expected to be sorted into
  705. * ascending address order in the fw file.
  706. *
  707. * Note also that the start address record, originally an S7 record in
  708. * the srec file, is expected in the fw file to be like a data record but
  709. * with a certain address to make it identifiable.
  710. *
  711. * Here's the SREC format that the fw should have come from:
  712. * S[37]nnaaaaaaaaddd...dddcc
  713. *
  714. * nn - number of bytes starting with the address field
  715. * aaaaaaaa - address in readable (or big endian) format
  716. * dd....dd - 0-245 data bytes (two chars per byte)
  717. * cc - checksum
  718. *
  719. * The S7 record's (there should be only one) address value gets
  720. * converted to an S3 record with address of 0xff400000, with the
  721. * start address being stored as a 4 byte data word. That address is
  722. * the start execution address used for RAM downloads.
  723. *
  724. * The S3 records have a collection of subformats indicated by the
  725. * value of aaaaaaaa:
  726. * 0xff000000 - Plug record, data field format:
  727. * xxxxxxxxaaaaaaaassssssss
  728. * x - PDR code number (little endian)
  729. * a - Address in load image to plug (little endian)
  730. * s - Length of plug data area (little endian)
  731. *
  732. * 0xff100000 - CRC16 generation record, data field format:
  733. * aaaaaaaassssssssbbbbbbbb
  734. * a - Start address for CRC calculation (little endian)
  735. * s - Length of data to calculate over (little endian)
  736. * b - Boolean, true=write crc, false=don't write
  737. *
  738. * 0xff200000 - Info record, data field format:
  739. * ssssttttdd..dd
  740. * s - Size in words (little endian)
  741. * t - Info type (little endian), see #defines and
  742. * struct s3inforec for details about types.
  743. * d - (s - 1) little endian words giving the contents of
  744. * the given info type.
  745. *
  746. * 0xff400000 - Start address record, data field format:
  747. * aaaaaaaa
  748. * a - Address in load image to plug (little endian)
  749. *
  750. * Arguments:
  751. * record firmware image (ihex record structure) in kernel memory
  752. *
  753. * Returns:
  754. * 0 - success
  755. * ~0 - failure (probably an errno)
  756. ----------------------------------------------------------------*/
  757. static int read_fwfile(const struct ihex_binrec *record)
  758. {
  759. int i;
  760. int rcnt = 0;
  761. u16 *tmpinfo;
  762. u16 *ptr16;
  763. u32 *ptr32, len, addr;
  764. pr_debug("Reading fw file ...\n");
  765. while (record) {
  766. rcnt++;
  767. len = be16_to_cpu(record->len);
  768. addr = be32_to_cpu(record->addr);
  769. /* Point into data for different word lengths */
  770. ptr32 = (u32 *) record->data;
  771. ptr16 = (u16 *) record->data;
  772. /* parse what was an S3 srec and put it in the right array */
  773. switch (addr) {
  774. case S3ADDR_START:
  775. startaddr = *ptr32;
  776. pr_debug(" S7 start addr, record=%d addr=0x%08x\n",
  777. rcnt,
  778. startaddr);
  779. break;
  780. case S3ADDR_PLUG:
  781. s3plug[ns3plug].itemcode = *ptr32;
  782. s3plug[ns3plug].addr = *(ptr32 + 1);
  783. s3plug[ns3plug].len = *(ptr32 + 2);
  784. pr_debug(" S3 plugrec, record=%d itemcode=0x%08x addr=0x%08x len=%d\n",
  785. rcnt,
  786. s3plug[ns3plug].itemcode,
  787. s3plug[ns3plug].addr,
  788. s3plug[ns3plug].len);
  789. ns3plug++;
  790. if (ns3plug == S3PLUG_MAX) {
  791. pr_err("S3 plugrec limit reached - aborting\n");
  792. return 1;
  793. }
  794. break;
  795. case S3ADDR_CRC:
  796. s3crc[ns3crc].addr = *ptr32;
  797. s3crc[ns3crc].len = *(ptr32 + 1);
  798. s3crc[ns3crc].dowrite = *(ptr32 + 2);
  799. pr_debug(" S3 crcrec, record=%d addr=0x%08x len=%d write=0x%08x\n",
  800. rcnt,
  801. s3crc[ns3crc].addr,
  802. s3crc[ns3crc].len,
  803. s3crc[ns3crc].dowrite);
  804. ns3crc++;
  805. if (ns3crc == S3CRC_MAX) {
  806. pr_err("S3 crcrec limit reached - aborting\n");
  807. return 1;
  808. }
  809. break;
  810. case S3ADDR_INFO:
  811. s3info[ns3info].len = *ptr16;
  812. s3info[ns3info].type = *(ptr16 + 1);
  813. pr_debug(" S3 inforec, record=%d len=0x%04x type=0x%04x\n",
  814. rcnt,
  815. s3info[ns3info].len,
  816. s3info[ns3info].type);
  817. if (((s3info[ns3info].len - 1) * sizeof(u16)) > sizeof(s3info[ns3info].info)) {
  818. pr_err("S3 inforec length too long - aborting\n");
  819. return 1;
  820. }
  821. tmpinfo = (u16 *)&(s3info[ns3info].info.version);
  822. pr_debug(" info=");
  823. for (i = 0; i < s3info[ns3info].len - 1; i++) {
  824. tmpinfo[i] = *(ptr16 + 2 + i);
  825. pr_debug("%04x ", tmpinfo[i]);
  826. }
  827. pr_debug("\n");
  828. ns3info++;
  829. if (ns3info == S3INFO_MAX) {
  830. pr_err("S3 inforec limit reached - aborting\n");
  831. return 1;
  832. }
  833. break;
  834. default: /* Data record */
  835. s3data[ns3data].addr = addr;
  836. s3data[ns3data].len = len;
  837. s3data[ns3data].data = (uint8_t *) record->data;
  838. ns3data++;
  839. if (ns3data == S3DATA_MAX) {
  840. pr_err("S3 datarec limit reached - aborting\n");
  841. return 1;
  842. }
  843. break;
  844. }
  845. record = ihex_next_binrec(record);
  846. }
  847. return 0;
  848. }
  849. /*----------------------------------------------------------------
  850. * writeimage
  851. *
  852. * Takes the chunks, builds p80211 messages and sends them down
  853. * to the driver for writing to the card.
  854. *
  855. * Arguments:
  856. * wlandev device
  857. * fchunk Array of image chunks
  858. * nfchunks Number of image chunks
  859. *
  860. * Returns:
  861. * 0 success
  862. * ~0 failure
  863. ----------------------------------------------------------------*/
  864. static int writeimage(wlandevice_t *wlandev, struct imgchunk *fchunk,
  865. unsigned int nfchunks)
  866. {
  867. int result = 0;
  868. struct p80211msg_p2req_ramdl_state *rstmsg;
  869. struct p80211msg_p2req_ramdl_write *rwrmsg;
  870. u32 resultcode;
  871. int i;
  872. int j;
  873. unsigned int nwrites;
  874. u32 curroff;
  875. u32 currlen;
  876. u32 currdaddr;
  877. rstmsg = kmalloc(sizeof(*rstmsg), GFP_KERNEL);
  878. rwrmsg = kmalloc(sizeof(*rwrmsg), GFP_KERNEL);
  879. if (!rstmsg || !rwrmsg) {
  880. kfree(rstmsg);
  881. kfree(rwrmsg);
  882. netdev_err(wlandev->netdev,
  883. "writeimage: no memory for firmware download, aborting download\n");
  884. return -ENOMEM;
  885. }
  886. /* Initialize the messages */
  887. memset(rstmsg, 0, sizeof(*rstmsg));
  888. strcpy(rstmsg->devname, wlandev->name);
  889. rstmsg->msgcode = DIDmsg_p2req_ramdl_state;
  890. rstmsg->msglen = sizeof(*rstmsg);
  891. rstmsg->enable.did = DIDmsg_p2req_ramdl_state_enable;
  892. rstmsg->exeaddr.did = DIDmsg_p2req_ramdl_state_exeaddr;
  893. rstmsg->resultcode.did = DIDmsg_p2req_ramdl_state_resultcode;
  894. rstmsg->enable.status = P80211ENUM_msgitem_status_data_ok;
  895. rstmsg->exeaddr.status = P80211ENUM_msgitem_status_data_ok;
  896. rstmsg->resultcode.status = P80211ENUM_msgitem_status_no_value;
  897. rstmsg->enable.len = sizeof(u32);
  898. rstmsg->exeaddr.len = sizeof(u32);
  899. rstmsg->resultcode.len = sizeof(u32);
  900. memset(rwrmsg, 0, sizeof(*rwrmsg));
  901. strcpy(rwrmsg->devname, wlandev->name);
  902. rwrmsg->msgcode = DIDmsg_p2req_ramdl_write;
  903. rwrmsg->msglen = sizeof(*rwrmsg);
  904. rwrmsg->addr.did = DIDmsg_p2req_ramdl_write_addr;
  905. rwrmsg->len.did = DIDmsg_p2req_ramdl_write_len;
  906. rwrmsg->data.did = DIDmsg_p2req_ramdl_write_data;
  907. rwrmsg->resultcode.did = DIDmsg_p2req_ramdl_write_resultcode;
  908. rwrmsg->addr.status = P80211ENUM_msgitem_status_data_ok;
  909. rwrmsg->len.status = P80211ENUM_msgitem_status_data_ok;
  910. rwrmsg->data.status = P80211ENUM_msgitem_status_data_ok;
  911. rwrmsg->resultcode.status = P80211ENUM_msgitem_status_no_value;
  912. rwrmsg->addr.len = sizeof(u32);
  913. rwrmsg->len.len = sizeof(u32);
  914. rwrmsg->data.len = WRITESIZE_MAX;
  915. rwrmsg->resultcode.len = sizeof(u32);
  916. /* Send xxx_state(enable) */
  917. pr_debug("Sending dl_state(enable) message.\n");
  918. rstmsg->enable.data = P80211ENUM_truth_true;
  919. rstmsg->exeaddr.data = startaddr;
  920. result = prism2mgmt_ramdl_state(wlandev, rstmsg);
  921. if (result) {
  922. netdev_err(wlandev->netdev,
  923. "writeimage state enable failed w/ result=%d, aborting download\n",
  924. result);
  925. goto free_result;
  926. }
  927. resultcode = rstmsg->resultcode.data;
  928. if (resultcode != P80211ENUM_resultcode_success) {
  929. netdev_err(wlandev->netdev,
  930. "writeimage()->xxxdl_state msg indicates failure, w/ resultcode=%d, aborting download.\n",
  931. resultcode);
  932. result = 1;
  933. goto free_result;
  934. }
  935. /* Now, loop through the data chunks and send WRITESIZE_MAX data */
  936. for (i = 0; i < nfchunks; i++) {
  937. nwrites = fchunk[i].len / WRITESIZE_MAX;
  938. nwrites += (fchunk[i].len % WRITESIZE_MAX) ? 1 : 0;
  939. curroff = 0;
  940. for (j = 0; j < nwrites; j++) {
  941. /* TODO Move this to a separate function */
  942. int lenleft = fchunk[i].len - (WRITESIZE_MAX * j);
  943. if (fchunk[i].len > WRITESIZE_MAX)
  944. currlen = WRITESIZE_MAX;
  945. else
  946. currlen = lenleft;
  947. curroff = j * WRITESIZE_MAX;
  948. currdaddr = fchunk[i].addr + curroff;
  949. /* Setup the message */
  950. rwrmsg->addr.data = currdaddr;
  951. rwrmsg->len.data = currlen;
  952. memcpy(rwrmsg->data.data,
  953. fchunk[i].data + curroff, currlen);
  954. /* Send flashdl_write(pda) */
  955. pr_debug
  956. ("Sending xxxdl_write message addr=%06x len=%d.\n",
  957. currdaddr, currlen);
  958. result = prism2mgmt_ramdl_write(wlandev, rwrmsg);
  959. /* Check the results */
  960. if (result) {
  961. netdev_err(wlandev->netdev,
  962. "writeimage chunk write failed w/ result=%d, aborting download\n",
  963. result);
  964. goto free_result;
  965. }
  966. resultcode = rstmsg->resultcode.data;
  967. if (resultcode != P80211ENUM_resultcode_success) {
  968. pr_err("writeimage()->xxxdl_write msg indicates failure, w/ resultcode=%d, aborting download.\n",
  969. resultcode);
  970. result = 1;
  971. goto free_result;
  972. }
  973. }
  974. }
  975. /* Send xxx_state(disable) */
  976. pr_debug("Sending dl_state(disable) message.\n");
  977. rstmsg->enable.data = P80211ENUM_truth_false;
  978. rstmsg->exeaddr.data = 0;
  979. result = prism2mgmt_ramdl_state(wlandev, rstmsg);
  980. if (result) {
  981. netdev_err(wlandev->netdev,
  982. "writeimage state disable failed w/ result=%d, aborting download\n",
  983. result);
  984. goto free_result;
  985. }
  986. resultcode = rstmsg->resultcode.data;
  987. if (resultcode != P80211ENUM_resultcode_success) {
  988. netdev_err(wlandev->netdev,
  989. "writeimage()->xxxdl_state msg indicates failure, w/ resultcode=%d, aborting download.\n",
  990. resultcode);
  991. result = 1;
  992. goto free_result;
  993. }
  994. free_result:
  995. kfree(rstmsg);
  996. kfree(rwrmsg);
  997. return result;
  998. }
  999. static int validate_identity(void)
  1000. {
  1001. int i;
  1002. int result = 1;
  1003. int trump = 0;
  1004. pr_debug("NIC ID: %#x v%d.%d.%d\n",
  1005. nicid.id, nicid.major, nicid.minor, nicid.variant);
  1006. pr_debug("MFI ID: %#x v%d %d->%d\n",
  1007. rfid.id, rfid.variant, rfid.bottom, rfid.top);
  1008. pr_debug("CFI ID: %#x v%d %d->%d\n",
  1009. macid.id, macid.variant, macid.bottom, macid.top);
  1010. pr_debug("PRI ID: %#x v%d %d->%d\n",
  1011. priid.id, priid.variant, priid.bottom, priid.top);
  1012. for (i = 0; i < ns3info; i++) {
  1013. switch (s3info[i].type) {
  1014. case 1:
  1015. pr_debug("Version: ID %#x %d.%d.%d\n",
  1016. s3info[i].info.version.id,
  1017. s3info[i].info.version.major,
  1018. s3info[i].info.version.minor,
  1019. s3info[i].info.version.variant);
  1020. break;
  1021. case 2:
  1022. pr_debug("Compat: Role %#x Id %#x v%d %d->%d\n",
  1023. s3info[i].info.compat.role,
  1024. s3info[i].info.compat.id,
  1025. s3info[i].info.compat.variant,
  1026. s3info[i].info.compat.bottom,
  1027. s3info[i].info.compat.top);
  1028. /* MAC compat range */
  1029. if ((s3info[i].info.compat.role == 1) &&
  1030. (s3info[i].info.compat.id == 2)) {
  1031. if (s3info[i].info.compat.variant !=
  1032. macid.variant) {
  1033. result = 2;
  1034. }
  1035. }
  1036. /* PRI compat range */
  1037. if ((s3info[i].info.compat.role == 1) &&
  1038. (s3info[i].info.compat.id == 3)) {
  1039. if ((s3info[i].info.compat.bottom > priid.top)
  1040. || (s3info[i].info.compat.top <
  1041. priid.bottom)) {
  1042. result = 3;
  1043. }
  1044. }
  1045. /* SEC compat range */
  1046. if ((s3info[i].info.compat.role == 1) &&
  1047. (s3info[i].info.compat.id == 4)) {
  1048. /* FIXME: isn't something missing here? */
  1049. }
  1050. break;
  1051. case 3:
  1052. pr_debug("Seq: %#x\n", s3info[i].info.buildseq);
  1053. break;
  1054. case 4:
  1055. pr_debug("Platform: ID %#x %d.%d.%d\n",
  1056. s3info[i].info.version.id,
  1057. s3info[i].info.version.major,
  1058. s3info[i].info.version.minor,
  1059. s3info[i].info.version.variant);
  1060. if (nicid.id != s3info[i].info.version.id)
  1061. continue;
  1062. if (nicid.major != s3info[i].info.version.major)
  1063. continue;
  1064. if (nicid.minor != s3info[i].info.version.minor)
  1065. continue;
  1066. if ((nicid.variant != s3info[i].info.version.variant) &&
  1067. (nicid.id != 0x8008))
  1068. continue;
  1069. trump = 1;
  1070. break;
  1071. case 0x8001:
  1072. pr_debug("name inforec len %d\n", s3info[i].len);
  1073. break;
  1074. default:
  1075. pr_debug("Unknown inforec type %d\n", s3info[i].type);
  1076. }
  1077. }
  1078. /* walk through */
  1079. if (trump && (result != 2))
  1080. result = 0;
  1081. return result;
  1082. }