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 | |
| 17 | #include <fcntl.h> |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 18 | #include <getopt.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 19 | #include <poll.h> |
| 20 | #include <unistd.h> |
| 21 | |
| 22 | #include <array> |
| 23 | #include <cstdint> |
| 24 | #include <iostream> |
| 25 | #include <memory> |
| 26 | #include <thread> |
| 27 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 28 | #include "lpcsnoop/snoop.hpp" |
| 29 | |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 30 | static const char* snoopFilename = "/dev/aspeed-lpc-snoop0"; |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 31 | static size_t codeSize = 1; /* Size of each POST code in bytes */ |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 32 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 33 | /* |
| 34 | * 256 bytes is a nice amount. It's improbable we'd need this many, but its |
| 35 | * gives us leg room in the event the driver poll doesn't return in a timely |
| 36 | * fashion. So, mostly arbitrarily chosen. |
| 37 | */ |
| 38 | static constexpr size_t BUFFER_SIZE = 256; |
| 39 | |
| 40 | /* |
| 41 | * Process any incoming dbus inquiries, which include introspection etc. |
| 42 | */ |
| 43 | void ProcessDbus(sdbusplus::bus::bus& bus) |
| 44 | { |
| 45 | while (true) |
| 46 | { |
| 47 | bus.process_discard(); |
| 48 | bus.wait(); // wait indefinitely |
| 49 | } |
| 50 | |
| 51 | return; |
| 52 | } |
| 53 | |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 54 | static void usage(const char* name) |
| 55 | { |
| 56 | fprintf(stderr, |
| 57 | "Usage: %s [-d <DEVICE>]\n" |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 58 | " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. " |
| 59 | "Default is %zu\n" |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 60 | " -d, --device <DEVICE> use <DEVICE> file. Default is '%s'\n\n", |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 61 | name, codeSize, snoopFilename); |
| 62 | } |
| 63 | |
| 64 | static uint64_t assembleBytes(std::array<uint8_t, BUFFER_SIZE> buf, int start, |
| 65 | int size) |
| 66 | { |
| 67 | uint64_t result = 0; |
| 68 | |
| 69 | for (int i = start + size - 1; i >= start; i--) |
| 70 | { |
| 71 | result <<= 8; |
| 72 | result |= buf[i]; |
| 73 | } |
| 74 | |
| 75 | return result; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 78 | /* |
| 79 | * TODO(venture): this only listens one of the possible snoop ports, but |
| 80 | * doesn't share the namespace. |
| 81 | * |
| 82 | * This polls() the lpc snoop character device and it owns the dbus object |
| 83 | * whose value is the latest port 80h value. |
| 84 | */ |
| 85 | int main(int argc, char* argv[]) |
| 86 | { |
| 87 | int rc = 0; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 88 | int opt; |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 89 | struct pollfd pollset; |
| 90 | int pollr; |
| 91 | int readb; |
| 92 | int postFd = -1; |
| 93 | std::array<uint8_t, BUFFER_SIZE> buffer; |
| 94 | |
| 95 | /* |
| 96 | * These string constants are only used in this method within this object |
| 97 | * and this object is the only object feeding into the final binary. |
| 98 | * |
| 99 | * If however, another object is added to this binary it would be proper |
| 100 | * to move these declarations to be global and extern to the other object. |
| 101 | */ |
| 102 | const char* snoopObject = SNOOP_OBJECTPATH; |
| 103 | const char* snoopDbus = SNOOP_BUSNAME; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 104 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 105 | bool deferSignals = true; |
| 106 | |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 107 | static const struct option long_options[] = { |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 108 | {"bytes", required_argument, NULL, 'b'}, |
| 109 | {"device", required_argument, NULL, 'd'}, |
| 110 | {0, 0, 0, 0}}; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 111 | |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 112 | while ((opt = getopt_long(argc, argv, "b:d:", long_options, NULL)) != -1) |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 113 | { |
| 114 | switch (opt) |
| 115 | { |
| 116 | case 0: |
| 117 | break; |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 118 | case 'b': |
| 119 | codeSize = atoi(optarg); |
| 120 | |
| 121 | if (codeSize < 1 || codeSize > 8) |
| 122 | { |
| 123 | fprintf(stderr, |
| 124 | "Invalid POST code size '%s'. Must be " |
| 125 | "an integer from 1 to 8.\n", |
| 126 | optarg); |
| 127 | exit(EXIT_FAILURE); |
| 128 | } |
| 129 | break; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 130 | case 'd': |
| 131 | snoopFilename = optarg; |
| 132 | break; |
| 133 | default: |
| 134 | usage(argv[0]); |
| 135 | exit(EXIT_FAILURE); |
| 136 | } |
| 137 | } |
| 138 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 139 | postFd = open(snoopFilename, 0); |
| 140 | if (postFd < 0) |
| 141 | { |
| 142 | fprintf(stderr, "Unable to open: %s\n", snoopFilename); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | pollset.fd = postFd; |
| 147 | pollset.events |= POLLIN; |
| 148 | |
| 149 | auto bus = sdbusplus::bus::new_default(); |
| 150 | |
| 151 | // Add systemd object manager. |
| 152 | sdbusplus::server::manager::manager(bus, snoopObject); |
| 153 | |
| 154 | PostReporter reporter(bus, snoopObject, deferSignals); |
| 155 | reporter.emit_object_added(); |
| 156 | |
| 157 | bus.request_name(snoopDbus); |
| 158 | |
| 159 | /* |
| 160 | * I don't see a public interface for getting the underlying sd_bus* |
| 161 | * so instead of poll(bus, driver), I'll just create a separate thread. |
| 162 | * |
| 163 | * TODO(venture): There may be a way to use sdevent to poll both the file |
| 164 | * and the dbus in the same event loop. If I could get the sdbus pointer |
| 165 | * from bus directly, I'd grab a file handler from it, and then just poll on |
| 166 | * both in one loop. From a cursory look at sdevent, I should be able to do |
| 167 | * something similar with that at some point. |
| 168 | */ |
| 169 | std::thread lt(ProcessDbus, std::ref(bus)); |
| 170 | |
| 171 | /* infinitely listen for POST codes and broadcast. */ |
| 172 | while (true) |
| 173 | { |
| 174 | pollr = poll(&pollset, 1, -1); /* polls indefinitely. */ |
| 175 | if (pollr < 0) |
| 176 | { |
| 177 | /* poll returned error. */ |
| 178 | rc = -errno; |
| 179 | goto exit; |
| 180 | } |
| 181 | |
| 182 | if (pollr > 0) |
| 183 | { |
| 184 | if (pollset.revents & POLLIN) |
| 185 | { |
| 186 | readb = read(postFd, buffer.data(), buffer.size()); |
| 187 | if (readb < 0) |
| 188 | { |
| 189 | /* Read failure. */ |
| 190 | rc = readb; |
| 191 | goto exit; |
| 192 | } |
| 193 | else |
| 194 | { |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 195 | if (readb % codeSize != 0) |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 196 | { |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 197 | fprintf(stderr, |
| 198 | "Warning: read size %d not a multiple of " |
| 199 | "POST code length %zu. Some codes may be " |
| 200 | "corrupt or missing\n", |
| 201 | readb, codeSize); |
| 202 | readb -= (readb % codeSize); |
| 203 | } |
| 204 | |
| 205 | /* Broadcast the values read. */ |
| 206 | for (int i = 0; i < readb; i += codeSize) |
| 207 | { |
| 208 | reporter.value(assembleBytes(buffer, i, codeSize)); |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | exit: |
| 216 | if (postFd > -1) |
| 217 | { |
| 218 | close(postFd); |
| 219 | } |
| 220 | |
| 221 | lt.join(); |
| 222 | |
| 223 | return rc; |
| 224 | } |