nrf_dfu_validation.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /**
  2. * Copyright (c) 2017 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include <stdbool.h>
  41. #include "nrf_dfu_types.h"
  42. #include "nrf_dfu_settings.h"
  43. #include "nrf_dfu_utils.h"
  44. #include "nrf_dfu_flash.h"
  45. #include "nrf_bootloader_info.h"
  46. #include "pb.h"
  47. #include "pb_common.h"
  48. #include "pb_decode.h"
  49. #include "dfu-cc.pb.h"
  50. #include "crc32.h"
  51. #include "nrf_crypto.h"
  52. #include "nrf_crypto_shared.h"
  53. #include "nrf_assert.h"
  54. #include "nrf_dfu_validation.h"
  55. #include "nrf_dfu_ver_validation.h"
  56. #define NRF_LOG_MODULE_NAME nrf_dfu_validation
  57. #include "nrf_log.h"
  58. #include "nrf_log_ctrl.h"
  59. NRF_LOG_MODULE_REGISTER();
  60. #ifndef DFU_REQUIRES_SOFTDEVICE
  61. #ifndef BLE_STACK_SUPPORT_REQD
  62. #define DFU_REQUIRES_SOFTDEVICE 0
  63. #else
  64. #define DFU_REQUIRES_SOFTDEVICE 1
  65. #endif
  66. #endif
  67. #define EXT_ERR(err) (nrf_dfu_result_t)((uint32_t)NRF_DFU_RES_CODE_EXT_ERROR + (uint32_t)err)
  68. /* Whether a complete init command has been received and prevalidated, but the firmware
  69. * is not yet fully transferred. This value will also be correct after reset.
  70. */
  71. static bool m_valid_init_cmd_present = false;
  72. static dfu_packet_t m_packet = DFU_PACKET_INIT_DEFAULT;
  73. static uint8_t* m_init_packet_data_ptr = 0;
  74. static uint32_t m_init_packet_data_len = 0;
  75. static pb_istream_t m_pb_stream;
  76. static nrf_crypto_ecdsa_verify_context_t m_verify_context = {0};
  77. static nrf_crypto_hash_context_t m_hash_context = {0};
  78. __ALIGN(4) extern const uint8_t pk[64];
  79. /** @brief Value length structure holding the public key.
  80. *
  81. * @details The pk value pointed to is the public key present in dfu_public_key.c
  82. */
  83. static nrf_crypto_ecc_public_key_t m_public_key;
  84. /** @brief Structure to hold a signature
  85. */
  86. static nrf_crypto_ecdsa_secp256r1_signature_t m_signature;
  87. /** @brief Structure to hold the hash for the init packet
  88. */
  89. static nrf_crypto_hash_sha256_digest_t m_init_packet_hash;
  90. /** @brief Structure to hold the hash for the firmware image
  91. */
  92. static nrf_crypto_hash_sha256_digest_t m_fw_hash;
  93. static void pb_decoding_callback(pb_istream_t *str,
  94. uint32_t tag,
  95. pb_wire_type_t wire_type,
  96. void *iter)
  97. {
  98. pb_field_iter_t* p_iter = (pb_field_iter_t *) iter;
  99. // Match the beginning of the init command.
  100. if (p_iter->pos->ptr == &dfu_init_command_fields[0])
  101. {
  102. uint8_t * ptr = (uint8_t *)str->state;
  103. uint32_t size = str->bytes_left;
  104. // Remove tag byte.
  105. ptr++;
  106. size--;
  107. // Store the info in init_packet_data.
  108. m_init_packet_data_ptr = ptr;
  109. m_init_packet_data_len = size;
  110. NRF_LOG_DEBUG("PB: Init packet data len: %d", size);
  111. }
  112. }
  113. /** @brief Function for decoding byte stream into variable.
  114. *
  115. * @retval true If the stored init command was successfully decoded.
  116. * @retval false If there was no stored init command, or the decoding failed.
  117. */
  118. static bool stored_init_cmd_decode(void)
  119. {
  120. m_pb_stream = pb_istream_from_buffer(s_dfu_settings.init_command,
  121. s_dfu_settings.progress.command_size);
  122. // Attach our callback to follow the field decoding.
  123. m_pb_stream.decoding_callback = pb_decoding_callback;
  124. m_init_packet_data_ptr = NULL;
  125. m_init_packet_data_len = 0;
  126. if (!pb_decode(&m_pb_stream, dfu_packet_fields, &m_packet))
  127. {
  128. NRF_LOG_ERROR("Handler: Invalid protocol buffer m_pb_stream");
  129. return false;
  130. }
  131. return true;
  132. }
  133. void nrf_dfu_validation_init(void)
  134. {
  135. ret_code_t err_code;
  136. uint8_t pk_copy[sizeof(pk)];
  137. // If the command is stored to flash, init command was valid.
  138. if ( (s_dfu_settings.progress.command_size != 0)
  139. && stored_init_cmd_decode()
  140. && (s_dfu_settings.bank_1.image_size != 0))
  141. {
  142. m_valid_init_cmd_present = true;
  143. }
  144. else
  145. {
  146. m_valid_init_cmd_present = false;
  147. }
  148. err_code = nrf_crypto_init();
  149. ASSERT(err_code == NRF_SUCCESS);
  150. UNUSED_PARAMETER(err_code);
  151. // Convert public key to big-endian format for use in nrf_crypto.
  152. nrf_crypto_internal_double_swap_endian(pk_copy, pk, sizeof(pk) / 2);
  153. err_code = nrf_crypto_ecc_public_key_from_raw(&g_nrf_crypto_ecc_secp256r1_curve_info,
  154. &m_public_key,
  155. pk_copy,
  156. sizeof(pk));
  157. ASSERT(err_code == NRF_SUCCESS);
  158. UNUSED_PARAMETER(err_code);
  159. }
  160. nrf_dfu_result_t nrf_dfu_validation_init_cmd_create(uint32_t size)
  161. {
  162. nrf_dfu_result_t ret_val = NRF_DFU_RES_CODE_SUCCESS;
  163. if (size == 0)
  164. {
  165. ret_val = NRF_DFU_RES_CODE_INVALID_PARAMETER;
  166. }
  167. else if (size > INIT_COMMAND_MAX_SIZE)
  168. {
  169. ret_val = NRF_DFU_RES_CODE_INSUFFICIENT_RESOURCES;
  170. }
  171. else
  172. {
  173. // Set DFU to uninitialized.
  174. m_valid_init_cmd_present = false;
  175. // Reset all progress.
  176. s_dfu_settings.write_offset = 0;
  177. memset(&s_dfu_settings.progress, 0x00, sizeof(dfu_progress_t));
  178. // Set the init command size.
  179. s_dfu_settings.progress.command_size = size;
  180. }
  181. return ret_val;
  182. }
  183. nrf_dfu_result_t nrf_dfu_validation_init_cmd_append(uint8_t const * p_data, uint32_t length)
  184. {
  185. nrf_dfu_result_t ret_val = NRF_DFU_RES_CODE_SUCCESS;
  186. if ((length + s_dfu_settings.progress.command_offset) > s_dfu_settings.progress.command_size)
  187. {
  188. NRF_LOG_ERROR("Init command larger than expected.");
  189. ret_val = NRF_DFU_RES_CODE_INVALID_PARAMETER;
  190. }
  191. else
  192. {
  193. // Copy the received data to RAM, update offset and calculate CRC.
  194. memcpy(&s_dfu_settings.init_command[s_dfu_settings.progress.command_offset],
  195. p_data,
  196. length);
  197. s_dfu_settings.progress.command_offset += length;
  198. s_dfu_settings.progress.command_crc = crc32_compute(p_data,
  199. length,
  200. &s_dfu_settings.progress.command_crc);
  201. }
  202. return ret_val;
  203. }
  204. void nrf_dfu_validation_init_cmd_status_get(uint32_t * p_offset,
  205. uint32_t * p_crc,
  206. uint32_t * p_max_size)
  207. {
  208. *p_offset = s_dfu_settings.progress.command_offset;
  209. *p_crc = s_dfu_settings.progress.command_crc;
  210. *p_max_size = INIT_COMMAND_MAX_SIZE;
  211. }
  212. bool nrf_dfu_validation_init_cmd_present(void)
  213. {
  214. return m_valid_init_cmd_present;
  215. }
  216. // Function determines if init command signature is obligatory.
  217. static bool signature_required(dfu_fw_type_t fw_type_to_be_updated)
  218. {
  219. bool result = true;
  220. if ((!DFU_REQUIRES_SOFTDEVICE && (fw_type_to_be_updated == DFU_FW_TYPE_SOFTDEVICE)) ||
  221. (fw_type_to_be_updated == DFU_FW_TYPE_APPLICATION))
  222. {
  223. result = NRF_DFU_REQUIRE_SIGNED_APP_UPDATE;
  224. }
  225. return result;
  226. }
  227. // Function to perform signature check if required.
  228. static nrf_dfu_result_t signature_check(dfu_fw_type_t fw_type,
  229. dfu_signature_type_t signature_type,
  230. dfu_signed_command_signature_t const * p_signature)
  231. {
  232. ret_code_t err_code;
  233. size_t hash_len = NRF_CRYPTO_HASH_SIZE_SHA256;
  234. if (!signature_required(fw_type))
  235. {
  236. return NRF_DFU_RES_CODE_SUCCESS;
  237. }
  238. NRF_LOG_INFO("Signature required. Checking signature.")
  239. if (p_signature == NULL)
  240. {
  241. NRF_LOG_WARNING("No signature found.");
  242. return EXT_ERR(NRF_DFU_EXT_ERROR_SIGNATURE_MISSING);
  243. }
  244. if (signature_type != DFU_SIGNATURE_TYPE_ECDSA_P256_SHA256)
  245. {
  246. NRF_LOG_INFO("Invalid signature type");
  247. return EXT_ERR(NRF_DFU_EXT_ERROR_WRONG_SIGNATURE_TYPE);
  248. }
  249. NRF_LOG_INFO("Calculating init packet hash (init packet len: %d)", m_init_packet_data_len);
  250. err_code = nrf_crypto_hash_calculate(&m_hash_context,
  251. &g_nrf_crypto_hash_sha256_info,
  252. m_init_packet_data_ptr,
  253. m_init_packet_data_len,
  254. m_init_packet_hash,
  255. &hash_len);
  256. if (err_code != NRF_SUCCESS)
  257. {
  258. return NRF_DFU_RES_CODE_OPERATION_FAILED;
  259. }
  260. if (sizeof(m_signature) != p_signature->size)
  261. {
  262. return NRF_DFU_RES_CODE_OPERATION_FAILED;
  263. }
  264. // Prepare the signature received over the air.
  265. memcpy(m_signature, p_signature->bytes, p_signature->size);
  266. // Calculate the signature.
  267. NRF_LOG_INFO("Verify signature");
  268. // The signature is in little-endian format. Change it to big-endian format for nrf_crypto use.
  269. nrf_crypto_internal_double_swap_endian_in_place(m_signature, sizeof(m_signature) / 2);
  270. err_code = nrf_crypto_ecdsa_verify(&m_verify_context,
  271. &m_public_key,
  272. m_init_packet_hash,
  273. hash_len,
  274. m_signature,
  275. sizeof(m_signature));
  276. if (err_code != NRF_SUCCESS)
  277. {
  278. NRF_LOG_ERROR("Signature failed (err_code: 0x%x)", err_code);
  279. NRF_LOG_DEBUG("Signature:");
  280. NRF_LOG_HEXDUMP_DEBUG(m_signature, sizeof(m_signature));
  281. NRF_LOG_DEBUG("Hash:");
  282. NRF_LOG_HEXDUMP_DEBUG(m_init_packet_hash, hash_len);
  283. NRF_LOG_DEBUG("Public Key:");
  284. NRF_LOG_HEXDUMP_DEBUG(pk, sizeof(pk));
  285. NRF_LOG_FLUSH();
  286. return NRF_DFU_RES_CODE_INVALID_OBJECT;
  287. }
  288. NRF_LOG_INFO("Image verified");
  289. return NRF_DFU_RES_CODE_SUCCESS;
  290. }
  291. // Function to calculate the total size of the firmware(s) in the update.
  292. static nrf_dfu_result_t update_data_size_get(dfu_init_command_t const * p_init, uint32_t * p_size)
  293. {
  294. nrf_dfu_result_t ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_INIT_COMMAND_INVALID);
  295. uint32_t fw_sz = 0;
  296. if ((p_init->type == DFU_FW_TYPE_APPLICATION) && (p_init->has_app_size == true))
  297. {
  298. fw_sz = p_init->app_size;
  299. }
  300. else
  301. {
  302. if ((p_init->type & DFU_FW_TYPE_SOFTDEVICE) && (p_init->has_sd_size == true))
  303. {
  304. fw_sz = p_init->sd_size;
  305. }
  306. if ((p_init->type & DFU_FW_TYPE_BOOTLOADER) && (p_init->has_bl_size == true))
  307. {
  308. if (p_init->bl_size <= BOOTLOADER_SIZE)
  309. {
  310. fw_sz += p_init->bl_size;
  311. }
  312. else
  313. {
  314. NRF_LOG_ERROR("BL size (%d) over limit (%d)", p_init->bl_size, BOOTLOADER_SIZE);
  315. fw_sz = 0;
  316. ret_val = NRF_DFU_RES_CODE_INSUFFICIENT_RESOURCES;
  317. }
  318. }
  319. }
  320. if (fw_sz)
  321. {
  322. *p_size = fw_sz;
  323. ret_val = NRF_DFU_RES_CODE_SUCCESS;
  324. }
  325. else
  326. {
  327. NRF_LOG_ERROR("Init packet does not contain valid firmware size");
  328. }
  329. return ret_val;
  330. }
  331. /**
  332. * @brief Function to check if single bank update should be used.
  333. *
  334. * @param new_fw_type Firmware type.
  335. */
  336. static bool use_single_bank(dfu_fw_type_t new_fw_type)
  337. {
  338. bool result = false;
  339. if (((new_fw_type == DFU_FW_TYPE_APPLICATION) || (new_fw_type == DFU_FW_TYPE_SOFTDEVICE)) &&
  340. NRF_DFU_SINGLE_BANK_APP_UPDATES)
  341. {
  342. result = true;
  343. }
  344. return result;
  345. }
  346. // Function to determine whether the new firmware needs a SoftDevice to be present.
  347. static bool update_requires_softdevice(dfu_init_command_t const * p_init)
  348. {
  349. return ((p_init->sd_req_count > 0) && (p_init->sd_req[0] != SD_REQ_APP_OVERWRITES_SD));
  350. }
  351. // Function to determine whether the SoftDevice can be removed during the update or not.
  352. static bool keep_softdevice(dfu_init_command_t const * p_init)
  353. {
  354. UNUSED_PARAMETER(p_init); // It's unused when DFU_REQUIRES_SOFTDEVICE is true.
  355. return DFU_REQUIRES_SOFTDEVICE || update_requires_softdevice(p_init);
  356. }
  357. /**@brief Function to determine where to temporarily store the incoming firmware.
  358. * This also checks whether the update will fit, and deletes existing
  359. * firmware to make room for the new firmware.
  360. *
  361. * @param[in] p_init Init command.
  362. * @param[in] fw_size The size of the incoming firmware.
  363. * @param[out] p_addr The address at which to initially store the firmware.
  364. *
  365. * @retval NRF_DFU_RES_CODE_SUCCESS If the size check passed and
  366. * an address was found.
  367. * @retval NRF_DFU_RES_CODE_INSUFFICIENT_RESOURCES If the size check failed.
  368. */
  369. static nrf_dfu_result_t update_data_addr_get(dfu_init_command_t const * p_init,
  370. uint32_t fw_size,
  371. uint32_t * p_addr)
  372. {
  373. nrf_dfu_result_t ret_val = NRF_DFU_RES_CODE_SUCCESS;
  374. ret_code_t err_code = nrf_dfu_cache_prepare(fw_size,
  375. use_single_bank(p_init->type),
  376. NRF_DFU_FORCE_DUAL_BANK_APP_UPDATES,
  377. keep_softdevice(p_init));
  378. if (err_code != NRF_SUCCESS)
  379. {
  380. NRF_LOG_ERROR("Can't find room for update");
  381. ret_val = NRF_DFU_RES_CODE_INSUFFICIENT_RESOURCES;
  382. }
  383. else
  384. {
  385. *p_addr = nrf_dfu_bank1_start_addr();
  386. NRF_LOG_DEBUG("Write address set to 0x%08x", *p_addr);
  387. }
  388. return ret_val;
  389. }
  390. nrf_dfu_result_t nrf_dfu_validation_init_cmd_execute(uint32_t * p_dst_data_addr,
  391. uint32_t * p_data_len)
  392. {
  393. nrf_dfu_result_t ret_val = NRF_DFU_RES_CODE_SUCCESS;
  394. if (s_dfu_settings.progress.command_offset != s_dfu_settings.progress.command_size)
  395. {
  396. // The object wasn't the right (requested) size.
  397. NRF_LOG_ERROR("Execute with faulty offset");
  398. ret_val = NRF_DFU_RES_CODE_OPERATION_NOT_PERMITTED;
  399. }
  400. else if (m_valid_init_cmd_present)
  401. {
  402. *p_dst_data_addr = nrf_dfu_bank1_start_addr();
  403. *p_data_len = s_dfu_settings.bank_1.image_size;
  404. ret_val = NRF_DFU_RES_CODE_SUCCESS;
  405. }
  406. else if (stored_init_cmd_decode() &&
  407. (m_packet.has_signed_command || m_packet.has_command))
  408. {
  409. dfu_command_t const * p_command = &m_packet.command;
  410. dfu_signature_type_t signature_type = (dfu_signature_type_t) 0; // Placeholder.
  411. dfu_signed_command_signature_t * p_signature = NULL;
  412. *p_dst_data_addr = 0;
  413. *p_data_len = 0;
  414. if (m_packet.has_signed_command)
  415. {
  416. p_command = &m_packet.signed_command.command;
  417. signature_type = m_packet.signed_command.signature_type;
  418. p_signature = &m_packet.signed_command.signature;
  419. }
  420. // Validate signature.
  421. ret_val = signature_check(p_command->init.type, signature_type, p_signature);
  422. // Validate versions.
  423. if (ret_val == NRF_DFU_RES_CODE_SUCCESS)
  424. {
  425. ret_val = nrf_dfu_ver_validation_check(&p_command->init);
  426. if (ret_val == NRF_DFU_RES_CODE_SUCCESS)
  427. {
  428. m_valid_init_cmd_present = true;
  429. }
  430. }
  431. // Get size of binary.
  432. if (ret_val == NRF_DFU_RES_CODE_SUCCESS)
  433. {
  434. ret_val = update_data_size_get(&p_command->init, p_data_len);
  435. }
  436. //Get address where to flash the binary.
  437. if (ret_val == NRF_DFU_RES_CODE_SUCCESS)
  438. {
  439. ret_val = update_data_addr_get(&p_command->init, *p_data_len, p_dst_data_addr);
  440. }
  441. }
  442. else
  443. {
  444. NRF_LOG_ERROR("Failed to decode init packet");
  445. ret_val = NRF_DFU_RES_CODE_INVALID_OBJECT;
  446. }
  447. return ret_val;
  448. }
  449. // Function to check the hash received in the init command against the received firmware.
  450. static bool fw_hash_ok(dfu_init_command_t const * p_init, uint32_t fw_start_addr, uint32_t fw_size)
  451. {
  452. ret_code_t err_code;
  453. bool result = true;
  454. uint8_t hash_be[NRF_CRYPTO_HASH_SIZE_SHA256];
  455. size_t hash_len = NRF_CRYPTO_HASH_SIZE_SHA256;
  456. ASSERT(p_init != NULL);
  457. // Convert to hash to big-endian format for use in nrf_crypto.
  458. nrf_crypto_internal_swap_endian(hash_be,
  459. (uint8_t *)p_init->hash.hash.bytes,
  460. NRF_CRYPTO_HASH_SIZE_SHA256);
  461. NRF_LOG_DEBUG("Hash verification. Firmware start address: 0x%x, size: 0x%x",
  462. fw_start_addr,
  463. fw_size);
  464. err_code = nrf_crypto_hash_calculate(&m_hash_context,
  465. &g_nrf_crypto_hash_sha256_info,
  466. (uint8_t*)fw_start_addr,
  467. fw_size,
  468. m_fw_hash,
  469. &hash_len);
  470. if (err_code != NRF_SUCCESS)
  471. {
  472. NRF_LOG_ERROR("Could not run hash verification (err_code 0x%x).", err_code);
  473. result = false;
  474. }
  475. else if (memcmp(m_fw_hash, hash_be, NRF_CRYPTO_HASH_SIZE_SHA256) != 0)
  476. {
  477. NRF_LOG_WARNING("Hash verification failed.");
  478. NRF_LOG_DEBUG("Expected FW hash:")
  479. NRF_LOG_HEXDUMP_DEBUG(p_init->hash.hash.bytes, NRF_CRYPTO_HASH_SIZE_SHA256);
  480. NRF_LOG_DEBUG("Actual FW hash:")
  481. NRF_LOG_HEXDUMP_DEBUG(m_fw_hash, NRF_CRYPTO_HASH_SIZE_SHA256);
  482. NRF_LOG_FLUSH();
  483. result = false;
  484. }
  485. return result;
  486. }
  487. // Function to check whether the update contains a SoftDevice and, if so, if it is of a different
  488. // major version than the existing SoftDevice.
  489. static bool is_major_softdevice_update(uint32_t new_sd_addr)
  490. {
  491. // True if there is no SD right now, but there is a new one coming. This counts as a major update.
  492. bool result = !SD_PRESENT && (SD_MAGIC_NUMBER_GET(new_sd_addr) == SD_MAGIC_NUMBER);
  493. if (SD_PRESENT && (SD_MAGIC_NUMBER_GET(new_sd_addr) == SD_MAGIC_NUMBER))
  494. {
  495. // Both SoftDevices are present.
  496. uint32_t current_SD_major = SD_MAJOR_VERSION_EXTRACT(SD_VERSION_GET(MBR_SIZE));
  497. uint32_t new_SD_major = SD_MAJOR_VERSION_EXTRACT(SD_VERSION_GET(new_sd_addr));
  498. result = (current_SD_major != new_SD_major);
  499. NRF_LOG_INFO("SoftDevice update is a %s version update. Current: %d. New: %d.",
  500. result ? "major" : "minor",
  501. current_SD_major,
  502. new_SD_major);
  503. }
  504. return result;
  505. }
  506. /**@brief Validate the SoftDevice size and magic number in structure found at 0x2000 in received SoftDevice.
  507. *
  508. * @param[in] sd_start_addr Start address of received SoftDevice.
  509. * @param[in] sd_size Size of received SoftDevice in bytes.
  510. */
  511. static bool softdevice_info_ok(uint32_t sd_start_addr, uint32_t sd_size)
  512. {
  513. bool result = true;
  514. if (SD_MAGIC_NUMBER_GET(sd_start_addr) != SD_MAGIC_NUMBER)
  515. {
  516. NRF_LOG_ERROR("The SoftDevice does not contain the magic number identifying it as a SoftDevice.");
  517. result = false;
  518. }
  519. else if (SD_SIZE_GET(sd_start_addr) < ALIGN_TO_PAGE(sd_size + MBR_SIZE))
  520. {
  521. // The size in the info struct should be rounded up to a page boundary
  522. // and be larger than the actual size + the size of the MBR.
  523. NRF_LOG_ERROR("The SoftDevice size in the info struct is too small compared with the size reported in the init command.");
  524. result = false;
  525. }
  526. return result;
  527. }
  528. static void postvalidate_app(dfu_init_command_t * p_init)
  529. {
  530. s_dfu_settings.bank_1.bank_code = NRF_DFU_BANK_VALID_APP;
  531. NRF_LOG_DEBUG("Invalidating old application in bank 0.");
  532. s_dfu_settings.bank_0.bank_code = NRF_DFU_BANK_INVALID;
  533. if (!DFU_REQUIRES_SOFTDEVICE && !update_requires_softdevice(p_init))
  534. {
  535. // App does not need SD, so it should be placed where SD is.
  536. nrf_dfu_softdevice_invalidate();
  537. }
  538. if (!NRF_DFU_DEBUG ||
  539. (NRF_DFU_DEBUG && (p_init->has_is_debug == false || p_init->is_debug == false)))
  540. {
  541. s_dfu_settings.app_version = p_init->fw_version;
  542. }
  543. }
  544. // Function to check a received SoftDevice or Bootloader firmware, or both,
  545. // before it is copied into place.
  546. static bool postvalidate_sd_bl(dfu_init_command_t * p_init,
  547. bool with_sd,
  548. bool with_bl,
  549. uint32_t start_addr)
  550. {
  551. if (with_sd)
  552. {
  553. if (!softdevice_info_ok(start_addr, p_init->sd_size))
  554. {
  555. return false;
  556. }
  557. if (is_major_softdevice_update(start_addr))
  558. {
  559. NRF_LOG_WARNING("Invalidating app because it is incompatible with the SoftDevice.");
  560. if (DFU_REQUIRES_SOFTDEVICE && !with_bl)
  561. {
  562. NRF_LOG_ERROR("Major SD update but no BL. Abort to avoid incapacitating the BL.");
  563. return false;
  564. }
  565. // Invalidate app since it may not be compatible with new SD.
  566. nrf_dfu_bank_invalidate(&s_dfu_settings.bank_0);
  567. }
  568. // Mark the update as valid.
  569. s_dfu_settings.bank_1.bank_code = with_bl ? NRF_DFU_BANK_VALID_SD_BL
  570. : NRF_DFU_BANK_VALID_SD;
  571. s_dfu_settings.sd_size = p_init->sd_size;
  572. }
  573. else
  574. {
  575. s_dfu_settings.bank_1.bank_code = NRF_DFU_BANK_VALID_BL;
  576. }
  577. if (with_bl &&
  578. (!NRF_DFU_DEBUG ||
  579. (NRF_DFU_DEBUG && (p_init->has_is_debug == false || p_init->is_debug == false))))
  580. {
  581. // If the update contains a bootloader, update the version.
  582. // Unless the update is a debug packet.
  583. s_dfu_settings.bootloader_version = p_init->fw_version;
  584. }
  585. return true;
  586. }
  587. nrf_dfu_result_t nrf_dfu_validation_post_data_execute(uint32_t src_addr, uint32_t data_len)
  588. {
  589. nrf_dfu_result_t ret_val = NRF_DFU_RES_CODE_SUCCESS;
  590. dfu_init_command_t * p_init = m_packet.has_signed_command ?
  591. &m_packet.signed_command.command.init : &m_packet.command.init;
  592. if (!fw_hash_ok(p_init, src_addr, data_len))
  593. {
  594. ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_VERIFICATION_FAILED);
  595. }
  596. else
  597. {
  598. if (p_init->type == DFU_FW_TYPE_APPLICATION)
  599. {
  600. postvalidate_app(p_init);
  601. }
  602. else
  603. {
  604. bool with_sd = p_init->type & DFU_FW_TYPE_SOFTDEVICE;
  605. bool with_bl = p_init->type & DFU_FW_TYPE_BOOTLOADER;
  606. if (!postvalidate_sd_bl(p_init, with_sd, with_bl, src_addr))
  607. {
  608. ret_val = NRF_DFU_RES_CODE_INVALID_OBJECT;
  609. if (with_sd && !DFU_REQUIRES_SOFTDEVICE &&
  610. (src_addr == nrf_dfu_softdevice_start_address()))
  611. {
  612. nrf_dfu_softdevice_invalidate();
  613. }
  614. }
  615. }
  616. }
  617. if (ret_val == NRF_DFU_RES_CODE_SUCCESS)
  618. {
  619. // Store CRC32 for image.
  620. s_dfu_settings.bank_1.image_crc = s_dfu_settings.progress.firmware_image_crc;
  621. s_dfu_settings.bank_1.image_size = data_len;
  622. }
  623. else
  624. {
  625. nrf_dfu_bank_invalidate(&s_dfu_settings.bank_1);
  626. }
  627. // Set the progress to zero and remove the last command.
  628. memset(&s_dfu_settings.progress, 0, sizeof(dfu_progress_t));
  629. memset(s_dfu_settings.init_command, 0xFF, DFU_SIGNED_COMMAND_SIZE);
  630. s_dfu_settings.write_offset = 0;
  631. s_dfu_settings.progress.update_start_address = src_addr;
  632. return ret_val;
  633. }