nrf_dfu_settings.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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_dfu_settings.h"
  41. #include <stddef.h>
  42. #include <string.h>
  43. #include "nrf_dfu_flash.h"
  44. #include "nrf_soc.h"
  45. #include "crc32.h"
  46. #include "nrf_nvmc.h"
  47. #include "sdk_config.h"
  48. #define DFU_SETTINGS_VERSION_OFFSET (offsetof(nrf_dfu_settings_t, settings_version)) //<! Offset in the settings struct where the settings version is located.
  49. #define DFU_SETTINGS_INIT_COMMAND_OFFSET (offsetof(nrf_dfu_settings_t, init_command)) //<! Offset in the settings struct where the InitCommand is located.
  50. #define DFU_SETTINGS_BOOT_VALIDATION_OFFSET (offsetof(nrf_dfu_settings_t, boot_validation_crc)) //<! Offset in the settings struct where the boot validation info is located.
  51. #define DFU_SETTINGS_BOOT_VALIDATION_SIZE ((3 * sizeof(boot_validation_t)) + 4)
  52. #define DFU_SETTINGS_BOND_DATA_OFFSET_V1 (offsetof(nrf_dfu_settings_t, init_command) + INIT_COMMAND_MAX_SIZE_v1) //<! Offset in the settings struct where the bond data was located in settings version 1.
  53. #define DFU_SETTINGS_ADV_NAME_OFFSET_V1 (offsetof(nrf_dfu_settings_t, init_command) + INIT_COMMAND_MAX_SIZE_v1 + NRF_DFU_PEER_DATA_LEN) //<! Offset in the settings struct where the bond data was located in settings version 1.
  54. #define NRF_LOG_MODULE_NAME nrf_dfu_settings
  55. #include "nrf_log.h"
  56. NRF_LOG_MODULE_REGISTER();
  57. /**@brief This variable reserves a page in flash for bootloader settings
  58. * to ensure the linker doesn't place any code or variables at this location.
  59. */
  60. #if defined (__CC_ARM )
  61. uint8_t m_dfu_settings_buffer[BOOTLOADER_SETTINGS_PAGE_SIZE]
  62. __attribute__((at(BOOTLOADER_SETTINGS_ADDRESS)))
  63. __attribute__((used));
  64. #elif defined ( __GNUC__ ) || defined ( __SES_ARM )
  65. uint8_t m_dfu_settings_buffer[BOOTLOADER_SETTINGS_PAGE_SIZE]
  66. __attribute__((section(".bootloader_settings_page")))
  67. __attribute__((used));
  68. #elif defined ( __ICCARM__ )
  69. __no_init __root uint8_t m_dfu_settings_buffer[BOOTLOADER_SETTINGS_PAGE_SIZE]
  70. @ BOOTLOADER_SETTINGS_ADDRESS;
  71. #else
  72. #error Not a valid compiler/linker for m_dfu_settings placement.
  73. #endif // Compiler specific
  74. #if defined(NRF52_SERIES)
  75. /**@brief This variable reserves a page in flash for MBR parameters
  76. * to ensure the linker doesn't place any code or variables at this location.
  77. */
  78. #if defined ( __CC_ARM )
  79. uint8_t m_mbr_params_page[NRF_MBR_PARAMS_PAGE_SIZE]
  80. __attribute__((at(NRF_MBR_PARAMS_PAGE_ADDRESS)))
  81. __attribute__((used));
  82. #elif defined ( __GNUC__ ) || defined ( __SES_ARM )
  83. uint8_t m_mbr_params_page[NRF_MBR_PARAMS_PAGE_SIZE]
  84. __attribute__((section(".mbr_params_page")))
  85. __attribute__((used));
  86. #elif defined ( __ICCARM__ )
  87. __no_init uint8_t m_mbr_params_page[NRF_MBR_PARAMS_PAGE_SIZE]
  88. @ NRF_MBR_PARAMS_PAGE_ADDRESS;
  89. #else
  90. #error Not a valid compiler/linker for m_mbr_params_page placement.
  91. #endif // Compiler specific
  92. uint8_t * mp_dfu_settings_backup_buffer = &m_mbr_params_page[0];
  93. #ifndef NRF_DFU_IN_APP
  94. #define NRF_DFU_IN_APP 0
  95. #endif
  96. #define UICR_PARAM_PAGE_ADDR 0x10001018
  97. #if !defined(BL_SETTINGS_ACCESS_ONLY) && !NRF_DFU_IN_APP
  98. /**@brief This variable has the linker write the MBR parameters page address to the
  99. * UICR register. This value will be written in the HEX file and thus to the
  100. * UICR when the bootloader is flashed into the chip.
  101. */
  102. #if defined ( __CC_ARM )
  103. uint32_t const m_uicr_mbr_params_page_address
  104. __attribute__((at(UICR_PARAM_PAGE_ADDR))) = NRF_MBR_PARAMS_PAGE_ADDRESS;
  105. #elif defined ( __GNUC__ ) || defined ( __SES_ARM )
  106. uint32_t const m_uicr_mbr_params_page_address
  107. __attribute__ ((section(".uicr_mbr_params_page")))
  108. __attribute__ ((used)) = NRF_MBR_PARAMS_PAGE_ADDRESS;
  109. #elif defined ( __ICCARM__ )
  110. __root uint32_t const m_uicr_mbr_params_page_address
  111. @ UICR_PARAM_PAGE_ADDR = NRF_MBR_PARAMS_PAGE_ADDRESS;
  112. #else
  113. #error Not a valid compiler/linker for m_mbr_params_page placement.
  114. #endif // Compiler specific
  115. #endif // #ifndef BL_SETTINGS_ACCESS_ONLY
  116. #endif // #if defined( NRF52_SERIES )
  117. nrf_dfu_settings_t s_dfu_settings;
  118. static uint32_t settings_crc_get(nrf_dfu_settings_t const * p_settings)
  119. {
  120. ASSERT(offsetof(nrf_dfu_settings_t, crc) == 0);
  121. // The crc is calculated from the s_dfu_settings struct, except the crc itself, the init command, bond data, and boot validation.
  122. return crc32_compute((uint8_t*)(p_settings) + 4, DFU_SETTINGS_INIT_COMMAND_OFFSET - 4, NULL);
  123. }
  124. static bool crc_ok(nrf_dfu_settings_t const * p_settings)
  125. {
  126. if (p_settings->crc != 0xFFFFFFFF)
  127. {
  128. // CRC is set. Content must be valid
  129. uint32_t crc = settings_crc_get(p_settings);
  130. if (crc == p_settings->crc)
  131. {
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. static uint32_t boot_validation_crc(nrf_dfu_settings_t const * p_settings)
  138. {
  139. return crc32_compute((const uint8_t *)&p_settings->boot_validation_softdevice,
  140. DFU_SETTINGS_BOOT_VALIDATION_SIZE - 4,
  141. NULL);
  142. }
  143. static bool boot_validation_crc_ok(nrf_dfu_settings_t const * p_settings)
  144. {
  145. return (boot_validation_crc(p_settings) == p_settings->boot_validation_crc);
  146. }
  147. static bool settings_crc_ok(void)
  148. {
  149. nrf_dfu_settings_t const * p_settings = (nrf_dfu_settings_t const *)m_dfu_settings_buffer;
  150. return crc_ok(p_settings);
  151. }
  152. static bool settings_backup_crc_ok(void)
  153. {
  154. nrf_dfu_settings_t const * p_settings = (nrf_dfu_settings_t const *)mp_dfu_settings_backup_buffer;
  155. return crc_ok(p_settings) && ((p_settings->settings_version == 1) || boot_validation_crc_ok(p_settings));
  156. }
  157. #define REGION_COPY_BY_MEMBER(start_member, end_member, p_dst_addr) \
  158. memcpy(p_dst_addr + offsetof(nrf_dfu_settings_t, start_member), \
  159. mp_dfu_settings_backup_buffer + offsetof(nrf_dfu_settings_t, start_member), \
  160. offsetof(nrf_dfu_settings_t, end_member) - offsetof(nrf_dfu_settings_t, start_member))
  161. static void settings_forbidden_parts_copy_from_backup(uint8_t * p_dst_addr)
  162. {
  163. #if NRF_DFU_IN_APP || NRF_BL_DFU_ALLOW_UPDATE_FROM_APP
  164. REGION_COPY_BY_MEMBER(settings_version, bank_current, p_dst_addr);
  165. REGION_COPY_BY_MEMBER(bank_0, write_offset, p_dst_addr);
  166. REGION_COPY_BY_MEMBER(sd_size, progress, p_dst_addr);
  167. REGION_COPY_BY_MEMBER(boot_validation_crc, peer_data, p_dst_addr);
  168. #else
  169. REGION_COPY_BY_MEMBER(settings_version, enter_buttonless_dfu, p_dst_addr);
  170. REGION_COPY_BY_MEMBER(init_command, peer_data, p_dst_addr);
  171. #endif
  172. }
  173. void nrf_dfu_settings_reinit(void)
  174. {
  175. bool settings_valid = settings_crc_ok();
  176. bool settings_backup_valid = settings_backup_crc_ok();
  177. if (settings_valid)
  178. {
  179. NRF_LOG_DEBUG("Using settings page.");
  180. memcpy(&s_dfu_settings, m_dfu_settings_buffer, sizeof(nrf_dfu_settings_t));
  181. if (settings_backup_valid)
  182. {
  183. NRF_LOG_DEBUG("Copying forbidden parts from backup page.");
  184. settings_forbidden_parts_copy_from_backup((uint8_t *)&s_dfu_settings);
  185. }
  186. }
  187. else if (settings_backup_valid)
  188. {
  189. NRF_LOG_INFO("Restoring settings from backup since the settings page contents are "
  190. "invalid (CRC error).");
  191. memcpy(&s_dfu_settings,
  192. mp_dfu_settings_backup_buffer,
  193. sizeof(nrf_dfu_settings_t));
  194. }
  195. else
  196. {
  197. NRF_LOG_WARNING("Resetting bootloader settings since neither the settings page nor the "
  198. "backup are valid (CRC error).");
  199. memset(&s_dfu_settings, 0x00, sizeof(nrf_dfu_settings_t));
  200. s_dfu_settings.settings_version = NRF_DFU_SETTINGS_VERSION;
  201. }
  202. if (NRF_DFU_SETTINGS_COMPATIBILITY_MODE && !NRF_DFU_IN_APP && (s_dfu_settings.settings_version == 1))
  203. {
  204. NRF_LOG_INFO("Old settings page detected. Upgrading info.");
  205. // Old version. Translate.
  206. memcpy(&s_dfu_settings.peer_data, (uint8_t *)&s_dfu_settings + DFU_SETTINGS_BOND_DATA_OFFSET_V1, NRF_DFU_PEER_DATA_LEN);
  207. memcpy(&s_dfu_settings.adv_name, (uint8_t *)&s_dfu_settings + DFU_SETTINGS_ADV_NAME_OFFSET_V1, NRF_DFU_ADV_NAME_LEN);
  208. // Initialize with defaults.
  209. s_dfu_settings.boot_validation_softdevice.type = NO_VALIDATION;
  210. s_dfu_settings.boot_validation_app.type = VALIDATE_CRC;
  211. s_dfu_settings.boot_validation_bootloader.type = NO_VALIDATION;
  212. memcpy(s_dfu_settings.boot_validation_app.bytes, &s_dfu_settings.bank_0.image_crc, sizeof(uint32_t));
  213. s_dfu_settings.settings_version = NRF_DFU_SETTINGS_VERSION;
  214. }
  215. return;
  216. }
  217. ret_code_t nrf_dfu_settings_init(bool sd_irq_initialized)
  218. {
  219. NRF_LOG_DEBUG("Calling nrf_dfu_settings_init()...");
  220. ret_code_t err_code = nrf_dfu_flash_init(sd_irq_initialized);
  221. if (err_code != NRF_SUCCESS)
  222. {
  223. NRF_LOG_ERROR("nrf_dfu_flash_init() failed with error: %x", err_code);
  224. return NRF_ERROR_INTERNAL;
  225. }
  226. nrf_dfu_settings_reinit();
  227. err_code = nrf_dfu_settings_write_and_backup(NULL);
  228. if (err_code != NRF_SUCCESS)
  229. {
  230. NRF_LOG_ERROR("nrf_dfu_settings_write_and_backup() failed with error: %x", err_code);
  231. return NRF_ERROR_INTERNAL;
  232. }
  233. return NRF_SUCCESS;
  234. }
  235. static bool settings_forbidden_parts_equal_to_backup(uint8_t * p_compare_addr)
  236. {
  237. nrf_dfu_settings_t temp_settings;
  238. memcpy(&temp_settings, p_compare_addr, sizeof(nrf_dfu_settings_t));
  239. settings_forbidden_parts_copy_from_backup((uint8_t *)&temp_settings);
  240. return memcmp(&temp_settings, p_compare_addr, sizeof(nrf_dfu_settings_t)) == 0;
  241. }
  242. static ret_code_t settings_write(void * p_dst,
  243. void const * p_src,
  244. nrf_dfu_flash_callback_t callback,
  245. nrf_dfu_settings_t * p_dfu_settings_buffer)
  246. {
  247. ret_code_t err_code;
  248. if (memcmp(p_dst, p_src, sizeof(nrf_dfu_settings_t)) == 0)
  249. {
  250. NRF_LOG_DEBUG("Destination settings are identical to source, write not needed. Skipping.");
  251. if (callback != NULL)
  252. {
  253. callback(NULL);
  254. }
  255. return NRF_SUCCESS;
  256. }
  257. if (NRF_DFU_IN_APP && !settings_forbidden_parts_equal_to_backup((uint8_t *)&s_dfu_settings))
  258. {
  259. NRF_LOG_WARNING("Settings write aborted since it tries writing to forbidden settings.");
  260. return NRF_ERROR_FORBIDDEN;
  261. }
  262. NRF_LOG_DEBUG("Writing settings...");
  263. NRF_LOG_DEBUG("Erasing old settings at: 0x%08x", p_dst);
  264. // Not setting the callback function because ERASE is required before STORE
  265. // Only report completion on successful STORE.
  266. err_code = nrf_dfu_flash_erase((uint32_t)p_dst, 1, NULL);
  267. if (err_code != NRF_SUCCESS)
  268. {
  269. NRF_LOG_ERROR("Could not erase the settings page!");
  270. return NRF_ERROR_INTERNAL;
  271. }
  272. ASSERT(p_dfu_settings_buffer != NULL);
  273. memcpy(p_dfu_settings_buffer, p_src, sizeof(nrf_dfu_settings_t));
  274. err_code = nrf_dfu_flash_store((uint32_t)p_dst,
  275. p_dfu_settings_buffer,
  276. sizeof(nrf_dfu_settings_t),
  277. callback);
  278. if (err_code != NRF_SUCCESS)
  279. {
  280. NRF_LOG_ERROR("Could not write the DFU settings page!");
  281. return NRF_ERROR_INTERNAL;
  282. }
  283. return NRF_SUCCESS;
  284. }
  285. ret_code_t nrf_dfu_settings_write(nrf_dfu_flash_callback_t callback)
  286. {
  287. static nrf_dfu_settings_t dfu_settings_buffer;
  288. s_dfu_settings.crc = settings_crc_get(&s_dfu_settings);
  289. s_dfu_settings.boot_validation_crc = boot_validation_crc(&s_dfu_settings);
  290. return settings_write(m_dfu_settings_buffer,
  291. &s_dfu_settings,
  292. callback,
  293. &dfu_settings_buffer);
  294. }
  295. void settings_backup(nrf_dfu_flash_callback_t callback, void * p_src)
  296. {
  297. #if NRF_DFU_IN_APP
  298. NRF_LOG_INFO("Settings backup not available from app.");
  299. #else
  300. static nrf_dfu_settings_t dfu_settings_buffer;
  301. NRF_LOG_INFO("Backing up settings page to address 0x%x.", mp_dfu_settings_backup_buffer);
  302. ASSERT(crc_ok(p_src));
  303. ret_code_t err_code = settings_write(mp_dfu_settings_backup_buffer,
  304. p_src,
  305. callback,
  306. &dfu_settings_buffer);
  307. if (err_code != NRF_SUCCESS)
  308. {
  309. NRF_LOG_ERROR("Could not perform backup of bootloader settings! Error: 0x%x", err_code);
  310. }
  311. #endif
  312. }
  313. void nrf_dfu_settings_backup(nrf_dfu_flash_callback_t callback)
  314. {
  315. settings_backup(callback, m_dfu_settings_buffer);
  316. }
  317. ret_code_t nrf_dfu_settings_write_and_backup(nrf_dfu_flash_callback_t callback)
  318. {
  319. #if NRF_DFU_IN_APP
  320. ret_code_t err_code = nrf_dfu_settings_write(callback);
  321. #else
  322. ret_code_t err_code = nrf_dfu_settings_write(NULL);
  323. if (err_code == NRF_SUCCESS)
  324. {
  325. settings_backup(callback, &s_dfu_settings);
  326. }
  327. #endif
  328. return err_code;
  329. }
  330. __WEAK ret_code_t nrf_dfu_settings_additional_erase(void)
  331. {
  332. NRF_LOG_WARNING("No additional data erased");
  333. return NRF_SUCCESS;
  334. }
  335. void nrf_dfu_settings_progress_reset(void)
  336. {
  337. memset(s_dfu_settings.init_command, 0xFF, INIT_COMMAND_MAX_SIZE); // Remove the last init command
  338. memset(&s_dfu_settings.progress, 0, sizeof(dfu_progress_t));
  339. s_dfu_settings.write_offset = 0;
  340. }