blob: 26e39fb6a095d7b2650c3e43bdf57ee8039fa1df [file] [log] [blame]
John Edward Broadbent605085a2021-11-05 13:45:45 -07001#include "sanitize.hpp"
2
3#include "estoraged_conf.hpp"
4
5#include <linux/mmc/ioctl.h>
6#include <sys/ioctl.h>
7
8#include <phosphor-logging/lg2.hpp>
9#include <stdplus/fd/create.hpp>
10#include <stdplus/fd/managed.hpp>
11#include <stdplus/handle/managed.hpp>
12#include <xyz/openbmc_project/Common/error.hpp>
13
14#include <array>
15#include <cstddef>
16#include <span>
17#include <string>
18#include <string_view>
19
20namespace
21{
22
23constexpr uint32_t mmcSwitch = 6;
24constexpr uint32_t mmcSendExtCsd = 8;
25constexpr uint32_t mmcSwitchModeWriteByte = 0x03;
26constexpr uint32_t extCsdSanitizeStart = 165;
27constexpr uint32_t extCsdCmdSetNormal = (1 << 0);
28
29constexpr uint32_t mmcRspPresent = (1 << 0);
30constexpr uint32_t mmcRspCrc = (1 << 2);
31constexpr uint32_t mmcRspBusy = (1 << 3);
32constexpr uint32_t mmcRspOpcode = (1 << 4);
33
34constexpr uint32_t mmcCmdAc = (0 << 5);
35constexpr uint32_t mmcCmdAdtc = (1 << 5);
36
37constexpr uint32_t mmcRspSpiS1 = (1 << 7);
38constexpr uint32_t mmcRspSpiBusy = (1 << 10);
39
40constexpr uint32_t mmcRspSpiR1 = (mmcRspSpiS1);
41constexpr uint32_t mmcRspSpiR1B = (mmcRspSpiS1 | mmcRspSpiBusy);
42
43constexpr uint32_t mmcRspR1B =
44 (mmcRspPresent | mmcRspCrc | mmcRspOpcode | mmcRspBusy);
45
46constexpr uint32_t mmcRspR1 = (mmcRspPresent | mmcRspCrc | mmcRspOpcode);
47
48constexpr uint32_t mmcEraseGroupStart = 35;
49constexpr uint32_t mmcEraseGroupEnd = 36;
50constexpr uint32_t mmcErase = 38;
51} // namespace
52
53namespace estoraged
54{
55
56using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
57using stdplus::fd::ManagedFd;
58
59void Sanitize::doSanitize(uint64_t driveSize)
60{
61 try
62 {
63 emmcErase(driveSize);
64 emmcSanitize();
65 }
66 catch (...)
67 {
68 lg2::error("eStorageD erase sanitize failure", "REDFISH_MESSAGE_ID",
69 std::string("eStorageD.1.0.EraseFailure"));
70 throw InternalFailure();
71 }
72 lg2::info("eStorageD successfully erase sanitize", "REDFISH_MESSAGE_ID",
73 std::string("eStorageD.1.0.EraseSuccessful"));
74}
75
76void Sanitize::emmcErase(uint64_t driveSize)
77{
78
79 uint64_t sectorSize = 0x200; // default value see eMMC spec 6.6.34.
80 // NOTE: 0x200 is only valid for eMMC greater
81 // then 2 GB
82 struct mmc_io_multi_cmd_erase eraseCmd = {};
83
84 eraseCmd.num_of_cmds = 3;
85 eraseCmd.cmds[0].opcode = mmcEraseGroupStart;
86 eraseCmd.cmds[0].arg = 0;
87 eraseCmd.cmds[0].flags = mmcRspSpiR1 | mmcRspR1 | mmcCmdAc;
88 eraseCmd.cmds[0].write_flag = 1;
89
90 eraseCmd.cmds[1].opcode = mmcEraseGroupEnd;
91 eraseCmd.cmds[1].arg = (driveSize / sectorSize) - 1;
92 eraseCmd.cmds[1].flags = mmcRspSpiR1 | mmcRspR1 | mmcCmdAc;
93 eraseCmd.cmds[1].write_flag = 1;
94
95 /* Send Erase Command */
96 eraseCmd.cmds[2].opcode = mmcErase;
97 eraseCmd.cmds[2].arg = 0x00000000;
98 eraseCmd.cmds[2].cmd_timeout_ms = 0x0FFFFFFF;
99 eraseCmd.cmds[2].flags = mmcRspSpiR1B | mmcRspR1B | mmcCmdAc;
100 eraseCmd.cmds[2].write_flag = 1;
101
102 if (ioctlWrapper->doIoctlMulti(devPath, MMC_IOC_MULTI_CMD, eraseCmd) != 0)
103 {
104 throw InternalFailure();
105 }
106}
107
108void Sanitize::emmcSanitize()
109{
110
111 struct mmc_ioc_cmd idata = {};
112 idata.write_flag = 1;
113 idata.opcode = mmcSwitch;
114 idata.arg = (mmcSwitchModeWriteByte << 24) | (extCsdSanitizeStart << 16) |
115 (1 << 8) | extCsdCmdSetNormal;
116 idata.flags = mmcRspSpiR1B | mmcRspR1B | mmcCmdAc;
117
118 // make the eMMC sanitize ioctl
119 if (ioctlWrapper->doIoctl(devPath, MMC_IOC_CMD, idata) != 0)
120 {
121 throw InternalFailure();
122 }
123}
124
125int IOCTLWrapperImpl::doIoctl(std::string_view devPath, unsigned long request,
126 struct mmc_ioc_cmd data)
127
128{
129 ManagedFd fd = stdplus::fd::open(std::string(devPath).c_str(),
130 stdplus::fd::OpenAccess::ReadOnly);
131
132 return fd.ioctl(request, static_cast<void*>(&data));
133}
134
135int IOCTLWrapperImpl::doIoctlMulti(std::string_view devPath,
136 unsigned long request,
137 struct mmc_io_multi_cmd_erase data)
138{
139 ManagedFd fd = stdplus::fd::open(std::string(devPath).c_str(),
140 stdplus::fd::OpenAccess::ReadOnly);
141
142 return fd.ioctl(request, static_cast<void*>(&data));
143}
144
145} // namespace estoraged