fw.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  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. * (this is decribed for USB, but for SDIO is similar)
  44. *
  45. * The 2400m works in two modes: boot-mode or normal mode. In boot
  46. * mode we can execute only a handful of commands targeted at
  47. * uploading the firmware and launching it.
  48. *
  49. * The 2400m enters boot mode when it is first connected to the
  50. * system, when it crashes and when you ask it to reboot. There are
  51. * two submodes of the boot mode: signed and non-signed. Signed takes
  52. * firmwares signed with a certain private key, non-signed takes any
  53. * firmware. Normal hardware takes only signed firmware.
  54. *
  55. * Upon entrance to boot mode, the device sends a few zero length
  56. * packets (ZLPs) on the notification endpoint, then a reboot barker
  57. * (4 le32 words with value I2400M_{S,N}BOOT_BARKER). We ack it by
  58. * sending the same barker on the bulk out endpoint. The device acks
  59. * with a reboot ack barker (4 le32 words with value 0xfeedbabe) and
  60. * then the device is fully rebooted. At this point we can upload the
  61. * firmware.
  62. *
  63. * This process is accomplished by the i2400m_bootrom_init()
  64. * function. All the device interaction happens through the
  65. * i2400m_bm_cmd() [boot mode command]. Special return values will
  66. * indicate if the device resets.
  67. *
  68. * After this, we read the MAC address and then (if needed)
  69. * reinitialize the device. We need to read it ahead of time because
  70. * in the future, we might not upload the firmware until userspace
  71. * 'ifconfig up's the device.
  72. *
  73. * We can then upload the firmware file. The file is composed of a BCF
  74. * header (basic data, keys and signatures) and a list of write
  75. * commands and payloads. We first upload the header
  76. * [i2400m_dnload_init()] and then pass the commands and payloads
  77. * verbatim to the i2400m_bm_cmd() function
  78. * [i2400m_dnload_bcf()]. Then we tell the device to jump to the new
  79. * firmware [i2400m_dnload_finalize()].
  80. *
  81. * Once firmware is uploaded, we are good to go :)
  82. *
  83. * When we don't know in which mode we are, we first try by sending a
  84. * warm reset request that will take us to boot-mode. If we time out
  85. * waiting for a reboot barker, that means maybe we are already in
  86. * boot mode, so we send a reboot barker.
  87. *
  88. * COMMAND EXECUTION
  89. *
  90. * This code (and process) is single threaded; for executing commands,
  91. * we post a URB to the notification endpoint, post the command, wait
  92. * for data on the notification buffer. We don't need to worry about
  93. * others as we know we are the only ones in there.
  94. *
  95. * BACKEND IMPLEMENTATION
  96. *
  97. * This code is bus-generic; the bus-specific driver provides back end
  98. * implementations to send a boot mode command to the device and to
  99. * read an acknolwedgement from it (or an asynchronous notification)
  100. * from it.
  101. *
  102. * ROADMAP
  103. *
  104. * i2400m_dev_bootstrap Called by __i2400m_dev_start()
  105. * request_firmware
  106. * i2400m_fw_check
  107. * i2400m_fw_dnload
  108. * release_firmware
  109. *
  110. * i2400m_fw_dnload
  111. * i2400m_bootrom_init
  112. * i2400m_bm_cmd
  113. * i2400m->bus_reset
  114. * i2400m_dnload_init
  115. * i2400m_dnload_init_signed
  116. * i2400m_dnload_init_nonsigned
  117. * i2400m_download_chunk
  118. * i2400m_bm_cmd
  119. * i2400m_dnload_bcf
  120. * i2400m_bm_cmd
  121. * i2400m_dnload_finalize
  122. * i2400m_bm_cmd
  123. *
  124. * i2400m_bm_cmd
  125. * i2400m->bus_bm_cmd_send()
  126. * i2400m->bus_bm_wait_for_ack
  127. * __i2400m_bm_ack_verify
  128. *
  129. * i2400m_bm_cmd_prepare Used by bus-drivers to prep
  130. * commands before sending
  131. */
  132. #include <linux/firmware.h>
  133. #include <linux/sched.h>
  134. #include <linux/usb.h>
  135. #include "i2400m.h"
  136. #define D_SUBMODULE fw
  137. #include "debug-levels.h"
  138. static const __le32 i2400m_ACK_BARKER[4] = {
  139. cpu_to_le32(I2400M_ACK_BARKER),
  140. cpu_to_le32(I2400M_ACK_BARKER),
  141. cpu_to_le32(I2400M_ACK_BARKER),
  142. cpu_to_le32(I2400M_ACK_BARKER)
  143. };
  144. /**
  145. * Prepare a boot-mode command for delivery
  146. *
  147. * @cmd: pointer to bootrom header to prepare
  148. *
  149. * Computes checksum if so needed. After calling this function, DO NOT
  150. * modify the command or header as the checksum won't work anymore.
  151. *
  152. * We do it from here because some times we cannot do it in the
  153. * original context the command was sent (it is a const), so when we
  154. * copy it to our staging buffer, we add the checksum there.
  155. */
  156. void i2400m_bm_cmd_prepare(struct i2400m_bootrom_header *cmd)
  157. {
  158. if (i2400m_brh_get_use_checksum(cmd)) {
  159. int i;
  160. u32 checksum = 0;
  161. const u32 *checksum_ptr = (void *) cmd->payload;
  162. for (i = 0; i < cmd->data_size / 4; i++)
  163. checksum += cpu_to_le32(*checksum_ptr++);
  164. checksum += cmd->command + cmd->target_addr + cmd->data_size;
  165. cmd->block_checksum = cpu_to_le32(checksum);
  166. }
  167. }
  168. EXPORT_SYMBOL_GPL(i2400m_bm_cmd_prepare);
  169. /*
  170. * Verify the ack data received
  171. *
  172. * Given a reply to a boot mode command, chew it and verify everything
  173. * is ok.
  174. *
  175. * @opcode: opcode which generated this ack. For error messages.
  176. * @ack: pointer to ack data we received
  177. * @ack_size: size of that data buffer
  178. * @flags: I2400M_BM_CMD_* flags we called the command with.
  179. *
  180. * Way too long function -- maybe it should be further split
  181. */
  182. static
  183. ssize_t __i2400m_bm_ack_verify(struct i2400m *i2400m, int opcode,
  184. struct i2400m_bootrom_header *ack,
  185. size_t ack_size, int flags)
  186. {
  187. ssize_t result = -ENOMEM;
  188. struct device *dev = i2400m_dev(i2400m);
  189. d_fnstart(8, dev, "(i2400m %p opcode %d ack %p size %zu)\n",
  190. i2400m, opcode, ack, ack_size);
  191. if (ack_size < sizeof(*ack)) {
  192. result = -EIO;
  193. dev_err(dev, "boot-mode cmd %d: HW BUG? notification didn't "
  194. "return enough data (%zu bytes vs %zu expected)\n",
  195. opcode, ack_size, sizeof(*ack));
  196. goto error_ack_short;
  197. }
  198. if (ack_size == sizeof(i2400m_NBOOT_BARKER)
  199. && memcmp(ack, i2400m_NBOOT_BARKER, sizeof(*ack)) == 0) {
  200. result = -ERESTARTSYS;
  201. i2400m->sboot = 0;
  202. d_printf(6, dev, "boot-mode cmd %d: "
  203. "HW non-signed boot barker\n", opcode);
  204. goto error_reboot;
  205. }
  206. if (ack_size == sizeof(i2400m_SBOOT_BARKER)
  207. && memcmp(ack, i2400m_SBOOT_BARKER, sizeof(*ack)) == 0) {
  208. result = -ERESTARTSYS;
  209. i2400m->sboot = 1;
  210. d_printf(6, dev, "boot-mode cmd %d: HW signed reboot barker\n",
  211. opcode);
  212. goto error_reboot;
  213. }
  214. if (ack_size == sizeof(i2400m_ACK_BARKER)
  215. && memcmp(ack, i2400m_ACK_BARKER, sizeof(*ack)) == 0) {
  216. result = -EISCONN;
  217. d_printf(3, dev, "boot-mode cmd %d: HW reboot ack barker\n",
  218. opcode);
  219. goto error_reboot_ack;
  220. }
  221. result = 0;
  222. if (flags & I2400M_BM_CMD_RAW)
  223. goto out_raw;
  224. ack->data_size = le32_to_cpu(ack->data_size);
  225. ack->target_addr = le32_to_cpu(ack->target_addr);
  226. ack->block_checksum = le32_to_cpu(ack->block_checksum);
  227. d_printf(5, dev, "boot-mode cmd %d: notification for opcode %u "
  228. "response %u csum %u rr %u da %u\n",
  229. opcode, i2400m_brh_get_opcode(ack),
  230. i2400m_brh_get_response(ack),
  231. i2400m_brh_get_use_checksum(ack),
  232. i2400m_brh_get_response_required(ack),
  233. i2400m_brh_get_direct_access(ack));
  234. result = -EIO;
  235. if (i2400m_brh_get_signature(ack) != 0xcbbc) {
  236. dev_err(dev, "boot-mode cmd %d: HW BUG? wrong signature "
  237. "0x%04x\n", opcode, i2400m_brh_get_signature(ack));
  238. goto error_ack_signature;
  239. }
  240. if (opcode != -1 && opcode != i2400m_brh_get_opcode(ack)) {
  241. dev_err(dev, "boot-mode cmd %d: HW BUG? "
  242. "received response for opcode %u, expected %u\n",
  243. opcode, i2400m_brh_get_opcode(ack), opcode);
  244. goto error_ack_opcode;
  245. }
  246. if (i2400m_brh_get_response(ack) != 0) { /* failed? */
  247. dev_err(dev, "boot-mode cmd %d: error; hw response %u\n",
  248. opcode, i2400m_brh_get_response(ack));
  249. goto error_ack_failed;
  250. }
  251. if (ack_size < ack->data_size + sizeof(*ack)) {
  252. dev_err(dev, "boot-mode cmd %d: SW BUG "
  253. "driver provided only %zu bytes for %zu bytes "
  254. "of data\n", opcode, ack_size,
  255. (size_t) le32_to_cpu(ack->data_size) + sizeof(*ack));
  256. goto error_ack_short_buffer;
  257. }
  258. result = ack_size;
  259. /* Don't you love this stack of empty targets? Well, I don't
  260. * either, but it helps track exactly who comes in here and
  261. * why :) */
  262. error_ack_short_buffer:
  263. error_ack_failed:
  264. error_ack_opcode:
  265. error_ack_signature:
  266. out_raw:
  267. error_reboot_ack:
  268. error_reboot:
  269. error_ack_short:
  270. d_fnend(8, dev, "(i2400m %p opcode %d ack %p size %zu) = %d\n",
  271. i2400m, opcode, ack, ack_size, (int) result);
  272. return result;
  273. }
  274. /**
  275. * i2400m_bm_cmd - Execute a boot mode command
  276. *
  277. * @cmd: buffer containing the command data (pointing at the header).
  278. * This data can be ANYWHERE (for USB, we will copy it to an
  279. * specific buffer). Make sure everything is in proper little
  280. * endian.
  281. *
  282. * A raw buffer can be also sent, just cast it and set flags to
  283. * I2400M_BM_CMD_RAW.
  284. *
  285. * This function will generate a checksum for you if the
  286. * checksum bit in the command is set (unless I2400M_BM_CMD_RAW
  287. * is set).
  288. *
  289. * You can use the i2400m->bm_cmd_buf to stage your commands and
  290. * send them.
  291. *
  292. * If NULL, no command is sent (we just wait for an ack).
  293. *
  294. * @cmd_size: size of the command. Will be auto padded to the
  295. * bus-specific drivers padding requirements.
  296. *
  297. * @ack: buffer where to place the acknowledgement. If it is a regular
  298. * command response, all fields will be returned with the right,
  299. * native endianess.
  300. *
  301. * You *cannot* use i2400m->bm_ack_buf for this buffer.
  302. *
  303. * @ack_size: size of @ack, 16 aligned; you need to provide at least
  304. * sizeof(*ack) bytes and then enough to contain the return data
  305. * from the command
  306. *
  307. * @flags: see I2400M_BM_CMD_* above.
  308. *
  309. * @returns: bytes received by the notification; if < 0, an errno code
  310. * denoting an error or:
  311. *
  312. * -ERESTARTSYS The device has rebooted
  313. *
  314. * Executes a boot-mode command and waits for a response, doing basic
  315. * validation on it; if a zero length response is received, it retries
  316. * waiting for a response until a non-zero one is received (timing out
  317. * after %I2400M_BOOT_RETRIES retries).
  318. */
  319. static
  320. ssize_t i2400m_bm_cmd(struct i2400m *i2400m,
  321. const struct i2400m_bootrom_header *cmd, size_t cmd_size,
  322. struct i2400m_bootrom_header *ack, size_t ack_size,
  323. int flags)
  324. {
  325. ssize_t result = -ENOMEM, rx_bytes;
  326. struct device *dev = i2400m_dev(i2400m);
  327. int opcode = cmd == NULL ? -1 : i2400m_brh_get_opcode(cmd);
  328. d_fnstart(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu)\n",
  329. i2400m, cmd, cmd_size, ack, ack_size);
  330. BUG_ON(ack_size < sizeof(*ack));
  331. BUG_ON(i2400m->boot_mode == 0);
  332. if (cmd != NULL) { /* send the command */
  333. memcpy(i2400m->bm_cmd_buf, cmd, cmd_size);
  334. result = i2400m->bus_bm_cmd_send(i2400m, cmd, cmd_size, flags);
  335. if (result < 0)
  336. goto error_cmd_send;
  337. if ((flags & I2400M_BM_CMD_RAW) == 0)
  338. d_printf(5, dev,
  339. "boot-mode cmd %d csum %u rr %u da %u: "
  340. "addr 0x%04x size %u block csum 0x%04x\n",
  341. opcode, i2400m_brh_get_use_checksum(cmd),
  342. i2400m_brh_get_response_required(cmd),
  343. i2400m_brh_get_direct_access(cmd),
  344. cmd->target_addr, cmd->data_size,
  345. cmd->block_checksum);
  346. }
  347. result = i2400m->bus_bm_wait_for_ack(i2400m, ack, ack_size);
  348. if (result < 0) {
  349. dev_err(dev, "boot-mode cmd %d: error waiting for an ack: %d\n",
  350. opcode, (int) result); /* bah, %zd doesn't work */
  351. goto error_wait_for_ack;
  352. }
  353. rx_bytes = result;
  354. /* verify the ack and read more if neccessary [result is the
  355. * final amount of bytes we get in the ack] */
  356. result = __i2400m_bm_ack_verify(i2400m, opcode, ack, ack_size, flags);
  357. if (result < 0)
  358. goto error_bad_ack;
  359. /* Don't you love this stack of empty targets? Well, I don't
  360. * either, but it helps track exactly who comes in here and
  361. * why :) */
  362. result = rx_bytes;
  363. error_bad_ack:
  364. error_wait_for_ack:
  365. error_cmd_send:
  366. d_fnend(6, dev, "(i2400m %p cmd %p size %zu ack %p size %zu) = %d\n",
  367. i2400m, cmd, cmd_size, ack, ack_size, (int) result);
  368. return result;
  369. }
  370. /**
  371. * i2400m_download_chunk - write a single chunk of data to the device's memory
  372. *
  373. * @i2400m: device descriptor
  374. * @buf: the buffer to write
  375. * @buf_len: length of the buffer to write
  376. * @addr: address in the device memory space
  377. * @direct: bootrom write mode
  378. * @do_csum: should a checksum validation be performed
  379. */
  380. static int i2400m_download_chunk(struct i2400m *i2400m, const void *chunk,
  381. size_t __chunk_len, unsigned long addr,
  382. unsigned int direct, unsigned int do_csum)
  383. {
  384. int ret;
  385. size_t chunk_len = ALIGN(__chunk_len, I2400M_PL_PAD);
  386. struct device *dev = i2400m_dev(i2400m);
  387. struct {
  388. struct i2400m_bootrom_header cmd;
  389. u8 cmd_payload[chunk_len];
  390. } __attribute__((packed)) *buf;
  391. struct i2400m_bootrom_header ack;
  392. d_fnstart(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
  393. "direct %u do_csum %u)\n", i2400m, chunk, __chunk_len,
  394. addr, direct, do_csum);
  395. buf = i2400m->bm_cmd_buf;
  396. memcpy(buf->cmd_payload, chunk, __chunk_len);
  397. memset(buf->cmd_payload + __chunk_len, 0xad, chunk_len - __chunk_len);
  398. buf->cmd.command = i2400m_brh_command(I2400M_BRH_WRITE,
  399. __chunk_len & 0x3 ? 0 : do_csum,
  400. __chunk_len & 0xf ? 0 : direct);
  401. buf->cmd.target_addr = cpu_to_le32(addr);
  402. buf->cmd.data_size = cpu_to_le32(__chunk_len);
  403. ret = i2400m_bm_cmd(i2400m, &buf->cmd, sizeof(buf->cmd) + chunk_len,
  404. &ack, sizeof(ack), 0);
  405. if (ret >= 0)
  406. ret = 0;
  407. d_fnend(5, dev, "(i2400m %p chunk %p __chunk_len %zu addr 0x%08lx "
  408. "direct %u do_csum %u) = %d\n", i2400m, chunk, __chunk_len,
  409. addr, direct, do_csum, ret);
  410. return ret;
  411. }
  412. /*
  413. * Download a BCF file's sections to the device
  414. *
  415. * @i2400m: device descriptor
  416. * @bcf: pointer to firmware data (followed by the payloads). Assumed
  417. * verified and consistent.
  418. * @bcf_len: length (in bytes) of the @bcf buffer.
  419. *
  420. * Returns: < 0 errno code on error or the offset to the jump instruction.
  421. *
  422. * Given a BCF file, downloads each section (a command and a payload)
  423. * to the device's address space. Actually, it just executes each
  424. * command i the BCF file.
  425. *
  426. * The section size has to be aligned to 4 bytes AND the padding has
  427. * to be taken from the firmware file, as the signature takes it into
  428. * account.
  429. */
  430. static
  431. ssize_t i2400m_dnload_bcf(struct i2400m *i2400m,
  432. const struct i2400m_bcf_hdr *bcf, size_t bcf_len)
  433. {
  434. ssize_t ret;
  435. struct device *dev = i2400m_dev(i2400m);
  436. size_t offset, /* iterator offset */
  437. data_size, /* Size of the data payload */
  438. section_size, /* Size of the whole section (cmd + payload) */
  439. section = 1;
  440. const struct i2400m_bootrom_header *bh;
  441. struct i2400m_bootrom_header ack;
  442. d_fnstart(3, dev, "(i2400m %p bcf %p bcf_len %zu)\n",
  443. i2400m, bcf, bcf_len);
  444. /* Iterate over the command blocks in the BCF file that start
  445. * after the header */
  446. offset = le32_to_cpu(bcf->header_len) * sizeof(u32);
  447. while (1) { /* start sending the file */
  448. bh = (void *) bcf + offset;
  449. data_size = le32_to_cpu(bh->data_size);
  450. section_size = ALIGN(sizeof(*bh) + data_size, 4);
  451. d_printf(7, dev,
  452. "downloading section #%zu (@%zu %zu B) to 0x%08x\n",
  453. section, offset, sizeof(*bh) + data_size,
  454. le32_to_cpu(bh->target_addr));
  455. if (i2400m_brh_get_opcode(bh) == I2400M_BRH_SIGNED_JUMP) {
  456. /* Secure boot needs to stop here */
  457. d_printf(5, dev, "signed jump found @%zu\n", offset);
  458. break;
  459. }
  460. if (offset + section_size == bcf_len)
  461. /* Non-secure boot stops here */
  462. break;
  463. if (offset + section_size > bcf_len) {
  464. dev_err(dev, "fw %s: bad section #%zu, "
  465. "end (@%zu) beyond EOF (@%zu)\n",
  466. i2400m->fw_name, section,
  467. offset + section_size, bcf_len);
  468. ret = -EINVAL;
  469. goto error_section_beyond_eof;
  470. }
  471. __i2400m_msleep(20);
  472. ret = i2400m_bm_cmd(i2400m, bh, section_size,
  473. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  474. if (ret < 0) {
  475. dev_err(dev, "fw %s: section #%zu (@%zu %zu B) "
  476. "failed %d\n", i2400m->fw_name, section,
  477. offset, sizeof(*bh) + data_size, (int) ret);
  478. goto error_send;
  479. }
  480. offset += section_size;
  481. section++;
  482. }
  483. ret = offset;
  484. error_section_beyond_eof:
  485. error_send:
  486. d_fnend(3, dev, "(i2400m %p bcf %p bcf_len %zu) = %d\n",
  487. i2400m, bcf, bcf_len, (int) ret);
  488. return ret;
  489. }
  490. /*
  491. * Do the final steps of uploading firmware
  492. *
  493. * Depending on the boot mode (signed vs non-signed), different
  494. * actions need to be taken.
  495. */
  496. static
  497. int i2400m_dnload_finalize(struct i2400m *i2400m,
  498. const struct i2400m_bcf_hdr *bcf, size_t offset)
  499. {
  500. int ret = 0;
  501. struct device *dev = i2400m_dev(i2400m);
  502. struct i2400m_bootrom_header *cmd, ack;
  503. struct {
  504. struct i2400m_bootrom_header cmd;
  505. u8 cmd_pl[0];
  506. } __attribute__((packed)) *cmd_buf;
  507. size_t signature_block_offset, signature_block_size;
  508. d_fnstart(3, dev, "offset %zu\n", offset);
  509. cmd = (void *) bcf + offset;
  510. if (i2400m->sboot == 0) {
  511. struct i2400m_bootrom_header jump_ack;
  512. d_printf(3, dev, "unsecure boot, jumping to 0x%08x\n",
  513. le32_to_cpu(cmd->target_addr));
  514. i2400m_brh_set_opcode(cmd, I2400M_BRH_JUMP);
  515. cmd->data_size = 0;
  516. ret = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  517. &jump_ack, sizeof(jump_ack), 0);
  518. } else {
  519. d_printf(3, dev, "secure boot, jumping to 0x%08x\n",
  520. le32_to_cpu(cmd->target_addr));
  521. cmd_buf = i2400m->bm_cmd_buf;
  522. memcpy(&cmd_buf->cmd, cmd, sizeof(*cmd));
  523. signature_block_offset =
  524. sizeof(*bcf)
  525. + le32_to_cpu(bcf->key_size) * sizeof(u32)
  526. + le32_to_cpu(bcf->exponent_size) * sizeof(u32);
  527. signature_block_size =
  528. le32_to_cpu(bcf->modulus_size) * sizeof(u32);
  529. memcpy(cmd_buf->cmd_pl, (void *) bcf + signature_block_offset,
  530. signature_block_size);
  531. ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd,
  532. sizeof(cmd_buf->cmd) + signature_block_size,
  533. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  534. }
  535. d_fnend(3, dev, "returning %d\n", ret);
  536. return ret;
  537. }
  538. /**
  539. * i2400m_bootrom_init - Reboots a powered device into boot mode
  540. *
  541. * @i2400m: device descriptor
  542. * @flags:
  543. * I2400M_BRI_SOFT: a reboot notification has been seen
  544. * already, so don't wait for it.
  545. *
  546. * I2400M_BRI_NO_REBOOT: Don't send a reboot command, but wait
  547. * for a reboot barker notification. This is a one shot; if
  548. * the state machine needs to send a reboot command it will.
  549. *
  550. * Returns:
  551. *
  552. * < 0 errno code on error, 0 if ok.
  553. *
  554. * i2400m->sboot set to 0 for unsecure boot process, 1 for secure
  555. * boot process.
  556. *
  557. * Description:
  558. *
  559. * Tries hard enough to put the device in boot-mode. There are two
  560. * main phases to this:
  561. *
  562. * a. (1) send a reboot command and (2) get a reboot barker
  563. * b. (1) ack the reboot sending a reboot barker and (2) getting an
  564. * ack barker in return
  565. *
  566. * We want to skip (a) in some cases [soft]. The state machine is
  567. * horrible, but it is basically: on each phase, send what has to be
  568. * sent (if any), wait for the answer and act on the answer. We might
  569. * have to backtrack and retry, so we keep a max tries counter for
  570. * that.
  571. *
  572. * If we get a timeout after sending a warm reset, we do it again.
  573. */
  574. int i2400m_bootrom_init(struct i2400m *i2400m, enum i2400m_bri flags)
  575. {
  576. int result;
  577. struct device *dev = i2400m_dev(i2400m);
  578. struct i2400m_bootrom_header *cmd;
  579. struct i2400m_bootrom_header ack;
  580. int count = I2400M_BOOT_RETRIES;
  581. int ack_timeout_cnt = 1;
  582. BUILD_BUG_ON(sizeof(*cmd) != sizeof(i2400m_NBOOT_BARKER));
  583. BUILD_BUG_ON(sizeof(ack) != sizeof(i2400m_ACK_BARKER));
  584. d_fnstart(4, dev, "(i2400m %p flags 0x%08x)\n", i2400m, flags);
  585. result = -ENOMEM;
  586. cmd = i2400m->bm_cmd_buf;
  587. if (flags & I2400M_BRI_SOFT)
  588. goto do_reboot_ack;
  589. do_reboot:
  590. if (--count < 0)
  591. goto error_timeout;
  592. d_printf(4, dev, "device reboot: reboot command [%d # left]\n",
  593. count);
  594. if ((flags & I2400M_BRI_NO_REBOOT) == 0)
  595. i2400m->bus_reset(i2400m, I2400M_RT_WARM);
  596. result = i2400m_bm_cmd(i2400m, NULL, 0, &ack, sizeof(ack),
  597. I2400M_BM_CMD_RAW);
  598. flags &= ~I2400M_BRI_NO_REBOOT;
  599. switch (result) {
  600. case -ERESTARTSYS:
  601. d_printf(4, dev, "device reboot: got reboot barker\n");
  602. break;
  603. case -EISCONN: /* we don't know how it got here...but we follow it */
  604. d_printf(4, dev, "device reboot: got ack barker - whatever\n");
  605. goto do_reboot;
  606. case -ETIMEDOUT: /* device has timed out, we might be in boot
  607. * mode already and expecting an ack, let's try
  608. * that */
  609. dev_info(dev, "warm reset timed out, trying an ack\n");
  610. goto do_reboot_ack;
  611. case -EPROTO:
  612. case -ESHUTDOWN: /* dev is gone */
  613. case -EINTR: /* user cancelled */
  614. goto error_dev_gone;
  615. default:
  616. dev_err(dev, "device reboot: error %d while waiting "
  617. "for reboot barker - rebooting\n", result);
  618. goto do_reboot;
  619. }
  620. /* At this point we ack back with 4 REBOOT barkers and expect
  621. * 4 ACK barkers. This is ugly, as we send a raw command --
  622. * hence the cast. _bm_cmd() will catch the reboot ack
  623. * notification and report it as -EISCONN. */
  624. do_reboot_ack:
  625. d_printf(4, dev, "device reboot ack: sending ack [%d # left]\n", count);
  626. if (i2400m->sboot == 0)
  627. memcpy(cmd, i2400m_NBOOT_BARKER,
  628. sizeof(i2400m_NBOOT_BARKER));
  629. else
  630. memcpy(cmd, i2400m_SBOOT_BARKER,
  631. sizeof(i2400m_SBOOT_BARKER));
  632. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  633. &ack, sizeof(ack), I2400M_BM_CMD_RAW);
  634. switch (result) {
  635. case -ERESTARTSYS:
  636. d_printf(4, dev, "reboot ack: got reboot barker - retrying\n");
  637. if (--count < 0)
  638. goto error_timeout;
  639. goto do_reboot_ack;
  640. case -EISCONN:
  641. d_printf(4, dev, "reboot ack: got ack barker - good\n");
  642. break;
  643. case -ETIMEDOUT: /* no response, maybe it is the other type? */
  644. if (ack_timeout_cnt-- >= 0) {
  645. d_printf(4, dev, "reboot ack timedout: "
  646. "trying the other type?\n");
  647. i2400m->sboot = !i2400m->sboot;
  648. goto do_reboot_ack;
  649. } else {
  650. dev_err(dev, "reboot ack timedout too long: "
  651. "trying reboot\n");
  652. goto do_reboot;
  653. }
  654. break;
  655. case -EPROTO:
  656. case -ESHUTDOWN: /* dev is gone */
  657. goto error_dev_gone;
  658. default:
  659. dev_err(dev, "device reboot ack: error %d while waiting for "
  660. "reboot ack barker - rebooting\n", result);
  661. goto do_reboot;
  662. }
  663. d_printf(2, dev, "device reboot ack: got ack barker - boot done\n");
  664. result = 0;
  665. exit_timeout:
  666. error_dev_gone:
  667. d_fnend(4, dev, "(i2400m %p flags 0x%08x) = %d\n",
  668. i2400m, flags, result);
  669. return result;
  670. error_timeout:
  671. dev_err(dev, "Timed out waiting for reboot ack, resetting\n");
  672. i2400m->bus_reset(i2400m, I2400M_RT_BUS);
  673. result = -ETIMEDOUT;
  674. goto exit_timeout;
  675. }
  676. /*
  677. * Read the MAC addr
  678. *
  679. * The position this function reads is fixed in device memory and
  680. * always available, even without firmware.
  681. *
  682. * Note we specify we want to read only six bytes, but provide space
  683. * for 16, as we always get it rounded up.
  684. */
  685. int i2400m_read_mac_addr(struct i2400m *i2400m)
  686. {
  687. int result;
  688. struct device *dev = i2400m_dev(i2400m);
  689. struct net_device *net_dev = i2400m->wimax_dev.net_dev;
  690. struct i2400m_bootrom_header *cmd;
  691. struct {
  692. struct i2400m_bootrom_header ack;
  693. u8 ack_pl[16];
  694. } __attribute__((packed)) ack_buf;
  695. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  696. cmd = i2400m->bm_cmd_buf;
  697. cmd->command = i2400m_brh_command(I2400M_BRH_READ, 0, 1);
  698. cmd->target_addr = cpu_to_le32(0x00203fe8);
  699. cmd->data_size = cpu_to_le32(6);
  700. result = i2400m_bm_cmd(i2400m, cmd, sizeof(*cmd),
  701. &ack_buf.ack, sizeof(ack_buf), 0);
  702. if (result < 0) {
  703. dev_err(dev, "BM: read mac addr failed: %d\n", result);
  704. goto error_read_mac;
  705. }
  706. d_printf(2, dev,
  707. "mac addr is %02x:%02x:%02x:%02x:%02x:%02x\n",
  708. ack_buf.ack_pl[0], ack_buf.ack_pl[1],
  709. ack_buf.ack_pl[2], ack_buf.ack_pl[3],
  710. ack_buf.ack_pl[4], ack_buf.ack_pl[5]);
  711. if (i2400m->bus_bm_mac_addr_impaired == 1) {
  712. ack_buf.ack_pl[0] = 0x00;
  713. ack_buf.ack_pl[1] = 0x16;
  714. ack_buf.ack_pl[2] = 0xd3;
  715. get_random_bytes(&ack_buf.ack_pl[3], 3);
  716. dev_err(dev, "BM is MAC addr impaired, faking MAC addr to "
  717. "mac addr is %02x:%02x:%02x:%02x:%02x:%02x\n",
  718. ack_buf.ack_pl[0], ack_buf.ack_pl[1],
  719. ack_buf.ack_pl[2], ack_buf.ack_pl[3],
  720. ack_buf.ack_pl[4], ack_buf.ack_pl[5]);
  721. result = 0;
  722. }
  723. net_dev->addr_len = ETH_ALEN;
  724. memcpy(net_dev->perm_addr, ack_buf.ack_pl, ETH_ALEN);
  725. memcpy(net_dev->dev_addr, ack_buf.ack_pl, ETH_ALEN);
  726. error_read_mac:
  727. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, result);
  728. return result;
  729. }
  730. /*
  731. * Initialize a non signed boot
  732. *
  733. * This implies sending some magic values to the device's memory. Note
  734. * we convert the values to little endian in the same array
  735. * declaration.
  736. */
  737. static
  738. int i2400m_dnload_init_nonsigned(struct i2400m *i2400m)
  739. {
  740. #define POKE(a, d) { \
  741. .address = cpu_to_le32(a), \
  742. .data = cpu_to_le32(d) \
  743. }
  744. static const struct {
  745. __le32 address;
  746. __le32 data;
  747. } i2400m_pokes[] = {
  748. POKE(0x081A58, 0xA7810230),
  749. POKE(0x080040, 0x00000000),
  750. POKE(0x080048, 0x00000082),
  751. POKE(0x08004C, 0x0000081F),
  752. POKE(0x080054, 0x00000085),
  753. POKE(0x080058, 0x00000180),
  754. POKE(0x08005C, 0x00000018),
  755. POKE(0x080060, 0x00000010),
  756. POKE(0x080574, 0x00000001),
  757. POKE(0x080550, 0x00000005),
  758. POKE(0xAE0000, 0x00000000),
  759. };
  760. #undef POKE
  761. unsigned i;
  762. int ret;
  763. struct device *dev = i2400m_dev(i2400m);
  764. dev_warn(dev, "WARNING!!! non-signed boot UNTESTED PATH!\n");
  765. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  766. for (i = 0; i < ARRAY_SIZE(i2400m_pokes); i++) {
  767. ret = i2400m_download_chunk(i2400m, &i2400m_pokes[i].data,
  768. sizeof(i2400m_pokes[i].data),
  769. i2400m_pokes[i].address, 1, 1);
  770. if (ret < 0)
  771. break;
  772. }
  773. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  774. return ret;
  775. }
  776. /*
  777. * Initialize the signed boot process
  778. *
  779. * @i2400m: device descriptor
  780. *
  781. * @bcf_hdr: pointer to the firmware header; assumes it is fully in
  782. * memory (it has gone through basic validation).
  783. *
  784. * Returns: 0 if ok, < 0 errno code on error, -ERESTARTSYS if the hw
  785. * rebooted.
  786. *
  787. * This writes the firmware BCF header to the device using the
  788. * HASH_PAYLOAD_ONLY command.
  789. */
  790. static
  791. int i2400m_dnload_init_signed(struct i2400m *i2400m,
  792. const struct i2400m_bcf_hdr *bcf_hdr)
  793. {
  794. int ret;
  795. struct device *dev = i2400m_dev(i2400m);
  796. struct {
  797. struct i2400m_bootrom_header cmd;
  798. struct i2400m_bcf_hdr cmd_pl;
  799. } __attribute__((packed)) *cmd_buf;
  800. struct i2400m_bootrom_header ack;
  801. d_fnstart(5, dev, "(i2400m %p bcf_hdr %p)\n", i2400m, bcf_hdr);
  802. cmd_buf = i2400m->bm_cmd_buf;
  803. cmd_buf->cmd.command =
  804. i2400m_brh_command(I2400M_BRH_HASH_PAYLOAD_ONLY, 0, 0);
  805. cmd_buf->cmd.target_addr = 0;
  806. cmd_buf->cmd.data_size = cpu_to_le32(sizeof(cmd_buf->cmd_pl));
  807. memcpy(&cmd_buf->cmd_pl, bcf_hdr, sizeof(*bcf_hdr));
  808. ret = i2400m_bm_cmd(i2400m, &cmd_buf->cmd, sizeof(*cmd_buf),
  809. &ack, sizeof(ack), 0);
  810. if (ret >= 0)
  811. ret = 0;
  812. d_fnend(5, dev, "(i2400m %p bcf_hdr %p) = %d\n", i2400m, bcf_hdr, ret);
  813. return ret;
  814. }
  815. /*
  816. * Initialize the firmware download at the device size
  817. *
  818. * Multiplex to the one that matters based on the device's mode
  819. * (signed or non-signed).
  820. */
  821. static
  822. int i2400m_dnload_init(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf)
  823. {
  824. int result;
  825. struct device *dev = i2400m_dev(i2400m);
  826. u32 module_id = le32_to_cpu(bcf->module_id);
  827. if (i2400m->sboot == 0
  828. && (module_id & I2400M_BCF_MOD_ID_POKES) == 0) {
  829. /* non-signed boot process without pokes */
  830. result = i2400m_dnload_init_nonsigned(i2400m);
  831. if (result == -ERESTARTSYS)
  832. return result;
  833. if (result < 0)
  834. dev_err(dev, "fw %s: non-signed download "
  835. "initialization failed: %d\n",
  836. i2400m->fw_name, result);
  837. } else if (i2400m->sboot == 0
  838. && (module_id & I2400M_BCF_MOD_ID_POKES)) {
  839. /* non-signed boot process with pokes, nothing to do */
  840. result = 0;
  841. } else { /* signed boot process */
  842. result = i2400m_dnload_init_signed(i2400m, bcf);
  843. if (result == -ERESTARTSYS)
  844. return result;
  845. if (result < 0)
  846. dev_err(dev, "fw %s: signed boot download "
  847. "initialization failed: %d\n",
  848. i2400m->fw_name, result);
  849. }
  850. return result;
  851. }
  852. /*
  853. * Run quick consistency tests on the firmware file
  854. *
  855. * Check for the firmware being made for the i2400m device,
  856. * etc...These checks are mostly informative, as the device will make
  857. * them too; but the driver's response is more informative on what
  858. * went wrong.
  859. */
  860. static
  861. int i2400m_fw_check(struct i2400m *i2400m,
  862. const struct i2400m_bcf_hdr *bcf,
  863. size_t bcf_size)
  864. {
  865. int result;
  866. struct device *dev = i2400m_dev(i2400m);
  867. unsigned module_type, header_len, major_version, minor_version,
  868. module_id, module_vendor, date, size;
  869. /* Check hard errors */
  870. result = -EINVAL;
  871. if (bcf_size < sizeof(*bcf)) { /* big enough header? */
  872. dev_err(dev, "firmware %s too short: "
  873. "%zu B vs %zu (at least) expected\n",
  874. i2400m->fw_name, bcf_size, sizeof(*bcf));
  875. goto error;
  876. }
  877. module_type = bcf->module_type;
  878. header_len = sizeof(u32) * le32_to_cpu(bcf->header_len);
  879. major_version = le32_to_cpu(bcf->header_version) & 0xffff0000 >> 16;
  880. minor_version = le32_to_cpu(bcf->header_version) & 0x0000ffff;
  881. module_id = le32_to_cpu(bcf->module_id);
  882. module_vendor = le32_to_cpu(bcf->module_vendor);
  883. date = le32_to_cpu(bcf->date);
  884. size = sizeof(u32) * le32_to_cpu(bcf->size);
  885. if (bcf_size != size) { /* annoyingly paranoid */
  886. dev_err(dev, "firmware %s: bad size, got "
  887. "%zu B vs %u expected\n",
  888. i2400m->fw_name, bcf_size, size);
  889. goto error;
  890. }
  891. d_printf(2, dev, "type 0x%x id 0x%x vendor 0x%x; header v%u.%u (%zu B) "
  892. "date %08x (%zu B)\n",
  893. module_type, module_id, module_vendor,
  894. major_version, minor_version, (size_t) header_len,
  895. date, (size_t) size);
  896. if (module_type != 6) { /* built for the right hardware? */
  897. dev_err(dev, "bad fw %s: unexpected module type 0x%x; "
  898. "aborting\n", i2400m->fw_name, module_type);
  899. goto error;
  900. }
  901. /* Check soft-er errors */
  902. result = 0;
  903. if (module_vendor != 0x8086)
  904. dev_err(dev, "bad fw %s? unexpected vendor 0x%04x\n",
  905. i2400m->fw_name, module_vendor);
  906. if (date < 0x20080300)
  907. dev_err(dev, "bad fw %s? build date too old %08x\n",
  908. i2400m->fw_name, date);
  909. error:
  910. return result;
  911. }
  912. /*
  913. * Download the firmware to the device
  914. *
  915. * @i2400m: device descriptor
  916. * @bcf: pointer to loaded (and minimally verified for consistency)
  917. * firmware
  918. * @bcf_size: size of the @bcf buffer (header plus payloads)
  919. *
  920. * The process for doing this is described in this file's header.
  921. *
  922. * Note we only reinitialize boot-mode if the flags say so. Some hw
  923. * iterations need it, some don't. In any case, if we loop, we always
  924. * need to reinitialize the boot room, hence the flags modification.
  925. */
  926. static
  927. int i2400m_fw_dnload(struct i2400m *i2400m, const struct i2400m_bcf_hdr *bcf,
  928. size_t bcf_size, enum i2400m_bri flags)
  929. {
  930. int ret = 0;
  931. struct device *dev = i2400m_dev(i2400m);
  932. int count = I2400M_BOOT_RETRIES;
  933. d_fnstart(5, dev, "(i2400m %p bcf %p size %zu)\n",
  934. i2400m, bcf, bcf_size);
  935. i2400m->boot_mode = 1;
  936. hw_reboot:
  937. if (count-- == 0) {
  938. ret = -ERESTARTSYS;
  939. dev_err(dev, "device rebooted too many times, aborting\n");
  940. goto error_too_many_reboots;
  941. }
  942. if (flags & I2400M_BRI_MAC_REINIT) {
  943. ret = i2400m_bootrom_init(i2400m, flags);
  944. if (ret < 0) {
  945. dev_err(dev, "bootrom init failed: %d\n", ret);
  946. goto error_bootrom_init;
  947. }
  948. }
  949. flags |= I2400M_BRI_MAC_REINIT;
  950. /*
  951. * Initialize the download, push the bytes to the device and
  952. * then jump to the new firmware. Note @ret is passed with the
  953. * offset of the jump instruction to _dnload_finalize()
  954. */
  955. ret = i2400m_dnload_init(i2400m, bcf); /* Init device's dnload */
  956. if (ret == -ERESTARTSYS)
  957. goto error_dev_rebooted;
  958. if (ret < 0)
  959. goto error_dnload_init;
  960. ret = i2400m_dnload_bcf(i2400m, bcf, bcf_size);
  961. if (ret == -ERESTARTSYS)
  962. goto error_dev_rebooted;
  963. if (ret < 0) {
  964. dev_err(dev, "fw %s: download failed: %d\n",
  965. i2400m->fw_name, ret);
  966. goto error_dnload_bcf;
  967. }
  968. ret = i2400m_dnload_finalize(i2400m, bcf, ret);
  969. if (ret == -ERESTARTSYS)
  970. goto error_dev_rebooted;
  971. if (ret < 0) {
  972. dev_err(dev, "fw %s: "
  973. "download finalization failed: %d\n",
  974. i2400m->fw_name, ret);
  975. goto error_dnload_finalize;
  976. }
  977. d_printf(2, dev, "fw %s successfully uploaded\n",
  978. i2400m->fw_name);
  979. i2400m->boot_mode = 0;
  980. error_dnload_finalize:
  981. error_dnload_bcf:
  982. error_dnload_init:
  983. error_bootrom_init:
  984. error_too_many_reboots:
  985. d_fnend(5, dev, "(i2400m %p bcf %p size %zu) = %d\n",
  986. i2400m, bcf, bcf_size, ret);
  987. return ret;
  988. error_dev_rebooted:
  989. dev_err(dev, "device rebooted, %d tries left\n", count);
  990. /* we got the notification already, no need to wait for it again */
  991. flags |= I2400M_BRI_SOFT;
  992. goto hw_reboot;
  993. }
  994. /**
  995. * i2400m_dev_bootstrap - Bring the device to a known state and upload firmware
  996. *
  997. * @i2400m: device descriptor
  998. *
  999. * Returns: >= 0 if ok, < 0 errno code on error.
  1000. *
  1001. * This sets up the firmware upload environment, loads the firmware
  1002. * file from disk, verifies and then calls the firmware upload process
  1003. * per se.
  1004. *
  1005. * Can be called either from probe, or after a warm reset. Can not be
  1006. * called from within an interrupt. All the flow in this code is
  1007. * single-threade; all I/Os are synchronous.
  1008. */
  1009. int i2400m_dev_bootstrap(struct i2400m *i2400m, enum i2400m_bri flags)
  1010. {
  1011. int ret = 0, itr = 0;
  1012. struct device *dev = i2400m_dev(i2400m);
  1013. const struct firmware *fw;
  1014. const struct i2400m_bcf_hdr *bcf; /* Firmware data */
  1015. const char *fw_name;
  1016. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  1017. /* Load firmware files to memory. */
  1018. itr = 0;
  1019. while(1) {
  1020. fw_name = i2400m->bus_fw_names[itr];
  1021. if (fw_name == NULL) {
  1022. dev_err(dev, "Could not find a usable firmware image\n");
  1023. ret = -ENOENT;
  1024. goto error_no_fw;
  1025. }
  1026. ret = request_firmware(&fw, fw_name, dev);
  1027. if (ret == 0)
  1028. break; /* got it */
  1029. if (ret < 0)
  1030. dev_err(dev, "fw %s: cannot load file: %d\n",
  1031. fw_name, ret);
  1032. itr++;
  1033. }
  1034. bcf = (void *) fw->data;
  1035. i2400m->fw_name = fw_name;
  1036. ret = i2400m_fw_check(i2400m, bcf, fw->size);
  1037. if (ret < 0)
  1038. goto error_fw_bad;
  1039. ret = i2400m_fw_dnload(i2400m, bcf, fw->size, flags);
  1040. error_fw_bad:
  1041. release_firmware(fw);
  1042. error_no_fw:
  1043. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1044. return ret;
  1045. }
  1046. EXPORT_SYMBOL_GPL(i2400m_dev_bootstrap);