blob: 2c5a6f5b49ff2fcf2e5533380e45147ea015476f [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
John Edward Broadbent69786762022-01-21 14:16:23 -080019void Zero::writeZero(const uint64_t driveSize)
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080020{
21
John Edward Broadbent69786762022-01-21 14:16:23 -080022 ManagedFd fd =
23 stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
24
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080025 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
John Edward Broadbent69786762022-01-21 14:16:23 -080048void Zero::verifyZero(uint64_t driveSize)
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080049{
John Edward Broadbent69786762022-01-21 14:16:23 -080050 ManagedFd fd =
51 stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
52
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080053 uint64_t currentIndex = 0;
Ed Tanous82897c32022-02-21 14:11:59 -080054 std::array<std::byte, blockSize> readArr{};
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080055 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 }
Ed Tanous82897c32022-02-21 14:11:59 -080073 if (memcmp(readArr.data(), blockOfZeros.data(), readSize) != 0)
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080074 {
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