blob: 6644d93826a0b345f184b868a6829b5f13ea3420 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Brad Bishope45d8c72022-05-25 15:12:53 -040016/// \file fru_device.cpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
Brad Bishope45d8c72022-05-25 15:12:53 -040018#include "fru_utils.hpp"
19#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070020
James Feist3cb5fec2018-01-23 14:41:51 -080021#include <fcntl.h>
James Feist3b860982018-10-02 14:34:07 -070022#include <sys/inotify.h>
23#include <sys/ioctl.h>
24
James Feist3b860982018-10-02 14:34:07 -070025#include <boost/algorithm/string/predicate.hpp>
Ed Tanous34c3e012023-03-06 13:42:00 -080026#include <boost/asio/io_context.hpp>
Ed Tanousaa497ed2023-02-28 13:43:41 -080027#include <boost/asio/steady_timer.hpp>
James Feist3b860982018-10-02 14:34:07 -070028#include <boost/container/flat_map.hpp>
James Feist8c505da2020-05-28 10:06:33 -070029#include <nlohmann/json.hpp>
30#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
33#include <array>
Ed Tanous07d467b2021-02-23 14:48:37 -080034#include <cerrno>
Ed Tanous3013fb42022-07-09 08:27:06 -070035#include <charconv>
James Feist3b860982018-10-02 14:34:07 -070036#include <chrono>
37#include <ctime>
Patrick Venturee3754002019-08-06 09:39:12 -070038#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080039#include <fstream>
Patrick Venturebaba7672019-10-26 09:26:41 -070040#include <functional>
James Feist3cb5fec2018-01-23 14:41:51 -080041#include <future>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070042#include <iomanip>
James Feist3cb5fec2018-01-23 14:41:51 -080043#include <iostream>
Patrick Venturee26395d2019-10-29 14:05:16 -070044#include <limits>
Andrew Jefferya9c58922021-06-01 09:28:59 +093045#include <map>
Bonnie Lobfda2c42022-10-26 16:50:54 +080046#include <optional>
James Feist3f8a2782018-02-12 09:24:42 -080047#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070048#include <set>
49#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070050#include <string>
James Feist3b860982018-10-02 14:34:07 -070051#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080052#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080053#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070054#include <vector>
James Feist3b860982018-10-02 14:34:07 -070055
James Feist8c505da2020-05-28 10:06:33 -070056extern "C"
57{
James Feist3b860982018-10-02 14:34:07 -070058#include <i2c/smbus.h>
59#include <linux/i2c-dev.h>
60}
James Feist3cb5fec2018-01-23 14:41:51 -080061
Ed Tanous072e25d2018-12-16 21:45:20 -080062namespace fs = std::filesystem;
Ed Tanous07d467b2021-02-23 14:48:37 -080063static constexpr bool debug = false;
Ed Tanous07d467b2021-02-23 14:48:37 -080064constexpr size_t maxFruSize = 512;
65constexpr size_t maxEepromPageIndex = 255;
ankita prasad7329a222023-07-07 09:18:10 +000066constexpr size_t busTimeoutSeconds = 10;
James Feist3cb5fec2018-01-23 14:41:51 -080067
Patrick Venture11f1ff42019-08-01 10:42:12 -070068constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
69
Ed Tanous07d467b2021-02-23 14:48:37 -080070const static constexpr char* baseboardFruLocation =
James Feist3cb5fec2018-01-23 14:41:51 -080071 "/etc/fru/baseboard.fru.bin";
72
Ed Tanous07d467b2021-02-23 14:48:37 -080073const static constexpr char* i2CDevLocation = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080074
Bonnie Lobfda2c42022-10-26 16:50:54 +080075static boost::container::flat_map<size_t, std::optional<std::set<size_t>>>
76 busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070077struct FindDevicesWithCallback;
78
James Feist8a983922019-10-24 17:11:56 -070079static boost::container::flat_map<
80 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
81 foundDevices;
82
James Feist7972bb92019-11-13 15:59:24 -080083static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +000084static boost::container::flat_map<size_t, std::set<size_t>> fruAddresses;
James Feist7972bb92019-11-13 15:59:24 -080085
Ed Tanous34c3e012023-03-06 13:42:00 -080086boost::asio::io_context io;
James Feist5cb06082019-10-24 17:11:13 -070087
Andrei Kartashev2f0de172020-08-13 14:29:40 +030088bool updateFRUProperty(
Ed Tanous3013fb42022-07-09 08:27:06 -070089 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -080090 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +053091 boost::container::flat_map<
92 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +053093 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -080094 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +053095 sdbusplus::asio::object_server& objServer,
96 std::shared_ptr<sdbusplus::asio::connection>& systemBus);
Patrick Venturebaba7672019-10-26 09:26:41 -070097
Patrick Venturec50e1ff2019-08-06 10:22:28 -070098// Given a bus/address, produce the path in sysfs for an eeprom.
99static std::string getEepromPath(size_t bus, size_t address)
100{
101 std::stringstream output;
102 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
103 << std::setfill('0') << std::setw(4) << std::hex << address
104 << "/eeprom";
105 return output.str();
106}
107
108static bool hasEepromFile(size_t bus, size_t address)
109{
110 auto path = getEepromPath(bus, address);
111 try
112 {
113 return fs::exists(path);
114 }
115 catch (...)
116 {
117 return false;
118 }
119}
120
Zev Weiss309c0b12022-02-25 01:44:12 +0000121static int64_t readFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700122{
123 auto result = lseek(fd, offset, SEEK_SET);
124 if (result < 0)
125 {
126 std::cerr << "failed to seek\n";
127 return -1;
128 }
129
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700130 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700131}
132
Ed Tanous3013fb42022-07-09 08:27:06 -0700133static int busStrToInt(const std::string_view busName)
James Feist5d7f4692020-06-17 18:22:33 -0700134{
Ed Tanous07d467b2021-02-23 14:48:37 -0800135 auto findBus = busName.rfind('-');
James Feist5d7f4692020-06-17 18:22:33 -0700136 if (findBus == std::string::npos)
137 {
138 return -1;
139 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700140 std::string_view num = busName.substr(findBus + 1);
141 int val = 0;
142 std::from_chars(num.data(), num.data() + num.size(), val);
143 return val;
James Feist5d7f4692020-06-17 18:22:33 -0700144}
145
James Feist7972bb92019-11-13 15:59:24 -0800146static int getRootBus(size_t bus)
147{
148 auto ec = std::error_code();
149 auto path = std::filesystem::read_symlink(
150 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
151 std::to_string(bus) + "/mux_device"),
152 ec);
153 if (ec)
154 {
155 return -1;
156 }
157
158 std::string filename = path.filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800159 auto findBus = filename.find('-');
James Feist7972bb92019-11-13 15:59:24 -0800160 if (findBus == std::string::npos)
161 {
162 return -1;
163 }
164 return std::stoi(filename.substr(0, findBus));
165}
166
James Feistc95cb142018-02-26 10:41:42 -0800167static bool isMuxBus(size_t bus)
168{
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700169 auto ec = std::error_code();
170 auto isSymlink =
171 is_symlink(std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
172 std::to_string(bus) + "/mux_device"),
173 ec);
174 return (!ec && isSymlink);
James Feistc95cb142018-02-26 10:41:42 -0800175}
176
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530177static void makeProbeInterface(size_t bus, size_t address,
178 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700179{
180 if (isMuxBus(bus))
181 {
182 return; // the mux buses are random, no need to publish
183 }
184 auto [it, success] = foundDevices.emplace(
185 std::make_pair(bus, address),
186 objServer.add_interface(
187 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
188 std::to_string(address),
189 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
190 if (!success)
191 {
192 return; // already added
193 }
194 it->second->register_property("Bus", bus);
195 it->second->register_property("Address", address);
196 it->second->initialize();
197}
198
Zev Weiss309c0b12022-02-25 01:44:12 +0000199static std::optional<bool> isDevice16Bit(int file)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800200{
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700201 // Set the higher data word address bits to 0. It's safe on 8-bit addressing
202 // EEPROMs because it doesn't write any actual data.
203 int ret = i2c_smbus_write_byte(file, 0);
204 if (ret < 0)
205 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000206 return std::nullopt;
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700207 }
208
Vijay Khemka2d681f62018-11-06 15:51:00 -0800209 /* Get first byte */
210 int byte1 = i2c_smbus_read_byte_data(file, 0);
211 if (byte1 < 0)
212 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000213 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800214 }
215 /* Read 7 more bytes, it will read same first byte in case of
216 * 8 bit but it will read next byte in case of 16 bit
217 */
218 for (int i = 0; i < 7; i++)
219 {
220 int byte2 = i2c_smbus_read_byte_data(file, 0);
221 if (byte2 < 0)
222 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000223 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800224 }
225 if (byte2 != byte1)
226 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000227 return true;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800228 }
229 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000230 return false;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800231}
232
Xiang Liu2801a702020-01-20 14:29:34 -0800233// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
234// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800235static int i2cSmbusWriteThenRead(int file, uint16_t address,
236 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
237 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800238{
Ed Tanous07d467b2021-02-23 14:48:37 -0800239 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
240 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800241 {
242 return -1;
243 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800244
Ed Tanous3013fb42022-07-09 08:27:06 -0700245 constexpr size_t smbusWriteThenReadMsgCount = 2;
246 std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{};
247 struct i2c_rdwr_ioctl_data rdwr
248 {};
Xiang Liu2801a702020-01-20 14:29:34 -0800249
250 msgs[0].addr = address;
251 msgs[0].flags = 0;
252 msgs[0].len = toSlaveBufLen;
253 msgs[0].buf = toSlaveBuf;
254 msgs[1].addr = address;
255 msgs[1].flags = I2C_M_RD;
256 msgs[1].len = fromSlaveBufLen;
257 msgs[1].buf = fromSlaveBuf;
258
Ed Tanous3013fb42022-07-09 08:27:06 -0700259 rdwr.msgs = msgs.data();
260 rdwr.nmsgs = msgs.size();
Xiang Liu2801a702020-01-20 14:29:34 -0800261
262 int ret = ioctl(file, I2C_RDWR, &rdwr);
263
Jason M. Billsf6e4c1f2022-08-19 12:23:51 -0700264 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800265}
266
Marvin Drees2b3ed302023-04-14 16:35:14 +0200267static int64_t readData(bool is16bit, bool isBytewise, int file,
268 uint16_t address, off_t offset, size_t len,
269 uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800270{
Zev Weiss309c0b12022-02-25 01:44:12 +0000271 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800272 {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200273 if (!isBytewise)
274 {
275 return i2c_smbus_read_i2c_block_data(
276 file, static_cast<uint8_t>(offset), len, buf);
277 }
278
279 std::span<uint8_t> bufspan{buf, len};
280 for (size_t i = 0; i < len; i++)
281 {
282 int byte = i2c_smbus_read_byte_data(
283 file, static_cast<uint8_t>(offset + i));
284 if (byte < 0)
285 {
286 return static_cast<int64_t>(byte);
287 }
288 bufspan[i] = static_cast<uint8_t>(byte);
289 }
290 return static_cast<int64_t>(len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800291 }
292
Xiang Liu2801a702020-01-20 14:29:34 -0800293 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700294 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
295 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
296 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800297}
298
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700299// TODO: This code is very similar to the non-eeprom version and can be merged
300// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300301static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700302{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700303 auto path = getEepromPath(bus, address);
304
305 int file = open(path.c_str(), O_RDONLY);
306 if (file < 0)
307 {
308 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700309 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700310 }
311
Patrick Venturebaba7672019-10-26 09:26:41 -0700312 std::string errorMessage = "eeprom at " + std::to_string(bus) +
313 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000314 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
315 return readFromEeprom(file, offset, length, outbuf);
316 };
317 FRUReader reader(std::move(readFunc));
Marvin Drees2b3ed302023-04-14 16:35:14 +0200318 std::pair<std::vector<uint8_t>, bool> pair = readFRUContents(reader,
319 errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700320
321 close(file);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200322 return pair.first;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700323}
324
Bonnie Lobfda2c42022-10-26 16:50:54 +0800325std::set<size_t> findI2CEeproms(int i2cBus,
326 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700327{
Bonnie Lobfda2c42022-10-26 16:50:54 +0800328 std::set<size_t> foundList;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700329
330 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
331
332 // For each file listed under the i2c device
333 // NOTE: This should be faster than just checking for each possible address
334 // path.
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700335 auto ec = std::error_code();
336 for (const auto& p : fs::directory_iterator(path, ec))
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700337 {
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700338 if (ec)
339 {
340 std::cerr << "directory_iterator err " << ec.message() << "\n";
341 break;
342 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700343 const std::string node = p.path().string();
344 std::smatch m;
Patrick Williamsdf190612023-05-10 07:51:34 -0500345 bool found = std::regex_match(node, m,
346 std::regex(".+\\d+-([0-9abcdef]+$)"));
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700347
348 if (!found)
349 {
350 continue;
351 }
352 if (m.size() != 2)
353 {
354 std::cerr << "regex didn't capture\n";
355 continue;
356 }
357
358 std::ssub_match subMatch = m[1];
359 std::string addressString = subMatch.str();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800360 std::string_view addressStringView(addressString);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700361
Bonnie Lobfda2c42022-10-26 16:50:54 +0800362 size_t address = 0;
363 std::from_chars(addressStringView.begin(), addressStringView.end(),
364 address, 16);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700365
366 const std::string eeprom = node + "/eeprom";
367
368 try
369 {
370 if (!fs::exists(eeprom))
371 {
372 continue;
373 }
374 }
375 catch (...)
376 {
377 continue;
378 }
379
380 // There is an eeprom file at this address, it may have invalid
381 // contents, but we found it.
382 foundList.insert(address);
383
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300384 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700385 if (!device.empty())
386 {
387 devices->emplace(address, device);
388 }
389 }
390
391 return foundList;
392}
393
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300394int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800395 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530396 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800397{
James Feist26c27ad2018-07-25 15:09:40 -0700398 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700399 // NOTE: When reading the devices raw on the bus, it can interfere with
400 // the driver's ability to operate, therefore read eeproms first before
401 // scanning for devices without drivers. Several experiments were run
402 // and it was determined that if there were any devices on the bus
403 // before the eeprom was hit and read, the eeprom driver wouldn't open
404 // while the bus device was open. An experiment was not performed to see
405 // if this issue was resolved if the i2c bus device was closed, but
406 // hexdumps of the eeprom later were successful.
407
408 // Scan for i2c eeproms loaded on this bus.
Bonnie Lobfda2c42022-10-26 16:50:54 +0800409 std::set<size_t> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800410 std::set<size_t>& failedItems = failedAddresses[bus];
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000411 std::set<size_t>& foundItems = fruAddresses[bus];
412 foundItems.clear();
James Feist7972bb92019-11-13 15:59:24 -0800413
Bonnie Lobfda2c42022-10-26 16:50:54 +0800414 auto busFind = busBlacklist.find(bus);
415 if (busFind != busBlacklist.end())
416 {
417 if (busFind->second != std::nullopt)
418 {
419 for (const auto& address : *(busFind->second))
420 {
421 skipList.insert(address);
422 }
423 }
424 }
425
James Feist7972bb92019-11-13 15:59:24 -0800426 std::set<size_t>* rootFailures = nullptr;
427 int rootBus = getRootBus(bus);
428
429 if (rootBus >= 0)
430 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800431 auto rootBusFind = busBlacklist.find(rootBus);
432 if (rootBusFind != busBlacklist.end())
433 {
434 if (rootBusFind->second != std::nullopt)
435 {
436 for (const auto& rootAddress : *(rootBusFind->second))
437 {
438 skipList.insert(rootAddress);
439 }
440 }
441 }
James Feist7972bb92019-11-13 15:59:24 -0800442 rootFailures = &(failedAddresses[rootBus]);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000443 foundItems = fruAddresses[rootBus];
James Feist7972bb92019-11-13 15:59:24 -0800444 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700445
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530446 constexpr int startSkipSlaveAddr = 0;
447 constexpr int endSkipSlaveAddr = 12;
448
James Feist26c27ad2018-07-25 15:09:40 -0700449 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800450 {
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000451 if (foundItems.find(ii) != foundItems.end())
452 {
453 continue;
454 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700455 if (skipList.find(ii) != skipList.end())
456 {
457 continue;
458 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530459 // skipping since no device is present in this range
460 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
461 {
462 continue;
463 }
James Feist26c27ad2018-07-25 15:09:40 -0700464 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530465 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800466 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300467 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700468 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700469 continue;
470 }
471 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800472 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700473 {
474 continue;
475 }
James Feist3cb5fec2018-01-23 14:41:51 -0800476
Ed Tanous07d467b2021-02-23 14:48:37 -0800477 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700478 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700479 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700480 << "\n";
481 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800482
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530483 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700484
James Feist7972bb92019-11-13 15:59:24 -0800485 if (failedItems.find(ii) != failedItems.end())
486 {
487 // if we failed to read it once, unlikely we can read it later
488 continue;
489 }
490
491 if (rootFailures != nullptr)
492 {
493 if (rootFailures->find(ii) != rootFailures->end())
494 {
495 continue;
496 }
497 }
498
Vijay Khemka2d681f62018-11-06 15:51:00 -0800499 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000500 std::optional<bool> is16Bit = isDevice16Bit(file);
501 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800502 {
503 std::cerr << "failed to read bus " << bus << " address " << ii
504 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700505 if (powerIsOn)
506 {
507 failedItems.insert(ii);
508 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800509 continue;
510 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000511 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800512
Zev Weiss309c0b12022-02-25 01:44:12 +0000513 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
514 uint8_t* outbuf) {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200515 return readData(is16BitBool, false, file, ii, offset, length,
516 outbuf);
Zev Weiss309c0b12022-02-25 01:44:12 +0000517 };
518 FRUReader reader(std::move(readFunc));
Patrick Williamsdf190612023-05-10 07:51:34 -0500519 std::string errorMessage = "bus " + std::to_string(bus) +
520 " address " + std::to_string(ii);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200521 std::pair<std::vector<uint8_t>, bool> pair =
522 readFRUContents(reader, errorMessage);
523 const bool foundHeader = pair.second;
524
525 if (!foundHeader && !is16BitBool)
526 {
527 // certain FRU eeproms require bytewise reading.
528 // otherwise garbage is read. e.g. SuperMicro PWS 920P-SQ
529
530 auto readFunc = [is16BitBool, file, ii](off_t offset,
531 size_t length,
532 uint8_t* outbuf) {
533 return readData(is16BitBool, true, file, ii, offset, length,
534 outbuf);
535 };
536 FRUReader readerBytewise(std::move(readFunc));
537 pair = readFRUContents(readerBytewise, errorMessage);
538 }
539
540 if (pair.first.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700541 {
James Feist26c27ad2018-07-25 15:09:40 -0700542 continue;
543 }
James Feist26c27ad2018-07-25 15:09:40 -0700544
Marvin Drees2b3ed302023-04-14 16:35:14 +0200545 devices->emplace(ii, pair.first);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000546 fruAddresses[bus].insert(ii);
James Feist3cb5fec2018-01-23 14:41:51 -0800547 }
James Feist26c27ad2018-07-25 15:09:40 -0700548 return 1;
549 });
550 std::future_status status =
551 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
552 if (status == std::future_status::timeout)
553 {
554 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700555 if (powerIsOn)
556 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800557 busBlacklist[bus] = std::nullopt;
James Feistcb5661d2020-06-12 15:05:01 -0700558 }
James Feist444830e2019-04-05 08:38:16 -0700559 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700560 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800561 }
562
James Feist444830e2019-04-05 08:38:16 -0700563 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700564 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800565}
566
Patrick Venture11f1ff42019-08-01 10:42:12 -0700567void loadBlacklist(const char* path)
568{
569 std::ifstream blacklistStream(path);
570 if (!blacklistStream.good())
571 {
572 // File is optional.
573 std::cerr << "Cannot open blacklist file.\n\n";
574 return;
575 }
576
Patrick Williamsdf190612023-05-10 07:51:34 -0500577 nlohmann::json data = nlohmann::json::parse(blacklistStream, nullptr,
578 false);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700579 if (data.is_discarded())
580 {
581 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
582 "exiting\n";
583 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700584 }
585
586 // It's expected to have at least one field, "buses" that is an array of the
587 // buses by integer. Allow for future options to exclude further aspects,
588 // such as specific addresses or ranges.
589 if (data.type() != nlohmann::json::value_t::object)
590 {
591 std::cerr << "Illegal blacklist, expected to read dictionary\n";
592 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700593 }
594
595 // If buses field is missing, that's fine.
596 if (data.count("buses") == 1)
597 {
598 // Parse the buses array after a little validation.
599 auto buses = data.at("buses");
600 if (buses.type() != nlohmann::json::value_t::array)
601 {
602 // Buses field present but invalid, therefore this is an error.
603 std::cerr << "Invalid contents for blacklist buses field\n";
604 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700605 }
606
607 // Catch exception here for type mis-match.
608 try
609 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800610 for (const auto& busIterator : buses)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700611 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800612 // If bus and addresses field are missing, that's fine.
613 if (busIterator.contains("bus") &&
614 busIterator.contains("addresses"))
615 {
616 auto busData = busIterator.at("bus");
617 auto bus = busData.get<size_t>();
618
619 auto addressData = busIterator.at("addresses");
620 auto addresses =
621 addressData.get<std::set<std::string_view>>();
622
623 busBlacklist[bus].emplace();
624 for (const auto& address : addresses)
625 {
626 size_t addressInt = 0;
627 std::from_chars(address.begin() + 2, address.end(),
628 addressInt, 16);
629 busBlacklist[bus]->insert(addressInt);
630 }
631 }
632 else
633 {
634 busBlacklist[busIterator.get<size_t>()] = std::nullopt;
635 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700636 }
637 }
638 catch (const nlohmann::detail::type_error& e)
639 {
640 // Type mis-match is a critical error.
641 std::cerr << "Invalid bus type: " << e.what() << "\n";
642 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700643 }
644 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700645}
646
Ed Tanous07d467b2021-02-23 14:48:37 -0800647static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800648 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530649 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800650{
Ed Tanous3013fb42022-07-09 08:27:06 -0700651 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800652 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700653 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800654
James Feist5d7f4692020-06-17 18:22:33 -0700655 if (bus < 0)
656 {
657 std::cerr << "Cannot translate " << i2cBus << " to int\n";
658 continue;
659 }
Bonnie Lobfda2c42022-10-26 16:50:54 +0800660 auto busFind = busBlacklist.find(bus);
661 if (busFind != busBlacklist.end())
James Feist444830e2019-04-05 08:38:16 -0700662 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800663 if (busFind->second == std::nullopt)
664 {
665 continue; // Skip blocked busses.
666 }
James Feist444830e2019-04-05 08:38:16 -0700667 }
James Feist0c3980a2019-12-19 11:09:27 -0800668 int rootBus = getRootBus(bus);
Bonnie Lobfda2c42022-10-26 16:50:54 +0800669 auto rootBusFind = busBlacklist.find(rootBus);
670 if (rootBusFind != busBlacklist.end())
James Feist0c3980a2019-12-19 11:09:27 -0800671 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800672 if (rootBusFind->second == std::nullopt)
673 {
674 continue;
675 }
James Feist0c3980a2019-12-19 11:09:27 -0800676 }
677
James Feist3cb5fec2018-01-23 14:41:51 -0800678 auto file = open(i2cBus.c_str(), O_RDWR);
679 if (file < 0)
680 {
681 std::cerr << "unable to open i2c device " << i2cBus.string()
682 << "\n";
683 continue;
684 }
685 unsigned long funcs = 0;
686
687 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
688 {
689 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700690 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800691 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700692 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800693 continue;
694 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700695 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
696 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800697 {
698 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
699 << bus << "\n";
700 continue;
701 }
James Feist98132792019-07-09 13:29:09 -0700702 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800703 device = std::make_shared<DeviceMap>();
704
Nikhil Potaded8884f12019-03-27 13:27:13 -0700705 // i2cdetect by default uses the range 0x03 to 0x77, as
706 // this is what we have tested with, use this range. Could be
707 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800708 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800709 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700710 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800711 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700712
713 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530714 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700715
Ed Tanous07d467b2021-02-23 14:48:37 -0800716 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800717 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700718 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800719 }
James Feist3cb5fec2018-01-23 14:41:51 -0800720 }
James Feist3cb5fec2018-01-23 14:41:51 -0800721}
722
James Feist6ebf9de2018-05-15 15:01:17 -0700723// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700724struct FindDevicesWithCallback :
725 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700726{
James Feista465ccc2019-02-08 12:51:01 -0800727 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800728 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530729 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800730 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700731 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530732 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
733 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700734 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700735 ~FindDevicesWithCallback()
736 {
737 _callback();
738 }
739 void run()
740 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530741 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700742 }
743
James Feista465ccc2019-02-08 12:51:01 -0800744 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800745 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800746 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530747 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700748 std::function<void(void)> _callback;
749};
750
Ed Tanous07d467b2021-02-23 14:48:37 -0800751void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300752 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800753 boost::container::flat_map<
754 std::pair<size_t, size_t>,
755 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530756 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800757 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530758 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800759{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300760 boost::container::flat_map<std::string, std::string> formattedFRU;
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530761
762 std::optional<std::string> optionalProductName = getProductName(
763 device, formattedFRU, bus, address, unknownBusObjectCount);
764 if (!optionalProductName)
James Feist3cb5fec2018-01-23 14:41:51 -0800765 {
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530766 std::cerr << "getProductName failed. product name is empty.\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800767 return;
768 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700769
Patrick Williamsdf190612023-05-10 07:51:34 -0500770 std::string productName = "/xyz/openbmc_project/FruDevice/" +
771 optionalProductName.value();
James Feist3cb5fec2018-01-23 14:41:51 -0800772
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530773 std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName);
774 if (index.has_value())
James Feist918e18c2018-02-13 15:51:07 -0800775 {
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530776 productName += "_";
777 productName += std::to_string(++(*index));
James Feist918e18c2018-02-13 15:51:07 -0800778 }
James Feist3cb5fec2018-01-23 14:41:51 -0800779
James Feist9eb0b582018-04-27 12:15:46 -0700780 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
781 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
782 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
783
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300784 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800785 {
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700786 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800787 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530788 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700789 {
790 continue;
791 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500792 std::string key = std::regex_replace(property.first, nonAsciiRegex,
793 "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530794
795 if (property.first == "PRODUCT_ASSET_TAG")
796 {
797 std::string propertyName = property.first;
798 iface->register_property(
799 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530800 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530801 &unknownBusObjectCount, &powerIsOn, &objServer,
802 &systemBus](const std::string& req, std::string& resp) {
Patrick Williamsdf190612023-05-10 07:51:34 -0500803 if (strcmp(req.c_str(), resp.c_str()) != 0)
804 {
805 // call the method which will update
806 if (updateFRUProperty(req, bus, address, propertyName,
807 dbusInterfaceMap,
808 unknownBusObjectCount, powerIsOn,
809 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530810 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500811 resp = req;
Joshi-Mansid73b7442020-03-27 19:10:21 +0530812 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500813 else
814 {
815 throw std::invalid_argument(
816 "FRU property update failed.");
817 }
818 }
819 return 1;
Joshi-Mansid73b7442020-03-27 19:10:21 +0530820 });
821 }
822 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700823 {
824 std::cerr << "illegal key: " << key << "\n";
825 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800826 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700827 {
828 std::cout << property.first << ": " << property.second << "\n";
829 }
James Feist3cb5fec2018-01-23 14:41:51 -0800830 }
James Feist2a9d6db2018-04-27 15:48:28 -0700831
832 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700833 iface->register_property("BUS", bus);
834 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700835
James Feist9eb0b582018-04-27 12:15:46 -0700836 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800837}
838
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300839static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800840{
841 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800842 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300843 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800844 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300845 baseboardFRUFile.seekg(0, std::ios_base::end);
846 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
847 baseboardFRU.resize(fileSize);
848 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700849 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
850 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
851 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800852 }
853 else
854 {
855 return false;
856 }
857 return true;
858}
859
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300860bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700861{
862 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800863 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700864 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300865 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700866 return false;
867 }
868 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800869 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700870 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300871 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700872 return false;
873 }
874 // baseboard fru
875 if (bus == 0 && address == 0)
876 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800877 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700878 if (!file.good())
879 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800880 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700881 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700882 return false;
883 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700884 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
885 const char* charOffset = reinterpret_cast<const char*>(fru.data());
886 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700887 return file.good();
888 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800889
890 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700891 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800892 auto path = getEepromPath(bus, address);
893 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
894 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700895 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800896 std::cerr << "unable to open i2c device " << path << "\n";
897 throw DBusInternalError();
898 return false;
899 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700900
Ed Tanous07d467b2021-02-23 14:48:37 -0800901 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
902 if (writtenBytes < 0)
903 {
904 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700905 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700906 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700907 return false;
908 }
909
Ed Tanous07d467b2021-02-23 14:48:37 -0800910 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700911 return true;
912 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800913
914 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
915
916 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
917 if (file < 0)
918 {
919 std::cerr << "unable to open i2c device " << i2cBus << "\n";
920 throw DBusInternalError();
921 return false;
922 }
923 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
924 {
925 std::cerr << "unable to set device address\n";
926 close(file);
927 throw DBusInternalError();
928 return false;
929 }
930
931 constexpr const size_t retryMax = 2;
932 uint16_t index = 0;
933 size_t retries = retryMax;
934 while (index < fru.size())
935 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700936 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800937 (retries == retryMax))
938 {
939 // The 4K EEPROM only uses the A2 and A1 device address bits
940 // with the third bit being a memory page address bit.
941 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
942 {
943 std::cerr << "unable to set device address\n";
944 close(file);
945 throw DBusInternalError();
946 return false;
947 }
948 }
949
950 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
951 fru[index]) < 0)
952 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700953 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800954 {
955 std::cerr << "error writing fru: " << strerror(errno) << "\n";
956 close(file);
957 throw DBusInternalError();
958 return false;
959 }
960 }
961 else
962 {
963 retries = retryMax;
964 index++;
965 }
966 // most eeproms require 5-10ms between writes
967 std::this_thread::sleep_for(std::chrono::milliseconds(10));
968 }
969 close(file);
970 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700971}
972
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800973void rescanOneBus(
krishnar4213ee212022-11-11 15:39:30 +0530974 BusMap& busmap, uint16_t busNum,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800975 boost::container::flat_map<
976 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700977 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800978 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530979 sdbusplus::asio::object_server& objServer,
980 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800981{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800982 for (auto& [pair, interface] : foundDevices)
983 {
984 if (pair.first == static_cast<size_t>(busNum))
985 {
986 objServer.remove_interface(interface);
987 foundDevices.erase(pair);
988 }
989 }
990
James Feist5d7f4692020-06-17 18:22:33 -0700991 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
992 if (!fs::exists(busPath))
993 {
994 if (dbusCall)
995 {
996 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
997 << "\n";
998 throw std::invalid_argument("Invalid Bus.");
999 }
1000 return;
1001 }
1002
1003 std::vector<fs::path> i2cBuses;
1004 i2cBuses.emplace_back(busPath);
1005
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001006 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301007 i2cBuses, busmap, powerIsOn, objServer,
1008 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
1009 &objServer, &systemBus]() {
Patrick Williamsdf190612023-05-10 07:51:34 -05001010 for (auto& busIface : dbusInterfaceMap)
1011 {
1012 if (busIface.first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001013 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001014 objServer.remove_interface(busIface.second);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001015 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001016 }
1017 auto found = busmap.find(busNum);
1018 if (found == busmap.end() || found->second == nullptr)
1019 {
1020 return;
1021 }
1022 for (auto& device : *(found->second))
1023 {
1024 addFruObjectToDbus(device.second, dbusInterfaceMap,
1025 static_cast<uint32_t>(busNum), device.first,
1026 unknownBusObjectCount, powerIsOn, objServer,
1027 systemBus);
1028 }
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001029 });
1030 scan->run();
1031}
1032
James Feist9eb0b582018-04-27 12:15:46 -07001033void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -07001034 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -08001035 boost::container::flat_map<
1036 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301037 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001038 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301039 sdbusplus::asio::object_server& objServer,
1040 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -08001041{
Ed Tanousaa497ed2023-02-28 13:43:41 -08001042 static boost::asio::steady_timer timer(io);
1043 timer.expires_from_now(std::chrono::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001044
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001045 // setup an async wait in case we get flooded with requests
Jian Zhang6e7cebf2022-11-29 18:30:21 +08001046 timer.async_wait([&](const boost::system::error_code& ec) {
1047 if (ec == boost::asio::error::operation_aborted)
1048 {
1049 return;
1050 }
1051
1052 if (ec)
1053 {
1054 std::cerr << "Error in timer: " << ec.message() << "\n";
1055 return;
1056 }
1057
James Feist4131aea2018-03-09 09:47:30 -08001058 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001059 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001060
Nikhil Potaded8884f12019-03-27 13:27:13 -07001061 boost::container::flat_map<size_t, fs::path> busPaths;
1062 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001063 {
James Feist4131aea2018-03-09 09:47:30 -08001064 std::cerr << "unable to find i2c devices\n";
1065 return;
James Feist918e18c2018-02-13 15:51:07 -08001066 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001067
Ed Tanous07d467b2021-02-23 14:48:37 -08001068 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001069 {
1070 i2cBuses.emplace_back(busPath.second);
1071 }
James Feist4131aea2018-03-09 09:47:30 -08001072
James Feist98132792019-07-09 13:29:09 -07001073 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001074 for (auto& [pair, interface] : foundDevices)
1075 {
1076 objServer.remove_interface(interface);
1077 }
1078 foundDevices.clear();
1079
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301080 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301081 i2cBuses, busmap, powerIsOn, objServer, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001082 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001083 {
1084 objServer.remove_interface(busIface.second);
1085 }
James Feist4131aea2018-03-09 09:47:30 -08001086
James Feist6ebf9de2018-05-15 15:01:17 -07001087 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -08001088 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001089
James Feist6ebf9de2018-05-15 15:01:17 -07001090 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001091 std::vector<uint8_t> baseboardFRU;
1092 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001093 {
Helen Huangf69fe902021-02-19 16:18:22 +08001094 // If no device on i2c bus 0, the insertion will happen.
1095 auto bus0 =
1096 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1097 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001098 }
James Feist98132792019-07-09 13:29:09 -07001099 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001100 {
James Feista465ccc2019-02-08 12:51:01 -08001101 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001102 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001103 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301104 devicemap.first, device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301105 unknownBusObjectCount, powerIsOn,
1106 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001107 }
1108 }
1109 });
1110 scan->run();
1111 });
James Feist918e18c2018-02-13 15:51:07 -08001112}
1113
Joshi-Mansid73b7442020-03-27 19:10:21 +05301114// Details with example of Asset Tag Update
1115// To find location of Product Info Area asset tag as per FRU specification
1116// 1. Find product Info area starting offset (*8 - as header will be in
1117// multiple of 8 bytes).
1118// 2. Skip 3 bytes of product info area (like format version, area length,
1119// and language code).
1120// 3. Traverse manufacturer name, product name, product version, & product
1121// serial number, by reading type/length code to reach the Asset Tag.
1122// 4. Update the Asset Tag, reposition the product Info area in multiple of
1123// 8 bytes. Update the Product area length and checksum.
1124
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001125bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301126 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001127 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301128 boost::container::flat_map<
1129 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301130 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001131 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301132 sdbusplus::asio::object_server& objServer,
1133 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301134{
1135 size_t updatePropertyReqLen = updatePropertyReq.length();
1136 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1137 {
1138 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001139 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301140 "Invalid Length "
1141 << updatePropertyReqLen << "\n";
1142 return false;
1143 }
1144
1145 std::vector<uint8_t> fruData;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301146
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301147 if (!getFruData(fruData, bus, address))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301148 {
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301149 std::cerr << "Failure getting FRU Data \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301150 return false;
1151 }
1152
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +00001153 struct FruArea fruAreaParams
1154 {};
Joshi-Mansid73b7442020-03-27 19:10:21 +05301155
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301156 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301157 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301158 std::cerr << "findFruAreaLocationAndField failed \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301159 return false;
1160 }
1161
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301162 std::vector<uint8_t> restFRUAreaFieldsData;
1163 if (!copyRestFRUArea(fruData, propertyName, fruAreaParams,
1164 restFRUAreaFieldsData))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301165 {
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301166 std::cerr << "copyRestFRUArea failed \n";
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001167 return false;
1168 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301169
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001170 // Push post update fru areas if any
1171 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001172 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1173 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001174 {
1175 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001176 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301177 if ((fruAreaLoc > fruAreaParams.restFieldsEnd) &&
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001178 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1179 {
1180 nextFRUAreaLoc = fruAreaLoc;
1181 }
1182 }
1183 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001184 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001185 {
1186 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1187 fruData.size() - nextFRUAreaLoc,
1188 std::back_inserter(restFRUAreasData));
1189 }
1190
1191 // check FRU area size
1192 size_t fruAreaDataSize =
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301193 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) +
1194 restFRUAreaFieldsData.size());
1195 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001196 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1197 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001198#ifdef ENABLE_FRU_AREA_RESIZE
1199 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1200 // round size to 8-byte blocks
Patrick Williamsdf190612023-05-10 07:51:34 -05001201 newFRUAreaSize = ((newFRUAreaSize - 1) / fruBlockSize + 1) *
1202 fruBlockSize;
1203 size_t newFRUDataSize = fruData.size() + newFRUAreaSize -
1204 fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001205 fruData.resize(newFRUDataSize);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301206 fruAreaParams.size = newFRUAreaSize;
1207 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001208#else
1209 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1210 << " should not be greater than available FRU area size: "
1211 << fruAreaAvailableSize << "\n";
1212 return false;
1213#endif // ENABLE_FRU_AREA_RESIZE
1214 }
1215
Joshi-Mansid73b7442020-03-27 19:10:21 +05301216 // write new requested property field length and data
1217 constexpr uint8_t newTypeLenMask = 0xC0;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301218 fruData[fruAreaParams.updateFieldLoc] =
Joshi-Mansid73b7442020-03-27 19:10:21 +05301219 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301220 fruAreaParams.updateFieldLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301221 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301222 fruData.begin() + fruAreaParams.updateFieldLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301223
1224 // Copy remaining data to main fru area - post updated fru field vector
Patrick Williamsdf190612023-05-10 07:51:34 -05001225 fruAreaParams.restFieldsLoc = fruAreaParams.updateFieldLoc +
1226 updatePropertyReqLen;
1227 size_t fruAreaDataEnd = fruAreaParams.restFieldsLoc +
1228 restFRUAreaFieldsData.size();
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301229
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001230 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301231 fruData.begin() + fruAreaParams.restFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301232
1233 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001234 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301235 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301236
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001237#ifdef ENABLE_FRU_AREA_RESIZE
1238 ++nextFRUAreaNewLoc;
Patrick Williamsdf190612023-05-10 07:51:34 -05001239 ssize_t nextFRUAreaOffsetDiff = (nextFRUAreaNewLoc - nextFRUAreaLoc) /
1240 fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001241 // Append rest FRU Areas if size changed and there were other sections after
1242 // updated one
1243 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1244 {
1245 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1246 fruData.begin() + nextFRUAreaNewLoc);
1247 // Update Common Header
Ed Tanous6e22c872023-02-16 15:19:28 -08001248 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1249 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001250 {
Ed Tanous6e22c872023-02-16 15:19:28 -08001251 unsigned int fruAreaOffsetField =
1252 getHeaderAreaFieldOffset(nextFRUArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001253 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
Ed Tanous6e22c872023-02-16 15:19:28 -08001254 if (curFRUAreaOffset > fruAreaParams.end)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001255 {
1256 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1257 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1258 }
1259 }
1260 // Calculate new checksum
1261 std::vector<uint8_t> headerFRUData;
1262 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1263 size_t checksumVal = calculateChecksum(headerFRUData);
1264 fruData[7] = static_cast<uint8_t>(checksumVal);
1265 // fill zeros if FRU Area size decreased
1266 if (nextFRUAreaOffsetDiff < 0)
1267 {
1268 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1269 restFRUAreasData.size(),
1270 fruData.end(), 0);
1271 }
1272 }
1273#else
1274 // this is to avoid "unused variable" warning
1275 (void)nextFRUAreaNewLoc;
1276#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301277 if (fruData.empty())
1278 {
1279 return false;
1280 }
1281
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001282 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301283 fruData))
1284 {
1285 return false;
1286 }
1287
1288 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301289 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1290 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301291 return true;
1292}
1293
James Feist98132792019-07-09 13:29:09 -07001294int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001295{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301296 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1297 sdbusplus::asio::object_server objServer(systemBus);
1298
Kumar Thangavel41a90512021-11-24 15:44:20 +05301299 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301300 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001301 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001302 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001303 std::vector<fs::path> i2cBuses;
1304
James Feista3c180a2018-08-09 16:06:04 -07001305 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001306 {
1307 std::cerr << "unable to find i2c devices\n";
1308 return 1;
1309 }
James Feist3cb5fec2018-01-23 14:41:51 -08001310
Patrick Venture11f1ff42019-08-01 10:42:12 -07001311 // check for and load blacklist with initial buses.
1312 loadBlacklist(blacklistPath);
1313
Vijay Khemka065f6d92018-12-18 10:37:47 -08001314 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001315
James Feist6ebf9de2018-05-15 15:01:17 -07001316 // this is a map with keys of pair(bus number, address) and values of
1317 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001318 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001319 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1320 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001321
James Feist9eb0b582018-04-27 12:15:46 -07001322 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1323 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1324 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001325
Kumar Thangavel41a90512021-11-24 15:44:20 +05301326 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301327 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1328 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301329 });
James Feist2a9d6db2018-04-27 15:48:28 -07001330
krishnar4213ee212022-11-11 15:39:30 +05301331 iface->register_method("ReScanBus", [&](uint16_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301332 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301333 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001334 });
1335
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001336 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001337
Patrick Williamsdf190612023-05-10 07:51:34 -05001338 iface->register_method("WriteFru",
1339 [&](const uint16_t bus, const uint8_t address,
1340 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001341 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001342 {
James Feistddb78302018-09-06 11:45:42 -07001343 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001344 return;
1345 }
1346 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301347 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1348 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001349 });
James Feist9eb0b582018-04-27 12:15:46 -07001350 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001351
Patrick Williams2af39222022-07-22 19:26:56 -05001352 std::function<void(sdbusplus::message_t & message)> eventHandler =
1353 [&](sdbusplus::message_t& message) {
Patrick Williamsdf190612023-05-10 07:51:34 -05001354 std::string objectName;
1355 boost::container::flat_map<
1356 std::string,
1357 std::variant<std::string, bool, int64_t, uint64_t, double>>
1358 values;
1359 message.read(objectName, values);
1360 auto findState = values.find("CurrentHostState");
1361 if (findState != values.end())
1362 {
1363 if (std::get<std::string>(findState->second) ==
1364 "xyz.openbmc_project.State.Host.HostState.Running")
James Feist918e18c2018-02-13 15:51:07 -08001365 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001366 powerIsOn = true;
James Feist0eeb79c2019-10-09 13:35:29 -07001367 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001368 }
James Feist6ebf9de2018-05-15 15:01:17 -07001369
Patrick Williamsdf190612023-05-10 07:51:34 -05001370 if (powerIsOn)
1371 {
1372 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1373 powerIsOn, objServer, systemBus);
1374 }
1375 };
James Feist9eb0b582018-04-27 12:15:46 -07001376
Patrick Williams2af39222022-07-22 19:26:56 -05001377 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1378 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001379 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001380 "openbmc_project/state/"
1381 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001382 eventHandler);
1383
James Feist4131aea2018-03-09 09:47:30 -08001384 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001385 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001386 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001387 // monitor for new i2c devices
1388 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1389 std::function<void(const boost::system::error_code, std::size_t)>
Patrick Williamsdf190612023-05-10 07:51:34 -05001390 watchI2cBusses =
1391 [&](const boost::system::error_code& ec,
1392 std::size_t bytesTransferred) {
1393 if (ec)
1394 {
1395 std::cout << "Callback Error " << ec << "\n";
1396 return;
1397 }
1398 size_t index = 0;
1399 while ((index + sizeof(inotify_event)) <= bytesTransferred)
1400 {
1401 const char* p = &readBuffer[index];
1402 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1403 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
1404 switch (iEvent->mask)
James Feist4131aea2018-03-09 09:47:30 -08001405 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001406 case IN_CREATE:
1407 case IN_MOVED_TO:
1408 case IN_DELETE:
1409 std::string_view name(&iEvent->name[0], iEvent->len);
1410 if (boost::starts_with(name, "i2c"))
1411 {
1412 int bus = busStrToInt(name);
1413 if (bus < 0)
James Feist9eb0b582018-04-27 12:15:46 -07001414 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001415 std::cerr << "Could not parse bus " << name << "\n";
1416 continue;
1417 }
1418 int rootBus = getRootBus(bus);
1419 if (rootBus >= 0)
1420 {
1421 rescanOneBus(busMap, static_cast<uint16_t>(rootBus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301422 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301423 unknownBusObjectCount, powerIsOn,
1424 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001425 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001426 rescanOneBus(busMap, static_cast<uint16_t>(bus),
1427 dbusInterfaceMap, false,
1428 unknownBusObjectCount, powerIsOn,
1429 objServer, systemBus);
1430 }
James Feist4131aea2018-03-09 09:47:30 -08001431 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001432 index += sizeof(inotify_event) + iEvent->len;
1433 }
James Feist6ebf9de2018-05-15 15:01:17 -07001434
Patrick Williamsdf190612023-05-10 07:51:34 -05001435 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1436 watchI2cBusses);
1437 };
James Feist4131aea2018-03-09 09:47:30 -08001438
1439 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001440 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301441 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1442 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001443
James Feist3cb5fec2018-01-23 14:41:51 -08001444 io.run();
1445 return 0;
1446}