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