nrf_bootloader_fw_activation.c 15 KB

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