John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 1 | #include "pattern.hpp" |
| 2 | |
| 3 | #include "erase.hpp" |
| 4 | |
| 5 | #include <unistd.h> |
| 6 | |
| 7 | #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 Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 13 | #include <chrono> |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 14 | #include <iostream> |
| 15 | #include <random> |
| 16 | #include <span> |
| 17 | #include <string> |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 18 | #include <thread> |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 19 | |
John Edward Broadbent | d3bfa7b | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 20 | namespace estoraged |
| 21 | { |
| 22 | |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 23 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 24 | using stdplus::fd::Fd; |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 25 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 26 | void Pattern::writePattern(const uint64_t driveSize, Fd& fd) |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 27 | { |
Manojkiran Eda | d4554f2 | 2024-06-17 14:11:30 +0530 | [diff] [blame^] | 28 | // static seed defines a fixed prng sequence so it can be verified later, |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 29 | // and validated for entropy |
| 30 | uint64_t currentIndex = 0; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 31 | |
| 32 | // random number generator seeded with a constant value will |
| 33 | // generate a predictable sequence of values NOLINTNEXTLINE |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 34 | std::minstd_rand0 generator(seed); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 35 | std::array<std::byte, blockSize> randArr{}; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 36 | |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 37 | while (currentIndex < driveSize) |
| 38 | { |
| 39 | // generate a 4k block of prng |
| 40 | std::array<uint32_t, blockSizeUsing32>* randArrFill = |
| 41 | reinterpret_cast<std::array<uint32_t, blockSizeUsing32>*>(&randArr); |
| 42 | for (uint32_t i = 0; i < blockSizeUsing32; i++) |
| 43 | { |
| 44 | (*randArrFill)[i] = generator(); |
| 45 | } |
| 46 | // if we can write all 4k bytes do that, else write the remainder |
| 47 | size_t writeSize = currentIndex + blockSize < driveSize |
| 48 | ? blockSize |
| 49 | : driveSize - currentIndex; |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 50 | size_t written = 0; |
| 51 | size_t retry = 0; |
| 52 | while (written < writeSize) |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 53 | { |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 54 | written += fd.write({randArr.data() + written, writeSize - written}) |
| 55 | .size(); |
| 56 | if (written == writeSize) |
| 57 | { |
| 58 | break; |
| 59 | } |
| 60 | if (written > writeSize) |
| 61 | { |
| 62 | throw InternalFailure(); |
| 63 | } |
| 64 | retry++; |
| 65 | if (retry > maxRetry) |
| 66 | { |
| 67 | lg2::error("Unable to do full write", "REDFISH_MESSAGE_ID", |
| 68 | std::string("eStorageD.1.0.EraseFailure")); |
| 69 | throw InternalFailure(); |
| 70 | } |
| 71 | std::this_thread::sleep_for(delay); |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 72 | } |
| 73 | currentIndex = currentIndex + writeSize; |
| 74 | } |
| 75 | } |
| 76 | |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 77 | void Pattern::verifyPattern(const uint64_t driveSize, Fd& fd) |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 78 | { |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 79 | uint64_t currentIndex = 0; |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 80 | // random number generator seeded with a constant value will |
| 81 | // generate a predictable sequence of values NOLINTNEXTLINE |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 82 | std::minstd_rand0 generator(seed); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 83 | std::array<std::byte, blockSize> randArr{}; |
| 84 | std::array<std::byte, blockSize> readArr{}; |
John Edward Broadbent | 6978676 | 2022-01-21 14:16:23 -0800 | [diff] [blame] | 85 | |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 86 | while (currentIndex < driveSize) |
| 87 | { |
| 88 | size_t readSize = currentIndex + blockSize < driveSize |
| 89 | ? blockSize |
| 90 | : driveSize - currentIndex; |
| 91 | try |
| 92 | { |
| 93 | std::array<uint32_t, blockSizeUsing32>* randArrFill = |
| 94 | reinterpret_cast<std::array<uint32_t, blockSizeUsing32>*>( |
| 95 | &randArr); |
| 96 | for (uint32_t i = 0; i < blockSizeUsing32; i++) |
| 97 | { |
| 98 | (*randArrFill)[i] = generator(); |
| 99 | } |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 100 | size_t read = 0; |
| 101 | size_t retry = 0; |
| 102 | while (read < readSize) |
| 103 | { |
| 104 | read += |
| 105 | fd.read({readArr.data() + read, readSize - read}).size(); |
| 106 | if (read == readSize) |
| 107 | { |
| 108 | break; |
| 109 | } |
| 110 | if (read > readSize) |
| 111 | { |
| 112 | throw InternalFailure(); |
| 113 | } |
| 114 | retry++; |
| 115 | if (retry > maxRetry) |
| 116 | { |
| 117 | lg2::error("Unable to do full read", "REDFISH_MESSAGE_ID", |
| 118 | std::string("eStorageD.1.0.EraseFailure")); |
| 119 | throw InternalFailure(); |
| 120 | } |
| 121 | std::this_thread::sleep_for(delay); |
| 122 | } |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 123 | } |
| 124 | catch (...) |
| 125 | { |
| 126 | lg2::error("Estoraged erase pattern unable to read", |
| 127 | "REDFISH_MESSAGE_ID", |
John Edward Broadbent | d6071fc | 2022-03-31 19:33:21 -0700 | [diff] [blame] | 128 | std::string("eStorageD.1.0.EraseFailure")); |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 129 | throw InternalFailure(); |
| 130 | } |
| 131 | |
| 132 | if (!std::equal(randArr.begin(), randArr.begin() + readSize, |
| 133 | readArr.begin(), readArr.begin() + readSize)) |
| 134 | { |
| 135 | lg2::error("Estoraged erase pattern does not match", |
| 136 | "REDFISH_MESSAGE_ID", |
| 137 | std::string("eStorageD.1.0.EraseFailure")); |
| 138 | throw InternalFailure(); |
| 139 | } |
| 140 | currentIndex = currentIndex + readSize; |
| 141 | } |
| 142 | } |
John Edward Broadbent | d3bfa7b | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 143 | |
| 144 | } // namespace estoraged |