George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "usb_manager.hpp" |
| 4 | |
George Liu | 6d775e6 | 2021-10-26 10:44:30 +0800 | [diff] [blame] | 5 | #include <sys/mount.h> |
| 6 | |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 7 | #include <system_error> |
| 8 | |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 9 | namespace phosphor |
| 10 | { |
| 11 | namespace usb |
| 12 | { |
| 13 | |
| 14 | bool USBManager::run() |
| 15 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 16 | std::error_code ec; |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 17 | fs::path dir(usbPath); |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 18 | fs::create_directories(dir, ec); |
George Liu | 6d775e6 | 2021-10-26 10:44:30 +0800 | [diff] [blame] | 19 | |
| 20 | auto rc = mount(devicePath.c_str(), usbPath.c_str(), "vfat", 0, NULL); |
| 21 | if (rc) |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 22 | { |
George Liu | 6d775e6 | 2021-10-26 10:44:30 +0800 | [diff] [blame] | 23 | lg2::error("Error ({ERRNO}) occurred during the mount call", "ERRNO", |
| 24 | errno); |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 25 | return false; |
| 26 | } |
| 27 | |
| 28 | for (const auto& p : std::filesystem::directory_iterator(dir)) |
| 29 | { |
| 30 | if (p.path().extension() == ".tar") |
| 31 | { |
| 32 | fs::path dstPath{IMG_UPLOAD_DIR / p.path().filename()}; |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 33 | if (fs::exists(dstPath, ec)) |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 34 | { |
| 35 | lg2::info( |
| 36 | "{DSTPATH} already exists in the /tmp/images directory, exit the upgrade", |
| 37 | "DSTPATH", p.path().filename()); |
| 38 | |
| 39 | break; |
| 40 | } |
| 41 | |
| 42 | try |
| 43 | { |
| 44 | return fs::copy_file(fs::absolute(p.path()), dstPath); |
| 45 | } |
| 46 | catch (const std::exception& e) |
| 47 | { |
| 48 | lg2::error("Error when copying {SRC} to /tmp/images: {ERROR}", |
| 49 | "SRC", p.path(), "ERROR", e.what()); |
| 50 | } |
| 51 | |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return false; |
| 57 | } |
| 58 | |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 59 | void USBManager::setApplyTime() |
| 60 | { |
| 61 | utils::PropertyValue value = |
| 62 | "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset"; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 63 | try |
| 64 | { |
Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 65 | constexpr auto objectPath = "/xyz/openbmc_project/software/apply_time"; |
| 66 | constexpr auto interface = "xyz.openbmc_project.Software.ApplyTime"; |
| 67 | constexpr auto propertyName = "RequestedApplyTime"; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 68 | utils::setProperty(bus, objectPath, interface, propertyName, value); |
| 69 | } |
| 70 | catch (const std::exception& e) |
| 71 | { |
| 72 | lg2::error("Failed to set RequestedApplyTime property, ERROR:{ERROR}", |
| 73 | "ERROR", e.what()); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void USBManager::setRequestedActivation(const std::string& path) |
| 78 | { |
| 79 | utils::PropertyValue value = |
| 80 | "xyz.openbmc_project.Software.Activation.RequestedActivations.Active"; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 81 | try |
| 82 | { |
Lei YU | 0cd6d84 | 2021-12-27 11:56:02 +0800 | [diff] [blame] | 83 | constexpr auto interface = "xyz.openbmc_project.Software.Activation"; |
| 84 | constexpr auto propertyName = "RequestedActivation"; |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 85 | utils::setProperty(bus, path, interface, propertyName, value); |
| 86 | } |
| 87 | catch (const std::exception& e) |
| 88 | { |
| 89 | lg2::error("Failed to set RequestedActivation property, ERROR:{ERROR}", |
| 90 | "ERROR", e.what()); |
| 91 | } |
| 92 | |
| 93 | return; |
| 94 | } |
| 95 | |
Patrick Williams | bf2bb2b | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 96 | void USBManager::updateActivation(sdbusplus::message_t& msg) |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 97 | { |
| 98 | std::map<std::string, std::map<std::string, std::variant<std::string>>> |
| 99 | interfaces; |
| 100 | sdbusplus::message::object_path path; |
| 101 | msg.read(path, interfaces); |
| 102 | |
| 103 | constexpr auto imageInterface = "xyz.openbmc_project.Software.Activation"; |
| 104 | constexpr auto readyPro = |
| 105 | "xyz.openbmc_project.Software.Activation.Activations.Ready"; |
| 106 | for (auto& interface : interfaces) |
| 107 | { |
| 108 | if (interface.first != imageInterface) |
| 109 | { |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | try |
| 114 | { |
Andrew Geissler | 047f60e | 2022-04-06 15:38:07 -0500 | [diff] [blame] | 115 | auto imageProp = utils::getProperty<std::string>( |
| 116 | bus, path.str, imageInterface, "Activation"); |
| 117 | |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 118 | if (imageProp == readyPro && isUSBCodeUpdate) |
| 119 | { |
| 120 | setApplyTime(); |
| 121 | setRequestedActivation(path.str); |
| 122 | event.exit(0); |
| 123 | } |
| 124 | } |
| 125 | catch (const std::exception& e) |
| 126 | { |
| 127 | lg2::error("Failed in getting Activation status, ERROR:{ERROR}", |
| 128 | "ERROR", e.what()); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
George Liu | 073a653 | 2021-10-25 14:40:03 +0800 | [diff] [blame] | 133 | } // namespace usb |
George Liu | 5107c45 | 2021-11-09 20:06:31 +0800 | [diff] [blame] | 134 | } // namespace phosphor |