From 5324fcc0a677f1733c2366990bc078901f49c1a1 Mon Sep 17 00:00:00 2001 From: Lucjan Bryndza Date: Wed, 7 Jul 2021 09:05:08 +0200 Subject: [PATCH] [CP-312] Add reboot to update function Because update procedure it is splitted to the two stages downloading and update throught separate binary we need to pass ecoboot special flag to request reboot to updater bin after update procedure. This patch adds this functionality to the PureOS. Signed-off-by: Lucjan Bryndza --- module-bsp/board/linux/lpm/LinuxLPM.cpp | 5 ++--- module-bsp/board/linux/lpm/LinuxLPM.h | 2 +- module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp | 12 ++++++++++- module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp | 2 +- module-bsp/bsp/lpm/bsp_lpm.hpp | 14 ++++++++----- module-sys/SystemManager/PowerManager.cpp | 9 ++++++-- module-sys/SystemManager/PowerManager.hpp | 1 + module-sys/SystemManager/SystemManager.cpp | 21 +++++++++++++++---- module-sys/SystemManager/SystemManager.hpp | 14 +++++++++---- 9 files changed, 59 insertions(+), 21 deletions(-) diff --git a/module-bsp/board/linux/lpm/LinuxLPM.cpp b/module-bsp/board/linux/lpm/LinuxLPM.cpp index a4d86c6ee61f4d65dec9864cd4f58b22748449e6..daa8e215ebe61340b4f04683eb7cc1ecda92f487 100644 --- a/module-bsp/board/linux/lpm/LinuxLPM.cpp +++ b/module-bsp/board/linux/lpm/LinuxLPM.cpp @@ -15,7 +15,7 @@ namespace bsp return 0; } - int32_t LinuxLPM::Reboot() + int32_t LinuxLPM::Reboot(RebootType) { return 0; } @@ -31,7 +31,6 @@ namespace bsp } void LinuxLPM::SwitchOscillatorSource(bsp::LowPowerMode::OscillatorSource source) - { - } + {} } // namespace bsp diff --git a/module-bsp/board/linux/lpm/LinuxLPM.h b/module-bsp/board/linux/lpm/LinuxLPM.h index 339b21a42d5edaf50c24df83aad0dbac4f914c4a..512807298c4d2d5747278c0d224d79da785147e7 100644 --- a/module-bsp/board/linux/lpm/LinuxLPM.h +++ b/module-bsp/board/linux/lpm/LinuxLPM.h @@ -13,7 +13,7 @@ namespace bsp { public: int32_t PowerOff() override final; - int32_t Reboot() override final; + int32_t Reboot(RebootType reason) override final; void SetCpuFrequency(CpuFrequencyHz freq) final; [[nodiscard]] uint32_t GetCpuFrequency() const noexcept final; void SwitchOscillatorSource(OscillatorSource source) final; diff --git a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp index b998506bd3cf8b6dd6c3f1dfa9dceab6e1f30a60..74e4070f5361b6f1d74ad0683ade09686b3d7097 100644 --- a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp +++ b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.cpp @@ -39,8 +39,18 @@ namespace bsp return 0; } - int32_t RT1051LPM::Reboot() + int32_t RT1051LPM::Reboot(RebootType reason) { + constexpr uint32_t rebootToUpdaterCode{0xdeadbeaf}; + constexpr uint32_t rebootNormalCode{0}; + switch (reason) { + case RebootType::GoToUpdater: + SNVS->LPGPR[0] = rebootToUpdaterCode; + break; + case RebootType::NormalRestart: + SNVS->LPGPR[0] = rebootNormalCode; + break; + } NVIC_SystemReset(); return 0; } diff --git a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp index dfbaba8904336e15026b6cdf3e3b93d517f5de33..8a1eaefcbb1bf2a98538cfcfbb4bc6dd53bb910f 100644 --- a/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp +++ b/module-bsp/board/rt1051/bsp/lpm/RT1051LPM.hpp @@ -15,7 +15,7 @@ namespace bsp public: RT1051LPM(); int32_t PowerOff() override final; - int32_t Reboot() override final; + int32_t Reboot(RebootType reason) override final; void SetCpuFrequency(CpuFrequencyHz freq) final; [[nodiscard]] uint32_t GetCpuFrequency() const noexcept final; void SwitchOscillatorSource(OscillatorSource source) final; diff --git a/module-bsp/bsp/lpm/bsp_lpm.hpp b/module-bsp/bsp/lpm/bsp_lpm.hpp index 1b29a2b0c845fd1eebab636b17a51f70c2f2a246..388ca61745bba57ad4880f475de925330635ff6c 100644 --- a/module-bsp/bsp/lpm/bsp_lpm.hpp +++ b/module-bsp/bsp/lpm/bsp_lpm.hpp @@ -7,7 +7,8 @@ #include #include -namespace bsp { +namespace bsp +{ class LowPowerMode { @@ -17,14 +18,18 @@ namespace bsp { External, Internal }; + enum class RebootType { + NormalRestart, + GoToUpdater, + }; LowPowerMode() = default; virtual ~LowPowerMode() = default; static std::optional> Create(); - virtual int32_t PowerOff() = 0; - virtual int32_t Reboot() = 0; + virtual int32_t PowerOff() = 0; + virtual int32_t Reboot(RebootType reason) = 0; virtual void SetCpuFrequency(CpuFrequencyHz freq) = 0; [[nodiscard]] CpuFrequencyHz GetCurrentFrequencyLevel() const noexcept; @@ -32,8 +37,7 @@ namespace bsp { virtual void SwitchOscillatorSource(OscillatorSource source) = 0; - protected: + protected: CpuFrequencyHz currentFrequency = CpuFrequencyHz::Level_6; }; } // namespace bsp - diff --git a/module-sys/SystemManager/PowerManager.cpp b/module-sys/SystemManager/PowerManager.cpp index 465ac899c1e5979d55177af25eb20ddbf5bdac74..600b31ae5e983ec2b9513d4fe56b5edce915606a 100644 --- a/module-sys/SystemManager/PowerManager.cpp +++ b/module-sys/SystemManager/PowerManager.cpp @@ -24,7 +24,12 @@ namespace sys int32_t PowerManager::Reboot() { - return lowPowerControl->Reboot(); + return lowPowerControl->Reboot(bsp::LowPowerMode::RebootType::NormalRestart); + } + + int32_t PowerManager::RebootToUpdate() + { + return lowPowerControl->Reboot(bsp::LowPowerMode::RebootType::GoToUpdater); } void PowerManager::UpdateCpuFrequency(uint32_t cpuLoad) @@ -58,7 +63,7 @@ namespace sys void PowerManager::IncreaseCpuFrequency() const { - const auto freq = lowPowerControl->GetCurrentFrequencyLevel(); + const auto freq = lowPowerControl->GetCurrentFrequencyLevel(); if (freq == bsp::CpuFrequencyHz::Level_1) { // switch osc source first diff --git a/module-sys/SystemManager/PowerManager.hpp b/module-sys/SystemManager/PowerManager.hpp index 1e7b0e4794e5d8b26b8ed96f6487e23fc8825155..3f3fb65ab786335aa60e692ddde5569753308a03 100644 --- a/module-sys/SystemManager/PowerManager.hpp +++ b/module-sys/SystemManager/PowerManager.hpp @@ -26,6 +26,7 @@ namespace sys int32_t PowerOff(); int32_t Reboot(); + int32_t RebootToUpdate(); /// called periodically to calculate the CPU requirement /// diff --git a/module-sys/SystemManager/SystemManager.cpp b/module-sys/SystemManager/SystemManager.cpp index 9cf239723ad7158c8dc70159b460116bb07e0733..e957e9d9bb871d9316bc65b3cbdbbc17b198d85a 100644 --- a/module-sys/SystemManager/SystemManager.cpp +++ b/module-sys/SystemManager/SystemManager.cpp @@ -97,7 +97,7 @@ namespace sys : Service(service::name::system_manager, "", systemManagerStack), systemServiceCreators{std::move(creators)} { // Specify list of channels which System Manager is registered to - bus.channels = {BusChannel::SystemManagerRequests}; + bus.channels = {BusChannel::SystemManagerRequests}; lowBatteryShutdownDelay = sys::TimerFactory::createPeriodicTimer( this, "lowBatteryShutdownDelay", lowBatteryShutdownDelayTime, [this](sys::Timer &) { CloseSystemHandler(CloseReason::LowBattery); @@ -155,6 +155,10 @@ namespace sys LOG_INFO(" ---> SHUTDOWN <--- "); powerManager->PowerOff(); break; + case State::RebootToUpdate: + LOG_INFO(" ---> REBOOT TO UPDATER <--- "); + powerManager->RebootToUpdate(); + break; default: LOG_FATAL("State changed after reset/shutdown was requested to: %s! this is terrible failure!", c_str(state)); @@ -271,6 +275,12 @@ namespace sys return true; } + bool SystemManager::RebootToUpdate(Service *s) + { + s->bus.sendUnicast(std::make_shared(Code::RebootToUpdate), service::name::system_manager); + return true; + } + bool SystemManager::SuspendService(const std::string &name, sys::Service *caller) { auto ret = caller->bus.sendUnicastSync( @@ -501,7 +511,10 @@ namespace sys RestoreSystemHandler(); break; case Code::Reboot: - RebootHandler(); + RebootHandler(State::Reboot); + break; + case Code::RebootToUpdate: + RebootHandler(State::RebootToUpdate); break; case Code::None: break; @@ -711,10 +724,10 @@ namespace sys DestroyServices(sys::state::update::whitelist); } - void SystemManager::RebootHandler() + void SystemManager::RebootHandler(State state) { CloseSystemHandler(CloseReason::Reboot); - set(State::Reboot); + set(state); } void SystemManager::CpuStatisticsTimerHandler() diff --git a/module-sys/SystemManager/SystemManager.hpp b/module-sys/SystemManager/SystemManager.hpp index 800d9c65ecaa7749553db6f5d62862d93cc3173d..9ae8e50d1653c48c632dddf7dc62096ca0b2e885 100644 --- a/module-sys/SystemManager/SystemManager.hpp +++ b/module-sys/SystemManager/SystemManager.hpp @@ -40,8 +40,8 @@ namespace sys inline constexpr auto restoreTimeout{5000}; } // namespace constants - class PhoneModeRequest; // Forward declaration - class TetheringStateRequest; // Forward declaration + class PhoneModeRequest; // Forward declaration + class TetheringStateRequest; // Forward declaration class TetheringEnabledResponse; // Forward declaration enum class Code @@ -50,6 +50,7 @@ namespace sys Update, Restore, Reboot, + RebootToUpdate, None, }; @@ -81,7 +82,8 @@ namespace sys Suspend, Shutdown, ShutdownReady, - Reboot + Reboot, + RebootToUpdate } state = State::Running; explicit SystemManager(std::vector> &&creators); @@ -102,6 +104,8 @@ namespace sys static bool Reboot(Service *s); + static bool RebootToUpdate(Service *s); + static void storeOsVersion(Service *s, const std::string &updateOSVer, const std::string ¤tOSVer); static bool SuspendService(const std::string &name, Service *caller); @@ -176,7 +180,7 @@ namespace sys void RestoreSystemHandler(); - void RebootHandler(); + void RebootHandler(State state); /// loop to handle prior to full system close /// for now for rt1051 we need to @@ -234,6 +238,8 @@ inline const char *c_str(sys::SystemManager::State state) return "Shutdown"; case sys::SystemManager::State::Reboot: return "Reboot"; + case sys::SystemManager::State::RebootToUpdate: + return "RebootToUpdate"; case sys::SystemManager::State::ShutdownReady: return "ShutdownReady"; }