blob: 53a075e4919130419ada2415d68c7fd7076a426e [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
Kun Yi1c16ad82018-09-12 10:01:49 -070030#include <sdeventplus/event.hpp>
31#include <sdeventplus/source/event.hpp>
32#include <sdeventplus/source/io.hpp>
William A. Kennington III6a5e0a12021-12-19 20:47:34 -080033#include <sdeventplus/source/signal.hpp>
Jonathan Doman38b09462023-04-26 11:45:39 -070034#include <sdeventplus/source/time.hpp>
William A. Kennington III30751ec2022-11-21 18:38:36 -080035#include <sdeventplus/utility/sdbus.hpp>
William A. Kennington III6a5e0a12021-12-19 20:47:34 -080036#include <stdplus/signal.hpp>
Patrick Williams0ea73572023-05-10 07:50:44 -050037
38#include <chrono>
39#include <cstdint>
40#include <exception>
41#include <functional>
42#include <iostream>
43#include <optional>
44#include <span>
Patrick Venture33569752018-03-12 18:56:14 -070045#include <thread>
46
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053047#ifdef ENABLE_IPMI_SNOOP
48#include <xyz/openbmc_project/State/Boot/Raw/server.hpp>
49#endif
50
Benjamin Fairf69ad7e2018-07-13 13:41:10 -070051static size_t codeSize = 1; /* Size of each POST code in bytes */
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053052const char* defaultHostInstances = "0";
Jonathan Doman38b09462023-04-26 11:45:39 -070053static bool verbose = false;
Brad Bishop8f6c0ce2022-05-03 17:29:05 -040054#ifdef ENABLE_IPMI_SNOOP
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053055const uint8_t minPositionVal = 0;
56const uint8_t maxPositionVal = 5;
Brad Bishop8f6c0ce2022-05-03 17:29:05 -040057#endif
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053058
59#ifdef ENABLE_IPMI_SNOOP
60std::vector<std::unique_ptr<IpmiPostReporter>> reporters;
61#endif
62
63#ifdef ENABLE_IPMI_SNOOP
Patrick Williamsaebf87c2022-07-22 19:26:54 -050064void IpmiPostReporter::getSelectorPositionSignal(sdbusplus::bus_t& bus)
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053065{
66 size_t posVal = 0;
67
68 matchSignal = std::make_unique<sdbusplus::bus::match_t>(
69 bus,
70 sdbusplus::bus::match::rules::propertiesChanged(selectorObject,
71 selectorIface),
Patrick Williamsaebf87c2022-07-22 19:26:54 -050072 [&](sdbusplus::message_t& msg) {
Patrick Williams0ea73572023-05-10 07:50:44 -050073 std::string objectName;
74 std::map<std::string, Selector::PropertiesVariant> msgData;
75 msg.read(objectName, msgData);
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053076
Patrick Williams0ea73572023-05-10 07:50:44 -050077 auto valPropMap = msgData.find("Position");
78 {
79 if (valPropMap == msgData.end())
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053080 {
Patrick Williams0ea73572023-05-10 07:50:44 -050081 std::cerr << "Position property not found " << std::endl;
82 return;
83 }
84
85 posVal = std::get<size_t>(valPropMap->second);
86
87 if (posVal > minPositionVal && posVal < maxPositionVal)
88 {
89 std::tuple<uint64_t, secondary_post_code_t> postcodes =
90 reporters[posVal - 1]->value();
91 uint64_t postcode = std::get<uint64_t>(postcodes);
92
93 // write postcode into seven segment display
94 if (postCodeDisplay(postcode) < 0)
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053095 {
Patrick Williams0ea73572023-05-10 07:50:44 -050096 fprintf(stderr, "Error in display the postcode\n");
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053097 }
98 }
Patrick Williams0ea73572023-05-10 07:50:44 -050099 }
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530100 });
101}
102#endif
Benjamin Faire7b07f02018-07-02 09:51:37 -0700103
Benjamin Faire7b07f02018-07-02 09:51:37 -0700104static void usage(const char* name)
105{
106 fprintf(stderr,
107 "Usage: %s [-d <DEVICE>]\n"
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700108 " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. "
109 "Default is %zu\n"
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530110 " -d, --device <DEVICE> use <DEVICE> file.\n"
Jonathan Domanfd2430d2023-05-03 11:44:01 -0700111 " -r, --rate-limit=<N> Only process N POST codes from the "
112 "device per second.\n"
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530113 " -h, --host <host instances> . Default is '%s'\n"
William A. Kennington III66efa632020-04-16 18:46:20 -0700114 " -v, --verbose Prints verbose information while running\n\n",
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530115 name, codeSize, defaultHostInstances);
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700116}
117
Jonathan Doman38b09462023-04-26 11:45:39 -0700118/**
119 * Call once for each POST code received. If the number of POST codes exceeds
120 * the configured rate limit, this function will disable the snoop device IO
121 * source until the end of the 1 second interval, then re-enable it.
122 *
123 * @return Whether the rate limit is exceeded.
124 */
125bool rateLimit(PostReporter& reporter, sdeventplus::source::IO& ioSource)
126{
127 if (reporter.rateLimit == 0)
128 {
129 // Rate limiting is disabled.
130 return false;
131 }
132
133 using Clock = sdeventplus::Clock<sdeventplus::ClockId::Monotonic>;
134
135 static constexpr std::chrono::seconds rateLimitInterval(1);
136 static unsigned int rateLimitCount = 0;
137 static Clock::time_point rateLimitEndTime;
138
139 const sdeventplus::Event& event = ioSource.get_event();
140
141 if (rateLimitCount == 0)
142 {
143 // Initialize the end time when we start a new interval
144 rateLimitEndTime = Clock(event).now() + rateLimitInterval;
145 }
146
147 if (++rateLimitCount < reporter.rateLimit)
148 {
149 return false;
150 }
151
152 rateLimitCount = 0;
153
154 if (rateLimitEndTime < Clock(event).now())
155 {
156 return false;
157 }
158
159 if (verbose)
160 {
161 fprintf(stderr, "Hit POST code rate limit - disabling temporarily\n");
162 }
163
164 ioSource.set_enabled(sdeventplus::source::Enabled::Off);
165 sdeventplus::source::Time<sdeventplus::ClockId::Monotonic>(
166 event, rateLimitEndTime, std::chrono::milliseconds(100),
167 [&ioSource](auto&, auto) {
Patrick Williams0ea73572023-05-10 07:50:44 -0500168 if (verbose)
169 {
170 fprintf(stderr, "Reenabling POST code handler\n");
171 }
172 ioSource.set_enabled(sdeventplus::source::Enabled::On);
Jonathan Doman38b09462023-04-26 11:45:39 -0700173 })
174 .set_floating(true);
175 return true;
176}
177
Patrick Venture33569752018-03-12 18:56:14 -0700178/*
Kun Yieb312312018-06-13 09:20:50 -0700179 * Callback handling IO event from the POST code fd. i.e. there is new
180 * POST code available to read.
181 */
Jonathan Doman38b09462023-04-26 11:45:39 -0700182void PostCodeEventHandler(PostReporter* reporter, sdeventplus::source::IO& s,
183 int postFd, uint32_t)
Kun Yieb312312018-06-13 09:20:50 -0700184{
William A. Kennington III6dac4c52019-12-13 15:05:00 -0800185 uint64_t code = 0;
William A. Kennington III0f964b42020-04-16 18:46:03 -0700186 ssize_t readb;
Jonathan Doman38b09462023-04-26 11:45:39 -0700187
William A. Kennington III0f964b42020-04-16 18:46:03 -0700188 while ((readb = read(postFd, &code, codeSize)) > 0)
Kun Yi1c16ad82018-09-12 10:01:49 -0700189 {
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800190 code = le64toh(code);
William A. Kennington III66efa632020-04-16 18:46:20 -0700191 if (verbose)
192 {
193 fprintf(stderr, "Code: 0x%" PRIx64 "\n", code);
194 }
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800195 // HACK: Always send property changed signal even for the same code
196 // since we are single threaded, external users will never see the
197 // first value.
Manojkiran Edaba5258f2021-02-25 13:23:33 +0530198 reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true);
199 reporter->value(std::make_tuple(code, secondary_post_code_t{}));
William A. Kennington III0f964b42020-04-16 18:46:03 -0700200
201 // read depends on old data being cleared since it doens't always read
202 // the full code size
203 code = 0;
Jonathan Doman38b09462023-04-26 11:45:39 -0700204
205 if (rateLimit(*reporter, s))
206 {
207 return;
208 }
Kun Yi1c16ad82018-09-12 10:01:49 -0700209 }
William A. Kennington III0f964b42020-04-16 18:46:03 -0700210
211 if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
212 {
213 return;
214 }
215
216 /* Read failure. */
217 if (readb == 0)
218 {
219 fprintf(stderr, "Unexpected EOF reading postcode\n");
220 }
221 else
222 {
223 fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno));
224 }
225 s.get_event().exit(1);
Kun Yieb312312018-06-13 09:20:50 -0700226}
227
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530228#ifdef ENABLE_IPMI_SNOOP
229// handle muti-host D-bus
230int postCodeIpmiHandler(const std::string& snoopObject,
Patrick Williamsaebf87c2022-07-22 19:26:54 -0500231 const std::string& snoopDbus, sdbusplus::bus_t& bus,
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530232 std::span<std::string> host)
233{
234 int ret = 0;
235
236 try
237 {
238 for (size_t iteration = 0; iteration < host.size(); iteration++)
239 {
240 std::string objPathInst = snoopObject + host[iteration];
241
242 sdbusplus::server::manager_t m{bus, objPathInst.c_str()};
243
244 /* Create a monitor object and let it do all the rest */
245 reporters.emplace_back(
246 std::make_unique<IpmiPostReporter>(bus, objPathInst.c_str()));
247
248 reporters[iteration]->emit_object_added();
249 }
250
251 bus.request_name(snoopDbus.c_str());
Kumar Thangavelaee65402022-08-09 17:21:48 +0530252
253 /* sevenSegmentLedEnabled flag is unset when GPIO pins are not there 7
254 seg display for fewer platforms. So, the code for postcode dispay and
255 Get Selector position can be skipped in those platforms.
256 */
257 if (sevenSegmentLedEnabled)
258 {
259 reporters[0]->getSelectorPositionSignal(bus);
260 }
261 else
262 {
263 reporters.clear();
264 }
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530265 }
266 catch (const std::exception& e)
267 {
268 fprintf(stderr, "%s\n", e.what());
269 }
270
271 // Configure seven segment dsiplay connected to GPIOs as output
272 ret = configGPIODirOutput();
273 if (ret < 0)
274 {
Kumar Thangavelaee65402022-08-09 17:21:48 +0530275 fprintf(stderr, "Failed find the gpio line. Cannot display postcodes "
276 "in seven segment display..\n");
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530277 }
278
279 while (true)
280 {
281 bus.process_discard();
282 bus.wait();
283 }
284 exit(EXIT_SUCCESS);
285}
286#endif
287
Kun Yieb312312018-06-13 09:20:50 -0700288/*
Patrick Venture33569752018-03-12 18:56:14 -0700289 * TODO(venture): this only listens one of the possible snoop ports, but
290 * doesn't share the namespace.
291 *
292 * This polls() the lpc snoop character device and it owns the dbus object
293 * whose value is the latest port 80h value.
294 */
295int main(int argc, char* argv[])
296{
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530297#ifndef ENABLE_IPMI_SNOOP
298 int postFd = -1;
Jonathan Doman38b09462023-04-26 11:45:39 -0700299 unsigned int rateLimit = 0;
Jonathan Domanfd2430d2023-05-03 11:44:01 -0700300#endif
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530301
302 int opt;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530303
304#ifdef ENABLE_IPMI_SNOOP
305 std::vector<std::string> host;
306#endif
Patrick Venture33569752018-03-12 18:56:14 -0700307 /*
308 * These string constants are only used in this method within this object
309 * and this object is the only object feeding into the final binary.
310 *
311 * If however, another object is added to this binary it would be proper
312 * to move these declarations to be global and extern to the other object.
313 */
Patrick Venture33569752018-03-12 18:56:14 -0700314
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700315 // clang-format off
Benjamin Faire7b07f02018-07-02 09:51:37 -0700316 static const struct option long_options[] = {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530317 #ifdef ENABLE_IPMI_SNOOP
318 {"host", optional_argument, NULL, 'h'},
319 #endif
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700320 {"bytes", required_argument, NULL, 'b'},
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530321 #ifndef ENABLE_IPMI_SNOOP
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530322 {"device", optional_argument, NULL, 'd'},
Jonathan Doman38b09462023-04-26 11:45:39 -0700323 {"rate-limit", optional_argument, NULL, 'r'},
Jonathan Domanfd2430d2023-05-03 11:44:01 -0700324 #endif
William A. Kennington III66efa632020-04-16 18:46:20 -0700325 {"verbose", no_argument, NULL, 'v'},
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700326 {0, 0, 0, 0}
327 };
328 // clang-format on
Benjamin Faire7b07f02018-07-02 09:51:37 -0700329
Jonathan Doman38b09462023-04-26 11:45:39 -0700330 while ((opt = getopt_long(argc, argv, "h:b:d:r:v", long_options, NULL)) !=
331 -1)
Benjamin Faire7b07f02018-07-02 09:51:37 -0700332 {
333 switch (opt)
334 {
335 case 0:
336 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530337#ifdef ENABLE_IPMI_SNOOP
Patrick Williams0ea73572023-05-10 07:50:44 -0500338 case 'h':
339 {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530340 std::string_view instances = optarg;
341 size_t pos = 0;
342
343 while ((pos = instances.find(" ")) != std::string::npos)
344 {
345 host.emplace_back(instances.substr(0, pos));
346 instances.remove_prefix(pos + 1);
347 }
348 host.emplace_back(instances);
349 break;
350 }
351#endif
Patrick Williams0ea73572023-05-10 07:50:44 -0500352 case 'b':
353 {
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700354 codeSize = atoi(optarg);
355
356 if (codeSize < 1 || codeSize > 8)
357 {
358 fprintf(stderr,
359 "Invalid POST code size '%s'. Must be "
360 "an integer from 1 to 8.\n",
361 optarg);
362 exit(EXIT_FAILURE);
363 }
364 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530365 }
366#ifndef ENABLE_IPMI_SNOOP
Benjamin Faire7b07f02018-07-02 09:51:37 -0700367 case 'd':
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530368
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530369 postFd = open(optarg, O_NONBLOCK);
370 if (postFd < 0)
371 {
372 fprintf(stderr, "Unable to open: %s\n", optarg);
373 return -1;
374 }
Benjamin Faire7b07f02018-07-02 09:51:37 -0700375 break;
Patrick Williams0ea73572023-05-10 07:50:44 -0500376 case 'r':
377 {
Jonathan Doman38b09462023-04-26 11:45:39 -0700378 int argVal = -1;
379 try
380 {
381 argVal = std::stoi(optarg);
382 }
383 catch (...)
Patrick Williams0ea73572023-05-10 07:50:44 -0500384 {}
Jonathan Doman38b09462023-04-26 11:45:39 -0700385
386 if (argVal < 1)
387 {
388 fprintf(stderr, "Invalid rate limit '%s'. Must be >= 1.\n",
389 optarg);
390 return EXIT_FAILURE;
391 }
392
393 rateLimit = static_cast<unsigned int>(argVal);
394 fprintf(stderr, "Rate limiting to %d POST codes per second.\n",
395 argVal);
396 break;
397 }
Jonathan Domanfd2430d2023-05-03 11:44:01 -0700398#endif
William A. Kennington III66efa632020-04-16 18:46:20 -0700399 case 'v':
400 verbose = true;
401 break;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700402 default:
403 usage(argv[0]);
Jonathan Doman38b09462023-04-26 11:45:39 -0700404 return EXIT_FAILURE;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700405 }
406 }
407
Patrick Venture33569752018-03-12 18:56:14 -0700408 auto bus = sdbusplus::bus::new_default();
409
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530410#ifdef ENABLE_IPMI_SNOOP
411 std::cout << "Verbose = " << verbose << std::endl;
412 int ret = postCodeIpmiHandler(ipmiSnoopObject, snoopDbus, bus, host);
413 if (ret < 0)
414 {
415 fprintf(stderr, "Error in postCodeIpmiHandler\n");
416 return ret;
417 }
418 return 0;
419#endif
420
421#ifndef ENABLE_IPMI_SNOOP
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530422
423 bool deferSignals = true;
424
Patrick Venture33569752018-03-12 18:56:14 -0700425 // Add systemd object manager.
Patrick Williamsaebf87c2022-07-22 19:26:54 -0500426 sdbusplus::server::manager_t snoopdManager(bus, snoopObject);
Patrick Venture33569752018-03-12 18:56:14 -0700427
428 PostReporter reporter(bus, snoopObject, deferSignals);
Kun Yieb312312018-06-13 09:20:50 -0700429 reporter.emit_object_added();
430 bus.request_name(snoopDbus);
Kun Yieb312312018-06-13 09:20:50 -0700431
Kun Yi1c16ad82018-09-12 10:01:49 -0700432 // Create sdevent and add IO source
433 try
434 {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530435 sdeventplus::Event event = sdeventplus::Event::get_default();
Harvey.Wu4f26b3e2022-05-12 08:54:30 +0800436 std::optional<sdeventplus::source::IO> reporterSource;
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530437 if (postFd > 0)
438 {
Jonathan Doman38b09462023-04-26 11:45:39 -0700439 reporter.rateLimit = rateLimit;
Harvey.Wu4f26b3e2022-05-12 08:54:30 +0800440 reporterSource.emplace(
Jonathan Doman38b09462023-04-26 11:45:39 -0700441 event, postFd, EPOLLIN,
442 std::bind_front(PostCodeEventHandler, &reporter));
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530443 }
Kun Yi1c16ad82018-09-12 10:01:49 -0700444 // Enable bus to handle incoming IO and bus events
William A. Kennington III30751ec2022-11-21 18:38:36 -0800445 auto intCb = [](sdeventplus::source::Signal& source,
446 const struct signalfd_siginfo*) {
William A. Kennington III6a5e0a12021-12-19 20:47:34 -0800447 source.get_event().exit(0);
448 };
449 stdplus::signal::block(SIGINT);
450 sdeventplus::source::Signal(event, SIGINT, intCb).set_floating(true);
451 stdplus::signal::block(SIGTERM);
452 sdeventplus::source::Signal(event, SIGTERM, std::move(intCb))
453 .set_floating(true);
William A. Kennington III30751ec2022-11-21 18:38:36 -0800454 return sdeventplus::utility::loopWithBus(event, bus);
Kun Yi1c16ad82018-09-12 10:01:49 -0700455 }
456 catch (const std::exception& e)
457 {
458 fprintf(stderr, "%s\n", e.what());
459 }
Kun Yieb312312018-06-13 09:20:50 -0700460
Patrick Venture33569752018-03-12 18:56:14 -0700461 if (postFd > -1)
462 {
463 close(postFd);
464 }
465
Sunita Kumari56127ef2022-10-12 05:01:20 +0000466 return 0;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530467#endif
Patrick Venture33569752018-03-12 18:56:14 -0700468}