blob: 1c49974a4ddf65c3acf87a97e3e3ba41cb0dac4f [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 Ventureab296412020-12-30 13:39:37 -080018#include "FruUtils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070019#include "Utils.hpp"
20
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>
Ed Tanous07d467b2021-02-23 14:48:37 -080034#include <cerrno>
James Feist3b860982018-10-02 14:34:07 -070035#include <chrono>
36#include <ctime>
Patrick Venturee3754002019-08-06 09:39:12 -070037#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080038#include <fstream>
Patrick Venturebaba7672019-10-26 09:26:41 -070039#include <functional>
James Feist3cb5fec2018-01-23 14:41:51 -080040#include <future>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070041#include <iomanip>
James Feist3cb5fec2018-01-23 14:41:51 -080042#include <iostream>
Patrick Venturee26395d2019-10-29 14:05:16 -070043#include <limits>
Andrew Jefferya9c58922021-06-01 09:28:59 +093044#include <map>
James Feist3f8a2782018-02-12 09:24:42 -080045#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070046#include <set>
47#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070048#include <string>
James Feist3b860982018-10-02 14:34:07 -070049#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080050#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080051#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070052#include <vector>
James Feist3b860982018-10-02 14:34:07 -070053
James Feist8c505da2020-05-28 10:06:33 -070054extern "C"
55{
James Feist3b860982018-10-02 14:34:07 -070056#include <i2c/smbus.h>
57#include <linux/i2c-dev.h>
58}
James Feist3cb5fec2018-01-23 14:41:51 -080059
Ed Tanous072e25d2018-12-16 21:45:20 -080060namespace fs = std::filesystem;
Ed Tanous07d467b2021-02-23 14:48:37 -080061static constexpr bool debug = false;
Ed Tanous07d467b2021-02-23 14:48:37 -080062constexpr size_t maxFruSize = 512;
63constexpr size_t maxEepromPageIndex = 255;
James Feist26c27ad2018-07-25 15:09:40 -070064constexpr size_t busTimeoutSeconds = 5;
James Feist3cb5fec2018-01-23 14:41:51 -080065
Patrick Venture11f1ff42019-08-01 10:42:12 -070066constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
67
Ed Tanous07d467b2021-02-23 14:48:37 -080068const static constexpr char* baseboardFruLocation =
James Feist3cb5fec2018-01-23 14:41:51 -080069 "/etc/fru/baseboard.fru.bin";
70
Ed Tanous07d467b2021-02-23 14:48:37 -080071const static constexpr char* i2CDevLocation = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080072
James Feist444830e2019-04-05 08:38:16 -070073static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070074struct FindDevicesWithCallback;
75
James Feist8a983922019-10-24 17:11:56 -070076static boost::container::flat_map<
77 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
78 foundDevices;
79
James Feist7972bb92019-11-13 15:59:24 -080080static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
81
James Feist5cb06082019-10-24 17:11:13 -070082boost::asio::io_service io;
James Feist5cb06082019-10-24 17:11:13 -070083
Andrei Kartashev2a7e3952020-09-02 20:32:26 +030084uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter,
85 std::vector<uint8_t>::const_iterator end);
Andrei Kartashev2f0de172020-08-13 14:29:40 +030086bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +053087 const std::string& assetTag, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -080088 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +053089 boost::container::flat_map<
90 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +053091 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -080092 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +053093 sdbusplus::asio::object_server& objServer,
94 std::shared_ptr<sdbusplus::asio::connection>& systemBus);
Patrick Venturebaba7672019-10-26 09:26:41 -070095
Patrick Venturec50e1ff2019-08-06 10:22:28 -070096// Given a bus/address, produce the path in sysfs for an eeprom.
97static std::string getEepromPath(size_t bus, size_t address)
98{
99 std::stringstream output;
100 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
101 << std::setfill('0') << std::setw(4) << std::hex << address
102 << "/eeprom";
103 return output.str();
104}
105
106static bool hasEepromFile(size_t bus, size_t address)
107{
108 auto path = getEepromPath(bus, address);
109 try
110 {
111 return fs::exists(path);
112 }
113 catch (...)
114 {
115 return false;
116 }
117}
118
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700119static int64_t readFromEeprom(int flag __attribute__((unused)), int fd,
Xiang Liu2801a702020-01-20 14:29:34 -0800120 uint16_t address __attribute__((unused)),
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700121 uint16_t offset, uint8_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700122{
123 auto result = lseek(fd, offset, SEEK_SET);
124 if (result < 0)
125 {
126 std::cerr << "failed to seek\n";
127 return -1;
128 }
129
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700130 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700131}
132
James Feist5d7f4692020-06-17 18:22:33 -0700133static int busStrToInt(const std::string& busName)
134{
Ed Tanous07d467b2021-02-23 14:48:37 -0800135 auto findBus = busName.rfind('-');
James Feist5d7f4692020-06-17 18:22:33 -0700136 if (findBus == std::string::npos)
137 {
138 return -1;
139 }
140 return std::stoi(busName.substr(findBus + 1));
141}
142
James Feist7972bb92019-11-13 15:59:24 -0800143static int getRootBus(size_t bus)
144{
145 auto ec = std::error_code();
146 auto path = std::filesystem::read_symlink(
147 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
148 std::to_string(bus) + "/mux_device"),
149 ec);
150 if (ec)
151 {
152 return -1;
153 }
154
155 std::string filename = path.filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800156 auto findBus = filename.find('-');
James Feist7972bb92019-11-13 15:59:24 -0800157 if (findBus == std::string::npos)
158 {
159 return -1;
160 }
161 return std::stoi(filename.substr(0, findBus));
162}
163
James Feistc95cb142018-02-26 10:41:42 -0800164static bool isMuxBus(size_t bus)
165{
Ed Tanous072e25d2018-12-16 21:45:20 -0800166 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800167 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
168}
169
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530170static void makeProbeInterface(size_t bus, size_t address,
171 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700172{
173 if (isMuxBus(bus))
174 {
175 return; // the mux buses are random, no need to publish
176 }
177 auto [it, success] = foundDevices.emplace(
178 std::make_pair(bus, address),
179 objServer.add_interface(
180 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
181 std::to_string(address),
182 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
183 if (!success)
184 {
185 return; // already added
186 }
187 it->second->register_property("Bus", bus);
188 it->second->register_property("Address", address);
189 it->second->initialize();
190}
191
Vijay Khemka2d681f62018-11-06 15:51:00 -0800192static int isDevice16Bit(int file)
193{
194 /* Get first byte */
195 int byte1 = i2c_smbus_read_byte_data(file, 0);
196 if (byte1 < 0)
197 {
198 return byte1;
199 }
200 /* Read 7 more bytes, it will read same first byte in case of
201 * 8 bit but it will read next byte in case of 16 bit
202 */
203 for (int i = 0; i < 7; i++)
204 {
205 int byte2 = i2c_smbus_read_byte_data(file, 0);
206 if (byte2 < 0)
207 {
208 return byte2;
209 }
210 if (byte2 != byte1)
211 {
212 return 1;
213 }
214 }
215 return 0;
216}
217
Xiang Liu2801a702020-01-20 14:29:34 -0800218// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
219// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800220static int i2cSmbusWriteThenRead(int file, uint16_t address,
221 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
222 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800223{
Ed Tanous07d467b2021-02-23 14:48:37 -0800224 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
225 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800226 {
227 return -1;
228 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800229
Xiang Liu2801a702020-01-20 14:29:34 -0800230#define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2
231 struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT];
232 struct i2c_rdwr_ioctl_data rdwr;
233
234 msgs[0].addr = address;
235 msgs[0].flags = 0;
236 msgs[0].len = toSlaveBufLen;
237 msgs[0].buf = toSlaveBuf;
238 msgs[1].addr = address;
239 msgs[1].flags = I2C_M_RD;
240 msgs[1].len = fromSlaveBufLen;
241 msgs[1].buf = fromSlaveBuf;
242
243 rdwr.msgs = msgs;
244 rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT;
245
246 int ret = ioctl(file, I2C_RDWR, &rdwr);
247
Zev Weiss07108282022-03-01 18:51:31 -0800248 return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800249}
250
251static int64_t readBlockData(int flag, int file, uint16_t address,
252 uint16_t offset, uint8_t len, uint8_t* buf)
253{
Vijay Khemka2d681f62018-11-06 15:51:00 -0800254 if (flag == 0)
255 {
Xiang Liu2801a702020-01-20 14:29:34 -0800256 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
257 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800258 }
259
Xiang Liu2801a702020-01-20 14:29:34 -0800260 offset = htobe16(offset);
Ed Tanous07d467b2021-02-23 14:48:37 -0800261 return i2cSmbusWriteThenRead(
Xiang Liu2801a702020-01-20 14:29:34 -0800262 file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800263}
264
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700265// TODO: This code is very similar to the non-eeprom version and can be merged
266// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300267static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700268{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700269 auto path = getEepromPath(bus, address);
270
271 int file = open(path.c_str(), O_RDONLY);
272 if (file < 0)
273 {
274 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700275 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700276 }
277
Patrick Venturebaba7672019-10-26 09:26:41 -0700278 std::string errorMessage = "eeprom at " + std::to_string(bus) +
279 " address " + std::to_string(address);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300280 std::vector<uint8_t> device = readFRUContents(
Xiang Liu2801a702020-01-20 14:29:34 -0800281 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700282
283 close(file);
284 return device;
285}
286
Ed Tanous07d467b2021-02-23 14:48:37 -0800287std::set<int> findI2CEeproms(int i2cBus,
288 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700289{
290 std::set<int> foundList;
291
292 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
293
294 // For each file listed under the i2c device
295 // NOTE: This should be faster than just checking for each possible address
296 // path.
297 for (const auto& p : fs::directory_iterator(path))
298 {
299 const std::string node = p.path().string();
300 std::smatch m;
301 bool found =
302 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
303
304 if (!found)
305 {
306 continue;
307 }
308 if (m.size() != 2)
309 {
310 std::cerr << "regex didn't capture\n";
311 continue;
312 }
313
314 std::ssub_match subMatch = m[1];
315 std::string addressString = subMatch.str();
316
317 std::size_t ignored;
318 const int hexBase = 16;
319 int address = std::stoi(addressString, &ignored, hexBase);
320
321 const std::string eeprom = node + "/eeprom";
322
323 try
324 {
325 if (!fs::exists(eeprom))
326 {
327 continue;
328 }
329 }
330 catch (...)
331 {
332 continue;
333 }
334
335 // There is an eeprom file at this address, it may have invalid
336 // contents, but we found it.
337 foundList.insert(address);
338
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300339 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700340 if (!device.empty())
341 {
342 devices->emplace(address, device);
343 }
344 }
345
346 return foundList;
347}
348
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300349int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800350 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530351 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800352{
James Feist3cb5fec2018-01-23 14:41:51 -0800353
James Feist26c27ad2018-07-25 15:09:40 -0700354 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700355 // NOTE: When reading the devices raw on the bus, it can interfere with
356 // the driver's ability to operate, therefore read eeproms first before
357 // scanning for devices without drivers. Several experiments were run
358 // and it was determined that if there were any devices on the bus
359 // before the eeprom was hit and read, the eeprom driver wouldn't open
360 // while the bus device was open. An experiment was not performed to see
361 // if this issue was resolved if the i2c bus device was closed, but
362 // hexdumps of the eeprom later were successful.
363
364 // Scan for i2c eeproms loaded on this bus.
365 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800366 std::set<size_t>& failedItems = failedAddresses[bus];
367
368 std::set<size_t>* rootFailures = nullptr;
369 int rootBus = getRootBus(bus);
370
371 if (rootBus >= 0)
372 {
373 rootFailures = &(failedAddresses[rootBus]);
374 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700375
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530376 constexpr int startSkipSlaveAddr = 0;
377 constexpr int endSkipSlaveAddr = 12;
378
James Feist26c27ad2018-07-25 15:09:40 -0700379 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800380 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700381 if (skipList.find(ii) != skipList.end())
382 {
383 continue;
384 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530385 // skipping since no device is present in this range
386 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
387 {
388 continue;
389 }
James Feist26c27ad2018-07-25 15:09:40 -0700390 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530391 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800392 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300393 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700394 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700395 continue;
396 }
397 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800398 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700399 {
400 continue;
401 }
James Feist3cb5fec2018-01-23 14:41:51 -0800402
Ed Tanous07d467b2021-02-23 14:48:37 -0800403 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700404 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700405 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700406 << "\n";
407 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800408
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530409 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700410
James Feist7972bb92019-11-13 15:59:24 -0800411 if (failedItems.find(ii) != failedItems.end())
412 {
413 // if we failed to read it once, unlikely we can read it later
414 continue;
415 }
416
417 if (rootFailures != nullptr)
418 {
419 if (rootFailures->find(ii) != rootFailures->end())
420 {
421 continue;
422 }
423 }
424
Vijay Khemka2d681f62018-11-06 15:51:00 -0800425 /* Check for Device type if it is 8 bit or 16 bit */
426 int flag = isDevice16Bit(file);
427 if (flag < 0)
428 {
429 std::cerr << "failed to read bus " << bus << " address " << ii
430 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700431 if (powerIsOn)
432 {
433 failedItems.insert(ii);
434 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800435 continue;
436 }
437
Patrick Venturebaba7672019-10-26 09:26:41 -0700438 std::string errorMessage =
439 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300440 std::vector<uint8_t> device =
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300441 readFRUContents(flag, file, static_cast<uint16_t>(ii),
Xiang Liu2801a702020-01-20 14:29:34 -0800442 readBlockData, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700443 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700444 {
James Feist26c27ad2018-07-25 15:09:40 -0700445 continue;
446 }
James Feist26c27ad2018-07-25 15:09:40 -0700447
Patrick Venture786f1792019-08-05 16:33:44 -0700448 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800449 }
James Feist26c27ad2018-07-25 15:09:40 -0700450 return 1;
451 });
452 std::future_status status =
453 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
454 if (status == std::future_status::timeout)
455 {
456 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700457 if (powerIsOn)
458 {
459 busBlacklist.insert(bus);
460 }
James Feist444830e2019-04-05 08:38:16 -0700461 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700462 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800463 }
464
James Feist444830e2019-04-05 08:38:16 -0700465 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700466 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800467}
468
Patrick Venture11f1ff42019-08-01 10:42:12 -0700469void loadBlacklist(const char* path)
470{
471 std::ifstream blacklistStream(path);
472 if (!blacklistStream.good())
473 {
474 // File is optional.
475 std::cerr << "Cannot open blacklist file.\n\n";
476 return;
477 }
478
479 nlohmann::json data =
480 nlohmann::json::parse(blacklistStream, nullptr, false);
481 if (data.is_discarded())
482 {
483 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
484 "exiting\n";
485 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700486 }
487
488 // It's expected to have at least one field, "buses" that is an array of the
489 // buses by integer. Allow for future options to exclude further aspects,
490 // such as specific addresses or ranges.
491 if (data.type() != nlohmann::json::value_t::object)
492 {
493 std::cerr << "Illegal blacklist, expected to read dictionary\n";
494 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700495 }
496
497 // If buses field is missing, that's fine.
498 if (data.count("buses") == 1)
499 {
500 // Parse the buses array after a little validation.
501 auto buses = data.at("buses");
502 if (buses.type() != nlohmann::json::value_t::array)
503 {
504 // Buses field present but invalid, therefore this is an error.
505 std::cerr << "Invalid contents for blacklist buses field\n";
506 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700507 }
508
509 // Catch exception here for type mis-match.
510 try
511 {
512 for (const auto& bus : buses)
513 {
514 busBlacklist.insert(bus.get<size_t>());
515 }
516 }
517 catch (const nlohmann::detail::type_error& e)
518 {
519 // Type mis-match is a critical error.
520 std::cerr << "Invalid bus type: " << e.what() << "\n";
521 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700522 }
523 }
524
525 return;
526}
527
Ed Tanous07d467b2021-02-23 14:48:37 -0800528static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800529 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530530 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800531{
James Feista465ccc2019-02-08 12:51:01 -0800532 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800533 {
James Feist5d7f4692020-06-17 18:22:33 -0700534 int bus = busStrToInt(i2cBus);
James Feist0c3980a2019-12-19 11:09:27 -0800535
James Feist5d7f4692020-06-17 18:22:33 -0700536 if (bus < 0)
537 {
538 std::cerr << "Cannot translate " << i2cBus << " to int\n";
539 continue;
540 }
James Feist444830e2019-04-05 08:38:16 -0700541 if (busBlacklist.find(bus) != busBlacklist.end())
542 {
543 continue; // skip previously failed busses
544 }
James Feist3cb5fec2018-01-23 14:41:51 -0800545
James Feist0c3980a2019-12-19 11:09:27 -0800546 int rootBus = getRootBus(bus);
547 if (busBlacklist.find(rootBus) != busBlacklist.end())
548 {
549 continue;
550 }
551
James Feist3cb5fec2018-01-23 14:41:51 -0800552 auto file = open(i2cBus.c_str(), O_RDWR);
553 if (file < 0)
554 {
555 std::cerr << "unable to open i2c device " << i2cBus.string()
556 << "\n";
557 continue;
558 }
559 unsigned long funcs = 0;
560
561 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
562 {
563 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700564 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800565 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700566 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800567 continue;
568 }
569 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
570 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
571 {
572 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
573 << bus << "\n";
574 continue;
575 }
James Feist98132792019-07-09 13:29:09 -0700576 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800577 device = std::make_shared<DeviceMap>();
578
Nikhil Potaded8884f12019-03-27 13:27:13 -0700579 // i2cdetect by default uses the range 0x03 to 0x77, as
580 // this is what we have tested with, use this range. Could be
581 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800582 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800583 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700584 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800585 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700586
587 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530588 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700589
Ed Tanous07d467b2021-02-23 14:48:37 -0800590 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800591 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700592 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800593 }
James Feist3cb5fec2018-01-23 14:41:51 -0800594 }
James Feist3cb5fec2018-01-23 14:41:51 -0800595}
596
James Feist6ebf9de2018-05-15 15:01:17 -0700597// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700598struct FindDevicesWithCallback :
599 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700600{
James Feista465ccc2019-02-08 12:51:01 -0800601 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800602 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530603 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800604 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700605 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530606 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
607 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700608 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700609 ~FindDevicesWithCallback()
610 {
611 _callback();
612 }
613 void run()
614 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530615 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700616 }
617
James Feista465ccc2019-02-08 12:51:01 -0800618 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800619 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800620 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530621 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700622 std::function<void(void)> _callback;
623};
624
Ed Tanous07d467b2021-02-23 14:48:37 -0800625void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300626 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800627 boost::container::flat_map<
628 std::pair<size_t, size_t>,
629 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530630 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800631 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530632 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800633{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300634 boost::container::flat_map<std::string, std::string> formattedFRU;
Michael Shen0961b112022-02-22 11:06:33 +0800635 resCodes res = formatIPMIFRU(device, formattedFRU);
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300636 if (res == resCodes::resErr)
James Feist3cb5fec2018-01-23 14:41:51 -0800637 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300638 std::cerr << "failed to parse FRU for device at bus " << bus
Patrick Ventureb755c832019-08-07 11:09:14 -0700639 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800640 return;
641 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800642 if (res == resCodes::resWarn)
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300643 {
644 std::cerr << "there were warnings while parsing FRU for device at bus "
645 << bus << " address " << address << "\n";
646 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700647
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300648 auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800649 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700650 // Not found under Board section or an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300651 if (productNameFind == formattedFRU.end() ||
Patrick Venture96cdaef2019-07-30 13:30:52 -0700652 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800653 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300654 productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800655 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700656 // Found under Product section and not an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300657 if (productNameFind != formattedFRU.end() &&
Patrick Venture96cdaef2019-07-30 13:30:52 -0700658 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800659 {
660 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800661 std::regex illegalObject("[^A-Za-z0-9_]");
662 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800663 }
664 else
665 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800666 productName = "UNKNOWN" + std::to_string(unknownBusObjectCount);
667 unknownBusObjectCount++;
James Feist3cb5fec2018-01-23 14:41:51 -0800668 }
669
James Feist918e18c2018-02-13 15:51:07 -0800670 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800671 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700672 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800673 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700674 int highest = -1;
675 bool found = false;
676
James Feista465ccc2019-02-08 12:51:01 -0800677 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800678 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700679 std::string path = busIface.second->get_object_path();
680 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
James Feist918e18c2018-02-13 15:51:07 -0800681 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700682 if (isMuxBus(bus) && address == busIface.first.second &&
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300683 (getFRUInfo(static_cast<uint8_t>(busIface.first.first),
James Feist98132792019-07-09 13:29:09 -0700684 static_cast<uint8_t>(busIface.first.second)) ==
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300685 getFRUInfo(static_cast<uint8_t>(bus),
James Feist98132792019-07-09 13:29:09 -0700686 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700687 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700688 // This device is already added to the lower numbered bus,
689 // do not replicate it.
690 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700691 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700692
693 // Check if the match named has extra information.
694 found = true;
Ed Tanous07d467b2021-02-23 14:48:37 -0800695 std::smatch baseMatch;
Patrick Venture015fb0a2019-08-16 09:33:31 -0700696
697 bool match = std::regex_match(
Ed Tanous07d467b2021-02-23 14:48:37 -0800698 path, baseMatch, std::regex(productName + "_(\\d+)$"));
Patrick Venture015fb0a2019-08-16 09:33:31 -0700699 if (match)
James Feist79e9c0b2018-03-15 15:21:17 -0700700 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800701 if (baseMatch.size() == 2)
Patrick Venture015fb0a2019-08-16 09:33:31 -0700702 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800703 std::ssub_match baseSubMatch = baseMatch[1];
704 std::string base = baseSubMatch.str();
Patrick Venture015fb0a2019-08-16 09:33:31 -0700705
706 int value = std::stoi(base);
707 highest = (value > highest) ? value : highest;
708 }
James Feist79e9c0b2018-03-15 15:21:17 -0700709 }
James Feist918e18c2018-02-13 15:51:07 -0800710 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700711 } // end searching objects
712
713 if (found)
714 {
715 // We found something with the same name. If highest was still -1,
716 // it means this new entry will be _0.
717 productName += "_";
718 productName += std::to_string(++highest);
James Feist918e18c2018-02-13 15:51:07 -0800719 }
720 }
James Feist3cb5fec2018-01-23 14:41:51 -0800721
James Feist9eb0b582018-04-27 12:15:46 -0700722 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
723 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
724 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
725
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300726 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800727 {
James Feist9eb0b582018-04-27 12:15:46 -0700728
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700729 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800730 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530731 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700732 {
733 continue;
734 }
735 std::string key =
Ed Tanous07d467b2021-02-23 14:48:37 -0800736 std::regex_replace(property.first, nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530737
738 if (property.first == "PRODUCT_ASSET_TAG")
739 {
740 std::string propertyName = property.first;
741 iface->register_property(
742 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530743 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530744 &unknownBusObjectCount, &powerIsOn, &objServer,
745 &systemBus](const std::string& req, std::string& resp) {
Ed Tanous07d467b2021-02-23 14:48:37 -0800746 if (strcmp(req.c_str(), resp.c_str()) != 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +0530747 {
748 // call the method which will update
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300749 if (updateFRUProperty(req, bus, address, propertyName,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530750 dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530751 unknownBusObjectCount, powerIsOn,
752 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530753 {
754 resp = req;
755 }
756 else
757 {
758 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300759 "FRU property update failed.");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530760 }
761 }
762 return 1;
763 });
764 }
765 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700766 {
767 std::cerr << "illegal key: " << key << "\n";
768 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800769 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700770 {
771 std::cout << property.first << ": " << property.second << "\n";
772 }
James Feist3cb5fec2018-01-23 14:41:51 -0800773 }
James Feist2a9d6db2018-04-27 15:48:28 -0700774
775 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700776 iface->register_property("BUS", bus);
777 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700778
James Feist9eb0b582018-04-27 12:15:46 -0700779 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800780}
781
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300782static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800783{
784 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800785 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300786 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800787 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300788 baseboardFRUFile.seekg(0, std::ios_base::end);
789 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
790 baseboardFRU.resize(fileSize);
791 baseboardFRUFile.seekg(0, std::ios_base::beg);
792 baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()),
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300793 fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800794 }
795 else
796 {
797 return false;
798 }
799 return true;
800}
801
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300802bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700803{
804 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800805 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700806 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300807 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700808 return false;
809 }
810 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800811 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700812 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300813 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700814 return false;
815 }
816 // baseboard fru
817 if (bus == 0 && address == 0)
818 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800819 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700820 if (!file.good())
821 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800822 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700823 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700824 return false;
825 }
James Feista465ccc2019-02-08 12:51:01 -0800826 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700827 return file.good();
828 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800829
830 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700831 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800832 auto path = getEepromPath(bus, address);
833 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
834 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700835 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800836 std::cerr << "unable to open i2c device " << path << "\n";
837 throw DBusInternalError();
838 return false;
839 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700840
Ed Tanous07d467b2021-02-23 14:48:37 -0800841 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
842 if (writtenBytes < 0)
843 {
844 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700845 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700846 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700847 return false;
848 }
849
Ed Tanous07d467b2021-02-23 14:48:37 -0800850 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700851 return true;
852 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800853
854 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
855
856 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
857 if (file < 0)
858 {
859 std::cerr << "unable to open i2c device " << i2cBus << "\n";
860 throw DBusInternalError();
861 return false;
862 }
863 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
864 {
865 std::cerr << "unable to set device address\n";
866 close(file);
867 throw DBusInternalError();
868 return false;
869 }
870
871 constexpr const size_t retryMax = 2;
872 uint16_t index = 0;
873 size_t retries = retryMax;
874 while (index < fru.size())
875 {
876 if ((index && ((index % (maxEepromPageIndex + 1)) == 0)) &&
877 (retries == retryMax))
878 {
879 // The 4K EEPROM only uses the A2 and A1 device address bits
880 // with the third bit being a memory page address bit.
881 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
882 {
883 std::cerr << "unable to set device address\n";
884 close(file);
885 throw DBusInternalError();
886 return false;
887 }
888 }
889
890 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
891 fru[index]) < 0)
892 {
893 if (!retries--)
894 {
895 std::cerr << "error writing fru: " << strerror(errno) << "\n";
896 close(file);
897 throw DBusInternalError();
898 return false;
899 }
900 }
901 else
902 {
903 retries = retryMax;
904 index++;
905 }
906 // most eeproms require 5-10ms between writes
907 std::this_thread::sleep_for(std::chrono::milliseconds(10));
908 }
909 close(file);
910 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700911}
912
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800913void rescanOneBus(
914 BusMap& busmap, uint8_t busNum,
915 boost::container::flat_map<
916 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700917 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800918 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530919 sdbusplus::asio::object_server& objServer,
920 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800921{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800922 for (auto& [pair, interface] : foundDevices)
923 {
924 if (pair.first == static_cast<size_t>(busNum))
925 {
926 objServer.remove_interface(interface);
927 foundDevices.erase(pair);
928 }
929 }
930
James Feist5d7f4692020-06-17 18:22:33 -0700931 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
932 if (!fs::exists(busPath))
933 {
934 if (dbusCall)
935 {
936 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
937 << "\n";
938 throw std::invalid_argument("Invalid Bus.");
939 }
940 return;
941 }
942
943 std::vector<fs::path> i2cBuses;
944 i2cBuses.emplace_back(busPath);
945
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800946 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530947 i2cBuses, busmap, powerIsOn, objServer,
948 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
949 &objServer, &systemBus]() {
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800950 for (auto& busIface : dbusInterfaceMap)
951 {
952 if (busIface.first.first == static_cast<size_t>(busNum))
953 {
954 objServer.remove_interface(busIface.second);
955 }
956 }
Wludzik, Jozefc1136b72020-06-24 11:58:32 +0200957 auto found = busmap.find(busNum);
958 if (found == busmap.end() || found->second == nullptr)
959 {
960 return;
961 }
962 for (auto& device : *(found->second))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800963 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800964 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530965 static_cast<uint32_t>(busNum), device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530966 unknownBusObjectCount, powerIsOn, objServer,
967 systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800968 }
969 });
970 scan->run();
971}
972
James Feist9eb0b582018-04-27 12:15:46 -0700973void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700974 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800975 boost::container::flat_map<
976 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530977 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800978 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530979 sdbusplus::asio::object_server& objServer,
980 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -0800981{
James Feist6ebf9de2018-05-15 15:01:17 -0700982 static boost::asio::deadline_timer timer(io);
983 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800984
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500985 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -0700986 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -0800987 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -0800988 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800989
Nikhil Potaded8884f12019-03-27 13:27:13 -0700990 boost::container::flat_map<size_t, fs::path> busPaths;
991 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -0800992 {
James Feist4131aea2018-03-09 09:47:30 -0800993 std::cerr << "unable to find i2c devices\n";
994 return;
James Feist918e18c2018-02-13 15:51:07 -0800995 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700996
Ed Tanous07d467b2021-02-23 14:48:37 -0800997 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -0700998 {
999 i2cBuses.emplace_back(busPath.second);
1000 }
James Feist4131aea2018-03-09 09:47:30 -08001001
James Feist98132792019-07-09 13:29:09 -07001002 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001003 for (auto& [pair, interface] : foundDevices)
1004 {
1005 objServer.remove_interface(interface);
1006 }
1007 foundDevices.clear();
1008
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301009 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301010 i2cBuses, busmap, powerIsOn, objServer, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001011 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001012 {
1013 objServer.remove_interface(busIface.second);
1014 }
James Feist4131aea2018-03-09 09:47:30 -08001015
James Feist6ebf9de2018-05-15 15:01:17 -07001016 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -08001017 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001018
James Feist6ebf9de2018-05-15 15:01:17 -07001019 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001020 std::vector<uint8_t> baseboardFRU;
1021 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001022 {
Helen Huangf69fe902021-02-19 16:18:22 +08001023 // If no device on i2c bus 0, the insertion will happen.
1024 auto bus0 =
1025 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1026 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001027 }
James Feist98132792019-07-09 13:29:09 -07001028 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001029 {
James Feista465ccc2019-02-08 12:51:01 -08001030 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001031 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001032 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301033 devicemap.first, device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301034 unknownBusObjectCount, powerIsOn,
1035 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001036 }
1037 }
1038 });
1039 scan->run();
1040 });
James Feist918e18c2018-02-13 15:51:07 -08001041}
1042
Joshi-Mansid73b7442020-03-27 19:10:21 +05301043// Details with example of Asset Tag Update
1044// To find location of Product Info Area asset tag as per FRU specification
1045// 1. Find product Info area starting offset (*8 - as header will be in
1046// multiple of 8 bytes).
1047// 2. Skip 3 bytes of product info area (like format version, area length,
1048// and language code).
1049// 3. Traverse manufacturer name, product name, product version, & product
1050// serial number, by reading type/length code to reach the Asset Tag.
1051// 4. Update the Asset Tag, reposition the product Info area in multiple of
1052// 8 bytes. Update the Product area length and checksum.
1053
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001054bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301055 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001056 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301057 boost::container::flat_map<
1058 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301059 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001060 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301061 sdbusplus::asio::object_server& objServer,
1062 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301063{
1064 size_t updatePropertyReqLen = updatePropertyReq.length();
1065 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1066 {
1067 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001068 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301069 "Invalid Length "
1070 << updatePropertyReqLen << "\n";
1071 return false;
1072 }
1073
1074 std::vector<uint8_t> fruData;
1075 try
1076 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001077 fruData = getFRUInfo(static_cast<uint8_t>(bus),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301078 static_cast<uint8_t>(address));
1079 }
Patrick Williams83b1e9b2021-10-06 12:47:24 -05001080 catch (const std::invalid_argument& e)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301081 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001082 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301083 return false;
1084 }
1085
1086 if (fruData.empty())
1087 {
1088 return false;
1089 }
1090
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001091 const std::vector<std::string>* fruAreaFieldNames;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301092
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001093 uint8_t fruAreaOffsetFieldValue = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301094 size_t offset = 0;
Ed Tanous07d467b2021-02-23 14:48:37 -08001095 std::string areaName = propertyName.substr(0, propertyName.find('_'));
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001096 std::string propertyNamePrefix = areaName + "_";
Ed Tanous07d467b2021-02-23 14:48:37 -08001097 auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName);
1098 if (it == fruAreaNames.end())
Joshi-Mansid73b7442020-03-27 19:10:21 +05301099 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001100 std::cerr << "Can't parse area name for property " << propertyName
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001101 << " \n";
1102 return false;
1103 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001104 fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin());
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001105 fruAreaOffsetFieldValue =
1106 fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)];
1107 switch (fruAreaToUpdate)
1108 {
1109 case fruAreas::fruAreaChassis:
1110 offset = 3; // chassis part number offset. Skip fixed first 3 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001111 fruAreaFieldNames = &chassisFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001112 break;
1113 case fruAreas::fruAreaBoard:
1114 offset = 6; // board manufacturer offset. Skip fixed first 6 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001115 fruAreaFieldNames = &boardFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001116 break;
1117 case fruAreas::fruAreaProduct:
1118 // Manufacturer name offset. Skip fixed first 3 product fru bytes
1119 // i.e. version, area length and language code
1120 offset = 3;
Ed Tanous07d467b2021-02-23 14:48:37 -08001121 fruAreaFieldNames = &productFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001122 break;
1123 default:
1124 std::cerr << "Don't know how to handle property " << propertyName
1125 << " \n";
1126 return false;
1127 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001128 if (fruAreaOffsetFieldValue == 0)
1129 {
1130 std::cerr << "FRU Area for " << propertyName << " not present \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301131 return false;
1132 }
1133
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001134 size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001135 size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize;
1136 size_t fruAreaEnd = fruAreaStart + fruAreaSize;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001137 size_t fruDataIter = fruAreaStart + offset;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001138 size_t skipToFRUUpdateField = 0;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001139 ssize_t fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301140
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001141 bool found = false;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001142 for (auto& field : *fruAreaFieldNames)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301143 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001144 skipToFRUUpdateField++;
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001145 if (propertyName == propertyNamePrefix + field)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301146 {
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001147 found = true;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301148 break;
1149 }
1150 }
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001151 if (!found)
1152 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001153 std::size_t pos = propertyName.find(fruCustomFieldName);
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001154 if (pos == std::string::npos)
1155 {
1156 std::cerr << "PropertyName doesn't exist in FRU Area Vectors: "
1157 << propertyName << "\n";
1158 return false;
1159 }
1160 std::string fieldNumStr =
Ed Tanous07d467b2021-02-23 14:48:37 -08001161 propertyName.substr(pos + fruCustomFieldName.length());
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001162 size_t fieldNum = std::stoi(fieldNumStr);
1163 if (fieldNum == 0)
1164 {
1165 std::cerr << "PropertyName not recognized: " << propertyName
1166 << "\n";
1167 return false;
1168 }
1169 skipToFRUUpdateField += fieldNum;
1170 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301171
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001172 for (size_t i = 1; i < skipToFRUUpdateField; i++)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301173 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001174 fieldLength = getFieldLength(fruData[fruDataIter]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001175 if (fieldLength < 0)
1176 {
1177 break;
1178 }
1179 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301180 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001181 size_t fruUpdateFieldLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301182
1183 // Push post update fru field bytes to a vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001184 fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001185 if (fieldLength < 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301186 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001187 std::cerr << "Property " << propertyName << " not present \n";
1188 return false;
1189 }
1190 fruDataIter += 1 + fieldLength;
1191 size_t restFRUFieldsLoc = fruDataIter;
1192 size_t endOfFieldsLoc = 0;
1193 while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0)
1194 {
1195 if (fruDataIter >= (fruAreaStart + fruAreaSize))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301196 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001197 fruDataIter = fruAreaStart + fruAreaSize;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301198 break;
1199 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001200 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301201 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001202 endOfFieldsLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301203
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001204 std::vector<uint8_t> restFRUAreaFieldsData;
1205 std::copy_n(fruData.begin() + restFRUFieldsLoc,
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001206 endOfFieldsLoc - restFRUFieldsLoc + 1,
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001207 std::back_inserter(restFRUAreaFieldsData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301208
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001209 // Push post update fru areas if any
1210 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001211 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1212 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001213 {
1214 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001215 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001216 if ((fruAreaLoc > endOfFieldsLoc) &&
1217 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1218 {
1219 nextFRUAreaLoc = fruAreaLoc;
1220 }
1221 }
1222 std::vector<uint8_t> restFRUAreasData;
1223 if (nextFRUAreaLoc)
1224 {
1225 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1226 fruData.size() - nextFRUAreaLoc,
1227 std::back_inserter(restFRUAreasData));
1228 }
1229
1230 // check FRU area size
1231 size_t fruAreaDataSize =
1232 ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size());
1233 size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize;
1234 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1235 {
1236
1237#ifdef ENABLE_FRU_AREA_RESIZE
1238 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1239 // round size to 8-byte blocks
1240 newFRUAreaSize =
1241 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
1242 size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize;
1243 fruData.resize(newFRUDataSize);
1244 fruAreaSize = newFRUAreaSize;
1245 fruAreaEnd = fruAreaStart + fruAreaSize;
1246#else
1247 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1248 << " should not be greater than available FRU area size: "
1249 << fruAreaAvailableSize << "\n";
1250 return false;
1251#endif // ENABLE_FRU_AREA_RESIZE
1252 }
1253
Joshi-Mansid73b7442020-03-27 19:10:21 +05301254 // write new requested property field length and data
1255 constexpr uint8_t newTypeLenMask = 0xC0;
1256 fruData[fruUpdateFieldLoc] =
1257 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
1258 fruUpdateFieldLoc++;
1259 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
1260 fruData.begin() + fruUpdateFieldLoc);
1261
1262 // Copy remaining data to main fru area - post updated fru field vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001263 restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen;
1264 size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size();
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001265 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
1266 fruData.begin() + restFRUFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301267
1268 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001269 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
1270 fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301271
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001272#ifdef ENABLE_FRU_AREA_RESIZE
1273 ++nextFRUAreaNewLoc;
1274 ssize_t nextFRUAreaOffsetDiff =
1275 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
1276 // Append rest FRU Areas if size changed and there were other sections after
1277 // updated one
1278 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1279 {
1280 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1281 fruData.begin() + nextFRUAreaNewLoc);
1282 // Update Common Header
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001283 for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord;
1284 fruArea++)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001285 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001286 unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001287 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
1288 if (curFRUAreaOffset > fruAreaOffsetFieldValue)
1289 {
1290 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1291 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1292 }
1293 }
1294 // Calculate new checksum
1295 std::vector<uint8_t> headerFRUData;
1296 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1297 size_t checksumVal = calculateChecksum(headerFRUData);
1298 fruData[7] = static_cast<uint8_t>(checksumVal);
1299 // fill zeros if FRU Area size decreased
1300 if (nextFRUAreaOffsetDiff < 0)
1301 {
1302 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1303 restFRUAreasData.size(),
1304 fruData.end(), 0);
1305 }
1306 }
1307#else
1308 // this is to avoid "unused variable" warning
1309 (void)nextFRUAreaNewLoc;
1310#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301311 if (fruData.empty())
1312 {
1313 return false;
1314 }
1315
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001316 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301317 fruData))
1318 {
1319 return false;
1320 }
1321
1322 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301323 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1324 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301325 return true;
1326}
1327
James Feist98132792019-07-09 13:29:09 -07001328int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001329{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301330 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1331 sdbusplus::asio::object_server objServer(systemBus);
1332
Kumar Thangavel41a90512021-11-24 15:44:20 +05301333 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301334 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001335 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001336 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001337 std::vector<fs::path> i2cBuses;
1338
James Feista3c180a2018-08-09 16:06:04 -07001339 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001340 {
1341 std::cerr << "unable to find i2c devices\n";
1342 return 1;
1343 }
James Feist3cb5fec2018-01-23 14:41:51 -08001344
Patrick Venture11f1ff42019-08-01 10:42:12 -07001345 // check for and load blacklist with initial buses.
1346 loadBlacklist(blacklistPath);
1347
Vijay Khemka065f6d92018-12-18 10:37:47 -08001348 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001349
James Feist6ebf9de2018-05-15 15:01:17 -07001350 // this is a map with keys of pair(bus number, address) and values of
1351 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001352 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001353 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1354 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001355
James Feist9eb0b582018-04-27 12:15:46 -07001356 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1357 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1358 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001359
Kumar Thangavel41a90512021-11-24 15:44:20 +05301360 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301361 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1362 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301363 });
James Feist2a9d6db2018-04-27 15:48:28 -07001364
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001365 iface->register_method("ReScanBus", [&](uint8_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301366 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301367 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001368 });
1369
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001370 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001371
1372 iface->register_method("WriteFru", [&](const uint8_t bus,
1373 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001374 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001375 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001376 {
James Feistddb78302018-09-06 11:45:42 -07001377 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001378 return;
1379 }
1380 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301381 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1382 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001383 });
James Feist9eb0b582018-04-27 12:15:46 -07001384 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001385
James Feist9eb0b582018-04-27 12:15:46 -07001386 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -08001387 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -08001388 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001389 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001390 std::string,
1391 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001392 values;
1393 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001394 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001395 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001396 {
James Feistcb5661d2020-06-12 15:05:01 -07001397 powerIsOn = boost::ends_with(
1398 std::get<std::string>(findState->second), "Running");
James Feist0eeb79c2019-10-09 13:35:29 -07001399 }
James Feist6ebf9de2018-05-15 15:01:17 -07001400
James Feistcb5661d2020-06-12 15:05:01 -07001401 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001402 {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301403 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301404 powerIsOn, objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001405 }
James Feist918e18c2018-02-13 15:51:07 -08001406 };
James Feist9eb0b582018-04-27 12:15:46 -07001407
1408 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -08001409 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001410 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001411 "openbmc_project/state/"
1412 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001413 eventHandler);
1414
James Feist4131aea2018-03-09 09:47:30 -08001415 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001416 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -08001417 std::array<char, 4096> readBuffer;
1418 std::string pendingBuffer;
1419 // monitor for new i2c devices
1420 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1421 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001422 watchI2cBusses = [&](const boost::system::error_code& ec,
Ed Tanous07d467b2021-02-23 14:48:37 -08001423 std::size_t bytesTransferred) {
James Feist4131aea2018-03-09 09:47:30 -08001424 if (ec)
1425 {
1426 std::cout << "Callback Error " << ec << "\n";
1427 return;
1428 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001429 pendingBuffer += std::string(readBuffer.data(), bytesTransferred);
James Feist4131aea2018-03-09 09:47:30 -08001430 while (pendingBuffer.size() > sizeof(inotify_event))
1431 {
James Feista465ccc2019-02-08 12:51:01 -08001432 const inotify_event* iEvent =
1433 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -08001434 pendingBuffer.data());
1435 switch (iEvent->mask)
1436 {
James Feist9eb0b582018-04-27 12:15:46 -07001437 case IN_CREATE:
1438 case IN_MOVED_TO:
1439 case IN_DELETE:
James Feist5d7f4692020-06-17 18:22:33 -07001440 std::string name(iEvent->name);
1441 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001442 {
James Feist5d7f4692020-06-17 18:22:33 -07001443 int bus = busStrToInt(name);
1444 if (bus < 0)
1445 {
1446 std::cerr << "Could not parse bus " << name
1447 << "\n";
1448 continue;
1449 }
1450 rescanOneBus(busMap, static_cast<uint8_t>(bus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301451 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301452 unknownBusObjectCount, powerIsOn,
1453 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001454 }
James Feist4131aea2018-03-09 09:47:30 -08001455 }
1456
1457 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
1458 }
James Feist6ebf9de2018-05-15 15:01:17 -07001459
James Feist4131aea2018-03-09 09:47:30 -08001460 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1461 watchI2cBusses);
1462 };
1463
1464 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001465 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301466 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1467 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001468
James Feist3cb5fec2018-01-23 14:41:51 -08001469 io.run();
1470 return 0;
1471}