| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 1 | |
| 2 | #include "estoraged.hpp" |
| 3 | |
| John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 4 | #include "cryptErase.hpp" |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 5 | #include "cryptsetupInterface.hpp" |
| Willy Tu | 30bfe8e | 2025-12-30 19:47:52 +0000 | [diff] [blame^] | 6 | #include "estoraged_conf.hpp" |
| John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 7 | #include "pattern.hpp" |
| John Edward Broadbent | 605085a | 2021-11-05 13:45:45 -0700 | [diff] [blame] | 8 | #include "sanitize.hpp" |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 9 | #include "verifyDriveGeometry.hpp" |
| John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 10 | #include "zero.hpp" |
| John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 11 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 12 | #include <libcryptsetup.h> |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 13 | #include <linux/mmc/core.h> |
| 14 | #include <linux/mmc/ioctl.h> |
| 15 | #include <linux/mmc/mmc.h> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 16 | #include <openssl/rand.h> |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 17 | #include <sys/ioctl.h> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 18 | |
| 19 | #include <phosphor-logging/lg2.hpp> |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 20 | #include <sdbusplus/asio/object_server.hpp> |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 21 | #include <stdplus/fd/create.hpp> |
| 22 | #include <stdplus/fd/managed.hpp> |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 23 | #include <xyz/openbmc_project/Common/error.hpp> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 24 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 25 | #include <cstdlib> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 26 | #include <filesystem> |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 27 | #include <iostream> |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 28 | #include <string> |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 29 | #include <string_view> |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 30 | #include <utility> |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| 33 | namespace estoraged |
| 34 | { |
| 35 | |
| John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 36 | using Association = std::tuple<std::string, std::string, std::string>; |
| John Wedig | 1755d1b | 2025-07-21 22:26:30 +0000 | [diff] [blame] | 37 | using sdbusplus::asio::PropertyPermission; |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 38 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 39 | using sdbusplus::xyz::openbmc_project::Common::Error::UnsupportedRequest; |
| John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 40 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive; |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 41 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume; |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 42 | |
| Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame] | 43 | EStoraged::EStoraged( |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 44 | std::unique_ptr<stdplus::Fd> fd, sdbusplus::asio::object_server& server, |
| 45 | const std::string& configPath, const std::string& devPath, |
| 46 | const std::string& luksName, uint64_t size, uint8_t lifeTime, |
| 47 | const std::string& partNumber, const std::string& serialNumber, |
| 48 | const std::string& locationCode, uint64_t eraseMaxGeometry, |
| 49 | uint64_t eraseMinGeometry, const std::string& driveType, |
| 50 | const std::string& driveProtocol, |
| Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame] | 51 | std::unique_ptr<CryptsetupInterface> cryptInterface, |
| 52 | std::unique_ptr<FilesystemInterface> fsInterface) : |
| 53 | devPath(devPath), containerName(luksName), |
| 54 | mountPoint("/mnt/" + luksName + "_fs"), eraseMaxGeometry(eraseMaxGeometry), |
| 55 | eraseMinGeometry(eraseMinGeometry), cryptIface(std::move(cryptInterface)), |
| 56 | fsIface(std::move(fsInterface)), |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 57 | cryptDevicePath(cryptIface->cryptGetDir() + "/" + luksName), |
| John Edward Broadbent | 6771c69 | 2022-06-22 19:49:27 -0700 | [diff] [blame] | 58 | objectServer(server) |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 59 | { |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 60 | try |
| 61 | { |
| Willy Tu | 30bfe8e | 2025-12-30 19:47:52 +0000 | [diff] [blame^] | 62 | changeHsTimingIfNeeded(fd.get(), devPath, partNumber); |
| 63 | lg2::info("Change HS_TIMING for {DEV} with {PARTNUMBER}", "DEV", |
| 64 | devPath, "PARTNUMBER", partNumber); |
| 65 | } |
| 66 | catch (const HsModeError& e) |
| 67 | { |
| 68 | lg2::error(e.what()); |
| 69 | } |
| 70 | |
| 71 | try |
| 72 | { |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 73 | enableBackgroundOperation(std::move(fd), devPath); |
| 74 | } |
| 75 | catch (const BkopsError& e) |
| 76 | { |
| 77 | lg2::error("Failed to enable background operation for {PATH}: {ERROR}", |
| 78 | "PATH", devPath, "ERROR", e.what()); |
| 79 | } |
| 80 | |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 81 | /* Get the filename of the device (without "/dev/"). */ |
| 82 | std::string deviceName = std::filesystem::path(devPath).filename().string(); |
| 83 | /* DBus object path */ |
| Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame] | 84 | std::string objectPath = |
| 85 | "/xyz/openbmc_project/inventory/storage/" + deviceName; |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 86 | |
| 87 | /* Add Volume interface. */ |
| 88 | volumeInterface = objectServer.add_interface( |
| John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 89 | objectPath, "xyz.openbmc_project.Inventory.Item.Volume"); |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 90 | volumeInterface->register_method( |
| 91 | "FormatLuks", [this](const std::vector<uint8_t>& password, |
| 92 | Volume::FilesystemType type) { |
| Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame] | 93 | this->formatLuks(password, type); |
| 94 | }); |
| Patrick Williams | ff1b64f | 2023-10-20 11:19:56 -0500 | [diff] [blame] | 95 | volumeInterface->register_method( |
| 96 | "Erase", |
| 97 | [this](Volume::EraseMethod eraseType) { this->erase(eraseType); }); |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 98 | volumeInterface->register_method("Lock", [this]() { this->lock(); }); |
| Patrick Williams | ff1b64f | 2023-10-20 11:19:56 -0500 | [diff] [blame] | 99 | volumeInterface->register_method( |
| 100 | "Unlock", |
| 101 | [this](std::vector<uint8_t>& password) { this->unlock(password); }); |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 102 | volumeInterface->register_method( |
| 103 | "ChangePassword", [this](const std::vector<uint8_t>& oldPassword, |
| 104 | const std::vector<uint8_t>& newPassword) { |
| Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame] | 105 | this->changePassword(oldPassword, newPassword); |
| 106 | }); |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 107 | volumeInterface->register_property_r( |
| 108 | "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change, |
| 109 | [this](bool& value) { |
| Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame] | 110 | value = this->isLocked(); |
| 111 | return value; |
| 112 | }); |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 113 | |
| 114 | /* Add Drive interface. */ |
| 115 | driveInterface = objectServer.add_interface( |
| John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 116 | objectPath, "xyz.openbmc_project.Inventory.Item.Drive"); |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 117 | driveInterface->register_property("Capacity", size); |
| John Wedig | 1755d1b | 2025-07-21 22:26:30 +0000 | [diff] [blame] | 118 | /* The lifetime property is read/write only for testing purposes. */ |
| 119 | driveInterface->register_property("PredictedMediaLifeLeftPercent", lifeTime, |
| 120 | PropertyPermission::readWrite); |
| John Wedig | d7be42b | 2024-01-19 16:07:19 -0800 | [diff] [blame] | 121 | driveInterface->register_property( |
| 122 | "Type", |
| 123 | "xyz.openbmc_project.Inventory.Item.Drive.DriveType." + driveType); |
| John Wedig | c0d66eb | 2024-02-26 15:54:47 -0800 | [diff] [blame] | 124 | driveInterface->register_property( |
| 125 | "Protocol", "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol." + |
| 126 | driveProtocol); |
| John Edward Broadbent | 14aee77 | 2022-04-20 13:46:48 -0700 | [diff] [blame] | 127 | /* This registers the Locked property for the Drives interface. |
| 128 | * Now it is the same as the volume Locked property */ |
| 129 | driveInterface->register_property_r( |
| 130 | "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change, |
| 131 | [this](bool& value) { |
| Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame] | 132 | value = this->isLocked(); |
| 133 | return value; |
| 134 | }); |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 135 | |
| John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 136 | driveInterface->register_property_r( |
| 137 | "EncryptionStatus", encryptionStatus, |
| 138 | sdbusplus::vtable::property_::emits_change, |
| 139 | [this](Drive::DriveEncryptionState& value) { |
| Patrick Williams | 15b63e1 | 2024-08-16 15:22:01 -0400 | [diff] [blame] | 140 | value = this->findEncryptionStatus(); |
| 141 | return value; |
| 142 | }); |
| John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 143 | |
| John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 144 | embeddedLocationInterface = objectServer.add_interface( |
| 145 | objectPath, "xyz.openbmc_project.Inventory.Connector.Embedded"); |
| John Edward Broadbent | 740e94b | 2022-06-10 19:42:30 -0700 | [diff] [blame] | 146 | |
| Rahul Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 147 | if (!locationCode.empty()) |
| 148 | { |
| 149 | locationCodeInterface = objectServer.add_interface( |
| 150 | objectPath, "xyz.openbmc_project.Inventory.Decorator.LocationCode"); |
| 151 | locationCodeInterface->register_property("LocationCode", locationCode); |
| 152 | locationCodeInterface->initialize(); |
| 153 | } |
| 154 | |
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 155 | /* Add Asset interface. */ |
| 156 | assetInterface = objectServer.add_interface( |
| 157 | objectPath, "xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 158 | assetInterface->register_property("PartNumber", partNumber); |
| 159 | assetInterface->register_property("SerialNumber", serialNumber); |
| 160 | |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 161 | volumeInterface->initialize(); |
| 162 | driveInterface->initialize(); |
| John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 163 | embeddedLocationInterface->initialize(); |
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 164 | assetInterface->initialize(); |
| John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 165 | |
| 166 | /* Set up the association between chassis and drive. */ |
| 167 | association = objectServer.add_interface( |
| 168 | objectPath, "xyz.openbmc_project.Association.Definitions"); |
| 169 | |
| 170 | std::vector<Association> associations; |
| 171 | associations.emplace_back("chassis", "drive", |
| 172 | std::filesystem::path(configPath).parent_path()); |
| 173 | association->register_property("Associations", associations); |
| 174 | association->initialize(); |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | EStoraged::~EStoraged() |
| 178 | { |
| 179 | objectServer.remove_interface(volumeInterface); |
| 180 | objectServer.remove_interface(driveInterface); |
| John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 181 | objectServer.remove_interface(embeddedLocationInterface); |
| John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 182 | objectServer.remove_interface(assetInterface); |
| John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 183 | objectServer.remove_interface(association); |
| Rahul Kapoor | 1982505 | 2023-05-27 01:52:23 +0000 | [diff] [blame] | 184 | |
| 185 | if (locationCodeInterface != nullptr) |
| 186 | { |
| 187 | objectServer.remove_interface(locationCodeInterface); |
| 188 | } |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void EStoraged::formatLuks(const std::vector<uint8_t>& password, |
| 192 | Volume::FilesystemType type) |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 193 | { |
| John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 194 | std::string msg = "OpenBMC.0.1.DriveFormat"; |
| 195 | lg2::info("Starting format", "REDFISH_MESSAGE_ID", msg); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 196 | |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 197 | if (type != Volume::FilesystemType::ext4) |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 198 | { |
| 199 | lg2::error("Only ext4 filesystems are supported currently", |
| 200 | "REDFISH_MESSAGE_ID", std::string("OpenBMC.0.1.FormatFail")); |
| 201 | throw UnsupportedRequest(); |
| 202 | } |
| 203 | |
| John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 204 | formatLuksDev(password); |
| 205 | activateLuksDev(password); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 206 | |
| 207 | createFilesystem(); |
| 208 | mountFilesystem(); |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 211 | void EStoraged::erase(Volume::EraseMethod inEraseMethod) |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 212 | { |
| 213 | std::cerr << "Erasing encrypted eMMC" << std::endl; |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 214 | lg2::info("Starting erase", "REDFISH_MESSAGE_ID", |
| 215 | std::string("OpenBMC.0.1.DriveErase")); |
| 216 | switch (inEraseMethod) |
| 217 | { |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 218 | case Volume::EraseMethod::CryptoErase: |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 219 | { |
| John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 220 | CryptErase myCryptErase(devPath); |
| 221 | myCryptErase.doErase(); |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 222 | break; |
| 223 | } |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 224 | case Volume::EraseMethod::VerifyGeometry: |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 225 | { |
| 226 | VerifyDriveGeometry myVerifyGeometry(devPath); |
| Tom Tung | 043af59 | 2023-11-24 13:37:05 +0800 | [diff] [blame] | 227 | myVerifyGeometry.geometryOkay(eraseMaxGeometry, eraseMinGeometry); |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 228 | break; |
| 229 | } |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 230 | case Volume::EraseMethod::LogicalOverWrite: |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 231 | { |
| John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 232 | Pattern myErasePattern(devPath); |
| John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 233 | myErasePattern.writePattern(); |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 234 | break; |
| 235 | } |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 236 | case Volume::EraseMethod::LogicalVerify: |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 237 | { |
| John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 238 | Pattern myErasePattern(devPath); |
| John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 239 | myErasePattern.verifyPattern(); |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 240 | break; |
| 241 | } |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 242 | case Volume::EraseMethod::VendorSanitize: |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 243 | { |
| John Edward Broadbent | 605085a | 2021-11-05 13:45:45 -0700 | [diff] [blame] | 244 | Sanitize mySanitize(devPath); |
| 245 | mySanitize.doSanitize(); |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 246 | break; |
| 247 | } |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 248 | case Volume::EraseMethod::ZeroOverWrite: |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 249 | { |
| John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 250 | Zero myZero(devPath); |
| John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 251 | myZero.writeZero(); |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 252 | break; |
| 253 | } |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 254 | case Volume::EraseMethod::ZeroVerify: |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 255 | { |
| John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 256 | Zero myZero(devPath); |
| John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 257 | myZero.verifyZero(); |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 258 | break; |
| 259 | } |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 260 | case Volume::EraseMethod::SecuredLocked: |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 261 | { |
| John Wedig | 47cd799 | 2022-10-05 15:45:11 -0700 | [diff] [blame] | 262 | if (!isLocked()) |
| John Edward Broadbent | f59b729 | 2022-02-15 15:07:15 -0800 | [diff] [blame] | 263 | { |
| 264 | lock(); |
| 265 | } |
| 266 | // TODO: implement hardware locking |
| 267 | // Until that is done, we can lock using eStoraged::lock() |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 268 | break; |
| 269 | } |
| 270 | } |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 273 | void EStoraged::lock() |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 274 | { |
| John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 275 | std::string msg = "OpenBMC.0.1.DriveLock"; |
| 276 | lg2::info("Starting lock", "REDFISH_MESSAGE_ID", msg); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 277 | |
| 278 | unmountFilesystem(); |
| 279 | deactivateLuksDev(); |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 282 | void EStoraged::unlock(std::vector<uint8_t> password) |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 283 | { |
| John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 284 | std::string msg = "OpenBMC.0.1.DriveUnlock"; |
| 285 | lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 286 | |
| John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 287 | activateLuksDev(std::move(password)); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 288 | mountFilesystem(); |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame] | 291 | void EStoraged::changePassword(const std::vector<uint8_t>& oldPassword, |
| 292 | const std::vector<uint8_t>& newPassword) |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 293 | { |
| John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 294 | lg2::info("Starting change password", "REDFISH_MESSAGE_ID", |
| 295 | std::string("OpenBMC.0.1.DrivePasswordChanged")); |
| John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame] | 296 | |
| 297 | CryptHandle cryptHandle = loadLuksHeader(); |
| 298 | |
| 299 | int retval = cryptIface->cryptKeyslotChangeByPassphrase( |
| 300 | cryptHandle.get(), CRYPT_ANY_SLOT, CRYPT_ANY_SLOT, |
| 301 | reinterpret_cast<const char*>(oldPassword.data()), oldPassword.size(), |
| 302 | reinterpret_cast<const char*>(newPassword.data()), newPassword.size()); |
| 303 | if (retval < 0) |
| 304 | { |
| 305 | lg2::error("Failed to change password", "REDFISH_MESSAGE_ID", |
| 306 | std::string("OpenBMC.0.1.DrivePasswordChangeFail")); |
| 307 | throw InternalFailure(); |
| 308 | } |
| 309 | |
| 310 | lg2::info("Successfully changed password for {DEV}", "DEV", devPath, |
| 311 | "REDFISH_MESSAGE_ID", |
| 312 | std::string("OpenBMC.0.1.DrivePasswordChangeSuccess")); |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 315 | bool EStoraged::isLocked() const |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 316 | { |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 317 | /* |
| 318 | * Check if the mapped virtual device exists. If it exists, the LUKS volume |
| 319 | * is unlocked. |
| 320 | */ |
| 321 | try |
| 322 | { |
| 323 | std::filesystem::path mappedDevicePath(cryptDevicePath); |
| 324 | return (std::filesystem::exists(mappedDevicePath) == false); |
| 325 | } |
| 326 | catch (const std::exception& e) |
| 327 | { |
| 328 | lg2::error("Failed to query locked status: {EXCEPT}", "EXCEPT", |
| 329 | e.what(), "REDFISH_MESSAGE_ID", |
| 330 | std::string("OpenBMC.0.1.IsLockedFail")); |
| 331 | /* If we couldn't query the filesystem path, assume unlocked. */ |
| 332 | return false; |
| 333 | } |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 334 | } |
| 335 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 336 | std::string_view EStoraged::getMountPoint() const |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 337 | { |
| 338 | return mountPoint; |
| 339 | } |
| 340 | |
| John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 341 | void EStoraged::formatLuksDev(std::vector<uint8_t> password) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 342 | { |
| 343 | lg2::info("Formatting device {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID", |
| 344 | std::string("OpenBMC.0.1.FormatLuksDev")); |
| 345 | |
| 346 | /* Generate the volume key. */ |
| 347 | const std::size_t keySize = 64; |
| 348 | std::vector<uint8_t> volumeKey(keySize); |
| 349 | if (RAND_bytes(volumeKey.data(), keySize) != 1) |
| 350 | { |
| 351 | lg2::error("Failed to create volume key", "REDFISH_MESSAGE_ID", |
| 352 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 353 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 354 | } |
| John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 355 | |
| 356 | /* Create the handle. */ |
| 357 | CryptHandle cryptHandle(devPath); |
| 358 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 359 | /* Format the LUKS encrypted device. */ |
| John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 360 | int retval = cryptIface->cryptFormat( |
| 361 | cryptHandle.get(), CRYPT_LUKS2, "aes", "xts-plain64", nullptr, |
| 362 | reinterpret_cast<const char*>(volumeKey.data()), volumeKey.size(), |
| 363 | nullptr); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 364 | if (retval < 0) |
| 365 | { |
| 366 | lg2::error("Failed to format encrypted device: {RETVAL}", "RETVAL", |
| 367 | retval, "REDFISH_MESSAGE_ID", |
| 368 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 369 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 372 | /* Set the password. */ |
| 373 | retval = cryptIface->cryptKeyslotAddByVolumeKey( |
| John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 374 | cryptHandle.get(), CRYPT_ANY_SLOT, nullptr, 0, |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 375 | reinterpret_cast<const char*>(password.data()), password.size()); |
| 376 | |
| 377 | if (retval < 0) |
| 378 | { |
| 379 | lg2::error("Failed to set encryption password", "REDFISH_MESSAGE_ID", |
| 380 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 381 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | lg2::info("Encrypted device {DEV} successfully formatted", "DEV", devPath, |
| 385 | "REDFISH_MESSAGE_ID", |
| 386 | std::string("OpenBMC.0.1.FormatLuksDevSuccess")); |
| 387 | } |
| 388 | |
| John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 389 | CryptHandle EStoraged::loadLuksHeader() |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 390 | { |
| John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 391 | CryptHandle cryptHandle(devPath); |
| 392 | |
| 393 | int retval = cryptIface->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 394 | if (retval < 0) |
| 395 | { |
| 396 | lg2::error("Failed to load LUKS header: {RETVAL}", "RETVAL", retval, |
| 397 | "REDFISH_MESSAGE_ID", |
| 398 | std::string("OpenBMC.0.1.ActivateLuksDevFail")); |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 399 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 400 | } |
| John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 401 | return cryptHandle; |
| 402 | } |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 403 | |
| John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 404 | Drive::DriveEncryptionState EStoraged::findEncryptionStatus() |
| 405 | { |
| 406 | try |
| 407 | { |
| 408 | loadLuksHeader(); |
| 409 | return Drive::DriveEncryptionState::Encrypted; |
| 410 | } |
| 411 | catch (...) |
| 412 | { |
| Hao Zhou | 0cec428 | 2024-03-12 22:16:16 +0000 | [diff] [blame] | 413 | return Drive::DriveEncryptionState::Unencrypted; |
| John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | void EStoraged::activateLuksDev(std::vector<uint8_t> password) |
| 418 | { |
| 419 | lg2::info("Activating LUKS dev {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID", |
| 420 | std::string("OpenBMC.0.1.ActivateLuksDev")); |
| 421 | |
| 422 | /* Create the handle. */ |
| 423 | CryptHandle cryptHandle = loadLuksHeader(); |
| 424 | |
| 425 | int retval = cryptIface->cryptActivateByPassphrase( |
| John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 426 | cryptHandle.get(), containerName.c_str(), CRYPT_ANY_SLOT, |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 427 | reinterpret_cast<const char*>(password.data()), password.size(), |
| 428 | CRYPT_ACTIVATE_ALLOW_DISCARDS); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 429 | |
| 430 | if (retval < 0) |
| 431 | { |
| 432 | lg2::error("Failed to activate LUKS dev: {RETVAL}", "RETVAL", retval, |
| 433 | "REDFISH_MESSAGE_ID", |
| 434 | std::string("OpenBMC.0.1.ActivateLuksDevFail")); |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 435 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 438 | lg2::info("Successfully activated LUKS dev {DEV}", "DEV", devPath, |
| 439 | "REDFISH_MESSAGE_ID", |
| 440 | std::string("OpenBMC.0.1.ActivateLuksDevSuccess")); |
| 441 | } |
| 442 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 443 | void EStoraged::createFilesystem() |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 444 | { |
| 445 | /* Run the command to create the filesystem. */ |
| Rom Lemarchand | 530ce1e | 2025-12-16 21:45:08 +0000 | [diff] [blame] | 446 | int retval = fsIface->runMkfs(cryptDevicePath, {"-E", "discard"}); |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 447 | if (retval != 0) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 448 | { |
| 449 | lg2::error("Failed to create filesystem: {RETVAL}", "RETVAL", retval, |
| 450 | "REDFISH_MESSAGE_ID", |
| 451 | std::string("OpenBMC.0.1.CreateFilesystemFail")); |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 452 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 453 | } |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 454 | lg2::info("Successfully created filesystem for {CONTAINER}", "CONTAINER", |
| 455 | cryptDevicePath, "REDFISH_MESSAGE_ID", |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 456 | std::string("OpenBMC.0.1.CreateFilesystemSuccess")); |
| 457 | } |
| 458 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 459 | void EStoraged::mountFilesystem() |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 460 | { |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 461 | /* |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame] | 462 | * Before mounting, run fsck to check for and resolve any filesystem errors. |
| 463 | */ |
| 464 | int retval = fsIface->runFsck(cryptDevicePath, "-t ext4 -p"); |
| 465 | if (retval != 0) |
| 466 | { |
| 467 | lg2::error("The fsck command failed: {RETVAL}", "RETVAL", retval, |
| 468 | "REDFISH_MESSAGE_ID", |
| 469 | std::string("OpenBMC.0.1.FixFilesystemFail")); |
| 470 | /* We'll still try to mount the filesystem, though. */ |
| 471 | } |
| 472 | |
| 473 | /* |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 474 | * Create directory for the filesystem, if it's not already present. It |
| 475 | * might already exist if, for example, the BMC reboots after creating the |
| 476 | * directory. |
| 477 | */ |
| 478 | if (!fsIface->directoryExists(std::filesystem::path(mountPoint))) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 479 | { |
| John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 480 | bool success = |
| 481 | fsIface->createDirectory(std::filesystem::path(mountPoint)); |
| 482 | if (!success) |
| 483 | { |
| 484 | lg2::error("Failed to create mount point: {DIR}", "DIR", mountPoint, |
| 485 | "REDFISH_MESSAGE_ID", |
| 486 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 487 | throw InternalFailure(); |
| 488 | } |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* Run the command to mount the filesystem. */ |
| John Wedig | 1d6665f | 2025-12-04 19:08:06 +0000 | [diff] [blame] | 492 | retval = fsIface->doMount(cryptDevicePath.c_str(), mountPoint.c_str(), |
| 493 | "ext4", 0, nullptr); |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 494 | if (retval != 0) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 495 | { |
| 496 | lg2::error("Failed to mount filesystem: {RETVAL}", "RETVAL", retval, |
| 497 | "REDFISH_MESSAGE_ID", |
| 498 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 499 | bool removeSuccess = |
| 500 | fsIface->removeDirectory(std::filesystem::path(mountPoint)); |
| 501 | if (!removeSuccess) |
| 502 | { |
| 503 | lg2::error("Failed to remove mount point: {DIR}", "DIR", mountPoint, |
| 504 | "REDFISH_MESSAGE_ID", |
| 505 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 506 | } |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 507 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | lg2::info("Successfully mounted filesystem at {DIR}", "DIR", mountPoint, |
| 511 | "REDFISH_MESSAGE_ID", |
| 512 | std::string("OpenBMC.0.1.MountFilesystemSuccess")); |
| 513 | } |
| 514 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 515 | void EStoraged::unmountFilesystem() |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 516 | { |
| 517 | int retval = fsIface->doUnmount(mountPoint.c_str()); |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 518 | if (retval != 0) |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 519 | { |
| 520 | lg2::error("Failed to unmount filesystem: {RETVAL}", "RETVAL", retval, |
| 521 | "REDFISH_MESSAGE_ID", |
| 522 | std::string("OpenBMC.0.1.UnmountFilesystemFail")); |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 523 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /* Remove the mount point. */ |
| 527 | bool success = fsIface->removeDirectory(std::filesystem::path(mountPoint)); |
| 528 | if (!success) |
| 529 | { |
| 530 | lg2::error("Failed to remove mount point {DIR}", "DIR", mountPoint, |
| 531 | "REDFISH_MESSAGE_ID", |
| 532 | std::string("OpenBMC.0.1.UnmountFilesystemFail")); |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 533 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | lg2::info("Successfully unmounted filesystem at {DIR}", "DIR", mountPoint, |
| 537 | "REDFISH_MESSAGE_ID", |
| 538 | std::string("OpenBMC.0.1.MountFilesystemSuccess")); |
| 539 | } |
| 540 | |
| Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 541 | void EStoraged::deactivateLuksDev() |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 542 | { |
| 543 | lg2::info("Deactivating LUKS device {DEV}", "DEV", devPath, |
| 544 | "REDFISH_MESSAGE_ID", |
| 545 | std::string("OpenBMC.0.1.DeactivateLuksDev")); |
| 546 | |
| 547 | int retval = cryptIface->cryptDeactivate(nullptr, containerName.c_str()); |
| 548 | if (retval < 0) |
| 549 | { |
| 550 | lg2::error("Failed to deactivate crypt device: {RETVAL}", "RETVAL", |
| 551 | retval, "REDFISH_MESSAGE_ID", |
| 552 | std::string("OpenBMC.0.1.DeactivateLuksDevFail")); |
| John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 553 | throw InternalFailure(); |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 554 | } |
| 555 | |
| John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 556 | lg2::info("Successfully deactivated LUKS device {DEV}", "DEV", devPath, |
| 557 | "REDFISH_MESSAGE_ID", |
| 558 | std::string("OpenBMC.0.1.DeactivateLuksDevSuccess")); |
| 559 | } |
| 560 | |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 561 | std::string_view EStoraged::getCryptDevicePath() const |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 562 | { |
| John Wedig | 2443a02 | 2023-03-17 13:42:32 -0700 | [diff] [blame] | 563 | return cryptDevicePath; |
| John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| Willy Tu | da5aa61 | 2025-12-18 07:32:34 +0000 | [diff] [blame] | 566 | bool EStoraged::enableBackgroundOperation(std::unique_ptr<stdplus::Fd> fd, |
| 567 | std::string_view devPath) |
| 568 | { |
| 569 | struct mmc_ioc_cmd idata{}; |
| 570 | memset(&idata, 0, sizeof(idata)); |
| 571 | // Extended Device Specific Data. Contains information about the Device |
| 572 | // capabilities and selected modes. |
| 573 | std::array<uint8_t, /*EXT_CSD*/ 512> extCsd{}; |
| 574 | idata.write_flag = 0; |
| 575 | idata.opcode = MMC_SEND_EXT_CSD; |
| 576 | idata.arg = 0; |
| 577 | idata.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 578 | idata.blksz = extCsd.size(); |
| 579 | idata.blocks = 1; |
| 580 | mmc_ioc_cmd_set_data(idata, extCsd.data()); |
| 581 | if (fd->ioctl(MMC_IOC_CMD, &idata) != 0) |
| 582 | { |
| 583 | throw BkopsIoctlFailure(devPath, |
| 584 | "Failed to get Extended Device Specific Data"); |
| 585 | } |
| 586 | |
| 587 | if ((extCsd[EXT_CSD_BKOPS_SUPPORT] & 0x1) == 0) |
| 588 | { |
| 589 | lg2::info("BKOPS is not supported for {DEV}", "DEV", devPath); |
| 590 | return false; |
| 591 | } |
| 592 | lg2::info("BKOPS is supported for {DEV}", "DEV", devPath); |
| 593 | |
| 594 | if ((extCsd[EXT_CSD_BKOPS_EN] & |
| 595 | (EXT_CSD_MANUAL_BKOPS_MASK | EXT_CSD_AUTO_BKOPS_MASK)) != 0) |
| 596 | { |
| 597 | lg2::info("BKOPS is already enabled for {DEV}: Mode: {MODE}", "DEV", |
| 598 | devPath, "MODE", extCsd[EXT_CSD_BKOPS_EN]); |
| 599 | return false; |
| 600 | } |
| 601 | |
| 602 | // Clear the input data. |
| 603 | memset(&idata, 0, sizeof(idata)); |
| 604 | idata.write_flag = 1; |
| 605 | idata.opcode = MMC_SWITCH; |
| 606 | idata.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | (EXT_CSD_BKOPS_EN << 16) | |
| 607 | (EXT_CSD_MANUAL_BKOPS_MASK << 8) | EXT_CSD_CMD_SET_NORMAL; |
| 608 | idata.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 609 | if (fd->ioctl(MMC_IOC_CMD, &idata) != 0) |
| 610 | { |
| 611 | throw BkopsEnableFailure(devPath); |
| 612 | } |
| 613 | |
| 614 | lg2::info("Successfully enable BKOPS for {DEV}", "DEV", devPath); |
| 615 | return true; |
| 616 | } |
| 617 | |
| Willy Tu | 30bfe8e | 2025-12-30 19:47:52 +0000 | [diff] [blame^] | 618 | bool EStoraged::changeHsTiming(stdplus::Fd* fd, std::string_view devPath) |
| 619 | { |
| 620 | if (fd == nullptr) |
| 621 | { |
| 622 | return false; |
| 623 | } |
| 624 | struct mmc_ioc_cmd cmd = {}; |
| 625 | cmd.write_flag = 1; |
| 626 | cmd.opcode = MMC_SWITCH; |
| 627 | cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | (EXT_CSD_HS_TIMING << 16) | |
| 628 | (EXT_CSD_TIMING_HS400 << 8); |
| 629 | cmd.flags = MMC_RSP_R1B | MMC_CMD_AC; |
| 630 | if (fd->ioctl(MMC_IOC_CMD, &cmd) != 0) |
| 631 | { |
| 632 | throw HsModeError(devPath); |
| 633 | } |
| 634 | return true; |
| 635 | } |
| 636 | |
| 637 | bool EStoraged::changeHsTimingIfNeeded( |
| 638 | stdplus::Fd* fd, std::string_view devPath, std::string_view partNumber) |
| 639 | { |
| 640 | if (fd == nullptr) |
| 641 | { |
| 642 | return false; |
| 643 | } |
| 644 | if (std::ranges::contains(highSpeedMMC, partNumber)) |
| 645 | { |
| 646 | return changeHsTiming(fd, devPath); |
| 647 | } |
| 648 | return false; |
| 649 | } |
| 650 | |
| John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 651 | } // namespace estoraged |