blob: 226a5784a3ba7a177cc01370fb1f4128aec9246c [file] [log] [blame]
George Liu073a6532021-10-25 14:40:03 +08001#include "config.h"
2
3#include "usb_manager.hpp"
4
George Liu6d775e62021-10-26 10:44:30 +08005#include <sys/mount.h>
6
George Liu44b9fef2023-02-07 14:31:32 +08007#include <system_error>
8
George Liu073a6532021-10-25 14:40:03 +08009namespace phosphor
10{
11namespace usb
12{
13
14bool USBManager::run()
15{
George Liu44b9fef2023-02-07 14:31:32 +080016 std::error_code ec;
George Liu073a6532021-10-25 14:40:03 +080017 fs::path dir(usbPath);
George Liu44b9fef2023-02-07 14:31:32 +080018 fs::create_directories(dir, ec);
George Liu6d775e62021-10-26 10:44:30 +080019
20 auto rc = mount(devicePath.c_str(), usbPath.c_str(), "vfat", 0, NULL);
21 if (rc)
George Liu073a6532021-10-25 14:40:03 +080022 {
George Liu6d775e62021-10-26 10:44:30 +080023 lg2::error("Error ({ERRNO}) occurred during the mount call", "ERRNO",
24 errno);
George Liu073a6532021-10-25 14:40:03 +080025 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 Liu44b9fef2023-02-07 14:31:32 +080033 if (fs::exists(dstPath, ec))
George Liu073a6532021-10-25 14:40:03 +080034 {
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 Liu5107c452021-11-09 20:06:31 +080059void USBManager::setApplyTime()
60{
61 utils::PropertyValue value =
62 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
George Liu5107c452021-11-09 20:06:31 +080063 try
64 {
Lei YU0cd6d842021-12-27 11:56:02 +080065 constexpr auto objectPath = "/xyz/openbmc_project/software/apply_time";
66 constexpr auto interface = "xyz.openbmc_project.Software.ApplyTime";
67 constexpr auto propertyName = "RequestedApplyTime";
George Liu5107c452021-11-09 20:06:31 +080068 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
77void USBManager::setRequestedActivation(const std::string& path)
78{
79 utils::PropertyValue value =
80 "xyz.openbmc_project.Software.Activation.RequestedActivations.Active";
George Liu5107c452021-11-09 20:06:31 +080081 try
82 {
Lei YU0cd6d842021-12-27 11:56:02 +080083 constexpr auto interface = "xyz.openbmc_project.Software.Activation";
84 constexpr auto propertyName = "RequestedActivation";
George Liu5107c452021-11-09 20:06:31 +080085 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 Williamsbf2bb2b2022-07-22 19:26:52 -050096void USBManager::updateActivation(sdbusplus::message_t& msg)
George Liu5107c452021-11-09 20:06:31 +080097{
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 Geissler047f60e2022-04-06 15:38:07 -0500115 auto imageProp = utils::getProperty<std::string>(
116 bus, path.str, imageInterface, "Activation");
117
George Liu5107c452021-11-09 20:06:31 +0800118 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 Liu073a6532021-10-25 14:40:03 +0800133} // namespace usb
George Liu5107c452021-11-09 20:06:31 +0800134} // namespace phosphor