blob: 1bb68b79053110bd9d0fae45c05476fc418c5a1f [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;
88auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
89auto objServer = sdbusplus::asio::object_server(systemBus);
90
Andrei Kartashev2a7e3952020-09-02 20:32:26 +030091uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter,
92 std::vector<uint8_t>::const_iterator end);
Andrei Kartashev2f0de172020-08-13 14:29:40 +030093bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +053094 const std::string& assetTag, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -080095 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +053096 boost::container::flat_map<
97 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +053098 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +053099 size_t& unknownBusObjectCount, bool powerIsOn);
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
James Feist8a983922019-10-24 17:11:56 -0700175static void makeProbeInterface(size_t bus, size_t address)
176{
177 if (isMuxBus(bus))
178 {
179 return; // the mux buses are random, no need to publish
180 }
181 auto [it, success] = foundDevices.emplace(
182 std::make_pair(bus, address),
183 objServer.add_interface(
184 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
185 std::to_string(address),
186 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
187 if (!success)
188 {
189 return; // already added
190 }
191 it->second->register_property("Bus", bus);
192 it->second->register_property("Address", address);
193 it->second->initialize();
194}
195
Vijay Khemka2d681f62018-11-06 15:51:00 -0800196static int isDevice16Bit(int file)
197{
198 /* Get first byte */
199 int byte1 = i2c_smbus_read_byte_data(file, 0);
200 if (byte1 < 0)
201 {
202 return byte1;
203 }
204 /* Read 7 more bytes, it will read same first byte in case of
205 * 8 bit but it will read next byte in case of 16 bit
206 */
207 for (int i = 0; i < 7; i++)
208 {
209 int byte2 = i2c_smbus_read_byte_data(file, 0);
210 if (byte2 < 0)
211 {
212 return byte2;
213 }
214 if (byte2 != byte1)
215 {
216 return 1;
217 }
218 }
219 return 0;
220}
221
Xiang Liu2801a702020-01-20 14:29:34 -0800222// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
223// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800224static int i2cSmbusWriteThenRead(int file, uint16_t address,
225 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
226 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800227{
Ed Tanous07d467b2021-02-23 14:48:37 -0800228 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
229 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800230 {
231 return -1;
232 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800233
Xiang Liu2801a702020-01-20 14:29:34 -0800234#define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2
235 struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT];
236 struct i2c_rdwr_ioctl_data rdwr;
237
238 msgs[0].addr = address;
239 msgs[0].flags = 0;
240 msgs[0].len = toSlaveBufLen;
241 msgs[0].buf = toSlaveBuf;
242 msgs[1].addr = address;
243 msgs[1].flags = I2C_M_RD;
244 msgs[1].len = fromSlaveBufLen;
245 msgs[1].buf = fromSlaveBuf;
246
247 rdwr.msgs = msgs;
248 rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT;
249
250 int ret = ioctl(file, I2C_RDWR, &rdwr);
251
252 return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? ret : -1;
253}
254
255static int64_t readBlockData(int flag, int file, uint16_t address,
256 uint16_t offset, uint8_t len, uint8_t* buf)
257{
Vijay Khemka2d681f62018-11-06 15:51:00 -0800258 if (flag == 0)
259 {
Xiang Liu2801a702020-01-20 14:29:34 -0800260 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
261 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800262 }
263
Xiang Liu2801a702020-01-20 14:29:34 -0800264 offset = htobe16(offset);
Ed Tanous07d467b2021-02-23 14:48:37 -0800265 return i2cSmbusWriteThenRead(
Xiang Liu2801a702020-01-20 14:29:34 -0800266 file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800267}
268
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700269// TODO: This code is very similar to the non-eeprom version and can be merged
270// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300271static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700272{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700273 auto path = getEepromPath(bus, address);
274
275 int file = open(path.c_str(), O_RDONLY);
276 if (file < 0)
277 {
278 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700279 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700280 }
281
Patrick Venturebaba7672019-10-26 09:26:41 -0700282 std::string errorMessage = "eeprom at " + std::to_string(bus) +
283 " address " + std::to_string(address);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300284 std::vector<uint8_t> device = readFRUContents(
Xiang Liu2801a702020-01-20 14:29:34 -0800285 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700286
287 close(file);
288 return device;
289}
290
Ed Tanous07d467b2021-02-23 14:48:37 -0800291std::set<int> findI2CEeproms(int i2cBus,
292 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700293{
294 std::set<int> foundList;
295
296 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
297
298 // For each file listed under the i2c device
299 // NOTE: This should be faster than just checking for each possible address
300 // path.
301 for (const auto& p : fs::directory_iterator(path))
302 {
303 const std::string node = p.path().string();
304 std::smatch m;
305 bool found =
306 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
307
308 if (!found)
309 {
310 continue;
311 }
312 if (m.size() != 2)
313 {
314 std::cerr << "regex didn't capture\n";
315 continue;
316 }
317
318 std::ssub_match subMatch = m[1];
319 std::string addressString = subMatch.str();
320
321 std::size_t ignored;
322 const int hexBase = 16;
323 int address = std::stoi(addressString, &ignored, hexBase);
324
325 const std::string eeprom = node + "/eeprom";
326
327 try
328 {
329 if (!fs::exists(eeprom))
330 {
331 continue;
332 }
333 }
334 catch (...)
335 {
336 continue;
337 }
338
339 // There is an eeprom file at this address, it may have invalid
340 // contents, but we found it.
341 foundList.insert(address);
342
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300343 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700344 if (!device.empty())
345 {
346 devices->emplace(address, device);
347 }
348 }
349
350 return foundList;
351}
352
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300353int getBusFRUs(int file, int first, int last, int bus,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530354 std::shared_ptr<DeviceMap> devices, bool powerIsOn)
James Feist3cb5fec2018-01-23 14:41:51 -0800355{
James Feist3cb5fec2018-01-23 14:41:51 -0800356
James Feist26c27ad2018-07-25 15:09:40 -0700357 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700358 // NOTE: When reading the devices raw on the bus, it can interfere with
359 // the driver's ability to operate, therefore read eeproms first before
360 // scanning for devices without drivers. Several experiments were run
361 // and it was determined that if there were any devices on the bus
362 // before the eeprom was hit and read, the eeprom driver wouldn't open
363 // while the bus device was open. An experiment was not performed to see
364 // if this issue was resolved if the i2c bus device was closed, but
365 // hexdumps of the eeprom later were successful.
366
367 // Scan for i2c eeproms loaded on this bus.
368 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800369 std::set<size_t>& failedItems = failedAddresses[bus];
370
371 std::set<size_t>* rootFailures = nullptr;
372 int rootBus = getRootBus(bus);
373
374 if (rootBus >= 0)
375 {
376 rootFailures = &(failedAddresses[rootBus]);
377 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700378
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530379 constexpr int startSkipSlaveAddr = 0;
380 constexpr int endSkipSlaveAddr = 12;
381
James Feist26c27ad2018-07-25 15:09:40 -0700382 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800383 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700384 if (skipList.find(ii) != skipList.end())
385 {
386 continue;
387 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530388 // skipping since no device is present in this range
389 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
390 {
391 continue;
392 }
James Feist26c27ad2018-07-25 15:09:40 -0700393 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530394 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800395 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300396 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700397 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700398 continue;
399 }
400 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800401 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700402 {
403 continue;
404 }
James Feist3cb5fec2018-01-23 14:41:51 -0800405
Ed Tanous07d467b2021-02-23 14:48:37 -0800406 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700407 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700408 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700409 << "\n";
410 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800411
James Feist8a983922019-10-24 17:11:56 -0700412 makeProbeInterface(bus, ii);
413
James Feist7972bb92019-11-13 15:59:24 -0800414 if (failedItems.find(ii) != failedItems.end())
415 {
416 // if we failed to read it once, unlikely we can read it later
417 continue;
418 }
419
420 if (rootFailures != nullptr)
421 {
422 if (rootFailures->find(ii) != rootFailures->end())
423 {
424 continue;
425 }
426 }
427
Vijay Khemka2d681f62018-11-06 15:51:00 -0800428 /* Check for Device type if it is 8 bit or 16 bit */
429 int flag = isDevice16Bit(file);
430 if (flag < 0)
431 {
432 std::cerr << "failed to read bus " << bus << " address " << ii
433 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700434 if (powerIsOn)
435 {
436 failedItems.insert(ii);
437 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800438 continue;
439 }
440
Patrick Venturebaba7672019-10-26 09:26:41 -0700441 std::string errorMessage =
442 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300443 std::vector<uint8_t> device =
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300444 readFRUContents(flag, file, static_cast<uint16_t>(ii),
Xiang Liu2801a702020-01-20 14:29:34 -0800445 readBlockData, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700446 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700447 {
James Feist26c27ad2018-07-25 15:09:40 -0700448 continue;
449 }
James Feist26c27ad2018-07-25 15:09:40 -0700450
Patrick Venture786f1792019-08-05 16:33:44 -0700451 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800452 }
James Feist26c27ad2018-07-25 15:09:40 -0700453 return 1;
454 });
455 std::future_status status =
456 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
457 if (status == std::future_status::timeout)
458 {
459 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700460 if (powerIsOn)
461 {
462 busBlacklist.insert(bus);
463 }
James Feist444830e2019-04-05 08:38:16 -0700464 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700465 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800466 }
467
James Feist444830e2019-04-05 08:38:16 -0700468 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700469 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800470}
471
Patrick Venture11f1ff42019-08-01 10:42:12 -0700472void loadBlacklist(const char* path)
473{
474 std::ifstream blacklistStream(path);
475 if (!blacklistStream.good())
476 {
477 // File is optional.
478 std::cerr << "Cannot open blacklist file.\n\n";
479 return;
480 }
481
482 nlohmann::json data =
483 nlohmann::json::parse(blacklistStream, nullptr, false);
484 if (data.is_discarded())
485 {
486 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
487 "exiting\n";
488 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700489 }
490
491 // It's expected to have at least one field, "buses" that is an array of the
492 // buses by integer. Allow for future options to exclude further aspects,
493 // such as specific addresses or ranges.
494 if (data.type() != nlohmann::json::value_t::object)
495 {
496 std::cerr << "Illegal blacklist, expected to read dictionary\n";
497 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700498 }
499
500 // If buses field is missing, that's fine.
501 if (data.count("buses") == 1)
502 {
503 // Parse the buses array after a little validation.
504 auto buses = data.at("buses");
505 if (buses.type() != nlohmann::json::value_t::array)
506 {
507 // Buses field present but invalid, therefore this is an error.
508 std::cerr << "Invalid contents for blacklist buses field\n";
509 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700510 }
511
512 // Catch exception here for type mis-match.
513 try
514 {
515 for (const auto& bus : buses)
516 {
517 busBlacklist.insert(bus.get<size_t>());
518 }
519 }
520 catch (const nlohmann::detail::type_error& e)
521 {
522 // Type mis-match is a critical error.
523 std::cerr << "Invalid bus type: " << e.what() << "\n";
524 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700525 }
526 }
527
528 return;
529}
530
Ed Tanous07d467b2021-02-23 14:48:37 -0800531static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530532 BusMap& busmap, bool powerIsOn)
James Feist3cb5fec2018-01-23 14:41:51 -0800533{
James Feista465ccc2019-02-08 12:51:01 -0800534 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800535 {
James Feist5d7f4692020-06-17 18:22:33 -0700536 int bus = busStrToInt(i2cBus);
James Feist0c3980a2019-12-19 11:09:27 -0800537
James Feist5d7f4692020-06-17 18:22:33 -0700538 if (bus < 0)
539 {
540 std::cerr << "Cannot translate " << i2cBus << " to int\n";
541 continue;
542 }
James Feist444830e2019-04-05 08:38:16 -0700543 if (busBlacklist.find(bus) != busBlacklist.end())
544 {
545 continue; // skip previously failed busses
546 }
James Feist3cb5fec2018-01-23 14:41:51 -0800547
James Feist0c3980a2019-12-19 11:09:27 -0800548 int rootBus = getRootBus(bus);
549 if (busBlacklist.find(rootBus) != busBlacklist.end())
550 {
551 continue;
552 }
553
James Feist3cb5fec2018-01-23 14:41:51 -0800554 auto file = open(i2cBus.c_str(), O_RDWR);
555 if (file < 0)
556 {
557 std::cerr << "unable to open i2c device " << i2cBus.string()
558 << "\n";
559 continue;
560 }
561 unsigned long funcs = 0;
562
563 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
564 {
565 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700566 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800567 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700568 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800569 continue;
570 }
571 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
572 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
573 {
574 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
575 << bus << "\n";
576 continue;
577 }
James Feist98132792019-07-09 13:29:09 -0700578 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800579 device = std::make_shared<DeviceMap>();
580
Nikhil Potaded8884f12019-03-27 13:27:13 -0700581 // i2cdetect by default uses the range 0x03 to 0x77, as
582 // this is what we have tested with, use this range. Could be
583 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800584 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800585 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700586 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800587 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700588
589 // fd is closed in this function in case the bus locks up
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530590 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700591
Ed Tanous07d467b2021-02-23 14:48:37 -0800592 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800593 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700594 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800595 }
James Feist3cb5fec2018-01-23 14:41:51 -0800596 }
James Feist3cb5fec2018-01-23 14:41:51 -0800597}
598
James Feist6ebf9de2018-05-15 15:01:17 -0700599// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700600struct FindDevicesWithCallback :
601 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700602{
James Feista465ccc2019-02-08 12:51:01 -0800603 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530604 BusMap& busmap, bool powerIsOn,
James Feista465ccc2019-02-08 12:51:01 -0800605 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700606 _i2cBuses(i2cBuses),
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530607 _busMap(busmap), _powerIsOn(powerIsOn), _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700608 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700609 ~FindDevicesWithCallback()
610 {
611 _callback();
612 }
613 void run()
614 {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530615 findI2CDevices(_i2cBuses, _busMap, _powerIsOn);
James Feist6ebf9de2018-05-15 15:01:17 -0700616 }
617
James Feista465ccc2019-02-08 12:51:01 -0800618 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800619 BusMap& _busMap;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530620 bool _powerIsOn;
James Feist6ebf9de2018-05-15 15:01:17 -0700621 std::function<void(void)> _callback;
622};
623
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300624std::vector<uint8_t>& getFRUInfo(const uint8_t& bus, const uint8_t& address)
Nikhil Potaded8884f12019-03-27 13:27:13 -0700625{
626 auto deviceMap = busMap.find(bus);
627 if (deviceMap == busMap.end())
628 {
629 throw std::invalid_argument("Invalid Bus.");
630 }
631 auto device = deviceMap->second->find(address);
632 if (device == deviceMap->second->end())
633 {
634 throw std::invalid_argument("Invalid Address.");
635 }
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300636 std::vector<uint8_t>& ret = device->second;
Nikhil Potaded8884f12019-03-27 13:27:13 -0700637
638 return ret;
639}
640
Ed Tanous07d467b2021-02-23 14:48:37 -0800641void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300642 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800643 boost::container::flat_map<
644 std::pair<size_t, size_t>,
645 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530646 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
647 bool powerIsOn)
James Feist3cb5fec2018-01-23 14:41:51 -0800648{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300649 boost::container::flat_map<std::string, std::string> formattedFRU;
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300650 resCodes res = formatFRU(device, formattedFRU);
651 if (res == resCodes::resErr)
James Feist3cb5fec2018-01-23 14:41:51 -0800652 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300653 std::cerr << "failed to parse FRU for device at bus " << bus
Patrick Ventureb755c832019-08-07 11:09:14 -0700654 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800655 return;
656 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800657 if (res == resCodes::resWarn)
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300658 {
659 std::cerr << "there were warnings while parsing FRU for device at bus "
660 << bus << " address " << address << "\n";
661 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700662
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300663 auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800664 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700665 // Not found under Board section or an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300666 if (productNameFind == formattedFRU.end() ||
Patrick Venture96cdaef2019-07-30 13:30:52 -0700667 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800668 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300669 productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800670 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700671 // Found under Product section and not an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300672 if (productNameFind != formattedFRU.end() &&
Patrick Venture96cdaef2019-07-30 13:30:52 -0700673 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800674 {
675 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800676 std::regex illegalObject("[^A-Za-z0-9_]");
677 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800678 }
679 else
680 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800681 productName = "UNKNOWN" + std::to_string(unknownBusObjectCount);
682 unknownBusObjectCount++;
James Feist3cb5fec2018-01-23 14:41:51 -0800683 }
684
James Feist918e18c2018-02-13 15:51:07 -0800685 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800686 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700687 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800688 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700689 int highest = -1;
690 bool found = false;
691
James Feista465ccc2019-02-08 12:51:01 -0800692 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800693 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700694 std::string path = busIface.second->get_object_path();
695 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
James Feist918e18c2018-02-13 15:51:07 -0800696 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700697 if (isMuxBus(bus) && address == busIface.first.second &&
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300698 (getFRUInfo(static_cast<uint8_t>(busIface.first.first),
James Feist98132792019-07-09 13:29:09 -0700699 static_cast<uint8_t>(busIface.first.second)) ==
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300700 getFRUInfo(static_cast<uint8_t>(bus),
James Feist98132792019-07-09 13:29:09 -0700701 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700702 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700703 // This device is already added to the lower numbered bus,
704 // do not replicate it.
705 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700706 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700707
708 // Check if the match named has extra information.
709 found = true;
Ed Tanous07d467b2021-02-23 14:48:37 -0800710 std::smatch baseMatch;
Patrick Venture015fb0a2019-08-16 09:33:31 -0700711
712 bool match = std::regex_match(
Ed Tanous07d467b2021-02-23 14:48:37 -0800713 path, baseMatch, std::regex(productName + "_(\\d+)$"));
Patrick Venture015fb0a2019-08-16 09:33:31 -0700714 if (match)
James Feist79e9c0b2018-03-15 15:21:17 -0700715 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800716 if (baseMatch.size() == 2)
Patrick Venture015fb0a2019-08-16 09:33:31 -0700717 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800718 std::ssub_match baseSubMatch = baseMatch[1];
719 std::string base = baseSubMatch.str();
Patrick Venture015fb0a2019-08-16 09:33:31 -0700720
721 int value = std::stoi(base);
722 highest = (value > highest) ? value : highest;
723 }
James Feist79e9c0b2018-03-15 15:21:17 -0700724 }
James Feist918e18c2018-02-13 15:51:07 -0800725 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700726 } // end searching objects
727
728 if (found)
729 {
730 // We found something with the same name. If highest was still -1,
731 // it means this new entry will be _0.
732 productName += "_";
733 productName += std::to_string(++highest);
James Feist918e18c2018-02-13 15:51:07 -0800734 }
735 }
James Feist3cb5fec2018-01-23 14:41:51 -0800736
James Feist9eb0b582018-04-27 12:15:46 -0700737 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
738 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
739 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
740
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300741 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800742 {
James Feist9eb0b582018-04-27 12:15:46 -0700743
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700744 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800745 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530746 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700747 {
748 continue;
749 }
750 std::string key =
Ed Tanous07d467b2021-02-23 14:48:37 -0800751 std::regex_replace(property.first, nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530752
753 if (property.first == "PRODUCT_ASSET_TAG")
754 {
755 std::string propertyName = property.first;
756 iface->register_property(
757 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530758 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530759 &unknownBusObjectCount,
760 &powerIsOn](const std::string& req, std::string& resp) {
Ed Tanous07d467b2021-02-23 14:48:37 -0800761 if (strcmp(req.c_str(), resp.c_str()) != 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +0530762 {
763 // call the method which will update
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300764 if (updateFRUProperty(req, bus, address, propertyName,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530765 dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530766 unknownBusObjectCount, powerIsOn))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530767 {
768 resp = req;
769 }
770 else
771 {
772 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300773 "FRU property update failed.");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530774 }
775 }
776 return 1;
777 });
778 }
779 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700780 {
781 std::cerr << "illegal key: " << key << "\n";
782 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800783 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700784 {
785 std::cout << property.first << ": " << property.second << "\n";
786 }
James Feist3cb5fec2018-01-23 14:41:51 -0800787 }
James Feist2a9d6db2018-04-27 15:48:28 -0700788
789 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700790 iface->register_property("BUS", bus);
791 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700792
James Feist9eb0b582018-04-27 12:15:46 -0700793 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800794}
795
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300796static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800797{
798 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800799 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300800 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800801 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300802 baseboardFRUFile.seekg(0, std::ios_base::end);
803 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
804 baseboardFRU.resize(fileSize);
805 baseboardFRUFile.seekg(0, std::ios_base::beg);
806 baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()),
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300807 fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800808 }
809 else
810 {
811 return false;
812 }
813 return true;
814}
815
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300816bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700817{
818 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800819 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700820 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300821 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700822 return false;
823 }
824 // verify legal fru by running it through fru parsing logic
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300825 if (formatFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700826 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300827 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700828 return false;
829 }
830 // baseboard fru
831 if (bus == 0 && address == 0)
832 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800833 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700834 if (!file.good())
835 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800836 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700837 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700838 return false;
839 }
James Feista465ccc2019-02-08 12:51:01 -0800840 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700841 return file.good();
842 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800843
844 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700845 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800846 auto path = getEepromPath(bus, address);
847 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
848 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700849 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800850 std::cerr << "unable to open i2c device " << path << "\n";
851 throw DBusInternalError();
852 return false;
853 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700854
Ed Tanous07d467b2021-02-23 14:48:37 -0800855 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
856 if (writtenBytes < 0)
857 {
858 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700859 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700860 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700861 return false;
862 }
863
Ed Tanous07d467b2021-02-23 14:48:37 -0800864 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700865 return true;
866 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800867
868 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
869
870 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
871 if (file < 0)
872 {
873 std::cerr << "unable to open i2c device " << i2cBus << "\n";
874 throw DBusInternalError();
875 return false;
876 }
877 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
878 {
879 std::cerr << "unable to set device address\n";
880 close(file);
881 throw DBusInternalError();
882 return false;
883 }
884
885 constexpr const size_t retryMax = 2;
886 uint16_t index = 0;
887 size_t retries = retryMax;
888 while (index < fru.size())
889 {
890 if ((index && ((index % (maxEepromPageIndex + 1)) == 0)) &&
891 (retries == retryMax))
892 {
893 // The 4K EEPROM only uses the A2 and A1 device address bits
894 // with the third bit being a memory page address bit.
895 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
896 {
897 std::cerr << "unable to set device address\n";
898 close(file);
899 throw DBusInternalError();
900 return false;
901 }
902 }
903
904 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
905 fru[index]) < 0)
906 {
907 if (!retries--)
908 {
909 std::cerr << "error writing fru: " << strerror(errno) << "\n";
910 close(file);
911 throw DBusInternalError();
912 return false;
913 }
914 }
915 else
916 {
917 retries = retryMax;
918 index++;
919 }
920 // most eeproms require 5-10ms between writes
921 std::this_thread::sleep_for(std::chrono::milliseconds(10));
922 }
923 close(file);
924 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700925}
926
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800927void rescanOneBus(
928 BusMap& busmap, uint8_t busNum,
929 boost::container::flat_map<
930 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700931 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530932 bool dbusCall, size_t& unknownBusObjectCount, bool powerIsOn)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800933{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800934 for (auto& [pair, interface] : foundDevices)
935 {
936 if (pair.first == static_cast<size_t>(busNum))
937 {
938 objServer.remove_interface(interface);
939 foundDevices.erase(pair);
940 }
941 }
942
James Feist5d7f4692020-06-17 18:22:33 -0700943 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
944 if (!fs::exists(busPath))
945 {
946 if (dbusCall)
947 {
948 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
949 << "\n";
950 throw std::invalid_argument("Invalid Bus.");
951 }
952 return;
953 }
954
955 std::vector<fs::path> i2cBuses;
956 i2cBuses.emplace_back(busPath);
957
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800958 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530959 i2cBuses, busmap, powerIsOn,
960 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount,
961 &powerIsOn]() {
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800962 for (auto& busIface : dbusInterfaceMap)
963 {
964 if (busIface.first.first == static_cast<size_t>(busNum))
965 {
966 objServer.remove_interface(busIface.second);
967 }
968 }
Wludzik, Jozefc1136b72020-06-24 11:58:32 +0200969 auto found = busmap.find(busNum);
970 if (found == busmap.end() || found->second == nullptr)
971 {
972 return;
973 }
974 for (auto& device : *(found->second))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800975 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800976 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530977 static_cast<uint32_t>(busNum), device.first,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530978 unknownBusObjectCount, powerIsOn);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800979 }
980 });
981 scan->run();
982}
983
James Feist9eb0b582018-04-27 12:15:46 -0700984void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700985 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800986 boost::container::flat_map<
987 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530988 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530989 size_t& unknownBusObjectCount, bool powerIsOn)
James Feist918e18c2018-02-13 15:51:07 -0800990{
James Feist6ebf9de2018-05-15 15:01:17 -0700991 static boost::asio::deadline_timer timer(io);
992 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800993
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500994 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -0700995 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -0800996 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -0800997 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800998
Nikhil Potaded8884f12019-03-27 13:27:13 -0700999 boost::container::flat_map<size_t, fs::path> busPaths;
1000 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001001 {
James Feist4131aea2018-03-09 09:47:30 -08001002 std::cerr << "unable to find i2c devices\n";
1003 return;
James Feist918e18c2018-02-13 15:51:07 -08001004 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001005
Ed Tanous07d467b2021-02-23 14:48:37 -08001006 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001007 {
1008 i2cBuses.emplace_back(busPath.second);
1009 }
James Feist4131aea2018-03-09 09:47:30 -08001010
James Feist98132792019-07-09 13:29:09 -07001011 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001012 for (auto& [pair, interface] : foundDevices)
1013 {
1014 objServer.remove_interface(interface);
1015 }
1016 foundDevices.clear();
1017
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301018 auto scan = std::make_shared<FindDevicesWithCallback>(
1019 i2cBuses, busmap, powerIsOn, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001020 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001021 {
1022 objServer.remove_interface(busIface.second);
1023 }
James Feist4131aea2018-03-09 09:47:30 -08001024
James Feist6ebf9de2018-05-15 15:01:17 -07001025 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -08001026 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001027
James Feist6ebf9de2018-05-15 15:01:17 -07001028 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001029 std::vector<uint8_t> baseboardFRU;
1030 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001031 {
Helen Huangf69fe902021-02-19 16:18:22 +08001032 // If no device on i2c bus 0, the insertion will happen.
1033 auto bus0 =
1034 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1035 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001036 }
James Feist98132792019-07-09 13:29:09 -07001037 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001038 {
James Feista465ccc2019-02-08 12:51:01 -08001039 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001040 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001041 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301042 devicemap.first, device.first,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301043 unknownBusObjectCount, powerIsOn);
James Feist6ebf9de2018-05-15 15:01:17 -07001044 }
1045 }
1046 });
1047 scan->run();
1048 });
James Feist918e18c2018-02-13 15:51:07 -08001049}
1050
Joshi-Mansid73b7442020-03-27 19:10:21 +05301051// Details with example of Asset Tag Update
1052// To find location of Product Info Area asset tag as per FRU specification
1053// 1. Find product Info area starting offset (*8 - as header will be in
1054// multiple of 8 bytes).
1055// 2. Skip 3 bytes of product info area (like format version, area length,
1056// and language code).
1057// 3. Traverse manufacturer name, product name, product version, & product
1058// serial number, by reading type/length code to reach the Asset Tag.
1059// 4. Update the Asset Tag, reposition the product Info area in multiple of
1060// 8 bytes. Update the Product area length and checksum.
1061
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001062bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301063 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001064 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301065 boost::container::flat_map<
1066 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301067 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301068 size_t& unknownBusObjectCount, bool powerIsOn)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301069{
1070 size_t updatePropertyReqLen = updatePropertyReq.length();
1071 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1072 {
1073 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001074 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301075 "Invalid Length "
1076 << updatePropertyReqLen << "\n";
1077 return false;
1078 }
1079
1080 std::vector<uint8_t> fruData;
1081 try
1082 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001083 fruData = getFRUInfo(static_cast<uint8_t>(bus),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301084 static_cast<uint8_t>(address));
1085 }
Patrick Williams83b1e9b2021-10-06 12:47:24 -05001086 catch (const std::invalid_argument& e)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301087 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001088 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301089 return false;
1090 }
1091
1092 if (fruData.empty())
1093 {
1094 return false;
1095 }
1096
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001097 const std::vector<std::string>* fruAreaFieldNames;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301098
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001099 uint8_t fruAreaOffsetFieldValue = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301100 size_t offset = 0;
Ed Tanous07d467b2021-02-23 14:48:37 -08001101 std::string areaName = propertyName.substr(0, propertyName.find('_'));
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001102 std::string propertyNamePrefix = areaName + "_";
Ed Tanous07d467b2021-02-23 14:48:37 -08001103 auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName);
1104 if (it == fruAreaNames.end())
Joshi-Mansid73b7442020-03-27 19:10:21 +05301105 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001106 std::cerr << "Can't parse area name for property " << propertyName
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001107 << " \n";
1108 return false;
1109 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001110 fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin());
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001111 fruAreaOffsetFieldValue =
1112 fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)];
1113 switch (fruAreaToUpdate)
1114 {
1115 case fruAreas::fruAreaChassis:
1116 offset = 3; // chassis part number offset. Skip fixed first 3 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001117 fruAreaFieldNames = &chassisFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001118 break;
1119 case fruAreas::fruAreaBoard:
1120 offset = 6; // board manufacturer offset. Skip fixed first 6 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001121 fruAreaFieldNames = &boardFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001122 break;
1123 case fruAreas::fruAreaProduct:
1124 // Manufacturer name offset. Skip fixed first 3 product fru bytes
1125 // i.e. version, area length and language code
1126 offset = 3;
Ed Tanous07d467b2021-02-23 14:48:37 -08001127 fruAreaFieldNames = &productFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001128 break;
1129 default:
1130 std::cerr << "Don't know how to handle property " << propertyName
1131 << " \n";
1132 return false;
1133 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001134 if (fruAreaOffsetFieldValue == 0)
1135 {
1136 std::cerr << "FRU Area for " << propertyName << " not present \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301137 return false;
1138 }
1139
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001140 size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001141 size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize;
1142 size_t fruAreaEnd = fruAreaStart + fruAreaSize;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001143 size_t fruDataIter = fruAreaStart + offset;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001144 size_t skipToFRUUpdateField = 0;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001145 ssize_t fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301146
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001147 bool found = false;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001148 for (auto& field : *fruAreaFieldNames)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301149 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001150 skipToFRUUpdateField++;
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001151 if (propertyName == propertyNamePrefix + field)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301152 {
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001153 found = true;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301154 break;
1155 }
1156 }
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001157 if (!found)
1158 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001159 std::size_t pos = propertyName.find(fruCustomFieldName);
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001160 if (pos == std::string::npos)
1161 {
1162 std::cerr << "PropertyName doesn't exist in FRU Area Vectors: "
1163 << propertyName << "\n";
1164 return false;
1165 }
1166 std::string fieldNumStr =
Ed Tanous07d467b2021-02-23 14:48:37 -08001167 propertyName.substr(pos + fruCustomFieldName.length());
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001168 size_t fieldNum = std::stoi(fieldNumStr);
1169 if (fieldNum == 0)
1170 {
1171 std::cerr << "PropertyName not recognized: " << propertyName
1172 << "\n";
1173 return false;
1174 }
1175 skipToFRUUpdateField += fieldNum;
1176 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301177
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001178 for (size_t i = 1; i < skipToFRUUpdateField; i++)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301179 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001180 fieldLength = getFieldLength(fruData[fruDataIter]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001181 if (fieldLength < 0)
1182 {
1183 break;
1184 }
1185 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301186 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001187 size_t fruUpdateFieldLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301188
1189 // Push post update fru field bytes to a vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001190 fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001191 if (fieldLength < 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301192 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001193 std::cerr << "Property " << propertyName << " not present \n";
1194 return false;
1195 }
1196 fruDataIter += 1 + fieldLength;
1197 size_t restFRUFieldsLoc = fruDataIter;
1198 size_t endOfFieldsLoc = 0;
1199 while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0)
1200 {
1201 if (fruDataIter >= (fruAreaStart + fruAreaSize))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301202 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001203 fruDataIter = fruAreaStart + fruAreaSize;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301204 break;
1205 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001206 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301207 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001208 endOfFieldsLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301209
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001210 std::vector<uint8_t> restFRUAreaFieldsData;
1211 std::copy_n(fruData.begin() + restFRUFieldsLoc,
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001212 endOfFieldsLoc - restFRUFieldsLoc + 1,
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001213 std::back_inserter(restFRUAreaFieldsData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301214
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001215 // Push post update fru areas if any
1216 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001217 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1218 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001219 {
1220 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001221 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001222 if ((fruAreaLoc > endOfFieldsLoc) &&
1223 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1224 {
1225 nextFRUAreaLoc = fruAreaLoc;
1226 }
1227 }
1228 std::vector<uint8_t> restFRUAreasData;
1229 if (nextFRUAreaLoc)
1230 {
1231 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1232 fruData.size() - nextFRUAreaLoc,
1233 std::back_inserter(restFRUAreasData));
1234 }
1235
1236 // check FRU area size
1237 size_t fruAreaDataSize =
1238 ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size());
1239 size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize;
1240 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1241 {
1242
1243#ifdef ENABLE_FRU_AREA_RESIZE
1244 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1245 // round size to 8-byte blocks
1246 newFRUAreaSize =
1247 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
1248 size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize;
1249 fruData.resize(newFRUDataSize);
1250 fruAreaSize = newFRUAreaSize;
1251 fruAreaEnd = fruAreaStart + fruAreaSize;
1252#else
1253 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1254 << " should not be greater than available FRU area size: "
1255 << fruAreaAvailableSize << "\n";
1256 return false;
1257#endif // ENABLE_FRU_AREA_RESIZE
1258 }
1259
Joshi-Mansid73b7442020-03-27 19:10:21 +05301260 // write new requested property field length and data
1261 constexpr uint8_t newTypeLenMask = 0xC0;
1262 fruData[fruUpdateFieldLoc] =
1263 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
1264 fruUpdateFieldLoc++;
1265 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
1266 fruData.begin() + fruUpdateFieldLoc);
1267
1268 // Copy remaining data to main fru area - post updated fru field vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001269 restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen;
1270 size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size();
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001271 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
1272 fruData.begin() + restFRUFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301273
1274 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001275 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
1276 fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301277
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001278#ifdef ENABLE_FRU_AREA_RESIZE
1279 ++nextFRUAreaNewLoc;
1280 ssize_t nextFRUAreaOffsetDiff =
1281 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
1282 // Append rest FRU Areas if size changed and there were other sections after
1283 // updated one
1284 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1285 {
1286 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1287 fruData.begin() + nextFRUAreaNewLoc);
1288 // Update Common Header
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001289 for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord;
1290 fruArea++)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001291 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001292 unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001293 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
1294 if (curFRUAreaOffset > fruAreaOffsetFieldValue)
1295 {
1296 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1297 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1298 }
1299 }
1300 // Calculate new checksum
1301 std::vector<uint8_t> headerFRUData;
1302 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1303 size_t checksumVal = calculateChecksum(headerFRUData);
1304 fruData[7] = static_cast<uint8_t>(checksumVal);
1305 // fill zeros if FRU Area size decreased
1306 if (nextFRUAreaOffsetDiff < 0)
1307 {
1308 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1309 restFRUAreasData.size(),
1310 fruData.end(), 0);
1311 }
1312 }
1313#else
1314 // this is to avoid "unused variable" warning
1315 (void)nextFRUAreaNewLoc;
1316#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301317 if (fruData.empty())
1318 {
1319 return false;
1320 }
1321
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001322 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301323 fruData))
1324 {
1325 return false;
1326 }
1327
1328 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301329 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301330 return true;
1331}
1332
James Feist98132792019-07-09 13:29:09 -07001333int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001334{
Kumar Thangavel41a90512021-11-24 15:44:20 +05301335 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301336 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001337 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001338 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001339 std::vector<fs::path> i2cBuses;
1340
James Feista3c180a2018-08-09 16:06:04 -07001341 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001342 {
1343 std::cerr << "unable to find i2c devices\n";
1344 return 1;
1345 }
James Feist3cb5fec2018-01-23 14:41:51 -08001346
Patrick Venture11f1ff42019-08-01 10:42:12 -07001347 // check for and load blacklist with initial buses.
1348 loadBlacklist(blacklistPath);
1349
Vijay Khemka065f6d92018-12-18 10:37:47 -08001350 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001351
James Feist6ebf9de2018-05-15 15:01:17 -07001352 // this is a map with keys of pair(bus number, address) and values of
1353 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001354 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001355 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1356 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001357
James Feist9eb0b582018-04-27 12:15:46 -07001358 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1359 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1360 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001361
Kumar Thangavel41a90512021-11-24 15:44:20 +05301362 iface->register_method("ReScan", [&]() {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301363 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1364 powerIsOn);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301365 });
James Feist2a9d6db2018-04-27 15:48:28 -07001366
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001367 iface->register_method("ReScanBus", [&](uint8_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301368 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
1369 powerIsOn);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001370 });
1371
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001372 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001373
1374 iface->register_method("WriteFru", [&](const uint8_t bus,
1375 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001376 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001377 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001378 {
James Feistddb78302018-09-06 11:45:42 -07001379 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001380 return;
1381 }
1382 // schedule rescan on success
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301383 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1384 powerIsOn);
James Feistb49ffc32018-05-02 11:10:43 -07001385 });
James Feist9eb0b582018-04-27 12:15:46 -07001386 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001387
James Feist9eb0b582018-04-27 12:15:46 -07001388 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -08001389 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -08001390 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001391 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001392 std::string,
1393 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001394 values;
1395 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001396 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001397 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001398 {
James Feistcb5661d2020-06-12 15:05:01 -07001399 powerIsOn = boost::ends_with(
1400 std::get<std::string>(findState->second), "Running");
James Feist0eeb79c2019-10-09 13:35:29 -07001401 }
James Feist6ebf9de2018-05-15 15:01:17 -07001402
James Feistcb5661d2020-06-12 15:05:01 -07001403 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001404 {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301405 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
1406 powerIsOn);
James Feist918e18c2018-02-13 15:51:07 -08001407 }
James Feist918e18c2018-02-13 15:51:07 -08001408 };
James Feist9eb0b582018-04-27 12:15:46 -07001409
1410 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -08001411 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001412 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001413 "openbmc_project/state/"
1414 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001415 eventHandler);
1416
James Feist4131aea2018-03-09 09:47:30 -08001417 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001418 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -08001419 std::array<char, 4096> readBuffer;
1420 std::string pendingBuffer;
1421 // monitor for new i2c devices
1422 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1423 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001424 watchI2cBusses = [&](const boost::system::error_code& ec,
Ed Tanous07d467b2021-02-23 14:48:37 -08001425 std::size_t bytesTransferred) {
James Feist4131aea2018-03-09 09:47:30 -08001426 if (ec)
1427 {
1428 std::cout << "Callback Error " << ec << "\n";
1429 return;
1430 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001431 pendingBuffer += std::string(readBuffer.data(), bytesTransferred);
James Feist4131aea2018-03-09 09:47:30 -08001432 while (pendingBuffer.size() > sizeof(inotify_event))
1433 {
James Feista465ccc2019-02-08 12:51:01 -08001434 const inotify_event* iEvent =
1435 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -08001436 pendingBuffer.data());
1437 switch (iEvent->mask)
1438 {
James Feist9eb0b582018-04-27 12:15:46 -07001439 case IN_CREATE:
1440 case IN_MOVED_TO:
1441 case IN_DELETE:
James Feist5d7f4692020-06-17 18:22:33 -07001442 std::string name(iEvent->name);
1443 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001444 {
James Feist5d7f4692020-06-17 18:22:33 -07001445 int bus = busStrToInt(name);
1446 if (bus < 0)
1447 {
1448 std::cerr << "Could not parse bus " << name
1449 << "\n";
1450 continue;
1451 }
1452 rescanOneBus(busMap, static_cast<uint8_t>(bus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301453 dbusInterfaceMap, false,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301454 unknownBusObjectCount, powerIsOn);
James Feist9eb0b582018-04-27 12:15:46 -07001455 }
James Feist4131aea2018-03-09 09:47:30 -08001456 }
1457
1458 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
1459 }
James Feist6ebf9de2018-05-15 15:01:17 -07001460
James Feist4131aea2018-03-09 09:47:30 -08001461 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1462 watchI2cBusses);
1463 };
1464
1465 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001466 // run the initial scan
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301467 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn);
James Feist918e18c2018-02-13 15:51:07 -08001468
James Feist3cb5fec2018-01-23 14:41:51 -08001469 io.run();
1470 return 0;
1471}