xref: /openbmc/estoraged/src/erase/zero.cpp (revision 0c2808f0)
1 #include "zero.hpp"
2 
3 #include "erase.hpp"
4 
5 #include <phosphor-logging/lg2.hpp>
6 #include <stdplus/fd/create.hpp>
7 #include <stdplus/fd/managed.hpp>
8 #include <xyz/openbmc_project/Common/error.hpp>
9 
10 #include <array>
11 #include <span>
12 
13 namespace estoraged
14 {
15 
16 using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
17 using stdplus::fd::ManagedFd;
18 
19 void Zero::writeZero(const uint64_t driveSize)
20 {
21 
22     ManagedFd fd =
23         stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
24 
25     uint64_t currentIndex = 0;
26     const std::array<const std::byte, blockSize> blockOfZeros{};
27 
28     while (currentIndex < driveSize)
29     {
30         uint32_t writeSize = currentIndex + blockSize < driveSize
31                                  ? blockSize
32                                  : driveSize - currentIndex;
33         try
34         {
35             fd.write({blockOfZeros.data(), writeSize});
36         }
37         catch (...)
38         {
39             lg2::error("Estoraged erase zeros unable to write size",
40                        "REDFISH_MESSAGE_ID",
41                        std::string("eStorageD.1.0.EraseFailure"));
42             throw InternalFailure();
43         }
44         currentIndex += writeSize;
45     }
46 }
47 
48 void Zero::verifyZero(uint64_t driveSize)
49 {
50     ManagedFd fd =
51         stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
52 
53     uint64_t currentIndex = 0;
54     std::array<std::byte, blockSize> readArr;
55     const std::array<const std::byte, blockSize> blockOfZeros{};
56 
57     while (currentIndex < driveSize)
58     {
59         uint32_t readSize = currentIndex + blockSize < driveSize
60                                 ? blockSize
61                                 : driveSize - currentIndex;
62         try
63         {
64             fd.read({readArr.data(), readSize});
65         }
66         catch (...)
67         {
68             lg2::error("Estoraged erase zeros block unable to read size",
69                        "REDFISH_MESSAGE_ID",
70                        std::string("eStorageD.1.0.EraseFailure"));
71             throw InternalFailure();
72         }
73         if (memcmp(readArr.data(), blockOfZeros.data(), readSize))
74         {
75             lg2::error("Estoraged erase zeros block is not zero",
76                        "REDFISH_MESSAGE_ID",
77                        std::string("eStorageD.1.0.EraseFailure"));
78             throw InternalFailure();
79         }
80         currentIndex += readSize;
81     }
82 }
83 
84 } // namespace estoraged
85