blob: 138568c1c711530a297eda33ddf51b31c00923db [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Brad Bishope45d8c72022-05-25 15:12:53 -040016/// \file fru_device.cpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
Brad Bishope45d8c72022-05-25 15:12:53 -040018#include "fru_utils.hpp"
19#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070020
James Feist3cb5fec2018-01-23 14:41:51 -080021#include <fcntl.h>
James Feist3b860982018-10-02 14:34:07 -070022#include <sys/inotify.h>
23#include <sys/ioctl.h>
24
James Feist3b860982018-10-02 14:34:07 -070025#include <boost/algorithm/string/predicate.hpp>
Ed Tanous34c3e012023-03-06 13:42:00 -080026#include <boost/asio/io_context.hpp>
Ed Tanousaa497ed2023-02-28 13:43:41 -080027#include <boost/asio/steady_timer.hpp>
James Feist3b860982018-10-02 14:34:07 -070028#include <boost/container/flat_map.hpp>
James Feist8c505da2020-05-28 10:06:33 -070029#include <nlohmann/json.hpp>
30#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
33#include <array>
Ed Tanous07d467b2021-02-23 14:48:37 -080034#include <cerrno>
Ed Tanous3013fb42022-07-09 08:27:06 -070035#include <charconv>
James Feist3b860982018-10-02 14:34:07 -070036#include <chrono>
37#include <ctime>
Patrick Venturee3754002019-08-06 09:39:12 -070038#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080039#include <fstream>
Patrick Venturebaba7672019-10-26 09:26:41 -070040#include <functional>
James Feist3cb5fec2018-01-23 14:41:51 -080041#include <future>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070042#include <iomanip>
James Feist3cb5fec2018-01-23 14:41:51 -080043#include <iostream>
Patrick Venturee26395d2019-10-29 14:05:16 -070044#include <limits>
Andrew Jefferya9c58922021-06-01 09:28:59 +093045#include <map>
Bonnie Lobfda2c42022-10-26 16:50:54 +080046#include <optional>
James Feist3f8a2782018-02-12 09:24:42 -080047#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070048#include <set>
49#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070050#include <string>
James Feist3b860982018-10-02 14:34:07 -070051#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080052#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080053#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070054#include <vector>
James Feist3b860982018-10-02 14:34:07 -070055
James Feist8c505da2020-05-28 10:06:33 -070056extern "C"
57{
James Feist3b860982018-10-02 14:34:07 -070058#include <i2c/smbus.h>
59#include <linux/i2c-dev.h>
60}
James Feist3cb5fec2018-01-23 14:41:51 -080061
Ed Tanous072e25d2018-12-16 21:45:20 -080062namespace fs = std::filesystem;
Ed Tanous07d467b2021-02-23 14:48:37 -080063static constexpr bool debug = false;
Ed Tanous07d467b2021-02-23 14:48:37 -080064constexpr size_t maxFruSize = 512;
65constexpr size_t maxEepromPageIndex = 255;
ankita prasad7329a222023-07-07 09:18:10 +000066constexpr size_t busTimeoutSeconds = 10;
James Feist3cb5fec2018-01-23 14:41:51 -080067
Matt Simmering2447c242023-11-09 14:18:39 -080068constexpr const char* blocklistPath = PACKAGE_DIR "blacklist.json";
Patrick Venture11f1ff42019-08-01 10:42:12 -070069
Ed Tanous07d467b2021-02-23 14:48:37 -080070const static constexpr char* baseboardFruLocation =
James Feist3cb5fec2018-01-23 14:41:51 -080071 "/etc/fru/baseboard.fru.bin";
72
Ed Tanous07d467b2021-02-23 14:48:37 -080073const static constexpr char* i2CDevLocation = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080074
Bonnie Lobfda2c42022-10-26 16:50:54 +080075static boost::container::flat_map<size_t, std::optional<std::set<size_t>>>
Matt Simmering2447c242023-11-09 14:18:39 -080076 busBlocklist;
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
Matt Simmering2447c242023-11-09 14:18:39 -0800233// Issue an I2C transaction to first write to_target_buf_len bytes,then read
234// from_target_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800235static int i2cSmbusWriteThenRead(int file, uint16_t address,
Matt Simmering2447c242023-11-09 14:18:39 -0800236 uint8_t* toTargetBuf, uint8_t toTargetBufLen,
237 uint8_t* fromTargetBuf,
238 uint8_t fromTargetBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800239{
Matt Simmering2447c242023-11-09 14:18:39 -0800240 if (toTargetBuf == nullptr || toTargetBufLen == 0 ||
241 fromTargetBuf == nullptr || fromTargetBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800242 {
243 return -1;
244 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800245
Ed Tanous3013fb42022-07-09 08:27:06 -0700246 constexpr size_t smbusWriteThenReadMsgCount = 2;
247 std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{};
248 struct i2c_rdwr_ioctl_data rdwr
249 {};
Xiang Liu2801a702020-01-20 14:29:34 -0800250
251 msgs[0].addr = address;
252 msgs[0].flags = 0;
Matt Simmering2447c242023-11-09 14:18:39 -0800253 msgs[0].len = toTargetBufLen;
254 msgs[0].buf = toTargetBuf;
Xiang Liu2801a702020-01-20 14:29:34 -0800255 msgs[1].addr = address;
256 msgs[1].flags = I2C_M_RD;
Matt Simmering2447c242023-11-09 14:18:39 -0800257 msgs[1].len = fromTargetBufLen;
258 msgs[1].buf = fromTargetBuf;
Xiang Liu2801a702020-01-20 14:29:34 -0800259
Ed Tanous3013fb42022-07-09 08:27:06 -0700260 rdwr.msgs = msgs.data();
261 rdwr.nmsgs = msgs.size();
Xiang Liu2801a702020-01-20 14:29:34 -0800262
263 int ret = ioctl(file, I2C_RDWR, &rdwr);
264
Jason M. Billsf6e4c1f2022-08-19 12:23:51 -0700265 return (ret == static_cast<int>(msgs.size())) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800266}
267
Marvin Drees2b3ed302023-04-14 16:35:14 +0200268static int64_t readData(bool is16bit, bool isBytewise, int file,
269 uint16_t address, off_t offset, size_t len,
270 uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800271{
Zev Weiss309c0b12022-02-25 01:44:12 +0000272 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800273 {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200274 if (!isBytewise)
275 {
276 return i2c_smbus_read_i2c_block_data(
277 file, static_cast<uint8_t>(offset), len, buf);
278 }
279
280 std::span<uint8_t> bufspan{buf, len};
281 for (size_t i = 0; i < len; i++)
282 {
283 int byte = i2c_smbus_read_byte_data(
284 file, static_cast<uint8_t>(offset + i));
285 if (byte < 0)
286 {
287 return static_cast<int64_t>(byte);
288 }
289 bufspan[i] = static_cast<uint8_t>(byte);
290 }
291 return static_cast<int64_t>(len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800292 }
293
Xiang Liu2801a702020-01-20 14:29:34 -0800294 offset = htobe16(offset);
Ed Tanous3013fb42022-07-09 08:27:06 -0700295 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
296 uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
297 return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800298}
299
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700300// TODO: This code is very similar to the non-eeprom version and can be merged
301// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300302static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700303{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700304 auto path = getEepromPath(bus, address);
305
306 int file = open(path.c_str(), O_RDONLY);
307 if (file < 0)
308 {
309 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700310 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700311 }
312
Patrick Venturebaba7672019-10-26 09:26:41 -0700313 std::string errorMessage = "eeprom at " + std::to_string(bus) +
314 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000315 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
316 return readFromEeprom(file, offset, length, outbuf);
317 };
318 FRUReader reader(std::move(readFunc));
Marvin Drees2b3ed302023-04-14 16:35:14 +0200319 std::pair<std::vector<uint8_t>, bool> pair = readFRUContents(reader,
320 errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700321
322 close(file);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200323 return pair.first;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700324}
325
Bonnie Lobfda2c42022-10-26 16:50:54 +0800326std::set<size_t> findI2CEeproms(int i2cBus,
327 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700328{
Bonnie Lobfda2c42022-10-26 16:50:54 +0800329 std::set<size_t> foundList;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700330
331 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
332
333 // For each file listed under the i2c device
334 // NOTE: This should be faster than just checking for each possible address
335 // path.
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700336 auto ec = std::error_code();
337 for (const auto& p : fs::directory_iterator(path, ec))
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700338 {
Zhikui Ren8af5d3c2023-08-09 20:35:35 -0700339 if (ec)
340 {
341 std::cerr << "directory_iterator err " << ec.message() << "\n";
342 break;
343 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700344 const std::string node = p.path().string();
345 std::smatch m;
Patrick Williamsdf190612023-05-10 07:51:34 -0500346 bool found = std::regex_match(node, m,
347 std::regex(".+\\d+-([0-9abcdef]+$)"));
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700348
349 if (!found)
350 {
351 continue;
352 }
353 if (m.size() != 2)
354 {
355 std::cerr << "regex didn't capture\n";
356 continue;
357 }
358
359 std::ssub_match subMatch = m[1];
360 std::string addressString = subMatch.str();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800361 std::string_view addressStringView(addressString);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700362
Bonnie Lobfda2c42022-10-26 16:50:54 +0800363 size_t address = 0;
364 std::from_chars(addressStringView.begin(), addressStringView.end(),
365 address, 16);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700366
367 const std::string eeprom = node + "/eeprom";
368
369 try
370 {
371 if (!fs::exists(eeprom))
372 {
373 continue;
374 }
375 }
376 catch (...)
377 {
378 continue;
379 }
380
381 // There is an eeprom file at this address, it may have invalid
382 // contents, but we found it.
383 foundList.insert(address);
384
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300385 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700386 if (!device.empty())
387 {
388 devices->emplace(address, device);
389 }
390 }
391
392 return foundList;
393}
394
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300395int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800396 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530397 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800398{
James Feist26c27ad2018-07-25 15:09:40 -0700399 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700400 // NOTE: When reading the devices raw on the bus, it can interfere with
401 // the driver's ability to operate, therefore read eeproms first before
402 // scanning for devices without drivers. Several experiments were run
403 // and it was determined that if there were any devices on the bus
404 // before the eeprom was hit and read, the eeprom driver wouldn't open
405 // while the bus device was open. An experiment was not performed to see
406 // if this issue was resolved if the i2c bus device was closed, but
407 // hexdumps of the eeprom later were successful.
408
409 // Scan for i2c eeproms loaded on this bus.
Bonnie Lobfda2c42022-10-26 16:50:54 +0800410 std::set<size_t> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800411 std::set<size_t>& failedItems = failedAddresses[bus];
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000412 std::set<size_t>& foundItems = fruAddresses[bus];
413 foundItems.clear();
James Feist7972bb92019-11-13 15:59:24 -0800414
Matt Simmering2447c242023-11-09 14:18:39 -0800415 auto busFind = busBlocklist.find(bus);
416 if (busFind != busBlocklist.end())
Bonnie Lobfda2c42022-10-26 16:50:54 +0800417 {
418 if (busFind->second != std::nullopt)
419 {
420 for (const auto& address : *(busFind->second))
421 {
422 skipList.insert(address);
423 }
424 }
425 }
426
James Feist7972bb92019-11-13 15:59:24 -0800427 std::set<size_t>* rootFailures = nullptr;
428 int rootBus = getRootBus(bus);
429
430 if (rootBus >= 0)
431 {
Matt Simmering2447c242023-11-09 14:18:39 -0800432 auto rootBusFind = busBlocklist.find(rootBus);
433 if (rootBusFind != busBlocklist.end())
Bonnie Lobfda2c42022-10-26 16:50:54 +0800434 {
435 if (rootBusFind->second != std::nullopt)
436 {
437 for (const auto& rootAddress : *(rootBusFind->second))
438 {
439 skipList.insert(rootAddress);
440 }
441 }
442 }
James Feist7972bb92019-11-13 15:59:24 -0800443 rootFailures = &(failedAddresses[rootBus]);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000444 foundItems = fruAddresses[rootBus];
James Feist7972bb92019-11-13 15:59:24 -0800445 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700446
Matt Simmering2447c242023-11-09 14:18:39 -0800447 constexpr int startSkipTargetAddr = 0;
448 constexpr int endSkipTargetAddr = 12;
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530449
James Feist26c27ad2018-07-25 15:09:40 -0700450 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800451 {
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000452 if (foundItems.find(ii) != foundItems.end())
453 {
454 continue;
455 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700456 if (skipList.find(ii) != skipList.end())
457 {
458 continue;
459 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530460 // skipping since no device is present in this range
Matt Simmering2447c242023-11-09 14:18:39 -0800461 if (ii >= startSkipTargetAddr && ii <= endSkipTargetAddr)
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530462 {
463 continue;
464 }
Matt Simmering2447c242023-11-09 14:18:39 -0800465 // Set target address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530466 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800467 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300468 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700469 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700470 continue;
471 }
472 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800473 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700474 {
475 continue;
476 }
James Feist3cb5fec2018-01-23 14:41:51 -0800477
Ed Tanous07d467b2021-02-23 14:48:37 -0800478 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700479 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700480 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700481 << "\n";
482 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800483
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530484 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700485
James Feist7972bb92019-11-13 15:59:24 -0800486 if (failedItems.find(ii) != failedItems.end())
487 {
488 // if we failed to read it once, unlikely we can read it later
489 continue;
490 }
491
492 if (rootFailures != nullptr)
493 {
494 if (rootFailures->find(ii) != rootFailures->end())
495 {
496 continue;
497 }
498 }
499
Vijay Khemka2d681f62018-11-06 15:51:00 -0800500 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000501 std::optional<bool> is16Bit = isDevice16Bit(file);
502 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800503 {
504 std::cerr << "failed to read bus " << bus << " address " << ii
505 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700506 if (powerIsOn)
507 {
508 failedItems.insert(ii);
509 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800510 continue;
511 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000512 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800513
Zev Weiss309c0b12022-02-25 01:44:12 +0000514 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
515 uint8_t* outbuf) {
Marvin Drees2b3ed302023-04-14 16:35:14 +0200516 return readData(is16BitBool, false, file, ii, offset, length,
517 outbuf);
Zev Weiss309c0b12022-02-25 01:44:12 +0000518 };
519 FRUReader reader(std::move(readFunc));
Patrick Williamsdf190612023-05-10 07:51:34 -0500520 std::string errorMessage = "bus " + std::to_string(bus) +
521 " address " + std::to_string(ii);
Marvin Drees2b3ed302023-04-14 16:35:14 +0200522 std::pair<std::vector<uint8_t>, bool> pair =
523 readFRUContents(reader, errorMessage);
524 const bool foundHeader = pair.second;
525
526 if (!foundHeader && !is16BitBool)
527 {
528 // certain FRU eeproms require bytewise reading.
529 // otherwise garbage is read. e.g. SuperMicro PWS 920P-SQ
530
531 auto readFunc = [is16BitBool, file, ii](off_t offset,
532 size_t length,
533 uint8_t* outbuf) {
534 return readData(is16BitBool, true, file, ii, offset, length,
535 outbuf);
536 };
537 FRUReader readerBytewise(std::move(readFunc));
538 pair = readFRUContents(readerBytewise, errorMessage);
539 }
540
541 if (pair.first.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700542 {
James Feist26c27ad2018-07-25 15:09:40 -0700543 continue;
544 }
James Feist26c27ad2018-07-25 15:09:40 -0700545
Marvin Drees2b3ed302023-04-14 16:35:14 +0200546 devices->emplace(ii, pair.first);
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +0000547 fruAddresses[bus].insert(ii);
James Feist3cb5fec2018-01-23 14:41:51 -0800548 }
James Feist26c27ad2018-07-25 15:09:40 -0700549 return 1;
550 });
551 std::future_status status =
552 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
553 if (status == std::future_status::timeout)
554 {
555 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700556 if (powerIsOn)
557 {
Matt Simmering2447c242023-11-09 14:18:39 -0800558 busBlocklist[bus] = std::nullopt;
James Feistcb5661d2020-06-12 15:05:01 -0700559 }
James Feist444830e2019-04-05 08:38:16 -0700560 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700561 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800562 }
563
James Feist444830e2019-04-05 08:38:16 -0700564 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700565 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800566}
567
Matt Simmering2447c242023-11-09 14:18:39 -0800568void loadBlocklist(const char* path)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700569{
Matt Simmering2447c242023-11-09 14:18:39 -0800570 std::ifstream blocklistStream(path);
571 if (!blocklistStream.good())
Patrick Venture11f1ff42019-08-01 10:42:12 -0700572 {
573 // File is optional.
Matt Simmering2447c242023-11-09 14:18:39 -0800574 std::cerr << "Cannot open blocklist file.\n\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700575 return;
576 }
577
Matt Simmering2447c242023-11-09 14:18:39 -0800578 nlohmann::json data = nlohmann::json::parse(blocklistStream, nullptr,
Patrick Williamsdf190612023-05-10 07:51:34 -0500579 false);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700580 if (data.is_discarded())
581 {
Matt Simmering2447c242023-11-09 14:18:39 -0800582 std::cerr << "Illegal blocklist file detected, cannot validate JSON, "
Patrick Venture11f1ff42019-08-01 10:42:12 -0700583 "exiting\n";
584 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700585 }
586
587 // It's expected to have at least one field, "buses" that is an array of the
588 // buses by integer. Allow for future options to exclude further aspects,
589 // such as specific addresses or ranges.
590 if (data.type() != nlohmann::json::value_t::object)
591 {
Matt Simmering2447c242023-11-09 14:18:39 -0800592 std::cerr << "Illegal blocklist, expected to read dictionary\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700593 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700594 }
595
596 // If buses field is missing, that's fine.
597 if (data.count("buses") == 1)
598 {
599 // Parse the buses array after a little validation.
600 auto buses = data.at("buses");
601 if (buses.type() != nlohmann::json::value_t::array)
602 {
603 // Buses field present but invalid, therefore this is an error.
Matt Simmering2447c242023-11-09 14:18:39 -0800604 std::cerr << "Invalid contents for blocklist buses field\n";
Patrick Venture11f1ff42019-08-01 10:42:12 -0700605 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700606 }
607
608 // Catch exception here for type mis-match.
609 try
610 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800611 for (const auto& busIterator : buses)
Patrick Venture11f1ff42019-08-01 10:42:12 -0700612 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800613 // If bus and addresses field are missing, that's fine.
614 if (busIterator.contains("bus") &&
615 busIterator.contains("addresses"))
616 {
617 auto busData = busIterator.at("bus");
618 auto bus = busData.get<size_t>();
619
620 auto addressData = busIterator.at("addresses");
621 auto addresses =
622 addressData.get<std::set<std::string_view>>();
623
Matt Simmering2447c242023-11-09 14:18:39 -0800624 busBlocklist[bus].emplace();
Bonnie Lobfda2c42022-10-26 16:50:54 +0800625 for (const auto& address : addresses)
626 {
627 size_t addressInt = 0;
628 std::from_chars(address.begin() + 2, address.end(),
629 addressInt, 16);
Matt Simmering2447c242023-11-09 14:18:39 -0800630 busBlocklist[bus]->insert(addressInt);
Bonnie Lobfda2c42022-10-26 16:50:54 +0800631 }
632 }
633 else
634 {
Matt Simmering2447c242023-11-09 14:18:39 -0800635 busBlocklist[busIterator.get<size_t>()] = std::nullopt;
Bonnie Lobfda2c42022-10-26 16:50:54 +0800636 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700637 }
638 }
639 catch (const nlohmann::detail::type_error& e)
640 {
641 // Type mis-match is a critical error.
642 std::cerr << "Invalid bus type: " << e.what() << "\n";
643 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700644 }
645 }
Patrick Venture11f1ff42019-08-01 10:42:12 -0700646}
647
Ed Tanous07d467b2021-02-23 14:48:37 -0800648static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800649 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530650 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800651{
Ed Tanous3013fb42022-07-09 08:27:06 -0700652 for (const auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800653 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700654 int bus = busStrToInt(i2cBus.string());
James Feist0c3980a2019-12-19 11:09:27 -0800655
James Feist5d7f4692020-06-17 18:22:33 -0700656 if (bus < 0)
657 {
658 std::cerr << "Cannot translate " << i2cBus << " to int\n";
659 continue;
660 }
Matt Simmering2447c242023-11-09 14:18:39 -0800661 auto busFind = busBlocklist.find(bus);
662 if (busFind != busBlocklist.end())
James Feist444830e2019-04-05 08:38:16 -0700663 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800664 if (busFind->second == std::nullopt)
665 {
666 continue; // Skip blocked busses.
667 }
James Feist444830e2019-04-05 08:38:16 -0700668 }
James Feist0c3980a2019-12-19 11:09:27 -0800669 int rootBus = getRootBus(bus);
Matt Simmering2447c242023-11-09 14:18:39 -0800670 auto rootBusFind = busBlocklist.find(rootBus);
671 if (rootBusFind != busBlocklist.end())
James Feist0c3980a2019-12-19 11:09:27 -0800672 {
Bonnie Lobfda2c42022-10-26 16:50:54 +0800673 if (rootBusFind->second == std::nullopt)
674 {
675 continue;
676 }
James Feist0c3980a2019-12-19 11:09:27 -0800677 }
678
James Feist3cb5fec2018-01-23 14:41:51 -0800679 auto file = open(i2cBus.c_str(), O_RDWR);
680 if (file < 0)
681 {
682 std::cerr << "unable to open i2c device " << i2cBus.string()
683 << "\n";
684 continue;
685 }
686 unsigned long funcs = 0;
687
688 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
689 {
690 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700691 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800692 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700693 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800694 continue;
695 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700696 if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
697 ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
James Feist3cb5fec2018-01-23 14:41:51 -0800698 {
699 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
700 << bus << "\n";
701 continue;
702 }
James Feist98132792019-07-09 13:29:09 -0700703 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800704 device = std::make_shared<DeviceMap>();
705
Nikhil Potaded8884f12019-03-27 13:27:13 -0700706 // i2cdetect by default uses the range 0x03 to 0x77, as
707 // this is what we have tested with, use this range. Could be
708 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800709 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800710 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700711 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800712 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700713
714 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530715 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700716
Ed Tanous07d467b2021-02-23 14:48:37 -0800717 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800718 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700719 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800720 }
James Feist3cb5fec2018-01-23 14:41:51 -0800721 }
James Feist3cb5fec2018-01-23 14:41:51 -0800722}
723
James Feist6ebf9de2018-05-15 15:01:17 -0700724// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700725struct FindDevicesWithCallback :
726 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700727{
James Feista465ccc2019-02-08 12:51:01 -0800728 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800729 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530730 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800731 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700732 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530733 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
734 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700735 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700736 ~FindDevicesWithCallback()
737 {
738 _callback();
739 }
740 void run()
741 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530742 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700743 }
744
James Feista465ccc2019-02-08 12:51:01 -0800745 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800746 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800747 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530748 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700749 std::function<void(void)> _callback;
750};
751
Ed Tanous07d467b2021-02-23 14:48:37 -0800752void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300753 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800754 boost::container::flat_map<
755 std::pair<size_t, size_t>,
756 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530757 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800758 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530759 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800760{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300761 boost::container::flat_map<std::string, std::string> formattedFRU;
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530762
763 std::optional<std::string> optionalProductName = getProductName(
764 device, formattedFRU, bus, address, unknownBusObjectCount);
765 if (!optionalProductName)
James Feist3cb5fec2018-01-23 14:41:51 -0800766 {
Kumar Thangavel9f2162a2022-08-10 18:05:20 +0530767 std::cerr << "getProductName failed. product name is empty.\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800768 return;
769 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700770
Patrick Williamsdf190612023-05-10 07:51:34 -0500771 std::string productName = "/xyz/openbmc_project/FruDevice/" +
772 optionalProductName.value();
James Feist3cb5fec2018-01-23 14:41:51 -0800773
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530774 std::optional<int> index = findIndexForFRU(dbusInterfaceMap, productName);
775 if (index.has_value())
James Feist918e18c2018-02-13 15:51:07 -0800776 {
Kumar Thangaveld79d0252022-08-24 14:26:01 +0530777 productName += "_";
778 productName += std::to_string(++(*index));
James Feist918e18c2018-02-13 15:51:07 -0800779 }
James Feist3cb5fec2018-01-23 14:41:51 -0800780
James Feist9eb0b582018-04-27 12:15:46 -0700781 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
782 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
783 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
784
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300785 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800786 {
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700787 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800788 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530789 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700790 {
791 continue;
792 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500793 std::string key = std::regex_replace(property.first, nonAsciiRegex,
794 "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530795
796 if (property.first == "PRODUCT_ASSET_TAG")
797 {
798 std::string propertyName = property.first;
799 iface->register_property(
800 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530801 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530802 &unknownBusObjectCount, &powerIsOn, &objServer,
803 &systemBus](const std::string& req, std::string& resp) {
Patrick Williamsdf190612023-05-10 07:51:34 -0500804 if (strcmp(req.c_str(), resp.c_str()) != 0)
805 {
806 // call the method which will update
807 if (updateFRUProperty(req, bus, address, propertyName,
808 dbusInterfaceMap,
809 unknownBusObjectCount, powerIsOn,
810 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530811 {
Patrick Williamsdf190612023-05-10 07:51:34 -0500812 resp = req;
Joshi-Mansid73b7442020-03-27 19:10:21 +0530813 }
Patrick Williamsdf190612023-05-10 07:51:34 -0500814 else
815 {
816 throw std::invalid_argument(
817 "FRU property update failed.");
818 }
819 }
820 return 1;
Patrick Williamsb9dd7f82023-10-20 11:20:11 -0500821 });
Joshi-Mansid73b7442020-03-27 19:10:21 +0530822 }
823 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700824 {
825 std::cerr << "illegal key: " << key << "\n";
826 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800827 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700828 {
829 std::cout << property.first << ": " << property.second << "\n";
830 }
James Feist3cb5fec2018-01-23 14:41:51 -0800831 }
James Feist2a9d6db2018-04-27 15:48:28 -0700832
833 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700834 iface->register_property("BUS", bus);
835 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700836
James Feist9eb0b582018-04-27 12:15:46 -0700837 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800838}
839
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300840static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800841{
842 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800843 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300844 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800845 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300846 baseboardFRUFile.seekg(0, std::ios_base::end);
847 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
848 baseboardFRU.resize(fileSize);
849 baseboardFRUFile.seekg(0, std::ios_base::beg);
Ed Tanous3013fb42022-07-09 08:27:06 -0700850 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
851 char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
852 baseboardFRUFile.read(charOffset, fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800853 }
854 else
855 {
856 return false;
857 }
858 return true;
859}
860
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300861bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700862{
863 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800864 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700865 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300866 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700867 return false;
868 }
869 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800870 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700871 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300872 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700873 return false;
874 }
875 // baseboard fru
876 if (bus == 0 && address == 0)
877 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800878 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700879 if (!file.good())
880 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800881 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700882 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700883 return false;
884 }
Ed Tanous3013fb42022-07-09 08:27:06 -0700885 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
886 const char* charOffset = reinterpret_cast<const char*>(fru.data());
887 file.write(charOffset, fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700888 return file.good();
889 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800890
891 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700892 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800893 auto path = getEepromPath(bus, address);
894 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
895 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700896 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800897 std::cerr << "unable to open i2c device " << path << "\n";
898 throw DBusInternalError();
899 return false;
900 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700901
Ed Tanous07d467b2021-02-23 14:48:37 -0800902 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
903 if (writtenBytes < 0)
904 {
905 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700906 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700907 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700908 return false;
909 }
910
Ed Tanous07d467b2021-02-23 14:48:37 -0800911 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700912 return true;
913 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800914
915 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
916
917 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
918 if (file < 0)
919 {
920 std::cerr << "unable to open i2c device " << i2cBus << "\n";
921 throw DBusInternalError();
922 return false;
923 }
924 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
925 {
926 std::cerr << "unable to set device address\n";
927 close(file);
928 throw DBusInternalError();
929 return false;
930 }
931
932 constexpr const size_t retryMax = 2;
933 uint16_t index = 0;
934 size_t retries = retryMax;
935 while (index < fru.size())
936 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700937 if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
Ed Tanous07d467b2021-02-23 14:48:37 -0800938 (retries == retryMax))
939 {
940 // The 4K EEPROM only uses the A2 and A1 device address bits
941 // with the third bit being a memory page address bit.
942 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
943 {
944 std::cerr << "unable to set device address\n";
945 close(file);
946 throw DBusInternalError();
947 return false;
948 }
949 }
950
951 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
952 fru[index]) < 0)
953 {
Ed Tanous3013fb42022-07-09 08:27:06 -0700954 if ((retries--) == 0U)
Ed Tanous07d467b2021-02-23 14:48:37 -0800955 {
956 std::cerr << "error writing fru: " << strerror(errno) << "\n";
957 close(file);
958 throw DBusInternalError();
959 return false;
960 }
961 }
962 else
963 {
964 retries = retryMax;
965 index++;
966 }
967 // most eeproms require 5-10ms between writes
968 std::this_thread::sleep_for(std::chrono::milliseconds(10));
969 }
970 close(file);
971 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700972}
973
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800974void rescanOneBus(
krishnar4213ee212022-11-11 15:39:30 +0530975 BusMap& busmap, uint16_t busNum,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800976 boost::container::flat_map<
977 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700978 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800979 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530980 sdbusplus::asio::object_server& objServer,
981 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800982{
Yong Li6fdfac02023-10-30 11:21:30 +0800983 for (auto device = foundDevices.begin(); device != foundDevices.end();)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800984 {
Yong Li6fdfac02023-10-30 11:21:30 +0800985 if (device->first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800986 {
Yong Li6fdfac02023-10-30 11:21:30 +0800987 objServer.remove_interface(device->second);
988 device = foundDevices.erase(device);
989 }
990 else
991 {
992 device++;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800993 }
994 }
995
James Feist5d7f4692020-06-17 18:22:33 -0700996 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
997 if (!fs::exists(busPath))
998 {
999 if (dbusCall)
1000 {
1001 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
1002 << "\n";
1003 throw std::invalid_argument("Invalid Bus.");
1004 }
1005 return;
1006 }
1007
1008 std::vector<fs::path> i2cBuses;
1009 i2cBuses.emplace_back(busPath);
1010
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001011 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301012 i2cBuses, busmap, powerIsOn, objServer,
1013 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
1014 &objServer, &systemBus]() {
Yong Li6fdfac02023-10-30 11:21:30 +08001015 for (auto busIface = dbusInterfaceMap.begin();
1016 busIface != dbusInterfaceMap.end();)
Patrick Williamsdf190612023-05-10 07:51:34 -05001017 {
Yong Li6fdfac02023-10-30 11:21:30 +08001018 if (busIface->first.first == static_cast<size_t>(busNum))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001019 {
Yong Li6fdfac02023-10-30 11:21:30 +08001020 objServer.remove_interface(busIface->second);
1021 busIface = dbusInterfaceMap.erase(busIface);
1022 }
1023 else
1024 {
1025 busIface++;
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001026 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001027 }
1028 auto found = busmap.find(busNum);
1029 if (found == busmap.end() || found->second == nullptr)
1030 {
1031 return;
1032 }
1033 for (auto& device : *(found->second))
1034 {
1035 addFruObjectToDbus(device.second, dbusInterfaceMap,
1036 static_cast<uint32_t>(busNum), device.first,
1037 unknownBusObjectCount, powerIsOn, objServer,
1038 systemBus);
1039 }
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001040 });
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001041 scan->run();
1042}
1043
James Feist9eb0b582018-04-27 12:15:46 -07001044void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -07001045 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -08001046 boost::container::flat_map<
1047 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301048 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001049 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301050 sdbusplus::asio::object_server& objServer,
1051 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -08001052{
Ed Tanousaa497ed2023-02-28 13:43:41 -08001053 static boost::asio::steady_timer timer(io);
1054 timer.expires_from_now(std::chrono::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001055
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001056 // setup an async wait in case we get flooded with requests
Jian Zhang6e7cebf2022-11-29 18:30:21 +08001057 timer.async_wait([&](const boost::system::error_code& ec) {
1058 if (ec == boost::asio::error::operation_aborted)
1059 {
1060 return;
1061 }
1062
1063 if (ec)
1064 {
1065 std::cerr << "Error in timer: " << ec.message() << "\n";
1066 return;
1067 }
1068
James Feist4131aea2018-03-09 09:47:30 -08001069 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001070 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001071
Nikhil Potaded8884f12019-03-27 13:27:13 -07001072 boost::container::flat_map<size_t, fs::path> busPaths;
1073 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001074 {
James Feist4131aea2018-03-09 09:47:30 -08001075 std::cerr << "unable to find i2c devices\n";
1076 return;
James Feist918e18c2018-02-13 15:51:07 -08001077 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001078
Ed Tanous07d467b2021-02-23 14:48:37 -08001079 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001080 {
1081 i2cBuses.emplace_back(busPath.second);
1082 }
James Feist4131aea2018-03-09 09:47:30 -08001083
James Feist98132792019-07-09 13:29:09 -07001084 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001085 for (auto& [pair, interface] : foundDevices)
1086 {
1087 objServer.remove_interface(interface);
1088 }
1089 foundDevices.clear();
1090
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301091 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301092 i2cBuses, busmap, powerIsOn, objServer, [&]() {
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001093 for (auto& busIface : dbusInterfaceMap)
1094 {
1095 objServer.remove_interface(busIface.second);
1096 }
James Feist4131aea2018-03-09 09:47:30 -08001097
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001098 dbusInterfaceMap.clear();
1099 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001100
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001101 // todo, get this from a more sensable place
1102 std::vector<uint8_t> baseboardFRU;
1103 if (readBaseboardFRU(baseboardFRU))
1104 {
1105 // If no device on i2c bus 0, the insertion will happen.
1106 auto bus0 = busmap.try_emplace(0,
1107 std::make_shared<DeviceMap>());
1108 bus0.first->second->emplace(0, baseboardFRU);
1109 }
1110 for (auto& devicemap : busmap)
1111 {
1112 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001113 {
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001114 addFruObjectToDbus(device.second, dbusInterfaceMap,
1115 devicemap.first, device.first,
1116 unknownBusObjectCount, powerIsOn,
1117 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001118 }
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001119 }
1120 });
James Feist6ebf9de2018-05-15 15:01:17 -07001121 scan->run();
1122 });
James Feist918e18c2018-02-13 15:51:07 -08001123}
1124
Joshi-Mansid73b7442020-03-27 19:10:21 +05301125// Details with example of Asset Tag Update
1126// To find location of Product Info Area asset tag as per FRU specification
1127// 1. Find product Info area starting offset (*8 - as header will be in
1128// multiple of 8 bytes).
1129// 2. Skip 3 bytes of product info area (like format version, area length,
1130// and language code).
1131// 3. Traverse manufacturer name, product name, product version, & product
1132// serial number, by reading type/length code to reach the Asset Tag.
1133// 4. Update the Asset Tag, reposition the product Info area in multiple of
1134// 8 bytes. Update the Product area length and checksum.
1135
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001136bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301137 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001138 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301139 boost::container::flat_map<
1140 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301141 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001142 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301143 sdbusplus::asio::object_server& objServer,
1144 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301145{
1146 size_t updatePropertyReqLen = updatePropertyReq.length();
1147 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1148 {
1149 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001150 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301151 "Invalid Length "
1152 << updatePropertyReqLen << "\n";
1153 return false;
1154 }
1155
1156 std::vector<uint8_t> fruData;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301157
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301158 if (!getFruData(fruData, bus, address))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301159 {
Kumar Thangavel9d6f5902022-08-26 16:52:14 +05301160 std::cerr << "Failure getting FRU Data \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301161 return false;
1162 }
1163
AKSHAY RAVEENDRAN K4d4df5b2022-11-14 11:16:29 +00001164 struct FruArea fruAreaParams
1165 {};
Joshi-Mansid73b7442020-03-27 19:10:21 +05301166
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301167 if (!findFruAreaLocationAndField(fruData, propertyName, fruAreaParams))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301168 {
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301169 std::cerr << "findFruAreaLocationAndField failed \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301170 return false;
1171 }
1172
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301173 std::vector<uint8_t> restFRUAreaFieldsData;
1174 if (!copyRestFRUArea(fruData, propertyName, fruAreaParams,
1175 restFRUAreaFieldsData))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301176 {
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301177 std::cerr << "copyRestFRUArea failed \n";
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001178 return false;
1179 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301180
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001181 // Push post update fru areas if any
1182 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001183 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1184 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001185 {
1186 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001187 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301188 if ((fruAreaLoc > fruAreaParams.restFieldsEnd) &&
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001189 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1190 {
1191 nextFRUAreaLoc = fruAreaLoc;
1192 }
1193 }
1194 std::vector<uint8_t> restFRUAreasData;
Ed Tanous3013fb42022-07-09 08:27:06 -07001195 if (nextFRUAreaLoc != 0U)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001196 {
1197 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1198 fruData.size() - nextFRUAreaLoc,
1199 std::back_inserter(restFRUAreasData));
1200 }
1201
1202 // check FRU area size
1203 size_t fruAreaDataSize =
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301204 ((fruAreaParams.updateFieldLoc - fruAreaParams.start + 1) +
1205 restFRUAreaFieldsData.size());
1206 size_t fruAreaAvailableSize = fruAreaParams.size - fruAreaDataSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001207 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1208 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001209#ifdef ENABLE_FRU_AREA_RESIZE
1210 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1211 // round size to 8-byte blocks
Patrick Williamsdf190612023-05-10 07:51:34 -05001212 newFRUAreaSize = ((newFRUAreaSize - 1) / fruBlockSize + 1) *
1213 fruBlockSize;
1214 size_t newFRUDataSize = fruData.size() + newFRUAreaSize -
1215 fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001216 fruData.resize(newFRUDataSize);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301217 fruAreaParams.size = newFRUAreaSize;
1218 fruAreaParams.end = fruAreaParams.start + fruAreaParams.size;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001219#else
1220 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1221 << " should not be greater than available FRU area size: "
1222 << fruAreaAvailableSize << "\n";
1223 return false;
1224#endif // ENABLE_FRU_AREA_RESIZE
1225 }
1226
Joshi-Mansid73b7442020-03-27 19:10:21 +05301227 // write new requested property field length and data
1228 constexpr uint8_t newTypeLenMask = 0xC0;
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301229 fruData[fruAreaParams.updateFieldLoc] =
Joshi-Mansid73b7442020-03-27 19:10:21 +05301230 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301231 fruAreaParams.updateFieldLoc++;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301232 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301233 fruData.begin() + fruAreaParams.updateFieldLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301234
1235 // Copy remaining data to main fru area - post updated fru field vector
Patrick Williamsdf190612023-05-10 07:51:34 -05001236 fruAreaParams.restFieldsLoc = fruAreaParams.updateFieldLoc +
1237 updatePropertyReqLen;
1238 size_t fruAreaDataEnd = fruAreaParams.restFieldsLoc +
1239 restFRUAreaFieldsData.size();
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301240
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001241 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
Kumar Thangavel51b557b2022-09-13 13:40:47 +05301242 fruData.begin() + fruAreaParams.restFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301243
1244 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001245 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
Kumar Thangavelbdfc5ec2022-08-29 22:23:00 +05301246 fruData, fruAreaParams.start, fruAreaDataEnd, fruAreaParams.end);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301247
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001248#ifdef ENABLE_FRU_AREA_RESIZE
1249 ++nextFRUAreaNewLoc;
Patrick Williamsdf190612023-05-10 07:51:34 -05001250 ssize_t nextFRUAreaOffsetDiff = (nextFRUAreaNewLoc - nextFRUAreaLoc) /
1251 fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001252 // Append rest FRU Areas if size changed and there were other sections after
1253 // updated one
1254 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1255 {
1256 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1257 fruData.begin() + nextFRUAreaNewLoc);
1258 // Update Common Header
Ed Tanous6e22c872023-02-16 15:19:28 -08001259 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1260 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001261 {
Ed Tanous6e22c872023-02-16 15:19:28 -08001262 unsigned int fruAreaOffsetField =
1263 getHeaderAreaFieldOffset(nextFRUArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001264 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
Ed Tanous6e22c872023-02-16 15:19:28 -08001265 if (curFRUAreaOffset > fruAreaParams.end)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001266 {
1267 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1268 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1269 }
1270 }
1271 // Calculate new checksum
1272 std::vector<uint8_t> headerFRUData;
1273 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1274 size_t checksumVal = calculateChecksum(headerFRUData);
1275 fruData[7] = static_cast<uint8_t>(checksumVal);
1276 // fill zeros if FRU Area size decreased
1277 if (nextFRUAreaOffsetDiff < 0)
1278 {
1279 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1280 restFRUAreasData.size(),
1281 fruData.end(), 0);
1282 }
1283 }
1284#else
1285 // this is to avoid "unused variable" warning
1286 (void)nextFRUAreaNewLoc;
1287#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301288 if (fruData.empty())
1289 {
1290 return false;
1291 }
1292
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001293 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301294 fruData))
1295 {
1296 return false;
1297 }
1298
1299 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301300 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1301 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301302 return true;
1303}
1304
James Feist98132792019-07-09 13:29:09 -07001305int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001306{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301307 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1308 sdbusplus::asio::object_server objServer(systemBus);
1309
Kumar Thangavel41a90512021-11-24 15:44:20 +05301310 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301311 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001312 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001313 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001314 std::vector<fs::path> i2cBuses;
1315
James Feista3c180a2018-08-09 16:06:04 -07001316 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001317 {
1318 std::cerr << "unable to find i2c devices\n";
1319 return 1;
1320 }
James Feist3cb5fec2018-01-23 14:41:51 -08001321
Matt Simmering2447c242023-11-09 14:18:39 -08001322 // check for and load blocklist with initial buses.
1323 loadBlocklist(blocklistPath);
Patrick Venture11f1ff42019-08-01 10:42:12 -07001324
Vijay Khemka065f6d92018-12-18 10:37:47 -08001325 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001326
James Feist6ebf9de2018-05-15 15:01:17 -07001327 // this is a map with keys of pair(bus number, address) and values of
1328 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001329 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001330 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1331 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001332
James Feist9eb0b582018-04-27 12:15:46 -07001333 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1334 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1335 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001336
Kumar Thangavel41a90512021-11-24 15:44:20 +05301337 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301338 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1339 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301340 });
James Feist2a9d6db2018-04-27 15:48:28 -07001341
krishnar4213ee212022-11-11 15:39:30 +05301342 iface->register_method("ReScanBus", [&](uint16_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301343 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301344 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001345 });
1346
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001347 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001348
Patrick Williamsdf190612023-05-10 07:51:34 -05001349 iface->register_method("WriteFru",
1350 [&](const uint16_t bus, const uint8_t address,
1351 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001352 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001353 {
James Feistddb78302018-09-06 11:45:42 -07001354 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001355 return;
1356 }
1357 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301358 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1359 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001360 });
James Feist9eb0b582018-04-27 12:15:46 -07001361 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001362
Patrick Williams2af39222022-07-22 19:26:56 -05001363 std::function<void(sdbusplus::message_t & message)> eventHandler =
1364 [&](sdbusplus::message_t& message) {
Patrick Williamsdf190612023-05-10 07:51:34 -05001365 std::string objectName;
1366 boost::container::flat_map<
1367 std::string,
1368 std::variant<std::string, bool, int64_t, uint64_t, double>>
1369 values;
1370 message.read(objectName, values);
1371 auto findState = values.find("CurrentHostState");
1372 if (findState != values.end())
1373 {
1374 if (std::get<std::string>(findState->second) ==
1375 "xyz.openbmc_project.State.Host.HostState.Running")
James Feist918e18c2018-02-13 15:51:07 -08001376 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001377 powerIsOn = true;
James Feist0eeb79c2019-10-09 13:35:29 -07001378 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001379 }
James Feist6ebf9de2018-05-15 15:01:17 -07001380
Patrick Williamsdf190612023-05-10 07:51:34 -05001381 if (powerIsOn)
1382 {
1383 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1384 powerIsOn, objServer, systemBus);
1385 }
1386 };
James Feist9eb0b582018-04-27 12:15:46 -07001387
Patrick Williams2af39222022-07-22 19:26:56 -05001388 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1389 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001390 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001391 "openbmc_project/state/"
1392 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001393 eventHandler);
1394
James Feist4131aea2018-03-09 09:47:30 -08001395 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001396 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
Ed Tanous3013fb42022-07-09 08:27:06 -07001397 std::array<char, 4096> readBuffer{};
James Feist4131aea2018-03-09 09:47:30 -08001398 // monitor for new i2c devices
1399 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1400 std::function<void(const boost::system::error_code, std::size_t)>
Patrick Williamsb9dd7f82023-10-20 11:20:11 -05001401 watchI2cBusses = [&](const boost::system::error_code& ec,
1402 std::size_t bytesTransferred) {
Patrick Williamsdf190612023-05-10 07:51:34 -05001403 if (ec)
1404 {
1405 std::cout << "Callback Error " << ec << "\n";
1406 return;
1407 }
1408 size_t index = 0;
1409 while ((index + sizeof(inotify_event)) <= bytesTransferred)
1410 {
1411 const char* p = &readBuffer[index];
1412 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
1413 const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
1414 switch (iEvent->mask)
James Feist4131aea2018-03-09 09:47:30 -08001415 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001416 case IN_CREATE:
1417 case IN_MOVED_TO:
1418 case IN_DELETE:
1419 std::string_view name(&iEvent->name[0], iEvent->len);
1420 if (boost::starts_with(name, "i2c"))
1421 {
1422 int bus = busStrToInt(name);
1423 if (bus < 0)
James Feist9eb0b582018-04-27 12:15:46 -07001424 {
Patrick Williamsdf190612023-05-10 07:51:34 -05001425 std::cerr << "Could not parse bus " << name << "\n";
1426 continue;
1427 }
1428 int rootBus = getRootBus(bus);
1429 if (rootBus >= 0)
1430 {
1431 rescanOneBus(busMap, static_cast<uint16_t>(rootBus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301432 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301433 unknownBusObjectCount, powerIsOn,
1434 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001435 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001436 rescanOneBus(busMap, static_cast<uint16_t>(bus),
1437 dbusInterfaceMap, false,
1438 unknownBusObjectCount, powerIsOn,
1439 objServer, systemBus);
1440 }
James Feist4131aea2018-03-09 09:47:30 -08001441 }
Patrick Williamsdf190612023-05-10 07:51:34 -05001442 index += sizeof(inotify_event) + iEvent->len;
1443 }
James Feist6ebf9de2018-05-15 15:01:17 -07001444
Patrick Williamsdf190612023-05-10 07:51:34 -05001445 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1446 watchI2cBusses);
1447 };
James Feist4131aea2018-03-09 09:47:30 -08001448
1449 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001450 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301451 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1452 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001453
James Feist3cb5fec2018-01-23 14:41:51 -08001454 io.run();
1455 return 0;
1456}