1 #include "config.h"
2 
3 #include "usb_manager.hpp"
4 
5 #include <sys/mount.h>
6 
7 namespace phosphor
8 {
9 namespace usb
10 {
11 
12 bool USBManager::run()
13 {
14     fs::path dir(usbPath);
15     fs::create_directories(dir);
16 
17     auto rc = mount(devicePath.c_str(), usbPath.c_str(), "vfat", 0, NULL);
18     if (rc)
19     {
20         lg2::error("Error ({ERRNO}) occurred during the mount call", "ERRNO",
21                    errno);
22         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 
56 void USBManager::setApplyTime()
57 {
58     utils::PropertyValue value =
59         "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
60     try
61     {
62         constexpr auto objectPath = "/xyz/openbmc_project/software/apply_time";
63         constexpr auto interface = "xyz.openbmc_project.Software.ApplyTime";
64         constexpr auto propertyName = "RequestedApplyTime";
65         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 
74 void USBManager::setRequestedActivation(const std::string& path)
75 {
76     utils::PropertyValue value =
77         "xyz.openbmc_project.Software.Activation.RequestedActivations.Active";
78     try
79     {
80         constexpr auto interface = "xyz.openbmc_project.Software.Activation";
81         constexpr auto propertyName = "RequestedActivation";
82         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 
93 void 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 
130 } // namespace usb
131 } // namespace phosphor
132