From 8b2852f042d706ce0942b6534b66e011d4073a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=98=E7=BD=97=E5=AE=87?= Date: Fri, 20 Jun 2025 14:28:49 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=9B=9E=E6=BB=9A?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 甘罗宇 --- interfaces/kits/include/ota_event/ota_event.h | 31 ++++++++ interfaces/kits/include/slot_info/slot_info.h | 1 + interfaces/kits/ota_event/BUILD.gn | 45 ++++++++++++ interfaces/kits/ota_event/ota_event.cpp | 71 +++++++++++++++++++ interfaces/kits/slot_info/slot_info.cpp | 18 ++++- services/updater_main.cpp | 1 + 6 files changed, 164 insertions(+), 3 deletions(-) create mode 100755 interfaces/kits/include/ota_event/ota_event.h create mode 100755 interfaces/kits/ota_event/BUILD.gn create mode 100755 interfaces/kits/ota_event/ota_event.cpp diff --git a/interfaces/kits/include/ota_event/ota_event.h b/interfaces/kits/include/ota_event/ota_event.h new file mode 100755 index 00000000..93c781d0 --- /dev/null +++ b/interfaces/kits/include/ota_event/ota_event.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OTA_EVENT_H +#define OTA_EVENT_H + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + +int SetOTAStatus(int status); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif +#endif /* SLOT_INFO_H */ \ No newline at end of file diff --git a/interfaces/kits/include/slot_info/slot_info.h b/interfaces/kits/include/slot_info/slot_info.h index fa92c546..f94cb242 100644 --- a/interfaces/kits/include/slot_info/slot_info.h +++ b/interfaces/kits/include/slot_info/slot_info.h @@ -24,5 +24,6 @@ namespace Updater { void GetPartitionSuffix(std::string &suffix); void GetActivePartitionSuffix(std::string &suffix); void SetActiveSlot(); +void SetOTACheckpoint(int32_t checkpoint); } // Updater #endif /* SLOT_INFO_H */ diff --git a/interfaces/kits/ota_event/BUILD.gn b/interfaces/kits/ota_event/BUILD.gn new file mode 100755 index 00000000..d5cf2c6b --- /dev/null +++ b/interfaces/kits/ota_event/BUILD.gn @@ -0,0 +1,45 @@ +# Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/update/updater/updater_default_cfg.gni") +import("//build/ohos.gni") + +updater_path = rebase_path("${updater_absolutely_path}", ".") +config("ota_event_exported_config") { + visibility = [ ":*" ] + include_dirs = [ "${updater_path}/interfaces/kits/include" ] +} +ohos_static_library("libotainfo") { + sources = [ "ota_event.cpp" ] + + include_dirs = [ + "${updater_path}/interfaces/kits/include", + "${updater_path}/services/include", + "${updater_path}/utils/include", + "//drivers/peripheral/partitionslot/hal/include/", + ] + + deps = [ + "${updater_path}/interfaces/kits/slot_info:libslotinfo", + "${updater_path}/services/log:libupdaterlog", + ] + public_configs = [ ":ota_event_exported_config" ] + if (init_feature_ab_partition) { + defines = [ "UPDATER_AB_SUPPORT" ] + external_deps = + [ "drivers_peripheral_partitionslot:libpartition_slot_manager" ] + } + + subsystem_name = "updater" + part_name = "updater" +} \ No newline at end of file diff --git a/interfaces/kits/ota_event/ota_event.cpp b/interfaces/kits/ota_event/ota_event.cpp new file mode 100755 index 00000000..072a0245 --- /dev/null +++ b/interfaces/kits/ota_event/ota_event.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ota_event/ota_event.h" +#include "log/log.h" +#include +#include +#include +#include "slot_info/slot_info.h" +#ifdef UPDATER_AB_SUPPORT +#include "partitionslot_manager.h" +#endif + +#ifdef UPDATER_AB_SUPPORT +enum ReturnSetOtaStatus { + SET_OTA_STATUS_FAILED = -1, // Failed to call SetOTAStatus to set information. + BOOT_SUCCESS_IN_OTA = 0, // In the ota stage, startup is complete. + BOOT_SUCCESS_IN_NORMAL = 1, // In the non-ota stage, startup is complete. + BOOT_FAILD_IN_OTA = 2, // In the ota stage, startup is failed. + BOOT_FAILD_IN_NORMAL = 3, // In the non-ota stage, startup is failed. +}; +int SetOTAStatus(int status) +{ + OHOS::HDI::Partitionslot::V1_1::PartitionSlotManager psMgr; + int32_t checkpoint = -1; + + int32_t retGetcheckpoint = psMgr.GetCheckpoint(checkpoint); + if (retGetcheckpoint != 0 || checkpoint == -1) { + return SET_OTA_STATUS_FAILED; + } + // In the ota stage, startup is failed + if (status == 0 && checkpoint == 1) { + // switch partition + Updater::SetActiveSlot(); + // Write checkpoint=0 to prevent remounting the data partition the next time it is accessed + // Upgrade fails, reboot to do userdata rollback + int retSetcheckpoint = psMgr.SetCheckpoint(0); + if (retSetcheckpoint != 0) { + return SET_OTA_STATUS_FAILED; + } + return BOOT_FAILD_IN_OTA; + // In the ota stage, startup is complete. + } else if (status == 1 && checkpoint == 1) { + // Write checkpoint=0 to prevent remounting the data partition the next time it is accessed + int retSetcheckpoint = psMgr.SetCheckpoint(0); + if (retSetcheckpoint != 0) { + return SET_OTA_STATUS_FAILED; + } + return BOOT_SUCCESS_IN_OTA; + // In the non-ota stage, startup is complete + } else if (status == 1 && checkpoint == 0) { + return BOOT_SUCCESS_IN_NORMAL; + // In the non-ota stage, startup is failed + } else if (status == 0 && checkpoint == 0) { + return BOOT_FAILD_IN_NORMAL; + } + return SET_OTA_STATUS_FAILED; +} +#endif \ No newline at end of file diff --git a/interfaces/kits/slot_info/slot_info.cpp b/interfaces/kits/slot_info/slot_info.cpp index e6f2787d..79d6cfb7 100644 --- a/interfaces/kits/slot_info/slot_info.cpp +++ b/interfaces/kits/slot_info/slot_info.cpp @@ -33,10 +33,13 @@ void GetActivePartitionSuffix(std::string &suffix) void SetActiveSlot() { } +void SetOTACheckpoint(int32_t checkpoint) +{ +} #else void GetPartitionSuffix(std::string &suffix) { - OHOS::HDI::Partitionslot::V1_0::PartitionSlotManager psMgr; + OHOS::HDI::Partitionslot::V1_1::PartitionSlotManager psMgr; int32_t curSlot = -1; int32_t numOfSlots = 0; int32_t ret = psMgr.GetCurrentSlot(curSlot, numOfSlots); @@ -56,7 +59,7 @@ void GetPartitionSuffix(std::string &suffix) void GetActivePartitionSuffix(std::string &suffix) { - OHOS::HDI::Partitionslot::V1_0::PartitionSlotManager psMgr; + OHOS::HDI::Partitionslot::V1_1::PartitionSlotManager psMgr; int32_t curSlot = -1; int32_t numOfSlots = 0; int32_t ret = psMgr.GetCurrentSlot(curSlot, numOfSlots); @@ -75,7 +78,7 @@ void GetActivePartitionSuffix(std::string &suffix) void SetActiveSlot() { - OHOS::HDI::Partitionslot::V1_0::PartitionSlotManager psMgr; + OHOS::HDI::Partitionslot::V1_1::PartitionSlotManager psMgr; int32_t curSlot = -1; int32_t numOfSlots = 0; int32_t ret = psMgr.GetCurrentSlot(curSlot, numOfSlots); @@ -90,5 +93,14 @@ void SetActiveSlot() LOG(ERROR) << "Set active slot error, slot: " << activeSlot; } } + +void SetOTACheckpoint(int32_t checkpoint) +{ + OHOS::HDI::Partitionslot::V1_1::PartitionSlotManager psMgr; + int32_t ret = psMgr.SetCheckpoint(checkpoint); + if (ret!= 0) { + LOG(ERROR) << "Set checkpoint error, checkpoint: " << checkpoint; + } +} #endif } // Updater diff --git a/services/updater_main.cpp b/services/updater_main.cpp index 13731085..93c75bc5 100644 --- a/services/updater_main.cpp +++ b/services/updater_main.cpp @@ -920,6 +920,7 @@ __attribute__((weak)) UpdaterStatus CheckAndSetSlot([[maybe_unused]]UpdaterParam LOG(INFO) << "not need check and set slot"; #ifndef UPDATER_UT SetActiveSlot(); // UPDATER_AB_SUPPORT + SetOTACheckpoint(1); // init rollback #endif return UPDATE_SUCCESS; } -- Gitee From 64540cb9735af3f915d2eb59cfc01df7e510dd36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=98=E7=BD=97=E5=AE=87?= Date: Fri, 20 Jun 2025 16:14:07 +0800 Subject: [PATCH 2/6] fix code check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 甘罗宇 --- OAT.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/OAT.xml b/OAT.xml index 4e1bc72d..d3374be4 100755 --- a/OAT.xml +++ b/OAT.xml @@ -59,6 +59,7 @@ Note:If the text contains special characters, please escape them according to th +