From b0a17d943306c5133fbe312bd9d8795ae19cbd52 Mon Sep 17 00:00:00 2001 From: xieluyao Date: Mon, 14 Oct 2024 16:33:02 +0800 Subject: [PATCH 1/3] update Signed-off-by: xieluyao --- services/updater_binary/update_image_block.h | 1 + .../updater_binary/update_image_patch.cpp | 4 ++ services/updater_binary/update_processor.cpp | 13 +++++- services/updater_binary/update_processor.h | 7 ++++ services/updater_main.cpp | 9 +++-- services/updater_main.h | 1 + services/updater_ui.cpp | 7 ++-- .../UpdaterStartUpdaterProc_fuzzer.cpp | 3 +- .../applypatch_fuzzer/applypatch_fuzzer.cpp | 40 ++++++++++++++++--- test/unittest/flashd_test/flashd_unittest.cpp | 5 --- test/unittest/script/script_unittest.h | 4 ++ .../service_test/updater_service_unittest.cpp | 5 +-- test/unittest/updater_binary/BUILD.gn | 2 + .../update_partitions_unittest.cpp | 4 +- .../updater_binary_unittest.cpp | 2 - .../updater_test/updater_unittest.cpp | 11 +++-- test/unittest/updater_ui_test/BUILD.gn | 3 ++ .../updaterkits_test/updaterkits_unittest.cpp | 2 +- updater_default_cfg.gni | 2 +- utils/include/utils.h | 12 +++++- utils/utils.cpp | 4 +- 21 files changed, 104 insertions(+), 37 deletions(-) diff --git a/services/updater_binary/update_image_block.h b/services/updater_binary/update_image_block.h index 2006b5ae..05a97670 100644 --- a/services/updater_binary/update_image_block.h +++ b/services/updater_binary/update_image_block.h @@ -53,6 +53,7 @@ private: void PrintAbnormalBlockHash(const std::string &devPath, const std::string &blockPairs); std::string CalculateBlockSha(const std::string &devPath, const std::string &blockPairs); int32_t SetShaInfo(Uscript::UScriptContext &context, ShaInfo &shaInfo); + bool IsTargetShaDiff(const std::string &devPath, const ShaInfo shaInfo); }; } diff --git a/services/updater_binary/update_image_patch.cpp b/services/updater_binary/update_image_patch.cpp index a10acd9d..172451b8 100644 --- a/services/updater_binary/update_image_patch.cpp +++ b/services/updater_binary/update_image_patch.cpp @@ -266,6 +266,10 @@ int32_t USInstrImageShaCheck::CheckHash(const CheckPara ¶) LOG(ERROR) << "PatchMapFile error"; return USCRIPT_ERROR_EXECUTE; } + if (!std:all_of(para.srcSize.begin(), para.srcSize.end(), ::isdigit)) { + LOG(ERROR) << "para size error" << para.srcSize; + return USCRIPT_ERROR_EXECUTE; + } size_t length = std::stoul(para.srcSize); UpdatePatch::BlockBuffer data = { mapBuffer.memory, length }; std::string resultSha = UpdatePatch::GeneraterBufferHash(data); diff --git a/services/updater_binary/update_processor.cpp b/services/updater_binary/update_processor.cpp index 5045e2df..238b4abb 100644 --- a/services/updater_binary/update_processor.cpp +++ b/services/updater_binary/update_processor.cpp @@ -83,7 +83,7 @@ const std::vector UpdaterEnv::GetInstructionNames() const static std::vector updaterCmds = { "sha_check", "first_block_check", "block_update", "raw_image_write", "update_partitions", "image_patch", - "image_sha_check", "pkg_extract", "update_from_bin" + "image_sha_check", "pkg_extract", "pkg_extract_no_ret", "update_from_bin" }; return updaterCmds; } @@ -107,6 +107,8 @@ int32_t UpdaterInstructionFactory::CreateInstructionInstance(UScriptInstructionP instr = new USInstrImageShaCheck(); } else if (name == "pkg_extract") { instr = new UScriptInstructionPkgExtract(); + } else if (name == "pkg_extract_no_ret") { + instr = new UScriptInstructionPkgExtractRetSuc(); } else if (name == "update_from_bin") { instr = new UScriptInstructionBinFlowWrite(); } @@ -292,6 +294,15 @@ int32_t UScriptInstructionPkgExtract::Execute(Uscript::UScriptEnv &env, Uscript: return ret; } +int32_t UScriptInstructionPkgExtractRetSuc:Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) +{ + int32_t ret = UScriptInstructionPkgExtract::Execute(env context); + if (ret != USCRIPT_SUCCESS) { + LOG(ERROR) << "Error to extract file, ret = " << ret; + } + return USCRIPT_SUCCESS; +} + int32_t UScriptInstructionUpdateFromBin::Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) { std::string upgradeFileName; diff --git a/services/updater_binary/update_processor.h b/services/updater_binary/update_processor.h index 47d5d02b..85a89657 100644 --- a/services/updater_binary/update_processor.h +++ b/services/updater_binary/update_processor.h @@ -85,6 +85,13 @@ public: int32_t Execute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override; }; +class UScriptInstructionUpdateFromBin : public UScriptInstructionPkgExtract { +public: + UScriptInstructionPkgExtractRetSuc() {} + ~UScriptInstructionPkgExtractRetSuc() override {} + int32_t Excute(Uscript::UScriptEnv &env, Uscript::UScriptContext &context) override; +}; + class UScriptInstructionUpdateFromBin : public Uscript::UScriptInstruction { public: UScriptInstructionUpdateFromBin() {} diff --git a/services/updater_main.cpp b/services/updater_main.cpp index 4c4ccb62..2fa90162 100644 --- a/services/updater_main.cpp +++ b/services/updater_main.cpp @@ -679,6 +679,11 @@ std::unordered_map> InitOptionsFuncTab(char* (void)UPDATER_UI_INSTANCE.SetMode(UPDATERMODE_REBOOTFACTORYRST); upParams.factoryResetMode = "menu_wipe_data"; }}, + {"wipe_data_factory_lowlever", [&]() -> void + { + (void)UPDATER_UI_INSTANCE.SetMode(UPDATERMODE_REBOOTFACTORYRST); + upParams.factoryResetMode = "user_wipe_data"; + }} {"upgraded_pkg_num", [&]() -> void { upParams.pkgLocation = static_cast(atoi(optarg)); @@ -791,7 +796,7 @@ __attribute__((weak)) bool IsNeedWipe() void RebootAfterUpdateSuccess(const UpdaterParams &upParams) { - if (IsNeedWipe() || upParams.sdExtMode == SDCARD_UPDATE_FROM_DEV) { + if (IsNeedWipe() || upParams.sdExtMode == SDCARD_UPDATE_FROM_DEV || upParams.sdExtMode == SDCARD_UPDATE_FROM_DATA) { Utils::UpdaterDoReboot("updater", "--user_wipe_data"); return; } @@ -830,8 +835,6 @@ int UpdaterMain(int argc, char **argv) UPDATER_UI_INSTANCE.ShowLogRes( status == UPDATE_CORRUPT ? TR(LOGRES_VERIFY_FAILED) : TR(LOGRES_UPDATE_FAILED)); UPDATER_UI_INSTANCE.ShowFailedPage(); - Utils::UsSleep(5 * DISPLAY_TIME); // 5 : 5s - UPDATER_UI_INSTANCE.ShowMainpage(); } else if (upParams.factoryResetMode == "user_wipe_data" || upParams.factoryResetMode == "menu_wipe_data" || upParams.factoryResetMode == "factory_wipe_data") { UPDATER_UI_INSTANCE.ShowFailedPage(); diff --git a/services/updater_main.h b/services/updater_main.h index f0019166..e612012c 100644 --- a/services/updater_main.h +++ b/services/updater_main.h @@ -25,6 +25,7 @@ namespace Updater { enum FactoryResetMode { USER_WIPE_DATA = 0, FACTORY_WIPE_DATA, + INVALID_MODE, MENU_WIPE_DATA, }; diff --git a/services/updater_ui.cpp b/services/updater_ui.cpp index c1fa1cbc..68032356 100644 --- a/services/updater_ui.cpp +++ b/services/updater_ui.cpp @@ -23,11 +23,11 @@ #include "updater_main.h" #include "updater_ui_facade.h" #include "utils.h" +#include "updater_ui_stub.h" namespace Updater { namespace { constexpr uint32_t DISPLAY_TIME = 1 * 1000 * 1000; /* 1s */ -constexpr uint32_t FAIL_DELAY = 5 * 1000 * 1000; constexpr uint32_t SUCCESS_DELAY = 3 * 1000 * 1000; constexpr int CALLBACK_DELAY = 20 * 1000; /* 20ms */ @@ -100,19 +100,18 @@ DEFINE_ASYN_CALLBACK(OnLabelSDCardEvt) DEFINE_ASYN_CALLBACK(OnLabelSDCardNoDelayEvt) { - LOG(INFO) << "On Label SDCard"; + LOG(INFO) << "On Label SDCard No Delay"; if (!GetFacade().SetMode(UPDATERMODE_SDCARD)) { return; } Utils::UsSleep(CALLBACK_DELAY); UpdaterParams upParams; upParams.updateMode = SDCARD_UPDATE; + UPDATER_UI_INSTANCE.ShowProgressPage(); if (auto res = UpdaterFromSdcard(upParams); res != UPDATE_SUCCESS) { Utils::RemoveUpdateInfoFromMisc("sdcard_update"); GetFacade().ShowLogRes(res == UPDATE_CORRUPT ? TR(LOGRES_VERIFY_FAILED) : TR(LOGRES_UPDATE_FAILED)); GetFacade().ShowFailedPage(); - Utils::UsSleep(FAIL_DELAY); - GetFacade().ShowMainpage(); return; } GetFacade().ShowLogRes(TR(LABEL_UPD_OK_DONE)); diff --git a/test/fuzztest/UpdaterStartUpdaterProc_fuzzer/UpdaterStartUpdaterProc_fuzzer.cpp b/test/fuzztest/UpdaterStartUpdaterProc_fuzzer/UpdaterStartUpdaterProc_fuzzer.cpp index d59dcd28..1a3120b2 100644 --- a/test/fuzztest/UpdaterStartUpdaterProc_fuzzer/UpdaterStartUpdaterProc_fuzzer.cpp +++ b/test/fuzztest/UpdaterStartUpdaterProc_fuzzer/UpdaterStartUpdaterProc_fuzzer.cpp @@ -160,14 +160,13 @@ static int StartUpdaterProcFun(const std::string &patch) { UpdaterStatus status; std::vector components; - int maxTemperature; PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance(); pkgManager->LoadPackage(patch, GetTestCertName(), components); UpdaterParams upParams; upParams.updatePackage.push_back(patch); upParams.retryCount = 0; - status = StartUpdaterProc(pkgManager, upParams, maxTemperature); + status = StartUpdaterProc(pkgManager, upParams); LOG(INFO) << "[fuzz] status " << status; PkgManager::ReleasePackageInstance(pkgManager); return status; diff --git a/test/fuzztest/applypatch_fuzzer/applypatch_fuzzer.cpp b/test/fuzztest/applypatch_fuzzer/applypatch_fuzzer.cpp index dbd74df5..ac608194 100644 --- a/test/fuzztest/applypatch_fuzzer/applypatch_fuzzer.cpp +++ b/test/fuzztest/applypatch_fuzzer/applypatch_fuzzer.cpp @@ -21,15 +21,46 @@ #include #include #include +#include +#include +#include "log/log.h" using namespace Updater; namespace OHOS { + bool WriteDataToFile(const char* data, size_t size, const char* filePath) + { + std::ofstream ofs(filePath, std::ios::app | std::ios::binary); + if (!ofs.is_oper()) { + LOG(ERROR) << "open" << filePath << " failed"; + return false; + } + ofs.write(data, size); + ofs.close(); + return true; + } + void FuzzApplyPatch(const uint8_t* data, size_t size) { - const std::string filePath = "/data/fuzz/test/MountForPath_fuzzer.fstable"; - ApplyPatch(filePath, filePath, std::string(reinterpret_cast(data), size)); - ApplyPatch(filePath, std::string(reinterpret_cast(data), size), filePath); - ApplyPatch(std::string(reinterpret_cast(data), size), filePath, filePath); + bool ret =true; + const int magicNumberSize = 4; + const char* bspatchPath = "/data/applyPatchfuzzerBspatch"; + const char* imgpatchPath = "/data/applyPatchfuzzerImagpatch"; + const char* oldFilePath = "/data/applyPatchfuzzerOldFile"; + const char* newFilePath = "/data/applyPatchfuzzerNewFile"; + ret &= WriteDataToFile("BSDIFF40", magicNumSize, bspatchPath); + ret &= WriteDataToFile(reinterpret_cast(data), size, bspatchPath); + ret &= WriteDataToFile("PKGDIFF0", magicNumsize, imgpatchPth); + ret &= WriteDataToFile(reinterpret_cast(data), size, imgpatchPath); + ret &= WriteDataToFile(reinterpret_cast(data), size, oldpatchPath); + if (!ret) { + LOG(ERROR) << "create file failed"; + return; + } + ApplyPatch(bspatchPath, oldFilePath, newFilePath); + ApplyPatch(imgpatchPath,oldFilePath, newFilePath); + if (remove(bspatchPath) != 0 || remove(imgpatchPath) != 0 || remove(oldFilePath)) { + LOG(WARRING) << "Failde to delete file"; + } } } @@ -40,4 +71,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) OHOS::FuzzApplyPatch(data, size); return 0; } - diff --git a/test/unittest/flashd_test/flashd_unittest.cpp b/test/unittest/flashd_test/flashd_unittest.cpp index 465869fc..b1111095 100644 --- a/test/unittest/flashd_test/flashd_unittest.cpp +++ b/test/unittest/flashd_test/flashd_unittest.cpp @@ -249,11 +249,6 @@ std::unique_ptr GetTestWriter() return std::make_unique(); } -HWTEST_F(FLashServiceUnitTest, FlashdRegisterWriter, TestSize.Level1) -{ - FlashdImageWriter::GetInstance().RegisterUserWriter(IsTestImg, GetTestWriter); -} - HWTEST_F(FLashServiceUnitTest, PartitionDoErase, TestSize.Level1) { std::string partitionName = "test"; diff --git a/test/unittest/script/script_unittest.h b/test/unittest/script/script_unittest.h index 34a70a48..c5001bdf 100644 --- a/test/unittest/script/script_unittest.h +++ b/test/unittest/script/script_unittest.h @@ -51,6 +51,10 @@ public: { return PKG_SUCCESS; } + int32_t VerifyOtaPackage(const std::string &devPath, uint64_t offset, size_t size) override + { + return PKG_SUCCESS; + } int32_t VerifyBinFile(const std::string &packagePath, const std::string &keyPath, const std::string &version, const PkgBuffer &digest) override { diff --git a/test/unittest/service_test/updater_service_unittest.cpp b/test/unittest/service_test/updater_service_unittest.cpp index 99210414..a79f9294 100644 --- a/test/unittest/service_test/updater_service_unittest.cpp +++ b/test/unittest/service_test/updater_service_unittest.cpp @@ -316,9 +316,8 @@ HWTEST_F(UpdaterUtilUnitTest, StartUpdaterProcTest, TestSize.Level1) { Hpackage::PkgManager::PkgManagerPtr pkgManager = Hpackage::PkgManager::CreatePackageInstance(); UpdaterParams upParams; - int maxTemperature = 0; - EXPECT_EQ(StartUpdaterProc(nullptr, upParams, maxTemperature), UPDATE_CORRUPT); - EXPECT_EQ(StartUpdaterProc(pkgManager, upParams, maxTemperature), UPDATE_ERROR); + EXPECT_EQ(StartUpdaterProc(nullptr, upParams), UPDATE_CORRUPT); + EXPECT_EQ(StartUpdaterProc(pkgManager, upParams), UPDATE_ERROR); } HWTEST_F(UpdaterUtilUnitTest, CheckPathNeedMountSD, TestSize.Level0) diff --git a/test/unittest/updater_binary/BUILD.gn b/test/unittest/updater_binary/BUILD.gn index 9df5834b..7aa7c82d 100644 --- a/test/unittest/updater_binary/BUILD.gn +++ b/test/unittest/updater_binary/BUILD.gn @@ -23,6 +23,7 @@ ohos_unittest("binary_unittest") { sources = [ "update_image_block_test.cpp", "update_image_patch_unittest.cpp", + "update_partition_unittest.cpp", "update_processor_unittest.cpp", ] @@ -46,6 +47,7 @@ ohos_unittest("binary_unittest") { "${updater_path}/services/include", "${updater_path}/utils/include", "${updater_path}/utils/json", + "${updater_path}/services/package/pkg_algorithm", "${updater_path}/services/include/applypatch", "${updater_path}/services/script/script_manager", "${updater_path}/services/script/script_interpreter", diff --git a/test/unittest/updater_binary/update_partitions_unittest.cpp b/test/unittest/updater_binary/update_partitions_unittest.cpp index 504ded8a..36979836 100755 --- a/test/unittest/updater_binary/update_partitions_unittest.cpp +++ b/test/unittest/updater_binary/update_partitions_unittest.cpp @@ -61,7 +61,7 @@ HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest01, TestSize.Level1) int ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components); cout << "load package's ret:" << ret << endl; UpdaterEnv* env = new UpdaterEnv(pkgManager, nullptr, false); - ScriptManager* scriptManager = ScriptManager::GetScriptManager(env); + ScriptManager* scriptManager = ScriptManager::GetScriptManager(env, &scriptVerifier); for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) { ret = scriptManager->ExecuteScript(i); cout << " execute ret:" << ret << endl; @@ -84,7 +84,7 @@ HWTEST_F(UpdatePartitionsUnitTest, UpdatePartitions_Unitest02, TestSize.Level1) int ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components); cout << "load package's ret:" << ret << endl; UpdaterEnv* env = new UpdaterEnv(pkgManager, nullptr, false); - ScriptManager* scriptManager = ScriptManager::GetScriptManager(env); + ScriptManager* scriptManager = ScriptManager::GetScriptManager(env, &scriptVerifier); for (int32_t i = 0; i < ScriptManager::MAX_PRIORITY; i++) { ret = scriptManager->ExecuteScript(i); cout << " execute ret:" << ret << endl; diff --git a/test/unittest/updater_binary/updater_binary_unittest.cpp b/test/unittest/updater_binary/updater_binary_unittest.cpp index 6ddcb012..efb2d882 100755 --- a/test/unittest/updater_binary/updater_binary_unittest.cpp +++ b/test/unittest/updater_binary/updater_binary_unittest.cpp @@ -26,14 +26,12 @@ #include "script_utils.h" #include "unittest_comm.h" #include "update_processor.h" -#include "utils.h" using namespace std; using namespace Hpackage; using namespace Uscript; using namespace Updater; using namespace testing::ext; -using namespace Updater::Utils; namespace { class UpdaterBinaryUnittest : public ::testing::Test { diff --git a/test/unittest/updater_test/updater_unittest.cpp b/test/unittest/updater_test/updater_unittest.cpp index 9e8f0c5e..20ddcd35 100755 --- a/test/unittest/updater_test/updater_unittest.cpp +++ b/test/unittest/updater_test/updater_unittest.cpp @@ -71,28 +71,27 @@ HWTEST_F(UpdaterUnitTest, updater_StartUpdaterProc, TestSize.Level1) { std::string packagePath = "/data/updater/updater/updater_without_updater_binary.zip"; PkgManager::PkgManagerPtr pkgManager = PkgManager::CreatePackageInstance(); - int maxTemperature; UpdaterStatus status; - status = StartUpdaterProc(pkgManager, packagePath, 0, maxTemperature); + status = StartUpdaterProc(pkgManager, packagePath, 0); EXPECT_EQ(status, UPDATE_CORRUPT); packagePath = "/data/updater/updater/updater_with_incorrect_binary.zip"; - status = StartUpdaterProc(pkgManager, packagePath, 0, maxTemperature); + status = StartUpdaterProc(pkgManager, packagePath, 0); EXPECT_EQ(status, UPDATE_CORRUPT); packagePath = "/data/updater/updater/updater.zip"; std::vector components; int32_t ret = pkgManager->LoadPackage(packagePath, GetTestCertName(), components); EXPECT_EQ(ret, 0); - status = StartUpdaterProc(pkgManager, packagePath, 0, maxTemperature); + status = StartUpdaterProc(pkgManager, packagePath, 0); EXPECT_EQ(status, UPDATE_SUCCESS); // retrycount is greater than 0. - status = StartUpdaterProc(pkgManager, packagePath, 1, maxTemperature); + status = StartUpdaterProc(pkgManager, packagePath, 1); EXPECT_EQ(status, UPDATE_RETRY); packagePath = "/data/updater/updater/updater_binary_abnormal.zip"; - status = StartUpdaterProc(pkgManager, packagePath, 1, maxTemperature); + status = StartUpdaterProc(pkgManager, packagePath, 1); PkgManager::ReleasePackageInstance(pkgManager); EXPECT_EQ(status, UPDATE_ERROR); } diff --git a/test/unittest/updater_ui_test/BUILD.gn b/test/unittest/updater_ui_test/BUILD.gn index 8307b03e..6e96ca68 100644 --- a/test/unittest/updater_ui_test/BUILD.gn +++ b/test/unittest/updater_ui_test/BUILD.gn @@ -32,6 +32,7 @@ ohos_unittest("ui_unittest") { "ENABLE_VECTOR_FONT = 1", ] sources = [ + "control/ui_control_unittest.cpp", "lang/ui_language_unittest.cpp", "strategy/ui_strategy_unittest.cpp", "view/ui_component_unittest.cpp", @@ -42,6 +43,7 @@ ohos_unittest("ui_unittest") { "view/ui_view_proxy_unittest.cpp", ] sources += [ + "${updater_path}/services/ui/control/event_listener.cpp", "${updater_path}/services/ui/driver/drm_driver.cpp", "${updater_path}/services/ui/driver/fbdev_driver.cpp", "${updater_path}/services/ui/driver/graphic_engine.cpp", @@ -84,6 +86,7 @@ ohos_unittest("ui_unittest") { "${updater_path}/interfaces/kits/packages:libpackageExt", "${updater_path}/services/log:libupdaterlog", "${updater_path}/utils:libutils", + "//third_party/services/ui/control/event_listener.cpp", ] configs = [ "${updater_path}/test/unittest:utest_config" ] external_deps = [ diff --git a/test/unittest/updaterkits_test/updaterkits_unittest.cpp b/test/unittest/updaterkits_test/updaterkits_unittest.cpp index bd29f57c..0ac3b7e3 100755 --- a/test/unittest/updaterkits_test/updaterkits_unittest.cpp +++ b/test/unittest/updaterkits_test/updaterkits_unittest.cpp @@ -40,7 +40,7 @@ HWTEST_F(UpdaterKitsUnitTest, updater_kits_test01, TestSize.Level1) { const std::vector packageName1 = {""}; int ret = RebootAndInstallUpgradePackage(MISC_FILE, packageName1); - EXPECT_EQ(ret, 2); // 2 : path not exit + EXPECT_EQ(ret, 1); const std::vector packageName2 = {"/data/updater/updater/updater_without_updater_binary.zip"}; auto fp = std::unique_ptr(fopen(MISC_FILE.c_str(), "wb+"), fclose); diff --git a/updater_default_cfg.gni b/updater_default_cfg.gni index ca51e0ec..2f8f2101 100644 --- a/updater_default_cfg.gni +++ b/updater_default_cfg.gni @@ -13,7 +13,7 @@ import("//build/ohos.gni") -enable_ohos_startup_init_feature_ab_partition = false +init_feature_ab_partition = false declare_args() { updater_cfg_file = "" updater_ui_support = true diff --git a/utils/include/utils.h b/utils/include/utils.h index 018d8d08..7c7756eb 100644 --- a/utils/include/utils.h +++ b/utils/include/utils.h @@ -40,6 +40,8 @@ constexpr const char* ON_SERVER = "ON_SERVER"; template T String2Int(const std::string &str, int base = N_HEX) { + static_assert(std::is_same_v || std::is_same_v || std::is_same_v, + "type should be int or size_t or unsigned long long int"); char *end = nullptr; if (str.empty()) { errno = EINVAL; @@ -48,7 +50,15 @@ T String2Int(const std::string &str, int base = N_HEX) if (((str[0] == '0') && (str[1] == 'x')) || (str[1] == 'X')) { base = N_HEX; } - T result = strtoull(str.c_str(), &end, base); + T result = 0; + if constexpr (std::is_same_v) { + result = strtol(str.c_str(), &end, base); + else if constexpr (std::is_same_v || std::is_same_v) { + result = strtoull(str.c_str(), &end, base); + } else { + errno = EINVAL; + } + } return result; } int32_t DeleteFile(const std::string& filename); diff --git a/utils/utils.cpp b/utils/utils.cpp index b41c3c48..4f33a23c 100644 --- a/utils/utils.cpp +++ b/utils/utils.cpp @@ -290,12 +290,14 @@ bool WriteFully(int fd, const uint8_t *data, size_t size) do { written = write(fd, data, rest); } while (written < 0 && errno == EINTR); - if (written < 0) { return false; } data += written; rest -= static_cast(written); + if (rest != 0) { + LOG(INFO) << "totalSize = " << size << ", rest = " << rest; + } } return true; } -- Gitee From 548096f062f7e239b15f165dd98745525f303bfe Mon Sep 17 00:00:00 2001 From: xieluyao Date: Wed, 16 Oct 2024 11:41:38 +0800 Subject: [PATCH 2/3] update Signed-off-by: xieluyao --- services/diffpatch/patch/image_patch.cpp | 5 +- services/etc/updater_normal.cfg | 1 + .../include/applypatch/transfer_manager.h | 1 + services/include/package/pkg_manager.h | 2 + services/include/updater/updater.h | 2 +- services/include/updater/updater_const.h | 2 + .../package/pkg_manager/pkg_managerImpl.cpp | 51 +++++++++++++++++++ .../package/pkg_manager/pkg_manager_impl.h | 1 + services/ptable_parse/emmc_ptable.cpp | 32 +++++++----- services/ptable_parse/emmc_ptable.h | 4 +- services/sdcard_update/sdcard_update.cpp | 10 ++++ services/sdcard_update/sdcard_update.h | 1 + services/ui/control/event_listener.cpp | 2 + services/ui/view/view_api.cpp | 8 ++- services/updater.cpp | 8 +-- .../updater_binary/update_image_block.cpp | 30 ++++++----- services/updater_main.cpp | 1 + 17 files changed, 125 insertions(+), 36 deletions(-) diff --git a/services/diffpatch/patch/image_patch.cpp b/services/diffpatch/patch/image_patch.cpp index 7c313929..c197fff0 100644 --- a/services/diffpatch/patch/image_patch.cpp +++ b/services/diffpatch/patch/image_patch.cpp @@ -44,7 +44,7 @@ int32_t NormalImagePatch::ApplyImagePatch(const PatchParam ¶m, size_t &start offset += sizeof(int64_t); size_t patchOffset = static_cast(ReadLE(param.patch + offset)); offset += sizeof(int64_t); - if (srcStart + srcLen > param.oldSize) { + if (srcStart + srcLen > param.oldSize - srcStart param.oldSize) { + if (header.srcStart > param.oldsize || param.oldSize - header.srcStart < header.srcLength || header.patchOffset > param.patchSize) {} + header.srcStart, header.srcLength, param.oldSize, header.patchOffset) { PATCH_LOGE("Failed to check patch"); return -1; } diff --git a/services/etc/updater_normal.cfg b/services/etc/updater_normal.cfg index 3400b841..26ec19b7 100644 --- a/services/etc/updater_normal.cfg +++ b/services/etc/updater_normal.cfg @@ -14,6 +14,7 @@ "path" : ["/system/bin/write_updater", "updater_para"], "uid" : "update", "gid" : ["update", "system", "root"], + "once" : 1, "secon" : "u:r:write_updater:s0" } ] diff --git a/services/include/applypatch/transfer_manager.h b/services/include/applypatch/transfer_manager.h index 65b05101..3956e7ca 100644 --- a/services/include/applypatch/transfer_manager.h +++ b/services/include/applypatch/transfer_manager.h @@ -51,6 +51,7 @@ struct TransferParams { std::string storeBase; std::string freeStash; std::string retryFile; + std::string devPath; uint8_t *patchDataBuffer; size_t patchDataSize; }; diff --git a/services/include/package/pkg_manager.h b/services/include/package/pkg_manager.h index a455c619..5c636072 100644 --- a/services/include/package/pkg_manager.h +++ b/services/include/package/pkg_manager.h @@ -260,6 +260,8 @@ public: virtual int32_t VerifyAccPackage(const std::string &packagePath, const std::string &keyPath) = 0; + VerifyOtaPackage(const std::string &devPath, unit64_t offset, size_t size) = 0; + virtual int32_t VerifyOtaPackage(const std::string &packagePath) = 0; virtual int32_t VerifyBinFile(const std::string &packagePath, const std::string &keyPath, diff --git a/services/include/updater/updater.h b/services/include/updater/updater.h index a51a294e..7c3fd9fa 100644 --- a/services/include/updater/updater.h +++ b/services/include/updater/updater.h @@ -81,7 +81,7 @@ UpdaterStatus DoInstallUpdaterPackage(Hpackage::PkgManager::PkgManagerPtr pkgMan UpdaterParams &upParams, PackageUpdateMode updateMode); UpdaterStatus StartUpdaterProc(Hpackage::PkgManager::PkgManagerPtr pkgManager, - UpdaterParams &upParams, int &maxTemperature); + UpdaterParams &upParams); int GetUpdatePackageInfo(Hpackage::PkgManager::PkgManagerPtr pkgManager, const std::string& path); diff --git a/services/include/updater/updater_const.h b/services/include/updater/updater_const.h index 9466c8bd..672f6383 100644 --- a/services/include/updater/updater_const.h +++ b/services/include/updater/updater_const.h @@ -51,11 +51,13 @@ constexpr const char *OTA_MODE = "update_package"; constexpr const char *USB_MODE = "usb_update"; constexpr const char *SDCARD_INTRAL_MODE = "sdcard_intral_update"; constexpr const char *UPDATRE_SCRIPT_ZIP = "/etc/updater_script.zip"; +constexpr const char *FACTORY_INTERNAL_MODE = "factory_internal_update"; // sd update ext mode constexpr const char *SDCARD_NORMAL_UPDATE = "sdUpdate"; constexpr const char *SDCARD_UPDATE_FROM_DEV = "sdUpdateFromDev"; constexpr const char *SDCARD_MAINIMG = "mainUpdate"; +constexpr const char *SDCARD_UPDATE_FROM_DATA = "sdUpdateFromData"; #ifndef UPDATER_UT constexpr const char *SDCARD_CARD_PATH = "/sdcard/updater"; diff --git a/services/package/pkg_manager/pkg_managerImpl.cpp b/services/package/pkg_manager/pkg_managerImpl.cpp index 817c0b00..2a52d127 100644 --- a/services/package/pkg_manager/pkg_managerImpl.cpp +++ b/services/package/pkg_manager/pkg_managerImpl.cpp @@ -987,6 +987,57 @@ int32_t PkgManagerImpl::VerifyAccPackage(const std::string &packagePath, const s return PKG_SUCCESS; } +int32_t PkgManagerlmpl::VerifyOtaPakcage(const std::string &devPath,unit64_t offset, size_t size) +{ +#ifndef DIFF_PATCH_SDK + constexpr size_t pageSize = 4096; + size_t offsetAlogned = (offset / pageSize) * pageSize; + if (size == 0 || size + offset < offsetAligend || offset < offsetAligend){ + PKG_LOGE("invaild param %zu %" PRlu64 "%zu", size, offset, offsetAligned); + return PKG_INVAILD_PARAM; + } + char devRealPath[PATH_MAX + 1] = {}; + if (realpath(devPath.c_str(),devRealPath) == nullptr) { + PKG_LOGE("realPath is nullptr, err %s", strerror(erron)); + return PKG_INVALID_PARAM; + } + int fd = open(devRealPath, O_RDONLY | O_LARGEFILE); + if (fd < 0) { + PKG_LOGE("open %s fail, %s", devRealPath, strerror(errno)); + return PKG_INVAILD_FILE; + } + ON_SCOPE_EXIT(closePath) { + close(fd) + } + unit8_t *pMap = static_cast(mmap64(unllptr, size + offset - offsetAligned, PROT_READ, MAP_PRIVATE, + fd, offsetAligned)); + if (pMap == MAP_FAILD) { + PKG_LOGE("mmap64 %s fail, %s %zu %" PRlu64, devRealPath, strrerror(errno), size, offset); + return PKG_NONE_MEMORY; + } + ON_SCOPE_EXIT(unmapMem) { + munmap(pMap, size + offset - offsetAligned); + }; + PkgBuffer buffer(pMap + (offset - offsetAligned), size); + PkgStreamPtr pkgStream = nullptr; + int32_t ret = CreatPkgStream(pkgStream, devRealPath, buffer); + if (ret != PKG_SUCCCESS) { + PKG_LOGE("create pakage stream fail %s %s," devRealPath, strerror(errno)); + return ret; + } + ON_SCOPE_EXIT(closeStream) { + ClosePkgStream(pkgStream); + }; + PkgVerifyUtil verifyUtil {}; + ret = verifyUtil.VerifyPackageSign(PkgStream); + if (ret != PKG_SUCCESS) { + PKG_LOGE("verify pkcs7 signature failed."); + return ret; + } +#endif + return PKG_SUCCESS; +} + int32_t PkgManagerImpl::VerifyOtaPackage(const std::string &packagePath) { PkgStreamPtr pkgStream = nullptr; diff --git a/services/package/pkg_manager/pkg_manager_impl.h b/services/package/pkg_manager/pkg_manager_impl.h index 9cab6bd7..0cdeb253 100644 --- a/services/package/pkg_manager/pkg_manager_impl.h +++ b/services/package/pkg_manager/pkg_manager_impl.h @@ -77,6 +77,7 @@ public: uint64_t fileLen, Updater::RingBuffer *buffer) override; int32_t VerifyAccPackage(const std::string &packagePath, const std::string &keyPath) override; int32_t VerifyOtaPackage(const std::string &packagePath) override; + int32_t VerifyOtaPackage(const std::string &devPath, unit64_t offset, size_t size) override; int32_t VerifyBinFile(const std::string &packagePath, const std::string &keyPath, const std::string &version, const PkgBuffer &digest) override; diff --git a/services/ptable_parse/emmc_ptable.cpp b/services/ptable_parse/emmc_ptable.cpp index 8bce4a2a..9675fe2f 100644 --- a/services/ptable_parse/emmc_ptable.cpp +++ b/services/ptable_parse/emmc_ptable.cpp @@ -123,7 +123,7 @@ bool EmmcPtable::ParsePartitionFromBuffer(uint8_t *ptbImgBuffer, const uint32_t bool EmmcPtable::ParseGptHeaderByEmmc(uint8_t *gptImage, const uint32_t len) { GPTHeaderInfo gptHeaderInfo; - uint32_t blockSize = ptableData_.blockSize; // 4096 + uint32_t blockSize = EMMC_BLOCK_SIZE; (void)memset_s(&gptHeaderInfo, sizeof(GPTHeaderInfo), 0, sizeof(GPTHeaderInfo)); if (!GetPartitionGptHeaderInfo(gptImage + blockSize, blockSize, gptHeaderInfo)) { LOG(ERROR) << "GetPartitionGptHeaderInfo fail"; @@ -138,9 +138,15 @@ bool EmmcPtable::ParseGptHeaderByEmmc(uint8_t *gptImage, const uint32_t len) return PartitionCheckGptHeader(gptImage, len, lbaNum, blockSize, gptHeaderInfo); } -bool EmmcPtable::EmmcReadGpt(uint8_t *ptableData) +bool EmmcPtable::EmmcReadGpt(uint8_t *ptableData, unit32_t len) { uint32_t number = 0; + + partitionInfo_.clear(); + if (!ParseGptHeaderByEmmc(emmcPtnDataInfo_.data, len)) { + LOG(ERROR) << "Primary signatrue invaild"; + rerurn false; + } for (uint32_t i = 0; i < MAX_PARTITION_NUM; i++) { uint8_t *startLbaOffset = ptableData + GPT_PARTITION_START_LBA_OFFSET; uint8_t *endLbaOffset = ptableData + GPT_PARTITION_START_LBA_OFFSET + GPT_PARTITION_END_LBA_OFFSET; @@ -159,6 +165,8 @@ bool EmmcPtable::EmmcReadGpt(uint8_t *ptableData) LOG(ERROR) << "memcpy guid fail"; } newPtnInfo.startAddr = startLba * LBA_LENGTH; + newPtnInfo.writePath = MMC_BLOCK_DEV_NAME; + newPtnInfo.wirteMode = "WIRTE_RAW"; /* General algorithm : calculate partition size by lba */ newPtnInfo.partitionSize = (endLba - startLba + 1) * LBA_LENGTH; ptableData += GPT_PARTITION_INFO_LENGTH; @@ -176,7 +184,7 @@ bool EmmcPtable::UpdateCommInitializeGptPartition(uint8_t *gptImage, const uint3 return false; } uint32_t imgBlockSize = ptableData_.lbaLen; // 512 - uint32_t deviceBlockSize = ptableData_.blockSize; // 4096 + uint32_t deviceBlockSize = EMMC_BLOCK_SIZE; uint8_t *gptHeaderStart = gptImage + imgBlockSize; uint8_t *ptableData = gptImage + PROTECTIVE_MBR_SIZE + LBA_LENGTH; /* skip MBR and gpt header */ @@ -204,14 +212,10 @@ bool EmmcPtable::UpdateCommInitializeGptPartition(uint8_t *gptImage, const uint3 return false; } emmcPtnDataInfo_.writeDataLen = len; - if (!ParseGptHeaderByEmmc(emmcPtnDataInfo_.data, len)) { - LOG(ERROR) << "Primary signature invalid"; - return false; - } EmmcPatchGptHeader(emmcPtnDataInfo_, deviceBlockSize); emmcPtnDataInfo_.isGptVaild = true; - return EmmcReadGpt(ptableData); + return EmmcReadGpt(emmcPtnDataInfo_.data + 2 * deviceBlockSize, len); // 2:skip 2lba length to set gpt entry } bool EmmcPtable::ReadEmmcGptImageToRam() @@ -224,7 +228,7 @@ bool EmmcPtable::ReadEmmcGptImageToRam() int32_t imgLen = GPT_PARTITION_SIZE; auto buf = std::make_unique(imgLen); uint8_t *buffer = buf.get(); - uint32_t deviceBlockSize = ptableData_.blockSize; // 4096 + uint32_t deviceBlockSize = EMMC_BLOCK_SIZE; if (buffer == nullptr) { LOG(ERROR) << "new buffer failed!"; return false; @@ -251,7 +255,7 @@ bool EmmcPtable::ReadEmmcGptImageToRam() emmcPtnDataInfo_.isGptVaild = true; uint8_t *ptableData = buffer + 2 * deviceBlockSize; /* skip MBR and gpt header */ - return EmmcReadGpt(ptableData); + return EmmcReadGpt(ptableData, imglen); } bool EmmcPtable::LoadPtableFromDevice() @@ -282,7 +286,7 @@ bool EmmcPtable::GetPtableImageBuffer(uint8_t *imageBuf, const uint32_t imgBufSi bool EmmcPtable::EditPartitionBuf(uint8_t *imageBuf, uint64_t imgBufSize, std::vector &modifyList) { - if (imageBuf == nullptr || imgBufSize == 0 || modifyList.empty() == 0) { + if (imageBuf == nullptr || imgBufSize == 0 || modifyList.empty()) { LOG(ERROR) << "input invalid"; return false; } @@ -291,9 +295,9 @@ bool EmmcPtable::EditPartitionBuf(uint8_t *imageBuf, uint64_t imgBufSize, std::v uint8_t *gptHeader = gptImage + imgBlockSize; uint32_t maxPtnCnt = GET_LWORD_FROM_BYTE(&gptHeader[PARTITION_COUNT_OFFSET]); uint32_t ptnEntrySize = GET_LWORD_FROM_BYTE(&gptHeader[PENTRY_SIZE_OFFSET]); - uint32_t gptHeaderLen = EMMC_BLOCK_SIZE; - uint32_t gptSize = static_cast(maxPtnCnt) * ptnEntrySize + imgBlockSize + gptHeaderLen; - uint32_t devDensity = GetDeviceCapacity(); + uint64_t gptHeaderLen = EMMC_BLOCK_SIZE; + uint64_t gptSize = static_cast(maxPtnCnt) * ptnEntrySize + imgBlockSize + gptHeaderLen; + uint64_t devDensity = GetDeviceCapacity(); if (devDensity == 0) { LOG(ERROR) << "get emmc capacity fail"; return false; diff --git a/services/ptable_parse/emmc_ptable.h b/services/ptable_parse/emmc_ptable.h index fcec50f7..8f9ccded 100644 --- a/services/ptable_parse/emmc_ptable.h +++ b/services/ptable_parse/emmc_ptable.h @@ -42,7 +42,7 @@ private: static constexpr uint32_t LBA_LENGTH = 512; static constexpr uint32_t GPT_PARTITION_INFO_LENGTH = 128; static constexpr uint32_t PROTECTIVE_MBR_SIZE = 512; - static constexpr uint32_t MIN_EMMC_WRITE_SIZE = 4096; + static constexpr uint32_t MIN_EMMC_WRITE_SIZE = 512; static constexpr uint32_t EMMC_BLOCK_SIZE = 512; struct EmmcPartitionDataInfo { @@ -53,7 +53,7 @@ private: struct EmmcPartitionDataInfo emmcPtnDataInfo_; - bool EmmcReadGpt(uint8_t *ptableData); + bool EmmcReadGpt(uint8_t *ptableData, unit32_t len); bool UpdateCommInitializeGptPartition(uint8_t *gptImage, const uint32_t len); bool ReadEmmcGptImageToRam(); uint64_t GetDeviceCapacity(); diff --git a/services/sdcard_update/sdcard_update.cpp b/services/sdcard_update/sdcard_update.cpp index ef0d4766..f504ef29 100644 --- a/services/sdcard_update/sdcard_update.cpp +++ b/services/sdcard_update/sdcard_update.cpp @@ -98,6 +98,11 @@ UpdaterStatus CheckSdcardPkgs(UpdaterParams &upParams) LOG(INFO) << "get sd card from dev succeed, skip get package from sd card"; return UPDATE_SUCCESS; } + if (GetSdcardInternalPkgs(upParam == UPDATE_SUCCESS)) { + LOG(INFO) << "get sdcard internal pkgs succeed"; + return UPDATE_SUCCESS; + } + std::string mountPoint = std::string(SDCARD_PATH); std::vector sdcardStr = GetBlockDevicesByMountPoint(mountPoint); if (sdcardStr.empty()) { @@ -125,4 +130,9 @@ UpdaterStatus CheckSdcardPkgs(UpdaterParams &upParams) } return UPDATE_SUCCESS; } + __attribute__((weak)) UpdaterStatusGetSdcardInternalPkgs(UpdaterParams &upParams) + { + LOG(INFO) << "not implemented ger normal updater sdcard pkgs"; + return UPDATE_ERROR; + } } // Updater diff --git a/services/sdcard_update/sdcard_update.h b/services/sdcard_update/sdcard_update.h index 5e607594..9f238b03 100644 --- a/services/sdcard_update/sdcard_update.h +++ b/services/sdcard_update/sdcard_update.h @@ -29,6 +29,7 @@ extern "C" { #endif UpdaterStatus GetSdcardPkgsPath(UpdaterParams &upParams); UpdaterStatus GetSdcardPkgsFromDev(UpdaterParams &upParams); +UpdaterStatus GetSdcardInternalPkgs(UpdaterParams &upParams); #ifdef __cplusplus #if __cplusplus } diff --git a/services/ui/control/event_listener.cpp b/services/ui/control/event_listener.cpp index 93f6c92d..faced61b 100644 --- a/services/ui/control/event_listener.cpp +++ b/services/ui/control/event_listener.cpp @@ -154,6 +154,7 @@ bool KeyListener::OnKeyAct(OHOS::UIView &view, const OHOS::KeyEvent &event) bool KeyListener::ProcessPowerKey(OHOS::UIView &view, const OHOS::KeyEvent &event) { + #ifndef UPDATER_UT OHOS::UIView *pView = OHOS::FocusManager::GetInstance()->GetFocusedView(); if (pView == nullptr) { LOG(ERROR) << "focused view is nullptr"; @@ -170,6 +171,7 @@ bool KeyListener::ProcessPowerKey(OHOS::UIView &view, const OHOS::KeyEvent &even LOG(DEBUG) << "OnPress"; pView->OnClickEvent(OHOS::Point { centerX, centerY }); } + #endif return true; } diff --git a/services/ui/view/view_api.cpp b/services/ui/view/view_api.cpp index c8bf1d48..44f33650 100644 --- a/services/ui/view/view_api.cpp +++ b/services/ui/view/view_api.cpp @@ -72,7 +72,13 @@ OHOS::ColorType StrToColor(const std::string &hexColor) std::size_t startPos = 1uL; auto getNextField = [&startPos, &hexColor] () { constexpr std::size_t width = 2uL; - uint8_t ret = (startPos > hexColor.size()) ? 0 : Utils::String2Int(hexColor.substr(startPos, width)); + constexpr unit8_t colorMaxSize = 255; + int reset = Utils::String2Int(hexColor.substr(startPos, width)); + if (reset < 0 || reset > static_cast(colorMaxSize)) { + LOG(ERROR) << "String2Int error, rest =" << reset; + return colorMaxSize; + } + uint8_t ret = (startPos > hexColor.size()) ? 0 : static_cast(reset)); startPos += width; return ret; }; diff --git a/services/updater.cpp b/services/updater.cpp index cb0bb70a..18d82359 100644 --- a/services/updater.cpp +++ b/services/updater.cpp @@ -258,7 +258,8 @@ UpdaterStatus DoInstallUpdaterPackage(PkgManager::PkgManagerPtr pkgManager, Upda } if (SetupPartitions(updateMode != SDCARD_UPDATE || upParams.sdExtMode == SDCARD_UPDATE_FROM_DEV || - Utils::CheckUpdateMode(Updater::SDCARD_INTRAL_MODE)) != 0) { + upParams.sdExtMode == SDCARD_UPDATE_FROM_DATA || Utils::CheckUpdateMode(Updater::SDCARD_INTAL_MODE) || + Utils::CheckUpdateMode(Updater::ACTORY_INTERNAL_MODE)) != 0) { UPDATER_UI_INSTANCE.ShowUpdInfo(TR(UPD_SETPART_FAIL), true); UPDATER_LAST_WORD(UPDATE_ERROR); return UPDATE_ERROR; @@ -278,8 +279,7 @@ UpdaterStatus DoInstallUpdaterPackage(PkgManager::PkgManagerPtr pkgManager, Upda } g_tmpProgressValue = 0; - int maxTemperature; - UpdaterStatus updateRet = StartUpdaterProc(pkgManager, upParams, maxTemperature); + UpdaterStatus updateRet = StartUpdaterProc(pkgManager, upParams); if (updateRet != UPDATE_SUCCESS) { UPDATER_UI_INSTANCE.ShowUpdInfo(TR(UPD_INSTALL_FAIL)); LOG(ERROR) << "Install package failed."; @@ -438,7 +438,7 @@ UpdaterStatus CheckProcStatus(pid_t pid, bool retryUpdate) return UPDATE_SUCCESS; } -UpdaterStatus StartUpdaterProc(PkgManager::PkgManagerPtr pkgManager, UpdaterParams &upParams, int &maxTemperature) +UpdaterStatus StartUpdaterProc(PkgManager::PkgManagerPtr pkgManager, UpdaterParams &upParams) { UPDATER_INIT_RECORD; int pfd[DEFAULT_PIPE_NUM]; /* communication between parent and child */ diff --git a/services/updater_binary/update_image_block.cpp b/services/updater_binary/update_image_block.cpp index 21926f8e..54357b60 100644 --- a/services/updater_binary/update_image_block.cpp +++ b/services/updater_binary/update_image_block.cpp @@ -186,13 +186,14 @@ static int32_t GetUpdateBlockInfo(struct UpdateBlockInfo &infos, Uscript::UScrip } static int32_t ExecuteTransferCommand(int fd, const std::vector &lines, TransferManagerPtr tm, - Uscript::UScriptContext &context, const std::string &partitionName) + Uscript::UScriptContext &context, const UpdateBlockInfo &infos) { auto transferParams = tm->GetTransferParams(); auto writerThreadInfo = transferParams->writerThreadInfo.get(); - transferParams->storeBase = std::string("/data/updater") + partitionName + "_tmp"; - transferParams->retryFile = std::string("/data/updater") + partitionName + "_retry"; + transferParams->storeBase = std::string("/data/updater") + infos.partitionName + "_tmp"; + transferParams->retryFile = std::string("/data/updater") + infos.partitionName + "_retry"; + transferParams->devPath = infos.devPath; LOG(INFO) << "Store base path is " << transferParams->storeBase; int32_t ret = Store::CreateNewSpace(transferParams->storeBase, !transferParams->env->IsRetry()); if (ret == -1) { @@ -289,7 +290,7 @@ static int32_t DoExecuteUpdateBlock(const UpdateBlockInfo &infos, TransferManage env->GetPkgManager()->ClosePkgStream(outStream); return USCRIPT_ERROR_EXECUTE; } - int32_t ret = ExecuteTransferCommand(fd, lines, tm, context, infos.partitionName); + int32_t ret = ExecuteTransferCommand(fd, lines, tm, context, infos); fsync(fd); close(fd); fd = -1; @@ -471,21 +472,26 @@ int32_t UScriptInstructionBlockCheck::Execute(Uscript::UScriptEnv &env, Uscript: return USCRIPT_SUCCESS; } +bool UScriptInstructionShaCheck::IsTargetShaDiff(const std::string &devPath, const ShaInfo &shaInfo) +{ + std::string tgtResultSha = CaculateBlockSha(devPath, shaInfo.targetParis); + if (tgtResultSha.empty()) { + LOG(WARRING) << target sha is empty; + return true; + } + LOG(INFO) << "tgtResultSha: " << tgtResultSha << ", shaInfo.targetSha: " << shaInfo.targetSha; + return (tgtResultSha != shaInfo.targetSha); +} + int UScriptInstructionShaCheck::ExecReadShaInfo(Uscript::UScriptEnv &env, const std::string &devPath, const ShaInfo &shaInfo) { UPDATER_INIT_RECORD; std::string resultSha = CalculateBlockSha(devPath, shaInfo.blockPairs); - std::string tgtResultSha = CalculateBlockSha(devPath, shaInfo.targetPairs); - if (resultSha.empty() && tgtResultSha.empty()) { - LOG(ERROR) << "All sha is empty"; - return USCRIPT_ERROR_EXECUTE; - } - - bool isTargetDiff = tgtResultSha.empty() ? true : (tgtResultSha != shaInfo.targetSha); - if (resultSha != shaInfo.contrastSha && isTargetDiff) { + if (resultSha != shaInfo.contrastSha && isTargetDiff(devPath, shaInfo)) { LOG(ERROR) << "Different sha256, cannot continue"; LOG(ERROR) << "blockPairs:" << shaInfo.blockPairs; + LOG(ERROR) << "resultSha: " << resultSha << ", shaInfo.contrastSha: " << shaInfo.contrastSha; PrintAbnormalBlockHash(devPath, shaInfo.blockPairs); UPDATER_LAST_WORD(devPath.substr(devPath.find_last_of("/") + 1), USCRIPT_ERROR_EXECUTE); env.PostMessage(UPDATER_RETRY_TAG, VERIFY_FAILED_REBOOT); diff --git a/services/updater_main.cpp b/services/updater_main.cpp index 2fa90162..40d2fb69 100644 --- a/services/updater_main.cpp +++ b/services/updater_main.cpp @@ -75,6 +75,7 @@ constexpr struct option OPTIONS[] = { { "UPDATE:SD", no_argument, nullptr, 0 }, { "UPDATE:SDFROMDEV", no_argument, nullptr, 0 }, { "sdcard_intral_update", optional_argument, nullptr, 0}, + { "wipe_data_factory_lowlevel", no_argument, nullptr, 0}, { nullptr, 0, nullptr, 0 }, }; constexpr float VERIFY_PERCENT = 0.05; -- Gitee From 1dc9a6f34e6657e24c83e3547de4622b4f52c3b2 Mon Sep 17 00:00:00 2001 From: xieluyao Date: Thu, 17 Oct 2024 02:02:44 +0000 Subject: [PATCH 3/3] update services/diffpatch/patch/image_patch.cpp. Signed-off-by: xieluyao --- services/diffpatch/patch/image_patch.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/services/diffpatch/patch/image_patch.cpp b/services/diffpatch/patch/image_patch.cpp index c197fff0..6e77cd81 100644 --- a/services/diffpatch/patch/image_patch.cpp +++ b/services/diffpatch/patch/image_patch.cpp @@ -44,7 +44,8 @@ int32_t NormalImagePatch::ApplyImagePatch(const PatchParam ¶m, size_t &start offset += sizeof(int64_t); size_t patchOffset = static_cast(ReadLE(param.patch + offset)); offset += sizeof(int64_t); - if (srcStart + srcLen > param.oldSize - srcStart param.oldSize || param.oldSize - srcStart < srcLen || + patchOffset > param.patchSzie) { PATCH_LOGE("error, srcStart: %zu srcLen: %zu , param.oldSize: %zu, patchOffset: %zu", srcStart, srcLen, param.oldSize, patchOffset); return -1; @@ -96,9 +97,10 @@ int32_t CompressedImagePatch::StartReadHeader(const PatchParam ¶m, PatchHead } PATCH_LOGI("ApplyImagePatch srcStart %zu srcLen %zu patchOffset: %zu expandedLen:%zu %zu", header.srcStart, header.srcLength, header.patchOffset, header.expandedLen, header.targetSize); - if (header.srcStart > param.oldsize || param.oldSize - header.srcStart < header.srcLength || header.patchOffset > param.patchSize) {} - header.srcStart, header.srcLength, param.oldSize, header.patchOffset) { - PATCH_LOGE("Failed to check patch"); + if (header.srcStart > param.oldsize || param.oldSize - header.srcStart < header.srcLength || + header.patchOffset > param.patchSize) { + PATCH_LOGE("Failed to check patch, srcStart: %zu , param.oldSize: %zu, patchOffset: %zu", + header.srcStart, header.srcLength, param.oldSize, header.patchOffset); return -1; } return 0; -- Gitee