1 #include "config.h"
2 
3 #include "usb_manager.hpp"
4 
5 #include <sys/mount.h>
6 
7 #include <system_error>
8 
9 namespace phosphor
10 {
11 namespace usb
12 {
13 
14 bool USBManager::run()
15 {
16     std::error_code ec;
17     fs::path dir(usbPath);
18     fs::create_directories(dir, ec);
19 
20     auto rc = mount(devicePath.c_str(), usbPath.c_str(), "vfat", 0, NULL);
21     if (rc)
22     {
23         lg2::error("Error ({ERRNO}) occurred during the mount call", "ERRNO",
24                    errno);
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()};
33             if (fs::exists(dstPath, ec))
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 
59 void USBManager::setApplyTime()
60 {
61     utils::PropertyValue value =
62         "xyz.openbmc_project.Software.ApplyTime.RequestedApplyTimes.OnReset";
63     try
64     {
65         constexpr auto objectPath = "/xyz/openbmc_project/software/apply_time";
66         constexpr auto interface = "xyz.openbmc_project.Software.ApplyTime";
67         constexpr auto propertyName = "RequestedApplyTime";
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";
81     try
82     {
83         constexpr auto interface = "xyz.openbmc_project.Software.Activation";
84         constexpr auto propertyName = "RequestedActivation";
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 
96 void USBManager::updateActivation(sdbusplus::message_t& msg)
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         {
115             auto imageProp = utils::getProperty<std::string>(
116                 bus, path.str, imageInterface, "Activation");
117 
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 
133 } // namespace usb
134 } // namespace phosphor
135