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 /// \file fru_device.cpp 17 18 #include "fru_utils.hpp" 19 #include "utils.hpp" 20 21 #include <fcntl.h> 22 #include <sys/inotify.h> 23 #include <sys/ioctl.h> 24 25 #include <boost/algorithm/string/predicate.hpp> 26 #include <boost/asio/io_context.hpp> 27 #include <boost/asio/steady_timer.hpp> 28 #include <boost/container/flat_map.hpp> 29 #include <nlohmann/json.hpp> 30 #include <sdbusplus/asio/connection.hpp> 31 #include <sdbusplus/asio/object_server.hpp> 32 33 #include <array> 34 #include <cerrno> 35 #include <charconv> 36 #include <chrono> 37 #include <ctime> 38 #include <filesystem> 39 #include <fstream> 40 #include <functional> 41 #include <future> 42 #include <iomanip> 43 #include <iostream> 44 #include <limits> 45 #include <map> 46 #include <optional> 47 #include <regex> 48 #include <set> 49 #include <sstream> 50 #include <string> 51 #include <thread> 52 #include <utility> 53 #include <variant> 54 #include <vector> 55 56 extern "C" 57 { 58 #include <i2c/smbus.h> 59 #include <linux/i2c-dev.h> 60 } 61 62 namespace fs = std::filesystem; 63 static constexpr bool debug = false; 64 constexpr size_t maxFruSize = 512; 65 constexpr size_t maxEepromPageIndex = 255; 66 constexpr size_t busTimeoutSeconds = 10; 67 68 constexpr const char* blocklistPath = PACKAGE_DIR "blacklist.json"; 69 70 const static constexpr char* baseboardFruLocation = 71 "/etc/fru/baseboard.fru.bin"; 72 73 const static constexpr char* i2CDevLocation = "/dev"; 74 75 static boost::container::flat_map<size_t, std::optional<std::set<size_t>>> 76 busBlocklist; 77 struct FindDevicesWithCallback; 78 79 static boost::container::flat_map< 80 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>> 81 foundDevices; 82 83 static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses; 84 static boost::container::flat_map<size_t, std::set<size_t>> fruAddresses; 85 86 boost::asio::io_context io; 87 88 bool updateFRUProperty( 89 const std::string& updatePropertyReq, uint32_t bus, uint32_t address, 90 const std::string& propertyName, 91 boost::container::flat_map< 92 std::pair<size_t, size_t>, 93 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, 94 size_t& unknownBusObjectCount, const bool& powerIsOn, 95 sdbusplus::asio::object_server& objServer, 96 std::shared_ptr<sdbusplus::asio::connection>& systemBus); 97 98 // Given a bus/address, produce the path in sysfs for an eeprom. 99 static std::string getEepromPath(size_t bus, size_t address) 100 { 101 std::stringstream output; 102 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right 103 << std::setfill('0') << std::setw(4) << std::hex << address 104 << "/eeprom"; 105 return output.str(); 106 } 107 108 static bool hasEepromFile(size_t bus, size_t address) 109 { 110 auto path = getEepromPath(bus, address); 111 try 112 { 113 return fs::exists(path); 114 } 115 catch (...) 116 { 117 return false; 118 } 119 } 120 121 static int64_t readFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf) 122 { 123 auto result = lseek(fd, offset, SEEK_SET); 124 if (result < 0) 125 { 126 std::cerr << "failed to seek\n"; 127 return -1; 128 } 129 130 return read(fd, buf, len); 131 } 132 133 static int busStrToInt(const std::string_view busName) 134 { 135 auto findBus = busName.rfind('-'); 136 if (findBus == std::string::npos) 137 { 138 return -1; 139 } 140 std::string_view num = busName.substr(findBus + 1); 141 int val = 0; 142 std::from_chars(num.data(), num.data() + num.size(), val); 143 return val; 144 } 145 146 static int getRootBus(size_t bus) 147 { 148 auto ec = std::error_code(); 149 auto path = std::filesystem::read_symlink( 150 std::filesystem::path("/sys/bus/i2c/devices/i2c-" + 151 std::to_string(bus) + "/mux_device"), 152 ec); 153 if (ec) 154 { 155 return -1; 156 } 157 158 std::string filename = path.filename(); 159 auto findBus = filename.find('-'); 160 if (findBus == std::string::npos) 161 { 162 return -1; 163 } 164 return std::stoi(filename.substr(0, findBus)); 165 } 166 167 static bool isMuxBus(size_t bus) 168 { 169 auto ec = std::error_code(); 170 auto isSymlink = 171 is_symlink(std::filesystem::path("/sys/bus/i2c/devices/i2c-" + 172 std::to_string(bus) + "/mux_device"), 173 ec); 174 return (!ec && isSymlink); 175 } 176 177 static void makeProbeInterface(size_t bus, size_t address, 178 sdbusplus::asio::object_server& objServer) 179 { 180 if (isMuxBus(bus)) 181 { 182 return; // the mux buses are random, no need to publish 183 } 184 auto [it, success] = foundDevices.emplace( 185 std::make_pair(bus, address), 186 objServer.add_interface( 187 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" + 188 std::to_string(address), 189 "xyz.openbmc_project.Inventory.Item.I2CDevice")); 190 if (!success) 191 { 192 return; // already added 193 } 194 it->second->register_property("Bus", bus); 195 it->second->register_property("Address", address); 196 it->second->initialize(); 197 } 198 199 static std::optional<bool> isDevice16Bit(int file) 200 { 201 // Set the higher data word address bits to 0. It's safe on 8-bit addressing 202 // EEPROMs because it doesn't write any actual data. 203 int ret = i2c_smbus_write_byte(file, 0); 204 if (ret < 0) 205 { 206 return std::nullopt; 207 } 208 209 /* Get first byte */ 210 int byte1 = i2c_smbus_read_byte_data(file, 0); 211 if (byte1 < 0) 212 { 213 return std::nullopt; 214 } 215 /* Read 7 more bytes, it will read same first byte in case of 216 * 8 bit but it will read next byte in case of 16 bit 217 */ 218 for (int i = 0; i < 7; i++) 219 { 220 int byte2 = i2c_smbus_read_byte_data(file, 0); 221 if (byte2 < 0) 222 { 223 return std::nullopt; 224 } 225 if (byte2 != byte1) 226 { 227 return true; 228 } 229 } 230 return false; 231 } 232 233 // Issue an I2C transaction to first write to_target_buf_len bytes,then read 234 // from_target_buf_len bytes. 235 static int i2cSmbusWriteThenRead(int file, uint16_t address, 236 uint8_t* toTargetBuf, uint8_t toTargetBufLen, 237 uint8_t* fromTargetBuf, 238 uint8_t fromTargetBufLen) 239 { 240 if (toTargetBuf == nullptr || toTargetBufLen == 0 || 241 fromTargetBuf == nullptr || fromTargetBufLen == 0) 242 { 243 return -1; 244 } 245 246 constexpr size_t smbusWriteThenReadMsgCount = 2; 247 std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{}; 248 struct i2c_rdwr_ioctl_data rdwr 249 {}; 250 251 msgs[0].addr = address; 252 msgs[0].flags = 0; 253 msgs[0].len = toTargetBufLen; 254 msgs[0].buf = toTargetBuf; 255 msgs[1].addr = address; 256 msgs[1].flags = I2C_M_RD; 257 msgs[1].len = fromTargetBufLen; 258 msgs[1].buf = fromTargetBuf; 259 260 rdwr.msgs = msgs.data(); 261 rdwr.nmsgs = msgs.size(); 262 263 int ret = ioctl(file, I2C_RDWR, &rdwr); 264 265 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1; 266 } 267 268 static int64_t readData(bool is16bit, bool isBytewise, int file, 269 uint16_t address, off_t offset, size_t len, 270 uint8_t* buf) 271 { 272 if (!is16bit) 273 { 274 if (!isBytewise) 275 { 276 return i2c_smbus_read_i2c_block_data( 277 file, static_cast<uint8_t>(offset), len, buf); 278 } 279 280 std::span<uint8_t> bufspan{buf, len}; 281 for (size_t i = 0; i < len; i++) 282 { 283 int byte = i2c_smbus_read_byte_data( 284 file, static_cast<uint8_t>(offset + i)); 285 if (byte < 0) 286 { 287 return static_cast<int64_t>(byte); 288 } 289 bufspan[i] = static_cast<uint8_t>(byte); 290 } 291 return static_cast<int64_t>(len); 292 } 293 294 offset = htobe16(offset); 295 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 296 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset); 297 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len); 298 } 299 300 // TODO: This code is very similar to the non-eeprom version and can be merged 301 // with some tweaks. 302 static std::vector<uint8_t> processEeprom(int bus, int address) 303 { 304 auto path = getEepromPath(bus, address); 305 306 int file = open(path.c_str(), O_RDONLY); 307 if (file < 0) 308 { 309 std::cerr << "Unable to open eeprom file: " << path << "\n"; 310 return {}; 311 } 312 313 std::string errorMessage = "eeprom at " + std::to_string(bus) + 314 " address " + std::to_string(address); 315 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) { 316 return readFromEeprom(file, offset, length, outbuf); 317 }; 318 FRUReader reader(std::move(readFunc)); 319 std::pair<std::vector<uint8_t>, bool> pair = readFRUContents(reader, 320 errorMessage); 321 322 close(file); 323 return pair.first; 324 } 325 326 std::set<size_t> findI2CEeproms(int i2cBus, 327 const std::shared_ptr<DeviceMap>& devices) 328 { 329 std::set<size_t> foundList; 330 331 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus); 332 333 // For each file listed under the i2c device 334 // NOTE: This should be faster than just checking for each possible address 335 // path. 336 auto ec = std::error_code(); 337 for (const auto& p : fs::directory_iterator(path, ec)) 338 { 339 if (ec) 340 { 341 std::cerr << "directory_iterator err " << ec.message() << "\n"; 342 break; 343 } 344 const std::string node = p.path().string(); 345 std::smatch m; 346 bool found = std::regex_match(node, m, 347 std::regex(".+\\d+-([0-9abcdef]+$)")); 348 349 if (!found) 350 { 351 continue; 352 } 353 if (m.size() != 2) 354 { 355 std::cerr << "regex didn't capture\n"; 356 continue; 357 } 358 359 std::ssub_match subMatch = m[1]; 360 std::string addressString = subMatch.str(); 361 std::string_view addressStringView(addressString); 362 363 size_t address = 0; 364 std::from_chars(addressStringView.begin(), addressStringView.end(), 365 address, 16); 366 367 const std::string eeprom = node + "/eeprom"; 368 369 try 370 { 371 if (!fs::exists(eeprom)) 372 { 373 continue; 374 } 375 } 376 catch (...) 377 { 378 continue; 379 } 380 381 // There is an eeprom file at this address, it may have invalid 382 // contents, but we found it. 383 foundList.insert(address); 384 385 std::vector<uint8_t> device = processEeprom(i2cBus, address); 386 if (!device.empty()) 387 { 388 devices->emplace(address, device); 389 } 390 } 391 392 return foundList; 393 } 394 395 int getBusFRUs(int file, int first, int last, int bus, 396 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn, 397 sdbusplus::asio::object_server& objServer) 398 { 399 std::future<int> future = std::async(std::launch::async, [&]() { 400 // NOTE: When reading the devices raw on the bus, it can interfere with 401 // the driver's ability to operate, therefore read eeproms first before 402 // scanning for devices without drivers. Several experiments were run 403 // and it was determined that if there were any devices on the bus 404 // before the eeprom was hit and read, the eeprom driver wouldn't open 405 // while the bus device was open. An experiment was not performed to see 406 // if this issue was resolved if the i2c bus device was closed, but 407 // hexdumps of the eeprom later were successful. 408 409 // Scan for i2c eeproms loaded on this bus. 410 std::set<size_t> skipList = findI2CEeproms(bus, devices); 411 std::set<size_t>& failedItems = failedAddresses[bus]; 412 std::set<size_t>& foundItems = fruAddresses[bus]; 413 foundItems.clear(); 414 415 auto busFind = busBlocklist.find(bus); 416 if (busFind != busBlocklist.end()) 417 { 418 if (busFind->second != std::nullopt) 419 { 420 for (const auto& address : *(busFind->second)) 421 { 422 skipList.insert(address); 423 } 424 } 425 } 426 427 std::set<size_t>* rootFailures = nullptr; 428 int rootBus = getRootBus(bus); 429 430 if (rootBus >= 0) 431 { 432 auto rootBusFind = busBlocklist.find(rootBus); 433 if (rootBusFind != busBlocklist.end()) 434 { 435 if (rootBusFind->second != std::nullopt) 436 { 437 for (const auto& rootAddress : *(rootBusFind->second)) 438 { 439 skipList.insert(rootAddress); 440 } 441 } 442 } 443 rootFailures = &(failedAddresses[rootBus]); 444 foundItems = fruAddresses[rootBus]; 445 } 446 447 constexpr int startSkipTargetAddr = 0; 448 constexpr int endSkipTargetAddr = 12; 449 450 for (int ii = first; ii <= last; ii++) 451 { 452 if (foundItems.find(ii) != foundItems.end()) 453 { 454 continue; 455 } 456 if (skipList.find(ii) != skipList.end()) 457 { 458 continue; 459 } 460 // skipping since no device is present in this range 461 if (ii >= startSkipTargetAddr && ii <= endSkipTargetAddr) 462 { 463 continue; 464 } 465 // Set target address 466 if (ioctl(file, I2C_SLAVE, ii) < 0) 467 { 468 std::cerr << "device at bus " << bus << " address " << ii 469 << " busy\n"; 470 continue; 471 } 472 // probe 473 if (i2c_smbus_read_byte(file) < 0) 474 { 475 continue; 476 } 477 478 if (debug) 479 { 480 std::cout << "something at bus " << bus << " addr " << ii 481 << "\n"; 482 } 483 484 makeProbeInterface(bus, ii, objServer); 485 486 if (failedItems.find(ii) != failedItems.end()) 487 { 488 // if we failed to read it once, unlikely we can read it later 489 continue; 490 } 491 492 if (rootFailures != nullptr) 493 { 494 if (rootFailures->find(ii) != rootFailures->end()) 495 { 496 continue; 497 } 498 } 499 500 /* Check for Device type if it is 8 bit or 16 bit */ 501 std::optional<bool> is16Bit = isDevice16Bit(file); 502 if (!is16Bit.has_value()) 503 { 504 std::cerr << "failed to read bus " << bus << " address " << ii 505 << "\n"; 506 if (powerIsOn) 507 { 508 failedItems.insert(ii); 509 } 510 continue; 511 } 512 bool is16BitBool{*is16Bit}; 513 514 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length, 515 uint8_t* outbuf) { 516 return readData(is16BitBool, false, file, ii, offset, length, 517 outbuf); 518 }; 519 FRUReader reader(std::move(readFunc)); 520 std::string errorMessage = "bus " + std::to_string(bus) + 521 " address " + std::to_string(ii); 522 std::pair<std::vector<uint8_t>, bool> pair = 523 readFRUContents(reader, errorMessage); 524 const bool foundHeader = pair.second; 525 526 if (!foundHeader && !is16BitBool) 527 { 528 // certain FRU eeproms require bytewise reading. 529 // otherwise garbage is read. e.g. SuperMicro PWS 920P-SQ 530 531 auto readFunc = [is16BitBool, file, ii](off_t offset, 532 size_t length, 533 uint8_t* outbuf) { 534 return readData(is16BitBool, true, file, ii, offset, length, 535 outbuf); 536 }; 537 FRUReader readerBytewise(std::move(readFunc)); 538 pair = readFRUContents(readerBytewise, errorMessage); 539 } 540 541 if (pair.first.empty()) 542 { 543 continue; 544 } 545 546 devices->emplace(ii, pair.first); 547 fruAddresses[bus].insert(ii); 548 } 549 return 1; 550 }); 551 std::future_status status = 552 future.wait_for(std::chrono::seconds(busTimeoutSeconds)); 553 if (status == std::future_status::timeout) 554 { 555 std::cerr << "Error reading bus " << bus << "\n"; 556 if (powerIsOn) 557 { 558 busBlocklist[bus] = std::nullopt; 559 } 560 close(file); 561 return -1; 562 } 563 564 close(file); 565 return future.get(); 566 } 567 568 void loadBlocklist(const char* path) 569 { 570 std::ifstream blocklistStream(path); 571 if (!blocklistStream.good()) 572 { 573 // File is optional. 574 std::cerr << "Cannot open blocklist file.\n\n"; 575 return; 576 } 577 578 nlohmann::json data = nlohmann::json::parse(blocklistStream, nullptr, 579 false); 580 if (data.is_discarded()) 581 { 582 std::cerr << "Illegal blocklist file detected, cannot validate JSON, " 583 "exiting\n"; 584 std::exit(EXIT_FAILURE); 585 } 586 587 // It's expected to have at least one field, "buses" that is an array of the 588 // buses by integer. Allow for future options to exclude further aspects, 589 // such as specific addresses or ranges. 590 if (data.type() != nlohmann::json::value_t::object) 591 { 592 std::cerr << "Illegal blocklist, expected to read dictionary\n"; 593 std::exit(EXIT_FAILURE); 594 } 595 596 // If buses field is missing, that's fine. 597 if (data.count("buses") == 1) 598 { 599 // Parse the buses array after a little validation. 600 auto buses = data.at("buses"); 601 if (buses.type() != nlohmann::json::value_t::array) 602 { 603 // Buses field present but invalid, therefore this is an error. 604 std::cerr << "Invalid contents for blocklist buses field\n"; 605 std::exit(EXIT_FAILURE); 606 } 607 608 // Catch exception here for type mis-match. 609 try 610 { 611 for (const auto& busIterator : buses) 612 { 613 // If bus and addresses field are missing, that's fine. 614 if (busIterator.contains("bus") && 615 busIterator.contains("addresses")) 616 { 617 auto busData = busIterator.at("bus"); 618 auto bus = busData.get<size_t>(); 619 620 auto addressData = busIterator.at("addresses"); 621 auto addresses = 622 addressData.get<std::set<std::string_view>>(); 623 624 busBlocklist[bus].emplace(); 625 for (const auto& address : addresses) 626 { 627 size_t addressInt = 0; 628 std::from_chars(address.begin() + 2, address.end(), 629 addressInt, 16); 630 busBlocklist[bus]->insert(addressInt); 631 } 632 } 633 else 634 { 635 busBlocklist[busIterator.get<size_t>()] = std::nullopt; 636 } 637 } 638 } 639 catch (const nlohmann::detail::type_error& e) 640 { 641 // Type mis-match is a critical error. 642 std::cerr << "Invalid bus type: " << e.what() << "\n"; 643 std::exit(EXIT_FAILURE); 644 } 645 } 646 } 647 648 static void findI2CDevices(const std::vector<fs::path>& i2cBuses, 649 BusMap& busmap, const bool& powerIsOn, 650 sdbusplus::asio::object_server& objServer) 651 { 652 for (const auto& i2cBus : i2cBuses) 653 { 654 int bus = busStrToInt(i2cBus.string()); 655 656 if (bus < 0) 657 { 658 std::cerr << "Cannot translate " << i2cBus << " to int\n"; 659 continue; 660 } 661 auto busFind = busBlocklist.find(bus); 662 if (busFind != busBlocklist.end()) 663 { 664 if (busFind->second == std::nullopt) 665 { 666 continue; // Skip blocked busses. 667 } 668 } 669 int rootBus = getRootBus(bus); 670 auto rootBusFind = busBlocklist.find(rootBus); 671 if (rootBusFind != busBlocklist.end()) 672 { 673 if (rootBusFind->second == std::nullopt) 674 { 675 continue; 676 } 677 } 678 679 auto file = open(i2cBus.c_str(), O_RDWR); 680 if (file < 0) 681 { 682 std::cerr << "unable to open i2c device " << i2cBus.string() 683 << "\n"; 684 continue; 685 } 686 unsigned long funcs = 0; 687 688 if (ioctl(file, I2C_FUNCS, &funcs) < 0) 689 { 690 std::cerr 691 << "Error: Could not get the adapter functionality matrix bus " 692 << bus << "\n"; 693 close(file); 694 continue; 695 } 696 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) || 697 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0)) 698 { 699 std::cerr << "Error: Can't use SMBus Receive Byte command bus " 700 << bus << "\n"; 701 continue; 702 } 703 auto& device = busmap[bus]; 704 device = std::make_shared<DeviceMap>(); 705 706 // i2cdetect by default uses the range 0x03 to 0x77, as 707 // this is what we have tested with, use this range. Could be 708 // changed in future. 709 if (debug) 710 { 711 std::cerr << "Scanning bus " << bus << "\n"; 712 } 713 714 // fd is closed in this function in case the bus locks up 715 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer); 716 717 if (debug) 718 { 719 std::cerr << "Done scanning bus " << bus << "\n"; 720 } 721 } 722 } 723 724 // this class allows an async response after all i2c devices are discovered 725 struct FindDevicesWithCallback : 726 std::enable_shared_from_this<FindDevicesWithCallback> 727 { 728 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses, 729 BusMap& busmap, const bool& powerIsOn, 730 sdbusplus::asio::object_server& objServer, 731 std::function<void(void)>&& callback) : 732 _i2cBuses(i2cBuses), 733 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer), 734 _callback(std::move(callback)) 735 {} 736 ~FindDevicesWithCallback() 737 { 738 _callback(); 739 } 740 void run() 741 { 742 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer); 743 } 744 745 const std::vector<fs::path>& _i2cBuses; 746 BusMap& _busMap; 747 const bool& _powerIsOn; 748 sdbusplus::asio::object_server& _objServer; 749 std::function<void(void)> _callback; 750 }; 751 752 void addFruObjectToDbus( 753 std::vector<uint8_t>& device, 754 boost::container::flat_map< 755 std::pair<size_t, size_t>, 756 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, 757 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount, 758 const bool& powerIsOn, sdbusplus::asio::object_server& objServer, 759 std::shared_ptr<sdbusplus::asio::connection>& systemBus) 760 { 761 boost::container::flat_map<std::string, std::string> formattedFRU; 762 763 std::optional<std::string> optionalProductName = getProductName( 764 device, formattedFRU, bus, address, unknownBusObjectCount); 765 if (!optionalProductName) 766 { 767 std::cerr << "getProductName failed. product name is empty.\n"; 768 return; 769 } 770 771 std::string productName = "/xyz/openbmc_project/FruDevice/" + 772 optionalProductName.value(); 773 774 std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName); 775 if (index.has_value()) 776 { 777 productName += "_"; 778 productName += std::to_string(++(*index)); 779 } 780 781 std::shared_ptr<sdbusplus::asio::dbus_interface> iface = 782 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice"); 783 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface; 784 785 for (auto& property : formattedFRU) 786 { 787 std::regex_replace(property.second.begin(), property.second.begin(), 788 property.second.end(), nonAsciiRegex, "_"); 789 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG") 790 { 791 continue; 792 } 793 std::string key = std::regex_replace(property.first, nonAsciiRegex, 794 "_"); 795 796 if (property.first == "PRODUCT_ASSET_TAG") 797 { 798 std::string propertyName = property.first; 799 iface->register_property( 800 key, property.second + '\0', 801 [bus, address, propertyName, &dbusInterfaceMap, 802 &unknownBusObjectCount, &powerIsOn, &objServer, 803 &systemBus](const std::string& req, std::string& resp) { 804 if (strcmp(req.c_str(), resp.c_str()) != 0) 805 { 806 // call the method which will update 807 if (updateFRUProperty(req, bus, address, propertyName, 808 dbusInterfaceMap, 809 unknownBusObjectCount, powerIsOn, 810 objServer, systemBus)) 811 { 812 resp = req; 813 } 814 else 815 { 816 throw std::invalid_argument( 817 "FRU property update failed."); 818 } 819 } 820 return 1; 821 }); 822 } 823 else if (!iface->register_property(key, property.second + '\0')) 824 { 825 std::cerr << "illegal key: " << key << "\n"; 826 } 827 if (debug) 828 { 829 std::cout << property.first << ": " << property.second << "\n"; 830 } 831 } 832 833 // baseboard will be 0, 0 834 iface->register_property("BUS", bus); 835 iface->register_property("ADDRESS", address); 836 837 iface->initialize(); 838 } 839 840 static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU) 841 { 842 // try to read baseboard fru from file 843 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary); 844 if (baseboardFRUFile.good()) 845 { 846 baseboardFRUFile.seekg(0, std::ios_base::end); 847 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg()); 848 baseboardFRU.resize(fileSize); 849 baseboardFRUFile.seekg(0, std::ios_base::beg); 850 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 851 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data()); 852 baseboardFRUFile.read(charOffset, fileSize); 853 } 854 else 855 { 856 return false; 857 } 858 return true; 859 } 860 861 bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru) 862 { 863 boost::container::flat_map<std::string, std::string> tmp; 864 if (fru.size() > maxFruSize) 865 { 866 std::cerr << "Invalid fru.size() during writeFRU\n"; 867 return false; 868 } 869 // verify legal fru by running it through fru parsing logic 870 if (formatIPMIFRU(fru, tmp) != resCodes::resOK) 871 { 872 std::cerr << "Invalid fru format during writeFRU\n"; 873 return false; 874 } 875 // baseboard fru 876 if (bus == 0 && address == 0) 877 { 878 std::ofstream file(baseboardFruLocation, std::ios_base::binary); 879 if (!file.good()) 880 { 881 std::cerr << "Error opening file " << baseboardFruLocation << "\n"; 882 throw DBusInternalError(); 883 return false; 884 } 885 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 886 const char* charOffset = reinterpret_cast<const char*>(fru.data()); 887 file.write(charOffset, fru.size()); 888 return file.good(); 889 } 890 891 if (hasEepromFile(bus, address)) 892 { 893 auto path = getEepromPath(bus, address); 894 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC); 895 if (eeprom < 0) 896 { 897 std::cerr << "unable to open i2c device " << path << "\n"; 898 throw DBusInternalError(); 899 return false; 900 } 901 902 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size()); 903 if (writtenBytes < 0) 904 { 905 std::cerr << "unable to write to i2c device " << path << "\n"; 906 close(eeprom); 907 throw DBusInternalError(); 908 return false; 909 } 910 911 close(eeprom); 912 return true; 913 } 914 915 std::string i2cBus = "/dev/i2c-" + std::to_string(bus); 916 917 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); 918 if (file < 0) 919 { 920 std::cerr << "unable to open i2c device " << i2cBus << "\n"; 921 throw DBusInternalError(); 922 return false; 923 } 924 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) 925 { 926 std::cerr << "unable to set device address\n"; 927 close(file); 928 throw DBusInternalError(); 929 return false; 930 } 931 932 constexpr const size_t retryMax = 2; 933 uint16_t index = 0; 934 size_t retries = retryMax; 935 while (index < fru.size()) 936 { 937 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) && 938 (retries == retryMax)) 939 { 940 // The 4K EEPROM only uses the A2 and A1 device address bits 941 // with the third bit being a memory page address bit. 942 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0) 943 { 944 std::cerr << "unable to set device address\n"; 945 close(file); 946 throw DBusInternalError(); 947 return false; 948 } 949 } 950 951 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index), 952 fru[index]) < 0) 953 { 954 if ((retries--) == 0U) 955 { 956 std::cerr << "error writing fru: " << strerror(errno) << "\n"; 957 close(file); 958 throw DBusInternalError(); 959 return false; 960 } 961 } 962 else 963 { 964 retries = retryMax; 965 index++; 966 } 967 // most eeproms require 5-10ms between writes 968 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 969 } 970 close(file); 971 return true; 972 } 973 974 void rescanOneBus( 975 BusMap& busmap, uint16_t busNum, 976 boost::container::flat_map< 977 std::pair<size_t, size_t>, 978 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, 979 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn, 980 sdbusplus::asio::object_server& objServer, 981 std::shared_ptr<sdbusplus::asio::connection>& systemBus) 982 { 983 for (auto device = foundDevices.begin(); device != foundDevices.end();) 984 { 985 if (device->first.first == static_cast<size_t>(busNum)) 986 { 987 objServer.remove_interface(device->second); 988 device = foundDevices.erase(device); 989 } 990 else 991 { 992 device++; 993 } 994 } 995 996 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum)); 997 if (!fs::exists(busPath)) 998 { 999 if (dbusCall) 1000 { 1001 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum) 1002 << "\n"; 1003 throw std::invalid_argument("Invalid Bus."); 1004 } 1005 return; 1006 } 1007 1008 std::vector<fs::path> i2cBuses; 1009 i2cBuses.emplace_back(busPath); 1010 1011 auto scan = std::make_shared<FindDevicesWithCallback>( 1012 i2cBuses, busmap, powerIsOn, objServer, 1013 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn, 1014 &objServer, &systemBus]() { 1015 for (auto busIface = dbusInterfaceMap.begin(); 1016 busIface != dbusInterfaceMap.end();) 1017 { 1018 if (busIface->first.first == static_cast<size_t>(busNum)) 1019 { 1020 objServer.remove_interface(busIface->second); 1021 busIface = dbusInterfaceMap.erase(busIface); 1022 } 1023 else 1024 { 1025 busIface++; 1026 } 1027 } 1028 auto found = busmap.find(busNum); 1029 if (found == busmap.end() || found->second == nullptr) 1030 { 1031 return; 1032 } 1033 for (auto& device : *(found->second)) 1034 { 1035 addFruObjectToDbus(device.second, dbusInterfaceMap, 1036 static_cast<uint32_t>(busNum), device.first, 1037 unknownBusObjectCount, powerIsOn, objServer, 1038 systemBus); 1039 } 1040 }); 1041 scan->run(); 1042 } 1043 1044 void rescanBusses( 1045 BusMap& busmap, 1046 boost::container::flat_map< 1047 std::pair<size_t, size_t>, 1048 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, 1049 size_t& unknownBusObjectCount, const bool& powerIsOn, 1050 sdbusplus::asio::object_server& objServer, 1051 std::shared_ptr<sdbusplus::asio::connection>& systemBus) 1052 { 1053 static boost::asio::steady_timer timer(io); 1054 timer.expires_from_now(std::chrono::seconds(1)); 1055 1056 // setup an async wait in case we get flooded with requests 1057 timer.async_wait([&](const boost::system::error_code& ec) { 1058 if (ec == boost::asio::error::operation_aborted) 1059 { 1060 return; 1061 } 1062 1063 if (ec) 1064 { 1065 std::cerr << "Error in timer: " << ec.message() << "\n"; 1066 return; 1067 } 1068 1069 auto devDir = fs::path("/dev/"); 1070 std::vector<fs::path> i2cBuses; 1071 1072 boost::container::flat_map<size_t, fs::path> busPaths; 1073 if (!getI2cDevicePaths(devDir, busPaths)) 1074 { 1075 std::cerr << "unable to find i2c devices\n"; 1076 return; 1077 } 1078 1079 for (const auto& busPath : busPaths) 1080 { 1081 i2cBuses.emplace_back(busPath.second); 1082 } 1083 1084 busmap.clear(); 1085 for (auto& [pair, interface] : foundDevices) 1086 { 1087 objServer.remove_interface(interface); 1088 } 1089 foundDevices.clear(); 1090 1091 auto scan = std::make_shared<FindDevicesWithCallback>( 1092 i2cBuses, busmap, powerIsOn, objServer, [&]() { 1093 for (auto& busIface : dbusInterfaceMap) 1094 { 1095 objServer.remove_interface(busIface.second); 1096 } 1097 1098 dbusInterfaceMap.clear(); 1099 unknownBusObjectCount = 0; 1100 1101 // todo, get this from a more sensable place 1102 std::vector<uint8_t> baseboardFRU; 1103 if (readBaseboardFRU(baseboardFRU)) 1104 { 1105 // If no device on i2c bus 0, the insertion will happen. 1106 auto bus0 = busmap.try_emplace(0, 1107 std::make_shared<DeviceMap>()); 1108 bus0.first->second->emplace(0, baseboardFRU); 1109 } 1110 for (auto& devicemap : busmap) 1111 { 1112 for (auto& device : *devicemap.second) 1113 { 1114 addFruObjectToDbus(device.second, dbusInterfaceMap, 1115 devicemap.first, device.first, 1116 unknownBusObjectCount, powerIsOn, 1117 objServer, systemBus); 1118 } 1119 } 1120 }); 1121 scan->run(); 1122 }); 1123 } 1124 1125 // Details with example of Asset Tag Update 1126 // To find location of Product Info Area asset tag as per FRU specification 1127 // 1. Find product Info area starting offset (*8 - as header will be in 1128 // multiple of 8 bytes). 1129 // 2. Skip 3 bytes of product info area (like format version, area length, 1130 // and language code). 1131 // 3. Traverse manufacturer name, product name, product version, & product 1132 // serial number, by reading type/length code to reach the Asset Tag. 1133 // 4. Update the Asset Tag, reposition the product Info area in multiple of 1134 // 8 bytes. Update the Product area length and checksum. 1135 1136 bool updateFRUProperty( 1137 const std::string& updatePropertyReq, uint32_t bus, uint32_t address, 1138 const std::string& propertyName, 1139 boost::container::flat_map< 1140 std::pair<size_t, size_t>, 1141 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, 1142 size_t& unknownBusObjectCount, const bool& powerIsOn, 1143 sdbusplus::asio::object_server& objServer, 1144 std::shared_ptr<sdbusplus::asio::connection>& systemBus) 1145 { 1146 size_t updatePropertyReqLen = updatePropertyReq.length(); 1147 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63) 1148 { 1149 std::cerr 1150 << "FRU field data cannot be of 1 char or more than 63 chars. " 1151 "Invalid Length " 1152 << updatePropertyReqLen << "\n"; 1153 return false; 1154 } 1155 1156 std::vector<uint8_t> fruData; 1157 1158 if (!getFruData(fruData, bus, address)) 1159 { 1160 std::cerr << "Failure getting FRU Data \n"; 1161 return false; 1162 } 1163 1164 struct FruArea fruAreaParams 1165 {}; 1166 1167 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams)) 1168 { 1169 std::cerr << "findFruAreaLocationAndField failed \n"; 1170 return false; 1171 } 1172 1173 std::vector<uint8_t> restFRUAreaFieldsData; 1174 if (!copyRestFRUArea(fruData, propertyName, fruAreaParams, 1175 restFRUAreaFieldsData)) 1176 { 1177 std::cerr << "copyRestFRUArea failed \n"; 1178 return false; 1179 } 1180 1181 // Push post update fru areas if any 1182 unsigned int nextFRUAreaLoc = 0; 1183 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal; 1184 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea) 1185 { 1186 unsigned int fruAreaLoc = 1187 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize; 1188 if ((fruAreaLoc > fruAreaParams.restFieldsEnd) && 1189 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc))) 1190 { 1191 nextFRUAreaLoc = fruAreaLoc; 1192 } 1193 } 1194 std::vector<uint8_t> restFRUAreasData; 1195 if (nextFRUAreaLoc != 0U) 1196 { 1197 std::copy_n(fruData.begin() + nextFRUAreaLoc, 1198 fruData.size() - nextFRUAreaLoc, 1199 std::back_inserter(restFRUAreasData)); 1200 } 1201 1202 // check FRU area size 1203 size_t fruAreaDataSize = 1204 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) + 1205 restFRUAreaFieldsData.size()); 1206 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize; 1207 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize) 1208 { 1209 #ifdef ENABLE_FRU_AREA_RESIZE 1210 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1; 1211 // round size to 8-byte blocks 1212 newFRUAreaSize = ((newFRUAreaSize - 1) / fruBlockSize + 1) * 1213 fruBlockSize; 1214 size_t newFRUDataSize = fruData.size() + newFRUAreaSize - 1215 fruAreaParams.size; 1216 fruData.resize(newFRUDataSize); 1217 fruAreaParams.size = newFRUAreaSize; 1218 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size; 1219 #else 1220 std::cerr << "FRU field length: " << updatePropertyReqLen + 1 1221 << " should not be greater than available FRU area size: " 1222 << fruAreaAvailableSize << "\n"; 1223 return false; 1224 #endif // ENABLE_FRU_AREA_RESIZE 1225 } 1226 1227 // write new requested property field length and data 1228 constexpr uint8_t newTypeLenMask = 0xC0; 1229 fruData[fruAreaParams.updateFieldLoc] = 1230 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask); 1231 fruAreaParams.updateFieldLoc++; 1232 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(), 1233 fruData.begin() + fruAreaParams.updateFieldLoc); 1234 1235 // Copy remaining data to main fru area - post updated fru field vector 1236 fruAreaParams.restFieldsLoc = fruAreaParams.updateFieldLoc + 1237 updatePropertyReqLen; 1238 size_t fruAreaDataEnd = fruAreaParams.restFieldsLoc + 1239 restFRUAreaFieldsData.size(); 1240 1241 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(), 1242 fruData.begin() + fruAreaParams.restFieldsLoc); 1243 1244 // Update final fru with new fru area length and checksum 1245 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum( 1246 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end); 1247 1248 #ifdef ENABLE_FRU_AREA_RESIZE 1249 ++nextFRUAreaNewLoc; 1250 ssize_t nextFRUAreaOffsetDiff = (nextFRUAreaNewLoc - nextFRUAreaLoc) / 1251 fruBlockSize; 1252 // Append rest FRU Areas if size changed and there were other sections after 1253 // updated one 1254 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc) 1255 { 1256 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(), 1257 fruData.begin() + nextFRUAreaNewLoc); 1258 // Update Common Header 1259 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal; 1260 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea) 1261 { 1262 unsigned int fruAreaOffsetField = 1263 getHeaderAreaFieldOffset(nextFRUArea); 1264 size_t curFRUAreaOffset = fruData[fruAreaOffsetField]; 1265 if (curFRUAreaOffset > fruAreaParams.end) 1266 { 1267 fruData[fruAreaOffsetField] = static_cast<int8_t>( 1268 curFRUAreaOffset + nextFRUAreaOffsetDiff); 1269 } 1270 } 1271 // Calculate new checksum 1272 std::vector<uint8_t> headerFRUData; 1273 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData)); 1274 size_t checksumVal = calculateChecksum(headerFRUData); 1275 fruData[7] = static_cast<uint8_t>(checksumVal); 1276 // fill zeros if FRU Area size decreased 1277 if (nextFRUAreaOffsetDiff < 0) 1278 { 1279 std::fill(fruData.begin() + nextFRUAreaNewLoc + 1280 restFRUAreasData.size(), 1281 fruData.end(), 0); 1282 } 1283 } 1284 #else 1285 // this is to avoid "unused variable" warning 1286 (void)nextFRUAreaNewLoc; 1287 #endif // ENABLE_FRU_AREA_RESIZE 1288 if (fruData.empty()) 1289 { 1290 return false; 1291 } 1292 1293 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address), 1294 fruData)) 1295 { 1296 return false; 1297 } 1298 1299 // Rescan the bus so that GetRawFru dbus-call fetches updated values 1300 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn, 1301 objServer, systemBus); 1302 return true; 1303 } 1304 1305 int main() 1306 { 1307 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); 1308 sdbusplus::asio::object_server objServer(systemBus); 1309 1310 static size_t unknownBusObjectCount = 0; 1311 static bool powerIsOn = false; 1312 auto devDir = fs::path("/dev/"); 1313 auto matchString = std::string(R"(i2c-\d+$)"); 1314 std::vector<fs::path> i2cBuses; 1315 1316 if (!findFiles(devDir, matchString, i2cBuses)) 1317 { 1318 std::cerr << "unable to find i2c devices\n"; 1319 return 1; 1320 } 1321 1322 // check for and load blocklist with initial buses. 1323 loadBlocklist(blocklistPath); 1324 1325 systemBus->request_name("xyz.openbmc_project.FruDevice"); 1326 1327 // this is a map with keys of pair(bus number, address) and values of 1328 // the object on dbus 1329 boost::container::flat_map<std::pair<size_t, size_t>, 1330 std::shared_ptr<sdbusplus::asio::dbus_interface>> 1331 dbusInterfaceMap; 1332 1333 std::shared_ptr<sdbusplus::asio::dbus_interface> iface = 1334 objServer.add_interface("/xyz/openbmc_project/FruDevice", 1335 "xyz.openbmc_project.FruDeviceManager"); 1336 1337 iface->register_method("ReScan", [&]() { 1338 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn, 1339 objServer, systemBus); 1340 }); 1341 1342 iface->register_method("ReScanBus", [&](uint16_t bus) { 1343 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount, 1344 powerIsOn, objServer, systemBus); 1345 }); 1346 1347 iface->register_method("GetRawFru", getFRUInfo); 1348 1349 iface->register_method("WriteFru", 1350 [&](const uint16_t bus, const uint8_t address, 1351 const std::vector<uint8_t>& data) { 1352 if (!writeFRU(bus, address, data)) 1353 { 1354 throw std::invalid_argument("Invalid Arguments."); 1355 return; 1356 } 1357 // schedule rescan on success 1358 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn, 1359 objServer, systemBus); 1360 }); 1361 iface->initialize(); 1362 1363 std::function<void(sdbusplus::message_t & message)> eventHandler = 1364 [&](sdbusplus::message_t& message) { 1365 std::string objectName; 1366 boost::container::flat_map< 1367 std::string, 1368 std::variant<std::string, bool, int64_t, uint64_t, double>> 1369 values; 1370 message.read(objectName, values); 1371 auto findState = values.find("CurrentHostState"); 1372 if (findState != values.end()) 1373 { 1374 if (std::get<std::string>(findState->second) == 1375 "xyz.openbmc_project.State.Host.HostState.Running") 1376 { 1377 powerIsOn = true; 1378 } 1379 } 1380 1381 if (powerIsOn) 1382 { 1383 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, 1384 powerIsOn, objServer, systemBus); 1385 } 1386 }; 1387 1388 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t( 1389 static_cast<sdbusplus::bus_t&>(*systemBus), 1390 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/" 1391 "openbmc_project/state/" 1392 "host0',arg0='xyz.openbmc_project.State.Host'", 1393 eventHandler); 1394 1395 int fd = inotify_init(); 1396 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE); 1397 std::array<char, 4096> readBuffer{}; 1398 // monitor for new i2c devices 1399 boost::asio::posix::stream_descriptor dirWatch(io, fd); 1400 std::function<void(const boost::system::error_code, std::size_t)> 1401 watchI2cBusses = [&](const boost::system::error_code& ec, 1402 std::size_t bytesTransferred) { 1403 if (ec) 1404 { 1405 std::cout << "Callback Error " << ec << "\n"; 1406 return; 1407 } 1408 size_t index = 0; 1409 while ((index + sizeof(inotify_event)) <= bytesTransferred) 1410 { 1411 const char* p = &readBuffer[index]; 1412 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) 1413 const auto* iEvent = reinterpret_cast<const inotify_event*>(p); 1414 switch (iEvent->mask) 1415 { 1416 case IN_CREATE: 1417 case IN_MOVED_TO: 1418 case IN_DELETE: 1419 std::string_view name(&iEvent->name[0], iEvent->len); 1420 if (boost::starts_with(name, "i2c")) 1421 { 1422 int bus = busStrToInt(name); 1423 if (bus < 0) 1424 { 1425 std::cerr << "Could not parse bus " << name << "\n"; 1426 continue; 1427 } 1428 int rootBus = getRootBus(bus); 1429 if (rootBus >= 0) 1430 { 1431 rescanOneBus(busMap, static_cast<uint16_t>(rootBus), 1432 dbusInterfaceMap, false, 1433 unknownBusObjectCount, powerIsOn, 1434 objServer, systemBus); 1435 } 1436 rescanOneBus(busMap, static_cast<uint16_t>(bus), 1437 dbusInterfaceMap, false, 1438 unknownBusObjectCount, powerIsOn, 1439 objServer, systemBus); 1440 } 1441 } 1442 index += sizeof(inotify_event) + iEvent->len; 1443 } 1444 1445 dirWatch.async_read_some(boost::asio::buffer(readBuffer), 1446 watchI2cBusses); 1447 }; 1448 1449 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses); 1450 // run the initial scan 1451 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn, 1452 objServer, systemBus); 1453 1454 io.run(); 1455 return 0; 1456 } 1457