1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "update_policy_utils.h"
17
18 #include <unistd.h>
19
20 #include "securec.h"
21
22 #include "edm_constants.h"
23 #include "edm_log.h"
24
25 namespace OHOS {
26 namespace EDM {
27
28 constexpr uint32_t MAX_PACKAGES_SIZE = 10;
ProcessUpdatePolicyType(int32_t type,UpdatePolicyType & updatePolicyType)29 bool UpdatePolicyUtils::ProcessUpdatePolicyType(int32_t type, UpdatePolicyType &updatePolicyType)
30 {
31 if (type >= static_cast<int32_t>(UpdatePolicyType::DEFAULT) &&
32 type <= static_cast<int32_t>(UpdatePolicyType::POSTPONE)) {
33 updatePolicyType = static_cast<UpdatePolicyType>(type);
34 return true;
35 }
36 return false;
37 }
38
ProcessPackageType(int32_t type,PackageType & packageType)39 void UpdatePolicyUtils::ProcessPackageType(int32_t type, PackageType &packageType)
40 {
41 packageType = PackageType::UNKNOWN;
42 if (type == static_cast<int32_t>(PackageType::FIRMWARE)) {
43 packageType = static_cast<PackageType>(type);
44 }
45 }
46
ProcessUpgradeStatus(int32_t status,UpgradeStatus & upgradeStatus)47 void UpdatePolicyUtils::ProcessUpgradeStatus(int32_t status, UpgradeStatus &upgradeStatus)
48 {
49 upgradeStatus = UpgradeStatus::UPGRADE_SUCCESS;
50 if (status >= static_cast<int32_t>(UpgradeStatus::NO_UPGRADE_PACKAGE) &&
51 status <= static_cast<int32_t>(UpgradeStatus::UPGRADE_SUCCESS)) {
52 upgradeStatus = static_cast<UpgradeStatus>(status);
53 }
54 }
55
WriteUpdatePolicy(MessageParcel & data,const UpdatePolicy & updatePolicy)56 void UpdatePolicyUtils::WriteUpdatePolicy(MessageParcel &data, const UpdatePolicy &updatePolicy)
57 {
58 data.WriteInt32(static_cast<int32_t>(updatePolicy.type));
59 data.WriteString(updatePolicy.version);
60 data.WriteInt64(updatePolicy.installTime.latestUpdateTime);
61 data.WriteInt64(updatePolicy.installTime.delayUpdateTime);
62 data.WriteInt64(updatePolicy.installTime.installWindowStart);
63 data.WriteInt64(updatePolicy.installTime.installWindowEnd);
64 }
65
ReadUpdatePolicy(MessageParcel & data,UpdatePolicy & updatePolicy)66 void UpdatePolicyUtils::ReadUpdatePolicy(MessageParcel &data, UpdatePolicy &updatePolicy)
67 {
68 UpdatePolicyUtils::ProcessUpdatePolicyType(data.ReadInt32(), updatePolicy.type);
69 data.ReadString(updatePolicy.version);
70 data.ReadInt64(updatePolicy.installTime.latestUpdateTime);
71 data.ReadInt64(updatePolicy.installTime.delayUpdateTime);
72 data.ReadInt64(updatePolicy.installTime.installWindowStart);
73 data.ReadInt64(updatePolicy.installTime.installWindowEnd);
74 }
75
WriteUpgradePackageInfo(MessageParcel & data,UpgradePackageInfo & packageInfo)76 void UpdatePolicyUtils::WriteUpgradePackageInfo(MessageParcel &data, UpgradePackageInfo &packageInfo)
77 {
78 data.WriteString(packageInfo.version);
79 data.WriteUint32(packageInfo.packages.size());
80 for (auto package : packageInfo.packages) {
81 data.WriteInt32(static_cast<int32_t>(package.type));
82 data.WriteString(package.path);
83 data.WriteFileDescriptor(package.fd);
84 }
85 data.WriteString(packageInfo.description.notify.installTips);
86 data.WriteString(packageInfo.description.notify.installTipsDetail);
87 data.WriteUint32(packageInfo.authInfoSize);
88 if (packageInfo.authInfoSize >= EdmConstants::AUTH_INFO_MAX_SIZE) {
89 EDMLOGE("UpdatePolicyUtils::WriteUpgradePackageInfo auth info too long.");
90 return;
91 }
92 data.WriteCString(packageInfo.authInfo);
93 errno_t err = memset_s(packageInfo.authInfo, sizeof(packageInfo.authInfo), 0, sizeof(packageInfo.authInfo));
94 if (err != EOK) {
95 EDMLOGE("UpdatePolicyUtils::WriteUpgradePackageInfo memset_s failed: %{public}d.", err);
96 }
97 }
98
ReadUpgradePackageInfo(MessageParcel & data,UpgradePackageInfo & packageInfo)99 void UpdatePolicyUtils::ReadUpgradePackageInfo(MessageParcel &data, UpgradePackageInfo &packageInfo)
100 {
101 data.ReadString(packageInfo.version);
102 size_t size = data.ReadUint32();
103 if (size > MAX_PACKAGES_SIZE) {
104 return;
105 }
106 for (size_t i = 0; i < size; i++) {
107 Package package;
108 ProcessPackageType(data.ReadInt32(), package.type);
109 data.ReadString(package.path);
110 package.fd = data.ReadFileDescriptor();
111 packageInfo.packages.push_back(package);
112 }
113 data.ReadString(packageInfo.description.notify.installTips);
114 data.ReadString(packageInfo.description.notify.installTipsDetail);
115 data.ReadUint32(packageInfo.authInfoSize);
116 if (packageInfo.authInfoSize >= EdmConstants::AUTH_INFO_MAX_SIZE) {
117 EDMLOGE("UpdatePolicyUtils::ReadUpgradePackageInfo auth info too long.");
118 return;
119 }
120 errno_t err = memcpy_s(packageInfo.authInfo, sizeof(packageInfo.authInfo), data.ReadCString(),
121 packageInfo.authInfoSize);
122 if (err != EOK) {
123 EDMLOGE("UpdatePolicyUtils::ReadUpgradePackageInfo memset_s failed: %{public}d.", err);
124 }
125 }
126
WriteUpgradeResult(MessageParcel & data,const UpgradeResult & result)127 void UpdatePolicyUtils::WriteUpgradeResult(MessageParcel &data, const UpgradeResult &result)
128 {
129 data.WriteString(result.version);
130 data.WriteInt32(static_cast<int32_t>(result.status));
131 data.WriteInt32(result.errorCode);
132 data.WriteString(result.errorMessage);
133 }
134
ReadUpgradeResult(MessageParcel & data,UpgradeResult & result)135 void UpdatePolicyUtils::ReadUpgradeResult(MessageParcel &data, UpgradeResult &result)
136 {
137 data.ReadString(result.version);
138 ProcessUpgradeStatus(data.ReadInt32(), result.status);
139 data.ReadInt32(result.errorCode);
140 data.ReadString(result.errorMessage);
141 }
142
ClosePackagesFileHandle(std::vector<Package> & packages)143 void UpdatePolicyUtils::ClosePackagesFileHandle(std::vector<Package> &packages)
144 {
145 for (auto &package : packages) {
146 if (package.fd >= 0) {
147 if (close(package.fd) != 0) {
148 EDMLOGW("ClosePackagesFileHandle failed");
149 }
150 package.fd = -1;
151 }
152 }
153 }
154 } // namespace EDM
155 } // namespace OHOS