es_flash.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * Copyright (c) 2016 - 2018, 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; //!< Should factory reset be performed.
  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 != FDS_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. case FDS_EVT_DEL_RECORD:
  106. // Schedule garbage collection
  107. err_code = app_sched_event_put(NULL, 0, fds_gc_event);
  108. APP_ERROR_CHECK(err_code);
  109. break;
  110. case FDS_EVT_GC:
  111. if (m_factory_reset && m_conn_handle != BLE_CONN_HANDLE_INVALID)
  112. {
  113. err_code =
  114. sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  115. APP_ERROR_CHECK(err_code);
  116. }
  117. // Fall through:
  118. case FDS_EVT_UPDATE:
  119. case FDS_EVT_WRITE:
  120. if (m_num_pending_ops > 0)
  121. {
  122. m_num_pending_ops--;
  123. }
  124. break;
  125. }
  126. }
  127. /**@brief Function performing flash access (read/write/clear).
  128. *
  129. * @param[in] p_params Flash access parameters.
  130. */
  131. static ret_code_t access_flash_data(const flash_access_params_t * p_params)
  132. {
  133. ret_code_t err_code;
  134. fds_flash_record_t record = {0};
  135. fds_record_desc_t desc = {0};
  136. fds_find_token_t ft = {0};
  137. fds_record_t record_to_write =
  138. {
  139. .data.p_data = p_params->p_data_buf,
  140. .file_id = p_params->file_id
  141. };
  142. err_code = fds_record_find_by_key(p_params->record_key, &desc, &ft);
  143. // If its a read or clear, we can not accept errors on lookup
  144. if (p_params->access_type == ES_FLASH_ACCESS_READ)
  145. {
  146. RETURN_IF_ERROR(err_code);
  147. }
  148. if (p_params->access_type == ES_FLASH_ACCESS_CLEAR && err_code == FDS_ERR_NOT_FOUND)
  149. {
  150. return NRF_SUCCESS;
  151. }
  152. switch (p_params->access_type)
  153. {
  154. case ES_FLASH_ACCESS_READ:
  155. err_code = fds_record_open(&desc, &record);
  156. RETURN_IF_ERROR(err_code);
  157. memcpy(p_params->p_data, record.p_data, p_params->size_bytes);
  158. err_code = fds_record_close(&desc);
  159. RETURN_IF_ERROR(err_code);
  160. break;
  161. case ES_FLASH_ACCESS_WRITE:
  162. memcpy(p_params->p_data_buf, p_params->p_data, p_params->size_bytes);
  163. record_to_write.data.length_words = (p_params->size_bytes +3) / 4;
  164. record_to_write.key = p_params->record_key;
  165. if (err_code == FDS_ERR_NOT_FOUND)
  166. {
  167. err_code = fds_record_write(&desc, &record_to_write);
  168. }
  169. else
  170. {
  171. err_code = fds_record_update(&desc, &record_to_write);
  172. }
  173. RETURN_IF_ERROR(err_code);
  174. m_num_pending_ops++;
  175. break;
  176. case ES_FLASH_ACCESS_CLEAR:
  177. err_code = fds_record_delete(&desc);
  178. RETURN_IF_ERROR(err_code);
  179. m_num_pending_ops++;
  180. break;
  181. default:
  182. break;
  183. }
  184. return NRF_SUCCESS;
  185. }
  186. ret_code_t es_flash_access_lock_key(uint8_t * p_lock_key, es_flash_access_t access_type)
  187. {
  188. flash_access_params_t params = {.record_key = RECORD_KEY_LOCK_KEY,
  189. .file_id = FILE_ID_ES_FLASH_LOCK_KEY,
  190. .p_data_buf = lock_key_buf,
  191. .p_data = (uint8_t *)p_lock_key,
  192. .size_bytes = SIZE_OF_LOCK_KEY,
  193. .access_type = access_type};
  194. return access_flash_data(&params);
  195. }
  196. ret_code_t es_flash_access_beacon_config(es_flash_beacon_config_t * p_config,
  197. es_flash_access_t access_type)
  198. {
  199. ret_code_t err_code;
  200. flash_access_params_t params = {.record_key = RECORD_KEY_BEACON_CONFIG,
  201. .file_id = FILE_ID_ES_FLASH,
  202. .p_data_buf = beacon_config_buf,
  203. .p_data = (uint8_t *)p_config,
  204. .size_bytes = sizeof(es_flash_beacon_config_t),
  205. .access_type = access_type};
  206. err_code = access_flash_data(&params);
  207. return err_code;
  208. }
  209. ret_code_t es_flash_access_slot_configs(uint8_t slot_no,
  210. es_slot_t * p_slot,
  211. es_flash_access_t access_type)
  212. {
  213. if (slot_no >= APP_MAX_ADV_SLOTS)
  214. {
  215. return NRF_ERROR_INVALID_PARAM;
  216. }
  217. flash_access_params_t params = {.record_key = RECORD_KEY_SLOTS[slot_no],
  218. .file_id = FILE_ID_ES_FLASH,
  219. .p_data_buf = slots_buf_p[slot_no],
  220. .p_data = (uint8_t *)p_slot,
  221. .size_bytes = sizeof(es_slot_t),
  222. .access_type = access_type};
  223. return access_flash_data(&params);
  224. }
  225. ret_code_t es_flash_access_flags(es_flash_flags_t * p_flags, es_flash_access_t access_type)
  226. {
  227. flash_access_params_t params = {.record_key = RECORD_KEY_FLAGS,
  228. .file_id = FILE_ID_ES_FLASH,
  229. .p_data_buf = flash_flags_buf,
  230. .p_data = (uint8_t *)p_flags,
  231. .size_bytes = sizeof(es_flash_flags_t),
  232. .access_type = access_type};
  233. return access_flash_data(&params);
  234. }
  235. ret_code_t es_flash_factory_reset(void)
  236. {
  237. // Delete everything except the lock key:
  238. ret_code_t ret_code = fds_file_delete(FILE_ID_ES_FLASH);
  239. if (ret_code == FDS_SUCCESS)
  240. m_factory_reset = true;
  241. return ret_code;
  242. }
  243. uint32_t es_flash_num_pending_ops(void)
  244. {
  245. return m_num_pending_ops;
  246. }
  247. void es_flash_on_ble_evt(ble_evt_t const * p_evt)
  248. {
  249. switch (p_evt->header.evt_id)
  250. {
  251. case BLE_GAP_EVT_CONNECTED:
  252. m_conn_handle = p_evt->evt.common_evt.conn_handle;
  253. break;
  254. case BLE_GAP_EVT_DISCONNECTED:
  255. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  256. if (m_factory_reset)
  257. {
  258. (void)sd_nvic_SystemReset();
  259. }
  260. break;
  261. }
  262. }
  263. ret_code_t es_flash_init(void)
  264. {
  265. ret_code_t err_code;
  266. m_num_pending_ops = 1; // Will be set to 0 when getting FDS_EVT_INIT event
  267. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  268. m_factory_reset = false;
  269. err_code = fds_register(fds_cb);
  270. RETURN_IF_ERROR(err_code);
  271. err_code = fds_init();
  272. RETURN_IF_ERROR(err_code);
  273. return NRF_SUCCESS;
  274. }