blob: 62a08dafe4c160206046102c23c18000e4d2cd7c [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"
Jonathan Domanfd2430d2023-05-03 11:44:01 -0700110 " -r, --rate-limit=<N> Only process N POST codes from the "
111 "device per second.\n"
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530112 " -h, --host <host instances> . Default is '%s'\n"
William A. Kennington III66efa632020-04-16 18:46:20 -0700113 " -v, --verbose Prints verbose information while running\n\n",
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530114 name, codeSize, defaultHostInstances);
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700115}
116
Jonathan Doman38b09462023-04-26 11:45:39 -0700117/**
118 * Call once for each POST code received. If the number of POST codes exceeds
119 * the configured rate limit, this function will disable the snoop device IO
120 * source until the end of the 1 second interval, then re-enable it.
121 *
122 * @return Whether the rate limit is exceeded.
123 */
124bool rateLimit(PostReporter& reporter, sdeventplus::source::IO& ioSource)
125{
126 if (reporter.rateLimit == 0)
127 {
128 // Rate limiting is disabled.
129 return false;
130 }
131
132 using Clock = sdeventplus::Clock<sdeventplus::ClockId::Monotonic>;
133
134 static constexpr std::chrono::seconds rateLimitInterval(1);
135 static unsigned int rateLimitCount = 0;
136 static Clock::time_point rateLimitEndTime;
137
138 const sdeventplus::Event& event = ioSource.get_event();
139
140 if (rateLimitCount == 0)
141 {
142 // Initialize the end time when we start a new interval
143 rateLimitEndTime = Clock(event).now() + rateLimitInterval;
144 }
145
146 if (++rateLimitCount < reporter.rateLimit)
147 {
148 return false;
149 }
150
151 rateLimitCount = 0;
152
153 if (rateLimitEndTime < Clock(event).now())
154 {
155 return false;
156 }
157
158 if (verbose)
159 {
160 fprintf(stderr, "Hit POST code rate limit - disabling temporarily\n");
161 }
162
163 ioSource.set_enabled(sdeventplus::source::Enabled::Off);
164 sdeventplus::source::Time<sdeventplus::ClockId::Monotonic>(
165 event, rateLimitEndTime, std::chrono::milliseconds(100),
166 [&ioSource](auto&, auto) {
167 if (verbose)
168 {
169 fprintf(stderr, "Reenabling POST code handler\n");
170 }
171 ioSource.set_enabled(sdeventplus::source::Enabled::On);
172 })
173 .set_floating(true);
174 return true;
175}
176
Patrick Venture33569752018-03-12 18:56:14 -0700177/*
Kun Yieb312312018-06-13 09:20:50 -0700178 * Callback handling IO event from the POST code fd. i.e. there is new
179 * POST code available to read.
180 */
Jonathan Doman38b09462023-04-26 11:45:39 -0700181void PostCodeEventHandler(PostReporter* reporter, sdeventplus::source::IO& s,
182 int postFd, uint32_t)
Kun Yieb312312018-06-13 09:20:50 -0700183{
William A. Kennington III6dac4c52019-12-13 15:05:00 -0800184 uint64_t code = 0;
William A. Kennington III0f964b42020-04-16 18:46:03 -0700185 ssize_t readb;
Jonathan Doman38b09462023-04-26 11:45:39 -0700186
William A. Kennington III0f964b42020-04-16 18:46:03 -0700187 while ((readb = read(postFd, &code, codeSize)) > 0)
Kun Yi1c16ad82018-09-12 10:01:49 -0700188 {
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800189 code = le64toh(code);
William A. Kennington III66efa632020-04-16 18:46:20 -0700190 if (verbose)
191 {
192 fprintf(stderr, "Code: 0x%" PRIx64 "\n", code);
193 }
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800194 // HACK: Always send property changed signal even for the same code
195 // since we are single threaded, external users will never see the
196 // first value.
Manojkiran Edaba5258f2021-02-25 13:23:33 +0530197 reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true);
198 reporter->value(std::make_tuple(code, secondary_post_code_t{}));
William A. Kennington III0f964b42020-04-16 18:46:03 -0700199
200 // read depends on old data being cleared since it doens't always read
201 // the full code size
202 code = 0;
Jonathan Doman38b09462023-04-26 11:45:39 -0700203
204 if (rateLimit(*reporter, s))
205 {
206 return;
207 }
Kun Yi1c16ad82018-09-12 10:01:49 -0700208 }
William A. Kennington III0f964b42020-04-16 18:46:03 -0700209
210 if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
211 {
212 return;
213 }
214
215 /* Read failure. */
216 if (readb == 0)
217 {
218 fprintf(stderr, "Unexpected EOF reading postcode\n");
219 }
220 else
221 {
222 fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno));
223 }
224 s.get_event().exit(1);
Kun Yieb312312018-06-13 09:20:50 -0700225}
226
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530227#ifdef ENABLE_IPMI_SNOOP
228// handle muti-host D-bus
229int postCodeIpmiHandler(const std::string& snoopObject,
Patrick Williamsaebf87c2022-07-22 19:26:54 -0500230 const std::string& snoopDbus, sdbusplus::bus_t& bus,
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530231 std::span<std::string> host)
232{
233 int ret = 0;
234
235 try
236 {
237 for (size_t iteration = 0; iteration < host.size(); iteration++)
238 {
239 std::string objPathInst = snoopObject + host[iteration];
240
241 sdbusplus::server::manager_t m{bus, objPathInst.c_str()};
242
243 /* Create a monitor object and let it do all the rest */
244 reporters.emplace_back(
245 std::make_unique<IpmiPostReporter>(bus, objPathInst.c_str()));
246
247 reporters[iteration]->emit_object_added();
248 }
249
250 bus.request_name(snoopDbus.c_str());
Kumar Thangavelaee65402022-08-09 17:21:48 +0530251
252 /* sevenSegmentLedEnabled flag is unset when GPIO pins are not there 7
253 seg display for fewer platforms. So, the code for postcode dispay and
254 Get Selector position can be skipped in those platforms.
255 */
256 if (sevenSegmentLedEnabled)
257 {
258 reporters[0]->getSelectorPositionSignal(bus);
259 }
260 else
261 {
262 reporters.clear();
263 }
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530264 }
265 catch (const std::exception& e)
266 {
267 fprintf(stderr, "%s\n", e.what());
268 }
269
270 // Configure seven segment dsiplay connected to GPIOs as output
271 ret = configGPIODirOutput();
272 if (ret < 0)
273 {
Kumar Thangavelaee65402022-08-09 17:21:48 +0530274 fprintf(stderr, "Failed find the gpio line. Cannot display postcodes "
275 "in seven segment display..\n");
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530276 }
277
278 while (true)
279 {
280 bus.process_discard();
281 bus.wait();
282 }
283 exit(EXIT_SUCCESS);
284}
285#endif
286
Kun Yieb312312018-06-13 09:20:50 -0700287/*
Patrick Venture33569752018-03-12 18:56:14 -0700288 * TODO(venture): this only listens one of the possible snoop ports, but
289 * doesn't share the namespace.
290 *
291 * This polls() the lpc snoop character device and it owns the dbus object
292 * whose value is the latest port 80h value.
293 */
294int main(int argc, char* argv[])
295{
Patrick Venture33569752018-03-12 18:56:14 -0700296
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
338 case 'h': {
339 std::string_view instances = optarg;
340 size_t pos = 0;
341
342 while ((pos = instances.find(" ")) != std::string::npos)
343 {
344 host.emplace_back(instances.substr(0, pos));
345 instances.remove_prefix(pos + 1);
346 }
347 host.emplace_back(instances);
348 break;
349 }
350#endif
351 case 'b': {
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700352 codeSize = atoi(optarg);
353
354 if (codeSize < 1 || codeSize > 8)
355 {
356 fprintf(stderr,
357 "Invalid POST code size '%s'. Must be "
358 "an integer from 1 to 8.\n",
359 optarg);
360 exit(EXIT_FAILURE);
361 }
362 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530363 }
364#ifndef ENABLE_IPMI_SNOOP
Benjamin Faire7b07f02018-07-02 09:51:37 -0700365 case 'd':
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530366
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530367 postFd = open(optarg, O_NONBLOCK);
368 if (postFd < 0)
369 {
370 fprintf(stderr, "Unable to open: %s\n", optarg);
371 return -1;
372 }
Benjamin Faire7b07f02018-07-02 09:51:37 -0700373 break;
Jonathan Doman38b09462023-04-26 11:45:39 -0700374 case 'r': {
375 int argVal = -1;
376 try
377 {
378 argVal = std::stoi(optarg);
379 }
380 catch (...)
381 {
382 }
383
384 if (argVal < 1)
385 {
386 fprintf(stderr, "Invalid rate limit '%s'. Must be >= 1.\n",
387 optarg);
388 return EXIT_FAILURE;
389 }
390
391 rateLimit = static_cast<unsigned int>(argVal);
392 fprintf(stderr, "Rate limiting to %d POST codes per second.\n",
393 argVal);
394 break;
395 }
Jonathan Domanfd2430d2023-05-03 11:44:01 -0700396#endif
William A. Kennington III66efa632020-04-16 18:46:20 -0700397 case 'v':
398 verbose = true;
399 break;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700400 default:
401 usage(argv[0]);
Jonathan Doman38b09462023-04-26 11:45:39 -0700402 return EXIT_FAILURE;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700403 }
404 }
405
Patrick Venture33569752018-03-12 18:56:14 -0700406 auto bus = sdbusplus::bus::new_default();
407
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530408#ifdef ENABLE_IPMI_SNOOP
409 std::cout << "Verbose = " << verbose << std::endl;
410 int ret = postCodeIpmiHandler(ipmiSnoopObject, snoopDbus, bus, host);
411 if (ret < 0)
412 {
413 fprintf(stderr, "Error in postCodeIpmiHandler\n");
414 return ret;
415 }
416 return 0;
417#endif
418
419#ifndef ENABLE_IPMI_SNOOP
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530420
421 bool deferSignals = true;
422
Patrick Venture33569752018-03-12 18:56:14 -0700423 // Add systemd object manager.
Patrick Williamsaebf87c2022-07-22 19:26:54 -0500424 sdbusplus::server::manager_t snoopdManager(bus, snoopObject);
Patrick Venture33569752018-03-12 18:56:14 -0700425
426 PostReporter reporter(bus, snoopObject, deferSignals);
Kun Yieb312312018-06-13 09:20:50 -0700427 reporter.emit_object_added();
428 bus.request_name(snoopDbus);
Kun Yieb312312018-06-13 09:20:50 -0700429
Kun Yi1c16ad82018-09-12 10:01:49 -0700430 // Create sdevent and add IO source
431 try
432 {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530433 sdeventplus::Event event = sdeventplus::Event::get_default();
Harvey.Wu4f26b3e2022-05-12 08:54:30 +0800434 std::optional<sdeventplus::source::IO> reporterSource;
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530435 if (postFd > 0)
436 {
Jonathan Doman38b09462023-04-26 11:45:39 -0700437 reporter.rateLimit = rateLimit;
Harvey.Wu4f26b3e2022-05-12 08:54:30 +0800438 reporterSource.emplace(
Jonathan Doman38b09462023-04-26 11:45:39 -0700439 event, postFd, EPOLLIN,
440 std::bind_front(PostCodeEventHandler, &reporter));
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530441 }
Kun Yi1c16ad82018-09-12 10:01:49 -0700442 // Enable bus to handle incoming IO and bus events
William A. Kennington III30751ec2022-11-21 18:38:36 -0800443 auto intCb = [](sdeventplus::source::Signal& source,
444 const struct signalfd_siginfo*) {
William A. Kennington III6a5e0a12021-12-19 20:47:34 -0800445 source.get_event().exit(0);
446 };
447 stdplus::signal::block(SIGINT);
448 sdeventplus::source::Signal(event, SIGINT, intCb).set_floating(true);
449 stdplus::signal::block(SIGTERM);
450 sdeventplus::source::Signal(event, SIGTERM, std::move(intCb))
451 .set_floating(true);
William A. Kennington III30751ec2022-11-21 18:38:36 -0800452 return sdeventplus::utility::loopWithBus(event, bus);
Kun Yi1c16ad82018-09-12 10:01:49 -0700453 }
454 catch (const std::exception& e)
455 {
456 fprintf(stderr, "%s\n", e.what());
457 }
Kun Yieb312312018-06-13 09:20:50 -0700458
Patrick Venture33569752018-03-12 18:56:14 -0700459 if (postFd > -1)
460 {
461 close(postFd);
462 }
463
Sunita Kumari56127ef2022-10-12 05:01:20 +0000464 return 0;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530465#endif
Patrick Venture33569752018-03-12 18:56:14 -0700466}