es_flash.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <string.h>
  41. #include "es_flash.h"
  42. #include "es_util.h"
  43. #include "app_scheduler.h"
  44. #include "ble_hci.h"
  45. #include "fds.h"
  46. #include "nrf_nvic.h"
  47. #define SIZE_OF_PRIV_KEY ESCS_ECDH_KEY_SIZE //!< Size of ECDH private key.
  48. #define SIZE_OF_PUB_KEY ESCS_ECDH_KEY_SIZE //!< Size of ECDH public key.
  49. #define SIZE_OF_LOCK_KEY ESCS_AES_KEY_SIZE //!< Size of lock key.
  50. #define FILE_ID_ES_FLASH 0x1337 //!< File ID used for all flash access EXCEPT lock code.
  51. #define FILE_ID_ES_FLASH_LOCK_KEY 0x1338 //!< File ID used for lock code flash access.
  52. #define RECORD_KEY_FLAGS 0x1 //!< File record for flash flags.
  53. #define RECORD_KEY_PRIV_KEY 0x2 //!< File record for private key.
  54. #define RECORD_KEY_PUB_KEY 0x3 //!< File record for public key.
  55. #define RECORD_KEY_LOCK_KEY 0x4 //!< File record for lock key.
  56. #define RECORD_KEY_BEACON_CONFIG 0x5 //!< File record for lock key.
  57. static uint16_t RECORD_KEY_SLOTS[5] = {0x6, 0x7, 0x8, 0x9, 0xa}; //!< File record for slots.
  58. /**@brief Structure used for invoking flash access function. */
  59. typedef struct
  60. {
  61. uint16_t record_key;
  62. uint16_t file_id;
  63. uint8_t * p_data_buf;
  64. uint8_t * p_data;
  65. uint16_t size_bytes;
  66. es_flash_access_t access_type;
  67. } flash_access_params_t;
  68. static volatile uint32_t m_num_pending_ops; //!< Current number of outstanding FDS operations.
  69. static volatile bool m_factory_reset_done; //!< Has a factory reset operation been completed.
  70. static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; //!< Current connection handle.
  71. #if APP_MAX_ADV_SLOTS > 32
  72. #error "APP_MAX_ADV_SLOTS must be <= 32"
  73. #endif
  74. #define SLOT_DECL(i, _) __ALIGN(4) static uint8_t slot## i ##_buf[sizeof(es_slot_t)];
  75. EVAL(REPEAT(APP_MAX_ADV_SLOTS, SLOT_DECL, ~))
  76. __ALIGN(4) static uint8_t lock_key_buf[SIZE_OF_LOCK_KEY]; //!< Buffer for lock key flash access.
  77. #define SLOT(i, _) slot## i ##_buf,
  78. static uint8_t * slots_buf_p[APP_MAX_ADV_SLOTS] = {
  79. EVAL(REPEAT(APP_MAX_ADV_SLOTS, SLOT, ~))
  80. };
  81. __ALIGN(4) static uint8_t flash_flags_buf[sizeof(es_flash_flags_t)]; //!< Buffer for flash flags flash access.
  82. __ALIGN(4) static uint8_t beacon_config_buf[sizeof(es_flash_beacon_config_t)]; //!< Buffer for beacon config flash access.
  83. /**@brief Function handling scheduled FDS garbage collection. */
  84. static void fds_gc_event(void * p_event_data, uint16_t event_size)
  85. {
  86. ret_code_t fds_err_code;
  87. fds_err_code = fds_gc();
  88. if (fds_err_code != NRF_SUCCESS)
  89. APP_ERROR_CHECK_BOOL(NRF_ERROR_INTERNAL);
  90. m_num_pending_ops++;
  91. }
  92. /**@brief Function handling FDS events.
  93. *
  94. * @param[in] p_evt FDS event.
  95. */
  96. static void fds_cb(fds_evt_t const * const p_evt)
  97. {
  98. ret_code_t err_code;
  99. switch (p_evt->id)
  100. {
  101. case FDS_EVT_INIT:
  102. m_num_pending_ops = 0;
  103. break;
  104. case FDS_EVT_DEL_FILE:
  105. if (p_evt->del.file_id == FILE_ID_ES_FLASH)
  106. {
  107. m_factory_reset_done = true;
  108. }
  109. // Fall through
  110. case FDS_EVT_DEL_RECORD:
  111. // Schedule garbage collection
  112. err_code = app_sched_event_put(NULL, 0, fds_gc_event);
  113. APP_ERROR_CHECK(err_code);
  114. break;
  115. case FDS_EVT_GC:
  116. // During factory reset, a file is deleted, and garbage collection is scheduled
  117. // when the callback for that deletion is invoked.
  118. // So here we know that the factory reset is completed.
  119. if (m_factory_reset_done)
  120. {
  121. if (m_conn_handle != BLE_CONN_HANDLE_INVALID)
  122. {
  123. err_code =
  124. sd_ble_gap_disconnect(m_conn_handle,
  125. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  126. APP_ERROR_CHECK(err_code);
  127. }
  128. else
  129. {
  130. m_factory_reset_done = false;
  131. (void)sd_nvic_SystemReset();
  132. }
  133. }
  134. // Fall through:
  135. case FDS_EVT_UPDATE:
  136. // Fall through:
  137. case FDS_EVT_WRITE:
  138. if (m_num_pending_ops > 0)
  139. {
  140. m_num_pending_ops--;
  141. }
  142. break;
  143. }
  144. }
  145. /**@brief Function performing flash access (read/write/clear).
  146. *
  147. * @param[in] p_params Flash access parameters.
  148. */
  149. static ret_code_t access_flash_data(const flash_access_params_t * p_params)
  150. {
  151. ret_code_t err_code;
  152. fds_flash_record_t record = {0};
  153. fds_record_desc_t desc = {0};
  154. fds_find_token_t ft = {0};
  155. fds_record_t record_to_write =
  156. {
  157. .data.p_data = p_params->p_data_buf,
  158. .file_id = p_params->file_id
  159. };
  160. err_code = fds_record_find_by_key(p_params->record_key, &desc, &ft);
  161. // If its a read or clear, we can not accept errors on lookup
  162. if (p_params->access_type == ES_FLASH_ACCESS_READ)
  163. {
  164. RETURN_IF_ERROR(err_code);
  165. }
  166. if (p_params->access_type == ES_FLASH_ACCESS_CLEAR && err_code == FDS_ERR_NOT_FOUND)
  167. {
  168. return NRF_SUCCESS;
  169. }
  170. switch (p_params->access_type)
  171. {
  172. case ES_FLASH_ACCESS_READ:
  173. err_code = fds_record_open(&desc, &record);
  174. RETURN_IF_ERROR(err_code);
  175. memcpy(p_params->p_data, record.p_data, p_params->size_bytes);
  176. err_code = fds_record_close(&desc);
  177. RETURN_IF_ERROR(err_code);
  178. break;
  179. case ES_FLASH_ACCESS_WRITE:
  180. memcpy(p_params->p_data_buf, p_params->p_data, p_params->size_bytes);
  181. record_to_write.data.length_words = (p_params->size_bytes +3) / 4;
  182. record_to_write.key = p_params->record_key;
  183. if (err_code == FDS_ERR_NOT_FOUND)
  184. {
  185. err_code = fds_record_write(&desc, &record_to_write);
  186. }
  187. else
  188. {
  189. err_code = fds_record_update(&desc, &record_to_write);
  190. }
  191. RETURN_IF_ERROR(err_code);
  192. m_num_pending_ops++;
  193. break;
  194. case ES_FLASH_ACCESS_CLEAR:
  195. err_code = fds_record_delete(&desc);
  196. RETURN_IF_ERROR(err_code);
  197. m_num_pending_ops++;
  198. break;
  199. default:
  200. break;
  201. }
  202. return NRF_SUCCESS;
  203. }
  204. ret_code_t es_flash_access_lock_key(uint8_t * p_lock_key, es_flash_access_t access_type)
  205. {
  206. flash_access_params_t params = {.record_key = RECORD_KEY_LOCK_KEY,
  207. .file_id = FILE_ID_ES_FLASH_LOCK_KEY,
  208. .p_data_buf = lock_key_buf,
  209. .p_data = (uint8_t *)p_lock_key,
  210. .size_bytes = SIZE_OF_LOCK_KEY,
  211. .access_type = access_type};
  212. return access_flash_data(&params);
  213. }
  214. ret_code_t es_flash_access_beacon_config(es_flash_beacon_config_t * p_config,
  215. es_flash_access_t access_type)
  216. {
  217. ret_code_t err_code;
  218. flash_access_params_t params = {.record_key = RECORD_KEY_BEACON_CONFIG,
  219. .file_id = FILE_ID_ES_FLASH,
  220. .p_data_buf = beacon_config_buf,
  221. .p_data = (uint8_t *)p_config,
  222. .size_bytes = sizeof(es_flash_beacon_config_t),
  223. .access_type = access_type};
  224. err_code = access_flash_data(&params);
  225. return err_code;
  226. }
  227. ret_code_t es_flash_access_slot_configs(uint8_t slot_no,
  228. es_slot_t * p_slot,
  229. es_flash_access_t access_type)
  230. {
  231. if (slot_no >= APP_MAX_ADV_SLOTS)
  232. {
  233. return NRF_ERROR_INVALID_PARAM;
  234. }
  235. flash_access_params_t params = {.record_key = RECORD_KEY_SLOTS[slot_no],
  236. .file_id = FILE_ID_ES_FLASH,
  237. .p_data_buf = slots_buf_p[slot_no],
  238. .p_data = (uint8_t *)p_slot,
  239. .size_bytes = sizeof(es_slot_t),
  240. .access_type = access_type};
  241. return access_flash_data(&params);
  242. }
  243. ret_code_t es_flash_access_flags(es_flash_flags_t * p_flags, es_flash_access_t access_type)
  244. {
  245. flash_access_params_t params = {.record_key = RECORD_KEY_FLAGS,
  246. .file_id = FILE_ID_ES_FLASH,
  247. .p_data_buf = flash_flags_buf,
  248. .p_data = (uint8_t *)p_flags,
  249. .size_bytes = sizeof(es_flash_flags_t),
  250. .access_type = access_type};
  251. return access_flash_data(&params);
  252. }
  253. ret_code_t es_flash_factory_reset(void)
  254. {
  255. // Delete everything except the lock key:
  256. ret_code_t ret_code = fds_file_delete(FILE_ID_ES_FLASH);
  257. return ret_code;
  258. }
  259. uint32_t es_flash_num_pending_ops(void)
  260. {
  261. return m_num_pending_ops;
  262. }
  263. void es_flash_on_ble_evt(ble_evt_t const * p_evt)
  264. {
  265. switch (p_evt->header.evt_id)
  266. {
  267. case BLE_GAP_EVT_CONNECTED:
  268. m_conn_handle = p_evt->evt.common_evt.conn_handle;
  269. break;
  270. case BLE_GAP_EVT_DISCONNECTED:
  271. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  272. if (m_factory_reset_done)
  273. {
  274. m_factory_reset_done = false;
  275. (void)sd_nvic_SystemReset();
  276. }
  277. break;
  278. }
  279. }
  280. ret_code_t es_flash_init(void)
  281. {
  282. ret_code_t err_code;
  283. m_num_pending_ops = 1; // Will be set to 0 when getting FDS_EVT_INIT event
  284. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  285. m_factory_reset_done = false;
  286. err_code = fds_register(fds_cb);
  287. RETURN_IF_ERROR(err_code);
  288. err_code = fds_init();
  289. RETURN_IF_ERROR(err_code);
  290. return NRF_SUCCESS;
  291. }