blob: 05f12bf0ad5bf17f2d712e602b3efa89e7d8764c [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(
153 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
154 std::to_string(bus) + "/mux_device"),
155 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.
Ed Tanous07d467b2021-02-23 14:48:37 -0800238static int i2cSmbusWriteThenRead(int file, uint16_t address,
Matt Simmering2447c242023-11-09 14:18:39 -0800239 uint8_t* toTargetBuf, uint8_t toTargetBufLen,
240 uint8_t* fromTargetBuf,
241 uint8_t fromTargetBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800242{
Matt Simmering2447c242023-11-09 14:18:39 -0800243 if (toTargetBuf == nullptr || toTargetBufLen == 0 ||
244 fromTargetBuf == nullptr || fromTargetBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800245 {
246 return -1;
247 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800248
Ed Tanous3013fb42022-07-09 08:27:06 -0700249 constexpr size_t smbusWriteThenReadMsgCount = 2;
250 std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{};
251 struct i2c_rdwr_ioctl_data rdwr
252 {};
Xiang Liu2801a702020-01-20 14:29:34 -0800253
254 msgs[0].addr = address;
255 msgs[0].flags = 0;
Matt Simmering2447c242023-11-09 14:18:39 -0800256 msgs[0].len = toTargetBufLen;
257 msgs[0].buf = toTargetBuf;
Xiang Liu2801a702020-01-20 14:29:34 -0800258 msgs[1].addr = address;
259 msgs[1].flags = I2C_M_RD;
Matt Simmering2447c242023-11-09 14:18:39 -0800260 msgs[1].len = fromTargetBufLen;
261 msgs[1].buf = fromTargetBuf;
Xiang Liu2801a702020-01-20 14:29:34 -0800262
Ed Tanous3013fb42022-07-09 08:27:06 -0700263 rdwr.msgs = msgs.data();
264 rdwr.nmsgs = msgs.size();
Xiang Liu2801a702020-01-20 14:29:34 -0800265
266 int ret = ioctl(file, I2C_RDWR, &rdwr);
267
Jason M. Billsf6e4c1f2022-08-19 12:23:51 -0700268 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800269}
270
Marvin Drees2b3ed302023-04-14 16:35:14 +0200271static int64_t readData(bool is16bit, bool isBytewise, int file,
272 uint16_t address, off_t offset, size_t len,
273 uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800274{
Zev Weiss309c0b12022-02-25 01:44:12 +0000275 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800276 {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200277 if (!isBytewise)
278 {
279 return i2c_smbus_read_i2c_block_data(
280 file, static_cast<uint8_t>(offset), len, buf);
281 }
282
283 std::span<uint8_t> bufspan{buf, len};
284 for (size_t i = 0; i < len; i++)
285 {
286 int byte = i2c_smbus_read_byte_data(
287 file, static_cast<uint8_t>(offset + i));
288 if (byte < 0)
289 {
290 return static_cast<int64_t>(byte);
291 }
292 bufspan[i] = static_cast<uint8_t>(byte);
293 }
294 return static_cast<int64_t>(len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800295 }
296
Xiang Liu2801a702020-01-20 14:29:34 -0800297 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700298 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
299 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
300 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800301}
302
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700303// TODO: This code is very similar to the non-eeprom version and can be merged
304// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300305static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700306{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700307 auto path = getEepromPath(bus, address);
308
309 int file = open(path.c_str(), O_RDONLY);
310 if (file < 0)
311 {
312 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700313 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700314 }
315
Patrick Venturebaba7672019-10-26 09:26:41 -0700316 std::string errorMessage = "eeprom at " + std::to_string(bus) +
317 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000318 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
319 return readFromEeprom(file, offset, length, outbuf);
320 };
321 FRUReader reader(std::move(readFunc));
Marvin Drees2b3ed302023-04-14 16:35:14 +0200322 std::pair<std::vector<uint8_t>, bool> pair = readFRUContents(reader,
323 errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700324
325 close(file);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200326 return pair.first;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700327}
328
Bonnie Lobfda2c42022-10-26 16:50:54 +0800329std::set<size_t> findI2CEeproms(int i2cBus,
330 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700331{
Bonnie Lobfda2c42022-10-26 16:50:54 +0800332 std::set<size_t> foundList;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700333
334 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
335
336 // For each file listed under the i2c device
337 // NOTE: This should be faster than just checking for each possible address
338 // path.
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700339 auto ec = std::error_code();
340 for (const auto& p : fs::directory_iterator(path, ec))
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700341 {
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700342 if (ec)
343 {
344 std::cerr << "directory_iterator err " << ec.message() << "\n";
345 break;
346 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700347 const std::string node = p.path().string();
348 std::smatch m;
Patrick Williamsdf190612023-05-10 07:51:34 -0500349 bool found = std::regex_match(node, m,
350 std::regex(".+\\d+-([0-9abcdef]+$)"));
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700351
352 if (!found)
353 {
354 continue;
355 }
356 if (m.size() != 2)
357 {
358 std::cerr << "regex didn't capture\n";
359 continue;
360 }
361
362 std::ssub_match subMatch = m[1];
363 std::string addressString = subMatch.str();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800364 std::string_view addressStringView(addressString);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700365
Bonnie Lobfda2c42022-10-26 16:50:54 +0800366 size_t address = 0;
367 std::from_chars(addressStringView.begin(), addressStringView.end(),
368 address, 16);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700369
370 const std::string eeprom = node + "/eeprom";
371
372 try
373 {
374 if (!fs::exists(eeprom))
375 {
376 continue;
377 }
378 }
379 catch (...)
380 {
381 continue;
382 }
383
384 // There is an eeprom file at this address, it may have invalid
385 // contents, but we found it.
386 foundList.insert(address);
387
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300388 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700389 if (!device.empty())
390 {
391 devices->emplace(address, device);
392 }
393 }
394
395 return foundList;
396}
397
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300398int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800399 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530400 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800401{
James Feist26c27ad2018-07-25 15:09:40 -0700402 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700403 // NOTE: When reading the devices raw on the bus, it can interfere with
404 // the driver's ability to operate, therefore read eeproms first before
405 // scanning for devices without drivers. Several experiments were run
406 // and it was determined that if there were any devices on the bus
407 // before the eeprom was hit and read, the eeprom driver wouldn't open
408 // while the bus device was open. An experiment was not performed to see
409 // if this issue was resolved if the i2c bus device was closed, but
410 // hexdumps of the eeprom later were successful.
411
412 // Scan for i2c eeproms loaded on this bus.
Bonnie Lobfda2c42022-10-26 16:50:54 +0800413 std::set<size_t> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800414 std::set<size_t>& failedItems = failedAddresses[bus];
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000415 std::set<size_t>& foundItems = fruAddresses[bus];
416 foundItems.clear();
James Feist7972bb92019-11-13 15:59:24 -0800417
Matt Simmering2447c242023-11-09 14:18:39 -0800418 auto busFind = busBlocklist.find(bus);
419 if (busFind != busBlocklist.end())
Bonnie Lobfda2c42022-10-26 16:50:54 +0800420 {
421 if (busFind->second != std::nullopt)
422 {
423 for (const auto& address : *(busFind->second))
424 {
425 skipList.insert(address);
426 }
427 }
428 }
429
James Feist7972bb92019-11-13 15:59:24 -0800430 std::set<size_t>* rootFailures = nullptr;
431 int rootBus = getRootBus(bus);
432
433 if (rootBus >= 0)
434 {
Matt Simmering2447c242023-11-09 14:18:39 -0800435 auto rootBusFind = busBlocklist.find(rootBus);
436 if (rootBusFind != busBlocklist.end())
Bonnie Lobfda2c42022-10-26 16:50:54 +0800437 {
438 if (rootBusFind->second != std::nullopt)
439 {
440 for (const auto& rootAddress : *(rootBusFind->second))
441 {
442 skipList.insert(rootAddress);
443 }
444 }
445 }
James Feist7972bb92019-11-13 15:59:24 -0800446 rootFailures = &(failedAddresses[rootBus]);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000447 foundItems = fruAddresses[rootBus];
James Feist7972bb92019-11-13 15:59:24 -0800448 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700449
Matt Simmering2447c242023-11-09 14:18:39 -0800450 constexpr int startSkipTargetAddr = 0;
451 constexpr int endSkipTargetAddr = 12;
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530452
James Feist26c27ad2018-07-25 15:09:40 -0700453 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800454 {
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000455 if (foundItems.find(ii) != foundItems.end())
456 {
457 continue;
458 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700459 if (skipList.find(ii) != skipList.end())
460 {
461 continue;
462 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530463 // skipping since no device is present in this range
Matt Simmering2447c242023-11-09 14:18:39 -0800464 if (ii >= startSkipTargetAddr && ii <= endSkipTargetAddr)
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530465 {
466 continue;
467 }
Matt Simmering2447c242023-11-09 14:18:39 -0800468 // Set target address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530469 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800470 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300471 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700472 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700473 continue;
474 }
475 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800476 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700477 {
478 continue;
479 }
James Feist3cb5fec2018-01-23 14:41:51 -0800480
Ed Tanous07d467b2021-02-23 14:48:37 -0800481 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700482 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700483 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700484 << "\n";
485 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800486
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530487 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700488
James Feist7972bb92019-11-13 15:59:24 -0800489 if (failedItems.find(ii) != failedItems.end())
490 {
491 // if we failed to read it once, unlikely we can read it later
492 continue;
493 }
494
495 if (rootFailures != nullptr)
496 {
497 if (rootFailures->find(ii) != rootFailures->end())
498 {
499 continue;
500 }
501 }
502
Vijay Khemka2d681f62018-11-06 15:51:00 -0800503 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000504 std::optional<bool> is16Bit = isDevice16Bit(file);
505 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800506 {
507 std::cerr << "failed to read bus " << bus << " address " << ii
508 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700509 if (powerIsOn)
510 {
511 failedItems.insert(ii);
512 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800513 continue;
514 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000515 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800516
Zev Weiss309c0b12022-02-25 01:44:12 +0000517 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
518 uint8_t* outbuf) {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200519 return readData(is16BitBool, false, file, ii, offset, length,
520 outbuf);
Zev Weiss309c0b12022-02-25 01:44:12 +0000521 };
522 FRUReader reader(std::move(readFunc));
Patrick Williamsdf190612023-05-10 07:51:34 -0500523 std::string errorMessage = "bus " + std::to_string(bus) +
524 " address " + std::to_string(ii);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200525 std::pair<std::vector<uint8_t>, bool> pair =
526 readFRUContents(reader, errorMessage);
527 const bool foundHeader = pair.second;
528
529 if (!foundHeader && !is16BitBool)
530 {
531 // certain FRU eeproms require bytewise reading.
532 // otherwise garbage is read. e.g. SuperMicro PWS 920P-SQ
533
534 auto readFunc = [is16BitBool, file, ii](off_t offset,
535 size_t length,
536 uint8_t* outbuf) {
537 return readData(is16BitBool, true, file, ii, offset, length,
538 outbuf);
539 };
540 FRUReader readerBytewise(std::move(readFunc));
541 pair = readFRUContents(readerBytewise, errorMessage);
542 }
543
544 if (pair.first.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700545 {
James Feist26c27ad2018-07-25 15:09:40 -0700546 continue;
547 }
James Feist26c27ad2018-07-25 15:09:40 -0700548
Marvin Drees2b3ed302023-04-14 16:35:14 +0200549 devices->emplace(ii, pair.first);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000550 fruAddresses[bus].insert(ii);
James Feist3cb5fec2018-01-23 14:41:51 -0800551 }
James Feist26c27ad2018-07-25 15:09:40 -0700552 return 1;
553 });
554 std::future_status status =
555 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
556 if (status == std::future_status::timeout)
557 {
558 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700559 if (powerIsOn)
560 {
Matt Simmering2447c242023-11-09 14:18:39 -0800561 busBlocklist[bus] = std::nullopt;
James Feistcb5661d2020-06-12 15:05:01 -0700562 }
James Feist444830e2019-04-05 08:38:16 -0700563 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700564 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800565 }
566
James Feist444830e2019-04-05 08:38:16 -0700567 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700568 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800569}
570
Matt Simmering2447c242023-11-09 14:18:39 -0800571void loadBlocklist(const char* path)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700572{
Matt Simmering2447c242023-11-09 14:18:39 -0800573 std::ifstream blocklistStream(path);
574 if (!blocklistStream.good())
Patrick Venture11f1ff42019-08-01 10:42:12 -0700575 {
576 // File is optional.
Matt Simmering2447c242023-11-09 14:18:39 -0800577 std::cerr << "Cannot open blocklist file.\n\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700578 return;
579 }
580
Matt Simmering2447c242023-11-09 14:18:39 -0800581 nlohmann::json data = nlohmann::json::parse(blocklistStream, nullptr,
Patrick Williamsdf190612023-05-10 07:51:34 -0500582 false);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700583 if (data.is_discarded())
584 {
Matt Simmering2447c242023-11-09 14:18:39 -0800585 std::cerr << "Illegal blocklist file detected, cannot validate JSON, "
Patrick Venture11f1ff42019-08-01 10:42:12 -0700586 "exiting\n";
587 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700588 }
589
590 // It's expected to have at least one field, "buses" that is an array of the
591 // buses by integer. Allow for future options to exclude further aspects,
592 // such as specific addresses or ranges.
593 if (data.type() != nlohmann::json::value_t::object)
594 {
Matt Simmering2447c242023-11-09 14:18:39 -0800595 std::cerr << "Illegal blocklist, expected to read dictionary\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700596 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700597 }
598
599 // If buses field is missing, that's fine.
600 if (data.count("buses") == 1)
601 {
602 // Parse the buses array after a little validation.
603 auto buses = data.at("buses");
604 if (buses.type() != nlohmann::json::value_t::array)
605 {
606 // Buses field present but invalid, therefore this is an error.
Matt Simmering2447c242023-11-09 14:18:39 -0800607 std::cerr << "Invalid contents for blocklist buses field\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700608 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700609 }
610
611 // Catch exception here for type mis-match.
612 try
613 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800614 for (const auto& busIterator : buses)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700615 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800616 // If bus and addresses field are missing, that's fine.
617 if (busIterator.contains("bus") &&
618 busIterator.contains("addresses"))
619 {
620 auto busData = busIterator.at("bus");
621 auto bus = busData.get<size_t>();
622
623 auto addressData = busIterator.at("addresses");
624 auto addresses =
625 addressData.get<std::set<std::string_view>>();
626
Ed Tanousfc171422024-04-04 17:18:16 -0700627 auto& block = busBlocklist[bus].emplace();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800628 for (const auto& address : addresses)
629 {
630 size_t addressInt = 0;
631 std::from_chars(address.begin() + 2, address.end(),
632 addressInt, 16);
Ed Tanousfc171422024-04-04 17:18:16 -0700633 block.insert(addressInt);
Bonnie Lobfda2c42022-10-26 16:50:54 +0800634 }
635 }
636 else
637 {
Matt Simmering2447c242023-11-09 14:18:39 -0800638 busBlocklist[busIterator.get<size_t>()] = std::nullopt;
Bonnie Lobfda2c42022-10-26 16:50:54 +0800639 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700640 }
641 }
642 catch (const nlohmann::detail::type_error& e)
643 {
644 // Type mis-match is a critical error.
645 std::cerr << "Invalid bus type: " << e.what() << "\n";
646 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700647 }
648 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700649}
650
Ed Tanous07d467b2021-02-23 14:48:37 -0800651static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800652 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530653 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800654{
Ed Tanous3013fb42022-07-09 08:27:06 -0700655 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800656 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700657 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800658
James Feist5d7f4692020-06-17 18:22:33 -0700659 if (bus < 0)
660 {
661 std::cerr << "Cannot translate " << i2cBus << " to int\n";
662 continue;
663 }
Matt Simmering2447c242023-11-09 14:18:39 -0800664 auto busFind = busBlocklist.find(bus);
665 if (busFind != busBlocklist.end())
James Feist444830e2019-04-05 08:38:16 -0700666 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800667 if (busFind->second == std::nullopt)
668 {
669 continue; // Skip blocked busses.
670 }
James Feist444830e2019-04-05 08:38:16 -0700671 }
James Feist0c3980a2019-12-19 11:09:27 -0800672 int rootBus = getRootBus(bus);
Matt Simmering2447c242023-11-09 14:18:39 -0800673 auto rootBusFind = busBlocklist.find(rootBus);
674 if (rootBusFind != busBlocklist.end())
James Feist0c3980a2019-12-19 11:09:27 -0800675 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800676 if (rootBusFind->second == std::nullopt)
677 {
678 continue;
679 }
James Feist0c3980a2019-12-19 11:09:27 -0800680 }
681
James Feist3cb5fec2018-01-23 14:41:51 -0800682 auto file = open(i2cBus.c_str(), O_RDWR);
683 if (file < 0)
684 {
685 std::cerr << "unable to open i2c device " << i2cBus.string()
686 << "\n";
687 continue;
688 }
689 unsigned long funcs = 0;
690
691 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
692 {
693 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700694 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800695 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700696 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800697 continue;
698 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700699 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
700 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800701 {
702 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
703 << bus << "\n";
704 continue;
705 }
James Feist98132792019-07-09 13:29:09 -0700706 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800707 device = std::make_shared<DeviceMap>();
708
Nikhil Potaded8884f12019-03-27 13:27:13 -0700709 // i2cdetect by default uses the range 0x03 to 0x77, as
710 // this is what we have tested with, use this range. Could be
711 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800712 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800713 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700714 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800715 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700716
717 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530718 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700719
Ed Tanous07d467b2021-02-23 14:48:37 -0800720 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800721 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700722 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800723 }
James Feist3cb5fec2018-01-23 14:41:51 -0800724 }
James Feist3cb5fec2018-01-23 14:41:51 -0800725}
726
James Feist6ebf9de2018-05-15 15:01:17 -0700727// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700728struct FindDevicesWithCallback :
729 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700730{
James Feista465ccc2019-02-08 12:51:01 -0800731 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800732 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530733 sdbusplus::asio::object_server& objServer,
Delphine CC Chiua3ca14a2024-03-27 17:02:24 +0800734 std::function<void()>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700735 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530736 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
737 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700738 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700739 ~FindDevicesWithCallback()
740 {
741 _callback();
742 }
743 void run()
744 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530745 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700746 }
747
James Feista465ccc2019-02-08 12:51:01 -0800748 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800749 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800750 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530751 sdbusplus::asio::object_server& _objServer;
Delphine CC Chiua3ca14a2024-03-27 17:02:24 +0800752 std::function<void()> _callback;
James Feist6ebf9de2018-05-15 15:01:17 -0700753};
754
Ed Tanous07d467b2021-02-23 14:48:37 -0800755void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300756 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800757 boost::container::flat_map<
758 std::pair<size_t, size_t>,
759 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530760 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800761 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530762 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800763{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300764 boost::container::flat_map<std::string, std::string> formattedFRU;
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530765
766 std::optional<std::string> optionalProductName = getProductName(
767 device, formattedFRU, bus, address, unknownBusObjectCount);
768 if (!optionalProductName)
James Feist3cb5fec2018-01-23 14:41:51 -0800769 {
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530770 std::cerr << "getProductName failed. product name is empty.\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800771 return;
772 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700773
Patrick Williamsdf190612023-05-10 07:51:34 -0500774 std::string productName = "/xyz/openbmc_project/FruDevice/" +
775 optionalProductName.value();
James Feist3cb5fec2018-01-23 14:41:51 -0800776
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530777 std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName);
778 if (index.has_value())
James Feist918e18c2018-02-13 15:51:07 -0800779 {
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530780 productName += "_";
781 productName += std::to_string(++(*index));
James Feist918e18c2018-02-13 15:51:07 -0800782 }
James Feist3cb5fec2018-01-23 14:41:51 -0800783
James Feist9eb0b582018-04-27 12:15:46 -0700784 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
785 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
786 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
787
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300788 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800789 {
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700790 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800791 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530792 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700793 {
794 continue;
795 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500796 std::string key = std::regex_replace(property.first, nonAsciiRegex,
797 "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530798
799 if (property.first == "PRODUCT_ASSET_TAG")
800 {
801 std::string propertyName = property.first;
802 iface->register_property(
803 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530804 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530805 &unknownBusObjectCount, &powerIsOn, &objServer,
806 &systemBus](const std::string& req, std::string& resp) {
Patrick Williamsdf190612023-05-10 07:51:34 -0500807 if (strcmp(req.c_str(), resp.c_str()) != 0)
808 {
809 // call the method which will update
810 if (updateFRUProperty(req, bus, address, propertyName,
811 dbusInterfaceMap,
812 unknownBusObjectCount, powerIsOn,
813 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530814 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500815 resp = req;
Joshi-Mansid73b7442020-03-27 19:10:21 +0530816 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500817 else
818 {
819 throw std::invalid_argument(
820 "FRU property update failed.");
821 }
822 }
823 return 1;
Patrick Williamsb9dd7f82023-10-20 11:20:11 -0500824 });
Joshi-Mansid73b7442020-03-27 19:10:21 +0530825 }
826 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700827 {
828 std::cerr << "illegal key: " << key << "\n";
829 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800830 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700831 {
832 std::cout << property.first << ": " << property.second << "\n";
833 }
James Feist3cb5fec2018-01-23 14:41:51 -0800834 }
James Feist2a9d6db2018-04-27 15:48:28 -0700835
836 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700837 iface->register_property("BUS", bus);
838 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700839
James Feist9eb0b582018-04-27 12:15:46 -0700840 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800841}
842
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300843static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800844{
845 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800846 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300847 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800848 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300849 baseboardFRUFile.seekg(0, std::ios_base::end);
850 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
851 baseboardFRU.resize(fileSize);
852 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700853 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
854 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
855 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800856 }
857 else
858 {
859 return false;
860 }
861 return true;
862}
863
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300864bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700865{
866 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800867 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700868 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300869 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700870 return false;
871 }
872 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800873 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700874 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300875 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700876 return false;
877 }
878 // baseboard fru
879 if (bus == 0 && address == 0)
880 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800881 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700882 if (!file.good())
883 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800884 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700885 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700886 return false;
887 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700888 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
889 const char* charOffset = reinterpret_cast<const char*>(fru.data());
890 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700891 return file.good();
892 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800893
894 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700895 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800896 auto path = getEepromPath(bus, address);
897 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
898 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700899 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800900 std::cerr << "unable to open i2c device " << path << "\n";
901 throw DBusInternalError();
902 return false;
903 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700904
Ed Tanous07d467b2021-02-23 14:48:37 -0800905 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
906 if (writtenBytes < 0)
907 {
908 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700909 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700910 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700911 return false;
912 }
913
Ed Tanous07d467b2021-02-23 14:48:37 -0800914 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700915 return true;
916 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800917
918 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
919
920 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
921 if (file < 0)
922 {
923 std::cerr << "unable to open i2c device " << i2cBus << "\n";
924 throw DBusInternalError();
925 return false;
926 }
927 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
928 {
929 std::cerr << "unable to set device address\n";
930 close(file);
931 throw DBusInternalError();
932 return false;
933 }
934
935 constexpr const size_t retryMax = 2;
936 uint16_t index = 0;
937 size_t retries = retryMax;
938 while (index < fru.size())
939 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700940 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800941 (retries == retryMax))
942 {
943 // The 4K EEPROM only uses the A2 and A1 device address bits
944 // with the third bit being a memory page address bit.
945 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
946 {
947 std::cerr << "unable to set device address\n";
948 close(file);
949 throw DBusInternalError();
950 return false;
951 }
952 }
953
954 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
955 fru[index]) < 0)
956 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700957 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800958 {
959 std::cerr << "error writing fru: " << strerror(errno) << "\n";
960 close(file);
961 throw DBusInternalError();
962 return false;
963 }
964 }
965 else
966 {
967 retries = retryMax;
968 index++;
969 }
970 // most eeproms require 5-10ms between writes
971 std::this_thread::sleep_for(std::chrono::milliseconds(10));
972 }
973 close(file);
974 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700975}
976
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800977void rescanOneBus(
krishnar4213ee212022-11-11 15:39:30 +0530978 BusMap& busmap, uint16_t busNum,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800979 boost::container::flat_map<
980 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700981 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800982 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530983 sdbusplus::asio::object_server& objServer,
984 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800985{
Yong Li6fdfac02023-10-30 11:21:30 +0800986 for (auto device = foundDevices.begin(); device != foundDevices.end();)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800987 {
Yong Li6fdfac02023-10-30 11:21:30 +0800988 if (device->first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800989 {
Yong Li6fdfac02023-10-30 11:21:30 +0800990 objServer.remove_interface(device->second);
991 device = foundDevices.erase(device);
992 }
993 else
994 {
995 device++;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800996 }
997 }
998
James Feist5d7f4692020-06-17 18:22:33 -0700999 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
1000 if (!fs::exists(busPath))
1001 {
1002 if (dbusCall)
1003 {
1004 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
1005 << "\n";
1006 throw std::invalid_argument("Invalid Bus.");
1007 }
1008 return;
1009 }
1010
1011 std::vector<fs::path> i2cBuses;
1012 i2cBuses.emplace_back(busPath);
1013
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001014 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301015 i2cBuses, busmap, powerIsOn, objServer,
1016 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
1017 &objServer, &systemBus]() {
Yong Li6fdfac02023-10-30 11:21:30 +08001018 for (auto busIface = dbusInterfaceMap.begin();
1019 busIface != dbusInterfaceMap.end();)
Patrick Williamsdf190612023-05-10 07:51:34 -05001020 {
Yong Li6fdfac02023-10-30 11:21:30 +08001021 if (busIface->first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001022 {
Yong Li6fdfac02023-10-30 11:21:30 +08001023 objServer.remove_interface(busIface->second);
1024 busIface = dbusInterfaceMap.erase(busIface);
1025 }
1026 else
1027 {
1028 busIface++;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001029 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001030 }
1031 auto found = busmap.find(busNum);
1032 if (found == busmap.end() || found->second == nullptr)
1033 {
1034 return;
1035 }
1036 for (auto& device : *(found->second))
1037 {
1038 addFruObjectToDbus(device.second, dbusInterfaceMap,
1039 static_cast<uint32_t>(busNum), device.first,
1040 unknownBusObjectCount, powerIsOn, objServer,
1041 systemBus);
1042 }
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001043 });
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001044 scan->run();
1045}
1046
James Feist9eb0b582018-04-27 12:15:46 -07001047void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -07001048 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -08001049 boost::container::flat_map<
1050 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301051 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001052 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301053 sdbusplus::asio::object_server& objServer,
1054 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -08001055{
Ed Tanousaa497ed2023-02-28 13:43:41 -08001056 static boost::asio::steady_timer timer(io);
1057 timer.expires_from_now(std::chrono::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001058
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001059 // setup an async wait in case we get flooded with requests
Jian Zhang6e7cebf2022-11-29 18:30:21 +08001060 timer.async_wait([&](const boost::system::error_code& ec) {
1061 if (ec == boost::asio::error::operation_aborted)
1062 {
1063 return;
1064 }
1065
1066 if (ec)
1067 {
1068 std::cerr << "Error in timer: " << ec.message() << "\n";
1069 return;
1070 }
1071
James Feist4131aea2018-03-09 09:47:30 -08001072 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001073 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001074
Nikhil Potaded8884f12019-03-27 13:27:13 -07001075 boost::container::flat_map<size_t, fs::path> busPaths;
1076 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001077 {
James Feist4131aea2018-03-09 09:47:30 -08001078 std::cerr << "unable to find i2c devices\n";
1079 return;
James Feist918e18c2018-02-13 15:51:07 -08001080 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001081
Ed Tanous07d467b2021-02-23 14:48:37 -08001082 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001083 {
1084 i2cBuses.emplace_back(busPath.second);
1085 }
James Feist4131aea2018-03-09 09:47:30 -08001086
James Feist98132792019-07-09 13:29:09 -07001087 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001088 for (auto& [pair, interface] : foundDevices)
1089 {
1090 objServer.remove_interface(interface);
1091 }
1092 foundDevices.clear();
1093
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301094 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301095 i2cBuses, busmap, powerIsOn, objServer, [&]() {
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001096 for (auto& busIface : dbusInterfaceMap)
1097 {
1098 objServer.remove_interface(busIface.second);
1099 }
James Feist4131aea2018-03-09 09:47:30 -08001100
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001101 dbusInterfaceMap.clear();
1102 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001103
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001104 // todo, get this from a more sensable place
1105 std::vector<uint8_t> baseboardFRU;
1106 if (readBaseboardFRU(baseboardFRU))
1107 {
1108 // If no device on i2c bus 0, the insertion will happen.
1109 auto bus0 = busmap.try_emplace(0,
1110 std::make_shared<DeviceMap>());
1111 bus0.first->second->emplace(0, baseboardFRU);
1112 }
1113 for (auto& devicemap : busmap)
1114 {
1115 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001116 {
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001117 addFruObjectToDbus(device.second, dbusInterfaceMap,
1118 devicemap.first, device.first,
1119 unknownBusObjectCount, powerIsOn,
1120 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001121 }
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001122 }
1123 });
James Feist6ebf9de2018-05-15 15:01:17 -07001124 scan->run();
1125 });
James Feist918e18c2018-02-13 15:51:07 -08001126}
1127
Joshi-Mansid73b7442020-03-27 19:10:21 +05301128// Details with example of Asset Tag Update
1129// To find location of Product Info Area asset tag as per FRU specification
1130// 1. Find product Info area starting offset (*8 - as header will be in
1131// multiple of 8 bytes).
1132// 2. Skip 3 bytes of product info area (like format version, area length,
1133// and language code).
1134// 3. Traverse manufacturer name, product name, product version, & product
1135// serial number, by reading type/length code to reach the Asset Tag.
1136// 4. Update the Asset Tag, reposition the product Info area in multiple of
1137// 8 bytes. Update the Product area length and checksum.
1138
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001139bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301140 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001141 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301142 boost::container::flat_map<
1143 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301144 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001145 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301146 sdbusplus::asio::object_server& objServer,
1147 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301148{
1149 size_t updatePropertyReqLen = updatePropertyReq.length();
1150 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1151 {
1152 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001153 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301154 "Invalid Length "
1155 << updatePropertyReqLen << "\n";
1156 return false;
1157 }
1158
1159 std::vector<uint8_t> fruData;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301160
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301161 if (!getFruData(fruData, bus, address))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301162 {
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301163 std::cerr << "Failure getting FRU Data \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301164 return false;
1165 }
1166
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +00001167 struct FruArea fruAreaParams
1168 {};
Joshi-Mansid73b7442020-03-27 19:10:21 +05301169
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301170 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301171 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301172 std::cerr << "findFruAreaLocationAndField failed \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301173 return false;
1174 }
1175
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301176 std::vector<uint8_t> restFRUAreaFieldsData;
1177 if (!copyRestFRUArea(fruData, propertyName, fruAreaParams,
1178 restFRUAreaFieldsData))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301179 {
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301180 std::cerr << "copyRestFRUArea failed \n";
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001181 return false;
1182 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301183
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001184 // Push post update fru areas if any
1185 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001186 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1187 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001188 {
1189 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001190 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301191 if ((fruAreaLoc > fruAreaParams.restFieldsEnd) &&
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001192 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1193 {
1194 nextFRUAreaLoc = fruAreaLoc;
1195 }
1196 }
1197 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001198 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001199 {
1200 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1201 fruData.size() - nextFRUAreaLoc,
1202 std::back_inserter(restFRUAreasData));
1203 }
1204
1205 // check FRU area size
1206 size_t fruAreaDataSize =
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301207 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) +
1208 restFRUAreaFieldsData.size());
1209 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001210 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1211 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001212#ifdef ENABLE_FRU_AREA_RESIZE
1213 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1214 // round size to 8-byte blocks
Patrick Williamsdf190612023-05-10 07:51:34 -05001215 newFRUAreaSize = ((newFRUAreaSize - 1) / fruBlockSize + 1) *
1216 fruBlockSize;
1217 size_t newFRUDataSize = fruData.size() + newFRUAreaSize -
1218 fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001219 fruData.resize(newFRUDataSize);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301220 fruAreaParams.size = newFRUAreaSize;
1221 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001222#else
1223 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1224 << " should not be greater than available FRU area size: "
1225 << fruAreaAvailableSize << "\n";
1226 return false;
1227#endif // ENABLE_FRU_AREA_RESIZE
1228 }
1229
Joshi-Mansid73b7442020-03-27 19:10:21 +05301230 // write new requested property field length and data
1231 constexpr uint8_t newTypeLenMask = 0xC0;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301232 fruData[fruAreaParams.updateFieldLoc] =
Joshi-Mansid73b7442020-03-27 19:10:21 +05301233 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301234 fruAreaParams.updateFieldLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301235 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301236 fruData.begin() + fruAreaParams.updateFieldLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301237
1238 // Copy remaining data to main fru area - post updated fru field vector
Patrick Williamsdf190612023-05-10 07:51:34 -05001239 fruAreaParams.restFieldsLoc = fruAreaParams.updateFieldLoc +
1240 updatePropertyReqLen;
1241 size_t fruAreaDataEnd = fruAreaParams.restFieldsLoc +
1242 restFRUAreaFieldsData.size();
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301243
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001244 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301245 fruData.begin() + fruAreaParams.restFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301246
1247 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001248 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301249 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301250
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001251#ifdef ENABLE_FRU_AREA_RESIZE
1252 ++nextFRUAreaNewLoc;
Patrick Williamsdf190612023-05-10 07:51:34 -05001253 ssize_t nextFRUAreaOffsetDiff = (nextFRUAreaNewLoc - nextFRUAreaLoc) /
1254 fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001255 // Append rest FRU Areas if size changed and there were other sections after
1256 // updated one
1257 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1258 {
1259 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1260 fruData.begin() + nextFRUAreaNewLoc);
1261 // Update Common Header
Ed Tanous6e22c872023-02-16 15:19:28 -08001262 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1263 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001264 {
Ed Tanous6e22c872023-02-16 15:19:28 -08001265 unsigned int fruAreaOffsetField =
1266 getHeaderAreaFieldOffset(nextFRUArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001267 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
Ed Tanous6e22c872023-02-16 15:19:28 -08001268 if (curFRUAreaOffset > fruAreaParams.end)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001269 {
1270 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1271 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1272 }
1273 }
1274 // Calculate new checksum
1275 std::vector<uint8_t> headerFRUData;
1276 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1277 size_t checksumVal = calculateChecksum(headerFRUData);
1278 fruData[7] = static_cast<uint8_t>(checksumVal);
1279 // fill zeros if FRU Area size decreased
1280 if (nextFRUAreaOffsetDiff < 0)
1281 {
1282 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1283 restFRUAreasData.size(),
1284 fruData.end(), 0);
1285 }
1286 }
1287#else
1288 // this is to avoid "unused variable" warning
1289 (void)nextFRUAreaNewLoc;
1290#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301291 if (fruData.empty())
1292 {
1293 return false;
1294 }
1295
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001296 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301297 fruData))
1298 {
1299 return false;
1300 }
1301
1302 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301303 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1304 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301305 return true;
1306}
1307
James Feist98132792019-07-09 13:29:09 -07001308int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001309{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301310 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1311 sdbusplus::asio::object_server objServer(systemBus);
1312
Kumar Thangavel41a90512021-11-24 15:44:20 +05301313 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301314 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001315 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001316 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001317 std::vector<fs::path> i2cBuses;
1318
James Feista3c180a2018-08-09 16:06:04 -07001319 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001320 {
1321 std::cerr << "unable to find i2c devices\n";
1322 return 1;
1323 }
James Feist3cb5fec2018-01-23 14:41:51 -08001324
Matt Simmering2447c242023-11-09 14:18:39 -08001325 // check for and load blocklist with initial buses.
1326 loadBlocklist(blocklistPath);
Patrick Venture11f1ff42019-08-01 10:42:12 -07001327
Vijay Khemka065f6d92018-12-18 10:37:47 -08001328 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001329
James Feist6ebf9de2018-05-15 15:01:17 -07001330 // this is a map with keys of pair(bus number, address) and values of
1331 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001332 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001333 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1334 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001335
James Feist9eb0b582018-04-27 12:15:46 -07001336 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1337 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1338 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001339
Kumar Thangavel41a90512021-11-24 15:44:20 +05301340 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301341 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1342 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301343 });
James Feist2a9d6db2018-04-27 15:48:28 -07001344
krishnar4213ee212022-11-11 15:39:30 +05301345 iface->register_method("ReScanBus", [&](uint16_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301346 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301347 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001348 });
1349
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001350 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001351
Patrick Williamsdf190612023-05-10 07:51:34 -05001352 iface->register_method("WriteFru",
1353 [&](const uint16_t bus, const uint8_t address,
1354 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001355 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001356 {
James Feistddb78302018-09-06 11:45:42 -07001357 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001358 return;
1359 }
1360 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301361 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1362 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001363 });
James Feist9eb0b582018-04-27 12:15:46 -07001364 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001365
Patrick Williams2af39222022-07-22 19:26:56 -05001366 std::function<void(sdbusplus::message_t & message)> eventHandler =
1367 [&](sdbusplus::message_t& message) {
Patrick Williamsdf190612023-05-10 07:51:34 -05001368 std::string objectName;
1369 boost::container::flat_map<
1370 std::string,
1371 std::variant<std::string, bool, int64_t, uint64_t, double>>
1372 values;
1373 message.read(objectName, values);
1374 auto findState = values.find("CurrentHostState");
1375 if (findState != values.end())
1376 {
1377 if (std::get<std::string>(findState->second) ==
1378 "xyz.openbmc_project.State.Host.HostState.Running")
James Feist918e18c2018-02-13 15:51:07 -08001379 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001380 powerIsOn = true;
James Feist0eeb79c2019-10-09 13:35:29 -07001381 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001382 }
James Feist6ebf9de2018-05-15 15:01:17 -07001383
Patrick Williamsdf190612023-05-10 07:51:34 -05001384 if (powerIsOn)
1385 {
1386 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1387 powerIsOn, objServer, systemBus);
1388 }
1389 };
James Feist9eb0b582018-04-27 12:15:46 -07001390
Patrick Williams2af39222022-07-22 19:26:56 -05001391 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1392 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001393 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001394 "openbmc_project/state/"
1395 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001396 eventHandler);
1397
James Feist4131aea2018-03-09 09:47:30 -08001398 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001399 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001400 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001401 // monitor for new i2c devices
1402 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1403 std::function<void(const boost::system::error_code, std::size_t)>
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001404 watchI2cBusses = [&](const boost::system::error_code& ec,
1405 std::size_t bytesTransferred) {
Patrick Williamsdf190612023-05-10 07:51:34 -05001406 if (ec)
1407 {
1408 std::cout << "Callback Error " << ec << "\n";
1409 return;
1410 }
1411 size_t index = 0;
1412 while ((index + sizeof(inotify_event)) <= bytesTransferred)
1413 {
1414 const char* p = &readBuffer[index];
1415 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1416 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
1417 switch (iEvent->mask)
James Feist4131aea2018-03-09 09:47:30 -08001418 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001419 case IN_CREATE:
1420 case IN_MOVED_TO:
1421 case IN_DELETE:
Ed Tanousfc171422024-04-04 17:18:16 -07001422 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001423 std::string_view name(&iEvent->name[0], iEvent->len);
1424 if (boost::starts_with(name, "i2c"))
1425 {
1426 int bus = busStrToInt(name);
1427 if (bus < 0)
James Feist9eb0b582018-04-27 12:15:46 -07001428 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001429 std::cerr << "Could not parse bus " << name << "\n";
1430 continue;
1431 }
1432 int rootBus = getRootBus(bus);
1433 if (rootBus >= 0)
1434 {
1435 rescanOneBus(busMap, static_cast<uint16_t>(rootBus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301436 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301437 unknownBusObjectCount, powerIsOn,
1438 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001439 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001440 rescanOneBus(busMap, static_cast<uint16_t>(bus),
1441 dbusInterfaceMap, false,
1442 unknownBusObjectCount, powerIsOn,
1443 objServer, systemBus);
1444 }
Ed Tanousfc171422024-04-04 17:18:16 -07001445 }
1446 break;
1447 default:
1448 break;
James Feist4131aea2018-03-09 09:47:30 -08001449 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001450 index += sizeof(inotify_event) + iEvent->len;
1451 }
James Feist6ebf9de2018-05-15 15:01:17 -07001452
Patrick Williamsdf190612023-05-10 07:51:34 -05001453 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}