blob: 22ad48b4c2bdc072f72bab7dec5691ef5f0e6b89 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
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
Patrick Venturea49dc332019-10-26 08:32:02 -070017#include "Utils.hpp"
18
James Feist3b860982018-10-02 14:34:07 -070019#include <errno.h>
James Feist3cb5fec2018-01-23 14:41:51 -080020#include <fcntl.h>
James Feist3b860982018-10-02 14:34:07 -070021#include <sys/inotify.h>
22#include <sys/ioctl.h>
23
James Feist3b860982018-10-02 14:34:07 -070024#include <boost/algorithm/string/predicate.hpp>
25#include <boost/container/flat_map.hpp>
James Feist8c505da2020-05-28 10:06:33 -070026#include <nlohmann/json.hpp>
27#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29
30#include <array>
James Feist3b860982018-10-02 14:34:07 -070031#include <chrono>
32#include <ctime>
Patrick Venturee3754002019-08-06 09:39:12 -070033#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080034#include <fstream>
Patrick Venturebaba7672019-10-26 09:26:41 -070035#include <functional>
James Feist3cb5fec2018-01-23 14:41:51 -080036#include <future>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070037#include <iomanip>
James Feist3cb5fec2018-01-23 14:41:51 -080038#include <iostream>
Patrick Venturee26395d2019-10-29 14:05:16 -070039#include <limits>
James Feist3f8a2782018-02-12 09:24:42 -080040#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070041#include <set>
42#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070043#include <string>
James Feist3b860982018-10-02 14:34:07 -070044#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080045#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080046#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070047#include <vector>
James Feist3b860982018-10-02 14:34:07 -070048
James Feist8c505da2020-05-28 10:06:33 -070049extern "C"
50{
James Feist3b860982018-10-02 14:34:07 -070051#include <i2c/smbus.h>
52#include <linux/i2c-dev.h>
53}
James Feist3cb5fec2018-01-23 14:41:51 -080054
Ed Tanous072e25d2018-12-16 21:45:20 -080055namespace fs = std::filesystem;
James Feist3cb5fec2018-01-23 14:41:51 -080056static constexpr bool DEBUG = false;
57static size_t UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feistb49ffc32018-05-02 11:10:43 -070058constexpr size_t MAX_FRU_SIZE = 512;
59constexpr size_t MAX_EEPROM_PAGE_INDEX = 255;
James Feist26c27ad2018-07-25 15:09:40 -070060constexpr size_t busTimeoutSeconds = 5;
James Feist3cb5fec2018-01-23 14:41:51 -080061
Patrick Venture11f1ff42019-08-01 10:42:12 -070062constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
63
James Feista465ccc2019-02-08 12:51:01 -080064const static constexpr char* BASEBOARD_FRU_LOCATION =
James Feist3cb5fec2018-01-23 14:41:51 -080065 "/etc/fru/baseboard.fru.bin";
66
James Feista465ccc2019-02-08 12:51:01 -080067const static constexpr char* I2C_DEV_LOCATION = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080068
James Feista465ccc2019-02-08 12:51:01 -080069static constexpr std::array<const char*, 5> FRU_AREAS = {
James Feist3cb5fec2018-01-23 14:41:51 -080070 "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"};
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -070071const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]");
James Feist3cb5fec2018-01-23 14:41:51 -080072using DeviceMap = boost::container::flat_map<int, std::vector<char>>;
73using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
74
James Feist444830e2019-04-05 08:38:16 -070075static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070076struct FindDevicesWithCallback;
77
Nikhil Potaded8884f12019-03-27 13:27:13 -070078static BusMap busMap;
79
James Feistcb5661d2020-06-12 15:05:01 -070080static bool powerIsOn = false;
81
James Feist8a983922019-10-24 17:11:56 -070082static boost::container::flat_map<
83 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
84 foundDevices;
85
James Feist7972bb92019-11-13 15:59:24 -080086static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
87
James Feist5cb06082019-10-24 17:11:13 -070088boost::asio::io_service io;
89auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
90auto objServer = sdbusplus::asio::object_server(systemBus);
91
Patrick Venturebaba7672019-10-26 09:26:41 -070092bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData);
93
Xiang Liu2801a702020-01-20 14:29:34 -080094using ReadBlockFunc =
95 std::function<int64_t(int flag, int file, uint16_t address, uint16_t offset,
96 uint8_t length, uint8_t* outBuf)>;
Patrick Venturebaba7672019-10-26 09:26:41 -070097
98// Read and validate FRU contents, given the flag required for raw i2c, the open
99// file handle, a read method, and a helper string for failures.
Xiang Liu2801a702020-01-20 14:29:34 -0800100std::vector<char> readFruContents(int flag, int file, uint16_t address,
101 ReadBlockFunc readBlock,
Patrick Venturebaba7672019-10-26 09:26:41 -0700102 const std::string& errorHelp)
103{
104 std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
105
Xiang Liu2801a702020-01-20 14:29:34 -0800106 if (readBlock(flag, file, address, 0x0, 0x8, blockData.data()) < 0)
Patrick Venturebaba7672019-10-26 09:26:41 -0700107 {
108 std::cerr << "failed to read " << errorHelp << "\n";
109 return {};
110 }
111
112 // check the header checksum
113 if (!validateHeader(blockData))
114 {
115 if (DEBUG)
116 {
117 std::cerr << "Illegal header " << errorHelp << "\n";
118 }
119
120 return {};
121 }
122
123 std::vector<char> device;
124 device.insert(device.end(), blockData.begin(), blockData.begin() + 8);
125
Patrick Venturee26395d2019-10-29 14:05:16 -0700126 bool hasMultiRecords = false;
Patrick Venturebaba7672019-10-26 09:26:41 -0700127 int fruLength = 0;
128 for (size_t jj = 1; jj <= FRU_AREAS.size(); jj++)
129 {
Patrick Venturef2ad76b2019-10-30 08:49:27 -0700130 // Offset value can be 255.
131 int areaOffset = static_cast<uint8_t>(device[jj]);
Patrick Venturebaba7672019-10-26 09:26:41 -0700132 if (areaOffset == 0)
133 {
134 continue;
135 }
136
Patrick Venturee26395d2019-10-29 14:05:16 -0700137 // MultiRecords are different. jj is not tracking section, it's walking
138 // the common header.
139 if (std::string(FRU_AREAS[jj - 1]) == std::string("MULTIRECORD"))
140 {
141 hasMultiRecords = true;
142 break;
143 }
144
Patrick Venturebaba7672019-10-26 09:26:41 -0700145 areaOffset *= 8;
146
Xiang Liu2801a702020-01-20 14:29:34 -0800147 if (readBlock(flag, file, address, static_cast<uint16_t>(areaOffset),
148 0x2, blockData.data()) < 0)
Patrick Venturebaba7672019-10-26 09:26:41 -0700149 {
150 std::cerr << "failed to read " << errorHelp << "\n";
151 return {};
152 }
153
Patrick Venturef2ad76b2019-10-30 08:49:27 -0700154 // Ignore data type (blockData is already unsigned).
Patrick Venturebaba7672019-10-26 09:26:41 -0700155 int length = blockData[1] * 8;
156 areaOffset += length;
157 fruLength = (areaOffset > fruLength) ? areaOffset : fruLength;
158 }
159
Patrick Venturee26395d2019-10-29 14:05:16 -0700160 if (hasMultiRecords)
161 {
162 // device[area count] is the index to the last area because the 0th
163 // entry is not an offset in the common header.
Patrick Venturef2ad76b2019-10-30 08:49:27 -0700164 int areaOffset = static_cast<uint8_t>(device[FRU_AREAS.size()]);
Patrick Venturee26395d2019-10-29 14:05:16 -0700165 areaOffset *= 8;
166
167 // the multi-area record header is 5 bytes long.
168 constexpr int multiRecordHeaderSize = 5;
169 constexpr int multiRecordEndOfList = 0x80;
170
171 // Sanity hard-limit to 64KB.
172 while (areaOffset < std::numeric_limits<uint16_t>::max())
173 {
174 // In multi-area, the area offset points to the 0th record, each
175 // record has 3 bytes of the header we care about.
Xiang Liu2801a702020-01-20 14:29:34 -0800176 if (readBlock(flag, file, address,
177 static_cast<uint16_t>(areaOffset), 0x3,
Patrick Venturee26395d2019-10-29 14:05:16 -0700178 blockData.data()) < 0)
179 {
180 std::cerr << "failed to read " << errorHelp << "\n";
181 return {};
182 }
183
184 // Ok, let's check the record length, which is in bytes (unsigned,
185 // up to 255, so blockData should hold uint8_t not char)
186 int recordLength = blockData[2];
187 areaOffset += (recordLength + multiRecordHeaderSize);
188 fruLength = (areaOffset > fruLength) ? areaOffset : fruLength;
189
190 // If this is the end of the list bail.
191 if ((blockData[1] & multiRecordEndOfList))
192 {
193 break;
194 }
195 }
196 }
197
Patrick Venturebaba7672019-10-26 09:26:41 -0700198 // You already copied these first 8 bytes (the ipmi fru header size)
199 fruLength -= 8;
200
201 int readOffset = 8;
202
203 while (fruLength > 0)
204 {
205 int requestLength = std::min(I2C_SMBUS_BLOCK_MAX, fruLength);
206
Xiang Liu2801a702020-01-20 14:29:34 -0800207 if (readBlock(flag, file, address, static_cast<uint16_t>(readOffset),
Patrick Venturebaba7672019-10-26 09:26:41 -0700208 static_cast<uint8_t>(requestLength),
209 blockData.data()) < 0)
210 {
211 std::cerr << "failed to read " << errorHelp << "\n";
212 return {};
213 }
214
215 device.insert(device.end(), blockData.begin(),
216 blockData.begin() + requestLength);
217
218 readOffset += requestLength;
219 fruLength -= requestLength;
220 }
221
222 return device;
223}
224
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700225// Given a bus/address, produce the path in sysfs for an eeprom.
226static std::string getEepromPath(size_t bus, size_t address)
227{
228 std::stringstream output;
229 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
230 << std::setfill('0') << std::setw(4) << std::hex << address
231 << "/eeprom";
232 return output.str();
233}
234
235static bool hasEepromFile(size_t bus, size_t address)
236{
237 auto path = getEepromPath(bus, address);
238 try
239 {
240 return fs::exists(path);
241 }
242 catch (...)
243 {
244 return false;
245 }
246}
247
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700248static int64_t readFromEeprom(int flag __attribute__((unused)), int fd,
Xiang Liu2801a702020-01-20 14:29:34 -0800249 uint16_t address __attribute__((unused)),
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700250 uint16_t offset, uint8_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700251{
252 auto result = lseek(fd, offset, SEEK_SET);
253 if (result < 0)
254 {
255 std::cerr << "failed to seek\n";
256 return -1;
257 }
258
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700259 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700260}
261
James Feist7972bb92019-11-13 15:59:24 -0800262static int getRootBus(size_t bus)
263{
264 auto ec = std::error_code();
265 auto path = std::filesystem::read_symlink(
266 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
267 std::to_string(bus) + "/mux_device"),
268 ec);
269 if (ec)
270 {
271 return -1;
272 }
273
274 std::string filename = path.filename();
275 auto findBus = filename.find("-");
276 if (findBus == std::string::npos)
277 {
278 return -1;
279 }
280 return std::stoi(filename.substr(0, findBus));
281}
282
James Feistc95cb142018-02-26 10:41:42 -0800283static bool isMuxBus(size_t bus)
284{
Ed Tanous072e25d2018-12-16 21:45:20 -0800285 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800286 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
287}
288
James Feist8a983922019-10-24 17:11:56 -0700289static void makeProbeInterface(size_t bus, size_t address)
290{
291 if (isMuxBus(bus))
292 {
293 return; // the mux buses are random, no need to publish
294 }
295 auto [it, success] = foundDevices.emplace(
296 std::make_pair(bus, address),
297 objServer.add_interface(
298 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
299 std::to_string(address),
300 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
301 if (!success)
302 {
303 return; // already added
304 }
305 it->second->register_property("Bus", bus);
306 it->second->register_property("Address", address);
307 it->second->initialize();
308}
309
Vijay Khemka2d681f62018-11-06 15:51:00 -0800310static int isDevice16Bit(int file)
311{
312 /* Get first byte */
313 int byte1 = i2c_smbus_read_byte_data(file, 0);
314 if (byte1 < 0)
315 {
316 return byte1;
317 }
318 /* Read 7 more bytes, it will read same first byte in case of
319 * 8 bit but it will read next byte in case of 16 bit
320 */
321 for (int i = 0; i < 7; i++)
322 {
323 int byte2 = i2c_smbus_read_byte_data(file, 0);
324 if (byte2 < 0)
325 {
326 return byte2;
327 }
328 if (byte2 != byte1)
329 {
330 return 1;
331 }
332 }
333 return 0;
334}
335
Xiang Liu2801a702020-01-20 14:29:34 -0800336// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
337// from_slave_buf_len bytes.
338static int i2c_smbus_write_then_read(int file, uint16_t address,
339 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
340 uint8_t* fromSlaveBuf,
341 uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800342{
Xiang Liu2801a702020-01-20 14:29:34 -0800343 if (toSlaveBuf == NULL || toSlaveBufLen == 0 || fromSlaveBuf == NULL ||
344 fromSlaveBufLen == 0)
345 {
346 return -1;
347 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800348
Xiang Liu2801a702020-01-20 14:29:34 -0800349#define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2
350 struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT];
351 struct i2c_rdwr_ioctl_data rdwr;
352
353 msgs[0].addr = address;
354 msgs[0].flags = 0;
355 msgs[0].len = toSlaveBufLen;
356 msgs[0].buf = toSlaveBuf;
357 msgs[1].addr = address;
358 msgs[1].flags = I2C_M_RD;
359 msgs[1].len = fromSlaveBufLen;
360 msgs[1].buf = fromSlaveBuf;
361
362 rdwr.msgs = msgs;
363 rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT;
364
365 int ret = ioctl(file, I2C_RDWR, &rdwr);
366
367 return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? ret : -1;
368}
369
370static int64_t readBlockData(int flag, int file, uint16_t address,
371 uint16_t offset, uint8_t len, uint8_t* buf)
372{
Vijay Khemka2d681f62018-11-06 15:51:00 -0800373 if (flag == 0)
374 {
Xiang Liu2801a702020-01-20 14:29:34 -0800375 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
376 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800377 }
378
Xiang Liu2801a702020-01-20 14:29:34 -0800379 offset = htobe16(offset);
380 return i2c_smbus_write_then_read(
381 file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800382}
383
James Feist24bae7a2019-04-03 09:50:56 -0700384bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData)
385{
386 // ipmi spec format version number is currently at 1, verify it
387 if (blockData[0] != 0x1)
388 {
389 return false;
390 }
391
392 // verify pad is set to 0
393 if (blockData[6] != 0x0)
394 {
395 return false;
396 }
397
398 // verify offsets are 0, or don't point to another offset
399 std::set<uint8_t> foundOffsets;
400 for (int ii = 1; ii < 6; ii++)
401 {
402 if (blockData[ii] == 0)
403 {
404 continue;
405 }
James Feist0eb40352019-04-09 14:44:04 -0700406 auto inserted = foundOffsets.insert(blockData[ii]);
407 if (!inserted.second)
James Feist24bae7a2019-04-03 09:50:56 -0700408 {
409 return false;
410 }
411 }
412
413 // validate checksum
414 size_t sum = 0;
415 for (int jj = 0; jj < 7; jj++)
416 {
417 sum += blockData[jj];
418 }
419 sum = (256 - sum) & 0xFF;
420
421 if (sum != blockData[7])
422 {
423 return false;
424 }
425 return true;
426}
427
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700428// TODO: This code is very similar to the non-eeprom version and can be merged
429// with some tweaks.
430static std::vector<char> processEeprom(int bus, int address)
431{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700432 auto path = getEepromPath(bus, address);
433
434 int file = open(path.c_str(), O_RDONLY);
435 if (file < 0)
436 {
437 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700438 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700439 }
440
Patrick Venturebaba7672019-10-26 09:26:41 -0700441 std::string errorMessage = "eeprom at " + std::to_string(bus) +
442 " address " + std::to_string(address);
Xiang Liu2801a702020-01-20 14:29:34 -0800443 std::vector<char> device = readFruContents(
444 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700445
446 close(file);
447 return device;
448}
449
450std::set<int> findI2CEeproms(int i2cBus, std::shared_ptr<DeviceMap> devices)
451{
452 std::set<int> foundList;
453
454 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
455
456 // For each file listed under the i2c device
457 // NOTE: This should be faster than just checking for each possible address
458 // path.
459 for (const auto& p : fs::directory_iterator(path))
460 {
461 const std::string node = p.path().string();
462 std::smatch m;
463 bool found =
464 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
465
466 if (!found)
467 {
468 continue;
469 }
470 if (m.size() != 2)
471 {
472 std::cerr << "regex didn't capture\n";
473 continue;
474 }
475
476 std::ssub_match subMatch = m[1];
477 std::string addressString = subMatch.str();
478
479 std::size_t ignored;
480 const int hexBase = 16;
481 int address = std::stoi(addressString, &ignored, hexBase);
482
483 const std::string eeprom = node + "/eeprom";
484
485 try
486 {
487 if (!fs::exists(eeprom))
488 {
489 continue;
490 }
491 }
492 catch (...)
493 {
494 continue;
495 }
496
497 // There is an eeprom file at this address, it may have invalid
498 // contents, but we found it.
499 foundList.insert(address);
500
501 std::vector<char> device = processEeprom(i2cBus, address);
502 if (!device.empty())
503 {
504 devices->emplace(address, device);
505 }
506 }
507
508 return foundList;
509}
510
Patrick Venture4f47fe62019-08-08 16:30:38 -0700511int getBusFrus(int file, int first, int last, int bus,
512 std::shared_ptr<DeviceMap> devices)
James Feist3cb5fec2018-01-23 14:41:51 -0800513{
James Feist3cb5fec2018-01-23 14:41:51 -0800514
James Feist26c27ad2018-07-25 15:09:40 -0700515 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700516 // NOTE: When reading the devices raw on the bus, it can interfere with
517 // the driver's ability to operate, therefore read eeproms first before
518 // scanning for devices without drivers. Several experiments were run
519 // and it was determined that if there were any devices on the bus
520 // before the eeprom was hit and read, the eeprom driver wouldn't open
521 // while the bus device was open. An experiment was not performed to see
522 // if this issue was resolved if the i2c bus device was closed, but
523 // hexdumps of the eeprom later were successful.
524
525 // Scan for i2c eeproms loaded on this bus.
526 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800527 std::set<size_t>& failedItems = failedAddresses[bus];
528
529 std::set<size_t>* rootFailures = nullptr;
530 int rootBus = getRootBus(bus);
531
532 if (rootBus >= 0)
533 {
534 rootFailures = &(failedAddresses[rootBus]);
535 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700536
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530537 constexpr int startSkipSlaveAddr = 0;
538 constexpr int endSkipSlaveAddr = 12;
539
James Feist26c27ad2018-07-25 15:09:40 -0700540 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800541 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700542 if (skipList.find(ii) != skipList.end())
543 {
544 continue;
545 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530546 // skipping since no device is present in this range
547 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
548 {
549 continue;
550 }
James Feist26c27ad2018-07-25 15:09:40 -0700551 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530552 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800553 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700554 std::cerr << "device at bus " << bus << " register " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700555 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700556 continue;
557 }
558 // probe
559 else if (i2c_smbus_read_byte(file) < 0)
560 {
561 continue;
562 }
James Feist3cb5fec2018-01-23 14:41:51 -0800563
James Feist26c27ad2018-07-25 15:09:40 -0700564 if (DEBUG)
565 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700566 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700567 << "\n";
568 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800569
James Feist8a983922019-10-24 17:11:56 -0700570 makeProbeInterface(bus, ii);
571
James Feist7972bb92019-11-13 15:59:24 -0800572 if (failedItems.find(ii) != failedItems.end())
573 {
574 // if we failed to read it once, unlikely we can read it later
575 continue;
576 }
577
578 if (rootFailures != nullptr)
579 {
580 if (rootFailures->find(ii) != rootFailures->end())
581 {
582 continue;
583 }
584 }
585
Vijay Khemka2d681f62018-11-06 15:51:00 -0800586 /* Check for Device type if it is 8 bit or 16 bit */
587 int flag = isDevice16Bit(file);
588 if (flag < 0)
589 {
590 std::cerr << "failed to read bus " << bus << " address " << ii
591 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700592 if (powerIsOn)
593 {
594 failedItems.insert(ii);
595 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800596 continue;
597 }
598
Patrick Venturebaba7672019-10-26 09:26:41 -0700599 std::string errorMessage =
600 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
601 std::vector<char> device =
Xiang Liu2801a702020-01-20 14:29:34 -0800602 readFruContents(flag, file, static_cast<uint16_t>(ii),
603 readBlockData, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700604 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700605 {
James Feist26c27ad2018-07-25 15:09:40 -0700606 continue;
607 }
James Feist26c27ad2018-07-25 15:09:40 -0700608
Patrick Venture786f1792019-08-05 16:33:44 -0700609 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800610 }
James Feist26c27ad2018-07-25 15:09:40 -0700611 return 1;
612 });
613 std::future_status status =
614 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
615 if (status == std::future_status::timeout)
616 {
617 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700618 if (powerIsOn)
619 {
620 busBlacklist.insert(bus);
621 }
James Feist444830e2019-04-05 08:38:16 -0700622 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700623 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800624 }
625
James Feist444830e2019-04-05 08:38:16 -0700626 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700627 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800628}
629
Patrick Venture11f1ff42019-08-01 10:42:12 -0700630void loadBlacklist(const char* path)
631{
632 std::ifstream blacklistStream(path);
633 if (!blacklistStream.good())
634 {
635 // File is optional.
636 std::cerr << "Cannot open blacklist file.\n\n";
637 return;
638 }
639
640 nlohmann::json data =
641 nlohmann::json::parse(blacklistStream, nullptr, false);
642 if (data.is_discarded())
643 {
644 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
645 "exiting\n";
646 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700647 }
648
649 // It's expected to have at least one field, "buses" that is an array of the
650 // buses by integer. Allow for future options to exclude further aspects,
651 // such as specific addresses or ranges.
652 if (data.type() != nlohmann::json::value_t::object)
653 {
654 std::cerr << "Illegal blacklist, expected to read dictionary\n";
655 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700656 }
657
658 // If buses field is missing, that's fine.
659 if (data.count("buses") == 1)
660 {
661 // Parse the buses array after a little validation.
662 auto buses = data.at("buses");
663 if (buses.type() != nlohmann::json::value_t::array)
664 {
665 // Buses field present but invalid, therefore this is an error.
666 std::cerr << "Invalid contents for blacklist buses field\n";
667 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700668 }
669
670 // Catch exception here for type mis-match.
671 try
672 {
673 for (const auto& bus : buses)
674 {
675 busBlacklist.insert(bus.get<size_t>());
676 }
677 }
678 catch (const nlohmann::detail::type_error& e)
679 {
680 // Type mis-match is a critical error.
681 std::cerr << "Invalid bus type: " << e.what() << "\n";
682 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700683 }
684 }
685
686 return;
687}
688
James Feista465ccc2019-02-08 12:51:01 -0800689static void FindI2CDevices(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700690 BusMap& busmap)
James Feist3cb5fec2018-01-23 14:41:51 -0800691{
James Feista465ccc2019-02-08 12:51:01 -0800692 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800693 {
694 auto busnum = i2cBus.string();
695 auto lastDash = busnum.rfind(std::string("-"));
696 // delete everything before dash inclusive
697 if (lastDash != std::string::npos)
698 {
699 busnum.erase(0, lastDash + 1);
700 }
James Feist0c3980a2019-12-19 11:09:27 -0800701
James Feist3cb5fec2018-01-23 14:41:51 -0800702 auto bus = std::stoi(busnum);
James Feist444830e2019-04-05 08:38:16 -0700703 if (busBlacklist.find(bus) != busBlacklist.end())
704 {
705 continue; // skip previously failed busses
706 }
James Feist3cb5fec2018-01-23 14:41:51 -0800707
James Feist0c3980a2019-12-19 11:09:27 -0800708 int rootBus = getRootBus(bus);
709 if (busBlacklist.find(rootBus) != busBlacklist.end())
710 {
711 continue;
712 }
713
James Feist3cb5fec2018-01-23 14:41:51 -0800714 auto file = open(i2cBus.c_str(), O_RDWR);
715 if (file < 0)
716 {
717 std::cerr << "unable to open i2c device " << i2cBus.string()
718 << "\n";
719 continue;
720 }
721 unsigned long funcs = 0;
722
723 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
724 {
725 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700726 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800727 << bus << "\n";
728 continue;
729 }
730 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
731 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
732 {
733 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
734 << bus << "\n";
735 continue;
736 }
James Feist98132792019-07-09 13:29:09 -0700737 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800738 device = std::make_shared<DeviceMap>();
739
Nikhil Potaded8884f12019-03-27 13:27:13 -0700740 // i2cdetect by default uses the range 0x03 to 0x77, as
741 // this is what we have tested with, use this range. Could be
742 // changed in future.
743 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800744 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700745 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800746 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700747
748 // fd is closed in this function in case the bus locks up
Patrick Venture4f47fe62019-08-08 16:30:38 -0700749 getBusFrus(file, 0x03, 0x77, bus, device);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700750
751 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800752 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700753 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800754 }
James Feist3cb5fec2018-01-23 14:41:51 -0800755 }
James Feist3cb5fec2018-01-23 14:41:51 -0800756}
757
James Feist6ebf9de2018-05-15 15:01:17 -0700758// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700759struct FindDevicesWithCallback :
760 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700761{
James Feista465ccc2019-02-08 12:51:01 -0800762 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
James Feist5cb06082019-10-24 17:11:13 -0700763 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800764 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700765 _i2cBuses(i2cBuses),
James Feist5cb06082019-10-24 17:11:13 -0700766 _busMap(busmap), _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700767 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700768 ~FindDevicesWithCallback()
769 {
770 _callback();
771 }
772 void run()
773 {
James Feist98132792019-07-09 13:29:09 -0700774 FindI2CDevices(_i2cBuses, _busMap);
James Feist6ebf9de2018-05-15 15:01:17 -0700775 }
776
James Feista465ccc2019-02-08 12:51:01 -0800777 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800778 BusMap& _busMap;
James Feist6ebf9de2018-05-15 15:01:17 -0700779 std::function<void(void)> _callback;
780};
781
James Feist3cb5fec2018-01-23 14:41:51 -0800782static const std::tm intelEpoch(void)
783{
James Feist98132792019-07-09 13:29:09 -0700784 std::tm val = {};
James Feist3cb5fec2018-01-23 14:41:51 -0800785 val.tm_year = 1996 - 1900;
786 return val;
787}
788
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +0800789static char sixBitToChar(uint8_t val)
790{
791 return static_cast<char>((val & 0x3f) + ' ');
792}
793
794/* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */
795static const char bcdHighChars[] = {
796 ' ', '-', '.', 'X', 'X', 'X',
797};
798
799static char bcdPlusToChar(uint8_t val)
800{
801 val &= 0xf;
802 return (val < 10) ? static_cast<char>(val + '0') : bcdHighChars[val - 10];
803}
804
805enum class DecodeState
806{
807 ok,
808 end,
809 err,
810};
811
812enum FRUDataEncoding
813{
814 binary = 0x0,
815 bcdPlus = 0x1,
816 sixBitASCII = 0x2,
817 languageDependent = 0x3,
818};
819
820/* Decode FRU data into a std::string, given an input iterator and end. If the
821 * state returned is fruDataOk, then the resulting string is the decoded FRU
822 * data. The input iterator is advanced past the data consumed.
823 *
824 * On fruDataErr, we have lost synchronisation with the length bytes, so the
825 * iterator is no longer usable.
826 */
827static std::pair<DecodeState, std::string>
828 decodeFruData(std::vector<char>::const_iterator& iter,
829 const std::vector<char>::const_iterator& end)
830{
831 std::string value;
832 unsigned int i;
833
834 /* we need at least one byte to decode the type/len header */
835 if (iter == end)
836 {
837 std::cerr << "Truncated FRU data\n";
838 return make_pair(DecodeState::err, value);
839 }
840
841 uint8_t c = *(iter++);
842
843 /* 0xc1 is the end marker */
844 if (c == 0xc1)
845 {
846 return make_pair(DecodeState::end, value);
847 }
848
849 /* decode type/len byte */
850 uint8_t type = static_cast<uint8_t>(c >> 6);
851 uint8_t len = static_cast<uint8_t>(c & 0x3f);
852
853 /* we should have at least len bytes of data available overall */
854 if (iter + len > end)
855 {
856 std::cerr << "FRU data field extends past end of FRU data\n";
857 return make_pair(DecodeState::err, value);
858 }
859
860 switch (type)
861 {
862 case FRUDataEncoding::binary:
863 case FRUDataEncoding::languageDependent:
864 /* For language-code dependent encodings, assume 8-bit ASCII */
865 value = std::string(iter, iter + len);
866 iter += len;
867 break;
868
869 case FRUDataEncoding::bcdPlus:
870 value = std::string();
871 for (i = 0; i < len; i++, iter++)
872 {
873 uint8_t val = static_cast<uint8_t>(*iter);
874 value.push_back(bcdPlusToChar(val >> 4));
875 value.push_back(bcdPlusToChar(val & 0xf));
876 }
877 break;
878
879 case FRUDataEncoding::sixBitASCII:
880 {
881 unsigned int accum;
882 unsigned int accumBitLen = 0;
883 value = std::string();
884 for (i = 0; i < len; i++, iter++)
885 {
886 /* we need to cast to unsigned before the promotion to
887 * int, so we don't sign-extend. */
888 accum |= static_cast<unsigned char>(*iter) << accumBitLen;
889 accumBitLen += 8;
890 while (accumBitLen >= 6)
891 {
892 value.push_back(sixBitToChar(accum & 0x3f));
893 accum >>= 6;
894 accumBitLen -= 6;
895 }
896 }
897 }
898 break;
899 }
900
901 return make_pair(DecodeState::ok, value);
902}
903
James Feista465ccc2019-02-08 12:51:01 -0800904bool formatFru(const std::vector<char>& fruBytes,
905 boost::container::flat_map<std::string, std::string>& result)
James Feist3cb5fec2018-01-23 14:41:51 -0800906{
James Feista465ccc2019-02-08 12:51:01 -0800907 static const std::vector<const char*> CHASSIS_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800908 "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800909
James Feista465ccc2019-02-08 12:51:01 -0800910 static const std::vector<const char*> BOARD_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800911 "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
912 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800913
James Feista465ccc2019-02-08 12:51:01 -0800914 static const std::vector<const char*> PRODUCT_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800915 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER",
916 "VERSION", "SERIAL_NUMBER", "ASSET_TAG",
917 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800918
James Feistd068e932018-09-20 10:53:07 -0700919 if (fruBytes.size() <= 8)
James Feist3cb5fec2018-01-23 14:41:51 -0800920 {
921 return false;
922 }
923 std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin();
James Feist9eb0b582018-04-27 12:15:46 -0700924 result["Common_Format_Version"] =
James Feist3cb5fec2018-01-23 14:41:51 -0800925 std::to_string(static_cast<int>(*fruAreaOffsetField));
926
James Feista465ccc2019-02-08 12:51:01 -0800927 const std::vector<const char*>* fieldData;
James Feist3cb5fec2018-01-23 14:41:51 -0800928
James Feist0eb40352019-04-09 14:44:04 -0700929 for (const std::string& area : FRU_AREAS)
James Feist3cb5fec2018-01-23 14:41:51 -0800930 {
Patrick Venture4b7a7452019-10-28 08:41:02 -0700931 ++fruAreaOffsetField;
James Feist3cb5fec2018-01-23 14:41:51 -0800932 if (fruAreaOffsetField >= fruBytes.end())
933 {
934 return false;
935 }
936 size_t offset = (*fruAreaOffsetField) * 8;
937
938 if (offset > 1)
939 {
940 // +2 to skip format and length
941 std::vector<char>::const_iterator fruBytesIter =
942 fruBytes.begin() + offset + 2;
943
944 if (fruBytesIter >= fruBytes.end())
945 {
946 return false;
947 }
948
949 if (area == "CHASSIS")
950 {
951 result["CHASSIS_TYPE"] =
952 std::to_string(static_cast<int>(*fruBytesIter));
953 fruBytesIter += 1;
954 fieldData = &CHASSIS_FRU_AREAS;
955 }
956 else if (area == "BOARD")
957 {
958 result["BOARD_LANGUAGE_CODE"] =
959 std::to_string(static_cast<int>(*fruBytesIter));
960 fruBytesIter += 1;
961 if (fruBytesIter >= fruBytes.end())
962 {
963 return false;
964 }
965
966 unsigned int minutes = *fruBytesIter |
967 *(fruBytesIter + 1) << 8 |
968 *(fruBytesIter + 2) << 16;
969 std::tm fruTime = intelEpoch();
Patrick Venturee0e6f5f2019-08-12 19:00:11 -0700970 std::time_t timeValue = std::mktime(&fruTime);
James Feist3cb5fec2018-01-23 14:41:51 -0800971 timeValue += minutes * 60;
Patrick Venturee0e6f5f2019-08-12 19:00:11 -0700972 fruTime = *std::gmtime(&timeValue);
James Feist3cb5fec2018-01-23 14:41:51 -0800973
Patrick Venturee0e6f5f2019-08-12 19:00:11 -0700974 // Tue Nov 20 23:08:00 2018
975 char timeString[32] = {0};
976 auto bytes = std::strftime(timeString, sizeof(timeString),
Patrick Venturefff050a2019-08-13 11:44:30 -0700977 "%Y-%m-%d - %H:%M:%S", &fruTime);
Patrick Venturee0e6f5f2019-08-12 19:00:11 -0700978 if (bytes == 0)
979 {
980 std::cerr << "invalid time string encountered\n";
981 return false;
982 }
983
984 result["BOARD_MANUFACTURE_DATE"] = std::string(timeString);
James Feist3cb5fec2018-01-23 14:41:51 -0800985 fruBytesIter += 3;
986 fieldData = &BOARD_FRU_AREAS;
987 }
988 else if (area == "PRODUCT")
989 {
990 result["PRODUCT_LANGUAGE_CODE"] =
991 std::to_string(static_cast<int>(*fruBytesIter));
992 fruBytesIter += 1;
993 fieldData = &PRODUCT_FRU_AREAS;
994 }
995 else
996 {
997 continue;
998 }
James Feista465ccc2019-02-08 12:51:01 -0800999 for (auto& field : *fieldData)
James Feist3cb5fec2018-01-23 14:41:51 -08001000 {
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001001 auto res = decodeFruData(fruBytesIter, fruBytes.end());
1002 std::string name = area + "_" + field;
James Feist3cb5fec2018-01-23 14:41:51 -08001003
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001004 if (res.first == DecodeState::end)
Vijay Khemka5d5de442018-11-07 10:51:25 -08001005 {
1006 break;
1007 }
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001008 else if (res.first == DecodeState::err)
James Feist3cb5fec2018-01-23 14:41:51 -08001009 {
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001010 std::cerr << "Error while parsing " << name << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -08001011 return false;
1012 }
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001013
1014 std::string value = res.second;
James Feist3cb5fec2018-01-23 14:41:51 -08001015
Ed Tanous2147e672019-02-27 13:59:56 -08001016 // Strip non null characters from the end
1017 value.erase(std::find_if(value.rbegin(), value.rend(),
1018 [](char ch) { return ch != 0; })
1019 .base(),
1020 value.end());
1021
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001022 result[name] = std::move(value);
James Feist3cb5fec2018-01-23 14:41:51 -08001023 }
1024 }
1025 }
1026
1027 return true;
1028}
1029
Nikhil Potaded8884f12019-03-27 13:27:13 -07001030std::vector<uint8_t>& getFruInfo(const uint8_t& bus, const uint8_t& address)
1031{
1032 auto deviceMap = busMap.find(bus);
1033 if (deviceMap == busMap.end())
1034 {
1035 throw std::invalid_argument("Invalid Bus.");
1036 }
1037 auto device = deviceMap->second->find(address);
1038 if (device == deviceMap->second->end())
1039 {
1040 throw std::invalid_argument("Invalid Address.");
1041 }
1042 std::vector<uint8_t>& ret =
1043 reinterpret_cast<std::vector<uint8_t>&>(device->second);
1044
1045 return ret;
1046}
1047
James Feist3cb5fec2018-01-23 14:41:51 -08001048void AddFruObjectToDbus(
James Feist5cb06082019-10-24 17:11:13 -07001049 std::vector<char>& device,
James Feista465ccc2019-02-08 12:51:01 -08001050 boost::container::flat_map<
1051 std::pair<size_t, size_t>,
1052 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -07001053 uint32_t bus, uint32_t address)
James Feist3cb5fec2018-01-23 14:41:51 -08001054{
1055 boost::container::flat_map<std::string, std::string> formattedFru;
1056 if (!formatFru(device, formattedFru))
1057 {
Patrick Ventureb755c832019-08-07 11:09:14 -07001058 std::cerr << "failed to format fru for device at bus " << bus
1059 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -08001060 return;
1061 }
Patrick Venture96cdaef2019-07-30 13:30:52 -07001062
James Feist3cb5fec2018-01-23 14:41:51 -08001063 auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME");
1064 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -07001065 // Not found under Board section or an empty string.
1066 if (productNameFind == formattedFru.end() ||
1067 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -08001068 {
1069 productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME");
1070 }
Patrick Venture96cdaef2019-07-30 13:30:52 -07001071 // Found under Product section and not an empty string.
1072 if (productNameFind != formattedFru.end() &&
1073 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -08001074 {
1075 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -08001076 std::regex illegalObject("[^A-Za-z0-9_]");
1077 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -08001078 }
1079 else
1080 {
1081 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
1082 UNKNOWN_BUS_OBJECT_COUNT++;
1083 }
1084
James Feist918e18c2018-02-13 15:51:07 -08001085 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -08001086 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -07001087 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -08001088 {
Patrick Venture015fb0a2019-08-16 09:33:31 -07001089 int highest = -1;
1090 bool found = false;
1091
James Feista465ccc2019-02-08 12:51:01 -08001092 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -08001093 {
Patrick Venture015fb0a2019-08-16 09:33:31 -07001094 std::string path = busIface.second->get_object_path();
1095 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
James Feist918e18c2018-02-13 15:51:07 -08001096 {
Nikhil Potaded8884f12019-03-27 13:27:13 -07001097 if (isMuxBus(bus) && address == busIface.first.second &&
James Feist98132792019-07-09 13:29:09 -07001098 (getFruInfo(static_cast<uint8_t>(busIface.first.first),
1099 static_cast<uint8_t>(busIface.first.second)) ==
1100 getFruInfo(static_cast<uint8_t>(bus),
1101 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -07001102 {
Nikhil Potaded8884f12019-03-27 13:27:13 -07001103 // This device is already added to the lower numbered bus,
1104 // do not replicate it.
1105 return;
James Feist79e9c0b2018-03-15 15:21:17 -07001106 }
Patrick Venture015fb0a2019-08-16 09:33:31 -07001107
1108 // Check if the match named has extra information.
1109 found = true;
1110 std::smatch base_match;
1111
1112 bool match = std::regex_match(
1113 path, base_match, std::regex(productName + "_(\\d+)$"));
1114 if (match)
James Feist79e9c0b2018-03-15 15:21:17 -07001115 {
Patrick Venture015fb0a2019-08-16 09:33:31 -07001116 if (base_match.size() == 2)
1117 {
1118 std::ssub_match base_sub_match = base_match[1];
1119 std::string base = base_sub_match.str();
1120
1121 int value = std::stoi(base);
1122 highest = (value > highest) ? value : highest;
1123 }
James Feist79e9c0b2018-03-15 15:21:17 -07001124 }
James Feist918e18c2018-02-13 15:51:07 -08001125 }
Patrick Venture015fb0a2019-08-16 09:33:31 -07001126 } // end searching objects
1127
1128 if (found)
1129 {
1130 // We found something with the same name. If highest was still -1,
1131 // it means this new entry will be _0.
1132 productName += "_";
1133 productName += std::to_string(++highest);
James Feist918e18c2018-02-13 15:51:07 -08001134 }
1135 }
James Feist3cb5fec2018-01-23 14:41:51 -08001136
James Feist9eb0b582018-04-27 12:15:46 -07001137 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1138 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
1139 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
1140
James Feista465ccc2019-02-08 12:51:01 -08001141 for (auto& property : formattedFru)
James Feist3cb5fec2018-01-23 14:41:51 -08001142 {
James Feist9eb0b582018-04-27 12:15:46 -07001143
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -07001144 std::regex_replace(property.second.begin(), property.second.begin(),
1145 property.second.end(), NON_ASCII_REGEX, "_");
James Feist9eb0b582018-04-27 12:15:46 -07001146 if (property.second.empty())
1147 {
1148 continue;
1149 }
1150 std::string key =
1151 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
1152 if (!iface->register_property(key, property.second + '\0'))
1153 {
1154 std::cerr << "illegal key: " << key << "\n";
1155 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -07001156 if (DEBUG)
1157 {
1158 std::cout << property.first << ": " << property.second << "\n";
1159 }
James Feist3cb5fec2018-01-23 14:41:51 -08001160 }
James Feist2a9d6db2018-04-27 15:48:28 -07001161
1162 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -07001163 iface->register_property("BUS", bus);
1164 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -07001165
James Feist9eb0b582018-04-27 12:15:46 -07001166 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001167}
1168
James Feista465ccc2019-02-08 12:51:01 -08001169static bool readBaseboardFru(std::vector<char>& baseboardFru)
James Feist3cb5fec2018-01-23 14:41:51 -08001170{
1171 // try to read baseboard fru from file
1172 std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
1173 if (baseboardFruFile.good())
1174 {
1175 baseboardFruFile.seekg(0, std::ios_base::end);
James Feist98132792019-07-09 13:29:09 -07001176 size_t fileSize = static_cast<size_t>(baseboardFruFile.tellg());
James Feist3cb5fec2018-01-23 14:41:51 -08001177 baseboardFru.resize(fileSize);
1178 baseboardFruFile.seekg(0, std::ios_base::beg);
1179 baseboardFruFile.read(baseboardFru.data(), fileSize);
1180 }
1181 else
1182 {
1183 return false;
1184 }
1185 return true;
1186}
1187
James Feista465ccc2019-02-08 12:51:01 -08001188bool writeFru(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -07001189{
1190 boost::container::flat_map<std::string, std::string> tmp;
1191 if (fru.size() > MAX_FRU_SIZE)
1192 {
1193 std::cerr << "Invalid fru.size() during writeFru\n";
1194 return false;
1195 }
1196 // verify legal fru by running it through fru parsing logic
James Feista465ccc2019-02-08 12:51:01 -08001197 if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp))
James Feistb49ffc32018-05-02 11:10:43 -07001198 {
1199 std::cerr << "Invalid fru format during writeFru\n";
1200 return false;
1201 }
1202 // baseboard fru
1203 if (bus == 0 && address == 0)
1204 {
1205 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
1206 if (!file.good())
1207 {
1208 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
1209 << "\n";
James Feistddb78302018-09-06 11:45:42 -07001210 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001211 return false;
1212 }
James Feista465ccc2019-02-08 12:51:01 -08001213 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -07001214 return file.good();
1215 }
1216 else
1217 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -07001218 if (hasEepromFile(bus, address))
1219 {
1220 auto path = getEepromPath(bus, address);
1221 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
1222 if (eeprom < 0)
1223 {
1224 std::cerr << "unable to open i2c device " << path << "\n";
1225 throw DBusInternalError();
1226 return false;
1227 }
1228
1229 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
1230 if (writtenBytes < 0)
1231 {
1232 std::cerr << "unable to write to i2c device " << path << "\n";
1233 close(eeprom);
1234 throw DBusInternalError();
1235 return false;
1236 }
1237
1238 close(eeprom);
1239 return true;
1240 }
1241
James Feistb49ffc32018-05-02 11:10:43 -07001242 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
1243
1244 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
1245 if (file < 0)
1246 {
1247 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -07001248 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001249 return false;
1250 }
1251 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
1252 {
1253 std::cerr << "unable to set device address\n";
1254 close(file);
James Feistddb78302018-09-06 11:45:42 -07001255 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001256 return false;
1257 }
1258
1259 constexpr const size_t RETRY_MAX = 2;
1260 uint16_t index = 0;
1261 size_t retries = RETRY_MAX;
1262 while (index < fru.size())
1263 {
1264 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
1265 (retries == RETRY_MAX))
1266 {
1267 // The 4K EEPROM only uses the A2 and A1 device address bits
1268 // with the third bit being a memory page address bit.
1269 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
1270 {
1271 std::cerr << "unable to set device address\n";
1272 close(file);
James Feistddb78302018-09-06 11:45:42 -07001273 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001274 return false;
1275 }
1276 }
1277
James Feist98132792019-07-09 13:29:09 -07001278 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
1279 fru[index]) < 0)
James Feistb49ffc32018-05-02 11:10:43 -07001280 {
1281 if (!retries--)
1282 {
1283 std::cerr << "error writing fru: " << strerror(errno)
1284 << "\n";
1285 close(file);
James Feistddb78302018-09-06 11:45:42 -07001286 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001287 return false;
1288 }
1289 }
1290 else
1291 {
1292 retries = RETRY_MAX;
1293 index++;
1294 }
1295 // most eeproms require 5-10ms between writes
1296 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1297 }
1298 close(file);
1299 return true;
1300 }
1301}
1302
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001303void rescanOneBus(
1304 BusMap& busmap, uint8_t busNum,
1305 boost::container::flat_map<
1306 std::pair<size_t, size_t>,
1307 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap)
1308{
1309 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
1310 if (!fs::exists(busPath))
1311 {
1312 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
1313 << "\n";
1314 throw std::invalid_argument("Invalid Bus.");
1315 }
1316
1317 std::vector<fs::path> i2cBuses;
1318 i2cBuses.emplace_back(busPath);
1319
1320 for (auto& [pair, interface] : foundDevices)
1321 {
1322 if (pair.first == static_cast<size_t>(busNum))
1323 {
1324 objServer.remove_interface(interface);
1325 foundDevices.erase(pair);
1326 }
1327 }
1328
1329 auto scan = std::make_shared<FindDevicesWithCallback>(
1330 i2cBuses, busmap, [busNum, &busmap, &dbusInterfaceMap]() {
1331 for (auto& busIface : dbusInterfaceMap)
1332 {
1333 if (busIface.first.first == static_cast<size_t>(busNum))
1334 {
1335 objServer.remove_interface(busIface.second);
1336 }
1337 }
1338 auto& devicemap = busmap[busNum];
1339 for (auto& device : *devicemap)
1340 {
1341 AddFruObjectToDbus(device.second, dbusInterfaceMap,
1342 static_cast<uint32_t>(busNum), device.first);
1343 }
1344 });
1345 scan->run();
1346}
1347
James Feist9eb0b582018-04-27 12:15:46 -07001348void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -07001349 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -08001350 boost::container::flat_map<
1351 std::pair<size_t, size_t>,
James Feist5cb06082019-10-24 17:11:13 -07001352 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -08001353{
James Feist6ebf9de2018-05-15 15:01:17 -07001354 static boost::asio::deadline_timer timer(io);
1355 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001356
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001357 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -07001358 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -08001359 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001360 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001361
Nikhil Potaded8884f12019-03-27 13:27:13 -07001362 boost::container::flat_map<size_t, fs::path> busPaths;
1363 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001364 {
James Feist4131aea2018-03-09 09:47:30 -08001365 std::cerr << "unable to find i2c devices\n";
1366 return;
James Feist918e18c2018-02-13 15:51:07 -08001367 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001368
1369 for (auto busPath : busPaths)
1370 {
1371 i2cBuses.emplace_back(busPath.second);
1372 }
James Feist4131aea2018-03-09 09:47:30 -08001373
James Feist98132792019-07-09 13:29:09 -07001374 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001375 for (auto& [pair, interface] : foundDevices)
1376 {
1377 objServer.remove_interface(interface);
1378 }
1379 foundDevices.clear();
1380
James Feist5cb06082019-10-24 17:11:13 -07001381 auto scan =
1382 std::make_shared<FindDevicesWithCallback>(i2cBuses, busmap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001383 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001384 {
1385 objServer.remove_interface(busIface.second);
1386 }
James Feist4131aea2018-03-09 09:47:30 -08001387
James Feist6ebf9de2018-05-15 15:01:17 -07001388 dbusInterfaceMap.clear();
1389 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -08001390
James Feist6ebf9de2018-05-15 15:01:17 -07001391 // todo, get this from a more sensable place
1392 std::vector<char> baseboardFru;
1393 if (readBaseboardFru(baseboardFru))
1394 {
1395 boost::container::flat_map<int, std::vector<char>>
1396 baseboardDev;
1397 baseboardDev.emplace(0, baseboardFru);
James Feist98132792019-07-09 13:29:09 -07001398 busmap[0] = std::make_shared<DeviceMap>(baseboardDev);
James Feist6ebf9de2018-05-15 15:01:17 -07001399 }
James Feist98132792019-07-09 13:29:09 -07001400 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001401 {
James Feista465ccc2019-02-08 12:51:01 -08001402 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001403 {
James Feist5cb06082019-10-24 17:11:13 -07001404 AddFruObjectToDbus(device.second, dbusInterfaceMap,
1405 devicemap.first, device.first);
James Feist6ebf9de2018-05-15 15:01:17 -07001406 }
1407 }
1408 });
1409 scan->run();
1410 });
James Feist918e18c2018-02-13 15:51:07 -08001411}
1412
James Feist98132792019-07-09 13:29:09 -07001413int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001414{
1415 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001416 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001417 std::vector<fs::path> i2cBuses;
1418
James Feista3c180a2018-08-09 16:06:04 -07001419 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001420 {
1421 std::cerr << "unable to find i2c devices\n";
1422 return 1;
1423 }
James Feist3cb5fec2018-01-23 14:41:51 -08001424
Patrick Venture11f1ff42019-08-01 10:42:12 -07001425 // check for and load blacklist with initial buses.
1426 loadBlacklist(blacklistPath);
1427
Vijay Khemka065f6d92018-12-18 10:37:47 -08001428 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001429
James Feist6ebf9de2018-05-15 15:01:17 -07001430 // this is a map with keys of pair(bus number, address) and values of
1431 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001432 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001433 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1434 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001435
James Feist9eb0b582018-04-27 12:15:46 -07001436 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1437 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1438 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001439
James Feist5cb06082019-10-24 17:11:13 -07001440 iface->register_method("ReScan",
1441 [&]() { rescanBusses(busMap, dbusInterfaceMap); });
James Feist2a9d6db2018-04-27 15:48:28 -07001442
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001443 iface->register_method("ReScanBus", [&](uint8_t bus) {
1444 rescanOneBus(busMap, bus, dbusInterfaceMap);
1445 });
1446
Nikhil Potaded8884f12019-03-27 13:27:13 -07001447 iface->register_method("GetRawFru", getFruInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001448
1449 iface->register_method("WriteFru", [&](const uint8_t bus,
1450 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001451 const std::vector<uint8_t>& data) {
James Feistb49ffc32018-05-02 11:10:43 -07001452 if (!writeFru(bus, address, data))
1453 {
James Feistddb78302018-09-06 11:45:42 -07001454 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001455 return;
1456 }
1457 // schedule rescan on success
James Feist5cb06082019-10-24 17:11:13 -07001458 rescanBusses(busMap, dbusInterfaceMap);
James Feistb49ffc32018-05-02 11:10:43 -07001459 });
James Feist9eb0b582018-04-27 12:15:46 -07001460 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001461
James Feist9eb0b582018-04-27 12:15:46 -07001462 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -08001463 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -08001464 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001465 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001466 std::string,
1467 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001468 values;
1469 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001470 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001471 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001472 {
James Feistcb5661d2020-06-12 15:05:01 -07001473 powerIsOn = boost::ends_with(
1474 std::get<std::string>(findState->second), "Running");
James Feist0eeb79c2019-10-09 13:35:29 -07001475 }
James Feist6ebf9de2018-05-15 15:01:17 -07001476
James Feistcb5661d2020-06-12 15:05:01 -07001477 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001478 {
James Feist5cb06082019-10-24 17:11:13 -07001479 rescanBusses(busMap, dbusInterfaceMap);
James Feist918e18c2018-02-13 15:51:07 -08001480 }
James Feist918e18c2018-02-13 15:51:07 -08001481 };
James Feist9eb0b582018-04-27 12:15:46 -07001482
1483 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -08001484 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001485 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001486 "openbmc_project/state/"
1487 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001488 eventHandler);
1489
James Feist4131aea2018-03-09 09:47:30 -08001490 int fd = inotify_init();
James Feist0eb40352019-04-09 14:44:04 -07001491 inotify_add_watch(fd, I2C_DEV_LOCATION,
1492 IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -08001493 std::array<char, 4096> readBuffer;
1494 std::string pendingBuffer;
1495 // monitor for new i2c devices
1496 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1497 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001498 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -08001499 std::size_t bytes_transferred) {
1500 if (ec)
1501 {
1502 std::cout << "Callback Error " << ec << "\n";
1503 return;
1504 }
1505 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
1506 bool devChange = false;
1507 while (pendingBuffer.size() > sizeof(inotify_event))
1508 {
James Feista465ccc2019-02-08 12:51:01 -08001509 const inotify_event* iEvent =
1510 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -08001511 pendingBuffer.data());
1512 switch (iEvent->mask)
1513 {
James Feist9eb0b582018-04-27 12:15:46 -07001514 case IN_CREATE:
1515 case IN_MOVED_TO:
1516 case IN_DELETE:
1517 if (boost::starts_with(std::string(iEvent->name),
1518 "i2c"))
1519 {
1520 devChange = true;
1521 }
James Feist4131aea2018-03-09 09:47:30 -08001522 }
1523
1524 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
1525 }
James Feist6ebf9de2018-05-15 15:01:17 -07001526 if (devChange)
James Feist4131aea2018-03-09 09:47:30 -08001527 {
James Feist5cb06082019-10-24 17:11:13 -07001528 rescanBusses(busMap, dbusInterfaceMap);
James Feist4131aea2018-03-09 09:47:30 -08001529 }
James Feist6ebf9de2018-05-15 15:01:17 -07001530
James Feist4131aea2018-03-09 09:47:30 -08001531 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1532 watchI2cBusses);
1533 };
1534
1535 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001536 // run the initial scan
James Feist5cb06082019-10-24 17:11:13 -07001537 rescanBusses(busMap, dbusInterfaceMap);
James Feist918e18c2018-02-13 15:51:07 -08001538
James Feist3cb5fec2018-01-23 14:41:51 -08001539 io.run();
1540 return 0;
1541}