nrf_bootloader_app_start_final.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #include "sdk_config.h"
  41. #include "nrf_bootloader_app_start.h"
  42. #include <stdint.h>
  43. #include "nrf.h"
  44. #include "nrf_peripherals.h"
  45. #include "nrf_bootloader_info.h"
  46. #include "nrf_dfu_types.h"
  47. #include "nrf_dfu_utils.h"
  48. #include "nrf_dfu_settings.h"
  49. #include "nrf_assert.h"
  50. #include "nrf_log.h"
  51. #include "sdk_config.h"
  52. // Enabling the NRF_BOOTLOADER_READ_PROTECT define is untested.
  53. // Read-protecting the bootloader requires certain functions to run from RAM.
  54. // In GCC and SES this is done automatically when the define is enabled. You will
  55. // get warnings which can be ignored.
  56. // In Keil you must change project settings to run the entire file from RAM.
  57. #ifndef NRF_BOOTLOADER_READ_PROTECT
  58. #define NRF_BOOTLOADER_READ_PROTECT 0
  59. #endif
  60. #define HANDLER_MODE_EXIT 0xFFFFFFF9 // When this is jumped to, the CPU will exit interrupt context
  61. // (handler mode), and pop values from the stack into registers.
  62. // See ARM's documentation for "Exception entry and return".
  63. #define EXCEPTION_STACK_WORD_COUNT 8 // The number of words popped from the stack when
  64. // HANDLER_MODE_EXIT is branched to.
  65. /**@brief Function that sets the stack pointer and link register, and starts executing a particular address.
  66. *
  67. * @param[in] new_msp The new value to set in the main stack pointer.
  68. * @param[in] new_lr The new value to set in the link register.
  69. * @param[in] addr The address to execute.
  70. */
  71. #if defined ( __CC_ARM )
  72. __ASM __STATIC_INLINE void jump_to_addr(uint32_t new_msp, uint32_t new_lr, uint32_t addr)
  73. {
  74. MSR MSP, R0;
  75. MOV LR, R1;
  76. BX R2;
  77. }
  78. #else
  79. __STATIC_INLINE void jump_to_addr(uint32_t new_msp, uint32_t new_lr, uint32_t addr)
  80. {
  81. __ASM volatile ("MSR MSP, %[arg]" : : [arg] "r" (new_msp));
  82. __ASM volatile ("MOV LR, %[arg]" : : [arg] "r" (new_lr) : "lr");
  83. __ASM volatile ("BX %[arg]" : : [arg] "r" (addr));
  84. }
  85. #endif
  86. /**@brief Function for booting an app as if the chip was reset.
  87. *
  88. * @param[in] vector_table_addr The address of the app's vector table.
  89. */
  90. __STATIC_INLINE void app_start(uint32_t vector_table_addr)
  91. {
  92. const uint32_t current_isr_num = (__get_IPSR() & IPSR_ISR_Msk);
  93. const uint32_t new_msp = *((uint32_t *)(vector_table_addr)); // The app's Stack Pointer is found as the first word of the vector table.
  94. const uint32_t reset_handler = *((uint32_t *)(vector_table_addr + sizeof(uint32_t))); // The app's Reset Handler is found as the second word of the vector table.
  95. const uint32_t new_lr = 0xFFFFFFFF;
  96. __set_CONTROL(0x00000000); // Set CONTROL to its reset value 0.
  97. __set_PRIMASK(0x00000000); // Set PRIMASK to its reset value 0.
  98. __set_BASEPRI(0x00000000); // Set BASEPRI to its reset value 0.
  99. __set_FAULTMASK(0x00000000); // Set FAULTMASK to its reset value 0.
  100. ASSERT(current_isr_num == 0); // If this is triggered, the CPU is currently in an interrupt.
  101. // The CPU is in Thread mode (main context).
  102. jump_to_addr(new_msp, new_lr, reset_handler); // Jump directly to the App's Reset Handler.
  103. }
  104. #if NRF_BOOTLOADER_READ_PROTECT
  105. #ifdef __ICCARM__
  106. __ramfunc
  107. #elif defined ( __GNUC__ ) || defined ( __SES_ARM )
  108. __attribute__((noinline, long_call, section(".data")))
  109. #elif defined ( __CC_ARM )
  110. #warning "Keil requires changes to project settings to run this file from RAM. Ignore this warning if configuration has been made."
  111. #endif
  112. #endif
  113. ret_code_t nrf_bootloader_flash_protect(uint32_t address, uint32_t size, bool read_protect)
  114. {
  115. if ((size & (CODE_PAGE_SIZE - 1)) || (address > BOOTLOADER_SETTINGS_ADDRESS))
  116. {
  117. return NRF_ERROR_INVALID_PARAM;
  118. }
  119. #if defined(ACL_PRESENT)
  120. // Protect using ACL.
  121. static uint32_t acl_instance = 0;
  122. uint32_t const wmask = (ACL_ACL_PERM_WRITE_Disable << ACL_ACL_PERM_WRITE_Pos);
  123. uint32_t const rwmask = wmask | (ACL_ACL_PERM_READ_Disable << ACL_ACL_PERM_READ_Pos);
  124. uint32_t const mask = read_protect ? rwmask: wmask;
  125. do
  126. {
  127. if (acl_instance >= ACL_REGIONS_COUNT)
  128. {
  129. return NRF_ERROR_NO_MEM;
  130. }
  131. NRF_ACL->ACL[acl_instance].ADDR = address;
  132. NRF_ACL->ACL[acl_instance].SIZE = size;
  133. NRF_ACL->ACL[acl_instance].PERM = mask;
  134. acl_instance++;
  135. } while (NRF_ACL->ACL[acl_instance - 1].ADDR != address
  136. || NRF_ACL->ACL[acl_instance - 1].SIZE != size
  137. || NRF_ACL->ACL[acl_instance - 1].PERM != mask); // Check whether the acl_instance has been used before.
  138. #elif defined (BPROT_PRESENT)
  139. // Protect using BPROT. BPROT does not support read protection.
  140. uint32_t pagenum_start = address / CODE_PAGE_SIZE;
  141. uint32_t pagenum_end = pagenum_start + ((size - 1) / CODE_PAGE_SIZE);
  142. for (uint32_t i = pagenum_start; i <= pagenum_end; i++)
  143. {
  144. uint32_t config_index = i / 32;
  145. uint32_t mask = (1 << (i - config_index * 32));
  146. switch (config_index)
  147. {
  148. case 0:
  149. NRF_BPROT->CONFIG0 = mask;
  150. break;
  151. case 1:
  152. NRF_BPROT->CONFIG1 = mask;
  153. break;
  154. #if BPROT_REGIONS_NUM > 64
  155. case 2:
  156. NRF_BPROT->CONFIG2 = mask;
  157. break;
  158. case 3:
  159. NRF_BPROT->CONFIG3 = mask;
  160. break;
  161. #endif
  162. }
  163. }
  164. #endif
  165. return NRF_SUCCESS;
  166. }
  167. #if NRF_BOOTLOADER_READ_PROTECT
  168. #ifdef __ICCARM__
  169. __ramfunc
  170. #elif defined ( __GNUC__ ) || defined ( __SES_ARM )
  171. __attribute__((noinline, long_call, section(".data")))
  172. #elif defined ( __CC_ARM )
  173. #warning "Keil requires changes to project settings to run this file from RAM. Ignore this warning if configuration has been made."
  174. #endif
  175. #endif
  176. void nrf_bootloader_app_start_final(uint32_t vector_table_addr)
  177. {
  178. ret_code_t ret_val;
  179. // Protect MBR & bootloader code and params pages.
  180. if (NRF_BOOTLOADER_READ_PROTECT)
  181. {
  182. ret_val = nrf_bootloader_flash_protect(0, MBR_SIZE, NRF_BOOTLOADER_READ_PROTECT);
  183. }
  184. // Size of the flash area to protect.
  185. uint32_t area_size;
  186. area_size = BOOTLOADER_SIZE + NRF_MBR_PARAMS_PAGE_SIZE;
  187. if (!NRF_BL_DFU_ALLOW_UPDATE_FROM_APP && !NRF_BL_DFU_ENTER_METHOD_BUTTONLESS && !NRF_DFU_TRANSPORT_BLE)
  188. {
  189. area_size += BOOTLOADER_SETTINGS_PAGE_SIZE;
  190. }
  191. ret_val = nrf_bootloader_flash_protect(BOOTLOADER_START_ADDR,
  192. area_size,
  193. NRF_BOOTLOADER_READ_PROTECT);
  194. if (!NRF_BOOTLOADER_READ_PROTECT && (ret_val != NRF_SUCCESS))
  195. {
  196. NRF_LOG_ERROR("Could not protect bootloader and settings pages, 0x%x.", ret_val);
  197. }
  198. APP_ERROR_CHECK(ret_val);
  199. ret_val = nrf_bootloader_flash_protect(0,
  200. nrf_dfu_bank0_start_addr() + ALIGN_TO_PAGE(s_dfu_settings.bank_0.image_size),
  201. false);
  202. if (!NRF_BOOTLOADER_READ_PROTECT && (ret_val != NRF_SUCCESS))
  203. {
  204. NRF_LOG_ERROR("Could not protect SoftDevice and application, 0x%x.", ret_val);
  205. }
  206. APP_ERROR_CHECK(ret_val);
  207. // Run application
  208. app_start(vector_table_addr);
  209. }