blob: 461c5edb96c0c4c5b54b15671ebd16216445fc2e [file] [log] [blame]
Patrick Venture33569752018-03-12 18:56:14 -07001/**
2 * Copyright 2017 Google Inc.
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
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053017#ifdef ENABLE_IPMI_SNOOP
18#include "ipmisnoop/ipmisnoop.hpp"
19#endif
20
Patrick Ventureb5754fd2018-09-10 13:13:58 -070021#include "lpcsnoop/snoop.hpp"
22
William A. Kennington III6dac4c52019-12-13 15:05:00 -080023#include <endian.h>
Patrick Venture33569752018-03-12 18:56:14 -070024#include <fcntl.h>
Benjamin Faire7b07f02018-07-02 09:51:37 -070025#include <getopt.h>
Patrick Ventureb5754fd2018-09-10 13:13:58 -070026#include <sys/epoll.h>
27#include <systemd/sd-event.h>
Patrick Venture33569752018-03-12 18:56:14 -070028#include <unistd.h>
29
Patrick Venture33569752018-03-12 18:56:14 -070030#include <cstdint>
Kun Yi1c16ad82018-09-12 10:01:49 -070031#include <exception>
Willy Tud1ac1972022-03-21 16:20:39 -070032#include <functional>
Patrick Venture33569752018-03-12 18:56:14 -070033#include <iostream>
William A. Kennington IIIb3baa682021-12-19 20:47:05 -080034#include <optional>
Kun Yi1c16ad82018-09-12 10:01:49 -070035#include <sdeventplus/event.hpp>
36#include <sdeventplus/source/event.hpp>
37#include <sdeventplus/source/io.hpp>
William A. Kennington III6a5e0a12021-12-19 20:47:34 -080038#include <sdeventplus/source/signal.hpp>
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053039#include <span>
William A. Kennington III6a5e0a12021-12-19 20:47:34 -080040#include <stdplus/signal.hpp>
Patrick Venture33569752018-03-12 18:56:14 -070041#include <thread>
42
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053043#ifdef ENABLE_IPMI_SNOOP
44#include <xyz/openbmc_project/State/Boot/Raw/server.hpp>
45#endif
46
Benjamin Fairf69ad7e2018-07-13 13:41:10 -070047static size_t codeSize = 1; /* Size of each POST code in bytes */
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053048const char* defaultHostInstances = "0";
49const uint8_t minPositionVal = 0;
50const uint8_t maxPositionVal = 5;
51
52#ifdef ENABLE_IPMI_SNOOP
53std::vector<std::unique_ptr<IpmiPostReporter>> reporters;
54#endif
55
56#ifdef ENABLE_IPMI_SNOOP
57void IpmiPostReporter::getSelectorPositionSignal(sdbusplus::bus::bus& bus)
58{
59 size_t posVal = 0;
60
61 matchSignal = std::make_unique<sdbusplus::bus::match_t>(
62 bus,
63 sdbusplus::bus::match::rules::propertiesChanged(selectorObject,
64 selectorIface),
65 [&](sdbusplus::message::message& msg) {
66 std::string objectName;
67 std::map<std::string, Selector::PropertiesVariant> msgData;
68 msg.read(objectName, msgData);
69
70 auto valPropMap = msgData.find("Position");
71 {
72 if (valPropMap == msgData.end())
73 {
74 std::cerr << "Position property not found " << std::endl;
75 return;
76 }
77
78 posVal = std::get<size_t>(valPropMap->second);
79
80 if (posVal > minPositionVal && posVal < maxPositionVal)
81 {
82 std::tuple<uint64_t, secondary_post_code_t> postcodes =
83 reporters[posVal - 1]->value();
84 uint64_t postcode = std::get<uint64_t>(postcodes);
85
86 // write postcode into seven segment display
87 if (postCodeDisplay(postcode) < 0)
88 {
89 fprintf(stderr, "Error in display the postcode\n");
90 }
91 }
92 }
93 });
94}
95#endif
Benjamin Faire7b07f02018-07-02 09:51:37 -070096
Benjamin Faire7b07f02018-07-02 09:51:37 -070097static void usage(const char* name)
98{
99 fprintf(stderr,
100 "Usage: %s [-d <DEVICE>]\n"
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700101 " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. "
102 "Default is %zu\n"
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530103 " -d, --device <DEVICE> use <DEVICE> file.\n"
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530104 " -h, --host <host instances> . Default is '%s'\n"
William A. Kennington III66efa632020-04-16 18:46:20 -0700105 " -v, --verbose Prints verbose information while running\n\n",
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530106 name, codeSize, defaultHostInstances);
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700107}
108
Patrick Venture33569752018-03-12 18:56:14 -0700109/*
Kun Yieb312312018-06-13 09:20:50 -0700110 * Callback handling IO event from the POST code fd. i.e. there is new
111 * POST code available to read.
112 */
Willy Tud1ac1972022-03-21 16:20:39 -0700113void PostCodeEventHandler(PostReporter* reporter, bool verbose,
114 sdeventplus::source::IO& s, int postFd, uint32_t)
Kun Yieb312312018-06-13 09:20:50 -0700115{
William A. Kennington III6dac4c52019-12-13 15:05:00 -0800116 uint64_t code = 0;
William A. Kennington III0f964b42020-04-16 18:46:03 -0700117 ssize_t readb;
118 while ((readb = read(postFd, &code, codeSize)) > 0)
Kun Yi1c16ad82018-09-12 10:01:49 -0700119 {
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800120 code = le64toh(code);
William A. Kennington III66efa632020-04-16 18:46:20 -0700121 if (verbose)
122 {
123 fprintf(stderr, "Code: 0x%" PRIx64 "\n", code);
124 }
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800125 // HACK: Always send property changed signal even for the same code
126 // since we are single threaded, external users will never see the
127 // first value.
Manojkiran Edaba5258f2021-02-25 13:23:33 +0530128 reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true);
129 reporter->value(std::make_tuple(code, secondary_post_code_t{}));
William A. Kennington III0f964b42020-04-16 18:46:03 -0700130
131 // read depends on old data being cleared since it doens't always read
132 // the full code size
133 code = 0;
Kun Yi1c16ad82018-09-12 10:01:49 -0700134 }
William A. Kennington III0f964b42020-04-16 18:46:03 -0700135
136 if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
137 {
138 return;
139 }
140
141 /* Read failure. */
142 if (readb == 0)
143 {
144 fprintf(stderr, "Unexpected EOF reading postcode\n");
145 }
146 else
147 {
148 fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno));
149 }
150 s.get_event().exit(1);
Kun Yieb312312018-06-13 09:20:50 -0700151}
152
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530153#ifdef ENABLE_IPMI_SNOOP
154// handle muti-host D-bus
155int postCodeIpmiHandler(const std::string& snoopObject,
156 const std::string& snoopDbus, sdbusplus::bus::bus& bus,
157 std::span<std::string> host)
158{
159 int ret = 0;
160
161 try
162 {
163 for (size_t iteration = 0; iteration < host.size(); iteration++)
164 {
165 std::string objPathInst = snoopObject + host[iteration];
166
167 sdbusplus::server::manager_t m{bus, objPathInst.c_str()};
168
169 /* Create a monitor object and let it do all the rest */
170 reporters.emplace_back(
171 std::make_unique<IpmiPostReporter>(bus, objPathInst.c_str()));
172
173 reporters[iteration]->emit_object_added();
174 }
175
176 bus.request_name(snoopDbus.c_str());
177 reporters[0]->getSelectorPositionSignal(bus);
178 }
179 catch (const std::exception& e)
180 {
181 fprintf(stderr, "%s\n", e.what());
182 }
183
184 // Configure seven segment dsiplay connected to GPIOs as output
185 ret = configGPIODirOutput();
186 if (ret < 0)
187 {
188 fprintf(stderr, "Failed find the gpio line\n");
189 }
190
191 while (true)
192 {
193 bus.process_discard();
194 bus.wait();
195 }
196 exit(EXIT_SUCCESS);
197}
198#endif
199
Kun Yieb312312018-06-13 09:20:50 -0700200/*
Patrick Venture33569752018-03-12 18:56:14 -0700201 * TODO(venture): this only listens one of the possible snoop ports, but
202 * doesn't share the namespace.
203 *
204 * This polls() the lpc snoop character device and it owns the dbus object
205 * whose value is the latest port 80h value.
206 */
207int main(int argc, char* argv[])
208{
Patrick Venture33569752018-03-12 18:56:14 -0700209
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530210#ifndef ENABLE_IPMI_SNOOP
211 int postFd = -1;
212#endif
213
214 int opt;
215 bool verbose = false;
216
217#ifdef ENABLE_IPMI_SNOOP
218 std::vector<std::string> host;
219#endif
Patrick Venture33569752018-03-12 18:56:14 -0700220 /*
221 * These string constants are only used in this method within this object
222 * and this object is the only object feeding into the final binary.
223 *
224 * If however, another object is added to this binary it would be proper
225 * to move these declarations to be global and extern to the other object.
226 */
Patrick Venture33569752018-03-12 18:56:14 -0700227
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700228 // clang-format off
Benjamin Faire7b07f02018-07-02 09:51:37 -0700229 static const struct option long_options[] = {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530230 #ifdef ENABLE_IPMI_SNOOP
231 {"host", optional_argument, NULL, 'h'},
232 #endif
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700233 {"bytes", required_argument, NULL, 'b'},
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530234 #ifndef ENABLE_IPMI_SNOOP
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530235 {"device", optional_argument, NULL, 'd'},
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530236 #endif
William A. Kennington III66efa632020-04-16 18:46:20 -0700237 {"verbose", no_argument, NULL, 'v'},
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700238 {0, 0, 0, 0}
239 };
240 // clang-format on
Benjamin Faire7b07f02018-07-02 09:51:37 -0700241
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530242 while ((opt = getopt_long(argc, argv, "h:b:d:v", long_options, NULL)) != -1)
Benjamin Faire7b07f02018-07-02 09:51:37 -0700243 {
244 switch (opt)
245 {
246 case 0:
247 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530248#ifdef ENABLE_IPMI_SNOOP
249 case 'h': {
250 std::string_view instances = optarg;
251 size_t pos = 0;
252
253 while ((pos = instances.find(" ")) != std::string::npos)
254 {
255 host.emplace_back(instances.substr(0, pos));
256 instances.remove_prefix(pos + 1);
257 }
258 host.emplace_back(instances);
259 break;
260 }
261#endif
262 case 'b': {
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700263 codeSize = atoi(optarg);
264
265 if (codeSize < 1 || codeSize > 8)
266 {
267 fprintf(stderr,
268 "Invalid POST code size '%s'. Must be "
269 "an integer from 1 to 8.\n",
270 optarg);
271 exit(EXIT_FAILURE);
272 }
273 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530274 }
275#ifndef ENABLE_IPMI_SNOOP
Benjamin Faire7b07f02018-07-02 09:51:37 -0700276 case 'd':
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530277
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530278 postFd = open(optarg, O_NONBLOCK);
279 if (postFd < 0)
280 {
281 fprintf(stderr, "Unable to open: %s\n", optarg);
282 return -1;
283 }
Benjamin Faire7b07f02018-07-02 09:51:37 -0700284 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530285#endif
William A. Kennington III66efa632020-04-16 18:46:20 -0700286 case 'v':
287 verbose = true;
288 break;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700289 default:
290 usage(argv[0]);
Benjamin Faire7b07f02018-07-02 09:51:37 -0700291 }
292 }
293
Patrick Venture33569752018-03-12 18:56:14 -0700294 auto bus = sdbusplus::bus::new_default();
295
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530296#ifdef ENABLE_IPMI_SNOOP
297 std::cout << "Verbose = " << verbose << std::endl;
298 int ret = postCodeIpmiHandler(ipmiSnoopObject, snoopDbus, bus, host);
299 if (ret < 0)
300 {
301 fprintf(stderr, "Error in postCodeIpmiHandler\n");
302 return ret;
303 }
304 return 0;
305#endif
306
307#ifndef ENABLE_IPMI_SNOOP
308 int rc = 0;
309
310 bool deferSignals = true;
311
Patrick Venture33569752018-03-12 18:56:14 -0700312 // Add systemd object manager.
Willy Tua396c852021-12-09 22:23:14 -0800313 sdbusplus::server::manager::manager snoopdManager(bus, snoopObject);
Patrick Venture33569752018-03-12 18:56:14 -0700314
315 PostReporter reporter(bus, snoopObject, deferSignals);
Kun Yieb312312018-06-13 09:20:50 -0700316 reporter.emit_object_added();
317 bus.request_name(snoopDbus);
Kun Yieb312312018-06-13 09:20:50 -0700318
Kun Yi1c16ad82018-09-12 10:01:49 -0700319 // Create sdevent and add IO source
320 try
321 {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530322 sdeventplus::Event event = sdeventplus::Event::get_default();
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530323 if (postFd > 0)
324 {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530325
326 sdeventplus::source::IO reporterSource(
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530327 event, postFd, EPOLLIN | EPOLLET,
Willy Tud1ac1972022-03-21 16:20:39 -0700328 std::bind_front(PostCodeEventHandler, &reporter, verbose));
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530329 }
Kun Yi1c16ad82018-09-12 10:01:49 -0700330 // Enable bus to handle incoming IO and bus events
331 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
William A. Kennington III6a5e0a12021-12-19 20:47:34 -0800332 auto intCb = [](sdeventplus::source::Signal& source,
333 const struct signalfd_siginfo*) {
334 source.get_event().exit(0);
335 };
336 stdplus::signal::block(SIGINT);
337 sdeventplus::source::Signal(event, SIGINT, intCb).set_floating(true);
338 stdplus::signal::block(SIGTERM);
339 sdeventplus::source::Signal(event, SIGTERM, std::move(intCb))
340 .set_floating(true);
Kun Yi1c16ad82018-09-12 10:01:49 -0700341 rc = event.loop();
342 }
343 catch (const std::exception& e)
344 {
345 fprintf(stderr, "%s\n", e.what());
346 }
Kun Yieb312312018-06-13 09:20:50 -0700347
Patrick Venture33569752018-03-12 18:56:14 -0700348 if (postFd > -1)
349 {
350 close(postFd);
351 }
352
Patrick Venture33569752018-03-12 18:56:14 -0700353 return rc;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530354#endif
Patrick Venture33569752018-03-12 18:56:14 -0700355}