1 /*
2 * Copyright (c) 2023 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 "firmware_update_helper.h"
17
18 namespace OHOS {
19 namespace UpdateEngine {
BuildTaskId(const std::vector<FirmwareComponent> & componentList)20 std::string FirmwareUpdateHelper::BuildTaskId(const std::vector<FirmwareComponent> &componentList)
21 {
22 if (componentList.empty()) {
23 return "";
24 }
25 std::string taskId;
26 for (const FirmwareComponent &component : componentList) {
27 if (!taskId.empty()) {
28 taskId.append("_");
29 }
30 taskId.append(component.versionId);
31 }
32 return Sha256Utils::CalculateHashCode(taskId);
33 }
34
ClearFirmwareInfo()35 void FirmwareUpdateHelper::ClearFirmwareInfo()
36 {
37 FIRMWARE_LOGI("ClearFirmwareInfo");
38 std::shared_ptr<FirmwarePreferencesUtil> preferencesUtil = DelayedSingleton<FirmwarePreferencesUtil>::GetInstance();
39
40 FirmwareTask task;
41 FirmwareTaskOperator firmwareTaskOperator;
42 firmwareTaskOperator.QueryTask(task);
43 firmwareTaskOperator.DeleteAll();
44 FirmwareComponentOperator().DeleteAll();
45 FirmwareFileUtils::DeleteDownloadFiles();
46 }
47
48 // 此处为桩函数,默认电量足够。
IsBatteryEnough(int32_t batteryLimit)49 bool FirmwareUpdateHelper::IsBatteryEnough(int32_t batteryLimit)
50 {
51 return true;
52 }
53
GetInstallType()54 InstallType FirmwareUpdateHelper::GetInstallType()
55 {
56 InstallType installType = (DeviceAdapter::GetBootSlot()) == Firmware::BOOTSLOTS_AB_UPDATE_VALUE ?
57 InstallType::SYS_INSTALLER : InstallType::UPDATER;
58 FIRMWARE_LOGI("get installType(bootsloot) %{public}d", CAST_INT(installType));
59 return installType;
60 }
61
BuildHotaVersionComponent(std::vector<FirmwareComponent> & hotaComponents)62 VersionComponent FirmwareUpdateHelper::BuildHotaVersionComponent(std::vector<FirmwareComponent> &hotaComponents)
63 {
64 VersionComponent hotaVersionComponent;
65 hotaVersionComponent.componentId = hotaComponents[0].componentId;
66 hotaVersionComponent.upgradeAction = UpgradeAction::UPGRADE;
67 hotaVersionComponent.componentType = CAST_INT(ComponentType::OTA);
68 hotaVersionComponent.effectiveMode = FirmwareUpdateHelper::GetInstallType() == InstallType::SYS_INSTALLER ?
69 static_cast<size_t>(EffectiveMode::LIVE_AND_COLD): static_cast<size_t>(EffectiveMode::COLD);
70 hotaVersionComponent.innerVersion = hotaComponents[0].targetBlVersionNumber;
71 hotaVersionComponent.displayVersion = hotaComponents[0].targetBlDisplayVersionNumber;
72 for (const auto &component : hotaComponents) {
73 hotaVersionComponent.size += static_cast<size_t>(component.size);
74 }
75 return hotaVersionComponent;
76 }
77
BuildNewVersionInfo(const std::vector<FirmwareComponent> & components,std::vector<VersionComponent> & versionComponents)78 void FirmwareUpdateHelper::BuildNewVersionInfo(const std::vector<FirmwareComponent> &components,
79 std::vector<VersionComponent> &versionComponents)
80 {
81 if (components.empty()) {
82 FIRMWARE_LOGI("BuildNewVersionInfo component is null");
83 return;
84 }
85 std::vector<FirmwareComponent> hotaComponents;
86 for (auto &component : components) {
87 if (component.blVersionType == CAST_INT(BlType::HOTA_TYPE)) {
88 hotaComponents.push_back(component);
89 }
90 if (component.blVersionType == CAST_INT(BlType::DYNAMIC_TYPE)) {
91 hotaComponents.push_back(component);
92 }
93 }
94
95 std::string hotaVersionNumber;
96 std::string hotaDisplayVersionNumber;
97 if (!hotaComponents.empty()) {
98 VersionComponent hotaVersionComponent = BuildHotaVersionComponent(hotaComponents);
99 hotaVersionNumber = hotaVersionComponent.innerVersion;
100 hotaDisplayVersionNumber = hotaVersionComponent.displayVersion;
101 versionComponents.push_back(hotaVersionComponent);
102 } else {
103 hotaVersionNumber = DeviceAdapter::GetRealVersion();
104 hotaDisplayVersionNumber = DeviceAdapter::GetDisplayVersion();
105 }
106 }
107
BuildCurrentVersionInfo(CurrentVersionInfo & currentVersionInfo)108 void FirmwareUpdateHelper::BuildCurrentVersionInfo(CurrentVersionInfo ¤tVersionInfo)
109 {
110 currentVersionInfo.osVersion = DeviceAdapter::GetOsVersion();
111 currentVersionInfo.deviceName = DeviceAdapter::GetDeviceName();
112
113 std::shared_ptr<FirmwarePreferencesUtil> utils = DelayedSingleton<FirmwarePreferencesUtil>::GetInstance();
114 VersionComponent hotaVersionComponent;
115 hotaVersionComponent.displayVersion = DeviceAdapter::GetDisplayVersion();
116 hotaVersionComponent.innerVersion = DeviceAdapter::GetRealVersion();
117 hotaVersionComponent.descriptionInfo.descriptionType = DescriptionType::ID;
118 hotaVersionComponent.componentId = utils->ObtainString(Firmware::HOTA_CURRENT_COMPONENT_ID, "");
119 hotaVersionComponent.componentType = CAST_INT(ComponentType::OTA);
120 currentVersionInfo.versionComponents.push_back(hotaVersionComponent);
121 }
122
IsUpgradePackagesReady(const std::vector<FirmwareComponent> & componentList)123 bool FirmwareUpdateHelper::IsUpgradePackagesReady(const std::vector<FirmwareComponent> &componentList)
124 {
125 if (componentList.empty()) {
126 FIRMWARE_LOGE("IsUpgradePackagesReady: componentList is null!");
127 return false;
128 }
129
130 return std::all_of(componentList.begin(), componentList.end(), [](const FirmwareComponent &component) {
131 if (!FileUtils::IsFileExist(component.spath)) {
132 FIRMWARE_LOGE("IsUpgradePackagesReady: package [%{public}s] is not exist!", component.spath.c_str());
133 return false;
134 }
135 int64_t realSize = FileUtils::GetFileSize(component.spath);
136 if (realSize != component.size) {
137 FIRMWARE_LOGE("IsUpgradePackagesReady: calculate component size %{public}s != DB component size %{public}s",
138 std::to_string(realSize).c_str(), std::to_string(component.size).c_str());
139 return false;
140 }
141 return true;
142 });
143 }
144 } // namespace UpdateEngine
145 } // namespace OHOS