xref: /openbmc/estoraged/include/zero.hpp (revision d6071fc2)
1 #pragma once
2 
3 #include "erase.hpp"
4 #include "util.hpp"
5 
6 #include <stdplus/fd/create.hpp>
7 #include <stdplus/fd/managed.hpp>
8 
9 #include <chrono>
10 
11 namespace estoraged
12 {
13 
14 using stdplus::fd::Fd;
15 
16 class Zero : public Erase
17 {
18   public:
19     /** @brief Creates a zero erase object.
20      *
21      *  @param[in] inDevPath - the linux device path for the block device.
22      */
23     Zero(std::string_view inDevPath) : Erase(inDevPath)
24     {}
25     /** @brief writes zero to the drive
26      * and throws errors accordingly.
27      *  @param[in] driveSize - the size of the block device in bytes
28      *  @param[in] fd - the stdplus file descriptor
29      */
30     void writeZero(uint64_t driveSize, Fd& fd);
31 
32     /** @brief writes zero to the drive using default parameters,
33      * and throws errors accordingly.
34      */
35     void writeZero()
36     {
37         stdplus::fd::Fd&& fd =
38             stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
39         writeZero(util::findSizeOfBlockDevice(devPath), fd);
40     }
41 
42     /** @brief verifies the drive has only zeros on it,
43      * and throws errors accordingly.
44      *  @param[in] driveSize - the size of the block device in bytes
45      *  @param[in] fd - the stdplus file descriptor
46      */
47     void verifyZero(uint64_t driveSize, Fd& fd);
48 
49     /** @brief verifies the drive has only zeros on it,
50      * using the default parameters. It also throws errors accordingly.
51      */
52     void verifyZero()
53     {
54         stdplus::fd::Fd&& fd =
55             stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
56         verifyZero(util::findSizeOfBlockDevice(devPath), fd);
57     }
58 
59   private:
60     /* @brief the size of the blocks in bytes used for write and verify.
61      * 32768 was also tested. It had almost identical performance.
62      */
63     static constexpr size_t blockSize = 4096;
64     static constexpr size_t maxRetry = 32;
65     static constexpr std::chrono::duration delay = std::chrono::milliseconds(1);
66 };
67 
68 } // namespace estoraged
69