nrf_dfu_settings.c 13 KB

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