blob: accb8c82b1abf0715250cf25eaea4e77ae956e8f [file] [log] [blame]
John Edward Broadbent4bc8a102021-12-30 16:11:49 -08001#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
13namespace estoraged
14{
15
16using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
17using stdplus::fd::ManagedFd;
18
19void 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
45void 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