#include "zero.hpp" #include "erase.hpp" #include #include #include #include #include #include #include #include #include namespace estoraged { using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; using stdplus::fd::Fd; void Zero::writeZero(const uint64_t driveSize, Fd& fd) { uint64_t currentIndex = 0; const std::array blockOfZeros{}; while (currentIndex < driveSize) { uint32_t writeSize = currentIndex + blockSize < driveSize ? blockSize : driveSize - currentIndex; try { size_t written = 0; size_t retry = 0; while (written < writeSize) { written += fd.write({blockOfZeros.data() + written, writeSize - written}) .size(); if (written == writeSize) { break; } if (written > writeSize) { throw InternalFailure(); } retry++; if (retry > maxRetry) { lg2::error("Unable to make full write", "REDFISH_MESSAGE_ID", std::string("eStorageD.1.0.EraseFailure")); throw InternalFailure(); } std::this_thread::sleep_for(delay); } } catch (...) { lg2::error("Estoraged erase zeros unable to write size", "REDFISH_MESSAGE_ID", std::string("eStorageD.1.0.EraseFailure")); throw InternalFailure(); } currentIndex += writeSize; } } void Zero::verifyZero(uint64_t driveSize, Fd& fd) { uint64_t currentIndex = 0; std::array readArr{}; const std::array blockOfZeros{}; while (currentIndex < driveSize) { uint32_t readSize = currentIndex + blockSize < driveSize ? blockSize : driveSize - currentIndex; try { size_t read = 0; size_t retry = 0; while (read < readSize) { read += fd.read({readArr.data() + read, readSize - read}).size(); if (read == readSize) { break; } if (read > readSize) { throw InternalFailure(); } retry++; if (retry > maxRetry) { lg2::error("Unable to make full read", "REDFISH_MESSAGE_ID", std::string("eStorageD.1.0.EraseFailure")); throw InternalFailure(); } std::this_thread::sleep_for(delay); } } catch (...) { lg2::error("Estoraged erase zeros block unable to read size", "REDFISH_MESSAGE_ID", std::string("eStorageD.1.0.EraseFailure")); throw InternalFailure(); } if (memcmp(readArr.data(), blockOfZeros.data(), readSize) != 0) { lg2::error("Estoraged erase zeros block is not zero", "REDFISH_MESSAGE_ID", std::string("eStorageD.1.0.EraseFailure")); throw InternalFailure(); } currentIndex += readSize; } } } // namespace estoraged