xref: /openbmc/estoraged/include/zero.hpp (revision d6071fc25a9880e8b622792b1e48d83fb5010e5e)
14bc8a10cSJohn Edward Broadbent #pragma once
24bc8a10cSJohn Edward Broadbent 
34bc8a10cSJohn Edward Broadbent #include "erase.hpp"
4a6e3b99dSJohn Edward Broadbent #include "util.hpp"
54bc8a10cSJohn Edward Broadbent 
64bc8a10cSJohn Edward Broadbent #include <stdplus/fd/create.hpp>
74bc8a10cSJohn Edward Broadbent #include <stdplus/fd/managed.hpp>
84bc8a10cSJohn Edward Broadbent 
9*d6071fc2SJohn Edward Broadbent #include <chrono>
10*d6071fc2SJohn Edward Broadbent 
114bc8a10cSJohn Edward Broadbent namespace estoraged
124bc8a10cSJohn Edward Broadbent {
134bc8a10cSJohn Edward Broadbent 
14*d6071fc2SJohn Edward Broadbent using stdplus::fd::Fd;
154bc8a10cSJohn Edward Broadbent 
164bc8a10cSJohn Edward Broadbent class Zero : public Erase
174bc8a10cSJohn Edward Broadbent {
184bc8a10cSJohn Edward Broadbent   public:
194bc8a10cSJohn Edward Broadbent     /** @brief Creates a zero erase object.
204bc8a10cSJohn Edward Broadbent      *
214bc8a10cSJohn Edward Broadbent      *  @param[in] inDevPath - the linux device path for the block device.
224bc8a10cSJohn Edward Broadbent      */
234bc8a10cSJohn Edward Broadbent     Zero(std::string_view inDevPath) : Erase(inDevPath)
244bc8a10cSJohn Edward Broadbent     {}
254bc8a10cSJohn Edward Broadbent     /** @brief writes zero to the drive
264bc8a10cSJohn Edward Broadbent      * and throws errors accordingly.
27a6e3b99dSJohn Edward Broadbent      *  @param[in] driveSize - the size of the block device in bytes
28*d6071fc2SJohn Edward Broadbent      *  @param[in] fd - the stdplus file descriptor
294bc8a10cSJohn Edward Broadbent      */
30*d6071fc2SJohn Edward Broadbent     void writeZero(uint64_t driveSize, Fd& fd);
314bc8a10cSJohn Edward Broadbent 
32*d6071fc2SJohn Edward Broadbent     /** @brief writes zero to the drive using default parameters,
33a6e3b99dSJohn Edward Broadbent      * and throws errors accordingly.
34a6e3b99dSJohn Edward Broadbent      */
35a6e3b99dSJohn Edward Broadbent     void writeZero()
36a6e3b99dSJohn Edward Broadbent     {
37*d6071fc2SJohn Edward Broadbent         stdplus::fd::Fd&& fd =
38*d6071fc2SJohn Edward Broadbent             stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
39*d6071fc2SJohn Edward Broadbent         writeZero(util::findSizeOfBlockDevice(devPath), fd);
40a6e3b99dSJohn Edward Broadbent     }
41a6e3b99dSJohn Edward Broadbent 
42*d6071fc2SJohn Edward Broadbent     /** @brief verifies the drive has only zeros on it,
434bc8a10cSJohn Edward Broadbent      * and throws errors accordingly.
44a6e3b99dSJohn Edward Broadbent      *  @param[in] driveSize - the size of the block device in bytes
45*d6071fc2SJohn Edward Broadbent      *  @param[in] fd - the stdplus file descriptor
464bc8a10cSJohn Edward Broadbent      */
47*d6071fc2SJohn Edward Broadbent     void verifyZero(uint64_t driveSize, Fd& fd);
484bc8a10cSJohn Edward Broadbent 
49*d6071fc2SJohn Edward Broadbent     /** @brief verifies the drive has only zeros on it,
50*d6071fc2SJohn Edward Broadbent      * using the default parameters. It also throws errors accordingly.
51a6e3b99dSJohn Edward Broadbent      */
52a6e3b99dSJohn Edward Broadbent     void verifyZero()
53a6e3b99dSJohn Edward Broadbent     {
54*d6071fc2SJohn Edward Broadbent         stdplus::fd::Fd&& fd =
55*d6071fc2SJohn Edward Broadbent             stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
56*d6071fc2SJohn Edward Broadbent         verifyZero(util::findSizeOfBlockDevice(devPath), fd);
57a6e3b99dSJohn Edward Broadbent     }
58a6e3b99dSJohn Edward Broadbent 
594bc8a10cSJohn Edward Broadbent   private:
604bc8a10cSJohn Edward Broadbent     /* @brief the size of the blocks in bytes used for write and verify.
614bc8a10cSJohn Edward Broadbent      * 32768 was also tested. It had almost identical performance.
624bc8a10cSJohn Edward Broadbent      */
634bc8a10cSJohn Edward Broadbent     static constexpr size_t blockSize = 4096;
64*d6071fc2SJohn Edward Broadbent     static constexpr size_t maxRetry = 32;
65*d6071fc2SJohn Edward Broadbent     static constexpr std::chrono::duration delay = std::chrono::milliseconds(1);
664bc8a10cSJohn Edward Broadbent };
674bc8a10cSJohn Edward Broadbent 
684bc8a10cSJohn Edward Broadbent } // namespace estoraged
69