fw.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373
  1. /*
  2. * Intel Wireless WiMAX Connection 2400m
  3. * Firmware uploader
  4. *
  5. *
  6. * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name of Intel Corporation nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. *
  35. * Intel Corporation <linux-wimax@intel.com>
  36. * Yanir Lubetkin <yanirx.lubetkin@intel.com>
  37. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  38. * - Initial implementation
  39. *
  40. *
  41. * THE PROCEDURE
  42. *
  43. * The 2400m and derived devices work in two modes: boot-mode or
  44. * normal mode. In boot mode we can execute only a handful of commands
  45. * targeted at uploading the firmware and launching it.
  46. *
  47. * The 2400m enters boot mode when it is first connected to the
  48. * system, when it crashes and when you ask it to reboot. There are
  49. * two submodes of the boot mode: signed and non-signed. Signed takes
  50. * firmwares signed with a certain private key, non-signed takes any
  51. * firmware. Normal hardware takes only signed firmware.
  52. *
  53. * On boot mode, in USB, we write to the device using the bulk out
  54. * endpoint and read from it in the notification endpoint. In SDIO we
  55. * talk to it via the write address and read from the read address.
  56. *
  57. * Upon entrance to boot mode, the device sends (preceeded with a few
  58. * zero length packets (ZLPs) on the notification endpoint in USB) a
  59. * reboot barker (4 le32 words with the same value). We ack it by
  60. * sending the same barker to the device. The device acks with a
  61. * reboot ack barker (4 le32 words with value I2400M_ACK_BARKER) and
  62. * then is fully booted. At this point we can upload the firmware.
  63. *
  64. * Note that different iterations of the device and EEPROM
  65. * configurations will send different [re]boot barkers; these are
  66. * collected in i2400m_barker_db along with the firmware
  67. * characteristics they require.
  68. *
  69. * This process is accomplished by the i2400m_bootrom_init()
  70. * function. All the device interaction happens through the
  71. * i2400m_bm_cmd() [boot mode command]. Special return values will
  72. * indicate if the device did reset during the process.
  73. *
  74. * After this, we read the MAC address and then (if needed)
  75. * reinitialize the device. We need to read it ahead of time because
  76. * in the future, we might not upload the firmware until userspace
  77. * 'ifconfig up's the device.
  78. *
  79. * We can then upload the firmware file. The file is composed of a BCF
  80. * header (basic data, keys and signatures) and a list of write
  81. * commands and payloads. We first upload the header
  82. * [i2400m_dnload_init()] and then pass the commands and payloads
  83. * verbatim to the i2400m_bm_cmd() function
  84. * [i2400m_dnload_bcf()]. Then we tell the device to jump to the new
  85. * firmware [i2400m_dnload_finalize()].
  86. *
  87. * Once firmware is uploaded, we are good to go :)
  88. *
  89. * When we don't know in which mode we are, we first try by sending a
  90. * warm reset request that will take us to boot-mode. If we time out
  91. * waiting for a reboot barker, that means maybe we are already in
  92. * boot mode, so we send a reboot barker.
  93. *
  94. * COMMAND EXECUTION
  95. *
  96. * This code (and process) is single threaded; for executing commands,
  97. * we post a URB to the notification endpoint, post the command, wait
  98. * for data on the notification buffer. We don't need to worry about
  99. * others as we know we are the only ones in there.
  100. *
  101. * BACKEND IMPLEMENTATION
  102. *
  103. * This code is bus-generic; the bus-specific driver provides back end
  104. * implementations to send a boot mode command to the device and to
  105. * read an acknolwedgement from it (or an asynchronous notification)
  106. * from it.
  107. *
  108. * ROADMAP
  109. *
  110. * i2400m_barker_db_init Called by i2400m_driver_init()
  111. * i2400m_barker_db_add
  112. *
  113. * i2400m_barker_db_exit Called by i2400m_driver_exit()
  114. *
  115. * i2400m_dev_bootstrap Called by __i2400m_dev_start()
  116. * request_firmware
  117. * i2400m_fw_check
  118. * i2400m_fw_dnload
  119. * release_firmware
  120. *
  121. * i2400m_fw_dnload
  122. * i2400m_bootrom_init
  123. * i2400m_bm_cmd
  124. * i2400m->bus_reset
  125. * i2400m_dnload_init
  126. * i2400m_dnload_init_signed
  127. * i2400m_dnload_init_nonsigned
  128. * i2400m_download_chunk
  129. * i2400m_bm_cmd
  130. * i2400m_dnload_bcf
  131. * i2400m_bm_cmd
  132. * i2400m_dnload_finalize
  133. * i2400m_bm_cmd
  134. *
  135. * i2400m_bm_cmd
  136. * i2400m->bus_bm_cmd_send()
  137. * i2400m->bus_bm_wait_for_ack
  138. * __i2400m_bm_ack_verify
  139. * i2400m_is_boot_barker
  140. *
  141. * i2400m_bm_cmd_prepare Used by bus-drivers to prep
  142. * commands before sending
  143. */
  144. #include <linux/firmware.h>
  145. #include <linux/sched.h>
  146. #include <linux/usb.h>
  147. #include "i2400m.h"
  148. #define D_SUBMODULE fw
  149. #include "debug-levels.h"
  150. static const __le32 i2400m_ACK_BARKER[4] = {
  151. cpu_to_le32(I2400M_ACK_BARKER),
  152. cpu_to_le32(I2400M_ACK_BARKER),
  153. cpu_to_le32(I2400M_ACK_BARKER),
  154. cpu_to_le32(I2400M_ACK_BARKER)
  155. };
  156. /**
  157. * Prepare a boot-mode command for delivery
  158. *
  159. * @cmd: pointer to bootrom header to prepare
  160. *
  161. * Computes checksum if so needed. After calling this function, DO NOT
  162. * modify the command or header as the checksum won't work anymore.
  163. *
  164. * We do it from here because some times we cannot do it in the
  165. * original context the command was sent (it is a const), so when we
  166. * copy it to our staging buffer, we add the checksum there.
  167. */
  168. void i2400m_bm_cmd_prepare(struct i2400m_bootrom_header *cmd)
  169. {
  170. if (i2400m_brh_get_use_checksum(cmd)) {
  171. int i;
  172. u32 checksum = 0;
  173. const u32 *checksum_ptr = (void *) cmd->payload;
  174. for (i = 0; i < cmd->data_size / 4; i++)
  175. checksum += cpu_to_le32(*checksum_ptr++);
  176. checksum += cmd->command + cmd->target_addr + cmd->data_size;
  177. cmd->block_checksum = cpu_to_le32(checksum);
  178. }
  179. }
  180. EXPORT_SYMBOL_GPL(i2400m_bm_cmd_prepare);
  181. /*
  182. * Database of known barkers.
  183. *
  184. * A barker is what the device sends indicating he is ready to be
  185. * bootloaded. Different versions of the device will send different
  186. * barkers. Depending on the barker, it might mean the device wants
  187. * some kind of firmware or the other.
  188. */
  189. static struct i2400m_barker_db {
  190. __le32 data[4];
  191. } *i2400m_barker_db;
  192. static size_t i2400m_barker_db_used, i2400m_barker_db_size;
  193. static
  194. int i2400m_zrealloc_2x(void **ptr, size_t *_count, size_t el_size,
  195. gfp_t gfp_flags)
  196. {
  197. size_t old_count = *_count,
  198. new_count = old_count ? 2 * old_count : 2,
  199. old_size = el_size * old_count,
  200. new_size = el_size * new_count;
  201. void *nptr = krealloc(*ptr, new_size, gfp_flags);
  202. if (nptr) {
  203. /* zero the other half or the whole thing if old_count
  204. * was zero */
  205. if (old_size == 0)
  206. memset(nptr, 0, new_size);
  207. else
  208. memset(nptr + old_size, 0, old_size);
  209. *_count = new_count;
  210. *ptr = nptr;
  211. return 0;
  212. } else
  213. return -ENOMEM;
  214. }
  215. /*
  216. * Add a barker to the database
  217. *
  218. * This cannot used outside of this module and only at at module_init
  219. * time. This is to avoid the need to do locking.
  220. */
  221. static
  222. int i2400m_barker_db_add(u32 barker_id)
  223. {
  224. int result;
  225. struct i2400m_barker_db *barker;
  226. if (i2400m_barker_db_used >= i2400m_barker_db_size) {
  227. result = i2400m_zrealloc_2x(
  228. (void **) &i2400m_barker_db, &i2400m_barker_db_size,
  229. sizeof(i2400m_barker_db[0]), GFP_KERNEL);
  230. if (result < 0)
  231. return result;
  232. }
  233. barker = i2400m_barker_db + i2400m_barker_db_used++;
  234. barker->data[0] = le32_to_cpu(barker_id);
  235. barker->data[1] = le32_to_cpu(barker_id);
  236. barker->data[2] = le32_to_cpu(barker_id);
  237. barker->data[3] = le32_to_cpu(barker_id);
  238. return 0;
  239. }
  240. void i2400m_barker_db_exit(void)
  241. {
  242. kfree(i2400m_barker_db);
  243. i2400m_barker_db = NULL;
  244. i2400m_barker_db_size = 0;
  245. i2400m_barker_db_used = 0;
  246. }
  247. /*
  248. * Helper function to add all the known stable barkers to the barker
  249. * database.
  250. */
  251. static
  252. int i2400m_barker_db_known_barkers(void)
  253. {
  254. int result;
  255. result = i2400m_barker_db_add(I2400M_NBOOT_BARKER);
  256. if (result < 0)
  257. goto error_add;
  258. result = i2400m_barker_db_add(I2400M_SBOOT_BARKER);
  259. if (result < 0)
  260. goto error_add;
  261. error_add:
  262. return result;
  263. }
  264. /*
  265. * Initialize the barker database
  266. *
  267. * This can only be used from the module_init function for this
  268. * module; this is to avoid the need to do locking.
  269. *
  270. * @options: command line argument with extra barkers to
  271. * recognize. This is a comma-separated list of 32-bit hex
  272. * numbers. They are appended to the existing list. Setting 0
  273. * cleans the existing list and starts a new one.
  274. */
  275. int i2400m_barker_db_init(const char *_options)
  276. {
  277. int result;
  278. char *options = NULL, *options_orig, *token;
  279. i2400m_barker_db = NULL;
  280. i2400m_barker_db_size = 0;
  281. i2400m_barker_db_used = 0;
  282. result = i2400m_barker_db_known_barkers();
  283. if (result < 0)
  284. goto error_add;
  285. /* parse command line options from i2400m.barkers */
  286. if (_options != NULL) {
  287. unsigned barker;
  288. options_orig = kstrdup(_options, GFP_KERNEL);
  289. if (options_orig == NULL)
  290. goto error_parse;
  291. options = options_orig;
  292. while ((token = strsep(&options, ",")) != NULL) {
  293. if (*token == '\0') /* eat joint commas */
  294. continue;
  295. if (sscanf(token, "%x", &barker) != 1
  296. || barker > 0xffffffff) {
  297. printk(KERN_ERR "%s: can't recognize "
  298. "i2400m.barkers value '%s' as "
  299. "a 32-bit number\n",
  300. __func__, token);
  301. result = -EINVAL;
  302. goto error_parse;
  303. }
  304. if (barker == 0) {
  305. /* clean list and start new */
  306. i2400m_barker_db_exit();
  307. continue;
  308. }
  309. result = i2400m_barker_db_add(barker);
  310. if (result < 0)
  311. goto error_add;
  312. }
  313. kfree(options_orig);
  314. }
  315. return 0;
  316. error_parse:
  317. error_add:
  318. kfree(i2400m_barker_db);
  319. return result;
  320. }
  321. /*
  322. * Recognize a boot barker
  323. *
  324. * @buf: buffer where the boot barker.
  325. * @buf_size: size of the buffer (has to be 16 bytes). It is passed
  326. * here so the function can check it for the caller.
  327. *
  328. * Note that as a side effect, upon identifying the obtained boot
  329. * barker, this function will set i2400m->barker to point to the right
  330. * barker database entry. Subsequent calls to the function will result
  331. * in verifying that the same type of boot barker is returned when the
  332. * device [re]boots (as long as the same device instance is used).
  333. *
  334. * Return: 0 if @buf matches a known boot barker. -ENOENT if the
  335. * buffer in @buf doesn't match any boot barker in the database or
  336. * -EILSEQ if the buffer doesn't have the right size.
  337. */
  338. int i2400m_is_boot_barker(struct i2400m *i2400m,
  339. const void *buf, size_t buf_size)
  340. {
  341. int result;
  342. struct device *dev = i2400m_dev(i2400m);
  343. struct i2400m_barker_db *barker;
  344. int i;
  345. result = -ENOENT;
  346. if (buf_size != sizeof(i2400m_barker_db[i].data))
  347. return result;
  348. /* Short circuit if we have already discovered the barker
  349. * associated with the device. */
  350. if (i2400m->barker
  351. && !memcmp(buf, i2400m->barker, sizeof(i2400m->barker->data))) {
  352. unsigned index = (i2400m->barker - i2400m_barker_db)
  353. / sizeof(*i2400m->barker);
  354. d_printf(2, dev, "boot barker cache-confirmed #%u/%08x\n",
  355. index, le32_to_cpu(i2400m->barker->data[0]));
  356. return 0;
  357. }
  358. for (i = 0; i < i2400m_barker_db_used; i++) {
  359. barker = &i2400m_barker_db[i];
  360. BUILD_BUG_ON(sizeof(barker->data) != 16);
  361. if (memcmp(buf, barker->data, sizeof(barker->data)))
  362. continue;
  363. if (i2400m->barker == NULL) {
  364. i2400m->barker = barker;
  365. d_printf(1, dev, "boot barker set to #%u/%08x\n",
  366. i, le32_to_cpu(barker->data[0]));
  367. if (barker->data[0] == le32_to_cpu(I2400M_NBOOT_BARKER))
  368. i2400m->sboot = 0;
  369. else
  370. i2400m->sboot = 1;
  371. } else if (i2400m->barker != barker) {
  372. dev_err(dev, "HW inconsistency: device "
  373. "reports a different boot barker "
  374. "than set (from %08x to %08x)\n",
  375. le32_to_cpu(i2400m->barker->data[0]),
  376. le32_to_cpu(barker->data[0]));
  377. result = -EIO;
  378. } else
  379. d_printf(2, dev, "boot barker confirmed #%u/%08x\n",
  380. i, le32_to_cpu(barker->data[0]));
  381. result = 0;
  382. break;
  383. }
  384. return result;
  385. }
  386. EXPORT_SYMBOL_GPL(i2400m_is_boot_barker);
  387. /*
  388. * Verify the ack data received
  389. *
  390. * Given a reply to a boot mode command, chew it and verify everything
  391. * is ok.
  392. *
  393. * @opcode: opcode which generated this ack. For error messages.
  394. * @ack: pointer to ack data we received
  395. * @ack_size: size of that data buffer
  396. * @flags: I2400M_BM_CMD_* flags we called the command with.
  397. *
  398. * Way too long function -- maybe it should be further split
  399. */
  400. static
  401. ssize_t __i2400m_bm_ack_verify(struct i2400m *i2400m, int opcode,
  402. struct i2400m_bootrom_header *ack,
  403. size_t ack_size, int flags)
  404. {
  405. ssize_t result = -ENOMEM;
  406. struct device *dev = i2400m_dev(i2400m);
  407. d_fnstart(8, dev, "(i2400m %p opcode %d ack %p size %zu)\n",
  408. i2400m, opcode, ack, ack_size);
  409. if (ack_size < sizeof(*ack)) {
  410. result = -EIO;
  411. dev_err(dev, "boot-mode cmd %d: HW BUG? notification didn't "
  412. "return enough data (%zu bytes vs %zu expected)\n",
  413. opcode, ack_size, sizeof(*ack));
  414. goto error_ack_short;
  415. }
  416. result = i2400m_is_boot_barker(i2400m, ack, ack_size);
  417. if (result >= 0) {
  418. result = -ERESTARTSYS;
  419. d_printf(6, dev, "boot-mode cmd %d: HW boot barker\n", opcode);
  420. goto error_reboot;
  421. }
  422. if (ack_size == sizeof(i2400m_ACK_BARKER)
  423. && memcmp(ack, i2400m_ACK_BARKER, sizeof(*ack)) == 0) {
  424. result = -EISCONN;
  425. d_printf(3, dev, "boot-mode cmd %d: HW reboot ack barker\n",
  426. opcode);
  427. goto error_reboot_ack;
  428. }
  429. result = 0;
  430. if (flags & I2400M_BM_CMD_RAW)
  431. goto out_raw;
  432. ack->data_size = le32_to_cpu(ack->data_size);
  433. ack->target_addr = le32_to_cpu(ack->target_addr);
  434. ack->block_checksum = le32_to_cpu(ack->block_checksum);
  435. d_printf(5, dev, "boot-mode cmd %d: notification for opcode %u "
  436. "response %u csum %u rr %u da %u\n",
  437. opcode, i2400m_brh_get_opcode(ack),
  438. i2400m_brh_get_response(ack),
  439. i2400m_brh_get_use_checksum(ack),
  440. i2400m_brh_get_response_required(ack),
  441. i2400m_brh_get_direct_access(ack));
  442. result = -EIO;
  443. if (i2400m_brh_get_signature(ack) != 0xcbbc) {
  444. dev_err(dev, "boot-mode cmd %d: HW BUG? wrong signature "
  445. "0x%04x\n", opcode, i2400m_brh_get_signature(ack));
  446. goto error_ack_signature;
  447. }
  448. if (opcode != -1 && opcode != i2400m_brh_get_opcode(ack)) {
  449. dev_err(dev, "boot-mode cmd %d: HW BUG? "
  450. "received response for opcode %u, expected %u\n",
  451. opcode, i2400m_brh_get_opcode(ack), opcode);
  452. goto error_ack_opcode;
  453. }
  454. if (i2400m_brh_get_response(ack) != 0) { /* failed? */
  455. dev_err(dev, "boot-mode cmd %d: error; hw response %u\n",
  456. opcode, i2400m_brh_get_response(ack));
  457. goto error_ack_failed;
  458. }
  459. if (ack_size < ack->data_size + sizeof(*ack)) {
  460. dev_err(dev, "boot-mode cmd %d: SW BUG "
  461. "driver provided only %zu bytes for %zu bytes "
  462. "of data\n", opcode, ack_size,
  463. (size_t) le32_to_cpu(ack->data_size) + sizeof(*ack));
  464. goto error_ack_short_buffer;
  465. }
  466. result = ack_size;
  467. /* Don't you love this stack of empty targets? Well, I don't
  468. * either, but it helps track exactly who comes in here and
  469. * why :) */
  470. error_ack_short_buffer:
  471. error_ack_failed:
  472. error_ack_opcode:
  473. error_ack_signature:
  474. out_raw:
  475. error_reboot_ack:
  476. error_reboot:
  477. error_ack_short:
  478. d_fnend(8, dev, "(i2400m %p opcode %d ack %p size %zu) = %d\n",
  479. i2400m, opcode, ack, ack_size, (int) result);
  480. return result;
  481. }
  482. /**
  483. * i2400m_bm_cmd - Execute a boot mode command
  484. *
  485. * @cmd: buffer containing the command data (pointing at the header).
  486. * This data can be ANYWHERE (for USB, we will copy it to an
  487. * specific buffer). Make sure everything is in proper little
  488. * endian.
  489. *
  490. * A raw buffer can be also sent, just cast it and set flags to
  491. * I2400M_BM_CMD_RAW.
  492. *
  493. * This function will generate a checksum for you if the
  494. * checksum bit in the command is set (unless I2400M_BM_CMD_RAW
  495. * is set).
  496. *
  497. * You can use the i2400m->bm_cmd_buf to stage your commands and
  498. * send them.
  499. *
  500. * If NULL, no command is sent (we just wait for an ack).
  501. *
  502. * @cmd_size: size of the command. Will be auto padded to the
  503. * bus-specific drivers padding requirements.
  504. *
  505. * @ack: buffer where to place the acknowledgement. If it is a regular
  506. * command response, all fields will be returned with the right,
  507. * native endianess.
  508. *
  509. * You *cannot* use i2400m->bm_ack_buf for this buffer.
  510. *
  511. * @ack_size: size of @ack, 16 aligned; you need to provide at least
  512. * sizeof(*ack) bytes and then enough to contain the return data
  513. * from the command
  514. *
  515. * @flags: see I2400M_BM_CMD_* above.
  516. *
  517. * @returns: bytes received by the notification; if < 0, an errno code
  518. * denoting an error or:
  519. *
  520. * -ERESTARTSYS The device has rebooted
  521. *
  522. * Executes a boot-mode command and waits for a response, doing basic
  523. * validation on it; if a zero length response is received, it retries
  524. * waiting for a response until a non-zero one is received (timing out
  525. * after %I2400M_BOOT_RETRIES retries).
  526. */
  527. static
  528. ssize_t i2400m_bm_cmd(struct i2400m *i2400m,
  529. const struct i2400m_bootrom_header *cmd, size_t cmd_size,
  530. struct i2400m_bootrom_header *ack, size_t ack_size,
  531. int flags)
  532. {
  533. ssize_t result = -ENOMEM, rx_bytes;
  534. struct device *dev = i2400m_dev(i2400m);
  535. int opcode = cmd == NULL ? -1 : i2400m_brh_get_opcode(cmd);
  536. d_fnstart(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu)\n",
  537. i2400m, cmd, cmd_size, ack, ack_size);
  538. BUG_ON(ack_size < sizeof(*ack));
  539. BUG_ON(i2400m->boot_mode == 0);
  540. if (cmd != NULL) { /* send the command */
  541. result = i2400m->bus_bm_cmd_send(i2400m, cmd, cmd_size, flags);
  542. if (result < 0)
  543. goto error_cmd_send;
  544. if ((flags & I2400M_BM_CMD_RAW) == 0)
  545. d_printf(5, dev,
  546. "boot-mode cmd %d csum %u rr %u da %u: "
  547. "addr 0x%04x size %u block csum 0x%04x\n",
  548. opcode, i2400m_brh_get_use_checksum(cmd),
  549. i2400m_brh_get_response_required(cmd),
  550. i2400m_brh_get_direct_access(cmd),
  551. cmd->target_addr, cmd->data_size,
  552. cmd->block_checksum);
  553. }
  554. result = i2400m->bus_bm_wait_for_ack(i2400m, ack, ack_size);
  555. if (result < 0) {
  556. dev_err(dev, "boot-mode cmd %d: error waiting for an ack: %d\n",
  557. opcode, (int) result); /* bah, %zd doesn't work */
  558. goto error_wait_for_ack;
  559. }
  560. rx_bytes = result;
  561. /* verify the ack and read more if neccessary [result is the
  562. * final amount of bytes we get in the ack] */
  563. result = __i2400m_bm_ack_verify(i2400m, opcode, ack, ack_size, flags);
  564. if (result < 0)
  565. goto error_bad_ack;
  566. /* Don't you love this stack of empty targets? Well, I don't
  567. * either, but it helps track exactly who comes in here and
  568. * why :) */
  569. result = rx_bytes;
  570. error_bad_ack:
  571. error_wait_for_ack:
  572. error_cmd_send:
  573. d_fnend(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu) = %d\n",
  574. i2400m, cmd, cmd_size, ack, ack_size, (int) result);
  575. return result;
  576. }
  577. /**
  578. * i2400m_download_chunk - write a single chunk of data to the device's memory
  579. *
  580. * @i2400m: device descriptor
  581. * @buf: the buffer to write
  582. * @buf_len: length of the buffer to write
  583. * @addr: address in the device memory space
  584. * @direct: bootrom write mode
  585. * @do_csum: should a checksum validation be performed
  586. */
  587. static int i2400m_download_chunk(struct i2400m *i2400m, const void *chunk,
  588. size_t __chunk_len, unsigned long addr,
  589. unsigned int direct, unsigned int do_csum)
  590. {
  591. int ret;
  592. size_t chunk_len = ALIGN(__chunk_len, I2400M_PL_ALIGN);
  593. struct device *dev = i2400m_dev(i2400m);
  594. struct {
  595. struct i2400m_bootrom_header cmd;
  596. u8 cmd_payload[chunk_len];
  597. } __attribute__((packed)) *buf;
  598. struct i2400m_bootrom_header ack;
  599. d_fnstart(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
  600. "direct %u do_csum %u)\n", i2400m, chunk, __chunk_len,
  601. addr, direct, do_csum);
  602. buf = i2400m->bm_cmd_buf;
  603. memcpy(buf->cmd_payload, chunk, __chunk_len);
  604. memset(buf->cmd_payload + __chunk_len, 0xad, chunk_len - __chunk_len);
  605. buf->cmd.command = i2400m_brh_command(I2400M_BRH_WRITE,
  606. __chunk_len & 0x3 ? 0 : do_csum,
  607. __chunk_len & 0xf ? 0 : direct);
  608. buf->cmd.target_addr = cpu_to_le32(addr);
  609. buf->cmd.data_size = cpu_to_le32(__chunk_len);
  610. ret = i2400m_bm_cmd(i2400m, &buf->cmd, sizeof(buf->cmd) + chunk_len,
  611. &ack, sizeof(ack), 0);
  612. if (ret >= 0)
  613. ret = 0;
  614. d_fnend(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
  615. "direct %u do_csum %u) = %d\n", i2400m, chunk, __chunk_len,
  616. addr, direct, do_csum, ret);
  617. return ret;
  618. }
  619. /*
  620. * Download a BCF file's sections to the device
  621. *
  622. * @i2400m: device descriptor
  623. * @bcf: pointer to firmware data (followed by the payloads). Assumed
  624. * verified and consistent.
  625. * @bcf_len: length (in bytes) of the @bcf buffer.
  626. *
  627. * Returns: < 0 errno code on error or the offset to the jump instruction.
  628. *
  629. * Given a BCF file, downloads each section (a command and a payload)
  630. * to the device's address space. Actually, it just executes each
  631. * command i the BCF file.
  632. *
  633. * The section size has to be aligned to 4 bytes AND the padding has
  634. * to be taken from the firmware file, as the signature takes it into
  635. * account.
  636. */
  637. static
  638. ssize_t i2400m_dnload_bcf(struct i2400m *i2400m,
  639. const struct i2400m_bcf_hdr *bcf, size_t bcf_len)
  640. {
  641. ssize_t ret;
  642. struct device *dev = i2400m_dev(i2400m);
  643. size_t offset, /* iterator offset */
  644. data_size, /* Size of the data payload */
  645. section_size, /* Size of the whole section (cmd + payload) */
  646. section = 1;
  647. const struct i2400m_bootrom_header *bh;
  648. struct i2400m_bootrom_header ack;
  649. d_fnstart(3, dev, "(i2400m %p bcf %p bcf_len %zu)\n",
  650. i2400m, bcf, bcf_len);
  651. /* Iterate over the command blocks in the BCF file that start
  652. * after the header */
  653. offset = le32_to_cpu(bcf->header_len) * sizeof(u32);
  654. while (1) { /* start sending the file */
  655. bh = (void *) bcf + offset;
  656. data_size = le32_to_cpu(bh->data_size);
  657. section_size = ALIGN(sizeof(*bh) + data_size, 4);
  658. d_printf(7, dev,
  659. "downloading section #%zu (@%zu %zu B) to 0x%08x\n",
  660. section, offset, sizeof(*bh) + data_size,
  661. le32_to_cpu(bh->target_addr));
  662. if (i2400m_brh_get_opcode(bh) == I2400M_BRH_SIGNED_JUMP) {
  663. /* Secure boot needs to stop here */
  664. d_printf(5, dev, "signed jump found @%zu\n", offset);
  665. break;
  666. }
  667. if (offset + section_size == bcf_len)
  668. /* Non-secure boot stops here */
  669. break;
  670. if (offset + section_size > bcf_len) {
  671. dev_err(dev, "fw %s: bad section #%zu, "
  672. "end (@%zu) beyond EOF (@%zu)\n",
  673. i2400m->fw_name, section,
  674. offset + section_size, bcf_len);
  675. ret = -EINVAL;
  676. goto error_section_beyond_eof;
  677. }
  678. __i2400m_msleep(20);
  679. ret = i2400m_bm_cmd(i2400m, bh, section_size,
  680. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  681. if (ret < 0) {
  682. dev_err(dev, "fw %s: section #%zu (@%zu %zu B) "
  683. "failed %d\n", i2400m->fw_name, section,
  684. offset, sizeof(*bh) + data_size, (int) ret);
  685. goto error_send;
  686. }
  687. offset += section_size;
  688. section++;
  689. }
  690. ret = offset;
  691. error_section_beyond_eof:
  692. error_send:
  693. d_fnend(3, dev, "(i2400m %p bcf %p bcf_len %zu) = %d\n",
  694. i2400m, bcf, bcf_len, (int) ret);
  695. return ret;
  696. }
  697. /*
  698. * Indicate if the device emitted a reboot barker that indicates
  699. * "signed boot"
  700. */
  701. static
  702. unsigned i2400m_boot_is_signed(struct i2400m *i2400m)
  703. {
  704. return likely(i2400m->sboot);
  705. }
  706. /*
  707. * Do the final steps of uploading firmware
  708. *
  709. * Depending on the boot mode (signed vs non-signed), different
  710. * actions need to be taken.
  711. */
  712. static
  713. int i2400m_dnload_finalize(struct i2400m *i2400m,
  714. const struct i2400m_bcf_hdr *bcf, size_t offset)
  715. {
  716. int ret = 0;
  717. struct device *dev = i2400m_dev(i2400m);
  718. struct i2400m_bootrom_header *cmd, ack;
  719. struct {
  720. struct i2400m_bootrom_header cmd;
  721. u8 cmd_pl[0];
  722. } __attribute__((packed)) *cmd_buf;
  723. size_t signature_block_offset, signature_block_size;
  724. d_fnstart(3, dev, "offset %zu\n", offset);
  725. cmd = (void *) bcf + offset;
  726. if (i2400m_boot_is_signed(i2400m) == 0) {
  727. struct i2400m_bootrom_header jump_ack;
  728. d_printf(1, dev, "unsecure boot, jumping to 0x%08x\n",
  729. le32_to_cpu(cmd->target_addr));
  730. cmd_buf = i2400m->bm_cmd_buf;
  731. memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
  732. cmd = &cmd_buf->cmd;
  733. /* now cmd points to the actual bootrom_header in cmd_buf */
  734. i2400m_brh_set_opcode(cmd, I2400M_BRH_JUMP);
  735. cmd->data_size = 0;
  736. ret = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  737. &jump_ack, sizeof(jump_ack), 0);
  738. } else {
  739. d_printf(1, dev, "secure boot, jumping to 0x%08x\n",
  740. le32_to_cpu(cmd->target_addr));
  741. cmd_buf = i2400m->bm_cmd_buf;
  742. memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
  743. signature_block_offset =
  744. sizeof(*bcf)
  745. + le32_to_cpu(bcf->key_size) * sizeof(u32)
  746. + le32_to_cpu(bcf->exponent_size) * sizeof(u32);
  747. signature_block_size =
  748. le32_to_cpu(bcf->modulus_size) * sizeof(u32);
  749. memcpy(cmd_buf->cmd_pl, (void *) bcf + signature_block_offset,
  750. signature_block_size);
  751. ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd,
  752. sizeof(cmd_buf->cmd) + signature_block_size,
  753. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  754. }
  755. d_fnend(3, dev, "returning %d\n", ret);
  756. return ret;
  757. }
  758. /**
  759. * i2400m_bootrom_init - Reboots a powered device into boot mode
  760. *
  761. * @i2400m: device descriptor
  762. * @flags:
  763. * I2400M_BRI_SOFT: a reboot barker has been seen
  764. * already, so don't wait for it.
  765. *
  766. * I2400M_BRI_NO_REBOOT: Don't send a reboot command, but wait
  767. * for a reboot barker notification. This is a one shot; if
  768. * the state machine needs to send a reboot command it will.
  769. *
  770. * Returns:
  771. *
  772. * < 0 errno code on error, 0 if ok.
  773. *
  774. * Description:
  775. *
  776. * Tries hard enough to put the device in boot-mode. There are two
  777. * main phases to this:
  778. *
  779. * a. (1) send a reboot command and (2) get a reboot barker
  780. *
  781. * b. (1) echo/ack the reboot sending the reboot barker back and (2)
  782. * getting an ack barker in return
  783. *
  784. * We want to skip (a) in some cases [soft]. The state machine is
  785. * horrible, but it is basically: on each phase, send what has to be
  786. * sent (if any), wait for the answer and act on the answer. We might
  787. * have to backtrack and retry, so we keep a max tries counter for
  788. * that.
  789. *
  790. * It sucks because we don't know ahead of time which is going to be
  791. * the reboot barker (the device might send different ones depending
  792. * on its EEPROM config) and once the device reboots and waits for the
  793. * echo/ack reboot barker being sent back, it doesn't understand
  794. * anything else. So we can be left at the point where we don't know
  795. * what to send to it -- cold reset and bus reset seem to have little
  796. * effect. So the function iterates (in this case) through all the
  797. * known barkers and tries them all until an ACK is
  798. * received. Otherwise, it gives up.
  799. *
  800. * If we get a timeout after sending a warm reset, we do it again.
  801. */
  802. int i2400m_bootrom_init(struct i2400m *i2400m, enum i2400m_bri flags)
  803. {
  804. int result;
  805. struct device *dev = i2400m_dev(i2400m);
  806. struct i2400m_bootrom_header *cmd;
  807. struct i2400m_bootrom_header ack;
  808. int count = i2400m->bus_bm_retries;
  809. int ack_timeout_cnt = 1;
  810. unsigned i;
  811. BUILD_BUG_ON(sizeof(*cmd) != sizeof(i2400m_barker_db[0].data));
  812. BUILD_BUG_ON(sizeof(ack) != sizeof(i2400m_ACK_BARKER));
  813. d_fnstart(4, dev, "(i2400m %p flags 0x%08x)\n", i2400m, flags);
  814. result = -ENOMEM;
  815. cmd = i2400m->bm_cmd_buf;
  816. if (flags & I2400M_BRI_SOFT)
  817. goto do_reboot_ack;
  818. do_reboot:
  819. ack_timeout_cnt = 1;
  820. if (--count < 0)
  821. goto error_timeout;
  822. d_printf(4, dev, "device reboot: reboot command [%d # left]\n",
  823. count);
  824. if ((flags & I2400M_BRI_NO_REBOOT) == 0)
  825. i2400m->bus_reset(i2400m, I2400M_RT_WARM);
  826. result = i2400m_bm_cmd(i2400m, NULL, 0, &ack, sizeof(ack),
  827. I2400M_BM_CMD_RAW);
  828. flags &= ~I2400M_BRI_NO_REBOOT;
  829. switch (result) {
  830. case -ERESTARTSYS:
  831. /*
  832. * at this point, i2400m_bm_cmd(), through
  833. * __i2400m_bm_ack_process(), has updated
  834. * i2400m->barker and we are good to go.
  835. */
  836. d_printf(4, dev, "device reboot: got reboot barker\n");
  837. break;
  838. case -EISCONN: /* we don't know how it got here...but we follow it */
  839. d_printf(4, dev, "device reboot: got ack barker - whatever\n");
  840. goto do_reboot;
  841. case -ETIMEDOUT:
  842. /*
  843. * Device has timed out, we might be in boot mode
  844. * already and expecting an ack; if we don't know what
  845. * the barker is, we just send them all. Cold reset
  846. * and bus reset don't work. Beats me.
  847. */
  848. if (i2400m->barker != NULL) {
  849. dev_err(dev, "device boot: reboot barker timed out, "
  850. "trying (set) %08x echo/ack\n",
  851. le32_to_cpu(i2400m->barker->data[0]));
  852. goto do_reboot_ack;
  853. }
  854. for (i = 0; i < i2400m_barker_db_used; i++) {
  855. struct i2400m_barker_db *barker = &i2400m_barker_db[i];
  856. memcpy(cmd, barker->data, sizeof(barker->data));
  857. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  858. &ack, sizeof(ack),
  859. I2400M_BM_CMD_RAW);
  860. if (result == -EISCONN) {
  861. dev_warn(dev, "device boot: got ack barker "
  862. "after sending echo/ack barker "
  863. "#%d/%08x; rebooting j.i.c.\n",
  864. i, le32_to_cpu(barker->data[0]));
  865. flags &= ~I2400M_BRI_NO_REBOOT;
  866. goto do_reboot;
  867. }
  868. }
  869. dev_err(dev, "device boot: tried all the echo/acks, could "
  870. "not get device to respond; giving up");
  871. result = -ESHUTDOWN;
  872. case -EPROTO:
  873. case -ESHUTDOWN: /* dev is gone */
  874. case -EINTR: /* user cancelled */
  875. goto error_dev_gone;
  876. default:
  877. dev_err(dev, "device reboot: error %d while waiting "
  878. "for reboot barker - rebooting\n", result);
  879. d_dump(1, dev, &ack, result);
  880. goto do_reboot;
  881. }
  882. /* At this point we ack back with 4 REBOOT barkers and expect
  883. * 4 ACK barkers. This is ugly, as we send a raw command --
  884. * hence the cast. _bm_cmd() will catch the reboot ack
  885. * notification and report it as -EISCONN. */
  886. do_reboot_ack:
  887. d_printf(4, dev, "device reboot ack: sending ack [%d # left]\n", count);
  888. memcpy(cmd, i2400m->barker->data, sizeof(i2400m->barker->data));
  889. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  890. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  891. switch (result) {
  892. case -ERESTARTSYS:
  893. d_printf(4, dev, "reboot ack: got reboot barker - retrying\n");
  894. if (--count < 0)
  895. goto error_timeout;
  896. goto do_reboot_ack;
  897. case -EISCONN:
  898. d_printf(4, dev, "reboot ack: got ack barker - good\n");
  899. break;
  900. case -ETIMEDOUT: /* no response, maybe it is the other type? */
  901. if (ack_timeout_cnt-- < 0) {
  902. d_printf(4, dev, "reboot ack timedout: retrying\n");
  903. goto do_reboot_ack;
  904. } else {
  905. dev_err(dev, "reboot ack timedout too long: "
  906. "trying reboot\n");
  907. goto do_reboot;
  908. }
  909. break;
  910. case -EPROTO:
  911. case -ESHUTDOWN: /* dev is gone */
  912. goto error_dev_gone;
  913. default:
  914. dev_err(dev, "device reboot ack: error %d while waiting for "
  915. "reboot ack barker - rebooting\n", result);
  916. goto do_reboot;
  917. }
  918. d_printf(2, dev, "device reboot ack: got ack barker - boot done\n");
  919. result = 0;
  920. exit_timeout:
  921. error_dev_gone:
  922. d_fnend(4, dev, "(i2400m %p flags 0x%08x) = %d\n",
  923. i2400m, flags, result);
  924. return result;
  925. error_timeout:
  926. dev_err(dev, "Timed out waiting for reboot ack\n");
  927. result = -ETIMEDOUT;
  928. goto exit_timeout;
  929. }
  930. /*
  931. * Read the MAC addr
  932. *
  933. * The position this function reads is fixed in device memory and
  934. * always available, even without firmware.
  935. *
  936. * Note we specify we want to read only six bytes, but provide space
  937. * for 16, as we always get it rounded up.
  938. */
  939. int i2400m_read_mac_addr(struct i2400m *i2400m)
  940. {
  941. int result;
  942. struct device *dev = i2400m_dev(i2400m);
  943. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  944. struct i2400m_bootrom_header *cmd;
  945. struct {
  946. struct i2400m_bootrom_header ack;
  947. u8 ack_pl[16];
  948. } __attribute__((packed)) ack_buf;
  949. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  950. cmd = i2400m->bm_cmd_buf;
  951. cmd->command = i2400m_brh_command(I2400M_BRH_READ, 0, 1);
  952. cmd->target_addr = cpu_to_le32(0x00203fe8);
  953. cmd->data_size = cpu_to_le32(6);
  954. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  955. &ack_buf.ack, sizeof(ack_buf), 0);
  956. if (result < 0) {
  957. dev_err(dev, "BM: read mac addr failed: %d\n", result);
  958. goto error_read_mac;
  959. }
  960. d_printf(2, dev,
  961. "mac addr is %02x:%02x:%02x:%02x:%02x:%02x\n",
  962. ack_buf.ack_pl[0], ack_buf.ack_pl[1],
  963. ack_buf.ack_pl[2], ack_buf.ack_pl[3],
  964. ack_buf.ack_pl[4], ack_buf.ack_pl[5]);
  965. if (i2400m->bus_bm_mac_addr_impaired == 1) {
  966. ack_buf.ack_pl[0] = 0x00;
  967. ack_buf.ack_pl[1] = 0x16;
  968. ack_buf.ack_pl[2] = 0xd3;
  969. get_random_bytes(&ack_buf.ack_pl[3], 3);
  970. dev_err(dev, "BM is MAC addr impaired, faking MAC addr to "
  971. "mac addr is %02x:%02x:%02x:%02x:%02x:%02x\n",
  972. ack_buf.ack_pl[0], ack_buf.ack_pl[1],
  973. ack_buf.ack_pl[2], ack_buf.ack_pl[3],
  974. ack_buf.ack_pl[4], ack_buf.ack_pl[5]);
  975. result = 0;
  976. }
  977. net_dev->addr_len = ETH_ALEN;
  978. memcpy(net_dev->perm_addr, ack_buf.ack_pl, ETH_ALEN);
  979. memcpy(net_dev->dev_addr, ack_buf.ack_pl, ETH_ALEN);
  980. error_read_mac:
  981. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, result);
  982. return result;
  983. }
  984. /*
  985. * Initialize a non signed boot
  986. *
  987. * This implies sending some magic values to the device's memory. Note
  988. * we convert the values to little endian in the same array
  989. * declaration.
  990. */
  991. static
  992. int i2400m_dnload_init_nonsigned(struct i2400m *i2400m)
  993. {
  994. unsigned i = 0;
  995. int ret = 0;
  996. struct device *dev = i2400m_dev(i2400m);
  997. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  998. if (i2400m->bus_bm_pokes_table) {
  999. while (i2400m->bus_bm_pokes_table[i].address) {
  1000. ret = i2400m_download_chunk(
  1001. i2400m,
  1002. &i2400m->bus_bm_pokes_table[i].data,
  1003. sizeof(i2400m->bus_bm_pokes_table[i].data),
  1004. i2400m->bus_bm_pokes_table[i].address, 1, 1);
  1005. if (ret < 0)
  1006. break;
  1007. i++;
  1008. }
  1009. }
  1010. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1011. return ret;
  1012. }
  1013. /*
  1014. * Initialize the signed boot process
  1015. *
  1016. * @i2400m: device descriptor
  1017. *
  1018. * @bcf_hdr: pointer to the firmware header; assumes it is fully in
  1019. * memory (it has gone through basic validation).
  1020. *
  1021. * Returns: 0 if ok, < 0 errno code on error, -ERESTARTSYS if the hw
  1022. * rebooted.
  1023. *
  1024. * This writes the firmware BCF header to the device using the
  1025. * HASH_PAYLOAD_ONLY command.
  1026. */
  1027. static
  1028. int i2400m_dnload_init_signed(struct i2400m *i2400m,
  1029. const struct i2400m_bcf_hdr *bcf_hdr)
  1030. {
  1031. int ret;
  1032. struct device *dev = i2400m_dev(i2400m);
  1033. struct {
  1034. struct i2400m_bootrom_header cmd;
  1035. struct i2400m_bcf_hdr cmd_pl;
  1036. } __attribute__((packed)) *cmd_buf;
  1037. struct i2400m_bootrom_header ack;
  1038. d_fnstart(5, dev, "(i2400m %p bcf_hdr %p)\n", i2400m, bcf_hdr);
  1039. cmd_buf = i2400m->bm_cmd_buf;
  1040. cmd_buf->cmd.command =
  1041. i2400m_brh_command(I2400M_BRH_HASH_PAYLOAD_ONLY, 0, 0);
  1042. cmd_buf->cmd.target_addr = 0;
  1043. cmd_buf->cmd.data_size = cpu_to_le32(sizeof(cmd_buf->cmd_pl));
  1044. memcpy(&cmd_buf->cmd_pl, bcf_hdr, sizeof(*bcf_hdr));
  1045. ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd, sizeof(*cmd_buf),
  1046. &ack, sizeof(ack), 0);
  1047. if (ret >= 0)
  1048. ret = 0;
  1049. d_fnend(5, dev, "(i2400m %p bcf_hdr %p) = %d\n", i2400m, bcf_hdr, ret);
  1050. return ret;
  1051. }
  1052. /*
  1053. * Initialize the firmware download at the device size
  1054. *
  1055. * Multiplex to the one that matters based on the device's mode
  1056. * (signed or non-signed).
  1057. */
  1058. static
  1059. int i2400m_dnload_init(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf)
  1060. {
  1061. int result;
  1062. struct device *dev = i2400m_dev(i2400m);
  1063. if (i2400m_boot_is_signed(i2400m)) {
  1064. d_printf(1, dev, "signed boot\n");
  1065. result = i2400m_dnload_init_signed(i2400m, bcf);
  1066. if (result == -ERESTARTSYS)
  1067. return result;
  1068. if (result < 0)
  1069. dev_err(dev, "firmware %s: signed boot download "
  1070. "initialization failed: %d\n",
  1071. i2400m->fw_name, result);
  1072. } else {
  1073. /* non-signed boot process without pokes */
  1074. d_printf(1, dev, "non-signed boot\n");
  1075. result = i2400m_dnload_init_nonsigned(i2400m);
  1076. if (result == -ERESTARTSYS)
  1077. return result;
  1078. if (result < 0)
  1079. dev_err(dev, "firmware %s: non-signed download "
  1080. "initialization failed: %d\n",
  1081. i2400m->fw_name, result);
  1082. }
  1083. return result;
  1084. }
  1085. /*
  1086. * Run quick consistency tests on the firmware file
  1087. *
  1088. * Check for the firmware being made for the i2400m device,
  1089. * etc...These checks are mostly informative, as the device will make
  1090. * them too; but the driver's response is more informative on what
  1091. * went wrong.
  1092. */
  1093. static
  1094. int i2400m_fw_check(struct i2400m *i2400m,
  1095. const struct i2400m_bcf_hdr *bcf,
  1096. size_t bcf_size)
  1097. {
  1098. int result;
  1099. struct device *dev = i2400m_dev(i2400m);
  1100. unsigned module_type, header_len, major_version, minor_version,
  1101. module_id, module_vendor, date, size;
  1102. /* Check hard errors */
  1103. result = -EINVAL;
  1104. if (bcf_size < sizeof(*bcf)) { /* big enough header? */
  1105. dev_err(dev, "firmware %s too short: "
  1106. "%zu B vs %zu (at least) expected\n",
  1107. i2400m->fw_name, bcf_size, sizeof(*bcf));
  1108. goto error;
  1109. }
  1110. module_type = bcf->module_type;
  1111. header_len = sizeof(u32) * le32_to_cpu(bcf->header_len);
  1112. major_version = (le32_to_cpu(bcf->header_version) & 0xffff0000) >> 16;
  1113. minor_version = le32_to_cpu(bcf->header_version) & 0x0000ffff;
  1114. module_id = le32_to_cpu(bcf->module_id);
  1115. module_vendor = le32_to_cpu(bcf->module_vendor);
  1116. date = le32_to_cpu(bcf->date);
  1117. size = sizeof(u32) * le32_to_cpu(bcf->size);
  1118. if (bcf_size != size) { /* annoyingly paranoid */
  1119. dev_err(dev, "firmware %s: bad size, got "
  1120. "%zu B vs %u expected\n",
  1121. i2400m->fw_name, bcf_size, size);
  1122. goto error;
  1123. }
  1124. d_printf(2, dev, "type 0x%x id 0x%x vendor 0x%x; header v%u.%u (%zu B) "
  1125. "date %08x (%zu B)\n",
  1126. module_type, module_id, module_vendor,
  1127. major_version, minor_version, (size_t) header_len,
  1128. date, (size_t) size);
  1129. if (module_type != 6) { /* built for the right hardware? */
  1130. dev_err(dev, "bad fw %s: unexpected module type 0x%x; "
  1131. "aborting\n", i2400m->fw_name, module_type);
  1132. goto error;
  1133. }
  1134. if (major_version != 1) {
  1135. dev_err(dev, "%s: major header version v%u.%u not supported\n",
  1136. i2400m->fw_name, major_version, minor_version);
  1137. goto error;
  1138. }
  1139. /* Check soft-er errors */
  1140. result = 0;
  1141. if (module_vendor != 0x8086)
  1142. dev_err(dev, "bad fw %s? unexpected vendor 0x%04x\n",
  1143. i2400m->fw_name, module_vendor);
  1144. if (date < 0x20080300)
  1145. dev_err(dev, "bad fw %s? build date too old %08x\n",
  1146. i2400m->fw_name, date);
  1147. error:
  1148. return result;
  1149. }
  1150. /*
  1151. * Download the firmware to the device
  1152. *
  1153. * @i2400m: device descriptor
  1154. * @bcf: pointer to loaded (and minimally verified for consistency)
  1155. * firmware
  1156. * @bcf_size: size of the @bcf buffer (header plus payloads)
  1157. *
  1158. * The process for doing this is described in this file's header.
  1159. *
  1160. * Note we only reinitialize boot-mode if the flags say so. Some hw
  1161. * iterations need it, some don't. In any case, if we loop, we always
  1162. * need to reinitialize the boot room, hence the flags modification.
  1163. */
  1164. static
  1165. int i2400m_fw_dnload(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf,
  1166. size_t bcf_size, enum i2400m_bri flags)
  1167. {
  1168. int ret = 0;
  1169. struct device *dev = i2400m_dev(i2400m);
  1170. int count = i2400m->bus_bm_retries;
  1171. d_fnstart(5, dev, "(i2400m %p bcf %p size %zu)\n",
  1172. i2400m, bcf, bcf_size);
  1173. i2400m->boot_mode = 1;
  1174. wmb(); /* Make sure other readers see it */
  1175. hw_reboot:
  1176. if (count-- == 0) {
  1177. ret = -ERESTARTSYS;
  1178. dev_err(dev, "device rebooted too many times, aborting\n");
  1179. goto error_too_many_reboots;
  1180. }
  1181. if (flags & I2400M_BRI_MAC_REINIT) {
  1182. ret = i2400m_bootrom_init(i2400m, flags);
  1183. if (ret < 0) {
  1184. dev_err(dev, "bootrom init failed: %d\n", ret);
  1185. goto error_bootrom_init;
  1186. }
  1187. }
  1188. flags |= I2400M_BRI_MAC_REINIT;
  1189. /*
  1190. * Initialize the download, push the bytes to the device and
  1191. * then jump to the new firmware. Note @ret is passed with the
  1192. * offset of the jump instruction to _dnload_finalize()
  1193. */
  1194. ret = i2400m_dnload_init(i2400m, bcf); /* Init device's dnload */
  1195. if (ret == -ERESTARTSYS)
  1196. goto error_dev_rebooted;
  1197. if (ret < 0)
  1198. goto error_dnload_init;
  1199. ret = i2400m_dnload_bcf(i2400m, bcf, bcf_size);
  1200. if (ret == -ERESTARTSYS)
  1201. goto error_dev_rebooted;
  1202. if (ret < 0) {
  1203. dev_err(dev, "fw %s: download failed: %d\n",
  1204. i2400m->fw_name, ret);
  1205. goto error_dnload_bcf;
  1206. }
  1207. ret = i2400m_dnload_finalize(i2400m, bcf, ret);
  1208. if (ret == -ERESTARTSYS)
  1209. goto error_dev_rebooted;
  1210. if (ret < 0) {
  1211. dev_err(dev, "fw %s: "
  1212. "download finalization failed: %d\n",
  1213. i2400m->fw_name, ret);
  1214. goto error_dnload_finalize;
  1215. }
  1216. d_printf(2, dev, "fw %s successfully uploaded\n",
  1217. i2400m->fw_name);
  1218. i2400m->boot_mode = 0;
  1219. wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
  1220. error_dnload_finalize:
  1221. error_dnload_bcf:
  1222. error_dnload_init:
  1223. error_bootrom_init:
  1224. error_too_many_reboots:
  1225. d_fnend(5, dev, "(i2400m %p bcf %p size %zu) = %d\n",
  1226. i2400m, bcf, bcf_size, ret);
  1227. return ret;
  1228. error_dev_rebooted:
  1229. dev_err(dev, "device rebooted, %d tries left\n", count);
  1230. /* we got the notification already, no need to wait for it again */
  1231. flags |= I2400M_BRI_SOFT;
  1232. goto hw_reboot;
  1233. }
  1234. /**
  1235. * i2400m_dev_bootstrap - Bring the device to a known state and upload firmware
  1236. *
  1237. * @i2400m: device descriptor
  1238. *
  1239. * Returns: >= 0 if ok, < 0 errno code on error.
  1240. *
  1241. * This sets up the firmware upload environment, loads the firmware
  1242. * file from disk, verifies and then calls the firmware upload process
  1243. * per se.
  1244. *
  1245. * Can be called either from probe, or after a warm reset. Can not be
  1246. * called from within an interrupt. All the flow in this code is
  1247. * single-threade; all I/Os are synchronous.
  1248. */
  1249. int i2400m_dev_bootstrap(struct i2400m *i2400m, enum i2400m_bri flags)
  1250. {
  1251. int ret, itr;
  1252. struct device *dev = i2400m_dev(i2400m);
  1253. const struct firmware *fw;
  1254. const struct i2400m_bcf_hdr *bcf; /* Firmware data */
  1255. const char *fw_name;
  1256. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  1257. /* Load firmware files to memory. */
  1258. for (itr = 0, bcf = NULL, ret = -ENOENT; ; itr++) {
  1259. fw_name = i2400m->bus_fw_names[itr];
  1260. if (fw_name == NULL) {
  1261. dev_err(dev, "Could not find a usable firmware image\n");
  1262. ret = -ENOENT;
  1263. break;
  1264. }
  1265. d_printf(1, dev, "trying firmware %s (%d)\n", fw_name, itr);
  1266. ret = request_firmware(&fw, fw_name, dev);
  1267. if (ret < 0) {
  1268. dev_err(dev, "fw %s: cannot load file: %d\n",
  1269. fw_name, ret);
  1270. continue;
  1271. }
  1272. bcf = (void *) fw->data;
  1273. i2400m->fw_name = fw_name;
  1274. ret = i2400m_fw_check(i2400m, bcf, fw->size);
  1275. if (ret >= 0) {
  1276. ret = i2400m_fw_dnload(i2400m, bcf, fw->size, flags);
  1277. if (ret >= 0)
  1278. break;
  1279. } else
  1280. dev_err(dev, "%s: cannot use, skipping\n", fw_name);
  1281. release_firmware(fw);
  1282. }
  1283. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1284. return ret;
  1285. }
  1286. EXPORT_SYMBOL_GPL(i2400m_dev_bootstrap);