blob: b1c4d1d222ad392c2b2c64e126016da7111f7f71 [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 Bishop1fb9f3f2020-08-28 08:15:13 -040016/// \file FruDevice.cpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
Patrick Ventureab296412020-12-30 13:39:37 -080018#include "FruUtils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070019#include "Utils.hpp"
20
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>
James Feist3b860982018-10-02 14:34:07 -070035#include <chrono>
36#include <ctime>
Patrick Venturee3754002019-08-06 09:39:12 -070037#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080038#include <fstream>
Patrick Venturebaba7672019-10-26 09:26:41 -070039#include <functional>
James Feist3cb5fec2018-01-23 14:41:51 -080040#include <future>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070041#include <iomanip>
James Feist3cb5fec2018-01-23 14:41:51 -080042#include <iostream>
Patrick Venturee26395d2019-10-29 14:05:16 -070043#include <limits>
Andrew Jefferya9c58922021-06-01 09:28:59 +093044#include <map>
James Feist3f8a2782018-02-12 09:24:42 -080045#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070046#include <set>
47#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070048#include <string>
James Feist3b860982018-10-02 14:34:07 -070049#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080050#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080051#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070052#include <vector>
James Feist3b860982018-10-02 14:34:07 -070053
James Feist8c505da2020-05-28 10:06:33 -070054extern "C"
55{
James Feist3b860982018-10-02 14:34:07 -070056#include <i2c/smbus.h>
57#include <linux/i2c-dev.h>
58}
James Feist3cb5fec2018-01-23 14:41:51 -080059
Ed Tanous072e25d2018-12-16 21:45:20 -080060namespace fs = std::filesystem;
Ed Tanous07d467b2021-02-23 14:48:37 -080061static constexpr bool debug = false;
Ed Tanous07d467b2021-02-23 14:48:37 -080062constexpr size_t maxFruSize = 512;
63constexpr size_t maxEepromPageIndex = 255;
James Feist26c27ad2018-07-25 15:09:40 -070064constexpr size_t busTimeoutSeconds = 5;
James Feist3cb5fec2018-01-23 14:41:51 -080065
Patrick Venture11f1ff42019-08-01 10:42:12 -070066constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
67
Ed Tanous07d467b2021-02-23 14:48:37 -080068const static constexpr char* baseboardFruLocation =
James Feist3cb5fec2018-01-23 14:41:51 -080069 "/etc/fru/baseboard.fru.bin";
70
Ed Tanous07d467b2021-02-23 14:48:37 -080071const static constexpr char* i2CDevLocation = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080072
Andrei Kartashev1c5b7062020-08-19 14:47:30 +030073using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>;
James Feist3cb5fec2018-01-23 14:41:51 -080074using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
75
James Feist444830e2019-04-05 08:38:16 -070076static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070077struct FindDevicesWithCallback;
78
Nikhil Potaded8884f12019-03-27 13:27:13 -070079static BusMap busMap;
80
James Feist8a983922019-10-24 17:11:56 -070081static boost::container::flat_map<
82 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
83 foundDevices;
84
James Feist7972bb92019-11-13 15:59:24 -080085static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
86
James Feist5cb06082019-10-24 17:11:13 -070087boost::asio::io_service io;
James Feist5cb06082019-10-24 17:11:13 -070088
Andrei Kartashev2a7e3952020-09-02 20:32:26 +030089uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter,
90 std::vector<uint8_t>::const_iterator end);
Andrei Kartashev2f0de172020-08-13 14:29:40 +030091bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +053092 const std::string& assetTag, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -080093 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +053094 boost::container::flat_map<
95 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +053096 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -080097 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +053098 sdbusplus::asio::object_server& objServer,
99 std::shared_ptr<sdbusplus::asio::connection>& systemBus);
Patrick Venturebaba7672019-10-26 09:26:41 -0700100
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700101// Given a bus/address, produce the path in sysfs for an eeprom.
102static std::string getEepromPath(size_t bus, size_t address)
103{
104 std::stringstream output;
105 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
106 << std::setfill('0') << std::setw(4) << std::hex << address
107 << "/eeprom";
108 return output.str();
109}
110
111static bool hasEepromFile(size_t bus, size_t address)
112{
113 auto path = getEepromPath(bus, address);
114 try
115 {
116 return fs::exists(path);
117 }
118 catch (...)
119 {
120 return false;
121 }
122}
123
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700124static int64_t readFromEeprom(int flag __attribute__((unused)), int fd,
Xiang Liu2801a702020-01-20 14:29:34 -0800125 uint16_t address __attribute__((unused)),
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700126 uint16_t offset, uint8_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700127{
128 auto result = lseek(fd, offset, SEEK_SET);
129 if (result < 0)
130 {
131 std::cerr << "failed to seek\n";
132 return -1;
133 }
134
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700135 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700136}
137
James Feist5d7f4692020-06-17 18:22:33 -0700138static int busStrToInt(const std::string& busName)
139{
Ed Tanous07d467b2021-02-23 14:48:37 -0800140 auto findBus = busName.rfind('-');
James Feist5d7f4692020-06-17 18:22:33 -0700141 if (findBus == std::string::npos)
142 {
143 return -1;
144 }
145 return std::stoi(busName.substr(findBus + 1));
146}
147
James Feist7972bb92019-11-13 15:59:24 -0800148static int getRootBus(size_t bus)
149{
150 auto ec = std::error_code();
151 auto path = std::filesystem::read_symlink(
152 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
153 std::to_string(bus) + "/mux_device"),
154 ec);
155 if (ec)
156 {
157 return -1;
158 }
159
160 std::string filename = path.filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800161 auto findBus = filename.find('-');
James Feist7972bb92019-11-13 15:59:24 -0800162 if (findBus == std::string::npos)
163 {
164 return -1;
165 }
166 return std::stoi(filename.substr(0, findBus));
167}
168
James Feistc95cb142018-02-26 10:41:42 -0800169static bool isMuxBus(size_t bus)
170{
Ed Tanous072e25d2018-12-16 21:45:20 -0800171 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800172 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
173}
174
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530175static void makeProbeInterface(size_t bus, size_t address,
176 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700177{
178 if (isMuxBus(bus))
179 {
180 return; // the mux buses are random, no need to publish
181 }
182 auto [it, success] = foundDevices.emplace(
183 std::make_pair(bus, address),
184 objServer.add_interface(
185 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
186 std::to_string(address),
187 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
188 if (!success)
189 {
190 return; // already added
191 }
192 it->second->register_property("Bus", bus);
193 it->second->register_property("Address", address);
194 it->second->initialize();
195}
196
Vijay Khemka2d681f62018-11-06 15:51:00 -0800197static int isDevice16Bit(int file)
198{
199 /* Get first byte */
200 int byte1 = i2c_smbus_read_byte_data(file, 0);
201 if (byte1 < 0)
202 {
203 return byte1;
204 }
205 /* Read 7 more bytes, it will read same first byte in case of
206 * 8 bit but it will read next byte in case of 16 bit
207 */
208 for (int i = 0; i < 7; i++)
209 {
210 int byte2 = i2c_smbus_read_byte_data(file, 0);
211 if (byte2 < 0)
212 {
213 return byte2;
214 }
215 if (byte2 != byte1)
216 {
217 return 1;
218 }
219 }
220 return 0;
221}
222
Xiang Liu2801a702020-01-20 14:29:34 -0800223// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
224// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800225static int i2cSmbusWriteThenRead(int file, uint16_t address,
226 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
227 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800228{
Ed Tanous07d467b2021-02-23 14:48:37 -0800229 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
230 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800231 {
232 return -1;
233 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800234
Xiang Liu2801a702020-01-20 14:29:34 -0800235#define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2
236 struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT];
237 struct i2c_rdwr_ioctl_data rdwr;
238
239 msgs[0].addr = address;
240 msgs[0].flags = 0;
241 msgs[0].len = toSlaveBufLen;
242 msgs[0].buf = toSlaveBuf;
243 msgs[1].addr = address;
244 msgs[1].flags = I2C_M_RD;
245 msgs[1].len = fromSlaveBufLen;
246 msgs[1].buf = fromSlaveBuf;
247
248 rdwr.msgs = msgs;
249 rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT;
250
251 int ret = ioctl(file, I2C_RDWR, &rdwr);
252
253 return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? ret : -1;
254}
255
256static int64_t readBlockData(int flag, int file, uint16_t address,
257 uint16_t offset, uint8_t len, uint8_t* buf)
258{
Vijay Khemka2d681f62018-11-06 15:51:00 -0800259 if (flag == 0)
260 {
Xiang Liu2801a702020-01-20 14:29:34 -0800261 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
262 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800263 }
264
Xiang Liu2801a702020-01-20 14:29:34 -0800265 offset = htobe16(offset);
Ed Tanous07d467b2021-02-23 14:48:37 -0800266 return i2cSmbusWriteThenRead(
Xiang Liu2801a702020-01-20 14:29:34 -0800267 file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800268}
269
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700270// TODO: This code is very similar to the non-eeprom version and can be merged
271// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300272static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700273{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700274 auto path = getEepromPath(bus, address);
275
276 int file = open(path.c_str(), O_RDONLY);
277 if (file < 0)
278 {
279 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700280 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700281 }
282
Patrick Venturebaba7672019-10-26 09:26:41 -0700283 std::string errorMessage = "eeprom at " + std::to_string(bus) +
284 " address " + std::to_string(address);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300285 std::vector<uint8_t> device = readFRUContents(
Xiang Liu2801a702020-01-20 14:29:34 -0800286 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700287
288 close(file);
289 return device;
290}
291
Ed Tanous07d467b2021-02-23 14:48:37 -0800292std::set<int> findI2CEeproms(int i2cBus,
293 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700294{
295 std::set<int> foundList;
296
297 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
298
299 // For each file listed under the i2c device
300 // NOTE: This should be faster than just checking for each possible address
301 // path.
302 for (const auto& p : fs::directory_iterator(path))
303 {
304 const std::string node = p.path().string();
305 std::smatch m;
306 bool found =
307 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
308
309 if (!found)
310 {
311 continue;
312 }
313 if (m.size() != 2)
314 {
315 std::cerr << "regex didn't capture\n";
316 continue;
317 }
318
319 std::ssub_match subMatch = m[1];
320 std::string addressString = subMatch.str();
321
322 std::size_t ignored;
323 const int hexBase = 16;
324 int address = std::stoi(addressString, &ignored, hexBase);
325
326 const std::string eeprom = node + "/eeprom";
327
328 try
329 {
330 if (!fs::exists(eeprom))
331 {
332 continue;
333 }
334 }
335 catch (...)
336 {
337 continue;
338 }
339
340 // There is an eeprom file at this address, it may have invalid
341 // contents, but we found it.
342 foundList.insert(address);
343
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300344 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700345 if (!device.empty())
346 {
347 devices->emplace(address, device);
348 }
349 }
350
351 return foundList;
352}
353
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300354int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800355 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530356 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800357{
James Feist3cb5fec2018-01-23 14:41:51 -0800358
James Feist26c27ad2018-07-25 15:09:40 -0700359 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700360 // NOTE: When reading the devices raw on the bus, it can interfere with
361 // the driver's ability to operate, therefore read eeproms first before
362 // scanning for devices without drivers. Several experiments were run
363 // and it was determined that if there were any devices on the bus
364 // before the eeprom was hit and read, the eeprom driver wouldn't open
365 // while the bus device was open. An experiment was not performed to see
366 // if this issue was resolved if the i2c bus device was closed, but
367 // hexdumps of the eeprom later were successful.
368
369 // Scan for i2c eeproms loaded on this bus.
370 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800371 std::set<size_t>& failedItems = failedAddresses[bus];
372
373 std::set<size_t>* rootFailures = nullptr;
374 int rootBus = getRootBus(bus);
375
376 if (rootBus >= 0)
377 {
378 rootFailures = &(failedAddresses[rootBus]);
379 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700380
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530381 constexpr int startSkipSlaveAddr = 0;
382 constexpr int endSkipSlaveAddr = 12;
383
James Feist26c27ad2018-07-25 15:09:40 -0700384 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800385 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700386 if (skipList.find(ii) != skipList.end())
387 {
388 continue;
389 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530390 // skipping since no device is present in this range
391 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
392 {
393 continue;
394 }
James Feist26c27ad2018-07-25 15:09:40 -0700395 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530396 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800397 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300398 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700399 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700400 continue;
401 }
402 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800403 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700404 {
405 continue;
406 }
James Feist3cb5fec2018-01-23 14:41:51 -0800407
Ed Tanous07d467b2021-02-23 14:48:37 -0800408 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700409 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700410 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700411 << "\n";
412 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800413
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530414 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700415
James Feist7972bb92019-11-13 15:59:24 -0800416 if (failedItems.find(ii) != failedItems.end())
417 {
418 // if we failed to read it once, unlikely we can read it later
419 continue;
420 }
421
422 if (rootFailures != nullptr)
423 {
424 if (rootFailures->find(ii) != rootFailures->end())
425 {
426 continue;
427 }
428 }
429
Vijay Khemka2d681f62018-11-06 15:51:00 -0800430 /* Check for Device type if it is 8 bit or 16 bit */
431 int flag = isDevice16Bit(file);
432 if (flag < 0)
433 {
434 std::cerr << "failed to read bus " << bus << " address " << ii
435 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700436 if (powerIsOn)
437 {
438 failedItems.insert(ii);
439 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800440 continue;
441 }
442
Patrick Venturebaba7672019-10-26 09:26:41 -0700443 std::string errorMessage =
444 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300445 std::vector<uint8_t> device =
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300446 readFRUContents(flag, file, static_cast<uint16_t>(ii),
Xiang Liu2801a702020-01-20 14:29:34 -0800447 readBlockData, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700448 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700449 {
James Feist26c27ad2018-07-25 15:09:40 -0700450 continue;
451 }
James Feist26c27ad2018-07-25 15:09:40 -0700452
Patrick Venture786f1792019-08-05 16:33:44 -0700453 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800454 }
James Feist26c27ad2018-07-25 15:09:40 -0700455 return 1;
456 });
457 std::future_status status =
458 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
459 if (status == std::future_status::timeout)
460 {
461 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700462 if (powerIsOn)
463 {
464 busBlacklist.insert(bus);
465 }
James Feist444830e2019-04-05 08:38:16 -0700466 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700467 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800468 }
469
James Feist444830e2019-04-05 08:38:16 -0700470 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700471 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800472}
473
Patrick Venture11f1ff42019-08-01 10:42:12 -0700474void loadBlacklist(const char* path)
475{
476 std::ifstream blacklistStream(path);
477 if (!blacklistStream.good())
478 {
479 // File is optional.
480 std::cerr << "Cannot open blacklist file.\n\n";
481 return;
482 }
483
484 nlohmann::json data =
485 nlohmann::json::parse(blacklistStream, nullptr, false);
486 if (data.is_discarded())
487 {
488 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
489 "exiting\n";
490 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700491 }
492
493 // It's expected to have at least one field, "buses" that is an array of the
494 // buses by integer. Allow for future options to exclude further aspects,
495 // such as specific addresses or ranges.
496 if (data.type() != nlohmann::json::value_t::object)
497 {
498 std::cerr << "Illegal blacklist, expected to read dictionary\n";
499 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700500 }
501
502 // If buses field is missing, that's fine.
503 if (data.count("buses") == 1)
504 {
505 // Parse the buses array after a little validation.
506 auto buses = data.at("buses");
507 if (buses.type() != nlohmann::json::value_t::array)
508 {
509 // Buses field present but invalid, therefore this is an error.
510 std::cerr << "Invalid contents for blacklist buses field\n";
511 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700512 }
513
514 // Catch exception here for type mis-match.
515 try
516 {
517 for (const auto& bus : buses)
518 {
519 busBlacklist.insert(bus.get<size_t>());
520 }
521 }
522 catch (const nlohmann::detail::type_error& e)
523 {
524 // Type mis-match is a critical error.
525 std::cerr << "Invalid bus type: " << e.what() << "\n";
526 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700527 }
528 }
529
530 return;
531}
532
Ed Tanous07d467b2021-02-23 14:48:37 -0800533static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800534 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530535 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800536{
James Feista465ccc2019-02-08 12:51:01 -0800537 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800538 {
James Feist5d7f4692020-06-17 18:22:33 -0700539 int bus = busStrToInt(i2cBus);
James Feist0c3980a2019-12-19 11:09:27 -0800540
James Feist5d7f4692020-06-17 18:22:33 -0700541 if (bus < 0)
542 {
543 std::cerr << "Cannot translate " << i2cBus << " to int\n";
544 continue;
545 }
James Feist444830e2019-04-05 08:38:16 -0700546 if (busBlacklist.find(bus) != busBlacklist.end())
547 {
548 continue; // skip previously failed busses
549 }
James Feist3cb5fec2018-01-23 14:41:51 -0800550
James Feist0c3980a2019-12-19 11:09:27 -0800551 int rootBus = getRootBus(bus);
552 if (busBlacklist.find(rootBus) != busBlacklist.end())
553 {
554 continue;
555 }
556
James Feist3cb5fec2018-01-23 14:41:51 -0800557 auto file = open(i2cBus.c_str(), O_RDWR);
558 if (file < 0)
559 {
560 std::cerr << "unable to open i2c device " << i2cBus.string()
561 << "\n";
562 continue;
563 }
564 unsigned long funcs = 0;
565
566 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
567 {
568 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700569 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800570 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700571 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800572 continue;
573 }
574 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
575 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
576 {
577 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
578 << bus << "\n";
579 continue;
580 }
James Feist98132792019-07-09 13:29:09 -0700581 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800582 device = std::make_shared<DeviceMap>();
583
Nikhil Potaded8884f12019-03-27 13:27:13 -0700584 // i2cdetect by default uses the range 0x03 to 0x77, as
585 // this is what we have tested with, use this range. Could be
586 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800587 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800588 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700589 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800590 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700591
592 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530593 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700594
Ed Tanous07d467b2021-02-23 14:48:37 -0800595 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800596 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700597 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800598 }
James Feist3cb5fec2018-01-23 14:41:51 -0800599 }
James Feist3cb5fec2018-01-23 14:41:51 -0800600}
601
James Feist6ebf9de2018-05-15 15:01:17 -0700602// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700603struct FindDevicesWithCallback :
604 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700605{
James Feista465ccc2019-02-08 12:51:01 -0800606 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800607 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530608 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800609 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700610 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530611 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
612 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700613 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700614 ~FindDevicesWithCallback()
615 {
616 _callback();
617 }
618 void run()
619 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530620 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700621 }
622
James Feista465ccc2019-02-08 12:51:01 -0800623 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800624 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800625 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530626 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700627 std::function<void(void)> _callback;
628};
629
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300630std::vector<uint8_t>& getFRUInfo(const uint8_t& bus, const uint8_t& address)
Nikhil Potaded8884f12019-03-27 13:27:13 -0700631{
632 auto deviceMap = busMap.find(bus);
633 if (deviceMap == busMap.end())
634 {
635 throw std::invalid_argument("Invalid Bus.");
636 }
637 auto device = deviceMap->second->find(address);
638 if (device == deviceMap->second->end())
639 {
640 throw std::invalid_argument("Invalid Address.");
641 }
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300642 std::vector<uint8_t>& ret = device->second;
Nikhil Potaded8884f12019-03-27 13:27:13 -0700643
644 return ret;
645}
646
Ed Tanous07d467b2021-02-23 14:48:37 -0800647void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300648 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800649 boost::container::flat_map<
650 std::pair<size_t, size_t>,
651 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530652 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800653 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530654 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800655{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300656 boost::container::flat_map<std::string, std::string> formattedFRU;
Michael Shen0961b112022-02-22 11:06:33 +0800657 resCodes res = formatIPMIFRU(device, formattedFRU);
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300658 if (res == resCodes::resErr)
James Feist3cb5fec2018-01-23 14:41:51 -0800659 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300660 std::cerr << "failed to parse FRU for device at bus " << bus
Patrick Ventureb755c832019-08-07 11:09:14 -0700661 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800662 return;
663 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800664 if (res == resCodes::resWarn)
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300665 {
666 std::cerr << "there were warnings while parsing FRU for device at bus "
667 << bus << " address " << address << "\n";
668 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700669
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300670 auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800671 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700672 // Not found under Board section or 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 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300676 productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800677 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700678 // Found under Product section and not an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300679 if (productNameFind != formattedFRU.end() &&
Patrick Venture96cdaef2019-07-30 13:30:52 -0700680 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800681 {
682 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800683 std::regex illegalObject("[^A-Za-z0-9_]");
684 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800685 }
686 else
687 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800688 productName = "UNKNOWN" + std::to_string(unknownBusObjectCount);
689 unknownBusObjectCount++;
James Feist3cb5fec2018-01-23 14:41:51 -0800690 }
691
James Feist918e18c2018-02-13 15:51:07 -0800692 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800693 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700694 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800695 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700696 int highest = -1;
697 bool found = false;
698
James Feista465ccc2019-02-08 12:51:01 -0800699 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800700 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700701 std::string path = busIface.second->get_object_path();
702 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
James Feist918e18c2018-02-13 15:51:07 -0800703 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700704 if (isMuxBus(bus) && address == busIface.first.second &&
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300705 (getFRUInfo(static_cast<uint8_t>(busIface.first.first),
James Feist98132792019-07-09 13:29:09 -0700706 static_cast<uint8_t>(busIface.first.second)) ==
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300707 getFRUInfo(static_cast<uint8_t>(bus),
James Feist98132792019-07-09 13:29:09 -0700708 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700709 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700710 // This device is already added to the lower numbered bus,
711 // do not replicate it.
712 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700713 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700714
715 // Check if the match named has extra information.
716 found = true;
Ed Tanous07d467b2021-02-23 14:48:37 -0800717 std::smatch baseMatch;
Patrick Venture015fb0a2019-08-16 09:33:31 -0700718
719 bool match = std::regex_match(
Ed Tanous07d467b2021-02-23 14:48:37 -0800720 path, baseMatch, std::regex(productName + "_(\\d+)$"));
Patrick Venture015fb0a2019-08-16 09:33:31 -0700721 if (match)
James Feist79e9c0b2018-03-15 15:21:17 -0700722 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800723 if (baseMatch.size() == 2)
Patrick Venture015fb0a2019-08-16 09:33:31 -0700724 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800725 std::ssub_match baseSubMatch = baseMatch[1];
726 std::string base = baseSubMatch.str();
Patrick Venture015fb0a2019-08-16 09:33:31 -0700727
728 int value = std::stoi(base);
729 highest = (value > highest) ? value : highest;
730 }
James Feist79e9c0b2018-03-15 15:21:17 -0700731 }
James Feist918e18c2018-02-13 15:51:07 -0800732 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700733 } // end searching objects
734
735 if (found)
736 {
737 // We found something with the same name. If highest was still -1,
738 // it means this new entry will be _0.
739 productName += "_";
740 productName += std::to_string(++highest);
James Feist918e18c2018-02-13 15:51:07 -0800741 }
742 }
James Feist3cb5fec2018-01-23 14:41:51 -0800743
James Feist9eb0b582018-04-27 12:15:46 -0700744 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
745 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
746 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
747
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300748 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800749 {
James Feist9eb0b582018-04-27 12:15:46 -0700750
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700751 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800752 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530753 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700754 {
755 continue;
756 }
757 std::string key =
Ed Tanous07d467b2021-02-23 14:48:37 -0800758 std::regex_replace(property.first, nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530759
760 if (property.first == "PRODUCT_ASSET_TAG")
761 {
762 std::string propertyName = property.first;
763 iface->register_property(
764 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530765 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530766 &unknownBusObjectCount, &powerIsOn, &objServer,
767 &systemBus](const std::string& req, std::string& resp) {
Ed Tanous07d467b2021-02-23 14:48:37 -0800768 if (strcmp(req.c_str(), resp.c_str()) != 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +0530769 {
770 // call the method which will update
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300771 if (updateFRUProperty(req, bus, address, propertyName,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530772 dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530773 unknownBusObjectCount, powerIsOn,
774 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530775 {
776 resp = req;
777 }
778 else
779 {
780 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300781 "FRU property update failed.");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530782 }
783 }
784 return 1;
785 });
786 }
787 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700788 {
789 std::cerr << "illegal key: " << key << "\n";
790 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800791 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700792 {
793 std::cout << property.first << ": " << property.second << "\n";
794 }
James Feist3cb5fec2018-01-23 14:41:51 -0800795 }
James Feist2a9d6db2018-04-27 15:48:28 -0700796
797 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700798 iface->register_property("BUS", bus);
799 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700800
James Feist9eb0b582018-04-27 12:15:46 -0700801 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800802}
803
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300804static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800805{
806 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800807 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300808 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800809 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300810 baseboardFRUFile.seekg(0, std::ios_base::end);
811 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
812 baseboardFRU.resize(fileSize);
813 baseboardFRUFile.seekg(0, std::ios_base::beg);
814 baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()),
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300815 fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800816 }
817 else
818 {
819 return false;
820 }
821 return true;
822}
823
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300824bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700825{
826 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800827 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700828 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300829 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700830 return false;
831 }
832 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800833 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700834 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300835 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700836 return false;
837 }
838 // baseboard fru
839 if (bus == 0 && address == 0)
840 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800841 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700842 if (!file.good())
843 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800844 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700845 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700846 return false;
847 }
James Feista465ccc2019-02-08 12:51:01 -0800848 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700849 return file.good();
850 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800851
852 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700853 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800854 auto path = getEepromPath(bus, address);
855 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
856 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700857 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800858 std::cerr << "unable to open i2c device " << path << "\n";
859 throw DBusInternalError();
860 return false;
861 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700862
Ed Tanous07d467b2021-02-23 14:48:37 -0800863 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
864 if (writtenBytes < 0)
865 {
866 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700867 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700868 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700869 return false;
870 }
871
Ed Tanous07d467b2021-02-23 14:48:37 -0800872 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700873 return true;
874 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800875
876 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
877
878 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
879 if (file < 0)
880 {
881 std::cerr << "unable to open i2c device " << i2cBus << "\n";
882 throw DBusInternalError();
883 return false;
884 }
885 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
886 {
887 std::cerr << "unable to set device address\n";
888 close(file);
889 throw DBusInternalError();
890 return false;
891 }
892
893 constexpr const size_t retryMax = 2;
894 uint16_t index = 0;
895 size_t retries = retryMax;
896 while (index < fru.size())
897 {
898 if ((index && ((index % (maxEepromPageIndex + 1)) == 0)) &&
899 (retries == retryMax))
900 {
901 // The 4K EEPROM only uses the A2 and A1 device address bits
902 // with the third bit being a memory page address bit.
903 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
904 {
905 std::cerr << "unable to set device address\n";
906 close(file);
907 throw DBusInternalError();
908 return false;
909 }
910 }
911
912 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
913 fru[index]) < 0)
914 {
915 if (!retries--)
916 {
917 std::cerr << "error writing fru: " << strerror(errno) << "\n";
918 close(file);
919 throw DBusInternalError();
920 return false;
921 }
922 }
923 else
924 {
925 retries = retryMax;
926 index++;
927 }
928 // most eeproms require 5-10ms between writes
929 std::this_thread::sleep_for(std::chrono::milliseconds(10));
930 }
931 close(file);
932 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700933}
934
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800935void rescanOneBus(
936 BusMap& busmap, uint8_t busNum,
937 boost::container::flat_map<
938 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700939 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800940 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530941 sdbusplus::asio::object_server& objServer,
942 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800943{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800944 for (auto& [pair, interface] : foundDevices)
945 {
946 if (pair.first == static_cast<size_t>(busNum))
947 {
948 objServer.remove_interface(interface);
949 foundDevices.erase(pair);
950 }
951 }
952
James Feist5d7f4692020-06-17 18:22:33 -0700953 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
954 if (!fs::exists(busPath))
955 {
956 if (dbusCall)
957 {
958 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
959 << "\n";
960 throw std::invalid_argument("Invalid Bus.");
961 }
962 return;
963 }
964
965 std::vector<fs::path> i2cBuses;
966 i2cBuses.emplace_back(busPath);
967
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800968 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530969 i2cBuses, busmap, powerIsOn, objServer,
970 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
971 &objServer, &systemBus]() {
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800972 for (auto& busIface : dbusInterfaceMap)
973 {
974 if (busIface.first.first == static_cast<size_t>(busNum))
975 {
976 objServer.remove_interface(busIface.second);
977 }
978 }
Wludzik, Jozefc1136b72020-06-24 11:58:32 +0200979 auto found = busmap.find(busNum);
980 if (found == busmap.end() || found->second == nullptr)
981 {
982 return;
983 }
984 for (auto& device : *(found->second))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800985 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800986 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530987 static_cast<uint32_t>(busNum), device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530988 unknownBusObjectCount, powerIsOn, objServer,
989 systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800990 }
991 });
992 scan->run();
993}
994
James Feist9eb0b582018-04-27 12:15:46 -0700995void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700996 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800997 boost::container::flat_map<
998 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530999 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001000 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301001 sdbusplus::asio::object_server& objServer,
1002 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -08001003{
James Feist6ebf9de2018-05-15 15:01:17 -07001004 static boost::asio::deadline_timer timer(io);
1005 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001006
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001007 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -07001008 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -08001009 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001010 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001011
Nikhil Potaded8884f12019-03-27 13:27:13 -07001012 boost::container::flat_map<size_t, fs::path> busPaths;
1013 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001014 {
James Feist4131aea2018-03-09 09:47:30 -08001015 std::cerr << "unable to find i2c devices\n";
1016 return;
James Feist918e18c2018-02-13 15:51:07 -08001017 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001018
Ed Tanous07d467b2021-02-23 14:48:37 -08001019 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001020 {
1021 i2cBuses.emplace_back(busPath.second);
1022 }
James Feist4131aea2018-03-09 09:47:30 -08001023
James Feist98132792019-07-09 13:29:09 -07001024 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001025 for (auto& [pair, interface] : foundDevices)
1026 {
1027 objServer.remove_interface(interface);
1028 }
1029 foundDevices.clear();
1030
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301031 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301032 i2cBuses, busmap, powerIsOn, objServer, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001033 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001034 {
1035 objServer.remove_interface(busIface.second);
1036 }
James Feist4131aea2018-03-09 09:47:30 -08001037
James Feist6ebf9de2018-05-15 15:01:17 -07001038 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -08001039 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001040
James Feist6ebf9de2018-05-15 15:01:17 -07001041 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001042 std::vector<uint8_t> baseboardFRU;
1043 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001044 {
Helen Huangf69fe902021-02-19 16:18:22 +08001045 // If no device on i2c bus 0, the insertion will happen.
1046 auto bus0 =
1047 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1048 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001049 }
James Feist98132792019-07-09 13:29:09 -07001050 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001051 {
James Feista465ccc2019-02-08 12:51:01 -08001052 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001053 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001054 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301055 devicemap.first, device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301056 unknownBusObjectCount, powerIsOn,
1057 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001058 }
1059 }
1060 });
1061 scan->run();
1062 });
James Feist918e18c2018-02-13 15:51:07 -08001063}
1064
Joshi-Mansid73b7442020-03-27 19:10:21 +05301065// Details with example of Asset Tag Update
1066// To find location of Product Info Area asset tag as per FRU specification
1067// 1. Find product Info area starting offset (*8 - as header will be in
1068// multiple of 8 bytes).
1069// 2. Skip 3 bytes of product info area (like format version, area length,
1070// and language code).
1071// 3. Traverse manufacturer name, product name, product version, & product
1072// serial number, by reading type/length code to reach the Asset Tag.
1073// 4. Update the Asset Tag, reposition the product Info area in multiple of
1074// 8 bytes. Update the Product area length and checksum.
1075
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001076bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301077 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001078 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301079 boost::container::flat_map<
1080 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301081 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001082 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301083 sdbusplus::asio::object_server& objServer,
1084 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301085{
1086 size_t updatePropertyReqLen = updatePropertyReq.length();
1087 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1088 {
1089 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001090 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301091 "Invalid Length "
1092 << updatePropertyReqLen << "\n";
1093 return false;
1094 }
1095
1096 std::vector<uint8_t> fruData;
1097 try
1098 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001099 fruData = getFRUInfo(static_cast<uint8_t>(bus),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301100 static_cast<uint8_t>(address));
1101 }
Patrick Williams83b1e9b2021-10-06 12:47:24 -05001102 catch (const std::invalid_argument& e)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301103 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001104 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301105 return false;
1106 }
1107
1108 if (fruData.empty())
1109 {
1110 return false;
1111 }
1112
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001113 const std::vector<std::string>* fruAreaFieldNames;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301114
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001115 uint8_t fruAreaOffsetFieldValue = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301116 size_t offset = 0;
Ed Tanous07d467b2021-02-23 14:48:37 -08001117 std::string areaName = propertyName.substr(0, propertyName.find('_'));
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001118 std::string propertyNamePrefix = areaName + "_";
Ed Tanous07d467b2021-02-23 14:48:37 -08001119 auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName);
1120 if (it == fruAreaNames.end())
Joshi-Mansid73b7442020-03-27 19:10:21 +05301121 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001122 std::cerr << "Can't parse area name for property " << propertyName
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001123 << " \n";
1124 return false;
1125 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001126 fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin());
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001127 fruAreaOffsetFieldValue =
1128 fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)];
1129 switch (fruAreaToUpdate)
1130 {
1131 case fruAreas::fruAreaChassis:
1132 offset = 3; // chassis part number offset. Skip fixed first 3 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001133 fruAreaFieldNames = &chassisFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001134 break;
1135 case fruAreas::fruAreaBoard:
1136 offset = 6; // board manufacturer offset. Skip fixed first 6 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001137 fruAreaFieldNames = &boardFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001138 break;
1139 case fruAreas::fruAreaProduct:
1140 // Manufacturer name offset. Skip fixed first 3 product fru bytes
1141 // i.e. version, area length and language code
1142 offset = 3;
Ed Tanous07d467b2021-02-23 14:48:37 -08001143 fruAreaFieldNames = &productFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001144 break;
1145 default:
1146 std::cerr << "Don't know how to handle property " << propertyName
1147 << " \n";
1148 return false;
1149 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001150 if (fruAreaOffsetFieldValue == 0)
1151 {
1152 std::cerr << "FRU Area for " << propertyName << " not present \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301153 return false;
1154 }
1155
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001156 size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001157 size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize;
1158 size_t fruAreaEnd = fruAreaStart + fruAreaSize;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001159 size_t fruDataIter = fruAreaStart + offset;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001160 size_t skipToFRUUpdateField = 0;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001161 ssize_t fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301162
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001163 bool found = false;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001164 for (auto& field : *fruAreaFieldNames)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301165 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001166 skipToFRUUpdateField++;
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001167 if (propertyName == propertyNamePrefix + field)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301168 {
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001169 found = true;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301170 break;
1171 }
1172 }
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001173 if (!found)
1174 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001175 std::size_t pos = propertyName.find(fruCustomFieldName);
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001176 if (pos == std::string::npos)
1177 {
1178 std::cerr << "PropertyName doesn't exist in FRU Area Vectors: "
1179 << propertyName << "\n";
1180 return false;
1181 }
1182 std::string fieldNumStr =
Ed Tanous07d467b2021-02-23 14:48:37 -08001183 propertyName.substr(pos + fruCustomFieldName.length());
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001184 size_t fieldNum = std::stoi(fieldNumStr);
1185 if (fieldNum == 0)
1186 {
1187 std::cerr << "PropertyName not recognized: " << propertyName
1188 << "\n";
1189 return false;
1190 }
1191 skipToFRUUpdateField += fieldNum;
1192 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301193
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001194 for (size_t i = 1; i < skipToFRUUpdateField; i++)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301195 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001196 fieldLength = getFieldLength(fruData[fruDataIter]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001197 if (fieldLength < 0)
1198 {
1199 break;
1200 }
1201 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301202 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001203 size_t fruUpdateFieldLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301204
1205 // Push post update fru field bytes to a vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001206 fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001207 if (fieldLength < 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301208 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001209 std::cerr << "Property " << propertyName << " not present \n";
1210 return false;
1211 }
1212 fruDataIter += 1 + fieldLength;
1213 size_t restFRUFieldsLoc = fruDataIter;
1214 size_t endOfFieldsLoc = 0;
1215 while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0)
1216 {
1217 if (fruDataIter >= (fruAreaStart + fruAreaSize))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301218 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001219 fruDataIter = fruAreaStart + fruAreaSize;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301220 break;
1221 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001222 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301223 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001224 endOfFieldsLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301225
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001226 std::vector<uint8_t> restFRUAreaFieldsData;
1227 std::copy_n(fruData.begin() + restFRUFieldsLoc,
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001228 endOfFieldsLoc - restFRUFieldsLoc + 1,
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001229 std::back_inserter(restFRUAreaFieldsData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301230
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001231 // Push post update fru areas if any
1232 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001233 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1234 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001235 {
1236 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001237 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001238 if ((fruAreaLoc > endOfFieldsLoc) &&
1239 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1240 {
1241 nextFRUAreaLoc = fruAreaLoc;
1242 }
1243 }
1244 std::vector<uint8_t> restFRUAreasData;
1245 if (nextFRUAreaLoc)
1246 {
1247 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1248 fruData.size() - nextFRUAreaLoc,
1249 std::back_inserter(restFRUAreasData));
1250 }
1251
1252 // check FRU area size
1253 size_t fruAreaDataSize =
1254 ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size());
1255 size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize;
1256 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1257 {
1258
1259#ifdef ENABLE_FRU_AREA_RESIZE
1260 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1261 // round size to 8-byte blocks
1262 newFRUAreaSize =
1263 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
1264 size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize;
1265 fruData.resize(newFRUDataSize);
1266 fruAreaSize = newFRUAreaSize;
1267 fruAreaEnd = fruAreaStart + fruAreaSize;
1268#else
1269 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1270 << " should not be greater than available FRU area size: "
1271 << fruAreaAvailableSize << "\n";
1272 return false;
1273#endif // ENABLE_FRU_AREA_RESIZE
1274 }
1275
Joshi-Mansid73b7442020-03-27 19:10:21 +05301276 // write new requested property field length and data
1277 constexpr uint8_t newTypeLenMask = 0xC0;
1278 fruData[fruUpdateFieldLoc] =
1279 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
1280 fruUpdateFieldLoc++;
1281 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
1282 fruData.begin() + fruUpdateFieldLoc);
1283
1284 // Copy remaining data to main fru area - post updated fru field vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001285 restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen;
1286 size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size();
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001287 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
1288 fruData.begin() + restFRUFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301289
1290 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001291 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
1292 fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301293
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001294#ifdef ENABLE_FRU_AREA_RESIZE
1295 ++nextFRUAreaNewLoc;
1296 ssize_t nextFRUAreaOffsetDiff =
1297 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
1298 // Append rest FRU Areas if size changed and there were other sections after
1299 // updated one
1300 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1301 {
1302 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1303 fruData.begin() + nextFRUAreaNewLoc);
1304 // Update Common Header
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001305 for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord;
1306 fruArea++)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001307 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001308 unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001309 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
1310 if (curFRUAreaOffset > fruAreaOffsetFieldValue)
1311 {
1312 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1313 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1314 }
1315 }
1316 // Calculate new checksum
1317 std::vector<uint8_t> headerFRUData;
1318 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1319 size_t checksumVal = calculateChecksum(headerFRUData);
1320 fruData[7] = static_cast<uint8_t>(checksumVal);
1321 // fill zeros if FRU Area size decreased
1322 if (nextFRUAreaOffsetDiff < 0)
1323 {
1324 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1325 restFRUAreasData.size(),
1326 fruData.end(), 0);
1327 }
1328 }
1329#else
1330 // this is to avoid "unused variable" warning
1331 (void)nextFRUAreaNewLoc;
1332#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301333 if (fruData.empty())
1334 {
1335 return false;
1336 }
1337
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001338 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301339 fruData))
1340 {
1341 return false;
1342 }
1343
1344 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301345 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1346 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301347 return true;
1348}
1349
James Feist98132792019-07-09 13:29:09 -07001350int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001351{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301352 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1353 sdbusplus::asio::object_server objServer(systemBus);
1354
Kumar Thangavel41a90512021-11-24 15:44:20 +05301355 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301356 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001357 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001358 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001359 std::vector<fs::path> i2cBuses;
1360
James Feista3c180a2018-08-09 16:06:04 -07001361 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001362 {
1363 std::cerr << "unable to find i2c devices\n";
1364 return 1;
1365 }
James Feist3cb5fec2018-01-23 14:41:51 -08001366
Patrick Venture11f1ff42019-08-01 10:42:12 -07001367 // check for and load blacklist with initial buses.
1368 loadBlacklist(blacklistPath);
1369
Vijay Khemka065f6d92018-12-18 10:37:47 -08001370 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001371
James Feist6ebf9de2018-05-15 15:01:17 -07001372 // this is a map with keys of pair(bus number, address) and values of
1373 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001374 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001375 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1376 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001377
James Feist9eb0b582018-04-27 12:15:46 -07001378 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1379 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1380 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001381
Kumar Thangavel41a90512021-11-24 15:44:20 +05301382 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301383 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1384 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301385 });
James Feist2a9d6db2018-04-27 15:48:28 -07001386
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001387 iface->register_method("ReScanBus", [&](uint8_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301388 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301389 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001390 });
1391
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001392 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001393
1394 iface->register_method("WriteFru", [&](const uint8_t bus,
1395 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001396 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001397 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001398 {
James Feistddb78302018-09-06 11:45:42 -07001399 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001400 return;
1401 }
1402 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301403 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1404 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001405 });
James Feist9eb0b582018-04-27 12:15:46 -07001406 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001407
James Feist9eb0b582018-04-27 12:15:46 -07001408 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -08001409 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -08001410 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001411 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001412 std::string,
1413 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001414 values;
1415 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001416 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001417 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001418 {
James Feistcb5661d2020-06-12 15:05:01 -07001419 powerIsOn = boost::ends_with(
1420 std::get<std::string>(findState->second), "Running");
James Feist0eeb79c2019-10-09 13:35:29 -07001421 }
James Feist6ebf9de2018-05-15 15:01:17 -07001422
James Feistcb5661d2020-06-12 15:05:01 -07001423 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001424 {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301425 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301426 powerIsOn, objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001427 }
James Feist918e18c2018-02-13 15:51:07 -08001428 };
James Feist9eb0b582018-04-27 12:15:46 -07001429
1430 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -08001431 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001432 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001433 "openbmc_project/state/"
1434 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001435 eventHandler);
1436
James Feist4131aea2018-03-09 09:47:30 -08001437 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001438 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -08001439 std::array<char, 4096> readBuffer;
1440 std::string pendingBuffer;
1441 // monitor for new i2c devices
1442 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1443 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001444 watchI2cBusses = [&](const boost::system::error_code& ec,
Ed Tanous07d467b2021-02-23 14:48:37 -08001445 std::size_t bytesTransferred) {
James Feist4131aea2018-03-09 09:47:30 -08001446 if (ec)
1447 {
1448 std::cout << "Callback Error " << ec << "\n";
1449 return;
1450 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001451 pendingBuffer += std::string(readBuffer.data(), bytesTransferred);
James Feist4131aea2018-03-09 09:47:30 -08001452 while (pendingBuffer.size() > sizeof(inotify_event))
1453 {
James Feista465ccc2019-02-08 12:51:01 -08001454 const inotify_event* iEvent =
1455 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -08001456 pendingBuffer.data());
1457 switch (iEvent->mask)
1458 {
James Feist9eb0b582018-04-27 12:15:46 -07001459 case IN_CREATE:
1460 case IN_MOVED_TO:
1461 case IN_DELETE:
James Feist5d7f4692020-06-17 18:22:33 -07001462 std::string name(iEvent->name);
1463 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001464 {
James Feist5d7f4692020-06-17 18:22:33 -07001465 int bus = busStrToInt(name);
1466 if (bus < 0)
1467 {
1468 std::cerr << "Could not parse bus " << name
1469 << "\n";
1470 continue;
1471 }
1472 rescanOneBus(busMap, static_cast<uint8_t>(bus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301473 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301474 unknownBusObjectCount, powerIsOn,
1475 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001476 }
James Feist4131aea2018-03-09 09:47:30 -08001477 }
1478
1479 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
1480 }
James Feist6ebf9de2018-05-15 15:01:17 -07001481
James Feist4131aea2018-03-09 09:47:30 -08001482 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1483 watchI2cBusses);
1484 };
1485
1486 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001487 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301488 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1489 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001490
James Feist3cb5fec2018-01-23 14:41:51 -08001491 io.run();
1492 return 0;
1493}