nrf_dfu_utils.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. /**@file
  41. *
  42. * @defgroup sdk_nrf_dfu_utils DFU utilities
  43. * @{
  44. * @ingroup nrf_dfu
  45. */
  46. #ifndef NRF_DFU_UTILS_H__
  47. #define NRF_DFU_UTILS_H__
  48. #include <stdint.h>
  49. #include <stdbool.h>
  50. #include "nrf_dfu_types.h"
  51. #include "app_util.h"
  52. #ifdef __cplusplus
  53. extern "C"
  54. {
  55. #endif
  56. /**
  57. * Round up val to the next page boundary
  58. */
  59. #define ALIGN_TO_PAGE(val) ALIGN_NUM((CODE_PAGE_SIZE), (val))
  60. /** @brief Function for getting the start address of bank 0.
  61. *
  62. * @note Bank 0 starts after the SoftDevice if a SoftDevice is present.
  63. *
  64. * @return The start address of bank 0.
  65. */
  66. uint32_t nrf_dfu_bank0_start_addr(void);
  67. /** @brief Function for getting the start address of bank 1.
  68. *
  69. * @return The start address of bank 1.
  70. */
  71. uint32_t nrf_dfu_bank1_start_addr(void);
  72. /** @brief Function for getting the start address of the app.
  73. *
  74. * @return The start address of the bootable app.
  75. */
  76. uint32_t nrf_dfu_app_start_address(void);
  77. /** @brief Function for getting the start address of the SoftDevice.
  78. *
  79. * @return The start address of the SoftDevivce.
  80. */
  81. uint32_t nrf_dfu_softdevice_start_address(void);
  82. /** @brief Function for finding and preparing a place in flash in which to store a DFU update.
  83. *
  84. * @details This function checks the size requirements and selects a location for
  85. * placing the cache of the DFU images.
  86. * The function tries to find enough space after the existing firmwares. If there is not
  87. * enough space, the present application is deleted. If there is still not enough space,
  88. * the SoftDevice is deleted.
  89. * If @p single_bank is true, the default behavior is to immediately delete the app and
  90. * SoftDevice as necessary to place the new firmware at its intended location. If the
  91. * intended location cannot be made available, or if the update is a bootloader update,
  92. * the update will be a dual bank update, and nothing will be deleted by this function
  93. * except when needed for size.
  94. * If @p keep_app is true, the app is never deleted by this function. Likewise if @p
  95. * keep_softdevice is true, the SoftDevice is never deleted by this function.
  96. * If the new firmware cannot fit within the constraints, nothing is deleted and the
  97. * function fails.
  98. *
  99. * @param[in] required_size Requirements for the size of the new image.
  100. * @param[in] single_bank Whether to put the firmware directly where it's meant to go.
  101. * @p keep_app and @p keep_softdevice take precedence over this.
  102. * @param[in] keep_app True to ensure the app is not deleted by this function. This
  103. * effectively enforces dual bank update.
  104. * @param[out] keep_softdevice True to ensure the SoftDevice is not deleted by this function.
  105. *
  106. * @retval NRF_SUCCESS If a cache location was found for the DFU process.
  107. * @retval NRF_ERROR_NO_MEM If there is not enough space available to receive the update.
  108. * Nothing has been deleted.
  109. */
  110. uint32_t nrf_dfu_cache_prepare(uint32_t required_size, bool single_bank, bool keep_app, bool keep_softdevice);
  111. /**@brief Function for making sure a SoftDevice is not recognized as such anymore.
  112. *
  113. * @details It works by overwriting the magic number of the SoftDevice with 0s. The
  114. * magic number is used throughout the bootloader to detect whether a SoftDevice
  115. * is present.
  116. *
  117. * @warning This function should only be called when both banks are already invalid.
  118. * because the (implicit) position of the banks will shift when the SoftDevice
  119. * is invalidated.
  120. */
  121. void nrf_dfu_softdevice_invalidate(void);
  122. /**@brief Function for making sure a bank is not copied or booted.
  123. *
  124. * @details This also sets the size of the bank to 0.
  125. *
  126. * @param[in] p_bank Pointer to the bank to be invalidated.
  127. */
  128. void nrf_dfu_bank_invalidate(nrf_dfu_bank_t * const p_bank);
  129. #ifdef __cplusplus
  130. }
  131. #endif
  132. #endif // NRF_DFU_UTILS_H__
  133. /** @} */