• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 factoryWipeData = false;
46     bool userWipeData = false;
47     bool sdcardUpdate = false;
48     bool forceUpdate = false;
49     int retryCount = 0;
50     float initialProgress = 0; /* The upgrade starts at the progress bar location */
51     float currentPercentage = 0; /* The proportion of progress bars occupied by the upgrade process */
52     unsigned int pkgLocation = 0;
53     std::string miscCmd {"boot_updater"};
54     std::vector<std::string> updatePackage {};
55     std::vector<std::chrono::duration<double>> installTime {};
56     std::function<void(float)> callbackProgress {};
57 };
58 
59 using CondFunc = std::function<bool(const UpdateMessage &)>;
60 
61 using EntryFunc = std::function<int(int, char **)>;
62 
63 struct BootMode {
64     CondFunc cond {nullptr}; // enter mode condition
65     std::string modeName {}; // mode name
66     std::string modePara {}; // parameter config of mode
67     EntryFunc entryFunc {nullptr}; // mode entry
68     void InitMode(void) const; // mode related initialization
69 };
70 
71 int32_t ExtractUpdaterBinary(Hpackage::PkgManager::PkgManagerPtr manager, std::string &packagePath,
72     const std::string &updaterBinary);
73 
74 int GetTmpProgressValue();
75 
76 void ProgressSmoothHandler(int beginProgress, int endProgress);
77 
78 UpdaterStatus DoInstallUpdaterPackage(Hpackage::PkgManager::PkgManagerPtr pkgManager,
79     UpdaterParams &upParams, PackageUpdateMode updateMode);
80 
81 UpdaterStatus StartUpdaterProc(Hpackage::PkgManager::PkgManagerPtr pkgManager,
82     UpdaterParams &upParams, int &maxTemperature);
83 
84 int GetUpdatePackageInfo(Hpackage::PkgManager::PkgManagerPtr pkgManager, const std::string& path);
85 
86 int ExecUpdate(Hpackage::PkgManager::PkgManagerPtr pkgManager, int retry, const std::string &pkgPath,
87     PostMessageFunction postMessage);
88 
89 UpdaterStatus IsSpaceCapacitySufficient(const std::vector<std::string> &packagePath);
90 
91 std::vector<uint64_t> GetStashSizeList(const std::vector<std::string> &packagePath);
92 
93 void HandleChildOutput(const std::string &buffer, int32_t bufferLen, bool &retryUpdate, UpdaterParams &upParams);
94 
95 int CheckStatvfs(const uint64_t totalPkgSize);
96 
97 bool IsSDCardExist(const std::string &sdcard_path);
98 
99 void PostUpdater(bool clearMisc);
100 
101 bool DeleteUpdaterPath(const std::string &path);
102 
103 bool ClearMisc();
104 
105 std::string GetWorkPath();
106 
107 bool IsUpdater(const UpdateMessage &boot);
108 
109 bool IsFlashd(const UpdateMessage &boot);
110 
111 void RegisterMode(const BootMode &mode);
112 
113 std::vector<BootMode> &GetBootModes(void);
114 
115 std::optional<BootMode> SelectMode(const UpdateMessage &boot);
116 
117 void SetMessageToMisc(const std::string &miscCmd, const int message, const std::string headInfo);
118 } // Updater
119 #endif /* UPDATER_UPDATER_H */
120