diff options
Diffstat (limited to 'meta-ibs/meta-cp2-5422/recipes-phosphor/virtual-media/virtual-media/0014-virtual-media.1-Upd-Use-usb-ctrl-instead-of-sysfs-.-.patch')
-rw-r--r-- | meta-ibs/meta-cp2-5422/recipes-phosphor/virtual-media/virtual-media/0014-virtual-media.1-Upd-Use-usb-ctrl-instead-of-sysfs-.-.patch | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/meta-ibs/meta-cp2-5422/recipes-phosphor/virtual-media/virtual-media/0014-virtual-media.1-Upd-Use-usb-ctrl-instead-of-sysfs-.-.patch b/meta-ibs/meta-cp2-5422/recipes-phosphor/virtual-media/virtual-media/0014-virtual-media.1-Upd-Use-usb-ctrl-instead-of-sysfs-.-.patch new file mode 100644 index 0000000000..d52a8d4afd --- /dev/null +++ b/meta-ibs/meta-cp2-5422/recipes-phosphor/virtual-media/virtual-media/0014-virtual-media.1-Upd-Use-usb-ctrl-instead-of-sysfs-.-.patch @@ -0,0 +1,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 + |