nrf_bootloader_fw_activation.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /**
  2. * Copyright (c) 2016 - 2020, 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 "nrf_bootloader_fw_activation.h"
  41. #include "nrf_dfu_settings.h"
  42. #include "nrf_dfu_mbr.h"
  43. #include "nrf_bootloader_info.h"
  44. #include "crc32.h"
  45. #include "nrf_log.h"
  46. #include "nrf_log_ctrl.h"
  47. #include "nrf_dfu_utils.h"
  48. #include "nrf_bootloader_wdt.h"
  49. static volatile bool m_flash_write_done;
  50. /**
  51. * @brief Function for copying image. Image is copied in chunks. Frequency of storing progress
  52. * in flash is configured by input parameter.
  53. *
  54. * @param[in] dst_addr Destination address. Must be page aligned.
  55. * @param[in] src_addr Source address. Must be higher value than dst_addr.
  56. * @param[in] size Image size.
  57. * @param[in] progress_update_step Number of copied pages that triggers saving progress to non-volatile memory.
  58. * Note that step can be decreased if there is a risk of corruption caused by source
  59. * and destination overlapping.
  60. *
  61. * @return NRF_SUCCESS or error code in case of failure.
  62. */
  63. static uint32_t image_copy(uint32_t dst_addr,
  64. uint32_t src_addr,
  65. uint32_t size,
  66. uint32_t progress_update_step)
  67. {
  68. if (src_addr == dst_addr)
  69. {
  70. NRF_LOG_DEBUG("No copy needed");
  71. return NRF_SUCCESS;
  72. }
  73. ASSERT(src_addr >= dst_addr);
  74. ASSERT(progress_update_step > 0);
  75. if (size != 0)
  76. {
  77. ASSERT((dst_addr % CODE_PAGE_SIZE) == 0);
  78. }
  79. uint32_t max_safe_progress_upd_step = (src_addr - dst_addr)/CODE_PAGE_SIZE;
  80. ASSERT(max_safe_progress_upd_step > 0);
  81. uint32_t ret_val = NRF_SUCCESS;
  82. uint32_t pages_left = CEIL_DIV(size, CODE_PAGE_SIZE);
  83. //Firmware copying is time consuming operation thus watchdog handling is started
  84. nrf_bootloader_wdt_init();
  85. progress_update_step = MIN(progress_update_step, max_safe_progress_upd_step);
  86. while (size > 0)
  87. {
  88. uint32_t pages;
  89. uint32_t bytes;
  90. if (pages_left <= progress_update_step)
  91. {
  92. pages = pages_left;
  93. bytes = size;
  94. }
  95. else
  96. {
  97. pages = progress_update_step;
  98. bytes = progress_update_step * CODE_PAGE_SIZE;
  99. }
  100. // Erase the target pages
  101. ret_val = nrf_dfu_flash_erase(dst_addr, pages, NULL);
  102. if (ret_val != NRF_SUCCESS)
  103. {
  104. return ret_val;
  105. }
  106. // Flash one page
  107. NRF_LOG_DEBUG("Copying 0x%x to 0x%x, size: 0x%x", src_addr, dst_addr, bytes);
  108. ret_val = nrf_dfu_flash_store(dst_addr,
  109. (uint32_t *)src_addr,
  110. ALIGN_NUM(sizeof(uint32_t), bytes),
  111. NULL);
  112. if (ret_val != NRF_SUCCESS)
  113. {
  114. return ret_val;
  115. }
  116. pages_left -= pages;
  117. size -= bytes;
  118. dst_addr += bytes;
  119. src_addr += bytes;
  120. s_dfu_settings.write_offset += bytes;
  121. //store progress in flash on every successful chunk write
  122. ret_val = nrf_dfu_settings_write_and_backup(NULL);
  123. if (ret_val != NRF_SUCCESS)
  124. {
  125. NRF_LOG_ERROR("Failed to write image copying progress to settings page.");
  126. return ret_val;
  127. }
  128. }
  129. return ret_val;
  130. }
  131. /** @brief Function to continue application update.
  132. *
  133. * @details This function will be called after reset if there is a valid application in Bank1
  134. * required to be copied down to Bank 0.
  135. *
  136. * @return NRF_SUCCESS if continuation was successful, NRF_ERROR_INTERNAL if new firmware does not
  137. * contain softdevice or other error coming from modules used by this function.
  138. */
  139. static uint32_t app_activate(void)
  140. {
  141. // This function is only in use when new app is present in Bank 1
  142. uint32_t const image_size = s_dfu_settings.bank_1.image_size;
  143. uint32_t src_addr = s_dfu_settings.progress.update_start_address;
  144. uint32_t ret_val = NRF_SUCCESS;
  145. uint32_t target_addr = nrf_dfu_bank0_start_addr() + s_dfu_settings.write_offset;
  146. uint32_t length_left = (image_size - s_dfu_settings.write_offset);
  147. uint32_t crc;
  148. NRF_LOG_DEBUG("Enter nrf_dfu_app_continue");
  149. src_addr += s_dfu_settings.write_offset;
  150. if (src_addr == target_addr)
  151. {
  152. length_left = 0;
  153. }
  154. ret_val = image_copy(target_addr, src_addr, length_left, NRF_BL_FW_COPY_PROGRESS_STORE_STEP);
  155. if (ret_val != NRF_SUCCESS)
  156. {
  157. NRF_LOG_ERROR("Failed to copy firmware.");
  158. return ret_val;
  159. }
  160. // Check the CRC of the copied data. Enable if so.
  161. crc = crc32_compute((uint8_t*)nrf_dfu_bank0_start_addr(), image_size, NULL);
  162. if (crc == s_dfu_settings.bank_1.image_crc)
  163. {
  164. NRF_LOG_DEBUG("Setting app as valid");
  165. s_dfu_settings.bank_0.bank_code = NRF_DFU_BANK_VALID_APP;
  166. s_dfu_settings.bank_0.image_crc = crc;
  167. s_dfu_settings.bank_0.image_size = image_size;
  168. }
  169. else
  170. {
  171. NRF_LOG_ERROR("CRC computation failed for copied app: "
  172. "src crc: 0x%08x, res crc: 0x%08x",
  173. s_dfu_settings.bank_1.image_crc,
  174. crc);
  175. }
  176. return ret_val;
  177. }
  178. /** @brief Function to execute the continuation of a SoftDevice update.
  179. *
  180. * @return NRF_SUCCESS if continuation was successful, NRF_ERROR_INTERNAL if new firmware does not
  181. * contain softdevice or other error coming from modules used by this function.
  182. */
  183. static uint32_t sd_activate(void)
  184. {
  185. uint32_t ret_val = NRF_SUCCESS;
  186. uint32_t target_addr = nrf_dfu_softdevice_start_address() + s_dfu_settings.write_offset;
  187. uint32_t src_addr = s_dfu_settings.progress.update_start_address;
  188. uint32_t sd_size = s_dfu_settings.sd_size;
  189. uint32_t length_left = ALIGN_TO_PAGE(sd_size - s_dfu_settings.write_offset);
  190. NRF_LOG_DEBUG("Enter nrf_bootloader_dfu_sd_continue");
  191. if (SD_MAGIC_NUMBER_GET(src_addr) != SD_MAGIC_NUMBER)
  192. {
  193. NRF_LOG_ERROR("Source address does not contain a valid SoftDevice.")
  194. return NRF_ERROR_INTERNAL;
  195. }
  196. // This can be a continuation due to a power failure
  197. src_addr += s_dfu_settings.write_offset;
  198. if (s_dfu_settings.write_offset == sd_size)
  199. {
  200. NRF_LOG_DEBUG("SD already copied");
  201. return NRF_SUCCESS;
  202. }
  203. if (s_dfu_settings.write_offset == 0)
  204. {
  205. NRF_LOG_DEBUG("Updating SD. Old SD ver: %d, New ver: %d",
  206. SD_VERSION_GET(MBR_SIZE) / 1000000, SD_VERSION_GET(src_addr) / 1000000);
  207. }
  208. ret_val = image_copy(target_addr, src_addr, length_left, NRF_BL_FW_COPY_PROGRESS_STORE_STEP);
  209. if (ret_val != NRF_SUCCESS)
  210. {
  211. NRF_LOG_ERROR("Failed to copy firmware.");
  212. return ret_val;
  213. }
  214. ret_val = nrf_dfu_settings_write_and_backup(NULL);
  215. return ret_val;
  216. }
  217. /** @brief Function to continue bootloader update.
  218. *
  219. * @details This function will be called after reset if there is a valid bootloader in Bank 0 or Bank 1
  220. * required to be relocated and activated through MBR commands.
  221. *
  222. * @return This function will not return if the bootloader is copied successfully.
  223. * After the copy is verified, the device will reset and start the new bootloader.
  224. *
  225. * @retval NRF_SUCCESS Continuation was successful.
  226. * @retval NRF_ERROR_INVALID_LENGTH Invalid length of flash operation.
  227. * @retval NRF_ERROR_NO_MEM If no parameter page is provided (see sds for more info).
  228. * @retval NRF_ERROR_INVALID_PARAM If an invalid command is given.
  229. * @retval NRF_ERROR_INTERNAL Internal error that should not happen.
  230. * @retval NRF_ERROR_FORBIDDEN If NRF_UICR->BOOTADDR is not set.
  231. */
  232. static uint32_t bl_activate(void)
  233. {
  234. uint32_t ret_val = NRF_ERROR_INVALID_DATA;
  235. nrf_dfu_bank_t * p_bank = &s_dfu_settings.bank_1;
  236. uint32_t len = p_bank->image_size;
  237. uint32_t src_addr = s_dfu_settings.progress.update_start_address;
  238. if (p_bank->bank_code == NRF_DFU_BANK_VALID_SD_BL)
  239. {
  240. src_addr += s_dfu_settings.sd_size;
  241. len -= s_dfu_settings.sd_size;
  242. }
  243. else if (src_addr == 0)
  244. {
  245. src_addr = nrf_dfu_bank1_start_addr();
  246. }
  247. NRF_LOG_DEBUG("Verifying BL: Addr: 0x%08x, Src: 0x%08x, Len: 0x%08x", BOOTLOADER_START_ADDR, src_addr, len);
  248. // This code is a configurable workaround for updating SD+BL from SDK 12.x.y - 14.1.0
  249. // SoftDevice size increase would lead to unaligned source address when comparing new BL in SD+BL updates.
  250. // This workaround is not required once BL is successfully installed with a version that is compiled SDK 14.1.0
  251. #if defined(NRF52832_XXAA) && defined(BLE_STACK_SUPPORT_REQD)
  252. if ((p_bank->bank_code == NRF_DFU_BANK_VALID_SD_BL) &&
  253. (memcmp((void *)BOOTLOADER_START_ADDR, (void *)(src_addr - 0x4000), len) == 0))
  254. {
  255. ret_val = NRF_SUCCESS;
  256. }
  257. #endif // defined(NRF52832_XXAA)
  258. // Check if the BL has already been copied.
  259. if ((ret_val != NRF_SUCCESS) &&
  260. (memcmp((void *)BOOTLOADER_START_ADDR, (void *)src_addr, len) == 0))
  261. {
  262. ret_val = NRF_SUCCESS;
  263. }
  264. // If the bootloader is the same as the banked version, the copy is finished
  265. if (ret_val == NRF_SUCCESS)
  266. {
  267. NRF_LOG_DEBUG("No bootloader copy needed, bootloader update complete.");
  268. }
  269. else
  270. {
  271. NRF_LOG_DEBUG("Copying bootloader: Src: 0x%08x, Len: 0x%08x", src_addr, len);
  272. NRF_LOG_FLUSH();
  273. nrf_bootloader_wdt_feed();
  274. // Bootloader is different than the banked version. Continue copy
  275. // Note that if the SD and BL was combined, then the split point between them is in s_dfu_settings.sd_size
  276. // On success this function won't return.
  277. ret_val = nrf_dfu_mbr_copy_bl((uint32_t*)src_addr, len);
  278. if (ret_val != NRF_SUCCESS)
  279. {
  280. NRF_LOG_ERROR("Request to copy BL failed");
  281. }
  282. }
  283. return ret_val;
  284. }
  285. /** @brief Function to continue combined bootloader and SoftDevice update.
  286. *
  287. * @details This function will be called after reset if there is a valid bootloader and SoftDevice in Bank 0 or Bank 1
  288. * required to be relocated and activated through MBR commands.
  289. *
  290. * @retval NRF_SUCCESS Continuation was successful.
  291. * @retval NRF_ERROR_INVALID_LENGTH Invalid length.
  292. * @retval NRF_ERROR_NO_MEM If UICR.NRFFW[1] is not set (i.e. is 0xFFFFFFFF).
  293. * @retval NRF_ERROR_INVALID_PARAM If an invalid command is given.
  294. * @retval NRF_ERROR_INTERNAL Indicates that the contents of the memory blocks where not verified correctly after copying.
  295. * @retval NRF_ERROR_NULL If the content of the memory blocks differs after copying.
  296. * @retval NRF_ERROR_FORBIDDEN If NRF_UICR->BOOTADDR is not set.
  297. */
  298. static uint32_t sd_bl_activate()
  299. {
  300. uint32_t ret_val = NRF_SUCCESS;
  301. NRF_LOG_DEBUG("Enter nrf_dfu_sd_bl_continue");
  302. ret_val = sd_activate();
  303. if (ret_val != NRF_SUCCESS)
  304. {
  305. NRF_LOG_ERROR("SD+BL: SD copy failed");
  306. return ret_val;
  307. }
  308. ret_val = bl_activate();
  309. if (ret_val != NRF_SUCCESS)
  310. {
  311. NRF_LOG_ERROR("SD+BL: BL copy failed");
  312. return ret_val;
  313. }
  314. return ret_val;
  315. }
  316. static void flash_write_callback(void * p_context)
  317. {
  318. UNUSED_PARAMETER(p_context);
  319. m_flash_write_done = true;
  320. }
  321. nrf_bootloader_fw_activation_result_t nrf_bootloader_fw_activate(void)
  322. {
  323. nrf_bootloader_fw_activation_result_t result;
  324. uint32_t ret_val = NRF_SUCCESS;
  325. nrf_dfu_bank_t * p_bank = &s_dfu_settings.bank_1;
  326. bool sd_update = false;
  327. NRF_LOG_DEBUG("Enter nrf_bootloader_fw_activate");
  328. switch (p_bank->bank_code)
  329. {
  330. case NRF_DFU_BANK_VALID_APP:
  331. NRF_LOG_DEBUG("Valid App");
  332. ret_val = app_activate();
  333. break;
  334. case NRF_DFU_BANK_VALID_SD:
  335. NRF_LOG_DEBUG("Valid SD");
  336. ret_val = sd_activate();
  337. sd_update = true;
  338. break;
  339. case NRF_DFU_BANK_VALID_BL:
  340. NRF_LOG_DEBUG("Valid BL");
  341. ret_val = bl_activate();
  342. break;
  343. case NRF_DFU_BANK_VALID_SD_BL:
  344. NRF_LOG_DEBUG("Valid SD + BL");
  345. ret_val = sd_bl_activate();
  346. sd_update = true;
  347. break;
  348. case NRF_DFU_BANK_INVALID:
  349. default:
  350. NRF_LOG_INFO("No firmware to activate.");
  351. return ACTIVATION_NONE;
  352. }
  353. if (ret_val != NRF_SUCCESS)
  354. {
  355. NRF_LOG_ERROR("Activation failed with error %d (bank code: 0x%x)", ret_val, p_bank->bank_code);
  356. result = ACTIVATION_ERROR;
  357. }
  358. // Invalidate bank, marking completion.
  359. nrf_dfu_bank_invalidate(p_bank);
  360. m_flash_write_done = false;
  361. ret_val = nrf_dfu_settings_write_and_backup(flash_write_callback);
  362. ASSERT(m_flash_write_done); /* At this point flash module is performing blocking operation. It is expected that operation is already performed. */
  363. if (ret_val == NRF_SUCCESS)
  364. {
  365. result = ACTIVATION_SUCCESS;
  366. if (sd_update && (s_dfu_settings.bank_0.bank_code == NRF_DFU_BANK_VALID_APP))
  367. {
  368. //If SD was updated and application is valid we want to stay in DFU to receive application.
  369. NRF_LOG_DEBUG("A SoftDevice has just been activated. It's likely that an application will come immediately");
  370. result = ACTIVATION_SUCCESS_EXPECT_ADDITIONAL_UPDATE;
  371. }
  372. }
  373. else
  374. {
  375. NRF_LOG_ERROR("Could not write settings.");
  376. result = ACTIVATION_ERROR;
  377. }
  378. return result;
  379. }