blob: d26e1b97760f3a82c989b3cee08d9934de02c31d [file] [log] [blame]
John Edward Broadbent4bc8a102021-12-30 16:11:49 -08001#include "zero.hpp"
2
3#include "erase.hpp"
4
John Edward Broadbentd6071fc2022-03-31 19:33:21 -07005#include <unistd.h>
6
John Edward Broadbent4bc8a102021-12-30 16:11:49 -08007#include <phosphor-logging/lg2.hpp>
8#include <stdplus/fd/create.hpp>
9#include <stdplus/fd/managed.hpp>
10#include <xyz/openbmc_project/Common/error.hpp>
11
12#include <array>
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070013#include <chrono>
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080014#include <span>
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070015#include <thread>
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080016
17namespace estoraged
18{
19
20using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070021using stdplus::fd::Fd;
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080022
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070023void Zero::writeZero(const uint64_t driveSize, Fd& fd)
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080024{
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 {
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070035 size_t written = 0;
36 size_t retry = 0;
37 while (written < writeSize)
38 {
39 written += fd.write({blockOfZeros.data() + written,
40 writeSize - written})
41 .size();
42 if (written == writeSize)
43 {
44 break;
45 }
46 if (written > writeSize)
47 {
48 throw InternalFailure();
49 }
50 retry++;
51 if (retry > maxRetry)
52 {
53 lg2::error("Unable to make full write",
54 "REDFISH_MESSAGE_ID",
55 std::string("eStorageD.1.0.EraseFailure"));
56 throw InternalFailure();
57 }
58 std::this_thread::sleep_for(delay);
59 }
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080060 }
61 catch (...)
62 {
63 lg2::error("Estoraged erase zeros unable to write size",
64 "REDFISH_MESSAGE_ID",
65 std::string("eStorageD.1.0.EraseFailure"));
66 throw InternalFailure();
67 }
68 currentIndex += writeSize;
69 }
70}
71
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070072void Zero::verifyZero(uint64_t driveSize, Fd& fd)
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080073{
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080074 uint64_t currentIndex = 0;
Ed Tanous82897c32022-02-21 14:11:59 -080075 std::array<std::byte, blockSize> readArr{};
John Edward Broadbent4bc8a102021-12-30 16:11:49 -080076 const std::array<const std::byte, blockSize> blockOfZeros{};
77
78 while (currentIndex < driveSize)
79 {
80 uint32_t readSize = currentIndex + blockSize < driveSize
81 ? blockSize
82 : driveSize - currentIndex;
83 try
84 {
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070085 size_t read = 0;
86 size_t retry = 0;
87 while (read < readSize)
88 {
89 read +=
90 fd.read({readArr.data() + read, readSize - read}).size();
91 if (read == readSize)
92 {
93 break;
94 }
95 if (read > readSize)
96 {
97 throw InternalFailure();
98 }
99 retry++;
100 if (retry > maxRetry)
101 {
102 lg2::error("Unable to make full read", "REDFISH_MESSAGE_ID",
103 std::string("eStorageD.1.0.EraseFailure"));
104 throw InternalFailure();
105 }
106 std::this_thread::sleep_for(delay);
107 }
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800108 }
109 catch (...)
110 {
111 lg2::error("Estoraged erase zeros block unable to read size",
112 "REDFISH_MESSAGE_ID",
113 std::string("eStorageD.1.0.EraseFailure"));
114 throw InternalFailure();
115 }
Ed Tanous82897c32022-02-21 14:11:59 -0800116 if (memcmp(readArr.data(), blockOfZeros.data(), readSize) != 0)
John Edward Broadbent4bc8a102021-12-30 16:11:49 -0800117 {
118 lg2::error("Estoraged erase zeros block is not zero",
119 "REDFISH_MESSAGE_ID",
120 std::string("eStorageD.1.0.EraseFailure"));
121 throw InternalFailure();
122 }
123 currentIndex += readSize;
124 }
125}
126
127} // namespace estoraged