From e821a9700a1c1062602b459bdcfec1bcc6db7f18 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 19 Aug 2021 18:10:19 +0200 Subject: [PATCH] refactoring and bugfix --- bsp_q7s/boardtest/Q7STestTask.cpp | 2 +- bsp_q7s/core/CoreController.cpp | 167 +++++++++++++++++------------- bsp_q7s/core/CoreController.h | 3 + 3 files changed, 101 insertions(+), 71 deletions(-) diff --git a/bsp_q7s/boardtest/Q7STestTask.cpp b/bsp_q7s/boardtest/Q7STestTask.cpp index 85fdccaa..18baf124 100644 --- a/bsp_q7s/boardtest/Q7STestTask.cpp +++ b/bsp_q7s/boardtest/Q7STestTask.cpp @@ -25,7 +25,7 @@ ReturnValue_t Q7STestTask::performOneShotAction() { //testScratchApi(); //testJsonLibDirect(); //testDummyParams(); - testProtHandler(); + //testProtHandler(); //FsOpCodes opCode = FsOpCodes::ATTEMPT_DIR_REMOVAL_NON_EMPTY; //testFileSystemHandlerDirect(opCode); return TestTask::performOneShotAction(); diff --git a/bsp_q7s/core/CoreController.cpp b/bsp_q7s/core/CoreController.cpp index f81f1ff0..b899e60f 100644 --- a/bsp_q7s/core/CoreController.cpp +++ b/bsp_q7s/core/CoreController.cpp @@ -946,77 +946,10 @@ ReturnValue_t CoreController::setBootCopyProtection(Chip targetChip, Copy target } for(uint8_t arrIdx = 0; arrIdx < protArray.size(); arrIdx++) { - bool currentProt = protArray[arrIdx]; - std::ostringstream oss; - bool performOp = false; - if(protect == currentProt) { - continue; - } - if(protOperationPerformed) { - if((selfChip and selfCopy) or (not allCopies and not allChips)) { - // No need to continue, only one operation was requested - break; - } - } - Chip currentChip; - Copy currentCopy; - oss << "writeprotect "; - if(arrIdx == 0 or arrIdx == 1) { - oss << "0 "; - currentChip = Chip::CHIP_0; - } - else { - oss << "1 "; - currentChip = Chip::CHIP_1; - } - if(arrIdx == 0 or arrIdx == 2) { - oss << "0 "; - currentCopy = Copy::COPY_0; - } - else { - oss << "1 "; - currentCopy = Copy::COPY_1; - } - if(protect) { - oss << "1"; - } - else { - oss << "0"; - } - - int result = 0; - if(allChips and allCopies) { - performOp = true; - } - else if(allChips) { - if((selfCopy and CURRENT_COPY == targetCopy) or - (currentCopy == targetCopy)) { - performOp = true; - } - } - else if(allCopies) { - if((selfChip and CURRENT_COPY == targetCopy) or - (currentChip == targetChip)) { - performOp = true; - } - } - else if(selfChip and (currentChip == targetChip)) { - performOp = true; - } - else if(selfCopy and (currentCopy == targetCopy)) { - performOp = true; - } - else if((targetChip == currentChip) and (targetCopy == currentCopy)) { - performOp = true; - } + int result = handleBootCopyProtAtIndex(targetChip, targetCopy, protect, + protOperationPerformed, selfChip, selfCopy, allChips, allCopies, arrIdx); if(result != 0) { - utility::handleSystemError(result, "CoreController::checkAndSetBootCopyProtection"); - } - if(performOp) { - // TODO: Lock operation take a long time. Use command executor? - protOperationPerformed = true; - sif::info << "Executing command: " << oss.str() << std::endl; - result = std::system(oss.str().c_str()); + break; } } if(protOperationPerformed and updateProtFile) { @@ -1025,6 +958,100 @@ ReturnValue_t CoreController::setBootCopyProtection(Chip targetChip, Copy target return HasReturnvaluesIF::RETURN_OK; } +int CoreController::handleBootCopyProtAtIndex(Chip targetChip, Copy targetCopy, bool protect, + bool &protOperationPerformed, bool selfChip, bool selfCopy, bool allChips, + bool allCopies, uint8_t arrIdx) { + bool currentProt = protArray[arrIdx]; + std::ostringstream oss; + bool performOp = false; + if(protect == currentProt) { + return 0; + } + if(protOperationPerformed) { + if((selfChip and selfCopy) or (not allCopies and not allChips)) { + // No need to continue, only one operation was requested + return 1; + } + } + Chip currentChip; + Copy currentCopy; + oss << "writeprotect "; + if(arrIdx == 0 or arrIdx == 1) { + oss << "0 "; + currentChip = Chip::CHIP_0; + } + else { + oss << "1 "; + currentChip = Chip::CHIP_1; + } + if(arrIdx == 0 or arrIdx == 2) { + oss << "0 "; + currentCopy = Copy::COPY_0; + } + else { + oss << "1 "; + currentCopy = Copy::COPY_1; + } + if(protect) { + oss << "1"; + } + else { + oss << "0"; + } + + int result = 0; + if(allChips and allCopies) { + performOp = true; + } + else if(allChips) { + if((selfCopy and CURRENT_COPY == targetCopy) or + (currentCopy == targetCopy)) { + performOp = true; + } + } + else if(allCopies) { + if((selfChip and CURRENT_COPY == targetCopy) or + (currentChip == targetChip)) { + performOp = true; + } + } + else if(selfChip and (currentChip == targetChip)) { + if(selfCopy) { + if(currentCopy == targetCopy) { + performOp = true; + } + } + else { + performOp = true; + } + + } + else if(selfCopy and (currentCopy == targetCopy)) { + if(selfChip) { + if(currentChip == targetChip) { + performOp = true; + } + } + else { + performOp = true; + } + } + else if((targetChip == currentChip) and (targetCopy == currentCopy)) { + performOp = true; + } + if(result != 0) { + utility::handleSystemError(result, "CoreController::checkAndSetBootCopyProtection"); + } + if(performOp) { + // TODO: Lock operation take a long time. Use command executor? That would require a + // new state machine.. + protOperationPerformed = true; + sif::info << "Executing command: " << oss.str() << std::endl; + result = std::system(oss.str().c_str()); + } + return 0; +} + ReturnValue_t CoreController::updateProtInfo(bool regenerateChipStateFile) { using namespace std; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; diff --git a/bsp_q7s/core/CoreController.h b/bsp_q7s/core/CoreController.h index f527ade7..862162a0 100644 --- a/bsp_q7s/core/CoreController.h +++ b/bsp_q7s/core/CoreController.h @@ -176,6 +176,9 @@ private: void performWatchdogControlOperation(); ReturnValue_t handleProtInfoUpdateLine(std::string nextLine); + int handleBootCopyProtAtIndex(Chip targetChip, Copy targetCopy, bool protect, + bool &protOperationPerformed, bool selfChip, bool selfCopy, bool allChips, + bool allCopies, uint8_t arrIdx); }; #endif /* BSP_Q7S_CORE_CORECONTROLLER_H_ */