From 00eb5a2f1efdf8dd74bdc6fa87ea516bb03c855e Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 23 Feb 2022 17:36:29 +0100 Subject: [PATCH] continued reboot handler --- bsp_q7s/boardtest/Q7STestTask.cpp | 20 +- bsp_q7s/core/CoreController.cpp | 478 ++++++++++++++++++------------ bsp_q7s/core/CoreController.h | 55 ++-- 3 files changed, 339 insertions(+), 214 deletions(-) diff --git a/bsp_q7s/boardtest/Q7STestTask.cpp b/bsp_q7s/boardtest/Q7STestTask.cpp index 5d9042ac..bd9890a7 100644 --- a/bsp_q7s/boardtest/Q7STestTask.cpp +++ b/bsp_q7s/boardtest/Q7STestTask.cpp @@ -170,15 +170,15 @@ void Q7STestTask::testProtHandler() { bool opPerformed = false; ReturnValue_t result = HasReturnvaluesIF::RETURN_OK; // If any chips are unlocked, lock them here - result = coreController->setBootCopyProtection( - CoreController::Chip::ALL_CHIP, CoreController::Copy::ALL_COPY, true, opPerformed, true); + result = coreController->setBootCopyProtection(xsc::Chip::ALL_CHIP, xsc::Copy::ALL_COPY, true, + opPerformed, true); if (result != HasReturnvaluesIF::RETURN_OK) { sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl; } // unlock own copy - result = coreController->setBootCopyProtection( - CoreController::Chip::SELF_CHIP, CoreController::Copy::SELF_COPY, false, opPerformed, true); + result = coreController->setBootCopyProtection(xsc::Chip::SELF_CHIP, xsc::Copy::SELF_COPY, false, + opPerformed, true); if (result != HasReturnvaluesIF::RETURN_OK) { sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl; } @@ -191,8 +191,8 @@ void Q7STestTask::testProtHandler() { } // lock own copy - result = coreController->setBootCopyProtection( - CoreController::Chip::SELF_CHIP, CoreController::Copy::SELF_COPY, true, opPerformed, true); + result = coreController->setBootCopyProtection(xsc::Chip::SELF_CHIP, xsc::Copy::SELF_COPY, true, + opPerformed, true); if (result != HasReturnvaluesIF::RETURN_OK) { sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl; } @@ -205,8 +205,8 @@ void Q7STestTask::testProtHandler() { } // unlock specific copy - result = coreController->setBootCopyProtection( - CoreController::Chip::CHIP_1, CoreController::Copy::COPY_1, false, opPerformed, true); + result = coreController->setBootCopyProtection(xsc::Chip::CHIP_1, xsc::Copy::COPY_1, false, + opPerformed, true); if (result != HasReturnvaluesIF::RETURN_OK) { sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl; } @@ -219,8 +219,8 @@ void Q7STestTask::testProtHandler() { } // lock specific copy - result = coreController->setBootCopyProtection( - CoreController::Chip::CHIP_1, CoreController::Copy::COPY_1, true, opPerformed, true); + result = coreController->setBootCopyProtection(xsc::Chip::CHIP_1, xsc::Copy::COPY_1, true, + opPerformed, true); if (result != HasReturnvaluesIF::RETURN_OK) { sif::warning << "Q7STestTask::testProtHandler: Op failed" << std::endl; } diff --git a/bsp_q7s/core/CoreController.cpp b/bsp_q7s/core/CoreController.cpp index f60f3cc3..e6e8bca3 100644 --- a/bsp_q7s/core/CoreController.cpp +++ b/bsp_q7s/core/CoreController.cpp @@ -22,8 +22,8 @@ #include "bsp_q7s/memory/SdCardManager.h" #include "bsp_q7s/memory/scratchApi.h" -CoreController::Chip CoreController::CURRENT_CHIP = Chip::NO_CHIP; -CoreController::Copy CoreController::CURRENT_COPY = Copy::NO_COPY; +xsc::Chip CoreController::CURRENT_CHIP = xsc::Chip::NO_CHIP; +xsc::Copy CoreController::CURRENT_COPY = xsc::Copy::NO_COPY; CoreController::CoreController(object_id_t objectId) : ExtendedControllerBase(objectId, objects::NO_OBJECT, 5), opDivider(5) { @@ -700,22 +700,18 @@ ReturnValue_t CoreController::initBootCopy() { utility::handleSystemError(result, "CoreController::initBootCopy"); } } - std::ifstream file(CURR_COPY_FILE); - std::string line; - std::getline(file, line); - std::istringstream iss(line); - int value = 0; - iss >> value; - CURRENT_CHIP = static_cast(value); - iss >> value; - CURRENT_COPY = static_cast(value); + + getCurrentBootCopy(CURRENT_CHIP, CURRENT_COPY); return HasReturnvaluesIF::RETURN_OK; } -void CoreController::getCurrentBootCopy(Chip &chip, Copy ©) { +void CoreController::getCurrentBootCopy(xsc::Chip &chip, xsc::Copy ©) { + xsc_libnor_chip_t xscChip; + xsc_libnor_copy_t xscCopy; + xsc_boot_get_chip_copy(&xscChip, &xscCopy); // Not really thread-safe but it does not need to be - chip = CURRENT_CHIP; - copy = CURRENT_COPY; + chip = static_cast(xscChip); + copy = static_cast(xscCopy); } ReturnValue_t CoreController::initWatchdogFifo() { @@ -762,8 +758,8 @@ ReturnValue_t CoreController::actionPerformReboot(const uint8_t *data, size_t si SdCardManager::instance()->switchOffSdCard(sd::SdCard::SLOT_0); SdCardManager::instance()->switchOffSdCard(sd::SdCard::SLOT_1); // If any boot copies are unprotected - ReturnValue_t retval = - setBootCopyProtection(Chip::SELF_CHIP, Copy::SELF_COPY, true, protOpPerformed, false); + ReturnValue_t retval = setBootCopyProtection(xsc::Chip::SELF_CHIP, xsc::Copy::SELF_COPY, true, + protOpPerformed, false); if (retval == HasReturnvaluesIF::RETURN_OK and protOpPerformed) { sif::info << "Running slot was writeprotected before reboot" << std::endl; } @@ -785,23 +781,24 @@ ReturnValue_t CoreController::actionPerformReboot(const uint8_t *data, size_t si // Check that the target chip and copy is writeprotected first generateChipStateFile(); // If any boot copies are unprotected, protect them here - auto tgtChip = static_cast(data[1]); - auto tgtCopy = static_cast(data[2]); + auto tgtChip = static_cast(data[1]); + auto tgtCopy = static_cast(data[2]); - ReturnValue_t retval = setBootCopyProtection( - static_cast(data[1]), static_cast(data[2]), true, protOpPerformed, false); + ReturnValue_t retval = + setBootCopyProtection(static_cast(data[1]), static_cast(data[2]), true, + protOpPerformed, false); if (retval == HasReturnvaluesIF::RETURN_OK and protOpPerformed) { sif::info << "Target slot was writeprotected before reboot" << std::endl; } switch (tgtChip) { - case (Chip::CHIP_0): { + case (xsc::Chip::CHIP_0): { switch (tgtCopy) { - case (Copy::COPY_0): { + case (xsc::Copy::COPY_0): { xsc_boot_copy(XSC_LIBNOR_CHIP_0, XSC_LIBNOR_COPY_NOMINAL); break; } - case (Copy::COPY_1): { + case (xsc::Copy::COPY_1): { xsc_boot_copy(XSC_LIBNOR_CHIP_0, XSC_LIBNOR_COPY_GOLD); break; } @@ -811,13 +808,13 @@ ReturnValue_t CoreController::actionPerformReboot(const uint8_t *data, size_t si } break; } - case (Chip::CHIP_1): { + case (xsc::Chip::CHIP_1): { switch (tgtCopy) { - case (Copy::COPY_0): { + case (xsc::Copy::COPY_0): { xsc_boot_copy(XSC_LIBNOR_CHIP_1, XSC_LIBNOR_COPY_NOMINAL); break; } - case (Copy::COPY_1): { + case (xsc::Copy::COPY_1): { xsc_boot_copy(XSC_LIBNOR_CHIP_1, XSC_LIBNOR_COPY_GOLD); break; } @@ -884,8 +881,8 @@ ReturnValue_t CoreController::generateChipStateFile() { return HasReturnvaluesIF::RETURN_OK; } -ReturnValue_t CoreController::setBootCopyProtection(Chip targetChip, Copy targetCopy, bool protect, - bool &protOperationPerformed, +ReturnValue_t CoreController::setBootCopyProtection(xsc::Chip targetChip, xsc::Copy targetCopy, + bool protect, bool &protOperationPerformed, bool updateProtFile) { bool allChips = false; bool allCopies = false; @@ -894,14 +891,14 @@ ReturnValue_t CoreController::setBootCopyProtection(Chip targetChip, Copy target protOperationPerformed = false; switch (targetChip) { - case (Chip::ALL_CHIP): { + case (xsc::Chip::ALL_CHIP): { allChips = true; break; } - case (Chip::NO_CHIP): { + case (xsc::Chip::NO_CHIP): { return HasReturnvaluesIF::RETURN_OK; } - case (Chip::SELF_CHIP): { + case (xsc::Chip::SELF_CHIP): { selfChip = true; targetChip = CURRENT_CHIP; break; @@ -911,14 +908,14 @@ ReturnValue_t CoreController::setBootCopyProtection(Chip targetChip, Copy target } } switch (targetCopy) { - case (Copy::ALL_COPY): { + case (xsc::Copy::ALL_COPY): { allCopies = true; break; } - case (Copy::NO_COPY): { + case (xsc::Copy::NO_COPY): { return HasReturnvaluesIF::RETURN_OK; } - case (Copy::SELF_COPY): { + case (xsc::Copy::SELF_COPY): { selfCopy = true; targetCopy = CURRENT_COPY; break; @@ -941,10 +938,10 @@ 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) { +int CoreController::handleBootCopyProtAtIndex(xsc::Chip targetChip, xsc::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; @@ -957,22 +954,22 @@ int CoreController::handleBootCopyProtAtIndex(Chip targetChip, Copy targetCopy, return 1; } } - Chip currentChip; - Copy currentCopy; + xsc::Chip currentChip; + xsc::Copy currentCopy; oss << "writeprotect "; if (arrIdx == 0 or arrIdx == 1) { oss << "0 "; - currentChip = Chip::CHIP_0; + currentChip = xsc::Chip::CHIP_0; } else { oss << "1 "; - currentChip = Chip::CHIP_1; + currentChip = xsc::Chip::CHIP_1; } if (arrIdx == 0 or arrIdx == 2) { oss << "0 "; - currentCopy = Copy::COPY_0; + currentCopy = xsc::Copy::COPY_0; } else { oss << "1 "; - currentCopy = Copy::COPY_1; + currentCopy = xsc::Copy::COPY_1; } if (protect) { oss << "1"; @@ -1067,29 +1064,29 @@ ReturnValue_t CoreController::handleProtInfoUpdateLine(std::string nextLine) { uint8_t wordIdx = 0; uint8_t arrayIdx = 0; istringstream iss(nextLine); - Chip currentChip = Chip::CHIP_0; - Copy currentCopy = Copy::COPY_0; + xsc::Chip currentChip = xsc::Chip::CHIP_0; + xsc::Copy currentCopy = xsc::Copy::COPY_0; while (iss >> word) { if (wordIdx == 1) { - currentChip = static_cast(stoi(word)); + currentChip = static_cast(stoi(word)); } if (wordIdx == 3) { - currentCopy = static_cast(stoi(word)); + currentCopy = static_cast(stoi(word)); } if (wordIdx == 3) { - if (currentChip == Chip::CHIP_0) { - if (currentCopy == Copy::COPY_0) { + if (currentChip == xsc::Chip::CHIP_0) { + if (currentCopy == xsc::Copy::COPY_0) { arrayIdx = 0; - } else if (currentCopy == Copy::COPY_1) { + } else if (currentCopy == xsc::Copy::COPY_1) { arrayIdx = 1; } } - else if (currentChip == Chip::CHIP_1) { - if (currentCopy == Copy::COPY_0) { + else if (currentChip == xsc::Chip::CHIP_1) { + if (currentCopy == xsc::Copy::COPY_0) { arrayIdx = 2; - } else if (currentCopy == Copy::COPY_1) { + } else if (currentCopy == xsc::Copy::COPY_1) { arrayIdx = 3; } } @@ -1150,132 +1147,115 @@ void CoreController::performRebootFileHandling(bool recreateFile) { } std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE; if (not std::filesystem::exists(path) or recreateFile) { - std::ofstream rebootFile(path); - if (rebootFile.is_open()) { - // Initiate reboot file first. Reboot handling will be on on initialization - rebootFile << "on: 1\nmaxcnt: " << static_cast(DEFAULT_MAX_BOOT_CNT) - << "\nimg00: 0\nimg01: 0\nimg10: 0\nimg11: 0\n"; - } - } - xsc_libnor_chip_t chip; - xsc_libnor_copy_t copy; - xsc_boot_get_chip_copy(&chip, ©); - std::string selfMatch; - if (chip == XSC_LIBNOR_CHIP_0) { - if (copy == XSC_LIBNOR_COPY_NOMINAL) { - selfMatch = "00"; - } else { - selfMatch = "01"; - } + rebootFile.enabled = true; + rebootFile.img00Cnt = 0; + rebootFile.img01Cnt = 0; + rebootFile.img10Cnt = 0; + rebootFile.img11Cnt = 0; + rebootFile.lastChip = xsc::Chip::CHIP_0; + rebootFile.lastCopy = xsc::Copy::COPY_0; + rebootFile.bootFlag = false; + rewriteRebootFile(rebootFile); } else { - if (copy == XSC_LIBNOR_COPY_NOMINAL) { - selfMatch = "10"; - } else { - selfMatch = "11"; + if (not parseRebootFile(path, rebootFile)) { + performRebootFileHandling(true); } } - uint8_t relevantBootCnt = 0; - bool on = true; - size_t maxCnt = DEFAULT_MAX_BOOT_CNT; - uint8_t bootCnt00 = 0; - uint8_t bootCnt01 = 0; - uint8_t bootCnt10 = 0; - uint8_t bootCnt11 = 0; - ifstream file(path); - string word; - string line; - uint8_t lineIdx = 0; - while (std::getline(file, line)) { - istringstream iss(line); - switch (lineIdx) { - case 0: { - iss >> word; - if (word.find("on:") == string::npos) { - // invalid file - performRebootFileHandling(true); - return; - } - iss >> on; - break; - } - case 1: { - iss >> word; - if (word.find("maxcnt:") == string::npos) { - performRebootFileHandling(true); - return; - } - iss >> maxCnt; - break; - } - case 2: { - iss >> word; - if (word.find("img00:") == string::npos) { - performRebootFileHandling(true); - return; - } - iss >> bootCnt00; - if (word.find(selfMatch) != string::npos) { - relevantBootCnt = bootCnt00; - } - break; - } - case 3: { - iss >> word; - if (word.find("img01:") == string::npos) { - performRebootFileHandling(true); - return; - } - iss >> bootCnt01; - if (word.find(selfMatch) != string::npos) { - relevantBootCnt = bootCnt01; - } - break; - } - case 4: { - iss >> word; - if (word.find("img10:") == string::npos) { - performRebootFileHandling(true); - return; - } - iss >> bootCnt10; - if (word.find(selfMatch) != string::npos) { - relevantBootCnt = bootCnt10; - } - break; - } - case 5: { - iss >> word; - if (word.find("img11:") == string::npos) { - performRebootFileHandling(true); - return; - } - iss >> bootCnt11; - if (word.find(selfMatch) != string::npos) { - relevantBootCnt = bootCnt11; - } - break; - } - } - lineIdx++; + if (rebootFile.bootFlag) { + // Trigger event to inform ground that a reboot was triggered } - if (relevantBootCnt > maxCnt) { + + if (rebootFile.relevantBootCnt > rebootFile.maxCount) { // Reboot to other image - determineAndExecuteReboot(copy, chip, bootCnt00, bootCnt01, bootCnt10, bootCnt11, maxCnt); + bool doReboot = false; + xsc_libnor_chip_t tgtChip; + xsc_libnor_copy_t tgtCopy; + determineAndExecuteReboot(rebootFile, doReboot, tgtChip, tgtCopy); + if (doReboot) { +#if OBSW_VERBOSE_LEVEL >= 1 + sif::info << "Boot counter for image " << CURRENT_CHIP << " " << CURRENT_COPY + << "too high. Rebooting to " << tgtChip << " " << tgtCopy << std::endl; +#endif + xsc_boot_copy(tgtChip, tgtCopy); + } } doPerformRebootFileHandling = false; } } -void CoreController::determineAndExecuteReboot(xsc_libnor_copy_t copy, xsc_libnor_chip_t chip, - uint8_t cnt00, uint8_t cnt01, uint8_t cnt10, - uint8_t cnt11, uint8_t maxCnt) { - xsc_libnor_chip_t tgtChip = XSC_LIBNOR_CHIP_0; - xsc_libnor_copy_t tgtCopy = XSC_LIBNOR_COPY_NOMINAL; - if((copy == XSC_LIBNOR_COPY_GOLD) and (chip == XSC_LIBNOR_CHIP_1) and (cnt11 >= maxCnt)) { - if(cnt10 >= maxCnt) { - if(cnt00 >= maxCnt) { - if(cnt01 >= maxCnt) { +void CoreController::determineAndExecuteReboot(RebootFile &rf, bool &needsReboot, + xsc_libnor_chip_t &tgtChip, + xsc_libnor_copy_t &tgtCopy) { + tgtChip = XSC_LIBNOR_CHIP_0; + tgtCopy = XSC_LIBNOR_COPY_NOMINAL; + needsReboot = false; + if ((CURRENT_COPY == xsc::COPY_0) and (CURRENT_CHIP == xsc::CHIP_0) and + (rf.img00Cnt >= rf.maxCount)) { + needsReboot = true; + if (rf.img01Cnt >= rf.maxCount) { + if (rf.img10Cnt >= rf.maxCount) { + if (rf.img11Cnt >= rf.maxCount) { + // Can't really do much here. Stay on image + sif::warning << "All reboot counts too high, but already on fallback image" << std::endl; + return; + } else { + tgtCopy = XSC_LIBNOR_COPY_GOLD; + tgtChip = XSC_LIBNOR_CHIP_1; + } + } else { + tgtCopy = XSC_LIBNOR_COPY_GOLD; + tgtChip = XSC_LIBNOR_CHIP_0; + } + } else { + tgtCopy = XSC_LIBNOR_COPY_GOLD; + } + } + if ((CURRENT_COPY == xsc::COPY_0) and (CURRENT_CHIP == xsc::CHIP_1) and + (rf.img01Cnt >= rf.maxCount)) { + needsReboot = true; + if (rf.img00Cnt >= rf.maxCount) { + if (rf.img10Cnt >= rf.maxCount) { + if (rf.img11Cnt >= rf.maxCount) { + // Reboot to fallback image + } else { + tgtChip = XSC_LIBNOR_CHIP_1; + tgtCopy = XSC_LIBNOR_COPY_GOLD; + } + } else { + tgtChip = XSC_LIBNOR_CHIP_1; + tgtCopy = XSC_LIBNOR_COPY_NOMINAL; + } + } else { + // Reboot on fallback image + } + } + if ((CURRENT_COPY == xsc::COPY_1) and (CURRENT_CHIP == xsc::CHIP_0) and + (rf.img10Cnt >= rf.maxCount)) { + needsReboot = true; + if (rf.img11Cnt >= rf.maxCount) { + if (rf.img00Cnt >= rf.maxCount) { + if (rf.img01Cnt >= rf.maxCount) { + // Reboot to fallback image + } else { + tgtChip = XSC_LIBNOR_CHIP_0; + tgtCopy = XSC_LIBNOR_COPY_GOLD; + } + } else { + tgtChip = XSC_LIBNOR_CHIP_0; + tgtCopy = XSC_LIBNOR_COPY_NOMINAL; + } + } else { + tgtCopy = XSC_LIBNOR_COPY_GOLD; + tgtChip = XSC_LIBNOR_CHIP_1; + } + } + if ((CURRENT_COPY == xsc::COPY_1) and (CURRENT_CHIP == xsc::CHIP_1) and + (rf.img11Cnt >= rf.maxCount)) { + needsReboot = true; + if (rf.img10Cnt >= rf.maxCount) { + if (rf.img00Cnt >= rf.maxCount) { + if (rf.img01Cnt >= rf.maxCount) { // Reboot to fallback image } else { tgtChip = XSC_LIBNOR_CHIP_0; @@ -1290,24 +1270,150 @@ void CoreController::determineAndExecuteReboot(xsc_libnor_copy_t copy, xsc_libno tgtCopy = XSC_LIBNOR_COPY_NOMINAL; } } - if((copy == XSC_LIBNOR_COPY_NOMINAL) and (chip == XSC_LIBNOR_CHIP_0) and (cnt00 >= maxCnt)) { - if(cnt01 >= maxCnt) { - if(cnt10 >= maxCnt) { - if(cnt11 >= maxCnt) { - // Can't really do much here. Stay on image - sif::warning << "All reboot counts too high, but already on fallback image" << std::endl; - return; - } else { - tgtChip = XSC_LIBNOR_CHIP_1; - tgtCopy = XSC_LIBNOR_COPY_NOMINAL; - } - } else { - tgtChip = XSC_LIBNOR_CHIP_1; - tgtCopy = XSC_LIBNOR_COPY_GOLD; - } +} + +bool CoreController::parseRebootFile(std::string path, RebootFile &rf) { + using namespace std; + std::string selfMatch; + if (CURRENT_CHIP == xsc::CHIP_0) { + if (CURRENT_COPY == xsc::COPY_0) { + selfMatch = "00"; } else { - tgtCopy = XSC_LIBNOR_COPY_GOLD; + selfMatch = "01"; + } + } else { + if (CURRENT_COPY == xsc::COPY_0) { + selfMatch = "10"; + } else { + selfMatch = "11"; } } - xsc_boot_copy(tgtChip, tgtCopy); + ifstream file(path); + string word; + string line; + uint8_t lineIdx = 0; + while (std::getline(file, line)) { + istringstream iss(line); + switch (lineIdx) { + case 0: { + iss >> word; + if (word.find("on:") == string::npos) { + // invalid file + return false; + } + iss >> rf.enabled; + break; + } + case 1: { + iss >> word; + if (word.find("maxcnt:") == string::npos) { + return false; + } + iss >> rf.maxCount; + break; + } + case 2: { + iss >> word; + if (word.find("img00:") == string::npos) { + return false; + } + iss >> rf.img00Cnt; + if (word.find(selfMatch) != string::npos) { + rf.relevantBootCnt = rf.img00Cnt; + } + break; + } + case 3: { + iss >> word; + if (word.find("img01:") == string::npos) { + return false; + } + iss >> rf.img01Cnt; + if (word.find(selfMatch) != string::npos) { + rf.relevantBootCnt = rf.img01Cnt; + } + break; + } + case 4: { + iss >> word; + if (word.find("img10:") == string::npos) { + return false; + } + iss >> rf.img10Cnt; + if (word.find(selfMatch) != string::npos) { + rf.relevantBootCnt = rf.img10Cnt; + } + break; + } + case 5: { + iss >> word; + if (word.find("img11:") == string::npos) { + return false; + } + iss >> rf.img11Cnt; + if (word.find(selfMatch) != string::npos) { + rf.relevantBootCnt = rf.img11Cnt; + } + break; + } + case 6: { + iss >> word; + if (word.find("bootflag:") == string::npos) { + return false; + } + iss >> rf.bootFlag; + break; + } + case 7: { + iss >> word; + if (word.find("bootflag:") == string::npos) { + return false; + } + break; + } + case 8: { + iss >> word; + uint8_t copyRaw = 0; + uint8_t chipRaw = 0; + if (word.find("last:") == string::npos) { + return false; + } + iss >> chipRaw; + if (iss.fail()) { + return false; + } + iss >> copyRaw; + if (iss.fail()) { + return false; + } + + if (chipRaw > 1 or copyRaw > 1) { + return false; + } + rf.lastChip = static_cast(chipRaw); + rf.lastCopy = static_cast(copyRaw); + } + } + if (iss.fail()) { + return false; + } + lineIdx++; + } + if (lineIdx < 8) { + return false; + } + return true; +} + +void CoreController::rewriteRebootFile(RebootFile file) { + std::string path = sdcMan->getCurrentMountPrefix(sdInfo.pref) + REBOOT_FILE; + std::ofstream rebootFile(path); + if (rebootFile.is_open()) { + // Initiate reboot file first. Reboot handling will be on on initialization + rebootFile << "on: " << file.enabled << "\nmaxcnt: " << file.maxCount + << "\nimg00: " << file.img00Cnt << "\nimg01: " << file.img01Cnt + << "\nimg10: " << file.img10Cnt << "\nimg11: " << file.img11Cnt + << "\nbootflag: " << file.bootFlag << "\nlast: " << file.lastChip << " " + << file.lastCopy << "\n"; + } } diff --git a/bsp_q7s/core/CoreController.h b/bsp_q7s/core/CoreController.h index 78d7584b..85d704ee 100644 --- a/bsp_q7s/core/CoreController.h +++ b/bsp_q7s/core/CoreController.h @@ -1,23 +1,44 @@ #ifndef BSP_Q7S_CORE_CORECONTROLLER_H_ #define BSP_Q7S_CORE_CORECONTROLLER_H_ -#include "bsp_q7s/memory/SdCardManager.h" -#include "events/subsystemIdRanges.h" - #include -#include "fsfw/controller/ExtendedControllerBase.h" +#include #include -#include + +#include "bsp_q7s/memory/SdCardManager.h" +#include "events/subsystemIdRanges.h" +#include "fsfw/controller/ExtendedControllerBase.h" class Timer; class SdCardManager; +namespace xsc { + +enum Chip : uint8_t { CHIP_0, CHIP_1, NO_CHIP, SELF_CHIP, ALL_CHIP }; +enum Copy : uint8_t { COPY_0, COPY_1, NO_COPY, SELF_COPY, ALL_COPY }; + +} // namespace xsc + +struct RebootFile { + static constexpr uint8_t DEFAULT_MAX_BOOT_CNT = 10; + + bool enabled = false; + size_t maxCount = DEFAULT_MAX_BOOT_CNT; + uint8_t img00Cnt = 0; + uint8_t img01Cnt = 0; + uint8_t img10Cnt = 0; + uint8_t img11Cnt = 0; + uint8_t relevantBootCnt = 0; + bool bootFlag = false; + xsc::Chip lastChip = xsc::Chip::CHIP_0; + xsc::Copy lastCopy = xsc::Copy::COPY_0; +}; + class CoreController : public ExtendedControllerBase { public: - enum Chip : uint8_t { CHIP_0, CHIP_1, NO_CHIP, SELF_CHIP, ALL_CHIP }; - - enum Copy : uint8_t { COPY_0, COPY_1, NO_COPY, SELF_COPY, ALL_COPY }; + static xsc::Chip CURRENT_CHIP; + static xsc::Copy CURRENT_COPY; static constexpr char CHIP_PROT_SCRIPT[] = "/home/root/scripts/get-chip-prot-status.sh"; static constexpr char CHIP_STATE_FILE[] = "/tmp/chip_prot_status.txt"; @@ -25,8 +46,6 @@ class CoreController : public ExtendedControllerBase { static constexpr char VERSION_FILE[] = "/conf/version.txt"; static constexpr char REBOOT_FILE[] = "/conf/reboot.txt"; - static constexpr uint8_t DEFAULT_MAX_BOOT_CNT = 10; - static constexpr ActionId_t LIST_DIRECTORY_INTO_FILE = 0; static constexpr ActionId_t REBOOT_OBC = 32; static constexpr ActionId_t MOUNT_OTHER_COPY = 33; @@ -54,7 +73,7 @@ class CoreController : public ExtendedControllerBase { */ static ReturnValue_t generateChipStateFile(); static ReturnValue_t incrementAllocationFailureCount(); - static void getCurrentBootCopy(Chip& chip, Copy& copy); + static void getCurrentBootCopy(xsc::Chip& chip, xsc::Copy& copy); ReturnValue_t updateProtInfo(bool regenerateChipStateFile = true); @@ -69,15 +88,12 @@ class CoreController : public ExtendedControllerBase { * @param updateProtFile Specify whether the protection info file is updated * @return */ - ReturnValue_t setBootCopyProtection(Chip targetChip, Copy targetCopy, bool protect, + ReturnValue_t setBootCopyProtection(xsc::Chip targetChip, xsc::Copy targetCopy, bool protect, bool& protOperationPerformed, bool updateProtFile = true); bool sdInitFinished() const; private: - static Chip CURRENT_CHIP; - static Copy CURRENT_COPY; - // Designated value for rechecking FIFO open static constexpr int RETRY_FIFO_OPEN = -2; int watchdogFifoFd = 0; @@ -128,6 +144,7 @@ class CoreController : public ExtendedControllerBase { sd::SdState commandedState = sd::SdState::OFF; }; SdInfo sdInfo; + RebootFile rebootFile = {}; bool doPerformRebootFileHandling = true; /** @@ -168,11 +185,13 @@ class CoreController : public ExtendedControllerBase { void performWatchdogControlOperation(); ReturnValue_t handleProtInfoUpdateLine(std::string nextLine); - int handleBootCopyProtAtIndex(Chip targetChip, Copy targetCopy, bool protect, + int handleBootCopyProtAtIndex(xsc::Chip targetChip, xsc::Copy targetCopy, bool protect, bool& protOperationPerformed, bool selfChip, bool selfCopy, bool allChips, bool allCopies, uint8_t arrIdx); - void determineAndExecuteReboot(xsc_libnor_copy_t copy, xsc_libnor_chip_t chip, uint8_t cnt00, - uint8_t cnt01, uint8_t cnt10, uint8_t cnt11, uint8_t maxCnt); + void determineAndExecuteReboot(RebootFile& rf, bool& needsReboot, xsc_libnor_chip_t& tgtChip, + xsc_libnor_copy_t& tgtCopy); + bool parseRebootFile(std::string path, RebootFile& file); + void rewriteRebootFile(RebootFile file); }; #endif /* BSP_Q7S_CORE_CORECONTROLLER_H_ */