blob: baab14e32bf6e96cf987a36b95255d45ca82a7ef [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 Feist3b860982018-10-02 14:34:07 -070021#include <errno.h>
James Feist3cb5fec2018-01-23 14:41:51 -080022#include <fcntl.h>
James Feist3b860982018-10-02 14:34:07 -070023#include <sys/inotify.h>
24#include <sys/ioctl.h>
25
James Feist3b860982018-10-02 14:34:07 -070026#include <boost/algorithm/string/predicate.hpp>
Brad Bishop82c84bb2020-08-26 14:12:50 -040027#include <boost/asio/deadline_timer.hpp>
28#include <boost/asio/io_service.hpp>
James Feist3b860982018-10-02 14:34:07 -070029#include <boost/container/flat_map.hpp>
James Feist8c505da2020-05-28 10:06:33 -070030#include <nlohmann/json.hpp>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33
34#include <array>
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>
James Feist3f8a2782018-02-12 09:24:42 -080044#include <regex>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070045#include <set>
46#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070047#include <string>
James Feist3b860982018-10-02 14:34:07 -070048#include <thread>
Jeremy Kerr4e6a62f2020-06-09 11:26:40 +080049#include <utility>
James Feista465ccc2019-02-08 12:51:01 -080050#include <variant>
Patrick Venturec274f2f2019-10-26 09:27:23 -070051#include <vector>
James Feist3b860982018-10-02 14:34:07 -070052
James Feist8c505da2020-05-28 10:06:33 -070053extern "C"
54{
James Feist3b860982018-10-02 14:34:07 -070055#include <i2c/smbus.h>
56#include <linux/i2c-dev.h>
57}
James Feist3cb5fec2018-01-23 14:41:51 -080058
Ed Tanous072e25d2018-12-16 21:45:20 -080059namespace fs = std::filesystem;
James Feist3cb5fec2018-01-23 14:41:51 -080060static constexpr bool DEBUG = false;
61static size_t UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feistb49ffc32018-05-02 11:10:43 -070062constexpr size_t MAX_FRU_SIZE = 512;
63constexpr size_t MAX_EEPROM_PAGE_INDEX = 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
James Feista465ccc2019-02-08 12:51:01 -080068const static constexpr char* BASEBOARD_FRU_LOCATION =
James Feist3cb5fec2018-01-23 14:41:51 -080069 "/etc/fru/baseboard.fru.bin";
70
James Feista465ccc2019-02-08 12:51:01 -080071const static constexpr char* I2C_DEV_LOCATION = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080072
Andrei Kartashev1c5b7062020-08-19 14:47:30 +030073using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>;
James Feist3cb5fec2018-01-23 14:41:51 -080074using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
75
James Feist444830e2019-04-05 08:38:16 -070076static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070077struct FindDevicesWithCallback;
78
Nikhil Potaded8884f12019-03-27 13:27:13 -070079static BusMap busMap;
80
James Feistcb5661d2020-06-12 15:05:01 -070081static bool powerIsOn = false;
82
James Feist8a983922019-10-24 17:11:56 -070083static boost::container::flat_map<
84 std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>>
85 foundDevices;
86
James Feist7972bb92019-11-13 15:59:24 -080087static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses;
88
James Feist5cb06082019-10-24 17:11:13 -070089boost::asio::io_service io;
90auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
91auto objServer = sdbusplus::asio::object_server(systemBus);
92
Andrei Kartashev2a7e3952020-09-02 20:32:26 +030093uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter,
94 std::vector<uint8_t>::const_iterator end);
Andrei Kartashev2f0de172020-08-13 14:29:40 +030095bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +053096 const std::string& assetTag, uint32_t bus, uint32_t address,
97 std::string propertyName,
98 boost::container::flat_map<
99 std::pair<size_t, size_t>,
100 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap);
Patrick Venturebaba7672019-10-26 09:26:41 -0700101
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700102// Given a bus/address, produce the path in sysfs for an eeprom.
103static std::string getEepromPath(size_t bus, size_t address)
104{
105 std::stringstream output;
106 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
107 << std::setfill('0') << std::setw(4) << std::hex << address
108 << "/eeprom";
109 return output.str();
110}
111
112static bool hasEepromFile(size_t bus, size_t address)
113{
114 auto path = getEepromPath(bus, address);
115 try
116 {
117 return fs::exists(path);
118 }
119 catch (...)
120 {
121 return false;
122 }
123}
124
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700125static int64_t readFromEeprom(int flag __attribute__((unused)), int fd,
Xiang Liu2801a702020-01-20 14:29:34 -0800126 uint16_t address __attribute__((unused)),
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700127 uint16_t offset, uint8_t len, uint8_t* buf)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700128{
129 auto result = lseek(fd, offset, SEEK_SET);
130 if (result < 0)
131 {
132 std::cerr << "failed to seek\n";
133 return -1;
134 }
135
Patrick Venture3ac8e4f2019-10-28 13:07:21 -0700136 return read(fd, buf, len);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700137}
138
James Feist5d7f4692020-06-17 18:22:33 -0700139static int busStrToInt(const std::string& busName)
140{
141 auto findBus = busName.rfind("-");
142 if (findBus == std::string::npos)
143 {
144 return -1;
145 }
146 return std::stoi(busName.substr(findBus + 1));
147}
148
James Feist7972bb92019-11-13 15:59:24 -0800149static int getRootBus(size_t bus)
150{
151 auto ec = std::error_code();
152 auto path = std::filesystem::read_symlink(
153 std::filesystem::path("/sys/bus/i2c/devices/i2c-" +
154 std::to_string(bus) + "/mux_device"),
155 ec);
156 if (ec)
157 {
158 return -1;
159 }
160
161 std::string filename = path.filename();
162 auto findBus = filename.find("-");
163 if (findBus == std::string::npos)
164 {
165 return -1;
166 }
167 return std::stoi(filename.substr(0, findBus));
168}
169
James Feistc95cb142018-02-26 10:41:42 -0800170static bool isMuxBus(size_t bus)
171{
Ed Tanous072e25d2018-12-16 21:45:20 -0800172 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800173 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
174}
175
James Feist8a983922019-10-24 17:11:56 -0700176static void makeProbeInterface(size_t bus, size_t address)
177{
178 if (isMuxBus(bus))
179 {
180 return; // the mux buses are random, no need to publish
181 }
182 auto [it, success] = foundDevices.emplace(
183 std::make_pair(bus, address),
184 objServer.add_interface(
185 "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" +
186 std::to_string(address),
187 "xyz.openbmc_project.Inventory.Item.I2CDevice"));
188 if (!success)
189 {
190 return; // already added
191 }
192 it->second->register_property("Bus", bus);
193 it->second->register_property("Address", address);
194 it->second->initialize();
195}
196
Vijay Khemka2d681f62018-11-06 15:51:00 -0800197static int isDevice16Bit(int file)
198{
199 /* Get first byte */
200 int byte1 = i2c_smbus_read_byte_data(file, 0);
201 if (byte1 < 0)
202 {
203 return byte1;
204 }
205 /* Read 7 more bytes, it will read same first byte in case of
206 * 8 bit but it will read next byte in case of 16 bit
207 */
208 for (int i = 0; i < 7; i++)
209 {
210 int byte2 = i2c_smbus_read_byte_data(file, 0);
211 if (byte2 < 0)
212 {
213 return byte2;
214 }
215 if (byte2 != byte1)
216 {
217 return 1;
218 }
219 }
220 return 0;
221}
222
Xiang Liu2801a702020-01-20 14:29:34 -0800223// Issue an I2C transaction to first write to_slave_buf_len bytes,then read
224// from_slave_buf_len bytes.
225static int i2c_smbus_write_then_read(int file, uint16_t address,
226 uint8_t* toSlaveBuf, uint8_t toSlaveBufLen,
227 uint8_t* fromSlaveBuf,
228 uint8_t fromSlaveBufLen)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800229{
Xiang Liu2801a702020-01-20 14:29:34 -0800230 if (toSlaveBuf == NULL || toSlaveBufLen == 0 || fromSlaveBuf == NULL ||
231 fromSlaveBufLen == 0)
232 {
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
254 return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? ret : -1;
255}
256
257static int64_t readBlockData(int flag, int file, uint16_t address,
258 uint16_t offset, uint8_t len, uint8_t* buf)
259{
Vijay Khemka2d681f62018-11-06 15:51:00 -0800260 if (flag == 0)
261 {
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);
267 return i2c_smbus_write_then_read(
268 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);
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300286 std::vector<uint8_t> device = readFRUContents(
Xiang Liu2801a702020-01-20 14:29:34 -0800287 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700288
289 close(file);
290 return device;
291}
292
293std::set<int> findI2CEeproms(int i2cBus, std::shared_ptr<DeviceMap> devices)
294{
295 std::set<int> foundList;
296
297 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
298
299 // For each file listed under the i2c device
300 // NOTE: This should be faster than just checking for each possible address
301 // path.
302 for (const auto& p : fs::directory_iterator(path))
303 {
304 const std::string node = p.path().string();
305 std::smatch m;
306 bool found =
307 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
308
309 if (!found)
310 {
311 continue;
312 }
313 if (m.size() != 2)
314 {
315 std::cerr << "regex didn't capture\n";
316 continue;
317 }
318
319 std::ssub_match subMatch = m[1];
320 std::string addressString = subMatch.str();
321
322 std::size_t ignored;
323 const int hexBase = 16;
324 int address = std::stoi(addressString, &ignored, hexBase);
325
326 const std::string eeprom = node + "/eeprom";
327
328 try
329 {
330 if (!fs::exists(eeprom))
331 {
332 continue;
333 }
334 }
335 catch (...)
336 {
337 continue;
338 }
339
340 // There is an eeprom file at this address, it may have invalid
341 // contents, but we found it.
342 foundList.insert(address);
343
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300344 std::vector<uint8_t> device = processEeprom(i2cBus, address);
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700345 if (!device.empty())
346 {
347 devices->emplace(address, device);
348 }
349 }
350
351 return foundList;
352}
353
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300354int getBusFRUs(int file, int first, int last, int bus,
Patrick Venture4f47fe62019-08-08 16:30:38 -0700355 std::shared_ptr<DeviceMap> devices)
James Feist3cb5fec2018-01-23 14:41:51 -0800356{
James Feist3cb5fec2018-01-23 14:41:51 -0800357
James Feist26c27ad2018-07-25 15:09:40 -0700358 std::future<int> future = std::async(std::launch::async, [&]() {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700359 // NOTE: When reading the devices raw on the bus, it can interfere with
360 // the driver's ability to operate, therefore read eeproms first before
361 // scanning for devices without drivers. Several experiments were run
362 // and it was determined that if there were any devices on the bus
363 // before the eeprom was hit and read, the eeprom driver wouldn't open
364 // while the bus device was open. An experiment was not performed to see
365 // if this issue was resolved if the i2c bus device was closed, but
366 // hexdumps of the eeprom later were successful.
367
368 // Scan for i2c eeproms loaded on this bus.
369 std::set<int> skipList = findI2CEeproms(bus, devices);
James Feist7972bb92019-11-13 15:59:24 -0800370 std::set<size_t>& failedItems = failedAddresses[bus];
371
372 std::set<size_t>* rootFailures = nullptr;
373 int rootBus = getRootBus(bus);
374
375 if (rootBus >= 0)
376 {
377 rootFailures = &(failedAddresses[rootBus]);
378 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700379
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530380 constexpr int startSkipSlaveAddr = 0;
381 constexpr int endSkipSlaveAddr = 12;
382
James Feist26c27ad2018-07-25 15:09:40 -0700383 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800384 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700385 if (skipList.find(ii) != skipList.end())
386 {
387 continue;
388 }
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530389 // skipping since no device is present in this range
390 if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr)
391 {
392 continue;
393 }
James Feist26c27ad2018-07-25 15:09:40 -0700394 // Set slave address
Rajashekar Gade Reddyfa8d3222020-05-13 08:27:03 +0530395 if (ioctl(file, I2C_SLAVE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800396 {
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300397 std::cerr << "device at bus " << bus << " address " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700398 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700399 continue;
400 }
401 // probe
402 else if (i2c_smbus_read_byte(file) < 0)
403 {
404 continue;
405 }
James Feist3cb5fec2018-01-23 14:41:51 -0800406
James Feist26c27ad2018-07-25 15:09:40 -0700407 if (DEBUG)
408 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700409 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700410 << "\n";
411 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800412
James Feist8a983922019-10-24 17:11:56 -0700413 makeProbeInterface(bus, ii);
414
James Feist7972bb92019-11-13 15:59:24 -0800415 if (failedItems.find(ii) != failedItems.end())
416 {
417 // if we failed to read it once, unlikely we can read it later
418 continue;
419 }
420
421 if (rootFailures != nullptr)
422 {
423 if (rootFailures->find(ii) != rootFailures->end())
424 {
425 continue;
426 }
427 }
428
Vijay Khemka2d681f62018-11-06 15:51:00 -0800429 /* Check for Device type if it is 8 bit or 16 bit */
430 int flag = isDevice16Bit(file);
431 if (flag < 0)
432 {
433 std::cerr << "failed to read bus " << bus << " address " << ii
434 << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700435 if (powerIsOn)
436 {
437 failedItems.insert(ii);
438 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800439 continue;
440 }
441
Patrick Venturebaba7672019-10-26 09:26:41 -0700442 std::string errorMessage =
443 "bus " + std::to_string(bus) + " address " + std::to_string(ii);
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300444 std::vector<uint8_t> device =
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300445 readFRUContents(flag, file, static_cast<uint16_t>(ii),
Xiang Liu2801a702020-01-20 14:29:34 -0800446 readBlockData, errorMessage);
Patrick Venturebaba7672019-10-26 09:26:41 -0700447 if (device.empty())
James Feist26c27ad2018-07-25 15:09:40 -0700448 {
James Feist26c27ad2018-07-25 15:09:40 -0700449 continue;
450 }
James Feist26c27ad2018-07-25 15:09:40 -0700451
Patrick Venture786f1792019-08-05 16:33:44 -0700452 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800453 }
James Feist26c27ad2018-07-25 15:09:40 -0700454 return 1;
455 });
456 std::future_status status =
457 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
458 if (status == std::future_status::timeout)
459 {
460 std::cerr << "Error reading bus " << bus << "\n";
James Feistcb5661d2020-06-12 15:05:01 -0700461 if (powerIsOn)
462 {
463 busBlacklist.insert(bus);
464 }
James Feist444830e2019-04-05 08:38:16 -0700465 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700466 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800467 }
468
James Feist444830e2019-04-05 08:38:16 -0700469 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700470 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800471}
472
Patrick Venture11f1ff42019-08-01 10:42:12 -0700473void loadBlacklist(const char* path)
474{
475 std::ifstream blacklistStream(path);
476 if (!blacklistStream.good())
477 {
478 // File is optional.
479 std::cerr << "Cannot open blacklist file.\n\n";
480 return;
481 }
482
483 nlohmann::json data =
484 nlohmann::json::parse(blacklistStream, nullptr, false);
485 if (data.is_discarded())
486 {
487 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
488 "exiting\n";
489 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700490 }
491
492 // It's expected to have at least one field, "buses" that is an array of the
493 // buses by integer. Allow for future options to exclude further aspects,
494 // such as specific addresses or ranges.
495 if (data.type() != nlohmann::json::value_t::object)
496 {
497 std::cerr << "Illegal blacklist, expected to read dictionary\n";
498 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700499 }
500
501 // If buses field is missing, that's fine.
502 if (data.count("buses") == 1)
503 {
504 // Parse the buses array after a little validation.
505 auto buses = data.at("buses");
506 if (buses.type() != nlohmann::json::value_t::array)
507 {
508 // Buses field present but invalid, therefore this is an error.
509 std::cerr << "Invalid contents for blacklist buses field\n";
510 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700511 }
512
513 // Catch exception here for type mis-match.
514 try
515 {
516 for (const auto& bus : buses)
517 {
518 busBlacklist.insert(bus.get<size_t>());
519 }
520 }
521 catch (const nlohmann::detail::type_error& e)
522 {
523 // Type mis-match is a critical error.
524 std::cerr << "Invalid bus type: " << e.what() << "\n";
525 std::exit(EXIT_FAILURE);
Patrick Venture11f1ff42019-08-01 10:42:12 -0700526 }
527 }
528
529 return;
530}
531
James Feista465ccc2019-02-08 12:51:01 -0800532static void FindI2CDevices(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700533 BusMap& busmap)
James Feist3cb5fec2018-01-23 14:41:51 -0800534{
James Feista465ccc2019-02-08 12:51:01 -0800535 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800536 {
James Feist5d7f4692020-06-17 18:22:33 -0700537 int bus = busStrToInt(i2cBus);
James Feist0c3980a2019-12-19 11:09:27 -0800538
James Feist5d7f4692020-06-17 18:22:33 -0700539 if (bus < 0)
540 {
541 std::cerr << "Cannot translate " << i2cBus << " to int\n";
542 continue;
543 }
James Feist444830e2019-04-05 08:38:16 -0700544 if (busBlacklist.find(bus) != busBlacklist.end())
545 {
546 continue; // skip previously failed busses
547 }
James Feist3cb5fec2018-01-23 14:41:51 -0800548
James Feist0c3980a2019-12-19 11:09:27 -0800549 int rootBus = getRootBus(bus);
550 if (busBlacklist.find(rootBus) != busBlacklist.end())
551 {
552 continue;
553 }
554
James Feist3cb5fec2018-01-23 14:41:51 -0800555 auto file = open(i2cBus.c_str(), O_RDWR);
556 if (file < 0)
557 {
558 std::cerr << "unable to open i2c device " << i2cBus.string()
559 << "\n";
560 continue;
561 }
562 unsigned long funcs = 0;
563
564 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
565 {
566 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700567 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800568 << bus << "\n";
Zhikui Renc02d8cb2021-06-28 16:56:08 -0700569 close(file);
James Feist3cb5fec2018-01-23 14:41:51 -0800570 continue;
571 }
572 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
573 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
574 {
575 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
576 << bus << "\n";
577 continue;
578 }
James Feist98132792019-07-09 13:29:09 -0700579 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800580 device = std::make_shared<DeviceMap>();
581
Nikhil Potaded8884f12019-03-27 13:27:13 -0700582 // i2cdetect by default uses the range 0x03 to 0x77, as
583 // this is what we have tested with, use this range. Could be
584 // changed in future.
585 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800586 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700587 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800588 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700589
590 // fd is closed in this function in case the bus locks up
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300591 getBusFRUs(file, 0x03, 0x77, bus, device);
Nikhil Potaded8884f12019-03-27 13:27:13 -0700592
593 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800594 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700595 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800596 }
James Feist3cb5fec2018-01-23 14:41:51 -0800597 }
James Feist3cb5fec2018-01-23 14:41:51 -0800598}
599
James Feist6ebf9de2018-05-15 15:01:17 -0700600// this class allows an async response after all i2c devices are discovered
James Feist8c505da2020-05-28 10:06:33 -0700601struct FindDevicesWithCallback :
602 std::enable_shared_from_this<FindDevicesWithCallback>
James Feist6ebf9de2018-05-15 15:01:17 -0700603{
James Feista465ccc2019-02-08 12:51:01 -0800604 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
James Feist5cb06082019-10-24 17:11:13 -0700605 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800606 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700607 _i2cBuses(i2cBuses),
James Feist5cb06082019-10-24 17:11:13 -0700608 _busMap(busmap), _callback(std::move(callback))
James Feist8c505da2020-05-28 10:06:33 -0700609 {}
James Feist6ebf9de2018-05-15 15:01:17 -0700610 ~FindDevicesWithCallback()
611 {
612 _callback();
613 }
614 void run()
615 {
James Feist98132792019-07-09 13:29:09 -0700616 FindI2CDevices(_i2cBuses, _busMap);
James Feist6ebf9de2018-05-15 15:01:17 -0700617 }
618
James Feista465ccc2019-02-08 12:51:01 -0800619 const std::vector<fs::path>& _i2cBuses;
James Feista465ccc2019-02-08 12:51:01 -0800620 BusMap& _busMap;
James Feist6ebf9de2018-05-15 15:01:17 -0700621 std::function<void(void)> _callback;
622};
623
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300624std::vector<uint8_t>& getFRUInfo(const uint8_t& bus, const uint8_t& address)
Nikhil Potaded8884f12019-03-27 13:27:13 -0700625{
626 auto deviceMap = busMap.find(bus);
627 if (deviceMap == busMap.end())
628 {
629 throw std::invalid_argument("Invalid Bus.");
630 }
631 auto device = deviceMap->second->find(address);
632 if (device == deviceMap->second->end())
633 {
634 throw std::invalid_argument("Invalid Address.");
635 }
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300636 std::vector<uint8_t>& ret = device->second;
Nikhil Potaded8884f12019-03-27 13:27:13 -0700637
638 return ret;
639}
640
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300641void AddFRUObjectToDbus(
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300642 std::vector<uint8_t>& device,
James Feista465ccc2019-02-08 12:51:01 -0800643 boost::container::flat_map<
644 std::pair<size_t, size_t>,
645 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -0700646 uint32_t bus, uint32_t address)
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;
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300649 resCodes res = formatFRU(device, formattedFRU);
650 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 }
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300656 else if (res == resCodes::resWarn)
657 {
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 {
680 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
681 UNKNOWN_BUS_OBJECT_COUNT++;
682 }
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 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700696 if (isMuxBus(bus) && address == busIface.first.second &&
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300697 (getFRUInfo(static_cast<uint8_t>(busIface.first.first),
James Feist98132792019-07-09 13:29:09 -0700698 static_cast<uint8_t>(busIface.first.second)) ==
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300699 getFRUInfo(static_cast<uint8_t>(bus),
James Feist98132792019-07-09 13:29:09 -0700700 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700701 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700702 // This device is already added to the lower numbered bus,
703 // do not replicate it.
704 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700705 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700706
707 // Check if the match named has extra information.
708 found = true;
709 std::smatch base_match;
710
711 bool match = std::regex_match(
712 path, base_match, std::regex(productName + "_(\\d+)$"));
713 if (match)
James Feist79e9c0b2018-03-15 15:21:17 -0700714 {
Patrick Venture015fb0a2019-08-16 09:33:31 -0700715 if (base_match.size() == 2)
716 {
717 std::ssub_match base_sub_match = base_match[1];
718 std::string base = base_sub_match.str();
719
720 int value = std::stoi(base);
721 highest = (value > highest) ? value : highest;
722 }
James Feist79e9c0b2018-03-15 15:21:17 -0700723 }
James Feist918e18c2018-02-13 15:51:07 -0800724 }
Patrick Venture015fb0a2019-08-16 09:33:31 -0700725 } // end searching objects
726
727 if (found)
728 {
729 // We found something with the same name. If highest was still -1,
730 // it means this new entry will be _0.
731 productName += "_";
732 productName += std::to_string(++highest);
James Feist918e18c2018-02-13 15:51:07 -0800733 }
734 }
James Feist3cb5fec2018-01-23 14:41:51 -0800735
James Feist9eb0b582018-04-27 12:15:46 -0700736 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
737 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
738 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
739
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300740 for (auto& property : formattedFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800741 {
James Feist9eb0b582018-04-27 12:15:46 -0700742
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700743 std::regex_replace(property.second.begin(), property.second.begin(),
744 property.second.end(), NON_ASCII_REGEX, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530745 if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG")
James Feist9eb0b582018-04-27 12:15:46 -0700746 {
747 continue;
748 }
749 std::string key =
750 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530751
752 if (property.first == "PRODUCT_ASSET_TAG")
753 {
754 std::string propertyName = property.first;
755 iface->register_property(
756 key, property.second + '\0',
757 [bus, address, propertyName,
758 &dbusInterfaceMap](const std::string& req, std::string& resp) {
759 if (strcmp(req.c_str(), resp.c_str()))
760 {
761 // call the method which will update
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300762 if (updateFRUProperty(req, bus, address, propertyName,
Joshi-Mansid73b7442020-03-27 19:10:21 +0530763 dbusInterfaceMap))
764 {
765 resp = req;
766 }
767 else
768 {
769 throw std::invalid_argument(
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300770 "FRU property update failed.");
Joshi-Mansid73b7442020-03-27 19:10:21 +0530771 }
772 }
773 return 1;
774 });
775 }
776 else if (!iface->register_property(key, property.second + '\0'))
James Feist9eb0b582018-04-27 12:15:46 -0700777 {
778 std::cerr << "illegal key: " << key << "\n";
779 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700780 if (DEBUG)
781 {
782 std::cout << property.first << ": " << property.second << "\n";
783 }
James Feist3cb5fec2018-01-23 14:41:51 -0800784 }
James Feist2a9d6db2018-04-27 15:48:28 -0700785
786 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700787 iface->register_property("BUS", bus);
788 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700789
James Feist9eb0b582018-04-27 12:15:46 -0700790 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800791}
792
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300793static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU)
James Feist3cb5fec2018-01-23 14:41:51 -0800794{
795 // try to read baseboard fru from file
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300796 std::ifstream baseboardFRUFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
797 if (baseboardFRUFile.good())
James Feist3cb5fec2018-01-23 14:41:51 -0800798 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300799 baseboardFRUFile.seekg(0, std::ios_base::end);
800 size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
801 baseboardFRU.resize(fileSize);
802 baseboardFRUFile.seekg(0, std::ios_base::beg);
803 baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()),
Andrei Kartashev1c5b7062020-08-19 14:47:30 +0300804 fileSize);
James Feist3cb5fec2018-01-23 14:41:51 -0800805 }
806 else
807 {
808 return false;
809 }
810 return true;
811}
812
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300813bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700814{
815 boost::container::flat_map<std::string, std::string> tmp;
816 if (fru.size() > MAX_FRU_SIZE)
817 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300818 std::cerr << "Invalid fru.size() during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700819 return false;
820 }
821 // verify legal fru by running it through fru parsing logic
Andrei Kartashevb45324a2020-10-14 17:01:47 +0300822 if (formatFRU(fru, tmp) != resCodes::resOK)
James Feistb49ffc32018-05-02 11:10:43 -0700823 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300824 std::cerr << "Invalid fru format during writeFRU\n";
James Feistb49ffc32018-05-02 11:10:43 -0700825 return false;
826 }
827 // baseboard fru
828 if (bus == 0 && address == 0)
829 {
830 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
831 if (!file.good())
832 {
833 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
834 << "\n";
James Feistddb78302018-09-06 11:45:42 -0700835 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700836 return false;
837 }
James Feista465ccc2019-02-08 12:51:01 -0800838 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700839 return file.good();
840 }
841 else
842 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700843 if (hasEepromFile(bus, address))
844 {
845 auto path = getEepromPath(bus, address);
846 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
847 if (eeprom < 0)
848 {
849 std::cerr << "unable to open i2c device " << path << "\n";
850 throw DBusInternalError();
851 return false;
852 }
853
854 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
855 if (writtenBytes < 0)
856 {
857 std::cerr << "unable to write to i2c device " << path << "\n";
858 close(eeprom);
859 throw DBusInternalError();
860 return false;
861 }
862
863 close(eeprom);
864 return true;
865 }
866
James Feistb49ffc32018-05-02 11:10:43 -0700867 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
868
869 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
870 if (file < 0)
871 {
872 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -0700873 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700874 return false;
875 }
876 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
877 {
878 std::cerr << "unable to set device address\n";
879 close(file);
James Feistddb78302018-09-06 11:45:42 -0700880 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700881 return false;
882 }
883
884 constexpr const size_t RETRY_MAX = 2;
885 uint16_t index = 0;
886 size_t retries = RETRY_MAX;
887 while (index < fru.size())
888 {
889 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
890 (retries == RETRY_MAX))
891 {
892 // The 4K EEPROM only uses the A2 and A1 device address bits
893 // with the third bit being a memory page address bit.
894 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
895 {
896 std::cerr << "unable to set device address\n";
897 close(file);
James Feistddb78302018-09-06 11:45:42 -0700898 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700899 return false;
900 }
901 }
902
James Feist98132792019-07-09 13:29:09 -0700903 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
904 fru[index]) < 0)
James Feistb49ffc32018-05-02 11:10:43 -0700905 {
906 if (!retries--)
907 {
908 std::cerr << "error writing fru: " << strerror(errno)
909 << "\n";
910 close(file);
James Feistddb78302018-09-06 11:45:42 -0700911 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700912 return false;
913 }
914 }
915 else
916 {
917 retries = RETRY_MAX;
918 index++;
919 }
920 // most eeproms require 5-10ms between writes
921 std::this_thread::sleep_for(std::chrono::milliseconds(10));
922 }
923 close(file);
924 return true;
925 }
926}
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,
933 bool dbusCall)
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800934{
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800935 for (auto& [pair, interface] : foundDevices)
936 {
937 if (pair.first == static_cast<size_t>(busNum))
938 {
939 objServer.remove_interface(interface);
940 foundDevices.erase(pair);
941 }
942 }
943
James Feist5d7f4692020-06-17 18:22:33 -0700944 fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum));
945 if (!fs::exists(busPath))
946 {
947 if (dbusCall)
948 {
949 std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum)
950 << "\n";
951 throw std::invalid_argument("Invalid Bus.");
952 }
953 return;
954 }
955
956 std::vector<fs::path> i2cBuses;
957 i2cBuses.emplace_back(busPath);
958
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800959 auto scan = std::make_shared<FindDevicesWithCallback>(
960 i2cBuses, busmap, [busNum, &busmap, &dbusInterfaceMap]() {
961 for (auto& busIface : dbusInterfaceMap)
962 {
963 if (busIface.first.first == static_cast<size_t>(busNum))
964 {
965 objServer.remove_interface(busIface.second);
966 }
967 }
Wludzik, Jozefc1136b72020-06-24 11:58:32 +0200968 auto found = busmap.find(busNum);
969 if (found == busmap.end() || found->second == nullptr)
970 {
971 return;
972 }
973 for (auto& device : *(found->second))
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800974 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +0300975 AddFRUObjectToDbus(device.second, dbusInterfaceMap,
Cheng C Yangd5b87fb2020-01-23 10:10:45 +0800976 static_cast<uint32_t>(busNum), device.first);
977 }
978 });
979 scan->run();
980}
981
James Feist9eb0b582018-04-27 12:15:46 -0700982void rescanBusses(
James Feist5cb06082019-10-24 17:11:13 -0700983 BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800984 boost::container::flat_map<
985 std::pair<size_t, size_t>,
James Feist5cb06082019-10-24 17:11:13 -0700986 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800987{
James Feist6ebf9de2018-05-15 15:01:17 -0700988 static boost::asio::deadline_timer timer(io);
989 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800990
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500991 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -0700992 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -0800993 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -0800994 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800995
Nikhil Potaded8884f12019-03-27 13:27:13 -0700996 boost::container::flat_map<size_t, fs::path> busPaths;
997 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -0800998 {
James Feist4131aea2018-03-09 09:47:30 -0800999 std::cerr << "unable to find i2c devices\n";
1000 return;
James Feist918e18c2018-02-13 15:51:07 -08001001 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001002
1003 for (auto busPath : busPaths)
1004 {
1005 i2cBuses.emplace_back(busPath.second);
1006 }
James Feist4131aea2018-03-09 09:47:30 -08001007
James Feist98132792019-07-09 13:29:09 -07001008 busmap.clear();
James Feist8a983922019-10-24 17:11:56 -07001009 for (auto& [pair, interface] : foundDevices)
1010 {
1011 objServer.remove_interface(interface);
1012 }
1013 foundDevices.clear();
1014
James Feist5cb06082019-10-24 17:11:13 -07001015 auto scan =
1016 std::make_shared<FindDevicesWithCallback>(i2cBuses, busmap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001017 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001018 {
1019 objServer.remove_interface(busIface.second);
1020 }
James Feist4131aea2018-03-09 09:47:30 -08001021
James Feist6ebf9de2018-05-15 15:01:17 -07001022 dbusInterfaceMap.clear();
1023 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -08001024
James Feist6ebf9de2018-05-15 15:01:17 -07001025 // todo, get this from a more sensable place
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001026 std::vector<uint8_t> baseboardFRU;
1027 if (readBaseboardFRU(baseboardFRU))
James Feist6ebf9de2018-05-15 15:01:17 -07001028 {
Helen Huangf69fe902021-02-19 16:18:22 +08001029 // If no device on i2c bus 0, the insertion will happen.
1030 auto bus0 =
1031 busmap.try_emplace(0, std::make_shared<DeviceMap>());
1032 bus0.first->second->emplace(0, baseboardFRU);
James Feist6ebf9de2018-05-15 15:01:17 -07001033 }
James Feist98132792019-07-09 13:29:09 -07001034 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001035 {
James Feista465ccc2019-02-08 12:51:01 -08001036 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001037 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001038 AddFRUObjectToDbus(device.second, dbusInterfaceMap,
James Feist5cb06082019-10-24 17:11:13 -07001039 devicemap.first, device.first);
James Feist6ebf9de2018-05-15 15:01:17 -07001040 }
1041 }
1042 });
1043 scan->run();
1044 });
James Feist918e18c2018-02-13 15:51:07 -08001045}
1046
Joshi-Mansid73b7442020-03-27 19:10:21 +05301047// Details with example of Asset Tag Update
1048// To find location of Product Info Area asset tag as per FRU specification
1049// 1. Find product Info area starting offset (*8 - as header will be in
1050// multiple of 8 bytes).
1051// 2. Skip 3 bytes of product info area (like format version, area length,
1052// and language code).
1053// 3. Traverse manufacturer name, product name, product version, & product
1054// serial number, by reading type/length code to reach the Asset Tag.
1055// 4. Update the Asset Tag, reposition the product Info area in multiple of
1056// 8 bytes. Update the Product area length and checksum.
1057
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001058bool updateFRUProperty(
Joshi-Mansid73b7442020-03-27 19:10:21 +05301059 const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
1060 std::string propertyName,
1061 boost::container::flat_map<
1062 std::pair<size_t, size_t>,
1063 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap)
1064{
1065 size_t updatePropertyReqLen = updatePropertyReq.length();
1066 if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63)
1067 {
1068 std::cerr
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001069 << "FRU field data cannot be of 1 char or more than 63 chars. "
Joshi-Mansid73b7442020-03-27 19:10:21 +05301070 "Invalid Length "
1071 << updatePropertyReqLen << "\n";
1072 return false;
1073 }
1074
1075 std::vector<uint8_t> fruData;
1076 try
1077 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001078 fruData = getFRUInfo(static_cast<uint8_t>(bus),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301079 static_cast<uint8_t>(address));
1080 }
1081 catch (std::invalid_argument& e)
1082 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001083 std::cerr << "Failure getting FRU Info" << e.what() << "\n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301084 return false;
1085 }
1086
1087 if (fruData.empty())
1088 {
1089 return false;
1090 }
1091
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001092 const std::vector<std::string>* fruAreaFieldNames;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301093
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001094 uint8_t fruAreaOffsetFieldValue = 0;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301095 size_t offset = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001096 std::string areaName = propertyName.substr(0, propertyName.find("_"));
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001097 std::string propertyNamePrefix = areaName + "_";
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001098 auto it = std::find(FRU_AREA_NAMES.begin(), FRU_AREA_NAMES.end(), areaName);
1099 if (it == FRU_AREA_NAMES.end())
Joshi-Mansid73b7442020-03-27 19:10:21 +05301100 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001101 std::cerr << "Can't parse area name for property " << propertyName
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001102 << " \n";
1103 return false;
1104 }
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001105 fruAreas fruAreaToUpdate =
1106 static_cast<fruAreas>(it - FRU_AREA_NAMES.begin());
1107 fruAreaOffsetFieldValue =
1108 fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)];
1109 switch (fruAreaToUpdate)
1110 {
1111 case fruAreas::fruAreaChassis:
1112 offset = 3; // chassis part number offset. Skip fixed first 3 bytes
1113 fruAreaFieldNames = &CHASSIS_FRU_AREAS;
1114 break;
1115 case fruAreas::fruAreaBoard:
1116 offset = 6; // board manufacturer offset. Skip fixed first 6 bytes
1117 fruAreaFieldNames = &BOARD_FRU_AREAS;
1118 break;
1119 case fruAreas::fruAreaProduct:
1120 // Manufacturer name offset. Skip fixed first 3 product fru bytes
1121 // i.e. version, area length and language code
1122 offset = 3;
1123 fruAreaFieldNames = &PRODUCT_FRU_AREAS;
1124 break;
1125 default:
1126 std::cerr << "Don't know how to handle property " << propertyName
1127 << " \n";
1128 return false;
1129 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001130 if (fruAreaOffsetFieldValue == 0)
1131 {
1132 std::cerr << "FRU Area for " << propertyName << " not present \n";
Joshi-Mansid73b7442020-03-27 19:10:21 +05301133 return false;
1134 }
1135
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001136 size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001137 size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize;
1138 size_t fruAreaEnd = fruAreaStart + fruAreaSize;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001139 size_t fruDataIter = fruAreaStart + offset;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001140 size_t fruUpdateFieldLoc = fruDataIter;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001141 size_t skipToFRUUpdateField = 0;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001142 ssize_t fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301143
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001144 bool found = false;
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001145 for (auto& field : *fruAreaFieldNames)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301146 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001147 skipToFRUUpdateField++;
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001148 if (propertyName == propertyNamePrefix + field)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301149 {
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001150 found = true;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301151 break;
1152 }
1153 }
Andrei Kartashev6cfb7752020-08-10 19:50:33 +03001154 if (!found)
1155 {
1156 std::size_t pos = propertyName.find(FRU_CUSTOM_FIELD_NAME);
1157 if (pos == std::string::npos)
1158 {
1159 std::cerr << "PropertyName doesn't exist in FRU Area Vectors: "
1160 << propertyName << "\n";
1161 return false;
1162 }
1163 std::string fieldNumStr =
1164 propertyName.substr(pos + FRU_CUSTOM_FIELD_NAME.length());
1165 size_t fieldNum = std::stoi(fieldNumStr);
1166 if (fieldNum == 0)
1167 {
1168 std::cerr << "PropertyName not recognized: " << propertyName
1169 << "\n";
1170 return false;
1171 }
1172 skipToFRUUpdateField += fieldNum;
1173 }
Joshi-Mansid73b7442020-03-27 19:10:21 +05301174
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001175 for (size_t i = 1; i < skipToFRUUpdateField; i++)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301176 {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001177 fieldLength = getFieldLength(fruData[fruDataIter]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001178 if (fieldLength < 0)
1179 {
1180 break;
1181 }
1182 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301183 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001184 fruUpdateFieldLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301185
1186 // Push post update fru field bytes to a vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001187 fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001188 if (fieldLength < 0)
Joshi-Mansid73b7442020-03-27 19:10:21 +05301189 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001190 std::cerr << "Property " << propertyName << " not present \n";
1191 return false;
1192 }
1193 fruDataIter += 1 + fieldLength;
1194 size_t restFRUFieldsLoc = fruDataIter;
1195 size_t endOfFieldsLoc = 0;
1196 while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0)
1197 {
1198 if (fruDataIter >= (fruAreaStart + fruAreaSize))
Joshi-Mansid73b7442020-03-27 19:10:21 +05301199 {
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001200 fruDataIter = fruAreaStart + fruAreaSize;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301201 break;
1202 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001203 fruDataIter += 1 + fieldLength;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301204 }
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001205 endOfFieldsLoc = fruDataIter;
Joshi-Mansid73b7442020-03-27 19:10:21 +05301206
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001207 std::vector<uint8_t> restFRUAreaFieldsData;
1208 std::copy_n(fruData.begin() + restFRUFieldsLoc,
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001209 endOfFieldsLoc - restFRUFieldsLoc + 1,
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001210 std::back_inserter(restFRUAreaFieldsData));
Joshi-Mansid73b7442020-03-27 19:10:21 +05301211
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001212 // Push post update fru areas if any
1213 unsigned int nextFRUAreaLoc = 0;
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001214 for (fruAreas nextFRUArea = fruAreas::fruAreaInternal;
1215 nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001216 {
1217 unsigned int fruAreaLoc =
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001218 fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize;
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001219 if ((fruAreaLoc > endOfFieldsLoc) &&
1220 ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc)))
1221 {
1222 nextFRUAreaLoc = fruAreaLoc;
1223 }
1224 }
1225 std::vector<uint8_t> restFRUAreasData;
1226 if (nextFRUAreaLoc)
1227 {
1228 std::copy_n(fruData.begin() + nextFRUAreaLoc,
1229 fruData.size() - nextFRUAreaLoc,
1230 std::back_inserter(restFRUAreasData));
1231 }
1232
1233 // check FRU area size
1234 size_t fruAreaDataSize =
1235 ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size());
1236 size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize;
1237 if ((updatePropertyReqLen + 1) > fruAreaAvailableSize)
1238 {
1239
1240#ifdef ENABLE_FRU_AREA_RESIZE
1241 size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1;
1242 // round size to 8-byte blocks
1243 newFRUAreaSize =
1244 ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize;
1245 size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize;
1246 fruData.resize(newFRUDataSize);
1247 fruAreaSize = newFRUAreaSize;
1248 fruAreaEnd = fruAreaStart + fruAreaSize;
1249#else
1250 std::cerr << "FRU field length: " << updatePropertyReqLen + 1
1251 << " should not be greater than available FRU area size: "
1252 << fruAreaAvailableSize << "\n";
1253 return false;
1254#endif // ENABLE_FRU_AREA_RESIZE
1255 }
1256
Joshi-Mansid73b7442020-03-27 19:10:21 +05301257 // write new requested property field length and data
1258 constexpr uint8_t newTypeLenMask = 0xC0;
1259 fruData[fruUpdateFieldLoc] =
1260 static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask);
1261 fruUpdateFieldLoc++;
1262 std::copy(updatePropertyReq.begin(), updatePropertyReq.end(),
1263 fruData.begin() + fruUpdateFieldLoc);
1264
1265 // Copy remaining data to main fru area - post updated fru field vector
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001266 restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen;
1267 size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size();
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001268 std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(),
1269 fruData.begin() + restFRUFieldsLoc);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301270
1271 // Update final fru with new fru area length and checksum
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001272 unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum(
1273 fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd);
Joshi-Mansid73b7442020-03-27 19:10:21 +05301274
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001275#ifdef ENABLE_FRU_AREA_RESIZE
1276 ++nextFRUAreaNewLoc;
1277 ssize_t nextFRUAreaOffsetDiff =
1278 (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize;
1279 // Append rest FRU Areas if size changed and there were other sections after
1280 // updated one
1281 if (nextFRUAreaOffsetDiff && nextFRUAreaLoc)
1282 {
1283 std::copy(restFRUAreasData.begin(), restFRUAreasData.end(),
1284 fruData.begin() + nextFRUAreaNewLoc);
1285 // Update Common Header
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001286 for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord;
1287 fruArea++)
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001288 {
Andrei Kartashev9f0f2d12020-08-20 04:24:37 +03001289 unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea);
Andrei Kartashev6b3d4c52020-08-10 19:24:17 +03001290 size_t curFRUAreaOffset = fruData[fruAreaOffsetField];
1291 if (curFRUAreaOffset > fruAreaOffsetFieldValue)
1292 {
1293 fruData[fruAreaOffsetField] = static_cast<int8_t>(
1294 curFRUAreaOffset + nextFRUAreaOffsetDiff);
1295 }
1296 }
1297 // Calculate new checksum
1298 std::vector<uint8_t> headerFRUData;
1299 std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData));
1300 size_t checksumVal = calculateChecksum(headerFRUData);
1301 fruData[7] = static_cast<uint8_t>(checksumVal);
1302 // fill zeros if FRU Area size decreased
1303 if (nextFRUAreaOffsetDiff < 0)
1304 {
1305 std::fill(fruData.begin() + nextFRUAreaNewLoc +
1306 restFRUAreasData.size(),
1307 fruData.end(), 0);
1308 }
1309 }
1310#else
1311 // this is to avoid "unused variable" warning
1312 (void)nextFRUAreaNewLoc;
1313#endif // ENABLE_FRU_AREA_RESIZE
Joshi-Mansid73b7442020-03-27 19:10:21 +05301314 if (fruData.empty())
1315 {
1316 return false;
1317 }
1318
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001319 if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address),
Joshi-Mansid73b7442020-03-27 19:10:21 +05301320 fruData))
1321 {
1322 return false;
1323 }
1324
1325 // Rescan the bus so that GetRawFru dbus-call fetches updated values
1326 rescanBusses(busMap, dbusInterfaceMap);
1327 return true;
1328}
1329
James Feist98132792019-07-09 13:29:09 -07001330int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001331{
1332 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001333 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001334 std::vector<fs::path> i2cBuses;
1335
James Feista3c180a2018-08-09 16:06:04 -07001336 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001337 {
1338 std::cerr << "unable to find i2c devices\n";
1339 return 1;
1340 }
James Feist3cb5fec2018-01-23 14:41:51 -08001341
Patrick Venture11f1ff42019-08-01 10:42:12 -07001342 // check for and load blacklist with initial buses.
1343 loadBlacklist(blacklistPath);
1344
Vijay Khemka065f6d92018-12-18 10:37:47 -08001345 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001346
James Feist6ebf9de2018-05-15 15:01:17 -07001347 // this is a map with keys of pair(bus number, address) and values of
1348 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001349 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001350 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1351 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001352
James Feist9eb0b582018-04-27 12:15:46 -07001353 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1354 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1355 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001356
James Feist5cb06082019-10-24 17:11:13 -07001357 iface->register_method("ReScan",
1358 [&]() { rescanBusses(busMap, dbusInterfaceMap); });
James Feist2a9d6db2018-04-27 15:48:28 -07001359
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001360 iface->register_method("ReScanBus", [&](uint8_t bus) {
James Feist5d7f4692020-06-17 18:22:33 -07001361 rescanOneBus(busMap, bus, dbusInterfaceMap, true);
Cheng C Yangd5b87fb2020-01-23 10:10:45 +08001362 });
1363
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001364 iface->register_method("GetRawFru", getFRUInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001365
1366 iface->register_method("WriteFru", [&](const uint8_t bus,
1367 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001368 const std::vector<uint8_t>& data) {
Andrei Kartashev2f0de172020-08-13 14:29:40 +03001369 if (!writeFRU(bus, address, data))
James Feistb49ffc32018-05-02 11:10:43 -07001370 {
James Feistddb78302018-09-06 11:45:42 -07001371 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001372 return;
1373 }
1374 // schedule rescan on success
James Feist5cb06082019-10-24 17:11:13 -07001375 rescanBusses(busMap, dbusInterfaceMap);
James Feistb49ffc32018-05-02 11:10:43 -07001376 });
James Feist9eb0b582018-04-27 12:15:46 -07001377 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001378
James Feist9eb0b582018-04-27 12:15:46 -07001379 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -08001380 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -08001381 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001382 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001383 std::string,
1384 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001385 values;
1386 message.read(objectName, values);
James Feist0eeb79c2019-10-09 13:35:29 -07001387 auto findState = values.find("CurrentHostState");
James Feist0eeb79c2019-10-09 13:35:29 -07001388 if (findState != values.end())
James Feist918e18c2018-02-13 15:51:07 -08001389 {
James Feistcb5661d2020-06-12 15:05:01 -07001390 powerIsOn = boost::ends_with(
1391 std::get<std::string>(findState->second), "Running");
James Feist0eeb79c2019-10-09 13:35:29 -07001392 }
James Feist6ebf9de2018-05-15 15:01:17 -07001393
James Feistcb5661d2020-06-12 15:05:01 -07001394 if (powerIsOn)
James Feist0eeb79c2019-10-09 13:35:29 -07001395 {
James Feist5cb06082019-10-24 17:11:13 -07001396 rescanBusses(busMap, dbusInterfaceMap);
James Feist918e18c2018-02-13 15:51:07 -08001397 }
James Feist918e18c2018-02-13 15:51:07 -08001398 };
James Feist9eb0b582018-04-27 12:15:46 -07001399
1400 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -08001401 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001402 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
James Feist0eeb79c2019-10-09 13:35:29 -07001403 "openbmc_project/state/"
1404 "host0',arg0='xyz.openbmc_project.State.Host'",
James Feist9eb0b582018-04-27 12:15:46 -07001405 eventHandler);
1406
James Feist4131aea2018-03-09 09:47:30 -08001407 int fd = inotify_init();
James Feist0eb40352019-04-09 14:44:04 -07001408 inotify_add_watch(fd, I2C_DEV_LOCATION,
1409 IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -08001410 std::array<char, 4096> readBuffer;
1411 std::string pendingBuffer;
1412 // monitor for new i2c devices
1413 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1414 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001415 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -08001416 std::size_t bytes_transferred) {
1417 if (ec)
1418 {
1419 std::cout << "Callback Error " << ec << "\n";
1420 return;
1421 }
1422 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
James Feist4131aea2018-03-09 09:47:30 -08001423 while (pendingBuffer.size() > sizeof(inotify_event))
1424 {
James Feista465ccc2019-02-08 12:51:01 -08001425 const inotify_event* iEvent =
1426 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -08001427 pendingBuffer.data());
1428 switch (iEvent->mask)
1429 {
James Feist9eb0b582018-04-27 12:15:46 -07001430 case IN_CREATE:
1431 case IN_MOVED_TO:
1432 case IN_DELETE:
James Feist5d7f4692020-06-17 18:22:33 -07001433 std::string name(iEvent->name);
1434 if (boost::starts_with(name, "i2c"))
James Feist9eb0b582018-04-27 12:15:46 -07001435 {
James Feist5d7f4692020-06-17 18:22:33 -07001436 int bus = busStrToInt(name);
1437 if (bus < 0)
1438 {
1439 std::cerr << "Could not parse bus " << name
1440 << "\n";
1441 continue;
1442 }
1443 rescanOneBus(busMap, static_cast<uint8_t>(bus),
1444 dbusInterfaceMap, false);
James Feist9eb0b582018-04-27 12:15:46 -07001445 }
James Feist4131aea2018-03-09 09:47:30 -08001446 }
1447
1448 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
1449 }
James Feist6ebf9de2018-05-15 15:01:17 -07001450
James Feist4131aea2018-03-09 09:47:30 -08001451 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1452 watchI2cBusses);
1453 };
1454
1455 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001456 // run the initial scan
James Feist5cb06082019-10-24 17:11:13 -07001457 rescanBusses(busMap, dbusInterfaceMap);
James Feist918e18c2018-02-13 15:51:07 -08001458
James Feist3cb5fec2018-01-23 14:41:51 -08001459 io.run();
1460 return 0;
1461}