123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- #include <stdbool.h>
- #include "nrf_dfu_types.h"
- #include "nrf_dfu_settings.h"
- #include "nrf_dfu_utils.h"
- #include "nrf_bootloader_info.h"
- #include "nrf_crypto.h"
- #include "nrf_assert.h"
- #include "dfu-cc.pb.h"
- #include "nrf_dfu_ver_validation.h"
- #define NRF_LOG_MODULE_NAME nrf_dfu_ver_validation
- #include "nrf_log.h"
- NRF_LOG_MODULE_REGISTER();
- #ifndef _SD_FWID_GET
- #define _SD_FWID_GET(baseaddr) SD_OFFSET_GET_UINT16(baseaddr, 0x0C)
- #endif
- #define EXT_ERR(err) (nrf_dfu_result_t)((uint32_t)NRF_DFU_RES_CODE_EXT_ERROR + (uint32_t)err)
- static bool sd_req_check(uint32_t const * p_sd_req, uint8_t sd_req_cnt, bool accept_any)
- {
- bool result = false;
- for (uint8_t i = 0; i < sd_req_cnt; i++)
- {
- if ((SD_PRESENT && (p_sd_req[i] == _SD_FWID_GET(MBR_SIZE))) ||
- (accept_any && (p_sd_req[i] == SD_REQ_ANY_VERSION))
- )
- {
-
- result = true;
- break;
- }
- }
- return result;
- }
- static bool sd_req_ok(dfu_init_command_t const * p_init)
- {
- ASSERT(p_init != NULL);
- bool result;
- #if defined(BLE_STACK_SUPPORT_REQD) || defined(ANT_STACK_SUPPORT_REQD)
-
-
- const bool prevent_downgrade = NRF_DFU_APP_DOWNGRADE_PREVENTION || (p_init->type == DFU_FW_TYPE_SOFTDEVICE);
- #else
- const bool prevent_downgrade = NRF_DFU_APP_DOWNGRADE_PREVENTION;
- #endif
- if (SD_PRESENT)
- {
- if (p_init->sd_req_count == 0)
- {
- result = false;
- }
- else if (p_init->sd_req[0] != SD_REQ_APP_OVERWRITES_SD)
- {
- result = sd_req_check(p_init->sd_req,
- p_init->sd_req_count,
- (p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION));
- }
- else if (p_init->type == DFU_FW_TYPE_APPLICATION)
- {
-
- if (prevent_downgrade && (p_init->sd_req_count > 1) && (p_init->sd_req[0] == SD_REQ_APP_OVERWRITES_SD))
- {
-
- result = sd_req_check(p_init->sd_req, p_init->sd_req_count, false);
-
- #if defined(BLE_STACK_SUPPORT_REQD) || defined(ANT_STACK_SUPPORT_REQD)
- result = false;
- #endif
- }
- else
- {
- result = true;
- }
- }
- #if NRF_DFU_SUPPORTS_EXTERNAL_APP
- else if(p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
- {
-
-
- result = false;
- }
- #endif
- else
- {
-
- result = !prevent_downgrade || (p_init->type != DFU_FW_TYPE_SOFTDEVICE);
- }
- }
- else
- {
- if (p_init->sd_req_count && (p_init->sd_req[0] != SD_REQ_APP_OVERWRITES_SD))
- {
-
-
- result = false;
- #if NRF_DFU_SUPPORTS_EXTERNAL_APP
- result = sd_req_check(p_init->sd_req,
- p_init->sd_req_count,
- (p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION));
- #endif
- }
- else
- {
-
- result = !prevent_downgrade || p_init->has_fw_version;
- }
- }
- return result;
- }
- static bool fw_hash_type_ok(dfu_init_command_t const * p_init)
- {
- ASSERT(p_init != NULL);
- return (p_init->hash.hash_type == DFU_HASH_TYPE_SHA256);
- }
- static bool fw_version_required(dfu_fw_type_t new_fw_type)
- {
- bool result = true;
- if (new_fw_type == DFU_FW_TYPE_SOFTDEVICE)
- {
- result = false;
- }
- else if (new_fw_type == DFU_FW_TYPE_APPLICATION)
- {
- result = NRF_DFU_APP_DOWNGRADE_PREVENTION;
- }
- #if NRF_DFU_SUPPORTS_EXTERNAL_APP
- #if !NRF_DFU_EXTERNAL_APP_VERSIONING
- else if (new_fw_type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
- {
- return false;
- }
- #endif
- #endif
- return result;
- }
- static bool fw_type_ok(dfu_init_command_t const * p_init)
- {
- ASSERT(p_init != NULL);
- return ((p_init->has_type)
- && ( (p_init->type == DFU_FW_TYPE_APPLICATION)
- || (p_init->type == DFU_FW_TYPE_SOFTDEVICE)
- || (p_init->type == DFU_FW_TYPE_BOOTLOADER)
- || (p_init->type == DFU_FW_TYPE_SOFTDEVICE_BOOTLOADER)
- #if NRF_DFU_SUPPORTS_EXTERNAL_APP
- || (p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
- #endif
- ));
- }
- #ifndef NRF_DFU_APP_ACCEPT_SAME_VERSION
- #define NRF_DFU_APP_ACCEPT_SAME_VERSION 1
- #endif
- static bool fw_version_ok(dfu_init_command_t const * p_init)
- {
- ASSERT(p_init != NULL);
- ASSERT(p_init->has_fw_version);
- if ((p_init->type == DFU_FW_TYPE_APPLICATION) ||
- (p_init->type == DFU_FW_TYPE_SOFTDEVICE))
- {
- if (!NRF_DFU_APP_DOWNGRADE_PREVENTION)
- {
- return true;
- }
- else if ((p_init->fw_version > s_dfu_settings.app_version))
- {
- return true;
- }
- else if ((p_init->fw_version == s_dfu_settings.app_version))
- {
- return NRF_DFU_APP_ACCEPT_SAME_VERSION;
- }
- else
- {
- return false;
- }
- }
- #if NRF_DFU_SUPPORTS_EXTERNAL_APP
- #if NRF_DFU_EXTERNAL_APP_VERSIONING
- else if (p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
- {
- return (p_init->fw_version >= s_dfu_settings.app_version);
- }
- #else
- else if(p_init->type == DFU_FW_TYPE_EXTERNAL_APPLICATION)
- {
- return true;
- }
- #endif
- #endif
- else
- {
- return (p_init->fw_version > s_dfu_settings.bootloader_version);
- }
- }
- nrf_dfu_result_t nrf_dfu_ver_validation_check(dfu_init_command_t const * p_init)
- {
- nrf_dfu_result_t ret_val = NRF_DFU_RES_CODE_SUCCESS;
- if (!fw_type_ok(p_init))
- {
- NRF_LOG_ERROR("Invalid firmware type.");
- ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_INIT_COMMAND_INVALID);
- }
- else if (!fw_hash_type_ok(p_init))
- {
- NRF_LOG_ERROR("Invalid hash type.");
- ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_WRONG_HASH_TYPE);
- }
- else if (!NRF_DFU_DEBUG ||
- (NRF_DFU_DEBUG && ((p_init->has_is_debug == false) || (p_init->is_debug == false))))
- {
- if (p_init->has_hw_version == false)
- {
- NRF_LOG_ERROR("No HW version.");
- ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_INIT_COMMAND_INVALID);
- }
- else if (p_init->hw_version != NRF_DFU_HW_VERSION)
- {
- NRF_LOG_WARNING("Faulty HW version.");
- ret_val = EXT_ERR( NRF_DFU_EXT_ERROR_HW_VERSION_FAILURE);
- }
- else if (!sd_req_ok(p_init))
- {
- NRF_LOG_WARNING("SD req not met.");
- ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_SD_VERSION_FAILURE);
- }
- else if (p_init->has_fw_version)
- {
- if (!fw_version_ok(p_init))
- {
- NRF_LOG_WARNING("FW version too low.");
- ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_FW_VERSION_FAILURE);
- }
- }
- else
- {
- if (fw_version_required(p_init->type))
- {
- NRF_LOG_ERROR("FW version missing.");
- ret_val = EXT_ERR(NRF_DFU_EXT_ERROR_INIT_COMMAND_INVALID);
- }
- }
- }
- return ret_val;
- }
|