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