nrf_bootloader_info.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * Copyright (c) 2016 - 2018, 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 Bootloader start address in UICR.
  82. *
  83. * Register location in UICR where the bootloader start address is stored.
  84. *
  85. * @note If the value at the given location is 0xFFFFFFFF, the bootloader address is not set.
  86. */
  87. #define NRF_UICR_BOOTLOADER_START_ADDRESS (NRF_UICR_BASE + 0x14)
  88. // The following macros are for accessing the SoftDevice information structure,
  89. // which is found inside the SoftDevice binary.
  90. /** @brief Macro for converting an offset inside the SoftDevice information struct to an absolute address.
  91. */
  92. #define SD_INFO_ABS_OFFSET_GET(baseaddr, offset) ((baseaddr) + (SOFTDEVICE_INFO_STRUCT_OFFSET) + (offset))
  93. /** @brief Macros for reading a byte or a word at a particular offset inside a SoftDevice information struct.
  94. * Use MBR_SIZE as baseaddr when the SoftDevice is installed just above the MBR (the usual case).
  95. */
  96. #define SD_OFFSET_GET_UINT32(baseaddr, offset) (*((uint32_t *) SD_INFO_ABS_OFFSET_GET(baseaddr, offset)))
  97. #define SD_OFFSET_GET_UINT16(baseaddr, offset) (*((uint16_t *) SD_INFO_ABS_OFFSET_GET(baseaddr, offset)))
  98. #define SD_OFFSET_GET_UINT8(baseaddr, offset) (*((uint8_t *) SD_INFO_ABS_OFFSET_GET(baseaddr, offset)))
  99. #ifdef BLE_STACK_SUPPORT_REQD
  100. #include "nrf_sdm.h"
  101. #else
  102. /** @brief The offset inside the SoftDevice at which the information struct is placed.
  103. * To see the layout of the information struct, see the SoftDevice specification.
  104. */
  105. #define SOFTDEVICE_INFO_STRUCT_OFFSET (0x2000)
  106. #define SD_INFO_STRUCT_SIZE(baseaddr) SD_OFFSET_GET_UINT8(baseaddr, 0x00)
  107. /** @brief Macro for reading the size of a SoftDevice at a given base address.
  108. */
  109. #ifndef SD_SIZE_GET
  110. #define SD_SIZE_GET(baseaddr) SD_OFFSET_GET_UINT32(baseaddr, 0x08)
  111. #endif
  112. /** @brief Macro for reading the version of a SoftDevice at a given base address.
  113. * This expression checks the length of the information struct to see if the version is present.
  114. * The version number is constructed like this:
  115. * major_version * 1000000 + minor_version * 1000 + bugfix_version
  116. */
  117. #ifndef SD_VERSION_GET
  118. #define SD_VERSION_GET(baseaddr) ((SD_INFO_STRUCT_SIZE(baseaddr) > (0x14)) \
  119. ? SD_OFFSET_GET_UINT32(baseaddr, 0x14) \
  120. : 0)
  121. #endif
  122. #endif
  123. /** @brief Macro for reading the magic number of a SoftDevice at a given base address.
  124. */
  125. #ifndef SD_MAGIC_NUMBER_GET
  126. #define SD_MAGIC_NUMBER_GET(baseaddr) SD_OFFSET_GET_UINT32(baseaddr, 0x04)
  127. #endif
  128. /** @brief Macro for getting the absolute address of the magic number.
  129. */
  130. #define SD_MAGIC_NUMBER_ABS_OFFSET_GET(baseaddr) SD_INFO_ABS_OFFSET_GET(baseaddr, 0x04)
  131. /** @brief The number present at a specific location in all SoftDevices.
  132. */
  133. #define SD_MAGIC_NUMBER ((uint32_t)0x51B1E5DB)
  134. /** @brief Whether a SoftDevice is at its regular location.
  135. */
  136. #ifndef SD_PRESENT
  137. #define SD_PRESENT ((SD_MAGIC_NUMBER_GET(MBR_SIZE)) == (SD_MAGIC_NUMBER))
  138. #endif
  139. /** @brief The multiplier for the major version of the SoftDevice. See \ref SD_VERSION_GET
  140. */
  141. #define SD_MAJOR_VERSION_MULTIPLIER (1000000)
  142. /** @brief Read the major version of the SoftDevice from the raw version number. See \ref SD_VERSION_GET.
  143. */
  144. #define SD_MAJOR_VERSION_EXTRACT(raw_version) ((raw_version)/SD_MAJOR_VERSION_MULTIPLIER)
  145. #define BOOTLOADER_DFU_GPREGRET_MASK (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 (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_MASK | 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_MASK | 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. /** @} */