nrf_dfu_flash.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "nrf_dfu_flash.h"
  41. #include "nrf_dfu_types.h"
  42. #include "nrf_fstorage.h"
  43. #include "nrf_fstorage_sd.h"
  44. #include "nrf_fstorage_nvmc.h"
  45. #define NRF_LOG_MODULE_NAME nrf_dfu_flash
  46. #include "nrf_log.h"
  47. NRF_LOG_MODULE_REGISTER();
  48. void dfu_fstorage_evt_handler(nrf_fstorage_evt_t * p_evt);
  49. NRF_FSTORAGE_DEF(nrf_fstorage_t m_fs) =
  50. {
  51. .evt_handler = dfu_fstorage_evt_handler,
  52. .start_addr = MBR_SIZE,
  53. .end_addr = BOOTLOADER_SETTINGS_ADDRESS + BOOTLOADER_SETTINGS_PAGE_SIZE
  54. };
  55. static uint32_t m_flash_operations_pending;
  56. void dfu_fstorage_evt_handler(nrf_fstorage_evt_t * p_evt)
  57. {
  58. if (NRF_LOG_ENABLED && (m_flash_operations_pending > 0))
  59. {
  60. m_flash_operations_pending--;
  61. }
  62. if (p_evt->result == NRF_SUCCESS)
  63. {
  64. NRF_LOG_DEBUG("Flash %s success: addr=%p, pending %d",
  65. (p_evt->id == NRF_FSTORAGE_EVT_WRITE_RESULT) ? "write" : "erase",
  66. p_evt->addr, m_flash_operations_pending);
  67. }
  68. else
  69. {
  70. NRF_LOG_DEBUG("Flash %s failed (0x%x): addr=%p, len=0x%x bytes, pending %d",
  71. (p_evt->id == NRF_FSTORAGE_EVT_WRITE_RESULT) ? "write" : "erase",
  72. p_evt->result, p_evt->addr, p_evt->len, m_flash_operations_pending);
  73. }
  74. if (p_evt->p_param)
  75. {
  76. //lint -save -e611 (Suspicious cast)
  77. ((nrf_dfu_flash_callback_t)(p_evt->p_param))((void*)p_evt->p_src);
  78. //lint -restore
  79. }
  80. }
  81. ret_code_t nrf_dfu_flash_init(bool sd_irq_initialized)
  82. {
  83. nrf_fstorage_api_t * p_api_impl;
  84. /* Setup the desired API implementation. */
  85. #if defined(BLE_STACK_SUPPORT_REQD) || defined(ANT_STACK_SUPPORT_REQD)
  86. if (sd_irq_initialized)
  87. {
  88. NRF_LOG_DEBUG("Initializing nrf_fstorage_sd backend.");
  89. p_api_impl = &nrf_fstorage_sd;
  90. }
  91. else
  92. #endif
  93. {
  94. NRF_LOG_DEBUG("Initializing nrf_fstorage_nvmc backend.");
  95. p_api_impl = &nrf_fstorage_nvmc;
  96. }
  97. return nrf_fstorage_init(&m_fs, p_api_impl, NULL);
  98. }
  99. ret_code_t nrf_dfu_flash_store(uint32_t dest,
  100. void const * p_src,
  101. uint32_t len,
  102. nrf_dfu_flash_callback_t callback)
  103. {
  104. ret_code_t rc;
  105. NRF_LOG_DEBUG("nrf_fstorage_write(addr=%p, src=%p, len=%d bytes), queue usage: %d",
  106. dest, p_src, len, m_flash_operations_pending);
  107. //lint -save -e611 (Suspicious cast)
  108. rc = nrf_fstorage_write(&m_fs, dest, p_src, len, (void *)callback);
  109. //lint -restore
  110. if ((NRF_LOG_ENABLED) && (rc == NRF_SUCCESS))
  111. {
  112. m_flash_operations_pending++;
  113. }
  114. else
  115. {
  116. NRF_LOG_WARNING("nrf_fstorage_write() failed with error 0x%x.", rc);
  117. }
  118. return rc;
  119. }
  120. ret_code_t nrf_dfu_flash_erase(uint32_t page_addr,
  121. uint32_t num_pages,
  122. nrf_dfu_flash_callback_t callback)
  123. {
  124. ret_code_t rc;
  125. NRF_LOG_DEBUG("nrf_fstorage_erase(addr=0x%p, len=%d pages), queue usage: %d",
  126. page_addr, num_pages, m_flash_operations_pending);
  127. //lint -save -e611 (Suspicious cast)
  128. rc = nrf_fstorage_erase(&m_fs, page_addr, num_pages, (void *)callback);
  129. //lint -restore
  130. if ((NRF_LOG_ENABLED) && (rc == NRF_SUCCESS))
  131. {
  132. m_flash_operations_pending++;
  133. }
  134. else
  135. {
  136. NRF_LOG_WARNING("nrf_fstorage_erase() failed with error 0x%x.", rc);
  137. }
  138. return rc;
  139. }