diff --git a/interfaces/kits/updaterkits/updaterkits.cpp b/interfaces/kits/updaterkits/updaterkits.cpp index cefbd0e53f67d325bd26b2399d5894d282f21531..decb893ac037dfb072329edf91dee459fbbddb2f 100755 --- a/interfaces/kits/updaterkits/updaterkits.cpp +++ b/interfaces/kits/updaterkits/updaterkits.cpp @@ -14,6 +14,7 @@ */ #include "updaterkits/updaterkits.h" +#include #include #include #include @@ -28,6 +29,33 @@ using namespace Updater; using Updater::Utils::SplitString; +#ifndef UPDATER_UT +constexpr const char *HANDLE_MISC_LIB = "libupdater_handle_misc.z.so"; +constexpr const char *HANDLE_MISC_INFO = "HandleUpdateMiscInfo"; +constexpr const char *HANDLE_MISC_LIB_PATH = "/system/lib64/libupdater_handle_misc.z.so"; + +static void HandleMiscMsg(const struct UpdateMessage &updateMsg, const std::string &upgradeType) +{ + if (!Utils::IsFileExist(HANDLE_MISC_LIB_PATH)) { + LOG(WARNING) << "libupdater_handle_misc.z.so is not exist"; + return; + } + auto handle = Utils::LoadLibrary(HANDLE_MISC_LIB); + if (handle == nullptr) { + LOG(ERROR) << "load libupdater_handle_misc fail"; + return; + } + auto getFunc = (void(*)(const std::string &, const std::string &))Utils::GetFunction(handle, HANDLE_MISC_INFO); + if (getFunc == nullptr) { + LOG(ERROR) << "getFunc is nullptr"; + Utils::CloseLibrary(handle); + return; + } + getFunc(updateMsg.update, upgradeType); + Utils::CloseLibrary(handle); +} +#endif + static bool WriteToMiscAndRebootToUpdater(const struct UpdateMessage &updateMsg) { // Write package name to misc, then trigger reboot. @@ -38,6 +66,7 @@ static bool WriteToMiscAndRebootToUpdater(const struct UpdateMessage &updateMsg) return false; } #ifndef UPDATER_UT + HandleMiscMsg(updateMsg, ""); WriteUpdaterMiscMsg(updateMsg); DoReboot("updater:reboot to updater to trigger update"); while (true) { @@ -96,7 +125,8 @@ static std::string ParsePkgPath(const struct UpdateMessage &updateMsg) return pkgPath; } -static bool WriteToMiscAndResultFileRebootToUpdater(const struct UpdateMessage &updateMsg) +static bool WriteToMiscAndResultFileRebootToUpdater(const struct UpdateMessage &updateMsg, + const std::string &upgradeType) { // Write package name to misc, then trigger reboot. const char *bootCmd = "boot_updater"; @@ -110,6 +140,7 @@ static bool WriteToMiscAndResultFileRebootToUpdater(const struct UpdateMessage & std::string writeMiscBefore = "0x80000000"; WriteUpdaterResultFile(pkgPath, writeMiscBefore); #ifndef UPDATER_UT + HandleMiscMsg(updateMsg, upgradeType); WriteUpdaterMiscMsg(updateMsg); // Flag after the misc in written std::string writeMiscAfter = "0x80000008"; @@ -211,7 +242,7 @@ int RebootAndInstallUpgradePackage(const std::string &miscFile, const std::vecto return addRet; } if (upgradeType == UPGRADE_TYPE_OTA) { - WriteToMiscAndResultFileRebootToUpdater(updateMsg); + WriteToMiscAndResultFileRebootToUpdater(updateMsg, upgradeType); } else { WriteToMiscAndRebootToUpdater(updateMsg); } diff --git a/services/include/updater/updater.h b/services/include/updater/updater.h index a0e3a9150bdb6163c874b69162881217951ee7dc..798afa60396769eb310ada38e787634d311c1fcb 100644 --- a/services/include/updater/updater.h +++ b/services/include/updater/updater.h @@ -41,6 +41,14 @@ enum PackageUpdateMode { UNKNOWN_UPDATE, }; +enum NotifyAction { + PROCESS_PACKAGE = 0, + SET_INSTALL_STATUS, + GET_INSTALL_STATUS, + SET_UPDATE_STATUS, + GET_UPDATE_STATUS +}; + struct UpdaterParams { bool forceUpdate = false; bool forceReboot = false; diff --git a/services/updater_main.cpp b/services/updater_main.cpp index 4e3f573ae8e06ceb7dbfa75600c66d4abb4b0846..f098b64580ceeb623e9b8d56db8e60269c647887 100644 --- a/services/updater_main.cpp +++ b/services/updater_main.cpp @@ -168,6 +168,18 @@ __attribute__((weak)) void UpdaterVerifyFailEntry(bool verifyret) return; } +__attribute__((weak)) UpdaterStatus NotifyActionResult(UpdaterParams &upParams, + UpdaterStatus &status, const std::vector ¬ifyActionVec) +{ + return UPDATE_SUCCESS; +} + +__attribute__((weak)) void NotifyReboot(const std::string& rebootTarget, + const std::string &rebootReason, const std::string &extData) +{ + Updater::Utils::UpdaterDoReboot(rebootTarget, rebootReason, extData); +} + static UpdaterStatus VerifyPackages(UpdaterParams &upParams) { UPDATER_INIT_RECORD; @@ -378,6 +390,24 @@ static int CheckMountData() return UPDATE_ERROR; } +static UpdaterStatus CheckVerifyPackages(UpdaterParams &upParams) +{ + // verify packages first + UpdaterStatus status = VerifyPackages(upParams); + if (NotifyActionResult(upParams, status, {SET_INSTALL_STATUS}) != UPDATE_SUCCESS) { + LOG(ERROR) << "set status fail"; + return UPDATE_CORRUPT; + } + if (status != UPDATE_SUCCESS) { + return UPDATE_CORRUPT; + } + if (NotifyActionResult(upParams, status, {PROCESS_PACKAGE, GET_INSTALL_STATUS}) != UPDATE_SUCCESS) { + LOG(ERROR) << "notify action fail"; + return UPDATE_CORRUPT; + } + return UPDATE_SUCCESS; +} + static UpdaterStatus PreUpdatePackages(UpdaterParams &upParams) { UPDATER_INIT_RECORD; @@ -400,9 +430,9 @@ static UpdaterStatus PreUpdatePackages(UpdaterParams &upParams) return UPDATE_SUCCESS; } - // verify packages first - if (VerifyPackages(upParams) != UPDATE_SUCCESS) { - return UPDATE_CORRUPT; // verify package failed must return UPDATE_CORRUPT, ux need it !!! + if (CheckVerifyPackages(upParams) != UPDATE_SUCCESS) { + LOG(ERROR) << "verify packages fail"; + return UPDATE_CORRUPT; } // Only handle UPATE_ERROR and UPDATE_SUCCESS here.Let package verify handle others. @@ -496,6 +526,10 @@ UpdaterStatus DoUpdatePackages(UpdaterParams &upParams) UPDATER_UI_INSTANCE.GetCurrentPercent() : (updateStartPosition * FULL_PERCENT_PROGRESS); upParams.callbackProgress(value); status = DoInstallPackages(upParams, pkgStartPosition); + if (NotifyActionResult(upParams, status, {SET_UPDATE_STATUS}) != UPDATE_SUCCESS) { + LOG(ERROR) << "set status fail"; + return UPDATE_CORRUPT; + } if (status != UPDATE_SUCCESS) { UPDATER_LAST_WORD(status, "DoInstallPackages failed"); return status; @@ -503,6 +537,10 @@ UpdaterStatus DoUpdatePackages(UpdaterParams &upParams) if (upParams.forceUpdate) { UPDATER_UI_INSTANCE.ShowLogRes(TR(LABEL_UPD_OK_SHUTDOWN)); } + if (NotifyActionResult(upParams, status, {GET_UPDATE_STATUS}) != UPDATE_SUCCESS) { + LOG(ERROR) << "get status fail"; + return UPDATE_CORRUPT; + } UPDATER_UI_INSTANCE.ShowSuccessPage(); return status; } @@ -556,8 +594,9 @@ static UpdaterStatus PreSdcardUpdatePackages(UpdaterParams &upParams) return UPDATE_SKIP; } - if (VerifyPackages(upParams) != UPDATE_SUCCESS) { - return UPDATE_CORRUPT; // verify package failed must return UPDATE_CORRUPT, ux need it !!! + if (CheckVerifyPackages(upParams) != UPDATE_SUCCESS) { + LOG(ERROR) << "verify packages fail"; + return UPDATE_CORRUPT; } #ifdef UPDATER_USE_PTABLE if (!PtablePreProcess::GetInstance().DoPtableProcess(upParams)) { @@ -844,7 +883,7 @@ void RebootAfterUpdateSuccess(const UpdaterParams &upParams) if (IsNeedWipe() || upParams.sdExtMode == SDCARD_UPDATE_FROM_DEV || upParams.sdExtMode == SDCARD_UPDATE_FROM_DATA) { - Utils::UpdaterDoReboot("updater", "Updater wipe data after upgrade success", "--user_wipe_data"); + NotifyReboot("updater", "Updater wipe data after upgrade success", "--user_wipe_data"); return; } if (upParams.factoryResetMode == "factory_wipe_data") { @@ -855,7 +894,7 @@ void RebootAfterUpdateSuccess(const UpdaterParams &upParams) upParams.forceUpdate || upParams.factoryResetMode == "factory_wipe_data" || upParams.factoryResetMode == "menu_wipe_data" ? Utils::DoShutdown("Updater update success go shut down") : - Utils::UpdaterDoReboot("", "Updater update success"); + NotifyReboot("", "Updater update success"); } int UpdaterMain(int argc, char **argv) @@ -882,7 +921,7 @@ int UpdaterMain(int argc, char **argv) if (upParams.forceReboot) { Utils::UsSleep(5 * DISPLAY_TIME); // 5 : 5s PostUpdater(true); - Utils::UpdaterDoReboot("", "Updater night update fail"); + NotifyReboot("", "Updater night update fail"); return 0; } } else if (mode == SDCARD_UPDATE) { diff --git a/services/updater_main.h b/services/updater_main.h index 699df7ee794b417234a1b392cf3effdcabb0332d..ccf74b1cbb4ae1746c75349f96f740a32f7d26b7 100644 --- a/services/updater_main.h +++ b/services/updater_main.h @@ -70,6 +70,7 @@ int32_t VerifySpecialPkgs([[maybe_unused]]UpdaterParams &upParams); void UpdaterVerifyFailEntry(bool verifyret); bool IsSpareBoardBoot(void); bool IsNeedWipe(); +void NotifyReboot(const std::string& rebootTarget, const std::string &rebootReason, const std::string &extData = ""); #ifdef __cplusplus #if __cplusplus } diff --git a/services/updater_ui.cpp b/services/updater_ui.cpp index 44fe7c859231c00e0cbb5cfcce6cbfbfaeb690a6..ce20ea0e26b31437d84fcfde15412d4635f130fb 100644 --- a/services/updater_ui.cpp +++ b/services/updater_ui.cpp @@ -26,6 +26,7 @@ #include "updater/updater_const.h" #include "utils.h" #include "updater_ui_stub.h" +#include "updater_main.h" namespace Updater { namespace { @@ -70,7 +71,7 @@ DEFINE_ASYN_CALLBACK(OnRebootEvt) LOG(INFO) << "On Label Reboot"; GraphicEngine::GetInstance().StopEngine(); PostUpdater(false); - Utils::UpdaterDoReboot("", "Updater reboot btn event"); + NotifyReboot("", "Updater reboot btn event"); } DEFINE_SYNC_CALLBACK(OnLabelResetEvt) @@ -100,7 +101,7 @@ DEFINE_ASYN_CALLBACK(OnLabelSDCardEvt) return; } PostUpdater(true); - Utils::UpdaterDoReboot("", "Updater sdcard update success reboot"); + NotifyReboot("", "Updater sdcard update success reboot"); } DEFINE_ASYN_CALLBACK(OnLabelSDCardNoDelayEvt) @@ -123,7 +124,7 @@ DEFINE_ASYN_CALLBACK(OnLabelSDCardNoDelayEvt) GetFacade().ShowSuccessPage(); Utils::UsSleep(SUCCESS_DELAY); PostUpdater(true); - Utils::UpdaterDoReboot("", "Updater sdcard update success reboot"); + NotifyReboot("", "Updater sdcard update success reboot"); } DEFINE_ASYN_CALLBACK(OnLabelSDUpdateResEvt) @@ -153,7 +154,7 @@ DEFINE_ASYN_CALLBACK(OnLabelSDUpdateResEvt) GetFacade().ShowSuccessPage(); Utils::UsSleep(SUCCESS_DELAY); PostUpdater(true); - Utils::UpdaterDoReboot("", "Updater sdcard update success reboot"); + NotifyReboot("", "Updater sdcard update success reboot"); } DEFINE_SYNC_CALLBACK(OnLabelCancelEvt) @@ -208,7 +209,7 @@ DEFINE_ASYN_CALLBACK(OnConfirmRstEvt) GetFacade().ShowSuccessPage(); PostUpdater(true); Utils::UsSleep(SUCCESS_DELAY); - Utils::UpdaterDoReboot("", "Updater factory reset success"); + NotifyReboot("", "Updater factory reset success"); } } diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 50511168efe4fb20d082ff052c71fc9101274184..119c908f3ce8826ca5d1ec7063513aab2b0524d7 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -137,7 +137,10 @@ ohos_executable("write_updater") { "${updater_path}/services/include/", ] - deps = [ "${updater_path}/interfaces/kits/misc_info:libmiscinfo" ] + deps = [ + "${updater_path}/interfaces/kits/misc_info:libmiscinfo", + "${updater_path}/utils:libutils", + ] external_deps = [ "bounds_checking_function:libsec_static", "init:libbegetutil_static", diff --git a/utils/include/utils_fs.h b/utils/include/utils_fs.h index a257f81b2c78de7bdea0eb7feca9a65333ba6740..6a7fc0945f67349a4f491b351d9f889e71c8d6d6 100644 --- a/utils/include/utils_fs.h +++ b/utils/include/utils_fs.h @@ -29,6 +29,9 @@ int64_t GetFilesFromDirectory(const std::string &path, std::vector bool RemoveDir(const std::string &path); bool IsFileExist(const std::string &path); bool IsDirExist(const std::string &path); +void* LoadLibrary(const std::string &libName); +void CloseLibrary(void* handle); +void* GetFunction(void* handle, const std::string &funcName); } // Utils } // Updater #endif // UTILS_FS_H \ No newline at end of file diff --git a/utils/utils_fs.cpp b/utils/utils_fs.cpp index eb023c2833f07b96a77ae371a53ae0741647cac5..f96b9ee4c5308701928e22027a9a3822f5402ae1 100644 --- a/utils/utils_fs.cpp +++ b/utils/utils_fs.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -148,5 +149,42 @@ bool IsDirExist(const std::string &path) } return false; } + +void* LoadLibrary(const std::string &libName) +{ + if (libName.empty()) { + LOG(ERROR) << "lib name is empty"; + return nullptr; + } + void* handle = dlopen(libName.c_str(), RTLD_LAZY); + if (handle == nullptr) { + LOG(ERROR) << "dlopen fail, lib name = " << libName << "; dlerror = " << dlerror(); + return nullptr; + } + return handle; +} + +void CloseLibrary(void* handle) +{ + if (handle == nullptr) { + LOG(ERROR) << "handle is nulptr"; + return; + } + dlclose(handle); + handle = nullptr; +} + +void* GetFunction(void* handle, const std::string &funcName) +{ + if (handle == nullptr) { + LOG(ERROR) << "handle is nullptr"; + return nullptr; + } + if (funcName.empty()) { + LOG(ERROR) << "func name is empty"; + return nullptr; + } + return dlsym(handle, funcName.c_str()); +} } // Utils } // updater diff --git a/utils/write_updater.cpp b/utils/write_updater.cpp index c5703b1afe53be48aa8c31c03ca42f7e6aaa7cd2..3bcb7fec4c1b4fa0e375476f1c2708dc460170eb 100644 --- a/utils/write_updater.cpp +++ b/utils/write_updater.cpp @@ -12,16 +12,23 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include "fs_manager/mount.h" #include "misc_info/misc_info.h" #include "securec.h" #include "updater/updater_const.h" #include "parameter.h" +#include "utils.h" +#include "utils_fs.h" using namespace std; using namespace Updater; +constexpr const char *HANDLE_MISC_LIB = "libupdater_handle_misc.z.so"; +constexpr const char *NOTIFY_MISC_INFO = "NotifyWriteMiscInfo"; +constexpr const char *HANDLE_MISC_LIB_PATH = "/system/lib64/libupdater_handle_misc.z.so"; + static void PrintPrompts() { cout << "Please input correct command, examples :" << endl; @@ -66,6 +73,27 @@ static int WriteUpdaterPara(int argc, UpdaterPara ¶) return 0; } +static void HandleMiscInfo(int argc, char **argv) +{ + if (!Utils::IsFileExist(HANDLE_MISC_LIB_PATH)) { + cout << "libupdater_handle_misc.z.so is not exist"; + return; + } + auto handle = Utils::LoadLibrary(HANDLE_MISC_LIB); + if (handle == nullptr) { + cout << "load libupdater_handle_misc fail"; + return; + } + auto getFunc = (void(*)(int, char **))Utils::GetFunction(handle, NOTIFY_MISC_INFO); + if (getFunc == nullptr) { + cout << "getFunc is nullptr"; + Utils::CloseLibrary(handle); + return; + } + getFunc(argc, argv); + Utils::CloseLibrary(handle); +} + int main(int argc, char **argv) { if (argc == 1) { @@ -111,5 +139,6 @@ int main(int argc, char **argv) cout << "WriteUpdaterMessage failed!" << endl; return -1; } + HandleMiscInfo(argc, argv); return 0; }