blob: 2045b4ea713187cd739199fe1c5a003e1690bc36 [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>
Ed Tanous34c3e012023-03-06 13:42:00 -080026#include <boost/asio/io_context.hpp>
Ed Tanousaa497ed2023-02-28 13:43:41 -080027#include <boost/asio/steady_timer.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;
ankita prasad7329a222023-07-07 09:18:10 +000065constexpr size_t busTimeoutSeconds = 10;
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
Ed Tanous34c3e012023-03-06 13:42:00 -080084boost::asio::io_context 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{
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700167 auto ec = std::error_code();
168 auto isSymlink =
169 is_symlink(std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
170 std::to_string(bus) + "/mux_device"),
171 ec);
172 return (!ec && isSymlink);
James Feistc95cb142018-02-26 10:41:42 -0800173}
174
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530175static void makeProbeInterface(size_t bus, size_t address,
176 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700177{
178 if (isMuxBus(bus))
179 {
180 return; // the mux buses are random, no need to publish
181 }
182 auto [it, success] = foundDevices.emplace(
183 std::make_pair(bus, address),
184 objServer.add_interface(
185 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
186 std::to_string(address),
187 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
188 if (!success)
189 {
190 return; // already added
191 }
192 it->second->register_property("Bus", bus);
193 it->second->register_property("Address", address);
194 it->second->initialize();
195}
196
Zev Weiss309c0b12022-02-25 01:44:12 +0000197static std::optional<bool> isDevice16Bit(int file)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800198{
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700199 // Set the higher data word address bits to 0. It's safe on 8-bit addressing
200 // EEPROMs because it doesn't write any actual data.
201 int ret = i2c_smbus_write_byte(file, 0);
202 if (ret < 0)
203 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000204 return std::nullopt;
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700205 }
206
Vijay Khemka2d681f62018-11-06 15:51:00 -0800207 /* Get first byte */
208 int byte1 = i2c_smbus_read_byte_data(file, 0);
209 if (byte1 < 0)
210 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000211 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800212 }
213 /* Read 7 more bytes, it will read same first byte in case of
214 * 8 bit but it will read next byte in case of 16 bit
215 */
216 for (int i = 0; i < 7; i++)
217 {
218 int byte2 = i2c_smbus_read_byte_data(file, 0);
219 if (byte2 < 0)
220 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000221 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800222 }
223 if (byte2 != byte1)
224 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000225 return true;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800226 }
227 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000228 return false;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800229}
230
Xiang Liu2801a702020-01-20 14:29:34 -0800231// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
232// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800233static int i2cSmbusWriteThenRead(int file, uint16_t address,
234 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
235 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800236{
Ed Tanous07d467b2021-02-23 14:48:37 -0800237 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
238 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800239 {
240 return -1;
241 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800242
Ed Tanous3013fb42022-07-09 08:27:06 -0700243 constexpr size_t smbusWriteThenReadMsgCount = 2;
244 std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{};
245 struct i2c_rdwr_ioctl_data rdwr
246 {};
Xiang Liu2801a702020-01-20 14:29:34 -0800247
248 msgs[0].addr = address;
249 msgs[0].flags = 0;
250 msgs[0].len = toSlaveBufLen;
251 msgs[0].buf = toSlaveBuf;
252 msgs[1].addr = address;
253 msgs[1].flags = I2C_M_RD;
254 msgs[1].len = fromSlaveBufLen;
255 msgs[1].buf = fromSlaveBuf;
256
Ed Tanous3013fb42022-07-09 08:27:06 -0700257 rdwr.msgs = msgs.data();
258 rdwr.nmsgs = msgs.size();
Xiang Liu2801a702020-01-20 14:29:34 -0800259
260 int ret = ioctl(file, I2C_RDWR, &rdwr);
261
Jason M. Billsf6e4c1f2022-08-19 12:23:51 -0700262 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800263}
264
Zev Weiss309c0b12022-02-25 01:44:12 +0000265static int64_t readBlockData(bool is16bit, int file, uint16_t address,
266 off_t offset, size_t len, uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800267{
Zev Weiss309c0b12022-02-25 01:44:12 +0000268 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800269 {
Xiang Liu2801a702020-01-20 14:29:34 -0800270 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
271 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800272 }
273
Xiang Liu2801a702020-01-20 14:29:34 -0800274 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700275 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
276 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
277 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800278}
279
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700280// TODO: This code is very similar to the non-eeprom version and can be merged
281// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300282static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700283{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700284 auto path = getEepromPath(bus, address);
285
286 int file = open(path.c_str(), O_RDONLY);
287 if (file < 0)
288 {
289 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700290 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700291 }
292
Patrick Venturebaba7672019-10-26 09:26:41 -0700293 std::string errorMessage = "eeprom at " + std::to_string(bus) +
294 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000295 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
296 return readFromEeprom(file, offset, length, outbuf);
297 };
298 FRUReader reader(std::move(readFunc));
299 std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700300
301 close(file);
302 return device;
303}
304
Ed Tanous07d467b2021-02-23 14:48:37 -0800305std::set<int> findI2CEeproms(int i2cBus,
306 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700307{
308 std::set<int> foundList;
309
310 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
311
312 // For each file listed under the i2c device
313 // NOTE: This should be faster than just checking for each possible address
314 // path.
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700315 auto ec = std::error_code();
316 for (const auto& p : fs::directory_iterator(path, ec))
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700317 {
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700318 if (ec)
319 {
320 std::cerr << "directory_iterator err " << ec.message() << "\n";
321 break;
322 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700323 const std::string node = p.path().string();
324 std::smatch m;
Patrick Williamsdf190612023-05-10 07:51:34 -0500325 bool found = std::regex_match(node, m,
326 std::regex(".+\\d+-([0-9abcdef]+$)"));
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700327
328 if (!found)
329 {
330 continue;
331 }
332 if (m.size() != 2)
333 {
334 std::cerr << "regex didn't capture\n";
335 continue;
336 }
337
338 std::ssub_match subMatch = m[1];
339 std::string addressString = subMatch.str();
340
Ed Tanous3013fb42022-07-09 08:27:06 -0700341 std::size_t ignored = 0;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700342 const int hexBase = 16;
343 int address = std::stoi(addressString, &ignored, hexBase);
344
345 const std::string eeprom = node + "/eeprom";
346
347 try
348 {
349 if (!fs::exists(eeprom))
350 {
351 continue;
352 }
353 }
354 catch (...)
355 {
356 continue;
357 }
358
359 // There is an eeprom file at this address, it may have invalid
360 // contents, but we found it.
361 foundList.insert(address);
362
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300363 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700364 if (!device.empty())
365 {
366 devices->emplace(address, device);
367 }
368 }
369
370 return foundList;
371}
372
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300373int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800374 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530375 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800376{
James Feist26c27ad2018-07-25 15:09:40 -0700377 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700378 // NOTE: When reading the devices raw on the bus, it can interfere with
379 // the driver's ability to operate, therefore read eeproms first before
380 // scanning for devices without drivers. Several experiments were run
381 // and it was determined that if there were any devices on the bus
382 // before the eeprom was hit and read, the eeprom driver wouldn't open
383 // while the bus device was open. An experiment was not performed to see
384 // if this issue was resolved if the i2c bus device was closed, but
385 // hexdumps of the eeprom later were successful.
386
387 // Scan for i2c eeproms loaded on this bus.
388 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800389 std::set<size_t>& failedItems = failedAddresses[bus];
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000390 std::set<size_t>& foundItems = fruAddresses[bus];
391 foundItems.clear();
James Feist7972bb92019-11-13 15:59:24 -0800392
393 std::set<size_t>* rootFailures = nullptr;
394 int rootBus = getRootBus(bus);
395
396 if (rootBus >= 0)
397 {
398 rootFailures = &(failedAddresses[rootBus]);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000399 foundItems = fruAddresses[rootBus];
James Feist7972bb92019-11-13 15:59:24 -0800400 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700401
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530402 constexpr int startSkipSlaveAddr = 0;
403 constexpr int endSkipSlaveAddr = 12;
404
James Feist26c27ad2018-07-25 15:09:40 -0700405 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800406 {
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000407 if (foundItems.find(ii) != foundItems.end())
408 {
409 continue;
410 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700411 if (skipList.find(ii) != skipList.end())
412 {
413 continue;
414 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530415 // skipping since no device is present in this range
416 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
417 {
418 continue;
419 }
James Feist26c27ad2018-07-25 15:09:40 -0700420 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530421 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800422 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300423 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700424 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700425 continue;
426 }
427 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800428 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700429 {
430 continue;
431 }
James Feist3cb5fec2018-01-23 14:41:51 -0800432
Ed Tanous07d467b2021-02-23 14:48:37 -0800433 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700434 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700435 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700436 << "\n";
437 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800438
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530439 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700440
James Feist7972bb92019-11-13 15:59:24 -0800441 if (failedItems.find(ii) != failedItems.end())
442 {
443 // if we failed to read it once, unlikely we can read it later
444 continue;
445 }
446
447 if (rootFailures != nullptr)
448 {
449 if (rootFailures->find(ii) != rootFailures->end())
450 {
451 continue;
452 }
453 }
454
Vijay Khemka2d681f62018-11-06 15:51:00 -0800455 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000456 std::optional<bool> is16Bit = isDevice16Bit(file);
457 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800458 {
459 std::cerr << "failed to read bus " << bus << " address " << ii
460 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700461 if (powerIsOn)
462 {
463 failedItems.insert(ii);
464 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800465 continue;
466 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000467 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800468
Zev Weiss309c0b12022-02-25 01:44:12 +0000469 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
470 uint8_t* outbuf) {
471 return readBlockData(is16BitBool, file, ii, offset, length,
472 outbuf);
473 };
474 FRUReader reader(std::move(readFunc));
Patrick Williamsdf190612023-05-10 07:51:34 -0500475 std::string errorMessage = "bus " + std::to_string(bus) +
476 " address " + std::to_string(ii);
Zev Weiss309c0b12022-02-25 01:44:12 +0000477 std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700478 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700479 {
James Feist26c27ad2018-07-25 15:09:40 -0700480 continue;
481 }
James Feist26c27ad2018-07-25 15:09:40 -0700482
Patrick Venture786f1792019-08-05 16:33:44 -0700483 devices->emplace(ii, device);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000484 fruAddresses[bus].insert(ii);
James Feist3cb5fec2018-01-23 14:41:51 -0800485 }
James Feist26c27ad2018-07-25 15:09:40 -0700486 return 1;
487 });
488 std::future_status status =
489 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
490 if (status == std::future_status::timeout)
491 {
492 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700493 if (powerIsOn)
494 {
495 busBlacklist.insert(bus);
496 }
James Feist444830e2019-04-05 08:38:16 -0700497 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700498 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800499 }
500
James Feist444830e2019-04-05 08:38:16 -0700501 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700502 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800503}
504
Patrick Venture11f1ff42019-08-01 10:42:12 -0700505void loadBlacklist(const char* path)
506{
507 std::ifstream blacklistStream(path);
508 if (!blacklistStream.good())
509 {
510 // File is optional.
511 std::cerr << "Cannot open blacklist file.\n\n";
512 return;
513 }
514
Patrick Williamsdf190612023-05-10 07:51:34 -0500515 nlohmann::json data = nlohmann::json::parse(blacklistStream, nullptr,
516 false);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700517 if (data.is_discarded())
518 {
519 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
520 "exiting\n";
521 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700522 }
523
524 // It's expected to have at least one field, "buses" that is an array of the
525 // buses by integer. Allow for future options to exclude further aspects,
526 // such as specific addresses or ranges.
527 if (data.type() != nlohmann::json::value_t::object)
528 {
529 std::cerr << "Illegal blacklist, expected to read dictionary\n";
530 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700531 }
532
533 // If buses field is missing, that's fine.
534 if (data.count("buses") == 1)
535 {
536 // Parse the buses array after a little validation.
537 auto buses = data.at("buses");
538 if (buses.type() != nlohmann::json::value_t::array)
539 {
540 // Buses field present but invalid, therefore this is an error.
541 std::cerr << "Invalid contents for blacklist buses field\n";
542 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700543 }
544
545 // Catch exception here for type mis-match.
546 try
547 {
548 for (const auto& bus : buses)
549 {
550 busBlacklist.insert(bus.get<size_t>());
551 }
552 }
553 catch (const nlohmann::detail::type_error& e)
554 {
555 // Type mis-match is a critical error.
556 std::cerr << "Invalid bus type: " << e.what() << "\n";
557 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700558 }
559 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700560}
561
Ed Tanous07d467b2021-02-23 14:48:37 -0800562static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800563 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530564 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800565{
Ed Tanous3013fb42022-07-09 08:27:06 -0700566 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800567 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700568 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800569
James Feist5d7f4692020-06-17 18:22:33 -0700570 if (bus < 0)
571 {
572 std::cerr << "Cannot translate " << i2cBus << " to int\n";
573 continue;
574 }
James Feist444830e2019-04-05 08:38:16 -0700575 if (busBlacklist.find(bus) != busBlacklist.end())
576 {
577 continue; // skip previously failed busses
578 }
James Feist3cb5fec2018-01-23 14:41:51 -0800579
James Feist0c3980a2019-12-19 11:09:27 -0800580 int rootBus = getRootBus(bus);
581 if (busBlacklist.find(rootBus) != busBlacklist.end())
582 {
583 continue;
584 }
585
James Feist3cb5fec2018-01-23 14:41:51 -0800586 auto file = open(i2cBus.c_str(), O_RDWR);
587 if (file < 0)
588 {
589 std::cerr << "unable to open i2c device " << i2cBus.string()
590 << "\n";
591 continue;
592 }
593 unsigned long funcs = 0;
594
595 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
596 {
597 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700598 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800599 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700600 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800601 continue;
602 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700603 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
604 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800605 {
606 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
607 << bus << "\n";
608 continue;
609 }
James Feist98132792019-07-09 13:29:09 -0700610 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800611 device = std::make_shared<DeviceMap>();
612
Nikhil Potaded8884f12019-03-27 13:27:13 -0700613 // i2cdetect by default uses the range 0x03 to 0x77, as
614 // this is what we have tested with, use this range. Could be
615 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800616 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800617 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700618 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800619 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700620
621 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530622 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700623
Ed Tanous07d467b2021-02-23 14:48:37 -0800624 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800625 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700626 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800627 }
James Feist3cb5fec2018-01-23 14:41:51 -0800628 }
James Feist3cb5fec2018-01-23 14:41:51 -0800629}
630
James Feist6ebf9de2018-05-15 15:01:17 -0700631// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700632struct FindDevicesWithCallback :
633 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700634{
James Feista465ccc2019-02-08 12:51:01 -0800635 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800636 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530637 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800638 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700639 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530640 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
641 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700642 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700643 ~FindDevicesWithCallback()
644 {
645 _callback();
646 }
647 void run()
648 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530649 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700650 }
651
James Feista465ccc2019-02-08 12:51:01 -0800652 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800653 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800654 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530655 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700656 std::function<void(void)> _callback;
657};
658
Ed Tanous07d467b2021-02-23 14:48:37 -0800659void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300660 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800661 boost::container::flat_map<
662 std::pair<size_t, size_t>,
663 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530664 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800665 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530666 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800667{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300668 boost::container::flat_map<std::string, std::string> formattedFRU;
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530669
670 std::optional<std::string> optionalProductName = getProductName(
671 device, formattedFRU, bus, address, unknownBusObjectCount);
672 if (!optionalProductName)
James Feist3cb5fec2018-01-23 14:41:51 -0800673 {
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530674 std::cerr << "getProductName failed. product name is empty.\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800675 return;
676 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700677
Patrick Williamsdf190612023-05-10 07:51:34 -0500678 std::string productName = "/xyz/openbmc_project/FruDevice/" +
679 optionalProductName.value();
James Feist3cb5fec2018-01-23 14:41:51 -0800680
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530681 std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName);
682 if (index.has_value())
James Feist918e18c2018-02-13 15:51:07 -0800683 {
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530684 productName += "_";
685 productName += std::to_string(++(*index));
James Feist918e18c2018-02-13 15:51:07 -0800686 }
James Feist3cb5fec2018-01-23 14:41:51 -0800687
James Feist9eb0b582018-04-27 12:15:46 -0700688 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
689 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
690 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
691
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300692 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800693 {
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700694 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800695 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530696 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700697 {
698 continue;
699 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500700 std::string key = std::regex_replace(property.first, nonAsciiRegex,
701 "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530702
703 if (property.first == "PRODUCT_ASSET_TAG")
704 {
705 std::string propertyName = property.first;
706 iface->register_property(
707 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530708 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530709 &unknownBusObjectCount, &powerIsOn, &objServer,
710 &systemBus](const std::string& req, std::string& resp) {
Patrick Williamsdf190612023-05-10 07:51:34 -0500711 if (strcmp(req.c_str(), resp.c_str()) != 0)
712 {
713 // call the method which will update
714 if (updateFRUProperty(req, bus, address, propertyName,
715 dbusInterfaceMap,
716 unknownBusObjectCount, powerIsOn,
717 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530718 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500719 resp = req;
Joshi-Mansid73b7442020-03-27 19:10:21 +0530720 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500721 else
722 {
723 throw std::invalid_argument(
724 "FRU property update failed.");
725 }
726 }
727 return 1;
Joshi-Mansid73b7442020-03-27 19:10:21 +0530728 });
729 }
730 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700731 {
732 std::cerr << "illegal key: " << key << "\n";
733 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800734 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700735 {
736 std::cout << property.first << ": " << property.second << "\n";
737 }
James Feist3cb5fec2018-01-23 14:41:51 -0800738 }
James Feist2a9d6db2018-04-27 15:48:28 -0700739
740 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700741 iface->register_property("BUS", bus);
742 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700743
James Feist9eb0b582018-04-27 12:15:46 -0700744 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800745}
746
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300747static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800748{
749 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800750 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300751 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800752 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300753 baseboardFRUFile.seekg(0, std::ios_base::end);
754 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
755 baseboardFRU.resize(fileSize);
756 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700757 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
758 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
759 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800760 }
761 else
762 {
763 return false;
764 }
765 return true;
766}
767
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300768bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700769{
770 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800771 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700772 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300773 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700774 return false;
775 }
776 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800777 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700778 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300779 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700780 return false;
781 }
782 // baseboard fru
783 if (bus == 0 && address == 0)
784 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800785 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700786 if (!file.good())
787 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800788 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700789 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700790 return false;
791 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700792 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
793 const char* charOffset = reinterpret_cast<const char*>(fru.data());
794 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700795 return file.good();
796 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800797
798 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700799 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800800 auto path = getEepromPath(bus, address);
801 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
802 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700803 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800804 std::cerr << "unable to open i2c device " << path << "\n";
805 throw DBusInternalError();
806 return false;
807 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700808
Ed Tanous07d467b2021-02-23 14:48:37 -0800809 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
810 if (writtenBytes < 0)
811 {
812 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700813 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700814 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700815 return false;
816 }
817
Ed Tanous07d467b2021-02-23 14:48:37 -0800818 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700819 return true;
820 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800821
822 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
823
824 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
825 if (file < 0)
826 {
827 std::cerr << "unable to open i2c device " << i2cBus << "\n";
828 throw DBusInternalError();
829 return false;
830 }
831 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
832 {
833 std::cerr << "unable to set device address\n";
834 close(file);
835 throw DBusInternalError();
836 return false;
837 }
838
839 constexpr const size_t retryMax = 2;
840 uint16_t index = 0;
841 size_t retries = retryMax;
842 while (index < fru.size())
843 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700844 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800845 (retries == retryMax))
846 {
847 // The 4K EEPROM only uses the A2 and A1 device address bits
848 // with the third bit being a memory page address bit.
849 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
850 {
851 std::cerr << "unable to set device address\n";
852 close(file);
853 throw DBusInternalError();
854 return false;
855 }
856 }
857
858 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
859 fru[index]) < 0)
860 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700861 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800862 {
863 std::cerr << "error writing fru: " << strerror(errno) << "\n";
864 close(file);
865 throw DBusInternalError();
866 return false;
867 }
868 }
869 else
870 {
871 retries = retryMax;
872 index++;
873 }
874 // most eeproms require 5-10ms between writes
875 std::this_thread::sleep_for(std::chrono::milliseconds(10));
876 }
877 close(file);
878 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700879}
880
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800881void rescanOneBus(
krishnar4213ee212022-11-11 15:39:30 +0530882 BusMap& busmap, uint16_t busNum,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800883 boost::container::flat_map<
884 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700885 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800886 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530887 sdbusplus::asio::object_server& objServer,
888 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800889{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800890 for (auto& [pair, interface] : foundDevices)
891 {
892 if (pair.first == static_cast<size_t>(busNum))
893 {
894 objServer.remove_interface(interface);
895 foundDevices.erase(pair);
896 }
897 }
898
James Feist5d7f4692020-06-17 18:22:33 -0700899 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
900 if (!fs::exists(busPath))
901 {
902 if (dbusCall)
903 {
904 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
905 << "\n";
906 throw std::invalid_argument("Invalid Bus.");
907 }
908 return;
909 }
910
911 std::vector<fs::path> i2cBuses;
912 i2cBuses.emplace_back(busPath);
913
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800914 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530915 i2cBuses, busmap, powerIsOn, objServer,
916 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
917 &objServer, &systemBus]() {
Patrick Williamsdf190612023-05-10 07:51:34 -0500918 for (auto& busIface : dbusInterfaceMap)
919 {
920 if (busIface.first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800921 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500922 objServer.remove_interface(busIface.second);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800923 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500924 }
925 auto found = busmap.find(busNum);
926 if (found == busmap.end() || found->second == nullptr)
927 {
928 return;
929 }
930 for (auto& device : *(found->second))
931 {
932 addFruObjectToDbus(device.second, dbusInterfaceMap,
933 static_cast<uint32_t>(busNum), device.first,
934 unknownBusObjectCount, powerIsOn, objServer,
935 systemBus);
936 }
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800937 });
938 scan->run();
939}
940
James Feist9eb0b582018-04-27 12:15:46 -0700941void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700942 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800943 boost::container::flat_map<
944 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530945 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800946 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530947 sdbusplus::asio::object_server& objServer,
948 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -0800949{
Ed Tanousaa497ed2023-02-28 13:43:41 -0800950 static boost::asio::steady_timer timer(io);
951 timer.expires_from_now(std::chrono::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800952
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500953 // setup an async wait in case we get flooded with requests
Jian Zhang6e7cebf2022-11-29 18:30:21 +0800954 timer.async_wait([&](const boost::system::error_code& ec) {
955 if (ec == boost::asio::error::operation_aborted)
956 {
957 return;
958 }
959
960 if (ec)
961 {
962 std::cerr << "Error in timer: " << ec.message() << "\n";
963 return;
964 }
965
James Feist4131aea2018-03-09 09:47:30 -0800966 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -0800967 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800968
Nikhil Potaded8884f12019-03-27 13:27:13 -0700969 boost::container::flat_map<size_t, fs::path> busPaths;
970 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -0800971 {
James Feist4131aea2018-03-09 09:47:30 -0800972 std::cerr << "unable to find i2c devices\n";
973 return;
James Feist918e18c2018-02-13 15:51:07 -0800974 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700975
Ed Tanous07d467b2021-02-23 14:48:37 -0800976 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -0700977 {
978 i2cBuses.emplace_back(busPath.second);
979 }
James Feist4131aea2018-03-09 09:47:30 -0800980
James Feist98132792019-07-09 13:29:09 -0700981 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -0700982 for (auto& [pair, interface] : foundDevices)
983 {
984 objServer.remove_interface(interface);
985 }
986 foundDevices.clear();
987
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530988 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530989 i2cBuses, busmap, powerIsOn, objServer, [&]() {
James Feista465ccc2019-02-08 12:51:01 -0800990 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700991 {
992 objServer.remove_interface(busIface.second);
993 }
James Feist4131aea2018-03-09 09:47:30 -0800994
James Feist6ebf9de2018-05-15 15:01:17 -0700995 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -0800996 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -0800997
James Feist6ebf9de2018-05-15 15:01:17 -0700998 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300999 std::vector<uint8_t> baseboardFRU;
1000 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001001 {
Helen Huangf69fe902021-02-19 16:18:22 +08001002 // If no device on i2c bus 0, the insertion will happen.
1003 auto bus0 =
1004 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1005 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001006 }
James Feist98132792019-07-09 13:29:09 -07001007 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001008 {
James Feista465ccc2019-02-08 12:51:01 -08001009 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001010 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001011 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301012 devicemap.first, device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301013 unknownBusObjectCount, powerIsOn,
1014 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001015 }
1016 }
1017 });
1018 scan->run();
1019 });
James Feist918e18c2018-02-13 15:51:07 -08001020}
1021
Joshi-Mansid73b7442020-03-27 19:10:21 +05301022// Details with example of Asset Tag Update
1023// To find location of Product Info Area asset tag as per FRU specification
1024// 1. Find product Info area starting offset (*8 - as header will be in
1025// multiple of 8 bytes).
1026// 2. Skip 3 bytes of product info area (like format version, area length,
1027// and language code).
1028// 3. Traverse manufacturer name, product name, product version, & product
1029// serial number, by reading type/length code to reach the Asset Tag.
1030// 4. Update the Asset Tag, reposition the product Info area in multiple of
1031// 8 bytes. Update the Product area length and checksum.
1032
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001033bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301034 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001035 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301036 boost::container::flat_map<
1037 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301038 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001039 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301040 sdbusplus::asio::object_server& objServer,
1041 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301042{
1043 size_t updatePropertyReqLen = updatePropertyReq.length();
1044 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1045 {
1046 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001047 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301048 "Invalid Length "
1049 << updatePropertyReqLen << "\n";
1050 return false;
1051 }
1052
1053 std::vector<uint8_t> fruData;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301054
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301055 if (!getFruData(fruData, bus, address))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301056 {
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301057 std::cerr << "Failure getting FRU Data \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301058 return false;
1059 }
1060
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +00001061 struct FruArea fruAreaParams
1062 {};
Joshi-Mansid73b7442020-03-27 19:10:21 +05301063
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301064 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301065 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301066 std::cerr << "findFruAreaLocationAndField failed \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301067 return false;
1068 }
1069
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301070 std::vector<uint8_t> restFRUAreaFieldsData;
1071 if (!copyRestFRUArea(fruData, propertyName, fruAreaParams,
1072 restFRUAreaFieldsData))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301073 {
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301074 std::cerr << "copyRestFRUArea failed \n";
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001075 return false;
1076 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301077
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001078 // Push post update fru areas if any
1079 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001080 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1081 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001082 {
1083 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001084 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301085 if ((fruAreaLoc > fruAreaParams.restFieldsEnd) &&
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001086 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1087 {
1088 nextFRUAreaLoc = fruAreaLoc;
1089 }
1090 }
1091 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001092 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001093 {
1094 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1095 fruData.size() - nextFRUAreaLoc,
1096 std::back_inserter(restFRUAreasData));
1097 }
1098
1099 // check FRU area size
1100 size_t fruAreaDataSize =
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301101 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) +
1102 restFRUAreaFieldsData.size());
1103 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001104 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1105 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001106#ifdef ENABLE_FRU_AREA_RESIZE
1107 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1108 // round size to 8-byte blocks
Patrick Williamsdf190612023-05-10 07:51:34 -05001109 newFRUAreaSize = ((newFRUAreaSize - 1) / fruBlockSize + 1) *
1110 fruBlockSize;
1111 size_t newFRUDataSize = fruData.size() + newFRUAreaSize -
1112 fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001113 fruData.resize(newFRUDataSize);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301114 fruAreaParams.size = newFRUAreaSize;
1115 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001116#else
1117 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1118 << " should not be greater than available FRU area size: "
1119 << fruAreaAvailableSize << "\n";
1120 return false;
1121#endif // ENABLE_FRU_AREA_RESIZE
1122 }
1123
Joshi-Mansid73b7442020-03-27 19:10:21 +05301124 // write new requested property field length and data
1125 constexpr uint8_t newTypeLenMask = 0xC0;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301126 fruData[fruAreaParams.updateFieldLoc] =
Joshi-Mansid73b7442020-03-27 19:10:21 +05301127 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301128 fruAreaParams.updateFieldLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301129 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301130 fruData.begin() + fruAreaParams.updateFieldLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301131
1132 // Copy remaining data to main fru area - post updated fru field vector
Patrick Williamsdf190612023-05-10 07:51:34 -05001133 fruAreaParams.restFieldsLoc = fruAreaParams.updateFieldLoc +
1134 updatePropertyReqLen;
1135 size_t fruAreaDataEnd = fruAreaParams.restFieldsLoc +
1136 restFRUAreaFieldsData.size();
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301137
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001138 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301139 fruData.begin() + fruAreaParams.restFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301140
1141 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001142 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301143 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301144
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001145#ifdef ENABLE_FRU_AREA_RESIZE
1146 ++nextFRUAreaNewLoc;
Patrick Williamsdf190612023-05-10 07:51:34 -05001147 ssize_t nextFRUAreaOffsetDiff = (nextFRUAreaNewLoc - nextFRUAreaLoc) /
1148 fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001149 // Append rest FRU Areas if size changed and there were other sections after
1150 // updated one
1151 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1152 {
1153 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1154 fruData.begin() + nextFRUAreaNewLoc);
1155 // Update Common Header
Ed Tanous6e22c872023-02-16 15:19:28 -08001156 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1157 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001158 {
Ed Tanous6e22c872023-02-16 15:19:28 -08001159 unsigned int fruAreaOffsetField =
1160 getHeaderAreaFieldOffset(nextFRUArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001161 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
Ed Tanous6e22c872023-02-16 15:19:28 -08001162 if (curFRUAreaOffset > fruAreaParams.end)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001163 {
1164 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1165 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1166 }
1167 }
1168 // Calculate new checksum
1169 std::vector<uint8_t> headerFRUData;
1170 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1171 size_t checksumVal = calculateChecksum(headerFRUData);
1172 fruData[7] = static_cast<uint8_t>(checksumVal);
1173 // fill zeros if FRU Area size decreased
1174 if (nextFRUAreaOffsetDiff < 0)
1175 {
1176 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1177 restFRUAreasData.size(),
1178 fruData.end(), 0);
1179 }
1180 }
1181#else
1182 // this is to avoid "unused variable" warning
1183 (void)nextFRUAreaNewLoc;
1184#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301185 if (fruData.empty())
1186 {
1187 return false;
1188 }
1189
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001190 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301191 fruData))
1192 {
1193 return false;
1194 }
1195
1196 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301197 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1198 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301199 return true;
1200}
1201
James Feist98132792019-07-09 13:29:09 -07001202int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001203{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301204 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1205 sdbusplus::asio::object_server objServer(systemBus);
1206
Kumar Thangavel41a90512021-11-24 15:44:20 +05301207 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301208 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001209 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001210 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001211 std::vector<fs::path> i2cBuses;
1212
James Feista3c180a2018-08-09 16:06:04 -07001213 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001214 {
1215 std::cerr << "unable to find i2c devices\n";
1216 return 1;
1217 }
James Feist3cb5fec2018-01-23 14:41:51 -08001218
Patrick Venture11f1ff42019-08-01 10:42:12 -07001219 // check for and load blacklist with initial buses.
1220 loadBlacklist(blacklistPath);
1221
Vijay Khemka065f6d92018-12-18 10:37:47 -08001222 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001223
James Feist6ebf9de2018-05-15 15:01:17 -07001224 // this is a map with keys of pair(bus number, address) and values of
1225 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001226 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001227 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1228 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001229
James Feist9eb0b582018-04-27 12:15:46 -07001230 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1231 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1232 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001233
Kumar Thangavel41a90512021-11-24 15:44:20 +05301234 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301235 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1236 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301237 });
James Feist2a9d6db2018-04-27 15:48:28 -07001238
krishnar4213ee212022-11-11 15:39:30 +05301239 iface->register_method("ReScanBus", [&](uint16_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301240 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301241 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001242 });
1243
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001244 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001245
Patrick Williamsdf190612023-05-10 07:51:34 -05001246 iface->register_method("WriteFru",
1247 [&](const uint16_t bus, const uint8_t address,
1248 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001249 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001250 {
James Feistddb78302018-09-06 11:45:42 -07001251 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001252 return;
1253 }
1254 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301255 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1256 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001257 });
James Feist9eb0b582018-04-27 12:15:46 -07001258 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001259
Patrick Williams2af39222022-07-22 19:26:56 -05001260 std::function<void(sdbusplus::message_t & message)> eventHandler =
1261 [&](sdbusplus::message_t& message) {
Patrick Williamsdf190612023-05-10 07:51:34 -05001262 std::string objectName;
1263 boost::container::flat_map<
1264 std::string,
1265 std::variant<std::string, bool, int64_t, uint64_t, double>>
1266 values;
1267 message.read(objectName, values);
1268 auto findState = values.find("CurrentHostState");
1269 if (findState != values.end())
1270 {
1271 if (std::get<std::string>(findState->second) ==
1272 "xyz.openbmc_project.State.Host.HostState.Running")
James Feist918e18c2018-02-13 15:51:07 -08001273 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001274 powerIsOn = true;
James Feist0eeb79c2019-10-09 13:35:29 -07001275 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001276 }
James Feist6ebf9de2018-05-15 15:01:17 -07001277
Patrick Williamsdf190612023-05-10 07:51:34 -05001278 if (powerIsOn)
1279 {
1280 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1281 powerIsOn, objServer, systemBus);
1282 }
1283 };
James Feist9eb0b582018-04-27 12:15:46 -07001284
Patrick Williams2af39222022-07-22 19:26:56 -05001285 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1286 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001287 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001288 "openbmc_project/state/"
1289 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001290 eventHandler);
1291
James Feist4131aea2018-03-09 09:47:30 -08001292 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001293 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001294 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001295 // monitor for new i2c devices
1296 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1297 std::function<void(const boost::system::error_code, std::size_t)>
Patrick Williamsdf190612023-05-10 07:51:34 -05001298 watchI2cBusses =
1299 [&](const boost::system::error_code& ec,
1300 std::size_t bytesTransferred) {
1301 if (ec)
1302 {
1303 std::cout << "Callback Error " << ec << "\n";
1304 return;
1305 }
1306 size_t index = 0;
1307 while ((index + sizeof(inotify_event)) <= bytesTransferred)
1308 {
1309 const char* p = &readBuffer[index];
1310 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1311 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
1312 switch (iEvent->mask)
James Feist4131aea2018-03-09 09:47:30 -08001313 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001314 case IN_CREATE:
1315 case IN_MOVED_TO:
1316 case IN_DELETE:
1317 std::string_view name(&iEvent->name[0], iEvent->len);
1318 if (boost::starts_with(name, "i2c"))
1319 {
1320 int bus = busStrToInt(name);
1321 if (bus < 0)
James Feist9eb0b582018-04-27 12:15:46 -07001322 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001323 std::cerr << "Could not parse bus " << name << "\n";
1324 continue;
1325 }
1326 int rootBus = getRootBus(bus);
1327 if (rootBus >= 0)
1328 {
1329 rescanOneBus(busMap, static_cast<uint16_t>(rootBus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301330 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301331 unknownBusObjectCount, powerIsOn,
1332 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001333 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001334 rescanOneBus(busMap, static_cast<uint16_t>(bus),
1335 dbusInterfaceMap, false,
1336 unknownBusObjectCount, powerIsOn,
1337 objServer, systemBus);
1338 }
James Feist4131aea2018-03-09 09:47:30 -08001339 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001340 index += sizeof(inotify_event) + iEvent->len;
1341 }
James Feist6ebf9de2018-05-15 15:01:17 -07001342
Patrick Williamsdf190612023-05-10 07:51:34 -05001343 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1344 watchI2cBusses);
1345 };
James Feist4131aea2018-03-09 09:47:30 -08001346
1347 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001348 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301349 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1350 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001351
James Feist3cb5fec2018-01-23 14:41:51 -08001352 io.run();
1353 return 0;
1354}