George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 3 | #include "utils.hpp" |
| 4 | |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 5 | #include <phosphor-logging/lg2.hpp> |
Jagpal Singh Gill | 6fbb54c | 2024-08-03 16:39:24 -0700 | [diff] [blame] | 6 | #include <sdbusplus/async.hpp> |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 7 | #include <sdeventplus/event.hpp> |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 8 | |
| 9 | #include <filesystem> |
| 10 | |
| 11 | namespace phosphor |
| 12 | { |
| 13 | namespace usb |
| 14 | { |
| 15 | namespace fs = std::filesystem; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 16 | namespace MatchRules = sdbusplus::bus::match::rules; |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 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; |
Pavithra Barithaya | 4fd0d0f | 2024-06-22 00:37:22 -0500 | [diff] [blame] | 26 | USBManager& operator=(USBManager&&) = delete; |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 27 | |
Jagpal Singh Gill | 6fbb54c | 2024-08-03 16:39:24 -0700 | [diff] [blame] | 28 | #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 Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 51 | explicit USBManager(sdbusplus::bus_t& bus, sdeventplus::Event& event, |
George Liu | 6d775e6 | 2021-10-26 10:44:30 +0800 | [diff] [blame] | 52 | const fs::path& devPath, const fs::path& usbPath) : |
Jagpal Singh Gill | 6fbb54c | 2024-08-03 16:39:24 -0700 | [diff] [blame] | 53 | bus(bus), event(event), isUSBCodeUpdate(false), |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 54 | fwUpdateMatcher(bus, |
| 55 | MatchRules::interfacesAdded() + |
| 56 | MatchRules::path("/xyz/openbmc_project/software"), |
| 57 | std::bind(std::mem_fn(&USBManager::updateActivation), |
Jagpal Singh Gill | 6fbb54c | 2024-08-03 16:39:24 -0700 | [diff] [blame] | 58 | this, std::placeholders::_1)), |
| 59 | devicePath(devPath), usbPath(usbPath) |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 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 | } |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 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 | |
Jagpal Singh Gill | 6fbb54c | 2024-08-03 16:39:24 -0700 | [diff] [blame] | 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 | |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 91 | /** @brief Creates an Activation D-Bus object. |
| 92 | * |
| 93 | * @param[in] msg - Data associated with subscribed signal |
| 94 | */ |
Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 95 | void updateActivation(sdbusplus::message_t& msg); |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 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 | |
Jagpal Singh Gill | 6fbb54c | 2024-08-03 16:39:24 -0700 | [diff] [blame] | 108 | #endif /* START_UPDATE_DBUS_INTEFACE */ |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 109 | |
Jagpal Singh Gill | 6fbb54c | 2024-08-03 16:39:24 -0700 | [diff] [blame] | 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(); |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 116 | |
George Liu | 6d775e6 | 2021-10-26 10:44:30 +0800 | [diff] [blame] | 117 | /** The USB device path. */ |
| 118 | const fs::path& devicePath; |
| 119 | |
| 120 | /** The USB mount path. */ |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 121 | const fs::path& usbPath; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 122 | |
Jagpal Singh Gill | 6fbb54c | 2024-08-03 16:39:24 -0700 | [diff] [blame] | 123 | /** The destination path for copied over image file */ |
| 124 | fs::path imageDstPath; |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | } // namespace usb |
| 128 | } // namespace phosphor |