blob: f4f904da0cf9f133916d9c2ba750d3bc7f2e0603 [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*/
Brad Bishop1fb9f3f2020-08-28 08:15:13 -040016/// \file FruDevice.cpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
Patrick Venturea49dc332019-10-26 08:32:02 -070018#include "Utils.hpp"
19
James Feist3b860982018-10-02 14:34:07 -070020#include <errno.h>
James Feist3cb5fec2018-01-23 14:41:51 -080021#include <fcntl.h>
James Feist3b860982018-10-02 14:34:07 -070022#include <sys/inotify.h>
23#include <sys/ioctl.h>
24
James Feist3b860982018-10-02 14:34:07 -070025#include <boost/algorithm/string/predicate.hpp>
Brad Bishop82c84bb2020-08-26 14:12:50 -040026#include <boost/asio/deadline_timer.hpp>
27#include <boost/asio/io_service.hpp>
James Feist3b860982018-10-02 14:34:07 -070028#include <boost/container/flat_map.hpp>
James Feist8c505da2020-05-28 10:06:33 -070029#include <nlohmann/json.hpp>
30#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
33#include <array>
James Feist3b860982018-10-02 14:34:07 -070034#include <chrono>
35#include <ctime>
Patrick Venturee3754002019-08-06 09:39:12 -070036#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080037#include <fstream>
Patrick Venturebaba7672019-10-26 09:26:41 -070038#include <functional>
James Feist3cb5fec2018-01-23 14:41:51 -080039#include <future>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070040#include <iomanip>
James Feist3cb5fec2018-01-23 14:41:51 -080041#include <iostream>
Patrick Venturee26395d2019-10-29 14:05:16 -070042#include <limits>
James Feist3f8a2782018-02-12 09:24:42 -080043#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070044#include <set>
45#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070046#include <string>
James Feist3b860982018-10-02 14:34:07 -070047#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080048#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080049#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070050#include <vector>
James Feist3b860982018-10-02 14:34:07 -070051
James Feist8c505da2020-05-28 10:06:33 -070052extern "C"
53{
James Feist3b860982018-10-02 14:34:07 -070054#include <i2c/smbus.h>
55#include <linux/i2c-dev.h>
56}
James Feist3cb5fec2018-01-23 14:41:51 -080057
Ed Tanous072e25d2018-12-16 21:45:20 -080058namespace fs = std::filesystem;
James Feist3cb5fec2018-01-23 14:41:51 -080059static constexpr bool DEBUG = false;
60static size_t UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feistb49ffc32018-05-02 11:10:43 -070061constexpr size_t MAX_FRU_SIZE = 512;
62constexpr size_t MAX_EEPROM_PAGE_INDEX = 255;
James Feist26c27ad2018-07-25 15:09:40 -070063constexpr size_t busTimeoutSeconds = 5;
Andrei Kartashev2f0de172020-08-13 14:29:40 +030064constexpr size_t fruBlockSize = 8; // FRU areas are measured in 8-byte blocks
James Feist3cb5fec2018-01-23 14:41:51 -080065
Patrick Venture11f1ff42019-08-01 10:42:12 -070066constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
67
James Feista465ccc2019-02-08 12:51:01 -080068const static constexpr char* BASEBOARD_FRU_LOCATION =
James Feist3cb5fec2018-01-23 14:41:51 -080069 "/etc/fru/baseboard.fru.bin";
70
James Feista465ccc2019-02-08 12:51:01 -080071const static constexpr char* I2C_DEV_LOCATION = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080072
Andrei Kartashev2f0de172020-08-13 14:29:40 +030073static const std::array<std::string, 5> FRU_AREAS = {
James Feist3cb5fec2018-01-23 14:41:51 -080074 "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"};
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -070075const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]");
Andrei Kartashev1c5b7062020-08-19 14:47:30 +030076using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>;
James Feist3cb5fec2018-01-23 14:41:51 -080077using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
78
James Feist444830e2019-04-05 08:38:16 -070079static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070080struct FindDevicesWithCallback;
81
Nikhil Potaded8884f12019-03-27 13:27:13 -070082static BusMap busMap;
83
James Feistcb5661d2020-06-12 15:05:01 -070084static bool powerIsOn = false;
85
James Feist8a983922019-10-24 17:11:56 -070086static boost::container::flat_map<
87 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
88 foundDevices;
89
James Feist7972bb92019-11-13 15:59:24 -080090static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
91
James Feist5cb06082019-10-24 17:11:13 -070092boost::asio::io_service io;
93auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
94auto objServer = sdbusplus::asio::object_server(systemBus);
95
Andrei Kartashev2f0de172020-08-13 14:29:40 +030096static const std::vector<std::string> CHASSIS_FRU_AREAS = {
Joshi-Mansid73b7442020-03-27 19:10:21 +053097 "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"};
98
Andrei Kartashev2f0de172020-08-13 14:29:40 +030099static const std::vector<std::string> BOARD_FRU_AREAS = {
Joshi-Mansid73b7442020-03-27 19:10:21 +0530100 "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
101 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
102
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300103static const std::vector<std::string> PRODUCT_FRU_AREAS = {
Joshi-Mansid73b7442020-03-27 19:10:21 +0530104 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", "VERSION", "SERIAL_NUMBER",
105 "ASSET_TAG", "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
106
Patrick Venturebaba7672019-10-26 09:26:41 -0700107bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300108bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +0530109 const std::string& assetTag, uint32_t bus, uint32_t address,
110 std::string propertyName,
111 boost::container::flat_map<
112 std::pair<size_t, size_t>,
113 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap);
Patrick Venturebaba7672019-10-26 09:26:41 -0700114
Xiang Liu2801a702020-01-20 14:29:34 -0800115using ReadBlockFunc =
116 std::function<int64_t(int flag, int file, uint16_t address, uint16_t offset,
117 uint8_t length, uint8_t* outBuf)>;
Patrick Venturebaba7672019-10-26 09:26:41 -0700118
119// Read and validate FRU contents, given the flag required for raw i2c, the open
120// file handle, a read method, and a helper string for failures.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300121std::vector<uint8_t> readFRUContents(int flag, int file, uint16_t address,
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300122 ReadBlockFunc readBlock,
123 const std::string& errorHelp)
Patrick Venturebaba7672019-10-26 09:26:41 -0700124{
125 std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
126
Xiang Liu2801a702020-01-20 14:29:34 -0800127 if (readBlock(flag, file, address, 0x0, 0x8, blockData.data()) < 0)
Patrick Venturebaba7672019-10-26 09:26:41 -0700128 {
129 std::cerr << "failed to read " << errorHelp << "\n";
130 return {};
131 }
132
133 // check the header checksum
134 if (!validateHeader(blockData))
135 {
136 if (DEBUG)
137 {
138 std::cerr << "Illegal header " << errorHelp << "\n";
139 }
140
141 return {};
142 }
143
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300144 std::vector<uint8_t> device;
Patrick Venturebaba7672019-10-26 09:26:41 -0700145 device.insert(device.end(), blockData.begin(), blockData.begin() + 8);
146
Patrick Venturee26395d2019-10-29 14:05:16 -0700147 bool hasMultiRecords = false;
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300148 ssize_t fruLength = fruBlockSize; // At least FRU header is present
Patrick Venturebaba7672019-10-26 09:26:41 -0700149 for (size_t jj = 1; jj <= FRU_AREAS.size(); jj++)
150 {
Patrick Venturef2ad76b2019-10-30 08:49:27 -0700151 // Offset value can be 255.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300152 unsigned int areaOffset = device[jj];
Patrick Venturebaba7672019-10-26 09:26:41 -0700153 if (areaOffset == 0)
154 {
155 continue;
156 }
157
Patrick Venturee26395d2019-10-29 14:05:16 -0700158 // MultiRecords are different. jj is not tracking section, it's walking
159 // the common header.
160 if (std::string(FRU_AREAS[jj - 1]) == std::string("MULTIRECORD"))
161 {
162 hasMultiRecords = true;
163 break;
164 }
165
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300166 areaOffset *= fruBlockSize;
Patrick Venturebaba7672019-10-26 09:26:41 -0700167
Xiang Liu2801a702020-01-20 14:29:34 -0800168 if (readBlock(flag, file, address, static_cast<uint16_t>(areaOffset),
169 0x2, blockData.data()) < 0)
Patrick Venturebaba7672019-10-26 09:26:41 -0700170 {
171 std::cerr << "failed to read " << errorHelp << "\n";
172 return {};
173 }
174
Patrick Venturef2ad76b2019-10-30 08:49:27 -0700175 // Ignore data type (blockData is already unsigned).
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300176 size_t length = blockData[1] * fruBlockSize;
Patrick Venturebaba7672019-10-26 09:26:41 -0700177 areaOffset += length;
178 fruLength = (areaOffset > fruLength) ? areaOffset : fruLength;
179 }
180
Patrick Venturee26395d2019-10-29 14:05:16 -0700181 if (hasMultiRecords)
182 {
183 // device[area count] is the index to the last area because the 0th
184 // entry is not an offset in the common header.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300185 unsigned int areaOffset = device[FRU_AREAS.size()];
186 areaOffset *= fruBlockSize;
Patrick Venturee26395d2019-10-29 14:05:16 -0700187
188 // the multi-area record header is 5 bytes long.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300189 constexpr size_t multiRecordHeaderSize = 5;
190 constexpr uint8_t multiRecordEndOfListMask = 0x80;
Patrick Venturee26395d2019-10-29 14:05:16 -0700191
192 // Sanity hard-limit to 64KB.
193 while (areaOffset < std::numeric_limits<uint16_t>::max())
194 {
195 // In multi-area, the area offset points to the 0th record, each
196 // record has 3 bytes of the header we care about.
Xiang Liu2801a702020-01-20 14:29:34 -0800197 if (readBlock(flag, file, address,
198 static_cast<uint16_t>(areaOffset), 0x3,
Patrick Venturee26395d2019-10-29 14:05:16 -0700199 blockData.data()) < 0)
200 {
201 std::cerr << "failed to read " << errorHelp << "\n";
202 return {};
203 }
204
205 // Ok, let's check the record length, which is in bytes (unsigned,
206 // up to 255, so blockData should hold uint8_t not char)
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300207 size_t recordLength = blockData[2];
Patrick Venturee26395d2019-10-29 14:05:16 -0700208 areaOffset += (recordLength + multiRecordHeaderSize);
209 fruLength = (areaOffset > fruLength) ? areaOffset : fruLength;
210
211 // If this is the end of the list bail.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300212 if ((blockData[1] & multiRecordEndOfListMask))
Patrick Venturee26395d2019-10-29 14:05:16 -0700213 {
214 break;
215 }
216 }
217 }
218
Patrick Venturebaba7672019-10-26 09:26:41 -0700219 // You already copied these first 8 bytes (the ipmi fru header size)
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300220 fruLength -= fruBlockSize;
Patrick Venturebaba7672019-10-26 09:26:41 -0700221
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300222 int readOffset = fruBlockSize;
Patrick Venturebaba7672019-10-26 09:26:41 -0700223
224 while (fruLength > 0)
225 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300226 int requestLength =
227 std::min(I2C_SMBUS_BLOCK_MAX, static_cast<int>(fruLength));
Patrick Venturebaba7672019-10-26 09:26:41 -0700228
Xiang Liu2801a702020-01-20 14:29:34 -0800229 if (readBlock(flag, file, address, static_cast<uint16_t>(readOffset),
Patrick Venturebaba7672019-10-26 09:26:41 -0700230 static_cast<uint8_t>(requestLength),
231 blockData.data()) < 0)
232 {
233 std::cerr << "failed to read " << errorHelp << "\n";
234 return {};
235 }
236
237 device.insert(device.end(), blockData.begin(),
238 blockData.begin() + requestLength);
239
240 readOffset += requestLength;
241 fruLength -= requestLength;
242 }
243
244 return device;
245}
246
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700247// Given a bus/address, produce the path in sysfs for an eeprom.
248static std::string getEepromPath(size_t bus, size_t address)
249{
250 std::stringstream output;
251 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
252 << std::setfill('0') << std::setw(4) << std::hex << address
253 << "/eeprom";
254 return output.str();
255}
256
257static bool hasEepromFile(size_t bus, size_t address)
258{
259 auto path = getEepromPath(bus, address);
260 try
261 {
262 return fs::exists(path);
263 }
264 catch (...)
265 {
266 return false;
267 }
268}
269
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700270static int64_t readFromEeprom(int flag __attribute__((unused)), int fd,
Xiang Liu2801a702020-01-20 14:29:34 -0800271 uint16_t address __attribute__((unused)),
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700272 uint16_t offset, uint8_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700273{
274 auto result = lseek(fd, offset, SEEK_SET);
275 if (result < 0)
276 {
277 std::cerr << "failed to seek\n";
278 return -1;
279 }
280
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700281 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700282}
283
James Feist5d7f4692020-06-17 18:22:33 -0700284static int busStrToInt(const std::string& busName)
285{
286 auto findBus = busName.rfind("-");
287 if (findBus == std::string::npos)
288 {
289 return -1;
290 }
291 return std::stoi(busName.substr(findBus + 1));
292}
293
James Feist7972bb92019-11-13 15:59:24 -0800294static int getRootBus(size_t bus)
295{
296 auto ec = std::error_code();
297 auto path = std::filesystem::read_symlink(
298 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
299 std::to_string(bus) + "/mux_device"),
300 ec);
301 if (ec)
302 {
303 return -1;
304 }
305
306 std::string filename = path.filename();
307 auto findBus = filename.find("-");
308 if (findBus == std::string::npos)
309 {
310 return -1;
311 }
312 return std::stoi(filename.substr(0, findBus));
313}
314
James Feistc95cb142018-02-26 10:41:42 -0800315static bool isMuxBus(size_t bus)
316{
Ed Tanous072e25d2018-12-16 21:45:20 -0800317 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800318 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
319}
320
James Feist8a983922019-10-24 17:11:56 -0700321static void makeProbeInterface(size_t bus, size_t address)
322{
323 if (isMuxBus(bus))
324 {
325 return; // the mux buses are random, no need to publish
326 }
327 auto [it, success] = foundDevices.emplace(
328 std::make_pair(bus, address),
329 objServer.add_interface(
330 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
331 std::to_string(address),
332 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
333 if (!success)
334 {
335 return; // already added
336 }
337 it->second->register_property("Bus", bus);
338 it->second->register_property("Address", address);
339 it->second->initialize();
340}
341
Vijay Khemka2d681f62018-11-06 15:51:00 -0800342static int isDevice16Bit(int file)
343{
344 /* Get first byte */
345 int byte1 = i2c_smbus_read_byte_data(file, 0);
346 if (byte1 < 0)
347 {
348 return byte1;
349 }
350 /* Read 7 more bytes, it will read same first byte in case of
351 * 8 bit but it will read next byte in case of 16 bit
352 */
353 for (int i = 0; i < 7; i++)
354 {
355 int byte2 = i2c_smbus_read_byte_data(file, 0);
356 if (byte2 < 0)
357 {
358 return byte2;
359 }
360 if (byte2 != byte1)
361 {
362 return 1;
363 }
364 }
365 return 0;
366}
367
Xiang Liu2801a702020-01-20 14:29:34 -0800368// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
369// from_slave_buf_len bytes.
370static int i2c_smbus_write_then_read(int file, uint16_t address,
371 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
372 uint8_t* fromSlaveBuf,
373 uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800374{
Xiang Liu2801a702020-01-20 14:29:34 -0800375 if (toSlaveBuf == NULL || toSlaveBufLen == 0 || fromSlaveBuf == NULL ||
376 fromSlaveBufLen == 0)
377 {
378 return -1;
379 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800380
Xiang Liu2801a702020-01-20 14:29:34 -0800381#define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2
382 struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT];
383 struct i2c_rdwr_ioctl_data rdwr;
384
385 msgs[0].addr = address;
386 msgs[0].flags = 0;
387 msgs[0].len = toSlaveBufLen;
388 msgs[0].buf = toSlaveBuf;
389 msgs[1].addr = address;
390 msgs[1].flags = I2C_M_RD;
391 msgs[1].len = fromSlaveBufLen;
392 msgs[1].buf = fromSlaveBuf;
393
394 rdwr.msgs = msgs;
395 rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT;
396
397 int ret = ioctl(file, I2C_RDWR, &rdwr);
398
399 return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? ret : -1;
400}
401
402static int64_t readBlockData(int flag, int file, uint16_t address,
403 uint16_t offset, uint8_t len, uint8_t* buf)
404{
Vijay Khemka2d681f62018-11-06 15:51:00 -0800405 if (flag == 0)
406 {
Xiang Liu2801a702020-01-20 14:29:34 -0800407 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
408 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800409 }
410
Xiang Liu2801a702020-01-20 14:29:34 -0800411 offset = htobe16(offset);
412 return i2c_smbus_write_then_read(
413 file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800414}
415
James Feist24bae7a2019-04-03 09:50:56 -0700416bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData)
417{
418 // ipmi spec format version number is currently at 1, verify it
419 if (blockData[0] != 0x1)
420 {
421 return false;
422 }
423
424 // verify pad is set to 0
425 if (blockData[6] != 0x0)
426 {
427 return false;
428 }
429
430 // verify offsets are 0, or don't point to another offset
431 std::set<uint8_t> foundOffsets;
432 for (int ii = 1; ii < 6; ii++)
433 {
434 if (blockData[ii] == 0)
435 {
436 continue;
437 }
James Feist0eb40352019-04-09 14:44:04 -0700438 auto inserted = foundOffsets.insert(blockData[ii]);
439 if (!inserted.second)
James Feist24bae7a2019-04-03 09:50:56 -0700440 {
441 return false;
442 }
443 }
444
445 // validate checksum
446 size_t sum = 0;
447 for (int jj = 0; jj < 7; jj++)
448 {
449 sum += blockData[jj];
450 }
451 sum = (256 - sum) & 0xFF;
452
453 if (sum != blockData[7])
454 {
455 return false;
456 }
457 return true;
458}
459
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700460// TODO: This code is very similar to the non-eeprom version and can be merged
461// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300462static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700463{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700464 auto path = getEepromPath(bus, address);
465
466 int file = open(path.c_str(), O_RDONLY);
467 if (file < 0)
468 {
469 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700470 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700471 }
472
Patrick Venturebaba7672019-10-26 09:26:41 -0700473 std::string errorMessage = "eeprom at " + std::to_string(bus) +
474 " address " + std::to_string(address);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300475 std::vector<uint8_t> device = readFRUContents(
Xiang Liu2801a702020-01-20 14:29:34 -0800476 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700477
478 close(file);
479 return device;
480}
481
482std::set<int> findI2CEeproms(int i2cBus, std::shared_ptr<DeviceMap> devices)
483{
484 std::set<int> foundList;
485
486 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
487
488 // For each file listed under the i2c device
489 // NOTE: This should be faster than just checking for each possible address
490 // path.
491 for (const auto& p : fs::directory_iterator(path))
492 {
493 const std::string node = p.path().string();
494 std::smatch m;
495 bool found =
496 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
497
498 if (!found)
499 {
500 continue;
501 }
502 if (m.size() != 2)
503 {
504 std::cerr << "regex didn't capture\n";
505 continue;
506 }
507
508 std::ssub_match subMatch = m[1];
509 std::string addressString = subMatch.str();
510
511 std::size_t ignored;
512 const int hexBase = 16;
513 int address = std::stoi(addressString, &ignored, hexBase);
514
515 const std::string eeprom = node + "/eeprom";
516
517 try
518 {
519 if (!fs::exists(eeprom))
520 {
521 continue;
522 }
523 }
524 catch (...)
525 {
526 continue;
527 }
528
529 // There is an eeprom file at this address, it may have invalid
530 // contents, but we found it.
531 foundList.insert(address);
532
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300533 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700534 if (!device.empty())
535 {
536 devices->emplace(address, device);
537 }
538 }
539
540 return foundList;
541}
542
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300543int getBusFRUs(int file, int first, int last, int bus,
Patrick Venture4f47fe62019-08-08 16:30:38 -0700544 std::shared_ptr<DeviceMap> devices)
James Feist3cb5fec2018-01-23 14:41:51 -0800545{
James Feist3cb5fec2018-01-23 14:41:51 -0800546
James Feist26c27ad2018-07-25 15:09:40 -0700547 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700548 // NOTE: When reading the devices raw on the bus, it can interfere with
549 // the driver's ability to operate, therefore read eeproms first before
550 // scanning for devices without drivers. Several experiments were run
551 // and it was determined that if there were any devices on the bus
552 // before the eeprom was hit and read, the eeprom driver wouldn't open
553 // while the bus device was open. An experiment was not performed to see
554 // if this issue was resolved if the i2c bus device was closed, but
555 // hexdumps of the eeprom later were successful.
556
557 // Scan for i2c eeproms loaded on this bus.
558 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800559 std::set<size_t>& failedItems = failedAddresses[bus];
560
561 std::set<size_t>* rootFailures = nullptr;
562 int rootBus = getRootBus(bus);
563
564 if (rootBus >= 0)
565 {
566 rootFailures = &(failedAddresses[rootBus]);
567 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700568
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530569 constexpr int startSkipSlaveAddr = 0;
570 constexpr int endSkipSlaveAddr = 12;
571
James Feist26c27ad2018-07-25 15:09:40 -0700572 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800573 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700574 if (skipList.find(ii) != skipList.end())
575 {
576 continue;
577 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530578 // skipping since no device is present in this range
579 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
580 {
581 continue;
582 }
James Feist26c27ad2018-07-25 15:09:40 -0700583 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530584 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800585 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700586 std::cerr << "device at bus " << bus << " register " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700587 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700588 continue;
589 }
590 // probe
591 else if (i2c_smbus_read_byte(file) < 0)
592 {
593 continue;
594 }
James Feist3cb5fec2018-01-23 14:41:51 -0800595
James Feist26c27ad2018-07-25 15:09:40 -0700596 if (DEBUG)
597 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700598 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700599 << "\n";
600 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800601
James Feist8a983922019-10-24 17:11:56 -0700602 makeProbeInterface(bus, ii);
603
James Feist7972bb92019-11-13 15:59:24 -0800604 if (failedItems.find(ii) != failedItems.end())
605 {
606 // if we failed to read it once, unlikely we can read it later
607 continue;
608 }
609
610 if (rootFailures != nullptr)
611 {
612 if (rootFailures->find(ii) != rootFailures->end())
613 {
614 continue;
615 }
616 }
617
Vijay Khemka2d681f62018-11-06 15:51:00 -0800618 /* Check for Device type if it is 8 bit or 16 bit */
619 int flag = isDevice16Bit(file);
620 if (flag < 0)
621 {
622 std::cerr << "failed to read bus " << bus << " address " << ii
623 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700624 if (powerIsOn)
625 {
626 failedItems.insert(ii);
627 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800628 continue;
629 }
630
Patrick Venturebaba7672019-10-26 09:26:41 -0700631 std::string errorMessage =
632 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300633 std::vector<uint8_t> device =
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300634 readFRUContents(flag, file, static_cast<uint16_t>(ii),
Xiang Liu2801a702020-01-20 14:29:34 -0800635 readBlockData, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700636 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700637 {
James Feist26c27ad2018-07-25 15:09:40 -0700638 continue;
639 }
James Feist26c27ad2018-07-25 15:09:40 -0700640
Patrick Venture786f1792019-08-05 16:33:44 -0700641 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800642 }
James Feist26c27ad2018-07-25 15:09:40 -0700643 return 1;
644 });
645 std::future_status status =
646 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
647 if (status == std::future_status::timeout)
648 {
649 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700650 if (powerIsOn)
651 {
652 busBlacklist.insert(bus);
653 }
James Feist444830e2019-04-05 08:38:16 -0700654 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700655 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800656 }
657
James Feist444830e2019-04-05 08:38:16 -0700658 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700659 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800660}
661
Patrick Venture11f1ff42019-08-01 10:42:12 -0700662void loadBlacklist(const char* path)
663{
664 std::ifstream blacklistStream(path);
665 if (!blacklistStream.good())
666 {
667 // File is optional.
668 std::cerr << "Cannot open blacklist file.\n\n";
669 return;
670 }
671
672 nlohmann::json data =
673 nlohmann::json::parse(blacklistStream, nullptr, false);
674 if (data.is_discarded())
675 {
676 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
677 "exiting\n";
678 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700679 }
680
681 // It's expected to have at least one field, "buses" that is an array of the
682 // buses by integer. Allow for future options to exclude further aspects,
683 // such as specific addresses or ranges.
684 if (data.type() != nlohmann::json::value_t::object)
685 {
686 std::cerr << "Illegal blacklist, expected to read dictionary\n";
687 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700688 }
689
690 // If buses field is missing, that's fine.
691 if (data.count("buses") == 1)
692 {
693 // Parse the buses array after a little validation.
694 auto buses = data.at("buses");
695 if (buses.type() != nlohmann::json::value_t::array)
696 {
697 // Buses field present but invalid, therefore this is an error.
698 std::cerr << "Invalid contents for blacklist buses field\n";
699 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700700 }
701
702 // Catch exception here for type mis-match.
703 try
704 {
705 for (const auto& bus : buses)
706 {
707 busBlacklist.insert(bus.get<size_t>());
708 }
709 }
710 catch (const nlohmann::detail::type_error& e)
711 {
712 // Type mis-match is a critical error.
713 std::cerr << "Invalid bus type: " << e.what() << "\n";
714 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700715 }
716 }
717
718 return;
719}
720
James Feista465ccc2019-02-08 12:51:01 -0800721static void FindI2CDevices(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700722 BusMap& busmap)
James Feist3cb5fec2018-01-23 14:41:51 -0800723{
James Feista465ccc2019-02-08 12:51:01 -0800724 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800725 {
James Feist5d7f4692020-06-17 18:22:33 -0700726 int bus = busStrToInt(i2cBus);
James Feist0c3980a2019-12-19 11:09:27 -0800727
James Feist5d7f4692020-06-17 18:22:33 -0700728 if (bus < 0)
729 {
730 std::cerr << "Cannot translate " << i2cBus << " to int\n";
731 continue;
732 }
James Feist444830e2019-04-05 08:38:16 -0700733 if (busBlacklist.find(bus) != busBlacklist.end())
734 {
735 continue; // skip previously failed busses
736 }
James Feist3cb5fec2018-01-23 14:41:51 -0800737
James Feist0c3980a2019-12-19 11:09:27 -0800738 int rootBus = getRootBus(bus);
739 if (busBlacklist.find(rootBus) != busBlacklist.end())
740 {
741 continue;
742 }
743
James Feist3cb5fec2018-01-23 14:41:51 -0800744 auto file = open(i2cBus.c_str(), O_RDWR);
745 if (file < 0)
746 {
747 std::cerr << "unable to open i2c device " << i2cBus.string()
748 << "\n";
749 continue;
750 }
751 unsigned long funcs = 0;
752
753 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
754 {
755 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700756 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800757 << bus << "\n";
758 continue;
759 }
760 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
761 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
762 {
763 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
764 << bus << "\n";
765 continue;
766 }
James Feist98132792019-07-09 13:29:09 -0700767 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800768 device = std::make_shared<DeviceMap>();
769
Nikhil Potaded8884f12019-03-27 13:27:13 -0700770 // i2cdetect by default uses the range 0x03 to 0x77, as
771 // this is what we have tested with, use this range. Could be
772 // changed in future.
773 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800774 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700775 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800776 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700777
778 // fd is closed in this function in case the bus locks up
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300779 getBusFRUs(file, 0x03, 0x77, bus, device);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700780
781 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800782 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700783 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800784 }
James Feist3cb5fec2018-01-23 14:41:51 -0800785 }
James Feist3cb5fec2018-01-23 14:41:51 -0800786}
787
James Feist6ebf9de2018-05-15 15:01:17 -0700788// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700789struct FindDevicesWithCallback :
790 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700791{
James Feista465ccc2019-02-08 12:51:01 -0800792 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
James Feist5cb06082019-10-24 17:11:13 -0700793 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800794 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700795 _i2cBuses(i2cBuses),
James Feist5cb06082019-10-24 17:11:13 -0700796 _busMap(busmap), _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700797 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700798 ~FindDevicesWithCallback()
799 {
800 _callback();
801 }
802 void run()
803 {
James Feist98132792019-07-09 13:29:09 -0700804 FindI2CDevices(_i2cBuses, _busMap);
James Feist6ebf9de2018-05-15 15:01:17 -0700805 }
806
James Feista465ccc2019-02-08 12:51:01 -0800807 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800808 BusMap& _busMap;
James Feist6ebf9de2018-05-15 15:01:17 -0700809 std::function<void(void)> _callback;
810};
811
James Feist3cb5fec2018-01-23 14:41:51 -0800812static const std::tm intelEpoch(void)
813{
James Feist98132792019-07-09 13:29:09 -0700814 std::tm val = {};
James Feist3cb5fec2018-01-23 14:41:51 -0800815 val.tm_year = 1996 - 1900;
816 return val;
817}
818
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +0800819static char sixBitToChar(uint8_t val)
820{
821 return static_cast<char>((val & 0x3f) + ' ');
822}
823
824/* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */
825static const char bcdHighChars[] = {
826 ' ', '-', '.', 'X', 'X', 'X',
827};
828
829static char bcdPlusToChar(uint8_t val)
830{
831 val &= 0xf;
832 return (val < 10) ? static_cast<char>(val + '0') : bcdHighChars[val - 10];
833}
834
835enum class DecodeState
836{
837 ok,
838 end,
839 err,
840};
841
842enum FRUDataEncoding
843{
844 binary = 0x0,
845 bcdPlus = 0x1,
846 sixBitASCII = 0x2,
847 languageDependent = 0x3,
848};
849
850/* Decode FRU data into a std::string, given an input iterator and end. If the
851 * state returned is fruDataOk, then the resulting string is the decoded FRU
852 * data. The input iterator is advanced past the data consumed.
853 *
854 * On fruDataErr, we have lost synchronisation with the length bytes, so the
855 * iterator is no longer usable.
856 */
857static std::pair<DecodeState, std::string>
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300858 decodeFRUData(std::vector<uint8_t>::const_iterator& iter,
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300859 const std::vector<uint8_t>::const_iterator& end)
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +0800860{
861 std::string value;
862 unsigned int i;
863
864 /* we need at least one byte to decode the type/len header */
865 if (iter == end)
866 {
867 std::cerr << "Truncated FRU data\n";
868 return make_pair(DecodeState::err, value);
869 }
870
871 uint8_t c = *(iter++);
872
873 /* 0xc1 is the end marker */
874 if (c == 0xc1)
875 {
876 return make_pair(DecodeState::end, value);
877 }
878
879 /* decode type/len byte */
880 uint8_t type = static_cast<uint8_t>(c >> 6);
881 uint8_t len = static_cast<uint8_t>(c & 0x3f);
882
883 /* we should have at least len bytes of data available overall */
884 if (iter + len > end)
885 {
886 std::cerr << "FRU data field extends past end of FRU data\n";
887 return make_pair(DecodeState::err, value);
888 }
889
890 switch (type)
891 {
892 case FRUDataEncoding::binary:
Andrei Kartashevc994c022020-08-10 18:51:49 +0300893 {
894 std::stringstream ss;
895 ss << std::hex << std::setfill('0');
896 for (i = 0; i < len; i++, iter++)
897 {
898 uint8_t val = static_cast<uint8_t>(*iter);
899 ss << std::setw(2) << static_cast<int>(val);
900 }
901 value = ss.str();
902 break;
903 }
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +0800904 case FRUDataEncoding::languageDependent:
905 /* For language-code dependent encodings, assume 8-bit ASCII */
906 value = std::string(iter, iter + len);
907 iter += len;
908 break;
909
910 case FRUDataEncoding::bcdPlus:
911 value = std::string();
912 for (i = 0; i < len; i++, iter++)
913 {
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300914 uint8_t val = *iter;
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +0800915 value.push_back(bcdPlusToChar(val >> 4));
916 value.push_back(bcdPlusToChar(val & 0xf));
917 }
918 break;
919
920 case FRUDataEncoding::sixBitASCII:
921 {
Andrei Kartasheve707de62020-08-10 18:33:29 +0300922 unsigned int accum = 0;
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +0800923 unsigned int accumBitLen = 0;
924 value = std::string();
925 for (i = 0; i < len; i++, iter++)
926 {
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300927 accum |= *iter << accumBitLen;
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +0800928 accumBitLen += 8;
929 while (accumBitLen >= 6)
930 {
931 value.push_back(sixBitToChar(accum & 0x3f));
932 accum >>= 6;
933 accumBitLen -= 6;
934 }
935 }
936 }
937 break;
938 }
939
940 return make_pair(DecodeState::ok, value);
941}
942
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300943bool formatFRU(const std::vector<uint8_t>& fruBytes,
James Feista465ccc2019-02-08 12:51:01 -0800944 boost::container::flat_map<std::string, std::string>& result)
James Feist3cb5fec2018-01-23 14:41:51 -0800945{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300946 if (fruBytes.size() <= fruBlockSize)
James Feist3cb5fec2018-01-23 14:41:51 -0800947 {
948 return false;
949 }
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300950 std::vector<uint8_t>::const_iterator fruAreaOffsetField = fruBytes.begin();
James Feist9eb0b582018-04-27 12:15:46 -0700951 result["Common_Format_Version"] =
James Feist3cb5fec2018-01-23 14:41:51 -0800952 std::to_string(static_cast<int>(*fruAreaOffsetField));
953
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300954 const std::vector<std::string>* fruAreaFieldNames;
James Feist3cb5fec2018-01-23 14:41:51 -0800955
James Feist0eb40352019-04-09 14:44:04 -0700956 for (const std::string& area : FRU_AREAS)
James Feist3cb5fec2018-01-23 14:41:51 -0800957 {
Patrick Venture4b7a7452019-10-28 08:41:02 -0700958 ++fruAreaOffsetField;
James Feist3cb5fec2018-01-23 14:41:51 -0800959 if (fruAreaOffsetField >= fruBytes.end())
960 {
961 return false;
962 }
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300963 size_t offset = (*fruAreaOffsetField) * fruBlockSize;
James Feist3cb5fec2018-01-23 14:41:51 -0800964
965 if (offset > 1)
966 {
967 // +2 to skip format and length
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300968 std::vector<uint8_t>::const_iterator fruBytesIter =
James Feist3cb5fec2018-01-23 14:41:51 -0800969 fruBytes.begin() + offset + 2;
970
971 if (fruBytesIter >= fruBytes.end())
972 {
973 return false;
974 }
975
976 if (area == "CHASSIS")
977 {
978 result["CHASSIS_TYPE"] =
979 std::to_string(static_cast<int>(*fruBytesIter));
980 fruBytesIter += 1;
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300981 fruAreaFieldNames = &CHASSIS_FRU_AREAS;
James Feist3cb5fec2018-01-23 14:41:51 -0800982 }
983 else if (area == "BOARD")
984 {
985 result["BOARD_LANGUAGE_CODE"] =
986 std::to_string(static_cast<int>(*fruBytesIter));
987 fruBytesIter += 1;
988 if (fruBytesIter >= fruBytes.end())
989 {
990 return false;
991 }
992
993 unsigned int minutes = *fruBytesIter |
994 *(fruBytesIter + 1) << 8 |
995 *(fruBytesIter + 2) << 16;
996 std::tm fruTime = intelEpoch();
Patrick Venturee0e6f5f2019-08-12 19:00:11 -0700997 std::time_t timeValue = std::mktime(&fruTime);
James Feist3cb5fec2018-01-23 14:41:51 -0800998 timeValue += minutes * 60;
Patrick Venturee0e6f5f2019-08-12 19:00:11 -0700999 fruTime = *std::gmtime(&timeValue);
James Feist3cb5fec2018-01-23 14:41:51 -08001000
Patrick Venturee0e6f5f2019-08-12 19:00:11 -07001001 // Tue Nov 20 23:08:00 2018
1002 char timeString[32] = {0};
1003 auto bytes = std::strftime(timeString, sizeof(timeString),
Patrick Venturefff050a2019-08-13 11:44:30 -07001004 "%Y-%m-%d - %H:%M:%S", &fruTime);
Patrick Venturee0e6f5f2019-08-12 19:00:11 -07001005 if (bytes == 0)
1006 {
1007 std::cerr << "invalid time string encountered\n";
1008 return false;
1009 }
1010
1011 result["BOARD_MANUFACTURE_DATE"] = std::string(timeString);
James Feist3cb5fec2018-01-23 14:41:51 -08001012 fruBytesIter += 3;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001013 fruAreaFieldNames = &BOARD_FRU_AREAS;
James Feist3cb5fec2018-01-23 14:41:51 -08001014 }
1015 else if (area == "PRODUCT")
1016 {
1017 result["PRODUCT_LANGUAGE_CODE"] =
1018 std::to_string(static_cast<int>(*fruBytesIter));
1019 fruBytesIter += 1;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001020 fruAreaFieldNames = &PRODUCT_FRU_AREAS;
James Feist3cb5fec2018-01-23 14:41:51 -08001021 }
1022 else
1023 {
1024 continue;
1025 }
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001026 for (auto& field : *fruAreaFieldNames)
James Feist3cb5fec2018-01-23 14:41:51 -08001027 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001028 auto res = decodeFRUData(fruBytesIter, fruBytes.end());
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001029 std::string name = area + "_" + field;
James Feist3cb5fec2018-01-23 14:41:51 -08001030
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001031 if (res.first == DecodeState::end)
Vijay Khemka5d5de442018-11-07 10:51:25 -08001032 {
1033 break;
1034 }
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001035 else if (res.first == DecodeState::err)
James Feist3cb5fec2018-01-23 14:41:51 -08001036 {
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001037 std::cerr << "Error while parsing " << name << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -08001038 return false;
1039 }
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001040
1041 std::string value = res.second;
James Feist3cb5fec2018-01-23 14:41:51 -08001042
Ed Tanous2147e672019-02-27 13:59:56 -08001043 // Strip non null characters from the end
1044 value.erase(std::find_if(value.rbegin(), value.rend(),
1045 [](char ch) { return ch != 0; })
1046 .base(),
1047 value.end());
1048
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +08001049 result[name] = std::move(value);
James Feist3cb5fec2018-01-23 14:41:51 -08001050 }
1051 }
1052 }
1053
1054 return true;
1055}
1056
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001057std::vector<uint8_t>& getFRUInfo(const uint8_t& bus, const uint8_t& address)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001058{
1059 auto deviceMap = busMap.find(bus);
1060 if (deviceMap == busMap.end())
1061 {
1062 throw std::invalid_argument("Invalid Bus.");
1063 }
1064 auto device = deviceMap->second->find(address);
1065 if (device == deviceMap->second->end())
1066 {
1067 throw std::invalid_argument("Invalid Address.");
1068 }
Andrei Kartashev1c5b7062020-08-19 14:47:30 +03001069 std::vector<uint8_t>& ret = device->second;
Nikhil Potaded8884f12019-03-27 13:27:13 -07001070
1071 return ret;
1072}
1073
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001074void AddFRUObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +03001075 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -08001076 boost::container::flat_map<
1077 std::pair<size_t, size_t>,
1078 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -07001079 uint32_t bus, uint32_t address)
James Feist3cb5fec2018-01-23 14:41:51 -08001080{
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001081 boost::container::flat_map<std::string, std::string> formattedFRU;
1082 if (!formatFRU(device, formattedFRU))
James Feist3cb5fec2018-01-23 14:41:51 -08001083 {
Patrick Ventureb755c832019-08-07 11:09:14 -07001084 std::cerr << "failed to format fru for device at bus " << bus
1085 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -08001086 return;
1087 }
Patrick Venture96cdaef2019-07-30 13:30:52 -07001088
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001089 auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -08001090 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -07001091 // Not found under Board section or an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001092 if (productNameFind == formattedFRU.end() ||
Patrick Venture96cdaef2019-07-30 13:30:52 -07001093 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -08001094 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001095 productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -08001096 }
Patrick Venture96cdaef2019-07-30 13:30:52 -07001097 // Found under Product section and not an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001098 if (productNameFind != formattedFRU.end() &&
Patrick Venture96cdaef2019-07-30 13:30:52 -07001099 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -08001100 {
1101 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -08001102 std::regex illegalObject("[^A-Za-z0-9_]");
1103 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -08001104 }
1105 else
1106 {
1107 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
1108 UNKNOWN_BUS_OBJECT_COUNT++;
1109 }
1110
James Feist918e18c2018-02-13 15:51:07 -08001111 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -08001112 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -07001113 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -08001114 {
Patrick Venture015fb0a2019-08-16 09:33:31 -07001115 int highest = -1;
1116 bool found = false;
1117
James Feista465ccc2019-02-08 12:51:01 -08001118 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -08001119 {
Patrick Venture015fb0a2019-08-16 09:33:31 -07001120 std::string path = busIface.second->get_object_path();
1121 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
James Feist918e18c2018-02-13 15:51:07 -08001122 {
Nikhil Potaded8884f12019-03-27 13:27:13 -07001123 if (isMuxBus(bus) && address == busIface.first.second &&
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001124 (getFRUInfo(static_cast<uint8_t>(busIface.first.first),
James Feist98132792019-07-09 13:29:09 -07001125 static_cast<uint8_t>(busIface.first.second)) ==
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001126 getFRUInfo(static_cast<uint8_t>(bus),
James Feist98132792019-07-09 13:29:09 -07001127 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -07001128 {
Nikhil Potaded8884f12019-03-27 13:27:13 -07001129 // This device is already added to the lower numbered bus,
1130 // do not replicate it.
1131 return;
James Feist79e9c0b2018-03-15 15:21:17 -07001132 }
Patrick Venture015fb0a2019-08-16 09:33:31 -07001133
1134 // Check if the match named has extra information.
1135 found = true;
1136 std::smatch base_match;
1137
1138 bool match = std::regex_match(
1139 path, base_match, std::regex(productName + "_(\\d+)$"));
1140 if (match)
James Feist79e9c0b2018-03-15 15:21:17 -07001141 {
Patrick Venture015fb0a2019-08-16 09:33:31 -07001142 if (base_match.size() == 2)
1143 {
1144 std::ssub_match base_sub_match = base_match[1];
1145 std::string base = base_sub_match.str();
1146
1147 int value = std::stoi(base);
1148 highest = (value > highest) ? value : highest;
1149 }
James Feist79e9c0b2018-03-15 15:21:17 -07001150 }
James Feist918e18c2018-02-13 15:51:07 -08001151 }
Patrick Venture015fb0a2019-08-16 09:33:31 -07001152 } // end searching objects
1153
1154 if (found)
1155 {
1156 // We found something with the same name. If highest was still -1,
1157 // it means this new entry will be _0.
1158 productName += "_";
1159 productName += std::to_string(++highest);
James Feist918e18c2018-02-13 15:51:07 -08001160 }
1161 }
James Feist3cb5fec2018-01-23 14:41:51 -08001162
James Feist9eb0b582018-04-27 12:15:46 -07001163 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1164 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
1165 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
1166
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001167 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -08001168 {
James Feist9eb0b582018-04-27 12:15:46 -07001169
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -07001170 std::regex_replace(property.second.begin(), property.second.begin(),
1171 property.second.end(), NON_ASCII_REGEX, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +05301172 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -07001173 {
1174 continue;
1175 }
1176 std::string key =
1177 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +05301178
1179 if (property.first == "PRODUCT_ASSET_TAG")
1180 {
1181 std::string propertyName = property.first;
1182 iface->register_property(
1183 key, property.second + '\0',
1184 [bus, address, propertyName,
1185 &dbusInterfaceMap](const std::string& req, std::string& resp) {
1186 if (strcmp(req.c_str(), resp.c_str()))
1187 {
1188 // call the method which will update
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001189 if (updateFRUProperty(req, bus, address, propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301190 dbusInterfaceMap))
1191 {
1192 resp = req;
1193 }
1194 else
1195 {
1196 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001197 "FRU property update failed.");
Joshi-Mansid73b7442020-03-27 19:10:21 +05301198 }
1199 }
1200 return 1;
1201 });
1202 }
1203 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -07001204 {
1205 std::cerr << "illegal key: " << key << "\n";
1206 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -07001207 if (DEBUG)
1208 {
1209 std::cout << property.first << ": " << property.second << "\n";
1210 }
James Feist3cb5fec2018-01-23 14:41:51 -08001211 }
James Feist2a9d6db2018-04-27 15:48:28 -07001212
1213 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -07001214 iface->register_property("BUS", bus);
1215 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -07001216
James Feist9eb0b582018-04-27 12:15:46 -07001217 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001218}
1219
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001220static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -08001221{
1222 // try to read baseboard fru from file
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001223 std::ifstream baseboardFRUFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
1224 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -08001225 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001226 baseboardFRUFile.seekg(0, std::ios_base::end);
1227 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
1228 baseboardFRU.resize(fileSize);
1229 baseboardFRUFile.seekg(0, std::ios_base::beg);
1230 baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()),
Andrei Kartashev1c5b7062020-08-19 14:47:30 +03001231 fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -08001232 }
1233 else
1234 {
1235 return false;
1236 }
1237 return true;
1238}
1239
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001240bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -07001241{
1242 boost::container::flat_map<std::string, std::string> tmp;
1243 if (fru.size() > MAX_FRU_SIZE)
1244 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001245 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -07001246 return false;
1247 }
1248 // verify legal fru by running it through fru parsing logic
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001249 if (!formatFRU(fru, tmp))
James Feistb49ffc32018-05-02 11:10:43 -07001250 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001251 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -07001252 return false;
1253 }
1254 // baseboard fru
1255 if (bus == 0 && address == 0)
1256 {
1257 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
1258 if (!file.good())
1259 {
1260 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
1261 << "\n";
James Feistddb78302018-09-06 11:45:42 -07001262 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001263 return false;
1264 }
James Feista465ccc2019-02-08 12:51:01 -08001265 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -07001266 return file.good();
1267 }
1268 else
1269 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -07001270 if (hasEepromFile(bus, address))
1271 {
1272 auto path = getEepromPath(bus, address);
1273 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
1274 if (eeprom < 0)
1275 {
1276 std::cerr << "unable to open i2c device " << path << "\n";
1277 throw DBusInternalError();
1278 return false;
1279 }
1280
1281 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
1282 if (writtenBytes < 0)
1283 {
1284 std::cerr << "unable to write to i2c device " << path << "\n";
1285 close(eeprom);
1286 throw DBusInternalError();
1287 return false;
1288 }
1289
1290 close(eeprom);
1291 return true;
1292 }
1293
James Feistb49ffc32018-05-02 11:10:43 -07001294 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
1295
1296 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
1297 if (file < 0)
1298 {
1299 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -07001300 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001301 return false;
1302 }
1303 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
1304 {
1305 std::cerr << "unable to set device address\n";
1306 close(file);
James Feistddb78302018-09-06 11:45:42 -07001307 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001308 return false;
1309 }
1310
1311 constexpr const size_t RETRY_MAX = 2;
1312 uint16_t index = 0;
1313 size_t retries = RETRY_MAX;
1314 while (index < fru.size())
1315 {
1316 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
1317 (retries == RETRY_MAX))
1318 {
1319 // The 4K EEPROM only uses the A2 and A1 device address bits
1320 // with the third bit being a memory page address bit.
1321 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
1322 {
1323 std::cerr << "unable to set device address\n";
1324 close(file);
James Feistddb78302018-09-06 11:45:42 -07001325 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001326 return false;
1327 }
1328 }
1329
James Feist98132792019-07-09 13:29:09 -07001330 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
1331 fru[index]) < 0)
James Feistb49ffc32018-05-02 11:10:43 -07001332 {
1333 if (!retries--)
1334 {
1335 std::cerr << "error writing fru: " << strerror(errno)
1336 << "\n";
1337 close(file);
James Feistddb78302018-09-06 11:45:42 -07001338 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001339 return false;
1340 }
1341 }
1342 else
1343 {
1344 retries = RETRY_MAX;
1345 index++;
1346 }
1347 // most eeproms require 5-10ms between writes
1348 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1349 }
1350 close(file);
1351 return true;
1352 }
1353}
1354
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001355void rescanOneBus(
1356 BusMap& busmap, uint8_t busNum,
1357 boost::container::flat_map<
1358 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -07001359 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
1360 bool dbusCall)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001361{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001362 for (auto& [pair, interface] : foundDevices)
1363 {
1364 if (pair.first == static_cast<size_t>(busNum))
1365 {
1366 objServer.remove_interface(interface);
1367 foundDevices.erase(pair);
1368 }
1369 }
1370
James Feist5d7f4692020-06-17 18:22:33 -07001371 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
1372 if (!fs::exists(busPath))
1373 {
1374 if (dbusCall)
1375 {
1376 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
1377 << "\n";
1378 throw std::invalid_argument("Invalid Bus.");
1379 }
1380 return;
1381 }
1382
1383 std::vector<fs::path> i2cBuses;
1384 i2cBuses.emplace_back(busPath);
1385
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001386 auto scan = std::make_shared<FindDevicesWithCallback>(
1387 i2cBuses, busmap, [busNum, &busmap, &dbusInterfaceMap]() {
1388 for (auto& busIface : dbusInterfaceMap)
1389 {
1390 if (busIface.first.first == static_cast<size_t>(busNum))
1391 {
1392 objServer.remove_interface(busIface.second);
1393 }
1394 }
Wludzik, Jozefc1136b72020-06-24 11:58:32 +02001395 auto found = busmap.find(busNum);
1396 if (found == busmap.end() || found->second == nullptr)
1397 {
1398 return;
1399 }
1400 for (auto& device : *(found->second))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001401 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001402 AddFRUObjectToDbus(device.second, dbusInterfaceMap,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001403 static_cast<uint32_t>(busNum), device.first);
1404 }
1405 });
1406 scan->run();
1407}
1408
James Feist9eb0b582018-04-27 12:15:46 -07001409void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -07001410 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -08001411 boost::container::flat_map<
1412 std::pair<size_t, size_t>,
James Feist5cb06082019-10-24 17:11:13 -07001413 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -08001414{
James Feist6ebf9de2018-05-15 15:01:17 -07001415 static boost::asio::deadline_timer timer(io);
1416 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001417
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001418 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -07001419 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -08001420 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001421 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001422
Nikhil Potaded8884f12019-03-27 13:27:13 -07001423 boost::container::flat_map<size_t, fs::path> busPaths;
1424 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001425 {
James Feist4131aea2018-03-09 09:47:30 -08001426 std::cerr << "unable to find i2c devices\n";
1427 return;
James Feist918e18c2018-02-13 15:51:07 -08001428 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001429
1430 for (auto busPath : busPaths)
1431 {
1432 i2cBuses.emplace_back(busPath.second);
1433 }
James Feist4131aea2018-03-09 09:47:30 -08001434
James Feist98132792019-07-09 13:29:09 -07001435 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001436 for (auto& [pair, interface] : foundDevices)
1437 {
1438 objServer.remove_interface(interface);
1439 }
1440 foundDevices.clear();
1441
James Feist5cb06082019-10-24 17:11:13 -07001442 auto scan =
1443 std::make_shared<FindDevicesWithCallback>(i2cBuses, busmap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001444 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001445 {
1446 objServer.remove_interface(busIface.second);
1447 }
James Feist4131aea2018-03-09 09:47:30 -08001448
James Feist6ebf9de2018-05-15 15:01:17 -07001449 dbusInterfaceMap.clear();
1450 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -08001451
James Feist6ebf9de2018-05-15 15:01:17 -07001452 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001453 std::vector<uint8_t> baseboardFRU;
1454 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001455 {
Andrei Kartashev1c5b7062020-08-19 14:47:30 +03001456 boost::container::flat_map<int, std::vector<uint8_t>>
James Feist6ebf9de2018-05-15 15:01:17 -07001457 baseboardDev;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001458 baseboardDev.emplace(0, baseboardFRU);
James Feist98132792019-07-09 13:29:09 -07001459 busmap[0] = std::make_shared<DeviceMap>(baseboardDev);
James Feist6ebf9de2018-05-15 15:01:17 -07001460 }
James Feist98132792019-07-09 13:29:09 -07001461 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001462 {
James Feista465ccc2019-02-08 12:51:01 -08001463 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001464 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001465 AddFRUObjectToDbus(device.second, dbusInterfaceMap,
James Feist5cb06082019-10-24 17:11:13 -07001466 devicemap.first, device.first);
James Feist6ebf9de2018-05-15 15:01:17 -07001467 }
1468 }
1469 });
1470 scan->run();
1471 });
James Feist918e18c2018-02-13 15:51:07 -08001472}
1473
Joshi-Mansid73b7442020-03-27 19:10:21 +05301474// Calculate new checksum for fru info area
1475size_t calculateChecksum(std::vector<uint8_t>& fruAreaData)
1476{
1477 constexpr int checksumMod = 256;
1478 constexpr uint8_t modVal = 0xFF;
1479 int sum = std::accumulate(fruAreaData.begin(), fruAreaData.end(), 0);
1480 return (checksumMod - sum) & modVal;
1481}
1482
1483// Update new fru area length &
1484// Update checksum at new checksum location
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001485static void updateFRUAreaLenAndChecksum(std::vector<uint8_t>& fruData,
1486 size_t fruAreaStart,
1487 size_t fruAreaEndOfFieldsOffset)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301488{
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001489 size_t traverseFRUAreaIndex = fruAreaEndOfFieldsOffset - fruAreaStart;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301490
1491 // fill zeros for any remaining unused space
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001492 std::fill(fruData.begin() + fruAreaEndOfFieldsOffset, fruData.end(), 0);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301493
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001494 size_t mod = traverseFRUAreaIndex % fruBlockSize;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301495 size_t checksumLoc;
1496 if (!mod)
1497 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001498 traverseFRUAreaIndex += (fruBlockSize);
1499 checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - 1);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301500 }
1501 else
1502 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001503 traverseFRUAreaIndex += (fruBlockSize - mod);
1504 checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - mod - 1);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301505 }
1506
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001507 size_t newFRUAreaLen = (traverseFRUAreaIndex / fruBlockSize) +
1508 ((traverseFRUAreaIndex % fruBlockSize) != 0);
1509 size_t fruAreaLengthLoc = fruAreaStart + 1;
1510 fruData[fruAreaLengthLoc] = static_cast<uint8_t>(newFRUAreaLen);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301511
1512 // Calculate new checksum
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001513 std::vector<uint8_t> finalFRUData;
1514 std::copy_n(fruData.begin() + fruAreaStart, checksumLoc - fruAreaStart,
1515 std::back_inserter(finalFRUData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301516
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001517 size_t checksumVal = calculateChecksum(finalFRUData);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301518
1519 fruData[checksumLoc] = static_cast<uint8_t>(checksumVal);
1520}
1521
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001522static size_t getFieldLength(uint8_t fruFieldTypeLenValue)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301523{
1524 constexpr uint8_t typeLenMask = 0x3F;
1525 return fruFieldTypeLenValue & typeLenMask;
1526}
1527
1528// Details with example of Asset Tag Update
1529// To find location of Product Info Area asset tag as per FRU specification
1530// 1. Find product Info area starting offset (*8 - as header will be in
1531// multiple of 8 bytes).
1532// 2. Skip 3 bytes of product info area (like format version, area length,
1533// and language code).
1534// 3. Traverse manufacturer name, product name, product version, & product
1535// serial number, by reading type/length code to reach the Asset Tag.
1536// 4. Update the Asset Tag, reposition the product Info area in multiple of
1537// 8 bytes. Update the Product area length and checksum.
1538
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001539bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301540 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
1541 std::string propertyName,
1542 boost::container::flat_map<
1543 std::pair<size_t, size_t>,
1544 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap)
1545{
1546 size_t updatePropertyReqLen = updatePropertyReq.length();
1547 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1548 {
1549 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001550 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301551 "Invalid Length "
1552 << updatePropertyReqLen << "\n";
1553 return false;
1554 }
1555
1556 std::vector<uint8_t> fruData;
1557 try
1558 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001559 fruData = getFRUInfo(static_cast<uint8_t>(bus),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301560 static_cast<uint8_t>(address));
1561 }
1562 catch (std::invalid_argument& e)
1563 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001564 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301565 return false;
1566 }
1567
1568 if (fruData.empty())
1569 {
1570 return false;
1571 }
1572
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001573 const std::vector<std::string>* fruAreaFieldNames;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301574
Joshi-Mansid73b7442020-03-27 19:10:21 +05301575 uint8_t fruAreaOffsetFieldValue = 1;
1576 size_t offset = 0;
1577
1578 if (propertyName.find("CHASSIS") == 0)
1579 {
1580 constexpr size_t fruAreaOffsetField = 2;
1581 fruAreaOffsetFieldValue = fruData[fruAreaOffsetField];
1582
1583 offset = 3; // chassis part number offset. Skip fixed first 3 bytes
1584
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001585 fruAreaFieldNames = &CHASSIS_FRU_AREAS;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301586 }
1587 else if (propertyName.find("BOARD") == 0)
1588 {
1589 constexpr size_t fruAreaOffsetField = 3;
1590 fruAreaOffsetFieldValue = fruData[fruAreaOffsetField];
1591
1592 offset = 6; // board manufacturer offset. Skip fixed first 6 bytes
1593
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001594 fruAreaFieldNames = &BOARD_FRU_AREAS;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301595 }
1596 else if (propertyName.find("PRODUCT") == 0)
1597 {
1598 constexpr size_t fruAreaOffsetField = 4;
1599 fruAreaOffsetFieldValue = fruData[fruAreaOffsetField];
1600
1601 offset = 3;
1602 // Manufacturer name offset. Skip fixed first 3 product fru bytes i.e.
1603 // version, area length and language code
1604
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001605 fruAreaFieldNames = &PRODUCT_FRU_AREAS;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301606 }
1607 else
1608 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001609 std::cerr << "ProperyName doesn't exist in FRU Area Vector \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301610 return false;
1611 }
1612
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001613 size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize;
1614 size_t fruDataIter = fruAreaStart + offset;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301615 size_t fruUpdateFieldLoc = 0;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001616 size_t fieldLength;
1617 size_t skipToFRUUpdateField = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301618
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001619 for (auto& field : *fruAreaFieldNames)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301620 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001621 skipToFRUUpdateField++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301622 if (propertyName.find(field) != std::string::npos)
1623 {
1624 break;
1625 }
1626 }
1627
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001628 for (size_t i = 1; i < skipToFRUUpdateField; i++)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301629 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001630 fieldLength = getFieldLength(fruData[fruDataIter]);
1631 fruUpdateFieldLoc = fruDataIter + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301632 fruDataIter = ++fruUpdateFieldLoc;
1633 }
1634
1635 // Push post update fru field bytes to a vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001636 fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]);
1637 size_t restFRUFieldsLoc = fruUpdateFieldLoc + fieldLength;
1638 restFRUFieldsLoc++;
1639 skipToFRUUpdateField++;
1640 fruDataIter = restFRUFieldsLoc;
1641 size_t endOfFieldsLoc = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301642 size_t fruFieldLoc = 0;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001643 constexpr uint8_t endOfFields = 0xC1;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301644 for (size_t i = fruDataIter; i < fruData.size(); i++)
1645 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001646 fieldLength = getFieldLength(fruData[fruDataIter]);
1647 fruFieldLoc = fruDataIter + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301648 fruDataIter = ++fruFieldLoc;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001649 if (fruData[fruDataIter] == endOfFields)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301650 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001651 endOfFieldsLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301652 break;
1653 }
1654 }
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001655 endOfFieldsLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301656
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001657 std::vector<uint8_t> restFRUAreaFieldsData;
1658 std::copy_n(fruData.begin() + restFRUFieldsLoc,
1659 endOfFieldsLoc - restFRUFieldsLoc,
1660 std::back_inserter(restFRUAreaFieldsData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301661
1662 // write new requested property field length and data
1663 constexpr uint8_t newTypeLenMask = 0xC0;
1664 fruData[fruUpdateFieldLoc] =
1665 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
1666 fruUpdateFieldLoc++;
1667 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
1668 fruData.begin() + fruUpdateFieldLoc);
1669
1670 // Copy remaining data to main fru area - post updated fru field vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001671 restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen;
1672 size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size();
1673 if (fruAreaDataEnd > fruData.size())
Joshi-Mansid73b7442020-03-27 19:10:21 +05301674 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001675 std::cerr << "FRU area offset: " << fruAreaDataEnd
1676 << "should not be greater than FRU data size: "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301677 << fruData.size() << std::endl;
1678 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001679 "FRU area offset should not be greater than FRU data size");
Joshi-Mansid73b7442020-03-27 19:10:21 +05301680 }
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001681 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
1682 fruData.begin() + restFRUFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301683
1684 // Update final fru with new fru area length and checksum
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001685 updateFRUAreaLenAndChecksum(fruData, fruAreaStart, fruAreaDataEnd);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301686
1687 if (fruData.empty())
1688 {
1689 return false;
1690 }
1691
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001692 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301693 fruData))
1694 {
1695 return false;
1696 }
1697
1698 // Rescan the bus so that GetRawFru dbus-call fetches updated values
1699 rescanBusses(busMap, dbusInterfaceMap);
1700 return true;
1701}
1702
James Feist98132792019-07-09 13:29:09 -07001703int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001704{
1705 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001706 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001707 std::vector<fs::path> i2cBuses;
1708
James Feista3c180a2018-08-09 16:06:04 -07001709 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001710 {
1711 std::cerr << "unable to find i2c devices\n";
1712 return 1;
1713 }
James Feist3cb5fec2018-01-23 14:41:51 -08001714
Patrick Venture11f1ff42019-08-01 10:42:12 -07001715 // check for and load blacklist with initial buses.
1716 loadBlacklist(blacklistPath);
1717
Vijay Khemka065f6d92018-12-18 10:37:47 -08001718 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001719
James Feist6ebf9de2018-05-15 15:01:17 -07001720 // this is a map with keys of pair(bus number, address) and values of
1721 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001722 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001723 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1724 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001725
James Feist9eb0b582018-04-27 12:15:46 -07001726 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1727 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1728 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001729
James Feist5cb06082019-10-24 17:11:13 -07001730 iface->register_method("ReScan",
1731 [&]() { rescanBusses(busMap, dbusInterfaceMap); });
James Feist2a9d6db2018-04-27 15:48:28 -07001732
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001733 iface->register_method("ReScanBus", [&](uint8_t bus) {
James Feist5d7f4692020-06-17 18:22:33 -07001734 rescanOneBus(busMap, bus, dbusInterfaceMap, true);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001735 });
1736
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001737 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001738
1739 iface->register_method("WriteFru", [&](const uint8_t bus,
1740 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001741 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001742 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001743 {
James Feistddb78302018-09-06 11:45:42 -07001744 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001745 return;
1746 }
1747 // schedule rescan on success
James Feist5cb06082019-10-24 17:11:13 -07001748 rescanBusses(busMap, dbusInterfaceMap);
James Feistb49ffc32018-05-02 11:10:43 -07001749 });
James Feist9eb0b582018-04-27 12:15:46 -07001750 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001751
James Feist9eb0b582018-04-27 12:15:46 -07001752 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -08001753 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -08001754 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001755 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001756 std::string,
1757 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001758 values;
1759 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001760 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001761 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001762 {
James Feistcb5661d2020-06-12 15:05:01 -07001763 powerIsOn = boost::ends_with(
1764 std::get<std::string>(findState->second), "Running");
James Feist0eeb79c2019-10-09 13:35:29 -07001765 }
James Feist6ebf9de2018-05-15 15:01:17 -07001766
James Feistcb5661d2020-06-12 15:05:01 -07001767 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001768 {
James Feist5cb06082019-10-24 17:11:13 -07001769 rescanBusses(busMap, dbusInterfaceMap);
James Feist918e18c2018-02-13 15:51:07 -08001770 }
James Feist918e18c2018-02-13 15:51:07 -08001771 };
James Feist9eb0b582018-04-27 12:15:46 -07001772
1773 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -08001774 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001775 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001776 "openbmc_project/state/"
1777 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001778 eventHandler);
1779
James Feist4131aea2018-03-09 09:47:30 -08001780 int fd = inotify_init();
James Feist0eb40352019-04-09 14:44:04 -07001781 inotify_add_watch(fd, I2C_DEV_LOCATION,
1782 IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -08001783 std::array<char, 4096> readBuffer;
1784 std::string pendingBuffer;
1785 // monitor for new i2c devices
1786 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1787 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001788 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -08001789 std::size_t bytes_transferred) {
1790 if (ec)
1791 {
1792 std::cout << "Callback Error " << ec << "\n";
1793 return;
1794 }
1795 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
James Feist4131aea2018-03-09 09:47:30 -08001796 while (pendingBuffer.size() > sizeof(inotify_event))
1797 {
James Feista465ccc2019-02-08 12:51:01 -08001798 const inotify_event* iEvent =
1799 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -08001800 pendingBuffer.data());
1801 switch (iEvent->mask)
1802 {
James Feist9eb0b582018-04-27 12:15:46 -07001803 case IN_CREATE:
1804 case IN_MOVED_TO:
1805 case IN_DELETE:
James Feist5d7f4692020-06-17 18:22:33 -07001806 std::string name(iEvent->name);
1807 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001808 {
James Feist5d7f4692020-06-17 18:22:33 -07001809 int bus = busStrToInt(name);
1810 if (bus < 0)
1811 {
1812 std::cerr << "Could not parse bus " << name
1813 << "\n";
1814 continue;
1815 }
1816 rescanOneBus(busMap, static_cast<uint8_t>(bus),
1817 dbusInterfaceMap, false);
James Feist9eb0b582018-04-27 12:15:46 -07001818 }
James Feist4131aea2018-03-09 09:47:30 -08001819 }
1820
1821 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
1822 }
James Feist6ebf9de2018-05-15 15:01:17 -07001823
James Feist4131aea2018-03-09 09:47:30 -08001824 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1825 watchI2cBusses);
1826 };
1827
1828 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001829 // run the initial scan
James Feist5cb06082019-10-24 17:11:13 -07001830 rescanBusses(busMap, dbusInterfaceMap);
James Feist918e18c2018-02-13 15:51:07 -08001831
James Feist3cb5fec2018-01-23 14:41:51 -08001832 io.run();
1833 return 0;
1834}