blob: 403f6eb7d493e321746aef673e728a435060b7b2 [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>
Alexander Hansenc3db2c32024-08-20 15:01:38 +020030#include <phosphor-logging/lg2.hpp>
James Feist8c505da2020-05-28 10:06:33 -070031#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33
34#include <array>
Ed Tanous07d467b2021-02-23 14:48:37 -080035#include <cerrno>
Ed Tanous3013fb42022-07-09 08:27:06 -070036#include <charconv>
James Feist3b860982018-10-02 14:34:07 -070037#include <chrono>
38#include <ctime>
Patrick Venturee3754002019-08-06 09:39:12 -070039#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080040#include <fstream>
Patrick Venturebaba7672019-10-26 09:26:41 -070041#include <functional>
James Feist3cb5fec2018-01-23 14:41:51 -080042#include <future>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070043#include <iomanip>
James Feist3cb5fec2018-01-23 14:41:51 -080044#include <iostream>
Patrick Venturee26395d2019-10-29 14:05:16 -070045#include <limits>
Andrew Jefferya9c58922021-06-01 09:28:59 +093046#include <map>
Bonnie Lobfda2c42022-10-26 16:50:54 +080047#include <optional>
James Feist3f8a2782018-02-12 09:24:42 -080048#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070049#include <set>
50#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070051#include <string>
James Feist3b860982018-10-02 14:34:07 -070052#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080053#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080054#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070055#include <vector>
James Feist3b860982018-10-02 14:34:07 -070056
James Feist8c505da2020-05-28 10:06:33 -070057extern "C"
58{
James Feist3b860982018-10-02 14:34:07 -070059#include <i2c/smbus.h>
60#include <linux/i2c-dev.h>
61}
James Feist3cb5fec2018-01-23 14:41:51 -080062
Ed Tanous072e25d2018-12-16 21:45:20 -080063namespace fs = std::filesystem;
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
Matt Simmering2447c242023-11-09 14:18:39 -080068constexpr const char* blocklistPath = PACKAGE_DIR "blacklist.json";
Patrick Venture11f1ff42019-08-01 10:42:12 -070069
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
Ed Tanousfc171422024-04-04 17:18:16 -070075// TODO Refactor these to not be globals
76// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
Bonnie Lobfda2c42022-10-26 16:50:54 +080077static boost::container::flat_map<size_t, std::optional<std::set<size_t>>>
Matt Simmering2447c242023-11-09 14:18:39 -080078 busBlocklist;
James Feist6ebf9de2018-05-15 15:01:17 -070079struct FindDevicesWithCallback;
80
James Feist8a983922019-10-24 17:11:56 -070081static boost::container::flat_map<
82 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
83 foundDevices;
84
James Feist7972bb92019-11-13 15:59:24 -080085static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +000086static boost::container::flat_map<size_t, std::set<size_t>> fruAddresses;
James Feist7972bb92019-11-13 15:59:24 -080087
Ed Tanous34c3e012023-03-06 13:42:00 -080088boost::asio::io_context io;
Ed Tanousfc171422024-04-04 17:18:16 -070089// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
James Feist5cb06082019-10-24 17:11:13 -070090
Andrei Kartashev2f0de172020-08-13 14:29:40 +030091bool updateFRUProperty(
Ed Tanous3013fb42022-07-09 08:27:06 -070092 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -080093 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +053094 boost::container::flat_map<
95 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +053096 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -080097 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +053098 sdbusplus::asio::object_server& objServer,
99 std::shared_ptr<sdbusplus::asio::connection>& systemBus);
Patrick Venturebaba7672019-10-26 09:26:41 -0700100
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700101// Given a bus/address, produce the path in sysfs for an eeprom.
102static std::string getEepromPath(size_t bus, size_t address)
103{
104 std::stringstream output;
105 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
106 << std::setfill('0') << std::setw(4) << std::hex << address
107 << "/eeprom";
108 return output.str();
109}
110
111static bool hasEepromFile(size_t bus, size_t address)
112{
113 auto path = getEepromPath(bus, address);
114 try
115 {
116 return fs::exists(path);
117 }
118 catch (...)
119 {
120 return false;
121 }
122}
123
Zev Weiss309c0b12022-02-25 01:44:12 +0000124static int64_t readFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700125{
126 auto result = lseek(fd, offset, SEEK_SET);
127 if (result < 0)
128 {
129 std::cerr << "failed to seek\n";
130 return -1;
131 }
132
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700133 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700134}
135
Ed Tanous3013fb42022-07-09 08:27:06 -0700136static int busStrToInt(const std::string_view busName)
James Feist5d7f4692020-06-17 18:22:33 -0700137{
Ed Tanous07d467b2021-02-23 14:48:37 -0800138 auto findBus = busName.rfind('-');
James Feist5d7f4692020-06-17 18:22:33 -0700139 if (findBus == std::string::npos)
140 {
141 return -1;
142 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700143 std::string_view num = busName.substr(findBus + 1);
144 int val = 0;
145 std::from_chars(num.data(), num.data() + num.size(), val);
146 return val;
James Feist5d7f4692020-06-17 18:22:33 -0700147}
148
James Feist7972bb92019-11-13 15:59:24 -0800149static int getRootBus(size_t bus)
150{
151 auto ec = std::error_code();
152 auto path = std::filesystem::read_symlink(
Patrick Williamsb7077432024-08-16 15:22:21 -0400153 std::filesystem::path(
154 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"),
James Feist7972bb92019-11-13 15:59:24 -0800155 ec);
156 if (ec)
157 {
158 return -1;
159 }
160
161 std::string filename = path.filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800162 auto findBus = filename.find('-');
James Feist7972bb92019-11-13 15:59:24 -0800163 if (findBus == std::string::npos)
164 {
165 return -1;
166 }
167 return std::stoi(filename.substr(0, findBus));
168}
169
James Feistc95cb142018-02-26 10:41:42 -0800170static bool isMuxBus(size_t bus)
171{
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700172 auto ec = std::error_code();
173 auto isSymlink =
174 is_symlink(std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
175 std::to_string(bus) + "/mux_device"),
176 ec);
177 return (!ec && isSymlink);
James Feistc95cb142018-02-26 10:41:42 -0800178}
179
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530180static void makeProbeInterface(size_t bus, size_t address,
181 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700182{
183 if (isMuxBus(bus))
184 {
185 return; // the mux buses are random, no need to publish
186 }
187 auto [it, success] = foundDevices.emplace(
188 std::make_pair(bus, address),
189 objServer.add_interface(
190 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
191 std::to_string(address),
192 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
193 if (!success)
194 {
195 return; // already added
196 }
197 it->second->register_property("Bus", bus);
198 it->second->register_property("Address", address);
199 it->second->initialize();
200}
201
Zev Weiss309c0b12022-02-25 01:44:12 +0000202static std::optional<bool> isDevice16Bit(int file)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800203{
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700204 // Set the higher data word address bits to 0. It's safe on 8-bit addressing
205 // EEPROMs because it doesn't write any actual data.
206 int ret = i2c_smbus_write_byte(file, 0);
207 if (ret < 0)
208 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000209 return std::nullopt;
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700210 }
211
Vijay Khemka2d681f62018-11-06 15:51:00 -0800212 /* Get first byte */
213 int byte1 = i2c_smbus_read_byte_data(file, 0);
214 if (byte1 < 0)
215 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000216 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800217 }
218 /* Read 7 more bytes, it will read same first byte in case of
219 * 8 bit but it will read next byte in case of 16 bit
220 */
221 for (int i = 0; i < 7; i++)
222 {
223 int byte2 = i2c_smbus_read_byte_data(file, 0);
224 if (byte2 < 0)
225 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000226 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800227 }
228 if (byte2 != byte1)
229 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000230 return true;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800231 }
232 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000233 return false;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800234}
235
Matt Simmering2447c242023-11-09 14:18:39 -0800236// Issue an I2C transaction to first write to_target_buf_len bytes,then read
237// from_target_buf_len bytes.
Patrick Williamsb7077432024-08-16 15:22:21 -0400238static int i2cSmbusWriteThenRead(
239 int file, uint16_t address, uint8_t* toTargetBuf, uint8_t toTargetBufLen,
240 uint8_t* fromTargetBuf, uint8_t fromTargetBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800241{
Matt Simmering2447c242023-11-09 14:18:39 -0800242 if (toTargetBuf == nullptr || toTargetBufLen == 0 ||
243 fromTargetBuf == nullptr || fromTargetBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800244 {
245 return -1;
246 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800247
Ed Tanous3013fb42022-07-09 08:27:06 -0700248 constexpr size_t smbusWriteThenReadMsgCount = 2;
249 std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{};
Patrick Williamsa41f0142024-12-18 11:22:53 -0500250 struct i2c_rdwr_ioctl_data rdwr{};
Xiang Liu2801a702020-01-20 14:29:34 -0800251
252 msgs[0].addr = address;
253 msgs[0].flags = 0;
Matt Simmering2447c242023-11-09 14:18:39 -0800254 msgs[0].len = toTargetBufLen;
255 msgs[0].buf = toTargetBuf;
Xiang Liu2801a702020-01-20 14:29:34 -0800256 msgs[1].addr = address;
257 msgs[1].flags = I2C_M_RD;
Matt Simmering2447c242023-11-09 14:18:39 -0800258 msgs[1].len = fromTargetBufLen;
259 msgs[1].buf = fromTargetBuf;
Xiang Liu2801a702020-01-20 14:29:34 -0800260
Ed Tanous3013fb42022-07-09 08:27:06 -0700261 rdwr.msgs = msgs.data();
262 rdwr.nmsgs = msgs.size();
Xiang Liu2801a702020-01-20 14:29:34 -0800263
264 int ret = ioctl(file, I2C_RDWR, &rdwr);
265
Jason M. Billsf6e4c1f2022-08-19 12:23:51 -0700266 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800267}
268
Marvin Drees2b3ed302023-04-14 16:35:14 +0200269static int64_t readData(bool is16bit, bool isBytewise, int file,
270 uint16_t address, off_t offset, size_t len,
271 uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800272{
Zev Weiss309c0b12022-02-25 01:44:12 +0000273 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800274 {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200275 if (!isBytewise)
276 {
277 return i2c_smbus_read_i2c_block_data(
278 file, static_cast<uint8_t>(offset), len, buf);
279 }
280
281 std::span<uint8_t> bufspan{buf, len};
282 for (size_t i = 0; i < len; i++)
283 {
284 int byte = i2c_smbus_read_byte_data(
285 file, static_cast<uint8_t>(offset + i));
286 if (byte < 0)
287 {
288 return static_cast<int64_t>(byte);
289 }
290 bufspan[i] = static_cast<uint8_t>(byte);
291 }
292 return static_cast<int64_t>(len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800293 }
294
Xiang Liu2801a702020-01-20 14:29:34 -0800295 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700296 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
297 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
298 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800299}
300
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700301// TODO: This code is very similar to the non-eeprom version and can be merged
302// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300303static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700304{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700305 auto path = getEepromPath(bus, address);
306
307 int file = open(path.c_str(), O_RDONLY);
308 if (file < 0)
309 {
310 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700311 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700312 }
313
Patrick Venturebaba7672019-10-26 09:26:41 -0700314 std::string errorMessage = "eeprom at " + std::to_string(bus) +
315 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000316 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
317 return readFromEeprom(file, offset, length, outbuf);
318 };
319 FRUReader reader(std::move(readFunc));
Patrick Williamsb7077432024-08-16 15:22:21 -0400320 std::pair<std::vector<uint8_t>, bool> pair =
321 readFRUContents(reader, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700322
323 close(file);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200324 return pair.first;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700325}
326
Bonnie Lobfda2c42022-10-26 16:50:54 +0800327std::set<size_t> findI2CEeproms(int i2cBus,
328 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700329{
Bonnie Lobfda2c42022-10-26 16:50:54 +0800330 std::set<size_t> foundList;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700331
332 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
333
334 // For each file listed under the i2c device
335 // NOTE: This should be faster than just checking for each possible address
336 // path.
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700337 auto ec = std::error_code();
338 for (const auto& p : fs::directory_iterator(path, ec))
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700339 {
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700340 if (ec)
341 {
342 std::cerr << "directory_iterator err " << ec.message() << "\n";
343 break;
344 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700345 const std::string node = p.path().string();
346 std::smatch m;
Patrick Williamsb7077432024-08-16 15:22:21 -0400347 bool found =
348 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700349
350 if (!found)
351 {
352 continue;
353 }
354 if (m.size() != 2)
355 {
356 std::cerr << "regex didn't capture\n";
357 continue;
358 }
359
360 std::ssub_match subMatch = m[1];
361 std::string addressString = subMatch.str();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800362 std::string_view addressStringView(addressString);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700363
Bonnie Lobfda2c42022-10-26 16:50:54 +0800364 size_t address = 0;
365 std::from_chars(addressStringView.begin(), addressStringView.end(),
366 address, 16);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700367
368 const std::string eeprom = node + "/eeprom";
369
370 try
371 {
372 if (!fs::exists(eeprom))
373 {
374 continue;
375 }
376 }
377 catch (...)
378 {
379 continue;
380 }
381
382 // There is an eeprom file at this address, it may have invalid
383 // contents, but we found it.
384 foundList.insert(address);
385
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300386 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700387 if (!device.empty())
388 {
389 devices->emplace(address, device);
390 }
391 }
392
393 return foundList;
394}
395
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300396int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800397 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530398 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800399{
James Feist26c27ad2018-07-25 15:09:40 -0700400 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700401 // NOTE: When reading the devices raw on the bus, it can interfere with
402 // the driver's ability to operate, therefore read eeproms first before
403 // scanning for devices without drivers. Several experiments were run
404 // and it was determined that if there were any devices on the bus
405 // before the eeprom was hit and read, the eeprom driver wouldn't open
406 // while the bus device was open. An experiment was not performed to see
407 // if this issue was resolved if the i2c bus device was closed, but
408 // hexdumps of the eeprom later were successful.
409
410 // Scan for i2c eeproms loaded on this bus.
Bonnie Lobfda2c42022-10-26 16:50:54 +0800411 std::set<size_t> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800412 std::set<size_t>& failedItems = failedAddresses[bus];
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000413 std::set<size_t>& foundItems = fruAddresses[bus];
414 foundItems.clear();
James Feist7972bb92019-11-13 15:59:24 -0800415
Matt Simmering2447c242023-11-09 14:18:39 -0800416 auto busFind = busBlocklist.find(bus);
417 if (busFind != busBlocklist.end())
Bonnie Lobfda2c42022-10-26 16:50:54 +0800418 {
419 if (busFind->second != std::nullopt)
420 {
421 for (const auto& address : *(busFind->second))
422 {
423 skipList.insert(address);
424 }
425 }
426 }
427
James Feist7972bb92019-11-13 15:59:24 -0800428 std::set<size_t>* rootFailures = nullptr;
429 int rootBus = getRootBus(bus);
430
431 if (rootBus >= 0)
432 {
Matt Simmering2447c242023-11-09 14:18:39 -0800433 auto rootBusFind = busBlocklist.find(rootBus);
434 if (rootBusFind != busBlocklist.end())
Bonnie Lobfda2c42022-10-26 16:50:54 +0800435 {
436 if (rootBusFind->second != std::nullopt)
437 {
438 for (const auto& rootAddress : *(rootBusFind->second))
439 {
440 skipList.insert(rootAddress);
441 }
442 }
443 }
James Feist7972bb92019-11-13 15:59:24 -0800444 rootFailures = &(failedAddresses[rootBus]);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000445 foundItems = fruAddresses[rootBus];
James Feist7972bb92019-11-13 15:59:24 -0800446 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700447
Matt Simmering2447c242023-11-09 14:18:39 -0800448 constexpr int startSkipTargetAddr = 0;
449 constexpr int endSkipTargetAddr = 12;
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530450
James Feist26c27ad2018-07-25 15:09:40 -0700451 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800452 {
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000453 if (foundItems.find(ii) != foundItems.end())
454 {
455 continue;
456 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700457 if (skipList.find(ii) != skipList.end())
458 {
459 continue;
460 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530461 // skipping since no device is present in this range
Matt Simmering2447c242023-11-09 14:18:39 -0800462 if (ii >= startSkipTargetAddr && ii <= endSkipTargetAddr)
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530463 {
464 continue;
465 }
Matt Simmering2447c242023-11-09 14:18:39 -0800466 // Set target address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530467 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800468 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300469 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700470 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700471 continue;
472 }
473 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800474 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700475 {
476 continue;
477 }
James Feist3cb5fec2018-01-23 14:41:51 -0800478
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200479 lg2::debug("something at bus {BUS}, addr {ADDR}", "BUS", bus,
480 "ADDR", ii);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800481
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530482 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700483
James Feist7972bb92019-11-13 15:59:24 -0800484 if (failedItems.find(ii) != failedItems.end())
485 {
486 // if we failed to read it once, unlikely we can read it later
487 continue;
488 }
489
490 if (rootFailures != nullptr)
491 {
492 if (rootFailures->find(ii) != rootFailures->end())
493 {
494 continue;
495 }
496 }
497
Vijay Khemka2d681f62018-11-06 15:51:00 -0800498 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000499 std::optional<bool> is16Bit = isDevice16Bit(file);
500 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800501 {
502 std::cerr << "failed to read bus " << bus << " address " << ii
503 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700504 if (powerIsOn)
505 {
506 failedItems.insert(ii);
507 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800508 continue;
509 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000510 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800511
Patrick Williamsb7077432024-08-16 15:22:21 -0400512 auto readFunc = [is16BitBool, file,
513 ii](off_t offset, size_t length, uint8_t* outbuf) {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200514 return readData(is16BitBool, false, file, ii, offset, length,
515 outbuf);
Zev Weiss309c0b12022-02-25 01:44:12 +0000516 };
517 FRUReader reader(std::move(readFunc));
Patrick Williamsb7077432024-08-16 15:22:21 -0400518 std::string errorMessage =
519 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200520 std::pair<std::vector<uint8_t>, bool> pair =
521 readFRUContents(reader, errorMessage);
522 const bool foundHeader = pair.second;
523
524 if (!foundHeader && !is16BitBool)
525 {
526 // certain FRU eeproms require bytewise reading.
527 // otherwise garbage is read. e.g. SuperMicro PWS 920P-SQ
528
Patrick Williamsb7077432024-08-16 15:22:21 -0400529 auto readFunc =
530 [is16BitBool, file,
531 ii](off_t offset, size_t length, uint8_t* outbuf) {
532 return readData(is16BitBool, true, file, ii, offset,
533 length, outbuf);
534 };
Marvin Drees2b3ed302023-04-14 16:35:14 +0200535 FRUReader readerBytewise(std::move(readFunc));
536 pair = readFRUContents(readerBytewise, errorMessage);
537 }
538
539 if (pair.first.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700540 {
James Feist26c27ad2018-07-25 15:09:40 -0700541 continue;
542 }
James Feist26c27ad2018-07-25 15:09:40 -0700543
Marvin Drees2b3ed302023-04-14 16:35:14 +0200544 devices->emplace(ii, pair.first);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000545 fruAddresses[bus].insert(ii);
James Feist3cb5fec2018-01-23 14:41:51 -0800546 }
James Feist26c27ad2018-07-25 15:09:40 -0700547 return 1;
548 });
549 std::future_status status =
550 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
551 if (status == std::future_status::timeout)
552 {
553 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700554 if (powerIsOn)
555 {
Matt Simmering2447c242023-11-09 14:18:39 -0800556 busBlocklist[bus] = std::nullopt;
James Feistcb5661d2020-06-12 15:05:01 -0700557 }
James Feist444830e2019-04-05 08:38:16 -0700558 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700559 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800560 }
561
James Feist444830e2019-04-05 08:38:16 -0700562 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700563 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800564}
565
Matt Simmering2447c242023-11-09 14:18:39 -0800566void loadBlocklist(const char* path)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700567{
Matt Simmering2447c242023-11-09 14:18:39 -0800568 std::ifstream blocklistStream(path);
569 if (!blocklistStream.good())
Patrick Venture11f1ff42019-08-01 10:42:12 -0700570 {
571 // File is optional.
Matt Simmering2447c242023-11-09 14:18:39 -0800572 std::cerr << "Cannot open blocklist file.\n\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700573 return;
574 }
575
Patrick Williamsb7077432024-08-16 15:22:21 -0400576 nlohmann::json data =
577 nlohmann::json::parse(blocklistStream, nullptr, false);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700578 if (data.is_discarded())
579 {
Matt Simmering2447c242023-11-09 14:18:39 -0800580 std::cerr << "Illegal blocklist file detected, cannot validate JSON, "
Patrick Venture11f1ff42019-08-01 10:42:12 -0700581 "exiting\n";
582 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700583 }
584
585 // It's expected to have at least one field, "buses" that is an array of the
586 // buses by integer. Allow for future options to exclude further aspects,
587 // such as specific addresses or ranges.
588 if (data.type() != nlohmann::json::value_t::object)
589 {
Matt Simmering2447c242023-11-09 14:18:39 -0800590 std::cerr << "Illegal blocklist, expected to read dictionary\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700591 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700592 }
593
594 // If buses field is missing, that's fine.
595 if (data.count("buses") == 1)
596 {
597 // Parse the buses array after a little validation.
598 auto buses = data.at("buses");
599 if (buses.type() != nlohmann::json::value_t::array)
600 {
601 // Buses field present but invalid, therefore this is an error.
Matt Simmering2447c242023-11-09 14:18:39 -0800602 std::cerr << "Invalid contents for blocklist buses field\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700603 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700604 }
605
606 // Catch exception here for type mis-match.
607 try
608 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800609 for (const auto& busIterator : buses)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700610 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800611 // If bus and addresses field are missing, that's fine.
612 if (busIterator.contains("bus") &&
613 busIterator.contains("addresses"))
614 {
615 auto busData = busIterator.at("bus");
616 auto bus = busData.get<size_t>();
617
618 auto addressData = busIterator.at("addresses");
619 auto addresses =
620 addressData.get<std::set<std::string_view>>();
621
Ed Tanousfc171422024-04-04 17:18:16 -0700622 auto& block = busBlocklist[bus].emplace();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800623 for (const auto& address : addresses)
624 {
625 size_t addressInt = 0;
626 std::from_chars(address.begin() + 2, address.end(),
627 addressInt, 16);
Ed Tanousfc171422024-04-04 17:18:16 -0700628 block.insert(addressInt);
Bonnie Lobfda2c42022-10-26 16:50:54 +0800629 }
630 }
631 else
632 {
Matt Simmering2447c242023-11-09 14:18:39 -0800633 busBlocklist[busIterator.get<size_t>()] = std::nullopt;
Bonnie Lobfda2c42022-10-26 16:50:54 +0800634 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700635 }
636 }
637 catch (const nlohmann::detail::type_error& e)
638 {
639 // Type mis-match is a critical error.
640 std::cerr << "Invalid bus type: " << e.what() << "\n";
641 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700642 }
643 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700644}
645
Ed Tanous07d467b2021-02-23 14:48:37 -0800646static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800647 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530648 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800649{
Ed Tanous3013fb42022-07-09 08:27:06 -0700650 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800651 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700652 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800653
James Feist5d7f4692020-06-17 18:22:33 -0700654 if (bus < 0)
655 {
656 std::cerr << "Cannot translate " << i2cBus << " to int\n";
657 continue;
658 }
Matt Simmering2447c242023-11-09 14:18:39 -0800659 auto busFind = busBlocklist.find(bus);
660 if (busFind != busBlocklist.end())
James Feist444830e2019-04-05 08:38:16 -0700661 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800662 if (busFind->second == std::nullopt)
663 {
664 continue; // Skip blocked busses.
665 }
James Feist444830e2019-04-05 08:38:16 -0700666 }
James Feist0c3980a2019-12-19 11:09:27 -0800667 int rootBus = getRootBus(bus);
Matt Simmering2447c242023-11-09 14:18:39 -0800668 auto rootBusFind = busBlocklist.find(rootBus);
669 if (rootBusFind != busBlocklist.end())
James Feist0c3980a2019-12-19 11:09:27 -0800670 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800671 if (rootBusFind->second == std::nullopt)
672 {
673 continue;
674 }
James Feist0c3980a2019-12-19 11:09:27 -0800675 }
676
James Feist3cb5fec2018-01-23 14:41:51 -0800677 auto file = open(i2cBus.c_str(), O_RDWR);
678 if (file < 0)
679 {
680 std::cerr << "unable to open i2c device " << i2cBus.string()
681 << "\n";
682 continue;
683 }
684 unsigned long funcs = 0;
685
686 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
687 {
688 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700689 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800690 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700691 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800692 continue;
693 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700694 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
695 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800696 {
697 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
698 << bus << "\n";
699 continue;
700 }
James Feist98132792019-07-09 13:29:09 -0700701 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800702 device = std::make_shared<DeviceMap>();
703
Nikhil Potaded8884f12019-03-27 13:27:13 -0700704 // i2cdetect by default uses the range 0x03 to 0x77, as
705 // this is what we have tested with, use this range. Could be
706 // changed in future.
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200707 lg2::debug("Scanning bus {BUS}", "BUS", bus);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700708
709 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530710 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700711
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200712 lg2::debug("Done scanning bus {BUS}", "BUS", bus);
James Feist3cb5fec2018-01-23 14:41:51 -0800713 }
James Feist3cb5fec2018-01-23 14:41:51 -0800714}
715
James Feist6ebf9de2018-05-15 15:01:17 -0700716// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700717struct FindDevicesWithCallback :
718 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700719{
James Feista465ccc2019-02-08 12:51:01 -0800720 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800721 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530722 sdbusplus::asio::object_server& objServer,
Delphine CC Chiua3ca14a2024-03-27 17:02:24 +0800723 std::function<void()>&& callback) :
Patrick Williamsb7077432024-08-16 15:22:21 -0400724 _i2cBuses(i2cBuses), _busMap(busmap), _powerIsOn(powerIsOn),
725 _objServer(objServer), _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700726 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700727 ~FindDevicesWithCallback()
728 {
729 _callback();
730 }
731 void run()
732 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530733 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700734 }
735
James Feista465ccc2019-02-08 12:51:01 -0800736 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800737 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800738 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530739 sdbusplus::asio::object_server& _objServer;
Delphine CC Chiua3ca14a2024-03-27 17:02:24 +0800740 std::function<void()> _callback;
James Feist6ebf9de2018-05-15 15:01:17 -0700741};
742
Ed Tanous07d467b2021-02-23 14:48:37 -0800743void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300744 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800745 boost::container::flat_map<
746 std::pair<size_t, size_t>,
747 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530748 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800749 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530750 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800751{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300752 boost::container::flat_map<std::string, std::string> formattedFRU;
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530753
754 std::optional<std::string> optionalProductName = getProductName(
755 device, formattedFRU, bus, address, unknownBusObjectCount);
756 if (!optionalProductName)
James Feist3cb5fec2018-01-23 14:41:51 -0800757 {
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530758 std::cerr << "getProductName failed. product name is empty.\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800759 return;
760 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700761
Patrick Williamsb7077432024-08-16 15:22:21 -0400762 std::string productName =
763 "/xyz/openbmc_project/FruDevice/" + optionalProductName.value();
James Feist3cb5fec2018-01-23 14:41:51 -0800764
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530765 std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName);
766 if (index.has_value())
James Feist918e18c2018-02-13 15:51:07 -0800767 {
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530768 productName += "_";
769 productName += std::to_string(++(*index));
James Feist918e18c2018-02-13 15:51:07 -0800770 }
James Feist3cb5fec2018-01-23 14:41:51 -0800771
James Feist9eb0b582018-04-27 12:15:46 -0700772 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
773 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
774 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
775
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300776 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800777 {
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700778 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800779 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530780 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700781 {
782 continue;
783 }
Patrick Williamsb7077432024-08-16 15:22:21 -0400784 std::string key =
785 std::regex_replace(property.first, nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530786
787 if (property.first == "PRODUCT_ASSET_TAG")
788 {
789 std::string propertyName = property.first;
790 iface->register_property(
791 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530792 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530793 &unknownBusObjectCount, &powerIsOn, &objServer,
794 &systemBus](const std::string& req, std::string& resp) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400795 if (strcmp(req.c_str(), resp.c_str()) != 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +0530796 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400797 // call the method which will update
798 if (updateFRUProperty(req, bus, address, propertyName,
799 dbusInterfaceMap,
800 unknownBusObjectCount, powerIsOn,
801 objServer, systemBus))
802 {
803 resp = req;
804 }
805 else
806 {
807 throw std::invalid_argument(
808 "FRU property update failed.");
809 }
Joshi-Mansid73b7442020-03-27 19:10:21 +0530810 }
Patrick Williamsb7077432024-08-16 15:22:21 -0400811 return 1;
812 });
Joshi-Mansid73b7442020-03-27 19:10:21 +0530813 }
814 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700815 {
816 std::cerr << "illegal key: " << key << "\n";
817 }
Alexander Hansenc3db2c32024-08-20 15:01:38 +0200818 lg2::debug("parsed FRU property: {FIRST}: {SECOND}", "FIRST",
819 property.first, "SECOND", property.second);
James Feist3cb5fec2018-01-23 14:41:51 -0800820 }
James Feist2a9d6db2018-04-27 15:48:28 -0700821
822 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700823 iface->register_property("BUS", bus);
824 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700825
James Feist9eb0b582018-04-27 12:15:46 -0700826 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800827}
828
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300829static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800830{
831 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800832 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300833 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800834 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300835 baseboardFRUFile.seekg(0, std::ios_base::end);
836 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
837 baseboardFRU.resize(fileSize);
838 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700839 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
840 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
841 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800842 }
843 else
844 {
845 return false;
846 }
847 return true;
848}
849
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300850bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700851{
852 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800853 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700854 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300855 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700856 return false;
857 }
858 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800859 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700860 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300861 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700862 return false;
863 }
864 // baseboard fru
865 if (bus == 0 && address == 0)
866 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800867 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700868 if (!file.good())
869 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800870 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700871 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700872 return false;
873 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700874 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
875 const char* charOffset = reinterpret_cast<const char*>(fru.data());
876 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700877 return file.good();
878 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800879
880 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700881 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800882 auto path = getEepromPath(bus, address);
883 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
884 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700885 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800886 std::cerr << "unable to open i2c device " << path << "\n";
887 throw DBusInternalError();
888 return false;
889 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700890
Ed Tanous07d467b2021-02-23 14:48:37 -0800891 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
892 if (writtenBytes < 0)
893 {
894 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700895 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700896 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700897 return false;
898 }
899
Ed Tanous07d467b2021-02-23 14:48:37 -0800900 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700901 return true;
902 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800903
904 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
905
906 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
907 if (file < 0)
908 {
909 std::cerr << "unable to open i2c device " << i2cBus << "\n";
910 throw DBusInternalError();
911 return false;
912 }
913 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
914 {
915 std::cerr << "unable to set device address\n";
916 close(file);
917 throw DBusInternalError();
918 return false;
919 }
920
921 constexpr const size_t retryMax = 2;
922 uint16_t index = 0;
923 size_t retries = retryMax;
924 while (index < fru.size())
925 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700926 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800927 (retries == retryMax))
928 {
929 // The 4K EEPROM only uses the A2 and A1 device address bits
930 // with the third bit being a memory page address bit.
931 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
932 {
933 std::cerr << "unable to set device address\n";
934 close(file);
935 throw DBusInternalError();
936 return false;
937 }
938 }
939
940 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
941 fru[index]) < 0)
942 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700943 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800944 {
945 std::cerr << "error writing fru: " << strerror(errno) << "\n";
946 close(file);
947 throw DBusInternalError();
948 return false;
949 }
950 }
951 else
952 {
953 retries = retryMax;
954 index++;
955 }
956 // most eeproms require 5-10ms between writes
957 std::this_thread::sleep_for(std::chrono::milliseconds(10));
958 }
959 close(file);
960 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700961}
962
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800963void rescanOneBus(
krishnar4213ee212022-11-11 15:39:30 +0530964 BusMap& busmap, uint16_t busNum,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800965 boost::container::flat_map<
966 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700967 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800968 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530969 sdbusplus::asio::object_server& objServer,
970 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800971{
Yong Li6fdfac02023-10-30 11:21:30 +0800972 for (auto device = foundDevices.begin(); device != foundDevices.end();)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800973 {
Yong Li6fdfac02023-10-30 11:21:30 +0800974 if (device->first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800975 {
Yong Li6fdfac02023-10-30 11:21:30 +0800976 objServer.remove_interface(device->second);
977 device = foundDevices.erase(device);
978 }
979 else
980 {
981 device++;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800982 }
983 }
984
James Feist5d7f4692020-06-17 18:22:33 -0700985 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
986 if (!fs::exists(busPath))
987 {
988 if (dbusCall)
989 {
990 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
991 << "\n";
992 throw std::invalid_argument("Invalid Bus.");
993 }
994 return;
995 }
996
997 std::vector<fs::path> i2cBuses;
998 i2cBuses.emplace_back(busPath);
999
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001000 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301001 i2cBuses, busmap, powerIsOn, objServer,
1002 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
1003 &objServer, &systemBus]() {
Patrick Williamsb7077432024-08-16 15:22:21 -04001004 for (auto busIface = dbusInterfaceMap.begin();
1005 busIface != dbusInterfaceMap.end();)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001006 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001007 if (busIface->first.first == static_cast<size_t>(busNum))
1008 {
1009 objServer.remove_interface(busIface->second);
1010 busIface = dbusInterfaceMap.erase(busIface);
1011 }
1012 else
1013 {
1014 busIface++;
1015 }
Yong Li6fdfac02023-10-30 11:21:30 +08001016 }
Patrick Williamsb7077432024-08-16 15:22:21 -04001017 auto found = busmap.find(busNum);
1018 if (found == busmap.end() || found->second == nullptr)
Yong Li6fdfac02023-10-30 11:21:30 +08001019 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001020 return;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001021 }
Patrick Williamsb7077432024-08-16 15:22:21 -04001022 for (auto& device : *(found->second))
1023 {
1024 addFruObjectToDbus(device.second, dbusInterfaceMap,
1025 static_cast<uint32_t>(busNum), device.first,
1026 unknownBusObjectCount, powerIsOn, objServer,
1027 systemBus);
1028 }
1029 });
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001030 scan->run();
1031}
1032
James Feist9eb0b582018-04-27 12:15:46 -07001033void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -07001034 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -08001035 boost::container::flat_map<
1036 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301037 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001038 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301039 sdbusplus::asio::object_server& objServer,
1040 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -08001041{
Ed Tanousaa497ed2023-02-28 13:43:41 -08001042 static boost::asio::steady_timer timer(io);
1043 timer.expires_from_now(std::chrono::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001044
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001045 // setup an async wait in case we get flooded with requests
Jian Zhang6e7cebf2022-11-29 18:30:21 +08001046 timer.async_wait([&](const boost::system::error_code& ec) {
1047 if (ec == boost::asio::error::operation_aborted)
1048 {
1049 return;
1050 }
1051
1052 if (ec)
1053 {
1054 std::cerr << "Error in timer: " << ec.message() << "\n";
1055 return;
1056 }
1057
James Feist4131aea2018-03-09 09:47:30 -08001058 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001059 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001060
Nikhil Potaded8884f12019-03-27 13:27:13 -07001061 boost::container::flat_map<size_t, fs::path> busPaths;
1062 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001063 {
James Feist4131aea2018-03-09 09:47:30 -08001064 std::cerr << "unable to find i2c devices\n";
1065 return;
James Feist918e18c2018-02-13 15:51:07 -08001066 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001067
Ed Tanous07d467b2021-02-23 14:48:37 -08001068 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001069 {
1070 i2cBuses.emplace_back(busPath.second);
1071 }
James Feist4131aea2018-03-09 09:47:30 -08001072
James Feist98132792019-07-09 13:29:09 -07001073 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001074 for (auto& [pair, interface] : foundDevices)
1075 {
1076 objServer.remove_interface(interface);
1077 }
1078 foundDevices.clear();
1079
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301080 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301081 i2cBuses, busmap, powerIsOn, objServer, [&]() {
Patrick Williamsb7077432024-08-16 15:22:21 -04001082 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001083 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001084 objServer.remove_interface(busIface.second);
James Feist6ebf9de2018-05-15 15:01:17 -07001085 }
Patrick Williamsb7077432024-08-16 15:22:21 -04001086
1087 dbusInterfaceMap.clear();
1088 unknownBusObjectCount = 0;
1089
1090 // todo, get this from a more sensable place
1091 std::vector<uint8_t> baseboardFRU;
1092 if (readBaseboardFRU(baseboardFRU))
1093 {
1094 // If no device on i2c bus 0, the insertion will happen.
1095 auto bus0 =
1096 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1097 bus0.first->second->emplace(0, baseboardFRU);
1098 }
1099 for (auto& devicemap : busmap)
1100 {
1101 for (auto& device : *devicemap.second)
1102 {
1103 addFruObjectToDbus(device.second, dbusInterfaceMap,
1104 devicemap.first, device.first,
1105 unknownBusObjectCount, powerIsOn,
1106 objServer, systemBus);
1107 }
1108 }
1109 });
James Feist6ebf9de2018-05-15 15:01:17 -07001110 scan->run();
1111 });
James Feist918e18c2018-02-13 15:51:07 -08001112}
1113
Joshi-Mansid73b7442020-03-27 19:10:21 +05301114// Details with example of Asset Tag Update
1115// To find location of Product Info Area asset tag as per FRU specification
1116// 1. Find product Info area starting offset (*8 - as header will be in
1117// multiple of 8 bytes).
1118// 2. Skip 3 bytes of product info area (like format version, area length,
1119// and language code).
1120// 3. Traverse manufacturer name, product name, product version, & product
1121// serial number, by reading type/length code to reach the Asset Tag.
1122// 4. Update the Asset Tag, reposition the product Info area in multiple of
1123// 8 bytes. Update the Product area length and checksum.
1124
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001125bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301126 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001127 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301128 boost::container::flat_map<
1129 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301130 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001131 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301132 sdbusplus::asio::object_server& objServer,
1133 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301134{
1135 size_t updatePropertyReqLen = updatePropertyReq.length();
1136 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1137 {
1138 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001139 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301140 "Invalid Length "
1141 << updatePropertyReqLen << "\n";
1142 return false;
1143 }
1144
1145 std::vector<uint8_t> fruData;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301146
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301147 if (!getFruData(fruData, bus, address))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301148 {
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301149 std::cerr << "Failure getting FRU Data \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301150 return false;
1151 }
1152
Patrick Williamsa41f0142024-12-18 11:22:53 -05001153 struct FruArea fruAreaParams{};
Joshi-Mansid73b7442020-03-27 19:10:21 +05301154
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301155 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301156 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301157 std::cerr << "findFruAreaLocationAndField failed \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301158 return false;
1159 }
1160
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301161 std::vector<uint8_t> restFRUAreaFieldsData;
1162 if (!copyRestFRUArea(fruData, propertyName, fruAreaParams,
1163 restFRUAreaFieldsData))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301164 {
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301165 std::cerr << "copyRestFRUArea failed \n";
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001166 return false;
1167 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301168
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001169 // Push post update fru areas if any
1170 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001171 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1172 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001173 {
1174 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001175 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301176 if ((fruAreaLoc > fruAreaParams.restFieldsEnd) &&
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001177 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1178 {
1179 nextFRUAreaLoc = fruAreaLoc;
1180 }
1181 }
1182 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001183 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001184 {
1185 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1186 fruData.size() - nextFRUAreaLoc,
1187 std::back_inserter(restFRUAreasData));
1188 }
1189
1190 // check FRU area size
1191 size_t fruAreaDataSize =
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301192 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) +
1193 restFRUAreaFieldsData.size());
1194 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001195 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1196 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001197#ifdef ENABLE_FRU_AREA_RESIZE
1198 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1199 // round size to 8-byte blocks
Patrick Williamsb7077432024-08-16 15:22:21 -04001200 newFRUAreaSize =
1201 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
1202 size_t newFRUDataSize =
1203 fruData.size() + newFRUAreaSize - fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001204 fruData.resize(newFRUDataSize);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301205 fruAreaParams.size = newFRUAreaSize;
1206 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001207#else
1208 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1209 << " should not be greater than available FRU area size: "
1210 << fruAreaAvailableSize << "\n";
1211 return false;
1212#endif // ENABLE_FRU_AREA_RESIZE
1213 }
1214
Joshi-Mansid73b7442020-03-27 19:10:21 +05301215 // write new requested property field length and data
1216 constexpr uint8_t newTypeLenMask = 0xC0;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301217 fruData[fruAreaParams.updateFieldLoc] =
Joshi-Mansid73b7442020-03-27 19:10:21 +05301218 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301219 fruAreaParams.updateFieldLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301220 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301221 fruData.begin() + fruAreaParams.updateFieldLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301222
1223 // Copy remaining data to main fru area - post updated fru field vector
Patrick Williamsb7077432024-08-16 15:22:21 -04001224 fruAreaParams.restFieldsLoc =
1225 fruAreaParams.updateFieldLoc + updatePropertyReqLen;
1226 size_t fruAreaDataEnd =
1227 fruAreaParams.restFieldsLoc + restFRUAreaFieldsData.size();
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301228
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001229 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301230 fruData.begin() + fruAreaParams.restFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301231
1232 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001233 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301234 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301235
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001236#ifdef ENABLE_FRU_AREA_RESIZE
1237 ++nextFRUAreaNewLoc;
Patrick Williamsb7077432024-08-16 15:22:21 -04001238 ssize_t nextFRUAreaOffsetDiff =
1239 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001240 // Append rest FRU Areas if size changed and there were other sections after
1241 // updated one
1242 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1243 {
1244 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1245 fruData.begin() + nextFRUAreaNewLoc);
1246 // Update Common Header
Ed Tanous6e22c872023-02-16 15:19:28 -08001247 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1248 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001249 {
Ed Tanous6e22c872023-02-16 15:19:28 -08001250 unsigned int fruAreaOffsetField =
1251 getHeaderAreaFieldOffset(nextFRUArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001252 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
Ed Tanous6e22c872023-02-16 15:19:28 -08001253 if (curFRUAreaOffset > fruAreaParams.end)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001254 {
1255 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1256 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1257 }
1258 }
1259 // Calculate new checksum
1260 std::vector<uint8_t> headerFRUData;
1261 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1262 size_t checksumVal = calculateChecksum(headerFRUData);
1263 fruData[7] = static_cast<uint8_t>(checksumVal);
1264 // fill zeros if FRU Area size decreased
1265 if (nextFRUAreaOffsetDiff < 0)
1266 {
1267 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1268 restFRUAreasData.size(),
1269 fruData.end(), 0);
1270 }
1271 }
1272#else
1273 // this is to avoid "unused variable" warning
1274 (void)nextFRUAreaNewLoc;
1275#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301276 if (fruData.empty())
1277 {
1278 return false;
1279 }
1280
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001281 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301282 fruData))
1283 {
1284 return false;
1285 }
1286
1287 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301288 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1289 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301290 return true;
1291}
1292
James Feist98132792019-07-09 13:29:09 -07001293int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001294{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301295 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1296 sdbusplus::asio::object_server objServer(systemBus);
1297
Kumar Thangavel41a90512021-11-24 15:44:20 +05301298 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301299 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001300 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001301 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001302 std::vector<fs::path> i2cBuses;
1303
James Feista3c180a2018-08-09 16:06:04 -07001304 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001305 {
1306 std::cerr << "unable to find i2c devices\n";
1307 return 1;
1308 }
James Feist3cb5fec2018-01-23 14:41:51 -08001309
Matt Simmering2447c242023-11-09 14:18:39 -08001310 // check for and load blocklist with initial buses.
1311 loadBlocklist(blocklistPath);
Patrick Venture11f1ff42019-08-01 10:42:12 -07001312
Vijay Khemka065f6d92018-12-18 10:37:47 -08001313 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001314
James Feist6ebf9de2018-05-15 15:01:17 -07001315 // this is a map with keys of pair(bus number, address) and values of
1316 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001317 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001318 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1319 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001320
James Feist9eb0b582018-04-27 12:15:46 -07001321 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1322 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1323 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001324
Kumar Thangavel41a90512021-11-24 15:44:20 +05301325 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301326 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1327 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301328 });
James Feist2a9d6db2018-04-27 15:48:28 -07001329
krishnar4213ee212022-11-11 15:39:30 +05301330 iface->register_method("ReScanBus", [&](uint16_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301331 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301332 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001333 });
1334
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001335 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001336
Patrick Williamsb7077432024-08-16 15:22:21 -04001337 iface->register_method(
1338 "WriteFru", [&](const uint16_t bus, const uint8_t address,
1339 const std::vector<uint8_t>& data) {
1340 if (!writeFRU(bus, address, data))
1341 {
1342 throw std::invalid_argument("Invalid Arguments.");
1343 return;
1344 }
1345 // schedule rescan on success
1346 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1347 powerIsOn, objServer, systemBus);
1348 });
James Feist9eb0b582018-04-27 12:15:46 -07001349 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001350
Patrick Williams2af39222022-07-22 19:26:56 -05001351 std::function<void(sdbusplus::message_t & message)> eventHandler =
1352 [&](sdbusplus::message_t& message) {
Patrick Williamsb7077432024-08-16 15:22:21 -04001353 std::string objectName;
1354 boost::container::flat_map<
1355 std::string,
1356 std::variant<std::string, bool, int64_t, uint64_t, double>>
1357 values;
1358 message.read(objectName, values);
1359 auto findState = values.find("CurrentHostState");
1360 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001361 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001362 if (std::get<std::string>(findState->second) ==
1363 "xyz.openbmc_project.State.Host.HostState.Running")
1364 {
1365 powerIsOn = true;
1366 }
James Feist0eeb79c2019-10-09 13:35:29 -07001367 }
James Feist6ebf9de2018-05-15 15:01:17 -07001368
Patrick Williamsb7077432024-08-16 15:22:21 -04001369 if (powerIsOn)
1370 {
1371 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1372 powerIsOn, objServer, systemBus);
1373 }
1374 };
James Feist9eb0b582018-04-27 12:15:46 -07001375
Patrick Williams2af39222022-07-22 19:26:56 -05001376 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1377 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001378 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001379 "openbmc_project/state/"
1380 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001381 eventHandler);
1382
James Feist4131aea2018-03-09 09:47:30 -08001383 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001384 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001385 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001386 // monitor for new i2c devices
1387 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1388 std::function<void(const boost::system::error_code, std::size_t)>
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001389 watchI2cBusses = [&](const boost::system::error_code& ec,
1390 std::size_t bytesTransferred) {
Patrick Williamsb7077432024-08-16 15:22:21 -04001391 if (ec)
James Feist4131aea2018-03-09 09:47:30 -08001392 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001393 std::cout << "Callback Error " << ec << "\n";
1394 return;
1395 }
1396 size_t index = 0;
1397 while ((index + sizeof(inotify_event)) <= bytesTransferred)
1398 {
1399 const char* p = &readBuffer[index];
1400 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1401 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
1402 switch (iEvent->mask)
Ed Tanousfc171422024-04-04 17:18:16 -07001403 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001404 case IN_CREATE:
1405 case IN_MOVED_TO:
1406 case IN_DELETE:
Patrick Williamsdf190612023-05-10 07:51:34 -05001407 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001408 std::string_view name(&iEvent->name[0], iEvent->len);
1409 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001410 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001411 int bus = busStrToInt(name);
1412 if (bus < 0)
1413 {
1414 std::cerr
1415 << "Could not parse bus " << name << "\n";
1416 continue;
1417 }
1418 int rootBus = getRootBus(bus);
1419 if (rootBus >= 0)
1420 {
1421 rescanOneBus(busMap,
1422 static_cast<uint16_t>(rootBus),
1423 dbusInterfaceMap, false,
1424 unknownBusObjectCount, powerIsOn,
1425 objServer, systemBus);
1426 }
1427 rescanOneBus(busMap, static_cast<uint16_t>(bus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301428 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301429 unknownBusObjectCount, powerIsOn,
1430 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001431 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001432 }
Ed Tanousfc171422024-04-04 17:18:16 -07001433 break;
Patrick Williamsb7077432024-08-16 15:22:21 -04001434 default:
1435 break;
1436 }
1437 index += sizeof(inotify_event) + iEvent->len;
James Feist4131aea2018-03-09 09:47:30 -08001438 }
James Feist6ebf9de2018-05-15 15:01:17 -07001439
Patrick Williamsb7077432024-08-16 15:22:21 -04001440 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1441 watchI2cBusses);
1442 };
James Feist4131aea2018-03-09 09:47:30 -08001443
1444 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001445 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301446 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1447 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001448
James Feist3cb5fec2018-01-23 14:41:51 -08001449 io.run();
1450 return 0;
1451}