blob: 3a15dff1117bd589365343714c47fd59cb67b05e [file] [log] [blame]
John Edward Broadbent7f2ab642021-11-11 21:00:38 -08001#pragma once
2
3#include "erase.hpp"
John Edward Broadbenta6e3b992022-03-17 14:33:15 -07004#include "util.hpp"
John Edward Broadbent7f2ab642021-11-11 21:00:38 -08005
6#include <stdplus/fd/create.hpp>
7#include <stdplus/fd/managed.hpp>
8
John Edward Broadbentd6071fc2022-03-31 19:33:21 -07009#include <chrono>
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080010#include <span>
11#include <string>
12
John Edward Broadbentd3bfa7b2022-01-13 17:41:32 -080013namespace estoraged
14{
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070015using stdplus::fd::Fd;
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080016
17class Pattern : public Erase
18{
19 public:
20 /** @brief Creates a pattern erase object.
21 *
22 * @param[in] inDevPath - the linux device path for the block device.
23 */
Patrick Williams04c28fa2023-05-10 07:51:24 -050024 Pattern(std::string_view inDevPath) : Erase(inDevPath) {}
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080025
Manojkiran Edad4554f22024-06-17 14:11:30 +053026 /** @brief writes an incompressible random pattern to the drive, using
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070027 * default parameters. It also throws errors accordingly.
28 */
29 void writePattern()
30 {
31 stdplus::fd::Fd&& fd =
32 stdplus::fd::open(devPath, stdplus::fd::OpenAccess::WriteOnly);
33 writePattern(util::findSizeOfBlockDevice(devPath), fd);
34 }
35
Manojkiran Edad4554f22024-06-17 14:11:30 +053036 /** @brief writes an incompressible random pattern to the drive
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080037 * and throws errors accordingly.
38 *
39 * @param[in] bytes - Size of the block device
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070040 * @param[in] fd - the stdplus file descriptor
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080041 */
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070042 void writePattern(uint64_t driveSize, Fd& fd);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070043
Manojkiran Edad4554f22024-06-17 14:11:30 +053044 /** @brief verifies the incompressible random pattern is on the drive, using
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070045 * default parameters. It also throws errors accordingly.
46 */
47 void verifyPattern()
48 {
49 stdplus::fd::Fd&& fd =
50 stdplus::fd::open(devPath, stdplus::fd::OpenAccess::ReadOnly);
51 verifyPattern(util::findSizeOfBlockDevice(devPath), fd);
52 }
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080053
Manojkiran Edad4554f22024-06-17 14:11:30 +053054 /** @brief verifies the incompressible random pattern is on the drive
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080055 * and throws errors accordingly.
56 *
57 * @param[in] bytes - Size of the block device
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070058 * @param[in] fd - the stdplus file descriptor
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080059 */
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070060 void verifyPattern(uint64_t driveSize, Fd& fd);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070061
John Edward Broadbentd6071fc2022-03-31 19:33:21 -070062 private:
63 static constexpr uint32_t seed = 0x6a656272;
64 static constexpr size_t blockSize = 4096;
65 static constexpr size_t blockSizeUsing32 = blockSize / sizeof(uint32_t);
66 static constexpr size_t maxRetry = 32;
67 static constexpr std::chrono::duration delay = std::chrono::milliseconds(1);
John Edward Broadbent7f2ab642021-11-11 21:00:38 -080068};
John Edward Broadbentd3bfa7b2022-01-13 17:41:32 -080069
70} // namespace estoraged