blob: 7648c4a4660bdd551067c63536cd39eca6bb576c [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*/
16
James Feist3b860982018-10-02 14:34:07 -070017#include <errno.h>
James Feist3cb5fec2018-01-23 14:41:51 -080018#include <fcntl.h>
James Feist3b860982018-10-02 14:34:07 -070019#include <sys/inotify.h>
20#include <sys/ioctl.h>
21
22#include <Utils.hpp>
23#include <boost/algorithm/string/predicate.hpp>
24#include <boost/container/flat_map.hpp>
25#include <chrono>
26#include <ctime>
Patrick Venturee3754002019-08-06 09:39:12 -070027#include <filesystem>
James Feist3cb5fec2018-01-23 14:41:51 -080028#include <fstream>
29#include <future>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070030#include <iomanip>
James Feist3cb5fec2018-01-23 14:41:51 -080031#include <iostream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070032#include <nlohmann/json.hpp>
James Feist3f8a2782018-02-12 09:24:42 -080033#include <regex>
James Feist3b860982018-10-02 14:34:07 -070034#include <sdbusplus/asio/connection.hpp>
35#include <sdbusplus/asio/object_server.hpp>
Patrick Venturec50e1ff2019-08-06 10:22:28 -070036#include <set>
37#include <sstream>
Patrick Venture11f1ff42019-08-01 10:42:12 -070038#include <string>
James Feist3b860982018-10-02 14:34:07 -070039#include <thread>
James Feista465ccc2019-02-08 12:51:01 -080040#include <variant>
James Feist3b860982018-10-02 14:34:07 -070041
42extern "C" {
43#include <i2c/smbus.h>
44#include <linux/i2c-dev.h>
45}
James Feist3cb5fec2018-01-23 14:41:51 -080046
Ed Tanous072e25d2018-12-16 21:45:20 -080047namespace fs = std::filesystem;
James Feist3cb5fec2018-01-23 14:41:51 -080048static constexpr bool DEBUG = false;
49static size_t UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feistb49ffc32018-05-02 11:10:43 -070050constexpr size_t MAX_FRU_SIZE = 512;
51constexpr size_t MAX_EEPROM_PAGE_INDEX = 255;
James Feist26c27ad2018-07-25 15:09:40 -070052constexpr size_t busTimeoutSeconds = 5;
James Feist3cb5fec2018-01-23 14:41:51 -080053
Patrick Venture11f1ff42019-08-01 10:42:12 -070054constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json";
55
James Feista465ccc2019-02-08 12:51:01 -080056const static constexpr char* BASEBOARD_FRU_LOCATION =
James Feist3cb5fec2018-01-23 14:41:51 -080057 "/etc/fru/baseboard.fru.bin";
58
James Feista465ccc2019-02-08 12:51:01 -080059const static constexpr char* I2C_DEV_LOCATION = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080060
James Feista465ccc2019-02-08 12:51:01 -080061static constexpr std::array<const char*, 5> FRU_AREAS = {
James Feist3cb5fec2018-01-23 14:41:51 -080062 "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"};
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -070063const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]");
James Feist3cb5fec2018-01-23 14:41:51 -080064using DeviceMap = boost::container::flat_map<int, std::vector<char>>;
65using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
66
James Feist444830e2019-04-05 08:38:16 -070067static std::set<size_t> busBlacklist;
James Feist6ebf9de2018-05-15 15:01:17 -070068struct FindDevicesWithCallback;
69
Nikhil Potaded8884f12019-03-27 13:27:13 -070070static BusMap busMap;
71
Patrick Venturec50e1ff2019-08-06 10:22:28 -070072// Given a bus/address, produce the path in sysfs for an eeprom.
73static std::string getEepromPath(size_t bus, size_t address)
74{
75 std::stringstream output;
76 output << "/sys/bus/i2c/devices/" << bus << "-" << std::right
77 << std::setfill('0') << std::setw(4) << std::hex << address
78 << "/eeprom";
79 return output.str();
80}
81
82static bool hasEepromFile(size_t bus, size_t address)
83{
84 auto path = getEepromPath(bus, address);
85 try
86 {
87 return fs::exists(path);
88 }
89 catch (...)
90 {
91 return false;
92 }
93}
94
95static ssize_t readFromEeprom(int fd, uint16_t offset, uint8_t len,
96 uint8_t* buf)
97{
98 auto result = lseek(fd, offset, SEEK_SET);
99 if (result < 0)
100 {
101 std::cerr << "failed to seek\n";
102 return -1;
103 }
104
105 return read(fd, buf, len);
106}
107
James Feistc95cb142018-02-26 10:41:42 -0800108static bool isMuxBus(size_t bus)
109{
Ed Tanous072e25d2018-12-16 21:45:20 -0800110 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -0800111 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
112}
113
Vijay Khemka2d681f62018-11-06 15:51:00 -0800114static int isDevice16Bit(int file)
115{
116 /* Get first byte */
117 int byte1 = i2c_smbus_read_byte_data(file, 0);
118 if (byte1 < 0)
119 {
120 return byte1;
121 }
122 /* Read 7 more bytes, it will read same first byte in case of
123 * 8 bit but it will read next byte in case of 16 bit
124 */
125 for (int i = 0; i < 7; i++)
126 {
127 int byte2 = i2c_smbus_read_byte_data(file, 0);
128 if (byte2 < 0)
129 {
130 return byte2;
131 }
132 if (byte2 != byte1)
133 {
134 return 1;
135 }
136 }
137 return 0;
138}
139
140static int read_block_data(int flag, int file, uint16_t offset, uint8_t len,
James Feista465ccc2019-02-08 12:51:01 -0800141 uint8_t* buf)
Vijay Khemka2d681f62018-11-06 15:51:00 -0800142{
James Feist98132792019-07-09 13:29:09 -0700143 uint8_t low_addr = static_cast<uint8_t>(offset);
144 uint8_t high_addr = static_cast<uint8_t>(offset >> 8);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800145
146 if (flag == 0)
147 {
148 return i2c_smbus_read_i2c_block_data(file, low_addr, len, buf);
149 }
150
151 /* This is for 16 bit addressing EEPROM device. First an offset
152 * needs to be written before read data from a offset
153 */
154 int ret = i2c_smbus_write_byte_data(file, 0, low_addr);
155 if (ret < 0)
156 {
157 return ret;
158 }
159
160 return i2c_smbus_read_i2c_block_data(file, high_addr, len, buf);
161}
162
James Feist24bae7a2019-04-03 09:50:56 -0700163bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData)
164{
165 // ipmi spec format version number is currently at 1, verify it
166 if (blockData[0] != 0x1)
167 {
168 return false;
169 }
170
171 // verify pad is set to 0
172 if (blockData[6] != 0x0)
173 {
174 return false;
175 }
176
177 // verify offsets are 0, or don't point to another offset
178 std::set<uint8_t> foundOffsets;
179 for (int ii = 1; ii < 6; ii++)
180 {
181 if (blockData[ii] == 0)
182 {
183 continue;
184 }
James Feist0eb40352019-04-09 14:44:04 -0700185 auto inserted = foundOffsets.insert(blockData[ii]);
186 if (!inserted.second)
James Feist24bae7a2019-04-03 09:50:56 -0700187 {
188 return false;
189 }
190 }
191
192 // validate checksum
193 size_t sum = 0;
194 for (int jj = 0; jj < 7; jj++)
195 {
196 sum += blockData[jj];
197 }
198 sum = (256 - sum) & 0xFF;
199
200 if (sum != blockData[7])
201 {
202 return false;
203 }
204 return true;
205}
206
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700207// TODO: This code is very similar to the non-eeprom version and can be merged
208// with some tweaks.
209static std::vector<char> processEeprom(int bus, int address)
210{
211 std::vector<char> device;
212 std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> block_data;
213
214 auto path = getEepromPath(bus, address);
215
216 int file = open(path.c_str(), O_RDONLY);
217 if (file < 0)
218 {
219 std::cerr << "Unable to open eeprom file: " << path << "\n";
220 return device;
221 }
222
223 ssize_t readBytes = readFromEeprom(file, 0, 0x8, block_data.data());
224 if (readBytes < 0)
225 {
226 std::cerr << "failed to read eeprom at " << bus << " address "
227 << address << "\n";
228 close(file);
229 return device;
230 }
231
232 if (!validateHeader(block_data))
233 {
234 if (DEBUG)
235 {
236 std::cerr << "Illegal header at bus " << bus << " address "
237 << address << "\n";
238 }
239
240 close(file);
241 return device;
242 }
243
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700244 // Copy the IPMI Fru Header
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700245 device.insert(device.end(), block_data.begin(), block_data.begin() + 8);
246
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700247 int fruLength = 0;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700248 for (size_t jj = 1; jj <= FRU_AREAS.size(); jj++)
249 {
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700250 // TODO: offset can be 255, device is holding "chars" that's not good.
251 int area_offset = device[jj];
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700252 if (area_offset == 0)
253 {
254 continue;
255 }
256
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700257 area_offset *= 8;
258
259 if (readFromEeprom(file, static_cast<uint16_t>(area_offset), 0x2,
260 block_data.data()) < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700261 {
262 std::cerr << "failed to read bus " << bus << " address " << address
263 << "\n";
264 device.clear();
265 close(file);
266 return device;
267 }
268
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700269 // Ignore data type.
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700270 int length = block_data[1] * 8;
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700271 area_offset += length;
272 fruLength = (area_offset > fruLength) ? area_offset : fruLength;
273 }
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700274
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700275 // You already copied these first 8 bytes (the ipmi fru header size)
276 fruLength -= 8;
277
278 int readOffset = 8;
279
280 while (fruLength > 0)
281 {
282 int to_get = std::min(I2C_SMBUS_BLOCK_MAX, fruLength);
283
284 if (readFromEeprom(file, static_cast<uint16_t>(readOffset),
285 static_cast<uint8_t>(to_get), block_data.data()) < 0)
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700286 {
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700287 std::cerr << "failed to read bus " << bus << " address " << address
288 << "\n";
289 device.clear();
290 close(file);
291 return device;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700292 }
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700293
294 device.insert(device.end(), block_data.begin(),
295 block_data.begin() + to_get);
296
297 readOffset += to_get;
298 fruLength -= to_get;
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700299 }
300
301 close(file);
302 return device;
303}
304
305std::set<int> findI2CEeproms(int i2cBus, std::shared_ptr<DeviceMap> devices)
306{
307 std::set<int> foundList;
308
309 std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus);
310
311 // For each file listed under the i2c device
312 // NOTE: This should be faster than just checking for each possible address
313 // path.
314 for (const auto& p : fs::directory_iterator(path))
315 {
316 const std::string node = p.path().string();
317 std::smatch m;
318 bool found =
319 std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)"));
320
321 if (!found)
322 {
323 continue;
324 }
325 if (m.size() != 2)
326 {
327 std::cerr << "regex didn't capture\n";
328 continue;
329 }
330
331 std::ssub_match subMatch = m[1];
332 std::string addressString = subMatch.str();
333
334 std::size_t ignored;
335 const int hexBase = 16;
336 int address = std::stoi(addressString, &ignored, hexBase);
337
338 const std::string eeprom = node + "/eeprom";
339
340 try
341 {
342 if (!fs::exists(eeprom))
343 {
344 continue;
345 }
346 }
347 catch (...)
348 {
349 continue;
350 }
351
352 // There is an eeprom file at this address, it may have invalid
353 // contents, but we found it.
354 foundList.insert(address);
355
356 std::vector<char> device = processEeprom(i2cBus, address);
357 if (!device.empty())
358 {
359 devices->emplace(address, device);
360 }
361 }
362
363 return foundList;
364}
365
James Feist3cb5fec2018-01-23 14:41:51 -0800366int get_bus_frus(int file, int first, int last, int bus,
367 std::shared_ptr<DeviceMap> devices)
368{
James Feist3cb5fec2018-01-23 14:41:51 -0800369
James Feist26c27ad2018-07-25 15:09:40 -0700370 std::future<int> future = std::async(std::launch::async, [&]() {
371 std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> block_data;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800372
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700373 // NOTE: When reading the devices raw on the bus, it can interfere with
374 // the driver's ability to operate, therefore read eeproms first before
375 // scanning for devices without drivers. Several experiments were run
376 // and it was determined that if there were any devices on the bus
377 // before the eeprom was hit and read, the eeprom driver wouldn't open
378 // while the bus device was open. An experiment was not performed to see
379 // if this issue was resolved if the i2c bus device was closed, but
380 // hexdumps of the eeprom later were successful.
381
382 // Scan for i2c eeproms loaded on this bus.
383 std::set<int> skipList = findI2CEeproms(bus, devices);
384
James Feist26c27ad2018-07-25 15:09:40 -0700385 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800386 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700387 if (skipList.find(ii) != skipList.end())
388 {
389 continue;
390 }
James Feist3cb5fec2018-01-23 14:41:51 -0800391
James Feist26c27ad2018-07-25 15:09:40 -0700392 // Set slave address
393 if (ioctl(file, I2C_SLAVE_FORCE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800394 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700395 std::cerr << "device at bus " << bus << " register " << ii
Patrick Venture5d8b61d2019-08-06 12:36:10 -0700396 << " busy\n";
James Feist26c27ad2018-07-25 15:09:40 -0700397 continue;
398 }
399 // probe
400 else if (i2c_smbus_read_byte(file) < 0)
401 {
402 continue;
403 }
James Feist3cb5fec2018-01-23 14:41:51 -0800404
James Feist26c27ad2018-07-25 15:09:40 -0700405 if (DEBUG)
406 {
Patrick Venture98e0cf32019-08-02 11:11:03 -0700407 std::cout << "something at bus " << bus << " addr " << ii
James Feist26c27ad2018-07-25 15:09:40 -0700408 << "\n";
409 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800410
411 /* Check for Device type if it is 8 bit or 16 bit */
412 int flag = isDevice16Bit(file);
413 if (flag < 0)
414 {
415 std::cerr << "failed to read bus " << bus << " address " << ii
416 << "\n";
417 continue;
418 }
419
420 if (read_block_data(flag, file, 0x0, 0x8, block_data.data()) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700421 {
422 std::cerr << "failed to read bus " << bus << " address " << ii
423 << "\n";
424 continue;
425 }
James Feist26c27ad2018-07-25 15:09:40 -0700426
427 // check the header checksum
Patrick Venture786f1792019-08-05 16:33:44 -0700428 if (!validateHeader(block_data))
James Feist26c27ad2018-07-25 15:09:40 -0700429 {
Patrick Venture786f1792019-08-05 16:33:44 -0700430 if (DEBUG)
James Feist26c27ad2018-07-25 15:09:40 -0700431 {
Patrick Venture786f1792019-08-05 16:33:44 -0700432 std::cerr << "Illegal header at bus " << bus << " address "
433 << ii << "\n";
434 }
435 continue;
436 }
437
438 std::vector<char> device;
439 device.insert(device.end(), block_data.begin(),
440 block_data.begin() + 8);
441
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700442 int fruLength = 0;
Patrick Venture786f1792019-08-05 16:33:44 -0700443 for (size_t jj = 1; jj <= FRU_AREAS.size(); jj++)
444 {
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700445 // TODO: offset can be 255, device is holding "chars" that's not
446 // good.
447 int area_offset = device[jj];
Patrick Venture64fd7e22019-08-05 16:38:20 -0700448 if (area_offset == 0)
Patrick Venture786f1792019-08-05 16:33:44 -0700449 {
Patrick Venture64fd7e22019-08-05 16:38:20 -0700450 continue;
451 }
452
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700453 area_offset *= 8;
454
455 if (read_block_data(flag, file,
456 static_cast<uint16_t>(area_offset), 0x2,
Patrick Venture64fd7e22019-08-05 16:38:20 -0700457 block_data.data()) < 0)
458 {
459 std::cerr << "failed to read bus " << bus << " address "
460 << ii << "\n";
461 return -1;
462 }
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700463
464 // Ignore data type.
Patrick Venture64fd7e22019-08-05 16:38:20 -0700465 int length = block_data[1] * 8;
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700466 area_offset += length;
467 fruLength = (area_offset > fruLength) ? area_offset : fruLength;
468 }
Patrick Venture64fd7e22019-08-05 16:38:20 -0700469
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700470 // You already copied these first 8 bytes (the ipmi fru header size)
471 fruLength -= 8;
472
473 int readOffset = 8;
474
475 while (fruLength > 0)
476 {
477 int to_get = std::min(I2C_SMBUS_BLOCK_MAX, fruLength);
478
479 if (read_block_data(
480 flag, file, static_cast<uint16_t>(readOffset),
481 static_cast<uint8_t>(to_get), block_data.data()) < 0)
Patrick Venture64fd7e22019-08-05 16:38:20 -0700482 {
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700483 std::cerr << "failed to read bus " << bus << " address "
484 << ii << "\n";
485 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800486 }
Patrick Venture4cc29fc2019-08-08 13:36:18 -0700487
488 device.insert(device.end(), block_data.begin(),
489 block_data.begin() + to_get);
490
491 readOffset += to_get;
492 fruLength -= to_get;
James Feist3cb5fec2018-01-23 14:41:51 -0800493 }
Patrick Venture786f1792019-08-05 16:33:44 -0700494 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800495 }
James Feist26c27ad2018-07-25 15:09:40 -0700496 return 1;
497 });
498 std::future_status status =
499 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
500 if (status == std::future_status::timeout)
501 {
502 std::cerr << "Error reading bus " << bus << "\n";
James Feist444830e2019-04-05 08:38:16 -0700503 busBlacklist.insert(bus);
504 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700505 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800506 }
507
James Feist444830e2019-04-05 08:38:16 -0700508 close(file);
James Feist26c27ad2018-07-25 15:09:40 -0700509 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800510}
511
Patrick Venture11f1ff42019-08-01 10:42:12 -0700512void loadBlacklist(const char* path)
513{
514 std::ifstream blacklistStream(path);
515 if (!blacklistStream.good())
516 {
517 // File is optional.
518 std::cerr << "Cannot open blacklist file.\n\n";
519 return;
520 }
521
522 nlohmann::json data =
523 nlohmann::json::parse(blacklistStream, nullptr, false);
524 if (data.is_discarded())
525 {
526 std::cerr << "Illegal blacklist file detected, cannot validate JSON, "
527 "exiting\n";
528 std::exit(EXIT_FAILURE);
529 return;
530 }
531
532 // It's expected to have at least one field, "buses" that is an array of the
533 // buses by integer. Allow for future options to exclude further aspects,
534 // such as specific addresses or ranges.
535 if (data.type() != nlohmann::json::value_t::object)
536 {
537 std::cerr << "Illegal blacklist, expected to read dictionary\n";
538 std::exit(EXIT_FAILURE);
539 return;
540 }
541
542 // If buses field is missing, that's fine.
543 if (data.count("buses") == 1)
544 {
545 // Parse the buses array after a little validation.
546 auto buses = data.at("buses");
547 if (buses.type() != nlohmann::json::value_t::array)
548 {
549 // Buses field present but invalid, therefore this is an error.
550 std::cerr << "Invalid contents for blacklist buses field\n";
551 std::exit(EXIT_FAILURE);
552 return;
553 }
554
555 // Catch exception here for type mis-match.
556 try
557 {
558 for (const auto& bus : buses)
559 {
560 busBlacklist.insert(bus.get<size_t>());
561 }
562 }
563 catch (const nlohmann::detail::type_error& e)
564 {
565 // Type mis-match is a critical error.
566 std::cerr << "Invalid bus type: " << e.what() << "\n";
567 std::exit(EXIT_FAILURE);
568 return;
569 }
570 }
571
572 return;
573}
574
James Feista465ccc2019-02-08 12:51:01 -0800575static void FindI2CDevices(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700576 BusMap& busmap)
James Feist3cb5fec2018-01-23 14:41:51 -0800577{
James Feista465ccc2019-02-08 12:51:01 -0800578 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800579 {
580 auto busnum = i2cBus.string();
581 auto lastDash = busnum.rfind(std::string("-"));
582 // delete everything before dash inclusive
583 if (lastDash != std::string::npos)
584 {
585 busnum.erase(0, lastDash + 1);
586 }
587 auto bus = std::stoi(busnum);
James Feist444830e2019-04-05 08:38:16 -0700588 if (busBlacklist.find(bus) != busBlacklist.end())
589 {
590 continue; // skip previously failed busses
591 }
James Feist3cb5fec2018-01-23 14:41:51 -0800592
593 auto file = open(i2cBus.c_str(), O_RDWR);
594 if (file < 0)
595 {
596 std::cerr << "unable to open i2c device " << i2cBus.string()
597 << "\n";
598 continue;
599 }
600 unsigned long funcs = 0;
601
602 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
603 {
604 std::cerr
Patrick Venture98e0cf32019-08-02 11:11:03 -0700605 << "Error: Could not get the adapter functionality matrix bus "
James Feist3cb5fec2018-01-23 14:41:51 -0800606 << bus << "\n";
607 continue;
608 }
609 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
610 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
611 {
612 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
613 << bus << "\n";
614 continue;
615 }
James Feist98132792019-07-09 13:29:09 -0700616 auto& device = busmap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800617 device = std::make_shared<DeviceMap>();
618
Nikhil Potaded8884f12019-03-27 13:27:13 -0700619 // i2cdetect by default uses the range 0x03 to 0x77, as
620 // this is what we have tested with, use this range. Could be
621 // changed in future.
622 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800623 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700624 std::cerr << "Scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800625 }
Nikhil Potaded8884f12019-03-27 13:27:13 -0700626
627 // fd is closed in this function in case the bus locks up
628 get_bus_frus(file, 0x03, 0x77, bus, device);
629
630 if (DEBUG)
James Feistc95cb142018-02-26 10:41:42 -0800631 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700632 std::cerr << "Done scanning bus " << bus << "\n";
James Feistc95cb142018-02-26 10:41:42 -0800633 }
James Feist3cb5fec2018-01-23 14:41:51 -0800634 }
James Feist3cb5fec2018-01-23 14:41:51 -0800635}
636
James Feist6ebf9de2018-05-15 15:01:17 -0700637// this class allows an async response after all i2c devices are discovered
638struct FindDevicesWithCallback
639 : std::enable_shared_from_this<FindDevicesWithCallback>
640{
James Feista465ccc2019-02-08 12:51:01 -0800641 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
James Feist98132792019-07-09 13:29:09 -0700642 boost::asio::io_service& io, BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -0800643 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700644 _i2cBuses(i2cBuses),
James Feist98132792019-07-09 13:29:09 -0700645 _io(io), _busMap(busmap), _callback(std::move(callback))
James Feist6ebf9de2018-05-15 15:01:17 -0700646 {
647 }
648 ~FindDevicesWithCallback()
649 {
650 _callback();
651 }
652 void run()
653 {
James Feist98132792019-07-09 13:29:09 -0700654 FindI2CDevices(_i2cBuses, _busMap);
James Feist6ebf9de2018-05-15 15:01:17 -0700655 }
656
James Feista465ccc2019-02-08 12:51:01 -0800657 const std::vector<fs::path>& _i2cBuses;
658 boost::asio::io_service& _io;
659 BusMap& _busMap;
James Feist6ebf9de2018-05-15 15:01:17 -0700660 std::function<void(void)> _callback;
661};
662
James Feist3cb5fec2018-01-23 14:41:51 -0800663static const std::tm intelEpoch(void)
664{
James Feist98132792019-07-09 13:29:09 -0700665 std::tm val = {};
James Feist3cb5fec2018-01-23 14:41:51 -0800666 val.tm_year = 1996 - 1900;
667 return val;
668}
669
James Feista465ccc2019-02-08 12:51:01 -0800670bool formatFru(const std::vector<char>& fruBytes,
671 boost::container::flat_map<std::string, std::string>& result)
James Feist3cb5fec2018-01-23 14:41:51 -0800672{
James Feista465ccc2019-02-08 12:51:01 -0800673 static const std::vector<const char*> CHASSIS_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800674 "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800675
James Feista465ccc2019-02-08 12:51:01 -0800676 static const std::vector<const char*> BOARD_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800677 "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
678 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800679
James Feista465ccc2019-02-08 12:51:01 -0800680 static const std::vector<const char*> PRODUCT_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800681 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER",
682 "VERSION", "SERIAL_NUMBER", "ASSET_TAG",
683 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800684
James Feistd068e932018-09-20 10:53:07 -0700685 if (fruBytes.size() <= 8)
James Feist3cb5fec2018-01-23 14:41:51 -0800686 {
687 return false;
688 }
689 std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin();
James Feist9eb0b582018-04-27 12:15:46 -0700690 result["Common_Format_Version"] =
James Feist3cb5fec2018-01-23 14:41:51 -0800691 std::to_string(static_cast<int>(*fruAreaOffsetField));
692
James Feista465ccc2019-02-08 12:51:01 -0800693 const std::vector<const char*>* fieldData;
James Feist3cb5fec2018-01-23 14:41:51 -0800694
James Feist0eb40352019-04-09 14:44:04 -0700695 for (const std::string& area : FRU_AREAS)
James Feist3cb5fec2018-01-23 14:41:51 -0800696 {
697 fruAreaOffsetField++;
698 if (fruAreaOffsetField >= fruBytes.end())
699 {
700 return false;
701 }
702 size_t offset = (*fruAreaOffsetField) * 8;
703
704 if (offset > 1)
705 {
706 // +2 to skip format and length
707 std::vector<char>::const_iterator fruBytesIter =
708 fruBytes.begin() + offset + 2;
709
710 if (fruBytesIter >= fruBytes.end())
711 {
712 return false;
713 }
714
715 if (area == "CHASSIS")
716 {
717 result["CHASSIS_TYPE"] =
718 std::to_string(static_cast<int>(*fruBytesIter));
719 fruBytesIter += 1;
720 fieldData = &CHASSIS_FRU_AREAS;
721 }
722 else if (area == "BOARD")
723 {
724 result["BOARD_LANGUAGE_CODE"] =
725 std::to_string(static_cast<int>(*fruBytesIter));
726 fruBytesIter += 1;
727 if (fruBytesIter >= fruBytes.end())
728 {
729 return false;
730 }
731
732 unsigned int minutes = *fruBytesIter |
733 *(fruBytesIter + 1) << 8 |
734 *(fruBytesIter + 2) << 16;
735 std::tm fruTime = intelEpoch();
736 time_t timeValue = mktime(&fruTime);
737 timeValue += minutes * 60;
738 fruTime = *gmtime(&timeValue);
739
740 result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime);
741 result["BOARD_MANUFACTURE_DATE"]
742 .pop_back(); // remove trailing null
743 fruBytesIter += 3;
744 fieldData = &BOARD_FRU_AREAS;
745 }
746 else if (area == "PRODUCT")
747 {
748 result["PRODUCT_LANGUAGE_CODE"] =
749 std::to_string(static_cast<int>(*fruBytesIter));
750 fruBytesIter += 1;
751 fieldData = &PRODUCT_FRU_AREAS;
752 }
753 else
754 {
755 continue;
756 }
James Feista465ccc2019-02-08 12:51:01 -0800757 for (auto& field : *fieldData)
James Feist3cb5fec2018-01-23 14:41:51 -0800758 {
759 if (fruBytesIter >= fruBytes.end())
760 {
761 return false;
762 }
763
Vijay Khemka5d5de442018-11-07 10:51:25 -0800764 /* Checking for last byte C1 to indicate that no more
765 * field to be read */
James Feist98132792019-07-09 13:29:09 -0700766 if (static_cast<uint8_t>(*fruBytesIter) == 0xC1)
Vijay Khemka5d5de442018-11-07 10:51:25 -0800767 {
768 break;
769 }
770
Ed Tanous2147e672019-02-27 13:59:56 -0800771 size_t length = *fruBytesIter & 0x3f;
772 fruBytesIter += 1;
773
James Feist3cb5fec2018-01-23 14:41:51 -0800774 if (fruBytesIter >= fruBytes.end())
775 {
776 return false;
777 }
Ed Tanous2147e672019-02-27 13:59:56 -0800778 std::string value(fruBytesIter, fruBytesIter + length);
James Feist3cb5fec2018-01-23 14:41:51 -0800779
Ed Tanous2147e672019-02-27 13:59:56 -0800780 // Strip non null characters from the end
781 value.erase(std::find_if(value.rbegin(), value.rend(),
782 [](char ch) { return ch != 0; })
783 .base(),
784 value.end());
785
James Feist0eb40352019-04-09 14:44:04 -0700786 result[area + "_" + field] = std::move(value);
Ed Tanous2147e672019-02-27 13:59:56 -0800787
James Feist3cb5fec2018-01-23 14:41:51 -0800788 fruBytesIter += length;
789 if (fruBytesIter >= fruBytes.end())
790 {
791 std::cerr << "Warning Fru Length Mismatch:\n ";
James Feista465ccc2019-02-08 12:51:01 -0800792 for (auto& c : fruBytes)
James Feist3cb5fec2018-01-23 14:41:51 -0800793 {
794 std::cerr << c;
795 }
796 std::cerr << "\n";
797 if (DEBUG)
798 {
James Feista465ccc2019-02-08 12:51:01 -0800799 for (auto& keyPair : result)
James Feist3cb5fec2018-01-23 14:41:51 -0800800 {
801 std::cerr << keyPair.first << " : "
802 << keyPair.second << "\n";
803 }
804 }
805 return false;
806 }
807 }
808 }
809 }
810
811 return true;
812}
813
Nikhil Potaded8884f12019-03-27 13:27:13 -0700814std::vector<uint8_t>& getFruInfo(const uint8_t& bus, const uint8_t& address)
815{
816 auto deviceMap = busMap.find(bus);
817 if (deviceMap == busMap.end())
818 {
819 throw std::invalid_argument("Invalid Bus.");
820 }
821 auto device = deviceMap->second->find(address);
822 if (device == deviceMap->second->end())
823 {
824 throw std::invalid_argument("Invalid Address.");
825 }
826 std::vector<uint8_t>& ret =
827 reinterpret_cast<std::vector<uint8_t>&>(device->second);
828
829 return ret;
830}
831
James Feist3cb5fec2018-01-23 14:41:51 -0800832void AddFruObjectToDbus(
James Feista465ccc2019-02-08 12:51:01 -0800833 std::vector<char>& device, sdbusplus::asio::object_server& objServer,
834 boost::container::flat_map<
835 std::pair<size_t, size_t>,
836 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -0700837 uint32_t bus, uint32_t address)
James Feist3cb5fec2018-01-23 14:41:51 -0800838{
839 boost::container::flat_map<std::string, std::string> formattedFru;
840 if (!formatFru(device, formattedFru))
841 {
Patrick Ventureb755c832019-08-07 11:09:14 -0700842 std::cerr << "failed to format fru for device at bus " << bus
843 << " address " << address << "\n";
James Feist3cb5fec2018-01-23 14:41:51 -0800844 return;
845 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700846
James Feist3cb5fec2018-01-23 14:41:51 -0800847 auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME");
848 std::string productName;
Patrick Venture96cdaef2019-07-30 13:30:52 -0700849 // Not found under Board section or an empty string.
850 if (productNameFind == formattedFru.end() ||
851 productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800852 {
853 productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME");
854 }
Patrick Venture96cdaef2019-07-30 13:30:52 -0700855 // Found under Product section and not an empty string.
856 if (productNameFind != formattedFru.end() &&
857 !productNameFind->second.empty())
James Feist3cb5fec2018-01-23 14:41:51 -0800858 {
859 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800860 std::regex illegalObject("[^A-Za-z0-9_]");
861 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800862 }
863 else
864 {
865 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
866 UNKNOWN_BUS_OBJECT_COUNT++;
867 }
868
James Feist918e18c2018-02-13 15:51:07 -0800869 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800870 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700871 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800872 {
James Feist79e9c0b2018-03-15 15:21:17 -0700873 size_t index = 0;
James Feista465ccc2019-02-08 12:51:01 -0800874 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800875 {
James Feist9eb0b582018-04-27 12:15:46 -0700876 if ((busIface.second->get_object_path() == productName))
James Feist918e18c2018-02-13 15:51:07 -0800877 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700878 if (isMuxBus(bus) && address == busIface.first.second &&
James Feist98132792019-07-09 13:29:09 -0700879 (getFruInfo(static_cast<uint8_t>(busIface.first.first),
880 static_cast<uint8_t>(busIface.first.second)) ==
881 getFruInfo(static_cast<uint8_t>(bus),
882 static_cast<uint8_t>(address))))
James Feist79e9c0b2018-03-15 15:21:17 -0700883 {
Nikhil Potaded8884f12019-03-27 13:27:13 -0700884 // This device is already added to the lower numbered bus,
885 // do not replicate it.
886 return;
James Feist79e9c0b2018-03-15 15:21:17 -0700887 }
888 // add underscore _index for the same object path on dbus
889 std::string strIndex = std::to_string(index);
890 if (index > 0)
891 {
892 productName.substr(0, productName.size() - strIndex.size());
893 }
894 else
895 {
896 productName += "_";
897 }
898 productName += std::to_string(index++);
James Feist918e18c2018-02-13 15:51:07 -0800899 }
900 }
901 }
James Feist3cb5fec2018-01-23 14:41:51 -0800902
James Feist9eb0b582018-04-27 12:15:46 -0700903 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
904 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
905 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
906
James Feista465ccc2019-02-08 12:51:01 -0800907 for (auto& property : formattedFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800908 {
James Feist9eb0b582018-04-27 12:15:46 -0700909
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700910 std::regex_replace(property.second.begin(), property.second.begin(),
911 property.second.end(), NON_ASCII_REGEX, "_");
James Feist9eb0b582018-04-27 12:15:46 -0700912 if (property.second.empty())
913 {
914 continue;
915 }
916 std::string key =
917 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
918 if (!iface->register_property(key, property.second + '\0'))
919 {
920 std::cerr << "illegal key: " << key << "\n";
921 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700922 if (DEBUG)
923 {
924 std::cout << property.first << ": " << property.second << "\n";
925 }
James Feist3cb5fec2018-01-23 14:41:51 -0800926 }
James Feist2a9d6db2018-04-27 15:48:28 -0700927
928 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700929 iface->register_property("BUS", bus);
930 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700931
James Feist9eb0b582018-04-27 12:15:46 -0700932 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800933}
934
James Feista465ccc2019-02-08 12:51:01 -0800935static bool readBaseboardFru(std::vector<char>& baseboardFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800936{
937 // try to read baseboard fru from file
938 std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
939 if (baseboardFruFile.good())
940 {
941 baseboardFruFile.seekg(0, std::ios_base::end);
James Feist98132792019-07-09 13:29:09 -0700942 size_t fileSize = static_cast<size_t>(baseboardFruFile.tellg());
James Feist3cb5fec2018-01-23 14:41:51 -0800943 baseboardFru.resize(fileSize);
944 baseboardFruFile.seekg(0, std::ios_base::beg);
945 baseboardFruFile.read(baseboardFru.data(), fileSize);
946 }
947 else
948 {
949 return false;
950 }
951 return true;
952}
953
James Feista465ccc2019-02-08 12:51:01 -0800954bool writeFru(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700955{
956 boost::container::flat_map<std::string, std::string> tmp;
957 if (fru.size() > MAX_FRU_SIZE)
958 {
959 std::cerr << "Invalid fru.size() during writeFru\n";
960 return false;
961 }
962 // verify legal fru by running it through fru parsing logic
James Feista465ccc2019-02-08 12:51:01 -0800963 if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp))
James Feistb49ffc32018-05-02 11:10:43 -0700964 {
965 std::cerr << "Invalid fru format during writeFru\n";
966 return false;
967 }
968 // baseboard fru
969 if (bus == 0 && address == 0)
970 {
971 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
972 if (!file.good())
973 {
974 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
975 << "\n";
James Feistddb78302018-09-06 11:45:42 -0700976 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700977 return false;
978 }
James Feista465ccc2019-02-08 12:51:01 -0800979 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700980 return file.good();
981 }
982 else
983 {
Patrick Venturec50e1ff2019-08-06 10:22:28 -0700984 if (hasEepromFile(bus, address))
985 {
986 auto path = getEepromPath(bus, address);
987 int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC);
988 if (eeprom < 0)
989 {
990 std::cerr << "unable to open i2c device " << path << "\n";
991 throw DBusInternalError();
992 return false;
993 }
994
995 ssize_t writtenBytes = write(eeprom, fru.data(), fru.size());
996 if (writtenBytes < 0)
997 {
998 std::cerr << "unable to write to i2c device " << path << "\n";
999 close(eeprom);
1000 throw DBusInternalError();
1001 return false;
1002 }
1003
1004 close(eeprom);
1005 return true;
1006 }
1007
James Feistb49ffc32018-05-02 11:10:43 -07001008 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
1009
1010 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
1011 if (file < 0)
1012 {
1013 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -07001014 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001015 return false;
1016 }
1017 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
1018 {
1019 std::cerr << "unable to set device address\n";
1020 close(file);
James Feistddb78302018-09-06 11:45:42 -07001021 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001022 return false;
1023 }
1024
1025 constexpr const size_t RETRY_MAX = 2;
1026 uint16_t index = 0;
1027 size_t retries = RETRY_MAX;
1028 while (index < fru.size())
1029 {
1030 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
1031 (retries == RETRY_MAX))
1032 {
1033 // The 4K EEPROM only uses the A2 and A1 device address bits
1034 // with the third bit being a memory page address bit.
1035 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
1036 {
1037 std::cerr << "unable to set device address\n";
1038 close(file);
James Feistddb78302018-09-06 11:45:42 -07001039 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001040 return false;
1041 }
1042 }
1043
James Feist98132792019-07-09 13:29:09 -07001044 if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
1045 fru[index]) < 0)
James Feistb49ffc32018-05-02 11:10:43 -07001046 {
1047 if (!retries--)
1048 {
1049 std::cerr << "error writing fru: " << strerror(errno)
1050 << "\n";
1051 close(file);
James Feistddb78302018-09-06 11:45:42 -07001052 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -07001053 return false;
1054 }
1055 }
1056 else
1057 {
1058 retries = RETRY_MAX;
1059 index++;
1060 }
1061 // most eeproms require 5-10ms between writes
1062 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1063 }
1064 close(file);
1065 return true;
1066 }
1067}
1068
James Feist9eb0b582018-04-27 12:15:46 -07001069void rescanBusses(
James Feist98132792019-07-09 13:29:09 -07001070 boost::asio::io_service& io, BusMap& busmap,
James Feista465ccc2019-02-08 12:51:01 -08001071 boost::container::flat_map<
1072 std::pair<size_t, size_t>,
1073 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feista465ccc2019-02-08 12:51:01 -08001074 sdbusplus::asio::object_server& objServer)
James Feist918e18c2018-02-13 15:51:07 -08001075{
James Feist6ebf9de2018-05-15 15:01:17 -07001076 static boost::asio::deadline_timer timer(io);
1077 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -08001078
Gunnar Mills6f0ae942018-08-31 12:38:03 -05001079 // setup an async wait in case we get flooded with requests
James Feist98132792019-07-09 13:29:09 -07001080 timer.async_wait([&](const boost::system::error_code&) {
James Feist4131aea2018-03-09 09:47:30 -08001081 auto devDir = fs::path("/dev/");
James Feist4131aea2018-03-09 09:47:30 -08001082 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -08001083
Nikhil Potaded8884f12019-03-27 13:27:13 -07001084 boost::container::flat_map<size_t, fs::path> busPaths;
1085 if (!getI2cDevicePaths(devDir, busPaths))
James Feist918e18c2018-02-13 15:51:07 -08001086 {
James Feist4131aea2018-03-09 09:47:30 -08001087 std::cerr << "unable to find i2c devices\n";
1088 return;
James Feist918e18c2018-02-13 15:51:07 -08001089 }
Nikhil Potaded8884f12019-03-27 13:27:13 -07001090
1091 for (auto busPath : busPaths)
1092 {
1093 i2cBuses.emplace_back(busPath.second);
1094 }
James Feist4131aea2018-03-09 09:47:30 -08001095
James Feist98132792019-07-09 13:29:09 -07001096 busmap.clear();
James Feist6ebf9de2018-05-15 15:01:17 -07001097 auto scan = std::make_shared<FindDevicesWithCallback>(
James Feist98132792019-07-09 13:29:09 -07001098 i2cBuses, io, busmap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -08001099 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -07001100 {
1101 objServer.remove_interface(busIface.second);
1102 }
James Feist4131aea2018-03-09 09:47:30 -08001103
James Feist6ebf9de2018-05-15 15:01:17 -07001104 dbusInterfaceMap.clear();
1105 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -08001106
James Feist6ebf9de2018-05-15 15:01:17 -07001107 // todo, get this from a more sensable place
1108 std::vector<char> baseboardFru;
1109 if (readBaseboardFru(baseboardFru))
1110 {
1111 boost::container::flat_map<int, std::vector<char>>
1112 baseboardDev;
1113 baseboardDev.emplace(0, baseboardFru);
James Feist98132792019-07-09 13:29:09 -07001114 busmap[0] = std::make_shared<DeviceMap>(baseboardDev);
James Feist6ebf9de2018-05-15 15:01:17 -07001115 }
James Feist98132792019-07-09 13:29:09 -07001116 for (auto& devicemap : busmap)
James Feist6ebf9de2018-05-15 15:01:17 -07001117 {
James Feista465ccc2019-02-08 12:51:01 -08001118 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -07001119 {
James Feist98132792019-07-09 13:29:09 -07001120 AddFruObjectToDbus(device.second, objServer,
James Feist6ebf9de2018-05-15 15:01:17 -07001121 dbusInterfaceMap, devicemap.first,
1122 device.first);
1123 }
1124 }
1125 });
1126 scan->run();
1127 });
James Feist918e18c2018-02-13 15:51:07 -08001128}
1129
James Feist98132792019-07-09 13:29:09 -07001130int main()
James Feist3cb5fec2018-01-23 14:41:51 -08001131{
1132 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -08001133 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -08001134 std::vector<fs::path> i2cBuses;
1135
James Feista3c180a2018-08-09 16:06:04 -07001136 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -08001137 {
1138 std::cerr << "unable to find i2c devices\n";
1139 return 1;
1140 }
James Feist3cb5fec2018-01-23 14:41:51 -08001141
Patrick Venture11f1ff42019-08-01 10:42:12 -07001142 // check for and load blacklist with initial buses.
1143 loadBlacklist(blacklistPath);
1144
James Feist3cb5fec2018-01-23 14:41:51 -08001145 boost::asio::io_service io;
James Feist9eb0b582018-04-27 12:15:46 -07001146 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
1147 auto objServer = sdbusplus::asio::object_server(systemBus);
Vijay Khemka065f6d92018-12-18 10:37:47 -08001148 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -08001149
James Feist6ebf9de2018-05-15 15:01:17 -07001150 // this is a map with keys of pair(bus number, address) and values of
1151 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -08001152 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -07001153 std::shared_ptr<sdbusplus::asio::dbus_interface>>
1154 dbusInterfaceMap;
James Feist3cb5fec2018-01-23 14:41:51 -08001155
James Feist9eb0b582018-04-27 12:15:46 -07001156 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
1157 objServer.add_interface("/xyz/openbmc_project/FruDevice",
1158 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -08001159
1160 iface->register_method("ReScan", [&]() {
James Feist98132792019-07-09 13:29:09 -07001161 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist3cb5fec2018-01-23 14:41:51 -08001162 });
James Feist2a9d6db2018-04-27 15:48:28 -07001163
Nikhil Potaded8884f12019-03-27 13:27:13 -07001164 iface->register_method("GetRawFru", getFruInfo);
James Feistb49ffc32018-05-02 11:10:43 -07001165
1166 iface->register_method("WriteFru", [&](const uint8_t bus,
1167 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -08001168 const std::vector<uint8_t>& data) {
James Feistb49ffc32018-05-02 11:10:43 -07001169 if (!writeFru(bus, address, data))
1170 {
James Feistddb78302018-09-06 11:45:42 -07001171 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -07001172 return;
1173 }
1174 // schedule rescan on success
James Feist98132792019-07-09 13:29:09 -07001175 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feistb49ffc32018-05-02 11:10:43 -07001176 });
James Feist9eb0b582018-04-27 12:15:46 -07001177 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -08001178
James Feist9eb0b582018-04-27 12:15:46 -07001179 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -08001180 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -08001181 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -07001182 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -08001183 std::string,
1184 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -07001185 values;
1186 message.read(objectName, values);
James Feist918e18c2018-02-13 15:51:07 -08001187 auto findPgood = values.find("pgood");
1188 if (findPgood != values.end())
1189 {
James Feist6ebf9de2018-05-15 15:01:17 -07001190
James Feist98132792019-07-09 13:29:09 -07001191 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist918e18c2018-02-13 15:51:07 -08001192 }
James Feist918e18c2018-02-13 15:51:07 -08001193 };
James Feist9eb0b582018-04-27 12:15:46 -07001194
1195 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -08001196 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist7bcd3f22019-03-18 16:04:04 -07001197 "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/"
1198 "openbmc_project/Chassis/Control/"
1199 "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist9eb0b582018-04-27 12:15:46 -07001200 eventHandler);
1201
James Feist4131aea2018-03-09 09:47:30 -08001202 int fd = inotify_init();
James Feist0eb40352019-04-09 14:44:04 -07001203 inotify_add_watch(fd, I2C_DEV_LOCATION,
1204 IN_CREATE | IN_MOVED_TO | IN_DELETE);
James Feist4131aea2018-03-09 09:47:30 -08001205 std::array<char, 4096> readBuffer;
1206 std::string pendingBuffer;
1207 // monitor for new i2c devices
1208 boost::asio::posix::stream_descriptor dirWatch(io, fd);
1209 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -08001210 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -08001211 std::size_t bytes_transferred) {
1212 if (ec)
1213 {
1214 std::cout << "Callback Error " << ec << "\n";
1215 return;
1216 }
1217 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
1218 bool devChange = false;
1219 while (pendingBuffer.size() > sizeof(inotify_event))
1220 {
James Feista465ccc2019-02-08 12:51:01 -08001221 const inotify_event* iEvent =
1222 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -08001223 pendingBuffer.data());
1224 switch (iEvent->mask)
1225 {
James Feist9eb0b582018-04-27 12:15:46 -07001226 case IN_CREATE:
1227 case IN_MOVED_TO:
1228 case IN_DELETE:
1229 if (boost::starts_with(std::string(iEvent->name),
1230 "i2c"))
1231 {
1232 devChange = true;
1233 }
James Feist4131aea2018-03-09 09:47:30 -08001234 }
1235
1236 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
1237 }
James Feist6ebf9de2018-05-15 15:01:17 -07001238 if (devChange)
James Feist4131aea2018-03-09 09:47:30 -08001239 {
James Feist98132792019-07-09 13:29:09 -07001240 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist4131aea2018-03-09 09:47:30 -08001241 }
James Feist6ebf9de2018-05-15 15:01:17 -07001242
James Feist4131aea2018-03-09 09:47:30 -08001243 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
1244 watchI2cBusses);
1245 };
1246
1247 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -05001248 // run the initial scan
James Feist98132792019-07-09 13:29:09 -07001249 rescanBusses(io, busMap, dbusInterfaceMap, objServer);
James Feist918e18c2018-02-13 15:51:07 -08001250
James Feist3cb5fec2018-01-23 14:41:51 -08001251 io.run();
1252 return 0;
1253}