summaryrefslogtreecommitdiff
path: root/meta-ibs/meta-cp2-5422/recipes-phosphor/virtual-media/virtual-media/0014-virtual-media.1-Upd-Use-usb-ctrl-instead-of-sysfs-.-.patch
blob: d52a8d4afd28b87fad504f8921d1bc1c7a0e4dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
From c367cf3c1b092ad00ee4865b2f470169ef65c74e Mon Sep 17 00:00:00 2001
From: Alexandr Ilenko <AIlenko@IBS.RU>
Date: Mon, 20 Jun 2022 18:36:59 +0300
Subject: [PATCH 14/20] virtual-media.1: Upd: Use "usb-ctrl" instead of
 "sysfs". Add: machine.target.type

---
 src/interfaces/mount_point_state_machine.hpp |   1 +
 src/resources.cpp                            |   2 +
 src/state/initial_state.hpp                  |  20 +++-
 src/system.hpp                               | 120 ++++++++++---------
 4 files changed, 86 insertions(+), 57 deletions(-)

diff --git a/src/interfaces/mount_point_state_machine.hpp b/src/interfaces/mount_point_state_machine.hpp
index c4b4391..b023b63 100644
--- a/src/interfaces/mount_point_state_machine.hpp
+++ b/src/interfaces/mount_point_state_machine.hpp
@@ -16,6 +16,7 @@ struct MountPointStateMachine
     {
         std::string imgUrl;
         bool rw;
+        UsbGadget::InterfaceType type;
         std::unique_ptr<resource::Mount> mountPoint;
         std::unique_ptr<utils::CredentialsProvider> credentials;
     };
diff --git a/src/resources.cpp b/src/resources.cpp
index a501bba..950f698 100644
--- a/src/resources.cpp
+++ b/src/resources.cpp
@@ -23,6 +23,8 @@ Gadget::Gadget(interfaces::MountPointStateMachine& machine,
 {
     status = UsbGadget::configure(
         std::string(machine.getName()), machine.getConfig().nbdDevice, devState,
+        machine.getTarget() ? machine.getTarget()->type
+                            : UsbGadget::interfaceTypeUSB,
         machine.getTarget() ? machine.getTarget()->rw : false);
 }
 
diff --git a/src/state/initial_state.hpp b/src/state/initial_state.hpp
index 0e8876a..e0d8a5d 100644
--- a/src/state/initial_state.hpp
+++ b/src/state/initial_state.hpp
@@ -153,6 +153,21 @@ struct InitialState : public BasicStateT<InitialState>
                                  machine.getConfig().nbdDevice.to_string());
         iface->register_property("EndpointId", machine.getConfig().endPointId);
         iface->register_property("Socket", machine.getConfig().unixSocket);
