| #include "sanitize.hpp" |
| |
| #include "estoraged_conf.hpp" |
| |
| #include <linux/mmc/ioctl.h> |
| #include <sys/ioctl.h> |
| |
| #include <phosphor-logging/lg2.hpp> |
| #include <stdplus/fd/create.hpp> |
| #include <stdplus/fd/managed.hpp> |
| #include <stdplus/handle/managed.hpp> |
| #include <xyz/openbmc_project/Common/error.hpp> |
| |
| #include <array> |
| #include <cstddef> |
| #include <span> |
| #include <string> |
| #include <string_view> |
| |
| namespace |
| { |
| |
| constexpr uint32_t mmcSwitch = 6; |
| constexpr uint32_t mmcSwitchModeWriteByte = 0x03; |
| constexpr uint32_t extCsdSanitizeStart = 165; |
| constexpr uint32_t extCsdCmdSetNormal = (1 << 0); |
| |
| constexpr uint32_t mmcRspPresent = (1 << 0); |
| constexpr uint32_t mmcRspCrc = (1 << 2); |
| constexpr uint32_t mmcRspBusy = (1 << 3); |
| constexpr uint32_t mmcRspOpcode = (1 << 4); |
| |
| constexpr uint32_t mmcCmdAc = (0 << 5); |
| |
| constexpr uint32_t mmcRspSpiS1 = (1 << 7); |
| constexpr uint32_t mmcRspSpiBusy = (1 << 10); |
| |
| constexpr uint32_t mmcRspSpiR1 = (mmcRspSpiS1); |
| constexpr uint32_t mmcRspSpiR1B = (mmcRspSpiS1 | mmcRspSpiBusy); |
| |
| constexpr uint32_t mmcRspR1B = |
| (mmcRspPresent | mmcRspCrc | mmcRspOpcode | mmcRspBusy); |
| |
| constexpr uint32_t mmcRspR1 = (mmcRspPresent | mmcRspCrc | mmcRspOpcode); |
| |
| constexpr uint32_t mmcEraseGroupStart = 35; |
| constexpr uint32_t mmcEraseGroupEnd = 36; |
| constexpr uint32_t mmcErase = 38; |
| } // namespace |
| |
| namespace estoraged |
| { |
| |
| using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| using stdplus::fd::ManagedFd; |
| |
| void Sanitize::doSanitize(uint64_t driveSize) |
| { |
| try |
| { |
| emmcErase(driveSize); |
| emmcSanitize(); |
| } |
| catch (...) |
| { |
| lg2::error("eStorageD erase sanitize failure", "REDFISH_MESSAGE_ID", |
| std::string("eStorageD.1.0.EraseFailure")); |
| throw InternalFailure(); |
| } |
| lg2::info("eStorageD successfully erase sanitize", "REDFISH_MESSAGE_ID", |
| std::string("eStorageD.1.0.EraseSuccessful")); |
| } |
| |
| void Sanitize::emmcErase(uint64_t driveSize) |
| { |
| uint64_t sectorSize = 0x200; // default value see eMMC spec 6.6.34. |
| // NOTE: 0x200 is only valid for eMMC greater |
| // then 2 GB |
| struct mmc_io_multi_cmd_erase eraseCmd = {}; |
| |
| eraseCmd.num_of_cmds = 3; |
| eraseCmd.cmds[0].opcode = mmcEraseGroupStart; |
| eraseCmd.cmds[0].arg = 0; |
| eraseCmd.cmds[0].flags = mmcRspSpiR1 | mmcRspR1 | mmcCmdAc; |
| eraseCmd.cmds[0].write_flag = 1; |
| |
| eraseCmd.cmds[1].opcode = mmcEraseGroupEnd; |
| eraseCmd.cmds[1].arg = (driveSize / sectorSize) - 1; |
| eraseCmd.cmds[1].flags = mmcRspSpiR1 | mmcRspR1 | mmcCmdAc; |
| eraseCmd.cmds[1].write_flag = 1; |
| |
| /* Send Erase Command */ |
| eraseCmd.cmds[2].opcode = mmcErase; |
| eraseCmd.cmds[2].arg = 0x00000000; |
| eraseCmd.cmds[2].cmd_timeout_ms = 0x0FFFFFFF; |
| eraseCmd.cmds[2].flags = mmcRspSpiR1B | mmcRspR1B | mmcCmdAc; |
| eraseCmd.cmds[2].write_flag = 1; |
| |
| if (ioctlWrapper->doIoctlMulti(devPath, MMC_IOC_MULTI_CMD, eraseCmd) != 0) |
| { |
| throw InternalFailure(); |
| } |
| } |
| |
| void Sanitize::emmcSanitize() |
| { |
| struct mmc_ioc_cmd idata = {}; |
| idata.write_flag = 1; |
| idata.opcode = mmcSwitch; |
| idata.arg = (mmcSwitchModeWriteByte << 24) | (extCsdSanitizeStart << 16) | |
| (1 << 8) | extCsdCmdSetNormal; |
| idata.flags = mmcRspSpiR1B | mmcRspR1B | mmcCmdAc; |
| |
| // make the eMMC sanitize ioctl |
| if (ioctlWrapper->doIoctl(devPath, MMC_IOC_CMD, idata) != 0) |
| { |
| throw InternalFailure(); |
| } |
| } |
| |
| int IOCTLWrapperImpl::doIoctl(std::string_view devPath, unsigned long request, |
| struct mmc_ioc_cmd data) |
| |
| { |
| ManagedFd fd = stdplus::fd::open(std::string(devPath).c_str(), |
| stdplus::fd::OpenAccess::ReadOnly); |
| |
| return fd.ioctl(request, static_cast<void*>(&data)); |
| } |
| |
| int IOCTLWrapperImpl::doIoctlMulti(std::string_view devPath, |
| unsigned long request, |
| struct mmc_io_multi_cmd_erase data) |
| { |
| ManagedFd fd = stdplus::fd::open(std::string(devPath).c_str(), |
| stdplus::fd::OpenAccess::ReadOnly); |
| |
| return fd.ioctl(request, static_cast<void*>(&data)); |
| } |
| |
| } // namespace estoraged |