nrf_dfu_settings.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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_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. #if !defined(BL_SETTINGS_ACCESS_ONLY) && !NRF_DFU_IN_APP
  97. /**@brief This variable has the linker write the MBR parameters page address to the
  98. * UICR register. This value will be written in the HEX file and thus to the
  99. * UICR when the bootloader is flashed into the chip.
  100. */
  101. #if defined ( __CC_ARM )
  102. uint32_t const m_uicr_mbr_params_page_address
  103. __attribute__((at(NRF_UICR_MBR_PARAMS_PAGE_ADDRESS))) = NRF_MBR_PARAMS_PAGE_ADDRESS;
  104. #elif defined ( __GNUC__ ) || defined ( __SES_ARM )
  105. uint32_t const m_uicr_mbr_params_page_address
  106. __attribute__ ((section(".uicr_mbr_params_page")))
  107. __attribute__ ((used)) = NRF_MBR_PARAMS_PAGE_ADDRESS;
  108. #elif defined ( __ICCARM__ )
  109. __root uint32_t const m_uicr_mbr_params_page_address
  110. @ NRF_UICR_MBR_PARAMS_PAGE_ADDRESS = NRF_MBR_PARAMS_PAGE_ADDRESS;
  111. #else
  112. #error Not a valid compiler/linker for m_mbr_params_page placement.
  113. #endif // Compiler specific
  114. #endif // #ifndef BL_SETTINGS_ACCESS_ONLY
  115. #endif // #if defined( NRF52_SERIES )
  116. nrf_dfu_settings_t s_dfu_settings;
  117. static uint32_t settings_crc_get(nrf_dfu_settings_t const * p_settings)
  118. {
  119. ASSERT(offsetof(nrf_dfu_settings_t, crc) == 0);
  120. // The crc is calculated from the s_dfu_settings struct, except the crc itself, the init command, bond data, and boot validation.
  121. return crc32_compute((uint8_t*)(p_settings) + 4, DFU_SETTINGS_INIT_COMMAND_OFFSET - 4, NULL);
  122. }
  123. static bool crc_ok(nrf_dfu_settings_t const * p_settings)
  124. {
  125. if (p_settings->crc != 0xFFFFFFFF)
  126. {
  127. // CRC is set. Content must be valid
  128. uint32_t crc = settings_crc_get(p_settings);
  129. if (crc == p_settings->crc)
  130. {
  131. return true;
  132. }
  133. }
  134. return false;
  135. }
  136. static uint32_t boot_validation_crc(nrf_dfu_settings_t const * p_settings)
  137. {
  138. return crc32_compute((const uint8_t *)&p_settings->boot_validation_softdevice,
  139. DFU_SETTINGS_BOOT_VALIDATION_SIZE - 4,
  140. NULL);
  141. }
  142. static bool boot_validation_crc_ok(nrf_dfu_settings_t const * p_settings)
  143. {
  144. return (boot_validation_crc(p_settings) == p_settings->boot_validation_crc);
  145. }
  146. static bool settings_crc_ok(void)
  147. {
  148. nrf_dfu_settings_t const * p_settings = (nrf_dfu_settings_t const *)m_dfu_settings_buffer;
  149. return crc_ok(p_settings);
  150. }
  151. static bool settings_backup_crc_ok(void)
  152. {
  153. nrf_dfu_settings_t const * p_settings = (nrf_dfu_settings_t const *)mp_dfu_settings_backup_buffer;
  154. return crc_ok(p_settings) && ((p_settings->settings_version == 1) || boot_validation_crc_ok(p_settings));
  155. }
  156. #define REGION_COPY_BY_MEMBER(start_member, end_member, p_dst_addr) \
  157. memcpy(p_dst_addr + offsetof(nrf_dfu_settings_t, start_member), \
  158. mp_dfu_settings_backup_buffer + offsetof(nrf_dfu_settings_t, start_member), \
  159. offsetof(nrf_dfu_settings_t, end_member) - offsetof(nrf_dfu_settings_t, start_member))
  160. static void settings_forbidden_parts_copy_from_backup(uint8_t * p_dst_addr)
  161. {
  162. #if NRF_DFU_IN_APP || NRF_BL_DFU_ALLOW_UPDATE_FROM_APP
  163. REGION_COPY_BY_MEMBER(settings_version, bank_current, p_dst_addr);
  164. REGION_COPY_BY_MEMBER(bank_0, write_offset, p_dst_addr);
  165. REGION_COPY_BY_MEMBER(sd_size, progress, p_dst_addr);
  166. REGION_COPY_BY_MEMBER(boot_validation_crc, peer_data, p_dst_addr);
  167. #else
  168. REGION_COPY_BY_MEMBER(settings_version, enter_buttonless_dfu, p_dst_addr);
  169. REGION_COPY_BY_MEMBER(init_command, peer_data, p_dst_addr);
  170. #endif
  171. }
  172. ret_code_t nrf_dfu_settings_init(bool sd_irq_initialized)
  173. {
  174. NRF_LOG_DEBUG("Calling nrf_dfu_settings_init()...");
  175. ret_code_t err_code = nrf_dfu_flash_init(sd_irq_initialized);
  176. if (err_code != NRF_SUCCESS)
  177. {
  178. NRF_LOG_ERROR("nrf_dfu_flash_init() failed with error: %x", err_code);
  179. return NRF_ERROR_INTERNAL;
  180. }
  181. bool settings_valid = settings_crc_ok();
  182. bool settings_backup_valid = settings_backup_crc_ok();
  183. if (settings_valid)
  184. {
  185. NRF_LOG_DEBUG("Using settings page.");
  186. memcpy(&s_dfu_settings, m_dfu_settings_buffer, sizeof(nrf_dfu_settings_t));
  187. if (settings_backup_valid)
  188. {
  189. NRF_LOG_DEBUG("Copying forbidden parts from backup page.");
  190. settings_forbidden_parts_copy_from_backup((uint8_t *)&s_dfu_settings);
  191. }
  192. }
  193. else if (settings_backup_valid)
  194. {
  195. NRF_LOG_INFO("Restoring settings from backup since the settings page contents are "
  196. "invalid (CRC error).");
  197. memcpy(&s_dfu_settings,
  198. mp_dfu_settings_backup_buffer,
  199. sizeof(nrf_dfu_settings_t));
  200. }
  201. else
  202. {
  203. NRF_LOG_WARNING("Resetting bootloader settings since neither the settings page nor the "
  204. "backup are valid (CRC error).");
  205. memset(&s_dfu_settings, 0x00, sizeof(nrf_dfu_settings_t));
  206. s_dfu_settings.settings_version = NRF_DFU_SETTINGS_VERSION;
  207. }
  208. if (NRF_DFU_SETTINGS_COMPATIBILITY_MODE && !NRF_DFU_IN_APP && (s_dfu_settings.settings_version == 1))
  209. {
  210. NRF_LOG_INFO("Old settings page detected. Upgrading info.");
  211. // Old version. Translate.
  212. memcpy(&s_dfu_settings.peer_data, (uint8_t *)&s_dfu_settings + DFU_SETTINGS_BOND_DATA_OFFSET_V1, NRF_DFU_PEER_DATA_LEN);
  213. memcpy(&s_dfu_settings.adv_name, (uint8_t *)&s_dfu_settings + DFU_SETTINGS_ADV_NAME_OFFSET_V1, NRF_DFU_ADV_NAME_LEN);
  214. // Initialize with defaults.
  215. s_dfu_settings.boot_validation_softdevice.type = NO_VALIDATION;
  216. s_dfu_settings.boot_validation_app.type = VALIDATE_CRC;
  217. s_dfu_settings.boot_validation_bootloader.type = NO_VALIDATION;
  218. memcpy(s_dfu_settings.boot_validation_app.bytes, &s_dfu_settings.bank_0.image_crc, sizeof(uint32_t));
  219. s_dfu_settings.settings_version = NRF_DFU_SETTINGS_VERSION;
  220. }
  221. err_code = nrf_dfu_settings_write_and_backup(NULL);
  222. if (err_code != NRF_SUCCESS)
  223. {
  224. NRF_LOG_ERROR("nrf_dfu_settings_write_and_backup() failed with error: %x", err_code);
  225. return NRF_ERROR_INTERNAL;
  226. }
  227. return NRF_SUCCESS;
  228. }
  229. static bool settings_forbidden_parts_equal_to_backup(uint8_t * p_compare_addr)
  230. {
  231. nrf_dfu_settings_t temp_settings;
  232. memcpy(&temp_settings, p_compare_addr, sizeof(nrf_dfu_settings_t));
  233. settings_forbidden_parts_copy_from_backup((uint8_t *)&temp_settings);
  234. return memcmp(&temp_settings, p_compare_addr, sizeof(nrf_dfu_settings_t)) == 0;
  235. }
  236. static ret_code_t settings_write(void * p_dst,
  237. void const * p_src,
  238. nrf_dfu_flash_callback_t callback,
  239. nrf_dfu_settings_t * p_dfu_settings_buffer)
  240. {
  241. ret_code_t err_code;
  242. if (memcmp(p_dst, p_src, sizeof(nrf_dfu_settings_t)) == 0)
  243. {
  244. NRF_LOG_DEBUG("Destination settings are identical to source, write not needed. Skipping.");
  245. if (callback != NULL)
  246. {
  247. callback(NULL);
  248. }
  249. return NRF_SUCCESS;
  250. }
  251. if (NRF_DFU_IN_APP && !settings_forbidden_parts_equal_to_backup((uint8_t *)&s_dfu_settings))
  252. {
  253. NRF_LOG_WARNING("Settings write aborted since it tries writing to forbidden settings.");
  254. return NRF_ERROR_FORBIDDEN;
  255. }
  256. NRF_LOG_DEBUG("Writing settings...");
  257. NRF_LOG_DEBUG("Erasing old settings at: 0x%08x", p_dst);
  258. // Not setting the callback function because ERASE is required before STORE
  259. // Only report completion on successful STORE.
  260. err_code = nrf_dfu_flash_erase((uint32_t)p_dst, 1, NULL);
  261. if (err_code != NRF_SUCCESS)
  262. {
  263. NRF_LOG_ERROR("Could not erase the settings page!");
  264. return NRF_ERROR_INTERNAL;
  265. }
  266. ASSERT(p_dfu_settings_buffer != NULL);
  267. memcpy(p_dfu_settings_buffer, p_src, sizeof(nrf_dfu_settings_t));
  268. err_code = nrf_dfu_flash_store((uint32_t)p_dst,
  269. p_dfu_settings_buffer,
  270. sizeof(nrf_dfu_settings_t),
  271. callback);
  272. if (err_code != NRF_SUCCESS)
  273. {
  274. NRF_LOG_ERROR("Could not write the DFU settings page!");
  275. return NRF_ERROR_INTERNAL;
  276. }
  277. return NRF_SUCCESS;
  278. }
  279. ret_code_t nrf_dfu_settings_write(nrf_dfu_flash_callback_t callback)
  280. {
  281. static nrf_dfu_settings_t dfu_settings_buffer;
  282. s_dfu_settings.crc = settings_crc_get(&s_dfu_settings);
  283. s_dfu_settings.boot_validation_crc = boot_validation_crc(&s_dfu_settings);
  284. return settings_write(m_dfu_settings_buffer,
  285. &s_dfu_settings,
  286. callback,
  287. &dfu_settings_buffer);
  288. }
  289. void settings_backup(nrf_dfu_flash_callback_t callback, void * p_src)
  290. {
  291. #if NRF_DFU_IN_APP
  292. NRF_LOG_INFO("Settings backup not available from app.");
  293. #else
  294. static nrf_dfu_settings_t dfu_settings_buffer;
  295. NRF_LOG_INFO("Backing up settings page to address 0x%x.", mp_dfu_settings_backup_buffer);
  296. ASSERT(crc_ok(p_src));
  297. ret_code_t err_code = settings_write(mp_dfu_settings_backup_buffer,
  298. p_src,
  299. callback,
  300. &dfu_settings_buffer);
  301. if (err_code != NRF_SUCCESS)
  302. {
  303. NRF_LOG_ERROR("Could not perform backup of bootloader settings! Error: 0x%x", err_code);
  304. }
  305. #endif
  306. }
  307. void nrf_dfu_settings_backup(nrf_dfu_flash_callback_t callback)
  308. {
  309. settings_backup(callback, m_dfu_settings_buffer);
  310. }
  311. ret_code_t nrf_dfu_settings_write_and_backup(nrf_dfu_flash_callback_t callback)
  312. {
  313. #if NRF_DFU_IN_APP
  314. ret_code_t err_code = nrf_dfu_settings_write(callback);
  315. #else
  316. ret_code_t err_code = nrf_dfu_settings_write(NULL);
  317. if (err_code == NRF_SUCCESS)
  318. {
  319. settings_backup(callback, &s_dfu_settings);
  320. }
  321. #endif
  322. return err_code;
  323. }
  324. __WEAK ret_code_t nrf_dfu_settings_additional_erase(void)
  325. {
  326. NRF_LOG_WARNING("No additional data erased");
  327. return NRF_SUCCESS;
  328. }