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> |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 6 | #include <sdeventplus/event.hpp> |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 7 | |
| 8 | #include <filesystem> |
| 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace usb |
| 13 | { |
| 14 | namespace fs = std::filesystem; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 15 | namespace MatchRules = sdbusplus::bus::match::rules; |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 16 | |
| 17 | class USBManager |
| 18 | { |
| 19 | public: |
| 20 | ~USBManager() = default; |
| 21 | USBManager() = delete; |
| 22 | USBManager(const USBManager&) = delete; |
| 23 | USBManager(USBManager&&) = default; |
| 24 | USBManager& operator=(const USBManager&) = delete; |
| 25 | USBManager& operator=(USBManager&&) = default; |
| 26 | |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 27 | explicit USBManager(sdbusplus::bus::bus& bus, sdeventplus::Event& event, |
George Liu | 6d775e6 | 2021-10-26 10:44:30 +0800 | [diff] [blame] | 28 | const fs::path& devPath, const fs::path& usbPath) : |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 29 | bus(bus), |
George Liu | 6d775e6 | 2021-10-26 10:44:30 +0800 | [diff] [blame] | 30 | event(event), devicePath(devPath), usbPath(usbPath), |
| 31 | isUSBCodeUpdate(false), |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 32 | fwUpdateMatcher(bus, |
| 33 | MatchRules::interfacesAdded() + |
| 34 | MatchRules::path("/xyz/openbmc_project/software"), |
| 35 | std::bind(std::mem_fn(&USBManager::updateActivation), |
| 36 | this, std::placeholders::_1)) |
| 37 | { |
| 38 | if (!run()) |
| 39 | { |
| 40 | lg2::error("Failed to FW Update via USB, usbPath:{USBPATH}", |
| 41 | "USBPATH", usbPath); |
| 42 | event.exit(0); |
| 43 | } |
| 44 | |
| 45 | isUSBCodeUpdate = true; |
| 46 | } |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 47 | |
| 48 | /** @brief Find the first file with a .tar extension according to the USB |
| 49 | * file path. |
| 50 | * |
| 51 | * @return Success or Fail |
| 52 | */ |
| 53 | bool run(); |
| 54 | |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 55 | /** @brief Creates an Activation D-Bus object. |
| 56 | * |
| 57 | * @param[in] msg - Data associated with subscribed signal |
| 58 | */ |
| 59 | void updateActivation(sdbusplus::message::message& msg); |
| 60 | |
| 61 | /** @brief Set Apply Time to OnReset. |
| 62 | * |
| 63 | */ |
| 64 | void setApplyTime(); |
| 65 | |
| 66 | /** @brief Method to set the RequestedActivation D-Bus property. |
| 67 | * |
| 68 | * @param[in] path - Update the object path of the firmware |
| 69 | */ |
| 70 | void setRequestedActivation(const std::string& path); |
| 71 | |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 72 | private: |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 73 | /** @brief Persistent sdbusplus DBus bus connection. */ |
| 74 | sdbusplus::bus::bus& bus; |
| 75 | |
| 76 | /** sd event handler. */ |
| 77 | sdeventplus::Event& event; |
| 78 | |
George Liu | 6d775e6 | 2021-10-26 10:44:30 +0800 | [diff] [blame] | 79 | /** The USB device path. */ |
| 80 | const fs::path& devicePath; |
| 81 | |
| 82 | /** The USB mount path. */ |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 83 | const fs::path& usbPath; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 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; |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace usb |
| 93 | } // namespace phosphor |