blob: 620da6e1590065226f29d95d620a5d5c3343f0b5 [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
James Feist444830e2019-04-05 08:38:16 -070073static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070074struct FindDevicesWithCallback;
75
James Feist8a983922019-10-24 17:11:56 -070076static boost::container::flat_map<
77 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
78 foundDevices;
79
James Feist7972bb92019-11-13 15:59:24 -080080static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
81
James Feist5cb06082019-10-24 17:11:13 -070082boost::asio::io_service io;
James Feist5cb06082019-10-24 17:11:13 -070083
Andrei Kartashev2a7e3952020-09-02 20:32:26 +030084uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter,
85 std::vector<uint8_t>::const_iterator end);
Andrei Kartashev2f0de172020-08-13 14:29:40 +030086bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +053087 const std::string& assetTag, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -080088 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +053089 boost::container::flat_map<
90 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +053091 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -080092 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +053093 sdbusplus::asio::object_server& objServer,
94 std::shared_ptr<sdbusplus::asio::connection>& systemBus);
Patrick Venturebaba7672019-10-26 09:26:41 -070095
Patrick Venturec50e1ff2019-08-06 10:22:28 -070096// Given a bus/address, produce the path in sysfs for an eeprom.
97static std::string getEepromPath(size_t bus, size_t address)
98{
99 std::stringstream output;
100 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
101 << std::setfill('0') << std::setw(4) << std::hex << address
102 << "/eeprom";
103 return output.str();
104}
105
106static bool hasEepromFile(size_t bus, size_t address)
107{
108 auto path = getEepromPath(bus, address);
109 try
110 {
111 return fs::exists(path);
112 }
113 catch (...)
114 {
115 return false;
116 }
117}
118
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700119static int64_t readFromEeprom(int flag __attribute__((unused)), int fd,
Xiang Liu2801a702020-01-20 14:29:34 -0800120 uint16_t address __attribute__((unused)),
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700121 uint16_t offset, uint8_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700122{
123 auto result = lseek(fd, offset, SEEK_SET);
124 if (result < 0)
125 {
126 std::cerr << "failed to seek\n";
127 return -1;
128 }
129
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700130 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700131}
132
James Feist5d7f4692020-06-17 18:22:33 -0700133static int busStrToInt(const std::string& busName)
134{
Ed Tanous07d467b2021-02-23 14:48:37 -0800135 auto findBus = busName.rfind('-');
James Feist5d7f4692020-06-17 18:22:33 -0700136 if (findBus == std::string::npos)
137 {
138 return -1;
139 }
140 return std::stoi(busName.substr(findBus + 1));
141}
142
James Feist7972bb92019-11-13 15:59:24 -0800143static int getRootBus(size_t bus)
144{
145 auto ec = std::error_code();
146 auto path = std::filesystem::read_symlink(
147 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
148 std::to_string(bus) + "/mux_device"),
149 ec);
150 if (ec)
151 {
152 return -1;
153 }
154
155 std::string filename = path.filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800156 auto findBus = filename.find('-');
James Feist7972bb92019-11-13 15:59:24 -0800157 if (findBus == std::string::npos)
158 {
159 return -1;
160 }
161 return std::stoi(filename.substr(0, findBus));
162}
163
James Feistc95cb142018-02-26 10:41:42 -0800164static bool isMuxBus(size_t bus)
165{
Ed Tanous072e25d2018-12-16 21:45:20 -0800166 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800167 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
168}
169
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530170static void makeProbeInterface(size_t bus, size_t address,
171 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700172{
173 if (isMuxBus(bus))
174 {
175 return; // the mux buses are random, no need to publish
176 }
177 auto [it, success] = foundDevices.emplace(
178 std::make_pair(bus, address),
179 objServer.add_interface(
180 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
181 std::to_string(address),
182 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
183 if (!success)
184 {
185 return; // already added
186 }
187 it->second->register_property("Bus", bus);
188 it->second->register_property("Address", address);
189 it->second->initialize();
190}
191
Vijay Khemka2d681f62018-11-06 15:51:00 -0800192static int isDevice16Bit(int file)
193{
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700194 // Set the higher data word address bits to 0. It's safe on 8-bit addressing
195 // EEPROMs because it doesn't write any actual data.
196 int ret = i2c_smbus_write_byte(file, 0);
197 if (ret < 0)
198 {
199 return ret;
200 }
201
Vijay Khemka2d681f62018-11-06 15:51:00 -0800202 /* Get first byte */
203 int byte1 = i2c_smbus_read_byte_data(file, 0);
204 if (byte1 < 0)
205 {
206 return byte1;
207 }
208 /* Read 7 more bytes, it will read same first byte in case of
209 * 8 bit but it will read next byte in case of 16 bit
210 */
211 for (int i = 0; i < 7; i++)
212 {
213 int byte2 = i2c_smbus_read_byte_data(file, 0);
214 if (byte2 < 0)
215 {
216 return byte2;
217 }
218 if (byte2 != byte1)
219 {
220 return 1;
221 }
222 }
223 return 0;
224}
225
Xiang Liu2801a702020-01-20 14:29:34 -0800226// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
227// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800228static int i2cSmbusWriteThenRead(int file, uint16_t address,
229 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
230 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800231{
Ed Tanous07d467b2021-02-23 14:48:37 -0800232 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
233 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800234 {
235 return -1;
236 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800237
Xiang Liu2801a702020-01-20 14:29:34 -0800238#define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2
239 struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT];
240 struct i2c_rdwr_ioctl_data rdwr;
241
242 msgs[0].addr = address;
243 msgs[0].flags = 0;
244 msgs[0].len = toSlaveBufLen;
245 msgs[0].buf = toSlaveBuf;
246 msgs[1].addr = address;
247 msgs[1].flags = I2C_M_RD;
248 msgs[1].len = fromSlaveBufLen;
249 msgs[1].buf = fromSlaveBuf;
250
251 rdwr.msgs = msgs;
252 rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT;
253
254 int ret = ioctl(file, I2C_RDWR, &rdwr);
255
Zev Weiss07108282022-03-01 18:51:31 -0800256 return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800257}
258
259static int64_t readBlockData(int flag, int file, uint16_t address,
260 uint16_t offset, uint8_t len, uint8_t* buf)
261{
Vijay Khemka2d681f62018-11-06 15:51:00 -0800262 if (flag == 0)
263 {
Xiang Liu2801a702020-01-20 14:29:34 -0800264 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
265 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800266 }
267
Xiang Liu2801a702020-01-20 14:29:34 -0800268 offset = htobe16(offset);
Ed Tanous07d467b2021-02-23 14:48:37 -0800269 return i2cSmbusWriteThenRead(
Xiang Liu2801a702020-01-20 14:29:34 -0800270 file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800271}
272
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700273// TODO: This code is very similar to the non-eeprom version and can be merged
274// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300275static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700276{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700277 auto path = getEepromPath(bus, address);
278
279 int file = open(path.c_str(), O_RDONLY);
280 if (file < 0)
281 {
282 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700283 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700284 }
285
Patrick Venturebaba7672019-10-26 09:26:41 -0700286 std::string errorMessage = "eeprom at " + std::to_string(bus) +
287 " address " + std::to_string(address);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300288 std::vector<uint8_t> device = readFRUContents(
Xiang Liu2801a702020-01-20 14:29:34 -0800289 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700290
291 close(file);
292 return device;
293}
294
Ed Tanous07d467b2021-02-23 14:48:37 -0800295std::set<int> findI2CEeproms(int i2cBus,
296 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700297{
298 std::set<int> foundList;
299
300 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
301
302 // For each file listed under the i2c device
303 // NOTE: This should be faster than just checking for each possible address
304 // path.
305 for (const auto& p : fs::directory_iterator(path))
306 {
307 const std::string node = p.path().string();
308 std::smatch m;
309 bool found =
310 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
311
312 if (!found)
313 {
314 continue;
315 }
316 if (m.size() != 2)
317 {
318 std::cerr << "regex didn't capture\n";
319 continue;
320 }
321
322 std::ssub_match subMatch = m[1];
323 std::string addressString = subMatch.str();
324
325 std::size_t ignored;
326 const int hexBase = 16;
327 int address = std::stoi(addressString, &ignored, hexBase);
328
329 const std::string eeprom = node + "/eeprom";
330
331 try
332 {
333 if (!fs::exists(eeprom))
334 {
335 continue;
336 }
337 }
338 catch (...)
339 {
340 continue;
341 }
342
343 // There is an eeprom file at this address, it may have invalid
344 // contents, but we found it.
345 foundList.insert(address);
346
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300347 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700348 if (!device.empty())
349 {
350 devices->emplace(address, device);
351 }
352 }
353
354 return foundList;
355}
356
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300357int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800358 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530359 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800360{
James Feist3cb5fec2018-01-23 14:41:51 -0800361
James Feist26c27ad2018-07-25 15:09:40 -0700362 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700363 // NOTE: When reading the devices raw on the bus, it can interfere with
364 // the driver's ability to operate, therefore read eeproms first before
365 // scanning for devices without drivers. Several experiments were run
366 // and it was determined that if there were any devices on the bus
367 // before the eeprom was hit and read, the eeprom driver wouldn't open
368 // while the bus device was open. An experiment was not performed to see
369 // if this issue was resolved if the i2c bus device was closed, but
370 // hexdumps of the eeprom later were successful.
371
372 // Scan for i2c eeproms loaded on this bus.
373 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800374 std::set<size_t>& failedItems = failedAddresses[bus];
375
376 std::set<size_t>* rootFailures = nullptr;
377 int rootBus = getRootBus(bus);
378
379 if (rootBus >= 0)
380 {
381 rootFailures = &(failedAddresses[rootBus]);
382 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700383
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530384 constexpr int startSkipSlaveAddr = 0;
385 constexpr int endSkipSlaveAddr = 12;
386
James Feist26c27ad2018-07-25 15:09:40 -0700387 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800388 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700389 if (skipList.find(ii) != skipList.end())
390 {
391 continue;
392 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530393 // skipping since no device is present in this range
394 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
395 {
396 continue;
397 }
James Feist26c27ad2018-07-25 15:09:40 -0700398 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530399 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800400 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300401 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700402 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700403 continue;
404 }
405 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800406 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700407 {
408 continue;
409 }
James Feist3cb5fec2018-01-23 14:41:51 -0800410
Ed Tanous07d467b2021-02-23 14:48:37 -0800411 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700412 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700413 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700414 << "\n";
415 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800416
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530417 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700418
James Feist7972bb92019-11-13 15:59:24 -0800419 if (failedItems.find(ii) != failedItems.end())
420 {
421 // if we failed to read it once, unlikely we can read it later
422 continue;
423 }
424
425 if (rootFailures != nullptr)
426 {
427 if (rootFailures->find(ii) != rootFailures->end())
428 {
429 continue;
430 }
431 }
432
Vijay Khemka2d681f62018-11-06 15:51:00 -0800433 /* Check for Device type if it is 8 bit or 16 bit */
434 int flag = isDevice16Bit(file);
435 if (flag < 0)
436 {
437 std::cerr << "failed to read bus " << bus << " address " << ii
438 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700439 if (powerIsOn)
440 {
441 failedItems.insert(ii);
442 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800443 continue;
444 }
445
Patrick Venturebaba7672019-10-26 09:26:41 -0700446 std::string errorMessage =
447 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300448 std::vector<uint8_t> device =
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300449 readFRUContents(flag, file, static_cast<uint16_t>(ii),
Xiang Liu2801a702020-01-20 14:29:34 -0800450 readBlockData, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700451 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700452 {
James Feist26c27ad2018-07-25 15:09:40 -0700453 continue;
454 }
James Feist26c27ad2018-07-25 15:09:40 -0700455
Patrick Venture786f1792019-08-05 16:33:44 -0700456 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800457 }
James Feist26c27ad2018-07-25 15:09:40 -0700458 return 1;
459 });
460 std::future_status status =
461 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
462 if (status == std::future_status::timeout)
463 {
464 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700465 if (powerIsOn)
466 {
467 busBlacklist.insert(bus);
468 }
James Feist444830e2019-04-05 08:38:16 -0700469 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700470 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800471 }
472
James Feist444830e2019-04-05 08:38:16 -0700473 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700474 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800475}
476
Patrick Venture11f1ff42019-08-01 10:42:12 -0700477void loadBlacklist(const char* path)
478{
479 std::ifstream blacklistStream(path);
480 if (!blacklistStream.good())
481 {
482 // File is optional.
483 std::cerr << "Cannot open blacklist file.\n\n";
484 return;
485 }
486
487 nlohmann::json data =
488 nlohmann::json::parse(blacklistStream, nullptr, false);
489 if (data.is_discarded())
490 {
491 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
492 "exiting\n";
493 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700494 }
495
496 // It's expected to have at least one field, "buses" that is an array of the
497 // buses by integer. Allow for future options to exclude further aspects,
498 // such as specific addresses or ranges.
499 if (data.type() != nlohmann::json::value_t::object)
500 {
501 std::cerr << "Illegal blacklist, expected to read dictionary\n";
502 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700503 }
504
505 // If buses field is missing, that's fine.
506 if (data.count("buses") == 1)
507 {
508 // Parse the buses array after a little validation.
509 auto buses = data.at("buses");
510 if (buses.type() != nlohmann::json::value_t::array)
511 {
512 // Buses field present but invalid, therefore this is an error.
513 std::cerr << "Invalid contents for blacklist buses field\n";
514 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700515 }
516
517 // Catch exception here for type mis-match.
518 try
519 {
520 for (const auto& bus : buses)
521 {
522 busBlacklist.insert(bus.get<size_t>());
523 }
524 }
525 catch (const nlohmann::detail::type_error& e)
526 {
527 // Type mis-match is a critical error.
528 std::cerr << "Invalid bus type: " << e.what() << "\n";
529 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700530 }
531 }
532
533 return;
534}
535
Ed Tanous07d467b2021-02-23 14:48:37 -0800536static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800537 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530538 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800539{
James Feista465ccc2019-02-08 12:51:01 -0800540 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800541 {
James Feist5d7f4692020-06-17 18:22:33 -0700542 int bus = busStrToInt(i2cBus);
James Feist0c3980a2019-12-19 11:09:27 -0800543
James Feist5d7f4692020-06-17 18:22:33 -0700544 if (bus < 0)
545 {
546 std::cerr << "Cannot translate " << i2cBus << " to int\n";
547 continue;
548 }
James Feist444830e2019-04-05 08:38:16 -0700549 if (busBlacklist.find(bus) != busBlacklist.end())
550 {
551 continue; // skip previously failed busses
552 }
James Feist3cb5fec2018-01-23 14:41:51 -0800553
James Feist0c3980a2019-12-19 11:09:27 -0800554 int rootBus = getRootBus(bus);
555 if (busBlacklist.find(rootBus) != busBlacklist.end())
556 {
557 continue;
558 }
559
James Feist3cb5fec2018-01-23 14:41:51 -0800560 auto file = open(i2cBus.c_str(), O_RDWR);
561 if (file < 0)
562 {
563 std::cerr << "unable to open i2c device " << i2cBus.string()
564 << "\n";
565 continue;
566 }
567 unsigned long funcs = 0;
568
569 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
570 {
571 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700572 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800573 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700574 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800575 continue;
576 }
577 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
578 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
579 {
580 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
581 << bus << "\n";
582 continue;
583 }
James Feist98132792019-07-09 13:29:09 -0700584 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800585 device = std::make_shared<DeviceMap>();
586
Nikhil Potaded8884f12019-03-27 13:27:13 -0700587 // i2cdetect by default uses the range 0x03 to 0x77, as
588 // this is what we have tested with, use this range. Could be
589 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800590 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800591 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700592 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800593 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700594
595 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530596 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700597
Ed Tanous07d467b2021-02-23 14:48:37 -0800598 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800599 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700600 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800601 }
James Feist3cb5fec2018-01-23 14:41:51 -0800602 }
James Feist3cb5fec2018-01-23 14:41:51 -0800603}
604
James Feist6ebf9de2018-05-15 15:01:17 -0700605// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700606struct FindDevicesWithCallback :
607 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700608{
James Feista465ccc2019-02-08 12:51:01 -0800609 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800610 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530611 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800612 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700613 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530614 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
615 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700616 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700617 ~FindDevicesWithCallback()
618 {
619 _callback();
620 }
621 void run()
622 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530623 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700624 }
625
James Feista465ccc2019-02-08 12:51:01 -0800626 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800627 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800628 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530629 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700630 std::function<void(void)> _callback;
631};
632
Ed Tanous07d467b2021-02-23 14:48:37 -0800633void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300634 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800635 boost::container::flat_map<
636 std::pair<size_t, size_t>,
637 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530638 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800639 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530640 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800641{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300642 boost::container::flat_map<std::string, std::string> formattedFRU;
Michael Shen0961b112022-02-22 11:06:33 +0800643 resCodes res = formatIPMIFRU(device, formattedFRU);
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300644 if (res == resCodes::resErr)
James Feist3cb5fec2018-01-23 14:41:51 -0800645 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300646 std::cerr << "failed to parse FRU for device at bus " << bus
Patrick Ventureb755c832019-08-07 11:09:14 -0700647 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800648 return;
649 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800650 if (res == resCodes::resWarn)
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300651 {
652 std::cerr << "there were warnings while parsing FRU for device at bus "
653 << bus << " address " << address << "\n";
654 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700655
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300656 auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800657 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700658 // Not found under Board section or an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300659 if (productNameFind == formattedFRU.end() ||
Patrick Venture96cdaef2019-07-30 13:30:52 -0700660 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800661 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300662 productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800663 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700664 // Found under Product section and not an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300665 if (productNameFind != formattedFRU.end() &&
Patrick Venture96cdaef2019-07-30 13:30:52 -0700666 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800667 {
668 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800669 std::regex illegalObject("[^A-Za-z0-9_]");
670 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800671 }
672 else
673 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800674 productName = "UNKNOWN" + std::to_string(unknownBusObjectCount);
675 unknownBusObjectCount++;
James Feist3cb5fec2018-01-23 14:41:51 -0800676 }
677
James Feist918e18c2018-02-13 15:51:07 -0800678 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800679 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700680 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800681 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700682 int highest = -1;
683 bool found = false;
684
James Feista465ccc2019-02-08 12:51:01 -0800685 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800686 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700687 std::string path = busIface.second->get_object_path();
688 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
James Feist918e18c2018-02-13 15:51:07 -0800689 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700690 if (isMuxBus(bus) && address == busIface.first.second &&
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300691 (getFRUInfo(static_cast<uint8_t>(busIface.first.first),
James Feist98132792019-07-09 13:29:09 -0700692 static_cast<uint8_t>(busIface.first.second)) ==
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300693 getFRUInfo(static_cast<uint8_t>(bus),
James Feist98132792019-07-09 13:29:09 -0700694 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700695 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700696 // This device is already added to the lower numbered bus,
697 // do not replicate it.
698 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700699 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700700
701 // Check if the match named has extra information.
702 found = true;
Ed Tanous07d467b2021-02-23 14:48:37 -0800703 std::smatch baseMatch;
Patrick Venture015fb0a2019-08-16 09:33:31 -0700704
705 bool match = std::regex_match(
Ed Tanous07d467b2021-02-23 14:48:37 -0800706 path, baseMatch, std::regex(productName + "_(\\d+)$"));
Patrick Venture015fb0a2019-08-16 09:33:31 -0700707 if (match)
James Feist79e9c0b2018-03-15 15:21:17 -0700708 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800709 if (baseMatch.size() == 2)
Patrick Venture015fb0a2019-08-16 09:33:31 -0700710 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800711 std::ssub_match baseSubMatch = baseMatch[1];
712 std::string base = baseSubMatch.str();
Patrick Venture015fb0a2019-08-16 09:33:31 -0700713
714 int value = std::stoi(base);
715 highest = (value > highest) ? value : highest;
716 }
James Feist79e9c0b2018-03-15 15:21:17 -0700717 }
James Feist918e18c2018-02-13 15:51:07 -0800718 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700719 } // end searching objects
720
721 if (found)
722 {
723 // We found something with the same name. If highest was still -1,
724 // it means this new entry will be _0.
725 productName += "_";
726 productName += std::to_string(++highest);
James Feist918e18c2018-02-13 15:51:07 -0800727 }
728 }
James Feist3cb5fec2018-01-23 14:41:51 -0800729
James Feist9eb0b582018-04-27 12:15:46 -0700730 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
731 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
732 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
733
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300734 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800735 {
James Feist9eb0b582018-04-27 12:15:46 -0700736
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700737 std::regex_replace(property.second.begin(), property.second.begin(),
Ed Tanous07d467b2021-02-23 14:48:37 -0800738 property.second.end(), nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530739 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700740 {
741 continue;
742 }
743 std::string key =
Ed Tanous07d467b2021-02-23 14:48:37 -0800744 std::regex_replace(property.first, nonAsciiRegex, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530745
746 if (property.first == "PRODUCT_ASSET_TAG")
747 {
748 std::string propertyName = property.first;
749 iface->register_property(
750 key, property.second + '\0',
Kumar Thangavel41a90512021-11-24 15:44:20 +0530751 [bus, address, propertyName, &dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530752 &unknownBusObjectCount, &powerIsOn, &objServer,
753 &systemBus](const std::string& req, std::string& resp) {
Ed Tanous07d467b2021-02-23 14:48:37 -0800754 if (strcmp(req.c_str(), resp.c_str()) != 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +0530755 {
756 // call the method which will update
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300757 if (updateFRUProperty(req, bus, address, propertyName,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530758 dbusInterfaceMap,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530759 unknownBusObjectCount, powerIsOn,
760 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530761 {
762 resp = req;
763 }
764 else
765 {
766 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300767 "FRU property update failed.");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530768 }
769 }
770 return 1;
771 });
772 }
773 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700774 {
775 std::cerr << "illegal key: " << key << "\n";
776 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800777 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700778 {
779 std::cout << property.first << ": " << property.second << "\n";
780 }
James Feist3cb5fec2018-01-23 14:41:51 -0800781 }
James Feist2a9d6db2018-04-27 15:48:28 -0700782
783 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700784 iface->register_property("BUS", bus);
785 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700786
James Feist9eb0b582018-04-27 12:15:46 -0700787 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800788}
789
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300790static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800791{
792 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800793 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300794 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800795 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300796 baseboardFRUFile.seekg(0, std::ios_base::end);
797 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
798 baseboardFRU.resize(fileSize);
799 baseboardFRUFile.seekg(0, std::ios_base::beg);
800 baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()),
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300801 fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800802 }
803 else
804 {
805 return false;
806 }
807 return true;
808}
809
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300810bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700811{
812 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800813 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700814 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300815 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700816 return false;
817 }
818 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800819 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700820 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300821 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700822 return false;
823 }
824 // baseboard fru
825 if (bus == 0 && address == 0)
826 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800827 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700828 if (!file.good())
829 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800830 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700831 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700832 return false;
833 }
James Feista465ccc2019-02-08 12:51:01 -0800834 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700835 return file.good();
836 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800837
838 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700839 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800840 auto path = getEepromPath(bus, address);
841 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
842 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700843 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800844 std::cerr << "unable to open i2c device " << path << "\n";
845 throw DBusInternalError();
846 return false;
847 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700848
Ed Tanous07d467b2021-02-23 14:48:37 -0800849 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
850 if (writtenBytes < 0)
851 {
852 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700853 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700854 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700855 return false;
856 }
857
Ed Tanous07d467b2021-02-23 14:48:37 -0800858 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700859 return true;
860 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800861
862 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
863
864 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
865 if (file < 0)
866 {
867 std::cerr << "unable to open i2c device " << i2cBus << "\n";
868 throw DBusInternalError();
869 return false;
870 }
871 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
872 {
873 std::cerr << "unable to set device address\n";
874 close(file);
875 throw DBusInternalError();
876 return false;
877 }
878
879 constexpr const size_t retryMax = 2;
880 uint16_t index = 0;
881 size_t retries = retryMax;
882 while (index < fru.size())
883 {
884 if ((index && ((index % (maxEepromPageIndex + 1)) == 0)) &&
885 (retries == retryMax))
886 {
887 // The 4K EEPROM only uses the A2 and A1 device address bits
888 // with the third bit being a memory page address bit.
889 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
890 {
891 std::cerr << "unable to set device address\n";
892 close(file);
893 throw DBusInternalError();
894 return false;
895 }
896 }
897
898 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
899 fru[index]) < 0)
900 {
901 if (!retries--)
902 {
903 std::cerr << "error writing fru: " << strerror(errno) << "\n";
904 close(file);
905 throw DBusInternalError();
906 return false;
907 }
908 }
909 else
910 {
911 retries = retryMax;
912 index++;
913 }
914 // most eeproms require 5-10ms between writes
915 std::this_thread::sleep_for(std::chrono::milliseconds(10));
916 }
917 close(file);
918 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700919}
920
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800921void rescanOneBus(
922 BusMap& busmap, uint8_t busNum,
923 boost::container::flat_map<
924 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700925 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800926 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530927 sdbusplus::asio::object_server& objServer,
928 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800929{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800930 for (auto& [pair, interface] : foundDevices)
931 {
932 if (pair.first == static_cast<size_t>(busNum))
933 {
934 objServer.remove_interface(interface);
935 foundDevices.erase(pair);
936 }
937 }
938
James Feist5d7f4692020-06-17 18:22:33 -0700939 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
940 if (!fs::exists(busPath))
941 {
942 if (dbusCall)
943 {
944 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
945 << "\n";
946 throw std::invalid_argument("Invalid Bus.");
947 }
948 return;
949 }
950
951 std::vector<fs::path> i2cBuses;
952 i2cBuses.emplace_back(busPath);
953
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800954 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530955 i2cBuses, busmap, powerIsOn, objServer,
956 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
957 &objServer, &systemBus]() {
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800958 for (auto& busIface : dbusInterfaceMap)
959 {
960 if (busIface.first.first == static_cast<size_t>(busNum))
961 {
962 objServer.remove_interface(busIface.second);
963 }
964 }
Wludzik, Jozefc1136b72020-06-24 11:58:32 +0200965 auto found = busmap.find(busNum);
966 if (found == busmap.end() || found->second == nullptr)
967 {
968 return;
969 }
970 for (auto& device : *(found->second))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800971 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800972 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530973 static_cast<uint32_t>(busNum), device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530974 unknownBusObjectCount, powerIsOn, objServer,
975 systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800976 }
977 });
978 scan->run();
979}
980
James Feist9eb0b582018-04-27 12:15:46 -0700981void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700982 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800983 boost::container::flat_map<
984 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530985 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800986 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530987 sdbusplus::asio::object_server& objServer,
988 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -0800989{
James Feist6ebf9de2018-05-15 15:01:17 -0700990 static boost::asio::deadline_timer timer(io);
991 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800992
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500993 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -0700994 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -0800995 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -0800996 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800997
Nikhil Potaded8884f12019-03-27 13:27:13 -0700998 boost::container::flat_map<size_t, fs::path> busPaths;
999 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001000 {
James Feist4131aea2018-03-09 09:47:30 -08001001 std::cerr << "unable to find i2c devices\n";
1002 return;
James Feist918e18c2018-02-13 15:51:07 -08001003 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001004
Ed Tanous07d467b2021-02-23 14:48:37 -08001005 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001006 {
1007 i2cBuses.emplace_back(busPath.second);
1008 }
James Feist4131aea2018-03-09 09:47:30 -08001009
James Feist98132792019-07-09 13:29:09 -07001010 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001011 for (auto& [pair, interface] : foundDevices)
1012 {
1013 objServer.remove_interface(interface);
1014 }
1015 foundDevices.clear();
1016
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301017 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301018 i2cBuses, busmap, powerIsOn, objServer, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001019 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001020 {
1021 objServer.remove_interface(busIface.second);
1022 }
James Feist4131aea2018-03-09 09:47:30 -08001023
James Feist6ebf9de2018-05-15 15:01:17 -07001024 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -08001025 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001026
James Feist6ebf9de2018-05-15 15:01:17 -07001027 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001028 std::vector<uint8_t> baseboardFRU;
1029 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001030 {
Helen Huangf69fe902021-02-19 16:18:22 +08001031 // If no device on i2c bus 0, the insertion will happen.
1032 auto bus0 =
1033 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1034 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001035 }
James Feist98132792019-07-09 13:29:09 -07001036 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001037 {
James Feista465ccc2019-02-08 12:51:01 -08001038 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001039 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001040 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301041 devicemap.first, device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301042 unknownBusObjectCount, powerIsOn,
1043 objServer, systemBus);
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,
Jonathan Doman2418a612022-02-14 18:20:58 -08001068 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301069 sdbusplus::asio::object_server& objServer,
1070 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301071{
1072 size_t updatePropertyReqLen = updatePropertyReq.length();
1073 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1074 {
1075 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001076 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301077 "Invalid Length "
1078 << updatePropertyReqLen << "\n";
1079 return false;
1080 }
1081
1082 std::vector<uint8_t> fruData;
1083 try
1084 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001085 fruData = getFRUInfo(static_cast<uint8_t>(bus),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301086 static_cast<uint8_t>(address));
1087 }
Patrick Williams83b1e9b2021-10-06 12:47:24 -05001088 catch (const std::invalid_argument& e)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301089 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001090 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301091 return false;
1092 }
1093
1094 if (fruData.empty())
1095 {
1096 return false;
1097 }
1098
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001099 const std::vector<std::string>* fruAreaFieldNames;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301100
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001101 uint8_t fruAreaOffsetFieldValue = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301102 size_t offset = 0;
Ed Tanous07d467b2021-02-23 14:48:37 -08001103 std::string areaName = propertyName.substr(0, propertyName.find('_'));
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001104 std::string propertyNamePrefix = areaName + "_";
Ed Tanous07d467b2021-02-23 14:48:37 -08001105 auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName);
1106 if (it == fruAreaNames.end())
Joshi-Mansid73b7442020-03-27 19:10:21 +05301107 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001108 std::cerr << "Can't parse area name for property " << propertyName
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001109 << " \n";
1110 return false;
1111 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001112 fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin());
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001113 fruAreaOffsetFieldValue =
1114 fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)];
1115 switch (fruAreaToUpdate)
1116 {
1117 case fruAreas::fruAreaChassis:
1118 offset = 3; // chassis part number offset. Skip fixed first 3 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001119 fruAreaFieldNames = &chassisFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001120 break;
1121 case fruAreas::fruAreaBoard:
1122 offset = 6; // board manufacturer offset. Skip fixed first 6 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001123 fruAreaFieldNames = &boardFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001124 break;
1125 case fruAreas::fruAreaProduct:
1126 // Manufacturer name offset. Skip fixed first 3 product fru bytes
1127 // i.e. version, area length and language code
1128 offset = 3;
Ed Tanous07d467b2021-02-23 14:48:37 -08001129 fruAreaFieldNames = &productFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001130 break;
1131 default:
1132 std::cerr << "Don't know how to handle property " << propertyName
1133 << " \n";
1134 return false;
1135 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001136 if (fruAreaOffsetFieldValue == 0)
1137 {
1138 std::cerr << "FRU Area for " << propertyName << " not present \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301139 return false;
1140 }
1141
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001142 size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001143 size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize;
1144 size_t fruAreaEnd = fruAreaStart + fruAreaSize;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001145 size_t fruDataIter = fruAreaStart + offset;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001146 size_t skipToFRUUpdateField = 0;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001147 ssize_t fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301148
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001149 bool found = false;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001150 for (auto& field : *fruAreaFieldNames)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301151 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001152 skipToFRUUpdateField++;
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001153 if (propertyName == propertyNamePrefix + field)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301154 {
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001155 found = true;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301156 break;
1157 }
1158 }
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001159 if (!found)
1160 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001161 std::size_t pos = propertyName.find(fruCustomFieldName);
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001162 if (pos == std::string::npos)
1163 {
1164 std::cerr << "PropertyName doesn't exist in FRU Area Vectors: "
1165 << propertyName << "\n";
1166 return false;
1167 }
1168 std::string fieldNumStr =
Ed Tanous07d467b2021-02-23 14:48:37 -08001169 propertyName.substr(pos + fruCustomFieldName.length());
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001170 size_t fieldNum = std::stoi(fieldNumStr);
1171 if (fieldNum == 0)
1172 {
1173 std::cerr << "PropertyName not recognized: " << propertyName
1174 << "\n";
1175 return false;
1176 }
1177 skipToFRUUpdateField += fieldNum;
1178 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301179
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001180 for (size_t i = 1; i < skipToFRUUpdateField; i++)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301181 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001182 fieldLength = getFieldLength(fruData[fruDataIter]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001183 if (fieldLength < 0)
1184 {
1185 break;
1186 }
1187 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301188 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001189 size_t fruUpdateFieldLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301190
1191 // Push post update fru field bytes to a vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001192 fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001193 if (fieldLength < 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301194 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001195 std::cerr << "Property " << propertyName << " not present \n";
1196 return false;
1197 }
1198 fruDataIter += 1 + fieldLength;
1199 size_t restFRUFieldsLoc = fruDataIter;
1200 size_t endOfFieldsLoc = 0;
1201 while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0)
1202 {
1203 if (fruDataIter >= (fruAreaStart + fruAreaSize))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301204 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001205 fruDataIter = fruAreaStart + fruAreaSize;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301206 break;
1207 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001208 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301209 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001210 endOfFieldsLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301211
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001212 std::vector<uint8_t> restFRUAreaFieldsData;
1213 std::copy_n(fruData.begin() + restFRUFieldsLoc,
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001214 endOfFieldsLoc - restFRUFieldsLoc + 1,
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001215 std::back_inserter(restFRUAreaFieldsData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301216
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001217 // Push post update fru areas if any
1218 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001219 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1220 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001221 {
1222 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001223 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001224 if ((fruAreaLoc > endOfFieldsLoc) &&
1225 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1226 {
1227 nextFRUAreaLoc = fruAreaLoc;
1228 }
1229 }
1230 std::vector<uint8_t> restFRUAreasData;
1231 if (nextFRUAreaLoc)
1232 {
1233 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1234 fruData.size() - nextFRUAreaLoc,
1235 std::back_inserter(restFRUAreasData));
1236 }
1237
1238 // check FRU area size
1239 size_t fruAreaDataSize =
1240 ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size());
1241 size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize;
1242 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1243 {
1244
1245#ifdef ENABLE_FRU_AREA_RESIZE
1246 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1247 // round size to 8-byte blocks
1248 newFRUAreaSize =
1249 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
1250 size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize;
1251 fruData.resize(newFRUDataSize);
1252 fruAreaSize = newFRUAreaSize;
1253 fruAreaEnd = fruAreaStart + fruAreaSize;
1254#else
1255 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1256 << " should not be greater than available FRU area size: "
1257 << fruAreaAvailableSize << "\n";
1258 return false;
1259#endif // ENABLE_FRU_AREA_RESIZE
1260 }
1261
Joshi-Mansid73b7442020-03-27 19:10:21 +05301262 // write new requested property field length and data
1263 constexpr uint8_t newTypeLenMask = 0xC0;
1264 fruData[fruUpdateFieldLoc] =
1265 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
1266 fruUpdateFieldLoc++;
1267 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
1268 fruData.begin() + fruUpdateFieldLoc);
1269
1270 // Copy remaining data to main fru area - post updated fru field vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001271 restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen;
1272 size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size();
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001273 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
1274 fruData.begin() + restFRUFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301275
1276 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001277 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
1278 fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301279
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001280#ifdef ENABLE_FRU_AREA_RESIZE
1281 ++nextFRUAreaNewLoc;
1282 ssize_t nextFRUAreaOffsetDiff =
1283 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
1284 // Append rest FRU Areas if size changed and there were other sections after
1285 // updated one
1286 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1287 {
1288 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1289 fruData.begin() + nextFRUAreaNewLoc);
1290 // Update Common Header
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001291 for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord;
1292 fruArea++)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001293 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001294 unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001295 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
1296 if (curFRUAreaOffset > fruAreaOffsetFieldValue)
1297 {
1298 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1299 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1300 }
1301 }
1302 // Calculate new checksum
1303 std::vector<uint8_t> headerFRUData;
1304 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1305 size_t checksumVal = calculateChecksum(headerFRUData);
1306 fruData[7] = static_cast<uint8_t>(checksumVal);
1307 // fill zeros if FRU Area size decreased
1308 if (nextFRUAreaOffsetDiff < 0)
1309 {
1310 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1311 restFRUAreasData.size(),
1312 fruData.end(), 0);
1313 }
1314 }
1315#else
1316 // this is to avoid "unused variable" warning
1317 (void)nextFRUAreaNewLoc;
1318#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301319 if (fruData.empty())
1320 {
1321 return false;
1322 }
1323
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001324 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301325 fruData))
1326 {
1327 return false;
1328 }
1329
1330 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301331 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1332 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301333 return true;
1334}
1335
James Feist98132792019-07-09 13:29:09 -07001336int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001337{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301338 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1339 sdbusplus::asio::object_server objServer(systemBus);
1340
Kumar Thangavel41a90512021-11-24 15:44:20 +05301341 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301342 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001343 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001344 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001345 std::vector<fs::path> i2cBuses;
1346
James Feista3c180a2018-08-09 16:06:04 -07001347 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001348 {
1349 std::cerr << "unable to find i2c devices\n";
1350 return 1;
1351 }
James Feist3cb5fec2018-01-23 14:41:51 -08001352
Patrick Venture11f1ff42019-08-01 10:42:12 -07001353 // check for and load blacklist with initial buses.
1354 loadBlacklist(blacklistPath);
1355
Vijay Khemka065f6d92018-12-18 10:37:47 -08001356 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001357
James Feist6ebf9de2018-05-15 15:01:17 -07001358 // this is a map with keys of pair(bus number, address) and values of
1359 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001360 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001361 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1362 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001363
James Feist9eb0b582018-04-27 12:15:46 -07001364 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1365 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1366 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001367
Kumar Thangavel41a90512021-11-24 15:44:20 +05301368 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301369 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1370 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301371 });
James Feist2a9d6db2018-04-27 15:48:28 -07001372
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001373 iface->register_method("ReScanBus", [&](uint8_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301374 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301375 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001376 });
1377
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001378 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001379
1380 iface->register_method("WriteFru", [&](const uint8_t bus,
1381 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001382 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001383 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001384 {
James Feistddb78302018-09-06 11:45:42 -07001385 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001386 return;
1387 }
1388 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301389 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1390 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001391 });
James Feist9eb0b582018-04-27 12:15:46 -07001392 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001393
James Feist9eb0b582018-04-27 12:15:46 -07001394 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -08001395 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -08001396 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001397 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001398 std::string,
1399 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001400 values;
1401 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001402 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001403 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001404 {
James Feistcb5661d2020-06-12 15:05:01 -07001405 powerIsOn = boost::ends_with(
1406 std::get<std::string>(findState->second), "Running");
James Feist0eeb79c2019-10-09 13:35:29 -07001407 }
James Feist6ebf9de2018-05-15 15:01:17 -07001408
James Feistcb5661d2020-06-12 15:05:01 -07001409 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001410 {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301411 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301412 powerIsOn, objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001413 }
James Feist918e18c2018-02-13 15:51:07 -08001414 };
James Feist9eb0b582018-04-27 12:15:46 -07001415
1416 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -08001417 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001418 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001419 "openbmc_project/state/"
1420 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001421 eventHandler);
1422
James Feist4131aea2018-03-09 09:47:30 -08001423 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001424 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -08001425 std::array<char, 4096> readBuffer;
James Feist4131aea2018-03-09 09:47:30 -08001426 // monitor for new i2c devices
1427 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1428 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001429 watchI2cBusses = [&](const boost::system::error_code& ec,
Ed Tanous07d467b2021-02-23 14:48:37 -08001430 std::size_t bytesTransferred) {
James Feist4131aea2018-03-09 09:47:30 -08001431 if (ec)
1432 {
1433 std::cout << "Callback Error " << ec << "\n";
1434 return;
1435 }
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001436 size_t index = 0;
1437 while ((index + sizeof(inotify_event)) <= bytesTransferred)
James Feist4131aea2018-03-09 09:47:30 -08001438 {
James Feista465ccc2019-02-08 12:51:01 -08001439 const inotify_event* iEvent =
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001440 reinterpret_cast<const inotify_event*>(&readBuffer[index]);
James Feist4131aea2018-03-09 09:47:30 -08001441 switch (iEvent->mask)
1442 {
James Feist9eb0b582018-04-27 12:15:46 -07001443 case IN_CREATE:
1444 case IN_MOVED_TO:
1445 case IN_DELETE:
James Feist5d7f4692020-06-17 18:22:33 -07001446 std::string name(iEvent->name);
1447 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001448 {
James Feist5d7f4692020-06-17 18:22:33 -07001449 int bus = busStrToInt(name);
1450 if (bus < 0)
1451 {
1452 std::cerr << "Could not parse bus " << name
1453 << "\n";
1454 continue;
1455 }
1456 rescanOneBus(busMap, static_cast<uint8_t>(bus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301457 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301458 unknownBusObjectCount, powerIsOn,
1459 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001460 }
James Feist4131aea2018-03-09 09:47:30 -08001461 }
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001462 index += sizeof(inotify_event) + iEvent->len;
James Feist4131aea2018-03-09 09:47:30 -08001463 }
James Feist6ebf9de2018-05-15 15:01:17 -07001464
James Feist4131aea2018-03-09 09:47:30 -08001465 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1466 watchI2cBusses);
1467 };
1468
1469 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001470 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301471 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1472 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001473
James Feist3cb5fec2018-01-23 14:41:51 -08001474 io.run();
1475 return 0;
1476}