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