1 /**
2  * Copyright © 2019 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "data_interface.hpp"
17 #include "elog_entry.hpp"
18 #include "event_logger.hpp"
19 #include "extensions.hpp"
20 #include "journal.hpp"
21 #include "manager.hpp"
22 #include "pldm_interface.hpp"
23 
24 #include <phosphor-logging/lg2.hpp>
25 
26 #include <format>
27 
28 #ifdef PEL_ENABLE_PHAL
29 #include "libekb.H"
30 #include "libpdbg.h"
31 #endif
32 
33 namespace openpower
34 {
35 namespace pels
36 {
37 
38 using namespace phosphor::logging;
39 
40 std::unique_ptr<Manager> manager;
41 
42 DISABLE_LOG_ENTRY_CAPS()
43 
44 void pelStartup(internal::Manager& logManager)
45 {
46     EventLogger::LogFunction logger =
47         std::bind(std::mem_fn(&internal::Manager::create), &logManager,
48                   std::placeholders::_1, std::placeholders::_2,
49                   std::placeholders::_3, phosphor::logging::FFDCEntries{});
50 
51     std::unique_ptr<DataInterfaceBase> dataIface =
52         std::make_unique<DataInterface>(logManager.getBus());
53 
54     std::unique_ptr<JournalBase> journal = std::make_unique<Journal>();
55 
56 #ifndef DONT_SEND_PELS_TO_HOST
57     std::unique_ptr<HostInterface> hostIface = std::make_unique<PLDMInterface>(
58         logManager.getBus().get_event(), *(dataIface.get()));
59 
60     manager = std::make_unique<Manager>(logManager, std::move(dataIface),
61                                         std::move(logger), std::move(journal),
62                                         std::move(hostIface));
63 #else
64     manager = std::make_unique<Manager>(logManager, std::move(dataIface),
65                                         std::move(logger), std::move(journal));
66 #endif
67 
68 #ifdef PEL_ENABLE_PHAL
69     // PDBG_DTB environment variable set to CEC device tree path
70     static constexpr auto PDBG_DTB_PATH =
71         "/var/lib/phosphor-software-manager/pnor/rw/DEVTREE";
72 
73     if (setenv("PDBG_DTB", PDBG_DTB_PATH, 1))
74     {
75         // Log message and continue,
76         // This is to help continue creating PEL in raw format.
77         lg2::error("Failed to set PDBG_DTB: ({ERRNO})", "ERRNO",
78                    strerror(errno));
79     }
80 
81     if (!pdbg_targets_init(NULL))
82     {
83         lg2::error("pdbg_targets_init failed");
84         return;
85     }
86 
87     if (libekb_init())
88     {
89         lg2::error("libekb_init failed, skipping ffdc processing");
90         return;
91     }
92 
93 #endif
94 }
95 
96 REGISTER_EXTENSION_FUNCTION(pelStartup)
97 
98 void pelCreate(const std::string& message, uint32_t id, uint64_t timestamp,
99                Entry::Level severity, const AdditionalDataArg& additionalData,
100                const AssociationEndpointsArg& assocs, const FFDCArg& ffdc)
101 {
102     manager->create(message, id, timestamp, severity, additionalData, assocs,
103                     ffdc);
104 }
105 
106 REGISTER_EXTENSION_FUNCTION(pelCreate)
107 
108 void pelDelete(uint32_t id)
109 {
110     return manager->erase(id);
111 }
112 
113 REGISTER_EXTENSION_FUNCTION(pelDelete)
114 
115 void pelDeleteProhibited(uint32_t id, bool& prohibited)
116 {
117     prohibited = manager->isDeleteProhibited(id);
118 }
119 
120 REGISTER_EXTENSION_FUNCTION(pelDeleteProhibited)
121 
122 } // namespace pels
123 } // namespace openpower
124