blob: b503d80cfc1bd073bad4fa913058daefcc6bf7a0 [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Brad Bishope45d8c72022-05-25 15:12:53 -040016/// \file fru_device.cpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
Brad Bishope45d8c72022-05-25 15:12:53 -040018#include "fru_utils.hpp"
19#include "utils.hpp"
Patrick Venturea49dc332019-10-26 08:32:02 -070020
James Feist3cb5fec2018-01-23 14:41:51 -080021#include <fcntl.h>
James Feist3b860982018-10-02 14:34:07 -070022#include <sys/inotify.h>
23#include <sys/ioctl.h>
24
James Feist3b860982018-10-02 14:34:07 -070025#include <boost/algorithm/string/predicate.hpp>
Brad Bishop82c84bb2020-08-26 14:12:50 -040026#include <boost/asio/deadline_timer.hpp>
27#include <boost/asio/io_service.hpp>
James Feist3b860982018-10-02 14:34:07 -070028#include <boost/container/flat_map.hpp>
James Feist8c505da2020-05-28 10:06:33 -070029#include <nlohmann/json.hpp>
30#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
32
33#include <array>
Ed Tanous07d467b2021-02-23 14:48:37 -080034#include <cerrno>
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
Zev Weiss309c0b12022-02-25 01:44:12 +0000119static int64_t readFromEeprom(int fd, off_t offset, size_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700120{
121 auto result = lseek(fd, offset, SEEK_SET);
122 if (result < 0)
123 {
124 std::cerr << "failed to seek\n";
125 return -1;
126 }
127
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700128 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700129}
130
James Feist5d7f4692020-06-17 18:22:33 -0700131static int busStrToInt(const std::string& busName)
132{
Ed Tanous07d467b2021-02-23 14:48:37 -0800133 auto findBus = busName.rfind('-');
James Feist5d7f4692020-06-17 18:22:33 -0700134 if (findBus == std::string::npos)
135 {
136 return -1;
137 }
138 return std::stoi(busName.substr(findBus + 1));
139}
140
James Feist7972bb92019-11-13 15:59:24 -0800141static int getRootBus(size_t bus)
142{
143 auto ec = std::error_code();
144 auto path = std::filesystem::read_symlink(
145 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
146 std::to_string(bus) + "/mux_device"),
147 ec);
148 if (ec)
149 {
150 return -1;
151 }
152
153 std::string filename = path.filename();
Ed Tanous07d467b2021-02-23 14:48:37 -0800154 auto findBus = filename.find('-');
James Feist7972bb92019-11-13 15:59:24 -0800155 if (findBus == std::string::npos)
156 {
157 return -1;
158 }
159 return std::stoi(filename.substr(0, findBus));
160}
161
James Feistc95cb142018-02-26 10:41:42 -0800162static bool isMuxBus(size_t bus)
163{
Ed Tanous072e25d2018-12-16 21:45:20 -0800164 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800165 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
166}
167
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530168static void makeProbeInterface(size_t bus, size_t address,
169 sdbusplus::asio::object_server& objServer)
James Feist8a983922019-10-24 17:11:56 -0700170{
171 if (isMuxBus(bus))
172 {
173 return; // the mux buses are random, no need to publish
174 }
175 auto [it, success] = foundDevices.emplace(
176 std::make_pair(bus, address),
177 objServer.add_interface(
178 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
179 std::to_string(address),
180 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
181 if (!success)
182 {
183 return; // already added
184 }
185 it->second->register_property("Bus", bus);
186 it->second->register_property("Address", address);
187 it->second->initialize();
188}
189
Zev Weiss309c0b12022-02-25 01:44:12 +0000190static std::optional<bool> isDevice16Bit(int file)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800191{
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700192 // Set the higher data word address bits to 0. It's safe on 8-bit addressing
193 // EEPROMs because it doesn't write any actual data.
194 int ret = i2c_smbus_write_byte(file, 0);
195 if (ret < 0)
196 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000197 return std::nullopt;
Jae Hyun Yoof805baf2022-04-01 16:22:28 -0700198 }
199
Vijay Khemka2d681f62018-11-06 15:51:00 -0800200 /* Get first byte */
201 int byte1 = i2c_smbus_read_byte_data(file, 0);
202 if (byte1 < 0)
203 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000204 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800205 }
206 /* Read 7 more bytes, it will read same first byte in case of
207 * 8 bit but it will read next byte in case of 16 bit
208 */
209 for (int i = 0; i < 7; i++)
210 {
211 int byte2 = i2c_smbus_read_byte_data(file, 0);
212 if (byte2 < 0)
213 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000214 return std::nullopt;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800215 }
216 if (byte2 != byte1)
217 {
Zev Weiss309c0b12022-02-25 01:44:12 +0000218 return true;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800219 }
220 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000221 return false;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800222}
223
Xiang Liu2801a702020-01-20 14:29:34 -0800224// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
225// from_slave_buf_len bytes.
Ed Tanous07d467b2021-02-23 14:48:37 -0800226static int i2cSmbusWriteThenRead(int file, uint16_t address,
227 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
228 uint8_t* fromSlaveBuf, uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800229{
Ed Tanous07d467b2021-02-23 14:48:37 -0800230 if (toSlaveBuf == nullptr || toSlaveBufLen == 0 ||
231 fromSlaveBuf == nullptr || fromSlaveBufLen == 0)
Xiang Liu2801a702020-01-20 14:29:34 -0800232 {
233 return -1;
234 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800235
Xiang Liu2801a702020-01-20 14:29:34 -0800236#define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2
237 struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT];
238 struct i2c_rdwr_ioctl_data rdwr;
239
240 msgs[0].addr = address;
241 msgs[0].flags = 0;
242 msgs[0].len = toSlaveBufLen;
243 msgs[0].buf = toSlaveBuf;
244 msgs[1].addr = address;
245 msgs[1].flags = I2C_M_RD;
246 msgs[1].len = fromSlaveBufLen;
247 msgs[1].buf = fromSlaveBuf;
248
249 rdwr.msgs = msgs;
250 rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT;
251
252 int ret = ioctl(file, I2C_RDWR, &rdwr);
253
Zev Weiss07108282022-03-01 18:51:31 -0800254 return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? msgs[1].len : -1;
Xiang Liu2801a702020-01-20 14:29:34 -0800255}
256
Zev Weiss309c0b12022-02-25 01:44:12 +0000257static int64_t readBlockData(bool is16bit, int file, uint16_t address,
258 off_t offset, size_t len, uint8_t* buf)
Xiang Liu2801a702020-01-20 14:29:34 -0800259{
Zev Weiss309c0b12022-02-25 01:44:12 +0000260 if (!is16bit)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800261 {
Xiang Liu2801a702020-01-20 14:29:34 -0800262 return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset),
263 len, buf);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800264 }
265
Xiang Liu2801a702020-01-20 14:29:34 -0800266 offset = htobe16(offset);
Ed Tanous07d467b2021-02-23 14:48:37 -0800267 return i2cSmbusWriteThenRead(
Xiang Liu2801a702020-01-20 14:29:34 -0800268 file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800269}
270
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700271// TODO: This code is very similar to the non-eeprom version and can be merged
272// with some tweaks.
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300273static std::vector<uint8_t> processEeprom(int bus, int address)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700274{
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700275 auto path = getEepromPath(bus, address);
276
277 int file = open(path.c_str(), O_RDONLY);
278 if (file < 0)
279 {
280 std::cerr << "Unable to open eeprom file: " << path << "\n";
Patrick Venturebaba7672019-10-26 09:26:41 -0700281 return {};
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700282 }
283
Patrick Venturebaba7672019-10-26 09:26:41 -0700284 std::string errorMessage = "eeprom at " + std::to_string(bus) +
285 " address " + std::to_string(address);
Zev Weiss309c0b12022-02-25 01:44:12 +0000286 auto readFunc = [file](off_t offset, size_t length, uint8_t* outbuf) {
287 return readFromEeprom(file, offset, length, outbuf);
288 };
289 FRUReader reader(std::move(readFunc));
290 std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700291
292 close(file);
293 return device;
294}
295
Ed Tanous07d467b2021-02-23 14:48:37 -0800296std::set<int> findI2CEeproms(int i2cBus,
297 const std::shared_ptr<DeviceMap>& devices)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700298{
299 std::set<int> foundList;
300
301 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
302
303 // For each file listed under the i2c device
304 // NOTE: This should be faster than just checking for each possible address
305 // path.
306 for (const auto& p : fs::directory_iterator(path))
307 {
308 const std::string node = p.path().string();
309 std::smatch m;
310 bool found =
311 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
312
313 if (!found)
314 {
315 continue;
316 }
317 if (m.size() != 2)
318 {
319 std::cerr << "regex didn't capture\n";
320 continue;
321 }
322
323 std::ssub_match subMatch = m[1];
324 std::string addressString = subMatch.str();
325
326 std::size_t ignored;
327 const int hexBase = 16;
328 int address = std::stoi(addressString, &ignored, hexBase);
329
330 const std::string eeprom = node + "/eeprom";
331
332 try
333 {
334 if (!fs::exists(eeprom))
335 {
336 continue;
337 }
338 }
339 catch (...)
340 {
341 continue;
342 }
343
344 // There is an eeprom file at this address, it may have invalid
345 // contents, but we found it.
346 foundList.insert(address);
347
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300348 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700349 if (!device.empty())
350 {
351 devices->emplace(address, device);
352 }
353 }
354
355 return foundList;
356}
357
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300358int getBusFRUs(int file, int first, int last, int bus,
Jonathan Doman2418a612022-02-14 18:20:58 -0800359 std::shared_ptr<DeviceMap> devices, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530360 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800361{
James Feist3cb5fec2018-01-23 14:41:51 -0800362
James Feist26c27ad2018-07-25 15:09:40 -0700363 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700364 // NOTE: When reading the devices raw on the bus, it can interfere with
365 // the driver's ability to operate, therefore read eeproms first before
366 // scanning for devices without drivers. Several experiments were run
367 // and it was determined that if there were any devices on the bus
368 // before the eeprom was hit and read, the eeprom driver wouldn't open
369 // while the bus device was open. An experiment was not performed to see
370 // if this issue was resolved if the i2c bus device was closed, but
371 // hexdumps of the eeprom later were successful.
372
373 // Scan for i2c eeproms loaded on this bus.
374 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800375 std::set<size_t>& failedItems = failedAddresses[bus];
376
377 std::set<size_t>* rootFailures = nullptr;
378 int rootBus = getRootBus(bus);
379
380 if (rootBus >= 0)
381 {
382 rootFailures = &(failedAddresses[rootBus]);
383 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700384
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530385 constexpr int startSkipSlaveAddr = 0;
386 constexpr int endSkipSlaveAddr = 12;
387
James Feist26c27ad2018-07-25 15:09:40 -0700388 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800389 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700390 if (skipList.find(ii) != skipList.end())
391 {
392 continue;
393 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530394 // skipping since no device is present in this range
395 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
396 {
397 continue;
398 }
James Feist26c27ad2018-07-25 15:09:40 -0700399 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530400 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800401 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300402 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700403 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700404 continue;
405 }
406 // probe
Ed Tanous07d467b2021-02-23 14:48:37 -0800407 if (i2c_smbus_read_byte(file) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700408 {
409 continue;
410 }
James Feist3cb5fec2018-01-23 14:41:51 -0800411
Ed Tanous07d467b2021-02-23 14:48:37 -0800412 if (debug)
James Feist26c27ad2018-07-25 15:09:40 -0700413 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700414 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700415 << "\n";
416 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800417
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530418 makeProbeInterface(bus, ii, objServer);
James Feist8a983922019-10-24 17:11:56 -0700419
James Feist7972bb92019-11-13 15:59:24 -0800420 if (failedItems.find(ii) != failedItems.end())
421 {
422 // if we failed to read it once, unlikely we can read it later
423 continue;
424 }
425
426 if (rootFailures != nullptr)
427 {
428 if (rootFailures->find(ii) != rootFailures->end())
429 {
430 continue;
431 }
432 }
433
Vijay Khemka2d681f62018-11-06 15:51:00 -0800434 /* Check for Device type if it is 8 bit or 16 bit */
Zev Weiss309c0b12022-02-25 01:44:12 +0000435 std::optional<bool> is16Bit = isDevice16Bit(file);
436 if (!is16Bit.has_value())
Vijay Khemka2d681f62018-11-06 15:51:00 -0800437 {
438 std::cerr << "failed to read bus " << bus << " address " << ii
439 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700440 if (powerIsOn)
441 {
442 failedItems.insert(ii);
443 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800444 continue;
445 }
Zev Weiss309c0b12022-02-25 01:44:12 +0000446 bool is16BitBool{*is16Bit};
Vijay Khemka2d681f62018-11-06 15:51:00 -0800447
Zev Weiss309c0b12022-02-25 01:44:12 +0000448 auto readFunc = [is16BitBool, file, ii](off_t offset, size_t length,
449 uint8_t* outbuf) {
450 return readBlockData(is16BitBool, file, ii, offset, length,
451 outbuf);
452 };
453 FRUReader reader(std::move(readFunc));
Patrick Venturebaba7672019-10-26 09:26:41 -0700454 std::string errorMessage =
455 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Zev Weiss309c0b12022-02-25 01:44:12 +0000456 std::vector<uint8_t> device = readFRUContents(reader, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700457 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700458 {
James Feist26c27ad2018-07-25 15:09:40 -0700459 continue;
460 }
James Feist26c27ad2018-07-25 15:09:40 -0700461
Patrick Venture786f1792019-08-05 16:33:44 -0700462 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800463 }
James Feist26c27ad2018-07-25 15:09:40 -0700464 return 1;
465 });
466 std::future_status status =
467 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
468 if (status == std::future_status::timeout)
469 {
470 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700471 if (powerIsOn)
472 {
473 busBlacklist.insert(bus);
474 }
James Feist444830e2019-04-05 08:38:16 -0700475 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700476 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800477 }
478
James Feist444830e2019-04-05 08:38:16 -0700479 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700480 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800481}
482
Patrick Venture11f1ff42019-08-01 10:42:12 -0700483void loadBlacklist(const char* path)
484{
485 std::ifstream blacklistStream(path);
486 if (!blacklistStream.good())
487 {
488 // File is optional.
489 std::cerr << "Cannot open blacklist file.\n\n";
490 return;
491 }
492
493 nlohmann::json data =
494 nlohmann::json::parse(blacklistStream, nullptr, false);
495 if (data.is_discarded())
496 {
497 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
498 "exiting\n";
499 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700500 }
501
502 // It's expected to have at least one field, "buses" that is an array of the
503 // buses by integer. Allow for future options to exclude further aspects,
504 // such as specific addresses or ranges.
505 if (data.type() != nlohmann::json::value_t::object)
506 {
507 std::cerr << "Illegal blacklist, expected to read dictionary\n";
508 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700509 }
510
511 // If buses field is missing, that's fine.
512 if (data.count("buses") == 1)
513 {
514 // Parse the buses array after a little validation.
515 auto buses = data.at("buses");
516 if (buses.type() != nlohmann::json::value_t::array)
517 {
518 // Buses field present but invalid, therefore this is an error.
519 std::cerr << "Invalid contents for blacklist buses field\n";
520 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700521 }
522
523 // Catch exception here for type mis-match.
524 try
525 {
526 for (const auto& bus : buses)
527 {
528 busBlacklist.insert(bus.get<size_t>());
529 }
530 }
531 catch (const nlohmann::detail::type_error& e)
532 {
533 // Type mis-match is a critical error.
534 std::cerr << "Invalid bus type: " << e.what() << "\n";
535 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700536 }
537 }
538
539 return;
540}
541
Ed Tanous07d467b2021-02-23 14:48:37 -0800542static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800543 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530544 sdbusplus::asio::object_server& objServer)
James Feist3cb5fec2018-01-23 14:41:51 -0800545{
James Feista465ccc2019-02-08 12:51:01 -0800546 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800547 {
James Feist5d7f4692020-06-17 18:22:33 -0700548 int bus = busStrToInt(i2cBus);
James Feist0c3980a2019-12-19 11:09:27 -0800549
James Feist5d7f4692020-06-17 18:22:33 -0700550 if (bus < 0)
551 {
552 std::cerr << "Cannot translate " << i2cBus << " to int\n";
553 continue;
554 }
James Feist444830e2019-04-05 08:38:16 -0700555 if (busBlacklist.find(bus) != busBlacklist.end())
556 {
557 continue; // skip previously failed busses
558 }
James Feist3cb5fec2018-01-23 14:41:51 -0800559
James Feist0c3980a2019-12-19 11:09:27 -0800560 int rootBus = getRootBus(bus);
561 if (busBlacklist.find(rootBus) != busBlacklist.end())
562 {
563 continue;
564 }
565
James Feist3cb5fec2018-01-23 14:41:51 -0800566 auto file = open(i2cBus.c_str(), O_RDWR);
567 if (file < 0)
568 {
569 std::cerr << "unable to open i2c device " << i2cBus.string()
570 << "\n";
571 continue;
572 }
573 unsigned long funcs = 0;
574
575 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
576 {
577 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700578 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800579 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700580 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800581 continue;
582 }
583 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
584 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
585 {
586 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
587 << bus << "\n";
588 continue;
589 }
James Feist98132792019-07-09 13:29:09 -0700590 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800591 device = std::make_shared<DeviceMap>();
592
Nikhil Potaded8884f12019-03-27 13:27:13 -0700593 // i2cdetect by default uses the range 0x03 to 0x77, as
594 // this is what we have tested with, use this range. Could be
595 // changed in future.
Ed Tanous07d467b2021-02-23 14:48:37 -0800596 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800597 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700598 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800599 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700600
601 // fd is closed in this function in case the bus locks up
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530602 getBusFRUs(file, 0x03, 0x77, bus, device, powerIsOn, objServer);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700603
Ed Tanous07d467b2021-02-23 14:48:37 -0800604 if (debug)
James Feistc95cb142018-02-26 10:41:42 -0800605 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700606 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800607 }
James Feist3cb5fec2018-01-23 14:41:51 -0800608 }
James Feist3cb5fec2018-01-23 14:41:51 -0800609}
610
James Feist6ebf9de2018-05-15 15:01:17 -0700611// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700612struct FindDevicesWithCallback :
613 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700614{
James Feista465ccc2019-02-08 12:51:01 -0800615 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
Jonathan Doman2418a612022-02-14 18:20:58 -0800616 BusMap& busmap, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530617 sdbusplus::asio::object_server& objServer,
James Feista465ccc2019-02-08 12:51:01 -0800618 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700619 _i2cBuses(i2cBuses),
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530620 _busMap(busmap), _powerIsOn(powerIsOn), _objServer(objServer),
621 _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700622 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700623 ~FindDevicesWithCallback()
624 {
625 _callback();
626 }
627 void run()
628 {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530629 findI2CDevices(_i2cBuses, _busMap, _powerIsOn, _objServer);
James Feist6ebf9de2018-05-15 15:01:17 -0700630 }
631
James Feista465ccc2019-02-08 12:51:01 -0800632 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800633 BusMap& _busMap;
Jonathan Doman2418a612022-02-14 18:20:58 -0800634 const bool& _powerIsOn;
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530635 sdbusplus::asio::object_server& _objServer;
James Feist6ebf9de2018-05-15 15:01:17 -0700636 std::function<void(void)> _callback;
637};
638
Ed Tanous07d467b2021-02-23 14:48:37 -0800639void addFruObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300640 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800641 boost::container::flat_map<
642 std::pair<size_t, size_t>,
643 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +0530644 uint32_t bus, uint32_t address, size_t& unknownBusObjectCount,
Jonathan Doman2418a612022-02-14 18:20:58 -0800645 const bool& powerIsOn, sdbusplus::asio::object_server& objServer,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530646 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist3cb5fec2018-01-23 14:41:51 -0800647{
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300648 boost::container::flat_map<std::string, std::string> formattedFRU;
Michael Shen0961b112022-02-22 11:06:33 +0800649 resCodes res = formatIPMIFRU(device, formattedFRU);
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300650 if (res == resCodes::resErr)
James Feist3cb5fec2018-01-23 14:41:51 -0800651 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300652 std::cerr << "failed to parse FRU for device at bus " << bus
Patrick Ventureb755c832019-08-07 11:09:14 -0700653 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800654 return;
655 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800656 if (res == resCodes::resWarn)
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300657 {
658 std::cerr << "there were warnings while parsing FRU for device at bus "
659 << bus << " address " << address << "\n";
660 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700661
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300662 auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800663 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700664 // Not found under Board section or 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 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300668 productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME");
James Feist3cb5fec2018-01-23 14:41:51 -0800669 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700670 // Found under Product section and not an empty string.
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300671 if (productNameFind != formattedFRU.end() &&
Patrick Venture96cdaef2019-07-30 13:30:52 -0700672 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800673 {
674 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800675 std::regex illegalObject("[^A-Za-z0-9_]");
676 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800677 }
678 else
679 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800680 productName = "UNKNOWN" + std::to_string(unknownBusObjectCount);
681 unknownBusObjectCount++;
James Feist3cb5fec2018-01-23 14:41:51 -0800682 }
683
James Feist918e18c2018-02-13 15:51:07 -0800684 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800685 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700686 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800687 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700688 int highest = -1;
689 bool found = false;
690
James Feista465ccc2019-02-08 12:51:01 -0800691 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800692 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700693 std::string path = busIface.second->get_object_path();
694 if (std::regex_match(path, std::regex(productName + "(_\\d+|)$")))
James Feist918e18c2018-02-13 15:51:07 -0800695 {
Jiaqing Zhao8482ffc2022-05-20 16:06:54 +0800696 if (isMuxBus(bus) && bus != busIface.first.first &&
697 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 Thangavel7f43ea82021-12-07 17:20:16 +0530759 &unknownBusObjectCount, &powerIsOn, &objServer,
760 &systemBus](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 Thangavel7f43ea82021-12-07 17:20:16 +0530766 unknownBusObjectCount, powerIsOn,
767 objServer, systemBus))
Joshi-Mansid73b7442020-03-27 19:10:21 +0530768 {
769 resp = req;
770 }
771 else
772 {
773 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300774 "FRU property update failed.");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530775 }
776 }
777 return 1;
778 });
779 }
780 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700781 {
782 std::cerr << "illegal key: " << key << "\n";
783 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800784 if (debug)
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700785 {
786 std::cout << property.first << ": " << property.second << "\n";
787 }
James Feist3cb5fec2018-01-23 14:41:51 -0800788 }
James Feist2a9d6db2018-04-27 15:48:28 -0700789
790 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700791 iface->register_property("BUS", bus);
792 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700793
James Feist9eb0b582018-04-27 12:15:46 -0700794 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800795}
796
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300797static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800798{
799 // try to read baseboard fru from file
Ed Tanous07d467b2021-02-23 14:48:37 -0800800 std::ifstream baseboardFRUFile(baseboardFruLocation, std::ios::binary);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300801 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800802 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300803 baseboardFRUFile.seekg(0, std::ios_base::end);
804 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
805 baseboardFRU.resize(fileSize);
806 baseboardFRUFile.seekg(0, std::ios_base::beg);
807 baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()),
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300808 fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800809 }
810 else
811 {
812 return false;
813 }
814 return true;
815}
816
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300817bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700818{
819 boost::container::flat_map<std::string, std::string> tmp;
Ed Tanous07d467b2021-02-23 14:48:37 -0800820 if (fru.size() > maxFruSize)
James Feistb49ffc32018-05-02 11:10:43 -0700821 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300822 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700823 return false;
824 }
825 // verify legal fru by running it through fru parsing logic
Michael Shen0961b112022-02-22 11:06:33 +0800826 if (formatIPMIFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700827 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300828 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700829 return false;
830 }
831 // baseboard fru
832 if (bus == 0 && address == 0)
833 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800834 std::ofstream file(baseboardFruLocation, std::ios_base::binary);
James Feistb49ffc32018-05-02 11:10:43 -0700835 if (!file.good())
836 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800837 std::cerr << "Error opening file " << baseboardFruLocation << "\n";
James Feistddb78302018-09-06 11:45:42 -0700838 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700839 return false;
840 }
James Feista465ccc2019-02-08 12:51:01 -0800841 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700842 return file.good();
843 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800844
845 if (hasEepromFile(bus, address))
James Feistb49ffc32018-05-02 11:10:43 -0700846 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800847 auto path = getEepromPath(bus, address);
848 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
849 if (eeprom < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700850 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800851 std::cerr << "unable to open i2c device " << path << "\n";
852 throw DBusInternalError();
853 return false;
854 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700855
Ed Tanous07d467b2021-02-23 14:48:37 -0800856 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
857 if (writtenBytes < 0)
858 {
859 std::cerr << "unable to write to i2c device " << path << "\n";
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700860 close(eeprom);
James Feistddb78302018-09-06 11:45:42 -0700861 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700862 return false;
863 }
864
Ed Tanous07d467b2021-02-23 14:48:37 -0800865 close(eeprom);
James Feistb49ffc32018-05-02 11:10:43 -0700866 return true;
867 }
Ed Tanous07d467b2021-02-23 14:48:37 -0800868
869 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
870
871 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
872 if (file < 0)
873 {
874 std::cerr << "unable to open i2c device " << i2cBus << "\n";
875 throw DBusInternalError();
876 return false;
877 }
878 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
879 {
880 std::cerr << "unable to set device address\n";
881 close(file);
882 throw DBusInternalError();
883 return false;
884 }
885
886 constexpr const size_t retryMax = 2;
887 uint16_t index = 0;
888 size_t retries = retryMax;
889 while (index < fru.size())
890 {
891 if ((index && ((index % (maxEepromPageIndex + 1)) == 0)) &&
892 (retries == retryMax))
893 {
894 // The 4K EEPROM only uses the A2 and A1 device address bits
895 // with the third bit being a memory page address bit.
896 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
897 {
898 std::cerr << "unable to set device address\n";
899 close(file);
900 throw DBusInternalError();
901 return false;
902 }
903 }
904
905 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
906 fru[index]) < 0)
907 {
908 if (!retries--)
909 {
910 std::cerr << "error writing fru: " << strerror(errno) << "\n";
911 close(file);
912 throw DBusInternalError();
913 return false;
914 }
915 }
916 else
917 {
918 retries = retryMax;
919 index++;
920 }
921 // most eeproms require 5-10ms between writes
922 std::this_thread::sleep_for(std::chrono::milliseconds(10));
923 }
924 close(file);
925 return true;
James Feistb49ffc32018-05-02 11:10:43 -0700926}
927
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800928void rescanOneBus(
929 BusMap& busmap, uint8_t busNum,
930 boost::container::flat_map<
931 std::pair<size_t, size_t>,
James Feist5d7f4692020-06-17 18:22:33 -0700932 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800933 bool dbusCall, size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530934 sdbusplus::asio::object_server& objServer,
935 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800936{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800937 for (auto& [pair, interface] : foundDevices)
938 {
939 if (pair.first == static_cast<size_t>(busNum))
940 {
941 objServer.remove_interface(interface);
942 foundDevices.erase(pair);
943 }
944 }
945
James Feist5d7f4692020-06-17 18:22:33 -0700946 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
947 if (!fs::exists(busPath))
948 {
949 if (dbusCall)
950 {
951 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
952 << "\n";
953 throw std::invalid_argument("Invalid Bus.");
954 }
955 return;
956 }
957
958 std::vector<fs::path> i2cBuses;
959 i2cBuses.emplace_back(busPath);
960
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800961 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530962 i2cBuses, busmap, powerIsOn, objServer,
963 [busNum, &busmap, &dbusInterfaceMap, &unknownBusObjectCount, &powerIsOn,
964 &objServer, &systemBus]() {
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800965 for (auto& busIface : dbusInterfaceMap)
966 {
967 if (busIface.first.first == static_cast<size_t>(busNum))
968 {
969 objServer.remove_interface(busIface.second);
970 }
971 }
Wludzik, Jozefc1136b72020-06-24 11:58:32 +0200972 auto found = busmap.find(busNum);
973 if (found == busmap.end() || found->second == nullptr)
974 {
975 return;
976 }
977 for (auto& device : *(found->second))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800978 {
Ed Tanous07d467b2021-02-23 14:48:37 -0800979 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530980 static_cast<uint32_t>(busNum), device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530981 unknownBusObjectCount, powerIsOn, objServer,
982 systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800983 }
984 });
985 scan->run();
986}
987
James Feist9eb0b582018-04-27 12:15:46 -0700988void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700989 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800990 boost::container::flat_map<
991 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +0530992 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -0800993 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +0530994 sdbusplus::asio::object_server& objServer,
995 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
James Feist918e18c2018-02-13 15:51:07 -0800996{
James Feist6ebf9de2018-05-15 15:01:17 -0700997 static boost::asio::deadline_timer timer(io);
998 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800999
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001000 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -07001001 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -08001002 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001003 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001004
Nikhil Potaded8884f12019-03-27 13:27:13 -07001005 boost::container::flat_map<size_t, fs::path> busPaths;
1006 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001007 {
James Feist4131aea2018-03-09 09:47:30 -08001008 std::cerr << "unable to find i2c devices\n";
1009 return;
James Feist918e18c2018-02-13 15:51:07 -08001010 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001011
Ed Tanous07d467b2021-02-23 14:48:37 -08001012 for (const auto& busPath : busPaths)
Nikhil Potaded8884f12019-03-27 13:27:13 -07001013 {
1014 i2cBuses.emplace_back(busPath.second);
1015 }
James Feist4131aea2018-03-09 09:47:30 -08001016
James Feist98132792019-07-09 13:29:09 -07001017 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001018 for (auto& [pair, interface] : foundDevices)
1019 {
1020 objServer.remove_interface(interface);
1021 }
1022 foundDevices.clear();
1023
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301024 auto scan = std::make_shared<FindDevicesWithCallback>(
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301025 i2cBuses, busmap, powerIsOn, objServer, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001026 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001027 {
1028 objServer.remove_interface(busIface.second);
1029 }
James Feist4131aea2018-03-09 09:47:30 -08001030
James Feist6ebf9de2018-05-15 15:01:17 -07001031 dbusInterfaceMap.clear();
Ed Tanous07d467b2021-02-23 14:48:37 -08001032 unknownBusObjectCount = 0;
James Feist4131aea2018-03-09 09:47:30 -08001033
James Feist6ebf9de2018-05-15 15:01:17 -07001034 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001035 std::vector<uint8_t> baseboardFRU;
1036 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001037 {
Helen Huangf69fe902021-02-19 16:18:22 +08001038 // If no device on i2c bus 0, the insertion will happen.
1039 auto bus0 =
1040 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1041 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001042 }
James Feist98132792019-07-09 13:29:09 -07001043 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001044 {
James Feista465ccc2019-02-08 12:51:01 -08001045 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001046 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001047 addFruObjectToDbus(device.second, dbusInterfaceMap,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301048 devicemap.first, device.first,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301049 unknownBusObjectCount, powerIsOn,
1050 objServer, systemBus);
James Feist6ebf9de2018-05-15 15:01:17 -07001051 }
1052 }
1053 });
1054 scan->run();
1055 });
James Feist918e18c2018-02-13 15:51:07 -08001056}
1057
Joshi-Mansid73b7442020-03-27 19:10:21 +05301058// Details with example of Asset Tag Update
1059// To find location of Product Info Area asset tag as per FRU specification
1060// 1. Find product Info area starting offset (*8 - as header will be in
1061// multiple of 8 bytes).
1062// 2. Skip 3 bytes of product info area (like format version, area length,
1063// and language code).
1064// 3. Traverse manufacturer name, product name, product version, & product
1065// serial number, by reading type/length code to reach the Asset Tag.
1066// 4. Update the Asset Tag, reposition the product Info area in multiple of
1067// 8 bytes. Update the Product area length and checksum.
1068
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001069bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301070 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
Ed Tanous07d467b2021-02-23 14:48:37 -08001071 const std::string& propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +05301072 boost::container::flat_map<
1073 std::pair<size_t, size_t>,
Kumar Thangavel41a90512021-11-24 15:44:20 +05301074 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
Jonathan Doman2418a612022-02-14 18:20:58 -08001075 size_t& unknownBusObjectCount, const bool& powerIsOn,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301076 sdbusplus::asio::object_server& objServer,
1077 std::shared_ptr<sdbusplus::asio::connection>& systemBus)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301078{
1079 size_t updatePropertyReqLen = updatePropertyReq.length();
1080 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1081 {
1082 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001083 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301084 "Invalid Length "
1085 << updatePropertyReqLen << "\n";
1086 return false;
1087 }
1088
1089 std::vector<uint8_t> fruData;
1090 try
1091 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001092 fruData = getFRUInfo(static_cast<uint8_t>(bus),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301093 static_cast<uint8_t>(address));
1094 }
Patrick Williams83b1e9b2021-10-06 12:47:24 -05001095 catch (const std::invalid_argument& e)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301096 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001097 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301098 return false;
1099 }
1100
1101 if (fruData.empty())
1102 {
1103 return false;
1104 }
1105
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001106 const std::vector<std::string>* fruAreaFieldNames;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301107
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001108 uint8_t fruAreaOffsetFieldValue = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301109 size_t offset = 0;
Ed Tanous07d467b2021-02-23 14:48:37 -08001110 std::string areaName = propertyName.substr(0, propertyName.find('_'));
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001111 std::string propertyNamePrefix = areaName + "_";
Ed Tanous07d467b2021-02-23 14:48:37 -08001112 auto it = std::find(fruAreaNames.begin(), fruAreaNames.end(), areaName);
1113 if (it == fruAreaNames.end())
Joshi-Mansid73b7442020-03-27 19:10:21 +05301114 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001115 std::cerr << "Can't parse area name for property " << propertyName
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001116 << " \n";
1117 return false;
1118 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001119 fruAreas fruAreaToUpdate = static_cast<fruAreas>(it - fruAreaNames.begin());
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001120 fruAreaOffsetFieldValue =
1121 fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)];
1122 switch (fruAreaToUpdate)
1123 {
1124 case fruAreas::fruAreaChassis:
1125 offset = 3; // chassis part number offset. Skip fixed first 3 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001126 fruAreaFieldNames = &chassisFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001127 break;
1128 case fruAreas::fruAreaBoard:
1129 offset = 6; // board manufacturer offset. Skip fixed first 6 bytes
Ed Tanous07d467b2021-02-23 14:48:37 -08001130 fruAreaFieldNames = &boardFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001131 break;
1132 case fruAreas::fruAreaProduct:
1133 // Manufacturer name offset. Skip fixed first 3 product fru bytes
1134 // i.e. version, area length and language code
1135 offset = 3;
Ed Tanous07d467b2021-02-23 14:48:37 -08001136 fruAreaFieldNames = &productFruAreas;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001137 break;
1138 default:
1139 std::cerr << "Don't know how to handle property " << propertyName
1140 << " \n";
1141 return false;
1142 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001143 if (fruAreaOffsetFieldValue == 0)
1144 {
1145 std::cerr << "FRU Area for " << propertyName << " not present \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301146 return false;
1147 }
1148
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001149 size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001150 size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize;
1151 size_t fruAreaEnd = fruAreaStart + fruAreaSize;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001152 size_t fruDataIter = fruAreaStart + offset;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001153 size_t skipToFRUUpdateField = 0;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001154 ssize_t fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301155
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001156 bool found = false;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001157 for (auto& field : *fruAreaFieldNames)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301158 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001159 skipToFRUUpdateField++;
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001160 if (propertyName == propertyNamePrefix + field)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301161 {
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001162 found = true;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301163 break;
1164 }
1165 }
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001166 if (!found)
1167 {
Ed Tanous07d467b2021-02-23 14:48:37 -08001168 std::size_t pos = propertyName.find(fruCustomFieldName);
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001169 if (pos == std::string::npos)
1170 {
1171 std::cerr << "PropertyName doesn't exist in FRU Area Vectors: "
1172 << propertyName << "\n";
1173 return false;
1174 }
1175 std::string fieldNumStr =
Ed Tanous07d467b2021-02-23 14:48:37 -08001176 propertyName.substr(pos + fruCustomFieldName.length());
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001177 size_t fieldNum = std::stoi(fieldNumStr);
1178 if (fieldNum == 0)
1179 {
1180 std::cerr << "PropertyName not recognized: " << propertyName
1181 << "\n";
1182 return false;
1183 }
1184 skipToFRUUpdateField += fieldNum;
1185 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301186
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001187 for (size_t i = 1; i < skipToFRUUpdateField; i++)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301188 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001189 fieldLength = getFieldLength(fruData[fruDataIter]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001190 if (fieldLength < 0)
1191 {
1192 break;
1193 }
1194 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301195 }
Ed Tanous07d467b2021-02-23 14:48:37 -08001196 size_t fruUpdateFieldLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301197
1198 // Push post update fru field bytes to a vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001199 fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001200 if (fieldLength < 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301201 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001202 std::cerr << "Property " << propertyName << " not present \n";
1203 return false;
1204 }
1205 fruDataIter += 1 + fieldLength;
1206 size_t restFRUFieldsLoc = fruDataIter;
1207 size_t endOfFieldsLoc = 0;
1208 while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0)
1209 {
1210 if (fruDataIter >= (fruAreaStart + fruAreaSize))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301211 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001212 fruDataIter = fruAreaStart + fruAreaSize;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301213 break;
1214 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001215 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301216 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001217 endOfFieldsLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301218
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001219 std::vector<uint8_t> restFRUAreaFieldsData;
1220 std::copy_n(fruData.begin() + restFRUFieldsLoc,
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001221 endOfFieldsLoc - restFRUFieldsLoc + 1,
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001222 std::back_inserter(restFRUAreaFieldsData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301223
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001224 // Push post update fru areas if any
1225 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001226 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1227 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001228 {
1229 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001230 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001231 if ((fruAreaLoc > endOfFieldsLoc) &&
1232 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1233 {
1234 nextFRUAreaLoc = fruAreaLoc;
1235 }
1236 }
1237 std::vector<uint8_t> restFRUAreasData;
1238 if (nextFRUAreaLoc)
1239 {
1240 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1241 fruData.size() - nextFRUAreaLoc,
1242 std::back_inserter(restFRUAreasData));
1243 }
1244
1245 // check FRU area size
1246 size_t fruAreaDataSize =
1247 ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size());
1248 size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize;
1249 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1250 {
1251
1252#ifdef ENABLE_FRU_AREA_RESIZE
1253 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1254 // round size to 8-byte blocks
1255 newFRUAreaSize =
1256 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
1257 size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize;
1258 fruData.resize(newFRUDataSize);
1259 fruAreaSize = newFRUAreaSize;
1260 fruAreaEnd = fruAreaStart + fruAreaSize;
1261#else
1262 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1263 << " should not be greater than available FRU area size: "
1264 << fruAreaAvailableSize << "\n";
1265 return false;
1266#endif // ENABLE_FRU_AREA_RESIZE
1267 }
1268
Joshi-Mansid73b7442020-03-27 19:10:21 +05301269 // write new requested property field length and data
1270 constexpr uint8_t newTypeLenMask = 0xC0;
1271 fruData[fruUpdateFieldLoc] =
1272 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
1273 fruUpdateFieldLoc++;
1274 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
1275 fruData.begin() + fruUpdateFieldLoc);
1276
1277 // Copy remaining data to main fru area - post updated fru field vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001278 restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen;
1279 size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size();
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001280 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
1281 fruData.begin() + restFRUFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301282
1283 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001284 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
1285 fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301286
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001287#ifdef ENABLE_FRU_AREA_RESIZE
1288 ++nextFRUAreaNewLoc;
1289 ssize_t nextFRUAreaOffsetDiff =
1290 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
1291 // Append rest FRU Areas if size changed and there were other sections after
1292 // updated one
1293 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1294 {
1295 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1296 fruData.begin() + nextFRUAreaNewLoc);
1297 // Update Common Header
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001298 for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord;
1299 fruArea++)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001300 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001301 unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001302 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
1303 if (curFRUAreaOffset > fruAreaOffsetFieldValue)
1304 {
1305 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1306 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1307 }
1308 }
1309 // Calculate new checksum
1310 std::vector<uint8_t> headerFRUData;
1311 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1312 size_t checksumVal = calculateChecksum(headerFRUData);
1313 fruData[7] = static_cast<uint8_t>(checksumVal);
1314 // fill zeros if FRU Area size decreased
1315 if (nextFRUAreaOffsetDiff < 0)
1316 {
1317 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1318 restFRUAreasData.size(),
1319 fruData.end(), 0);
1320 }
1321 }
1322#else
1323 // this is to avoid "unused variable" warning
1324 (void)nextFRUAreaNewLoc;
1325#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301326 if (fruData.empty())
1327 {
1328 return false;
1329 }
1330
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001331 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301332 fruData))
1333 {
1334 return false;
1335 }
1336
1337 // Rescan the bus so that GetRawFru dbus-call fetches updated values
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301338 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1339 objServer, systemBus);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301340 return true;
1341}
1342
James Feist98132792019-07-09 13:29:09 -07001343int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001344{
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301345 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1346 sdbusplus::asio::object_server objServer(systemBus);
1347
Kumar Thangavel41a90512021-11-24 15:44:20 +05301348 static size_t unknownBusObjectCount = 0;
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301349 static bool powerIsOn = false;
James Feist3cb5fec2018-01-23 14:41:51 -08001350 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001351 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001352 std::vector<fs::path> i2cBuses;
1353
James Feista3c180a2018-08-09 16:06:04 -07001354 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001355 {
1356 std::cerr << "unable to find i2c devices\n";
1357 return 1;
1358 }
James Feist3cb5fec2018-01-23 14:41:51 -08001359
Patrick Venture11f1ff42019-08-01 10:42:12 -07001360 // check for and load blacklist with initial buses.
1361 loadBlacklist(blacklistPath);
1362
Vijay Khemka065f6d92018-12-18 10:37:47 -08001363 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001364
James Feist6ebf9de2018-05-15 15:01:17 -07001365 // this is a map with keys of pair(bus number, address) and values of
1366 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001367 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001368 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1369 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001370
James Feist9eb0b582018-04-27 12:15:46 -07001371 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1372 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1373 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001374
Kumar Thangavel41a90512021-11-24 15:44:20 +05301375 iface->register_method("ReScan", [&]() {
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301376 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1377 objServer, systemBus);
Kumar Thangavel41a90512021-11-24 15:44:20 +05301378 });
James Feist2a9d6db2018-04-27 15:48:28 -07001379
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001380 iface->register_method("ReScanBus", [&](uint8_t bus) {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301381 rescanOneBus(busMap, bus, dbusInterfaceMap, true, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301382 powerIsOn, objServer, systemBus);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001383 });
1384
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001385 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001386
1387 iface->register_method("WriteFru", [&](const uint8_t bus,
1388 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001389 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001390 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001391 {
James Feistddb78302018-09-06 11:45:42 -07001392 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001393 return;
1394 }
1395 // schedule rescan on success
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301396 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1397 objServer, systemBus);
James Feistb49ffc32018-05-02 11:10:43 -07001398 });
James Feist9eb0b582018-04-27 12:15:46 -07001399 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001400
Patrick Williams2af39222022-07-22 19:26:56 -05001401 std::function<void(sdbusplus::message_t & message)> eventHandler =
1402 [&](sdbusplus::message_t& message) {
James Feist918e18c2018-02-13 15:51:07 -08001403 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001404 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001405 std::string,
1406 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001407 values;
1408 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001409 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001410 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001411 {
James Feistcb5661d2020-06-12 15:05:01 -07001412 powerIsOn = boost::ends_with(
1413 std::get<std::string>(findState->second), "Running");
James Feist0eeb79c2019-10-09 13:35:29 -07001414 }
James Feist6ebf9de2018-05-15 15:01:17 -07001415
James Feistcb5661d2020-06-12 15:05:01 -07001416 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001417 {
Kumar Thangavel1e8e71f2021-12-01 19:39:11 +05301418 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301419 powerIsOn, objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001420 }
James Feist918e18c2018-02-13 15:51:07 -08001421 };
James Feist9eb0b582018-04-27 12:15:46 -07001422
Patrick Williams2af39222022-07-22 19:26:56 -05001423 sdbusplus::bus::match_t powerMatch = sdbusplus::bus::match_t(
1424 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001425 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001426 "openbmc_project/state/"
1427 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001428 eventHandler);
1429
James Feist4131aea2018-03-09 09:47:30 -08001430 int fd = inotify_init();
Ed Tanous07d467b2021-02-23 14:48:37 -08001431 inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -08001432 std::array<char, 4096> readBuffer;
James Feist4131aea2018-03-09 09:47:30 -08001433 // monitor for new i2c devices
1434 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1435 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001436 watchI2cBusses = [&](const boost::system::error_code& ec,
Ed Tanous07d467b2021-02-23 14:48:37 -08001437 std::size_t bytesTransferred) {
James Feist4131aea2018-03-09 09:47:30 -08001438 if (ec)
1439 {
1440 std::cout << "Callback Error " << ec << "\n";
1441 return;
1442 }
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001443 size_t index = 0;
1444 while ((index + sizeof(inotify_event)) <= bytesTransferred)
James Feist4131aea2018-03-09 09:47:30 -08001445 {
James Feista465ccc2019-02-08 12:51:01 -08001446 const inotify_event* iEvent =
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001447 reinterpret_cast<const inotify_event*>(&readBuffer[index]);
James Feist4131aea2018-03-09 09:47:30 -08001448 switch (iEvent->mask)
1449 {
James Feist9eb0b582018-04-27 12:15:46 -07001450 case IN_CREATE:
1451 case IN_MOVED_TO:
1452 case IN_DELETE:
James Feist5d7f4692020-06-17 18:22:33 -07001453 std::string name(iEvent->name);
1454 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001455 {
James Feist5d7f4692020-06-17 18:22:33 -07001456 int bus = busStrToInt(name);
1457 if (bus < 0)
1458 {
1459 std::cerr << "Could not parse bus " << name
1460 << "\n";
1461 continue;
1462 }
1463 rescanOneBus(busMap, static_cast<uint8_t>(bus),
Kumar Thangavel41a90512021-11-24 15:44:20 +05301464 dbusInterfaceMap, false,
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301465 unknownBusObjectCount, powerIsOn,
1466 objServer, systemBus);
James Feist9eb0b582018-04-27 12:15:46 -07001467 }
James Feist4131aea2018-03-09 09:47:30 -08001468 }
Jiaqing Zhao1dda2b32022-04-11 19:10:22 +08001469 index += sizeof(inotify_event) + iEvent->len;
James Feist4131aea2018-03-09 09:47:30 -08001470 }
James Feist6ebf9de2018-05-15 15:01:17 -07001471
James Feist4131aea2018-03-09 09:47:30 -08001472 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1473 watchI2cBusses);
1474 };
1475
1476 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001477 // run the initial scan
Kumar Thangavel7f43ea82021-12-07 17:20:16 +05301478 rescanBusses(busMap, dbusInterfaceMap, unknownBusObjectCount, powerIsOn,
1479 objServer, systemBus);
James Feist918e18c2018-02-13 15:51:07 -08001480
James Feist3cb5fec2018-01-23 14:41:51 -08001481 io.run();
1482 return 0;
1483}