nrf_mbr.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 2014 - 2017, Nordic Semiconductor ASA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form, except as embedded into a Nordic
  12. * Semiconductor ASA integrated circuit in a product or a software update for
  13. * such product, must reproduce the above copyright notice, this list of
  14. * conditions and the following disclaimer in the documentation and/or other
  15. * materials provided with the distribution.
  16. *
  17. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * 4. This software, with or without modification, must only be used with a
  22. * Nordic Semiconductor ASA integrated circuit.
  23. *
  24. * 5. Any software provided in binary form under this license must not be reverse
  25. * engineered, decompiled, modified and/or disassembled.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  28. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  36. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. /**
  39. @defgroup nrf_mbr_api Master Boot Record API
  40. @{
  41. @brief APIs for updating SoftDevice and BootLoader
  42. */
  43. #ifndef NRF_MBR_H__
  44. #define NRF_MBR_H__
  45. #include "nrf_svc.h"
  46. #include <stdint.h>
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /** @addtogroup NRF_MBR_DEFINES Defines
  51. * @{ */
  52. /**@brief MBR SVC Base number. */
  53. #define MBR_SVC_BASE (0x18)
  54. /**@brief Page size in words. */
  55. #define MBR_PAGE_SIZE_IN_WORDS (1024)
  56. /** @brief The size that must be reserved for the MBR when a SoftDevice is written to flash.
  57. This is the offset where the first byte of the SoftDevice hex file is written.*/
  58. #define MBR_SIZE (0x1000)
  59. /** @} */
  60. /** @addtogroup NRF_MBR_ENUMS Enumerations
  61. * @{ */
  62. /**@brief nRF Master Boot Record API SVC numbers. */
  63. enum NRF_MBR_SVCS
  64. {
  65. SD_MBR_COMMAND = MBR_SVC_BASE, /**< ::sd_mbr_command */
  66. };
  67. /**@brief Possible values for ::sd_mbr_command_t.command */
  68. enum NRF_MBR_COMMANDS
  69. {
  70. SD_MBR_COMMAND_COPY_BL, /**< Copy a new BootLoader. @see ::sd_mbr_command_copy_bl_t*/
  71. SD_MBR_COMMAND_COPY_SD, /**< Copy a new SoftDevice. @see ::sd_mbr_command_copy_sd_t*/
  72. SD_MBR_COMMAND_INIT_SD, /**< Initialize forwarding interrupts to SD, and run reset function in SD. Does not require any parameters in ::sd_mbr_command_t params.*/
  73. SD_MBR_COMMAND_COMPARE, /**< This command works like memcmp. @see ::sd_mbr_command_compare_t*/
  74. SD_MBR_COMMAND_VECTOR_TABLE_BASE_SET, /**< Change the address the MBR starts after a reset. @see ::sd_mbr_command_vector_table_base_set_t*/
  75. SD_MBR_COMMAND_RESERVED,
  76. SD_MBR_COMMAND_IRQ_FORWARD_ADDRESS_SET, /**< Start forwarding all interrupts to this address. @see ::sd_mbr_command_irq_forward_address_set_t*/
  77. };
  78. /** @} */
  79. /** @addtogroup NRF_MBR_TYPES Types
  80. * @{ */
  81. /**@brief This command copies part of a new SoftDevice
  82. *
  83. * The destination area is erased before copying.
  84. * If dst is in the middle of a flash page, that whole flash page will be erased.
  85. * If (dst+len) is in the middle of a flash page, that whole flash page will be erased.
  86. *
  87. * The user of this function is responsible for setting the BPROT registers.
  88. *
  89. * @retval ::NRF_SUCCESS indicates that the contents of the memory blocks where copied correctly.
  90. * @retval ::NRF_ERROR_INTERNAL indicates that the contents of the memory blocks where not verified correctly after copying.
  91. */
  92. typedef struct
  93. {
  94. uint32_t *src; /**< Pointer to the source of data to be copied.*/
  95. uint32_t *dst; /**< Pointer to the destination where the content is to be copied.*/
  96. uint32_t len; /**< Number of 32 bit words to copy. Must be a multiple of @ref MBR_PAGE_SIZE_IN_WORDS words.*/
  97. } sd_mbr_command_copy_sd_t;
  98. /**@brief This command works like memcmp, but takes the length in words.
  99. *
  100. * @retval ::NRF_SUCCESS indicates that the contents of both memory blocks are equal.
  101. * @retval ::NRF_ERROR_NULL indicates that the contents of the memory blocks are not equal.
  102. */
  103. typedef struct
  104. {
  105. uint32_t *ptr1; /**< Pointer to block of memory. */
  106. uint32_t *ptr2; /**< Pointer to block of memory. */
  107. uint32_t len; /**< Number of 32 bit words to compare.*/
  108. } sd_mbr_command_compare_t;
  109. /**@brief This command copies a new BootLoader.
  110. *
  111. * With this command, destination of BootLoader is always the address written in
  112. * NRF_UICR->BOOTADDR.
  113. *
  114. * Destination is erased by this function.
  115. * If (destination+bl_len) is in the middle of a flash page, that whole flash page will be erased.
  116. *
  117. * This function will use PROTENSET to protect the flash that is not intended to be written.
  118. *
  119. * On success, this function will not return. It will start the new BootLoader from reset-vector as normal.
  120. *
  121. * @retval ::NRF_ERROR_INTERNAL indicates an internal error that should not happen.
  122. * @retval ::NRF_ERROR_FORBIDDEN if NRF_UICR->BOOTADDR is not set.
  123. * @retval ::NRF_ERROR_INVALID_LENGTH if parameters attempts to read or write outside flash area.
  124. * @retval ::NRF_ERROR_NO_MEM if no parameter page is provided (see SoftDevice Specification for more info)
  125. */
  126. typedef struct
  127. {
  128. uint32_t *bl_src; /**< Pointer to the source of the Bootloader to be be copied.*/
  129. uint32_t bl_len; /**< Number of 32 bit words to copy for BootLoader. */
  130. } sd_mbr_command_copy_bl_t;
  131. /**@brief Change the address the MBR starts after a reset
  132. *
  133. * Once this function has been called, this address is where the MBR will start to forward
  134. * interrupts to after a reset.
  135. *
  136. * To restore default forwarding this function should be called with @ref address set to 0. The
  137. * MBR will then start forwarding interrupts to the address in NFR_UICR->BOOTADDR or to the
  138. * SoftDevice if the BOOTADDR is not set.
  139. *
  140. * On success, this function will not return. It will reset the device.
  141. *
  142. * @retval ::NRF_ERROR_INTERNAL indicates an internal error that should not happen.
  143. * @retval ::NRF_ERROR_INVALID_ADDR if parameter address is outside of the flash size.
  144. * @retval ::NRF_ERROR_NO_MEM if no parameter page is provided (see SoftDevice Specification for more info)
  145. */
  146. typedef struct
  147. {
  148. uint32_t address; /**< The base address of the interrupt vector table for forwarded interrupts.*/
  149. } sd_mbr_command_vector_table_base_set_t;
  150. /**@brief Sets the base address of the interrupt vector table for interrupts forwarded from the MBR
  151. *
  152. * Unlike sd_mbr_command_vector_table_base_set_t, this function does not reset, and it does not
  153. * change where the MBR starts after reset.
  154. *
  155. * @retval ::NRF_SUCCESS
  156. */
  157. typedef struct
  158. {
  159. uint32_t address; /**< The base address of the interrupt vector table for forwarded interrupts.*/
  160. } sd_mbr_command_irq_forward_address_set_t;
  161. /**@brief Input structure containing data used when calling ::sd_mbr_command
  162. *
  163. * Depending on what command value that is set, the corresponding params value type must also be
  164. * set. See @ref NRF_MBR_COMMANDS for command types and corresponding params value type. If command
  165. * @ref SD_MBR_COMMAND_INIT_SD is set, it is not necessary to set any values under params.
  166. */
  167. typedef struct
  168. {
  169. uint32_t command; /**< Type of command to be issued. See @ref NRF_MBR_COMMANDS. */
  170. union
  171. {
  172. sd_mbr_command_copy_sd_t copy_sd; /**< Parameters for copy SoftDevice.*/
  173. sd_mbr_command_compare_t compare; /**< Parameters for verify.*/
  174. sd_mbr_command_copy_bl_t copy_bl; /**< Parameters for copy BootLoader. Requires parameter page. */
  175. sd_mbr_command_vector_table_base_set_t base_set; /**< Parameters for vector table base set. Requires parameter page.*/
  176. sd_mbr_command_irq_forward_address_set_t irq_forward_address_set; /**< Parameters for irq forward address set*/
  177. } params; /**< Command parameters. */
  178. } sd_mbr_command_t;
  179. /** @} */
  180. /** @addtogroup NRF_MBR_FUNCTIONS Functions
  181. * @{ */
  182. /**@brief Issue Master Boot Record commands
  183. *
  184. * Commands used when updating a SoftDevice and bootloader.
  185. *
  186. * The @ref SD_MBR_COMMAND_COPY_BL and @ref SD_MBR_COMMAND_VECTOR_TABLE_BASE_SET requires
  187. * parameters to be retained by the MBR when resetting the IC. This is done in a separate flash
  188. * page provided by the application. The UICR register UICR.NRFFW[1] must be set to an address
  189. * corresponding to a page in the application flash space. This page will be cleared by the MBR and
  190. * used to store the command before reset. When the UICR.NRFFW[1] field is set the page it refers
  191. * to must not be used by the application. If the UICR.NRFFW[1] is set to 0xFFFFFFFF (the default)
  192. * MBR commands which use flash will be unavailable and return @ref NRF_ERROR_NO_MEM.
  193. *
  194. * @param[in] param Pointer to a struct describing the command.
  195. *
  196. * @note For return values, see ::sd_mbr_command_copy_sd_t, ::sd_mbr_command_copy_bl_t,
  197. * ::sd_mbr_command_compare_t, ::sd_mbr_command_vector_table_base_set_t,
  198. * ::sd_mbr_command_irq_forward_address_set_t
  199. *
  200. * @retval ::NRF_ERROR_NO_MEM if UICR.NRFFW[1] is not set (i.e. is 0xFFFFFFFF).
  201. * @retval ::NRF_ERROR_INVALID_PARAM if an invalid command is given.
  202. */
  203. SVCALL(SD_MBR_COMMAND, uint32_t, sd_mbr_command(sd_mbr_command_t* param));
  204. /** @} */
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. #endif // NRF_MBR_H__
  209. /**
  210. @}
  211. */