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 <sys/wait.h>
8cf2da1beSJayanth Othayoth #include <unistd.h>
9cf2da1beSJayanth Othayoth 
10cf2da1beSJayanth Othayoth #include <phosphor-logging/elog-errors.hpp>
11cf2da1beSJayanth Othayoth #include <phosphor-logging/elog.hpp>
12cf2da1beSJayanth Othayoth #include <phosphor-logging/log.hpp>
13cf2da1beSJayanth Othayoth #include <xyz/openbmc_project/Common/error.hpp>
14cf2da1beSJayanth Othayoth 
15cf2da1beSJayanth Othayoth #include <filesystem>
16*e0dd7af4SJayanth Othayoth #include <format>
17cf2da1beSJayanth Othayoth 
18cf2da1beSJayanth Othayoth namespace openpower
19cf2da1beSJayanth Othayoth {
20cf2da1beSJayanth Othayoth namespace phal
21cf2da1beSJayanth Othayoth {
22cf2da1beSJayanth Othayoth 
23cf2da1beSJayanth Othayoth using namespace phosphor::logging;
24cf2da1beSJayanth Othayoth 
importDevtree()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 
402a7322f4SJayanth Othayoth     // Update PDATA_INFODB value
412a7322f4SJayanth Othayoth     openpower::phal::setPdataInfoDBEnv();
422a7322f4SJayanth 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;
5061c8757cSJayanth Othayoth         cmd += " 2>";
5161c8757cSJayanth Othayoth         cmd += " /dev/null";
52cf2da1beSJayanth Othayoth         execl("/bin/sh", "sh", "-c", cmd.c_str(), 0);
53cf2da1beSJayanth Othayoth 
54cf2da1beSJayanth Othayoth         auto error = errno;
55*e0dd7af4SJayanth Othayoth         log<level::ERR>(std::format("Error occurred during attributes import "
56cf2da1beSJayanth Othayoth                                     "execution, errno({})",
57cf2da1beSJayanth Othayoth                                     error)
58cf2da1beSJayanth Othayoth                             .c_str());
59cf2da1beSJayanth Othayoth     }
60cf2da1beSJayanth Othayoth     else if (pid > 0)
61cf2da1beSJayanth Othayoth     {
62cf2da1beSJayanth Othayoth         waitpid(pid, &status, 0);
63cf2da1beSJayanth Othayoth         if (WEXITSTATUS(status))
64cf2da1beSJayanth Othayoth         {
65cf2da1beSJayanth Othayoth             log<level::ERR>("Failed to import attribute data");
66cf2da1beSJayanth Othayoth             openpower::pel::createPEL("org.open_power.PHAL.Error.devtreeSync");
67cf2da1beSJayanth Othayoth             return;
68cf2da1beSJayanth Othayoth         }
69cf2da1beSJayanth Othayoth     }
70cf2da1beSJayanth Othayoth     else
71cf2da1beSJayanth Othayoth     {
72cf2da1beSJayanth Othayoth         log<level::ERR>("fork() failed.");
73cf2da1beSJayanth Othayoth         throw std::runtime_error("importDevtree: fork() failed.");
74cf2da1beSJayanth Othayoth     }
75cf2da1beSJayanth Othayoth 
76cf2da1beSJayanth Othayoth     try
77cf2da1beSJayanth Othayoth     {
78cf2da1beSJayanth Othayoth         // Delete attribute data file once updated.
79cf2da1beSJayanth Othayoth         if (fs::exists(path))
80cf2da1beSJayanth Othayoth         {
81cf2da1beSJayanth Othayoth             // delete export data file
82cf2da1beSJayanth Othayoth             fs::remove_all(path);
83cf2da1beSJayanth Othayoth         }
84cf2da1beSJayanth Othayoth     }
851a9a5a6aSPatrick Williams     catch (const fs::filesystem_error& e)
86cf2da1beSJayanth Othayoth     { // Log message and continue. Data already applied successfully.
87*e0dd7af4SJayanth Othayoth         log<level::ERR>(std::format("File({}) delete failed Error:({})",
88cf2da1beSJayanth Othayoth                                     DEVTREE_EXP_FILE, e.what())
89cf2da1beSJayanth Othayoth                             .c_str());
90cf2da1beSJayanth Othayoth     }
91cf2da1beSJayanth Othayoth 
92cf2da1beSJayanth Othayoth     log<level::INFO>("Successfully imported devtree attribute data");
93cf2da1beSJayanth Othayoth }
94cf2da1beSJayanth Othayoth 
95cf2da1beSJayanth Othayoth REGISTER_PROCEDURE("importDevtree", importDevtree)
96cf2da1beSJayanth Othayoth 
97cf2da1beSJayanth Othayoth } // namespace phal
98cf2da1beSJayanth Othayoth } // namespace openpower
99