blob: fd083d0a6753370785ee6304f344d636a0f36ed0 [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>
Bonnie Lobfda2c42022-10-26 16:50:54 +080046#include <optional>
James Feist3f8a2782018-02-12 09:24:42 -080047#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070048#include <set>
49#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070050#include <string>
James Feist3b860982018-10-02 14:34:07 -070051#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080052#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080053#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070054#include <vector>
James Feist3b860982018-10-02 14:34:07 -070055
James Feist8c505da2020-05-28 10:06:33 -070056extern "C"
57{
James Feist3b860982018-10-02 14:34:07 -070058#include <i2c/smbus.h>
59#include <linux/i2c-dev.h>
60}
James Feist3cb5fec2018-01-23 14:41:51 -080061
Ed Tanous072e25d2018-12-16 21:45:20 -080062namespace fs = std::filesystem;
Ed Tanous07d467b2021-02-23 14:48:37 -080063static constexpr bool debug = false;
Ed Tanous07d467b2021-02-23 14:48:37 -080064constexpr size_t maxFruSize = 512;
65constexpr size_t maxEepromPageIndex = 255;
ankita prasad7329a222023-07-07 09:18:10 +000066constexpr size_t busTimeoutSeconds = 10;
James Feist3cb5fec2018-01-23 14:41:51 -080067
Patrick Venture11f1ff42019-08-01 10:42:12 -070068constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
69
Ed Tanous07d467b2021-02-23 14:48:37 -080070const static constexpr char* baseboardFruLocation =
James Feist3cb5fec2018-01-23 14:41:51 -080071 "/etc/fru/baseboard.fru.bin";
72
Ed Tanous07d467b2021-02-23 14:48:37 -080073const static constexpr char* i2CDevLocation = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080074
Bonnie Lobfda2c42022-10-26 16:50:54 +080075static boost::container::flat_map<size_t, std::optional<std::set<size_t>>>
76 busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070077struct FindDevicesWithCallback;
78
James Feist8a983922019-10-24 17:11:56 -070079static boost::container::flat_map<
80 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
81 foundDevices;
82
James Feist7972bb92019-11-13 15:59:24 -080083static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +000084static boost::container::flat_map<size_t, std::set<size_t>> fruAddresses;
James Feist7972bb92019-11-13 15:59:24 -080085
Ed Tanous34c3e012023-03-06 13:42:00 -080086boost::asio::io_context io;
James Feist5cb06082019-10-24 17:11:13 -070087
Andrei Kartashev2f0de172020-08-13 14:29:40 +030088bool updateFRUProperty(
Ed Tanous3013fb42022-07-09 08:27:06 -070089 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -080090 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +053091 boost::container::flat_map<
92 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +053093 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -080094 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +053095 sdbusplus::asio::object_server& objServer,
96 std::shared_ptr<sdbusplus::asio::connection>& systemBus);
Patrick Venturebaba7672019-10-26 09:26:41 -070097
Patrick Venturec50e1ff2019-08-06 10:22:28 -070098// Given a bus/address, produce the path in sysfs for an eeprom.
99static std::string getEepromPath(size_t bus, size_t address)
100{
101 std::stringstream output;
102 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
103 << std::setfill('0') << std::setw(4) << std::hex << address
104 << "/eeprom";
105 return output.str();
106}
107
108static bool hasEepromFile(size_t bus, size_t address)
109{
110 auto path = getEepromPath(bus, address);
111 try
112 {
113 return fs::exists(path);
114 }
115 catch (...)
116 {
117 return false;
118 }
119}
120
Zev Weiss309c0b12022-02-25 01:44:12 +0000121static int64_t readFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700122{
123 auto result = lseek(fd, offset, SEEK_SET);
124 if (result < 0)
125 {
126 std::cerr << "failed to seek\n";
127 return -1;
128 }
129
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700130 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700131}
132
Ed Tanous3013fb42022-07-09 08:27:06 -0700133static int busStrToInt(const std::string_view busName)
James Feist5d7f4692020-06-17 18:22:33 -0700134{
Ed Tanous07d467b2021-02-23 14:48:37 -0800135 auto findBus = busName.rfind('-');
James Feist5d7f4692020-06-17 18:22:33 -0700136 if (findBus == std::string::npos)
137 {
138 return -1;
139 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700140 std::string_view num = busName.substr(findBus + 1);
141 int val = 0;
142 std::from_chars(num.data(), num.data() + num.size(), val);
143 return val;
James Feist5d7f4692020-06-17 18:22:33 -0700144}
145
James Feist7972bb92019-11-13 15:59:24 -0800146static int getRootBus(size_t bus)
147{
148 auto ec = std::error_code();
149 auto path = std::filesystem::read_symlink(
150 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
151 std::to_string(bus) + "/mux_device"),
152 ec);
153 if (ec)
154 {
155 return -1;
156 }
157
158 std::string filename = path.filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800159 auto findBus = filename.find('-');
James Feist7972bb92019-11-13 15:59:24 -0800160 if (findBus == std::string::npos)
161 {
162 return -1;
163 }
164 return std::stoi(filename.substr(0, findBus));
165}
166
James Feistc95cb142018-02-26 10:41:42 -0800167static bool isMuxBus(size_t bus)
168{
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700169 auto ec = std::error_code();
170 auto isSymlink =
171 is_symlink(std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
172 std::to_string(bus) + "/mux_device"),
173 ec);
174 return (!ec && isSymlink);
James Feistc95cb142018-02-26 10:41:42 -0800175}
176
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530177static void makeProbeInterface(size_t bus, size_t address,
178 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700179{
180 if (isMuxBus(bus))
181 {
182 return; // the mux buses are random, no need to publish
183 }
184 auto [it, success] = foundDevices.emplace(
185 std::make_pair(bus, address),
186 objServer.add_interface(
187 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
188 std::to_string(address),
189 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
190 if (!success)
191 {
192 return; // already added
193 }
194 it->second->register_property("Bus", bus);
195 it->second->register_property("Address", address);
196 it->second->initialize();
197}
198
Zev Weiss309c0b12022-02-25 01:44:12 +0000199static std::optional<bool> isDevice16Bit(int file)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800200{
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700201 // Set the higher data word address bits to 0. It's safe on 8-bit addressing
202 // EEPROMs because it doesn't write any actual data.
203 int ret = i2c_smbus_write_byte(file, 0);
204 if (ret < 0)
205 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000206 return std::nullopt;
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700207 }
208
Vijay Khemka2d681f62018-11-06 15:51:00 -0800209 /* Get first byte */
210 int byte1 = i2c_smbus_read_byte_data(file, 0);
211 if (byte1 < 0)
212 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000213 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800214 }
215 /* Read 7 more bytes, it will read same first byte in case of
216 * 8 bit but it will read next byte in case of 16 bit
217 */
218 for (int i = 0; i < 7; i++)
219 {
220 int byte2 = i2c_smbus_read_byte_data(file, 0);
221 if (byte2 < 0)
222 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000223 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800224 }
225 if (byte2 != byte1)
226 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000227 return true;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800228 }
229 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000230 return false;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800231}
232
Xiang Liu2801a702020-01-20 14:29:34 -0800233// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
234// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800235static int i2cSmbusWriteThenRead(int file, uint16_t address,
236 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
237 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800238{
Ed Tanous07d467b2021-02-23 14:48:37 -0800239 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
240 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800241 {
242 return -1;
243 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800244
Ed Tanous3013fb42022-07-09 08:27:06 -0700245 constexpr size_t smbusWriteThenReadMsgCount = 2;
246 std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{};
247 struct i2c_rdwr_ioctl_data rdwr
248 {};
Xiang Liu2801a702020-01-20 14:29:34 -0800249
250 msgs[0].addr = address;
251 msgs[0].flags = 0;
252 msgs[0].len = toSlaveBufLen;
253 msgs[0].buf = toSlaveBuf;
254 msgs[1].addr = address;
255 msgs[1].flags = I2C_M_RD;
256 msgs[1].len = fromSlaveBufLen;
257 msgs[1].buf = fromSlaveBuf;
258
Ed Tanous3013fb42022-07-09 08:27:06 -0700259 rdwr.msgs = msgs.data();
260 rdwr.nmsgs = msgs.size();
Xiang Liu2801a702020-01-20 14:29:34 -0800261
262 int ret = ioctl(file, I2C_RDWR, &rdwr);
263
Jason M. Billsf6e4c1f2022-08-19 12:23:51 -0700264 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800265}
266
Marvin Drees2b3ed302023-04-14 16:35:14 +0200267static int64_t readData(bool is16bit, bool isBytewise, int file,
268 uint16_t address, off_t offset, size_t len,
269 uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800270{
Zev Weiss309c0b12022-02-25 01:44:12 +0000271 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800272 {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200273 if (!isBytewise)
274 {
275 return i2c_smbus_read_i2c_block_data(
276 file, static_cast<uint8_t>(offset), len, buf);
277 }
278
279 std::span<uint8_t> bufspan{buf, len};
280 for (size_t i = 0; i < len; i++)
281 {
282 int byte = i2c_smbus_read_byte_data(
283 file, static_cast<uint8_t>(offset + i));
284 if (byte < 0)
285 {
286 return static_cast<int64_t>(byte);
287 }
288 bufspan[i] = static_cast<uint8_t>(byte);
289 }
290 return static_cast<int64_t>(len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800291 }
292
Xiang Liu2801a702020-01-20 14:29:34 -0800293 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700294 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
295 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
296 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800297}
298
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700299// TODO: This code is very similar to the non-eeprom version and can be merged
300// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300301static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700302{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700303 auto path = getEepromPath(bus, address);
304
305 int file = open(path.c_str(), O_RDONLY);
306 if (file < 0)
307 {
308 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700309 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700310 }
311
Patrick Venturebaba7672019-10-26 09:26:41 -0700312 std::string errorMessage = "eeprom at " + std::to_string(bus) +
313 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000314 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
315 return readFromEeprom(file, offset, length, outbuf);
316 };
317 FRUReader reader(std::move(readFunc));
Marvin Drees2b3ed302023-04-14 16:35:14 +0200318 std::pair<std::vector<uint8_t>, bool> pair = readFRUContents(reader,
319 errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700320
321 close(file);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200322 return pair.first;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700323}
324
Bonnie Lobfda2c42022-10-26 16:50:54 +0800325std::set<size_t> findI2CEeproms(int i2cBus,
326 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700327{
Bonnie Lobfda2c42022-10-26 16:50:54 +0800328 std::set<size_t> foundList;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700329
330 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
331
332 // For each file listed under the i2c device
333 // NOTE: This should be faster than just checking for each possible address
334 // path.
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700335 auto ec = std::error_code();
336 for (const auto& p : fs::directory_iterator(path, ec))
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700337 {
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700338 if (ec)
339 {
340 std::cerr << "directory_iterator err " << ec.message() << "\n";
341 break;
342 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700343 const std::string node = p.path().string();
344 std::smatch m;
Patrick Williamsdf190612023-05-10 07:51:34 -0500345 bool found = std::regex_match(node, m,
346 std::regex(".+\\d+-([0-9abcdef]+$)"));
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700347
348 if (!found)
349 {
350 continue;
351 }
352 if (m.size() != 2)
353 {
354 std::cerr << "regex didn't capture\n";
355 continue;
356 }
357
358 std::ssub_match subMatch = m[1];
359 std::string addressString = subMatch.str();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800360 std::string_view addressStringView(addressString);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700361
Bonnie Lobfda2c42022-10-26 16:50:54 +0800362 size_t address = 0;
363 std::from_chars(addressStringView.begin(), addressStringView.end(),
364 address, 16);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700365
366 const std::string eeprom = node + "/eeprom";
367
368 try
369 {
370 if (!fs::exists(eeprom))
371 {
372 continue;
373 }
374 }
375 catch (...)
376 {
377 continue;
378 }
379
380 // There is an eeprom file at this address, it may have invalid
381 // contents, but we found it.
382 foundList.insert(address);
383
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300384 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700385 if (!device.empty())
386 {
387 devices->emplace(address, device);
388 }
389 }
390
391 return foundList;
392}
393
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300394int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800395 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530396 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800397{
James Feist26c27ad2018-07-25 15:09:40 -0700398 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700399 // NOTE: When reading the devices raw on the bus, it can interfere with
400 // the driver's ability to operate, therefore read eeproms first before
401 // scanning for devices without drivers. Several experiments were run
402 // and it was determined that if there were any devices on the bus
403 // before the eeprom was hit and read, the eeprom driver wouldn't open
404 // while the bus device was open. An experiment was not performed to see
405 // if this issue was resolved if the i2c bus device was closed, but
406 // hexdumps of the eeprom later were successful.
407
408 // Scan for i2c eeproms loaded on this bus.
Bonnie Lobfda2c42022-10-26 16:50:54 +0800409 std::set<size_t> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800410 std::set<size_t>& failedItems = failedAddresses[bus];
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000411 std::set<size_t>& foundItems = fruAddresses[bus];
412 foundItems.clear();
James Feist7972bb92019-11-13 15:59:24 -0800413
Bonnie Lobfda2c42022-10-26 16:50:54 +0800414 auto busFind = busBlacklist.find(bus);
415 if (busFind != busBlacklist.end())
416 {
417 if (busFind->second != std::nullopt)
418 {
419 for (const auto& address : *(busFind->second))
420 {
421 skipList.insert(address);
422 }
423 }
424 }
425
James Feist7972bb92019-11-13 15:59:24 -0800426 std::set<size_t>* rootFailures = nullptr;
427 int rootBus = getRootBus(bus);
428
429 if (rootBus >= 0)
430 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800431 auto rootBusFind = busBlacklist.find(rootBus);
432 if (rootBusFind != busBlacklist.end())
433 {
434 if (rootBusFind->second != std::nullopt)
435 {
436 for (const auto& rootAddress : *(rootBusFind->second))
437 {
438 skipList.insert(rootAddress);
439 }
440 }
441 }
James Feist7972bb92019-11-13 15:59:24 -0800442 rootFailures = &(failedAddresses[rootBus]);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000443 foundItems = fruAddresses[rootBus];
James Feist7972bb92019-11-13 15:59:24 -0800444 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700445
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530446 constexpr int startSkipSlaveAddr = 0;
447 constexpr int endSkipSlaveAddr = 12;
448
James Feist26c27ad2018-07-25 15:09:40 -0700449 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800450 {
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000451 if (foundItems.find(ii) != foundItems.end())
452 {
453 continue;
454 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700455 if (skipList.find(ii) != skipList.end())
456 {
457 continue;
458 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530459 // skipping since no device is present in this range
460 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
461 {
462 continue;
463 }
James Feist26c27ad2018-07-25 15:09:40 -0700464 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530465 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800466 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300467 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700468 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700469 continue;
470 }
471 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800472 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700473 {
474 continue;
475 }
James Feist3cb5fec2018-01-23 14:41:51 -0800476
Ed Tanous07d467b2021-02-23 14:48:37 -0800477 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700478 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700479 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700480 << "\n";
481 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800482
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530483 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700484
James Feist7972bb92019-11-13 15:59:24 -0800485 if (failedItems.find(ii) != failedItems.end())
486 {
487 // if we failed to read it once, unlikely we can read it later
488 continue;
489 }
490
491 if (rootFailures != nullptr)
492 {
493 if (rootFailures->find(ii) != rootFailures->end())
494 {
495 continue;
496 }
497 }
498
Vijay Khemka2d681f62018-11-06 15:51:00 -0800499 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000500 std::optional<bool> is16Bit = isDevice16Bit(file);
501 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800502 {
503 std::cerr << "failed to read bus " << bus << " address " << ii
504 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700505 if (powerIsOn)
506 {
507 failedItems.insert(ii);
508 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800509 continue;
510 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000511 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800512
Zev Weiss309c0b12022-02-25 01:44:12 +0000513 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
514 uint8_t* outbuf) {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200515 return readData(is16BitBool, false, file, ii, offset, length,
516 outbuf);
Zev Weiss309c0b12022-02-25 01:44:12 +0000517 };
518 FRUReader reader(std::move(readFunc));
Patrick Williamsdf190612023-05-10 07:51:34 -0500519 std::string errorMessage = "bus " + std::to_string(bus) +
520 " address " + std::to_string(ii);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200521 std::pair<std::vector<uint8_t>, bool> pair =
522 readFRUContents(reader, errorMessage);
523 const bool foundHeader = pair.second;
524
525 if (!foundHeader && !is16BitBool)
526 {
527 // certain FRU eeproms require bytewise reading.
528 // otherwise garbage is read. e.g. SuperMicro PWS 920P-SQ
529
530 auto readFunc = [is16BitBool, file, ii](off_t offset,
531 size_t length,
532 uint8_t* outbuf) {
533 return readData(is16BitBool, true, file, ii, offset, length,
534 outbuf);
535 };
536 FRUReader readerBytewise(std::move(readFunc));
537 pair = readFRUContents(readerBytewise, errorMessage);
538 }
539
540 if (pair.first.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700541 {
James Feist26c27ad2018-07-25 15:09:40 -0700542 continue;
543 }
James Feist26c27ad2018-07-25 15:09:40 -0700544
Marvin Drees2b3ed302023-04-14 16:35:14 +0200545 devices->emplace(ii, pair.first);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000546 fruAddresses[bus].insert(ii);
James Feist3cb5fec2018-01-23 14:41:51 -0800547 }
James Feist26c27ad2018-07-25 15:09:40 -0700548 return 1;
549 });
550 std::future_status status =
551 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
552 if (status == std::future_status::timeout)
553 {
554 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700555 if (powerIsOn)
556 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800557 busBlacklist[bus] = std::nullopt;
James Feistcb5661d2020-06-12 15:05:01 -0700558 }
James Feist444830e2019-04-05 08:38:16 -0700559 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700560 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800561 }
562
James Feist444830e2019-04-05 08:38:16 -0700563 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700564 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800565}
566
Patrick Venture11f1ff42019-08-01 10:42:12 -0700567void loadBlacklist(const char* path)
568{
569 std::ifstream blacklistStream(path);
570 if (!blacklistStream.good())
571 {
572 // File is optional.
573 std::cerr << "Cannot open blacklist file.\n\n";
574 return;
575 }
576
Patrick Williamsdf190612023-05-10 07:51:34 -0500577 nlohmann::json data = nlohmann::json::parse(blacklistStream, nullptr,
578 false);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700579 if (data.is_discarded())
580 {
581 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
582 "exiting\n";
583 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700584 }
585
586 // It's expected to have at least one field, "buses" that is an array of the
587 // buses by integer. Allow for future options to exclude further aspects,
588 // such as specific addresses or ranges.
589 if (data.type() != nlohmann::json::value_t::object)
590 {
591 std::cerr << "Illegal blacklist, expected to read dictionary\n";
592 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700593 }
594
595 // If buses field is missing, that's fine.
596 if (data.count("buses") == 1)
597 {
598 // Parse the buses array after a little validation.
599 auto buses = data.at("buses");
600 if (buses.type() != nlohmann::json::value_t::array)
601 {
602 // Buses field present but invalid, therefore this is an error.
603 std::cerr << "Invalid contents for blacklist buses field\n";
604 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700605 }
606
607 // Catch exception here for type mis-match.
608 try
609 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800610 for (const auto& busIterator : buses)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700611 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800612 // If bus and addresses field are missing, that's fine.
613 if (busIterator.contains("bus") &&
614 busIterator.contains("addresses"))
615 {
616 auto busData = busIterator.at("bus");
617 auto bus = busData.get<size_t>();
618
619 auto addressData = busIterator.at("addresses");
620 auto addresses =
621 addressData.get<std::set<std::string_view>>();
622
623 busBlacklist[bus].emplace();
624 for (const auto& address : addresses)
625 {
626 size_t addressInt = 0;
627 std::from_chars(address.begin() + 2, address.end(),
628 addressInt, 16);
629 busBlacklist[bus]->insert(addressInt);
630 }
631 }
632 else
633 {
634 busBlacklist[busIterator.get<size_t>()] = std::nullopt;
635 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700636 }
637 }
638 catch (const nlohmann::detail::type_error& e)
639 {
640 // Type mis-match is a critical error.
641 std::cerr << "Invalid bus type: " << e.what() << "\n";
642 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700643 }
644 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700645}
646
Ed Tanous07d467b2021-02-23 14:48:37 -0800647static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800648 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530649 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800650{
Ed Tanous3013fb42022-07-09 08:27:06 -0700651 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800652 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700653 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800654
James Feist5d7f4692020-06-17 18:22:33 -0700655 if (bus < 0)
656 {
657 std::cerr << "Cannot translate " << i2cBus << " to int\n";
658 continue;
659 }
Bonnie Lobfda2c42022-10-26 16:50:54 +0800660 auto busFind = busBlacklist.find(bus);
661 if (busFind != busBlacklist.end())
James Feist444830e2019-04-05 08:38:16 -0700662 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800663 if (busFind->second == std::nullopt)
664 {
665 continue; // Skip blocked busses.
666 }
James Feist444830e2019-04-05 08:38:16 -0700667 }
James Feist0c3980a2019-12-19 11:09:27 -0800668 int rootBus = getRootBus(bus);
Bonnie Lobfda2c42022-10-26 16:50:54 +0800669 auto rootBusFind = busBlacklist.find(rootBus);
670 if (rootBusFind != busBlacklist.end())
James Feist0c3980a2019-12-19 11:09:27 -0800671 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800672 if (rootBusFind->second == std::nullopt)
673 {
674 continue;
675 }
James Feist0c3980a2019-12-19 11:09:27 -0800676 }
677
James Feist3cb5fec2018-01-23 14:41:51 -0800678 auto file = open(i2cBus.c_str(), O_RDWR);
679 if (file < 0)
680 {
681 std::cerr << "unable to open i2c device " << i2cBus.string()
682 << "\n";
683 continue;
684 }
685 unsigned long funcs = 0;
686
687 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
688 {
689 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700690 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800691 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700692 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800693 continue;
694 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700695 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
696 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800697 {
698 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
699 << bus << "\n";
700 continue;
701 }
James Feist98132792019-07-09 13:29:09 -0700702 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800703 device = std::make_shared<DeviceMap>();
704
Nikhil Potaded8884f12019-03-27 13:27:13 -0700705 // i2cdetect by default uses the range 0x03 to 0x77, as
706 // this is what we have tested with, use this range. Could be
707 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800708 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800709 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700710 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800711 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700712
713 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530714 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700715
Ed Tanous07d467b2021-02-23 14:48:37 -0800716 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800717 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700718 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800719 }
James Feist3cb5fec2018-01-23 14:41:51 -0800720 }
James Feist3cb5fec2018-01-23 14:41:51 -0800721}
722
James Feist6ebf9de2018-05-15 15:01:17 -0700723// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700724struct FindDevicesWithCallback :
725 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700726{
James Feista465ccc2019-02-08 12:51:01 -0800727 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800728 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530729 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800730 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700731 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530732 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
733 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700734 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700735 ~FindDevicesWithCallback()
736 {
737 _callback();
738 }
739 void run()
740 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530741 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700742 }
743
James Feista465ccc2019-02-08 12:51:01 -0800744 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800745 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800746 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530747 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700748 std::function<void(void)> _callback;
749};
750
Ed Tanous07d467b2021-02-23 14:48:37 -0800751void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300752 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800753 boost::container::flat_map<
754 std::pair<size_t, size_t>,
755 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530756 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800757 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530758 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800759{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300760 boost::container::flat_map<std::string, std::string> formattedFRU;
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530761
762 std::optional<std::string> optionalProductName = getProductName(
763 device, formattedFRU, bus, address, unknownBusObjectCount);
764 if (!optionalProductName)
James Feist3cb5fec2018-01-23 14:41:51 -0800765 {
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530766 std::cerr << "getProductName failed. product name is empty.\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800767 return;
768 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700769
Patrick Williamsdf190612023-05-10 07:51:34 -0500770 std::string productName = "/xyz/openbmc_project/FruDevice/" +
771 optionalProductName.value();
James Feist3cb5fec2018-01-23 14:41:51 -0800772
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530773 std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName);
774 if (index.has_value())
James Feist918e18c2018-02-13 15:51:07 -0800775 {
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530776 productName += "_";
777 productName += std::to_string(++(*index));
James Feist918e18c2018-02-13 15:51:07 -0800778 }
James Feist3cb5fec2018-01-23 14:41:51 -0800779
James Feist9eb0b582018-04-27 12:15:46 -0700780 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
781 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
782 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
783
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300784 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800785 {
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700786 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800787 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530788 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700789 {
790 continue;
791 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500792 std::string key = std::regex_replace(property.first, nonAsciiRegex,
793 "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530794
795 if (property.first == "PRODUCT_ASSET_TAG")
796 {
797 std::string propertyName = property.first;
798 iface->register_property(
799 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530800 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530801 &unknownBusObjectCount, &powerIsOn, &objServer,
802 &systemBus](const std::string& req, std::string& resp) {
Patrick Williamsdf190612023-05-10 07:51:34 -0500803 if (strcmp(req.c_str(), resp.c_str()) != 0)
804 {
805 // call the method which will update
806 if (updateFRUProperty(req, bus, address, propertyName,
807 dbusInterfaceMap,
808 unknownBusObjectCount, powerIsOn,
809 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530810 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500811 resp = req;
Joshi-Mansid73b7442020-03-27 19:10:21 +0530812 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500813 else
814 {
815 throw std::invalid_argument(
816 "FRU property update failed.");
817 }
818 }
819 return 1;
Patrick Williamsb9dd7f82023-10-20 11:20:11 -0500820 });
Joshi-Mansid73b7442020-03-27 19:10:21 +0530821 }
822 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700823 {
824 std::cerr << "illegal key: " << key << "\n";
825 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800826 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700827 {
828 std::cout << property.first << ": " << property.second << "\n";
829 }
James Feist3cb5fec2018-01-23 14:41:51 -0800830 }
James Feist2a9d6db2018-04-27 15:48:28 -0700831
832 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700833 iface->register_property("BUS", bus);
834 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700835
James Feist9eb0b582018-04-27 12:15:46 -0700836 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800837}
838
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300839static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800840{
841 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800842 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300843 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800844 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300845 baseboardFRUFile.seekg(0, std::ios_base::end);
846 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
847 baseboardFRU.resize(fileSize);
848 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700849 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
850 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
851 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800852 }
853 else
854 {
855 return false;
856 }
857 return true;
858}
859
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300860bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700861{
862 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800863 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700864 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300865 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700866 return false;
867 }
868 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800869 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700870 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300871 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700872 return false;
873 }
874 // baseboard fru
875 if (bus == 0 && address == 0)
876 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800877 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700878 if (!file.good())
879 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800880 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700881 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700882 return false;
883 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700884 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
885 const char* charOffset = reinterpret_cast<const char*>(fru.data());
886 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700887 return file.good();
888 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800889
890 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700891 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800892 auto path = getEepromPath(bus, address);
893 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
894 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700895 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800896 std::cerr << "unable to open i2c device " << path << "\n";
897 throw DBusInternalError();
898 return false;
899 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700900
Ed Tanous07d467b2021-02-23 14:48:37 -0800901 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
902 if (writtenBytes < 0)
903 {
904 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700905 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700906 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700907 return false;
908 }
909
Ed Tanous07d467b2021-02-23 14:48:37 -0800910 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700911 return true;
912 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800913
914 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
915
916 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
917 if (file < 0)
918 {
919 std::cerr << "unable to open i2c device " << i2cBus << "\n";
920 throw DBusInternalError();
921 return false;
922 }
923 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
924 {
925 std::cerr << "unable to set device address\n";
926 close(file);
927 throw DBusInternalError();
928 return false;
929 }
930
931 constexpr const size_t retryMax = 2;
932 uint16_t index = 0;
933 size_t retries = retryMax;
934 while (index < fru.size())
935 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700936 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800937 (retries == retryMax))
938 {
939 // The 4K EEPROM only uses the A2 and A1 device address bits
940 // with the third bit being a memory page address bit.
941 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
942 {
943 std::cerr << "unable to set device address\n";
944 close(file);
945 throw DBusInternalError();
946 return false;
947 }
948 }
949
950 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
951 fru[index]) < 0)
952 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700953 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800954 {
955 std::cerr << "error writing fru: " << strerror(errno) << "\n";
956 close(file);
957 throw DBusInternalError();
958 return false;
959 }
960 }
961 else
962 {
963 retries = retryMax;
964 index++;
965 }
966 // most eeproms require 5-10ms between writes
967 std::this_thread::sleep_for(std::chrono::milliseconds(10));
968 }
969 close(file);
970 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700971}
972
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800973void rescanOneBus(
krishnar4213ee212022-11-11 15:39:30 +0530974 BusMap& busmap, uint16_t busNum,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800975 boost::container::flat_map<
976 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700977 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800978 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530979 sdbusplus::asio::object_server& objServer,
980 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800981{
Yong Li6fdfac02023-10-30 11:21:30 +0800982 for (auto device = foundDevices.begin(); device != foundDevices.end();)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800983 {
Yong Li6fdfac02023-10-30 11:21:30 +0800984 if (device->first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800985 {
Yong Li6fdfac02023-10-30 11:21:30 +0800986 objServer.remove_interface(device->second);
987 device = foundDevices.erase(device);
988 }
989 else
990 {
991 device++;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800992 }
993 }
994
James Feist5d7f4692020-06-17 18:22:33 -0700995 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
996 if (!fs::exists(busPath))
997 {
998 if (dbusCall)
999 {
1000 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
1001 << "\n";
1002 throw std::invalid_argument("Invalid Bus.");
1003 }
1004 return;
1005 }
1006
1007 std::vector<fs::path> i2cBuses;
1008 i2cBuses.emplace_back(busPath);
1009
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001010 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301011 i2cBuses, busmap, powerIsOn, objServer,
1012 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
1013 &objServer, &systemBus]() {
Yong Li6fdfac02023-10-30 11:21:30 +08001014 for (auto busIface = dbusInterfaceMap.begin();
1015 busIface != dbusInterfaceMap.end();)
Patrick Williamsdf190612023-05-10 07:51:34 -05001016 {
Yong Li6fdfac02023-10-30 11:21:30 +08001017 if (busIface->first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001018 {
Yong Li6fdfac02023-10-30 11:21:30 +08001019 objServer.remove_interface(busIface->second);
1020 busIface = dbusInterfaceMap.erase(busIface);
1021 }
1022 else
1023 {
1024 busIface++;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001025 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001026 }
1027 auto found = busmap.find(busNum);
1028 if (found == busmap.end() || found->second == nullptr)
1029 {
1030 return;
1031 }
1032 for (auto& device : *(found->second))
1033 {
1034 addFruObjectToDbus(device.second, dbusInterfaceMap,
1035 static_cast<uint32_t>(busNum), device.first,
1036 unknownBusObjectCount, powerIsOn, objServer,
1037 systemBus);
1038 }
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001039 });
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001040 scan->run();
1041}
1042
James Feist9eb0b582018-04-27 12:15:46 -07001043void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -07001044 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -08001045 boost::container::flat_map<
1046 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301047 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001048 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301049 sdbusplus::asio::object_server& objServer,
1050 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -08001051{
Ed Tanousaa497ed2023-02-28 13:43:41 -08001052 static boost::asio::steady_timer timer(io);
1053 timer.expires_from_now(std::chrono::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001054
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001055 // setup an async wait in case we get flooded with requests
Jian Zhang6e7cebf2022-11-29 18:30:21 +08001056 timer.async_wait([&](const boost::system::error_code& ec) {
1057 if (ec == boost::asio::error::operation_aborted)
1058 {
1059 return;
1060 }
1061
1062 if (ec)
1063 {
1064 std::cerr << "Error in timer: " << ec.message() << "\n";
1065 return;
1066 }
1067
James Feist4131aea2018-03-09 09:47:30 -08001068 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001069 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001070
Nikhil Potaded8884f12019-03-27 13:27:13 -07001071 boost::container::flat_map<size_t, fs::path> busPaths;
1072 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001073 {
James Feist4131aea2018-03-09 09:47:30 -08001074 std::cerr << "unable to find i2c devices\n";
1075 return;
James Feist918e18c2018-02-13 15:51:07 -08001076 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001077
Ed Tanous07d467b2021-02-23 14:48:37 -08001078 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001079 {
1080 i2cBuses.emplace_back(busPath.second);
1081 }
James Feist4131aea2018-03-09 09:47:30 -08001082
James Feist98132792019-07-09 13:29:09 -07001083 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001084 for (auto& [pair, interface] : foundDevices)
1085 {
1086 objServer.remove_interface(interface);
1087 }
1088 foundDevices.clear();
1089
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301090 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301091 i2cBuses, busmap, powerIsOn, objServer, [&]() {
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001092 for (auto& busIface : dbusInterfaceMap)
1093 {
1094 objServer.remove_interface(busIface.second);
1095 }
James Feist4131aea2018-03-09 09:47:30 -08001096
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001097 dbusInterfaceMap.clear();
1098 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001099
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001100 // todo, get this from a more sensable place
1101 std::vector<uint8_t> baseboardFRU;
1102 if (readBaseboardFRU(baseboardFRU))
1103 {
1104 // If no device on i2c bus 0, the insertion will happen.
1105 auto bus0 = busmap.try_emplace(0,
1106 std::make_shared<DeviceMap>());
1107 bus0.first->second->emplace(0, baseboardFRU);
1108 }
1109 for (auto& devicemap : busmap)
1110 {
1111 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001112 {
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001113 addFruObjectToDbus(device.second, dbusInterfaceMap,
1114 devicemap.first, device.first,
1115 unknownBusObjectCount, powerIsOn,
1116 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001117 }
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001118 }
1119 });
James Feist6ebf9de2018-05-15 15:01:17 -07001120 scan->run();
1121 });
James Feist918e18c2018-02-13 15:51:07 -08001122}
1123
Joshi-Mansid73b7442020-03-27 19:10:21 +05301124// Details with example of Asset Tag Update
1125// To find location of Product Info Area asset tag as per FRU specification
1126// 1. Find product Info area starting offset (*8 - as header will be in
1127// multiple of 8 bytes).
1128// 2. Skip 3 bytes of product info area (like format version, area length,
1129// and language code).
1130// 3. Traverse manufacturer name, product name, product version, & product
1131// serial number, by reading type/length code to reach the Asset Tag.
1132// 4. Update the Asset Tag, reposition the product Info area in multiple of
1133// 8 bytes. Update the Product area length and checksum.
1134
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001135bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301136 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001137 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301138 boost::container::flat_map<
1139 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301140 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001141 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301142 sdbusplus::asio::object_server& objServer,
1143 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301144{
1145 size_t updatePropertyReqLen = updatePropertyReq.length();
1146 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1147 {
1148 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001149 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301150 "Invalid Length "
1151 << updatePropertyReqLen << "\n";
1152 return false;
1153 }
1154
1155 std::vector<uint8_t> fruData;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301156
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301157 if (!getFruData(fruData, bus, address))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301158 {
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301159 std::cerr << "Failure getting FRU Data \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301160 return false;
1161 }
1162
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +00001163 struct FruArea fruAreaParams
1164 {};
Joshi-Mansid73b7442020-03-27 19:10:21 +05301165
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301166 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301167 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301168 std::cerr << "findFruAreaLocationAndField failed \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301169 return false;
1170 }
1171
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301172 std::vector<uint8_t> restFRUAreaFieldsData;
1173 if (!copyRestFRUArea(fruData, propertyName, fruAreaParams,
1174 restFRUAreaFieldsData))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301175 {
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301176 std::cerr << "copyRestFRUArea failed \n";
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001177 return false;
1178 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301179
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001180 // Push post update fru areas if any
1181 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001182 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1183 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001184 {
1185 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001186 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301187 if ((fruAreaLoc > fruAreaParams.restFieldsEnd) &&
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001188 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1189 {
1190 nextFRUAreaLoc = fruAreaLoc;
1191 }
1192 }
1193 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001194 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001195 {
1196 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1197 fruData.size() - nextFRUAreaLoc,
1198 std::back_inserter(restFRUAreasData));
1199 }
1200
1201 // check FRU area size
1202 size_t fruAreaDataSize =
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301203 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) +
1204 restFRUAreaFieldsData.size());
1205 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001206 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1207 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001208#ifdef ENABLE_FRU_AREA_RESIZE
1209 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1210 // round size to 8-byte blocks
Patrick Williamsdf190612023-05-10 07:51:34 -05001211 newFRUAreaSize = ((newFRUAreaSize - 1) / fruBlockSize + 1) *
1212 fruBlockSize;
1213 size_t newFRUDataSize = fruData.size() + newFRUAreaSize -
1214 fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001215 fruData.resize(newFRUDataSize);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301216 fruAreaParams.size = newFRUAreaSize;
1217 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001218#else
1219 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1220 << " should not be greater than available FRU area size: "
1221 << fruAreaAvailableSize << "\n";
1222 return false;
1223#endif // ENABLE_FRU_AREA_RESIZE
1224 }
1225
Joshi-Mansid73b7442020-03-27 19:10:21 +05301226 // write new requested property field length and data
1227 constexpr uint8_t newTypeLenMask = 0xC0;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301228 fruData[fruAreaParams.updateFieldLoc] =
Joshi-Mansid73b7442020-03-27 19:10:21 +05301229 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301230 fruAreaParams.updateFieldLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301231 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301232 fruData.begin() + fruAreaParams.updateFieldLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301233
1234 // Copy remaining data to main fru area - post updated fru field vector
Patrick Williamsdf190612023-05-10 07:51:34 -05001235 fruAreaParams.restFieldsLoc = fruAreaParams.updateFieldLoc +
1236 updatePropertyReqLen;
1237 size_t fruAreaDataEnd = fruAreaParams.restFieldsLoc +
1238 restFRUAreaFieldsData.size();
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301239
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001240 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301241 fruData.begin() + fruAreaParams.restFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301242
1243 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001244 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301245 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301246
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001247#ifdef ENABLE_FRU_AREA_RESIZE
1248 ++nextFRUAreaNewLoc;
Patrick Williamsdf190612023-05-10 07:51:34 -05001249 ssize_t nextFRUAreaOffsetDiff = (nextFRUAreaNewLoc - nextFRUAreaLoc) /
1250 fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001251 // Append rest FRU Areas if size changed and there were other sections after
1252 // updated one
1253 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1254 {
1255 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1256 fruData.begin() + nextFRUAreaNewLoc);
1257 // Update Common Header
Ed Tanous6e22c872023-02-16 15:19:28 -08001258 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1259 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001260 {
Ed Tanous6e22c872023-02-16 15:19:28 -08001261 unsigned int fruAreaOffsetField =
1262 getHeaderAreaFieldOffset(nextFRUArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001263 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
Ed Tanous6e22c872023-02-16 15:19:28 -08001264 if (curFRUAreaOffset > fruAreaParams.end)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001265 {
1266 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1267 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1268 }
1269 }
1270 // Calculate new checksum
1271 std::vector<uint8_t> headerFRUData;
1272 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1273 size_t checksumVal = calculateChecksum(headerFRUData);
1274 fruData[7] = static_cast<uint8_t>(checksumVal);
1275 // fill zeros if FRU Area size decreased
1276 if (nextFRUAreaOffsetDiff < 0)
1277 {
1278 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1279 restFRUAreasData.size(),
1280 fruData.end(), 0);
1281 }
1282 }
1283#else
1284 // this is to avoid "unused variable" warning
1285 (void)nextFRUAreaNewLoc;
1286#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301287 if (fruData.empty())
1288 {
1289 return false;
1290 }
1291
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001292 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301293 fruData))
1294 {
1295 return false;
1296 }
1297
1298 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301299 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1300 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301301 return true;
1302}
1303
James Feist98132792019-07-09 13:29:09 -07001304int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001305{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301306 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1307 sdbusplus::asio::object_server objServer(systemBus);
1308
Kumar Thangavel41a90512021-11-24 15:44:20 +05301309 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301310 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001311 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001312 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001313 std::vector<fs::path> i2cBuses;
1314
James Feista3c180a2018-08-09 16:06:04 -07001315 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001316 {
1317 std::cerr << "unable to find i2c devices\n";
1318 return 1;
1319 }
James Feist3cb5fec2018-01-23 14:41:51 -08001320
Patrick Venture11f1ff42019-08-01 10:42:12 -07001321 // check for and load blacklist with initial buses.
1322 loadBlacklist(blacklistPath);
1323
Vijay Khemka065f6d92018-12-18 10:37:47 -08001324 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001325
James Feist6ebf9de2018-05-15 15:01:17 -07001326 // this is a map with keys of pair(bus number, address) and values of
1327 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001328 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001329 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1330 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001331
James Feist9eb0b582018-04-27 12:15:46 -07001332 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1333 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1334 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001335
Kumar Thangavel41a90512021-11-24 15:44:20 +05301336 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301337 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1338 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301339 });
James Feist2a9d6db2018-04-27 15:48:28 -07001340
krishnar4213ee212022-11-11 15:39:30 +05301341 iface->register_method("ReScanBus", [&](uint16_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301342 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301343 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001344 });
1345
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001346 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001347
Patrick Williamsdf190612023-05-10 07:51:34 -05001348 iface->register_method("WriteFru",
1349 [&](const uint16_t bus, const uint8_t address,
1350 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001351 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001352 {
James Feistddb78302018-09-06 11:45:42 -07001353 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001354 return;
1355 }
1356 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301357 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1358 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001359 });
James Feist9eb0b582018-04-27 12:15:46 -07001360 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001361
Patrick Williams2af39222022-07-22 19:26:56 -05001362 std::function<void(sdbusplus::message_t & message)> eventHandler =
1363 [&](sdbusplus::message_t& message) {
Patrick Williamsdf190612023-05-10 07:51:34 -05001364 std::string objectName;
1365 boost::container::flat_map<
1366 std::string,
1367 std::variant<std::string, bool, int64_t, uint64_t, double>>
1368 values;
1369 message.read(objectName, values);
1370 auto findState = values.find("CurrentHostState");
1371 if (findState != values.end())
1372 {
1373 if (std::get<std::string>(findState->second) ==
1374 "xyz.openbmc_project.State.Host.HostState.Running")
James Feist918e18c2018-02-13 15:51:07 -08001375 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001376 powerIsOn = true;
James Feist0eeb79c2019-10-09 13:35:29 -07001377 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001378 }
James Feist6ebf9de2018-05-15 15:01:17 -07001379
Patrick Williamsdf190612023-05-10 07:51:34 -05001380 if (powerIsOn)
1381 {
1382 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1383 powerIsOn, objServer, systemBus);
1384 }
1385 };
James Feist9eb0b582018-04-27 12:15:46 -07001386
Patrick Williams2af39222022-07-22 19:26:56 -05001387 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1388 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001389 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001390 "openbmc_project/state/"
1391 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001392 eventHandler);
1393
James Feist4131aea2018-03-09 09:47:30 -08001394 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001395 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001396 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001397 // monitor for new i2c devices
1398 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1399 std::function<void(const boost::system::error_code, std::size_t)>
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001400 watchI2cBusses = [&](const boost::system::error_code& ec,
1401 std::size_t bytesTransferred) {
Patrick Williamsdf190612023-05-10 07:51:34 -05001402 if (ec)
1403 {
1404 std::cout << "Callback Error " << ec << "\n";
1405 return;
1406 }
1407 size_t index = 0;
1408 while ((index + sizeof(inotify_event)) <= bytesTransferred)
1409 {
1410 const char* p = &readBuffer[index];
1411 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1412 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
1413 switch (iEvent->mask)
James Feist4131aea2018-03-09 09:47:30 -08001414 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001415 case IN_CREATE:
1416 case IN_MOVED_TO:
1417 case IN_DELETE:
1418 std::string_view name(&iEvent->name[0], iEvent->len);
1419 if (boost::starts_with(name, "i2c"))
1420 {
1421 int bus = busStrToInt(name);
1422 if (bus < 0)
James Feist9eb0b582018-04-27 12:15:46 -07001423 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001424 std::cerr << "Could not parse bus " << name << "\n";
1425 continue;
1426 }
1427 int rootBus = getRootBus(bus);
1428 if (rootBus >= 0)
1429 {
1430 rescanOneBus(busMap, static_cast<uint16_t>(rootBus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301431 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301432 unknownBusObjectCount, powerIsOn,
1433 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001434 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001435 rescanOneBus(busMap, static_cast<uint16_t>(bus),
1436 dbusInterfaceMap, false,
1437 unknownBusObjectCount, powerIsOn,
1438 objServer, systemBus);
1439 }
James Feist4131aea2018-03-09 09:47:30 -08001440 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001441 index += sizeof(inotify_event) + iEvent->len;
1442 }
James Feist6ebf9de2018-05-15 15:01:17 -07001443
Patrick Williamsdf190612023-05-10 07:51:34 -05001444 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1445 watchI2cBusses);
1446 };
James Feist4131aea2018-03-09 09:47:30 -08001447
1448 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001449 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301450 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1451 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001452
James Feist3cb5fec2018-01-23 14:41:51 -08001453 io.run();
1454 return 0;
1455}