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