diff --git a/services/include/updater/updater.h b/services/include/updater/updater.h index 99e0a829e6f798998a2a75f8c9c67cc4f96edb16..54e8ea06ad630378a19d2d1acdffedff78d9f1f1 100644 --- a/services/include/updater/updater.h +++ b/services/include/updater/updater.h @@ -92,6 +92,10 @@ void SetTmpProgressValue(int value); void ProgressSmoothHandler(int beginProgress, int endProgress); +UpdaterStatus SetUpdateSlotParam(UpdaterParams &upParams, bool isUpdateCurrSlot); + +UpdaterStatus ClearUpdateSlotParam(); + UpdaterStatus DoInstallUpdaterPackage(Hpackage::PkgManager::PkgManagerPtr pkgManager, UpdaterParams &upParams, PackageUpdateMode updateMode); diff --git a/services/ptable_parse/ptable.cpp b/services/ptable_parse/ptable.cpp index 5484d245d77c1ac337050e7262870b1d916d12c7..4f830475845b7b6cbdb10176997d71917b65d3c2 100644 --- a/services/ptable_parse/ptable.cpp +++ b/services/ptable_parse/ptable.cpp @@ -561,11 +561,12 @@ bool Ptable::GetPartionInfoByName(const std::string &partitionName, PtnInfo &ptn return true; } std::string partitionNameAB = partitionName; - if (Utils::IsUpdaterMode()) { - partitionNameAB += (GetCurrentSlot() == 1 ? PARTITION_A_SUFFIX : PARTITION_B_SUFFIX); - } else { - partitionNameAB += (GetCurrentSlot() == 1 ? PARTITION_B_SUFFIX : PARTITION_A_SUFFIX); + int updateSlot = Utils::GetUpdateSlot(); + if (updateSlot < 1 || updateSlot > 2) { // 2 : slot b + LOG(ERROR) << "get update slot fail"; + return false; } + partitionNameAB += (updateSlot == SLOT_A ? PARTITION_A_SUFFIX : PARTITION_B_SUFFIX); if (findPart(partitionNameAB)) { LOG(INFO) << "find partitionAB name " << partitionNameAB; return true; diff --git a/services/updater.cpp b/services/updater.cpp index a5b2429e8eaa9c5a5bfe7274722cb735e9b282ea..8446f935ec512cf9b003c2399218374fe5b1ffe2 100644 --- a/services/updater.cpp +++ b/services/updater.cpp @@ -265,6 +265,53 @@ __attribute__((weak)) bool PreStartBinaryEntry([[maybe_unused]] const std::strin return true; } +bool IsUpdateBasePkg(UpdaterParams &upParams) +{ + for (auto pkgPath : upParams.updatePackage) { + if (pkgPath.find("_base") != std::string::npos) { + LOG(INFO) << "this update include base pkg"; + return true; + } + } + return false; +} + +UpdaterStatus SetUpdateSlotParam(UpdaterParams &upParams, bool isUpdateCurrSlot) +{ + if (!Utils::IsVabDevice()) { + return UPDATE_SUCCESS; + } + if (!isUpdateCurrSlot && !IsUpdateBasePkg(upParams)) { + isUpdateCurrSlot = true; + } + int currentSlot = GetCurrentSlot(); + if (currentSlot < 1 || currentSlot > 2) { // 2 : max slot + LOG(ERROR) << "GetCurrentSlot failed"; + return UPDATE_ERROR; + } + bool isUpdateSlotA = (currentSlot == SLOT_A && isUpdateCurrSlot) || + (currentSlot == SLOT_B && !isUpdateCurrSlot); + int updateSlot = isUpdateSlotA ? SLOT_A : SLOT_B; + if (!Utils::SetUpdateSlot(updateSlot)) { + LOG(ERROR) << "set update.part.slot fail"; + return UPDATE_ERROR; + } + return UPDATE_SUCCESS; +} + +UpdaterStatus ClearUpdateSlotParam() +{ + if (!Utils::IsVabDevice()) { + return UPDATE_SUCCESS; + } + int updateSlot = -10; // -10 : default value + if (!Utils::SetUpdateSlot(updateSlot)) { + LOG(ERROR) << "clear update.part.slot fail"; + return UPDATE_ERROR; + } + return UPDATE_SUCCESS; +} + UpdaterStatus DoInstallUpdaterBinfile(PkgManager::PkgManagerPtr pkgManager, UpdaterParams &upParams, PackageUpdateMode updateMode) { diff --git a/services/updater_main.cpp b/services/updater_main.cpp index 96cb87294daf32ec9c6a114f53f77c9446d344ad..eb16229dd602c85ca3b86963694794db66f97c09 100644 --- a/services/updater_main.cpp +++ b/services/updater_main.cpp @@ -666,6 +666,10 @@ static UpdaterStatus PreUpdatePackages(UpdaterParams &upParams) (void)DeleteFile(resultPath); LOG(INFO) << "delete last upgrade file"; } + if (SetUpdateSlotParam(upParams, false) != UPDATE_SUCCESS) { + LOG(ERROR) << "SetUpdateSlotParam failed"; + return UPDATE_ERROR; + } // verify package first if (VerifyCommonFiles(upParams) != UPDATE_SUCCESS) { return UPDATE_CORRUPT; // verify package failed must return UPDATE_CORRUPT, ux need it !!! @@ -858,20 +862,42 @@ UpdaterStatus DoUpdatePackages(UpdaterParams &upParams) UPDATER_LAST_WORD(status, "DoInstallPackages failed"); return status; } + return status; +} + +static void ShowSuccessUi(UpdaterParams &upParams, UpdaterStatus &status) +{ + if (status != UPDATE_SUCCESS) { + LOG(ERROR) << "update not succ"; + return; + } 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; + status = UPDATE_CORRUPT; + return; } UPDATER_UI_INSTANCE.ShowSuccessPage(); - return status; +} + +__attribute__((weak)) UpdaterStatus CheckAndSetSlot([[maybe_unused]]UpdaterParams &upParams) +{ + LOG(INFO) << "not need check and set slot"; + return UPDATE_SUCCESS; } -static void PostUpdate(UpdaterParams &upParams, bool updateResult, +static void PostUpdate(UpdaterParams &upParams, UpdaterStatus &status, const std::vector& updateList, const std::string& type) { + if (status == UPDATE_SUCCESS) { + status = CheckAndSetSlot(upParams); + } + ClearUpdateSlotParam(); + ShowSuccessUi(upParams, status); + bool updateResult = (status == UPDATE_SUCCESS); + std::string writeBuffer; std::string buf; std::string time; @@ -907,14 +933,14 @@ static void PostUpdate(UpdaterParams &upParams, bool updateResult, DeleteInstallTimeFile(); } -static void PostUpdateBinfiles(UpdaterParams &upParams, bool updateResult) +static void PostUpdateBinfiles(UpdaterParams &upParams, UpdaterStatus &status) { - PostUpdate(upParams, updateResult, upParams.updateBin, "Binfiles"); + PostUpdate(upParams, status, upParams.updateBin, "Binfiles"); } -static void PostUpdatePackages(UpdaterParams &upParams, bool updateResult) +static void PostUpdatePackages(UpdaterParams &upParams, UpdaterStatus &status) { - PostUpdate(upParams, updateResult, upParams.updatePackage, "Packages"); + PostUpdate(upParams, status, upParams.updatePackage, "Packages"); } static UpdaterStatus PreSdcardUpdatePackages(UpdaterParams &upParams) @@ -927,6 +953,10 @@ static UpdaterStatus PreSdcardUpdatePackages(UpdaterParams &upParams) LOG(ERROR) << "Battery is not sufficient for install package."; return UPDATE_SKIP; } + if (SetUpdateSlotParam(upParams, true) != UPDATE_SUCCESS) { + LOG(ERROR) << "SetUpdateSlotParam failed"; + return UPDATE_ERROR; + } UpdaterStatus status = VerifyPackages(upParams); if (status != UPDATE_SUCCESS) { return UPDATE_CORRUPT; // verify package failed must return UPDATE_CORRUPT, ux need it !!! @@ -940,10 +970,18 @@ static UpdaterStatus PreSdcardUpdatePackages(UpdaterParams &upParams) return UPDATE_SUCCESS; } -static void PostSdcardUpdatePackages(UpdaterParams &upParams, bool updateResult) +static void PostSdcardUpdatePackages(UpdaterParams &upParams, UpdaterStatus &status) { + ClearUpdateSlotParam(); if (Utils::CheckUpdateMode(Updater::SDCARD_INTRAL_MODE)) { - PostUpdatePackages(upParams, updateResult); + PostUpdatePackages(upParams, status); + } else if (status == UPDATE_SUCCESS) { + if (NotifyActionResult(upParams, status, {GET_UPDATE_STATUS}) != UPDATE_SUCCESS) { + LOG(ERROR) << "get status fail"; + status = UPDATE_CORRUPT; + return; + } + UPDATER_UI_INSTANCE.ShowSuccessPage(); } } @@ -972,7 +1010,7 @@ UpdaterStatus UpdaterFromSdcard(UpdaterParams &upParams) UPDATER_UI_INSTANCE.ShowLog(TR(LOG_SDCARD_NOTMOVE)); status = DoUpdatePackages(upParams); } - PostSdcardUpdatePackages(upParams, status == UPDATE_SUCCESS); + PostSdcardUpdatePackages(upParams, status); return status; } @@ -983,7 +1021,7 @@ UpdaterStatus InstallUpdaterBinfiles(UpdaterParams &upParams) if (status == UPDATE_SUCCESS) { status = DoUpdateBinfiles(upParams); } - PostUpdateBinfiles(upParams, status == UPDATE_SUCCESS); + PostUpdateBinfiles(upParams, status); UpdaterInit::GetInstance().InvokeEvent(UPDATER_POST_UPDATE_PACKAGE_EVENT); return status; } @@ -996,7 +1034,7 @@ UpdaterStatus InstallUpdaterPackages(UpdaterParams &upParams) if (status == UPDATE_SUCCESS) { status = DoUpdatePackages(upParams); } - PostUpdatePackages(upParams, status == UPDATE_SUCCESS); + PostUpdatePackages(upParams, status); UpdaterInit::GetInstance().InvokeEvent(UPDATER_POST_UPDATE_PACKAGE_EVENT); return status; } diff --git a/services/updater_main.h b/services/updater_main.h index 524c5380646ddfc91586b9e4014e3735e432e3e2..daf901f3479bcef8c96514d4e244b70001aec403 100644 --- a/services/updater_main.h +++ b/services/updater_main.h @@ -74,6 +74,7 @@ UpdaterStatus ProcessOtherOption([[maybe_unused]] const std::string &option, [[maybe_unused]] UpdaterParams &upParams, PackageUpdateMode &mode); bool PreStartBinaryEntry([[maybe_unused]] const std::string &path); int32_t VerifySpecialPkgs([[maybe_unused]]UpdaterParams &upParams); +UpdaterStatus CheckAndSetSlot([[maybe_unused]]UpdaterParams &upParams); void UpdaterVerifyFailEntry(bool verifyret); bool IsSpareBoardBoot(void); bool IsNeedWipe(); diff --git a/utils/include/utils.h b/utils/include/utils.h index 11170ddac0cabf48a86ecee9f8685adddfa3366c..c376dd7d555c71955bf26e053ac16e4e9bdda057 100644 --- a/utils/include/utils.h +++ b/utils/include/utils.h @@ -24,6 +24,11 @@ #include namespace Updater { +enum SlotType { + NOT_AB, + SLOT_A, + SLOT_B, +}; namespace Utils { constexpr int N_BIN = 2; constexpr int N_OCT = 8; @@ -109,6 +114,9 @@ bool ConvertToLong(const std::string &str, int32_t &value); bool ConvertToUnsignedLong(const std::string &str, uint32_t &value, int base = 0); bool ConvertToDouble(const std::string &str, double &value); bool ConvertToFloat(const std::string &str, float &value); +bool IsVabDevice(); +bool SetUpdateSlot(int updateSlot); +int GetUpdateSlot(); #ifndef __WIN32 void SetFileAttributes(const std::string& file, uid_t owner, gid_t group, mode_t mode); #endif diff --git a/utils/utils.cpp b/utils/utils.cpp index 96341e9159b3d6635262b897e0c10d6a468f3656..31ebf319b5a56b38174b259292b95ed3dcbcda3e 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -1055,6 +1055,59 @@ bool ConvertToFloat(const std::string &str, float &value) return true; } +bool IsVabDevice() +{ + char partType[PARAM_SIZE + 1] = {0}; + if (GetParameter("ohos.boot.update.partition_type", "", partType, sizeof(partType) - 1) <= 0) { + LOG(ERROR) << "get part partition failed"; + return false; + } + LOG(INFO) << "device type is " << partType; + return std::string(partType) == "vab"; +} + +bool SetUpdateSlot(int setSlot) +{ + if (setSlot == -1) { + LOG(ERROR) << "setSlot is invalid value"; + return false; + } + int tryNum = 3; + std::string setSlotStr = std::to_string(setSlot); + while (tryNum-- > 0) { + if (SetParameter("update.part.slot", setSlotStr.c_str()) != 0) { + LOG(ERROR) << "set update.part.slot fail"; + continue; + } + if (GetUpdateSlot() == setSlot) { + LOG(INFO) << "set update.part.slot is " << setSlot; + return true; + } + if (tryNum != 0) { + sleep(1); + } + } + return false; +} + +int GetUpdateSlot() +{ + if (!IsVabDevice()) { + return NOT_AB; + } + char paramValue[PARAM_SIZE + 1] = {0}; + if (GetParameter("update.part.slot", "", paramValue, sizeof(paramValue) - 1) <= 0) { + LOG(ERROR) << "get update.part.slot failed"; + return -1; + } + int updateSlot = -1; + if (!Utils::ConvertToLong(paramValue, updateSlot)) { + LOG(ERROR) << "ConvertToLong failed"; + return -1; + } + return updateSlot; +} + #ifndef __WIN32 void SetFileAttributes(const std::string& file, uid_t owner, gid_t group, mode_t mode) {