+        iface->register_property(
+            "InterfaceType", static_cast<uint8_t>(UsbGadget::interfaceTypeUSB),
+            [](uint8_t, uint8_t) -> bool {
+                throw sdbusplus::exception::SdBusError(
+                    EPERM, "Setting InterfaceType property is not allowed");
+                return false;
+            },
+            [&target = machine.getTarget()](
+                uint8_t) -> uint8_t {
+                if (!target.has_value())
+                {
+                    return UsbGadget::interfaceTypeUSB;
+                }
+                return target->type;
+            });
         iface->register_property(
             "ImageURL", std::string(),
             []([[maybe_unused]] const std::string& req,
@@ -234,12 +249,15 @@ struct InitialState : public BasicStateT<InitialState>
             iface->register_method(
                 "Mount", [&machine = machine](boost::asio::yield_context yield,
                                               std::string imgUrl, bool rw,
+                                              uint8_t type,
                                               optional_fd fd) {
                     LogMsg(Logger::Info, "[App]: Mount called on ",
                            getObjectPath(machine), machine.getName());
 
                     interfaces::MountPointStateMachine::Target target = {
-                        imgUrl, rw, nullptr, nullptr};
+                        imgUrl, rw,
+                        static_cast<UsbGadget::InterfaceType>(type),
+                        nullptr, nullptr};
 
                     if (std::holds_alternative<unix_fd>(fd))
                     {
diff --git a/src/system.hpp b/src/system.hpp
index d0af549..5dfb4df 100644
--- a/src/system.hpp
+++ b/src/system.hpp
@@ -498,24 +498,48 @@ struct UsbGadget : private FsHelper
     static const std::string getGadgetDirPrefix()
     {
         const std::string gadgetDirPrefix =
-            "/sys/kernel/config/usb_gadget/mass-storage-";
+            "/sys/kernel/config/usb_gadget/";
         return gadgetDirPrefix;
     }
 
   public:
+    enum InterfaceType : uint8_t
+    {
+        interfaceTypeUSB = 0,
+        interfaceTypeHDD = 1,
+        interfaceTypeCDROM = 2,
+        unknown = 0xFF
+    };
+
     static int32_t configure(const std::string& name, const NBDDevice& nbd,
-                             StateChange change, const bool rw = false)
+                             StateChange change,
+                             InterfaceType interfaceType = interfaceTypeUSB,
+                             const bool rw = false)
     {
-        return configure(name, nbd.to_path(), change, rw);
+        return configure(name, nbd.to_path(), change, interfaceType, rw);
     }
 
     static int32_t configure(const std::string& name, const fs::path& path,
-                             StateChange change, const bool rw = false)
+                             StateChange change,
+                             InterfaceType interfaceType = interfaceTypeUSB,
+                             const bool rw = false)
     {
         LogMsg(Logger::Info, "[App]: Configure USB Gadget (name=", name,
                ", path=", path, ", State=", static_cast<uint32_t>(change), ")");
         bool success = true;
-        std::error_code ec;
+        static const std::map<uint8_t, std::string> vmInterfaces {
+            {interfaceTypeUSB, "usb"},
+            {interfaceTypeHDD, "hdd"},
+            {interfaceTypeCDROM, "cdrom"},
+        };
+        auto interfaceIt = vmInterfaces.find(interfaceType);
+        if (interfaceIt == vmInterfaces.end())
+        {
+            LogMsg(Logger::Critical,
+                   "[App]: Spiecifed unknown interface type: ", interfaceType);
+            return -1;
+        }
+
         if (change == StateChange::unknown)
         {
             LogMsg(Logger::Critical,
@@ -523,50 +547,35 @@ struct UsbGadget : private FsHelper
             return -1;
         }
 
-        const fs::path gadgetDir = getGadgetDirPrefix() + name;
-        const fs::path funcMassStorageDir =
-            gadgetDir / "functions/mass_storage.usb0";
-        const fs::path stringsDir = gadgetDir / "strings/0x409";
-        const fs::path configDir = gadgetDir / "configs/c.1";
-        const fs::path massStorageDir = configDir / "mass_storage.usb0";
-        const fs::path configStringsDir = configDir / "strings/0x409";
-
         if (change == StateChange::inserted)
         {
+            const std::string roConfPath = "/sys/kernel/config/usb_gadget/" +
+                                           name + "/functions/mass_storage." +
+                                           name + "/lun.0/ro";
+            // /usr/bin/usb-ctrl insert <name> <file> [<type=usb|usb-ro|hdd|cdrom>]
             try
             {
-                fs::create_directories(gadgetDir);
-                echoToFile(gadgetDir / "idVendor", "0x1d6b");
-                echoToFile(gadgetDir / "idProduct", "0x0104");
-                fs::create_directories(stringsDir);
-                echoToFile(stringsDir / "manufacturer", "OpenBMC");
-                echoToFile(stringsDir / "product", "Virtual Media Device");
-                fs::create_directories(configStringsDir);
-                echoToFile(configStringsDir / "configuration", "config 1");
-                fs::create_directories(funcMassStorageDir);
-                fs::create_directories(funcMassStorageDir / "lun.0");
-                fs::create_directory_symlink(funcMassStorageDir,
-                                             massStorageDir);
-                echoToFile(funcMassStorageDir / "lun.0/removable", "1");
-                echoToFile(funcMassStorageDir / "lun.0/ro", rw ? "0" : "1");
-                echoToFile(funcMassStorageDir / "lun.0/cdrom", "0");
-                echoToFile(funcMassStorageDir / "lun.0/file", path);
-
-                for (const auto& port : fs::directory_iterator(
-                         "/sys/bus/platform/devices/1e6a0000.usb-vhub"))
+                std::vector<std::string> args{
+                    "insert",
+                    name,
+                    path,
+                    interfaceIt->second,
+                };
+                if (!rw)
                 {
-                    if (fs::is_directory(port) && !fs::is_symlink(port) &&
-                        !fs::exists(port.path() / "gadget/suspended"))
-                    {
-                        const std::string portId = port.path().filename();
-                        LogMsg(Logger::Debug,
-                               "Use port : ", port.path().filename());
-                        echoToFile(gadgetDir / "UDC", portId);
-                        return 0;
-                    }
+                    args.emplace_back("readonly");
                 }
+                auto child = boost::process::child(
+                    "/usr/bin/usb-ctrl", boost::process::args(args));
+                child.wait();
+
+                LogMsg(Logger::Debug, "Success of 'usb-ctrl insert ",
+                       name.c_str(), " ", path.c_str(), " ",
+                       interfaceIt->second.c_str(),
+                       "': ", std::strerror(child.exit_code()));
+                return 0;
             }
-            catch (fs::filesystem_error& e)
+            catch (boost::process::process_error& e)
             {
                 // Got error perform cleanup
                 LogMsg(Logger::Error, "[App]: UsbGadget: ", e.what());
@@ -574,7 +583,6 @@ struct UsbGadget : private FsHelper
             }
             catch (std::ofstream::failure& e)
             {
-                // Got error perform cleanup
                 LogMsg(Logger::Error, "[App]: UsbGadget: ", e.what());
                 success = false;
             }
@@ -582,19 +590,18 @@ struct UsbGadget : private FsHelper
         // StateChange: unknown, notMonitored, inserted were handler
         // earlier. We'll get here only for removed, or cleanup
 
-        echoToFile(gadgetDir / "UDC", "");
-        const std::array<const char*, 6> dirs = {
-            massStorageDir.c_str(),   funcMassStorageDir.c_str(),
-            configStringsDir.c_str(), configDir.c_str(),
-            stringsDir.c_str(),       gadgetDir.c_str()};
-        for (const char* dir : dirs)
+        // /usr/bin/usb-ctrl eject <name>
+        try
         {
-            fs::remove(dir, ec);
-            if (ec)
-            {
-                success = false;
-                LogMsg(Logger::Error, "[App]: UsbGadget ", ec.message());
-            }
+            auto result = boost::process::system(
+                "/usr/bin/usb-ctrl", "eject", name.c_str());
+            LogMsg(Logger::Debug, "Success of 'usb-ctrl eject ", name.c_str(),
+                   "': ", std::strerror(result));
+        }
+        catch (boost::process::process_error& e)
+        {
+            LogMsg(Logger::Error, "[App]: UsbGadget: ", e.what());
+            success = false;
         }
 
         if (success)
@@ -607,7 +614,8 @@ struct UsbGadget : private FsHelper
     static std::optional<std::string> getStats(const std::string& name)
     {
         const fs::path statsPath = getGadgetDirPrefix() + name +
-                                   "/functions/mass_storage.usb0/lun.0/stats";
+                                   "/functions/mass_storage." + name +
+                                   "/lun.0/stats";
 
         std::ifstream ifs(statsPath);
         if (!ifs.is_open())
-- 
2.35.1