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