blob: 5188502db208728aa70ec6b178149a7800a79486 [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
Jonathan Doman38b09462023-04-26 11:45:39 -070030#include <chrono>
Patrick Venture33569752018-03-12 18:56:14 -070031#include <cstdint>
Kun Yi1c16ad82018-09-12 10:01:49 -070032#include <exception>
Willy Tud1ac1972022-03-21 16:20:39 -070033#include <functional>
Patrick Venture33569752018-03-12 18:56:14 -070034#include <iostream>
William A. Kennington IIIb3baa682021-12-19 20:47:05 -080035#include <optional>
Kun Yi1c16ad82018-09-12 10:01:49 -070036#include <sdeventplus/event.hpp>
37#include <sdeventplus/source/event.hpp>
38#include <sdeventplus/source/io.hpp>
William A. Kennington III6a5e0a12021-12-19 20:47:34 -080039#include <sdeventplus/source/signal.hpp>
Jonathan Doman38b09462023-04-26 11:45:39 -070040#include <sdeventplus/source/time.hpp>
William A. Kennington III30751ec2022-11-21 18:38:36 -080041#include <sdeventplus/utility/sdbus.hpp>
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053042#include <span>
William A. Kennington III6a5e0a12021-12-19 20:47:34 -080043#include <stdplus/signal.hpp>
Patrick Venture33569752018-03-12 18:56:14 -070044#include <thread>
45
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053046#ifdef ENABLE_IPMI_SNOOP
47#include <xyz/openbmc_project/State/Boot/Raw/server.hpp>
48#endif
49
Benjamin Fairf69ad7e2018-07-13 13:41:10 -070050static size_t codeSize = 1; /* Size of each POST code in bytes */
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053051const char* defaultHostInstances = "0";
Jonathan Doman38b09462023-04-26 11:45:39 -070052static bool verbose = false;
Brad Bishop8f6c0ce2022-05-03 17:29:05 -040053#ifdef ENABLE_IPMI_SNOOP
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053054const uint8_t minPositionVal = 0;
55const uint8_t maxPositionVal = 5;
Brad Bishop8f6c0ce2022-05-03 17:29:05 -040056#endif
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053057
58#ifdef ENABLE_IPMI_SNOOP
59std::vector<std::unique_ptr<IpmiPostReporter>> reporters;
60#endif
61
62#ifdef ENABLE_IPMI_SNOOP
Patrick Williamsaebf87c2022-07-22 19:26:54 -050063void IpmiPostReporter::getSelectorPositionSignal(sdbusplus::bus_t& bus)
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053064{
65 size_t posVal = 0;
66
67 matchSignal = std::make_unique<sdbusplus::bus::match_t>(
68 bus,
69 sdbusplus::bus::match::rules::propertiesChanged(selectorObject,
70 selectorIface),
Patrick Williamsaebf87c2022-07-22 19:26:54 -050071 [&](sdbusplus::message_t& msg) {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053072 std::string objectName;
73 std::map<std::string, Selector::PropertiesVariant> msgData;
74 msg.read(objectName, msgData);
75
76 auto valPropMap = msgData.find("Position");
77 {
78 if (valPropMap == msgData.end())
79 {
80 std::cerr << "Position property not found " << std::endl;
81 return;
82 }
83
84 posVal = std::get<size_t>(valPropMap->second);
85
86 if (posVal > minPositionVal && posVal < maxPositionVal)
87 {
88 std::tuple<uint64_t, secondary_post_code_t> postcodes =
89 reporters[posVal - 1]->value();
90 uint64_t postcode = std::get<uint64_t>(postcodes);
91
92 // write postcode into seven segment display
93 if (postCodeDisplay(postcode) < 0)
94 {
95 fprintf(stderr, "Error in display the postcode\n");
96 }
97 }
98 }
99 });
100}
101#endif
Benjamin Faire7b07f02018-07-02 09:51:37 -0700102
Benjamin Faire7b07f02018-07-02 09:51:37 -0700103static void usage(const char* name)
104{
105 fprintf(stderr,
106 "Usage: %s [-d <DEVICE>]\n"
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700107 " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. "
108 "Default is %zu\n"
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530109 " -d, --device <DEVICE> use <DEVICE> file.\n"
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530110 " -h, --host <host instances> . Default is '%s'\n"
William A. Kennington III66efa632020-04-16 18:46:20 -0700111 " -v, --verbose Prints verbose information while running\n\n",
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530112 name, codeSize, defaultHostInstances);
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700113}
114
Jonathan Doman38b09462023-04-26 11:45:39 -0700115/**
116 * Call once for each POST code received. If the number of POST codes exceeds
117 * the configured rate limit, this function will disable the snoop device IO
118 * source until the end of the 1 second interval, then re-enable it.
119 *
120 * @return Whether the rate limit is exceeded.
121 */
122bool rateLimit(PostReporter& reporter, sdeventplus::source::IO& ioSource)
123{
124 if (reporter.rateLimit == 0)
125 {
126 // Rate limiting is disabled.
127 return false;
128 }
129
130 using Clock = sdeventplus::Clock<sdeventplus::ClockId::Monotonic>;
131
132 static constexpr std::chrono::seconds rateLimitInterval(1);
133 static unsigned int rateLimitCount = 0;
134 static Clock::time_point rateLimitEndTime;
135
136 const sdeventplus::Event& event = ioSource.get_event();
137
138 if (rateLimitCount == 0)
139 {
140 // Initialize the end time when we start a new interval
141 rateLimitEndTime = Clock(event).now() + rateLimitInterval;
142 }
143
144 if (++rateLimitCount < reporter.rateLimit)
145 {
146 return false;
147 }
148
149 rateLimitCount = 0;
150
151 if (rateLimitEndTime < Clock(event).now())
152 {
153 return false;
154 }
155
156 if (verbose)
157 {
158 fprintf(stderr, "Hit POST code rate limit - disabling temporarily\n");
159 }
160
161 ioSource.set_enabled(sdeventplus::source::Enabled::Off);
162 sdeventplus::source::Time<sdeventplus::ClockId::Monotonic>(
163 event, rateLimitEndTime, std::chrono::milliseconds(100),
164 [&ioSource](auto&, auto) {
165 if (verbose)
166 {
167 fprintf(stderr, "Reenabling POST code handler\n");
168 }
169 ioSource.set_enabled(sdeventplus::source::Enabled::On);
170 })
171 .set_floating(true);
172 return true;
173}
174
Patrick Venture33569752018-03-12 18:56:14 -0700175/*
Kun Yieb312312018-06-13 09:20:50 -0700176 * Callback handling IO event from the POST code fd. i.e. there is new
177 * POST code available to read.
178 */
Jonathan Doman38b09462023-04-26 11:45:39 -0700179void PostCodeEventHandler(PostReporter* reporter, sdeventplus::source::IO& s,
180 int postFd, uint32_t)
Kun Yieb312312018-06-13 09:20:50 -0700181{
William A. Kennington III6dac4c52019-12-13 15:05:00 -0800182 uint64_t code = 0;
William A. Kennington III0f964b42020-04-16 18:46:03 -0700183 ssize_t readb;
Jonathan Doman38b09462023-04-26 11:45:39 -0700184
William A. Kennington III0f964b42020-04-16 18:46:03 -0700185 while ((readb = read(postFd, &code, codeSize)) > 0)
Kun Yi1c16ad82018-09-12 10:01:49 -0700186 {
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800187 code = le64toh(code);
William A. Kennington III66efa632020-04-16 18:46:20 -0700188 if (verbose)
189 {
190 fprintf(stderr, "Code: 0x%" PRIx64 "\n", code);
191 }
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800192 // HACK: Always send property changed signal even for the same code
193 // since we are single threaded, external users will never see the
194 // first value.
Manojkiran Edaba5258f2021-02-25 13:23:33 +0530195 reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true);
196 reporter->value(std::make_tuple(code, secondary_post_code_t{}));
William A. Kennington III0f964b42020-04-16 18:46:03 -0700197
198 // read depends on old data being cleared since it doens't always read
199 // the full code size
200 code = 0;
Jonathan Doman38b09462023-04-26 11:45:39 -0700201
202 if (rateLimit(*reporter, s))
203 {
204 return;
205 }
Kun Yi1c16ad82018-09-12 10:01:49 -0700206 }
William A. Kennington III0f964b42020-04-16 18:46:03 -0700207
208 if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
209 {
210 return;
211 }
212
213 /* Read failure. */
214 if (readb == 0)
215 {
216 fprintf(stderr, "Unexpected EOF reading postcode\n");
217 }
218 else
219 {
220 fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno));
221 }
222 s.get_event().exit(1);
Kun Yieb312312018-06-13 09:20:50 -0700223}
224
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530225#ifdef ENABLE_IPMI_SNOOP
226// handle muti-host D-bus
227int postCodeIpmiHandler(const std::string& snoopObject,
Patrick Williamsaebf87c2022-07-22 19:26:54 -0500228 const std::string& snoopDbus, sdbusplus::bus_t& bus,
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530229 std::span<std::string> host)
230{
231 int ret = 0;
232
233 try
234 {
235 for (size_t iteration = 0; iteration < host.size(); iteration++)
236 {
237 std::string objPathInst = snoopObject + host[iteration];
238
239 sdbusplus::server::manager_t m{bus, objPathInst.c_str()};
240
241 /* Create a monitor object and let it do all the rest */
242 reporters.emplace_back(
243 std::make_unique<IpmiPostReporter>(bus, objPathInst.c_str()));
244
245 reporters[iteration]->emit_object_added();
246 }
247
248 bus.request_name(snoopDbus.c_str());
Kumar Thangavelaee65402022-08-09 17:21:48 +0530249
250 /* sevenSegmentLedEnabled flag is unset when GPIO pins are not there 7
251 seg display for fewer platforms. So, the code for postcode dispay and
252 Get Selector position can be skipped in those platforms.
253 */
254 if (sevenSegmentLedEnabled)
255 {
256 reporters[0]->getSelectorPositionSignal(bus);
257 }
258 else
259 {
260 reporters.clear();
261 }
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530262 }
263 catch (const std::exception& e)
264 {
265 fprintf(stderr, "%s\n", e.what());
266 }
267
268 // Configure seven segment dsiplay connected to GPIOs as output
269 ret = configGPIODirOutput();
270 if (ret < 0)
271 {
Kumar Thangavelaee65402022-08-09 17:21:48 +0530272 fprintf(stderr, "Failed find the gpio line. Cannot display postcodes "
273 "in seven segment display..\n");
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530274 }
275
276 while (true)
277 {
278 bus.process_discard();
279 bus.wait();
280 }
281 exit(EXIT_SUCCESS);
282}
283#endif
284
Kun Yieb312312018-06-13 09:20:50 -0700285/*
Patrick Venture33569752018-03-12 18:56:14 -0700286 * TODO(venture): this only listens one of the possible snoop ports, but
287 * doesn't share the namespace.
288 *
289 * This polls() the lpc snoop character device and it owns the dbus object
290 * whose value is the latest port 80h value.
291 */
292int main(int argc, char* argv[])
293{
Patrick Venture33569752018-03-12 18:56:14 -0700294
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530295#ifndef ENABLE_IPMI_SNOOP
296 int postFd = -1;
297#endif
Jonathan Doman38b09462023-04-26 11:45:39 -0700298 unsigned int rateLimit = 0;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530299
300 int opt;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530301
302#ifdef ENABLE_IPMI_SNOOP
303 std::vector<std::string> host;
304#endif
Patrick Venture33569752018-03-12 18:56:14 -0700305 /*
306 * These string constants are only used in this method within this object
307 * and this object is the only object feeding into the final binary.
308 *
309 * If however, another object is added to this binary it would be proper
310 * to move these declarations to be global and extern to the other object.
311 */
Patrick Venture33569752018-03-12 18:56:14 -0700312
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700313 // clang-format off
Benjamin Faire7b07f02018-07-02 09:51:37 -0700314 static const struct option long_options[] = {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530315 #ifdef ENABLE_IPMI_SNOOP
316 {"host", optional_argument, NULL, 'h'},
317 #endif
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700318 {"bytes", required_argument, NULL, 'b'},
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530319 #ifndef ENABLE_IPMI_SNOOP
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530320 {"device", optional_argument, NULL, 'd'},
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530321 #endif
Jonathan Doman38b09462023-04-26 11:45:39 -0700322 {"rate-limit", optional_argument, NULL, 'r'},
William A. Kennington III66efa632020-04-16 18:46:20 -0700323 {"verbose", no_argument, NULL, 'v'},
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700324 {0, 0, 0, 0}
325 };
326 // clang-format on
Benjamin Faire7b07f02018-07-02 09:51:37 -0700327
Jonathan Doman38b09462023-04-26 11:45:39 -0700328 while ((opt = getopt_long(argc, argv, "h:b:d:r:v", long_options, NULL)) !=
329 -1)
Benjamin Faire7b07f02018-07-02 09:51:37 -0700330 {
331 switch (opt)
332 {
333 case 0:
334 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530335#ifdef ENABLE_IPMI_SNOOP
336 case 'h': {
337 std::string_view instances = optarg;
338 size_t pos = 0;
339
340 while ((pos = instances.find(" ")) != std::string::npos)
341 {
342 host.emplace_back(instances.substr(0, pos));
343 instances.remove_prefix(pos + 1);
344 }
345 host.emplace_back(instances);
346 break;
347 }
348#endif
349 case 'b': {
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700350 codeSize = atoi(optarg);
351
352 if (codeSize < 1 || codeSize > 8)
353 {
354 fprintf(stderr,
355 "Invalid POST code size '%s'. Must be "
356 "an integer from 1 to 8.\n",
357 optarg);
358 exit(EXIT_FAILURE);
359 }
360 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530361 }
362#ifndef ENABLE_IPMI_SNOOP
Benjamin Faire7b07f02018-07-02 09:51:37 -0700363 case 'd':
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530364
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530365 postFd = open(optarg, O_NONBLOCK);
366 if (postFd < 0)
367 {
368 fprintf(stderr, "Unable to open: %s\n", optarg);
369 return -1;
370 }
Benjamin Faire7b07f02018-07-02 09:51:37 -0700371 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530372#endif
Jonathan Doman38b09462023-04-26 11:45:39 -0700373 case 'r': {
374 int argVal = -1;
375 try
376 {
377 argVal = std::stoi(optarg);
378 }
379 catch (...)
380 {
381 }
382
383 if (argVal < 1)
384 {
385 fprintf(stderr, "Invalid rate limit '%s'. Must be >= 1.\n",
386 optarg);
387 return EXIT_FAILURE;
388 }
389
390 rateLimit = static_cast<unsigned int>(argVal);
391 fprintf(stderr, "Rate limiting to %d POST codes per second.\n",
392 argVal);
393 break;
394 }
William A. Kennington III66efa632020-04-16 18:46:20 -0700395 case 'v':
396 verbose = true;
397 break;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700398 default:
399 usage(argv[0]);
Jonathan Doman38b09462023-04-26 11:45:39 -0700400 return EXIT_FAILURE;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700401 }
402 }
403
Patrick Venture33569752018-03-12 18:56:14 -0700404 auto bus = sdbusplus::bus::new_default();
405
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530406#ifdef ENABLE_IPMI_SNOOP
407 std::cout << "Verbose = " << verbose << std::endl;
408 int ret = postCodeIpmiHandler(ipmiSnoopObject, snoopDbus, bus, host);
409 if (ret < 0)
410 {
411 fprintf(stderr, "Error in postCodeIpmiHandler\n");
412 return ret;
413 }
414 return 0;
415#endif
416
417#ifndef ENABLE_IPMI_SNOOP
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530418
419 bool deferSignals = true;
420
Patrick Venture33569752018-03-12 18:56:14 -0700421 // Add systemd object manager.
Patrick Williamsaebf87c2022-07-22 19:26:54 -0500422 sdbusplus::server::manager_t snoopdManager(bus, snoopObject);
Patrick Venture33569752018-03-12 18:56:14 -0700423
424 PostReporter reporter(bus, snoopObject, deferSignals);
Kun Yieb312312018-06-13 09:20:50 -0700425 reporter.emit_object_added();
426 bus.request_name(snoopDbus);
Kun Yieb312312018-06-13 09:20:50 -0700427
Kun Yi1c16ad82018-09-12 10:01:49 -0700428 // Create sdevent and add IO source
429 try
430 {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530431 sdeventplus::Event event = sdeventplus::Event::get_default();
Harvey.Wu4f26b3e2022-05-12 08:54:30 +0800432 std::optional<sdeventplus::source::IO> reporterSource;
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530433 if (postFd > 0)
434 {
Jonathan Doman38b09462023-04-26 11:45:39 -0700435 reporter.rateLimit = rateLimit;
Harvey.Wu4f26b3e2022-05-12 08:54:30 +0800436 reporterSource.emplace(
Jonathan Doman38b09462023-04-26 11:45:39 -0700437 event, postFd, EPOLLIN,
438 std::bind_front(PostCodeEventHandler, &reporter));
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530439 }
Kun Yi1c16ad82018-09-12 10:01:49 -0700440 // Enable bus to handle incoming IO and bus events
William A. Kennington III30751ec2022-11-21 18:38:36 -0800441 auto intCb = [](sdeventplus::source::Signal& source,
442 const struct signalfd_siginfo*) {
William A. Kennington III6a5e0a12021-12-19 20:47:34 -0800443 source.get_event().exit(0);
444 };
445 stdplus::signal::block(SIGINT);
446 sdeventplus::source::Signal(event, SIGINT, intCb).set_floating(true);
447 stdplus::signal::block(SIGTERM);
448 sdeventplus::source::Signal(event, SIGTERM, std::move(intCb))
449 .set_floating(true);
William A. Kennington III30751ec2022-11-21 18:38:36 -0800450 return sdeventplus::utility::loopWithBus(event, bus);
Kun Yi1c16ad82018-09-12 10:01:49 -0700451 }
452 catch (const std::exception& e)
453 {
454 fprintf(stderr, "%s\n", e.what());
455 }
Kun Yieb312312018-06-13 09:20:50 -0700456
Patrick Venture33569752018-03-12 18:56:14 -0700457 if (postFd > -1)
458 {
459 close(postFd);
460 }
461
Sunita Kumari56127ef2022-10-12 05:01:20 +0000462 return 0;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530463#endif
Patrick Venture33569752018-03-12 18:56:14 -0700464}