xref: /openbmc/estoraged/include/zero.hpp (revision 04c28fad78934cab0c4cb0b4ef9a20f9261b4c1e)
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 
9d6071fc2SJohn Edward Broadbent #include <chrono>
10d6071fc2SJohn Edward Broadbent 
114bc8a10cSJohn Edward Broadbent namespace estoraged
124bc8a10cSJohn Edward Broadbent {
134bc8a10cSJohn Edward Broadbent 
14d6071fc2SJohn 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      */
Zero(std::string_view inDevPath)23*04c28fadSPatrick Williams     Zero(std::string_view inDevPath) : Erase(inDevPath) {}
244bc8a10cSJohn Edward Broadbent     /** @brief writes zero to the drive
254bc8a10cSJohn Edward Broadbent      * and throws errors accordingly.
26a6e3b99dSJohn Edward Broadbent      *  @param[in] driveSize - the size of the block device in bytes
27d6071fc2SJohn Edward Broadbent      *  @param[in] fd - the stdplus file descriptor
284bc8a10cSJohn Edward Broadbent      */
29d6071fc2SJohn Edward Broadbent     void writeZero(uint64_t driveSize, Fd& fd);
304bc8a10cSJohn Edward Broadbent 
31d6071fc2SJohn Edward Broadbent     /** @brief writes zero to the drive using default parameters,
32a6e3b99dSJohn Edward Broadbent      * and throws errors accordingly.
33a6e3b99dSJohn Edward Broadbent      */
writeZero()34a6e3b99dSJohn Edward Broadbent     void writeZero()
35a6e3b99dSJohn Edward Broadbent     {
36d6071fc2SJohn Edward Broadbent         stdplus::fd::Fd&& fd =
37d6071fc2SJohn Edward Broadbent             stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
38d6071fc2SJohn Edward Broadbent         writeZero(util::findSizeOfBlockDevice(devPath), fd);
39a6e3b99dSJohn Edward Broadbent     }
40a6e3b99dSJohn Edward Broadbent 
41d6071fc2SJohn Edward Broadbent     /** @brief verifies the drive has only zeros on it,
424bc8a10cSJohn Edward Broadbent      * and throws errors accordingly.
43a6e3b99dSJohn Edward Broadbent      *  @param[in] driveSize - the size of the block device in bytes
44d6071fc2SJohn Edward Broadbent      *  @param[in] fd - the stdplus file descriptor
454bc8a10cSJohn Edward Broadbent      */
46d6071fc2SJohn Edward Broadbent     void verifyZero(uint64_t driveSize, Fd& fd);
474bc8a10cSJohn Edward Broadbent 
48d6071fc2SJohn Edward Broadbent     /** @brief verifies the drive has only zeros on it,
49d6071fc2SJohn Edward Broadbent      * using the default parameters. It also throws errors accordingly.
50a6e3b99dSJohn Edward Broadbent      */
verifyZero()51a6e3b99dSJohn Edward Broadbent     void verifyZero()
52a6e3b99dSJohn Edward Broadbent     {
53d6071fc2SJohn Edward Broadbent         stdplus::fd::Fd&& fd =
54d6071fc2SJohn Edward Broadbent             stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
55d6071fc2SJohn Edward Broadbent         verifyZero(util::findSizeOfBlockDevice(devPath), fd);
56a6e3b99dSJohn Edward Broadbent     }
57a6e3b99dSJohn Edward Broadbent 
584bc8a10cSJohn Edward Broadbent   private:
594bc8a10cSJohn Edward Broadbent     /* @brief the size of the blocks in bytes used for write and verify.
604bc8a10cSJohn Edward Broadbent      * 32768 was also tested. It had almost identical performance.
614bc8a10cSJohn Edward Broadbent      */
624bc8a10cSJohn Edward Broadbent     static constexpr size_t blockSize = 4096;
63d6071fc2SJohn Edward Broadbent     static constexpr size_t maxRetry = 32;
64d6071fc2SJohn Edward Broadbent     static constexpr std::chrono::duration delay = std::chrono::milliseconds(1);
654bc8a10cSJohn Edward Broadbent };
664bc8a10cSJohn Edward Broadbent 
674bc8a10cSJohn Edward Broadbent } // namespace estoraged
68