nrf_dfu_ver_validation.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. * Copyright (c) 2017 - 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 <stdbool.h>
  41. #include "nrf_dfu_types.h"
  42. #include "nrf_dfu_settings.h"
  43. #include "nrf_dfu_utils.h"
  44. #include "nrf_bootloader_info.h"
  45. #include "nrf_crypto.h"
  46. #include "nrf_assert.h"
  47. #include "dfu-cc.pb.h"
  48. #include "nrf_dfu_ver_validation.h"
  49. #define NRF_LOG_MODULE_NAME nrf_dfu_ver_validation
  50. #include "nrf_log.h"
  51. NRF_LOG_MODULE_REGISTER();
  52. /** @brief Macro for reading the Firmware ID of a SoftDevice at a given base address.
  53. */
  54. #ifndef _SD_FWID_GET
  55. #define _SD_FWID_GET(baseaddr) SD_OFFSET_GET_UINT16(baseaddr, 0x0C)
  56. #endif
  57. #define EXT_ERR(err) (nrf_dfu_result_t)((uint32_t)NRF_DFU_RES_CODE_EXT_ERROR + (uint32_t)err)
  58. static bool sd_req_check(uint32_t const * p_sd_req, uint8_t sd_req_cnt, bool accept_any)
  59. {
  60. bool result = false;
  61. for (uint8_t i = 0; i < sd_req_cnt; i++)
  62. {
  63. if ((SD_PRESENT && (p_sd_req[i] == _SD_FWID_GET(MBR_SIZE))) ||
  64. (accept_any && (p_sd_req[i] == SD_REQ_ANY_VERSION))
  65. )
  66. {
  67. // Found a matching sd_req field. sd_req is ok.
  68. result = true;
  69. break;
  70. }
  71. }
  72. return result;
  73. }
  74. static bool sd_req_ok(dfu_init_command_t const * p_init)
  75. {
  76. ASSERT(p_init != NULL);
  77. bool result;
  78. #if defined(BLE_STACK_SUPPORT_REQD) || defined(ANT_STACK_SUPPORT_REQD)
  79. // The bootloader needs the SoftDevice, so disabling NRF_DFU_APP_DOWNGRADE_PREVENTION
  80. // should not be applied to SoftDevice updates.
  81. const bool prevent_downgrade = NRF_DFU_APP_DOWNGRADE_PREVENTION || (p_init->type == DFU_FW_TYPE_SOFTDEVICE);
  82. #else
  83. const bool prevent_downgrade = NRF_DFU_APP_DOWNGRADE_PREVENTION;
  84. #endif
  85. if (SD_PRESENT)
  86. {
  87. if (p_init->sd_req_count == 0)
  88. {
  89. result = false;
  90. }
  91. else if (p_init->sd_req[0] != SD_REQ_APP_OVERWRITES_SD)
  92. {
  93. result = sd_req_check(p_init->sd_req,
  94. p_init->sd_req_count,
  95. (p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION));
  96. }
  97. else if (p_init->type == DFU_FW_TYPE_APPLICATION)
  98. {
  99. // The application wants to overwrite the SoftDevice.
  100. if (prevent_downgrade && (p_init->sd_req_count > 1) && (p_init->sd_req[0] == SD_REQ_APP_OVERWRITES_SD))
  101. {
  102. // The application can overwrite the SD if sd_req[0] == 0 and table has the FWID of the current SD.
  103. result = sd_req_check(p_init->sd_req, p_init->sd_req_count, false);
  104. // Prevent BLE/ANT bootloaders from allowing applications overwriting the SoftDevice.
  105. #if defined(BLE_STACK_SUPPORT_REQD) || defined(ANT_STACK_SUPPORT_REQD)
  106. result = false;
  107. #endif
  108. }
  109. else
  110. {
  111. result = true;
  112. }
  113. }
  114. #if NRF_DFU_SUPPORTS_EXTERNAL_APP
  115. else if(p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
  116. {
  117. // Won't accept FW upgrade using external application to
  118. // enforce replacing SoftDevice (SD_REQ_APP_OVERWRITES_SD)
  119. result = false;
  120. }
  121. #endif // NRF_DFU_SUPPORTS_EXTERNAL_APP
  122. else
  123. {
  124. // Don't allow SoftDevice updates which assume no SD is present already.
  125. result = !prevent_downgrade || (p_init->type != DFU_FW_TYPE_SOFTDEVICE);
  126. }
  127. }
  128. else
  129. {
  130. if (p_init->sd_req_count && (p_init->sd_req[0] != SD_REQ_APP_OVERWRITES_SD))
  131. {
  132. // Fail if there is no SD and the update requires SD. The special "any" FWID is valid
  133. // for external apps only.
  134. result = false;
  135. #if NRF_DFU_SUPPORTS_EXTERNAL_APP
  136. result = sd_req_check(p_init->sd_req,
  137. p_init->sd_req_count,
  138. (p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION));
  139. #endif // NRF_DFU_SUPPORTS_EXTERNAL_APP
  140. }
  141. else
  142. {
  143. // If there is no SD and update has SD it is accepted only if it has a fw_version.
  144. result = !prevent_downgrade || p_init->has_fw_version;
  145. }
  146. }
  147. return result;
  148. }
  149. static bool fw_hash_type_ok(dfu_init_command_t const * p_init)
  150. {
  151. ASSERT(p_init != NULL);
  152. return (p_init->hash.hash_type == DFU_HASH_TYPE_SHA256);
  153. }
  154. static bool fw_version_required(dfu_fw_type_t new_fw_type)
  155. {
  156. bool result = true;
  157. if (new_fw_type == DFU_FW_TYPE_SOFTDEVICE)
  158. {
  159. result = false; // fw_version is optional in SoftDevice updates. If present, it will be checked against the app version.
  160. }
  161. else if (new_fw_type == DFU_FW_TYPE_APPLICATION)
  162. {
  163. result = NRF_DFU_APP_DOWNGRADE_PREVENTION; // fw_version is configurable in app updates.
  164. }
  165. #if NRF_DFU_SUPPORTS_EXTERNAL_APP
  166. #if !NRF_DFU_EXTERNAL_APP_VERSIONING
  167. else if (new_fw_type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
  168. {
  169. return false;
  170. }
  171. #endif //!NRF_DFU_EXTERNAL_APP_VERSIONING
  172. #endif // NRF_DFU_SUPPORTS_EXTERNAL_APP
  173. return result;
  174. }
  175. static bool fw_type_ok(dfu_init_command_t const * p_init)
  176. {
  177. ASSERT(p_init != NULL);
  178. return ((p_init->has_type)
  179. && ( (p_init->type == DFU_FW_TYPE_APPLICATION)
  180. || (p_init->type == DFU_FW_TYPE_SOFTDEVICE)
  181. || (p_init->type == DFU_FW_TYPE_BOOTLOADER)
  182. || (p_init->type == DFU_FW_TYPE_SOFTDEVICE_BOOTLOADER)
  183. #if NRF_DFU_SUPPORTS_EXTERNAL_APP
  184. || (p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
  185. #endif // NRF_DFU_SUPPORTS_EXTERNAL_APP
  186. ));
  187. }
  188. #ifndef NRF_DFU_APP_ACCEPT_SAME_VERSION
  189. #define NRF_DFU_APP_ACCEPT_SAME_VERSION 1
  190. #endif
  191. // This function assumes p_init->has_fw_version.
  192. static bool fw_version_ok(dfu_init_command_t const * p_init)
  193. {
  194. ASSERT(p_init != NULL);
  195. ASSERT(p_init->has_fw_version);
  196. if ((p_init->type == DFU_FW_TYPE_APPLICATION) ||
  197. (p_init->type == DFU_FW_TYPE_SOFTDEVICE))
  198. {
  199. if (!NRF_DFU_APP_DOWNGRADE_PREVENTION)
  200. {
  201. return true;
  202. }
  203. else if ((p_init->fw_version > s_dfu_settings.app_version))
  204. {
  205. return true;
  206. }
  207. else if ((p_init->fw_version == s_dfu_settings.app_version))
  208. {
  209. return NRF_DFU_APP_ACCEPT_SAME_VERSION;
  210. }
  211. else
  212. {
  213. return false;
  214. }
  215. }
  216. #if NRF_DFU_SUPPORTS_EXTERNAL_APP
  217. #if NRF_DFU_EXTERNAL_APP_VERSIONING
  218. else if (p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
  219. {
  220. return (p_init->fw_version >= s_dfu_settings.app_version);
  221. }
  222. #else
  223. else if(p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
  224. {
  225. return true;
  226. }
  227. #endif // NRF_DFU_EXTERNAL_APP_VERSIONING
  228. #endif // NRF_DFU_SUPPORTS_EXTERNAL_APP
  229. else
  230. {
  231. return (p_init->fw_version > s_dfu_settings.bootloader_version);
  232. }
  233. }
  234. nrf_dfu_result_t nrf_dfu_ver_validation_check(dfu_init_command_t const * p_init)
  235. {
  236. nrf_dfu_result_t ret_val = NRF_DFU_RES_CODE_SUCCESS;
  237. if (!fw_type_ok(p_init))
  238. {
  239. NRF_LOG_ERROR("Invalid firmware type.");
  240. ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_INIT_COMMAND_INVALID);
  241. }
  242. else if (!fw_hash_type_ok(p_init))
  243. {
  244. NRF_LOG_ERROR("Invalid hash type.");
  245. ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_WRONG_HASH_TYPE);
  246. }
  247. else if (!NRF_DFU_DEBUG ||
  248. (NRF_DFU_DEBUG && ((p_init->has_is_debug == false) || (p_init->is_debug == false))))
  249. {
  250. if (p_init->has_hw_version == false)
  251. {
  252. NRF_LOG_ERROR("No HW version.");
  253. ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_INIT_COMMAND_INVALID);
  254. }
  255. else if (p_init->hw_version != NRF_DFU_HW_VERSION)
  256. {
  257. NRF_LOG_WARNING("Faulty HW version.");
  258. ret_val = EXT_ERR( NRF_DFU_EXT_ERROR_HW_VERSION_FAILURE);
  259. }
  260. else if (!sd_req_ok(p_init))
  261. {
  262. NRF_LOG_WARNING("SD req not met.");
  263. ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_SD_VERSION_FAILURE);
  264. }
  265. else if (p_init->has_fw_version)
  266. {
  267. if (!fw_version_ok(p_init))
  268. {
  269. NRF_LOG_WARNING("FW version too low.");
  270. ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_FW_VERSION_FAILURE);
  271. }
  272. }
  273. else
  274. {
  275. if (fw_version_required(p_init->type))
  276. {
  277. NRF_LOG_ERROR("FW version missing.");
  278. ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_INIT_COMMAND_INVALID);
  279. }
  280. }
  281. }
  282. return ret_val;
  283. }