text: #include "zero.hpp"
  
  #include "erase.hpp"
  
  #include <unistd.h>
  
  #include <phosphor-logging/lg2.hpp>
  #include <stdplus/fd/create.hpp>
  #include <stdplus/fd/managed.hpp>
  #include <xyz/openbmc_project/Common/error.hpp>
  
  #include <array>
  #include <chrono>
  #include <span>
  #include <thread>
  
  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<const std::byte, blockSize> 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<std::byte, blockSize> readArr{};
      const std::array<const std::byte, blockSize> 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
  