dfu_single_bank.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include <stddef.h>
  13. #include "dfu.h"
  14. #include <dfu_types.h>
  15. #include "dfu_bank_internal.h"
  16. #include "nrf.h"
  17. #include "nrf_sdm.h"
  18. #include "app_error.h"
  19. #include "app_timer.h"
  20. #include "bootloader.h"
  21. #include "bootloader_types.h"
  22. #include "pstorage.h"
  23. #include "nrf_mbr.h"
  24. #include "dfu_init.h"
  25. #include "sdk_common.h"
  26. static dfu_state_t m_dfu_state; /**< Current DFU state. */
  27. static uint32_t m_image_size; /**< Size of the image that will be transmitted. */
  28. static dfu_start_packet_t m_start_packet; /**< Start packet received for this update procedure. Contains update mode and image sizes information to be used for image transfer. */
  29. static uint8_t m_init_packet[64]; /**< Init packet, can hold CRC, Hash, Signed Hash and similar, for image validation, integrety check and authorization checking. */
  30. static uint8_t m_init_packet_length; /**< Length of init packet received. */
  31. static uint16_t m_image_crc; /**< Calculated CRC of the image received. */
  32. APP_TIMER_DEF(m_dfu_timer_id); /**< Application timer id. */
  33. static bool m_dfu_timed_out = false; /**< Boolean flag value for tracking DFU timer timeout state. */
  34. static pstorage_handle_t m_storage_handle_app; /**< Pstorage handle for the application area (bank 0). Bank used when updating a SoftDevice w/wo bootloader. Handle also used when swapping received application from bank 1 to bank 0. */
  35. static pstorage_handle_t * mp_storage_handle_active; /**< Pointer to the pstorage handle for the active bank for receiving of data packets. */
  36. static dfu_callback_t m_data_pkt_cb; /**< Callback from DFU Bank module for notification of asynchronous operation such as flash prepare. */
  37. static dfu_bank_func_t m_functions; /**< Structure holding operations for the selected update process. */
  38. /**@brief Function for handling callbacks from pstorage module.
  39. *
  40. * @details Handles pstorage results for clear and storage operation. For detailed description of
  41. * the parameters provided with the callback, please refer to \ref pstorage_ntf_cb_t.
  42. */
  43. static void pstorage_callback_handler(pstorage_handle_t * p_handle,
  44. uint8_t op_code,
  45. uint32_t result,
  46. uint8_t * p_data,
  47. uint32_t data_len)
  48. {
  49. switch (op_code)
  50. {
  51. case PSTORAGE_STORE_OP_CODE:
  52. if ((m_dfu_state == DFU_STATE_RX_DATA_PKT) && (m_data_pkt_cb != NULL))
  53. {
  54. m_data_pkt_cb(DATA_PACKET, result, p_data);
  55. }
  56. break;
  57. case PSTORAGE_CLEAR_OP_CODE:
  58. if (m_dfu_state == DFU_STATE_PREPARING)
  59. {
  60. m_functions.cleared();
  61. m_dfu_state = DFU_STATE_RDY;
  62. if (m_data_pkt_cb != NULL)
  63. {
  64. m_data_pkt_cb(START_PACKET, result, p_data);
  65. }
  66. }
  67. break;
  68. default:
  69. break;
  70. }
  71. APP_ERROR_CHECK(result);
  72. }
  73. /**@brief Function for handling the DFU timeout.
  74. *
  75. * @param[in] p_context The timeout context.
  76. */
  77. static void dfu_timeout_handler(void * p_context)
  78. {
  79. UNUSED_PARAMETER(p_context);
  80. dfu_update_status_t update_status;
  81. m_dfu_timed_out = true;
  82. update_status.status_code = DFU_TIMEOUT;
  83. bootloader_dfu_update_process(update_status);
  84. }
  85. /**@brief Function for restarting the DFU Timer.
  86. *
  87. * @details This function will stop and restart the DFU timer. This function will be called by the
  88. * functions handling any DFU packet received from the peer that is transferring a firmware
  89. * image.
  90. */
  91. static uint32_t dfu_timer_restart(void)
  92. {
  93. if (m_dfu_timed_out)
  94. {
  95. // The DFU timer had already timed out.
  96. return NRF_ERROR_INVALID_STATE;
  97. }
  98. uint32_t err_code = app_timer_stop(m_dfu_timer_id);
  99. APP_ERROR_CHECK(err_code);
  100. err_code = app_timer_start(m_dfu_timer_id, DFU_TIMEOUT_INTERVAL, NULL);
  101. APP_ERROR_CHECK(err_code);
  102. return err_code;
  103. }
  104. /**@brief Function for preparing of flash before receiving SoftDevice image.
  105. *
  106. * @details This function will erase current application area to ensure sufficient amount of
  107. * storage for the SoftDevice image. Upon erase complete a callback will be done.
  108. * See \ref dfu_bank_prepare_t for further details.
  109. */
  110. static void dfu_prepare_func_app_erase(uint32_t image_size)
  111. {
  112. uint32_t err_code;
  113. mp_storage_handle_active = &m_storage_handle_app;
  114. // Doing a SoftDevice update thus current application must be cleared to ensure enough space
  115. // for new SoftDevice.
  116. m_dfu_state = DFU_STATE_PREPARING;
  117. err_code = pstorage_clear(&m_storage_handle_app, m_image_size);
  118. APP_ERROR_CHECK(err_code);
  119. }
  120. /**@brief Function for handling behaviour when clear operation has completed.
  121. */
  122. static void dfu_cleared_func_app(void)
  123. {
  124. dfu_update_status_t update_status = {DFU_BANK_0_ERASED, };
  125. bootloader_dfu_update_process(update_status);
  126. }
  127. /**@brief Function for calculating storage offset for receiving SoftDevice image.
  128. *
  129. * @details When a new SoftDevice is received it will be temporary stored in flash before moved to
  130. * address 0x0. In order to succesfully validate transfer and relocation it is important
  131. * that temporary image and final installed image does not ovwerlap hence an offset must
  132. * be calculated in case new image is larger than currently installed SoftDevice.
  133. */
  134. uint32_t offset_calculate(uint32_t sd_image_size)
  135. {
  136. uint32_t offset = 0;
  137. if (m_start_packet.sd_image_size > DFU_BANK_0_REGION_START)
  138. {
  139. uint32_t page_mask = (CODE_PAGE_SIZE - 1);
  140. uint32_t diff = m_start_packet.sd_image_size - DFU_BANK_0_REGION_START;
  141. offset = diff & ~page_mask;
  142. // Align offset to next page if image size is not page sized.
  143. if ((diff & page_mask) > 0)
  144. {
  145. offset += CODE_PAGE_SIZE;
  146. }
  147. }
  148. return offset;
  149. }
  150. /**@brief Function for activating received SoftDevice image.
  151. *
  152. * @note This function will not move the SoftDevice image.
  153. * The bootloader settings will be marked as SoftDevice update complete and the swapping of
  154. * current SoftDevice will occur after system reset.
  155. *
  156. * @return NRF_SUCCESS on success.
  157. */
  158. static uint32_t dfu_activate_sd(void)
  159. {
  160. dfu_update_status_t update_status;
  161. update_status.status_code = DFU_UPDATE_SD_COMPLETE;
  162. update_status.app_crc = m_image_crc;
  163. update_status.sd_image_start = DFU_BANK_0_REGION_START;
  164. update_status.sd_size = m_start_packet.sd_image_size;
  165. update_status.bl_size = m_start_packet.bl_image_size;
  166. update_status.app_size = m_start_packet.app_image_size;
  167. bootloader_dfu_update_process(update_status);
  168. return NRF_SUCCESS;
  169. }
  170. /**@brief Function for activating received Application image.
  171. *
  172. * @details This function will move the received application image fram swap (bank 1) to
  173. * application area (bank 0).
  174. *
  175. * @return NRF_SUCCESS on success. Error code otherwise.
  176. */
  177. static uint32_t dfu_activate_app(void)
  178. {
  179. uint32_t err_code = NRF_SUCCESS;
  180. dfu_update_status_t update_status;
  181. memset(&update_status, 0, sizeof(dfu_update_status_t ));
  182. update_status.status_code = DFU_UPDATE_APP_COMPLETE;
  183. update_status.app_crc = m_image_crc;
  184. update_status.app_size = m_start_packet.app_image_size;
  185. bootloader_dfu_update_process(update_status);
  186. return err_code;
  187. }
  188. /**@brief Function for activating received Bootloader image.
  189. *
  190. * @note This function will not move the bootloader image.
  191. * The bootloader settings will be marked as Bootloader update complete and the swapping of
  192. * current bootloader will occur after system reset.
  193. *
  194. * @return NRF_SUCCESS on success.
  195. */
  196. static uint32_t dfu_activate_bl(void)
  197. {
  198. dfu_update_status_t update_status;
  199. update_status.status_code = DFU_UPDATE_BOOT_COMPLETE;
  200. update_status.app_crc = m_image_crc;
  201. update_status.sd_size = m_start_packet.sd_image_size;
  202. update_status.bl_size = m_start_packet.bl_image_size;
  203. update_status.app_size = m_start_packet.app_image_size;
  204. bootloader_dfu_update_process(update_status);
  205. return NRF_SUCCESS;
  206. }
  207. uint32_t dfu_init(void)
  208. {
  209. uint32_t err_code;
  210. pstorage_module_param_t storage_module_param = {.cb = pstorage_callback_handler};
  211. m_init_packet_length = 0;
  212. m_image_crc = 0;
  213. err_code = pstorage_register(&storage_module_param, &m_storage_handle_app);
  214. if (err_code != NRF_SUCCESS)
  215. {
  216. m_dfu_state = DFU_STATE_INIT_ERROR;
  217. return err_code;
  218. }
  219. m_storage_handle_app.block_id = DFU_BANK_0_REGION_START;
  220. // Create the timer to monitor the activity by the peer doing the firmware update.
  221. err_code = app_timer_create(&m_dfu_timer_id,
  222. APP_TIMER_MODE_SINGLE_SHOT,
  223. dfu_timeout_handler);
  224. APP_ERROR_CHECK(err_code);
  225. // Start the DFU timer.
  226. err_code = app_timer_start(m_dfu_timer_id, DFU_TIMEOUT_INTERVAL, NULL);
  227. APP_ERROR_CHECK(err_code);
  228. m_data_received = 0;
  229. m_dfu_state = DFU_STATE_IDLE;
  230. return NRF_SUCCESS;
  231. }
  232. void dfu_register_callback(dfu_callback_t callback_handler)
  233. {
  234. m_data_pkt_cb = callback_handler;
  235. }
  236. uint32_t dfu_start_pkt_handle(dfu_update_packet_t * p_packet)
  237. {
  238. uint32_t err_code;
  239. m_start_packet = *(p_packet->params.start_packet);
  240. // Check that the requested update procedure is supported.
  241. // Currently the following combinations are allowed:
  242. // - Application
  243. // - SoftDevice
  244. // - Bootloader
  245. // - SoftDevice with Bootloader
  246. if (IS_UPDATING_APP(m_start_packet) &&
  247. (IS_UPDATING_SD(m_start_packet) || IS_UPDATING_BL(m_start_packet)))
  248. {
  249. // App update is only supported independently.
  250. return NRF_ERROR_NOT_SUPPORTED;
  251. }
  252. if (!(IS_WORD_SIZED(m_start_packet.sd_image_size) &&
  253. IS_WORD_SIZED(m_start_packet.bl_image_size) &&
  254. IS_WORD_SIZED(m_start_packet.app_image_size)))
  255. {
  256. // Image_sizes are not a multiple of 4 (word size).
  257. return NRF_ERROR_NOT_SUPPORTED;
  258. }
  259. m_image_size = m_start_packet.sd_image_size + m_start_packet.bl_image_size +
  260. m_start_packet.app_image_size;
  261. if (m_start_packet.bl_image_size > DFU_BL_IMAGE_MAX_SIZE)
  262. {
  263. return NRF_ERROR_DATA_SIZE;
  264. }
  265. if (m_image_size > (DFU_IMAGE_MAX_SIZE_FULL))
  266. {
  267. return NRF_ERROR_DATA_SIZE;
  268. }
  269. m_functions.prepare = dfu_prepare_func_app_erase;
  270. m_functions.cleared = dfu_cleared_func_app;
  271. if (IS_UPDATING_SD(m_start_packet))
  272. {
  273. m_functions.activate = dfu_activate_sd;
  274. }
  275. else if (IS_UPDATING_BL(m_start_packet))
  276. {
  277. m_functions.activate = dfu_activate_bl;
  278. }
  279. else
  280. {
  281. m_functions.activate = dfu_activate_app;
  282. }
  283. switch (m_dfu_state)
  284. {
  285. case DFU_STATE_IDLE:
  286. // Valid peer activity detected. Hence restart the DFU timer.
  287. err_code = dfu_timer_restart();
  288. VERIFY_SUCCESS(err_code);
  289. m_functions.prepare(m_image_size);
  290. break;
  291. default:
  292. err_code = NRF_ERROR_INVALID_STATE;
  293. break;
  294. }
  295. return err_code;
  296. }
  297. uint32_t dfu_data_pkt_handle(dfu_update_packet_t * p_packet)
  298. {
  299. uint32_t data_length;
  300. uint32_t err_code;
  301. uint32_t * p_data;
  302. VERIFY_PARAM_NOT_NULL(p_packet);
  303. // Check pointer alignment.
  304. if (!is_word_aligned(p_packet->params.data_packet.p_data_packet))
  305. {
  306. // The p_data_packet is not word aligned address.
  307. return NRF_ERROR_INVALID_ADDR;
  308. }
  309. switch (m_dfu_state)
  310. {
  311. case DFU_STATE_RDY:
  312. case DFU_STATE_RX_INIT_PKT:
  313. return NRF_ERROR_INVALID_STATE;
  314. case DFU_STATE_RX_DATA_PKT:
  315. data_length = p_packet->params.data_packet.packet_length * sizeof(uint32_t);
  316. if ((m_data_received + data_length) > m_image_size)
  317. {
  318. // The caller is trying to write more bytes into the flash than the size provided to
  319. // the dfu_image_size_set function. This is treated as a serious error condition and
  320. // an unrecoverable one. Hence point the variable mp_app_write_address to the top of
  321. // the flash area. This will ensure that all future application data packet writes
  322. // will be blocked because of the above check.
  323. m_data_received = 0xFFFFFFFF;
  324. return NRF_ERROR_DATA_SIZE;
  325. }
  326. // Valid peer activity detected. Hence restart the DFU timer.
  327. err_code = dfu_timer_restart();
  328. VERIFY_SUCCESS(err_code);
  329. p_data = (uint32_t *)p_packet->params.data_packet.p_data_packet;
  330. err_code = pstorage_store(mp_storage_handle_active,
  331. (uint8_t *)p_data,
  332. data_length,
  333. m_data_received);
  334. VERIFY_SUCCESS(err_code);
  335. m_data_received += data_length;
  336. if (m_data_received != m_image_size)
  337. {
  338. // The entire image is not received yet. More data is expected.
  339. err_code = NRF_ERROR_INVALID_LENGTH;
  340. }
  341. else
  342. {
  343. // The entire image has been received. Return NRF_SUCCESS.
  344. err_code = NRF_SUCCESS;
  345. }
  346. break;
  347. default:
  348. err_code = NRF_ERROR_INVALID_STATE;
  349. break;
  350. }
  351. return err_code;
  352. }
  353. uint32_t dfu_init_pkt_complete(void)
  354. {
  355. uint32_t err_code = NRF_ERROR_INVALID_STATE;
  356. // DFU initialization has been done and a start packet has been received.
  357. if (IMAGE_WRITE_IN_PROGRESS())
  358. {
  359. // Image write is already in progress. Cannot handle an init packet now.
  360. return NRF_ERROR_INVALID_STATE;
  361. }
  362. if (m_dfu_state == DFU_STATE_RX_INIT_PKT)
  363. {
  364. err_code = dfu_init_prevalidate(m_init_packet, m_init_packet_length);
  365. if (err_code == NRF_SUCCESS)
  366. {
  367. m_dfu_state = DFU_STATE_RX_DATA_PKT;
  368. }
  369. else
  370. {
  371. m_init_packet_length = 0;
  372. }
  373. }
  374. return err_code;
  375. }
  376. uint32_t dfu_init_pkt_handle(dfu_update_packet_t * p_packet)
  377. {
  378. uint32_t err_code = NRF_SUCCESS;
  379. uint32_t length;
  380. switch (m_dfu_state)
  381. {
  382. case DFU_STATE_RDY:
  383. m_dfu_state = DFU_STATE_RX_INIT_PKT;
  384. // When receiving init packet in state ready just update and fall through this case.
  385. case DFU_STATE_RX_INIT_PKT:
  386. // DFU initialization has been done and a start packet has been received.
  387. if (IMAGE_WRITE_IN_PROGRESS())
  388. {
  389. // Image write is already in progress. Cannot handle an init packet now.
  390. return NRF_ERROR_INVALID_STATE;
  391. }
  392. // Valid peer activity detected. Hence restart the DFU timer.
  393. err_code = dfu_timer_restart();
  394. VERIFY_SUCCESS(err_code);
  395. length = p_packet->params.data_packet.packet_length * sizeof(uint32_t);
  396. if ((m_init_packet_length + length) > sizeof(m_init_packet))
  397. {
  398. return NRF_ERROR_INVALID_LENGTH;
  399. }
  400. memcpy(&m_init_packet[m_init_packet_length],
  401. &p_packet->params.data_packet.p_data_packet[0],
  402. length);
  403. m_init_packet_length += length;
  404. break;
  405. default:
  406. // Either the start packet was not received or dfu_init function was not called before.
  407. err_code = NRF_ERROR_INVALID_STATE;
  408. break;
  409. }
  410. return err_code;
  411. }
  412. uint32_t dfu_image_validate()
  413. {
  414. uint32_t err_code;
  415. switch (m_dfu_state)
  416. {
  417. case DFU_STATE_RX_DATA_PKT:
  418. // Check if the application image write has finished.
  419. if (m_data_received != m_image_size)
  420. {
  421. // Image not yet fully transfered by the peer or the peer has attempted to write
  422. // too much data. Hence the validation should fail.
  423. err_code = NRF_ERROR_INVALID_STATE;
  424. }
  425. else
  426. {
  427. m_dfu_state = DFU_STATE_VALIDATE;
  428. // Valid peer activity detected. Hence restart the DFU timer.
  429. err_code = dfu_timer_restart();
  430. if (err_code == NRF_SUCCESS)
  431. {
  432. err_code = dfu_init_postvalidate((uint8_t *)mp_storage_handle_active->block_id,
  433. m_image_size);
  434. VERIFY_SUCCESS(err_code);
  435. m_dfu_state = DFU_STATE_WAIT_4_ACTIVATE;
  436. }
  437. }
  438. break;
  439. default:
  440. err_code = NRF_ERROR_INVALID_STATE;
  441. break;
  442. }
  443. return err_code;
  444. }
  445. uint32_t dfu_image_activate()
  446. {
  447. uint32_t err_code;
  448. switch (m_dfu_state)
  449. {
  450. case DFU_STATE_WAIT_4_ACTIVATE:
  451. // Stop the DFU Timer because the peer activity need not be monitored any longer.
  452. err_code = app_timer_stop(m_dfu_timer_id);
  453. APP_ERROR_CHECK(err_code);
  454. err_code = m_functions.activate();
  455. break;
  456. default:
  457. err_code = NRF_ERROR_INVALID_STATE;
  458. break;
  459. }
  460. return err_code;
  461. }
  462. void dfu_reset(void)
  463. {
  464. dfu_update_status_t update_status;
  465. update_status.status_code = DFU_RESET;
  466. bootloader_dfu_update_process(update_status);
  467. }
  468. static uint32_t dfu_compare_block(uint32_t * ptr1, uint32_t * ptr2, uint32_t len)
  469. {
  470. sd_mbr_command_t sd_mbr_cmd;
  471. sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
  472. sd_mbr_cmd.params.compare.ptr1 = ptr1;
  473. sd_mbr_cmd.params.compare.ptr2 = ptr2;
  474. sd_mbr_cmd.params.compare.len = len / sizeof(uint32_t);
  475. return sd_mbr_command(&sd_mbr_cmd);
  476. }
  477. static uint32_t dfu_copy_sd(uint32_t * src, uint32_t * dst, uint32_t len)
  478. {
  479. sd_mbr_command_t sd_mbr_cmd;
  480. sd_mbr_cmd.command = SD_MBR_COMMAND_COPY_SD;
  481. sd_mbr_cmd.params.copy_sd.src = src;
  482. sd_mbr_cmd.params.copy_sd.dst = dst;
  483. sd_mbr_cmd.params.copy_sd.len = len / sizeof(uint32_t);
  484. return sd_mbr_command(&sd_mbr_cmd);
  485. }
  486. static uint32_t dfu_sd_img_block_swap(uint32_t * src,
  487. uint32_t * dst,
  488. uint32_t len,
  489. uint32_t block_size)
  490. {
  491. // It is neccesarry to swap the new SoftDevice in 3 rounds to ensure correct copy of data
  492. // and verifucation of data in case power reset occurs during write to flash.
  493. // To ensure the robustness of swapping the images are compared backwards till start of
  494. // image swap. If the back is identical everything is swapped.
  495. uint32_t err_code = dfu_compare_block(src, dst, len);
  496. if (err_code == NRF_SUCCESS)
  497. {
  498. return err_code;
  499. }
  500. if ((uint32_t)dst > SOFTDEVICE_REGION_START)
  501. {
  502. err_code = dfu_sd_img_block_swap((uint32_t *)((uint32_t)src - block_size),
  503. (uint32_t *)((uint32_t)dst - block_size),
  504. block_size,
  505. block_size);
  506. VERIFY_SUCCESS(err_code);
  507. }
  508. err_code = dfu_copy_sd(src, dst, len);
  509. VERIFY_SUCCESS(err_code);
  510. return dfu_compare_block(src, dst, len);
  511. }
  512. uint32_t dfu_sd_image_swap(void)
  513. {
  514. bootloader_settings_t boot_settings;
  515. bootloader_settings_get(&boot_settings);
  516. if (boot_settings.sd_image_size == 0)
  517. {
  518. return NRF_SUCCESS;
  519. }
  520. if ((SOFTDEVICE_REGION_START + boot_settings.sd_image_size) > boot_settings.sd_image_start)
  521. {
  522. uint32_t err_code;
  523. uint32_t sd_start = SOFTDEVICE_REGION_START;
  524. uint32_t block_size = (boot_settings.sd_image_start - sd_start) / 2;
  525. uint32_t image_end = boot_settings.sd_image_start + boot_settings.sd_image_size;
  526. uint32_t img_block_start = boot_settings.sd_image_start + 2 * block_size;
  527. uint32_t sd_block_start = sd_start + 2 * block_size;
  528. if (SD_SIZE_GET(MBR_SIZE) < boot_settings.sd_image_size)
  529. {
  530. // This will clear a page thus ensuring the old image is invalidated before swapping.
  531. err_code = dfu_copy_sd((uint32_t *)(sd_start + block_size),
  532. (uint32_t *)(sd_start + block_size),
  533. sizeof(uint32_t));
  534. VERIFY_SUCCESS(err_code);
  535. err_code = dfu_copy_sd((uint32_t *)sd_start, (uint32_t *)sd_start, sizeof(uint32_t));
  536. VERIFY_SUCCESS(err_code);
  537. }
  538. return dfu_sd_img_block_swap((uint32_t *)img_block_start,
  539. (uint32_t *)sd_block_start,
  540. image_end - img_block_start,
  541. block_size);
  542. }
  543. else
  544. {
  545. if (boot_settings.sd_image_size != 0)
  546. {
  547. return dfu_copy_sd((uint32_t *)boot_settings.sd_image_start,
  548. (uint32_t *)SOFTDEVICE_REGION_START,
  549. boot_settings.sd_image_size);
  550. }
  551. }
  552. return NRF_SUCCESS;
  553. }
  554. uint32_t dfu_bl_image_swap(void)
  555. {
  556. bootloader_settings_t bootloader_settings;
  557. sd_mbr_command_t sd_mbr_cmd;
  558. bootloader_settings_get(&bootloader_settings);
  559. if (bootloader_settings.bl_image_size != 0)
  560. {
  561. uint32_t bl_image_start = (bootloader_settings.sd_image_size == 0) ?
  562. DFU_BANK_0_REGION_START :
  563. bootloader_settings.sd_image_start +
  564. bootloader_settings.sd_image_size;
  565. sd_mbr_cmd.command = SD_MBR_COMMAND_COPY_BL;
  566. sd_mbr_cmd.params.copy_bl.bl_src = (uint32_t *)(bl_image_start);
  567. sd_mbr_cmd.params.copy_bl.bl_len = bootloader_settings.bl_image_size / sizeof(uint32_t);
  568. return sd_mbr_command(&sd_mbr_cmd);
  569. }
  570. return NRF_SUCCESS;
  571. }
  572. uint32_t dfu_bl_image_validate(void)
  573. {
  574. bootloader_settings_t bootloader_settings;
  575. sd_mbr_command_t sd_mbr_cmd;
  576. bootloader_settings_get(&bootloader_settings);
  577. if (bootloader_settings.bl_image_size != 0)
  578. {
  579. uint32_t bl_image_start = (bootloader_settings.sd_image_size == 0) ?
  580. DFU_BANK_0_REGION_START :
  581. bootloader_settings.sd_image_start +
  582. bootloader_settings.sd_image_size;
  583. sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
  584. sd_mbr_cmd.params.compare.ptr1 = (uint32_t *)BOOTLOADER_REGION_START;
  585. sd_mbr_cmd.params.compare.ptr2 = (uint32_t *)(bl_image_start);
  586. sd_mbr_cmd.params.compare.len = bootloader_settings.bl_image_size / sizeof(uint32_t);
  587. return sd_mbr_command(&sd_mbr_cmd);
  588. }
  589. return NRF_SUCCESS;
  590. }
  591. uint32_t dfu_sd_image_validate(void)
  592. {
  593. bootloader_settings_t bootloader_settings;
  594. sd_mbr_command_t sd_mbr_cmd;
  595. bootloader_settings_get(&bootloader_settings);
  596. if (bootloader_settings.sd_image_size == 0)
  597. {
  598. return NRF_SUCCESS;
  599. }
  600. if ((SOFTDEVICE_REGION_START + bootloader_settings.sd_image_size) > bootloader_settings.sd_image_start)
  601. {
  602. uint32_t sd_start = SOFTDEVICE_REGION_START;
  603. uint32_t block_size = (bootloader_settings.sd_image_start - sd_start) / 2;
  604. uint32_t image_end = bootloader_settings.sd_image_start +
  605. bootloader_settings.sd_image_size;
  606. uint32_t img_block_start = bootloader_settings.sd_image_start + 2 * block_size;
  607. uint32_t sd_block_start = sd_start + 2 * block_size;
  608. if (SD_SIZE_GET(MBR_SIZE) < bootloader_settings.sd_image_size)
  609. {
  610. return NRF_ERROR_NULL;
  611. }
  612. return dfu_sd_img_block_swap((uint32_t *)img_block_start,
  613. (uint32_t *)sd_block_start,
  614. image_end - img_block_start,
  615. block_size);
  616. }
  617. sd_mbr_cmd.command = SD_MBR_COMMAND_COMPARE;
  618. sd_mbr_cmd.params.compare.ptr1 = (uint32_t *)SOFTDEVICE_REGION_START;
  619. sd_mbr_cmd.params.compare.ptr2 = (uint32_t *)bootloader_settings.sd_image_start;
  620. sd_mbr_cmd.params.compare.len = bootloader_settings.sd_image_size / sizeof(uint32_t);
  621. return sd_mbr_command(&sd_mbr_cmd);
  622. }