blob: 1b65964ec4b25aea8cd4c9766bc4008776ca7360 [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
William A. Kennington III6dac4c52019-12-13 15:05:00 -080019#include <endian.h>
Patrick Venture33569752018-03-12 18:56:14 -070020#include <fcntl.h>
Benjamin Faire7b07f02018-07-02 09:51:37 -070021#include <getopt.h>
Patrick Ventureb5754fd2018-09-10 13:13:58 -070022#include <sys/epoll.h>
23#include <systemd/sd-event.h>
Patrick Venture33569752018-03-12 18:56:14 -070024#include <unistd.h>
25
Patrick Venture33569752018-03-12 18:56:14 -070026#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
Benjamin Faire7b07f02018-07-02 09:51:37 -070038static void usage(const char* name)
39{
40 fprintf(stderr,
41 "Usage: %s [-d <DEVICE>]\n"
Benjamin Fairf69ad7e2018-07-13 13:41:10 -070042 " -b, --bytes <SIZE> set POST code length to <SIZE> bytes. "
43 "Default is %zu\n"
Benjamin Faire7b07f02018-07-02 09:51:37 -070044 " -d, --device <DEVICE> use <DEVICE> file. Default is '%s'\n\n",
Benjamin Fairf69ad7e2018-07-13 13:41:10 -070045 name, codeSize, snoopFilename);
46}
47
Patrick Venture33569752018-03-12 18:56:14 -070048/*
Kun Yieb312312018-06-13 09:20:50 -070049 * Callback handling IO event from the POST code fd. i.e. there is new
50 * POST code available to read.
51 */
Kun Yi1c16ad82018-09-12 10:01:49 -070052void PostCodeEventHandler(sdeventplus::source::IO& s, int postFd,
53 uint32_t revents, PostReporter* reporter)
Kun Yieb312312018-06-13 09:20:50 -070054{
William A. Kennington III6dac4c52019-12-13 15:05:00 -080055 uint64_t code = 0;
William A. Kennington III0f964b42020-04-16 18:46:03 -070056 ssize_t readb;
57 while ((readb = read(postFd, &code, codeSize)) > 0)
Kun Yi1c16ad82018-09-12 10:01:49 -070058 {
William A. Kennington IIIf21475a2019-12-13 17:21:24 -080059 code = le64toh(code);
60 // HACK: Always send property changed signal even for the same code
61 // since we are single threaded, external users will never see the
62 // first value.
63 reporter->value(~code, true);
64 reporter->value(code);
William A. Kennington III0f964b42020-04-16 18:46:03 -070065
66 // read depends on old data being cleared since it doens't always read
67 // the full code size
68 code = 0;
Kun Yi1c16ad82018-09-12 10:01:49 -070069 }
William A. Kennington III0f964b42020-04-16 18:46:03 -070070
71 if (readb < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
72 {
73 return;
74 }
75
76 /* Read failure. */
77 if (readb == 0)
78 {
79 fprintf(stderr, "Unexpected EOF reading postcode\n");
80 }
81 else
82 {
83 fprintf(stderr, "Failed to read postcode: %s\n", strerror(errno));
84 }
85 s.get_event().exit(1);
Kun Yieb312312018-06-13 09:20:50 -070086}
87
88/*
Patrick Venture33569752018-03-12 18:56:14 -070089 * TODO(venture): this only listens one of the possible snoop ports, but
90 * doesn't share the namespace.
91 *
92 * This polls() the lpc snoop character device and it owns the dbus object
93 * whose value is the latest port 80h value.
94 */
95int main(int argc, char* argv[])
96{
97 int rc = 0;
Benjamin Faire7b07f02018-07-02 09:51:37 -070098 int opt;
Patrick Venture33569752018-03-12 18:56:14 -070099 int postFd = -1;
Patrick Venture33569752018-03-12 18:56:14 -0700100
101 /*
102 * These string constants are only used in this method within this object
103 * and this object is the only object feeding into the final binary.
104 *
105 * If however, another object is added to this binary it would be proper
106 * to move these declarations to be global and extern to the other object.
107 */
108 const char* snoopObject = SNOOP_OBJECTPATH;
109 const char* snoopDbus = SNOOP_BUSNAME;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700110
Patrick Venture33569752018-03-12 18:56:14 -0700111 bool deferSignals = true;
112
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700113 // clang-format off
Benjamin Faire7b07f02018-07-02 09:51:37 -0700114 static const struct option long_options[] = {
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700115 {"bytes", required_argument, NULL, 'b'},
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700116 {"device", required_argument, NULL, 'd'},
Patrick Venture1ceb21b2018-08-08 11:29:36 -0700117 {0, 0, 0, 0}
118 };
119 // clang-format on
Benjamin Faire7b07f02018-07-02 09:51:37 -0700120
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700121 while ((opt = getopt_long(argc, argv, "b:d:", long_options, NULL)) != -1)
Benjamin Faire7b07f02018-07-02 09:51:37 -0700122 {
123 switch (opt)
124 {
125 case 0:
126 break;
Benjamin Fairf69ad7e2018-07-13 13:41:10 -0700127 case 'b':
128 codeSize = atoi(optarg);
129
130 if (codeSize < 1 || codeSize > 8)
131 {
132 fprintf(stderr,
133 "Invalid POST code size '%s'. Must be "
134 "an integer from 1 to 8.\n",
135 optarg);
136 exit(EXIT_FAILURE);
137 }
138 break;
Benjamin Faire7b07f02018-07-02 09:51:37 -0700139 case 'd':
140 snoopFilename = optarg;
141 break;
142 default:
143 usage(argv[0]);
144 exit(EXIT_FAILURE);
145 }
146 }
147
Tim Leee4090de2019-11-26 13:57:43 +0800148 postFd = open(snoopFilename, O_NONBLOCK);
Patrick Venture33569752018-03-12 18:56:14 -0700149 if (postFd < 0)
150 {
151 fprintf(stderr, "Unable to open: %s\n", snoopFilename);
152 return -1;
153 }
154
Patrick Venture33569752018-03-12 18:56:14 -0700155 auto bus = sdbusplus::bus::new_default();
156
157 // Add systemd object manager.
158 sdbusplus::server::manager::manager(bus, snoopObject);
159
160 PostReporter reporter(bus, snoopObject, deferSignals);
Kun Yieb312312018-06-13 09:20:50 -0700161 reporter.emit_object_added();
162 bus.request_name(snoopDbus);
Kun Yieb312312018-06-13 09:20:50 -0700163
Kun Yi1c16ad82018-09-12 10:01:49 -0700164 // Create sdevent and add IO source
165 try
166 {
167 sdeventplus::Event event = sdeventplus::Event::get_default();
168 sdeventplus::source::IO reporterSource(
William A. Kennington III0f964b42020-04-16 18:46:03 -0700169 event, postFd, EPOLLIN | EPOLLET,
Kun Yi1c16ad82018-09-12 10:01:49 -0700170 std::bind(PostCodeEventHandler, std::placeholders::_1,
171 std::placeholders::_2, std::placeholders::_3, &reporter));
172 // Enable bus to handle incoming IO and bus events
173 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
174 rc = event.loop();
175 }
176 catch (const std::exception& e)
177 {
178 fprintf(stderr, "%s\n", e.what());
179 }
Kun Yieb312312018-06-13 09:20:50 -0700180
Patrick Venture33569752018-03-12 18:56:14 -0700181 if (postFd > -1)
182 {
183 close(postFd);
184 }
185
Patrick Venture33569752018-03-12 18:56:14 -0700186 return rc;
187}