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