@@ -448,21 +448,52 @@ endfunction()
448448function (_board_mgr_download url _file_path)
449449
450450 cmake_parse_arguments (parsed_args "QUIET;REQUIRED"
451- "RESULT_VARIABLE" "" ${ARGN} )
451+ "RESULT_VARIABLE;EXPECTED_HASH " "" ${ARGN} )
452452
453453 if (CMAKE_VERBOSE_MAKEFILE )
454454 message (STATUS "Downloading ${url} ..." )
455455 endif ()
456456
457- file (DOWNLOAD "${url} " "${_file_path} " STATUS _status
458- ${parsed_args_UNPARSED_ARGUMENTS} )
459- list (GET _status 0 _result)
460- list (GET _status 1 _result_str)
457+ set (_result 1)
458+ if (parsed_args_EXPECTED_HASH)
459+ string (REPLACE "=" ";" _hash_args "${parsed_args_EXPECTED_HASH} " )
460+ list (GET _hash_args 0 _hash_type)
461+ list (GET _hash_args 1 _hash_value)
462+ # Check hash before download
463+ if (EXISTS "${_file_path} " )
464+ _board_mgr_check_file_hash("${_hash_type} " "${_hash_value} "
465+ "${_file_path} " _result QUIET )
466+ endif ()
467+ endif ()
468+
469+ if (NOT _result EQUAL 0)
470+ file (DOWNLOAD "${url} " "${_file_path} " STATUS _status INACTIVITY_TIMEOUT 5
471+ ${parsed_args_UNPARSED_ARGUMENTS} )
472+ if (CMAKE_VERBOSE_MAKEFILE )
473+ message ("${_file_path} download status:${_status} " )
474+ endif ()
475+ list (GET _status 0 _result)
476+ list (GET _status 1 _result_str)
477+ else ()
478+ if (CMAKE_VERBOSE_MAKEFILE )
479+ message (STATUS "${_file_path} with correct hash exists already!" )
480+ endif ()
481+ set (_result 0)
482+ endif ()
483+
484+ # Check hash after download
485+ if (_result EQUAL 0 AND parsed_args_EXPECTED_HASH)
486+ _board_mgr_check_file_hash("${_hash_type} " "${_hash_value} "
487+ "${_file_path} " _result)
488+ set (_result_str "Download HASH mismatch" )
489+ endif ()
490+
461491 if (DEFINED parsed_args_RESULT_VARIABLE)
462492 set (${parsed_args_RESULT_VARIABLE} ${_result} PARENT_SCOPE)
463493 endif ()
464494
465495 if (NOT _result EQUAL 0)
496+ file (REMOVE "${_file_path} " )
466497 if (parsed_args_REQUIRED)
467498 error_exit("${_result_str} \n "
468499 "Downloading ${url} failed!!!" )
@@ -559,5 +590,34 @@ macro(_board_mgr_get_pl_tools_list)
559590
560591endmacro ()
561592
593+ # Check hash, returns 0
594+ function (_board_mgr_check_file_hash hash_type expected_hash file_path result_var)
595+
596+ cmake_parse_arguments (parsed_args "QUIET;REQUIRED"
597+ "" "" ${ARGN} )
598+
599+ string (TOLOWER "${expected_hash} " expected_hash )
600+ file ("${hash_type} " "${file_path} " _actual_hash)
601+ string (TOLOWER "${_actual_hash} " _actual_hash)
602+ if (NOT "${_actual_hash} " STREQUAL "${expected_hash} " )
603+ set ("${result_var} " 1 PARENT_SCOPE)
604+ if (NOT parsed_args_QUIET)
605+ execute_process (COMMAND ${CMAKE_COMMAND} -E echo
606+ "HASH mismatch for ${file_path} \n "
607+ " expected: [${expected_hash} ]\n "
608+ " actual: [${_actual_hash} ]" )
609+ endif ()
610+
611+ if (parsed_args_REQUIRED)
612+ message (SEND_ERROR "Hash match failed!!!" )
613+ elseif (NOT parsed_args_QUIET)
614+ message (WARNING "Hash match failed!!!" )
615+ endif ()
616+ return ()
617+ endif ()
618+
619+ set ("${result_var} " 0 PARENT_SCOPE)
620+ endfunction ()
621+
562622#libraries
563623
0 commit comments