blob: 11f2872e64bdff4c6050bc4af08d4311da13dfdd [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
Marvin Drees2b3ed302023-04-14 16:35:14 +0200265static int64_t readData(bool is16bit, bool isBytewise, int file,
266 uint16_t address, off_t offset, size_t len,
267 uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800268{
Zev Weiss309c0b12022-02-25 01:44:12 +0000269 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800270 {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200271 if (!isBytewise)
272 {
273 return i2c_smbus_read_i2c_block_data(
274 file, static_cast<uint8_t>(offset), len, buf);
275 }
276
277 std::span<uint8_t> bufspan{buf, len};
278 for (size_t i = 0; i < len; i++)
279 {
280 int byte = i2c_smbus_read_byte_data(
281 file, static_cast<uint8_t>(offset + i));
282 if (byte < 0)
283 {
284 return static_cast<int64_t>(byte);
285 }
286 bufspan[i] = static_cast<uint8_t>(byte);
287 }
288 return static_cast<int64_t>(len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800289 }
290
Xiang Liu2801a702020-01-20 14:29:34 -0800291 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700292 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
293 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
294 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800295}
296
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700297// TODO: This code is very similar to the non-eeprom version and can be merged
298// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300299static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700300{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700301 auto path = getEepromPath(bus, address);
302
303 int file = open(path.c_str(), O_RDONLY);
304 if (file < 0)
305 {
306 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700307 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700308 }
309
Patrick Venturebaba7672019-10-26 09:26:41 -0700310 std::string errorMessage = "eeprom at " + std::to_string(bus) +
311 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000312 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
313 return readFromEeprom(file, offset, length, outbuf);
314 };
315 FRUReader reader(std::move(readFunc));
Marvin Drees2b3ed302023-04-14 16:35:14 +0200316 std::pair<std::vector<uint8_t>, bool> pair = readFRUContents(reader,
317 errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700318
319 close(file);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200320 return pair.first;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700321}
322
Ed Tanous07d467b2021-02-23 14:48:37 -0800323std::set<int> findI2CEeproms(int i2cBus,
324 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700325{
326 std::set<int> foundList;
327
328 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
329
330 // For each file listed under the i2c device
331 // NOTE: This should be faster than just checking for each possible address
332 // path.
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700333 auto ec = std::error_code();
334 for (const auto& p : fs::directory_iterator(path, ec))
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700335 {
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700336 if (ec)
337 {
338 std::cerr << "directory_iterator err " << ec.message() << "\n";
339 break;
340 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700341 const std::string node = p.path().string();
342 std::smatch m;
Patrick Williamsdf190612023-05-10 07:51:34 -0500343 bool found = std::regex_match(node, m,
344 std::regex(".+\\d+-([0-9abcdef]+$)"));
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700345
346 if (!found)
347 {
348 continue;
349 }
350 if (m.size() != 2)
351 {
352 std::cerr << "regex didn't capture\n";
353 continue;
354 }
355
356 std::ssub_match subMatch = m[1];
357 std::string addressString = subMatch.str();
358
Ed Tanous3013fb42022-07-09 08:27:06 -0700359 std::size_t ignored = 0;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700360 const int hexBase = 16;
361 int address = std::stoi(addressString, &ignored, hexBase);
362
363 const std::string eeprom = node + "/eeprom";
364
365 try
366 {
367 if (!fs::exists(eeprom))
368 {
369 continue;
370 }
371 }
372 catch (...)
373 {
374 continue;
375 }
376
377 // There is an eeprom file at this address, it may have invalid
378 // contents, but we found it.
379 foundList.insert(address);
380
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300381 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700382 if (!device.empty())
383 {
384 devices->emplace(address, device);
385 }
386 }
387
388 return foundList;
389}
390
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300391int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800392 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530393 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800394{
James Feist26c27ad2018-07-25 15:09:40 -0700395 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700396 // NOTE: When reading the devices raw on the bus, it can interfere with
397 // the driver's ability to operate, therefore read eeproms first before
398 // scanning for devices without drivers. Several experiments were run
399 // and it was determined that if there were any devices on the bus
400 // before the eeprom was hit and read, the eeprom driver wouldn't open
401 // while the bus device was open. An experiment was not performed to see
402 // if this issue was resolved if the i2c bus device was closed, but
403 // hexdumps of the eeprom later were successful.
404
405 // Scan for i2c eeproms loaded on this bus.
406 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800407 std::set<size_t>& failedItems = failedAddresses[bus];
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000408 std::set<size_t>& foundItems = fruAddresses[bus];
409 foundItems.clear();
James Feist7972bb92019-11-13 15:59:24 -0800410
411 std::set<size_t>* rootFailures = nullptr;
412 int rootBus = getRootBus(bus);
413
414 if (rootBus >= 0)
415 {
416 rootFailures = &(failedAddresses[rootBus]);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000417 foundItems = fruAddresses[rootBus];
James Feist7972bb92019-11-13 15:59:24 -0800418 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700419
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530420 constexpr int startSkipSlaveAddr = 0;
421 constexpr int endSkipSlaveAddr = 12;
422
James Feist26c27ad2018-07-25 15:09:40 -0700423 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800424 {
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000425 if (foundItems.find(ii) != foundItems.end())
426 {
427 continue;
428 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700429 if (skipList.find(ii) != skipList.end())
430 {
431 continue;
432 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530433 // skipping since no device is present in this range
434 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
435 {
436 continue;
437 }
James Feist26c27ad2018-07-25 15:09:40 -0700438 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530439 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800440 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300441 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700442 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700443 continue;
444 }
445 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800446 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700447 {
448 continue;
449 }
James Feist3cb5fec2018-01-23 14:41:51 -0800450
Ed Tanous07d467b2021-02-23 14:48:37 -0800451 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700452 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700453 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700454 << "\n";
455 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800456
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530457 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700458
James Feist7972bb92019-11-13 15:59:24 -0800459 if (failedItems.find(ii) != failedItems.end())
460 {
461 // if we failed to read it once, unlikely we can read it later
462 continue;
463 }
464
465 if (rootFailures != nullptr)
466 {
467 if (rootFailures->find(ii) != rootFailures->end())
468 {
469 continue;
470 }
471 }
472
Vijay Khemka2d681f62018-11-06 15:51:00 -0800473 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000474 std::optional<bool> is16Bit = isDevice16Bit(file);
475 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800476 {
477 std::cerr << "failed to read bus " << bus << " address " << ii
478 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700479 if (powerIsOn)
480 {
481 failedItems.insert(ii);
482 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800483 continue;
484 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000485 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800486
Zev Weiss309c0b12022-02-25 01:44:12 +0000487 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
488 uint8_t* outbuf) {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200489 return readData(is16BitBool, false, file, ii, offset, length,
490 outbuf);
Zev Weiss309c0b12022-02-25 01:44:12 +0000491 };
492 FRUReader reader(std::move(readFunc));
Patrick Williamsdf190612023-05-10 07:51:34 -0500493 std::string errorMessage = "bus " + std::to_string(bus) +
494 " address " + std::to_string(ii);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200495 std::pair<std::vector<uint8_t>, bool> pair =
496 readFRUContents(reader, errorMessage);
497 const bool foundHeader = pair.second;
498
499 if (!foundHeader && !is16BitBool)
500 {
501 // certain FRU eeproms require bytewise reading.
502 // otherwise garbage is read. e.g. SuperMicro PWS 920P-SQ
503
504 auto readFunc = [is16BitBool, file, ii](off_t offset,
505 size_t length,
506 uint8_t* outbuf) {
507 return readData(is16BitBool, true, file, ii, offset, length,
508 outbuf);
509 };
510 FRUReader readerBytewise(std::move(readFunc));
511 pair = readFRUContents(readerBytewise, errorMessage);
512 }
513
514 if (pair.first.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700515 {
James Feist26c27ad2018-07-25 15:09:40 -0700516 continue;
517 }
James Feist26c27ad2018-07-25 15:09:40 -0700518
Marvin Drees2b3ed302023-04-14 16:35:14 +0200519 devices->emplace(ii, pair.first);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000520 fruAddresses[bus].insert(ii);
James Feist3cb5fec2018-01-23 14:41:51 -0800521 }
James Feist26c27ad2018-07-25 15:09:40 -0700522 return 1;
523 });
524 std::future_status status =
525 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
526 if (status == std::future_status::timeout)
527 {
528 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700529 if (powerIsOn)
530 {
531 busBlacklist.insert(bus);
532 }
James Feist444830e2019-04-05 08:38:16 -0700533 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700534 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800535 }
536
James Feist444830e2019-04-05 08:38:16 -0700537 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700538 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800539}
540
Patrick Venture11f1ff42019-08-01 10:42:12 -0700541void loadBlacklist(const char* path)
542{
543 std::ifstream blacklistStream(path);
544 if (!blacklistStream.good())
545 {
546 // File is optional.
547 std::cerr << "Cannot open blacklist file.\n\n";
548 return;
549 }
550
Patrick Williamsdf190612023-05-10 07:51:34 -0500551 nlohmann::json data = nlohmann::json::parse(blacklistStream, nullptr,
552 false);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700553 if (data.is_discarded())
554 {
555 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
556 "exiting\n";
557 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700558 }
559
560 // It's expected to have at least one field, "buses" that is an array of the
561 // buses by integer. Allow for future options to exclude further aspects,
562 // such as specific addresses or ranges.
563 if (data.type() != nlohmann::json::value_t::object)
564 {
565 std::cerr << "Illegal blacklist, expected to read dictionary\n";
566 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700567 }
568
569 // If buses field is missing, that's fine.
570 if (data.count("buses") == 1)
571 {
572 // Parse the buses array after a little validation.
573 auto buses = data.at("buses");
574 if (buses.type() != nlohmann::json::value_t::array)
575 {
576 // Buses field present but invalid, therefore this is an error.
577 std::cerr << "Invalid contents for blacklist buses field\n";
578 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700579 }
580
581 // Catch exception here for type mis-match.
582 try
583 {
584 for (const auto& bus : buses)
585 {
586 busBlacklist.insert(bus.get<size_t>());
587 }
588 }
589 catch (const nlohmann::detail::type_error& e)
590 {
591 // Type mis-match is a critical error.
592 std::cerr << "Invalid bus type: " << e.what() << "\n";
593 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700594 }
595 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700596}
597
Ed Tanous07d467b2021-02-23 14:48:37 -0800598static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800599 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530600 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800601{
Ed Tanous3013fb42022-07-09 08:27:06 -0700602 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800603 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700604 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800605
James Feist5d7f4692020-06-17 18:22:33 -0700606 if (bus < 0)
607 {
608 std::cerr << "Cannot translate " << i2cBus << " to int\n";
609 continue;
610 }
James Feist444830e2019-04-05 08:38:16 -0700611 if (busBlacklist.find(bus) != busBlacklist.end())
612 {
613 continue; // skip previously failed busses
614 }
James Feist3cb5fec2018-01-23 14:41:51 -0800615
James Feist0c3980a2019-12-19 11:09:27 -0800616 int rootBus = getRootBus(bus);
617 if (busBlacklist.find(rootBus) != busBlacklist.end())
618 {
619 continue;
620 }
621
James Feist3cb5fec2018-01-23 14:41:51 -0800622 auto file = open(i2cBus.c_str(), O_RDWR);
623 if (file < 0)
624 {
625 std::cerr << "unable to open i2c device " << i2cBus.string()
626 << "\n";
627 continue;
628 }
629 unsigned long funcs = 0;
630
631 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
632 {
633 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700634 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800635 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700636 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800637 continue;
638 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700639 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
640 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800641 {
642 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
643 << bus << "\n";
644 continue;
645 }
James Feist98132792019-07-09 13:29:09 -0700646 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800647 device = std::make_shared<DeviceMap>();
648
Nikhil Potaded8884f12019-03-27 13:27:13 -0700649 // i2cdetect by default uses the range 0x03 to 0x77, as
650 // this is what we have tested with, use this range. Could be
651 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800652 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800653 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700654 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800655 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700656
657 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530658 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700659
Ed Tanous07d467b2021-02-23 14:48:37 -0800660 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800661 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700662 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800663 }
James Feist3cb5fec2018-01-23 14:41:51 -0800664 }
James Feist3cb5fec2018-01-23 14:41:51 -0800665}
666
James Feist6ebf9de2018-05-15 15:01:17 -0700667// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700668struct FindDevicesWithCallback :
669 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700670{
James Feista465ccc2019-02-08 12:51:01 -0800671 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800672 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530673 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800674 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700675 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530676 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
677 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700678 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700679 ~FindDevicesWithCallback()
680 {
681 _callback();
682 }
683 void run()
684 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530685 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700686 }
687
James Feista465ccc2019-02-08 12:51:01 -0800688 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800689 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800690 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530691 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700692 std::function<void(void)> _callback;
693};
694
Ed Tanous07d467b2021-02-23 14:48:37 -0800695void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300696 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800697 boost::container::flat_map<
698 std::pair<size_t, size_t>,
699 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530700 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800701 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530702 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800703{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300704 boost::container::flat_map<std::string, std::string> formattedFRU;
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530705
706 std::optional<std::string> optionalProductName = getProductName(
707 device, formattedFRU, bus, address, unknownBusObjectCount);
708 if (!optionalProductName)
James Feist3cb5fec2018-01-23 14:41:51 -0800709 {
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530710 std::cerr << "getProductName failed. product name is empty.\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800711 return;
712 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700713
Patrick Williamsdf190612023-05-10 07:51:34 -0500714 std::string productName = "/xyz/openbmc_project/FruDevice/" +
715 optionalProductName.value();
James Feist3cb5fec2018-01-23 14:41:51 -0800716
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530717 std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName);
718 if (index.has_value())
James Feist918e18c2018-02-13 15:51:07 -0800719 {
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530720 productName += "_";
721 productName += std::to_string(++(*index));
James Feist918e18c2018-02-13 15:51:07 -0800722 }
James Feist3cb5fec2018-01-23 14:41:51 -0800723
James Feist9eb0b582018-04-27 12:15:46 -0700724 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
725 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
726 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
727
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300728 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800729 {
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700730 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800731 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530732 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700733 {
734 continue;
735 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500736 std::string key = std::regex_replace(property.first, nonAsciiRegex,
737 "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530738
739 if (property.first == "PRODUCT_ASSET_TAG")
740 {
741 std::string propertyName = property.first;
742 iface->register_property(
743 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530744 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530745 &unknownBusObjectCount, &powerIsOn, &objServer,
746 &systemBus](const std::string& req, std::string& resp) {
Patrick Williamsdf190612023-05-10 07:51:34 -0500747 if (strcmp(req.c_str(), resp.c_str()) != 0)
748 {
749 // call the method which will update
750 if (updateFRUProperty(req, bus, address, propertyName,
751 dbusInterfaceMap,
752 unknownBusObjectCount, powerIsOn,
753 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530754 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500755 resp = req;
Joshi-Mansid73b7442020-03-27 19:10:21 +0530756 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500757 else
758 {
759 throw std::invalid_argument(
760 "FRU property update failed.");
761 }
762 }
763 return 1;
Joshi-Mansid73b7442020-03-27 19:10:21 +0530764 });
765 }
766 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700767 {
768 std::cerr << "illegal key: " << key << "\n";
769 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800770 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700771 {
772 std::cout << property.first << ": " << property.second << "\n";
773 }
James Feist3cb5fec2018-01-23 14:41:51 -0800774 }
James Feist2a9d6db2018-04-27 15:48:28 -0700775
776 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700777 iface->register_property("BUS", bus);
778 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700779
James Feist9eb0b582018-04-27 12:15:46 -0700780 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800781}
782
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300783static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800784{
785 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800786 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300787 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800788 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300789 baseboardFRUFile.seekg(0, std::ios_base::end);
790 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
791 baseboardFRU.resize(fileSize);
792 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700793 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
794 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
795 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800796 }
797 else
798 {
799 return false;
800 }
801 return true;
802}
803
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300804bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700805{
806 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800807 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700808 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300809 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700810 return false;
811 }
812 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800813 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700814 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300815 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700816 return false;
817 }
818 // baseboard fru
819 if (bus == 0 && address == 0)
820 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800821 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700822 if (!file.good())
823 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800824 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700825 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700826 return false;
827 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700828 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
829 const char* charOffset = reinterpret_cast<const char*>(fru.data());
830 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700831 return file.good();
832 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800833
834 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700835 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800836 auto path = getEepromPath(bus, address);
837 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
838 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700839 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800840 std::cerr << "unable to open i2c device " << path << "\n";
841 throw DBusInternalError();
842 return false;
843 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700844
Ed Tanous07d467b2021-02-23 14:48:37 -0800845 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
846 if (writtenBytes < 0)
847 {
848 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700849 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700850 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700851 return false;
852 }
853
Ed Tanous07d467b2021-02-23 14:48:37 -0800854 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700855 return true;
856 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800857
858 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
859
860 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
861 if (file < 0)
862 {
863 std::cerr << "unable to open i2c device " << i2cBus << "\n";
864 throw DBusInternalError();
865 return false;
866 }
867 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
868 {
869 std::cerr << "unable to set device address\n";
870 close(file);
871 throw DBusInternalError();
872 return false;
873 }
874
875 constexpr const size_t retryMax = 2;
876 uint16_t index = 0;
877 size_t retries = retryMax;
878 while (index < fru.size())
879 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700880 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800881 (retries == retryMax))
882 {
883 // The 4K EEPROM only uses the A2 and A1 device address bits
884 // with the third bit being a memory page address bit.
885 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
886 {
887 std::cerr << "unable to set device address\n";
888 close(file);
889 throw DBusInternalError();
890 return false;
891 }
892 }
893
894 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
895 fru[index]) < 0)
896 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700897 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800898 {
899 std::cerr << "error writing fru: " << strerror(errno) << "\n";
900 close(file);
901 throw DBusInternalError();
902 return false;
903 }
904 }
905 else
906 {
907 retries = retryMax;
908 index++;
909 }
910 // most eeproms require 5-10ms between writes
911 std::this_thread::sleep_for(std::chrono::milliseconds(10));
912 }
913 close(file);
914 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700915}
916
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800917void rescanOneBus(
krishnar4213ee212022-11-11 15:39:30 +0530918 BusMap& busmap, uint16_t busNum,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800919 boost::container::flat_map<
920 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700921 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800922 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530923 sdbusplus::asio::object_server& objServer,
924 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800925{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800926 for (auto& [pair, interface] : foundDevices)
927 {
928 if (pair.first == static_cast<size_t>(busNum))
929 {
930 objServer.remove_interface(interface);
931 foundDevices.erase(pair);
932 }
933 }
934
James Feist5d7f4692020-06-17 18:22:33 -0700935 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
936 if (!fs::exists(busPath))
937 {
938 if (dbusCall)
939 {
940 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
941 << "\n";
942 throw std::invalid_argument("Invalid Bus.");
943 }
944 return;
945 }
946
947 std::vector<fs::path> i2cBuses;
948 i2cBuses.emplace_back(busPath);
949
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800950 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530951 i2cBuses, busmap, powerIsOn, objServer,
952 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
953 &objServer, &systemBus]() {
Patrick Williamsdf190612023-05-10 07:51:34 -0500954 for (auto& busIface : dbusInterfaceMap)
955 {
956 if (busIface.first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800957 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500958 objServer.remove_interface(busIface.second);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800959 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500960 }
961 auto found = busmap.find(busNum);
962 if (found == busmap.end() || found->second == nullptr)
963 {
964 return;
965 }
966 for (auto& device : *(found->second))
967 {
968 addFruObjectToDbus(device.second, dbusInterfaceMap,
969 static_cast<uint32_t>(busNum), device.first,
970 unknownBusObjectCount, powerIsOn, objServer,
971 systemBus);
972 }
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800973 });
974 scan->run();
975}
976
James Feist9eb0b582018-04-27 12:15:46 -0700977void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700978 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800979 boost::container::flat_map<
980 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530981 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800982 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530983 sdbusplus::asio::object_server& objServer,
984 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -0800985{
Ed Tanousaa497ed2023-02-28 13:43:41 -0800986 static boost::asio::steady_timer timer(io);
987 timer.expires_from_now(std::chrono::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800988
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500989 // setup an async wait in case we get flooded with requests
Jian Zhang6e7cebf2022-11-29 18:30:21 +0800990 timer.async_wait([&](const boost::system::error_code& ec) {
991 if (ec == boost::asio::error::operation_aborted)
992 {
993 return;
994 }
995
996 if (ec)
997 {
998 std::cerr << "Error in timer: " << ec.message() << "\n";
999 return;
1000 }
1001
James Feist4131aea2018-03-09 09:47:30 -08001002 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001003 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001004
Nikhil Potaded8884f12019-03-27 13:27:13 -07001005 boost::container::flat_map<size_t, fs::path> busPaths;
1006 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001007 {
James Feist4131aea2018-03-09 09:47:30 -08001008 std::cerr << "unable to find i2c devices\n";
1009 return;
James Feist918e18c2018-02-13 15:51:07 -08001010 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001011
Ed Tanous07d467b2021-02-23 14:48:37 -08001012 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001013 {
1014 i2cBuses.emplace_back(busPath.second);
1015 }
James Feist4131aea2018-03-09 09:47:30 -08001016
James Feist98132792019-07-09 13:29:09 -07001017 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001018 for (auto& [pair, interface] : foundDevices)
1019 {
1020 objServer.remove_interface(interface);
1021 }
1022 foundDevices.clear();
1023
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301024 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301025 i2cBuses, busmap, powerIsOn, objServer, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001026 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001027 {
1028 objServer.remove_interface(busIface.second);
1029 }
James Feist4131aea2018-03-09 09:47:30 -08001030
James Feist6ebf9de2018-05-15 15:01:17 -07001031 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -08001032 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001033
James Feist6ebf9de2018-05-15 15:01:17 -07001034 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001035 std::vector<uint8_t> baseboardFRU;
1036 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001037 {
Helen Huangf69fe902021-02-19 16:18:22 +08001038 // If no device on i2c bus 0, the insertion will happen.
1039 auto bus0 =
1040 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1041 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001042 }
James Feist98132792019-07-09 13:29:09 -07001043 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001044 {
James Feista465ccc2019-02-08 12:51:01 -08001045 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001046 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001047 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301048 devicemap.first, device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301049 unknownBusObjectCount, powerIsOn,
1050 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001051 }
1052 }
1053 });
1054 scan->run();
1055 });
James Feist918e18c2018-02-13 15:51:07 -08001056}
1057
Joshi-Mansid73b7442020-03-27 19:10:21 +05301058// Details with example of Asset Tag Update
1059// To find location of Product Info Area asset tag as per FRU specification
1060// 1. Find product Info area starting offset (*8 - as header will be in
1061// multiple of 8 bytes).
1062// 2. Skip 3 bytes of product info area (like format version, area length,
1063// and language code).
1064// 3. Traverse manufacturer name, product name, product version, & product
1065// serial number, by reading type/length code to reach the Asset Tag.
1066// 4. Update the Asset Tag, reposition the product Info area in multiple of
1067// 8 bytes. Update the Product area length and checksum.
1068
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001069bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301070 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001071 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301072 boost::container::flat_map<
1073 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301074 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001075 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301076 sdbusplus::asio::object_server& objServer,
1077 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301078{
1079 size_t updatePropertyReqLen = updatePropertyReq.length();
1080 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1081 {
1082 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001083 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301084 "Invalid Length "
1085 << updatePropertyReqLen << "\n";
1086 return false;
1087 }
1088
1089 std::vector<uint8_t> fruData;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301090
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301091 if (!getFruData(fruData, bus, address))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301092 {
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301093 std::cerr << "Failure getting FRU Data \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301094 return false;
1095 }
1096
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +00001097 struct FruArea fruAreaParams
1098 {};
Joshi-Mansid73b7442020-03-27 19:10:21 +05301099
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301100 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301101 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301102 std::cerr << "findFruAreaLocationAndField failed \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301103 return false;
1104 }
1105
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301106 std::vector<uint8_t> restFRUAreaFieldsData;
1107 if (!copyRestFRUArea(fruData, propertyName, fruAreaParams,
1108 restFRUAreaFieldsData))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301109 {
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301110 std::cerr << "copyRestFRUArea failed \n";
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001111 return false;
1112 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301113
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001114 // Push post update fru areas if any
1115 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001116 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1117 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001118 {
1119 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001120 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301121 if ((fruAreaLoc > fruAreaParams.restFieldsEnd) &&
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001122 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1123 {
1124 nextFRUAreaLoc = fruAreaLoc;
1125 }
1126 }
1127 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001128 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001129 {
1130 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1131 fruData.size() - nextFRUAreaLoc,
1132 std::back_inserter(restFRUAreasData));
1133 }
1134
1135 // check FRU area size
1136 size_t fruAreaDataSize =
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301137 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) +
1138 restFRUAreaFieldsData.size());
1139 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001140 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1141 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001142#ifdef ENABLE_FRU_AREA_RESIZE
1143 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1144 // round size to 8-byte blocks
Patrick Williamsdf190612023-05-10 07:51:34 -05001145 newFRUAreaSize = ((newFRUAreaSize - 1) / fruBlockSize + 1) *
1146 fruBlockSize;
1147 size_t newFRUDataSize = fruData.size() + newFRUAreaSize -
1148 fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001149 fruData.resize(newFRUDataSize);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301150 fruAreaParams.size = newFRUAreaSize;
1151 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001152#else
1153 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1154 << " should not be greater than available FRU area size: "
1155 << fruAreaAvailableSize << "\n";
1156 return false;
1157#endif // ENABLE_FRU_AREA_RESIZE
1158 }
1159
Joshi-Mansid73b7442020-03-27 19:10:21 +05301160 // write new requested property field length and data
1161 constexpr uint8_t newTypeLenMask = 0xC0;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301162 fruData[fruAreaParams.updateFieldLoc] =
Joshi-Mansid73b7442020-03-27 19:10:21 +05301163 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301164 fruAreaParams.updateFieldLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301165 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301166 fruData.begin() + fruAreaParams.updateFieldLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301167
1168 // Copy remaining data to main fru area - post updated fru field vector
Patrick Williamsdf190612023-05-10 07:51:34 -05001169 fruAreaParams.restFieldsLoc = fruAreaParams.updateFieldLoc +
1170 updatePropertyReqLen;
1171 size_t fruAreaDataEnd = fruAreaParams.restFieldsLoc +
1172 restFRUAreaFieldsData.size();
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301173
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001174 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301175 fruData.begin() + fruAreaParams.restFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301176
1177 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001178 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301179 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301180
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001181#ifdef ENABLE_FRU_AREA_RESIZE
1182 ++nextFRUAreaNewLoc;
Patrick Williamsdf190612023-05-10 07:51:34 -05001183 ssize_t nextFRUAreaOffsetDiff = (nextFRUAreaNewLoc - nextFRUAreaLoc) /
1184 fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001185 // Append rest FRU Areas if size changed and there were other sections after
1186 // updated one
1187 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1188 {
1189 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1190 fruData.begin() + nextFRUAreaNewLoc);
1191 // Update Common Header
Ed Tanous6e22c872023-02-16 15:19:28 -08001192 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1193 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001194 {
Ed Tanous6e22c872023-02-16 15:19:28 -08001195 unsigned int fruAreaOffsetField =
1196 getHeaderAreaFieldOffset(nextFRUArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001197 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
Ed Tanous6e22c872023-02-16 15:19:28 -08001198 if (curFRUAreaOffset > fruAreaParams.end)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001199 {
1200 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1201 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1202 }
1203 }
1204 // Calculate new checksum
1205 std::vector<uint8_t> headerFRUData;
1206 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1207 size_t checksumVal = calculateChecksum(headerFRUData);
1208 fruData[7] = static_cast<uint8_t>(checksumVal);
1209 // fill zeros if FRU Area size decreased
1210 if (nextFRUAreaOffsetDiff < 0)
1211 {
1212 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1213 restFRUAreasData.size(),
1214 fruData.end(), 0);
1215 }
1216 }
1217#else
1218 // this is to avoid "unused variable" warning
1219 (void)nextFRUAreaNewLoc;
1220#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301221 if (fruData.empty())
1222 {
1223 return false;
1224 }
1225
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001226 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301227 fruData))
1228 {
1229 return false;
1230 }
1231
1232 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301233 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1234 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301235 return true;
1236}
1237
James Feist98132792019-07-09 13:29:09 -07001238int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001239{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301240 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1241 sdbusplus::asio::object_server objServer(systemBus);
1242
Kumar Thangavel41a90512021-11-24 15:44:20 +05301243 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301244 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001245 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001246 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001247 std::vector<fs::path> i2cBuses;
1248
James Feista3c180a2018-08-09 16:06:04 -07001249 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001250 {
1251 std::cerr << "unable to find i2c devices\n";
1252 return 1;
1253 }
James Feist3cb5fec2018-01-23 14:41:51 -08001254
Patrick Venture11f1ff42019-08-01 10:42:12 -07001255 // check for and load blacklist with initial buses.
1256 loadBlacklist(blacklistPath);
1257
Vijay Khemka065f6d92018-12-18 10:37:47 -08001258 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001259
James Feist6ebf9de2018-05-15 15:01:17 -07001260 // this is a map with keys of pair(bus number, address) and values of
1261 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001262 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001263 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1264 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001265
James Feist9eb0b582018-04-27 12:15:46 -07001266 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1267 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1268 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001269
Kumar Thangavel41a90512021-11-24 15:44:20 +05301270 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301271 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1272 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301273 });
James Feist2a9d6db2018-04-27 15:48:28 -07001274
krishnar4213ee212022-11-11 15:39:30 +05301275 iface->register_method("ReScanBus", [&](uint16_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301276 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301277 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001278 });
1279
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001280 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001281
Patrick Williamsdf190612023-05-10 07:51:34 -05001282 iface->register_method("WriteFru",
1283 [&](const uint16_t bus, const uint8_t address,
1284 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001285 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001286 {
James Feistddb78302018-09-06 11:45:42 -07001287 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001288 return;
1289 }
1290 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301291 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1292 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001293 });
James Feist9eb0b582018-04-27 12:15:46 -07001294 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001295
Patrick Williams2af39222022-07-22 19:26:56 -05001296 std::function<void(sdbusplus::message_t & message)> eventHandler =
1297 [&](sdbusplus::message_t& message) {
Patrick Williamsdf190612023-05-10 07:51:34 -05001298 std::string objectName;
1299 boost::container::flat_map<
1300 std::string,
1301 std::variant<std::string, bool, int64_t, uint64_t, double>>
1302 values;
1303 message.read(objectName, values);
1304 auto findState = values.find("CurrentHostState");
1305 if (findState != values.end())
1306 {
1307 if (std::get<std::string>(findState->second) ==
1308 "xyz.openbmc_project.State.Host.HostState.Running")
James Feist918e18c2018-02-13 15:51:07 -08001309 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001310 powerIsOn = true;
James Feist0eeb79c2019-10-09 13:35:29 -07001311 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001312 }
James Feist6ebf9de2018-05-15 15:01:17 -07001313
Patrick Williamsdf190612023-05-10 07:51:34 -05001314 if (powerIsOn)
1315 {
1316 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1317 powerIsOn, objServer, systemBus);
1318 }
1319 };
James Feist9eb0b582018-04-27 12:15:46 -07001320
Patrick Williams2af39222022-07-22 19:26:56 -05001321 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1322 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001323 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001324 "openbmc_project/state/"
1325 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001326 eventHandler);
1327
James Feist4131aea2018-03-09 09:47:30 -08001328 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001329 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001330 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001331 // monitor for new i2c devices
1332 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1333 std::function<void(const boost::system::error_code, std::size_t)>
Patrick Williamsdf190612023-05-10 07:51:34 -05001334 watchI2cBusses =
1335 [&](const boost::system::error_code& ec,
1336 std::size_t bytesTransferred) {
1337 if (ec)
1338 {
1339 std::cout << "Callback Error " << ec << "\n";
1340 return;
1341 }
1342 size_t index = 0;
1343 while ((index + sizeof(inotify_event)) <= bytesTransferred)
1344 {
1345 const char* p = &readBuffer[index];
1346 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1347 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
1348 switch (iEvent->mask)
James Feist4131aea2018-03-09 09:47:30 -08001349 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001350 case IN_CREATE:
1351 case IN_MOVED_TO:
1352 case IN_DELETE:
1353 std::string_view name(&iEvent->name[0], iEvent->len);
1354 if (boost::starts_with(name, "i2c"))
1355 {
1356 int bus = busStrToInt(name);
1357 if (bus < 0)
James Feist9eb0b582018-04-27 12:15:46 -07001358 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001359 std::cerr << "Could not parse bus " << name << "\n";
1360 continue;
1361 }
1362 int rootBus = getRootBus(bus);
1363 if (rootBus >= 0)
1364 {
1365 rescanOneBus(busMap, static_cast<uint16_t>(rootBus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301366 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301367 unknownBusObjectCount, powerIsOn,
1368 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001369 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001370 rescanOneBus(busMap, static_cast<uint16_t>(bus),
1371 dbusInterfaceMap, false,
1372 unknownBusObjectCount, powerIsOn,
1373 objServer, systemBus);
1374 }
James Feist4131aea2018-03-09 09:47:30 -08001375 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001376 index += sizeof(inotify_event) + iEvent->len;
1377 }
James Feist6ebf9de2018-05-15 15:01:17 -07001378
Patrick Williamsdf190612023-05-10 07:51:34 -05001379 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1380 watchI2cBusses);
1381 };
James Feist4131aea2018-03-09 09:47:30 -08001382
1383 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001384 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301385 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1386 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001387
James Feist3cb5fec2018-01-23 14:41:51 -08001388 io.run();
1389 return 0;
1390}