nrf_dfu_validation.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Copyright (c) 2017 - 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. /**@file
  41. *
  42. * @defgroup nrf_dfu_validation Validation
  43. * @{
  44. * @ingroup nrf_dfu
  45. */
  46. #ifndef __NRF_DFU_VALIDATION_H
  47. #define __NRF_DFU_VALIDATION_H
  48. #include "stdint.h"
  49. #include "sdk_errors.h"
  50. #include "dfu-cc.pb.h"
  51. #include "nrf_dfu_handling_error.h"
  52. /**
  53. * @brief Function for module initialization.
  54. *
  55. * Function checks if there is a valid init packet in DFU settings written in flash.
  56. */
  57. void nrf_dfu_validation_init(void);
  58. /**
  59. * @brief Function called on reception of init command creation request.
  60. *
  61. * @param[in] size Size of incoming init packet.
  62. *
  63. * @return Operation result. See @ref nrf_dfu_result_t
  64. */
  65. nrf_dfu_result_t nrf_dfu_validation_init_cmd_create(uint32_t size);
  66. /**
  67. * @brief Function called on reception of fragment of init command.
  68. *
  69. * @param[in] p_data Init command fragment.
  70. * @param[in] length Init command fragment size.
  71. *
  72. * @return Operation result. See @ref nrf_dfu_result_t
  73. */
  74. nrf_dfu_result_t nrf_dfu_validation_init_cmd_append(uint8_t const * p_data, uint32_t length);
  75. /**
  76. * @brief Function for getting init command status.
  77. *
  78. * @param[out] p_offset Current offset.
  79. * @param[out] p_crc Current CRC.
  80. * @param[out] p_max_size Maximum size of init command.
  81. */
  82. void nrf_dfu_validation_init_cmd_status_get(uint32_t * p_offset,
  83. uint32_t * p_crc,
  84. uint32_t * p_max_size);
  85. /**
  86. * @brief Function for inquiring whether a valid init command has been received.
  87. *
  88. * @return true if there is a valid init command. This can be true at boot time
  89. * if the device was reset during a DFU operation.
  90. */
  91. bool nrf_dfu_validation_init_cmd_present(void);
  92. /**
  93. * @brief Function for validating init command and retrieving the address and length of the firmware.
  94. *
  95. * If init command is successfully validated Bank 1 details are written to out parameters.
  96. *
  97. * Until @ref nrf_dfu_validation_init_cmd_create is called, this function can be called
  98. * again after the first time without side effects to retrieve address and length.
  99. *
  100. * @param[out] p_dst_data_addr Start address of received data, if validation is successful.
  101. * @param[out] p_data_len Expected length of received data, if validation is successful.
  102. *
  103. * @return Operation result. See @ref nrf_dfu_result_t
  104. */
  105. nrf_dfu_result_t nrf_dfu_validation_init_cmd_execute(uint32_t * p_dst_data_addr,
  106. uint32_t * p_data_len);
  107. /**
  108. * @brief Function for validating the init command.
  109. *
  110. * @return Operation result. See @ref nrf_dfu_result_t.
  111. */
  112. nrf_dfu_result_t nrf_dfu_validation_prevalidate(void);
  113. /**
  114. * @brief Function for validating the firmware for booting.
  115. *
  116. * @param[in] p_validation Validation parameters.
  117. * @param[in] data_addr Start address of the firmware.
  118. * @param[in] data_len Length of the firmware.
  119. *
  120. * @return Whether the firmware is valid for booting.
  121. */
  122. bool nrf_dfu_validation_boot_validate(boot_validation_t const * p_validation, uint32_t data_addr, uint32_t data_len);
  123. /**
  124. * @brief Function for postvalidating the update after all data is received.
  125. *
  126. * @param[in] data_addr Start address of the received data.
  127. * @param[in] data_len Length of the received data.
  128. *
  129. * @return Operation result. See @ref nrf_dfu_result_t.
  130. */
  131. nrf_dfu_result_t nrf_dfu_validation_post_data_execute(uint32_t data_addr, uint32_t data_len);
  132. /**
  133. * @brief Function for preparing the update for activation.
  134. *
  135. * This function is called after a reset, after all data is received. This function also runs
  136. * @ref nrf_dfu_validation_post_data_execute internally. If this succeeds, the update is
  137. * activated by the activation machinery in the bootloader the next time it runs.
  138. *
  139. * @note The caller must have permissions to edit the relevant entries in the settings.
  140. *
  141. * @param[in] data_addr Start address of the received data.
  142. * @param[in] data_len Length of the received data.
  143. *
  144. * @return Operation result. See @ref nrf_dfu_result_t
  145. */
  146. nrf_dfu_result_t nrf_dfu_validation_activation_prepare(uint32_t data_addr, uint32_t data_len);
  147. /**
  148. * @brief Function to execute on a validated external app.
  149. *
  150. * @details This function is called once all data is received with the parameter
  151. * @p is_boot set to false. The function is called during bootup with the parameter
  152. * set to true.
  153. *
  154. *
  155. *
  156. * @note This function requires that @ref NRF_DFU_SUPPORTS_EXTERNAL_APP is set to 1.
  157. * It is up to the user to implement this function.
  158. *
  159. * @warning Parameter @p is_trusted must be used to ensure that no loss of security of process can happen.
  160. * This parameter should only be set if the function is called after a root-of-trust
  161. * reset on the device.
  162. *
  163. * Parameter @p is_trusted can be used for the following:
  164. * - Ensuring that an external application is run only once (after root-of-trust).
  165. * - Ensuring that a bank flag or any other flash access can only happen after root-of-trust.
  166. * - Ensuring that the device reaches the correct state after a power failure on the device.
  167. *
  168. * @param[in] p_init Init command for the firmware upgrade.
  169. * @param[in] is_trusted Must be set to true if this is called after root-of-trust boot.
  170. * Must be set to false if this is called from DFU mode or background
  171. * DFU operation.
  172. *
  173. * @return Operation result. see @ref nrf_dfu_result_t.
  174. */
  175. nrf_dfu_result_t nrf_dfu_validation_post_external_app_execute(dfu_init_command_t const * p_init, bool is_trusted);
  176. /**
  177. * @brief Function to check if there is a valid external app in Bank 1.
  178. *
  179. * @returns True if valid external app, otherwise false.
  180. */
  181. bool nrf_dfu_validation_valid_external_app(void);
  182. #endif //__NRF_DFU_VALIDATION_H
  183. /** @} */