nrf_bootloader_info.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Copyright (c) 2016 - 2020, 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_bootloader_info Bootloader Information
  43. * @{
  44. * @ingroup nrf_bootloader
  45. */
  46. #ifndef NRF_BOOTLOADER_INFO_H__
  47. #define NRF_BOOTLOADER_INFO_H__
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. #include "app_util.h"
  52. #include "nrf.h"
  53. #include "nrf_mbr.h"
  54. /** @brief Macro for getting the start address of the bootloader image.
  55. *
  56. * The macro is not a compile time symbol. It cannot be used as a
  57. * constant expression, for example, inside a static assert or linker script
  58. * at-placement.
  59. */
  60. #ifndef BOOTLOADER_START_ADDR
  61. #if (__LINT__ == 1)
  62. #define BOOTLOADER_START_ADDR (0x3AC00)
  63. #elif defined(CODE_START)
  64. #define BOOTLOADER_START_ADDR (CODE_START)
  65. #else
  66. #error Not a valid compiler/linker for BOOTLOADER_START_ADDR.
  67. #endif
  68. #endif
  69. /** @brief Macro for getting the size of the bootloader image.
  70. */
  71. #ifndef BOOTLOADER_SIZE
  72. #if (__LINT__ == 1)
  73. #define BOOTLOADER_SIZE (0x6000)
  74. #elif defined ( NRF51 )
  75. #define BOOTLOADER_SIZE (BOOTLOADER_SETTINGS_ADDRESS - BOOTLOADER_START_ADDR)
  76. #elif defined( NRF52_SERIES )
  77. #define BOOTLOADER_SIZE (NRF_MBR_PARAMS_PAGE_ADDRESS - BOOTLOADER_START_ADDR)
  78. #endif
  79. #endif
  80. // The following macros are for accessing the SoftDevice information structure,
  81. // which is found inside the SoftDevice binary.
  82. /** @brief Macro for converting an offset inside the SoftDevice information struct to an absolute address.
  83. */
  84. #define SD_INFO_ABS_OFFSET_GET(baseaddr, offset) ((baseaddr) + (SOFTDEVICE_INFO_STRUCT_OFFSET) + (offset))
  85. /** @brief Macros for reading a byte or a word at a particular offset inside a SoftDevice information struct.
  86. * Use MBR_SIZE as baseaddr when the SoftDevice is installed just above the MBR (the usual case).
  87. */
  88. #define SD_OFFSET_GET_UINT32(baseaddr, offset) (*((uint32_t *) SD_INFO_ABS_OFFSET_GET(baseaddr, offset)))
  89. #define SD_OFFSET_GET_UINT16(baseaddr, offset) (*((uint16_t *) SD_INFO_ABS_OFFSET_GET(baseaddr, offset)))
  90. #define SD_OFFSET_GET_UINT8(baseaddr, offset) (*((uint8_t *) SD_INFO_ABS_OFFSET_GET(baseaddr, offset)))
  91. #if defined(BLE_STACK_SUPPORT_REQD) || defined(ANT_STACK_SUPPORT_REQD)
  92. #include "nrf_sdm.h"
  93. #else
  94. /** @brief The offset inside the SoftDevice at which the information struct is placed.
  95. * To see the layout of the information struct, see the SoftDevice specification.
  96. */
  97. #define SOFTDEVICE_INFO_STRUCT_OFFSET (0x2000)
  98. #define SD_INFO_STRUCT_SIZE(baseaddr) SD_OFFSET_GET_UINT8(baseaddr, 0x00)
  99. /** @brief Macro for reading the size of a SoftDevice at a given base address.
  100. */
  101. #ifndef SD_SIZE_GET
  102. #define SD_SIZE_GET(baseaddr) SD_OFFSET_GET_UINT32(baseaddr, 0x08)
  103. #endif
  104. /** @brief Macro for reading the version of a SoftDevice at a given base address.
  105. * This expression checks the length of the information struct to see if the version is present.
  106. * The version number is constructed like this:
  107. * major_version * 1000000 + minor_version * 1000 + bugfix_version
  108. */
  109. #ifndef SD_VERSION_GET
  110. #define SD_VERSION_GET(baseaddr) ((SD_INFO_STRUCT_SIZE(baseaddr) > (0x14)) \
  111. ? SD_OFFSET_GET_UINT32(baseaddr, 0x14) \
  112. : 0)
  113. #endif
  114. /** @brief Defines a macro for retrieving the actual SoftDevice ID from a given base address. Use
  115. * @ref MBR_SIZE as the argument when the SoftDevice is installed just above the MBR (the
  116. * usual case). */
  117. #ifndef SD_ID_GET
  118. #define SD_ID_GET(baseaddr) ((SD_INFO_STRUCT_SIZE(baseaddr) > 0x10) \
  119. ? SD_OFFSET_GET_UINT32(baseaddr, 0x10) : 0)
  120. #endif
  121. #endif
  122. /** @brief Macro for reading the magic number of a SoftDevice at a given base address.
  123. */
  124. #ifndef SD_MAGIC_NUMBER_GET
  125. #define SD_MAGIC_NUMBER_GET(baseaddr) SD_OFFSET_GET_UINT32(baseaddr, 0x04)
  126. #endif
  127. /** @brief Macro for getting the absolute address of the magic number.
  128. */
  129. #define SD_MAGIC_NUMBER_ABS_OFFSET_GET(baseaddr) SD_INFO_ABS_OFFSET_GET(baseaddr, 0x04)
  130. /** @brief The number present at a specific location in all SoftDevices.
  131. */
  132. #define SD_MAGIC_NUMBER ((uint32_t)0x51B1E5DB)
  133. /** @brief Whether a SoftDevice is at its regular location.
  134. */
  135. #ifndef SD_PRESENT
  136. #define SD_PRESENT ((SD_MAGIC_NUMBER_GET(MBR_SIZE)) == (SD_MAGIC_NUMBER))
  137. #endif
  138. /** @brief The multiplier for the major version of the SoftDevice. See \ref SD_VERSION_GET
  139. */
  140. #define SD_MAJOR_VERSION_MULTIPLIER (1000000)
  141. /** @brief Read the major version of the SoftDevice from the raw version number. See \ref SD_VERSION_GET.
  142. */
  143. #define SD_MAJOR_VERSION_EXTRACT(raw_version) ((raw_version)/SD_MAJOR_VERSION_MULTIPLIER)
  144. #define BOOTLOADER_DFU_GPREGRET_MASK (0xF8) /**< Mask for GPGPREGRET bits used for the magic pattern written to GPREGRET register to signal between main app and DFU. */
  145. #define BOOTLOADER_DFU_GPREGRET (0xB0) /**< Magic pattern written to GPREGRET register to signal between main app and DFU. The 3 lower bits are assumed to be used for signalling purposes.*/
  146. #define BOOTLOADER_DFU_START_BIT_MASK (0x01) /**< Bit mask to signal from main application to enter DFU mode using a buttonless service. */
  147. #define BOOTLOADER_DFU_GPREGRET2_MASK (0xF8) /**< Mask for GPGPREGRET2 bits used for the magic pattern written to GPREGRET2 register to signal between main app and DFU. */
  148. #define BOOTLOADER_DFU_GPREGRET2 (0xA8) /**< Magic pattern written to GPREGRET2 register to signal between main app and DFU. The 3 lower bits are assumed to be used for signalling purposes.*/
  149. #define BOOTLOADER_DFU_SKIP_CRC_BIT_MASK (0x01) /**< Bit mask to signal from main application that CRC-check is not needed for image verification. */
  150. #define BOOTLOADER_DFU_START (BOOTLOADER_DFU_GPREGRET | BOOTLOADER_DFU_START_BIT_MASK) /**< Magic number to signal that bootloader should enter DFU mode because of signal from Buttonless DFU in main app.*/
  151. #define BOOTLOADER_DFU_SKIP_CRC (BOOTLOADER_DFU_GPREGRET2 | BOOTLOADER_DFU_SKIP_CRC_BIT_MASK) /**< Magic number to signal that CRC can be skipped due to low power modes.*/
  152. /** @brief Macro based on @c NRF_DFU_DEBUG_VERSION that can be checked for true/false instead of defined/not defined.
  153. */
  154. #ifndef NRF_DFU_DEBUG
  155. #ifdef NRF_DFU_DEBUG_VERSION
  156. #define NRF_DFU_DEBUG 1
  157. #else
  158. #define NRF_DFU_DEBUG 0
  159. #endif
  160. #endif
  161. /** @brief Function for populating addresses in the MBR code page.
  162. *
  163. * This function writes two words to flash if the flash is 0xFFFFFFFF. This is done in code because
  164. * doing this through the hex file interferes with flashing algorithms. See nrf_mbr.h.
  165. */
  166. void nrf_bootloader_mbr_addrs_populate(void);
  167. /** @brief Function for checking if the debug port access is disabled.
  168. *
  169. * If the debug port access is enabled, disable it. This function checks and writes to the UICR
  170. * registers APPROTECT and DEBUGCTRL.
  171. */
  172. void nrf_bootloader_debug_port_disable(void);
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. #endif // #ifndef NRF_BOOTLOADER_INFO_H__
  177. /** @} */