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 | |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 17 | #include "lpcsnoop/snoop.hpp" |
| 18 | |
William A. Kennington III | 6dac4c5 | 2019-12-13 15:05:00 -0800 | [diff] [blame] | 19 | #include <endian.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 21 | #include <getopt.h> |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 22 | #include <sys/epoll.h> |
| 23 | #include <systemd/sd-event.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
| 25 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 26 | #include <cstdint> |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 27 | #include <exception> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 28 | #include <iostream> |
William A. Kennington III | b3baa68 | 2021-12-19 20:47:05 -0800 | [diff] [blame] | 29 | #include <optional> |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 30 | #include <sdeventplus/event.hpp> |
| 31 | #include <sdeventplus/source/event.hpp> |
| 32 | #include <sdeventplus/source/io.hpp> |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 33 | #include <sdeventplus/source/signal.hpp> |
| 34 | #include <stdplus/signal.hpp> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 35 | #include <thread> |
| 36 | |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 37 | static size_t codeSize = 1; /* Size of each POST code in bytes */ |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 38 | |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 39 | static void usage(const char* name) |
| 40 | { |
| 41 | fprintf(stderr, |
| 42 | "Usage: %s [-d <DEVICE>]\n" |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 43 | " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. " |
| 44 | "Default is %zu\n" |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 45 | " -d, --device <DEVICE> use <DEVICE> file.\n" |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 46 | " -v, --verbose Prints verbose information while running\n\n", |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 47 | name, codeSize); |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 50 | /* |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 51 | * Callback handling IO event from the POST code fd. i.e. there is new |
| 52 | * POST code available to read. |
| 53 | */ |
Kun Yi | cce0962 | 2020-04-06 14:01:55 -0700 | [diff] [blame] | 54 | void PostCodeEventHandler(sdeventplus::source::IO& s, int postFd, uint32_t, |
| 55 | PostReporter* reporter, bool verbose) |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 56 | { |
William A. Kennington III | 6dac4c5 | 2019-12-13 15:05:00 -0800 | [diff] [blame] | 57 | uint64_t code = 0; |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 58 | ssize_t readb; |
| 59 | while ((readb = read(postFd, &code, codeSize)) > 0) |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 60 | { |
William A. Kennington III | f21475a | 2019-12-13 17:21:24 -0800 | [diff] [blame] | 61 | code = le64toh(code); |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 62 | if (verbose) |
| 63 | { |
| 64 | fprintf(stderr, "Code: 0x%" PRIx64 "\n", code); |
| 65 | } |
William A. Kennington III | f21475a | 2019-12-13 17:21:24 -0800 | [diff] [blame] | 66 | // HACK: Always send property changed signal even for the same code |
| 67 | // since we are single threaded, external users will never see the |
| 68 | // first value. |
Manojkiran Eda | ba5258f | 2021-02-25 13:23:33 +0530 | [diff] [blame] | 69 | reporter->value(std::make_tuple(~code, secondary_post_code_t{}), true); |
| 70 | reporter->value(std::make_tuple(code, secondary_post_code_t{})); |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 71 | |
| 72 | // read depends on old data being cleared since it doens't always read |
| 73 | // the full code size |
| 74 | code = 0; |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 75 | } |
William A. Kennington III | 0f964b4 | 2020-04-16 18:46:03 -0700 | [diff] [blame] | 76 | |
| 77 | if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) |
| 78 | { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | /* Read failure. */ |
| 83 | if (readb == 0) |
| 84 | { |
| 85 | fprintf(stderr, "Unexpected EOF reading postcode\n"); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno)); |
| 90 | } |
| 91 | s.get_event().exit(1); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | /* |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 95 | * TODO(venture): this only listens one of the possible snoop ports, but |
| 96 | * doesn't share the namespace. |
| 97 | * |
| 98 | * This polls() the lpc snoop character device and it owns the dbus object |
| 99 | * whose value is the latest port 80h value. |
| 100 | */ |
| 101 | int main(int argc, char* argv[]) |
| 102 | { |
| 103 | int rc = 0; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 104 | int opt; |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 105 | int postFd = -1; |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * These string constants are only used in this method within this object |
| 109 | * and this object is the only object feeding into the final binary. |
| 110 | * |
| 111 | * If however, another object is added to this binary it would be proper |
| 112 | * to move these declarations to be global and extern to the other object. |
| 113 | */ |
| 114 | const char* snoopObject = SNOOP_OBJECTPATH; |
| 115 | const char* snoopDbus = SNOOP_BUSNAME; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 116 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 117 | bool deferSignals = true; |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 118 | bool verbose = false; |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 119 | |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 120 | // clang-format off |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 121 | static const struct option long_options[] = { |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 122 | {"bytes", required_argument, NULL, 'b'}, |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 123 | {"device", optional_argument, NULL, 'd'}, |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 124 | {"verbose", no_argument, NULL, 'v'}, |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 125 | {0, 0, 0, 0} |
| 126 | }; |
| 127 | // clang-format on |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 128 | |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 129 | while ((opt = getopt_long(argc, argv, "b:d:v", long_options, NULL)) != -1) |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 130 | { |
| 131 | switch (opt) |
| 132 | { |
| 133 | case 0: |
| 134 | break; |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 135 | case 'b': |
| 136 | codeSize = atoi(optarg); |
| 137 | |
| 138 | if (codeSize < 1 || codeSize > 8) |
| 139 | { |
| 140 | fprintf(stderr, |
| 141 | "Invalid POST code size '%s'. Must be " |
| 142 | "an integer from 1 to 8.\n", |
| 143 | optarg); |
| 144 | exit(EXIT_FAILURE); |
| 145 | } |
| 146 | break; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 147 | case 'd': |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 148 | postFd = open(optarg, O_NONBLOCK); |
| 149 | if (postFd < 0) |
| 150 | { |
| 151 | fprintf(stderr, "Unable to open: %s\n", optarg); |
| 152 | return -1; |
| 153 | } |
| 154 | |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 155 | break; |
William A. Kennington III | 66efa63 | 2020-04-16 18:46:20 -0700 | [diff] [blame] | 156 | case 'v': |
| 157 | verbose = true; |
| 158 | break; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 159 | default: |
| 160 | usage(argv[0]); |
| 161 | exit(EXIT_FAILURE); |
| 162 | } |
| 163 | } |
| 164 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 165 | auto bus = sdbusplus::bus::new_default(); |
| 166 | |
| 167 | // Add systemd object manager. |
Willy Tu | a396c85 | 2021-12-09 22:23:14 -0800 | [diff] [blame] | 168 | sdbusplus::server::manager::manager snoopdManager(bus, snoopObject); |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 169 | |
| 170 | PostReporter reporter(bus, snoopObject, deferSignals); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 171 | reporter.emit_object_added(); |
| 172 | bus.request_name(snoopDbus); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 173 | |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 174 | // Create sdevent and add IO source |
| 175 | try |
| 176 | { |
William A. Kennington III | b3baa68 | 2021-12-19 20:47:05 -0800 | [diff] [blame] | 177 | auto event = sdeventplus::Event::get_default(); |
| 178 | std::optional<sdeventplus::source::IO> reporterSource; |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 179 | if (postFd > 0) |
| 180 | { |
William A. Kennington III | b3baa68 | 2021-12-19 20:47:05 -0800 | [diff] [blame] | 181 | reporterSource.emplace( |
Manojkiran Eda | aade4ad | 2021-02-19 11:20:33 +0530 | [diff] [blame] | 182 | event, postFd, EPOLLIN | EPOLLET, |
| 183 | std::bind(PostCodeEventHandler, std::placeholders::_1, |
| 184 | std::placeholders::_2, std::placeholders::_3, |
| 185 | &reporter, verbose)); |
| 186 | } |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 187 | // Enable bus to handle incoming IO and bus events |
| 188 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
William A. Kennington III | 6a5e0a1 | 2021-12-19 20:47:34 -0800 | [diff] [blame] | 189 | auto intCb = [](sdeventplus::source::Signal& source, |
| 190 | const struct signalfd_siginfo*) { |
| 191 | source.get_event().exit(0); |
| 192 | }; |
| 193 | stdplus::signal::block(SIGINT); |
| 194 | sdeventplus::source::Signal(event, SIGINT, intCb).set_floating(true); |
| 195 | stdplus::signal::block(SIGTERM); |
| 196 | sdeventplus::source::Signal(event, SIGTERM, std::move(intCb)) |
| 197 | .set_floating(true); |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame] | 198 | rc = event.loop(); |
| 199 | } |
| 200 | catch (const std::exception& e) |
| 201 | { |
| 202 | fprintf(stderr, "%s\n", e.what()); |
| 203 | } |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 204 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 205 | if (postFd > -1) |
| 206 | { |
| 207 | close(postFd); |
| 208 | } |
| 209 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 210 | return rc; |
| 211 | } |