blob: 9ca578d9a4e8ef378792b45fc225568e26830cdb [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>
James Feist3cb5fec2018-01-23 14:41:51 -080027#include <fstream>
28#include <future>
James Feist3cb5fec2018-01-23 14:41:51 -080029#include <iostream>
James Feist3f8a2782018-02-12 09:24:42 -080030#include <regex>
James Feist3b860982018-10-02 14:34:07 -070031#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
33#include <thread>
James Feista465ccc2019-02-08 12:51:01 -080034#include <variant>
James Feist3b860982018-10-02 14:34:07 -070035
36extern "C" {
37#include <i2c/smbus.h>
38#include <linux/i2c-dev.h>
39}
James Feist3cb5fec2018-01-23 14:41:51 -080040
Ed Tanous072e25d2018-12-16 21:45:20 -080041namespace fs = std::filesystem;
James Feist3cb5fec2018-01-23 14:41:51 -080042static constexpr bool DEBUG = false;
43static size_t UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feistb49ffc32018-05-02 11:10:43 -070044constexpr size_t MAX_FRU_SIZE = 512;
45constexpr size_t MAX_EEPROM_PAGE_INDEX = 255;
James Feist26c27ad2018-07-25 15:09:40 -070046constexpr size_t busTimeoutSeconds = 5;
James Feist3cb5fec2018-01-23 14:41:51 -080047
James Feista465ccc2019-02-08 12:51:01 -080048const static constexpr char* BASEBOARD_FRU_LOCATION =
James Feist3cb5fec2018-01-23 14:41:51 -080049 "/etc/fru/baseboard.fru.bin";
50
James Feista465ccc2019-02-08 12:51:01 -080051const static constexpr char* I2C_DEV_LOCATION = "/dev";
James Feist4131aea2018-03-09 09:47:30 -080052
James Feista465ccc2019-02-08 12:51:01 -080053static constexpr std::array<const char*, 5> FRU_AREAS = {
James Feist3cb5fec2018-01-23 14:41:51 -080054 "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"};
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -070055const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]");
James Feist3cb5fec2018-01-23 14:41:51 -080056using DeviceMap = boost::container::flat_map<int, std::vector<char>>;
57using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>;
58
James Feist6ebf9de2018-05-15 15:01:17 -070059struct FindDevicesWithCallback;
60
James Feistc95cb142018-02-26 10:41:42 -080061static bool isMuxBus(size_t bus)
62{
Ed Tanous072e25d2018-12-16 21:45:20 -080063 return is_symlink(std::filesystem::path(
James Feistc95cb142018-02-26 10:41:42 -080064 "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device"));
65}
66
Vijay Khemka2d681f62018-11-06 15:51:00 -080067static int isDevice16Bit(int file)
68{
69 /* Get first byte */
70 int byte1 = i2c_smbus_read_byte_data(file, 0);
71 if (byte1 < 0)
72 {
73 return byte1;
74 }
75 /* Read 7 more bytes, it will read same first byte in case of
76 * 8 bit but it will read next byte in case of 16 bit
77 */
78 for (int i = 0; i < 7; i++)
79 {
80 int byte2 = i2c_smbus_read_byte_data(file, 0);
81 if (byte2 < 0)
82 {
83 return byte2;
84 }
85 if (byte2 != byte1)
86 {
87 return 1;
88 }
89 }
90 return 0;
91}
92
93static int read_block_data(int flag, int file, uint16_t offset, uint8_t len,
James Feista465ccc2019-02-08 12:51:01 -080094 uint8_t* buf)
Vijay Khemka2d681f62018-11-06 15:51:00 -080095{
96 uint8_t low_addr = offset & 0xFF;
97 uint8_t high_addr = (offset >> 8) & 0xFF;
98
99 if (flag == 0)
100 {
101 return i2c_smbus_read_i2c_block_data(file, low_addr, len, buf);
102 }
103
104 /* This is for 16 bit addressing EEPROM device. First an offset
105 * needs to be written before read data from a offset
106 */
107 int ret = i2c_smbus_write_byte_data(file, 0, low_addr);
108 if (ret < 0)
109 {
110 return ret;
111 }
112
113 return i2c_smbus_read_i2c_block_data(file, high_addr, len, buf);
114}
115
James Feist3cb5fec2018-01-23 14:41:51 -0800116int get_bus_frus(int file, int first, int last, int bus,
117 std::shared_ptr<DeviceMap> devices)
118{
James Feist3cb5fec2018-01-23 14:41:51 -0800119
James Feist26c27ad2018-07-25 15:09:40 -0700120 std::future<int> future = std::async(std::launch::async, [&]() {
121 std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> block_data;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800122
James Feist26c27ad2018-07-25 15:09:40 -0700123 for (int ii = first; ii <= last; ii++)
James Feist3cb5fec2018-01-23 14:41:51 -0800124 {
James Feist3cb5fec2018-01-23 14:41:51 -0800125
James Feist26c27ad2018-07-25 15:09:40 -0700126 // Set slave address
127 if (ioctl(file, I2C_SLAVE_FORCE, ii) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800128 {
James Feist26c27ad2018-07-25 15:09:40 -0700129 std::cerr << "device at bus " << bus << "register " << ii
130 << "busy\n";
131 continue;
132 }
133 // probe
134 else if (i2c_smbus_read_byte(file) < 0)
135 {
136 continue;
137 }
James Feist3cb5fec2018-01-23 14:41:51 -0800138
James Feist26c27ad2018-07-25 15:09:40 -0700139 if (DEBUG)
140 {
141 std::cout << "something at bus " << bus << "addr " << ii
142 << "\n";
143 }
Vijay Khemka2d681f62018-11-06 15:51:00 -0800144
145 /* Check for Device type if it is 8 bit or 16 bit */
146 int flag = isDevice16Bit(file);
147 if (flag < 0)
148 {
149 std::cerr << "failed to read bus " << bus << " address " << ii
150 << "\n";
151 continue;
152 }
153
154 if (read_block_data(flag, file, 0x0, 0x8, block_data.data()) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700155 {
156 std::cerr << "failed to read bus " << bus << " address " << ii
157 << "\n";
158 continue;
159 }
160 size_t sum = 0;
161 for (int jj = 0; jj < 7; jj++)
162 {
163 sum += block_data[jj];
164 }
165 sum = (256 - sum) & 0xFF;
166
167 // check the header checksum
168 if (sum == block_data[7])
169 {
170 std::vector<char> device;
171 device.insert(device.end(), block_data.begin(),
172 block_data.begin() + 8);
173
174 for (int jj = 1; jj <= FRU_AREAS.size(); jj++)
175 {
176 auto area_offset = device[jj];
177 if (area_offset != 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800178 {
James Feist26c27ad2018-07-25 15:09:40 -0700179 area_offset *= 8;
Vijay Khemka2d681f62018-11-06 15:51:00 -0800180 if (read_block_data(flag, file, area_offset, 0x8,
181 block_data.data()) < 0)
James Feist3cb5fec2018-01-23 14:41:51 -0800182 {
183 std::cerr << "failed to read bus " << bus
184 << " address " << ii << "\n";
185 return -1;
186 }
James Feist26c27ad2018-07-25 15:09:40 -0700187 int length = block_data[1] * 8;
James Feist3cb5fec2018-01-23 14:41:51 -0800188 device.insert(device.end(), block_data.begin(),
James Feist26c27ad2018-07-25 15:09:40 -0700189 block_data.begin() + 8);
190 length -= 8;
191 area_offset += 8;
192
193 while (length > 0)
194 {
195 auto to_get = std::min(0x20, length);
Vijay Khemka2d681f62018-11-06 15:51:00 -0800196 if (read_block_data(flag, file, area_offset, to_get,
197 block_data.data()) < 0)
James Feist26c27ad2018-07-25 15:09:40 -0700198 {
199 std::cerr << "failed to read bus " << bus
200 << " address " << ii << "\n";
201 return -1;
202 }
203 device.insert(device.end(), block_data.begin(),
204 block_data.begin() + to_get);
205 area_offset += to_get;
206 length -= to_get;
207 }
James Feist3cb5fec2018-01-23 14:41:51 -0800208 }
209 }
James Feist26c27ad2018-07-25 15:09:40 -0700210 devices->emplace(ii, device);
James Feist3cb5fec2018-01-23 14:41:51 -0800211 }
James Feist9945ddf2019-03-07 13:57:36 -0800212 else
213 {
214 std::cerr << "Illegal header checksum at bus " << bus
215 << " address " << ii << "\n";
216 }
James Feist3cb5fec2018-01-23 14:41:51 -0800217 }
James Feist26c27ad2018-07-25 15:09:40 -0700218 return 1;
219 });
220 std::future_status status =
221 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
222 if (status == std::future_status::timeout)
223 {
224 std::cerr << "Error reading bus " << bus << "\n";
225 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800226 }
227
James Feist26c27ad2018-07-25 15:09:40 -0700228 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800229}
230
James Feista465ccc2019-02-08 12:51:01 -0800231static void FindI2CDevices(const std::vector<fs::path>& i2cBuses,
James Feist6ebf9de2018-05-15 15:01:17 -0700232 std::shared_ptr<FindDevicesWithCallback> context,
James Feista465ccc2019-02-08 12:51:01 -0800233 boost::asio::io_service& io, BusMap& busMap)
James Feist3cb5fec2018-01-23 14:41:51 -0800234{
James Feista465ccc2019-02-08 12:51:01 -0800235 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800236 {
237 auto busnum = i2cBus.string();
238 auto lastDash = busnum.rfind(std::string("-"));
239 // delete everything before dash inclusive
240 if (lastDash != std::string::npos)
241 {
242 busnum.erase(0, lastDash + 1);
243 }
244 auto bus = std::stoi(busnum);
245
246 auto file = open(i2cBus.c_str(), O_RDWR);
247 if (file < 0)
248 {
249 std::cerr << "unable to open i2c device " << i2cBus.string()
250 << "\n";
251 continue;
252 }
253 unsigned long funcs = 0;
254
255 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
256 {
257 std::cerr
258 << "Error: Could not get the adapter functionality matrix bus"
259 << bus << "\n";
260 continue;
261 }
262 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
263 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
264 {
265 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
266 << bus << "\n";
267 continue;
268 }
James Feista465ccc2019-02-08 12:51:01 -0800269 auto& device = busMap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800270 device = std::make_shared<DeviceMap>();
271
James Feistc95cb142018-02-26 10:41:42 -0800272 // don't scan muxed buses async as don't want to confuse the mux
273 if (isMuxBus(bus))
274 {
275 get_bus_frus(file, 0x03, 0x77, bus, device);
276 close(file);
277 }
278 else
279 {
James Feist6ebf9de2018-05-15 15:01:17 -0700280 io.post([file, device, bus, context] {
281 // i2cdetect by default uses the range 0x03 to 0x77, as
282 // this is
283 // what we
284 // have tested with, use this range. Could be changed in
285 // future.
286 get_bus_frus(file, 0x03, 0x77, bus, device);
287 close(file);
288 });
James Feistc95cb142018-02-26 10:41:42 -0800289 }
James Feist3cb5fec2018-01-23 14:41:51 -0800290 }
James Feist3cb5fec2018-01-23 14:41:51 -0800291}
292
James Feist6ebf9de2018-05-15 15:01:17 -0700293// this class allows an async response after all i2c devices are discovered
294struct FindDevicesWithCallback
295 : std::enable_shared_from_this<FindDevicesWithCallback>
296{
James Feista465ccc2019-02-08 12:51:01 -0800297 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
298 boost::asio::io_service& io, BusMap& busMap,
299 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700300 _i2cBuses(i2cBuses),
301 _io(io), _busMap(busMap), _callback(std::move(callback))
302 {
303 }
304 ~FindDevicesWithCallback()
305 {
306 _callback();
307 }
308 void run()
309 {
310 FindI2CDevices(_i2cBuses, shared_from_this(), _io, _busMap);
311 }
312
James Feista465ccc2019-02-08 12:51:01 -0800313 const std::vector<fs::path>& _i2cBuses;
314 boost::asio::io_service& _io;
315 BusMap& _busMap;
James Feist6ebf9de2018-05-15 15:01:17 -0700316 std::function<void(void)> _callback;
317};
318
James Feist3cb5fec2018-01-23 14:41:51 -0800319static const std::tm intelEpoch(void)
320{
321 std::tm val = {0};
322 val.tm_year = 1996 - 1900;
323 return val;
324}
325
James Feista465ccc2019-02-08 12:51:01 -0800326bool formatFru(const std::vector<char>& fruBytes,
327 boost::container::flat_map<std::string, std::string>& result)
James Feist3cb5fec2018-01-23 14:41:51 -0800328{
James Feista465ccc2019-02-08 12:51:01 -0800329 static const std::vector<const char*> CHASSIS_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800330 "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800331
James Feista465ccc2019-02-08 12:51:01 -0800332 static const std::vector<const char*> BOARD_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800333 "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
334 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800335
James Feista465ccc2019-02-08 12:51:01 -0800336 static const std::vector<const char*> PRODUCT_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800337 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER",
338 "VERSION", "SERIAL_NUMBER", "ASSET_TAG",
339 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800340
341 size_t sum = 0;
342
James Feistd068e932018-09-20 10:53:07 -0700343 if (fruBytes.size() <= 8)
James Feist3cb5fec2018-01-23 14:41:51 -0800344 {
345 return false;
346 }
347 std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin();
James Feist9eb0b582018-04-27 12:15:46 -0700348 result["Common_Format_Version"] =
James Feist3cb5fec2018-01-23 14:41:51 -0800349 std::to_string(static_cast<int>(*fruAreaOffsetField));
350
James Feista465ccc2019-02-08 12:51:01 -0800351 const std::vector<const char*>* fieldData;
James Feist3cb5fec2018-01-23 14:41:51 -0800352
James Feista465ccc2019-02-08 12:51:01 -0800353 for (auto& area : FRU_AREAS)
James Feist3cb5fec2018-01-23 14:41:51 -0800354 {
355 fruAreaOffsetField++;
356 if (fruAreaOffsetField >= fruBytes.end())
357 {
358 return false;
359 }
360 size_t offset = (*fruAreaOffsetField) * 8;
361
362 if (offset > 1)
363 {
364 // +2 to skip format and length
365 std::vector<char>::const_iterator fruBytesIter =
366 fruBytes.begin() + offset + 2;
367
368 if (fruBytesIter >= fruBytes.end())
369 {
370 return false;
371 }
372
373 if (area == "CHASSIS")
374 {
375 result["CHASSIS_TYPE"] =
376 std::to_string(static_cast<int>(*fruBytesIter));
377 fruBytesIter += 1;
378 fieldData = &CHASSIS_FRU_AREAS;
379 }
380 else if (area == "BOARD")
381 {
382 result["BOARD_LANGUAGE_CODE"] =
383 std::to_string(static_cast<int>(*fruBytesIter));
384 fruBytesIter += 1;
385 if (fruBytesIter >= fruBytes.end())
386 {
387 return false;
388 }
389
390 unsigned int minutes = *fruBytesIter |
391 *(fruBytesIter + 1) << 8 |
392 *(fruBytesIter + 2) << 16;
393 std::tm fruTime = intelEpoch();
394 time_t timeValue = mktime(&fruTime);
395 timeValue += minutes * 60;
396 fruTime = *gmtime(&timeValue);
397
398 result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime);
399 result["BOARD_MANUFACTURE_DATE"]
400 .pop_back(); // remove trailing null
401 fruBytesIter += 3;
402 fieldData = &BOARD_FRU_AREAS;
403 }
404 else if (area == "PRODUCT")
405 {
406 result["PRODUCT_LANGUAGE_CODE"] =
407 std::to_string(static_cast<int>(*fruBytesIter));
408 fruBytesIter += 1;
409 fieldData = &PRODUCT_FRU_AREAS;
410 }
411 else
412 {
413 continue;
414 }
James Feista465ccc2019-02-08 12:51:01 -0800415 for (auto& field : *fieldData)
James Feist3cb5fec2018-01-23 14:41:51 -0800416 {
417 if (fruBytesIter >= fruBytes.end())
418 {
419 return false;
420 }
421
Vijay Khemka5d5de442018-11-07 10:51:25 -0800422 /* Checking for last byte C1 to indicate that no more
423 * field to be read */
Ed Tanous2147e672019-02-27 13:59:56 -0800424 if (*fruBytesIter == 0xC1)
Vijay Khemka5d5de442018-11-07 10:51:25 -0800425 {
426 break;
427 }
428
Ed Tanous2147e672019-02-27 13:59:56 -0800429 size_t length = *fruBytesIter & 0x3f;
430 fruBytesIter += 1;
431
James Feist3cb5fec2018-01-23 14:41:51 -0800432 if (fruBytesIter >= fruBytes.end())
433 {
434 return false;
435 }
Ed Tanous2147e672019-02-27 13:59:56 -0800436 std::string value(fruBytesIter, fruBytesIter + length);
James Feist3cb5fec2018-01-23 14:41:51 -0800437
Ed Tanous2147e672019-02-27 13:59:56 -0800438 // Strip non null characters from the end
439 value.erase(std::find_if(value.rbegin(), value.rend(),
440 [](char ch) { return ch != 0; })
441 .base(),
442 value.end());
443
444 result[std::string(area) + "_" + field] = std::move(value);
445
James Feist3cb5fec2018-01-23 14:41:51 -0800446 fruBytesIter += length;
447 if (fruBytesIter >= fruBytes.end())
448 {
449 std::cerr << "Warning Fru Length Mismatch:\n ";
James Feista465ccc2019-02-08 12:51:01 -0800450 for (auto& c : fruBytes)
James Feist3cb5fec2018-01-23 14:41:51 -0800451 {
452 std::cerr << c;
453 }
454 std::cerr << "\n";
455 if (DEBUG)
456 {
James Feista465ccc2019-02-08 12:51:01 -0800457 for (auto& keyPair : result)
James Feist3cb5fec2018-01-23 14:41:51 -0800458 {
459 std::cerr << keyPair.first << " : "
460 << keyPair.second << "\n";
461 }
462 }
463 return false;
464 }
465 }
466 }
467 }
468
469 return true;
470}
471
472void AddFruObjectToDbus(
James Feist9eb0b582018-04-27 12:15:46 -0700473 std::shared_ptr<sdbusplus::asio::connection> dbusConn,
James Feista465ccc2019-02-08 12:51:01 -0800474 std::vector<char>& device, sdbusplus::asio::object_server& objServer,
475 boost::container::flat_map<
476 std::pair<size_t, size_t>,
477 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -0700478 uint32_t bus, uint32_t address)
James Feist3cb5fec2018-01-23 14:41:51 -0800479{
480 boost::container::flat_map<std::string, std::string> formattedFru;
481 if (!formatFru(device, formattedFru))
482 {
483 std::cerr << "failed to format fru for device at bus " << std::hex
484 << bus << "address " << address << "\n";
485 return;
486 }
487 auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME");
488 std::string productName;
489 if (productNameFind == formattedFru.end())
490 {
491 productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME");
492 }
493 if (productNameFind != formattedFru.end())
494 {
495 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800496 std::regex illegalObject("[^A-Za-z0-9_]");
497 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800498 }
499 else
500 {
501 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
502 UNKNOWN_BUS_OBJECT_COUNT++;
503 }
504
James Feist918e18c2018-02-13 15:51:07 -0800505 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800506 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700507 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800508 {
James Feist79e9c0b2018-03-15 15:21:17 -0700509 size_t index = 0;
James Feista465ccc2019-02-08 12:51:01 -0800510 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800511 {
James Feist9eb0b582018-04-27 12:15:46 -0700512 if ((busIface.second->get_object_path() == productName))
James Feist918e18c2018-02-13 15:51:07 -0800513 {
James Feist9eb0b582018-04-27 12:15:46 -0700514 if (isMuxBus(bus) && address == busIface.first.second)
James Feist79e9c0b2018-03-15 15:21:17 -0700515 {
516 continue;
517 }
518 // add underscore _index for the same object path on dbus
519 std::string strIndex = std::to_string(index);
520 if (index > 0)
521 {
522 productName.substr(0, productName.size() - strIndex.size());
523 }
524 else
525 {
526 productName += "_";
527 }
528 productName += std::to_string(index++);
James Feist918e18c2018-02-13 15:51:07 -0800529 }
530 }
531 }
James Feist3cb5fec2018-01-23 14:41:51 -0800532
James Feist9eb0b582018-04-27 12:15:46 -0700533 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
534 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
535 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
536
James Feista465ccc2019-02-08 12:51:01 -0800537 for (auto& property : formattedFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800538 {
James Feist9eb0b582018-04-27 12:15:46 -0700539
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700540 std::regex_replace(property.second.begin(), property.second.begin(),
541 property.second.end(), NON_ASCII_REGEX, "_");
James Feist9eb0b582018-04-27 12:15:46 -0700542 if (property.second.empty())
543 {
544 continue;
545 }
546 std::string key =
547 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
548 if (!iface->register_property(key, property.second + '\0'))
549 {
550 std::cerr << "illegal key: " << key << "\n";
551 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700552 if (DEBUG)
553 {
554 std::cout << property.first << ": " << property.second << "\n";
555 }
James Feist3cb5fec2018-01-23 14:41:51 -0800556 }
James Feist2a9d6db2018-04-27 15:48:28 -0700557
558 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700559 iface->register_property("BUS", bus);
560 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700561
James Feist9eb0b582018-04-27 12:15:46 -0700562 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800563}
564
James Feista465ccc2019-02-08 12:51:01 -0800565static bool readBaseboardFru(std::vector<char>& baseboardFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800566{
567 // try to read baseboard fru from file
568 std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
569 if (baseboardFruFile.good())
570 {
571 baseboardFruFile.seekg(0, std::ios_base::end);
572 std::streampos fileSize = baseboardFruFile.tellg();
573 baseboardFru.resize(fileSize);
574 baseboardFruFile.seekg(0, std::ios_base::beg);
575 baseboardFruFile.read(baseboardFru.data(), fileSize);
576 }
577 else
578 {
579 return false;
580 }
581 return true;
582}
583
James Feista465ccc2019-02-08 12:51:01 -0800584bool writeFru(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700585{
586 boost::container::flat_map<std::string, std::string> tmp;
587 if (fru.size() > MAX_FRU_SIZE)
588 {
589 std::cerr << "Invalid fru.size() during writeFru\n";
590 return false;
591 }
592 // verify legal fru by running it through fru parsing logic
James Feista465ccc2019-02-08 12:51:01 -0800593 if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp))
James Feistb49ffc32018-05-02 11:10:43 -0700594 {
595 std::cerr << "Invalid fru format during writeFru\n";
596 return false;
597 }
598 // baseboard fru
599 if (bus == 0 && address == 0)
600 {
601 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
602 if (!file.good())
603 {
604 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
605 << "\n";
James Feistddb78302018-09-06 11:45:42 -0700606 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700607 return false;
608 }
James Feista465ccc2019-02-08 12:51:01 -0800609 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700610 return file.good();
611 }
612 else
613 {
614 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
615
616 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
617 if (file < 0)
618 {
619 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -0700620 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700621 return false;
622 }
623 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
624 {
625 std::cerr << "unable to set device address\n";
626 close(file);
James Feistddb78302018-09-06 11:45:42 -0700627 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700628 return false;
629 }
630
631 constexpr const size_t RETRY_MAX = 2;
632 uint16_t index = 0;
633 size_t retries = RETRY_MAX;
634 while (index < fru.size())
635 {
636 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
637 (retries == RETRY_MAX))
638 {
639 // The 4K EEPROM only uses the A2 and A1 device address bits
640 // with the third bit being a memory page address bit.
641 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
642 {
643 std::cerr << "unable to set device address\n";
644 close(file);
James Feistddb78302018-09-06 11:45:42 -0700645 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700646 return false;
647 }
648 }
649
650 if (i2c_smbus_write_byte_data(file, index & 0xFF, fru[index]) < 0)
651 {
652 if (!retries--)
653 {
654 std::cerr << "error writing fru: " << strerror(errno)
655 << "\n";
656 close(file);
James Feistddb78302018-09-06 11:45:42 -0700657 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700658 return false;
659 }
660 }
661 else
662 {
663 retries = RETRY_MAX;
664 index++;
665 }
666 // most eeproms require 5-10ms between writes
667 std::this_thread::sleep_for(std::chrono::milliseconds(10));
668 }
669 close(file);
670 return true;
671 }
672}
673
James Feist9eb0b582018-04-27 12:15:46 -0700674void rescanBusses(
James Feista465ccc2019-02-08 12:51:01 -0800675 boost::asio::io_service& io, BusMap& busMap,
676 boost::container::flat_map<
677 std::pair<size_t, size_t>,
678 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
679 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
680 sdbusplus::asio::object_server& objServer)
James Feist918e18c2018-02-13 15:51:07 -0800681{
James Feist6ebf9de2018-05-15 15:01:17 -0700682 static boost::asio::deadline_timer timer(io);
683 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800684
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500685 // setup an async wait in case we get flooded with requests
James Feista465ccc2019-02-08 12:51:01 -0800686 timer.async_wait([&](const boost::system::error_code& ec) {
James Feist4131aea2018-03-09 09:47:30 -0800687 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -0800688 auto matchString = std::string(R"(i2c-\d+$)");
James Feist4131aea2018-03-09 09:47:30 -0800689 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800690
James Feista3c180a2018-08-09 16:06:04 -0700691 if (!findFiles(devDir, matchString, i2cBuses))
James Feist918e18c2018-02-13 15:51:07 -0800692 {
James Feist4131aea2018-03-09 09:47:30 -0800693 std::cerr << "unable to find i2c devices\n";
694 return;
James Feist918e18c2018-02-13 15:51:07 -0800695 }
James Feist4131aea2018-03-09 09:47:30 -0800696 // scanning muxes in reverse order seems to have adverse effects
697 // sorting this list seems to be a fix for that
698 std::sort(i2cBuses.begin(), i2cBuses.end());
James Feist4131aea2018-03-09 09:47:30 -0800699
James Feist6ebf9de2018-05-15 15:01:17 -0700700 busMap.clear();
701 auto scan = std::make_shared<FindDevicesWithCallback>(
702 i2cBuses, io, busMap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -0800703 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700704 {
705 objServer.remove_interface(busIface.second);
706 }
James Feist4131aea2018-03-09 09:47:30 -0800707
James Feist6ebf9de2018-05-15 15:01:17 -0700708 dbusInterfaceMap.clear();
709 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -0800710
James Feist6ebf9de2018-05-15 15:01:17 -0700711 // todo, get this from a more sensable place
712 std::vector<char> baseboardFru;
713 if (readBaseboardFru(baseboardFru))
714 {
715 boost::container::flat_map<int, std::vector<char>>
716 baseboardDev;
717 baseboardDev.emplace(0, baseboardFru);
718 busMap[0] = std::make_shared<DeviceMap>(baseboardDev);
719 }
James Feista465ccc2019-02-08 12:51:01 -0800720 for (auto& devicemap : busMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700721 {
James Feista465ccc2019-02-08 12:51:01 -0800722 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -0700723 {
724 AddFruObjectToDbus(systemBus, device.second, objServer,
725 dbusInterfaceMap, devicemap.first,
726 device.first);
727 }
728 }
729 });
730 scan->run();
731 });
James Feist918e18c2018-02-13 15:51:07 -0800732}
733
James Feista465ccc2019-02-08 12:51:01 -0800734int main(int argc, char** argv)
James Feist3cb5fec2018-01-23 14:41:51 -0800735{
736 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -0800737 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -0800738 std::vector<fs::path> i2cBuses;
739
James Feista3c180a2018-08-09 16:06:04 -0700740 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -0800741 {
742 std::cerr << "unable to find i2c devices\n";
743 return 1;
744 }
James Feist3cb5fec2018-01-23 14:41:51 -0800745
746 boost::asio::io_service io;
James Feist9eb0b582018-04-27 12:15:46 -0700747 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
748 auto objServer = sdbusplus::asio::object_server(systemBus);
Vijay Khemka065f6d92018-12-18 10:37:47 -0800749 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -0800750
James Feist6ebf9de2018-05-15 15:01:17 -0700751 // this is a map with keys of pair(bus number, address) and values of
752 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -0800753 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -0700754 std::shared_ptr<sdbusplus::asio::dbus_interface>>
755 dbusInterfaceMap;
James Feist2a9d6db2018-04-27 15:48:28 -0700756 BusMap busmap;
James Feist3cb5fec2018-01-23 14:41:51 -0800757
James Feist9eb0b582018-04-27 12:15:46 -0700758 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
759 objServer.add_interface("/xyz/openbmc_project/FruDevice",
760 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -0800761
762 iface->register_method("ReScan", [&]() {
James Feist6ebf9de2018-05-15 15:01:17 -0700763 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feist3cb5fec2018-01-23 14:41:51 -0800764 });
James Feist2a9d6db2018-04-27 15:48:28 -0700765
766 iface->register_method(
James Feista465ccc2019-02-08 12:51:01 -0800767 "GetRawFru", [&](const uint8_t& bus, const uint8_t& address) {
James Feist2a9d6db2018-04-27 15:48:28 -0700768 auto deviceMap = busmap.find(bus);
769 if (deviceMap == busmap.end())
770 {
James Feistddb78302018-09-06 11:45:42 -0700771 throw std::invalid_argument("Invalid Bus.");
James Feist2a9d6db2018-04-27 15:48:28 -0700772 }
773 auto device = deviceMap->second->find(address);
774 if (device == deviceMap->second->end())
775 {
James Feistddb78302018-09-06 11:45:42 -0700776 throw std::invalid_argument("Invalid Address.");
James Feist2a9d6db2018-04-27 15:48:28 -0700777 }
James Feista465ccc2019-02-08 12:51:01 -0800778 std::vector<uint8_t>& ret =
779 reinterpret_cast<std::vector<uint8_t>&>(device->second);
James Feist2a9d6db2018-04-27 15:48:28 -0700780 return ret;
781 });
James Feistb49ffc32018-05-02 11:10:43 -0700782
783 iface->register_method("WriteFru", [&](const uint8_t bus,
784 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -0800785 const std::vector<uint8_t>& data) {
James Feistb49ffc32018-05-02 11:10:43 -0700786 if (!writeFru(bus, address, data))
787 {
James Feistddb78302018-09-06 11:45:42 -0700788 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -0700789 return;
790 }
791 // schedule rescan on success
792 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feistb49ffc32018-05-02 11:10:43 -0700793 });
James Feist9eb0b582018-04-27 12:15:46 -0700794 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800795
James Feist9eb0b582018-04-27 12:15:46 -0700796 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -0800797 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -0800798 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -0700799 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -0800800 std::string,
801 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -0700802 values;
803 message.read(objectName, values);
James Feist918e18c2018-02-13 15:51:07 -0800804 auto findPgood = values.find("pgood");
805 if (findPgood != values.end())
806 {
James Feist6ebf9de2018-05-15 15:01:17 -0700807
808 rescanBusses(io, busmap, dbusInterfaceMap, systemBus,
809 objServer);
James Feist918e18c2018-02-13 15:51:07 -0800810 }
James Feist918e18c2018-02-13 15:51:07 -0800811 };
James Feist9eb0b582018-04-27 12:15:46 -0700812
813 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -0800814 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist9eb0b582018-04-27 12:15:46 -0700815 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
Kuiying Wang1dc43102018-05-09 15:09:29 +0800816 "namespace='/xyz/openbmc_project/Chassis/Control/"
817 "power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist9eb0b582018-04-27 12:15:46 -0700818 eventHandler);
819
James Feist4131aea2018-03-09 09:47:30 -0800820 int fd = inotify_init();
821 int wd = inotify_add_watch(fd, I2C_DEV_LOCATION,
822 IN_CREATE | IN_MOVED_TO | IN_DELETE);
823 std::array<char, 4096> readBuffer;
824 std::string pendingBuffer;
825 // monitor for new i2c devices
826 boost::asio::posix::stream_descriptor dirWatch(io, fd);
827 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -0800828 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -0800829 std::size_t bytes_transferred) {
830 if (ec)
831 {
832 std::cout << "Callback Error " << ec << "\n";
833 return;
834 }
835 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
836 bool devChange = false;
837 while (pendingBuffer.size() > sizeof(inotify_event))
838 {
James Feista465ccc2019-02-08 12:51:01 -0800839 const inotify_event* iEvent =
840 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -0800841 pendingBuffer.data());
842 switch (iEvent->mask)
843 {
James Feist9eb0b582018-04-27 12:15:46 -0700844 case IN_CREATE:
845 case IN_MOVED_TO:
846 case IN_DELETE:
847 if (boost::starts_with(std::string(iEvent->name),
848 "i2c"))
849 {
850 devChange = true;
851 }
James Feist4131aea2018-03-09 09:47:30 -0800852 }
853
854 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
855 }
James Feist6ebf9de2018-05-15 15:01:17 -0700856 if (devChange)
James Feist4131aea2018-03-09 09:47:30 -0800857 {
James Feist6ebf9de2018-05-15 15:01:17 -0700858 rescanBusses(io, busmap, dbusInterfaceMap, systemBus,
859 objServer);
James Feist4131aea2018-03-09 09:47:30 -0800860 }
James Feist6ebf9de2018-05-15 15:01:17 -0700861
James Feist4131aea2018-03-09 09:47:30 -0800862 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
863 watchI2cBusses);
864 };
865
866 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -0500867 // run the initial scan
James Feist6ebf9de2018-05-15 15:01:17 -0700868 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800869
James Feist3cb5fec2018-01-23 14:41:51 -0800870 io.run();
871 return 0;
872}