Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 1 | /** |
| 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 Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 17 | #ifdef ENABLE_IPMI_SNOOP |
| 18 | #include "ipmisnoop/ipmisnoop.hpp" |
| 19 | #endif |
| 20 | |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 21 | #include "lpcsnoop/snoop.hpp" |
| 22 | |
William A. Kennington III | 6dac4c5 | 2019-12-13 15:05:00 -0800 | [diff] [blame] | 23 | #include <endian.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 25 | #include <getopt.h> |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 26 | #include <sys/epoll.h> |
| 27 | #include <systemd/sd-event.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 30 | #include <cstdint> |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 31 | #include <exception> |
Willy Tu | d1ac197 | 2022-03-21 16:20:39 -0700 | [diff] [blame] | 32 | #include <functional> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 33 | #include <iostream> |
William A. Kennington III | b3baa68 | 2021-12-19 20:47:05 -0800 | [diff] [blame] | 34 | #include <optional> |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 35 | #include <sdeventplus/event.hpp> |
| 36 | #include <sdeventplus/source/event.hpp> |
| 37 | #include <sdeventplus/source/io.hpp> |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 38 | #include <sdeventplus/source/signal.hpp> |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 39 | #include <span> |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 40 | #include <stdplus/signal.hpp> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 41 | #include <thread> |
| 42 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 43 | #ifdef ENABLE_IPMI_SNOOP |
| 44 | #include <xyz/openbmc_project/State/Boot/Raw/server.hpp> |
| 45 | #endif |
| 46 | |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 47 | static size_t codeSize = 1; /* Size of each POST code in bytes */ |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 48 | const char* defaultHostInstances = "0"; |
| 49 | const uint8_t minPositionVal = 0; |
| 50 | const uint8_t maxPositionVal = 5; |
| 51 | |
| 52 | #ifdef ENABLE_IPMI_SNOOP |
| 53 | std::vector<std::unique_ptr<IpmiPostReporter>> reporters; |
| 54 | #endif |
| 55 | |
| 56 | #ifdef ENABLE_IPMI_SNOOP |
| 57 | void 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 Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 96 | |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 97 | static void usage(const char* name) |
| 98 | { |
| 99 | fprintf(stderr, |
| 100 | "Usage: %s [-d <DEVICE>]\n" |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 101 | " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. " |
| 102 | "Default is %zu\n" |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 103 | " -d, --device <DEVICE> use <DEVICE> file.\n" |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 104 | " -h, --host <host instances> . Default is '%s'\n" |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 105 | " -v, --verbose Prints verbose information while running\n\n", |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 106 | name, codeSize, defaultHostInstances); |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 109 | /* |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 110 | * Callback handling IO event from the POST code fd. i.e. there is new |
| 111 | * POST code available to read. |
| 112 | */ |
Willy Tu | d1ac197 | 2022-03-21 16:20:39 -0700 | [diff] [blame] | 113 | void PostCodeEventHandler(PostReporter* reporter, bool verbose, |
| 114 | sdeventplus::source::IO& s, int postFd, uint32_t) |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 115 | { |
William A. Kennington III | 6dac4c5 | 2019-12-13 15:05:00 -0800 | [diff] [blame] | 116 | uint64_t code = 0; |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 117 | ssize_t readb; |
| 118 | while ((readb = read(postFd, &code, codeSize)) > 0) |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 119 | { |
William A. Kennington III | f21475a | 2019-12-13 17:21:24 -0800 | [diff] [blame] | 120 | code = le64toh(code); |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 121 | if (verbose) |
| 122 | { |
| 123 | fprintf(stderr, "Code: 0x%" PRIx64 "\n", code); |
| 124 | } |
William A. Kennington III | f21475a | 2019-12-13 17:21:24 -0800 | [diff] [blame] | 125 | // 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 Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 128 | 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 III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 130 | |
| 131 | // read depends on old data being cleared since it doens't always read |
| 132 | // the full code size |
| 133 | code = 0; |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 134 | } |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 135 | |
| 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 Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 153 | #ifdef ENABLE_IPMI_SNOOP |
| 154 | // handle muti-host D-bus |
| 155 | int 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 Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 200 | /* |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 201 | * 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 | */ |
| 207 | int main(int argc, char* argv[]) |
| 208 | { |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 209 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 210 | #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 Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 220 | /* |
| 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 Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 227 | |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 228 | // clang-format off |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 229 | static const struct option long_options[] = { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 230 | #ifdef ENABLE_IPMI_SNOOP |
| 231 | {"host", optional_argument, NULL, 'h'}, |
| 232 | #endif |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 233 | {"bytes", required_argument, NULL, 'b'}, |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 234 | #ifndef ENABLE_IPMI_SNOOP |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 235 | {"device", optional_argument, NULL, 'd'}, |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 236 | #endif |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 237 | {"verbose", no_argument, NULL, 'v'}, |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 238 | {0, 0, 0, 0} |
| 239 | }; |
| 240 | // clang-format on |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 241 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 242 | while ((opt = getopt_long(argc, argv, "h:b:d:v", long_options, NULL)) != -1) |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 243 | { |
| 244 | switch (opt) |
| 245 | { |
| 246 | case 0: |
| 247 | break; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 248 | #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 Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 263 | 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 Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 274 | } |
| 275 | #ifndef ENABLE_IPMI_SNOOP |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 276 | case 'd': |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 277 | |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 278 | postFd = open(optarg, O_NONBLOCK); |
| 279 | if (postFd < 0) |
| 280 | { |
| 281 | fprintf(stderr, "Unable to open: %s\n", optarg); |
| 282 | return -1; |
| 283 | } |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 284 | break; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 285 | #endif |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 286 | case 'v': |
| 287 | verbose = true; |
| 288 | break; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 289 | default: |
| 290 | usage(argv[0]); |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 294 | auto bus = sdbusplus::bus::new_default(); |
| 295 | |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 296 | #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 Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 312 | // Add systemd object manager. |
Willy Tu | a396c85 | 2021-12-09 22:23:14 -0800 | [diff] [blame] | 313 | sdbusplus::server::manager::manager snoopdManager(bus, snoopObject); |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 314 | |
| 315 | PostReporter reporter(bus, snoopObject, deferSignals); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 316 | reporter.emit_object_added(); |
| 317 | bus.request_name(snoopDbus); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 318 | |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 319 | // Create sdevent and add IO source |
| 320 | try |
| 321 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 322 | sdeventplus::Event event = sdeventplus::Event::get_default(); |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 323 | if (postFd > 0) |
| 324 | { |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 325 | |
| 326 | sdeventplus::source::IO reporterSource( |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 327 | event, postFd, EPOLLIN | EPOLLET, |
Willy Tu | d1ac197 | 2022-03-21 16:20:39 -0700 | [diff] [blame] | 328 | std::bind_front(PostCodeEventHandler, &reporter, verbose)); |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 329 | } |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 330 | // Enable bus to handle incoming IO and bus events |
| 331 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 332 | 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 Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 341 | rc = event.loop(); |
| 342 | } |
| 343 | catch (const std::exception& e) |
| 344 | { |
| 345 | fprintf(stderr, "%s\n", e.what()); |
| 346 | } |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 347 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 348 | if (postFd > -1) |
| 349 | { |
| 350 | close(postFd); |
| 351 | } |
| 352 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 353 | return rc; |
Kumar Thangavel | 0269eaf | 2021-08-11 15:45:18 +0530 | [diff] [blame^] | 354 | #endif |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 355 | } |