blob: 4a1f7689df16bc1f3ecc2e87c9c88f01eb460d34 [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 Liu073a6532021-10-25 14:40:03 +08007namespace phosphor
8{
9namespace usb
10{
11
12bool USBManager::run()
13{
14 fs::path dir(usbPath);
George Liu6d775e62021-10-26 10:44:30 +080015 fs::create_directories(dir);
16
17 auto rc = mount(devicePath.c_str(), usbPath.c_str(), "vfat", 0, NULL);
18 if (rc)
George Liu073a6532021-10-25 14:40:03 +080019 {
George Liu6d775e62021-10-26 10:44:30 +080020 lg2::error("Error ({ERRNO}) occurred during the mount call", "ERRNO",
21 errno);
George Liu073a6532021-10-25 14:40:03 +080022 return false;
23 }
24
25 for (const auto& p : std::filesystem::directory_iterator(dir))
26 {
27 if (p.path().extension() == ".tar")
28 {
29 fs::path dstPath{IMG_UPLOAD_DIR / p.path().filename()};
30 if (fs::exists(dstPath))
31 {
32 lg2::info(
33 "{DSTPATH} already exists in the /tmp/images directory, exit the upgrade",
34 "DSTPATH", p.path().filename());
35
36 break;
37 }
38
39 try
40 {
41 return fs::copy_file(fs::absolute(p.path()), dstPath);
42 }
43 catch (const std::exception& e)
44 {
45 lg2::error("Error when copying {SRC} to /tmp/images: {ERROR}",
46 "SRC", p.path(), "ERROR", e.what());
47 }
48
49 break;
50 }
51 }
52
53 return false;
54}
55
George Liu5107c452021-11-09 20:06:31 +080056void USBManager::setApplyTime()
57{
58 utils::PropertyValue value =
59 "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
George Liu5107c452021-11-09 20:06:31 +080060 try
61 {
Lei YU0cd6d842021-12-27 11:56:02 +080062 constexpr auto objectPath = "/xyz/openbmc_project/software/apply_time";
63 constexpr auto interface = "xyz.openbmc_project.Software.ApplyTime";
64 constexpr auto propertyName = "RequestedApplyTime";
George Liu5107c452021-11-09 20:06:31 +080065 utils::setProperty(bus, objectPath, interface, propertyName, value);
66 }
67 catch (const std::exception& e)
68 {
69 lg2::error("Failed to set RequestedApplyTime property, ERROR:{ERROR}",
70 "ERROR", e.what());
71 }
72}
73
74void USBManager::setRequestedActivation(const std::string& path)
75{
76 utils::PropertyValue value =
77 "xyz.openbmc_project.Software.Activation.RequestedActivations.Active";
George Liu5107c452021-11-09 20:06:31 +080078 try
79 {
Lei YU0cd6d842021-12-27 11:56:02 +080080 constexpr auto interface = "xyz.openbmc_project.Software.Activation";
81 constexpr auto propertyName = "RequestedActivation";
George Liu5107c452021-11-09 20:06:31 +080082 utils::setProperty(bus, path, interface, propertyName, value);
83 }
84 catch (const std::exception& e)
85 {
86 lg2::error("Failed to set RequestedActivation property, ERROR:{ERROR}",
87 "ERROR", e.what());
88 }
89
90 return;
91}
92
93void USBManager::updateActivation(sdbusplus::message::message& msg)
94{
95 std::map<std::string, std::map<std::string, std::variant<std::string>>>
96 interfaces;
97 sdbusplus::message::object_path path;
98 msg.read(path, interfaces);
99
100 constexpr auto imageInterface = "xyz.openbmc_project.Software.Activation";
101 constexpr auto readyPro =
102 "xyz.openbmc_project.Software.Activation.Activations.Ready";
103 for (auto& interface : interfaces)
104 {
105 if (interface.first != imageInterface)
106 {
107 continue;
108 }
109
110 try
111 {
112 auto propVal =
113 utils::getProperty(bus, path.str, imageInterface, "Activation");
114 const auto& imageProp = std::get<std::string>(propVal);
115 if (imageProp == readyPro && isUSBCodeUpdate)
116 {
117 setApplyTime();
118 setRequestedActivation(path.str);
119 event.exit(0);
120 }
121 }
122 catch (const std::exception& e)
123 {
124 lg2::error("Failed in getting Activation status, ERROR:{ERROR}",
125 "ERROR", e.what());
126 }
127 }
128}
129
George Liu073a6532021-10-25 14:40:03 +0800130} // namespace usb
George Liu5107c452021-11-09 20:06:31 +0800131} // namespace phosphor