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