nrf_bootloader.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * Copyright (c) 2016 - 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 "nrf_bootloader.h"
  41. #include "compiler_abstraction.h"
  42. #include "nrf.h"
  43. #include "boards.h"
  44. #include "sdk_config.h"
  45. #include "nrf_power.h"
  46. #include "nrf_delay.h"
  47. #include "nrf_log.h"
  48. #include "nrf_log_ctrl.h"
  49. #include "nrf_dfu.h"
  50. #include "nrf_error.h"
  51. #include "nrf_dfu_settings.h"
  52. #include "nrf_dfu_utils.h"
  53. #include "nrf_bootloader_wdt.h"
  54. #include "nrf_bootloader_info.h"
  55. #include "nrf_bootloader_app_start.h"
  56. #include "nrf_bootloader_fw_activation.h"
  57. #include "nrf_bootloader_dfu_timers.h"
  58. #include "app_scheduler.h"
  59. static nrf_dfu_observer_t m_user_observer; //<! Observer callback set by the user.
  60. static volatile bool m_flash_write_done;
  61. #define SCHED_QUEUE_SIZE 32 /**< Maximum number of events in the scheduler queue. */
  62. #define SCHED_EVENT_DATA_SIZE NRF_DFU_SCHED_EVENT_DATA_SIZE /**< Maximum app_scheduler event size. */
  63. #if !(defined(NRF_BL_DFU_ENTER_METHOD_BUTTON) && \
  64. defined(NRF_BL_DFU_ENTER_METHOD_PINRESET) && \
  65. defined(NRF_BL_DFU_ENTER_METHOD_GPREGRET) && \
  66. defined(NRF_BL_DFU_ENTER_METHOD_BUTTONLESS))
  67. #error Configuration file is missing flags. Update sdk_config.h.
  68. #endif
  69. STATIC_ASSERT((NRF_BL_DFU_INACTIVITY_TIMEOUT_MS >= 100) || (NRF_BL_DFU_INACTIVITY_TIMEOUT_MS == 0),
  70. "NRF_BL_DFU_INACTIVITY_TIMEOUT_MS must be 100 ms or more, or 0 to indicate that it is disabled.");
  71. #if defined(NRF_LOG_BACKEND_FLASH_START_PAGE)
  72. STATIC_ASSERT(NRF_LOG_BACKEND_FLASH_START_PAGE != 0,
  73. "If nrf_log flash backend is used it cannot use space after code because it would collide with settings page.");
  74. #endif
  75. /**@brief Weak implemenation of nrf_dfu_init
  76. *
  77. * @note This function will be overridden if nrf_dfu.c is
  78. * compiled and linked with the project
  79. */
  80. #if (__LINT__ != 1)
  81. __WEAK uint32_t nrf_dfu_init(nrf_dfu_observer_t observer)
  82. {
  83. NRF_LOG_DEBUG("in weak nrf_dfu_init");
  84. return NRF_SUCCESS;
  85. }
  86. #endif
  87. /**@brief Weak implementation of nrf_dfu_init
  88. *
  89. * @note This function must be overridden in application if
  90. * user-specific initialization is needed.
  91. */
  92. __WEAK uint32_t nrf_dfu_init_user(void)
  93. {
  94. NRF_LOG_DEBUG("in weak nrf_dfu_init_user");
  95. return NRF_SUCCESS;
  96. }
  97. static void flash_write_callback(void * p_context)
  98. {
  99. UNUSED_PARAMETER(p_context);
  100. m_flash_write_done = true;
  101. }
  102. static void reset_after_flash_write(void * p_context)
  103. {
  104. UNUSED_PARAMETER(p_context);
  105. NRF_LOG_FINAL_FLUSH();
  106. #if NRF_MODULE_ENABLED(NRF_LOG_BACKEND_RTT)
  107. // To allow the buffer to be flushed by the host.
  108. nrf_delay_ms(100);
  109. #endif
  110. NVIC_SystemReset();
  111. }
  112. static void bootloader_reset(void)
  113. {
  114. NRF_LOG_DEBUG("Resetting bootloader.");
  115. m_flash_write_done = false;
  116. nrf_dfu_settings_backup(reset_after_flash_write);
  117. }
  118. static void inactivity_timeout(void)
  119. {
  120. NRF_LOG_INFO("Inactivity timeout.");
  121. bootloader_reset();
  122. }
  123. /**@brief Function for handling DFU events.
  124. */
  125. static void dfu_observer(nrf_dfu_evt_type_t evt_type)
  126. {
  127. switch (evt_type)
  128. {
  129. case NRF_DFU_EVT_DFU_STARTED:
  130. case NRF_DFU_EVT_OBJECT_RECEIVED:
  131. nrf_bootloader_dfu_inactivity_timer_restart(
  132. NRF_BOOTLOADER_MS_TO_TICKS(NRF_BL_DFU_INACTIVITY_TIMEOUT_MS),
  133. inactivity_timeout);
  134. break;
  135. case NRF_DFU_EVT_DFU_COMPLETED:
  136. case NRF_DFU_EVT_DFU_ABORTED:
  137. bootloader_reset();
  138. break;
  139. default:
  140. break;
  141. }
  142. if (m_user_observer)
  143. {
  144. m_user_observer(evt_type);
  145. }
  146. }
  147. /**@brief Function for initializing the event scheduler.
  148. */
  149. static void scheduler_init(void)
  150. {
  151. APP_SCHED_INIT(SCHED_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
  152. }
  153. /**@brief Suspend the CPU until an interrupt occurs.
  154. */
  155. static void wait_for_event(void)
  156. {
  157. #ifdef BLE_STACK_SUPPORT_REQD
  158. (void)sd_app_evt_wait();
  159. #else
  160. // Wait for an event.
  161. __WFE();
  162. // Clear the internal event register.
  163. __SEV();
  164. __WFE();
  165. #endif
  166. }
  167. /**@brief Continually sleep and process tasks whenever woken.
  168. */
  169. static void loop_forever(void)
  170. {
  171. while (true)
  172. {
  173. //feed the watchdog if enabled.
  174. nrf_bootloader_wdt_feed();
  175. app_sched_execute();
  176. if (!NRF_LOG_PROCESS())
  177. {
  178. wait_for_event();
  179. }
  180. }
  181. }
  182. /**@brief Function for initializing button used to enter DFU mode.
  183. */
  184. static void dfu_enter_button_init(void)
  185. {
  186. nrf_gpio_cfg_sense_input(NRF_BL_DFU_ENTER_METHOD_BUTTON_PIN,
  187. BUTTON_PULL,
  188. NRF_GPIO_PIN_SENSE_LOW);
  189. }
  190. static bool crc_on_valid_app_required(void)
  191. {
  192. bool ret = true;
  193. if (NRF_BL_APP_CRC_CHECK_SKIPPED_ON_SYSTEMOFF_RESET &&
  194. (nrf_power_resetreas_get() & NRF_POWER_RESETREAS_OFF_MASK))
  195. {
  196. nrf_power_resetreas_clear(NRF_POWER_RESETREAS_OFF_MASK);
  197. ret = false;
  198. }
  199. else if (NRF_BL_APP_CRC_CHECK_SKIPPED_ON_GPREGRET2 &&
  200. (nrf_power_gpregret2_get() & BOOTLOADER_DFU_SKIP_CRC))
  201. {
  202. nrf_power_gpregret2_set(nrf_power_gpregret2_get() & ~BOOTLOADER_DFU_SKIP_CRC);
  203. ret = false;
  204. }
  205. else
  206. {
  207. }
  208. return ret;
  209. }
  210. /**@brief Function for clearing all DFU enter flags that
  211. * preserve state during reset.
  212. *
  213. * @details This is used to make sure that each of these flags
  214. * is checked only once after reset.
  215. */
  216. static void dfu_enter_flags_clear(void)
  217. {
  218. if (NRF_BL_DFU_ENTER_METHOD_PINRESET &&
  219. (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk))
  220. {
  221. // Clear RESETPIN flag.
  222. NRF_POWER->RESETREAS |= POWER_RESETREAS_RESETPIN_Msk;
  223. }
  224. if (NRF_BL_DFU_ENTER_METHOD_GPREGRET &&
  225. (nrf_power_gpregret_get() & BOOTLOADER_DFU_START))
  226. {
  227. // Clear DFU mark in GPREGRET register.
  228. nrf_power_gpregret_set(nrf_power_gpregret_get() & ~BOOTLOADER_DFU_START);
  229. }
  230. if (NRF_BL_DFU_ENTER_METHOD_BUTTONLESS &&
  231. (s_dfu_settings.enter_buttonless_dfu == 1))
  232. {
  233. // Clear DFU flag in flash settings.
  234. s_dfu_settings.enter_buttonless_dfu = 0;
  235. APP_ERROR_CHECK(nrf_dfu_settings_write(NULL));
  236. }
  237. }
  238. /**@brief Function for checking whether to enter DFU mode or not.
  239. */
  240. static bool dfu_enter_check(void)
  241. {
  242. if (!nrf_dfu_app_is_valid(crc_on_valid_app_required()))
  243. {
  244. NRF_LOG_DEBUG("DFU mode because app is not valid.");
  245. return true;
  246. }
  247. if (NRF_BL_DFU_ENTER_METHOD_BUTTON && (nrf_gpio_pin_read(NRF_BL_DFU_ENTER_METHOD_BUTTON_PIN) == 0))
  248. {
  249. NRF_LOG_DEBUG("DFU mode requested via button.");
  250. return true;
  251. }
  252. if (NRF_BL_DFU_ENTER_METHOD_PINRESET && (NRF_POWER->RESETREAS & POWER_RESETREAS_RESETPIN_Msk))
  253. {
  254. NRF_LOG_DEBUG("DFU mode requested via pin-reset.");
  255. return true;
  256. }
  257. if (NRF_BL_DFU_ENTER_METHOD_GPREGRET && (nrf_power_gpregret_get() & BOOTLOADER_DFU_START))
  258. {
  259. NRF_LOG_DEBUG("DFU mode requested via GPREGRET.");
  260. return true;
  261. }
  262. if (NRF_BL_DFU_ENTER_METHOD_BUTTONLESS && (s_dfu_settings.enter_buttonless_dfu == 1))
  263. {
  264. NRF_LOG_DEBUG("DFU mode requested via bootloader settings.");
  265. return true;
  266. }
  267. return false;
  268. }
  269. ret_code_t nrf_bootloader_init(nrf_dfu_observer_t observer)
  270. {
  271. NRF_LOG_DEBUG("In nrf_bootloader_init");
  272. ret_code_t ret_val;
  273. nrf_bootloader_fw_activation_result_t activation_result;
  274. uint32_t initial_timeout;
  275. bool dfu_enter = false;
  276. m_user_observer = observer;
  277. if (NRF_BL_DFU_ENTER_METHOD_BUTTON)
  278. {
  279. dfu_enter_button_init();
  280. }
  281. ret_val = nrf_dfu_settings_init(false);
  282. if (ret_val != NRF_SUCCESS)
  283. {
  284. return NRF_ERROR_INTERNAL;
  285. }
  286. // Check if an update needs to be activated and activate it.
  287. activation_result = nrf_bootloader_fw_activate();
  288. switch (activation_result)
  289. {
  290. case ACTIVATION_NONE:
  291. initial_timeout = NRF_BOOTLOADER_MS_TO_TICKS(NRF_BL_DFU_INACTIVITY_TIMEOUT_MS);
  292. dfu_enter = dfu_enter_check();
  293. break;
  294. case ACTIVATION_SUCCESS_EXPECT_ADDITIONAL_UPDATE:
  295. initial_timeout = NRF_BOOTLOADER_MS_TO_TICKS(NRF_BL_DFU_CONTINUATION_TIMEOUT_MS);
  296. dfu_enter = true;
  297. break;
  298. case ACTIVATION_SUCCESS:
  299. bootloader_reset();
  300. NRF_LOG_ERROR("Should never come here: After bootloader_reset()");
  301. return NRF_ERROR_INTERNAL; // Should not reach this.
  302. case ACTIVATION_ERROR:
  303. default:
  304. return NRF_ERROR_INTERNAL;
  305. }
  306. if (dfu_enter)
  307. {
  308. nrf_bootloader_wdt_init();
  309. scheduler_init();
  310. // Clear all DFU stop flags.
  311. dfu_enter_flags_clear();
  312. // Call user-defined init function if implemented
  313. ret_val = nrf_dfu_init_user();
  314. if (ret_val != NRF_SUCCESS)
  315. {
  316. return NRF_ERROR_INTERNAL;
  317. }
  318. nrf_bootloader_dfu_inactivity_timer_restart(initial_timeout, inactivity_timeout);
  319. ret_val = nrf_dfu_init(dfu_observer);
  320. if (ret_val != NRF_SUCCESS)
  321. {
  322. return NRF_ERROR_INTERNAL;
  323. }
  324. NRF_LOG_DEBUG("Enter main loop");
  325. loop_forever(); // This function will never return.
  326. NRF_LOG_ERROR("Should never come here: After looping forever.");
  327. }
  328. else
  329. {
  330. // Erase additional data like peer data or advertisement name
  331. ret_val = nrf_dfu_settings_additional_erase();
  332. if (ret_val != NRF_SUCCESS)
  333. {
  334. return NRF_ERROR_INTERNAL;
  335. }
  336. m_flash_write_done = false;
  337. nrf_dfu_settings_backup(flash_write_callback);
  338. ASSERT(m_flash_write_done);
  339. nrf_bootloader_app_start();
  340. NRF_LOG_ERROR("Should never come here: After nrf_bootloader_app_start()");
  341. }
  342. // Should not be reached.
  343. return NRF_ERROR_INTERNAL;
  344. }