xref: /openbmc/phosphor-bmc-code-mgmt/bmc/usb/usb_manager.cpp (revision cab87e9cdeeb3e166d6d577511f6be4dc7721aca)
1 #include "config.h"
2 
3 #include "usb_manager.hpp"
4 
5 #include <sys/mount.h>
6 
7 #include <xyz/openbmc_project/ObjectMapper/client.hpp>
8 #include <xyz/openbmc_project/Software/ApplyTime/common.hpp>
9 #include <xyz/openbmc_project/Software/Update/client.hpp>
10 
11 #include <system_error>
12 
13 namespace phosphor
14 {
15 namespace usb
16 {
17 
18 using Association = std::tuple<std::string, std::string, std::string>;
19 using Paths = std::vector<std::string>;
20 
copyImage()21 bool USBManager::copyImage()
22 {
23     std::error_code ec;
24     fs::path dir(usbPath);
25     fs::create_directories(dir, ec);
26 
27     auto rc = mount(devicePath.c_str(), usbPath.c_str(), "vfat", 0, NULL);
28     if (rc)
29     {
30         lg2::error("Error ({ERRNO}) occurred during the mount call", "ERRNO",
31                    errno);
32         return false;
33     }
34 
35     for (const auto& p : std::filesystem::directory_iterator(dir))
36     {
37         if (p.path().extension() == ".tar")
38         {
39             fs::path dstPath{IMG_UPLOAD_DIR / p.path().filename()};
40             if (fs::exists(dstPath, ec))
41             {
42                 lg2::info(
43                     "{DSTPATH} already exists in the /tmp/images directory, exit the upgrade",
44                     "DSTPATH", p.path().filename());
45 
46                 break;
47             }
48 
49             try
50             {
51                 imageDstPath = dstPath;
52                 return fs::copy_file(fs::absolute(p.path()), dstPath);
53             }
54             catch (const std::exception& e)
55             {
56                 lg2::error("Error when copying {SRC} to /tmp/images: {ERROR}",
57                            "SRC", p.path(), "ERROR", e.what());
58             }
59 
60             break;
61         }
62     }
63 
64     return false;
65 }
66 
67 #ifdef START_UPDATE_DBUS_INTEFACE
68 
69 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
findAssociatedUpdatablePath(sdbusplus::async::context & ctx)70 auto findAssociatedUpdatablePath(sdbusplus::async::context& ctx)
71     -> sdbusplus::async::task<Paths>
72 {
73     constexpr auto associatedPath =
74         "/xyz/openbmc_project/software/bmc/updateable";
75     constexpr auto interface = "xyz.openbmc_project.Association";
76     constexpr auto propertyName = "endpoints";
77 
78     co_return utils::getProperty<Paths>(ctx.get_bus(), associatedPath,
79                                         interface, propertyName);
80 }
81 
82 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
startUpdate(int fd)83 auto USBManager::startUpdate(int fd) -> sdbusplus::async::task<bool>
84 {
85     using Updater = sdbusplus::client::xyz::openbmc_project::software::Update<>;
86     using ApplyTimeIntf =
87         sdbusplus::common::xyz::openbmc_project::software::ApplyTime;
88 
89     constexpr auto serviceName = "xyz.openbmc_project.Software.Manager";
90 
91     auto paths = co_await findAssociatedUpdatablePath(ctx);
92     if (paths.size() != 1)
93     {
94         lg2::error("Failed to find associated updatable path");
95         co_return false;
96     }
97 
98     auto updater = Updater(ctx).service(serviceName).path(paths[0]);
99     sdbusplus::message::object_path objectPath = co_await updater.start_update(
100         fd, ApplyTimeIntf::RequestedApplyTimes::OnReset);
101     if (objectPath.str.empty())
102     {
103         lg2::error("StartUpdate failed");
104         co_return false;
105     }
106     lg2::info("StartUpdate succeeded, objectPath: {PATH}", "PATH", objectPath);
107 
108     co_return true;
109 }
110 
111 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
run()112 auto USBManager::run() -> sdbusplus::async::task<void>
113 {
114     auto res = copyImage();
115     if (!res)
116     {
117         lg2::error("Failed to copy image from USB");
118         co_return;
119     }
120 
121     int fd = open(imageDstPath.c_str(), O_RDONLY);
122     if (fd < 0)
123     {
124         lg2::error("Failed to open {PATH}", "PATH", imageDstPath);
125         co_return;
126     }
127 
128     co_await startUpdate(fd);
129 
130     ctx.request_stop();
131 
132     co_return;
133 }
134 
135 #else
136 
run()137 bool USBManager::run()
138 {
139     return copyImage();
140 }
141 
setApplyTime()142 void USBManager::setApplyTime()
143 {
144     utils::PropertyValue value =
145         "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
146     try
147     {
148         constexpr auto objectPath = "/xyz/openbmc_project/software/apply_time";
149         constexpr auto interface = "xyz.openbmc_project.Software.ApplyTime";
150         constexpr auto propertyName = "RequestedApplyTime";
151         utils::setProperty(bus, objectPath, interface, propertyName, value);
152     }
153     catch (const std::exception& e)
154     {
155         lg2::error("Failed to set RequestedApplyTime property, ERROR:{ERROR}",
156                    "ERROR", e.what());
157     }
158 }
159 
setRequestedActivation(const std::string & path)160 void USBManager::setRequestedActivation(const std::string& path)
161 {
162     utils::PropertyValue value =
163         "xyz.openbmc_project.Software.Activation.RequestedActivations.Active";
164     try
165     {
166         constexpr auto interface = "xyz.openbmc_project.Software.Activation";
167         constexpr auto propertyName = "RequestedActivation";
168         utils::setProperty(bus, path, interface, propertyName, value);
169     }
170     catch (const std::exception& e)
171     {
172         lg2::error("Failed to set RequestedActivation property, ERROR:{ERROR}",
173                    "ERROR", e.what());
174     }
175 
176     return;
177 }
178 
updateActivation(sdbusplus::message_t & msg)179 void USBManager::updateActivation(sdbusplus::message_t& msg)
180 {
181     std::map<std::string, std::map<std::string, std::variant<std::string>>>
182         interfaces;
183     sdbusplus::message::object_path path;
184     msg.read(path, interfaces);
185 
186     constexpr auto imageInterface = "xyz.openbmc_project.Software.Activation";
187     constexpr auto readyPro =
188         "xyz.openbmc_project.Software.Activation.Activations.Ready";
189     for (auto& interface : interfaces)
190     {
191         if (interface.first != imageInterface)
192         {
193             continue;
194         }
195 
196         try
197         {
198             auto imageProp = utils::getProperty<std::string>(
199                 bus, path.str, imageInterface, "Activation");
200 
201             if (imageProp == readyPro && isUSBCodeUpdate)
202             {
203                 setApplyTime();
204                 setRequestedActivation(path.str);
205                 event.exit(0);
206             }
207         }
208         catch (const std::exception& e)
209         {
210             lg2::error("Failed in getting Activation status, ERROR:{ERROR}",
211                        "ERROR", e.what());
212         }
213     }
214 }
215 
216 #endif // START_UPDATE_DBUS_INTEFACE
217 
218 } // namespace usb
219 } // namespace phosphor
220