blob: fd8f4b60858f0d646a13e642926cce4ededc19e6 [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";
Brad Bishop8f6c0ce2022-05-03 17:29:05 -040049#ifdef ENABLE_IPMI_SNOOP
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053050const uint8_t minPositionVal = 0;
51const uint8_t maxPositionVal = 5;
Brad Bishop8f6c0ce2022-05-03 17:29:05 -040052#endif
Kumar Thangavel0269eaf2021-08-11 15:45:18 +053053
54#ifdef ENABLE_IPMI_SNOOP
55std::vector<std::unique_ptr<IpmiPostReporter>> reporters;
56#endif
57
58#ifdef ENABLE_IPMI_SNOOP
59void IpmiPostReporter::getSelectorPositionSignal(sdbusplus::bus::bus& bus)
60{
61 size_t posVal = 0;
62
63 matchSignal = std::make_unique<sdbusplus::bus::match_t>(
64 bus,
65 sdbusplus::bus::match::rules::propertiesChanged(selectorObject,
66 selectorIface),
67 [&](sdbusplus::message::message& msg) {
68 std::string objectName;
69 std::map<std::string, Selector::PropertiesVariant> msgData;
70 msg.read(objectName, msgData);
71
72 auto valPropMap = msgData.find("Position");
73 {
74 if (valPropMap == msgData.end())
75 {
76 std::cerr << "Position property not found " << std::endl;
77 return;
78 }
79
80 posVal = std::get<size_t>(valPropMap->second);
81
82 if (posVal > minPositionVal && posVal < maxPositionVal)
83 {
84 std::tuple<uint64_t, secondary_post_code_t> postcodes =
85 reporters[posVal - 1]->value();
86 uint64_t postcode = std::get<uint64_t>(postcodes);
87
88 // write postcode into seven segment display
89 if (postCodeDisplay(postcode) < 0)
90 {
91 fprintf(stderr, "Error in display the postcode\n");
92 }
93 }
94 }
95 });
96}
97#endif
Benjamin Faire7b07f02018-07-02 09:51:37 -070098
Benjamin Faire7b07f02018-07-02 09:51:37 -070099static void usage(const char* name)
100{
101 fprintf(stderr,
102 "Usage: %s [-d <DEVICE>]\n"
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700103 " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. "
104 "Default is %zu\n"
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530105 " -d, --device <DEVICE> use <DEVICE> file.\n"
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530106 " -h, --host <host instances> . Default is '%s'\n"
William A. Kennington III66efa632020-04-16 18:46:20 -0700107 " -v, --verbose Prints verbose information while running\n\n",
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530108 name, codeSize, defaultHostInstances);
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700109}
110
Patrick Venture33569752018-03-12 18:56:14 -0700111/*
Kun Yieb312312018-06-13 09:20:50 -0700112 * Callback handling IO event from the POST code fd. i.e. there is new
113 * POST code available to read.
114 */
Willy Tud1ac1972022-03-21 16:20:39 -0700115void PostCodeEventHandler(PostReporter* reporter, bool verbose,
116 sdeventplus::source::IO& s, int postFd, uint32_t)
Kun Yieb312312018-06-13 09:20:50 -0700117{
William A. Kennington III6dac4c52019-12-13 15:05:00 -0800118 uint64_t code = 0;
William A. Kennington III0f964b42020-04-16 18:46:03 -0700119 ssize_t readb;
120 while ((readb = read(postFd, &code, codeSize)) > 0)
Kun Yi1c16ad82018-09-12 10:01:49 -0700121 {
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800122 code = le64toh(code);
William A. Kennington III66efa632020-04-16 18:46:20 -0700123 if (verbose)
124 {
125 fprintf(stderr, "Code: 0x%" PRIx64 "\n", code);
126 }
William A. Kennington IIIf21475a2019-12-13 17:21:24 -0800127 // HACK: Always send property changed signal even for the same code
128 // since we are single threaded, external users will never see the
129 // first value.
Manojkiran Edaba5258f2021-02-25 13:23:33 +0530130 reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true);
131 reporter->value(std::make_tuple(code, secondary_post_code_t{}));
William A. Kennington III0f964b42020-04-16 18:46:03 -0700132
133 // read depends on old data being cleared since it doens't always read
134 // the full code size
135 code = 0;
Kun Yi1c16ad82018-09-12 10:01:49 -0700136 }
William A. Kennington III0f964b42020-04-16 18:46:03 -0700137
138 if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
139 {
140 return;
141 }
142
143 /* Read failure. */
144 if (readb == 0)
145 {
146 fprintf(stderr, "Unexpected EOF reading postcode\n");
147 }
148 else
149 {
150 fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno));
151 }
152 s.get_event().exit(1);
Kun Yieb312312018-06-13 09:20:50 -0700153}
154
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530155#ifdef ENABLE_IPMI_SNOOP
156// handle muti-host D-bus
157int postCodeIpmiHandler(const std::string& snoopObject,
158 const std::string& snoopDbus, sdbusplus::bus::bus& bus,
159 std::span<std::string> host)
160{
161 int ret = 0;
162
163 try
164 {
165 for (size_t iteration = 0; iteration < host.size(); iteration++)
166 {
167 std::string objPathInst = snoopObject + host[iteration];
168
169 sdbusplus::server::manager_t m{bus, objPathInst.c_str()};
170
171 /* Create a monitor object and let it do all the rest */
172 reporters.emplace_back(
173 std::make_unique<IpmiPostReporter>(bus, objPathInst.c_str()));
174
175 reporters[iteration]->emit_object_added();
176 }
177
178 bus.request_name(snoopDbus.c_str());
179 reporters[0]->getSelectorPositionSignal(bus);
180 }
181 catch (const std::exception& e)
182 {
183 fprintf(stderr, "%s\n", e.what());
184 }
185
186 // Configure seven segment dsiplay connected to GPIOs as output
187 ret = configGPIODirOutput();
188 if (ret < 0)
189 {
190 fprintf(stderr, "Failed find the gpio line\n");
191 }
192
193 while (true)
194 {
195 bus.process_discard();
196 bus.wait();
197 }
198 exit(EXIT_SUCCESS);
199}
200#endif
201
Kun Yieb312312018-06-13 09:20:50 -0700202/*
Patrick Venture33569752018-03-12 18:56:14 -0700203 * TODO(venture): this only listens one of the possible snoop ports, but
204 * doesn't share the namespace.
205 *
206 * This polls() the lpc snoop character device and it owns the dbus object
207 * whose value is the latest port 80h value.
208 */
209int main(int argc, char* argv[])
210{
Patrick Venture33569752018-03-12 18:56:14 -0700211
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530212#ifndef ENABLE_IPMI_SNOOP
213 int postFd = -1;
214#endif
215
216 int opt;
217 bool verbose = false;
218
219#ifdef ENABLE_IPMI_SNOOP
220 std::vector<std::string> host;
221#endif
Patrick Venture33569752018-03-12 18:56:14 -0700222 /*
223 * These string constants are only used in this method within this object
224 * and this object is the only object feeding into the final binary.
225 *
226 * If however, another object is added to this binary it would be proper
227 * to move these declarations to be global and extern to the other object.
228 */
Patrick Venture33569752018-03-12 18:56:14 -0700229
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700230 // clang-format off
Benjamin Faire7b07f02018-07-02 09:51:37 -0700231 static const struct option long_options[] = {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530232 #ifdef ENABLE_IPMI_SNOOP
233 {"host", optional_argument, NULL, 'h'},
234 #endif
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700235 {"bytes", required_argument, NULL, 'b'},
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530236 #ifndef ENABLE_IPMI_SNOOP
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530237 {"device", optional_argument, NULL, 'd'},
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530238 #endif
William A. Kennington III66efa632020-04-16 18:46:20 -0700239 {"verbose", no_argument, NULL, 'v'},
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700240 {0, 0, 0, 0}
241 };
242 // clang-format on
Benjamin Faire7b07f02018-07-02 09:51:37 -0700243
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530244 while ((opt = getopt_long(argc, argv, "h:b:d:v", long_options, NULL)) != -1)
Benjamin Faire7b07f02018-07-02 09:51:37 -0700245 {
246 switch (opt)
247 {
248 case 0:
249 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530250#ifdef ENABLE_IPMI_SNOOP
251 case 'h': {
252 std::string_view instances = optarg;
253 size_t pos = 0;
254
255 while ((pos = instances.find(" ")) != std::string::npos)
256 {
257 host.emplace_back(instances.substr(0, pos));
258 instances.remove_prefix(pos + 1);
259 }
260 host.emplace_back(instances);
261 break;
262 }
263#endif
264 case 'b': {
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700265 codeSize = atoi(optarg);
266
267 if (codeSize < 1 || codeSize > 8)
268 {
269 fprintf(stderr,
270 "Invalid POST code size '%s'. Must be "
271 "an integer from 1 to 8.\n",
272 optarg);
273 exit(EXIT_FAILURE);
274 }
275 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530276 }
277#ifndef ENABLE_IPMI_SNOOP
Benjamin Faire7b07f02018-07-02 09:51:37 -0700278 case 'd':
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530279
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530280 postFd = open(optarg, O_NONBLOCK);
281 if (postFd < 0)
282 {
283 fprintf(stderr, "Unable to open: %s\n", optarg);
284 return -1;
285 }
Benjamin Faire7b07f02018-07-02 09:51:37 -0700286 break;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530287#endif
William A. Kennington III66efa632020-04-16 18:46:20 -0700288 case 'v':
289 verbose = true;
290 break;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700291 default:
292 usage(argv[0]);
Benjamin Faire7b07f02018-07-02 09:51:37 -0700293 }
294 }
295
Patrick Venture33569752018-03-12 18:56:14 -0700296 auto bus = sdbusplus::bus::new_default();
297
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530298#ifdef ENABLE_IPMI_SNOOP
299 std::cout << "Verbose = " << verbose << std::endl;
300 int ret = postCodeIpmiHandler(ipmiSnoopObject, snoopDbus, bus, host);
301 if (ret < 0)
302 {
303 fprintf(stderr, "Error in postCodeIpmiHandler\n");
304 return ret;
305 }
306 return 0;
307#endif
308
309#ifndef ENABLE_IPMI_SNOOP
310 int rc = 0;
311
312 bool deferSignals = true;
313
Patrick Venture33569752018-03-12 18:56:14 -0700314 // Add systemd object manager.
Willy Tua396c852021-12-09 22:23:14 -0800315 sdbusplus::server::manager::manager snoopdManager(bus, snoopObject);
Patrick Venture33569752018-03-12 18:56:14 -0700316
317 PostReporter reporter(bus, snoopObject, deferSignals);
Kun Yieb312312018-06-13 09:20:50 -0700318 reporter.emit_object_added();
319 bus.request_name(snoopDbus);
Kun Yieb312312018-06-13 09:20:50 -0700320
Kun Yi1c16ad82018-09-12 10:01:49 -0700321 // Create sdevent and add IO source
322 try
323 {
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530324 sdeventplus::Event event = sdeventplus::Event::get_default();
Harvey.Wu4f26b3e2022-05-12 08:54:30 +0800325 std::optional<sdeventplus::source::IO> reporterSource;
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530326 if (postFd > 0)
327 {
Harvey.Wu4f26b3e2022-05-12 08:54:30 +0800328 reporterSource.emplace(
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530329 event, postFd, EPOLLIN | EPOLLET,
Willy Tud1ac1972022-03-21 16:20:39 -0700330 std::bind_front(PostCodeEventHandler, &reporter, verbose));
Manojkiran Edaaade4ad2021-02-19 11:20:33 +0530331 }
Kun Yi1c16ad82018-09-12 10:01:49 -0700332 // Enable bus to handle incoming IO and bus events
Willy Tuc869aef2022-06-07 14:47:56 -0700333 bool done = false;
Kun Yi1c16ad82018-09-12 10:01:49 -0700334 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
Willy Tuc869aef2022-06-07 14:47:56 -0700335 auto intCb = [&done](sdeventplus::source::Signal& source,
336 const struct signalfd_siginfo*) {
William A. Kennington III6a5e0a12021-12-19 20:47:34 -0800337 source.get_event().exit(0);
Willy Tuc869aef2022-06-07 14:47:56 -0700338 done = true;
William A. Kennington III6a5e0a12021-12-19 20:47:34 -0800339 };
340 stdplus::signal::block(SIGINT);
341 sdeventplus::source::Signal(event, SIGINT, intCb).set_floating(true);
342 stdplus::signal::block(SIGTERM);
343 sdeventplus::source::Signal(event, SIGTERM, std::move(intCb))
344 .set_floating(true);
Willy Tuc869aef2022-06-07 14:47:56 -0700345
346 while (!done)
347 {
348 // Process all outstanding bus events before running the loop.
349 // This prevents the sd-bus handling logic from leaking memory.
350 // TODO: Remove when upstream fixes this bug
351 while (bus.process_discard() > 0)
352 ;
353
354 // Run and never timeout
355 rc = event.run(std::nullopt);
356 }
Kun Yi1c16ad82018-09-12 10:01:49 -0700357 }
358 catch (const std::exception& e)
359 {
360 fprintf(stderr, "%s\n", e.what());
361 }
Kun Yieb312312018-06-13 09:20:50 -0700362
Patrick Venture33569752018-03-12 18:56:14 -0700363 if (postFd > -1)
364 {
365 close(postFd);
366 }
367
Patrick Venture33569752018-03-12 18:56:14 -0700368 return rc;
Kumar Thangavel0269eaf2021-08-11 15:45:18 +0530369#endif
Patrick Venture33569752018-03-12 18:56:14 -0700370}