blob: d1cff3ee325bddb2e7ce94ae909a5dd6e28c8773 [file] [log] [blame]
Jayanth Othayothcf2da1b2021-07-29 03:15:20 -05001#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
18namespace openpower
19{
20namespace phal
21{
22
23using namespace phosphor::logging;
24
25void 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
Jayanth Othayoth2a7322f2021-09-10 09:31:54 -050040 // Update PDATA_INFODB value
41 openpower::phal::setPdataInfoDBEnv();
42
Jayanth Othayothcf2da1b2021-07-29 03:15:20 -050043 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;
Jayanth Othayoth61c87572021-09-10 10:42:42 -050050 cmd += " 2>";
51 cmd += " /dev/null";
Jayanth Othayothcf2da1b2021-07-29 03:15:20 -050052 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 }
Patrick Williams1a9a5a62021-10-06 13:05:06 -050085 catch (const fs::filesystem_error& e)
Jayanth Othayothcf2da1b2021-07-29 03:15:20 -050086 { // 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
95REGISTER_PROCEDURE("importDevtree", importDevtree)
96
97} // namespace phal
98} // namespace openpower