nrf_dfu_utils.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_utils.h"
  41. #include "nrf_dfu_settings.h"
  42. #include "nrf_bootloader_info.h"
  43. #include "crc32.h"
  44. #include "nrf_log.h"
  45. #include "nrf_dfu_validation.h"
  46. void nrf_dfu_bank_invalidate(nrf_dfu_bank_t * const p_bank)
  47. {
  48. // Set the bank-code to invalid, and reset size/CRC
  49. memset(p_bank, 0, sizeof(nrf_dfu_bank_t));
  50. // Reset write pointer after completed operation
  51. s_dfu_settings.write_offset = 0;
  52. }
  53. #if !defined(BLE_STACK_SUPPORT_REQD) && !defined(ANT_STACK_SUPPORT_REQD)
  54. void nrf_dfu_softdevice_invalidate(void)
  55. {
  56. static const uint32_t all_zero = 0UL;
  57. if (SD_PRESENT && !NRF_DFU_IN_APP)
  58. {
  59. ret_code_t err_code = nrf_dfu_flash_store(SD_MAGIC_NUMBER_ABS_OFFSET_GET(MBR_SIZE), &all_zero, 4, NULL);
  60. if (err_code != NRF_SUCCESS)
  61. {
  62. NRF_LOG_ERROR("Could not invalidate SoftDevice.")
  63. }
  64. else
  65. {
  66. // If there is an app it must be invalidated since its start address can no longer be resolved.
  67. if (s_dfu_settings.bank_0.bank_code == NRF_DFU_BANK_VALID_APP)
  68. {
  69. s_dfu_settings.bank_0.bank_code = NRF_DFU_BANK_INVALID;
  70. }
  71. // Since the start of bank 0 has now implicitly been moved to the start
  72. // of the invalidated SoftDevice, its image size must be increased by the
  73. // same amount so the start of bank 1 will be correctly calculated.
  74. s_dfu_settings.bank_0.image_size += SD_SIZE_GET(MBR_SIZE) - MBR_SIZE;
  75. }
  76. }
  77. }
  78. #endif
  79. uint32_t nrf_dfu_bank0_start_addr(void)
  80. {
  81. if (SD_PRESENT)
  82. {
  83. return ALIGN_TO_PAGE(SD_SIZE_GET(MBR_SIZE));
  84. }
  85. else
  86. {
  87. return MBR_SIZE;
  88. }
  89. }
  90. uint32_t nrf_dfu_bank1_start_addr(void)
  91. {
  92. uint32_t bank0_addr = nrf_dfu_bank0_start_addr();
  93. return ALIGN_TO_PAGE(bank0_addr + s_dfu_settings.bank_0.image_size);
  94. }
  95. uint32_t nrf_dfu_app_start_address(void)
  96. {
  97. return nrf_dfu_bank0_start_addr();
  98. }
  99. uint32_t nrf_dfu_softdevice_start_address(void)
  100. {
  101. return MBR_SIZE;
  102. }
  103. uint32_t nrf_dfu_cache_prepare(const uint32_t required_size, bool single_bank, bool keep_app, bool keep_softdevice)
  104. {
  105. ret_code_t err_code;
  106. bool cache_too_small;
  107. enum
  108. {
  109. INITIAL_DELETE_APP = 0,
  110. APP_DELETED_DELETE_SOFTDEVICE = 1,
  111. SOFTDEVICE_DELETED = 2
  112. } pass;
  113. NRF_LOG_DEBUG("Enter nrf_dfu_cache_prepare()");
  114. NRF_LOG_DEBUG("required_size: 0x%x.", required_size);
  115. NRF_LOG_DEBUG("single_bank: %s.", single_bank ? "true" : "false");
  116. NRF_LOG_DEBUG("keep_app: %s.", keep_app ? "true" : "false");
  117. NRF_LOG_DEBUG("keep_softdevice: %s.", keep_softdevice ? "true" : "false");
  118. NRF_LOG_DEBUG("SD_PRESENT: %s.", SD_PRESENT ? "true" : "false");
  119. NRF_LOG_DEBUG("Bank contents:");
  120. NRF_LOG_DEBUG("Bank 0 code: 0x%02x: Size: 0x%x", s_dfu_settings.bank_0.bank_code, s_dfu_settings.bank_0.image_size);
  121. NRF_LOG_DEBUG("Bank 1 code: 0x%02x: Size: 0x%x", s_dfu_settings.bank_1.bank_code, s_dfu_settings.bank_1.image_size);
  122. // Pass 0 deletes the app if necessary or requested, and if so, proceeds to pass 1.
  123. // Pass 1 deletes the SoftDevice if necessary or requested, and if so, proceeds to pass 2.
  124. // Pass 2 does a last size check.
  125. for (pass = INITIAL_DELETE_APP; pass <= SOFTDEVICE_DELETED; pass++)
  126. {
  127. uint32_t cache_address;
  128. const uint32_t bootloader_start_addr = BOOTLOADER_START_ADDR; // Assign to a variable to prevent warning in Keil 4.
  129. bool keep_firmware = true;
  130. bool delete_more;
  131. switch (pass)
  132. {
  133. case INITIAL_DELETE_APP:
  134. cache_address = nrf_dfu_bank1_start_addr();
  135. // If there is no app, keep_app should be assumed false, so we can free up more space.
  136. keep_firmware = keep_app && (s_dfu_settings.bank_0.bank_code == NRF_DFU_BANK_VALID_APP);
  137. break;
  138. case APP_DELETED_DELETE_SOFTDEVICE:
  139. cache_address = nrf_dfu_bank0_start_addr();
  140. // If there is no SoftDevice, keep_SoftDevice should be assumed true, because there is
  141. // no point to continuing since the SoftDevice is the last firmware that can be deleted.
  142. keep_firmware = keep_softdevice || !SD_PRESENT;
  143. break;
  144. case SOFTDEVICE_DELETED:
  145. cache_address = nrf_dfu_softdevice_start_address();
  146. break;
  147. default:
  148. ASSERT(false);
  149. cache_address = 0;
  150. break;
  151. }
  152. ASSERT(cache_address <= DFU_REGION_END(bootloader_start_addr));
  153. cache_too_small = required_size > (DFU_REGION_END(bootloader_start_addr) - cache_address);
  154. delete_more = cache_too_small || single_bank; // Delete app or SoftDevice only if we need more room, or if single bank is requested.
  155. NRF_LOG_DEBUG("pass: %d.", pass);
  156. NRF_LOG_DEBUG("cache_address: 0x%x.", cache_address);
  157. NRF_LOG_DEBUG("cache_too_small: %s.", cache_too_small ? "true" : "false");
  158. NRF_LOG_DEBUG("keep_firmware: %s.", keep_firmware ? "true" : "false");
  159. NRF_LOG_DEBUG("delete_more: %s.", delete_more ? "true" : "false");
  160. if (!delete_more || keep_firmware || (pass >= SOFTDEVICE_DELETED))
  161. {
  162. // Stop, done.
  163. break;
  164. }
  165. }
  166. if (cache_too_small)
  167. {
  168. NRF_LOG_WARNING("Aborting. Cannot fit new firmware on device");
  169. err_code = NRF_ERROR_NO_MEM;
  170. }
  171. else
  172. {
  173. // Room was found. Make the necessary preparations for receiving update.
  174. #if !defined(BLE_STACK_SUPPORT_REQD) && !defined(ANT_STACK_SUPPORT_REQD)
  175. if (pass >= SOFTDEVICE_DELETED)
  176. {
  177. NRF_LOG_DEBUG("Invalidating SoftDevice.");
  178. nrf_dfu_softdevice_invalidate();
  179. }
  180. #endif
  181. if (pass >= APP_DELETED_DELETE_SOFTDEVICE)
  182. {
  183. NRF_LOG_DEBUG("Invalidating app.");
  184. nrf_dfu_bank_invalidate(&s_dfu_settings.bank_0);
  185. }
  186. err_code = NRF_SUCCESS;
  187. }
  188. return err_code;
  189. }