• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_sys_installer_install.h"
17 
18 #include <dirent.h>
19 #include <iostream>
20 #include <unistd.h>
21 
22 #include "isys_installer.h"
23 #include "sys_installer_kits_impl.h"
24 #include "sys_installer_task_const.h"
25 
26 #include "config_parse.h"
27 #include "dupdate_errno.h"
28 #include "firmware_constant.h"
29 #include "firmware_log.h"
30 #include "firmware_sys_installer_callback.h"
31 #include "firmware_update_helper.h"
32 
33 namespace OHOS {
34 namespace UpdateService {
35 
IsComponentLegal(const std::vector<FirmwareComponent> & componentList)36 bool SysInstallerInstall::IsComponentLegal(const std::vector<FirmwareComponent> &componentList)
37 {
38     return FirmwareUpdateHelper::IsUpgradePackagesReady(componentList);
39 }
40 
PerformInstall(const std::vector<FirmwareComponent> & componentList,UpgradeStatus & status)41 bool SysInstallerInstall::PerformInstall(const std::vector<FirmwareComponent> &componentList, UpgradeStatus &status)
42 {
43     FIRMWARE_LOGI("SysInstallerInstall::PerformInstall");
44     if (componentList.empty()) {
45         return false;
46     }
47     uint32_t successCount = 0;
48     for (const auto &component : componentList) {
49         if (onInstallCallback_.onFirmwareStatus == nullptr) {
50             FIRMWARE_LOGE("SysInstallerInstall PerformInstall onFirmwareStatus is null");
51             continue;
52         }
53         onInstallCallback_.onFirmwareStatus(UpgradeStatus::INSTALLING);
54         if (DoSysInstall(component) == OHOS_SUCCESS) {
55             successCount ++;
56         }
57     }
58     return successCount == static_cast<uint32_t>(componentList.size());
59 }
60 
DoSysInstall(const FirmwareComponent & firmwareComponent)61 int32_t SysInstallerInstall::DoSysInstall(const FirmwareComponent &firmwareComponent)
62 {
63     FIRMWARE_LOGI("DoSysInstall, status=%{public}d", firmwareComponent.status);
64     FirmwareComponent sysComponent = firmwareComponent;
65     InitInstallProgress();
66     int32_t ret = SysInstaller::SysInstallerKitsImpl::GetInstance().SysInstallerInit(sysComponent.versionId);
67     if (ret != OHOS_SUCCESS) {
68         FIRMWARE_LOGE("sys installer init failed");
69         errMsg_.errorMessage = "sys installer init failed";
70         errMsg_.errorCode = DUPDATE_ERR_IPC_ERROR;
71         return OHOS_FAILURE;
72     }
73 
74     int32_t updateStatus = SysInstaller::SysInstallerKitsImpl::GetInstance().GetUpdateStatus(sysComponent.versionId);
75     if (updateStatus != CAST_INT(SysInstaller::UpdateStatus::UPDATE_STATE_INIT)) {
76         FIRMWARE_LOGE("StartUnpack status: %{public}d , system busy", updateStatus);
77         errMsg_.errorMessage = "sys installer is busy";
78         errMsg_.errorCode = ret;
79         return OHOS_FAILURE;
80     }
81 
82     SysInstallerExecutorCallback callback { [&](const InstallProgress &installProgress) {
83         sysInstallProgress_ = installProgress.progress;
84         errMsg_ = installProgress.errMsg;
85         sysComponent.status = installProgress.progress.status;
86         sysComponent.progress = installProgress.progress.percent;
87         FIRMWARE_LOGI("SysInstallerExecutorCallback status=%{public}d , progress=%{public}d",
88             sysComponent.status, sysComponent.progress);
89         if (onInstallCallback_.onFirmwareProgress == nullptr) {
90             FIRMWARE_LOGE("SysInstallerExecutorCallback onFirmwareProgress is null");
91             return;
92         }
93         onInstallCallback_.onFirmwareProgress(sysComponent);
94     } };
95     sptr<SysInstaller::ISysInstallerCallbackFunc> cb = new SysInstallerCallback(callback);
96     if (cb == nullptr) {
97         FIRMWARE_LOGE("sys installer callback is nullptr");
98         errMsg_.errorMessage = "sys installer callback is nullptr";
99         errMsg_.errorCode = DUPDATE_ERR_IPC_ERROR;
100         return OHOS_FAILURE;
101     }
102 
103     ret = SysInstaller::SysInstallerKitsImpl::GetInstance().SetUpdateCallback(sysComponent.versionId, cb);
104     if (ret != OHOS_SUCCESS) {
105         FIRMWARE_LOGE("set sys installer callback failed");
106         errMsg_.errorMessage = "set sys installer callback failed";
107         errMsg_.errorCode = ret;
108         return OHOS_FAILURE;
109     }
110 
111     if (StartUpdatePackageZip(sysComponent.versionId, sysComponent.spath) != OHOS_SUCCESS) {
112         return OHOS_FAILURE;
113     }
114     return WaitInstallResult(sysComponent.versionId);
115 }
116 
StartUpdatePackageZip(const std::string & versionId,std::string & path)117 int32_t SysInstallerInstall::StartUpdatePackageZip(const std::string &versionId, std::string &path)
118 {
119     auto ret = SysInstaller::SysInstallerKitsImpl::GetInstance().StartUpdatePackageZip(versionId, path);
120     if (ret != OHOS_SUCCESS) {
121         errMsg_.errorMessage = "sys installer StartUpdatePackageZip failed";
122         errMsg_.errorCode = ret;
123         FIRMWARE_LOGE("sys installer StartUpdatePackageZip failed ret = %{public}d", ret);
124         return OHOS_FAILURE;
125     }
126     return ret;
127 }
128 
InitInstallProgress()129 void SysInstallerInstall::InitInstallProgress()
130 {
131     sysInstallProgress_.status = UpgradeStatus::INSTALLING;
132     sysInstallProgress_.percent = 0;
133     sysInstallProgress_.endReason = "";
134     errMsg_.errorCode = 0;
135     errMsg_.errorMessage = "";
136 }
137 
WaitInstallResult(const std::string & versionId)138 int32_t SysInstallerInstall::WaitInstallResult(const std::string &versionId)
139 {
140     uint32_t timeout = 0;
141     uint32_t configTime = DelayedSingleton<ConfigParse>::GetInstance()->GetAbInstallerTimeout();
142     FIRMWARE_LOGI("sysinstaller wait result, max wait time=%{public}u", configTime);
143     while (timeout <= configTime) {
144         if (sysInstallProgress_.status == UpgradeStatus::INSTALL_FAIL) {
145             FIRMWARE_LOGE("WaitInstallResult sysinstaller fail");
146             SysInstaller::SysInstallerKitsImpl::GetInstance().GetUpdateResult(versionId,
147                 SysInstaller::TaskTypeConst::TASK_TYPE_AB_UPDATE, SysInstaller::ResultTypeConst::RESULT_TYPE_INSTALL);
148             return OHOS_FAILURE;
149         }
150         if (sysInstallProgress_.status == UpgradeStatus::INSTALL_SUCCESS &&
151             sysInstallProgress_.percent == Firmware::ONE_HUNDRED) {
152             SysInstaller::SysInstallerKitsImpl::GetInstance().GetUpdateResult(versionId,
153                 SysInstaller::TaskTypeConst::TASK_TYPE_AB_UPDATE, SysInstaller::ResultTypeConst::RESULT_TYPE_INSTALL);
154             return OHOS_SUCCESS;
155         }
156         timeout++;
157         sleep(SLEEP_INSTALL);
158     }
159     FIRMWARE_LOGI("WaitInstallResult time out, sysInstallProgress_.status=%{public}d",
160         CAST_INT(sysInstallProgress_.status));
161     return OHOS_FAILURE;
162 }
163 } // namespace UpdateService
164 } // namespace OHOS
165