bootloader.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 "bootloader.h"
  13. #include <string.h>
  14. #include "bootloader_types.h"
  15. #include "bootloader_util.h"
  16. #include "dfu.h"
  17. #include "dfu_transport.h"
  18. #include "nrf.h"
  19. #include "app_error.h"
  20. #include "nrf_sdm.h"
  21. #include "nrf_soc.h"
  22. #include "nordic_common.h"
  23. #include "pstorage.h"
  24. #include "app_scheduler.h"
  25. #include "nrf_delay.h"
  26. #include "debug_pin.h"
  27. #define IRQ_ENABLED 0x01 /**< Field identifying if an interrupt is enabled. */
  28. #define MAX_NUMBER_INTERRUPTS 32 /**< Maximum number of interrupts available. */
  29. /*
  30. * bootloader_settings space is subdivided into 8 128byte sized blocks to properly use pstorage data abstraction scheme
  31. * with the intent to use the last block located, specifically at 0x3FF80, for ant_boot_settings matters.
  32. */
  33. #define BOOTLOADER_SETTINGS_FLASH_BLOCK_SIZE 128
  34. #define BOOTLOADER_SETTINGS_FLASH_BLOCK_COUNT 8 /* Total of 1024 */
  35. typedef enum
  36. {
  37. BOOTLOADER_UPDATING,
  38. BOOTLOADER_SETTINGS_SAVING,
  39. BOOTLOADER_COMPLETE,
  40. BOOTLOADER_TIMEOUT,
  41. BOOTLOADER_RESET,
  42. } bootloader_status_t;
  43. static pstorage_handle_t m_bootsettings_handle; /**< Pstorage handle to use for registration and identifying the bootloader module on subsequent calls to the pstorage module for load and store of bootloader setting in flash. */
  44. static bootloader_status_t m_update_status; /**< Current update status for the bootloader module to ensure correct behaviour when updating settings and when update completes. */
  45. static uint8_t m_delay_applied = false;/**< Delay has been applied before the initial access to flash > */
  46. /* NOTE: Temporary use of this page until the bootloader_settings slotting is implemented */
  47. #define BOOT_SETTINGS_PEND_ADDRESS (BOOTLOADER_MBR_RETAINING_PAGE_ADDRESS)
  48. #define BOOT_SETTINGS_PEND_VALUE 0xFFFFFFFE
  49. uint8_t m_boot_settings_pend[CODE_PAGE_SIZE] __attribute__((at(BOOT_SETTINGS_PEND_ADDRESS))) __attribute__((used));
  50. uint32_t blocking_flash_page_erase(uint32_t page_number)
  51. {
  52. uint32_t err_code;
  53. do{
  54. err_code = sd_flash_page_erase(page_number);
  55. } while(err_code == NRF_ERROR_BUSY);
  56. return err_code;
  57. }
  58. uint32_t blocking_flash_word_write(uint32_t * const p_dst, uint32_t data)
  59. {
  60. uint32_t err_code;
  61. do{
  62. err_code = sd_flash_write(p_dst, &data, 1);
  63. } while(err_code == NRF_ERROR_BUSY);
  64. return err_code;
  65. }
  66. static void pstorage_callback_handler(pstorage_handle_t * handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len)
  67. {
  68. // If we are in BOOTLOADER_SETTINGS_SAVING state and we receive an PSTORAGE_STORE_OP_CODE
  69. // response then settings has been saved and update has completed.
  70. if ((m_update_status == BOOTLOADER_SETTINGS_SAVING) && (op_code == PSTORAGE_STORE_OP_CODE) && (handle->block_id == BOOTLOADER_SETTINGS_ADDRESS)) //
  71. {
  72. m_update_status = BOOTLOADER_COMPLETE;
  73. /*Clears bootloader_settings critical flag*/
  74. if (*((uint32_t *)BOOT_SETTINGS_PEND_ADDRESS) == BOOT_SETTINGS_PEND_VALUE)
  75. {
  76. uint32_t err_code = blocking_flash_page_erase(BOOT_SETTINGS_PEND_ADDRESS/CODE_PAGE_SIZE);
  77. APP_ERROR_CHECK(err_code);
  78. }
  79. }
  80. APP_ERROR_CHECK(result);
  81. }
  82. /**@brief Function for waiting for events.
  83. *
  84. * @details This function will place the chip in low power mode while waiting for events from
  85. * the S110 SoftDevice or other peripherals. When interrupted by an event, it will call the
  86. * @ref app_sched_execute function to process the received event. This function will return
  87. * when the final state of the firmware update is reached OR when a tear down is in
  88. * progress.
  89. */
  90. static void wait_for_events(void)
  91. {
  92. for (;;)
  93. {
  94. // Wait in low power state for any events.
  95. uint32_t err_code = sd_app_evt_wait();
  96. APP_ERROR_CHECK(err_code);
  97. // Event received. Process it from the scheduler.
  98. app_sched_execute();
  99. if ((m_update_status == BOOTLOADER_COMPLETE) ||
  100. (m_update_status == BOOTLOADER_TIMEOUT) ||
  101. (m_update_status == BOOTLOADER_RESET))
  102. {
  103. // When update has completed or a timeout/reset occured we will return.
  104. return;
  105. }
  106. }
  107. }
  108. bool bootloader_app_is_valid(uint32_t app_addr)
  109. {
  110. const bootloader_settings_t * p_bootloader_settings;
  111. // Critical flag was not cleared.
  112. if (*((uint32_t *)BOOT_SETTINGS_PEND_ADDRESS) == BOOT_SETTINGS_PEND_VALUE)
  113. {
  114. return false;
  115. }
  116. // There exists an application in CODE region 1.
  117. if (*((uint32_t *) app_addr) == EMPTY_FLASH_MASK)
  118. {
  119. return false;
  120. }
  121. bootloader_util_settings_get(&p_bootloader_settings);
  122. // The application in CODE region 1 was not flagged as invalid.
  123. if (p_bootloader_settings->valid_app == BOOTLOADER_SETTINGS_INVALID_APPLICATION)
  124. {
  125. return false;
  126. }
  127. return true;
  128. }
  129. static void bootloader_settings_save(bootloader_settings_t * p_settings)
  130. {
  131. //TODO. This is temporary
  132. static uint8_t ant_boot_settings[BOOTLOADER_SETTINGS_FLASH_BLOCK_SIZE];
  133. static pstorage_handle_t ant_boot_settings_handle;
  134. /* Backing up ant_boot_settings. */
  135. ant_boot_settings_handle.module_id = m_bootsettings_handle.module_id;
  136. ant_boot_settings_handle.block_id = m_bootsettings_handle.block_id + (BOOTLOADER_SETTINGS_FLASH_BLOCK_SIZE * (BOOTLOADER_SETTINGS_FLASH_BLOCK_COUNT - 1));
  137. memcpy(ant_boot_settings, (uint8_t*)ant_boot_settings_handle.block_id, BOOTLOADER_SETTINGS_FLASH_BLOCK_SIZE);
  138. /* NOTE: Must erase the whole module to prevent pstorage block erasing which uses swap space*/
  139. uint32_t err_code = pstorage_clear(&m_bootsettings_handle,
  140. BOOTLOADER_SETTINGS_FLASH_BLOCK_SIZE * BOOTLOADER_SETTINGS_FLASH_BLOCK_COUNT);
  141. APP_ERROR_CHECK(err_code);
  142. /* Write back ant_boot_settings */
  143. err_code = pstorage_store(&ant_boot_settings_handle,
  144. ant_boot_settings,
  145. BOOTLOADER_SETTINGS_FLASH_BLOCK_SIZE,
  146. 0);
  147. APP_ERROR_CHECK(err_code);
  148. err_code = pstorage_store(&m_bootsettings_handle,
  149. (uint8_t *)p_settings,
  150. sizeof(bootloader_settings_t),
  151. 0);
  152. APP_ERROR_CHECK(err_code);
  153. }
  154. void bootloader_dfu_update_process(dfu_update_status_t update_status)
  155. {
  156. static bootloader_settings_t settings;
  157. const bootloader_settings_t * p_bootloader_settings;
  158. uint32_t err_code;
  159. bootloader_util_settings_get(&p_bootloader_settings); /* Extract current values of the bootloader_settings*/
  160. memcpy(&settings, p_bootloader_settings, sizeof(bootloader_settings_t)); /* Copy over to local bootloader_settings*/
  161. if (update_status.status_code == DFU_UPDATE_NEW_IMAGES)
  162. {
  163. if (update_status.sd_image_size != NEW_IMAGE_SIZE_EMPTY)
  164. {
  165. settings.sd_image.st.size = update_status.sd_image_size;
  166. settings.sd_image.st.bank = update_status.bank_used;
  167. }
  168. else
  169. {
  170. settings.sd_image.st.size = NEW_IMAGE_SIZE_EMPTY;
  171. }
  172. if (update_status.bl_image_size != NEW_IMAGE_SIZE_EMPTY)
  173. {
  174. settings.bl_image.st.size = update_status.bl_image_size;
  175. settings.bl_image.st.bank = update_status.bank_used;
  176. }
  177. else
  178. {
  179. settings.bl_image.st.size = NEW_IMAGE_SIZE_EMPTY;
  180. }
  181. if (update_status.ap_image_size != NEW_IMAGE_SIZE_EMPTY)
  182. {
  183. settings.ap_image.st.size = update_status.ap_image_size;
  184. settings.ap_image.st.bank = update_status.bank_used;
  185. }
  186. else
  187. {
  188. settings.ap_image.st.size = NEW_IMAGE_SIZE_EMPTY;
  189. }
  190. settings.src_image_address = update_status.src_image_address;
  191. m_update_status = BOOTLOADER_SETTINGS_SAVING;
  192. /*Clears bootloader_settings critical flag - in case it was used by MBR*/
  193. if (*((uint32_t *)BOOT_SETTINGS_PEND_ADDRESS) != 0xFFFFFFFF)
  194. {
  195. err_code = blocking_flash_page_erase(BOOT_SETTINGS_PEND_ADDRESS/CODE_PAGE_SIZE);
  196. APP_ERROR_CHECK(err_code);
  197. }
  198. // TEMPORARY: This serves as a critical flag for the bootloader_settings updating.
  199. err_code = blocking_flash_word_write((uint32_t *)BOOT_SETTINGS_PEND_ADDRESS,
  200. BOOT_SETTINGS_PEND_VALUE);
  201. APP_ERROR_CHECK(err_code);
  202. bootloader_settings_save(&settings);
  203. }
  204. else if (update_status.status_code == DFU_UPDATE_AP_SWAPPED)
  205. {
  206. settings.ap_image.st.bank = NEW_IMAGE_BANK_DONE;
  207. settings.ap_image.st.size = p_bootloader_settings->ap_image.st.size;
  208. settings.valid_app = BOOTLOADER_SETTINGS_VALID_APPLICATION;
  209. m_update_status = BOOTLOADER_SETTINGS_SAVING;
  210. /*Clears bootloader_settings critical flag - in case it was used by MBR*/
  211. if (*((uint32_t *)BOOT_SETTINGS_PEND_ADDRESS) != 0xFFFFFFFF)
  212. {
  213. err_code = blocking_flash_page_erase(BOOT_SETTINGS_PEND_ADDRESS/CODE_PAGE_SIZE);
  214. APP_ERROR_CHECK(err_code);
  215. }
  216. // TEMPORARY: This serves as a critical flag for the bootloader_settings updating.
  217. err_code = blocking_flash_word_write((uint32_t *)BOOT_SETTINGS_PEND_ADDRESS,
  218. BOOT_SETTINGS_PEND_VALUE);
  219. APP_ERROR_CHECK(err_code);
  220. bootloader_settings_save(&settings);
  221. }
  222. else if (update_status.status_code == DFU_UPDATE_AP_INVALIDATED)
  223. {
  224. if (p_bootloader_settings->valid_app != BOOTLOADER_SETTINGS_INVALID_APPLICATION)
  225. {
  226. settings.valid_app = BOOTLOADER_SETTINGS_INVALID_APPLICATION;
  227. m_update_status = BOOTLOADER_UPDATING;
  228. bootloader_settings_save(&settings);
  229. }
  230. }
  231. else if (update_status.status_code == DFU_TIMEOUT)
  232. {
  233. // Timeout has occurred. Close the connection with the DFU Controller.
  234. // dfu_transport_close();
  235. m_update_status = BOOTLOADER_TIMEOUT;
  236. }
  237. else if (update_status.status_code == DFU_RESET)
  238. {
  239. // Timeout has occurred. Close the connection with the DFU Controller.
  240. // dfu_transport_close();
  241. m_update_status = BOOTLOADER_RESET;
  242. }
  243. else
  244. {
  245. // No implementation needed.
  246. }
  247. }
  248. uint32_t bootloader_init(void)
  249. {
  250. uint32_t err_code;
  251. pstorage_module_param_t storage_params;
  252. storage_params.cb = pstorage_callback_handler;
  253. storage_params.block_size = BOOTLOADER_SETTINGS_FLASH_BLOCK_SIZE;
  254. storage_params.block_count = BOOTLOADER_SETTINGS_FLASH_BLOCK_COUNT;
  255. err_code = pstorage_init();
  256. if (err_code != NRF_SUCCESS)
  257. {
  258. return err_code;
  259. }
  260. err_code = pstorage_register(&storage_params, &m_bootsettings_handle);
  261. m_delay_applied = false;
  262. return err_code;
  263. }
  264. uint32_t bootloader_dfu_start(void)
  265. {
  266. uint32_t err_code = NRF_SUCCESS;
  267. err_code = dfu_init();
  268. if (err_code != NRF_SUCCESS)
  269. {
  270. return err_code;
  271. }
  272. err_code = dfu_transport_update_start();
  273. wait_for_events();
  274. err_code = dfu_transport_close();
  275. return err_code;
  276. }
  277. /**@brief Function for disabling all interrupts before jumping from bootloader to application.
  278. */
  279. static void interrupts_disable(void)
  280. {
  281. uint32_t interrupt_setting_mask;
  282. uint8_t irq;
  283. // We start the loop from first interrupt, i.e. interrupt 0.
  284. irq = 0;
  285. // Fetch the current interrupt settings.
  286. interrupt_setting_mask = NVIC->ISER[0];
  287. for (; irq < MAX_NUMBER_INTERRUPTS; irq++)
  288. {
  289. if (interrupt_setting_mask & (IRQ_ENABLED << irq))
  290. {
  291. // The interrupt was enabled, and hence disable it.
  292. NVIC_DisableIRQ((IRQn_Type) irq);
  293. }
  294. }
  295. }
  296. // Ensure that flash operations are not executed within the first 100 ms seconds to allow
  297. // a debugger to be attached
  298. static void debugger_delay (void)
  299. {
  300. if (m_delay_applied == false)
  301. {
  302. m_delay_applied = true;
  303. nrf_delay_ms(100);
  304. }
  305. }
  306. void bootloader_app_start(uint32_t app_addr)
  307. {
  308. // If the applications CRC has been checked and passed, the magic number will be written and we
  309. // can start the application safely.
  310. uint32_t err_code = sd_softdevice_disable();
  311. APP_ERROR_CHECK(err_code);
  312. interrupts_disable();
  313. #if defined (S210_V3_STACK)
  314. err_code = sd_softdevice_forward_to_application();
  315. #else
  316. err_code = sd_softdevice_vector_table_base_set(CODE_REGION_1_START);
  317. #endif
  318. APP_ERROR_CHECK(err_code);
  319. bootloader_util_app_start(CODE_REGION_1_START);
  320. }
  321. #if !defined (S210_V3_STACK)
  322. uint32_t temp_value;
  323. uint32_t bootloader_dfu_sd_update_continue()
  324. {
  325. uint32_t err_code = NRF_SUCCESS;
  326. const bootloader_settings_t * p_bootloader_settings;
  327. bootloader_util_settings_get(&p_bootloader_settings);
  328. /* Ignore update attempts on invalid src_image_address */
  329. if( (p_bootloader_settings->src_image_address == SRC_IMAGE_ADDRESS_EMPTY) ||
  330. (p_bootloader_settings->src_image_address == SRC_IMAGE_ADDRESS_INVALID))
  331. {
  332. return NRF_SUCCESS;
  333. }
  334. if( (p_bootloader_settings->sd_image.st.bank == NEW_IMAGE_BANK_0) ||
  335. (p_bootloader_settings->sd_image.st.bank == NEW_IMAGE_BANK_1))
  336. {
  337. debugger_delay();
  338. err_code = dfu_sd_image_swap();
  339. if (dfu_sd_image_validate() == NRF_SUCCESS)
  340. {
  341. /* This is a manual write to flash, non-softdevice managed */
  342. uint32_t address = (uint32_t)p_bootloader_settings + BOOTLOADER_SETTINGS_SD_IMAGE_SIZE_ADR_OFFSET;
  343. temp_value = p_bootloader_settings->sd_image.all & 0x3FFFFFFF; // clears image bank bits.
  344. err_code = blocking_flash_word_write((uint32_t *)address, temp_value);
  345. //TODO need to catch verification error
  346. }
  347. }
  348. return err_code;
  349. }
  350. uint32_t bootloader_dfu_bl_update_continue(void)
  351. {
  352. uint32_t err_code = NRF_SUCCESS;
  353. const bootloader_settings_t * p_bootloader_settings;
  354. bootloader_util_settings_get(&p_bootloader_settings);
  355. /* Ignore update attempts on invalid src_image_address */
  356. if( (p_bootloader_settings->src_image_address == SRC_IMAGE_ADDRESS_EMPTY) ||
  357. (p_bootloader_settings->src_image_address == SRC_IMAGE_ADDRESS_INVALID))
  358. {
  359. return NRF_SUCCESS;
  360. }
  361. if( (p_bootloader_settings->bl_image.st.bank == NEW_IMAGE_BANK_0) ||
  362. (p_bootloader_settings->bl_image.st.bank == NEW_IMAGE_BANK_1))
  363. {
  364. debugger_delay();
  365. if (dfu_bl_image_validate() != NRF_SUCCESS)
  366. {
  367. err_code = dfu_bl_image_swap(); // reset is built in to the mbr bootloader swap
  368. }
  369. else
  370. {
  371. /* This is a manual write to flash, non-softdevice managed */
  372. uint32_t address = (uint32_t)p_bootloader_settings + BOOTLOADER_SETTINGS_BL_IMAGE_SIZE_ADR_OFFSET;
  373. uint32_t value = p_bootloader_settings->bl_image.all & 0x3FFFFFFF; // clears image bank bits.
  374. err_code = blocking_flash_word_write((uint32_t *)address, value);
  375. //TODO need to catch verification error
  376. }
  377. }
  378. return err_code;
  379. }
  380. #endif // !S210_V3_STACK
  381. uint32_t bootloader_dfu_ap_update_continue(void)
  382. {
  383. uint32_t err_code = NRF_SUCCESS;
  384. const bootloader_settings_t * p_bootloader_settings;
  385. bootloader_util_settings_get(&p_bootloader_settings);
  386. /* Ignore update attempts on invalid src_image_address */
  387. if( (p_bootloader_settings->src_image_address == SRC_IMAGE_ADDRESS_EMPTY) ||
  388. (p_bootloader_settings->src_image_address == SRC_IMAGE_ADDRESS_INVALID))
  389. {
  390. return NRF_SUCCESS;
  391. }
  392. if( (p_bootloader_settings->ap_image.st.bank == NEW_IMAGE_BANK_0) ||
  393. (p_bootloader_settings->ap_image.st.bank == NEW_IMAGE_BANK_1))
  394. {
  395. /* If updating application only, we can start the copy right now*/
  396. if ((p_bootloader_settings->sd_image.st.size == NEW_IMAGE_SIZE_EMPTY) &&
  397. (p_bootloader_settings->bl_image.st.size == NEW_IMAGE_SIZE_EMPTY))
  398. {
  399. err_code = dfu_ap_image_swap();
  400. dfu_update_status_t update_status = {DFU_UPDATE_AP_SWAPPED, };
  401. bootloader_dfu_update_process(update_status);
  402. wait_for_events();
  403. }
  404. }
  405. return err_code;
  406. }