LCOV - code coverage report
Current view: top level - basic_component/package_manager/src - package_loader.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.8 % 331 251
Test Date: 2026-07-28 10:52:48 Functions: 94.7 % 19 18

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2026 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "package_loader.h"
      12              : #include "package_manager.h"
      13              : #include <string>
      14              : #include <vector>
      15              : #include "driver/ascend_hal.h"
      16              : #include "driver/ascend_inpackage_hal.h"
      17              : #include "error_manager.h"
      18              : #include "tsd_log.h"
      19              : #include "tsd/status.h"
      20              : #include "weak_ascend_hal.h"
      21              : #include "tsd_util_func.h"
      22              : #include "env_internal_api.h"
      23              : #include "package_process_config.h"
      24              : #include "platform_info.h"
      25              : #include "hdc_message_builder.h"
      26              : 
      27              : namespace {
      28              : const std::string RUNTIME_PKG_NAME = "Ascend-runtime_device-minios.tar.gz";
      29              : const std::string DSHAPE_PKG_NAME = "Ascend-opp_rt-minios.aarch64.tar.gz";
      30              : const std::string UDF_PKG_NAME = "cann-udf-compat.tar.gz";
      31              : const std::string HCCD_PKG_NAME = "cann-hccd-compat.tar.gz";
      32              : const std::string HIXL_PKG_NAME = "cann-hixl-compat.tar.gz";
      33              : constexpr uint32_t OMFILE_LOAD_TIMEOUT = 200000U;
      34              : constexpr uint64_t HELPER_INPUT_MAX_FILE_PATH_LEN = 4096UL;
      35              : constexpr uint64_t HELPER_INPUT_MAX_FILE_NAME_LEN = 256UL;
      36              : const std::map<std::string, std::vector<tsd::ChipType_t>> PKG_CHIP_SUPPORT_MAP = {
      37              :     {"aicpu_hccl.tar.gz", {tsd::CHIP_ASCEND_910B, tsd::CHIP_ASCEND_950, tsd::CHIP_CLOUD_V5, tsd::CHIP_ASCEND_350}},
      38              :     {"mc2_server.tar.gz", {tsd::CHIP_ASCEND_950, tsd::CHIP_ASCEND_350}},
      39              :     {"aicpu_hcomm.tar.gz",
      40              :      {tsd::CHIP_DC, tsd::CHIP_ASCEND_910B, tsd::CHIP_ASCEND_950, tsd::CHIP_ASCEND_350, tsd::CHIP_CLOUD_V5}},
      41              :     {"cann-hcomm-compat.tar.gz",
      42              :      {tsd::CHIP_ASCEND_910B, tsd::CHIP_ASCEND_950, tsd::CHIP_ASCEND_350, tsd::CHIP_CLOUD_V5}},
      43              :     {HCCD_PKG_NAME, {tsd::CHIP_ASCEND_910B}},
      44              :     {"cann-tsch-compat.tar.gz", {}},
      45              :     {UDF_PKG_NAME, {tsd::CHIP_ASCEND_910B}},
      46              :     {HIXL_PKG_NAME, {tsd::CHIP_ASCEND_910B, tsd::CHIP_ASCEND_950, tsd::CHIP_ASCEND_350, tsd::CHIP_CLOUD_V5}}};
      47              : 
      48            3 : std::string ConstructVerifyPkgErrorReason(const std::string& loadPackageErrorMsg)
      49              : {
      50            3 :     std::string reason;
      51            3 :     if (loadPackageErrorMsg.find("does not match") != std::string::npos) {
      52            4 :         const std::string certType = tsd::ExtractSubString(loadPackageErrorMsg, "certType [", "]");
      53            3 :         const std::string verifyFlag = tsd::ExtractSubString(loadPackageErrorMsg, "verifyFlag [", "]");
      54            2 :         reason = "The current signature verification mode is [" + verifyFlag +
      55            2 :                  "], which does not match the actual digital signature [" + certType +
      56            1 :                  "] of the software package. Change the signature verification mode";
      57            3 :     } else if (loadPackageErrorMsg.find("verifyFlag is not [Close]") != std::string::npos) {
      58            3 :         const std::string verifyFlag = tsd::ExtractSubString(loadPackageErrorMsg, "verifyFlag [", "]");
      59              :         reason = "The signature verification mode has been enabled. However, the soft package does not "
      60              :                  "have a signature. Disable signature verification or use a software package whose "
      61            1 :                  "digital signature matches the current signature verification mode [" +
      62            1 :                  verifyFlag + "]";
      63            2 :     } else if (loadPackageErrorMsg.find("Signature verification failed") != std::string::npos) {
      64              :         reason = "Signature verification failed. The possible cause is that a multi-bit ECC error occurred "
      65              :                  "on the device or the software package has been tampered with. Obtain the device log, check whether "
      66            1 :                  "ECC errors are reported, and contact technical support at https://www.hiascend.com/support";
      67              :     }
      68            3 :     return reason;
      69            0 : }
      70              : } // namespace
      71              : 
      72              : namespace tsd {
      73              : 
      74          263 : PackageLoader::PackageLoader(
      75              :     PackageManager& mgr, DeviceCommAgent& commAgent, CapabilityManager& capabilityMgr, PackageEnvInfo& envInfo,
      76          263 :     PackageHashStore& hashStore, ResponseCode& pkgRspCode, std::string& loadPackageErrorMsg)
      77          263 :     : mgr_(mgr),
      78          263 :       commAgent_(commAgent),
      79          263 :       capabilityMgr_(capabilityMgr),
      80          263 :       envInfo_(envInfo),
      81          263 :       hashStore_(hashStore),
      82          263 :       pkgRspCode_(pkgRspCode),
      83          263 :       loadPackageErrorMsg_(loadPackageErrorMsg)
      84          263 : {}
      85              : 
      86           23 : void PackageLoader::Reset()
      87              : {
      88           23 :     aicpuPackageExistInDevice_ = false;
      89           23 :     hasSendConfigFile_ = false;
      90           23 :     hashStore_.Clear();
      91           23 : }
      92              : 
      93           31 : TSD_StatusT PackageLoader::LoadSysOpKernel()
      94              : {
      95           31 :     const bool loadAicpuKernelFlag = ShouldLoadLegacyPackage();
      96           31 :     if (!mgr_.CheckPackageExists(loadAicpuKernelFlag)) {
      97           20 :         TSD_RUN_INFO("[TsdClient][logicDeviceId_=%u] cannot find aicpu packages", envInfo_.GetLogicDeviceId());
      98           20 :         return TSD_OK;
      99              :     }
     100              : 
     101           11 :     TSD_StatusT ret = mgr_.GetDeviceCheckCode();
     102           11 :     if (ret == TSD_AICPUPACKAGE_EXISTED) {
     103            1 :         return TSD_OK;
     104              :     }
     105           10 :     if (ret != TSD_OK) {
     106            2 :         return (ret >= TSD_SUBPROCESS_NUM_EXCEED_THE_LIMIT) ? ret : TSD_DEVICEID_ERROR;
     107              :     }
     108              : 
     109            8 :     if (envInfo_.GetPlatInfoMode() == static_cast<uint32_t>(ModeType::OFFLINE)) {
     110            2 :         TSD_RUN_INFO("[TsdClient]in process offline mode, ignore send any package!");
     111            2 :         return TSD_OK;
     112              :     }
     113              : 
     114            6 :     return SendAllPackagesToPeer();
     115              : }
     116              : 
     117           31 : bool PackageLoader::ShouldLoadLegacyPackage() const
     118              : {
     119           31 :     if (!capabilityMgr_.IsSupportCommonSink()) {
     120           31 :         return true;
     121              :     }
     122            0 :     std::string packageTitle;
     123            0 :     (void)envInfo_.GetPackageTitle(packageTitle);
     124            0 :     const std::string pkgName = packageTitle + "-aicpu_legacy.tar.gz";
     125            0 :     if (PackageProcessConfig::GetInstance()->GetPackageHostTruePath(pkgName).empty()) {
     126            0 :         return true;
     127              :     }
     128            0 :     TSD_RUN_INFO("[TsdClient][logicDeviceId_=%u] use legacy package", envInfo_.GetLogicDeviceId());
     129            0 :     return false;
     130            0 : }
     131              : 
     132            6 : TSD_StatusT PackageLoader::SendAllPackagesToPeer()
     133              : {
     134            6 :     constexpr int32_t peerNode = 0;
     135            6 :     char_t drvPath[256U] = {};
     136              :     const int32_t drvRet =
     137            6 :         drvHdcGetTrustedBasePath(peerNode, static_cast<int32_t>(envInfo_.GetLogicDeviceId()), &drvPath[0], 256U);
     138            6 :     if (drvRet != DRV_ERROR_NONE) {
     139            1 :         TSD_ERROR(
     140              :             "[TsdClient][deviceId_=%u] drvHdcGetTrustedBasePath failed, ret[%d]", envInfo_.GetLogicDeviceId(), drvRet);
     141            1 :         return TSD_INTERNAL_ERROR;
     142              :     }
     143            5 :     const std::string basePath(drvPath);
     144              : 
     145            5 :     TSD_StatusT ret = mgr_.SendAICPUPackage(peerNode, basePath);
     146            5 :     if (ret != TSD_OK) {
     147              :         REPORT_INPUT_ERROR("E39006", std::vector<std::string>(), std::vector<std::string>());
     148            1 :         TSD_ERROR("[TsdClient][deviceId=%u] send aicpu package to device failed", envInfo_.GetLogicDeviceId());
     149            1 :         return ret;
     150              :     }
     151              : 
     152            4 :     ret = mgr_.SendCommonPackage(
     153              :         peerNode, basePath, static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_EXTEND_KERNEL));
     154            4 :     if (ret != TSD_OK) {
     155              :         REPORT_INPUT_ERROR("E39006", std::vector<std::string>(), std::vector<std::string>());
     156            1 :         TSD_ERROR("[TsdClient][deviceId=%u] send extend package to device failed", envInfo_.GetLogicDeviceId());
     157            1 :         return ret;
     158              :     }
     159              : 
     160            3 :     ret = mgr_.SendCommonPackage(peerNode, basePath, static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_ASCENDCPP));
     161            3 :     if (ret != TSD_OK) {
     162            0 :         TSD_ERROR("[TsdClient][deviceId=%u] send ascendcpp package to device failed", envInfo_.GetLogicDeviceId());
     163              :     }
     164            3 :     return ret;
     165            5 : }
     166              : 
     167            4 : TSD_StatusT PackageLoader::LoadHsPkgToDevice(
     168              :     const std::string& pkgName, const std::string& subPath, TsdLoadPackageType pkgType, HDCMessage::MsgType msgType,
     169              :     const MessageContext& baseCtx)
     170              : {
     171            4 :     std::string basePath;
     172            4 :     envInfo_.GetAscendLatestIntallPath(basePath);
     173            4 :     if (!basePath.empty() && basePath.back() == '/') {
     174            4 :         basePath = basePath + subPath;
     175              :     } else {
     176            0 :         basePath = basePath + "/" + subPath;
     177              :     }
     178            4 :     const std::string fullPkgPath = basePath + pkgName;
     179            4 :     TSD_INFO("target pkg path:%s pkgName:%s", basePath.c_str(), fullPkgPath.c_str());
     180            4 :     if ((mmAccess(basePath.c_str()) != EN_OK) || (mmIsDir(basePath.c_str()) != EN_OK)) {
     181            0 :         TSD_ERROR("[TsdClient] path[%s] does not exist, deviceId[%u]", basePath.c_str(), envInfo_.GetLogicDeviceId());
     182            0 :         return TSD_INTERNAL_ERROR;
     183              :     }
     184            4 :     if (mmAccess(fullPkgPath.c_str()) != EN_OK) {
     185            0 :         TSD_ERROR(
     186              :             "[TsdClient] file[%s] does not exist, deviceId[%u]", fullPkgPath.c_str(), envInfo_.GetLogicDeviceId());
     187            0 :         return TSD_INTERNAL_ERROR;
     188              :     }
     189            4 :     mgr_.packagePeerCheckCode_[static_cast<uint32_t>(pkgType)] = 0U;
     190            4 :     const uint32_t checkCode = CalFileSize(fullPkgPath.c_str());
     191              :     const auto ret =
     192            4 :         mgr_.SendFileToDevice(basePath.c_str(), basePath.length(), pkgName.c_str(), pkgName.length(), true);
     193            4 :     TSD_CHECK(ret == TSD_OK, ret, "send pkg to device failed.");
     194            4 :     if (mgr_.GetDeviceHsPkgCheckCode(checkCode, msgType, false, baseCtx) != TSD_OK) {
     195            0 :         TSD_ERROR("GetDeviceHsPkgCheckCode failed");
     196            0 :         return TSD_INTERNAL_ERROR;
     197              :     }
     198            4 :     if (checkCode != mgr_.packagePeerCheckCode_[static_cast<uint32_t>(pkgType)]) {
     199            2 :         TSD_ERROR(
     200              :             "checode verify is failed checkCode:%u, peerCheckCode:%u", checkCode,
     201              :             mgr_.packagePeerCheckCode_[static_cast<uint32_t>(pkgType)]);
     202            2 :         return TSD_INTERNAL_ERROR;
     203              :     }
     204            2 :     return TSD_OK;
     205            4 : }
     206              : 
     207            9 : TSD_StatusT PackageLoader::LoadRuntimePkgToDevice(const MessageContext& baseCtx)
     208              : {
     209            9 :     if (capabilityMgr_.IsSupportCommonSink() && (&drvHdcSendFileV2 != nullptr) &&
     210              :         (&drvHdcGetTrustedBasePathV2 != nullptr)) {
     211            7 :         (void)mgr_.LoadPackageConfigInfoToDevice(false);
     212            7 :         if (mgr_.LoadCannHsPkgToDevice(UDF_PKG_NAME, baseCtx) != TSD_OK) {
     213            6 :             TSD_ERROR("Load package failed, package:%s", UDF_PKG_NAME.c_str());
     214            6 :             return TSD_INTERNAL_ERROR;
     215              :         }
     216            1 :         if (mgr_.LoadCannHsPkgToDevice(HCCD_PKG_NAME, baseCtx) != TSD_OK) {
     217            0 :             TSD_ERROR("Load package failed, package:%s", HCCD_PKG_NAME.c_str());
     218            0 :             return TSD_INTERNAL_ERROR;
     219              :         }
     220            1 :         TSD_RUN_INFO("Load udf and hccd package success.");
     221            1 :         return TSD_OK;
     222              :     }
     223              : 
     224            6 :     return this->LoadHsPkgToDevice(
     225              :         RUNTIME_PKG_NAME, "runtime/lib64/", TsdLoadPackageType::TSD_PKG_TYPE_RUNTIME,
     226            2 :         HDCMessage::TSD_GET_DEVICE_RUNTIME_CHECKCODE, baseCtx);
     227              : }
     228              : 
     229            8 : TSD_StatusT PackageLoader::LoadCannHsPkgToDevice(const std::string& pkgPureName, const MessageContext& baseCtx)
     230              : {
     231            8 :     int32_t peerNode = 0;
     232            8 :     const std::string dstDirPreFix = envInfo_.GetTrustedBasePath(true);
     233              : 
     234            8 :     PackageProcessConfig* pkgConInst = PackageProcessConfig::GetInstance();
     235            8 :     std::string orgFile;
     236            8 :     std::string dstFile = dstDirPreFix;
     237            8 :     if (pkgConInst->GetPkgHostAndDeviceDstPath(pkgPureName, orgFile, dstFile, commAgent_.GetProcSign().tgid) !=
     238              :         TSD_OK) {
     239            0 :         return TSD_INTERNAL_ERROR;
     240              :     }
     241            8 :     if (orgFile.empty()) {
     242            0 :         TSD_RUN_WARN("cannot find package:%s, optional is true skip", pkgPureName.c_str());
     243            0 :         return TSD_OK;
     244              :     }
     245              : 
     246            8 :     const std::string hostHash = CalFileSha256HashValue(orgFile);
     247            8 :     hashStore_.SetHostCommonSinkPackHashValue(pkgPureName, hostHash);
     248            8 :     if (mgr_.IsCommonSinkHostAndDevicePkgSame(pkgPureName)) {
     249            1 :         TSD_INFO("current package:%s is same as device, skip load", pkgPureName.c_str());
     250            1 :         return TSD_OK;
     251              :     }
     252              : 
     253            7 :     if (mgr_.LoadFileAndWaitRsp(pkgPureName, hostHash, peerNode, orgFile, dstFile, baseCtx) != TSD_OK) {
     254            3 :         TSD_ERROR("compare and send package to device failed");
     255            3 :         return TSD_INTERNAL_ERROR;
     256              :     }
     257              : 
     258            4 :     if (static_cast<uint32_t>(pkgRspCode_) != 0U) {
     259            3 :         TSD_ERROR("host and device check code compare failed, package:%s", pkgPureName.c_str());
     260            3 :         return TSD_INTERNAL_ERROR;
     261              :     }
     262              : 
     263            1 :     TSD_RUN_INFO("Load package success, package:%s", pkgPureName.c_str());
     264            1 :     return TSD_OK;
     265            8 : }
     266              : 
     267            7 : TSD_StatusT PackageLoader::LoadFileAndWaitRsp(
     268              :     const std::string& pkgPureName, const std::string& hostPkgHash, const int32_t peerNode, const std::string& orgFile,
     269              :     const std::string& dstFile, const MessageContext& baseCtx)
     270              : {
     271            7 :     if (mgr_.SendAICPUPackageSimple(peerNode, orgFile, dstFile, true) != TSD_OK) {
     272            1 :         TSD_ERROR("send package to device failed, package:%s", pkgPureName.c_str());
     273            1 :         return TSD_INTERNAL_ERROR;
     274              :     }
     275              : 
     276            6 :     if (mgr_.GetCannHsPkgCheckCode(pkgPureName, hostPkgHash, baseCtx) != TSD_OK) {
     277            2 :         TSD_ERROR("get check code from device failed, package:%s", pkgPureName.c_str());
     278            2 :         return TSD_INTERNAL_ERROR;
     279              :     }
     280              : 
     281            4 :     return TSD_OK;
     282              : }
     283              : 
     284            2 : TSD_StatusT PackageLoader::LoadDShapePkgToDevice(const MessageContext& baseCtx)
     285              : {
     286            6 :     return this->LoadHsPkgToDevice(
     287              :         DSHAPE_PKG_NAME, "ops/built-in/", TsdLoadPackageType::TSD_PKG_TYPE_DSHAPE,
     288            4 :         HDCMessage::TSD_GET_DEVICE_DSHAPE_CHECKCODE, baseCtx);
     289              : }
     290              : 
     291            0 : TSD_StatusT PackageLoader::LoadOmFileToDevice(
     292              :     const char_t* const filePath, const uint64_t pathLen, const char_t* const fileName, const uint64_t fileNameLen,
     293              :     const MessageContext& baseCtx)
     294              : {
     295            0 :     if ((filePath == nullptr) || (pathLen == 0UL) || (pathLen >= HELPER_INPUT_MAX_FILE_PATH_LEN)) {
     296            0 :         TSD_ERROR("input param is error");
     297            0 :         return TSD_INTERNAL_ERROR;
     298              :     }
     299              :     try {
     300            0 :         std::string filePathStr(filePath, pathLen);
     301            0 :         TSD_RUN_INFO("file path is:%s", filePathStr.c_str());
     302            0 :     } catch (std::exception& e) {
     303            0 :         TSD_ERROR("input str is invalid reason:%s", e.what());
     304            0 :         return TSD_INTERNAL_ERROR;
     305            0 :     }
     306            0 :     auto ret = mgr_.SendFileToDevice(filePath, pathLen, fileName, fileNameLen, true);
     307            0 :     TSD_CHECK(ret == TSD_OK, ret, "SendFileToDevice failed.");
     308              : 
     309            0 :     ret = mgr_.InitTsdClient();
     310            0 :     TSD_CHECK(ret == TSD_OK, ret, "Init hdc client failed.");
     311            0 :     TSD_CHECK_NULLPTR(commAgent_.GetDeviceComm(), TSD_INSTANCE_NOT_FOUND, "devCommClient_ is null in send function");
     312            0 :     std::string curFile(fileName, fileNameLen);
     313            0 :     HDCMessage msg;
     314            0 :     const std::string curPid = std::to_string(commAgent_.GetProcSign().tgid);
     315            0 :     curFile = curPid + "_" + curFile;
     316            0 :     TSD_INFO("Load om file name:%s to device logic device id:%u", curFile.c_str(), envInfo_.GetLogicDeviceId());
     317            0 :     MessageContext ctx = baseCtx;
     318            0 :     ctx.omfileName = curFile;
     319            0 :     ret = HdcMessageBuilder::BuildOmFileDecompress(msg, ctx);
     320            0 :     TSD_CHECK(ret == TSD_OK, ret, "build TSD_OM_PKG_DECOMPRESS_STATUS msg failed.");
     321            0 :     ret = commAgent_.SendMsg(msg);
     322            0 :     TSD_CHECK(ret == TSD_OK, ret, "send TSD_OM_PKG_DECOMPRESS_STATUS msg failed.");
     323            0 :     ret = mgr_.WaitPkgRsp(OMFILE_LOAD_TIMEOUT);
     324            0 :     TSD_CHECK(ret == TSD_OK, ret, "Wait TSD_OM_PKG_DECOMPRESS_STATUS response from device failed.");
     325            0 :     TSD_INFO("LoadOmFileToDevice success filepath:%s, filename:%s", filePath, fileName);
     326            0 :     return TSD_OK;
     327            0 : }
     328              : 
     329            1 : TSD_StatusT PackageLoader::LoadFileToDevice(
     330              :     const char_t* const filePath, const uint64_t pathLen, const char_t* const fileName, const uint64_t fileNameLen,
     331              :     const MessageContext& baseCtx)
     332              : {
     333            1 :     if (!mgr_.IsOkToLoadFileToDevice(fileName, fileNameLen)) {
     334            1 :         TSD_ERROR("IsOkToLoadFileToDevice is false");
     335            1 :         return TSD_INTERNAL_ERROR;
     336              :     }
     337            0 :     const std::string loadFile(fileName, fileNameLen);
     338            0 :     TSD_RUN_INFO("begin load file:%s", loadFile.c_str());
     339            0 :     if (loadFile == RUNTIME_PKG_NAME) {
     340            0 :         return mgr_.LoadRuntimePkgToDevice(baseCtx);
     341            0 :     } else if (loadFile == DSHAPE_PKG_NAME) {
     342            0 :         return mgr_.LoadDShapePkgToDevice(baseCtx);
     343              :     } else {
     344            0 :         return mgr_.LoadOmFileToDevice(filePath, pathLen, fileName, fileNameLen, baseCtx);
     345              :     }
     346            0 : }
     347              : 
     348            2 : bool PackageLoader::IsOkToLoadFileToDevice(const char_t* const fileName, const uint64_t fileNameLen) const
     349              : {
     350            2 :     if ((fileName == nullptr) || (fileNameLen == 0UL) || (fileNameLen >= HELPER_INPUT_MAX_FILE_NAME_LEN)) {
     351            1 :         TSD_ERROR("input param is error");
     352            1 :         return false;
     353              :     }
     354            1 :     if (!capabilityMgr_.IsSupportCommonInterface(TSD_SUPPORT_HS_AISERVER_FEATURE_BIT)) {
     355            1 :         TSD_ERROR("cur device does not support heterogeneous AIServer");
     356            1 :         return false;
     357              :     }
     358              :     try {
     359            0 :         std::string loadFile(fileName, fileNameLen);
     360            0 :         TSD_RUN_INFO("loadfile name:%s", loadFile.c_str());
     361            0 :     } catch (std::exception& e) {
     362            0 :         TSD_ERROR("input fileName is invalid reason:%s", e.what());
     363            0 :         return false;
     364            0 :     }
     365            0 :     return true;
     366              : }
     367              : 
     368           25 : TSD_StatusT PackageLoader::LoadPackageConfigInfoToDevice(const bool hasPluginVersion)
     369              : {
     370           31 :     if ((capabilityMgr_.IsSupportCommonSink() == false) || (&drvHdcSendFileV2 == nullptr) ||
     371           31 :         (&drvHdcGetTrustedBasePathV2 == nullptr) || envInfo_.IsAdcEnv() || (&halGetSocVersion == nullptr)) {
     372           19 :         TSD_RUN_INFO("device does not support common sink package config info");
     373           19 :         return TSD_OK;
     374              :     }
     375              : 
     376            6 :     if (hasSendConfigFile_) {
     377            0 :         TSD_RUN_INFO(
     378              :             "The package config file has send to device, no need send again. deviceId=%u", envInfo_.GetLogicDeviceId());
     379            0 :         return TSD_OK;
     380              :     }
     381              : 
     382            6 :     TSD_StatusT ret = mgr_.InitTsdClient();
     383            6 :     if (ret != TSD_OK) {
     384            1 :         TSD_ERROR("[TsdClient][deviceId_=%u] InitTsdClient failed, ret[%d]", envInfo_.GetLogicDeviceId(), ret);
     385            1 :         return TSD_INTERNAL_ERROR;
     386              :     }
     387            5 :     TSD_CHECK_NULLPTR(commAgent_.GetDeviceComm(), TSD_INSTANCE_NOT_FOUND, "devCommClient is null");
     388            5 :     std::string packageTitle;
     389            5 :     (void)envInfo_.GetPackageTitle(packageTitle);
     390            5 :     std::string shortSocVersion;
     391            5 :     (void)envInfo_.GetShortSocVersion(shortSocVersion);
     392            5 :     packageTitle = packageTitle + ";" + shortSocVersion;
     393            5 :     PackageProcessConfig* pkgConInst = PackageProcessConfig::GetInstance();
     394            5 :     ret = pkgConInst->ParseConfigDataFromFile(packageTitle);
     395            5 :     if (ret != TSD_OK) {
     396            0 :         TSD_ERROR("Parsing config data was not successful");
     397            0 :         return TSD_INTERNAL_ERROR;
     398              :     }
     399            5 :     if (hasPluginVersion) {
     400            0 :         pkgConInst->RefreshHostPluginVersions();
     401              :     }
     402              : 
     403            5 :     MessageContext ctx{};
     404            5 :     ctx.logicDeviceId = envInfo_.GetLogicDeviceId();
     405            5 :     ctx.procSign = commAgent_.GetProcSign();
     406            5 :     HDCMessage msg;
     407            5 :     if (HdcMessageBuilder::BuildUpdatePackageConfig(msg, ctx) != TSD_OK) {
     408            0 :         TSD_ERROR("build update package config msg failed");
     409            0 :         return TSD_INTERNAL_ERROR;
     410              :     }
     411            5 :     pkgConInst->ConstructPkgConfigMsg(msg);
     412              : 
     413            5 :     ret = commAgent_.SendMsg(msg);
     414            5 :     if (ret != TSD_OK) {
     415            0 :         TSD_ERROR("Send update package config message failed.");
     416            0 :         return ret;
     417              :     }
     418              : 
     419            5 :     ret = commAgent_.RecvData();
     420            5 :     TSD_RUN_INFO("Receive load package config response result:%u", ret);
     421              : 
     422            5 :     hasSendConfigFile_ = true;
     423              : 
     424            5 :     return TSD_OK;
     425            5 : }
     426              : 
     427            8 : bool PackageLoader::SupportLoadPkg(const std::string& pkgName) const
     428              : {
     429            8 :     const auto iter = PKG_CHIP_SUPPORT_MAP.find(pkgName);
     430            8 :     if (iter == PKG_CHIP_SUPPORT_MAP.end()) {
     431            5 :         TSD_INFO("current package:%s does not need to check chiptype", pkgName.c_str());
     432            5 :         return true;
     433              :     }
     434              : 
     435            3 :     if ((pkgName == UDF_PKG_NAME) || (pkgName == HCCD_PKG_NAME)) {
     436            1 :         TSD_INFO("package is not need load, name:%s", pkgName.c_str());
     437            1 :         return false;
     438              :     }
     439              : 
     440            2 :     for (const auto chip : iter->second) {
     441            1 :         if (envInfo_.GetPlatInfoChipType() == static_cast<uint32_t>(chip)) {
     442            1 :             TSD_INFO("current device chip:%u support package:%s", envInfo_.GetPlatInfoChipType(), pkgName.c_str());
     443            1 :             return true;
     444              :         }
     445              :     }
     446              : 
     447            1 :     TSD_RUN_INFO("current device chip:%u does not support package:%s", envInfo_.GetPlatInfoChipType(), pkgName.c_str());
     448            1 :     return false;
     449              : }
     450              : 
     451           27 : TSD_StatusT PackageLoader::LoadSinglePackageToDevice(
     452              :     const std::string& pkgPureName, const PackConfDetail& detail, int32_t peerNode, const std::string& dstDirPreFix)
     453              : {
     454           27 :     PackageProcessConfig* pkgConInst = PackageProcessConfig::GetInstance();
     455           27 :     std::string orgFile;
     456           27 :     std::string dstFile = dstDirPreFix;
     457           27 :     TSD_RUN_INFO("begin to load package:%s to device:%u", pkgPureName.c_str(), envInfo_.GetLogicDeviceId());
     458           27 :     if ((!detail.loadAsPerSocFlag) && (!mgr_.SupportLoadPkg(pkgPureName))) {
     459            1 :         TSD_RUN_INFO(
     460              :             "current package:%s does not need to load to device:%u", pkgPureName.c_str(), envInfo_.GetLogicDeviceId());
     461            1 :         return TSD_OK;
     462              :     }
     463           28 :     if ((pkgPureName == "cann-hcomm-compat.tar.gz") && (envInfo_.GetPlatInfoChipType() == tsd::CHIP_ASCEND_910B) &&
     464            2 :         (!capabilityMgr_.IsSupportCommonInterface(TSD_SUPPORT_CANN_HCOMM_COMPAT_910B))) {
     465            1 :         TSD_RUN_INFO(
     466              :             "device does not support cann-hcomm-compat package, skip load to device:%u", envInfo_.GetLogicDeviceId());
     467            1 :         return TSD_OK;
     468              :     }
     469           25 :     if (pkgConInst->GetPkgHostAndDeviceDstPath(pkgPureName, orgFile, dstFile, commAgent_.GetProcSign().tgid) !=
     470              :         TSD_OK) {
     471            3 :         return TSD_INTERNAL_ERROR;
     472              :     }
     473           22 :     if (orgFile.empty()) {
     474           15 :         TSD_RUN_INFO("cannot find package:%s, optional is true skip", pkgPureName.c_str());
     475           15 :         return TSD_OK;
     476              :     }
     477            7 :     const std::string hostPkgHash = CalFileSha256HashValue(orgFile);
     478            7 :     hashStore_.SetHostCommonSinkPackHashValue(pkgPureName, hostPkgHash);
     479            7 :     if (mgr_.IsCompatPluginPackage(detail) && !mgr_.ShouldLoadCompatPluginPkg(pkgPureName)) {
     480            0 :         TSD_RUN_INFO("skip load compat plugin package:%s by version/strategy check", pkgPureName.c_str());
     481            0 :         return TSD_OK;
     482              :     }
     483            7 :     if (!mgr_.IsCompatPluginPackage(detail) && mgr_.IsCommonSinkHostAndDevicePkgSame(pkgPureName)) {
     484            1 :         TSD_RUN_INFO("current package:%s is same as device, skip load", pkgPureName.c_str());
     485            1 :         return TSD_OK;
     486              :     }
     487            6 :     if (mgr_.CompareAndSendCommonSinkPkg(pkgPureName, hostPkgHash, peerNode, orgFile, dstFile) != TSD_OK) {
     488            1 :         TSD_ERROR("compare and send package to device failed package:%s", pkgPureName.c_str());
     489            1 :         return TSD_INTERNAL_ERROR;
     490              :     }
     491            5 :     if (static_cast<uint32_t>(pkgRspCode_) != 0U) {
     492            4 :         this->ReportSinkPkgRspError(pkgPureName);
     493            4 :         return TSD_INTERNAL_ERROR;
     494              :     }
     495            1 :     TSD_RUN_INFO("load package:%s to device:%u success", pkgPureName.c_str(), envInfo_.GetLogicDeviceId());
     496            1 :     return TSD_OK;
     497           27 : }
     498              : 
     499            4 : void PackageLoader::ReportSinkPkgRspError(const std::string& pkgPureName)
     500              : {
     501            4 :     bool reportedFlag = false;
     502            4 :     if (!loadPackageErrorMsg_.empty()) {
     503            4 :         TSD_ERROR("[Device error message] %s", loadPackageErrorMsg_.c_str());
     504            4 :         const bool isCmsVerifyFail = (loadPackageErrorMsg_.find("cms verify failed") != std::string::npos);
     505              :         const std::string reason =
     506            4 :             isCmsVerifyFail ? ConstructVerifyPkgErrorReason(loadPackageErrorMsg_) : std::string{};
     507            4 :         if (!reason.empty()) {
     508           21 :             const std::vector<std::string> keys{"package_name", "reason"};
     509           12 :             const std::vector<std::string> values{pkgPureName, reason};
     510              :             REPORT_INPUT_ERROR("E30009", keys, values);
     511            3 :             reportedFlag = true;
     512            3 :         }
     513            4 :         loadPackageErrorMsg_ = "";
     514            4 :     }
     515            4 :     if (!reportedFlag) {
     516              :         REPORT_INPUT_ERROR("E39011", std::vector<std::string>{"package_name"}, std::vector<std::string>{pkgPureName});
     517              :     }
     518            4 :     TSD_ERROR("host and device checkcode compare failed package:%s", pkgPureName.c_str());
     519            7 : }
     520              : 
     521           29 : TSD_StatusT PackageLoader::LoadPackageToDeviceByConfig()
     522              : {
     523           37 :     if ((capabilityMgr_.IsSupportCommonSink() == false) || (&drvHdcSendFileV2 == nullptr) ||
     524           37 :         (&drvHdcGetTrustedBasePathV2 == nullptr) || envInfo_.IsAdcEnv()) {
     525           21 :         TSD_RUN_INFO("device does not support common sink package config info");
     526           21 :         return TSD_OK;
     527              :     }
     528              : 
     529            8 :     int32_t peerNode = 0;
     530            8 :     std::string dstDirPreFix;
     531            8 :     if (envInfo_.GetTrustedBasePathFromDevice(peerNode, dstDirPreFix) != TSD_OK) {
     532            0 :         return TSD_INTERNAL_ERROR;
     533              :     }
     534              : 
     535            8 :     PackageProcessConfig* pkgConInst = PackageProcessConfig::GetInstance();
     536            8 :     std::map<std::string, PackConfDetail> configMap = pkgConInst->GetAllPackageConfigInfo();
     537              : 
     538           26 :     for (auto& entry : configMap) {
     539           23 :         if (mgr_.LoadSinglePackageToDevice(entry.first, entry.second, peerNode, dstDirPreFix) != TSD_OK) {
     540            5 :             return TSD_INTERNAL_ERROR;
     541              :         }
     542              :     }
     543              : 
     544            3 :     return TSD_OK;
     545            8 : }
     546              : 
     547              : } // namespace tsd
        

Generated by: LCOV version 2.0-1