From a9096a46c0a14419a24c5c4cd35ac56cbcb5b175 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 25 Oct 2022 11:04:24 +0200 Subject: [PATCH 1/5] safety mechanism on reboots --- bsp_q7s/core/CoreController.cpp | 4 ++++ bsp_q7s/fs/SdCardManager.cpp | 12 ++++++++++++ bsp_q7s/fs/SdCardManager.h | 3 +++ 3 files changed, 19 insertions(+) diff --git a/bsp_q7s/core/CoreController.cpp b/bsp_q7s/core/CoreController.cpp index 95700148..6b3c08a9 100644 --- a/bsp_q7s/core/CoreController.cpp +++ b/bsp_q7s/core/CoreController.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "OBSWVersion.h" #include "fsfw/serviceinterface/ServiceInterface.h" @@ -932,6 +933,9 @@ ReturnValue_t CoreController::actionReboot(const uint8_t *data, size_t size) { ReturnValue_t CoreController::gracefulShutdownTasks(xsc::Chip chip, xsc::Copy copy, bool &protOpPerformed) { sdcMan->setBlocking(true); + sdcMan->markUnusable(); + // Wait two seconds to ensure no one uses the SD cards + TaskFactory::delayTask(2000); // Attempt graceful shutdown by unmounting and switching off SD cards sdcMan->switchOffSdCard(sd::SdCard::SLOT_0); sdcMan->switchOffSdCard(sd::SdCard::SLOT_1); diff --git a/bsp_q7s/fs/SdCardManager.cpp b/bsp_q7s/fs/SdCardManager.cpp index 42182829..4a6c12a1 100644 --- a/bsp_q7s/fs/SdCardManager.cpp +++ b/bsp_q7s/fs/SdCardManager.cpp @@ -460,6 +460,13 @@ void SdCardManager::setBlocking(bool blocking) { this->blocking = blocking; } void SdCardManager::setPrintCommandOutput(bool print) { this->printCmdOutput = print; } bool SdCardManager::isSdCardUsable(sd::SdCard sdCard) { + { + MutexGuard mg(mutex); + if (markedUnusable) { + return false; + } + } + SdCardManager::SdStatePair active; ReturnValue_t result = this->getSdCardsStatus(active); @@ -562,3 +569,8 @@ std::optional SdCardManager::getActiveSdCard() const { MutexGuard mg(mutex); return sdInfo.active; } + +void SdCardManager::markUnusable() { + MutexGuard mg(mutex); + markedUnusable = true; +} diff --git a/bsp_q7s/fs/SdCardManager.h b/bsp_q7s/fs/SdCardManager.h index 52c8ed9b..8ce13dbc 100644 --- a/bsp_q7s/fs/SdCardManager.h +++ b/bsp_q7s/fs/SdCardManager.h @@ -214,12 +214,15 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { ReturnValue_t performFsck(sd::SdCard sdcard, bool printOutput, int& linuxError); + void markUnusable(); + private: CommandExecutor cmdExecutor; Operations currentOp = Operations::IDLE; bool blocking = false; bool sdCardActive = true; bool printCmdOutput = true; + bool markedUnusable = false; MutexIF* mutex = nullptr; SdCardManager(); From f198a401f27cd1fce86ae1ee1adb0374105be3c5 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 25 Oct 2022 11:22:31 +0200 Subject: [PATCH 2/5] this should work as well --- bsp_q7s/fs/SdCardManager.cpp | 18 ++++++++++++++---- bsp_q7s/fs/SdCardManager.h | 2 +- mission/devices/ScexDeviceHandler.cpp | 9 +++++++++ mission/memory/SdCardMountedIF.h | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/bsp_q7s/fs/SdCardManager.cpp b/bsp_q7s/fs/SdCardManager.cpp index 4a6c12a1..8cea0991 100644 --- a/bsp_q7s/fs/SdCardManager.cpp +++ b/bsp_q7s/fs/SdCardManager.cpp @@ -459,7 +459,7 @@ void SdCardManager::setBlocking(bool blocking) { this->blocking = blocking; } void SdCardManager::setPrintCommandOutput(bool print) { this->printCmdOutput = print; } -bool SdCardManager::isSdCardUsable(sd::SdCard sdCard) { +bool SdCardManager::isSdCardUsable(std::optional sdCard) { { MutexGuard mg(mutex); if (markedUnusable) { @@ -474,20 +474,27 @@ bool SdCardManager::isSdCardUsable(sd::SdCard sdCard) { sif::debug << "SdCardManager::isSdCardMounted: Failed to get SD card active state"; return false; } + if (not sdCard) { + sdCard == sd::BOTH; + } if (sdCard == sd::SLOT_0) { if (active.first == sd::MOUNTED) { return true; } else { return false; } - } else if (sdCard == sd::SLOT_1) { + } + if (sdCard == sd::SLOT_1) { if (active.second == sd::MOUNTED) { return true; } else { return false; } - } else { - sif::debug << "SdCardManager::isSdCardMounted: Unknown SD card specified" << std::endl; + } + if (sdCard == sd::BOTH) { + if (active.first == sd::MOUNTED && active.second == sd::MOUNTED) { + return true; + } } return false; } @@ -567,6 +574,9 @@ void SdCardManager::setActiveSdCard(sd::SdCard sdCard) { std::optional SdCardManager::getActiveSdCard() const { MutexGuard mg(mutex); + if(markedUnusable) { + return std::nullopt; + } return sdInfo.active; } diff --git a/bsp_q7s/fs/SdCardManager.h b/bsp_q7s/fs/SdCardManager.h index 8ce13dbc..749cae62 100644 --- a/bsp_q7s/fs/SdCardManager.h +++ b/bsp_q7s/fs/SdCardManager.h @@ -206,7 +206,7 @@ class SdCardManager : public SystemObject, public SdCardMountedIF { * * @return true if mounted, otherwise false */ - bool isSdCardUsable(sd::SdCard sdCard) override; + bool isSdCardUsable(std::optional sdCard) override; ReturnValue_t isSdCardMountedReadOnly(sd::SdCard sdcard, bool& readOnly); diff --git a/mission/devices/ScexDeviceHandler.cpp b/mission/devices/ScexDeviceHandler.cpp index 1cc2cd39..db562f66 100644 --- a/mission/devices/ScexDeviceHandler.cpp +++ b/mission/devices/ScexDeviceHandler.cpp @@ -1,5 +1,6 @@ #include "ScexDeviceHandler.h" +#include #include #include @@ -200,6 +201,10 @@ ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, cons ReturnValue_t status = OK; auto oneFileHandler = [&](std::string cmdName) { + auto activeSd = sdcMan.getActiveSdCard(); + if (not activeSd) { + return HasFileSystemIF::GENERIC_FILE_ERROR; + } fileId = date_time_string(); std::ostringstream oss; auto prefix = sdcMan.getCurrentMountPrefix(); @@ -216,6 +221,10 @@ ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, cons }; auto multiFileHandler = [&](std::string cmdName) { if ((helper.getPacketCounter() == 1) or (not fileNameSet)) { + auto activeSd = sdcMan.getActiveSdCard(); + if (not activeSd) { + return HasFileSystemIF::GENERIC_FILE_ERROR; + } fileId = date_time_string(); std::ostringstream oss; auto prefix = sdcMan.getCurrentMountPrefix(); diff --git a/mission/memory/SdCardMountedIF.h b/mission/memory/SdCardMountedIF.h index d88bb57a..ac705e8d 100644 --- a/mission/memory/SdCardMountedIF.h +++ b/mission/memory/SdCardMountedIF.h @@ -10,7 +10,7 @@ class SdCardMountedIF { public: virtual ~SdCardMountedIF(){}; virtual const std::string& getCurrentMountPrefix() const = 0; - virtual bool isSdCardUsable(sd::SdCard sdCard) = 0; + virtual bool isSdCardUsable(std::optional sdCard) = 0; virtual std::optional getPreferredSdCard() const = 0; virtual void setActiveSdCard(sd::SdCard sdCard) = 0; virtual std::optional getActiveSdCard() const = 0; From b1e8be2ba5a25a0518998e44d9f8e79f5d1dc5db Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 25 Oct 2022 11:31:06 +0200 Subject: [PATCH 3/5] checked FS usage --- bsp_q7s/fs/SdCardManager.cpp | 2 +- fsfw | 2 +- linux/devices/ploc/PlocSupervisorHandler.cpp | 5 +++++ linux/devices/ploc/PlocSupvHelper.cpp | 6 +++++ linux/devices/startracker/StrHelper.cpp | 22 +++++++++++++++++++ mission/devices/ScexDeviceHandler.cpp | 4 ++-- .../devices/SolarArrayDeploymentHandler.cpp | 1 + 7 files changed, 38 insertions(+), 4 deletions(-) diff --git a/bsp_q7s/fs/SdCardManager.cpp b/bsp_q7s/fs/SdCardManager.cpp index 8cea0991..e547a6d0 100644 --- a/bsp_q7s/fs/SdCardManager.cpp +++ b/bsp_q7s/fs/SdCardManager.cpp @@ -574,7 +574,7 @@ void SdCardManager::setActiveSdCard(sd::SdCard sdCard) { std::optional SdCardManager::getActiveSdCard() const { MutexGuard mg(mutex); - if(markedUnusable) { + if (markedUnusable) { return std::nullopt; } return sdInfo.active; diff --git a/fsfw b/fsfw index b0c5a49b..1f05e6b2 160000 --- a/fsfw +++ b/fsfw @@ -1 +1 @@ -Subproject commit b0c5a49b504708ec9130228100d7bbd49025598d +Subproject commit 1f05e6b297af8a6d310394e959c4d0cf13632831 diff --git a/linux/devices/ploc/PlocSupervisorHandler.cpp b/linux/devices/ploc/PlocSupervisorHandler.cpp index f36c5a8d..dcdff2c3 100644 --- a/linux/devices/ploc/PlocSupervisorHandler.cpp +++ b/linux/devices/ploc/PlocSupervisorHandler.cpp @@ -1917,6 +1917,11 @@ ReturnValue_t PlocSupervisorHandler::checkMramPacketApid() { } ReturnValue_t PlocSupervisorHandler::handleMramDumpFile(DeviceCommandId_t id) { +#ifdef XIPHOS_Q7S + if (not sdcMan->getActiveSdCard()) { + return HasFileSystemIF::FILESYSTEM_INACTIVE; + } +#endif ReturnValue_t result = returnvalue::OK; uint16_t packetLen = readSpacePacketLength(spacePacketBuffer); uint8_t sequenceFlags = readSequenceFlags(spacePacketBuffer); diff --git a/linux/devices/ploc/PlocSupvHelper.cpp b/linux/devices/ploc/PlocSupvHelper.cpp index f0f94635..186eef6b 100644 --- a/linux/devices/ploc/PlocSupvHelper.cpp +++ b/linux/devices/ploc/PlocSupvHelper.cpp @@ -1,6 +1,7 @@ #include "PlocSupvHelper.h" #include +#include #include #include @@ -748,6 +749,11 @@ uint32_t PlocSupvHelper::getFileSize(std::string filename) { ReturnValue_t PlocSupvHelper::handleEventBufferReception(ploc::SpTmReader& reader) { ReturnValue_t result = returnvalue::OK; +#ifdef XIPHOS_Q7S + if (not sdcMan->getActiveSdCard()) { + return HasFileSystemIF::FILESYSTEM_INACTIVE; + } +#endif std::string filename = Filenaming::generateAbsoluteFilename( eventBufferReq.path, eventBufferReq.filename, timestamping); std::ofstream file(filename, std::ios_base::app | std::ios_base::out); diff --git a/linux/devices/startracker/StrHelper.cpp b/linux/devices/startracker/StrHelper.cpp index 685cf3b3..1f6472bb 100644 --- a/linux/devices/startracker/StrHelper.cpp +++ b/linux/devices/startracker/StrHelper.cpp @@ -1,5 +1,7 @@ #include "StrHelper.h" +#include + #include #include @@ -176,6 +178,11 @@ void StrHelper::disableTimestamping() { timestamping = false; } void StrHelper::enableTimestamping() { timestamping = true; } ReturnValue_t StrHelper::performImageDownload() { +#ifdef XIPHOS_Q7S + if (not sdcMan->getActiveSdCard()) { + return HasFileSystemIF::FILESYSTEM_INACTIVE; + } +#endif ReturnValue_t result; #if OBSW_DEBUG_STARTRACKER == 1 ProgressPrinter progressPrinter("Image download", ImageDownload::LAST_POSITION); @@ -244,6 +251,11 @@ ReturnValue_t StrHelper::performImageUpload() { uint32_t imageSize = 0; struct UploadActionRequest uploadReq; uploadReq.position = 0; +#ifdef XIPHOS_Q7S + if (not sdcMan->getActiveSdCard()) { + return HasFileSystemIF::FILESYSTEM_INACTIVE; + } +#endif std::memset(&uploadReq.data, 0, sizeof(uploadReq.data)); if (not std::filesystem::exists(uploadImage.uploadFile)) { triggerEvent(STR_HELPER_FILE_NOT_EXISTS, static_cast(internalState)); @@ -315,6 +327,11 @@ ReturnValue_t StrHelper::performFirmwareUpdate() { } ReturnValue_t StrHelper::performFlashWrite() { +#ifdef XIPHOS_Q7S + if (not sdcMan->getActiveSdCard()) { + return HasFileSystemIF::FILESYSTEM_INACTIVE; + } +#endif ReturnValue_t result = returnvalue::OK; uint32_t size = 0; uint32_t bytesWritten = 0; @@ -394,6 +411,11 @@ ReturnValue_t StrHelper::performFlashWrite() { } ReturnValue_t StrHelper::performFlashRead() { +#ifdef XIPHOS_Q7S + if (not sdcMan->getActiveSdCard()) { + return HasFileSystemIF::FILESYSTEM_INACTIVE; + } +#endif ReturnValue_t result; #if OBSW_DEBUG_STARTRACKER == 1 ProgressPrinter progressPrinter("Flash read", flashRead.size); diff --git a/mission/devices/ScexDeviceHandler.cpp b/mission/devices/ScexDeviceHandler.cpp index db562f66..8819cd95 100644 --- a/mission/devices/ScexDeviceHandler.cpp +++ b/mission/devices/ScexDeviceHandler.cpp @@ -203,7 +203,7 @@ ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, cons auto oneFileHandler = [&](std::string cmdName) { auto activeSd = sdcMan.getActiveSdCard(); if (not activeSd) { - return HasFileSystemIF::GENERIC_FILE_ERROR; + return HasFileSystemIF::FILESYSTEM_INACTIVE; } fileId = date_time_string(); std::ostringstream oss; @@ -223,7 +223,7 @@ ReturnValue_t ScexDeviceHandler::interpretDeviceReply(DeviceCommandId_t id, cons if ((helper.getPacketCounter() == 1) or (not fileNameSet)) { auto activeSd = sdcMan.getActiveSdCard(); if (not activeSd) { - return HasFileSystemIF::GENERIC_FILE_ERROR; + return HasFileSystemIF::FILESYSTEM_INACTIVE; } fileId = date_time_string(); std::ostringstream oss; diff --git a/mission/devices/SolarArrayDeploymentHandler.cpp b/mission/devices/SolarArrayDeploymentHandler.cpp index affc0e2e..6a29ecd5 100644 --- a/mission/devices/SolarArrayDeploymentHandler.cpp +++ b/mission/devices/SolarArrayDeploymentHandler.cpp @@ -1,5 +1,6 @@ #include "SolarArrayDeploymentHandler.h" +#include #include #include From 0135bebc70f8875d4cafa024b276b385188e27f0 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 25 Oct 2022 11:33:21 +0200 Subject: [PATCH 4/5] impl bugfix --- bsp_q7s/fs/SdCardManager.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bsp_q7s/fs/SdCardManager.cpp b/bsp_q7s/fs/SdCardManager.cpp index e547a6d0..0207cfbb 100644 --- a/bsp_q7s/fs/SdCardManager.cpp +++ b/bsp_q7s/fs/SdCardManager.cpp @@ -475,7 +475,9 @@ bool SdCardManager::isSdCardUsable(std::optional sdCard) { return false; } if (not sdCard) { - sdCard == sd::BOTH; + if (active.first == sd::MOUNTED or active.second == sd::MOUNTED) { + return true; + } } if (sdCard == sd::SLOT_0) { if (active.first == sd::MOUNTED) { From 4af8c85a8413fcbed0d8427407009efc5f855036 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Tue, 25 Oct 2022 11:39:57 +0200 Subject: [PATCH 5/5] small fix --- bsp_q7s/fs/SdCardManager.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/bsp_q7s/fs/SdCardManager.cpp b/bsp_q7s/fs/SdCardManager.cpp index 0207cfbb..9d5fcd6e 100644 --- a/bsp_q7s/fs/SdCardManager.cpp +++ b/bsp_q7s/fs/SdCardManager.cpp @@ -478,6 +478,7 @@ bool SdCardManager::isSdCardUsable(std::optional sdCard) { if (active.first == sd::MOUNTED or active.second == sd::MOUNTED) { return true; } + return false; } if (sdCard == sd::SLOT_0) { if (active.first == sd::MOUNTED) {