xref: /openbmc/phosphor-bmc-code-mgmt/bmc/usb/usb_manager.hpp (revision cab87e9cdeeb3e166d6d577511f6be4dc7721aca)
1 #pragma once
2 
3 #include "utils.hpp"
4 
5 #include <phosphor-logging/lg2.hpp>
6 #include <sdbusplus/async.hpp>
7 #include <sdeventplus/event.hpp>
8 
9 #include <filesystem>
10 
11 namespace phosphor
12 {
13 namespace usb
14 {
15 namespace fs = std::filesystem;
16 namespace MatchRules = sdbusplus::bus::match::rules;
17 
18 class USBManager
19 {
20   public:
21     ~USBManager() = default;
22     USBManager() = delete;
23     USBManager(const USBManager&) = delete;
24     USBManager(USBManager&&) = default;
25     USBManager& operator=(const USBManager&) = delete;
26     USBManager& operator=(USBManager&&) = delete;
27 
28 #ifdef START_UPDATE_DBUS_INTEFACE
29 
USBManager(sdbusplus::async::context & ctx,const fs::path & devPath,const fs::path & usbPath)30     explicit USBManager(sdbusplus::async::context& ctx, const fs::path& devPath,
31                         const fs::path& usbPath) :
32         ctx(ctx), devicePath(devPath), usbPath(usbPath)
33     {
34         ctx.spawn(run());
35     }
36 
37     /** @brief Run the USBManager */
38     auto run() -> sdbusplus::async::task<void>;
39 
40   private:
41     /** @brief D-Bus context. */
42     sdbusplus::async::context& ctx;
43 
44     /** @brief Starts the firmware update.
45      *  @param[in]  fd  - The file descriptor of the image to update.
46      *  @return Success or Fail
47      */
48     auto startUpdate(int fd) -> sdbusplus::async::task<bool>;
49 
50 #else
USBManager(sdbusplus::bus_t & bus,sdeventplus::Event & event,const fs::path & devPath,const fs::path & usbPath)51     explicit USBManager(sdbusplus::bus_t& bus, sdeventplus::Event& event,
52                         const fs::path& devPath, const fs::path& usbPath) :
53         bus(bus), event(event), isUSBCodeUpdate(false),
54         fwUpdateMatcher(bus,
55                         MatchRules::interfacesAdded() +
56                             MatchRules::path("/xyz/openbmc_project/software"),
57                         std::bind(std::mem_fn(&USBManager::updateActivation),
58                                   this, std::placeholders::_1)),
59         devicePath(devPath), usbPath(usbPath)
60     {
61         if (!run())
62         {
63             lg2::error("Failed to FW Update via USB, usbPath:{USBPATH}",
64                        "USBPATH", usbPath);
65             event.exit(0);
66         }
67 
68         isUSBCodeUpdate = true;
69     }
70 
71     /** @brief Find the first file with a .tar extension according to the USB
72      *         file path.
73      *
74      *  @return Success or Fail
75      */
76     bool run();
77 
78   private:
79     /** @brief Persistent sdbusplus DBus bus connection. */
80     sdbusplus::bus_t& bus;
81 
82     /** sd event handler. */
83     sdeventplus::Event& event;
84 
85     /** Indicates whether USB codeupdate is going on. */
86     bool isUSBCodeUpdate;
87 
88     /** sdbusplus signal match for new image. */
89     sdbusplus::bus::match_t fwUpdateMatcher;
90 
91     /** @brief Creates an Activation D-Bus object.
92      *
93      * @param[in]  msg   - Data associated with subscribed signal
94      */
95     void updateActivation(sdbusplus::message_t& msg);
96 
97     /** @brief Set Apply Time to OnReset.
98      *
99      */
100     void setApplyTime();
101 
102     /** @brief Method to set the RequestedActivation D-Bus property.
103      *
104      *  @param[in] path  - Update the object path of the firmware
105      */
106     void setRequestedActivation(const std::string& path);
107 
108 #endif /* START_UPDATE_DBUS_INTEFACE */
109 
110     /** @brief Find the first file with a .tar extension according to the USB
111      *         file path and copy to IMG_UPLOAD_DIR
112      *
113      *  @return Success or Fail
114      */
115     bool copyImage();
116 
117     /** The USB device path. */
118     const fs::path& devicePath;
119 
120     /** The USB mount path. */
121     const fs::path& usbPath;
122 
123     /** The destination path for copied over image file */
124     fs::path imageDstPath;
125 };
126 
127 } // namespace usb
128 } // namespace phosphor
129