fw.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. __constant_cpu_to_le32(I2400M_ACK_BARKER),
  140. __constant_cpu_to_le32(I2400M_ACK_BARKER),
  141. __constant_cpu_to_le32(I2400M_ACK_BARKER),
  142. __constant_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->bus_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->bus_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 = __constant_cpu_to_le32(a), \
  742. .data = __constant_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->bus_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->bus_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->bus_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->bus_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->bus_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->bus_fw_name, module_vendor);
  906. if (date < 0x20080300)
  907. dev_err(dev, "bad fw %s? build date too old %08x\n",
  908. i2400m->bus_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->bus_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->bus_fw_name, ret);
  975. goto error_dnload_finalize;
  976. }
  977. d_printf(2, dev, "fw %s successfully uploaded\n",
  978. i2400m->bus_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;
  1012. struct device *dev = i2400m_dev(i2400m);
  1013. const struct firmware *fw;
  1014. const struct i2400m_bcf_hdr *bcf; /* Firmware data */
  1015. d_fnstart(5, dev, "(i2400m %p)\n", i2400m);
  1016. /* Load firmware files to memory. */
  1017. ret = request_firmware(&fw, i2400m->bus_fw_name, dev);
  1018. if (ret) {
  1019. dev_err(dev, "fw %s: request failed: %d\n",
  1020. i2400m->bus_fw_name, ret);
  1021. goto error_fw_req;
  1022. }
  1023. bcf = (void *) fw->data;
  1024. ret = i2400m_fw_check(i2400m, bcf, fw->size);
  1025. if (ret < 0)
  1026. goto error_fw_bad;
  1027. ret = i2400m_fw_dnload(i2400m, bcf, fw->size, flags);
  1028. error_fw_bad:
  1029. release_firmware(fw);
  1030. error_fw_req:
  1031. d_fnend(5, dev, "(i2400m %p) = %d\n", i2400m, ret);
  1032. return ret;
  1033. }
  1034. EXPORT_SYMBOL_GPL(i2400m_dev_bootstrap);