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_updater_install.h"
17 
18 #include <algorithm>
19 #include <iostream>
20 #include <map>
21 #include <string>
22 #include <unistd.h>
23 
24 #include "updaterkits/updaterkits.h"
25 
26 #include "dupdate_errno.h"
27 #include "firmware_constant.h"
28 #include "firmware_log.h"
29 #include "firmware_update_helper.h"
30 
31 namespace OHOS {
32 namespace UpdateEngine {
IsComponentLegal(const std::vector<FirmwareComponent> & componentList)33 bool UpdaterInstall::IsComponentLegal(const std::vector<FirmwareComponent> &componentList)
34 {
35     return FirmwareUpdateHelper::IsUpgradePackagesReady(componentList);
36 }
37 
PerformInstall(const std::vector<FirmwareComponent> & componentList)38 bool UpdaterInstall::PerformInstall(const std::vector<FirmwareComponent> &componentList)
39 {
40     onInstallCallback_.onFirmwareStatus(UpgradeStatus::UPDATING);
41     return DoUpdaterInstall(componentList) == OHOS_SUCCESS;
42 }
43 
DoUpdaterInstall(const std::vector<FirmwareComponent> & componentList)44 int32_t UpdaterInstall::DoUpdaterInstall(const std::vector<FirmwareComponent> &componentList)
45 {
46     std::vector<std::string> installFiles;
47     std::vector<std::pair<int32_t, std::string>> componentPaths;
48     for (const auto &component : componentList) {
49         componentPaths.emplace_back(component.blVersionType, component.spath);
50     }
51     sort(componentPaths.begin(), componentPaths.end());
52     for (const auto &component : componentPaths) {
53         installFiles.push_back(component.second);
54     }
55     sleep(1);
56     int32_t ret = RebootAndInstallUpgradePackage("/misc", installFiles);
57     if (ret != 0) {
58         FIRMWARE_LOGE("RebootAndInstallUpgradePackage fail %{public}d", ret);
59         errMsg_.errorCode = DUPDATE_ERR_UPDATE_REBOOT_FAIL;
60         errMsg_.errorMessage = "updater install is failed, ret = " + std::to_string(ret);
61         return OHOS_FAILURE;
62     }
63     return OHOS_SUCCESS;
64 }
65 } // namespace UpdateEngine
66 } // namespace OHOS
67