nrf_mbr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. /** @brief Location (in the flash memory) of the bootloader address. */
  60. #define MBR_BOOTLOADER_ADDR (0xFF8)
  61. /** @brief Location (in UICR) of the bootloader address. */
  62. #define MBR_UICR_BOOTLOADER_ADDR (&(NRF_UICR->NRFFW[0]))
  63. /** @brief Location (in the flash memory) of the address of the MBR parameter page. */
  64. #define MBR_PARAM_PAGE_ADDR (0xFFC)
  65. /** @brief Location (in UICR) of the address of the MBR parameter page. */
  66. #define MBR_UICR_PARAM_PAGE_ADDR (&(NRF_UICR->NRFFW[1]))
  67. /** @} */
  68. /** @addtogroup NRF_MBR_ENUMS Enumerations
  69. * @{ */
  70. /**@brief nRF Master Boot Record API SVC numbers. */
  71. enum NRF_MBR_SVCS
  72. {
  73. SD_MBR_COMMAND = MBR_SVC_BASE, /**< ::sd_mbr_command */
  74. };
  75. /**@brief Possible values for ::sd_mbr_command_t.command */
  76. enum NRF_MBR_COMMANDS
  77. {
  78. SD_MBR_COMMAND_COPY_BL, /**< Copy a new BootLoader. @see ::sd_mbr_command_copy_bl_t*/
  79. SD_MBR_COMMAND_COPY_SD, /**< Copy a new SoftDevice. @see ::sd_mbr_command_copy_sd_t*/
  80. 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.*/
  81. SD_MBR_COMMAND_COMPARE, /**< This command works like memcmp. @see ::sd_mbr_command_compare_t*/
  82. 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*/
  83. SD_MBR_COMMAND_RESERVED,
  84. SD_MBR_COMMAND_IRQ_FORWARD_ADDRESS_SET, /**< Start forwarding all interrupts to this address. @see ::sd_mbr_command_irq_forward_address_set_t*/
  85. };
  86. /** @} */
  87. /** @addtogroup NRF_MBR_TYPES Types
  88. * @{ */
  89. /**@brief This command copies part of a new SoftDevice
  90. *
  91. * The destination area is erased before copying.
  92. * If dst is in the middle of a flash page, that whole flash page will be erased.
  93. * If (dst+len) is in the middle of a flash page, that whole flash page will be erased.
  94. *
  95. * The user of this function is responsible for setting the BPROT registers.
  96. *
  97. * @retval ::NRF_SUCCESS indicates that the contents of the memory blocks where copied correctly.
  98. * @retval ::NRF_ERROR_INTERNAL indicates that the contents of the memory blocks where not verified correctly after copying.
  99. */
  100. typedef struct
  101. {
  102. uint32_t *src; /**< Pointer to the source of data to be copied.*/
  103. uint32_t *dst; /**< Pointer to the destination where the content is to be copied.*/
  104. uint32_t len; /**< Number of 32 bit words to copy. Must be a multiple of @ref MBR_PAGE_SIZE_IN_WORDS words.*/
  105. } sd_mbr_command_copy_sd_t;
  106. /**@brief This command works like memcmp, but takes the length in words.
  107. *
  108. * @retval ::NRF_SUCCESS indicates that the contents of both memory blocks are equal.
  109. * @retval ::NRF_ERROR_NULL indicates that the contents of the memory blocks are not equal.
  110. */
  111. typedef struct
  112. {
  113. uint32_t *ptr1; /**< Pointer to block of memory. */
  114. uint32_t *ptr2; /**< Pointer to block of memory. */
  115. uint32_t len; /**< Number of 32 bit words to compare.*/
  116. } sd_mbr_command_compare_t;
  117. /**@brief This command copies a new BootLoader.
  118. *
  119. * The MBR assumes that either @ref MBR_BOOTLOADER_ADDR or @ref MBR_UICR_BOOTLOADER_ADDR is set to
  120. * the address where the bootloader will be copied. If both addresses are set, the MBR will prioritize
  121. * @ref MBR_BOOTLOADER_ADDR.
  122. *
  123. * The bootloader destination is erased by this function.
  124. * If (destination+bl_len) is in the middle of a flash page, that whole flash page will be erased.
  125. *
  126. * This command requires that @ref MBR_PARAM_PAGE_ADDR or @ref MBR_UICR_PARAM_PAGE_ADDR is set,
  127. * see @ref sd_mbr_command.
  128. *
  129. * This command will use the flash protect peripheral (BPROT or ACL) to protect the flash that is
  130. * not intended to be written.
  131. *
  132. * On success, this function will not return. It will start the new bootloader from reset-vector as normal.
  133. *
  134. * @retval ::NRF_ERROR_INTERNAL indicates an internal error that should not happen.
  135. * @retval ::NRF_ERROR_FORBIDDEN if the bootloader address is not set.
  136. * @retval ::NRF_ERROR_INVALID_LENGTH if parameters attempts to read or write outside flash area.
  137. * @retval ::NRF_ERROR_NO_MEM No MBR parameter page is provided. See @ref sd_mbr_command.
  138. */
  139. typedef struct
  140. {
  141. uint32_t *bl_src; /**< Pointer to the source of the bootloader to be be copied.*/
  142. uint32_t bl_len; /**< Number of 32 bit words to copy for BootLoader. */
  143. } sd_mbr_command_copy_bl_t;
  144. /**@brief Change the address the MBR starts after a reset
  145. *
  146. * Once this function has been called, this address is where the MBR will start to forward
  147. * interrupts to after a reset.
  148. *
  149. * To restore default forwarding, this function should be called with @ref address set to 0. If a
  150. * bootloader is present, interrupts will be forwarded to the bootloader. If not, interrupts will
  151. * be forwarded to the SoftDevice.
  152. *
  153. * The location of a bootloader can be specified in @ref MBR_BOOTLOADER_ADDR or
  154. * @ref MBR_UICR_BOOTLOADER_ADDR. If both addresses are set, the MBR will prioritize
  155. * @ref MBR_BOOTLOADER_ADDR.
  156. *
  157. * This command requires that @ref MBR_PARAM_PAGE_ADDR or @ref MBR_UICR_PARAM_PAGE_ADDR is set,
  158. * see @ref sd_mbr_command.
  159. *
  160. * On success, this function will not return. It will reset the device.
  161. *
  162. * @retval ::NRF_ERROR_INTERNAL indicates an internal error that should not happen.
  163. * @retval ::NRF_ERROR_INVALID_ADDR if parameter address is outside of the flash size.
  164. * @retval ::NRF_ERROR_NO_MEM No MBR parameter page is provided. See @ref sd_mbr_command.
  165. */
  166. typedef struct
  167. {
  168. uint32_t address; /**< The base address of the interrupt vector table for forwarded interrupts.*/
  169. } sd_mbr_command_vector_table_base_set_t;
  170. /**@brief Sets the base address of the interrupt vector table for interrupts forwarded from the MBR
  171. *
  172. * Unlike sd_mbr_command_vector_table_base_set_t, this function does not reset, and it does not
  173. * change where the MBR starts after reset.
  174. *
  175. * @retval ::NRF_SUCCESS
  176. */
  177. typedef struct
  178. {
  179. uint32_t address; /**< The base address of the interrupt vector table for forwarded interrupts.*/
  180. } sd_mbr_command_irq_forward_address_set_t;
  181. /**@brief Input structure containing data used when calling ::sd_mbr_command
  182. *
  183. * Depending on what command value that is set, the corresponding params value type must also be
  184. * set. See @ref NRF_MBR_COMMANDS for command types and corresponding params value type. If command
  185. * @ref SD_MBR_COMMAND_INIT_SD is set, it is not necessary to set any values under params.
  186. */
  187. typedef struct
  188. {
  189. uint32_t command; /**< Type of command to be issued. See @ref NRF_MBR_COMMANDS. */
  190. union
  191. {
  192. sd_mbr_command_copy_sd_t copy_sd; /**< Parameters for copy SoftDevice.*/
  193. sd_mbr_command_compare_t compare; /**< Parameters for verify.*/
  194. sd_mbr_command_copy_bl_t copy_bl; /**< Parameters for copy BootLoader. Requires parameter page. */
  195. sd_mbr_command_vector_table_base_set_t base_set; /**< Parameters for vector table base set. Requires parameter page.*/
  196. sd_mbr_command_irq_forward_address_set_t irq_forward_address_set; /**< Parameters for irq forward address set*/
  197. } params; /**< Command parameters. */
  198. } sd_mbr_command_t;
  199. /** @} */
  200. /** @addtogroup NRF_MBR_FUNCTIONS Functions
  201. * @{ */
  202. /**@brief Issue Master Boot Record commands
  203. *
  204. * Commands used when updating a SoftDevice and bootloader.
  205. *
  206. * The @ref SD_MBR_COMMAND_COPY_BL and @ref SD_MBR_COMMAND_VECTOR_TABLE_BASE_SET requires
  207. * parameters to be retained by the MBR when resetting the IC. This is done in a separate flash
  208. * page. The location of the flash page should be provided by the application in either
  209. * @ref MBR_PARAM_PAGE_ADDR or @ref MBR_UICR_PARAM_PAGE_ADDR. If both addresses are set, the MBR
  210. * will prioritize @ref MBR_PARAM_PAGE_ADDR. This page will be cleared by the MBR and is used to
  211. * store the command before reset. When an address is specified, the page it refers to must not be
  212. * used by the application. If no address is provided by the application, i.e. both
  213. * @ref MBR_PARAM_PAGE_ADDR and @ref MBR_UICR_PARAM_PAGE_ADDR is 0xFFFFFFFF, MBR commands which use
  214. * flash will be unavailable and return @ref NRF_ERROR_NO_MEM.
  215. *
  216. * @param[in] param Pointer to a struct describing the command.
  217. *
  218. * @note For a complete set of return values, see ::sd_mbr_command_copy_sd_t,
  219. * ::sd_mbr_command_copy_bl_t, ::sd_mbr_command_compare_t,
  220. * ::sd_mbr_command_vector_table_base_set_t, ::sd_mbr_command_irq_forward_address_set_t
  221. *
  222. * @retval ::NRF_ERROR_NO_MEM No MBR parameter page provided
  223. * @retval ::NRF_ERROR_INVALID_PARAM if an invalid command is given.
  224. */
  225. SVCALL(SD_MBR_COMMAND, uint32_t, sd_mbr_command(sd_mbr_command_t* param));
  226. /** @} */
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif // NRF_MBR_H__
  231. /**
  232. @}
  233. */