xref: /openbmc/estoraged/include/zero.hpp (revision e7f6975e)
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     /** @brief writes zero to the drive
25      * and throws errors accordingly.
26      *  @param[in] driveSize - the size of the block device in bytes
27      *  @param[in] fd - the stdplus file descriptor
28      */
29     void writeZero(uint64_t driveSize, Fd& fd);
30 
31     /** @brief writes zero to the drive using default parameters,
32      * and throws errors accordingly.
33      */
34     void writeZero()
35     {
36         stdplus::fd::Fd&& fd =
37             stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
38         writeZero(util::findSizeOfBlockDevice(devPath), fd);
39     }
40 
41     /** @brief verifies the drive has only zeros on it,
42      * and throws errors accordingly.
43      *  @param[in] driveSize - the size of the block device in bytes
44      *  @param[in] fd - the stdplus file descriptor
45      */
46     void verifyZero(uint64_t driveSize, Fd& fd);
47 
48     /** @brief verifies the drive has only zeros on it,
49      * using the default parameters. It also throws errors accordingly.
50      */
51     void verifyZero()
52     {
53         stdplus::fd::Fd&& fd =
54             stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
55         verifyZero(util::findSizeOfBlockDevice(devPath), fd);
56     }
57 
58   private:
59     /* @brief the size of the blocks in bytes used for write and verify.
60      * 32768 was also tested. It had almost identical performance.
61      */
62     static constexpr size_t blockSize = 4096;
63     static constexpr size_t maxRetry = 32;
64     static constexpr std::chrono::duration delay = std::chrono::milliseconds(1);
65 };
66 
67 } // namespace estoraged
68