blob: b265bf61c829f365fb8b42c67daa41d4a1fd05ed [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
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{};
250 struct i2c_rdwr_ioctl_data rdwr
251 {};
Xiang Liu2801a702020-01-20 14:29:34 -0800252
253 msgs[0].addr = address;
254 msgs[0].flags = 0;
Matt Simmering2447c242023-11-09 14:18:39 -0800255 msgs[0].len = toTargetBufLen;
256 msgs[0].buf = toTargetBuf;
Xiang Liu2801a702020-01-20 14:29:34 -0800257 msgs[1].addr = address;
258 msgs[1].flags = I2C_M_RD;
Matt Simmering2447c242023-11-09 14:18:39 -0800259 msgs[1].len = fromTargetBufLen;
260 msgs[1].buf = fromTargetBuf;
Xiang Liu2801a702020-01-20 14:29:34 -0800261
Ed Tanous3013fb42022-07-09 08:27:06 -0700262 rdwr.msgs = msgs.data();
263 rdwr.nmsgs = msgs.size();
Xiang Liu2801a702020-01-20 14:29:34 -0800264
265 int ret = ioctl(file, I2C_RDWR, &rdwr);
266
Jason M. Billsf6e4c1f2022-08-19 12:23:51 -0700267 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800268}
269
Marvin Drees2b3ed302023-04-14 16:35:14 +0200270static int64_t readData(bool is16bit, bool isBytewise, int file,
271 uint16_t address, off_t offset, size_t len,
272 uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800273{
Zev Weiss309c0b12022-02-25 01:44:12 +0000274 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800275 {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200276 if (!isBytewise)
277 {
278 return i2c_smbus_read_i2c_block_data(
279 file, static_cast<uint8_t>(offset), len, buf);
280 }
281
282 std::span<uint8_t> bufspan{buf, len};
283 for (size_t i = 0; i < len; i++)
284 {
285 int byte = i2c_smbus_read_byte_data(
286 file, static_cast<uint8_t>(offset + i));
287 if (byte < 0)
288 {
289 return static_cast<int64_t>(byte);
290 }
291 bufspan[i] = static_cast<uint8_t>(byte);
292 }
293 return static_cast<int64_t>(len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800294 }
295
Xiang Liu2801a702020-01-20 14:29:34 -0800296 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700297 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
298 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
299 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800300}
301
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700302// TODO: This code is very similar to the non-eeprom version and can be merged
303// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300304static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700305{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700306 auto path = getEepromPath(bus, address);
307
308 int file = open(path.c_str(), O_RDONLY);
309 if (file < 0)
310 {
311 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700312 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700313 }
314
Patrick Venturebaba7672019-10-26 09:26:41 -0700315 std::string errorMessage = "eeprom at " + std::to_string(bus) +
316 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000317 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
318 return readFromEeprom(file, offset, length, outbuf);
319 };
320 FRUReader reader(std::move(readFunc));
Patrick Williamsb7077432024-08-16 15:22:21 -0400321 std::pair<std::vector<uint8_t>, bool> pair =
322 readFRUContents(reader, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700323
324 close(file);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200325 return pair.first;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700326}
327
Bonnie Lobfda2c42022-10-26 16:50:54 +0800328std::set<size_t> findI2CEeproms(int i2cBus,
329 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700330{
Bonnie Lobfda2c42022-10-26 16:50:54 +0800331 std::set<size_t> foundList;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700332
333 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
334
335 // For each file listed under the i2c device
336 // NOTE: This should be faster than just checking for each possible address
337 // path.
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700338 auto ec = std::error_code();
339 for (const auto& p : fs::directory_iterator(path, ec))
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700340 {
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700341 if (ec)
342 {
343 std::cerr << "directory_iterator err " << ec.message() << "\n";
344 break;
345 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700346 const std::string node = p.path().string();
347 std::smatch m;
Patrick Williamsb7077432024-08-16 15:22:21 -0400348 bool found =
349 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700350
351 if (!found)
352 {
353 continue;
354 }
355 if (m.size() != 2)
356 {
357 std::cerr << "regex didn't capture\n";
358 continue;
359 }
360
361 std::ssub_match subMatch = m[1];
362 std::string addressString = subMatch.str();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800363 std::string_view addressStringView(addressString);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700364
Bonnie Lobfda2c42022-10-26 16:50:54 +0800365 size_t address = 0;
366 std::from_chars(addressStringView.begin(), addressStringView.end(),
367 address, 16);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700368
369 const std::string eeprom = node + "/eeprom";
370
371 try
372 {
373 if (!fs::exists(eeprom))
374 {
375 continue;
376 }
377 }
378 catch (...)
379 {
380 continue;
381 }
382
383 // There is an eeprom file at this address, it may have invalid
384 // contents, but we found it.
385 foundList.insert(address);
386
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300387 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700388 if (!device.empty())
389 {
390 devices->emplace(address, device);
391 }
392 }
393
394 return foundList;
395}
396
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300397int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800398 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530399 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800400{
James Feist26c27ad2018-07-25 15:09:40 -0700401 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700402 // NOTE: When reading the devices raw on the bus, it can interfere with
403 // the driver's ability to operate, therefore read eeproms first before
404 // scanning for devices without drivers. Several experiments were run
405 // and it was determined that if there were any devices on the bus
406 // before the eeprom was hit and read, the eeprom driver wouldn't open
407 // while the bus device was open. An experiment was not performed to see
408 // if this issue was resolved if the i2c bus device was closed, but
409 // hexdumps of the eeprom later were successful.
410
411 // Scan for i2c eeproms loaded on this bus.
Bonnie Lobfda2c42022-10-26 16:50:54 +0800412 std::set<size_t> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800413 std::set<size_t>& failedItems = failedAddresses[bus];
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000414 std::set<size_t>& foundItems = fruAddresses[bus];
415 foundItems.clear();
James Feist7972bb92019-11-13 15:59:24 -0800416
Matt Simmering2447c242023-11-09 14:18:39 -0800417 auto busFind = busBlocklist.find(bus);
418 if (busFind != busBlocklist.end())
Bonnie Lobfda2c42022-10-26 16:50:54 +0800419 {
420 if (busFind->second != std::nullopt)
421 {
422 for (const auto& address : *(busFind->second))
423 {
424 skipList.insert(address);
425 }
426 }
427 }
428
James Feist7972bb92019-11-13 15:59:24 -0800429 std::set<size_t>* rootFailures = nullptr;
430 int rootBus = getRootBus(bus);
431
432 if (rootBus >= 0)
433 {
Matt Simmering2447c242023-11-09 14:18:39 -0800434 auto rootBusFind = busBlocklist.find(rootBus);
435 if (rootBusFind != busBlocklist.end())
Bonnie Lobfda2c42022-10-26 16:50:54 +0800436 {
437 if (rootBusFind->second != std::nullopt)
438 {
439 for (const auto& rootAddress : *(rootBusFind->second))
440 {
441 skipList.insert(rootAddress);
442 }
443 }
444 }
James Feist7972bb92019-11-13 15:59:24 -0800445 rootFailures = &(failedAddresses[rootBus]);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000446 foundItems = fruAddresses[rootBus];
James Feist7972bb92019-11-13 15:59:24 -0800447 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700448
Matt Simmering2447c242023-11-09 14:18:39 -0800449 constexpr int startSkipTargetAddr = 0;
450 constexpr int endSkipTargetAddr = 12;
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530451
James Feist26c27ad2018-07-25 15:09:40 -0700452 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800453 {
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000454 if (foundItems.find(ii) != foundItems.end())
455 {
456 continue;
457 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700458 if (skipList.find(ii) != skipList.end())
459 {
460 continue;
461 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530462 // skipping since no device is present in this range
Matt Simmering2447c242023-11-09 14:18:39 -0800463 if (ii >= startSkipTargetAddr && ii <= endSkipTargetAddr)
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530464 {
465 continue;
466 }
Matt Simmering2447c242023-11-09 14:18:39 -0800467 // Set target address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530468 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800469 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300470 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700471 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700472 continue;
473 }
474 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800475 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700476 {
477 continue;
478 }
James Feist3cb5fec2018-01-23 14:41:51 -0800479
Ed Tanous07d467b2021-02-23 14:48:37 -0800480 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700481 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700482 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700483 << "\n";
484 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800485
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530486 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700487
James Feist7972bb92019-11-13 15:59:24 -0800488 if (failedItems.find(ii) != failedItems.end())
489 {
490 // if we failed to read it once, unlikely we can read it later
491 continue;
492 }
493
494 if (rootFailures != nullptr)
495 {
496 if (rootFailures->find(ii) != rootFailures->end())
497 {
498 continue;
499 }
500 }
501
Vijay Khemka2d681f62018-11-06 15:51:00 -0800502 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000503 std::optional<bool> is16Bit = isDevice16Bit(file);
504 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800505 {
506 std::cerr << "failed to read bus " << bus << " address " << ii
507 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700508 if (powerIsOn)
509 {
510 failedItems.insert(ii);
511 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800512 continue;
513 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000514 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800515
Patrick Williamsb7077432024-08-16 15:22:21 -0400516 auto readFunc = [is16BitBool, file,
517 ii](off_t offset, size_t length, uint8_t* outbuf) {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200518 return readData(is16BitBool, false, file, ii, offset, length,
519 outbuf);
Zev Weiss309c0b12022-02-25 01:44:12 +0000520 };
521 FRUReader reader(std::move(readFunc));
Patrick Williamsb7077432024-08-16 15:22:21 -0400522 std::string errorMessage =
523 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200524 std::pair<std::vector<uint8_t>, bool> pair =
525 readFRUContents(reader, errorMessage);
526 const bool foundHeader = pair.second;
527
528 if (!foundHeader && !is16BitBool)
529 {
530 // certain FRU eeproms require bytewise reading.
531 // otherwise garbage is read. e.g. SuperMicro PWS 920P-SQ
532
Patrick Williamsb7077432024-08-16 15:22:21 -0400533 auto readFunc =
534 [is16BitBool, file,
535 ii](off_t offset, size_t length, uint8_t* outbuf) {
536 return readData(is16BitBool, true, file, ii, offset,
537 length, outbuf);
538 };
Marvin Drees2b3ed302023-04-14 16:35:14 +0200539 FRUReader readerBytewise(std::move(readFunc));
540 pair = readFRUContents(readerBytewise, errorMessage);
541 }
542
543 if (pair.first.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700544 {
James Feist26c27ad2018-07-25 15:09:40 -0700545 continue;
546 }
James Feist26c27ad2018-07-25 15:09:40 -0700547
Marvin Drees2b3ed302023-04-14 16:35:14 +0200548 devices->emplace(ii, pair.first);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000549 fruAddresses[bus].insert(ii);
James Feist3cb5fec2018-01-23 14:41:51 -0800550 }
James Feist26c27ad2018-07-25 15:09:40 -0700551 return 1;
552 });
553 std::future_status status =
554 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
555 if (status == std::future_status::timeout)
556 {
557 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700558 if (powerIsOn)
559 {
Matt Simmering2447c242023-11-09 14:18:39 -0800560 busBlocklist[bus] = std::nullopt;
James Feistcb5661d2020-06-12 15:05:01 -0700561 }
James Feist444830e2019-04-05 08:38:16 -0700562 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700563 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800564 }
565
James Feist444830e2019-04-05 08:38:16 -0700566 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700567 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800568}
569
Matt Simmering2447c242023-11-09 14:18:39 -0800570void loadBlocklist(const char* path)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700571{
Matt Simmering2447c242023-11-09 14:18:39 -0800572 std::ifstream blocklistStream(path);
573 if (!blocklistStream.good())
Patrick Venture11f1ff42019-08-01 10:42:12 -0700574 {
575 // File is optional.
Matt Simmering2447c242023-11-09 14:18:39 -0800576 std::cerr << "Cannot open blocklist file.\n\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700577 return;
578 }
579
Patrick Williamsb7077432024-08-16 15:22:21 -0400580 nlohmann::json data =
581 nlohmann::json::parse(blocklistStream, nullptr, false);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700582 if (data.is_discarded())
583 {
Matt Simmering2447c242023-11-09 14:18:39 -0800584 std::cerr << "Illegal blocklist file detected, cannot validate JSON, "
Patrick Venture11f1ff42019-08-01 10:42:12 -0700585 "exiting\n";
586 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700587 }
588
589 // It's expected to have at least one field, "buses" that is an array of the
590 // buses by integer. Allow for future options to exclude further aspects,
591 // such as specific addresses or ranges.
592 if (data.type() != nlohmann::json::value_t::object)
593 {
Matt Simmering2447c242023-11-09 14:18:39 -0800594 std::cerr << "Illegal blocklist, expected to read dictionary\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700595 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700596 }
597
598 // If buses field is missing, that's fine.
599 if (data.count("buses") == 1)
600 {
601 // Parse the buses array after a little validation.
602 auto buses = data.at("buses");
603 if (buses.type() != nlohmann::json::value_t::array)
604 {
605 // Buses field present but invalid, therefore this is an error.
Matt Simmering2447c242023-11-09 14:18:39 -0800606 std::cerr << "Invalid contents for blocklist buses field\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700607 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700608 }
609
610 // Catch exception here for type mis-match.
611 try
612 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800613 for (const auto& busIterator : buses)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700614 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800615 // If bus and addresses field are missing, that's fine.
616 if (busIterator.contains("bus") &&
617 busIterator.contains("addresses"))
618 {
619 auto busData = busIterator.at("bus");
620 auto bus = busData.get<size_t>();
621
622 auto addressData = busIterator.at("addresses");
623 auto addresses =
624 addressData.get<std::set<std::string_view>>();
625
Ed Tanousfc171422024-04-04 17:18:16 -0700626 auto& block = busBlocklist[bus].emplace();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800627 for (const auto& address : addresses)
628 {
629 size_t addressInt = 0;
630 std::from_chars(address.begin() + 2, address.end(),
631 addressInt, 16);
Ed Tanousfc171422024-04-04 17:18:16 -0700632 block.insert(addressInt);
Bonnie Lobfda2c42022-10-26 16:50:54 +0800633 }
634 }
635 else
636 {
Matt Simmering2447c242023-11-09 14:18:39 -0800637 busBlocklist[busIterator.get<size_t>()] = std::nullopt;
Bonnie Lobfda2c42022-10-26 16:50:54 +0800638 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700639 }
640 }
641 catch (const nlohmann::detail::type_error& e)
642 {
643 // Type mis-match is a critical error.
644 std::cerr << "Invalid bus type: " << e.what() << "\n";
645 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700646 }
647 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700648}
649
Ed Tanous07d467b2021-02-23 14:48:37 -0800650static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800651 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530652 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800653{
Ed Tanous3013fb42022-07-09 08:27:06 -0700654 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800655 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700656 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800657
James Feist5d7f4692020-06-17 18:22:33 -0700658 if (bus < 0)
659 {
660 std::cerr << "Cannot translate " << i2cBus << " to int\n";
661 continue;
662 }
Matt Simmering2447c242023-11-09 14:18:39 -0800663 auto busFind = busBlocklist.find(bus);
664 if (busFind != busBlocklist.end())
James Feist444830e2019-04-05 08:38:16 -0700665 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800666 if (busFind->second == std::nullopt)
667 {
668 continue; // Skip blocked busses.
669 }
James Feist444830e2019-04-05 08:38:16 -0700670 }
James Feist0c3980a2019-12-19 11:09:27 -0800671 int rootBus = getRootBus(bus);
Matt Simmering2447c242023-11-09 14:18:39 -0800672 auto rootBusFind = busBlocklist.find(rootBus);
673 if (rootBusFind != busBlocklist.end())
James Feist0c3980a2019-12-19 11:09:27 -0800674 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800675 if (rootBusFind->second == std::nullopt)
676 {
677 continue;
678 }
James Feist0c3980a2019-12-19 11:09:27 -0800679 }
680
James Feist3cb5fec2018-01-23 14:41:51 -0800681 auto file = open(i2cBus.c_str(), O_RDWR);
682 if (file < 0)
683 {
684 std::cerr << "unable to open i2c device " << i2cBus.string()
685 << "\n";
686 continue;
687 }
688 unsigned long funcs = 0;
689
690 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
691 {
692 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700693 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800694 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700695 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800696 continue;
697 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700698 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
699 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800700 {
701 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
702 << bus << "\n";
703 continue;
704 }
James Feist98132792019-07-09 13:29:09 -0700705 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800706 device = std::make_shared<DeviceMap>();
707
Nikhil Potaded8884f12019-03-27 13:27:13 -0700708 // i2cdetect by default uses the range 0x03 to 0x77, as
709 // this is what we have tested with, use this range. Could be
710 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800711 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800712 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700713 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800714 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700715
716 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530717 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700718
Ed Tanous07d467b2021-02-23 14:48:37 -0800719 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800720 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700721 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800722 }
James Feist3cb5fec2018-01-23 14:41:51 -0800723 }
James Feist3cb5fec2018-01-23 14:41:51 -0800724}
725
James Feist6ebf9de2018-05-15 15:01:17 -0700726// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700727struct FindDevicesWithCallback :
728 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700729{
James Feista465ccc2019-02-08 12:51:01 -0800730 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800731 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530732 sdbusplus::asio::object_server& objServer,
Delphine CC Chiua3ca14a2024-03-27 17:02:24 +0800733 std::function<void()>&& callback) :
Patrick Williamsb7077432024-08-16 15:22:21 -0400734 _i2cBuses(i2cBuses), _busMap(busmap), _powerIsOn(powerIsOn),
735 _objServer(objServer), _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700736 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700737 ~FindDevicesWithCallback()
738 {
739 _callback();
740 }
741 void run()
742 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530743 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700744 }
745
James Feista465ccc2019-02-08 12:51:01 -0800746 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800747 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800748 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530749 sdbusplus::asio::object_server& _objServer;
Delphine CC Chiua3ca14a2024-03-27 17:02:24 +0800750 std::function<void()> _callback;
James Feist6ebf9de2018-05-15 15:01:17 -0700751};
752
Ed Tanous07d467b2021-02-23 14:48:37 -0800753void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300754 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800755 boost::container::flat_map<
756 std::pair<size_t, size_t>,
757 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530758 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800759 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530760 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800761{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300762 boost::container::flat_map<std::string, std::string> formattedFRU;
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530763
764 std::optional<std::string> optionalProductName = getProductName(
765 device, formattedFRU, bus, address, unknownBusObjectCount);
766 if (!optionalProductName)
James Feist3cb5fec2018-01-23 14:41:51 -0800767 {
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530768 std::cerr << "getProductName failed. product name is empty.\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800769 return;
770 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700771
Patrick Williamsb7077432024-08-16 15:22:21 -0400772 std::string productName =
773 "/xyz/openbmc_project/FruDevice/" + optionalProductName.value();
James Feist3cb5fec2018-01-23 14:41:51 -0800774
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530775 std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName);
776 if (index.has_value())
James Feist918e18c2018-02-13 15:51:07 -0800777 {
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530778 productName += "_";
779 productName += std::to_string(++(*index));
James Feist918e18c2018-02-13 15:51:07 -0800780 }
James Feist3cb5fec2018-01-23 14:41:51 -0800781
James Feist9eb0b582018-04-27 12:15:46 -0700782 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
783 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
784 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
785
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300786 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800787 {
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700788 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800789 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530790 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700791 {
792 continue;
793 }
Patrick Williamsb7077432024-08-16 15:22:21 -0400794 std::string key =
795 std::regex_replace(property.first, nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530796
797 if (property.first == "PRODUCT_ASSET_TAG")
798 {
799 std::string propertyName = property.first;
800 iface->register_property(
801 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530802 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530803 &unknownBusObjectCount, &powerIsOn, &objServer,
804 &systemBus](const std::string& req, std::string& resp) {
Patrick Williamsb7077432024-08-16 15:22:21 -0400805 if (strcmp(req.c_str(), resp.c_str()) != 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +0530806 {
Patrick Williamsb7077432024-08-16 15:22:21 -0400807 // call the method which will update
808 if (updateFRUProperty(req, bus, address, propertyName,
809 dbusInterfaceMap,
810 unknownBusObjectCount, powerIsOn,
811 objServer, systemBus))
812 {
813 resp = req;
814 }
815 else
816 {
817 throw std::invalid_argument(
818 "FRU property update failed.");
819 }
Joshi-Mansid73b7442020-03-27 19:10:21 +0530820 }
Patrick Williamsb7077432024-08-16 15:22:21 -0400821 return 1;
822 });
Joshi-Mansid73b7442020-03-27 19:10:21 +0530823 }
824 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700825 {
826 std::cerr << "illegal key: " << key << "\n";
827 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800828 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700829 {
830 std::cout << property.first << ": " << property.second << "\n";
831 }
James Feist3cb5fec2018-01-23 14:41:51 -0800832 }
James Feist2a9d6db2018-04-27 15:48:28 -0700833
834 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700835 iface->register_property("BUS", bus);
836 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700837
James Feist9eb0b582018-04-27 12:15:46 -0700838 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800839}
840
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300841static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800842{
843 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800844 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300845 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800846 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300847 baseboardFRUFile.seekg(0, std::ios_base::end);
848 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
849 baseboardFRU.resize(fileSize);
850 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700851 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
852 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
853 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800854 }
855 else
856 {
857 return false;
858 }
859 return true;
860}
861
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300862bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700863{
864 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800865 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700866 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300867 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700868 return false;
869 }
870 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800871 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700872 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300873 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700874 return false;
875 }
876 // baseboard fru
877 if (bus == 0 && address == 0)
878 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800879 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700880 if (!file.good())
881 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800882 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700883 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700884 return false;
885 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700886 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
887 const char* charOffset = reinterpret_cast<const char*>(fru.data());
888 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700889 return file.good();
890 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800891
892 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700893 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800894 auto path = getEepromPath(bus, address);
895 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
896 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700897 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800898 std::cerr << "unable to open i2c device " << path << "\n";
899 throw DBusInternalError();
900 return false;
901 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700902
Ed Tanous07d467b2021-02-23 14:48:37 -0800903 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
904 if (writtenBytes < 0)
905 {
906 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700907 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700908 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700909 return false;
910 }
911
Ed Tanous07d467b2021-02-23 14:48:37 -0800912 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700913 return true;
914 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800915
916 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
917
918 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
919 if (file < 0)
920 {
921 std::cerr << "unable to open i2c device " << i2cBus << "\n";
922 throw DBusInternalError();
923 return false;
924 }
925 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
926 {
927 std::cerr << "unable to set device address\n";
928 close(file);
929 throw DBusInternalError();
930 return false;
931 }
932
933 constexpr const size_t retryMax = 2;
934 uint16_t index = 0;
935 size_t retries = retryMax;
936 while (index < fru.size())
937 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700938 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800939 (retries == retryMax))
940 {
941 // The 4K EEPROM only uses the A2 and A1 device address bits
942 // with the third bit being a memory page address bit.
943 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
944 {
945 std::cerr << "unable to set device address\n";
946 close(file);
947 throw DBusInternalError();
948 return false;
949 }
950 }
951
952 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
953 fru[index]) < 0)
954 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700955 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800956 {
957 std::cerr << "error writing fru: " << strerror(errno) << "\n";
958 close(file);
959 throw DBusInternalError();
960 return false;
961 }
962 }
963 else
964 {
965 retries = retryMax;
966 index++;
967 }
968 // most eeproms require 5-10ms between writes
969 std::this_thread::sleep_for(std::chrono::milliseconds(10));
970 }
971 close(file);
972 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700973}
974
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800975void rescanOneBus(
krishnar4213ee212022-11-11 15:39:30 +0530976 BusMap& busmap, uint16_t busNum,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800977 boost::container::flat_map<
978 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700979 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800980 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530981 sdbusplus::asio::object_server& objServer,
982 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800983{
Yong Li6fdfac02023-10-30 11:21:30 +0800984 for (auto device = foundDevices.begin(); device != foundDevices.end();)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800985 {
Yong Li6fdfac02023-10-30 11:21:30 +0800986 if (device->first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800987 {
Yong Li6fdfac02023-10-30 11:21:30 +0800988 objServer.remove_interface(device->second);
989 device = foundDevices.erase(device);
990 }
991 else
992 {
993 device++;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800994 }
995 }
996
James Feist5d7f4692020-06-17 18:22:33 -0700997 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
998 if (!fs::exists(busPath))
999 {
1000 if (dbusCall)
1001 {
1002 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
1003 << "\n";
1004 throw std::invalid_argument("Invalid Bus.");
1005 }
1006 return;
1007 }
1008
1009 std::vector<fs::path> i2cBuses;
1010 i2cBuses.emplace_back(busPath);
1011
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001012 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301013 i2cBuses, busmap, powerIsOn, objServer,
1014 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
1015 &objServer, &systemBus]() {
Patrick Williamsb7077432024-08-16 15:22:21 -04001016 for (auto busIface = dbusInterfaceMap.begin();
1017 busIface != dbusInterfaceMap.end();)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001018 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001019 if (busIface->first.first == static_cast<size_t>(busNum))
1020 {
1021 objServer.remove_interface(busIface->second);
1022 busIface = dbusInterfaceMap.erase(busIface);
1023 }
1024 else
1025 {
1026 busIface++;
1027 }
Yong Li6fdfac02023-10-30 11:21:30 +08001028 }
Patrick Williamsb7077432024-08-16 15:22:21 -04001029 auto found = busmap.find(busNum);
1030 if (found == busmap.end() || found->second == nullptr)
Yong Li6fdfac02023-10-30 11:21:30 +08001031 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001032 return;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001033 }
Patrick Williamsb7077432024-08-16 15:22:21 -04001034 for (auto& device : *(found->second))
1035 {
1036 addFruObjectToDbus(device.second, dbusInterfaceMap,
1037 static_cast<uint32_t>(busNum), device.first,
1038 unknownBusObjectCount, powerIsOn, objServer,
1039 systemBus);
1040 }
1041 });
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001042 scan->run();
1043}
1044
James Feist9eb0b582018-04-27 12:15:46 -07001045void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -07001046 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -08001047 boost::container::flat_map<
1048 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301049 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001050 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301051 sdbusplus::asio::object_server& objServer,
1052 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -08001053{
Ed Tanousaa497ed2023-02-28 13:43:41 -08001054 static boost::asio::steady_timer timer(io);
1055 timer.expires_from_now(std::chrono::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001056
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001057 // setup an async wait in case we get flooded with requests
Jian Zhang6e7cebf2022-11-29 18:30:21 +08001058 timer.async_wait([&](const boost::system::error_code& ec) {
1059 if (ec == boost::asio::error::operation_aborted)
1060 {
1061 return;
1062 }
1063
1064 if (ec)
1065 {
1066 std::cerr << "Error in timer: " << ec.message() << "\n";
1067 return;
1068 }
1069
James Feist4131aea2018-03-09 09:47:30 -08001070 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001071 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001072
Nikhil Potaded8884f12019-03-27 13:27:13 -07001073 boost::container::flat_map<size_t, fs::path> busPaths;
1074 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001075 {
James Feist4131aea2018-03-09 09:47:30 -08001076 std::cerr << "unable to find i2c devices\n";
1077 return;
James Feist918e18c2018-02-13 15:51:07 -08001078 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001079
Ed Tanous07d467b2021-02-23 14:48:37 -08001080 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001081 {
1082 i2cBuses.emplace_back(busPath.second);
1083 }
James Feist4131aea2018-03-09 09:47:30 -08001084
James Feist98132792019-07-09 13:29:09 -07001085 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001086 for (auto& [pair, interface] : foundDevices)
1087 {
1088 objServer.remove_interface(interface);
1089 }
1090 foundDevices.clear();
1091
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301092 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301093 i2cBuses, busmap, powerIsOn, objServer, [&]() {
Patrick Williamsb7077432024-08-16 15:22:21 -04001094 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001095 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001096 objServer.remove_interface(busIface.second);
James Feist6ebf9de2018-05-15 15:01:17 -07001097 }
Patrick Williamsb7077432024-08-16 15:22:21 -04001098
1099 dbusInterfaceMap.clear();
1100 unknownBusObjectCount = 0;
1101
1102 // todo, get this from a more sensable place
1103 std::vector<uint8_t> baseboardFRU;
1104 if (readBaseboardFRU(baseboardFRU))
1105 {
1106 // If no device on i2c bus 0, the insertion will happen.
1107 auto bus0 =
1108 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1109 bus0.first->second->emplace(0, baseboardFRU);
1110 }
1111 for (auto& devicemap : busmap)
1112 {
1113 for (auto& device : *devicemap.second)
1114 {
1115 addFruObjectToDbus(device.second, dbusInterfaceMap,
1116 devicemap.first, device.first,
1117 unknownBusObjectCount, powerIsOn,
1118 objServer, systemBus);
1119 }
1120 }
1121 });
James Feist6ebf9de2018-05-15 15:01:17 -07001122 scan->run();
1123 });
James Feist918e18c2018-02-13 15:51:07 -08001124}
1125
Joshi-Mansid73b7442020-03-27 19:10:21 +05301126// Details with example of Asset Tag Update
1127// To find location of Product Info Area asset tag as per FRU specification
1128// 1. Find product Info area starting offset (*8 - as header will be in
1129// multiple of 8 bytes).
1130// 2. Skip 3 bytes of product info area (like format version, area length,
1131// and language code).
1132// 3. Traverse manufacturer name, product name, product version, & product
1133// serial number, by reading type/length code to reach the Asset Tag.
1134// 4. Update the Asset Tag, reposition the product Info area in multiple of
1135// 8 bytes. Update the Product area length and checksum.
1136
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001137bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301138 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001139 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301140 boost::container::flat_map<
1141 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301142 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001143 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301144 sdbusplus::asio::object_server& objServer,
1145 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301146{
1147 size_t updatePropertyReqLen = updatePropertyReq.length();
1148 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1149 {
1150 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001151 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301152 "Invalid Length "
1153 << updatePropertyReqLen << "\n";
1154 return false;
1155 }
1156
1157 std::vector<uint8_t> fruData;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301158
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301159 if (!getFruData(fruData, bus, address))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301160 {
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301161 std::cerr << "Failure getting FRU Data \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301162 return false;
1163 }
1164
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +00001165 struct FruArea fruAreaParams
1166 {};
Joshi-Mansid73b7442020-03-27 19:10:21 +05301167
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301168 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301169 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301170 std::cerr << "findFruAreaLocationAndField failed \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301171 return false;
1172 }
1173
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301174 std::vector<uint8_t> restFRUAreaFieldsData;
1175 if (!copyRestFRUArea(fruData, propertyName, fruAreaParams,
1176 restFRUAreaFieldsData))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301177 {
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301178 std::cerr << "copyRestFRUArea failed \n";
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001179 return false;
1180 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301181
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001182 // Push post update fru areas if any
1183 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001184 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1185 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001186 {
1187 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001188 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301189 if ((fruAreaLoc > fruAreaParams.restFieldsEnd) &&
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001190 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1191 {
1192 nextFRUAreaLoc = fruAreaLoc;
1193 }
1194 }
1195 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001196 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001197 {
1198 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1199 fruData.size() - nextFRUAreaLoc,
1200 std::back_inserter(restFRUAreasData));
1201 }
1202
1203 // check FRU area size
1204 size_t fruAreaDataSize =
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301205 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) +
1206 restFRUAreaFieldsData.size());
1207 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001208 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1209 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001210#ifdef ENABLE_FRU_AREA_RESIZE
1211 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1212 // round size to 8-byte blocks
Patrick Williamsb7077432024-08-16 15:22:21 -04001213 newFRUAreaSize =
1214 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
1215 size_t newFRUDataSize =
1216 fruData.size() + newFRUAreaSize - fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001217 fruData.resize(newFRUDataSize);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301218 fruAreaParams.size = newFRUAreaSize;
1219 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001220#else
1221 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1222 << " should not be greater than available FRU area size: "
1223 << fruAreaAvailableSize << "\n";
1224 return false;
1225#endif // ENABLE_FRU_AREA_RESIZE
1226 }
1227
Joshi-Mansid73b7442020-03-27 19:10:21 +05301228 // write new requested property field length and data
1229 constexpr uint8_t newTypeLenMask = 0xC0;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301230 fruData[fruAreaParams.updateFieldLoc] =
Joshi-Mansid73b7442020-03-27 19:10:21 +05301231 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301232 fruAreaParams.updateFieldLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301233 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301234 fruData.begin() + fruAreaParams.updateFieldLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301235
1236 // Copy remaining data to main fru area - post updated fru field vector
Patrick Williamsb7077432024-08-16 15:22:21 -04001237 fruAreaParams.restFieldsLoc =
1238 fruAreaParams.updateFieldLoc + updatePropertyReqLen;
1239 size_t fruAreaDataEnd =
1240 fruAreaParams.restFieldsLoc + restFRUAreaFieldsData.size();
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301241
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001242 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301243 fruData.begin() + fruAreaParams.restFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301244
1245 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001246 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301247 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301248
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001249#ifdef ENABLE_FRU_AREA_RESIZE
1250 ++nextFRUAreaNewLoc;
Patrick Williamsb7077432024-08-16 15:22:21 -04001251 ssize_t nextFRUAreaOffsetDiff =
1252 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001253 // Append rest FRU Areas if size changed and there were other sections after
1254 // updated one
1255 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1256 {
1257 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1258 fruData.begin() + nextFRUAreaNewLoc);
1259 // Update Common Header
Ed Tanous6e22c872023-02-16 15:19:28 -08001260 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1261 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001262 {
Ed Tanous6e22c872023-02-16 15:19:28 -08001263 unsigned int fruAreaOffsetField =
1264 getHeaderAreaFieldOffset(nextFRUArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001265 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
Ed Tanous6e22c872023-02-16 15:19:28 -08001266 if (curFRUAreaOffset > fruAreaParams.end)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001267 {
1268 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1269 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1270 }
1271 }
1272 // Calculate new checksum
1273 std::vector<uint8_t> headerFRUData;
1274 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1275 size_t checksumVal = calculateChecksum(headerFRUData);
1276 fruData[7] = static_cast<uint8_t>(checksumVal);
1277 // fill zeros if FRU Area size decreased
1278 if (nextFRUAreaOffsetDiff < 0)
1279 {
1280 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1281 restFRUAreasData.size(),
1282 fruData.end(), 0);
1283 }
1284 }
1285#else
1286 // this is to avoid "unused variable" warning
1287 (void)nextFRUAreaNewLoc;
1288#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301289 if (fruData.empty())
1290 {
1291 return false;
1292 }
1293
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001294 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301295 fruData))
1296 {
1297 return false;
1298 }
1299
1300 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301301 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1302 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301303 return true;
1304}
1305
James Feist98132792019-07-09 13:29:09 -07001306int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001307{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301308 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1309 sdbusplus::asio::object_server objServer(systemBus);
1310
Kumar Thangavel41a90512021-11-24 15:44:20 +05301311 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301312 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001313 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001314 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001315 std::vector<fs::path> i2cBuses;
1316
James Feista3c180a2018-08-09 16:06:04 -07001317 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001318 {
1319 std::cerr << "unable to find i2c devices\n";
1320 return 1;
1321 }
James Feist3cb5fec2018-01-23 14:41:51 -08001322
Matt Simmering2447c242023-11-09 14:18:39 -08001323 // check for and load blocklist with initial buses.
1324 loadBlocklist(blocklistPath);
Patrick Venture11f1ff42019-08-01 10:42:12 -07001325
Vijay Khemka065f6d92018-12-18 10:37:47 -08001326 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001327
James Feist6ebf9de2018-05-15 15:01:17 -07001328 // this is a map with keys of pair(bus number, address) and values of
1329 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001330 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001331 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1332 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001333
James Feist9eb0b582018-04-27 12:15:46 -07001334 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1335 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1336 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001337
Kumar Thangavel41a90512021-11-24 15:44:20 +05301338 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301339 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1340 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301341 });
James Feist2a9d6db2018-04-27 15:48:28 -07001342
krishnar4213ee212022-11-11 15:39:30 +05301343 iface->register_method("ReScanBus", [&](uint16_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301344 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301345 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001346 });
1347
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001348 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001349
Patrick Williamsb7077432024-08-16 15:22:21 -04001350 iface->register_method(
1351 "WriteFru", [&](const uint16_t bus, const uint8_t address,
1352 const std::vector<uint8_t>& data) {
1353 if (!writeFRU(bus, address, data))
1354 {
1355 throw std::invalid_argument("Invalid Arguments.");
1356 return;
1357 }
1358 // schedule rescan on success
1359 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1360 powerIsOn, objServer, systemBus);
1361 });
James Feist9eb0b582018-04-27 12:15:46 -07001362 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001363
Patrick Williams2af39222022-07-22 19:26:56 -05001364 std::function<void(sdbusplus::message_t & message)> eventHandler =
1365 [&](sdbusplus::message_t& message) {
Patrick Williamsb7077432024-08-16 15:22:21 -04001366 std::string objectName;
1367 boost::container::flat_map<
1368 std::string,
1369 std::variant<std::string, bool, int64_t, uint64_t, double>>
1370 values;
1371 message.read(objectName, values);
1372 auto findState = values.find("CurrentHostState");
1373 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001374 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001375 if (std::get<std::string>(findState->second) ==
1376 "xyz.openbmc_project.State.Host.HostState.Running")
1377 {
1378 powerIsOn = true;
1379 }
James Feist0eeb79c2019-10-09 13:35:29 -07001380 }
James Feist6ebf9de2018-05-15 15:01:17 -07001381
Patrick Williamsb7077432024-08-16 15:22:21 -04001382 if (powerIsOn)
1383 {
1384 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1385 powerIsOn, objServer, systemBus);
1386 }
1387 };
James Feist9eb0b582018-04-27 12:15:46 -07001388
Patrick Williams2af39222022-07-22 19:26:56 -05001389 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1390 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001391 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001392 "openbmc_project/state/"
1393 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001394 eventHandler);
1395
James Feist4131aea2018-03-09 09:47:30 -08001396 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001397 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001398 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001399 // monitor for new i2c devices
1400 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1401 std::function<void(const boost::system::error_code, std::size_t)>
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001402 watchI2cBusses = [&](const boost::system::error_code& ec,
1403 std::size_t bytesTransferred) {
Patrick Williamsb7077432024-08-16 15:22:21 -04001404 if (ec)
James Feist4131aea2018-03-09 09:47:30 -08001405 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001406 std::cout << "Callback Error " << ec << "\n";
1407 return;
1408 }
1409 size_t index = 0;
1410 while ((index + sizeof(inotify_event)) <= bytesTransferred)
1411 {
1412 const char* p = &readBuffer[index];
1413 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1414 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
1415 switch (iEvent->mask)
Ed Tanousfc171422024-04-04 17:18:16 -07001416 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001417 case IN_CREATE:
1418 case IN_MOVED_TO:
1419 case IN_DELETE:
Patrick Williamsdf190612023-05-10 07:51:34 -05001420 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001421 std::string_view name(&iEvent->name[0], iEvent->len);
1422 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001423 {
Patrick Williamsb7077432024-08-16 15:22:21 -04001424 int bus = busStrToInt(name);
1425 if (bus < 0)
1426 {
1427 std::cerr
1428 << "Could not parse bus " << name << "\n";
1429 continue;
1430 }
1431 int rootBus = getRootBus(bus);
1432 if (rootBus >= 0)
1433 {
1434 rescanOneBus(busMap,
1435 static_cast<uint16_t>(rootBus),
1436 dbusInterfaceMap, false,
1437 unknownBusObjectCount, powerIsOn,
1438 objServer, systemBus);
1439 }
1440 rescanOneBus(busMap, static_cast<uint16_t>(bus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301441 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301442 unknownBusObjectCount, powerIsOn,
1443 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001444 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001445 }
Ed Tanousfc171422024-04-04 17:18:16 -07001446 break;
Patrick Williamsb7077432024-08-16 15:22:21 -04001447 default:
1448 break;
1449 }
1450 index += sizeof(inotify_event) + iEvent->len;
James Feist4131aea2018-03-09 09:47:30 -08001451 }
James Feist6ebf9de2018-05-15 15:01:17 -07001452
Patrick Williamsb7077432024-08-16 15:22:21 -04001453 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1454 watchI2cBusses);
1455 };
James Feist4131aea2018-03-09 09:47:30 -08001456
1457 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001458 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301459 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1460 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001461
James Feist3cb5fec2018-01-23 14:41:51 -08001462 io.run();
1463 return 0;
1464}