1 /*
2  * Copyright (c) 2021-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 #ifndef UPDATER_UPDATER_H
17 #define UPDATER_UPDATER_H
18 #include <chrono>
19 #include <optional>
20 #include <string>
21 #include "misc_info/misc_info.h"
22 #include "package/packages_info.h"
23 #include "package/pkg_manager.h"
24 
25 namespace Updater {
26 enum UpdaterStatus {
27     UPDATE_ERROR = -1,
28     UPDATE_SUCCESS,
29     UPDATE_CORRUPT, /* package or verify failed, something is broken. */
30     UPDATE_SKIP, /* skip update because of condition is not satisfied, e.g, battery is low */
31     UPDATE_RETRY,
32     UPDATE_SPACE_NOTENOUGH,
33     UPDATE_UNKNOWN
34 };
35 
36 using PostMessageFunction = std::function<void(const char *cmd, const char *content)>;
37 
38 enum PackageUpdateMode {
39     HOTA_UPDATE = 0,
40     SDCARD_UPDATE,
41     UNKNOWN_UPDATE,
42 };
43 
44 struct UpdaterParams {
45     bool forceUpdate = false;
46     bool forceReboot = false;
47     std::string sdExtMode {};
48     std::string factoryResetMode {};
49     PackageUpdateMode updateMode = HOTA_UPDATE;
50     int retryCount = 0;
51     float initialProgress = 0; /* The upgrade starts at the progress bar location */
52     float currentPercentage = 0; /* The proportion of progress bars occupied by the upgrade process */
53     unsigned int pkgLocation = 0;
54     std::string miscCmd {"boot_updater"};
55     std::vector<std::string> updatePackage {};
56     std::vector<std::chrono::duration<double>> installTime {};
57     std::function<void(float)> callbackProgress {};
58 };
59 
60 using CondFunc = std::function<bool(const UpdateMessage &)>;
61 
62 using EntryFunc = std::function<int(int, char **)>;
63 
64 struct BootMode {
65     CondFunc cond {nullptr}; // enter mode condition
66     std::string modeName {}; // mode name
67     std::string modePara {}; // parameter config of mode
68     EntryFunc entryFunc {nullptr}; // mode entry
69     void InitMode(void) const; // mode related initialization
70 };
71 
72 int32_t ExtractUpdaterBinary(Hpackage::PkgManager::PkgManagerPtr manager, std::string &packagePath,
73     const std::string &updaterBinary);
74 
75 int GetTmpProgressValue();
76 void SetTmpProgressValue(int value);
77 
78 void ProgressSmoothHandler(int beginProgress, int endProgress);
79 
80 UpdaterStatus DoInstallUpdaterPackage(Hpackage::PkgManager::PkgManagerPtr pkgManager,
81     UpdaterParams &upParams, PackageUpdateMode updateMode);
82 
83 UpdaterStatus StartUpdaterProc(Hpackage::PkgManager::PkgManagerPtr pkgManager,
84     UpdaterParams &upParams);
85 
86 int GetUpdatePackageInfo(Hpackage::PkgManager::PkgManagerPtr pkgManager, const std::string& path);
87 
88 int ExecUpdate(Hpackage::PkgManager::PkgManagerPtr pkgManager, int retry, const std::string &pkgPath,
89     PostMessageFunction postMessage);
90 
91 UpdaterStatus IsSpaceCapacitySufficient(const UpdaterParams &upParams);
92 
93 std::vector<uint64_t> GetStashSizeList(const UpdaterParams &upParams);
94 
95 void HandleChildOutput(const std::string &buffer, int32_t bufferLen, bool &retryUpdate, UpdaterParams &upParams);
96 
97 int CheckStatvfs(const uint64_t totalPkgSize);
98 
99 bool IsSDCardExist(const std::string &sdcard_path);
100 
101 void PostUpdater(bool clearMisc);
102 
103 bool DeleteUpdaterPath(const std::string &path);
104 
105 bool ClearMisc();
106 
107 std::string GetWorkPath();
108 
109 bool IsUpdater(const UpdateMessage &boot);
110 
111 bool IsFlashd(const UpdateMessage &boot);
112 
113 void RegisterMode(const BootMode &mode);
114 
115 std::vector<BootMode> &GetBootModes(void);
116 
117 std::optional<BootMode> SelectMode(const UpdateMessage &boot);
118 } // Updater
119 #endif /* UPDATER_UPDATER_H */
120