blob: 8928a5b2cc2f39bfa5606742f91c98c06c008f1a [file] [log] [blame]
Patrick Venture33569752018-03-12 18:56:14 -07001/**
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 Ventureb5754fd2018-09-10 13:13:58 -070017#include "lpcsnoop/snoop.hpp"
18
Patrick Venture33569752018-03-12 18:56:14 -070019#include <fcntl.h>
Benjamin Faire7b07f02018-07-02 09:51:37 -070020#include <getopt.h>
Patrick Ventureb5754fd2018-09-10 13:13:58 -070021#include <sys/epoll.h>
22#include <systemd/sd-event.h>
Patrick Venture33569752018-03-12 18:56:14 -070023#include <unistd.h>
24
25#include <array>
26#include <cstdint>
Kun Yi1c16ad82018-09-12 10:01:49 -070027#include <exception>
Patrick Venture33569752018-03-12 18:56:14 -070028#include <iostream>
29#include <memory>
Kun Yi1c16ad82018-09-12 10:01:49 -070030#include <sdeventplus/event.hpp>
31#include <sdeventplus/source/event.hpp>
32#include <sdeventplus/source/io.hpp>
Patrick Venture33569752018-03-12 18:56:14 -070033#include <thread>
34
Benjamin Faire7b07f02018-07-02 09:51:37 -070035static const char* snoopFilename = "/dev/aspeed-lpc-snoop0";
Benjamin Fairf69ad7e2018-07-13 13:41:10 -070036static size_t codeSize = 1; /* Size of each POST code in bytes */
Benjamin Faire7b07f02018-07-02 09:51:37 -070037
Patrick Venture33569752018-03-12 18:56:14 -070038/*
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 */
43static constexpr size_t BUFFER_SIZE = 256;
44
Benjamin Faire7b07f02018-07-02 09:51:37 -070045static void usage(const char* name)
46{
47 fprintf(stderr,
48 "Usage: %s [-d <DEVICE>]\n"
Benjamin Fairf69ad7e2018-07-13 13:41:10 -070049 " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. "
50 "Default is %zu\n"
Benjamin Faire7b07f02018-07-02 09:51:37 -070051 " -d, --device <DEVICE> use <DEVICE> file. Default is '%s'\n\n",
Benjamin Fairf69ad7e2018-07-13 13:41:10 -070052 name, codeSize, snoopFilename);
53}
54
55static 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 Faire7b07f02018-07-02 09:51:37 -070067}
68
Patrick Venture33569752018-03-12 18:56:14 -070069/*
Kun Yieb312312018-06-13 09:20:50 -070070 * Callback handling IO event from the POST code fd. i.e. there is new
71 * POST code available to read.
72 */
Kun Yi1c16ad82018-09-12 10:01:49 -070073void PostCodeEventHandler(sdeventplus::source::IO& s, int postFd,
74 uint32_t revents, PostReporter* reporter)
Kun Yieb312312018-06-13 09:20:50 -070075{
Kun Yieb312312018-06-13 09:20:50 -070076 std::array<uint8_t, BUFFER_SIZE> buffer;
77 int readb;
78
Kun Yi1c16ad82018-09-12 10:01:49 -070079 readb = read(postFd, buffer.data(), buffer.size());
80 if (readb < 0)
Kun Yieb312312018-06-13 09:20:50 -070081 {
Kun Yi1c16ad82018-09-12 10:01:49 -070082 /* Read failure. */
83 s.get_event().exit(1);
84 return;
Kun Yieb312312018-06-13 09:20:50 -070085 }
86
Kun Yi1c16ad82018-09-12 10:01:49 -070087 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 Yieb312312018-06-13 09:20:50 -0700102}
103
104/*
Patrick Venture33569752018-03-12 18:56:14 -0700105 * 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 */
111int main(int argc, char* argv[])
112{
113 int rc = 0;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700114 int opt;
Patrick Venture33569752018-03-12 18:56:14 -0700115 int postFd = -1;
Patrick Venture33569752018-03-12 18:56:14 -0700116
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 Faire7b07f02018-07-02 09:51:37 -0700126
Patrick Venture33569752018-03-12 18:56:14 -0700127 bool deferSignals = true;
128
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700129 // clang-format off
Benjamin Faire7b07f02018-07-02 09:51:37 -0700130 static const struct option long_options[] = {
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700131 {"bytes", required_argument, NULL, 'b'},
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700132 {"device", required_argument, NULL, 'd'},
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700133 {0, 0, 0, 0}
134 };
135 // clang-format on
Benjamin Faire7b07f02018-07-02 09:51:37 -0700136
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700137 while ((opt = getopt_long(argc, argv, "b:d:", long_options, NULL)) != -1)
Benjamin Faire7b07f02018-07-02 09:51:37 -0700138 {
139 switch (opt)
140 {
141 case 0:
142 break;
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700143 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 Faire7b07f02018-07-02 09:51:37 -0700155 case 'd':
156 snoopFilename = optarg;
157 break;
158 default:
159 usage(argv[0]);
160 exit(EXIT_FAILURE);
161 }
162 }
163
Patrick Venture33569752018-03-12 18:56:14 -0700164 postFd = open(snoopFilename, 0);
165 if (postFd < 0)
166 {
167 fprintf(stderr, "Unable to open: %s\n", snoopFilename);
168 return -1;
169 }
170
Patrick Venture33569752018-03-12 18:56:14 -0700171 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 Yieb312312018-06-13 09:20:50 -0700177 reporter.emit_object_added();
178 bus.request_name(snoopDbus);
Kun Yieb312312018-06-13 09:20:50 -0700179
Kun Yi1c16ad82018-09-12 10:01:49 -0700180 // 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 Yieb312312018-06-13 09:20:50 -0700196
Patrick Venture33569752018-03-12 18:56:14 -0700197 if (postFd > -1)
198 {
199 close(postFd);
200 }
201
Patrick Venture33569752018-03-12 18:56:14 -0700202 return rc;
203}