James Feist | 3cb5fec | 2018-01-23 14:41:51 -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 | |
| 17 | #include <Utils.hpp> |
| 18 | #include <boost/container/flat_map.hpp> |
| 19 | #include <ctime> |
| 20 | #include <dbus/connection.hpp> |
| 21 | #include <dbus/endpoint.hpp> |
| 22 | #include <dbus/message.hpp> |
| 23 | #include <dbus/properties.hpp> |
| 24 | #include <fcntl.h> |
| 25 | #include <fstream> |
| 26 | #include <future> |
James Feist | b5320a7 | 2018-01-24 12:28:12 -0800 | [diff] [blame^] | 27 | #include <linux/i2c-dev-user.h> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 28 | #include <iostream> |
| 29 | #include <sys/ioctl.h> |
| 30 | |
| 31 | namespace fs = std::experimental::filesystem; |
| 32 | static constexpr bool DEBUG = false; |
| 33 | static size_t UNKNOWN_BUS_OBJECT_COUNT = 0; |
| 34 | |
| 35 | const static constexpr char *BASEBOARD_FRU_LOCATION = |
| 36 | "/etc/fru/baseboard.fru.bin"; |
| 37 | |
| 38 | static constexpr std::array<const char *, 5> FRU_AREAS = { |
| 39 | "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"}; |
| 40 | |
| 41 | using DeviceMap = boost::container::flat_map<int, std::vector<char>>; |
| 42 | using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>; |
| 43 | |
| 44 | int get_bus_frus(int file, int first, int last, int bus, |
| 45 | std::shared_ptr<DeviceMap> devices) |
| 46 | { |
| 47 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> block_data; |
| 48 | |
| 49 | for (int ii = first; ii <= last; ii++) |
| 50 | { |
| 51 | // Set slave address |
| 52 | if (ioctl(file, I2C_SLAVE_FORCE, ii) < 0) |
| 53 | { |
| 54 | std::cerr << "device at bus " << bus << "register " << ii |
| 55 | << "busy\n"; |
| 56 | continue; |
| 57 | } |
| 58 | // probe |
| 59 | else if (i2c_smbus_read_byte(file) < 0) |
| 60 | { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | if (DEBUG) |
| 65 | { |
| 66 | std::cout << "something at bus " << bus << "addr " << ii << "\n"; |
| 67 | } |
| 68 | if (i2c_smbus_read_i2c_block_data(file, 0x0, 0x8, block_data.data()) < |
| 69 | 0) |
| 70 | { |
| 71 | std::cerr << "failed to read bus " << bus << " address " << ii |
| 72 | << "\n"; |
| 73 | continue; |
| 74 | } |
| 75 | size_t sum = 0; |
| 76 | for (int jj = 0; jj < 7; jj++) |
| 77 | { |
| 78 | sum += block_data[jj]; |
| 79 | } |
| 80 | sum = (256 - sum) & 0xFF; |
| 81 | |
| 82 | // check the header checksum |
| 83 | if (sum == block_data[7]) |
| 84 | { |
| 85 | std::vector<char> device; |
| 86 | device.insert(device.end(), block_data.begin(), |
| 87 | block_data.begin() + 8); |
| 88 | |
| 89 | for (int jj = 1; jj <= FRU_AREAS.size(); jj++) |
| 90 | { |
| 91 | auto area_offset = device[jj]; |
| 92 | if (area_offset != 0) |
| 93 | { |
| 94 | area_offset *= 8; |
| 95 | if (i2c_smbus_read_i2c_block_data(file, area_offset, 0x8, |
| 96 | block_data.data()) < 0) |
| 97 | { |
| 98 | std::cerr << "failed to read bus " << bus << " address " |
| 99 | << ii << "\n"; |
| 100 | return -1; |
| 101 | } |
| 102 | int length = block_data[1] * 8; |
| 103 | device.insert(device.end(), block_data.begin(), |
| 104 | block_data.begin() + 8); |
| 105 | length -= 8; |
| 106 | area_offset += 8; |
| 107 | |
| 108 | while (length > 0) |
| 109 | { |
| 110 | auto to_get = std::min(0x20, length); |
| 111 | if (i2c_smbus_read_i2c_block_data( |
| 112 | file, area_offset, to_get, block_data.data()) < |
| 113 | 0) |
| 114 | { |
| 115 | std::cerr << "failed to read bus " << bus |
| 116 | << " address " << ii << "\n"; |
| 117 | return -1; |
| 118 | } |
| 119 | device.insert(device.end(), block_data.begin(), |
| 120 | block_data.begin() + to_get); |
| 121 | area_offset += to_get; |
| 122 | length -= to_get; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | (*devices).emplace(ii, device); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | static BusMap FindI2CDevices(const std::vector<fs::path> &i2cBuses) |
| 134 | { |
| 135 | static std::vector<std::future<void>> futures; |
| 136 | BusMap busMap; |
| 137 | for (auto &i2cBus : i2cBuses) |
| 138 | { |
| 139 | auto busnum = i2cBus.string(); |
| 140 | auto lastDash = busnum.rfind(std::string("-")); |
| 141 | // delete everything before dash inclusive |
| 142 | if (lastDash != std::string::npos) |
| 143 | { |
| 144 | busnum.erase(0, lastDash + 1); |
| 145 | } |
| 146 | auto bus = std::stoi(busnum); |
| 147 | |
| 148 | auto file = open(i2cBus.c_str(), O_RDWR); |
| 149 | if (file < 0) |
| 150 | { |
| 151 | std::cerr << "unable to open i2c device " << i2cBus.string() |
| 152 | << "\n"; |
| 153 | continue; |
| 154 | } |
| 155 | unsigned long funcs = 0; |
| 156 | |
| 157 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) |
| 158 | { |
| 159 | std::cerr |
| 160 | << "Error: Could not get the adapter functionality matrix bus" |
| 161 | << bus << "\n"; |
| 162 | continue; |
| 163 | } |
| 164 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) || |
| 165 | !(I2C_FUNC_SMBUS_READ_I2C_BLOCK)) |
| 166 | { |
| 167 | std::cerr << "Error: Can't use SMBus Receive Byte command bus " |
| 168 | << bus << "\n"; |
| 169 | continue; |
| 170 | } |
| 171 | auto &device = busMap[bus]; |
| 172 | device = std::make_shared<DeviceMap>(); |
| 173 | |
| 174 | // todo: call with boost asio? |
| 175 | futures.emplace_back( |
| 176 | std::async(std::launch::async, [file, device, bus] { |
| 177 | // i2cdetect by default uses the range 0x03 to 0x77, as this is |
| 178 | // what we |
| 179 | // have tested with, use this range. Could be changed in |
| 180 | // future. |
| 181 | get_bus_frus(file, 0x03, 0x77, bus, device); |
| 182 | close(file); |
| 183 | })); |
| 184 | } |
| 185 | for (auto &fut : futures) |
| 186 | { |
| 187 | fut.get(); // wait for all scans |
| 188 | } |
| 189 | return busMap; |
| 190 | } |
| 191 | |
| 192 | static const std::tm intelEpoch(void) |
| 193 | { |
| 194 | std::tm val = {0}; |
| 195 | val.tm_year = 1996 - 1900; |
| 196 | return val; |
| 197 | } |
| 198 | |
| 199 | bool formatFru(const std::vector<char> &fruBytes, |
| 200 | boost::container::flat_map<std::string, std::string> &result) |
| 201 | { |
| 202 | static const std::vector<const char *> CHASSIS_FRU_AREAS = { |
| 203 | "PART_NUMBER", "SERIAL_NUMBER", "CHASSIS_INFO_AM1", "CHASSIS_INFO_AM2"}; |
| 204 | |
| 205 | static const std::vector<const char *> BOARD_FRU_AREAS = { |
| 206 | "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER", |
| 207 | "VERSION_ID"}; |
| 208 | |
| 209 | static const std::vector<const char *> PRODUCT_FRU_AREAS = { |
| 210 | "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", |
| 211 | "PRODUCT_VERSION", "PRODUCT_SERIAL_NUMBER", "ASSET_TAG"}; |
| 212 | |
| 213 | size_t sum = 0; |
| 214 | |
| 215 | if (fruBytes.size() < 8) |
| 216 | { |
| 217 | return false; |
| 218 | } |
| 219 | std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin(); |
| 220 | result["Common Format Version"] = |
| 221 | std::to_string(static_cast<int>(*fruAreaOffsetField)); |
| 222 | |
| 223 | const std::vector<const char *> *fieldData; |
| 224 | |
| 225 | for (auto &area : FRU_AREAS) |
| 226 | { |
| 227 | fruAreaOffsetField++; |
| 228 | if (fruAreaOffsetField >= fruBytes.end()) |
| 229 | { |
| 230 | return false; |
| 231 | } |
| 232 | size_t offset = (*fruAreaOffsetField) * 8; |
| 233 | |
| 234 | if (offset > 1) |
| 235 | { |
| 236 | // +2 to skip format and length |
| 237 | std::vector<char>::const_iterator fruBytesIter = |
| 238 | fruBytes.begin() + offset + 2; |
| 239 | |
| 240 | if (fruBytesIter >= fruBytes.end()) |
| 241 | { |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | if (area == "CHASSIS") |
| 246 | { |
| 247 | result["CHASSIS_TYPE"] = |
| 248 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 249 | fruBytesIter += 1; |
| 250 | fieldData = &CHASSIS_FRU_AREAS; |
| 251 | } |
| 252 | else if (area == "BOARD") |
| 253 | { |
| 254 | result["BOARD_LANGUAGE_CODE"] = |
| 255 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 256 | fruBytesIter += 1; |
| 257 | if (fruBytesIter >= fruBytes.end()) |
| 258 | { |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | unsigned int minutes = *fruBytesIter | |
| 263 | *(fruBytesIter + 1) << 8 | |
| 264 | *(fruBytesIter + 2) << 16; |
| 265 | std::tm fruTime = intelEpoch(); |
| 266 | time_t timeValue = mktime(&fruTime); |
| 267 | timeValue += minutes * 60; |
| 268 | fruTime = *gmtime(&timeValue); |
| 269 | |
| 270 | result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime); |
| 271 | result["BOARD_MANUFACTURE_DATE"] |
| 272 | .pop_back(); // remove trailing null |
| 273 | fruBytesIter += 3; |
| 274 | fieldData = &BOARD_FRU_AREAS; |
| 275 | } |
| 276 | else if (area == "PRODUCT") |
| 277 | { |
| 278 | result["PRODUCT_LANGUAGE_CODE"] = |
| 279 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 280 | fruBytesIter += 1; |
| 281 | fieldData = &PRODUCT_FRU_AREAS; |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | continue; |
| 286 | } |
| 287 | for (auto &field : *fieldData) |
| 288 | { |
| 289 | if (fruBytesIter >= fruBytes.end()) |
| 290 | { |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | size_t length = *fruBytesIter & 0x3f; |
| 295 | fruBytesIter += 1; |
| 296 | |
| 297 | if (fruBytesIter >= fruBytes.end()) |
| 298 | { |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | result[std::string(area) + "_" + field] = |
| 303 | std::string(fruBytesIter, fruBytesIter + length); |
| 304 | fruBytesIter += length; |
| 305 | if (fruBytesIter >= fruBytes.end()) |
| 306 | { |
| 307 | std::cerr << "Warning Fru Length Mismatch:\n "; |
| 308 | for (auto &c : fruBytes) |
| 309 | { |
| 310 | std::cerr << c; |
| 311 | } |
| 312 | std::cerr << "\n"; |
| 313 | if (DEBUG) |
| 314 | { |
| 315 | for (auto &keyPair : result) |
| 316 | { |
| 317 | std::cerr << keyPair.first << " : " |
| 318 | << keyPair.second << "\n"; |
| 319 | } |
| 320 | } |
| 321 | return false; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | void AddFruObjectToDbus( |
| 331 | std::shared_ptr<dbus::connection> dbusConn, std::vector<char> &device, |
| 332 | dbus::DbusObjectServer &objServer, |
| 333 | boost::container::flat_map<std::pair<size_t, size_t>, |
| 334 | std::shared_ptr<dbus::DbusObject>> |
| 335 | &dbusObjectMap, |
| 336 | int bus, size_t address) |
| 337 | { |
| 338 | boost::container::flat_map<std::string, std::string> formattedFru; |
| 339 | if (!formatFru(device, formattedFru)) |
| 340 | { |
| 341 | std::cerr << "failed to format fru for device at bus " << std::hex |
| 342 | << bus << "address " << address << "\n"; |
| 343 | return; |
| 344 | } |
| 345 | auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME"); |
| 346 | std::string productName; |
| 347 | if (productNameFind == formattedFru.end()) |
| 348 | { |
| 349 | productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME"); |
| 350 | } |
| 351 | if (productNameFind != formattedFru.end()) |
| 352 | { |
| 353 | productName = productNameFind->second; |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT); |
| 358 | UNKNOWN_BUS_OBJECT_COUNT++; |
| 359 | } |
| 360 | |
| 361 | auto object = |
| 362 | objServer.add_object("/xyz/openbmc_project/FruDevice/" + productName); |
| 363 | dbusObjectMap[std::pair<size_t, size_t>(bus, address)] = object; |
| 364 | |
| 365 | auto iface = std::make_shared<dbus::DbusInterface>( |
| 366 | "xyz.openbmc_project.FruDevice", dbusConn); |
| 367 | object->register_interface(iface); |
| 368 | for (auto &property : formattedFru) |
| 369 | { |
| 370 | iface->set_property(property.first, property.second); |
| 371 | } |
| 372 | // baseboard can set this to -1 to not set a bus / address |
| 373 | if (bus > 0) |
| 374 | { |
| 375 | std::stringstream data; |
| 376 | data << "0x" << std::hex << bus; |
| 377 | iface->set_property("BUS", data.str()); |
| 378 | data.str(""); |
| 379 | data << "0x" << std::hex << address; |
| 380 | iface->set_property("ADDRESS", data.str()); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static bool readBaseboardFru(std::vector<char> &baseboardFru) |
| 385 | { |
| 386 | // try to read baseboard fru from file |
| 387 | std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary); |
| 388 | if (baseboardFruFile.good()) |
| 389 | { |
| 390 | baseboardFruFile.seekg(0, std::ios_base::end); |
| 391 | std::streampos fileSize = baseboardFruFile.tellg(); |
| 392 | baseboardFru.resize(fileSize); |
| 393 | baseboardFruFile.seekg(0, std::ios_base::beg); |
| 394 | baseboardFruFile.read(baseboardFru.data(), fileSize); |
| 395 | } |
| 396 | else |
| 397 | { |
| 398 | return false; |
| 399 | } |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | int main(int argc, char **argv) |
| 404 | { |
| 405 | auto devDir = fs::path("/dev/"); |
| 406 | auto matchString = std::string("i2c*"); |
| 407 | std::vector<fs::path> i2cBuses; |
| 408 | |
| 409 | if (!find_files(devDir, matchString, i2cBuses, 0)) |
| 410 | { |
| 411 | std::cerr << "unable to find i2c devices\n"; |
| 412 | return 1; |
| 413 | } |
| 414 | BusMap busMap = FindI2CDevices(i2cBuses); |
| 415 | |
| 416 | boost::asio::io_service io; |
| 417 | auto systemBus = std::make_shared<dbus::connection>(io, dbus::bus::system); |
| 418 | dbus::DbusObjectServer objServer(systemBus); |
| 419 | systemBus->request_name("com.intel.FruDevice"); |
| 420 | |
| 421 | // this is a map with keys of pair(bus number, adddress) and values of the |
| 422 | // object on dbus |
| 423 | boost::container::flat_map<std::pair<size_t, size_t>, |
| 424 | std::shared_ptr<dbus::DbusObject>> |
| 425 | dbusObjectMap; |
| 426 | |
| 427 | for (auto &devicemap : busMap) |
| 428 | { |
| 429 | for (auto &device : *devicemap.second) |
| 430 | { |
| 431 | AddFruObjectToDbus(systemBus, device.second, objServer, |
| 432 | dbusObjectMap, devicemap.first, device.first); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | std::vector<char> baseboardFru; |
| 437 | if (readBaseboardFru(baseboardFru)) |
| 438 | { |
| 439 | AddFruObjectToDbus(systemBus, baseboardFru, objServer, dbusObjectMap, |
| 440 | -1, -1); |
| 441 | } |
| 442 | |
| 443 | auto object = std::make_shared<dbus::DbusObject>( |
| 444 | systemBus, "/xyz/openbmc_project/FruDevice"); |
| 445 | objServer.register_object(object); |
| 446 | auto iface = std::make_shared<dbus::DbusInterface>( |
| 447 | "xyz.openbmc_project.FruDeviceManager", systemBus); |
| 448 | object->register_interface(iface); |
| 449 | |
| 450 | iface->register_method("ReScan", [&]() { |
| 451 | busMap = FindI2CDevices(i2cBuses); |
| 452 | |
| 453 | for (auto &busObj : dbusObjectMap) |
| 454 | { |
| 455 | objServer.remove_object(busObj.second); |
| 456 | } |
| 457 | dbusObjectMap.clear(); |
| 458 | UNKNOWN_BUS_OBJECT_COUNT = 0; |
| 459 | |
| 460 | for (auto &devicemap : busMap) |
| 461 | { |
| 462 | for (auto &device : *devicemap.second) |
| 463 | { |
| 464 | AddFruObjectToDbus(systemBus, device.second, objServer, |
| 465 | dbusObjectMap, devicemap.first, |
| 466 | device.first); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | return std::tuple<>(); // this is a bug in boost-dbus, needs some sort |
| 471 | // of return |
| 472 | }); |
| 473 | |
| 474 | io.run(); |
| 475 | return 0; |
| 476 | } |