nrf_mbr.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 the flash protect peripheral (BPROT or ACL) to protect the flash that is
  118. * not intended to be written.
  119. *
  120. * On success, this function will not return. It will start the new BootLoader from reset-vector as normal.
  121. *
  122. * @retval ::NRF_ERROR_INTERNAL indicates an internal error that should not happen.
  123. * @retval ::NRF_ERROR_FORBIDDEN if NRF_UICR->BOOTADDR is not set.
  124. * @retval ::NRF_ERROR_INVALID_LENGTH if parameters attempts to read or write outside flash area.
  125. * @retval ::NRF_ERROR_NO_MEM if no parameter page is provided (see SoftDevice Specification for more info)
  126. */
  127. typedef struct
  128. {
  129. uint32_t *bl_src; /**< Pointer to the source of the Bootloader to be be copied.*/
  130. uint32_t bl_len; /**< Number of 32 bit words to copy for BootLoader. */
  131. } sd_mbr_command_copy_bl_t;
  132. /**@brief Change the address the MBR starts after a reset
  133. *
  134. * Once this function has been called, this address is where the MBR will start to forward
  135. * interrupts to after a reset.
  136. *
  137. * To restore default forwarding this function should be called with @ref address set to 0. The
  138. * MBR will then start forwarding interrupts to the address in NFR_UICR->BOOTADDR or to the
  139. * SoftDevice if the BOOTADDR is not set.
  140. *
  141. * On success, this function will not return. It will reset the device.
  142. *
  143. * @retval ::NRF_ERROR_INTERNAL indicates an internal error that should not happen.
  144. * @retval ::NRF_ERROR_INVALID_ADDR if parameter address is outside of the flash size.
  145. * @retval ::NRF_ERROR_NO_MEM if no parameter page is provided (see SoftDevice Specification for more info)
  146. */
  147. typedef struct
  148. {
  149. uint32_t address; /**< The base address of the interrupt vector table for forwarded interrupts.*/
  150. } sd_mbr_command_vector_table_base_set_t;
  151. /**@brief Sets the base address of the interrupt vector table for interrupts forwarded from the MBR
  152. *
  153. * Unlike sd_mbr_command_vector_table_base_set_t, this function does not reset, and it does not
  154. * change where the MBR starts after reset.
  155. *
  156. * @retval ::NRF_SUCCESS
  157. */
  158. typedef struct
  159. {
  160. uint32_t address; /**< The base address of the interrupt vector table for forwarded interrupts.*/
  161. } sd_mbr_command_irq_forward_address_set_t;
  162. /**@brief Input structure containing data used when calling ::sd_mbr_command
  163. *
  164. * Depending on what command value that is set, the corresponding params value type must also be
  165. * set. See @ref NRF_MBR_COMMANDS for command types and corresponding params value type. If command
  166. * @ref SD_MBR_COMMAND_INIT_SD is set, it is not necessary to set any values under params.
  167. */
  168. typedef struct
  169. {
  170. uint32_t command; /**< Type of command to be issued. See @ref NRF_MBR_COMMANDS. */
  171. union
  172. {
  173. sd_mbr_command_copy_sd_t copy_sd; /**< Parameters for copy SoftDevice.*/
  174. sd_mbr_command_compare_t compare; /**< Parameters for verify.*/
  175. sd_mbr_command_copy_bl_t copy_bl; /**< Parameters for copy BootLoader. Requires parameter page. */
  176. sd_mbr_command_vector_table_base_set_t base_set; /**< Parameters for vector table base set. Requires parameter page.*/
  177. sd_mbr_command_irq_forward_address_set_t irq_forward_address_set; /**< Parameters for irq forward address set*/
  178. } params; /**< Command parameters. */
  179. } sd_mbr_command_t;
  180. /** @} */
  181. /** @addtogroup NRF_MBR_FUNCTIONS Functions
  182. * @{ */
  183. /**@brief Issue Master Boot Record commands
  184. *
  185. * Commands used when updating a SoftDevice and bootloader.
  186. *
  187. * The @ref SD_MBR_COMMAND_COPY_BL and @ref SD_MBR_COMMAND_VECTOR_TABLE_BASE_SET requires
  188. * parameters to be retained by the MBR when resetting the IC. This is done in a separate flash
  189. * page provided by the application. The UICR register UICR.NRFFW[1] must be set to an address
  190. * corresponding to a page in the application flash space. This page will be cleared by the MBR and
  191. * used to store the command before reset. When the UICR.NRFFW[1] field is set the page it refers
  192. * to must not be used by the application. If the UICR.NRFFW[1] is set to 0xFFFFFFFF (the default)
  193. * MBR commands which use flash will be unavailable and return @ref NRF_ERROR_NO_MEM.
  194. *
  195. * @param[in] param Pointer to a struct describing the command.
  196. *
  197. * @note For return values, see ::sd_mbr_command_copy_sd_t, ::sd_mbr_command_copy_bl_t,
  198. * ::sd_mbr_command_compare_t, ::sd_mbr_command_vector_table_base_set_t,
  199. * ::sd_mbr_command_irq_forward_address_set_t
  200. *
  201. * @retval ::NRF_ERROR_NO_MEM if UICR.NRFFW[1] is not set (i.e. is 0xFFFFFFFF).
  202. * @retval ::NRF_ERROR_INVALID_PARAM if an invalid command is given.
  203. */
  204. SVCALL(SD_MBR_COMMAND, uint32_t, sd_mbr_command(sd_mbr_command_t* param));
  205. /** @} */
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif // NRF_MBR_H__
  210. /**
  211. @}
  212. */