main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 bootloader_secure_ble main.c
  43. * @{
  44. * @ingroup dfu_bootloader_api
  45. * @brief Bootloader project main file for secure DFU.
  46. *
  47. */
  48. #include <stdint.h>
  49. #include "boards.h"
  50. #include "nrf_mbr.h"
  51. #include "nrf_bootloader.h"
  52. #include "nrf_bootloader_app_start.h"
  53. #include "nrf_bootloader_dfu_timers.h"
  54. #include "nrf_dfu.h"
  55. #include "nrf_log.h"
  56. #include "nrf_log_ctrl.h"
  57. #include "nrf_log_default_backends.h"
  58. #include "app_error.h"
  59. #include "app_error_weak.h"
  60. #include "nrf_bootloader_info.h"
  61. #include "nrf_delay.h"
  62. static void on_error(void)
  63. {
  64. NRF_LOG_FINAL_FLUSH();
  65. #if NRF_MODULE_ENABLED(NRF_LOG_BACKEND_RTT)
  66. // To allow the buffer to be flushed by the host.
  67. nrf_delay_ms(100);
  68. #endif
  69. #ifdef NRF_DFU_DEBUG_VERSION
  70. NRF_BREAKPOINT_COND;
  71. #endif
  72. NVIC_SystemReset();
  73. }
  74. void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t * p_file_name)
  75. {
  76. NRF_LOG_ERROR("%s:%d", p_file_name, line_num);
  77. on_error();
  78. }
  79. void app_error_fault_handler(uint32_t id, uint32_t pc, uint32_t info)
  80. {
  81. NRF_LOG_ERROR("Received a fault! id: 0x%08x, pc: 0x%08x, info: 0x%08x", id, pc, info);
  82. on_error();
  83. }
  84. void app_error_handler_bare(uint32_t error_code)
  85. {
  86. NRF_LOG_ERROR("Received an error: 0x%08x!", error_code);
  87. on_error();
  88. }
  89. /**
  90. * @brief Function notifies certain events in DFU process.
  91. */
  92. static void dfu_observer(nrf_dfu_evt_type_t evt_type)
  93. {
  94. switch (evt_type)
  95. {
  96. case NRF_DFU_EVT_DFU_FAILED:
  97. case NRF_DFU_EVT_DFU_ABORTED:
  98. case NRF_DFU_EVT_DFU_INITIALIZED:
  99. bsp_board_init(BSP_INIT_LEDS);
  100. bsp_board_led_on(BSP_BOARD_LED_0);
  101. bsp_board_led_on(BSP_BOARD_LED_1);
  102. bsp_board_led_off(BSP_BOARD_LED_2);
  103. break;
  104. case NRF_DFU_EVT_TRANSPORT_ACTIVATED:
  105. bsp_board_led_off(BSP_BOARD_LED_1);
  106. bsp_board_led_on(BSP_BOARD_LED_2);
  107. break;
  108. case NRF_DFU_EVT_DFU_STARTED:
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. /**@brief Function for application main entry. */
  115. int main(void)
  116. {
  117. uint32_t ret_val;
  118. // Must happen before flash protection is applied, since it edits a protected page.
  119. nrf_bootloader_mbr_addrs_populate();
  120. // Protect MBR and bootloader code from being overwritten.
  121. ret_val = nrf_bootloader_flash_protect(0, MBR_SIZE);
  122. APP_ERROR_CHECK(ret_val);
  123. ret_val = nrf_bootloader_flash_protect(BOOTLOADER_START_ADDR, BOOTLOADER_SIZE);
  124. APP_ERROR_CHECK(ret_val);
  125. (void) NRF_LOG_INIT(nrf_bootloader_dfu_timer_counter_get);
  126. NRF_LOG_DEFAULT_BACKENDS_INIT();
  127. NRF_LOG_INFO("Inside main");
  128. ret_val = nrf_bootloader_init(dfu_observer);
  129. APP_ERROR_CHECK(ret_val);
  130. NRF_LOG_FLUSH();
  131. NRF_LOG_ERROR("After main, should never be reached.");
  132. NRF_LOG_FLUSH();
  133. APP_ERROR_CHECK_BOOL(false);
  134. }
  135. /**
  136. * @}
  137. */