nrf_bootloader_info.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Copyright (c) 2016 - 2019, 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. /**
  81. * @brief Location of the pointer to the start of the bootloader.
  82. *
  83. * See also @c BOOTLOADER_ADDRESS in @c app_util.h.
  84. */
  85. #define NRF_UICR_BOOTLOADER_START_ADDRESS (MBR_BOOTLOADER_ADDR)
  86. // The following macros are for accessing the SoftDevice information structure,
  87. // which is found inside the SoftDevice binary.
  88. /** @brief Macro for converting an offset inside the SoftDevice information struct to an absolute address.
  89. */
  90. #define SD_INFO_ABS_OFFSET_GET(baseaddr, offset) ((baseaddr) + (SOFTDEVICE_INFO_STRUCT_OFFSET) + (offset))
  91. /** @brief Macros for reading a byte or a word at a particular offset inside a SoftDevice information struct.
  92. * Use MBR_SIZE as baseaddr when the SoftDevice is installed just above the MBR (the usual case).
  93. */
  94. #define SD_OFFSET_GET_UINT32(baseaddr, offset) (*((uint32_t *) SD_INFO_ABS_OFFSET_GET(baseaddr, offset)))
  95. #define SD_OFFSET_GET_UINT16(baseaddr, offset) (*((uint16_t *) SD_INFO_ABS_OFFSET_GET(baseaddr, offset)))
  96. #define SD_OFFSET_GET_UINT8(baseaddr, offset) (*((uint8_t *) SD_INFO_ABS_OFFSET_GET(baseaddr, offset)))
  97. #if defined(BLE_STACK_SUPPORT_REQD) || defined(ANT_STACK_SUPPORT_REQD)
  98. #include "nrf_sdm.h"
  99. #else
  100. /** @brief The offset inside the SoftDevice at which the information struct is placed.
  101. * To see the layout of the information struct, see the SoftDevice specification.
  102. */
  103. #define SOFTDEVICE_INFO_STRUCT_OFFSET (0x2000)
  104. #define SD_INFO_STRUCT_SIZE(baseaddr) SD_OFFSET_GET_UINT8(baseaddr, 0x00)
  105. /** @brief Macro for reading the size of a SoftDevice at a given base address.
  106. */
  107. #ifndef SD_SIZE_GET
  108. #define SD_SIZE_GET(baseaddr) SD_OFFSET_GET_UINT32(baseaddr, 0x08)
  109. #endif
  110. /** @brief Macro for reading the version of a SoftDevice at a given base address.
  111. * This expression checks the length of the information struct to see if the version is present.
  112. * The version number is constructed like this:
  113. * major_version * 1000000 + minor_version * 1000 + bugfix_version
  114. */
  115. #ifndef SD_VERSION_GET
  116. #define SD_VERSION_GET(baseaddr) ((SD_INFO_STRUCT_SIZE(baseaddr) > (0x14)) \
  117. ? SD_OFFSET_GET_UINT32(baseaddr, 0x14) \
  118. : 0)
  119. #endif
  120. #endif
  121. /** @brief Macro for reading the magic number of a SoftDevice at a given base address.
  122. */
  123. #ifndef SD_MAGIC_NUMBER_GET
  124. #define SD_MAGIC_NUMBER_GET(baseaddr) SD_OFFSET_GET_UINT32(baseaddr, 0x04)
  125. #endif
  126. /** @brief Macro for getting the absolute address of the magic number.
  127. */
  128. #define SD_MAGIC_NUMBER_ABS_OFFSET_GET(baseaddr) SD_INFO_ABS_OFFSET_GET(baseaddr, 0x04)
  129. /** @brief The number present at a specific location in all SoftDevices.
  130. */
  131. #define SD_MAGIC_NUMBER ((uint32_t)0x51B1E5DB)
  132. /** @brief Whether a SoftDevice is at its regular location.
  133. */
  134. #ifndef SD_PRESENT
  135. #define SD_PRESENT ((SD_MAGIC_NUMBER_GET(MBR_SIZE)) == (SD_MAGIC_NUMBER))
  136. #endif
  137. /** @brief The multiplier for the major version of the SoftDevice. See \ref SD_VERSION_GET
  138. */
  139. #define SD_MAJOR_VERSION_MULTIPLIER (1000000)
  140. /** @brief Read the major version of the SoftDevice from the raw version number. See \ref SD_VERSION_GET.
  141. */
  142. #define SD_MAJOR_VERSION_EXTRACT(raw_version) ((raw_version)/SD_MAJOR_VERSION_MULTIPLIER)
  143. #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. */
  144. #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.*/
  145. #define BOOTLOADER_DFU_START_BIT_MASK (0x01) /**< Bit mask to signal from main application to enter DFU mode using a buttonless service. */
  146. #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. */
  147. #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.*/
  148. #define BOOTLOADER_DFU_SKIP_CRC_BIT_MASK (0x01) /**< Bit mask to signal from main application that CRC-check is not needed for image verification. */
  149. #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.*/
  150. #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.*/
  151. /** @brief Macro based on @c NRF_DFU_DEBUG_VERSION that can be checked for true/false instead of defined/not defined.
  152. */
  153. #ifndef NRF_DFU_DEBUG
  154. #ifdef NRF_DFU_DEBUG_VERSION
  155. #define NRF_DFU_DEBUG 1
  156. #else
  157. #define NRF_DFU_DEBUG 0
  158. #endif
  159. #endif
  160. #ifdef __cplusplus
  161. }
  162. #endif
  163. #endif // #ifndef NRF_BOOTLOADER_INFO_H__
  164. /** @} */