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 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 20 | #include <getopt.h> |
Patrick Venture | b5754fd | 2018-09-10 13:13:58 -0700 | [diff] [blame] | 21 | #include <sys/epoll.h> |
| 22 | #include <systemd/sd-event.h> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <array> |
| 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 | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 35 | static const char* snoopFilename = "/dev/aspeed-lpc-snoop0"; |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 36 | static size_t codeSize = 1; /* Size of each POST code in bytes */ |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 37 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 38 | /* |
| 39 | * 256 bytes is a nice amount. It's improbable we'd need this many, but its |
| 40 | * gives us leg room in the event the driver poll doesn't return in a timely |
| 41 | * fashion. So, mostly arbitrarily chosen. |
| 42 | */ |
| 43 | static constexpr size_t BUFFER_SIZE = 256; |
| 44 | |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 45 | static void usage(const char* name) |
| 46 | { |
| 47 | fprintf(stderr, |
| 48 | "Usage: %s [-d <DEVICE>]\n" |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 49 | " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. " |
| 50 | "Default is %zu\n" |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 51 | " -d, --device <DEVICE> use <DEVICE> file. Default is '%s'\n\n", |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 52 | name, codeSize, snoopFilename); |
| 53 | } |
| 54 | |
| 55 | static uint64_t assembleBytes(std::array<uint8_t, BUFFER_SIZE> buf, int start, |
| 56 | int size) |
| 57 | { |
| 58 | uint64_t result = 0; |
| 59 | |
| 60 | for (int i = start + size - 1; i >= start; i--) |
| 61 | { |
| 62 | result <<= 8; |
| 63 | result |= buf[i]; |
| 64 | } |
| 65 | |
| 66 | return result; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 69 | /* |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 70 | * Callback handling IO event from the POST code fd. i.e. there is new |
| 71 | * POST code available to read. |
| 72 | */ |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame^] | 73 | void PostCodeEventHandler(sdeventplus::source::IO& s, int postFd, |
| 74 | uint32_t revents, PostReporter* reporter) |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 75 | { |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 76 | std::array<uint8_t, BUFFER_SIZE> buffer; |
| 77 | int readb; |
| 78 | |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame^] | 79 | readb = read(postFd, buffer.data(), buffer.size()); |
| 80 | if (readb < 0) |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 81 | { |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame^] | 82 | /* Read failure. */ |
| 83 | s.get_event().exit(1); |
| 84 | return; |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame^] | 87 | if (readb % codeSize != 0) |
| 88 | { |
| 89 | fprintf(stderr, |
| 90 | "Warning: read size %d not a multiple of " |
| 91 | "POST code length %zu. Some codes may be " |
| 92 | "corrupt or missing\n", |
| 93 | readb, codeSize); |
| 94 | readb -= (readb % codeSize); |
| 95 | } |
| 96 | |
| 97 | /* Broadcast the values read. */ |
| 98 | for (int i = 0; i < readb; i += codeSize) |
| 99 | { |
| 100 | reporter->value(assembleBytes(buffer, i, codeSize)); |
| 101 | } |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 105 | * TODO(venture): this only listens one of the possible snoop ports, but |
| 106 | * doesn't share the namespace. |
| 107 | * |
| 108 | * This polls() the lpc snoop character device and it owns the dbus object |
| 109 | * whose value is the latest port 80h value. |
| 110 | */ |
| 111 | int main(int argc, char* argv[]) |
| 112 | { |
| 113 | int rc = 0; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 114 | int opt; |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 115 | int postFd = -1; |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * These string constants are only used in this method within this object |
| 119 | * and this object is the only object feeding into the final binary. |
| 120 | * |
| 121 | * If however, another object is added to this binary it would be proper |
| 122 | * to move these declarations to be global and extern to the other object. |
| 123 | */ |
| 124 | const char* snoopObject = SNOOP_OBJECTPATH; |
| 125 | const char* snoopDbus = SNOOP_BUSNAME; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 126 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 127 | bool deferSignals = true; |
| 128 | |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 129 | // clang-format off |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 130 | static const struct option long_options[] = { |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 131 | {"bytes", required_argument, NULL, 'b'}, |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 132 | {"device", required_argument, NULL, 'd'}, |
Patrick Venture | 1ceb21b | 2018-08-08 11:29:36 -0700 | [diff] [blame] | 133 | {0, 0, 0, 0} |
| 134 | }; |
| 135 | // clang-format on |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 136 | |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 137 | while ((opt = getopt_long(argc, argv, "b:d:", long_options, NULL)) != -1) |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 138 | { |
| 139 | switch (opt) |
| 140 | { |
| 141 | case 0: |
| 142 | break; |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame] | 143 | case 'b': |
| 144 | codeSize = atoi(optarg); |
| 145 | |
| 146 | if (codeSize < 1 || codeSize > 8) |
| 147 | { |
| 148 | fprintf(stderr, |
| 149 | "Invalid POST code size '%s'. Must be " |
| 150 | "an integer from 1 to 8.\n", |
| 151 | optarg); |
| 152 | exit(EXIT_FAILURE); |
| 153 | } |
| 154 | break; |
Benjamin Fair | e7b07f0 | 2018-07-02 09:51:37 -0700 | [diff] [blame] | 155 | case 'd': |
| 156 | snoopFilename = optarg; |
| 157 | break; |
| 158 | default: |
| 159 | usage(argv[0]); |
| 160 | exit(EXIT_FAILURE); |
| 161 | } |
| 162 | } |
| 163 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 164 | postFd = open(snoopFilename, 0); |
| 165 | if (postFd < 0) |
| 166 | { |
| 167 | fprintf(stderr, "Unable to open: %s\n", snoopFilename); |
| 168 | return -1; |
| 169 | } |
| 170 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 171 | auto bus = sdbusplus::bus::new_default(); |
| 172 | |
| 173 | // Add systemd object manager. |
| 174 | sdbusplus::server::manager::manager(bus, snoopObject); |
| 175 | |
| 176 | PostReporter reporter(bus, snoopObject, deferSignals); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 177 | reporter.emit_object_added(); |
| 178 | bus.request_name(snoopDbus); |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 179 | |
Kun Yi | 1c16ad8 | 2018-09-12 10:01:49 -0700 | [diff] [blame^] | 180 | // Create sdevent and add IO source |
| 181 | try |
| 182 | { |
| 183 | sdeventplus::Event event = sdeventplus::Event::get_default(); |
| 184 | sdeventplus::source::IO reporterSource( |
| 185 | event, postFd, EPOLLIN, |
| 186 | std::bind(PostCodeEventHandler, std::placeholders::_1, |
| 187 | std::placeholders::_2, std::placeholders::_3, &reporter)); |
| 188 | // Enable bus to handle incoming IO and bus events |
| 189 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 190 | rc = event.loop(); |
| 191 | } |
| 192 | catch (const std::exception& e) |
| 193 | { |
| 194 | fprintf(stderr, "%s\n", e.what()); |
| 195 | } |
Kun Yi | eb31231 | 2018-06-13 09:20:50 -0700 | [diff] [blame] | 196 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 197 | if (postFd > -1) |
| 198 | { |
| 199 | close(postFd); |
| 200 | } |
| 201 | |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 202 | return rc; |
| 203 | } |