blob: 446d5c4d1a2c5569c43884ee44c3aa91c4083a32 [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 Bishope45d8c72022-05-25 15:12:53 -040016/// \file fru_device.cpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
Brad Bishope45d8c72022-05-25 15:12:53 -040018#include "fru_utils.hpp"
19#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070020
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>
Ed Tanous3013fb42022-07-09 08:27:06 -070035#include <charconv>
James Feist3b860982018-10-02 14:34:07 -070036#include <chrono>
37#include <ctime>
Patrick Venturee3754002019-08-06 09:39:12 -070038#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080039#include <fstream>
Patrick Venturebaba7672019-10-26 09:26:41 -070040#include <functional>
James Feist3cb5fec2018-01-23 14:41:51 -080041#include <future>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070042#include <iomanip>
James Feist3cb5fec2018-01-23 14:41:51 -080043#include <iostream>
Patrick Venturee26395d2019-10-29 14:05:16 -070044#include <limits>
Andrew Jefferya9c58922021-06-01 09:28:59 +093045#include <map>
James Feist3f8a2782018-02-12 09:24:42 -080046#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070047#include <set>
48#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070049#include <string>
James Feist3b860982018-10-02 14:34:07 -070050#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080051#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080052#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070053#include <vector>
James Feist3b860982018-10-02 14:34:07 -070054
James Feist8c505da2020-05-28 10:06:33 -070055extern "C"
56{
James Feist3b860982018-10-02 14:34:07 -070057#include <i2c/smbus.h>
58#include <linux/i2c-dev.h>
59}
James Feist3cb5fec2018-01-23 14:41:51 -080060
Ed Tanous072e25d2018-12-16 21:45:20 -080061namespace fs = std::filesystem;
Ed Tanous07d467b2021-02-23 14:48:37 -080062static constexpr bool debug = false;
Ed Tanous07d467b2021-02-23 14:48:37 -080063constexpr size_t maxFruSize = 512;
64constexpr size_t maxEepromPageIndex = 255;
James Feist26c27ad2018-07-25 15:09:40 -070065constexpr size_t busTimeoutSeconds = 5;
James Feist3cb5fec2018-01-23 14:41:51 -080066
Patrick Venture11f1ff42019-08-01 10:42:12 -070067constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
68
Ed Tanous07d467b2021-02-23 14:48:37 -080069const static constexpr char* baseboardFruLocation =
James Feist3cb5fec2018-01-23 14:41:51 -080070 "/etc/fru/baseboard.fru.bin";
71
Ed Tanous07d467b2021-02-23 14:48:37 -080072const static constexpr char* i2CDevLocation = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080073
James Feist444830e2019-04-05 08:38:16 -070074static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070075struct FindDevicesWithCallback;
76
James Feist8a983922019-10-24 17:11:56 -070077static boost::container::flat_map<
78 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
79 foundDevices;
80
James Feist7972bb92019-11-13 15:59:24 -080081static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +000082static boost::container::flat_map<size_t, std::set<size_t>> fruAddresses;
James Feist7972bb92019-11-13 15:59:24 -080083
James Feist5cb06082019-10-24 17:11:13 -070084boost::asio::io_service io;
James Feist5cb06082019-10-24 17:11:13 -070085
Andrei Kartashev2f0de172020-08-13 14:29:40 +030086bool updateFRUProperty(
Ed Tanous3013fb42022-07-09 08:27:06 -070087 const std::string& updatePropertyReq, 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
Zev Weiss309c0b12022-02-25 01:44:12 +0000119static int64_t readFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700120{
121 auto result = lseek(fd, offset, SEEK_SET);
122 if (result < 0)
123 {
124 std::cerr << "failed to seek\n";
125 return -1;
126 }
127
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700128 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700129}
130
Ed Tanous3013fb42022-07-09 08:27:06 -0700131static int busStrToInt(const std::string_view busName)
James Feist5d7f4692020-06-17 18:22:33 -0700132{
Ed Tanous07d467b2021-02-23 14:48:37 -0800133 auto findBus = busName.rfind('-');
James Feist5d7f4692020-06-17 18:22:33 -0700134 if (findBus == std::string::npos)
135 {
136 return -1;
137 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700138 std::string_view num = busName.substr(findBus + 1);
139 int val = 0;
140 std::from_chars(num.data(), num.data() + num.size(), val);
141 return val;
James Feist5d7f4692020-06-17 18:22:33 -0700142}
143
James Feist7972bb92019-11-13 15:59:24 -0800144static int getRootBus(size_t bus)
145{
146 auto ec = std::error_code();
147 auto path = std::filesystem::read_symlink(
148 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
149 std::to_string(bus) + "/mux_device"),
150 ec);
151 if (ec)
152 {
153 return -1;
154 }
155
156 std::string filename = path.filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800157 auto findBus = filename.find('-');
James Feist7972bb92019-11-13 15:59:24 -0800158 if (findBus == std::string::npos)
159 {
160 return -1;
161 }
162 return std::stoi(filename.substr(0, findBus));
163}
164
James Feistc95cb142018-02-26 10:41:42 -0800165static bool isMuxBus(size_t bus)
166{
Ed Tanous072e25d2018-12-16 21:45:20 -0800167 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800168 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
169}
170
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530171static void makeProbeInterface(size_t bus, size_t address,
172 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700173{
174 if (isMuxBus(bus))
175 {
176 return; // the mux buses are random, no need to publish
177 }
178 auto [it, success] = foundDevices.emplace(
179 std::make_pair(bus, address),
180 objServer.add_interface(
181 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
182 std::to_string(address),
183 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
184 if (!success)
185 {
186 return; // already added
187 }
188 it->second->register_property("Bus", bus);
189 it->second->register_property("Address", address);
190 it->second->initialize();
191}
192
Zev Weiss309c0b12022-02-25 01:44:12 +0000193static std::optional<bool> isDevice16Bit(int file)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800194{
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700195 // Set the higher data word address bits to 0. It's safe on 8-bit addressing
196 // EEPROMs because it doesn't write any actual data.
197 int ret = i2c_smbus_write_byte(file, 0);
198 if (ret < 0)
199 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000200 return std::nullopt;
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700201 }
202
Vijay Khemka2d681f62018-11-06 15:51:00 -0800203 /* Get first byte */
204 int byte1 = i2c_smbus_read_byte_data(file, 0);
205 if (byte1 < 0)
206 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000207 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800208 }
209 /* Read 7 more bytes, it will read same first byte in case of
210 * 8 bit but it will read next byte in case of 16 bit
211 */
212 for (int i = 0; i < 7; i++)
213 {
214 int byte2 = i2c_smbus_read_byte_data(file, 0);
215 if (byte2 < 0)
216 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000217 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800218 }
219 if (byte2 != byte1)
220 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000221 return true;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800222 }
223 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000224 return false;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800225}
226
Xiang Liu2801a702020-01-20 14:29:34 -0800227// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
228// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800229static int i2cSmbusWriteThenRead(int file, uint16_t address,
230 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
231 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800232{
Ed Tanous07d467b2021-02-23 14:48:37 -0800233 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
234 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800235 {
236 return -1;
237 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800238
Ed Tanous3013fb42022-07-09 08:27:06 -0700239 constexpr size_t smbusWriteThenReadMsgCount = 2;
240 std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{};
241 struct i2c_rdwr_ioctl_data rdwr
242 {};
Xiang Liu2801a702020-01-20 14:29:34 -0800243
244 msgs[0].addr = address;
245 msgs[0].flags = 0;
246 msgs[0].len = toSlaveBufLen;
247 msgs[0].buf = toSlaveBuf;
248 msgs[1].addr = address;
249 msgs[1].flags = I2C_M_RD;
250 msgs[1].len = fromSlaveBufLen;
251 msgs[1].buf = fromSlaveBuf;
252
Ed Tanous3013fb42022-07-09 08:27:06 -0700253 rdwr.msgs = msgs.data();
254 rdwr.nmsgs = msgs.size();
Xiang Liu2801a702020-01-20 14:29:34 -0800255
256 int ret = ioctl(file, I2C_RDWR, &rdwr);
257
Jason M. Billsf6e4c1f2022-08-19 12:23:51 -0700258 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800259}
260
Zev Weiss309c0b12022-02-25 01:44:12 +0000261static int64_t readBlockData(bool is16bit, int file, uint16_t address,
262 off_t offset, size_t len, uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800263{
Zev Weiss309c0b12022-02-25 01:44:12 +0000264 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800265 {
Xiang Liu2801a702020-01-20 14:29:34 -0800266 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
267 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800268 }
269
Xiang Liu2801a702020-01-20 14:29:34 -0800270 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700271 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
272 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
273 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800274}
275
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700276// TODO: This code is very similar to the non-eeprom version and can be merged
277// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300278static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700279{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700280 auto path = getEepromPath(bus, address);
281
282 int file = open(path.c_str(), O_RDONLY);
283 if (file < 0)
284 {
285 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700286 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700287 }
288
Patrick Venturebaba7672019-10-26 09:26:41 -0700289 std::string errorMessage = "eeprom at " + std::to_string(bus) +
290 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000291 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
292 return readFromEeprom(file, offset, length, outbuf);
293 };
294 FRUReader reader(std::move(readFunc));
295 std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700296
297 close(file);
298 return device;
299}
300
Ed Tanous07d467b2021-02-23 14:48:37 -0800301std::set<int> findI2CEeproms(int i2cBus,
302 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700303{
304 std::set<int> foundList;
305
306 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
307
308 // For each file listed under the i2c device
309 // NOTE: This should be faster than just checking for each possible address
310 // path.
311 for (const auto& p : fs::directory_iterator(path))
312 {
313 const std::string node = p.path().string();
314 std::smatch m;
315 bool found =
316 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
317
318 if (!found)
319 {
320 continue;
321 }
322 if (m.size() != 2)
323 {
324 std::cerr << "regex didn't capture\n";
325 continue;
326 }
327
328 std::ssub_match subMatch = m[1];
329 std::string addressString = subMatch.str();
330
Ed Tanous3013fb42022-07-09 08:27:06 -0700331 std::size_t ignored = 0;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700332 const int hexBase = 16;
333 int address = std::stoi(addressString, &ignored, hexBase);
334
335 const std::string eeprom = node + "/eeprom";
336
337 try
338 {
339 if (!fs::exists(eeprom))
340 {
341 continue;
342 }
343 }
344 catch (...)
345 {
346 continue;
347 }
348
349 // There is an eeprom file at this address, it may have invalid
350 // contents, but we found it.
351 foundList.insert(address);
352
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300353 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700354 if (!device.empty())
355 {
356 devices->emplace(address, device);
357 }
358 }
359
360 return foundList;
361}
362
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300363int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800364 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530365 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800366{
James Feist3cb5fec2018-01-23 14:41:51 -0800367
James Feist26c27ad2018-07-25 15:09:40 -0700368 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700369 // NOTE: When reading the devices raw on the bus, it can interfere with
370 // the driver's ability to operate, therefore read eeproms first before
371 // scanning for devices without drivers. Several experiments were run
372 // and it was determined that if there were any devices on the bus
373 // before the eeprom was hit and read, the eeprom driver wouldn't open
374 // while the bus device was open. An experiment was not performed to see
375 // if this issue was resolved if the i2c bus device was closed, but
376 // hexdumps of the eeprom later were successful.
377
378 // Scan for i2c eeproms loaded on this bus.
379 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800380 std::set<size_t>& failedItems = failedAddresses[bus];
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000381 std::set<size_t>& foundItems = fruAddresses[bus];
382 foundItems.clear();
James Feist7972bb92019-11-13 15:59:24 -0800383
384 std::set<size_t>* rootFailures = nullptr;
385 int rootBus = getRootBus(bus);
386
387 if (rootBus >= 0)
388 {
389 rootFailures = &(failedAddresses[rootBus]);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000390 foundItems = fruAddresses[rootBus];
James Feist7972bb92019-11-13 15:59:24 -0800391 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700392
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530393 constexpr int startSkipSlaveAddr = 0;
394 constexpr int endSkipSlaveAddr = 12;
395
James Feist26c27ad2018-07-25 15:09:40 -0700396 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800397 {
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000398 if (foundItems.find(ii) != foundItems.end())
399 {
400 continue;
401 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700402 if (skipList.find(ii) != skipList.end())
403 {
404 continue;
405 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530406 // skipping since no device is present in this range
407 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
408 {
409 continue;
410 }
James Feist26c27ad2018-07-25 15:09:40 -0700411 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530412 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800413 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300414 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700415 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700416 continue;
417 }
418 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800419 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700420 {
421 continue;
422 }
James Feist3cb5fec2018-01-23 14:41:51 -0800423
Ed Tanous07d467b2021-02-23 14:48:37 -0800424 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700425 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700426 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700427 << "\n";
428 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800429
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530430 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700431
James Feist7972bb92019-11-13 15:59:24 -0800432 if (failedItems.find(ii) != failedItems.end())
433 {
434 // if we failed to read it once, unlikely we can read it later
435 continue;
436 }
437
438 if (rootFailures != nullptr)
439 {
440 if (rootFailures->find(ii) != rootFailures->end())
441 {
442 continue;
443 }
444 }
445
Vijay Khemka2d681f62018-11-06 15:51:00 -0800446 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000447 std::optional<bool> is16Bit = isDevice16Bit(file);
448 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800449 {
450 std::cerr << "failed to read bus " << bus << " address " << ii
451 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700452 if (powerIsOn)
453 {
454 failedItems.insert(ii);
455 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800456 continue;
457 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000458 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800459
Zev Weiss309c0b12022-02-25 01:44:12 +0000460 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
461 uint8_t* outbuf) {
462 return readBlockData(is16BitBool, file, ii, offset, length,
463 outbuf);
464 };
465 FRUReader reader(std::move(readFunc));
Patrick Venturebaba7672019-10-26 09:26:41 -0700466 std::string errorMessage =
467 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Zev Weiss309c0b12022-02-25 01:44:12 +0000468 std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700469 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700470 {
James Feist26c27ad2018-07-25 15:09:40 -0700471 continue;
472 }
James Feist26c27ad2018-07-25 15:09:40 -0700473
Patrick Venture786f1792019-08-05 16:33:44 -0700474 devices->emplace(ii, device);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000475 fruAddresses[bus].insert(ii);
James Feist3cb5fec2018-01-23 14:41:51 -0800476 }
James Feist26c27ad2018-07-25 15:09:40 -0700477 return 1;
478 });
479 std::future_status status =
480 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
481 if (status == std::future_status::timeout)
482 {
483 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700484 if (powerIsOn)
485 {
486 busBlacklist.insert(bus);
487 }
James Feist444830e2019-04-05 08:38:16 -0700488 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700489 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800490 }
491
James Feist444830e2019-04-05 08:38:16 -0700492 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700493 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800494}
495
Patrick Venture11f1ff42019-08-01 10:42:12 -0700496void loadBlacklist(const char* path)
497{
498 std::ifstream blacklistStream(path);
499 if (!blacklistStream.good())
500 {
501 // File is optional.
502 std::cerr << "Cannot open blacklist file.\n\n";
503 return;
504 }
505
506 nlohmann::json data =
507 nlohmann::json::parse(blacklistStream, nullptr, false);
508 if (data.is_discarded())
509 {
510 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
511 "exiting\n";
512 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700513 }
514
515 // It's expected to have at least one field, "buses" that is an array of the
516 // buses by integer. Allow for future options to exclude further aspects,
517 // such as specific addresses or ranges.
518 if (data.type() != nlohmann::json::value_t::object)
519 {
520 std::cerr << "Illegal blacklist, expected to read dictionary\n";
521 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700522 }
523
524 // If buses field is missing, that's fine.
525 if (data.count("buses") == 1)
526 {
527 // Parse the buses array after a little validation.
528 auto buses = data.at("buses");
529 if (buses.type() != nlohmann::json::value_t::array)
530 {
531 // Buses field present but invalid, therefore this is an error.
532 std::cerr << "Invalid contents for blacklist buses field\n";
533 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700534 }
535
536 // Catch exception here for type mis-match.
537 try
538 {
539 for (const auto& bus : buses)
540 {
541 busBlacklist.insert(bus.get<size_t>());
542 }
543 }
544 catch (const nlohmann::detail::type_error& e)
545 {
546 // Type mis-match is a critical error.
547 std::cerr << "Invalid bus type: " << e.what() << "\n";
548 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700549 }
550 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700551}
552
Ed Tanous07d467b2021-02-23 14:48:37 -0800553static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800554 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530555 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800556{
Ed Tanous3013fb42022-07-09 08:27:06 -0700557 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800558 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700559 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800560
James Feist5d7f4692020-06-17 18:22:33 -0700561 if (bus < 0)
562 {
563 std::cerr << "Cannot translate " << i2cBus << " to int\n";
564 continue;
565 }
James Feist444830e2019-04-05 08:38:16 -0700566 if (busBlacklist.find(bus) != busBlacklist.end())
567 {
568 continue; // skip previously failed busses
569 }
James Feist3cb5fec2018-01-23 14:41:51 -0800570
James Feist0c3980a2019-12-19 11:09:27 -0800571 int rootBus = getRootBus(bus);
572 if (busBlacklist.find(rootBus) != busBlacklist.end())
573 {
574 continue;
575 }
576
James Feist3cb5fec2018-01-23 14:41:51 -0800577 auto file = open(i2cBus.c_str(), O_RDWR);
578 if (file < 0)
579 {
580 std::cerr << "unable to open i2c device " << i2cBus.string()
581 << "\n";
582 continue;
583 }
584 unsigned long funcs = 0;
585
586 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
587 {
588 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700589 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800590 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700591 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800592 continue;
593 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700594 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
595 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800596 {
597 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
598 << bus << "\n";
599 continue;
600 }
James Feist98132792019-07-09 13:29:09 -0700601 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800602 device = std::make_shared<DeviceMap>();
603
Nikhil Potaded8884f12019-03-27 13:27:13 -0700604 // i2cdetect by default uses the range 0x03 to 0x77, as
605 // this is what we have tested with, use this range. Could be
606 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800607 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800608 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700609 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800610 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700611
612 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530613 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700614
Ed Tanous07d467b2021-02-23 14:48:37 -0800615 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800616 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700617 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800618 }
James Feist3cb5fec2018-01-23 14:41:51 -0800619 }
James Feist3cb5fec2018-01-23 14:41:51 -0800620}
621
James Feist6ebf9de2018-05-15 15:01:17 -0700622// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700623struct FindDevicesWithCallback :
624 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700625{
James Feista465ccc2019-02-08 12:51:01 -0800626 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800627 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530628 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800629 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700630 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530631 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
632 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700633 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700634 ~FindDevicesWithCallback()
635 {
636 _callback();
637 }
638 void run()
639 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530640 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700641 }
642
James Feista465ccc2019-02-08 12:51:01 -0800643 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800644 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800645 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530646 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700647 std::function<void(void)> _callback;
648};
649
Ed Tanous07d467b2021-02-23 14:48:37 -0800650void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300651 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800652 boost::container::flat_map<
653 std::pair<size_t, size_t>,
654 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530655 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800656 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530657 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800658{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300659 boost::container::flat_map<std::string, std::string> formattedFRU;
Michael Shen0961b112022-02-22 11:06:33 +0800660 resCodes res = formatIPMIFRU(device, formattedFRU);
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300661 if (res == resCodes::resErr)
James Feist3cb5fec2018-01-23 14:41:51 -0800662 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300663 std::cerr << "failed to parse FRU for device at bus " << bus
Patrick Ventureb755c832019-08-07 11:09:14 -0700664 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800665 return;
666 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800667 if (res == resCodes::resWarn)
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300668 {
669 std::cerr << "there were warnings while parsing FRU for device at bus "
670 << bus << " address " << address << "\n";
671 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700672
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300673 auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800674 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700675 // Not found under Board section or an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300676 if (productNameFind == formattedFRU.end() ||
Patrick Venture96cdaef2019-07-30 13:30:52 -0700677 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800678 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300679 productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800680 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700681 // Found under Product section and not an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300682 if (productNameFind != formattedFRU.end() &&
Patrick Venture96cdaef2019-07-30 13:30:52 -0700683 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800684 {
685 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800686 std::regex illegalObject("[^A-Za-z0-9_]");
687 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800688 }
689 else
690 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800691 productName = "UNKNOWN" + std::to_string(unknownBusObjectCount);
692 unknownBusObjectCount++;
James Feist3cb5fec2018-01-23 14:41:51 -0800693 }
694
James Feist918e18c2018-02-13 15:51:07 -0800695 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800696 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700697 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800698 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700699 int highest = -1;
700 bool found = false;
701
James Feista465ccc2019-02-08 12:51:01 -0800702 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800703 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700704 std::string path = busIface.second->get_object_path();
705 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
James Feist918e18c2018-02-13 15:51:07 -0800706 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700707 // Check if the match named has extra information.
708 found = true;
Ed Tanous07d467b2021-02-23 14:48:37 -0800709 std::smatch baseMatch;
Patrick Venture015fb0a2019-08-16 09:33:31 -0700710
711 bool match = std::regex_match(
Ed Tanous07d467b2021-02-23 14:48:37 -0800712 path, baseMatch, std::regex(productName + "_(\\d+)$"));
Patrick Venture015fb0a2019-08-16 09:33:31 -0700713 if (match)
James Feist79e9c0b2018-03-15 15:21:17 -0700714 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800715 if (baseMatch.size() == 2)
Patrick Venture015fb0a2019-08-16 09:33:31 -0700716 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800717 std::ssub_match baseSubMatch = baseMatch[1];
718 std::string base = baseSubMatch.str();
Patrick Venture015fb0a2019-08-16 09:33:31 -0700719
720 int value = std::stoi(base);
721 highest = (value > highest) ? value : highest;
722 }
James Feist79e9c0b2018-03-15 15:21:17 -0700723 }
James Feist918e18c2018-02-13 15:51:07 -0800724 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700725 } // end searching objects
726
727 if (found)
728 {
729 // We found something with the same name. If highest was still -1,
730 // it means this new entry will be _0.
731 productName += "_";
732 productName += std::to_string(++highest);
James Feist918e18c2018-02-13 15:51:07 -0800733 }
734 }
James Feist3cb5fec2018-01-23 14:41:51 -0800735
James Feist9eb0b582018-04-27 12:15:46 -0700736 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
737 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
738 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
739
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300740 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800741 {
James Feist9eb0b582018-04-27 12:15:46 -0700742
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700743 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800744 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530745 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700746 {
747 continue;
748 }
749 std::string key =
Ed Tanous07d467b2021-02-23 14:48:37 -0800750 std::regex_replace(property.first, nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530751
752 if (property.first == "PRODUCT_ASSET_TAG")
753 {
754 std::string propertyName = property.first;
755 iface->register_property(
756 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530757 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530758 &unknownBusObjectCount, &powerIsOn, &objServer,
759 &systemBus](const std::string& req, std::string& resp) {
Ed Tanous07d467b2021-02-23 14:48:37 -0800760 if (strcmp(req.c_str(), resp.c_str()) != 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +0530761 {
762 // call the method which will update
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300763 if (updateFRUProperty(req, bus, address, propertyName,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530764 dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530765 unknownBusObjectCount, powerIsOn,
766 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530767 {
768 resp = req;
769 }
770 else
771 {
772 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300773 "FRU property update failed.");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530774 }
775 }
776 return 1;
777 });
778 }
779 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700780 {
781 std::cerr << "illegal key: " << key << "\n";
782 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800783 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700784 {
785 std::cout << property.first << ": " << property.second << "\n";
786 }
James Feist3cb5fec2018-01-23 14:41:51 -0800787 }
James Feist2a9d6db2018-04-27 15:48:28 -0700788
789 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700790 iface->register_property("BUS", bus);
791 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700792
James Feist9eb0b582018-04-27 12:15:46 -0700793 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800794}
795
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300796static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800797{
798 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800799 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300800 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800801 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300802 baseboardFRUFile.seekg(0, std::ios_base::end);
803 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
804 baseboardFRU.resize(fileSize);
805 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700806 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
807 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
808 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800809 }
810 else
811 {
812 return false;
813 }
814 return true;
815}
816
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300817bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700818{
819 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800820 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700821 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300822 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700823 return false;
824 }
825 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800826 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700827 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300828 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700829 return false;
830 }
831 // baseboard fru
832 if (bus == 0 && address == 0)
833 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800834 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700835 if (!file.good())
836 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800837 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700838 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700839 return false;
840 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700841 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
842 const char* charOffset = reinterpret_cast<const char*>(fru.data());
843 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700844 return file.good();
845 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800846
847 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700848 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800849 auto path = getEepromPath(bus, address);
850 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
851 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700852 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800853 std::cerr << "unable to open i2c device " << path << "\n";
854 throw DBusInternalError();
855 return false;
856 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700857
Ed Tanous07d467b2021-02-23 14:48:37 -0800858 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
859 if (writtenBytes < 0)
860 {
861 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700862 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700863 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700864 return false;
865 }
866
Ed Tanous07d467b2021-02-23 14:48:37 -0800867 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700868 return true;
869 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800870
871 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
872
873 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
874 if (file < 0)
875 {
876 std::cerr << "unable to open i2c device " << i2cBus << "\n";
877 throw DBusInternalError();
878 return false;
879 }
880 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
881 {
882 std::cerr << "unable to set device address\n";
883 close(file);
884 throw DBusInternalError();
885 return false;
886 }
887
888 constexpr const size_t retryMax = 2;
889 uint16_t index = 0;
890 size_t retries = retryMax;
891 while (index < fru.size())
892 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700893 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800894 (retries == retryMax))
895 {
896 // The 4K EEPROM only uses the A2 and A1 device address bits
897 // with the third bit being a memory page address bit.
898 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
899 {
900 std::cerr << "unable to set device address\n";
901 close(file);
902 throw DBusInternalError();
903 return false;
904 }
905 }
906
907 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
908 fru[index]) < 0)
909 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700910 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800911 {
912 std::cerr << "error writing fru: " << strerror(errno) << "\n";
913 close(file);
914 throw DBusInternalError();
915 return false;
916 }
917 }
918 else
919 {
920 retries = retryMax;
921 index++;
922 }
923 // most eeproms require 5-10ms between writes
924 std::this_thread::sleep_for(std::chrono::milliseconds(10));
925 }
926 close(file);
927 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700928}
929
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800930void rescanOneBus(
krishnar4213ee212022-11-11 15:39:30 +0530931 BusMap& busmap, uint16_t busNum,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800932 boost::container::flat_map<
933 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700934 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800935 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530936 sdbusplus::asio::object_server& objServer,
937 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800938{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800939 for (auto& [pair, interface] : foundDevices)
940 {
941 if (pair.first == static_cast<size_t>(busNum))
942 {
943 objServer.remove_interface(interface);
944 foundDevices.erase(pair);
945 }
946 }
947
James Feist5d7f4692020-06-17 18:22:33 -0700948 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
949 if (!fs::exists(busPath))
950 {
951 if (dbusCall)
952 {
953 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
954 << "\n";
955 throw std::invalid_argument("Invalid Bus.");
956 }
957 return;
958 }
959
960 std::vector<fs::path> i2cBuses;
961 i2cBuses.emplace_back(busPath);
962
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800963 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530964 i2cBuses, busmap, powerIsOn, objServer,
965 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
966 &objServer, &systemBus]() {
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800967 for (auto& busIface : dbusInterfaceMap)
968 {
969 if (busIface.first.first == static_cast<size_t>(busNum))
970 {
971 objServer.remove_interface(busIface.second);
972 }
973 }
Wludzik, Jozefc1136b72020-06-24 11:58:32 +0200974 auto found = busmap.find(busNum);
975 if (found == busmap.end() || found->second == nullptr)
976 {
977 return;
978 }
979 for (auto& device : *(found->second))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800980 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800981 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530982 static_cast<uint32_t>(busNum), device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530983 unknownBusObjectCount, powerIsOn, objServer,
984 systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800985 }
986 });
987 scan->run();
988}
989
James Feist9eb0b582018-04-27 12:15:46 -0700990void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700991 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800992 boost::container::flat_map<
993 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530994 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800995 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530996 sdbusplus::asio::object_server& objServer,
997 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -0800998{
James Feist6ebf9de2018-05-15 15:01:17 -0700999 static boost::asio::deadline_timer timer(io);
1000 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001001
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001002 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -07001003 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -08001004 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001005 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001006
Nikhil Potaded8884f12019-03-27 13:27:13 -07001007 boost::container::flat_map<size_t, fs::path> busPaths;
1008 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001009 {
James Feist4131aea2018-03-09 09:47:30 -08001010 std::cerr << "unable to find i2c devices\n";
1011 return;
James Feist918e18c2018-02-13 15:51:07 -08001012 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001013
Ed Tanous07d467b2021-02-23 14:48:37 -08001014 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001015 {
1016 i2cBuses.emplace_back(busPath.second);
1017 }
James Feist4131aea2018-03-09 09:47:30 -08001018
James Feist98132792019-07-09 13:29:09 -07001019 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001020 for (auto& [pair, interface] : foundDevices)
1021 {
1022 objServer.remove_interface(interface);
1023 }
1024 foundDevices.clear();
1025
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301026 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301027 i2cBuses, busmap, powerIsOn, objServer, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001028 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001029 {
1030 objServer.remove_interface(busIface.second);
1031 }
James Feist4131aea2018-03-09 09:47:30 -08001032
James Feist6ebf9de2018-05-15 15:01:17 -07001033 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -08001034 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001035
James Feist6ebf9de2018-05-15 15:01:17 -07001036 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001037 std::vector<uint8_t> baseboardFRU;
1038 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001039 {
Helen Huangf69fe902021-02-19 16:18:22 +08001040 // If no device on i2c bus 0, the insertion will happen.
1041 auto bus0 =
1042 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1043 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001044 }
James Feist98132792019-07-09 13:29:09 -07001045 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001046 {
James Feista465ccc2019-02-08 12:51:01 -08001047 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001048 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001049 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301050 devicemap.first, device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301051 unknownBusObjectCount, powerIsOn,
1052 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001053 }
1054 }
1055 });
1056 scan->run();
1057 });
James Feist918e18c2018-02-13 15:51:07 -08001058}
1059
Joshi-Mansid73b7442020-03-27 19:10:21 +05301060// Details with example of Asset Tag Update
1061// To find location of Product Info Area asset tag as per FRU specification
1062// 1. Find product Info area starting offset (*8 - as header will be in
1063// multiple of 8 bytes).
1064// 2. Skip 3 bytes of product info area (like format version, area length,
1065// and language code).
1066// 3. Traverse manufacturer name, product name, product version, & product
1067// serial number, by reading type/length code to reach the Asset Tag.
1068// 4. Update the Asset Tag, reposition the product Info area in multiple of
1069// 8 bytes. Update the Product area length and checksum.
1070
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001071bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301072 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001073 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301074 boost::container::flat_map<
1075 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301076 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001077 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301078 sdbusplus::asio::object_server& objServer,
1079 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301080{
1081 size_t updatePropertyReqLen = updatePropertyReq.length();
1082 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1083 {
1084 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001085 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301086 "Invalid Length "
1087 << updatePropertyReqLen << "\n";
1088 return false;
1089 }
1090
1091 std::vector<uint8_t> fruData;
1092 try
1093 {
krishnar4213ee212022-11-11 15:39:30 +05301094 fruData = getFRUInfo(static_cast<uint16_t>(bus),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301095 static_cast<uint8_t>(address));
1096 }
Patrick Williams83b1e9b2021-10-06 12:47:24 -05001097 catch (const std::invalid_argument& e)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301098 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001099 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301100 return false;
1101 }
1102
1103 if (fruData.empty())
1104 {
1105 return false;
1106 }
1107
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +00001108 struct FruArea fruAreaParams
1109 {};
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301110 size_t fruDataIter = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301111
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301112 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams,
1113 fruDataIter))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301114 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301115 std::cerr << "findFruAreaLocationAndField failed \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301116 return false;
1117 }
1118
Ed Tanous3013fb42022-07-09 08:27:06 -07001119 ssize_t fieldLength = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301120
Joshi-Mansid73b7442020-03-27 19:10:21 +05301121 // Push post update fru field bytes to a vector
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301122 fieldLength = getFieldLength(fruData[fruAreaParams.updateFieldLoc]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001123 if (fieldLength < 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301124 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001125 std::cerr << "Property " << propertyName << " not present \n";
1126 return false;
1127 }
1128 fruDataIter += 1 + fieldLength;
1129 size_t restFRUFieldsLoc = fruDataIter;
1130 size_t endOfFieldsLoc = 0;
1131 while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0)
1132 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301133 if (fruDataIter >= (fruAreaParams.start + fruAreaParams.size))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301134 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301135 fruDataIter = fruAreaParams.start + fruAreaParams.size;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301136 break;
1137 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001138 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301139 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001140 endOfFieldsLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301141
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001142 std::vector<uint8_t> restFRUAreaFieldsData;
1143 std::copy_n(fruData.begin() + restFRUFieldsLoc,
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001144 endOfFieldsLoc - restFRUFieldsLoc + 1,
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001145 std::back_inserter(restFRUAreaFieldsData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301146
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001147 // Push post update fru areas if any
1148 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001149 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1150 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001151 {
1152 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001153 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001154 if ((fruAreaLoc > endOfFieldsLoc) &&
1155 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1156 {
1157 nextFRUAreaLoc = fruAreaLoc;
1158 }
1159 }
1160 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001161 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001162 {
1163 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1164 fruData.size() - nextFRUAreaLoc,
1165 std::back_inserter(restFRUAreasData));
1166 }
1167
1168 // check FRU area size
1169 size_t fruAreaDataSize =
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301170 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) +
1171 restFRUAreaFieldsData.size());
1172 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001173 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1174 {
1175
1176#ifdef ENABLE_FRU_AREA_RESIZE
1177 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1178 // round size to 8-byte blocks
1179 newFRUAreaSize =
1180 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301181 size_t newFRUDataSize =
1182 fruData.size() + newFRUAreaSize - fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001183 fruData.resize(newFRUDataSize);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301184 fruAreaParams.size = newFRUAreaSize;
1185 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001186#else
1187 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1188 << " should not be greater than available FRU area size: "
1189 << fruAreaAvailableSize << "\n";
1190 return false;
1191#endif // ENABLE_FRU_AREA_RESIZE
1192 }
1193
Joshi-Mansid73b7442020-03-27 19:10:21 +05301194 // write new requested property field length and data
1195 constexpr uint8_t newTypeLenMask = 0xC0;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301196 fruData[fruAreaParams.updateFieldLoc] =
Joshi-Mansid73b7442020-03-27 19:10:21 +05301197 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301198 fruAreaParams.updateFieldLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301199 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301200 fruData.begin() + fruAreaParams.updateFieldLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301201
1202 // Copy remaining data to main fru area - post updated fru field vector
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301203 restFRUFieldsLoc = fruAreaParams.updateFieldLoc + updatePropertyReqLen;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001204 size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size();
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001205 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
1206 fruData.begin() + restFRUFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301207
1208 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001209 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301210 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301211
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001212#ifdef ENABLE_FRU_AREA_RESIZE
1213 ++nextFRUAreaNewLoc;
1214 ssize_t nextFRUAreaOffsetDiff =
1215 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
1216 // Append rest FRU Areas if size changed and there were other sections after
1217 // updated one
1218 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1219 {
1220 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1221 fruData.begin() + nextFRUAreaNewLoc);
1222 // Update Common Header
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001223 for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord;
1224 fruArea++)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001225 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001226 unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001227 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
1228 if (curFRUAreaOffset > fruAreaOffsetFieldValue)
1229 {
1230 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1231 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1232 }
1233 }
1234 // Calculate new checksum
1235 std::vector<uint8_t> headerFRUData;
1236 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1237 size_t checksumVal = calculateChecksum(headerFRUData);
1238 fruData[7] = static_cast<uint8_t>(checksumVal);
1239 // fill zeros if FRU Area size decreased
1240 if (nextFRUAreaOffsetDiff < 0)
1241 {
1242 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1243 restFRUAreasData.size(),
1244 fruData.end(), 0);
1245 }
1246 }
1247#else
1248 // this is to avoid "unused variable" warning
1249 (void)nextFRUAreaNewLoc;
1250#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301251 if (fruData.empty())
1252 {
1253 return false;
1254 }
1255
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001256 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301257 fruData))
1258 {
1259 return false;
1260 }
1261
1262 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301263 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1264 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301265 return true;
1266}
1267
James Feist98132792019-07-09 13:29:09 -07001268int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001269{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301270 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1271 sdbusplus::asio::object_server objServer(systemBus);
1272
Kumar Thangavel41a90512021-11-24 15:44:20 +05301273 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301274 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001275 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001276 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001277 std::vector<fs::path> i2cBuses;
1278
James Feista3c180a2018-08-09 16:06:04 -07001279 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001280 {
1281 std::cerr << "unable to find i2c devices\n";
1282 return 1;
1283 }
James Feist3cb5fec2018-01-23 14:41:51 -08001284
Patrick Venture11f1ff42019-08-01 10:42:12 -07001285 // check for and load blacklist with initial buses.
1286 loadBlacklist(blacklistPath);
1287
Vijay Khemka065f6d92018-12-18 10:37:47 -08001288 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001289
James Feist6ebf9de2018-05-15 15:01:17 -07001290 // this is a map with keys of pair(bus number, address) and values of
1291 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001292 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001293 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1294 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001295
James Feist9eb0b582018-04-27 12:15:46 -07001296 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1297 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1298 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001299
Kumar Thangavel41a90512021-11-24 15:44:20 +05301300 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301301 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1302 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301303 });
James Feist2a9d6db2018-04-27 15:48:28 -07001304
krishnar4213ee212022-11-11 15:39:30 +05301305 iface->register_method("ReScanBus", [&](uint16_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301306 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301307 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001308 });
1309
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001310 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001311
krishnar4213ee212022-11-11 15:39:30 +05301312 iface->register_method("WriteFru", [&](const uint16_t bus,
James Feistb49ffc32018-05-02 11:10:43 -07001313 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001314 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001315 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001316 {
James Feistddb78302018-09-06 11:45:42 -07001317 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001318 return;
1319 }
1320 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301321 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1322 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001323 });
James Feist9eb0b582018-04-27 12:15:46 -07001324 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001325
Patrick Williams2af39222022-07-22 19:26:56 -05001326 std::function<void(sdbusplus::message_t & message)> eventHandler =
1327 [&](sdbusplus::message_t& message) {
James Feist918e18c2018-02-13 15:51:07 -08001328 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001329 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001330 std::string,
1331 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001332 values;
1333 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001334 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001335 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001336 {
Thu Nguyen3b13fb42022-08-26 06:32:56 +07001337 if (std::get<std::string>(findState->second) ==
1338 "xyz.openbmc_project.State.Host.HostState.Running")
1339 {
1340 powerIsOn = true;
1341 }
James Feist0eeb79c2019-10-09 13:35:29 -07001342 }
James Feist6ebf9de2018-05-15 15:01:17 -07001343
James Feistcb5661d2020-06-12 15:05:01 -07001344 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001345 {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301346 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301347 powerIsOn, objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001348 }
James Feist918e18c2018-02-13 15:51:07 -08001349 };
James Feist9eb0b582018-04-27 12:15:46 -07001350
Patrick Williams2af39222022-07-22 19:26:56 -05001351 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1352 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001353 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001354 "openbmc_project/state/"
1355 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001356 eventHandler);
1357
James Feist4131aea2018-03-09 09:47:30 -08001358 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001359 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001360 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001361 // monitor for new i2c devices
1362 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1363 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001364 watchI2cBusses = [&](const boost::system::error_code& ec,
Ed Tanous07d467b2021-02-23 14:48:37 -08001365 std::size_t bytesTransferred) {
James Feist4131aea2018-03-09 09:47:30 -08001366 if (ec)
1367 {
1368 std::cout << "Callback Error " << ec << "\n";
1369 return;
1370 }
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001371 size_t index = 0;
1372 while ((index + sizeof(inotify_event)) <= bytesTransferred)
James Feist4131aea2018-03-09 09:47:30 -08001373 {
Ed Tanous3013fb42022-07-09 08:27:06 -07001374 const char* p = &readBuffer[index];
1375 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1376 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
James Feist4131aea2018-03-09 09:47:30 -08001377 switch (iEvent->mask)
1378 {
James Feist9eb0b582018-04-27 12:15:46 -07001379 case IN_CREATE:
1380 case IN_MOVED_TO:
1381 case IN_DELETE:
Ed Tanous3013fb42022-07-09 08:27:06 -07001382 std::string_view name(&iEvent->name[0], iEvent->len);
James Feist5d7f4692020-06-17 18:22:33 -07001383 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001384 {
James Feist5d7f4692020-06-17 18:22:33 -07001385 int bus = busStrToInt(name);
1386 if (bus < 0)
1387 {
1388 std::cerr << "Could not parse bus " << name
1389 << "\n";
1390 continue;
1391 }
krishnar4213ee212022-11-11 15:39:30 +05301392 rescanOneBus(busMap, static_cast<uint16_t>(bus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301393 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301394 unknownBusObjectCount, powerIsOn,
1395 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001396 }
James Feist4131aea2018-03-09 09:47:30 -08001397 }
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001398 index += sizeof(inotify_event) + iEvent->len;
James Feist4131aea2018-03-09 09:47:30 -08001399 }
James Feist6ebf9de2018-05-15 15:01:17 -07001400
James Feist4131aea2018-03-09 09:47:30 -08001401 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1402 watchI2cBusses);
1403 };
1404
1405 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001406 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301407 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1408 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001409
James Feist3cb5fec2018-01-23 14:41:51 -08001410 io.run();
1411 return 0;
1412}