1 #include "config.h"
2 
3 #include "extensions/phal/create_pel.hpp"
4 #include "extensions/phal/pdbg_utils.hpp"
5 #include "registration.hpp"
6 
7 #include <fmt/format.h>
8 #include <sys/wait.h>
9 #include <unistd.h>
10 
11 #include <phosphor-logging/elog-errors.hpp>
12 #include <phosphor-logging/elog.hpp>
13 #include <phosphor-logging/log.hpp>
14 #include <xyz/openbmc_project/Common/error.hpp>
15 
16 #include <filesystem>
17 
18 namespace openpower
19 {
20 namespace phal
21 {
22 
23 using namespace phosphor::logging;
24 
25 void importDevtree()
26 {
27     namespace fs = std::filesystem;
28 
29     // check import data file is present
30     auto path = fs::path(DEVTREE_EXP_FILE);
31     if (!fs::exists(path))
32     {
33         // No import data file skip devtree import
34         return;
35     }
36 
37     // Update PDBG_DTB value
38     openpower::phal::setDevtreeEnv();
39 
40     int status = 0;
41     pid_t pid = fork();
42     if (pid == 0)
43     {
44         std::string cmd("/usr/bin/attributes ");
45         cmd += "import ";
46         cmd += DEVTREE_EXP_FILE;
47         execl("/bin/sh", "sh", "-c", cmd.c_str(), 0);
48 
49         auto error = errno;
50         log<level::ERR>(fmt::format("Error occurred during attributes import "
51                                     "execution, errno({})",
52                                     error)
53                             .c_str());
54     }
55     else if (pid > 0)
56     {
57         waitpid(pid, &status, 0);
58         if (WEXITSTATUS(status))
59         {
60             log<level::ERR>("Failed to import attribute data");
61             openpower::pel::createPEL("org.open_power.PHAL.Error.devtreeSync");
62             return;
63         }
64     }
65     else
66     {
67         log<level::ERR>("fork() failed.");
68         throw std::runtime_error("importDevtree: fork() failed.");
69     }
70 
71     try
72     {
73         // Delete attribute data file once updated.
74         if (fs::exists(path))
75         {
76             // delete export data file
77             fs::remove_all(path);
78         }
79     }
80     catch (fs::filesystem_error& e)
81     { // Log message and continue. Data already applied successfully.
82         log<level::ERR>(fmt::format("File({}) delete failed Error:({})",
83                                     DEVTREE_EXP_FILE, e.what())
84                             .c_str());
85     }
86 
87     log<level::INFO>("Successfully imported devtree attribute data");
88 }
89 
90 REGISTER_PROCEDURE("importDevtree", importDevtree)
91 
92 } // namespace phal
93 } // namespace openpower
94