nrf_mbr.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 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 must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
  16. * contributors to this software may be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * 4. This software must only be used in a processor manufactured by Nordic
  20. * Semiconductor ASA, or in a processor manufactured by a third party that
  21. * is used in combination with a processor manufactured by Nordic Semiconductor.
  22. *
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  26. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  28. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  29. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  30. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  31. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. /**
  37. @defgroup nrf_mbr_api Master Boot Record API
  38. @{
  39. @brief APIs for updating SoftDevice and BootLoader
  40. */
  41. /* Header guard */
  42. #ifndef NRF_MBR_H__
  43. #define NRF_MBR_H__
  44. #include "nrf_svc.h"
  45. #include <stdint.h>
  46. #ifndef NRF51
  47. #error "This header file shall only be included for nRF51 projects"
  48. #endif
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52. /** @addtogroup NRF_MBR_DEFINES Defines
  53. * @{ */
  54. /**@brief MBR SVC Base number. */
  55. #define MBR_SVC_BASE (0x18)
  56. /**@brief Page size in words. */
  57. #define PAGE_SIZE_IN_WORDS 256
  58. /** @} */
  59. /** @brief The size that must be reserved for the MBR when a softdevice is written to flash.
  60. This is the offset where the first byte of the softdevice hex file is written.*/
  61. #define MBR_SIZE (0x1000)
  62. /** @addtogroup NRF_MBR_ENUMS Enumerations
  63. * @{ */
  64. /**@brief nRF Master Boot Record API SVC numbers. */
  65. enum NRF_MBR_SVCS
  66. {
  67. SD_MBR_COMMAND = MBR_SVC_BASE, /**< ::sd_mbr_command */
  68. };
  69. /**@brief Possible values for ::sd_mbr_command_t.command */
  70. enum NRF_MBR_COMMANDS
  71. {
  72. SD_MBR_COMMAND_COPY_BL, /**< Copy a new BootLoader. @see sd_mbr_command_copy_bl_t */
  73. SD_MBR_COMMAND_COPY_SD, /**< Copy a new SoftDevice. @see ::sd_mbr_command_copy_sd_t*/
  74. SD_MBR_COMMAND_INIT_SD, /**< Init forwarding interrupts to SD, and run reset function in SD*/
  75. SD_MBR_COMMAND_COMPARE, /**< This command works like memcmp. @see ::sd_mbr_command_compare_t*/
  76. SD_MBR_COMMAND_VECTOR_TABLE_BASE_SET, /**< Start forwarding all exception to this address @see ::sd_mbr_command_vector_table_base_set_t*/
  77. };
  78. /** @} */
  79. /** @addtogroup NRF_MBR_TYPES Types
  80. * @{ */
  81. /**@brief This command copies part of a new SoftDevice
  82. * The destination area is erased before copying.
  83. * If dst is in the middle of a flash page, that whole flash page will be erased.
  84. * If (dst+len) is in the middle of a flash page, that whole flash page will be erased.
  85. *
  86. * The user of this function is responsible for setting the PROTENSET registers.
  87. *
  88. * @retval ::NRF_SUCCESS indicates that the contents of the memory blocks where copied correctly.
  89. * @retval ::NRF_ERROR_INTERNAL indicates that the contents of the memory blocks where not verified correctly after copying.
  90. */
  91. typedef struct
  92. {
  93. uint32_t *src; /**< Pointer to the source of data to be copied.*/
  94. uint32_t *dst; /**< Pointer to the destination where the content is to be copied.*/
  95. uint32_t len; /**< Number of 32 bit words to copy. Must be a multiple of @ref PAGE_SIZE_IN_WORDS words.*/
  96. } sd_mbr_command_copy_sd_t;
  97. /**@brief This command works like memcmp, but takes the length in words.
  98. *
  99. * @retval ::NRF_SUCCESS indicates that the contents of both memory blocks are equal.
  100. * @retval ::NRF_ERROR_NULL indicates that the contents of the memory blocks are not equal.
  101. */
  102. typedef struct
  103. {
  104. uint32_t *ptr1; /**< Pointer to block of memory. */
  105. uint32_t *ptr2; /**< Pointer to block of memory. */
  106. uint32_t len; /**< Number of 32 bit words to compare.*/
  107. } sd_mbr_command_compare_t;
  108. /**@brief This command copies a new BootLoader.
  109. * With this command, destination of BootLoader is always the address written in NRF_UICR->BOOTADDR.
  110. *
  111. * Destination is erased by this function.
  112. * If (destination+bl_len) is in the middle of a flash page, that whole flash page will be erased.
  113. *
  114. * This function will use PROTENSET to protect the flash that is not intended to be written.
  115. *
  116. * On Success, this function will not return. It will start the new BootLoader from reset-vector as normal.
  117. *
  118. * @retval ::NRF_ERROR_INTERNAL indicates an internal error that should not happen.
  119. * @retval ::NRF_ERROR_FORBIDDEN if NRF_UICR->BOOTADDR is not set.
  120. * @retval ::NRF_ERROR_INVALID_LENGTH if parameters attempts to read or write outside flash area.
  121. */
  122. typedef struct
  123. {
  124. uint32_t *bl_src; /**< Pointer to the source of the Bootloader to be be copied.*/
  125. uint32_t bl_len; /**< Number of 32 bit words to copy for BootLoader. */
  126. } sd_mbr_command_copy_bl_t;
  127. /**@brief Sets the base address of the interrupt vector table for interrupts forwarded from the MBR
  128. *
  129. * Once this function has been called, this address is where the MBR will start to forward interrupts to after a reset.
  130. *
  131. * To restore default forwarding this function should be called with @param address set to 0.
  132. * The MBR will then start forwarding to interrupts to the address in NFR_UICR->BOOTADDR or to the SoftDevice if the BOOTADDR is not set.
  133. *
  134. * @retval ::NRF_ERROR_INTERNAL indicates an internal error that should not happen.
  135. * @retval ::NRF_ERROR_INVALID_ADDR if parameter address is outside of the flash size.
  136. */
  137. typedef struct
  138. {
  139. uint32_t address; /**< The base address of the interrupt vector table for forwarded interrupts.*/
  140. } sd_mbr_command_vector_table_base_set_t;
  141. typedef struct
  142. {
  143. uint32_t command; /**< type of command to be issued see @ref NRF_MBR_COMMANDS. */
  144. union
  145. {
  146. sd_mbr_command_copy_sd_t copy_sd; /**< Parameters for copy SoftDevice.*/
  147. sd_mbr_command_copy_bl_t copy_bl; /**< Parameters for copy BootLoader.*/
  148. sd_mbr_command_compare_t compare; /**< Parameters for verify.*/
  149. sd_mbr_command_vector_table_base_set_t base_set; /**< Parameters for vector table base set.*/
  150. } params;
  151. } sd_mbr_command_t;
  152. /** @} */
  153. /** @addtogroup NRF_MBR_FUNCTIONS Functions
  154. * @{ */
  155. /**@brief Issue Master Boot Record commands
  156. *
  157. * Commands used when updating a SoftDevice and bootloader.
  158. *
  159. * @param[in] param Pointer to a struct describing the command.
  160. *
  161. *@note for retvals see ::sd_mbr_command_copy_sd_t ::sd_mbr_command_copy_bl_t ::sd_mbr_command_compare_t
  162. */
  163. SVCALL(SD_MBR_COMMAND, uint32_t, sd_mbr_command(sd_mbr_command_t* param));
  164. /** @} */
  165. #ifdef __cplusplus
  166. }
  167. #endif
  168. #endif // NRF_MBR_H__
  169. /**
  170. @}
  171. */