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 SUBPKG_UPDATE, 42 UNKNOWN_UPDATE, 43 }; 44 45 enum NotifyAction { 46 PROCESS_PACKAGE = 0, 47 SET_INSTALL_STATUS, 48 GET_INSTALL_STATUS, 49 SET_UPDATE_STATUS, 50 GET_UPDATE_STATUS, 51 TRIGGER_SDUPDATE, 52 TRIGGER_FACTORYRST, 53 TRIGGER_EUPDATER, 54 }; 55 56 struct UpdaterParams { 57 bool forceUpdate = false; 58 bool forceReboot = false; 59 bool isLoadReduction = false; 60 std::string sdExtMode {}; 61 std::string factoryResetMode {}; 62 PackageUpdateMode updateMode = HOTA_UPDATE; 63 int retryCount = 0; 64 int panicCount = 0; 65 pid_t binaryPid = -1; 66 float initialProgress = 0; /* The upgrade starts at the progress bar location */ 67 float currentPercentage = 0; /* The proportion of progress bars occupied by the upgrade process */ 68 unsigned int pkgLocation = 0; 69 std::string shrinkInfo = ""; 70 std::string virtualShrinkInfo = ""; 71 std::string miscCmd {"boot_updater"}; 72 std::vector<std::string> updateBin {}; 73 std::vector<std::string> updatePackage {}; 74 std::vector<std::chrono::duration<double>> installTime {}; 75 std::function<void(float)> callbackProgress {}; 76 std::vector<std::string> binaryTids {}; 77 }; 78 79 using CondFunc = std::function<bool(const UpdateMessage &)>; 80 81 using EntryFunc = std::function<int(int, char **)>; 82 83 struct BootMode { 84 CondFunc cond {nullptr}; // enter mode condition 85 std::string modeName {}; // mode name 86 std::string modePara {}; // parameter config of mode 87 EntryFunc entryFunc {nullptr}; // mode entry 88 void InitMode(void) const; // mode related initialization 89 }; 90 91 int32_t ExtractUpdaterBinary(Hpackage::PkgManager::PkgManagerPtr manager, std::string &packagePath, 92 const std::string &updaterBinary); 93 94 int GetTmpProgressValue(); 95 void SetTmpProgressValue(int value); 96 97 void ProgressSmoothHandler(int beginProgress, int endProgress, 98 [[maybe_unused]] UpdaterParams upParams = {}, [[maybe_unused]] bool isFinish = false); 99 100 UpdaterStatus SetUpdateSlotParam(UpdaterParams &upParams, bool isUpdateCurrSlot); 101 102 UpdaterStatus SetUpdateSuffixParam(); 103 104 UpdaterStatus ClearUpdateSlotParam(); 105 106 UpdaterStatus ClearUpdateSuffixParam(); 107 108 UpdaterStatus DoInstallUpdaterPackage(Hpackage::PkgManager::PkgManagerPtr pkgManager, 109 UpdaterParams &upParams, PackageUpdateMode updateMode); 110 111 UpdaterStatus DoInstallUpdaterBinfile(Hpackage::PkgManager::PkgManagerPtr pkgManager, 112 UpdaterParams &upParams, PackageUpdateMode updateMode); 113 114 UpdaterStatus StartUpdaterProc(Hpackage::PkgManager::PkgManagerPtr pkgManager, 115 UpdaterParams &upParams); 116 117 int GetUpdatePackageInfo(Hpackage::PkgManager::PkgManagerPtr pkgManager, const std::string& path); 118 119 int ExecUpdate(Hpackage::PkgManager::PkgManagerPtr pkgManager, int retry, const std::string &pkgPath, 120 PostMessageFunction postMessage); 121 122 UpdaterStatus IsSpaceCapacitySufficient(const UpdaterParams &upParams); 123 124 std::vector<uint64_t> GetStashSizeList(const UpdaterParams &upParams); 125 126 void HandleChildOutput(const std::string &buffer, int32_t bufferLen, bool &retryUpdate, UpdaterParams &upParams); 127 128 int CheckStatvfs(const uint64_t totalPkgSize); 129 130 bool IsSDCardExist(const std::string &sdcard_path); 131 132 void PostUpdater(bool clearMisc); 133 134 bool DeleteUpdaterPath(const std::string &path); 135 136 bool ClearMisc(); 137 138 std::string GetWorkPath(); 139 140 bool IsUpdater(const UpdateMessage &boot); 141 142 bool IsFlashd(const UpdateMessage &boot); 143 144 void RegisterMode(const BootMode &mode); 145 146 std::vector<BootMode> &GetBootModes(void); 147 148 std::optional<BootMode> SelectMode(const UpdateMessage &boot); 149 150 bool SetCpuAffinityByPid(const UpdaterParams &upParams, unsigned int reservedCores); 151 152 void UpdateBinaryTids(const std::vector<std::string> &output, UpdaterParams &upParams); 153 } // Updater 154 #endif /* UPDATER_UPDATER_H */ 155