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     // Update PDATA_INFODB value
41     openpower::phal::setPdataInfoDBEnv();
42 
43     int status = 0;
44     pid_t pid = fork();
45     if (pid == 0)
46     {
47         std::string cmd("/usr/bin/attributes ");
48         cmd += "import ";
49         cmd += DEVTREE_EXP_FILE;
50         cmd += " 2>";
51         cmd += " /dev/null";
52         execl("/bin/sh", "sh", "-c", cmd.c_str(), 0);
53 
54         auto error = errno;
55         log<level::ERR>(fmt::format("Error occurred during attributes import "
56                                     "execution, errno({})",
57                                     error)
58                             .c_str());
59     }
60     else if (pid > 0)
61     {
62         waitpid(pid, &status, 0);
63         if (WEXITSTATUS(status))
64         {
65             log<level::ERR>("Failed to import attribute data");
66             openpower::pel::createPEL("org.open_power.PHAL.Error.devtreeSync");
67             return;
68         }
69     }
70     else
71     {
72         log<level::ERR>("fork() failed.");
73         throw std::runtime_error("importDevtree: fork() failed.");
74     }
75 
76     try
77     {
78         // Delete attribute data file once updated.
79         if (fs::exists(path))
80         {
81             // delete export data file
82             fs::remove_all(path);
83         }
84     }
85     catch (fs::filesystem_error& e)
86     { // Log message and continue. Data already applied successfully.
87         log<level::ERR>(fmt::format("File({}) delete failed Error:({})",
88                                     DEVTREE_EXP_FILE, e.what())
89                             .c_str());
90     }
91 
92     log<level::INFO>("Successfully imported devtree attribute data");
93 }
94 
95 REGISTER_PROCEDURE("importDevtree", importDevtree)
96 
97 } // namespace phal
98 } // namespace openpower
99