nrf_dfu_settings_svci.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright (c) 2017 - 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 <stddef.h>
  41. #include <string.h>
  42. #include "app_error.h"
  43. #include "sdk_macros.h"
  44. #include "nrf_dfu_settings.h"
  45. #include "nrf_nvmc.h"
  46. #include "crc32.h"
  47. #define NRF_LOG_MODULE_NAME nrf_dfu_settings_svci
  48. #include "nrf_log.h"
  49. NRF_LOG_MODULE_REGISTER();
  50. #define DFU_SETTINGS_PEER_DATA_OFFSET offsetof(nrf_dfu_settings_t, peer_data) //<! Offset in the settings struct where the additional peer data is located.
  51. #define DFU_SETTINGS_ADV_NAME_OFFSET offsetof(nrf_dfu_settings_t, adv_name) //<! Offset in the settings struct where the additional advertisement name is located.
  52. extern nrf_dfu_settings_t s_dfu_settings;
  53. extern uint8_t m_dfu_settings_buffer[CODE_PAGE_SIZE];
  54. #if defined(NRF_DFU_BLE_REQUIRES_BONDS) && (NRF_DFU_BLE_REQUIRES_BONDS == 1)
  55. ret_code_t nrf_dfu_settings_peer_data_write(nrf_dfu_peer_data_t * p_data)
  56. {
  57. uint32_t ret_val;
  58. uint32_t * p_peer_data_settings =
  59. (uint32_t*) &m_dfu_settings_buffer[DFU_SETTINGS_PEER_DATA_OFFSET];
  60. uint32_t crc = (uint32_t)*p_peer_data_settings;
  61. VERIFY_PARAM_NOT_NULL(p_data);
  62. if (crc != 0xFFFFFFFF)
  63. {
  64. // Already written to, must be cleared out
  65. // Reset required.
  66. return NRF_ERROR_INVALID_STATE;
  67. }
  68. // Calculate the CRC for the structure excluding the CRC value itself.
  69. p_data->crc = crc32_compute((uint8_t*)p_data + 4, sizeof(nrf_dfu_peer_data_t) - 4, NULL);
  70. // Using SoftDevice call since this function cannot use static memory.
  71. ret_val = sd_flash_write(p_peer_data_settings,
  72. (uint32_t*)p_data,
  73. sizeof(nrf_dfu_peer_data_t)/4);
  74. return ret_val;
  75. }
  76. ret_code_t nrf_dfu_settings_peer_data_copy(nrf_dfu_peer_data_t * p_data)
  77. {
  78. VERIFY_PARAM_NOT_NULL(p_data);
  79. memcpy(p_data, &m_dfu_settings_buffer[DFU_SETTINGS_PEER_DATA_OFFSET], sizeof(nrf_dfu_peer_data_t));
  80. return NRF_SUCCESS;
  81. }
  82. bool nrf_dfu_settings_peer_data_is_valid(void)
  83. {
  84. nrf_dfu_peer_data_t * p_peer_data =
  85. (nrf_dfu_peer_data_t*) &m_dfu_settings_buffer[DFU_SETTINGS_PEER_DATA_OFFSET];
  86. // Calculate the CRC for the structure excluding the CRC value itself.
  87. uint32_t crc = crc32_compute((uint8_t*)p_peer_data + 4, sizeof(nrf_dfu_peer_data_t) - 4, NULL);
  88. return (p_peer_data->crc == crc);
  89. }
  90. #else // not NRF_DFU_BLE_REQUIRES_BONDS
  91. ret_code_t nrf_dfu_settings_adv_name_write(nrf_dfu_adv_name_t * p_adv_name)
  92. {
  93. uint32_t ret_val;
  94. uint32_t * p_adv_name_settings =
  95. (uint32_t*) &m_dfu_settings_buffer[DFU_SETTINGS_ADV_NAME_OFFSET];
  96. uint32_t crc = (uint32_t)*p_adv_name_settings;
  97. VERIFY_PARAM_NOT_NULL(p_adv_name);
  98. if (crc != 0xFFFFFFFF)
  99. {
  100. // Already written to, must be cleared out.
  101. // Reset required
  102. return NRF_ERROR_INVALID_STATE;
  103. }
  104. // Calculate the CRC for the structure excluding the CRC value itself.
  105. p_adv_name->crc = crc32_compute((uint8_t *)p_adv_name + 4, sizeof(nrf_dfu_adv_name_t) - 4, NULL);
  106. // Using SoftDevice call since this function cannot use static memory.
  107. ret_val = sd_flash_write(p_adv_name_settings,
  108. (uint32_t*) p_adv_name,
  109. sizeof(nrf_dfu_adv_name_t)/4);
  110. return ret_val;
  111. }
  112. ret_code_t nrf_dfu_settings_adv_name_copy(nrf_dfu_adv_name_t * p_adv_name)
  113. {
  114. VERIFY_PARAM_NOT_NULL(p_adv_name);
  115. memcpy(p_adv_name, &m_dfu_settings_buffer[DFU_SETTINGS_ADV_NAME_OFFSET], sizeof(nrf_dfu_adv_name_t));
  116. return NRF_SUCCESS;
  117. }
  118. bool nrf_dfu_settings_adv_name_is_valid(void)
  119. {
  120. nrf_dfu_adv_name_t * p_adv_name =
  121. (nrf_dfu_adv_name_t*)&m_dfu_settings_buffer[DFU_SETTINGS_ADV_NAME_OFFSET];
  122. // Calculate the CRC for the structure excluding the CRC value itself.
  123. uint32_t crc = crc32_compute((uint8_t*)p_adv_name + 4, sizeof(nrf_dfu_adv_name_t) - 4, NULL);
  124. return (p_adv_name->crc == crc);
  125. }
  126. #endif
  127. //lint -save -e(14)
  128. ret_code_t nrf_dfu_settings_additional_erase(void)
  129. {
  130. ret_code_t ret_code = NRF_SUCCESS;
  131. // Check CRC for both types.
  132. if ( (s_dfu_settings.peer_data.crc != 0xFFFFFFFF)
  133. || (s_dfu_settings.adv_name.crc != 0xFFFFFFFF))
  134. {
  135. NRF_LOG_DEBUG("Erasing settings page additional data.");
  136. // Erasing and resetting the settings page without the peer data/adv data
  137. nrf_nvmc_page_erase(BOOTLOADER_SETTINGS_ADDRESS);
  138. nrf_nvmc_write_words(BOOTLOADER_SETTINGS_ADDRESS, (uint32_t const *)&s_dfu_settings, DFU_SETTINGS_PEER_DATA_OFFSET / 4);
  139. }
  140. return ret_code;
  141. }
  142. //lint -restore