Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 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 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "ChassisIntrusionSensor.hpp" |
| 18 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | #include <sys/ioctl.h> |
Matt Simmering | 79a160b | 2022-11-14 16:50:48 -0800 | [diff] [blame] | 21 | #include <systemd/sd-journal.h> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 24 | #include <Utils.hpp> |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 25 | #include <boost/asio/io_context.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 26 | #include <sdbusplus/asio/object_server.hpp> |
| 27 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 28 | #include <cerrno> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 29 | #include <chrono> |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 30 | #include <fstream> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 31 | #include <iostream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 32 | #include <memory> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 33 | #include <string> |
| 34 | #include <thread> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 35 | #include <utility> |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 36 | |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 37 | extern "C" |
| 38 | { |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 39 | #include <i2c/smbus.h> |
| 40 | #include <linux/i2c-dev.h> |
| 41 | } |
| 42 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 43 | static constexpr bool debug = false; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 44 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 45 | static constexpr unsigned int defaultPollSec = 1; |
| 46 | static constexpr unsigned int sensorFailedPollSec = 5; |
| 47 | static unsigned int intrusionSensorPollSec = defaultPollSec; |
| 48 | static constexpr const char* hwIntrusionValStr = "HardwareIntrusion"; |
| 49 | static constexpr const char* normalValStr = "Normal"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 50 | |
| 51 | // SMLink Status Register |
| 52 | const static constexpr size_t pchStatusRegIntrusion = 0x04; |
| 53 | |
| 54 | // Status bit field masks |
| 55 | const static constexpr size_t pchRegMaskIntrusion = 0x01; |
| 56 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 57 | void ChassisIntrusionSensor::updateValue(const size_t& value) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 58 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 59 | std::string newValue = value != 0 ? hwIntrusionValStr : normalValStr; |
| 60 | |
Josh Lehan | 833661a | 2020-03-04 17:35:41 -0800 | [diff] [blame] | 61 | // Take no action if value already equal |
| 62 | // Same semantics as Sensor::updateValue(const double&) |
| 63 | if (newValue == mValue) |
| 64 | { |
| 65 | return; |
| 66 | } |
| 67 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 68 | if constexpr (debug) |
| 69 | { |
| 70 | std::cout << "Update value from " << mValue << " to " << newValue |
| 71 | << "\n"; |
| 72 | } |
| 73 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 74 | // indicate that it is internal set call |
| 75 | mInternalSet = true; |
| 76 | mIface->set_property("Status", newValue); |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 77 | mInternalSet = false; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 78 | |
| 79 | mValue = newValue; |
| 80 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 81 | if (mOldValue == normalValStr && mValue != normalValStr) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 82 | { |
Matt Simmering | 79a160b | 2022-11-14 16:50:48 -0800 | [diff] [blame] | 83 | sd_journal_send("MESSAGE=%s", "Chassis intrusion assert event", |
| 84 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 85 | "OpenBMC.0.1.ChassisIntrusionDetected", NULL); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 86 | mOldValue = mValue; |
| 87 | } |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 88 | else if (mOldValue == hwIntrusionValStr && mValue == normalValStr) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 89 | { |
Matt Simmering | 79a160b | 2022-11-14 16:50:48 -0800 | [diff] [blame] | 90 | sd_journal_send("MESSAGE=%s", "Chassis intrusion de-assert event", |
| 91 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 92 | "OpenBMC.0.1.ChassisIntrusionReset", NULL); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 93 | mOldValue = mValue; |
| 94 | } |
| 95 | } |
| 96 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 97 | int ChassisIntrusionPchSensor::readSensor() |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 98 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 99 | int32_t statusMask = pchRegMaskIntrusion; |
| 100 | int32_t statusReg = pchStatusRegIntrusion; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 101 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 102 | int32_t value = i2c_smbus_read_byte_data(mBusFd, statusReg); |
| 103 | if constexpr (debug) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 104 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 105 | std::cout << "Pch type: raw value is " << value << "\n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 106 | } |
| 107 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 108 | if (value < 0) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 109 | { |
| 110 | std::cerr << "i2c_smbus_read_byte_data failed \n"; |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | // Get status value with mask |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 115 | value &= statusMask; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 116 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 117 | if constexpr (debug) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 118 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 119 | std::cout << "Pch type: masked raw value is " << value << "\n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 120 | } |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 121 | return value; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 122 | } |
| 123 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 124 | void ChassisIntrusionPchSensor::pollSensorStatus() |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 125 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 126 | std::weak_ptr<ChassisIntrusionPchSensor> weakRef = weak_from_this(); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 127 | // setting a new experation implicitly cancels any pending async wait |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 128 | mPollTimer.expires_after(std::chrono::seconds(intrusionSensorPollSec)); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 129 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 130 | mPollTimer.async_wait([weakRef](const boost::system::error_code& ec) { |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 131 | // case of being canceled |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 132 | if (ec == boost::asio::error::operation_aborted) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 133 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 134 | std::cerr << "Timer of intrusion sensor is cancelled\n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 135 | return; |
| 136 | } |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 137 | std::shared_ptr<ChassisIntrusionPchSensor> self = weakRef.lock(); |
| 138 | if (!self) |
| 139 | { |
| 140 | std::cerr << "ChassisIntrusionSensor no self\n"; |
| 141 | return; |
| 142 | } |
| 143 | int value = self->readSensor(); |
| 144 | if (value < 0) |
| 145 | { |
| 146 | intrusionSensorPollSec = sensorFailedPollSec; |
| 147 | } |
| 148 | else |
| 149 | { |
| 150 | intrusionSensorPollSec = defaultPollSec; |
| 151 | self->updateValue(value); |
| 152 | } |
| 153 | // trigger next polling |
| 154 | self->pollSensorStatus(); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 155 | }); |
| 156 | } |
| 157 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 158 | int ChassisIntrusionGpioSensor::readSensor() |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 159 | { |
ZhikuiRen | ba8a8bf | 2020-01-09 15:55:43 -0800 | [diff] [blame] | 160 | mGpioLine.event_read(); |
| 161 | auto value = mGpioLine.get_value(); |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 162 | if constexpr (debug) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 163 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 164 | std::cout << "Gpio type: raw value is " << value << "\n"; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 165 | } |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 166 | return value; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 167 | } |
| 168 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 169 | void ChassisIntrusionGpioSensor::pollSensorStatus() |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 170 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 171 | mGpioFd.async_wait(boost::asio::posix::stream_descriptor::wait_read, |
| 172 | [this](const boost::system::error_code& ec) { |
| 173 | if (ec == boost::system::errc::bad_file_descriptor) |
| 174 | { |
| 175 | return; // we're being destroyed |
| 176 | } |
| 177 | if (ec) |
| 178 | { |
| 179 | std::cerr << "Error on GPIO based intrusion sensor wait event\n"; |
| 180 | } |
| 181 | else |
| 182 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 183 | int value = readSensor(); |
| 184 | if (value >= 0) |
| 185 | { |
| 186 | updateValue(value); |
| 187 | } |
| 188 | // trigger next polling |
| 189 | pollSensorStatus(); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 190 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 191 | }); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 192 | } |
| 193 | |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 194 | int ChassisIntrusionSensor::setSensorValue(const std::string& req, |
| 195 | std::string& propertyValue) |
| 196 | { |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 197 | if (!mInternalSet) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 198 | { |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 199 | propertyValue = req; |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 200 | mOverridenState = true; |
| 201 | } |
Richard Marian Thomaiyar | af6b87c | 2019-04-03 23:54:28 +0530 | [diff] [blame] | 202 | else if (!mOverridenState) |
| 203 | { |
| 204 | propertyValue = req; |
| 205 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 206 | return 1; |
| 207 | } |
| 208 | |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 209 | void ChassisIntrusionSensor::start() |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 210 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 211 | mIface->register_property( |
| 212 | "Status", mValue, |
| 213 | [&](const std::string& req, std::string& propertyValue) { |
| 214 | return setSensorValue(req, propertyValue); |
| 215 | }); |
| 216 | mIface->initialize(); |
| 217 | pollSensorStatus(); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | ChassisIntrusionSensor::ChassisIntrusionSensor( |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 221 | sdbusplus::asio::object_server& objServer) : |
| 222 | mObjServer(objServer) |
| 223 | { |
| 224 | mIface = mObjServer.add_interface("/xyz/openbmc_project/Chassis/Intrusion", |
| 225 | "xyz.openbmc_project.Chassis.Intrusion"); |
| 226 | } |
| 227 | |
| 228 | ChassisIntrusionPchSensor::ChassisIntrusionPchSensor( |
| 229 | boost::asio::io_context& io, sdbusplus::asio::object_server& objServer, |
| 230 | int busId, int slaveAddr) : |
| 231 | ChassisIntrusionSensor(objServer), |
| 232 | mPollTimer(io) |
| 233 | { |
| 234 | if (busId < 0 || slaveAddr <= 0) |
| 235 | { |
| 236 | throw std::invalid_argument("Invalid i2c bus " + std::to_string(busId) + |
| 237 | " address " + std::to_string(slaveAddr) + |
| 238 | "\n"); |
| 239 | } |
| 240 | mSlaveAddr = slaveAddr; |
| 241 | std::string devPath = "/dev/i2c-" + std::to_string(busId); |
| 242 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 243 | mBusFd = open(devPath.c_str(), O_RDWR | O_CLOEXEC); |
| 244 | if (mBusFd < 0) |
| 245 | { |
| 246 | throw std::invalid_argument("Unable to open " + devPath + "\n"); |
| 247 | } |
| 248 | |
| 249 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 250 | if (ioctl(mBusFd, I2C_SLAVE_FORCE, mSlaveAddr) < 0) |
| 251 | { |
| 252 | throw std::runtime_error("Unable to set device address\n"); |
| 253 | } |
| 254 | |
| 255 | unsigned long funcs = 0; |
| 256 | |
| 257 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 258 | if (ioctl(mBusFd, I2C_FUNCS, &funcs) < 0) |
| 259 | { |
| 260 | throw std::runtime_error("Don't support I2C_FUNCS\n"); |
| 261 | } |
| 262 | |
| 263 | if ((funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA) == 0U) |
| 264 | { |
| 265 | throw std::runtime_error( |
| 266 | "Do not have I2C_FUNC_SMBUS_READ_BYTE_DATA \n"); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | ChassisIntrusionGpioSensor::ChassisIntrusionGpioSensor( |
| 271 | boost::asio::io_context& io, sdbusplus::asio::object_server& objServer, |
| 272 | bool gpioInverted) : |
| 273 | ChassisIntrusionSensor(objServer), |
| 274 | mGpioInverted(gpioInverted), mGpioFd(io) |
| 275 | { |
| 276 | mGpioLine = gpiod::find_line(mPinName); |
| 277 | if (!mGpioLine) |
| 278 | { |
| 279 | throw std::invalid_argument("Error finding gpio pin name: " + mPinName + |
| 280 | "\n"); |
| 281 | } |
| 282 | mGpioLine.request( |
| 283 | {"ChassisIntrusionSensor", gpiod::line_request::EVENT_BOTH_EDGES, |
| 284 | mGpioInverted ? gpiod::line_request::FLAG_ACTIVE_LOW : 0}); |
| 285 | |
| 286 | auto gpioLineFd = mGpioLine.event_get_fd(); |
| 287 | if (gpioLineFd < 0) |
| 288 | { |
| 289 | throw std::invalid_argument("Failed to get " + mPinName + " fd\n"); |
| 290 | } |
| 291 | mGpioFd.assign(gpioLineFd); |
| 292 | } |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 293 | |
| 294 | ChassisIntrusionSensor::~ChassisIntrusionSensor() |
| 295 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 296 | mObjServer.remove_interface(mIface); |
| 297 | } |
| 298 | |
| 299 | ChassisIntrusionPchSensor::~ChassisIntrusionPchSensor() |
| 300 | { |
| 301 | mPollTimer.cancel(); |
| 302 | if (close(mBusFd) < 0) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 303 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 304 | std::cerr << "Failed to close fd " << std::to_string(mBusFd); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 305 | } |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | ChassisIntrusionGpioSensor::~ChassisIntrusionGpioSensor() |
| 309 | { |
| 310 | mGpioFd.close(); |
| 311 | if (mGpioLine) |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 312 | { |
Chau Ly | cebb28c | 2022-10-21 10:01:52 +0000 | [diff] [blame] | 313 | mGpioLine.release(); |
Qiang XU | e28d1fa | 2019-02-27 13:50:56 +0800 | [diff] [blame] | 314 | } |
| 315 | } |