blob: c7f6403180cd229978e090c351358919a49a8e93 [file] [log] [blame]
George Liu073a6532021-10-25 14:40:03 +08001#pragma once
2
George Liu5107c452021-11-09 20:06:31 +08003#include "utils.hpp"
4
George Liu073a6532021-10-25 14:40:03 +08005#include <phosphor-logging/lg2.hpp>
Jagpal Singh Gill6fbb54c2024-08-03 16:39:24 -07006#include <sdbusplus/async.hpp>
George Liu5107c452021-11-09 20:06:31 +08007#include <sdeventplus/event.hpp>
George Liu073a6532021-10-25 14:40:03 +08008
9#include <filesystem>
10
11namespace phosphor
12{
13namespace usb
14{
15namespace fs = std::filesystem;
George Liu5107c452021-11-09 20:06:31 +080016namespace MatchRules = sdbusplus::bus::match::rules;
George Liu073a6532021-10-25 14:40:03 +080017
18class 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;
Pavithra Barithaya4fd0d0f2024-06-22 00:37:22 -050026 USBManager& operator=(USBManager&&) = delete;
George Liu073a6532021-10-25 14:40:03 +080027
Jagpal Singh Gill6fbb54c2024-08-03 16:39:24 -070028#ifdef START_UPDATE_DBUS_INTEFACE
29
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
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050051 explicit USBManager(sdbusplus::bus_t& bus, sdeventplus::Event& event,
George Liu6d775e62021-10-26 10:44:30 +080052 const fs::path& devPath, const fs::path& usbPath) :
Jagpal Singh Gill6fbb54c2024-08-03 16:39:24 -070053 bus(bus), event(event), isUSBCodeUpdate(false),
George Liu5107c452021-11-09 20:06:31 +080054 fwUpdateMatcher(bus,
55 MatchRules::interfacesAdded() +
56 MatchRules::path("/xyz/openbmc_project/software"),
57 std::bind(std::mem_fn(&USBManager::updateActivation),
Jagpal Singh Gill6fbb54c2024-08-03 16:39:24 -070058 this, std::placeholders::_1)),
59 devicePath(devPath), usbPath(usbPath)
George Liu5107c452021-11-09 20:06:31 +080060 {
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 }
George Liu073a6532021-10-25 14:40:03 +080070
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
Jagpal Singh Gill6fbb54c2024-08-03 16:39:24 -070078 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
George Liu5107c452021-11-09 20:06:31 +080091 /** @brief Creates an Activation D-Bus object.
92 *
93 * @param[in] msg - Data associated with subscribed signal
94 */
Patrick Williamsbf2bb2b2022-07-22 19:26:52 -050095 void updateActivation(sdbusplus::message_t& msg);
George Liu5107c452021-11-09 20:06:31 +080096
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
Jagpal Singh Gill6fbb54c2024-08-03 16:39:24 -0700108#endif /* START_UPDATE_DBUS_INTEFACE */
George Liu5107c452021-11-09 20:06:31 +0800109
Jagpal Singh Gill6fbb54c2024-08-03 16:39:24 -0700110 /** @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();
George Liu5107c452021-11-09 20:06:31 +0800116
George Liu6d775e62021-10-26 10:44:30 +0800117 /** The USB device path. */
118 const fs::path& devicePath;
119
120 /** The USB mount path. */
George Liu073a6532021-10-25 14:40:03 +0800121 const fs::path& usbPath;
George Liu5107c452021-11-09 20:06:31 +0800122
Jagpal Singh Gill6fbb54c2024-08-03 16:39:24 -0700123 /** The destination path for copied over image file */
124 fs::path imageDstPath;
George Liu073a6532021-10-25 14:40:03 +0800125};
126
127} // namespace usb
128} // namespace phosphor