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