blob: 60142d085788aef7d42bcd13fe1e1121d7f24b96 [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>
Brad Bishop82c84bb2020-08-26 14:12:50 -040026#include <boost/asio/deadline_timer.hpp>
27#include <boost/asio/io_service.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>
James Feist3f8a2782018-02-12 09:24:42 -080046#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070047#include <set>
48#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070049#include <string>
James Feist3b860982018-10-02 14:34:07 -070050#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080051#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080052#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070053#include <vector>
James Feist3b860982018-10-02 14:34:07 -070054
James Feist8c505da2020-05-28 10:06:33 -070055extern "C"
56{
James Feist3b860982018-10-02 14:34:07 -070057#include <i2c/smbus.h>
58#include <linux/i2c-dev.h>
59}
James Feist3cb5fec2018-01-23 14:41:51 -080060
Ed Tanous072e25d2018-12-16 21:45:20 -080061namespace fs = std::filesystem;
Ed Tanous07d467b2021-02-23 14:48:37 -080062static constexpr bool debug = false;
Ed Tanous07d467b2021-02-23 14:48:37 -080063constexpr size_t maxFruSize = 512;
64constexpr size_t maxEepromPageIndex = 255;
James Feist26c27ad2018-07-25 15:09:40 -070065constexpr size_t busTimeoutSeconds = 5;
James Feist3cb5fec2018-01-23 14:41:51 -080066
Patrick Venture11f1ff42019-08-01 10:42:12 -070067constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
68
Ed Tanous07d467b2021-02-23 14:48:37 -080069const static constexpr char* baseboardFruLocation =
James Feist3cb5fec2018-01-23 14:41:51 -080070 "/etc/fru/baseboard.fru.bin";
71
Ed Tanous07d467b2021-02-23 14:48:37 -080072const static constexpr char* i2CDevLocation = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080073
James Feist444830e2019-04-05 08:38:16 -070074static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070075struct FindDevicesWithCallback;
76
James Feist8a983922019-10-24 17:11:56 -070077static boost::container::flat_map<
78 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
79 foundDevices;
80
James Feist7972bb92019-11-13 15:59:24 -080081static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
82
James Feist5cb06082019-10-24 17:11:13 -070083boost::asio::io_service io;
James Feist5cb06082019-10-24 17:11:13 -070084
Andrei Kartashev2f0de172020-08-13 14:29:40 +030085bool updateFRUProperty(
Ed Tanous3013fb42022-07-09 08:27:06 -070086 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -080087 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +053088 boost::container::flat_map<
89 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +053090 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -080091 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +053092 sdbusplus::asio::object_server& objServer,
93 std::shared_ptr<sdbusplus::asio::connection>& systemBus);
Patrick Venturebaba7672019-10-26 09:26:41 -070094
Patrick Venturec50e1ff2019-08-06 10:22:28 -070095// Given a bus/address, produce the path in sysfs for an eeprom.
96static std::string getEepromPath(size_t bus, size_t address)
97{
98 std::stringstream output;
99 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
100 << std::setfill('0') << std::setw(4) << std::hex << address
101 << "/eeprom";
102 return output.str();
103}
104
105static bool hasEepromFile(size_t bus, size_t address)
106{
107 auto path = getEepromPath(bus, address);
108 try
109 {
110 return fs::exists(path);
111 }
112 catch (...)
113 {
114 return false;
115 }
116}
117
Zev Weiss309c0b12022-02-25 01:44:12 +0000118static int64_t readFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700119{
120 auto result = lseek(fd, offset, SEEK_SET);
121 if (result < 0)
122 {
123 std::cerr << "failed to seek\n";
124 return -1;
125 }
126
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700127 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700128}
129
Ed Tanous3013fb42022-07-09 08:27:06 -0700130static int busStrToInt(const std::string_view busName)
James Feist5d7f4692020-06-17 18:22:33 -0700131{
Ed Tanous07d467b2021-02-23 14:48:37 -0800132 auto findBus = busName.rfind('-');
James Feist5d7f4692020-06-17 18:22:33 -0700133 if (findBus == std::string::npos)
134 {
135 return -1;
136 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700137 std::string_view num = busName.substr(findBus + 1);
138 int val = 0;
139 std::from_chars(num.data(), num.data() + num.size(), val);
140 return val;
James Feist5d7f4692020-06-17 18:22:33 -0700141}
142
James Feist7972bb92019-11-13 15:59:24 -0800143static int getRootBus(size_t bus)
144{
145 auto ec = std::error_code();
146 auto path = std::filesystem::read_symlink(
147 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
148 std::to_string(bus) + "/mux_device"),
149 ec);
150 if (ec)
151 {
152 return -1;
153 }
154
155 std::string filename = path.filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800156 auto findBus = filename.find('-');
James Feist7972bb92019-11-13 15:59:24 -0800157 if (findBus == std::string::npos)
158 {
159 return -1;
160 }
161 return std::stoi(filename.substr(0, findBus));
162}
163
James Feistc95cb142018-02-26 10:41:42 -0800164static bool isMuxBus(size_t bus)
165{
Ed Tanous072e25d2018-12-16 21:45:20 -0800166 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800167 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
168}
169
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530170static void makeProbeInterface(size_t bus, size_t address,
171 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700172{
173 if (isMuxBus(bus))
174 {
175 return; // the mux buses are random, no need to publish
176 }
177 auto [it, success] = foundDevices.emplace(
178 std::make_pair(bus, address),
179 objServer.add_interface(
180 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
181 std::to_string(address),
182 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
183 if (!success)
184 {
185 return; // already added
186 }
187 it->second->register_property("Bus", bus);
188 it->second->register_property("Address", address);
189 it->second->initialize();
190}
191
Zev Weiss309c0b12022-02-25 01:44:12 +0000192static std::optional<bool> isDevice16Bit(int file)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800193{
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700194 // Set the higher data word address bits to 0. It's safe on 8-bit addressing
195 // EEPROMs because it doesn't write any actual data.
196 int ret = i2c_smbus_write_byte(file, 0);
197 if (ret < 0)
198 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000199 return std::nullopt;
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700200 }
201
Vijay Khemka2d681f62018-11-06 15:51:00 -0800202 /* Get first byte */
203 int byte1 = i2c_smbus_read_byte_data(file, 0);
204 if (byte1 < 0)
205 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000206 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800207 }
208 /* Read 7 more bytes, it will read same first byte in case of
209 * 8 bit but it will read next byte in case of 16 bit
210 */
211 for (int i = 0; i < 7; i++)
212 {
213 int byte2 = i2c_smbus_read_byte_data(file, 0);
214 if (byte2 < 0)
215 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000216 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800217 }
218 if (byte2 != byte1)
219 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000220 return true;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800221 }
222 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000223 return false;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800224}
225
Xiang Liu2801a702020-01-20 14:29:34 -0800226// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
227// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800228static int i2cSmbusWriteThenRead(int file, uint16_t address,
229 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
230 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800231{
Ed Tanous07d467b2021-02-23 14:48:37 -0800232 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
233 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800234 {
235 return -1;
236 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800237
Ed Tanous3013fb42022-07-09 08:27:06 -0700238 constexpr size_t smbusWriteThenReadMsgCount = 2;
239 std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{};
240 struct i2c_rdwr_ioctl_data rdwr
241 {};
Xiang Liu2801a702020-01-20 14:29:34 -0800242
243 msgs[0].addr = address;
244 msgs[0].flags = 0;
245 msgs[0].len = toSlaveBufLen;
246 msgs[0].buf = toSlaveBuf;
247 msgs[1].addr = address;
248 msgs[1].flags = I2C_M_RD;
249 msgs[1].len = fromSlaveBufLen;
250 msgs[1].buf = fromSlaveBuf;
251
Ed Tanous3013fb42022-07-09 08:27:06 -0700252 rdwr.msgs = msgs.data();
253 rdwr.nmsgs = msgs.size();
Xiang Liu2801a702020-01-20 14:29:34 -0800254
255 int ret = ioctl(file, I2C_RDWR, &rdwr);
256
Jason M. Billsf6e4c1f2022-08-19 12:23:51 -0700257 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800258}
259
Zev Weiss309c0b12022-02-25 01:44:12 +0000260static int64_t readBlockData(bool is16bit, int file, uint16_t address,
261 off_t offset, size_t len, uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800262{
Zev Weiss309c0b12022-02-25 01:44:12 +0000263 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800264 {
Xiang Liu2801a702020-01-20 14:29:34 -0800265 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
266 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800267 }
268
Xiang Liu2801a702020-01-20 14:29:34 -0800269 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700270 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
271 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
272 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800273}
274
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700275// TODO: This code is very similar to the non-eeprom version and can be merged
276// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300277static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700278{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700279 auto path = getEepromPath(bus, address);
280
281 int file = open(path.c_str(), O_RDONLY);
282 if (file < 0)
283 {
284 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700285 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700286 }
287
Patrick Venturebaba7672019-10-26 09:26:41 -0700288 std::string errorMessage = "eeprom at " + std::to_string(bus) +
289 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000290 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
291 return readFromEeprom(file, offset, length, outbuf);
292 };
293 FRUReader reader(std::move(readFunc));
294 std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700295
296 close(file);
297 return device;
298}
299
Ed Tanous07d467b2021-02-23 14:48:37 -0800300std::set<int> findI2CEeproms(int i2cBus,
301 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700302{
303 std::set<int> foundList;
304
305 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
306
307 // For each file listed under the i2c device
308 // NOTE: This should be faster than just checking for each possible address
309 // path.
310 for (const auto& p : fs::directory_iterator(path))
311 {
312 const std::string node = p.path().string();
313 std::smatch m;
314 bool found =
315 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
316
317 if (!found)
318 {
319 continue;
320 }
321 if (m.size() != 2)
322 {
323 std::cerr << "regex didn't capture\n";
324 continue;
325 }
326
327 std::ssub_match subMatch = m[1];
328 std::string addressString = subMatch.str();
329
Ed Tanous3013fb42022-07-09 08:27:06 -0700330 std::size_t ignored = 0;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700331 const int hexBase = 16;
332 int address = std::stoi(addressString, &ignored, hexBase);
333
334 const std::string eeprom = node + "/eeprom";
335
336 try
337 {
338 if (!fs::exists(eeprom))
339 {
340 continue;
341 }
342 }
343 catch (...)
344 {
345 continue;
346 }
347
348 // There is an eeprom file at this address, it may have invalid
349 // contents, but we found it.
350 foundList.insert(address);
351
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300352 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700353 if (!device.empty())
354 {
355 devices->emplace(address, device);
356 }
357 }
358
359 return foundList;
360}
361
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300362int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800363 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530364 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800365{
James Feist3cb5fec2018-01-23 14:41:51 -0800366
James Feist26c27ad2018-07-25 15:09:40 -0700367 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700368 // NOTE: When reading the devices raw on the bus, it can interfere with
369 // the driver's ability to operate, therefore read eeproms first before
370 // scanning for devices without drivers. Several experiments were run
371 // and it was determined that if there were any devices on the bus
372 // before the eeprom was hit and read, the eeprom driver wouldn't open
373 // while the bus device was open. An experiment was not performed to see
374 // if this issue was resolved if the i2c bus device was closed, but
375 // hexdumps of the eeprom later were successful.
376
377 // Scan for i2c eeproms loaded on this bus.
378 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800379 std::set<size_t>& failedItems = failedAddresses[bus];
380
381 std::set<size_t>* rootFailures = nullptr;
382 int rootBus = getRootBus(bus);
383
384 if (rootBus >= 0)
385 {
386 rootFailures = &(failedAddresses[rootBus]);
387 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700388
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530389 constexpr int startSkipSlaveAddr = 0;
390 constexpr int endSkipSlaveAddr = 12;
391
James Feist26c27ad2018-07-25 15:09:40 -0700392 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800393 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700394 if (skipList.find(ii) != skipList.end())
395 {
396 continue;
397 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530398 // skipping since no device is present in this range
399 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
400 {
401 continue;
402 }
James Feist26c27ad2018-07-25 15:09:40 -0700403 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530404 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800405 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300406 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700407 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700408 continue;
409 }
410 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800411 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700412 {
413 continue;
414 }
James Feist3cb5fec2018-01-23 14:41:51 -0800415
Ed Tanous07d467b2021-02-23 14:48:37 -0800416 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700417 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700418 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700419 << "\n";
420 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800421
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530422 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700423
James Feist7972bb92019-11-13 15:59:24 -0800424 if (failedItems.find(ii) != failedItems.end())
425 {
426 // if we failed to read it once, unlikely we can read it later
427 continue;
428 }
429
430 if (rootFailures != nullptr)
431 {
432 if (rootFailures->find(ii) != rootFailures->end())
433 {
434 continue;
435 }
436 }
437
Vijay Khemka2d681f62018-11-06 15:51:00 -0800438 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000439 std::optional<bool> is16Bit = isDevice16Bit(file);
440 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800441 {
442 std::cerr << "failed to read bus " << bus << " address " << ii
443 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700444 if (powerIsOn)
445 {
446 failedItems.insert(ii);
447 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800448 continue;
449 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000450 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800451
Zev Weiss309c0b12022-02-25 01:44:12 +0000452 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
453 uint8_t* outbuf) {
454 return readBlockData(is16BitBool, file, ii, offset, length,
455 outbuf);
456 };
457 FRUReader reader(std::move(readFunc));
Patrick Venturebaba7672019-10-26 09:26:41 -0700458 std::string errorMessage =
459 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Zev Weiss309c0b12022-02-25 01:44:12 +0000460 std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700461 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700462 {
James Feist26c27ad2018-07-25 15:09:40 -0700463 continue;
464 }
James Feist26c27ad2018-07-25 15:09:40 -0700465
Patrick Venture786f1792019-08-05 16:33:44 -0700466 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800467 }
James Feist26c27ad2018-07-25 15:09:40 -0700468 return 1;
469 });
470 std::future_status status =
471 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
472 if (status == std::future_status::timeout)
473 {
474 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700475 if (powerIsOn)
476 {
477 busBlacklist.insert(bus);
478 }
James Feist444830e2019-04-05 08:38:16 -0700479 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700480 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800481 }
482
James Feist444830e2019-04-05 08:38:16 -0700483 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700484 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800485}
486
Patrick Venture11f1ff42019-08-01 10:42:12 -0700487void loadBlacklist(const char* path)
488{
489 std::ifstream blacklistStream(path);
490 if (!blacklistStream.good())
491 {
492 // File is optional.
493 std::cerr << "Cannot open blacklist file.\n\n";
494 return;
495 }
496
497 nlohmann::json data =
498 nlohmann::json::parse(blacklistStream, nullptr, false);
499 if (data.is_discarded())
500 {
501 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
502 "exiting\n";
503 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700504 }
505
506 // It's expected to have at least one field, "buses" that is an array of the
507 // buses by integer. Allow for future options to exclude further aspects,
508 // such as specific addresses or ranges.
509 if (data.type() != nlohmann::json::value_t::object)
510 {
511 std::cerr << "Illegal blacklist, expected to read dictionary\n";
512 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700513 }
514
515 // If buses field is missing, that's fine.
516 if (data.count("buses") == 1)
517 {
518 // Parse the buses array after a little validation.
519 auto buses = data.at("buses");
520 if (buses.type() != nlohmann::json::value_t::array)
521 {
522 // Buses field present but invalid, therefore this is an error.
523 std::cerr << "Invalid contents for blacklist buses field\n";
524 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700525 }
526
527 // Catch exception here for type mis-match.
528 try
529 {
530 for (const auto& bus : buses)
531 {
532 busBlacklist.insert(bus.get<size_t>());
533 }
534 }
535 catch (const nlohmann::detail::type_error& e)
536 {
537 // Type mis-match is a critical error.
538 std::cerr << "Invalid bus type: " << e.what() << "\n";
539 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700540 }
541 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700542}
543
Ed Tanous07d467b2021-02-23 14:48:37 -0800544static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800545 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530546 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800547{
Ed Tanous3013fb42022-07-09 08:27:06 -0700548 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800549 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700550 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800551
James Feist5d7f4692020-06-17 18:22:33 -0700552 if (bus < 0)
553 {
554 std::cerr << "Cannot translate " << i2cBus << " to int\n";
555 continue;
556 }
James Feist444830e2019-04-05 08:38:16 -0700557 if (busBlacklist.find(bus) != busBlacklist.end())
558 {
559 continue; // skip previously failed busses
560 }
James Feist3cb5fec2018-01-23 14:41:51 -0800561
James Feist0c3980a2019-12-19 11:09:27 -0800562 int rootBus = getRootBus(bus);
563 if (busBlacklist.find(rootBus) != busBlacklist.end())
564 {
565 continue;
566 }
567
James Feist3cb5fec2018-01-23 14:41:51 -0800568 auto file = open(i2cBus.c_str(), O_RDWR);
569 if (file < 0)
570 {
571 std::cerr << "unable to open i2c device " << i2cBus.string()
572 << "\n";
573 continue;
574 }
575 unsigned long funcs = 0;
576
577 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
578 {
579 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700580 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800581 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700582 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800583 continue;
584 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700585 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
586 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800587 {
588 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
589 << bus << "\n";
590 continue;
591 }
James Feist98132792019-07-09 13:29:09 -0700592 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800593 device = std::make_shared<DeviceMap>();
594
Nikhil Potaded8884f12019-03-27 13:27:13 -0700595 // i2cdetect by default uses the range 0x03 to 0x77, as
596 // this is what we have tested with, use this range. Could be
597 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800598 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800599 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700600 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800601 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700602
603 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530604 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700605
Ed Tanous07d467b2021-02-23 14:48:37 -0800606 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800607 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700608 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800609 }
James Feist3cb5fec2018-01-23 14:41:51 -0800610 }
James Feist3cb5fec2018-01-23 14:41:51 -0800611}
612
James Feist6ebf9de2018-05-15 15:01:17 -0700613// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700614struct FindDevicesWithCallback :
615 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700616{
James Feista465ccc2019-02-08 12:51:01 -0800617 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800618 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530619 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800620 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700621 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530622 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
623 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700624 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700625 ~FindDevicesWithCallback()
626 {
627 _callback();
628 }
629 void run()
630 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530631 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700632 }
633
James Feista465ccc2019-02-08 12:51:01 -0800634 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800635 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800636 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530637 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700638 std::function<void(void)> _callback;
639};
640
Ed Tanous07d467b2021-02-23 14:48:37 -0800641void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300642 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800643 boost::container::flat_map<
644 std::pair<size_t, size_t>,
645 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530646 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800647 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530648 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800649{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300650 boost::container::flat_map<std::string, std::string> formattedFRU;
Michael Shen0961b112022-02-22 11:06:33 +0800651 resCodes res = formatIPMIFRU(device, formattedFRU);
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300652 if (res == resCodes::resErr)
James Feist3cb5fec2018-01-23 14:41:51 -0800653 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300654 std::cerr << "failed to parse FRU for device at bus " << bus
Patrick Ventureb755c832019-08-07 11:09:14 -0700655 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800656 return;
657 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800658 if (res == resCodes::resWarn)
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300659 {
660 std::cerr << "there were warnings while parsing FRU for device at bus "
661 << bus << " address " << address << "\n";
662 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700663
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300664 auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800665 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700666 // Not found under Board section or an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300667 if (productNameFind == formattedFRU.end() ||
Patrick Venture96cdaef2019-07-30 13:30:52 -0700668 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800669 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300670 productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800671 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700672 // Found under Product section and not an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300673 if (productNameFind != formattedFRU.end() &&
Patrick Venture96cdaef2019-07-30 13:30:52 -0700674 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800675 {
676 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800677 std::regex illegalObject("[^A-Za-z0-9_]");
678 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800679 }
680 else
681 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800682 productName = "UNKNOWN" + std::to_string(unknownBusObjectCount);
683 unknownBusObjectCount++;
James Feist3cb5fec2018-01-23 14:41:51 -0800684 }
685
James Feist918e18c2018-02-13 15:51:07 -0800686 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800687 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700688 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800689 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700690 int highest = -1;
691 bool found = false;
692
James Feista465ccc2019-02-08 12:51:01 -0800693 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800694 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700695 std::string path = busIface.second->get_object_path();
696 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
James Feist918e18c2018-02-13 15:51:07 -0800697 {
Jiaqing Zhao8482ffc2022-05-20 16:06:54 +0800698 if (isMuxBus(bus) && bus != busIface.first.first &&
699 address == busIface.first.second &&
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300700 (getFRUInfo(static_cast<uint8_t>(busIface.first.first),
James Feist98132792019-07-09 13:29:09 -0700701 static_cast<uint8_t>(busIface.first.second)) ==
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300702 getFRUInfo(static_cast<uint8_t>(bus),
James Feist98132792019-07-09 13:29:09 -0700703 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700704 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700705 // This device is already added to the lower numbered bus,
706 // do not replicate it.
707 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700708 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700709
710 // Check if the match named has extra information.
711 found = true;
Ed Tanous07d467b2021-02-23 14:48:37 -0800712 std::smatch baseMatch;
Patrick Venture015fb0a2019-08-16 09:33:31 -0700713
714 bool match = std::regex_match(
Ed Tanous07d467b2021-02-23 14:48:37 -0800715 path, baseMatch, std::regex(productName + "_(\\d+)$"));
Patrick Venture015fb0a2019-08-16 09:33:31 -0700716 if (match)
James Feist79e9c0b2018-03-15 15:21:17 -0700717 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800718 if (baseMatch.size() == 2)
Patrick Venture015fb0a2019-08-16 09:33:31 -0700719 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800720 std::ssub_match baseSubMatch = baseMatch[1];
721 std::string base = baseSubMatch.str();
Patrick Venture015fb0a2019-08-16 09:33:31 -0700722
723 int value = std::stoi(base);
724 highest = (value > highest) ? value : highest;
725 }
James Feist79e9c0b2018-03-15 15:21:17 -0700726 }
James Feist918e18c2018-02-13 15:51:07 -0800727 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700728 } // end searching objects
729
730 if (found)
731 {
732 // We found something with the same name. If highest was still -1,
733 // it means this new entry will be _0.
734 productName += "_";
735 productName += std::to_string(++highest);
James Feist918e18c2018-02-13 15:51:07 -0800736 }
737 }
James Feist3cb5fec2018-01-23 14:41:51 -0800738
James Feist9eb0b582018-04-27 12:15:46 -0700739 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
740 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
741 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
742
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300743 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800744 {
James Feist9eb0b582018-04-27 12:15:46 -0700745
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700746 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800747 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530748 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700749 {
750 continue;
751 }
752 std::string key =
Ed Tanous07d467b2021-02-23 14:48:37 -0800753 std::regex_replace(property.first, nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530754
755 if (property.first == "PRODUCT_ASSET_TAG")
756 {
757 std::string propertyName = property.first;
758 iface->register_property(
759 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530760 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530761 &unknownBusObjectCount, &powerIsOn, &objServer,
762 &systemBus](const std::string& req, std::string& resp) {
Ed Tanous07d467b2021-02-23 14:48:37 -0800763 if (strcmp(req.c_str(), resp.c_str()) != 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +0530764 {
765 // call the method which will update
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300766 if (updateFRUProperty(req, bus, address, propertyName,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530767 dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530768 unknownBusObjectCount, powerIsOn,
769 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530770 {
771 resp = req;
772 }
773 else
774 {
775 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300776 "FRU property update failed.");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530777 }
778 }
779 return 1;
780 });
781 }
782 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700783 {
784 std::cerr << "illegal key: " << key << "\n";
785 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800786 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700787 {
788 std::cout << property.first << ": " << property.second << "\n";
789 }
James Feist3cb5fec2018-01-23 14:41:51 -0800790 }
James Feist2a9d6db2018-04-27 15:48:28 -0700791
792 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700793 iface->register_property("BUS", bus);
794 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700795
James Feist9eb0b582018-04-27 12:15:46 -0700796 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800797}
798
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300799static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800800{
801 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800802 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300803 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800804 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300805 baseboardFRUFile.seekg(0, std::ios_base::end);
806 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
807 baseboardFRU.resize(fileSize);
808 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700809 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
810 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
811 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800812 }
813 else
814 {
815 return false;
816 }
817 return true;
818}
819
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300820bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700821{
822 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800823 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700824 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300825 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700826 return false;
827 }
828 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800829 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700830 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300831 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700832 return false;
833 }
834 // baseboard fru
835 if (bus == 0 && address == 0)
836 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800837 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700838 if (!file.good())
839 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800840 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700841 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700842 return false;
843 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700844 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
845 const char* charOffset = reinterpret_cast<const char*>(fru.data());
846 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700847 return file.good();
848 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800849
850 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700851 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800852 auto path = getEepromPath(bus, address);
853 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
854 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700855 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800856 std::cerr << "unable to open i2c device " << path << "\n";
857 throw DBusInternalError();
858 return false;
859 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700860
Ed Tanous07d467b2021-02-23 14:48:37 -0800861 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
862 if (writtenBytes < 0)
863 {
864 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700865 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700866 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700867 return false;
868 }
869
Ed Tanous07d467b2021-02-23 14:48:37 -0800870 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700871 return true;
872 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800873
874 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
875
876 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
877 if (file < 0)
878 {
879 std::cerr << "unable to open i2c device " << i2cBus << "\n";
880 throw DBusInternalError();
881 return false;
882 }
883 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
884 {
885 std::cerr << "unable to set device address\n";
886 close(file);
887 throw DBusInternalError();
888 return false;
889 }
890
891 constexpr const size_t retryMax = 2;
892 uint16_t index = 0;
893 size_t retries = retryMax;
894 while (index < fru.size())
895 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700896 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800897 (retries == retryMax))
898 {
899 // The 4K EEPROM only uses the A2 and A1 device address bits
900 // with the third bit being a memory page address bit.
901 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
902 {
903 std::cerr << "unable to set device address\n";
904 close(file);
905 throw DBusInternalError();
906 return false;
907 }
908 }
909
910 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
911 fru[index]) < 0)
912 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700913 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800914 {
915 std::cerr << "error writing fru: " << strerror(errno) << "\n";
916 close(file);
917 throw DBusInternalError();
918 return false;
919 }
920 }
921 else
922 {
923 retries = retryMax;
924 index++;
925 }
926 // most eeproms require 5-10ms between writes
927 std::this_thread::sleep_for(std::chrono::milliseconds(10));
928 }
929 close(file);
930 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700931}
932
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800933void rescanOneBus(
934 BusMap& busmap, uint8_t busNum,
935 boost::container::flat_map<
936 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700937 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800938 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530939 sdbusplus::asio::object_server& objServer,
940 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800941{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800942 for (auto& [pair, interface] : foundDevices)
943 {
944 if (pair.first == static_cast<size_t>(busNum))
945 {
946 objServer.remove_interface(interface);
947 foundDevices.erase(pair);
948 }
949 }
950
James Feist5d7f4692020-06-17 18:22:33 -0700951 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
952 if (!fs::exists(busPath))
953 {
954 if (dbusCall)
955 {
956 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
957 << "\n";
958 throw std::invalid_argument("Invalid Bus.");
959 }
960 return;
961 }
962
963 std::vector<fs::path> i2cBuses;
964 i2cBuses.emplace_back(busPath);
965
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800966 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530967 i2cBuses, busmap, powerIsOn, objServer,
968 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
969 &objServer, &systemBus]() {
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800970 for (auto& busIface : dbusInterfaceMap)
971 {
972 if (busIface.first.first == static_cast<size_t>(busNum))
973 {
974 objServer.remove_interface(busIface.second);
975 }
976 }
Wludzik, Jozefc1136b72020-06-24 11:58:32 +0200977 auto found = busmap.find(busNum);
978 if (found == busmap.end() || found->second == nullptr)
979 {
980 return;
981 }
982 for (auto& device : *(found->second))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800983 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800984 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530985 static_cast<uint32_t>(busNum), device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530986 unknownBusObjectCount, powerIsOn, objServer,
987 systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800988 }
989 });
990 scan->run();
991}
992
James Feist9eb0b582018-04-27 12:15:46 -0700993void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700994 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800995 boost::container::flat_map<
996 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530997 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800998 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530999 sdbusplus::asio::object_server& objServer,
1000 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -08001001{
James Feist6ebf9de2018-05-15 15:01:17 -07001002 static boost::asio::deadline_timer timer(io);
1003 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001004
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001005 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -07001006 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -08001007 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001008 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001009
Nikhil Potaded8884f12019-03-27 13:27:13 -07001010 boost::container::flat_map<size_t, fs::path> busPaths;
1011 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001012 {
James Feist4131aea2018-03-09 09:47:30 -08001013 std::cerr << "unable to find i2c devices\n";
1014 return;
James Feist918e18c2018-02-13 15:51:07 -08001015 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001016
Ed Tanous07d467b2021-02-23 14:48:37 -08001017 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001018 {
1019 i2cBuses.emplace_back(busPath.second);
1020 }
James Feist4131aea2018-03-09 09:47:30 -08001021
James Feist98132792019-07-09 13:29:09 -07001022 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001023 for (auto& [pair, interface] : foundDevices)
1024 {
1025 objServer.remove_interface(interface);
1026 }
1027 foundDevices.clear();
1028
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301029 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301030 i2cBuses, busmap, powerIsOn, objServer, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001031 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001032 {
1033 objServer.remove_interface(busIface.second);
1034 }
James Feist4131aea2018-03-09 09:47:30 -08001035
James Feist6ebf9de2018-05-15 15:01:17 -07001036 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -08001037 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001038
James Feist6ebf9de2018-05-15 15:01:17 -07001039 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001040 std::vector<uint8_t> baseboardFRU;
1041 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001042 {
Helen Huangf69fe902021-02-19 16:18:22 +08001043 // If no device on i2c bus 0, the insertion will happen.
1044 auto bus0 =
1045 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1046 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001047 }
James Feist98132792019-07-09 13:29:09 -07001048 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001049 {
James Feista465ccc2019-02-08 12:51:01 -08001050 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001051 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001052 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301053 devicemap.first, device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301054 unknownBusObjectCount, powerIsOn,
1055 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001056 }
1057 }
1058 });
1059 scan->run();
1060 });
James Feist918e18c2018-02-13 15:51:07 -08001061}
1062
Joshi-Mansid73b7442020-03-27 19:10:21 +05301063// Details with example of Asset Tag Update
1064// To find location of Product Info Area asset tag as per FRU specification
1065// 1. Find product Info area starting offset (*8 - as header will be in
1066// multiple of 8 bytes).
1067// 2. Skip 3 bytes of product info area (like format version, area length,
1068// and language code).
1069// 3. Traverse manufacturer name, product name, product version, & product
1070// serial number, by reading type/length code to reach the Asset Tag.
1071// 4. Update the Asset Tag, reposition the product Info area in multiple of
1072// 8 bytes. Update the Product area length and checksum.
1073
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001074bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301075 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001076 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301077 boost::container::flat_map<
1078 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301079 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001080 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301081 sdbusplus::asio::object_server& objServer,
1082 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301083{
1084 size_t updatePropertyReqLen = updatePropertyReq.length();
1085 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1086 {
1087 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001088 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301089 "Invalid Length "
1090 << updatePropertyReqLen << "\n";
1091 return false;
1092 }
1093
1094 std::vector<uint8_t> fruData;
1095 try
1096 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001097 fruData = getFRUInfo(static_cast<uint8_t>(bus),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301098 static_cast<uint8_t>(address));
1099 }
Patrick Williams83b1e9b2021-10-06 12:47:24 -05001100 catch (const std::invalid_argument& e)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301101 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001102 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301103 return false;
1104 }
1105
1106 if (fruData.empty())
1107 {
1108 return false;
1109 }
1110
Ed Tanous3013fb42022-07-09 08:27:06 -07001111 const std::vector<std::string>* fruAreaFieldNames = nullptr;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301112
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001113 uint8_t fruAreaOffsetFieldValue = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301114 size_t offset = 0;
Ed Tanous07d467b2021-02-23 14:48:37 -08001115 std::string areaName = propertyName.substr(0, propertyName.find('_'));
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001116 std::string propertyNamePrefix = areaName + "_";
Ed Tanous07d467b2021-02-23 14:48:37 -08001117 auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName);
1118 if (it == fruAreaNames.end())
Joshi-Mansid73b7442020-03-27 19:10:21 +05301119 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001120 std::cerr << "Can't parse area name for property " << propertyName
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001121 << " \n";
1122 return false;
1123 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001124 fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin());
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001125 fruAreaOffsetFieldValue =
1126 fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)];
1127 switch (fruAreaToUpdate)
1128 {
1129 case fruAreas::fruAreaChassis:
1130 offset = 3; // chassis part number offset. Skip fixed first 3 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001131 fruAreaFieldNames = &chassisFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001132 break;
1133 case fruAreas::fruAreaBoard:
1134 offset = 6; // board manufacturer offset. Skip fixed first 6 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001135 fruAreaFieldNames = &boardFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001136 break;
1137 case fruAreas::fruAreaProduct:
1138 // Manufacturer name offset. Skip fixed first 3 product fru bytes
1139 // i.e. version, area length and language code
1140 offset = 3;
Ed Tanous07d467b2021-02-23 14:48:37 -08001141 fruAreaFieldNames = &productFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001142 break;
1143 default:
1144 std::cerr << "Don't know how to handle property " << propertyName
1145 << " \n";
1146 return false;
1147 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001148 if (fruAreaOffsetFieldValue == 0)
1149 {
1150 std::cerr << "FRU Area for " << propertyName << " not present \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301151 return false;
1152 }
1153
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001154 size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001155 size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize;
1156 size_t fruAreaEnd = fruAreaStart + fruAreaSize;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001157 size_t fruDataIter = fruAreaStart + offset;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001158 size_t skipToFRUUpdateField = 0;
Ed Tanous3013fb42022-07-09 08:27:06 -07001159 ssize_t fieldLength = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301160
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001161 bool found = false;
Ed Tanous3013fb42022-07-09 08:27:06 -07001162 for (const auto& field : *fruAreaFieldNames)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301163 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001164 skipToFRUUpdateField++;
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001165 if (propertyName == propertyNamePrefix + field)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301166 {
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001167 found = true;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301168 break;
1169 }
1170 }
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001171 if (!found)
1172 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001173 std::size_t pos = propertyName.find(fruCustomFieldName);
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001174 if (pos == std::string::npos)
1175 {
1176 std::cerr << "PropertyName doesn't exist in FRU Area Vectors: "
1177 << propertyName << "\n";
1178 return false;
1179 }
1180 std::string fieldNumStr =
Ed Tanous07d467b2021-02-23 14:48:37 -08001181 propertyName.substr(pos + fruCustomFieldName.length());
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001182 size_t fieldNum = std::stoi(fieldNumStr);
1183 if (fieldNum == 0)
1184 {
1185 std::cerr << "PropertyName not recognized: " << propertyName
1186 << "\n";
1187 return false;
1188 }
1189 skipToFRUUpdateField += fieldNum;
1190 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301191
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001192 for (size_t i = 1; i < skipToFRUUpdateField; i++)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301193 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001194 fieldLength = getFieldLength(fruData[fruDataIter]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001195 if (fieldLength < 0)
1196 {
1197 break;
1198 }
1199 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301200 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001201 size_t fruUpdateFieldLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301202
1203 // Push post update fru field bytes to a vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001204 fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001205 if (fieldLength < 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301206 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001207 std::cerr << "Property " << propertyName << " not present \n";
1208 return false;
1209 }
1210 fruDataIter += 1 + fieldLength;
1211 size_t restFRUFieldsLoc = fruDataIter;
1212 size_t endOfFieldsLoc = 0;
1213 while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0)
1214 {
1215 if (fruDataIter >= (fruAreaStart + fruAreaSize))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301216 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001217 fruDataIter = fruAreaStart + fruAreaSize;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301218 break;
1219 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001220 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301221 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001222 endOfFieldsLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301223
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001224 std::vector<uint8_t> restFRUAreaFieldsData;
1225 std::copy_n(fruData.begin() + restFRUFieldsLoc,
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001226 endOfFieldsLoc - restFRUFieldsLoc + 1,
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001227 std::back_inserter(restFRUAreaFieldsData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301228
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001229 // Push post update fru areas if any
1230 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001231 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1232 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001233 {
1234 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001235 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001236 if ((fruAreaLoc > endOfFieldsLoc) &&
1237 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1238 {
1239 nextFRUAreaLoc = fruAreaLoc;
1240 }
1241 }
1242 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001243 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001244 {
1245 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1246 fruData.size() - nextFRUAreaLoc,
1247 std::back_inserter(restFRUAreasData));
1248 }
1249
1250 // check FRU area size
1251 size_t fruAreaDataSize =
1252 ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size());
1253 size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize;
1254 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1255 {
1256
1257#ifdef ENABLE_FRU_AREA_RESIZE
1258 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1259 // round size to 8-byte blocks
1260 newFRUAreaSize =
1261 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
1262 size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize;
1263 fruData.resize(newFRUDataSize);
1264 fruAreaSize = newFRUAreaSize;
1265 fruAreaEnd = fruAreaStart + fruAreaSize;
1266#else
1267 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1268 << " should not be greater than available FRU area size: "
1269 << fruAreaAvailableSize << "\n";
1270 return false;
1271#endif // ENABLE_FRU_AREA_RESIZE
1272 }
1273
Joshi-Mansid73b7442020-03-27 19:10:21 +05301274 // write new requested property field length and data
1275 constexpr uint8_t newTypeLenMask = 0xC0;
1276 fruData[fruUpdateFieldLoc] =
1277 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
1278 fruUpdateFieldLoc++;
1279 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
1280 fruData.begin() + fruUpdateFieldLoc);
1281
1282 // Copy remaining data to main fru area - post updated fru field vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001283 restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen;
1284 size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size();
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001285 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
1286 fruData.begin() + restFRUFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301287
1288 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001289 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
1290 fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301291
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001292#ifdef ENABLE_FRU_AREA_RESIZE
1293 ++nextFRUAreaNewLoc;
1294 ssize_t nextFRUAreaOffsetDiff =
1295 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
1296 // Append rest FRU Areas if size changed and there were other sections after
1297 // updated one
1298 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1299 {
1300 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1301 fruData.begin() + nextFRUAreaNewLoc);
1302 // Update Common Header
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001303 for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord;
1304 fruArea++)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001305 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001306 unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001307 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
1308 if (curFRUAreaOffset > fruAreaOffsetFieldValue)
1309 {
1310 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1311 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1312 }
1313 }
1314 // Calculate new checksum
1315 std::vector<uint8_t> headerFRUData;
1316 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1317 size_t checksumVal = calculateChecksum(headerFRUData);
1318 fruData[7] = static_cast<uint8_t>(checksumVal);
1319 // fill zeros if FRU Area size decreased
1320 if (nextFRUAreaOffsetDiff < 0)
1321 {
1322 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1323 restFRUAreasData.size(),
1324 fruData.end(), 0);
1325 }
1326 }
1327#else
1328 // this is to avoid "unused variable" warning
1329 (void)nextFRUAreaNewLoc;
1330#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301331 if (fruData.empty())
1332 {
1333 return false;
1334 }
1335
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001336 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301337 fruData))
1338 {
1339 return false;
1340 }
1341
1342 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301343 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1344 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301345 return true;
1346}
1347
James Feist98132792019-07-09 13:29:09 -07001348int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001349{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301350 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1351 sdbusplus::asio::object_server objServer(systemBus);
1352
Kumar Thangavel41a90512021-11-24 15:44:20 +05301353 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301354 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001355 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001356 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001357 std::vector<fs::path> i2cBuses;
1358
James Feista3c180a2018-08-09 16:06:04 -07001359 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001360 {
1361 std::cerr << "unable to find i2c devices\n";
1362 return 1;
1363 }
James Feist3cb5fec2018-01-23 14:41:51 -08001364
Patrick Venture11f1ff42019-08-01 10:42:12 -07001365 // check for and load blacklist with initial buses.
1366 loadBlacklist(blacklistPath);
1367
Vijay Khemka065f6d92018-12-18 10:37:47 -08001368 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001369
James Feist6ebf9de2018-05-15 15:01:17 -07001370 // this is a map with keys of pair(bus number, address) and values of
1371 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001372 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001373 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1374 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001375
James Feist9eb0b582018-04-27 12:15:46 -07001376 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1377 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1378 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001379
Kumar Thangavel41a90512021-11-24 15:44:20 +05301380 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301381 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1382 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301383 });
James Feist2a9d6db2018-04-27 15:48:28 -07001384
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001385 iface->register_method("ReScanBus", [&](uint8_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301386 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301387 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001388 });
1389
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001390 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001391
1392 iface->register_method("WriteFru", [&](const uint8_t bus,
1393 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001394 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001395 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001396 {
James Feistddb78302018-09-06 11:45:42 -07001397 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001398 return;
1399 }
1400 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301401 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1402 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001403 });
James Feist9eb0b582018-04-27 12:15:46 -07001404 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001405
Patrick Williams2af39222022-07-22 19:26:56 -05001406 std::function<void(sdbusplus::message_t & message)> eventHandler =
1407 [&](sdbusplus::message_t& message) {
James Feist918e18c2018-02-13 15:51:07 -08001408 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001409 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001410 std::string,
1411 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001412 values;
1413 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001414 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001415 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001416 {
James Feistcb5661d2020-06-12 15:05:01 -07001417 powerIsOn = boost::ends_with(
1418 std::get<std::string>(findState->second), "Running");
James Feist0eeb79c2019-10-09 13:35:29 -07001419 }
James Feist6ebf9de2018-05-15 15:01:17 -07001420
James Feistcb5661d2020-06-12 15:05:01 -07001421 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001422 {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301423 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301424 powerIsOn, objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001425 }
James Feist918e18c2018-02-13 15:51:07 -08001426 };
James Feist9eb0b582018-04-27 12:15:46 -07001427
Patrick Williams2af39222022-07-22 19:26:56 -05001428 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1429 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001430 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001431 "openbmc_project/state/"
1432 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001433 eventHandler);
1434
James Feist4131aea2018-03-09 09:47:30 -08001435 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001436 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001437 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001438 // monitor for new i2c devices
1439 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1440 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001441 watchI2cBusses = [&](const boost::system::error_code& ec,
Ed Tanous07d467b2021-02-23 14:48:37 -08001442 std::size_t bytesTransferred) {
James Feist4131aea2018-03-09 09:47:30 -08001443 if (ec)
1444 {
1445 std::cout << "Callback Error " << ec << "\n";
1446 return;
1447 }
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001448 size_t index = 0;
1449 while ((index + sizeof(inotify_event)) <= bytesTransferred)
James Feist4131aea2018-03-09 09:47:30 -08001450 {
Ed Tanous3013fb42022-07-09 08:27:06 -07001451 const char* p = &readBuffer[index];
1452 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1453 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
James Feist4131aea2018-03-09 09:47:30 -08001454 switch (iEvent->mask)
1455 {
James Feist9eb0b582018-04-27 12:15:46 -07001456 case IN_CREATE:
1457 case IN_MOVED_TO:
1458 case IN_DELETE:
Ed Tanous3013fb42022-07-09 08:27:06 -07001459 std::string_view name(&iEvent->name[0], iEvent->len);
James Feist5d7f4692020-06-17 18:22:33 -07001460 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001461 {
James Feist5d7f4692020-06-17 18:22:33 -07001462 int bus = busStrToInt(name);
1463 if (bus < 0)
1464 {
1465 std::cerr << "Could not parse bus " << name
1466 << "\n";
1467 continue;
1468 }
1469 rescanOneBus(busMap, static_cast<uint8_t>(bus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301470 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301471 unknownBusObjectCount, powerIsOn,
1472 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001473 }
James Feist4131aea2018-03-09 09:47:30 -08001474 }
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001475 index += sizeof(inotify_event) + iEvent->len;
James Feist4131aea2018-03-09 09:47:30 -08001476 }
James Feist6ebf9de2018-05-15 15:01:17 -07001477
James Feist4131aea2018-03-09 09:47:30 -08001478 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1479 watchI2cBusses);
1480 };
1481
1482 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001483 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301484 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1485 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001486
James Feist3cb5fec2018-01-23 14:41:51 -08001487 io.run();
1488 return 0;
1489}