xref: /openbmc/estoraged/src/erase/zero.cpp (revision 4bc8a10caff8d7154d9ad789b9d0fc01499fa02e)
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, ManagedFd& fd)
20 {
21 
22     uint64_t currentIndex = 0;
23     const std::array<const std::byte, blockSize> blockOfZeros{};
24 
25     while (currentIndex < driveSize)
26     {
27         uint32_t writeSize = currentIndex + blockSize < driveSize
28                                  ? blockSize
29                                  : driveSize - currentIndex;
30         try
31         {
32             fd.write({blockOfZeros.data(), writeSize});
33         }
34         catch (...)
35         {
36             lg2::error("Estoraged erase zeros unable to write size",
37                        "REDFISH_MESSAGE_ID",
38                        std::string("eStorageD.1.0.EraseFailure"));
39             throw InternalFailure();
40         }
41         currentIndex += writeSize;
42     }
43 }
44 
45 void Zero::verifyZero(uint64_t driveSize, ManagedFd& fd)
46 {
47     uint64_t currentIndex = 0;
48     std::array<std::byte, blockSize> readArr;
49     const std::array<const std::byte, blockSize> blockOfZeros{};
50 
51     while (currentIndex < driveSize)
52     {
53         uint32_t readSize = currentIndex + blockSize < driveSize
54                                 ? blockSize
55                                 : driveSize - currentIndex;
56         try
57         {
58             fd.read({readArr.data(), readSize});
59         }
60         catch (...)
61         {
62             lg2::error("Estoraged erase zeros block unable to read size",
63                        "REDFISH_MESSAGE_ID",
64                        std::string("eStorageD.1.0.EraseFailure"));
65             throw InternalFailure();
66         }
67         if (memcmp(readArr.data(), blockOfZeros.data(), readSize))
68         {
69             lg2::error("Estoraged erase zeros block is not zero",
70                        "REDFISH_MESSAGE_ID",
71                        std::string("eStorageD.1.0.EraseFailure"));
72             throw InternalFailure();
73         }
74         currentIndex += readSize;
75     }
76 }
77 
78 } // namespace estoraged
79