nrf_bootloader_app_start_final.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #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. #define HANDLER_MODE_EXIT 0xFFFFFFF9 // When this is jumped to, the CPU will exit interrupt context
  53. // (handler mode), and pop values from the stack into registers.
  54. // See ARM's documentation for "Exception entry and return".
  55. #define EXCEPTION_STACK_WORD_COUNT 8 // The number of words popped from the stack when
  56. // HANDLER_MODE_EXIT is branched to.
  57. /**@brief Function that sets the stack pointer and starts executing a particular address.
  58. *
  59. * @param[in] new_msp The new value to set in the main stack pointer.
  60. * @param[in] addr The address to execute.
  61. */
  62. void jump_to_addr(uint32_t new_msp, uint32_t addr)
  63. {
  64. __set_MSP(new_msp);
  65. ((void (*)(void))addr)();
  66. }
  67. /**@brief Function for booting an app as if the chip was reset.
  68. *
  69. * @param[in] vector_table_addr The address of the app's vector table.
  70. */
  71. __STATIC_INLINE void app_start(uint32_t vector_table_addr)
  72. {
  73. const uint32_t current_isr_num = (__get_IPSR() & IPSR_ISR_Msk);
  74. 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.
  75. 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.
  76. __set_CONTROL(0x00000000); // Set CONTROL to its reset value 0.
  77. __set_PRIMASK(0x00000000); // Set PRIMASK to its reset value 0.
  78. __set_BASEPRI(0x00000000); // Set BASEPRI to its reset value 0.
  79. __set_FAULTMASK(0x00000000); // Set FAULTMASK to its reset value 0.
  80. ASSERT(current_isr_num == 0); // If this is triggered, the CPU is currently in an interrupt.
  81. // The CPU is in Thread mode (main context).
  82. jump_to_addr(new_msp, reset_handler); // Jump directly to the App's Reset Handler.
  83. }
  84. ret_code_t nrf_bootloader_flash_protect(uint32_t address, uint32_t size)
  85. {
  86. if ((size & (CODE_PAGE_SIZE - 1)) || (address > BOOTLOADER_SETTINGS_ADDRESS))
  87. {
  88. return NRF_ERROR_INVALID_PARAM;
  89. }
  90. #if defined(ACL_PRESENT)
  91. // Protect using ACL.
  92. static uint32_t acl_instance = 0;
  93. uint32_t const mask = (ACL_ACL_PERM_WRITE_Disable << ACL_ACL_PERM_WRITE_Pos);
  94. if (acl_instance >= ACL_REGIONS_COUNT)
  95. {
  96. return NRF_ERROR_NO_MEM;
  97. }
  98. NRF_ACL->ACL[acl_instance].ADDR = address;
  99. NRF_ACL->ACL[acl_instance].SIZE = size;
  100. NRF_ACL->ACL[acl_instance].PERM = mask;
  101. acl_instance++;
  102. #elif defined (BPROT_PRESENT)
  103. // Protect using BPROT. BPROT does not support read protection.
  104. uint32_t pagenum_start = address / CODE_PAGE_SIZE;
  105. uint32_t pagenum_end = pagenum_start + ((size - 1) / CODE_PAGE_SIZE);
  106. for (uint32_t i = pagenum_start; i <= pagenum_end; i++)
  107. {
  108. uint32_t config_index = i / 32;
  109. uint32_t mask = (1 << (i - config_index * 32));
  110. switch (config_index)
  111. {
  112. case 0:
  113. NRF_BPROT->CONFIG0 = mask;
  114. break;
  115. case 1:
  116. NRF_BPROT->CONFIG1 = mask;
  117. break;
  118. #if BPROT_REGIONS_NUM > 64
  119. case 2:
  120. NRF_BPROT->CONFIG2 = mask;
  121. break;
  122. case 3:
  123. NRF_BPROT->CONFIG3 = mask;
  124. break;
  125. #endif
  126. }
  127. }
  128. #endif
  129. return NRF_SUCCESS;
  130. }
  131. void nrf_bootloader_app_start_final(uint32_t vector_table_addr)
  132. {
  133. ret_code_t ret_val;
  134. // Size of the flash area to protect.
  135. uint32_t area_size;
  136. area_size = BOOTLOADER_SIZE + NRF_MBR_PARAMS_PAGE_SIZE;
  137. if (!NRF_BL_DFU_ALLOW_UPDATE_FROM_APP && !NRF_BL_DFU_ENTER_METHOD_BUTTONLESS && !NRF_DFU_TRANSPORT_BLE)
  138. {
  139. area_size += BOOTLOADER_SETTINGS_PAGE_SIZE;
  140. }
  141. ret_val = nrf_bootloader_flash_protect(BOOTLOADER_START_ADDR, area_size);
  142. if (ret_val != NRF_SUCCESS)
  143. {
  144. NRF_LOG_ERROR("Could not protect bootloader and settings pages, 0x%x.", ret_val);
  145. }
  146. APP_ERROR_CHECK(ret_val);
  147. ret_val = nrf_bootloader_flash_protect(0,
  148. nrf_dfu_bank0_start_addr() + ALIGN_TO_PAGE(s_dfu_settings.bank_0.image_size));
  149. if (ret_val != NRF_SUCCESS)
  150. {
  151. NRF_LOG_ERROR("Could not protect SoftDevice and application, 0x%x.", ret_val);
  152. }
  153. APP_ERROR_CHECK(ret_val);
  154. // Run application
  155. app_start(vector_table_addr);
  156. }