blob: a5e5c802a74139660f573981bf9937ee287dd50e [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 Feist3cb5fec2018-01-23 14:41:51 -0800212 }
James Feist26c27ad2018-07-25 15:09:40 -0700213 return 1;
214 });
215 std::future_status status =
216 future.wait_for(std::chrono::seconds(busTimeoutSeconds));
217 if (status == std::future_status::timeout)
218 {
219 std::cerr << "Error reading bus " << bus << "\n";
220 return -1;
James Feist3cb5fec2018-01-23 14:41:51 -0800221 }
222
James Feist26c27ad2018-07-25 15:09:40 -0700223 return future.get();
James Feist3cb5fec2018-01-23 14:41:51 -0800224}
225
James Feista465ccc2019-02-08 12:51:01 -0800226static void FindI2CDevices(const std::vector<fs::path>& i2cBuses,
James Feist6ebf9de2018-05-15 15:01:17 -0700227 std::shared_ptr<FindDevicesWithCallback> context,
James Feista465ccc2019-02-08 12:51:01 -0800228 boost::asio::io_service& io, BusMap& busMap)
James Feist3cb5fec2018-01-23 14:41:51 -0800229{
James Feista465ccc2019-02-08 12:51:01 -0800230 for (auto& i2cBus : i2cBuses)
James Feist3cb5fec2018-01-23 14:41:51 -0800231 {
232 auto busnum = i2cBus.string();
233 auto lastDash = busnum.rfind(std::string("-"));
234 // delete everything before dash inclusive
235 if (lastDash != std::string::npos)
236 {
237 busnum.erase(0, lastDash + 1);
238 }
239 auto bus = std::stoi(busnum);
240
241 auto file = open(i2cBus.c_str(), O_RDWR);
242 if (file < 0)
243 {
244 std::cerr << "unable to open i2c device " << i2cBus.string()
245 << "\n";
246 continue;
247 }
248 unsigned long funcs = 0;
249
250 if (ioctl(file, I2C_FUNCS, &funcs) < 0)
251 {
252 std::cerr
253 << "Error: Could not get the adapter functionality matrix bus"
254 << bus << "\n";
255 continue;
256 }
257 if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
258 !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
259 {
260 std::cerr << "Error: Can't use SMBus Receive Byte command bus "
261 << bus << "\n";
262 continue;
263 }
James Feista465ccc2019-02-08 12:51:01 -0800264 auto& device = busMap[bus];
James Feist3cb5fec2018-01-23 14:41:51 -0800265 device = std::make_shared<DeviceMap>();
266
James Feistc95cb142018-02-26 10:41:42 -0800267 // don't scan muxed buses async as don't want to confuse the mux
268 if (isMuxBus(bus))
269 {
270 get_bus_frus(file, 0x03, 0x77, bus, device);
271 close(file);
272 }
273 else
274 {
James Feist6ebf9de2018-05-15 15:01:17 -0700275 io.post([file, device, bus, context] {
276 // i2cdetect by default uses the range 0x03 to 0x77, as
277 // this is
278 // what we
279 // have tested with, use this range. Could be changed in
280 // future.
281 get_bus_frus(file, 0x03, 0x77, bus, device);
282 close(file);
283 });
James Feistc95cb142018-02-26 10:41:42 -0800284 }
James Feist3cb5fec2018-01-23 14:41:51 -0800285 }
James Feist3cb5fec2018-01-23 14:41:51 -0800286}
287
James Feist6ebf9de2018-05-15 15:01:17 -0700288// this class allows an async response after all i2c devices are discovered
289struct FindDevicesWithCallback
290 : std::enable_shared_from_this<FindDevicesWithCallback>
291{
James Feista465ccc2019-02-08 12:51:01 -0800292 FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses,
293 boost::asio::io_service& io, BusMap& busMap,
294 std::function<void(void)>&& callback) :
James Feist6ebf9de2018-05-15 15:01:17 -0700295 _i2cBuses(i2cBuses),
296 _io(io), _busMap(busMap), _callback(std::move(callback))
297 {
298 }
299 ~FindDevicesWithCallback()
300 {
301 _callback();
302 }
303 void run()
304 {
305 FindI2CDevices(_i2cBuses, shared_from_this(), _io, _busMap);
306 }
307
James Feista465ccc2019-02-08 12:51:01 -0800308 const std::vector<fs::path>& _i2cBuses;
309 boost::asio::io_service& _io;
310 BusMap& _busMap;
James Feist6ebf9de2018-05-15 15:01:17 -0700311 std::function<void(void)> _callback;
312};
313
James Feist3cb5fec2018-01-23 14:41:51 -0800314static const std::tm intelEpoch(void)
315{
316 std::tm val = {0};
317 val.tm_year = 1996 - 1900;
318 return val;
319}
320
James Feista465ccc2019-02-08 12:51:01 -0800321bool formatFru(const std::vector<char>& fruBytes,
322 boost::container::flat_map<std::string, std::string>& result)
James Feist3cb5fec2018-01-23 14:41:51 -0800323{
James Feista465ccc2019-02-08 12:51:01 -0800324 static const std::vector<const char*> CHASSIS_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800325 "PART_NUMBER", "SERIAL_NUMBER", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800326
James Feista465ccc2019-02-08 12:51:01 -0800327 static const std::vector<const char*> BOARD_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800328 "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER",
329 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800330
James Feista465ccc2019-02-08 12:51:01 -0800331 static const std::vector<const char*> PRODUCT_FRU_AREAS = {
Vijay Khemka5d5de442018-11-07 10:51:25 -0800332 "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER",
333 "VERSION", "SERIAL_NUMBER", "ASSET_TAG",
334 "FRU_VERSION_ID", "INFO_AM1", "INFO_AM2"};
James Feist3cb5fec2018-01-23 14:41:51 -0800335
336 size_t sum = 0;
337
James Feistd068e932018-09-20 10:53:07 -0700338 if (fruBytes.size() <= 8)
James Feist3cb5fec2018-01-23 14:41:51 -0800339 {
340 return false;
341 }
342 std::vector<char>::const_iterator fruAreaOffsetField = fruBytes.begin();
James Feist9eb0b582018-04-27 12:15:46 -0700343 result["Common_Format_Version"] =
James Feist3cb5fec2018-01-23 14:41:51 -0800344 std::to_string(static_cast<int>(*fruAreaOffsetField));
345
James Feista465ccc2019-02-08 12:51:01 -0800346 const std::vector<const char*>* fieldData;
James Feist3cb5fec2018-01-23 14:41:51 -0800347
James Feista465ccc2019-02-08 12:51:01 -0800348 for (auto& area : FRU_AREAS)
James Feist3cb5fec2018-01-23 14:41:51 -0800349 {
350 fruAreaOffsetField++;
351 if (fruAreaOffsetField >= fruBytes.end())
352 {
353 return false;
354 }
355 size_t offset = (*fruAreaOffsetField) * 8;
356
357 if (offset > 1)
358 {
359 // +2 to skip format and length
360 std::vector<char>::const_iterator fruBytesIter =
361 fruBytes.begin() + offset + 2;
362
363 if (fruBytesIter >= fruBytes.end())
364 {
365 return false;
366 }
367
368 if (area == "CHASSIS")
369 {
370 result["CHASSIS_TYPE"] =
371 std::to_string(static_cast<int>(*fruBytesIter));
372 fruBytesIter += 1;
373 fieldData = &CHASSIS_FRU_AREAS;
374 }
375 else if (area == "BOARD")
376 {
377 result["BOARD_LANGUAGE_CODE"] =
378 std::to_string(static_cast<int>(*fruBytesIter));
379 fruBytesIter += 1;
380 if (fruBytesIter >= fruBytes.end())
381 {
382 return false;
383 }
384
385 unsigned int minutes = *fruBytesIter |
386 *(fruBytesIter + 1) << 8 |
387 *(fruBytesIter + 2) << 16;
388 std::tm fruTime = intelEpoch();
389 time_t timeValue = mktime(&fruTime);
390 timeValue += minutes * 60;
391 fruTime = *gmtime(&timeValue);
392
393 result["BOARD_MANUFACTURE_DATE"] = asctime(&fruTime);
394 result["BOARD_MANUFACTURE_DATE"]
395 .pop_back(); // remove trailing null
396 fruBytesIter += 3;
397 fieldData = &BOARD_FRU_AREAS;
398 }
399 else if (area == "PRODUCT")
400 {
401 result["PRODUCT_LANGUAGE_CODE"] =
402 std::to_string(static_cast<int>(*fruBytesIter));
403 fruBytesIter += 1;
404 fieldData = &PRODUCT_FRU_AREAS;
405 }
406 else
407 {
408 continue;
409 }
James Feista465ccc2019-02-08 12:51:01 -0800410 for (auto& field : *fieldData)
James Feist3cb5fec2018-01-23 14:41:51 -0800411 {
412 if (fruBytesIter >= fruBytes.end())
413 {
414 return false;
415 }
416
Vijay Khemka5d5de442018-11-07 10:51:25 -0800417 /* Checking for last byte C1 to indicate that no more
418 * field to be read */
Ed Tanous2147e672019-02-27 13:59:56 -0800419 if (*fruBytesIter == 0xC1)
Vijay Khemka5d5de442018-11-07 10:51:25 -0800420 {
421 break;
422 }
423
Ed Tanous2147e672019-02-27 13:59:56 -0800424 size_t length = *fruBytesIter & 0x3f;
425 fruBytesIter += 1;
426
James Feist3cb5fec2018-01-23 14:41:51 -0800427 if (fruBytesIter >= fruBytes.end())
428 {
429 return false;
430 }
Ed Tanous2147e672019-02-27 13:59:56 -0800431 std::string value(fruBytesIter, fruBytesIter + length);
James Feist3cb5fec2018-01-23 14:41:51 -0800432
Ed Tanous2147e672019-02-27 13:59:56 -0800433 // Strip non null characters from the end
434 value.erase(std::find_if(value.rbegin(), value.rend(),
435 [](char ch) { return ch != 0; })
436 .base(),
437 value.end());
438
439 result[std::string(area) + "_" + field] = std::move(value);
440
James Feist3cb5fec2018-01-23 14:41:51 -0800441 fruBytesIter += length;
442 if (fruBytesIter >= fruBytes.end())
443 {
444 std::cerr << "Warning Fru Length Mismatch:\n ";
James Feista465ccc2019-02-08 12:51:01 -0800445 for (auto& c : fruBytes)
James Feist3cb5fec2018-01-23 14:41:51 -0800446 {
447 std::cerr << c;
448 }
449 std::cerr << "\n";
450 if (DEBUG)
451 {
James Feista465ccc2019-02-08 12:51:01 -0800452 for (auto& keyPair : result)
James Feist3cb5fec2018-01-23 14:41:51 -0800453 {
454 std::cerr << keyPair.first << " : "
455 << keyPair.second << "\n";
456 }
457 }
458 return false;
459 }
460 }
461 }
462 }
463
464 return true;
465}
466
467void AddFruObjectToDbus(
James Feist9eb0b582018-04-27 12:15:46 -0700468 std::shared_ptr<sdbusplus::asio::connection> dbusConn,
James Feista465ccc2019-02-08 12:51:01 -0800469 std::vector<char>& device, sdbusplus::asio::object_server& objServer,
470 boost::container::flat_map<
471 std::pair<size_t, size_t>,
472 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -0700473 uint32_t bus, uint32_t address)
James Feist3cb5fec2018-01-23 14:41:51 -0800474{
475 boost::container::flat_map<std::string, std::string> formattedFru;
476 if (!formatFru(device, formattedFru))
477 {
478 std::cerr << "failed to format fru for device at bus " << std::hex
479 << bus << "address " << address << "\n";
480 return;
481 }
482 auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME");
483 std::string productName;
484 if (productNameFind == formattedFru.end())
485 {
486 productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME");
487 }
488 if (productNameFind != formattedFru.end())
489 {
490 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800491 std::regex illegalObject("[^A-Za-z0-9_]");
492 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800493 }
494 else
495 {
496 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
497 UNKNOWN_BUS_OBJECT_COUNT++;
498 }
499
James Feist918e18c2018-02-13 15:51:07 -0800500 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800501 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700502 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800503 {
James Feist79e9c0b2018-03-15 15:21:17 -0700504 size_t index = 0;
James Feista465ccc2019-02-08 12:51:01 -0800505 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800506 {
James Feist9eb0b582018-04-27 12:15:46 -0700507 if ((busIface.second->get_object_path() == productName))
James Feist918e18c2018-02-13 15:51:07 -0800508 {
James Feist9eb0b582018-04-27 12:15:46 -0700509 if (isMuxBus(bus) && address == busIface.first.second)
James Feist79e9c0b2018-03-15 15:21:17 -0700510 {
511 continue;
512 }
513 // add underscore _index for the same object path on dbus
514 std::string strIndex = std::to_string(index);
515 if (index > 0)
516 {
517 productName.substr(0, productName.size() - strIndex.size());
518 }
519 else
520 {
521 productName += "_";
522 }
523 productName += std::to_string(index++);
James Feist918e18c2018-02-13 15:51:07 -0800524 }
525 }
526 }
James Feist3cb5fec2018-01-23 14:41:51 -0800527
James Feist9eb0b582018-04-27 12:15:46 -0700528 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
529 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
530 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
531
James Feista465ccc2019-02-08 12:51:01 -0800532 for (auto& property : formattedFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800533 {
James Feist9eb0b582018-04-27 12:15:46 -0700534
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700535 std::regex_replace(property.second.begin(), property.second.begin(),
536 property.second.end(), NON_ASCII_REGEX, "_");
James Feist9eb0b582018-04-27 12:15:46 -0700537 if (property.second.empty())
538 {
539 continue;
540 }
541 std::string key =
542 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
543 if (!iface->register_property(key, property.second + '\0'))
544 {
545 std::cerr << "illegal key: " << key << "\n";
546 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700547 if (DEBUG)
548 {
549 std::cout << property.first << ": " << property.second << "\n";
550 }
James Feist3cb5fec2018-01-23 14:41:51 -0800551 }
James Feist2a9d6db2018-04-27 15:48:28 -0700552
553 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700554 iface->register_property("BUS", bus);
555 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700556
James Feist9eb0b582018-04-27 12:15:46 -0700557 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800558}
559
James Feista465ccc2019-02-08 12:51:01 -0800560static bool readBaseboardFru(std::vector<char>& baseboardFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800561{
562 // try to read baseboard fru from file
563 std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
564 if (baseboardFruFile.good())
565 {
566 baseboardFruFile.seekg(0, std::ios_base::end);
567 std::streampos fileSize = baseboardFruFile.tellg();
568 baseboardFru.resize(fileSize);
569 baseboardFruFile.seekg(0, std::ios_base::beg);
570 baseboardFruFile.read(baseboardFru.data(), fileSize);
571 }
572 else
573 {
574 return false;
575 }
576 return true;
577}
578
James Feista465ccc2019-02-08 12:51:01 -0800579bool writeFru(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700580{
581 boost::container::flat_map<std::string, std::string> tmp;
582 if (fru.size() > MAX_FRU_SIZE)
583 {
584 std::cerr << "Invalid fru.size() during writeFru\n";
585 return false;
586 }
587 // verify legal fru by running it through fru parsing logic
James Feista465ccc2019-02-08 12:51:01 -0800588 if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp))
James Feistb49ffc32018-05-02 11:10:43 -0700589 {
590 std::cerr << "Invalid fru format during writeFru\n";
591 return false;
592 }
593 // baseboard fru
594 if (bus == 0 && address == 0)
595 {
596 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
597 if (!file.good())
598 {
599 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
600 << "\n";
James Feistddb78302018-09-06 11:45:42 -0700601 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700602 return false;
603 }
James Feista465ccc2019-02-08 12:51:01 -0800604 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700605 return file.good();
606 }
607 else
608 {
609 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
610
611 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
612 if (file < 0)
613 {
614 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -0700615 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700616 return false;
617 }
618 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
619 {
620 std::cerr << "unable to set device address\n";
621 close(file);
James Feistddb78302018-09-06 11:45:42 -0700622 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700623 return false;
624 }
625
626 constexpr const size_t RETRY_MAX = 2;
627 uint16_t index = 0;
628 size_t retries = RETRY_MAX;
629 while (index < fru.size())
630 {
631 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
632 (retries == RETRY_MAX))
633 {
634 // The 4K EEPROM only uses the A2 and A1 device address bits
635 // with the third bit being a memory page address bit.
636 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
637 {
638 std::cerr << "unable to set device address\n";
639 close(file);
James Feistddb78302018-09-06 11:45:42 -0700640 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700641 return false;
642 }
643 }
644
645 if (i2c_smbus_write_byte_data(file, index & 0xFF, fru[index]) < 0)
646 {
647 if (!retries--)
648 {
649 std::cerr << "error writing fru: " << strerror(errno)
650 << "\n";
651 close(file);
James Feistddb78302018-09-06 11:45:42 -0700652 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700653 return false;
654 }
655 }
656 else
657 {
658 retries = RETRY_MAX;
659 index++;
660 }
661 // most eeproms require 5-10ms between writes
662 std::this_thread::sleep_for(std::chrono::milliseconds(10));
663 }
664 close(file);
665 return true;
666 }
667}
668
James Feist9eb0b582018-04-27 12:15:46 -0700669void rescanBusses(
James Feista465ccc2019-02-08 12:51:01 -0800670 boost::asio::io_service& io, BusMap& busMap,
671 boost::container::flat_map<
672 std::pair<size_t, size_t>,
673 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
674 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
675 sdbusplus::asio::object_server& objServer)
James Feist918e18c2018-02-13 15:51:07 -0800676{
James Feist6ebf9de2018-05-15 15:01:17 -0700677 static boost::asio::deadline_timer timer(io);
678 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800679
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500680 // setup an async wait in case we get flooded with requests
James Feista465ccc2019-02-08 12:51:01 -0800681 timer.async_wait([&](const boost::system::error_code& ec) {
James Feist4131aea2018-03-09 09:47:30 -0800682 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -0800683 auto matchString = std::string(R"(i2c-\d+$)");
James Feist4131aea2018-03-09 09:47:30 -0800684 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800685
James Feista3c180a2018-08-09 16:06:04 -0700686 if (!findFiles(devDir, matchString, i2cBuses))
James Feist918e18c2018-02-13 15:51:07 -0800687 {
James Feist4131aea2018-03-09 09:47:30 -0800688 std::cerr << "unable to find i2c devices\n";
689 return;
James Feist918e18c2018-02-13 15:51:07 -0800690 }
James Feist4131aea2018-03-09 09:47:30 -0800691 // scanning muxes in reverse order seems to have adverse effects
692 // sorting this list seems to be a fix for that
693 std::sort(i2cBuses.begin(), i2cBuses.end());
James Feist4131aea2018-03-09 09:47:30 -0800694
James Feist6ebf9de2018-05-15 15:01:17 -0700695 busMap.clear();
696 auto scan = std::make_shared<FindDevicesWithCallback>(
697 i2cBuses, io, busMap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -0800698 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700699 {
700 objServer.remove_interface(busIface.second);
701 }
James Feist4131aea2018-03-09 09:47:30 -0800702
James Feist6ebf9de2018-05-15 15:01:17 -0700703 dbusInterfaceMap.clear();
704 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -0800705
James Feist6ebf9de2018-05-15 15:01:17 -0700706 // todo, get this from a more sensable place
707 std::vector<char> baseboardFru;
708 if (readBaseboardFru(baseboardFru))
709 {
710 boost::container::flat_map<int, std::vector<char>>
711 baseboardDev;
712 baseboardDev.emplace(0, baseboardFru);
713 busMap[0] = std::make_shared<DeviceMap>(baseboardDev);
714 }
James Feista465ccc2019-02-08 12:51:01 -0800715 for (auto& devicemap : busMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700716 {
James Feista465ccc2019-02-08 12:51:01 -0800717 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -0700718 {
719 AddFruObjectToDbus(systemBus, device.second, objServer,
720 dbusInterfaceMap, devicemap.first,
721 device.first);
722 }
723 }
724 });
725 scan->run();
726 });
James Feist918e18c2018-02-13 15:51:07 -0800727}
728
James Feista465ccc2019-02-08 12:51:01 -0800729int main(int argc, char** argv)
James Feist3cb5fec2018-01-23 14:41:51 -0800730{
731 auto devDir = fs::path("/dev/");
James Feistc9dff1b2019-02-13 13:33:13 -0800732 auto matchString = std::string(R"(i2c-\d+$)");
James Feist3cb5fec2018-01-23 14:41:51 -0800733 std::vector<fs::path> i2cBuses;
734
James Feista3c180a2018-08-09 16:06:04 -0700735 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -0800736 {
737 std::cerr << "unable to find i2c devices\n";
738 return 1;
739 }
James Feist3cb5fec2018-01-23 14:41:51 -0800740
741 boost::asio::io_service io;
James Feist9eb0b582018-04-27 12:15:46 -0700742 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
743 auto objServer = sdbusplus::asio::object_server(systemBus);
Vijay Khemka065f6d92018-12-18 10:37:47 -0800744 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -0800745
James Feist6ebf9de2018-05-15 15:01:17 -0700746 // this is a map with keys of pair(bus number, address) and values of
747 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -0800748 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -0700749 std::shared_ptr<sdbusplus::asio::dbus_interface>>
750 dbusInterfaceMap;
James Feist2a9d6db2018-04-27 15:48:28 -0700751 BusMap busmap;
James Feist3cb5fec2018-01-23 14:41:51 -0800752
James Feist9eb0b582018-04-27 12:15:46 -0700753 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
754 objServer.add_interface("/xyz/openbmc_project/FruDevice",
755 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -0800756
757 iface->register_method("ReScan", [&]() {
James Feist6ebf9de2018-05-15 15:01:17 -0700758 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feist3cb5fec2018-01-23 14:41:51 -0800759 });
James Feist2a9d6db2018-04-27 15:48:28 -0700760
761 iface->register_method(
James Feista465ccc2019-02-08 12:51:01 -0800762 "GetRawFru", [&](const uint8_t& bus, const uint8_t& address) {
James Feist2a9d6db2018-04-27 15:48:28 -0700763 auto deviceMap = busmap.find(bus);
764 if (deviceMap == busmap.end())
765 {
James Feistddb78302018-09-06 11:45:42 -0700766 throw std::invalid_argument("Invalid Bus.");
James Feist2a9d6db2018-04-27 15:48:28 -0700767 }
768 auto device = deviceMap->second->find(address);
769 if (device == deviceMap->second->end())
770 {
James Feistddb78302018-09-06 11:45:42 -0700771 throw std::invalid_argument("Invalid Address.");
James Feist2a9d6db2018-04-27 15:48:28 -0700772 }
James Feista465ccc2019-02-08 12:51:01 -0800773 std::vector<uint8_t>& ret =
774 reinterpret_cast<std::vector<uint8_t>&>(device->second);
James Feist2a9d6db2018-04-27 15:48:28 -0700775 return ret;
776 });
James Feistb49ffc32018-05-02 11:10:43 -0700777
778 iface->register_method("WriteFru", [&](const uint8_t bus,
779 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -0800780 const std::vector<uint8_t>& data) {
James Feistb49ffc32018-05-02 11:10:43 -0700781 if (!writeFru(bus, address, data))
782 {
James Feistddb78302018-09-06 11:45:42 -0700783 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -0700784 return;
785 }
786 // schedule rescan on success
787 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feistb49ffc32018-05-02 11:10:43 -0700788 });
James Feist9eb0b582018-04-27 12:15:46 -0700789 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800790
James Feist9eb0b582018-04-27 12:15:46 -0700791 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -0800792 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -0800793 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -0700794 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -0800795 std::string,
796 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -0700797 values;
798 message.read(objectName, values);
James Feist918e18c2018-02-13 15:51:07 -0800799 auto findPgood = values.find("pgood");
800 if (findPgood != values.end())
801 {
James Feist6ebf9de2018-05-15 15:01:17 -0700802
803 rescanBusses(io, busmap, dbusInterfaceMap, systemBus,
804 objServer);
James Feist918e18c2018-02-13 15:51:07 -0800805 }
James Feist918e18c2018-02-13 15:51:07 -0800806 };
James Feist9eb0b582018-04-27 12:15:46 -0700807
808 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -0800809 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist9eb0b582018-04-27 12:15:46 -0700810 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
Kuiying Wang1dc43102018-05-09 15:09:29 +0800811 "namespace='/xyz/openbmc_project/Chassis/Control/"
812 "power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist9eb0b582018-04-27 12:15:46 -0700813 eventHandler);
814
James Feist4131aea2018-03-09 09:47:30 -0800815 int fd = inotify_init();
816 int wd = inotify_add_watch(fd, I2C_DEV_LOCATION,
817 IN_CREATE | IN_MOVED_TO | IN_DELETE);
818 std::array<char, 4096> readBuffer;
819 std::string pendingBuffer;
820 // monitor for new i2c devices
821 boost::asio::posix::stream_descriptor dirWatch(io, fd);
822 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -0800823 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -0800824 std::size_t bytes_transferred) {
825 if (ec)
826 {
827 std::cout << "Callback Error " << ec << "\n";
828 return;
829 }
830 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
831 bool devChange = false;
832 while (pendingBuffer.size() > sizeof(inotify_event))
833 {
James Feista465ccc2019-02-08 12:51:01 -0800834 const inotify_event* iEvent =
835 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -0800836 pendingBuffer.data());
837 switch (iEvent->mask)
838 {
James Feist9eb0b582018-04-27 12:15:46 -0700839 case IN_CREATE:
840 case IN_MOVED_TO:
841 case IN_DELETE:
842 if (boost::starts_with(std::string(iEvent->name),
843 "i2c"))
844 {
845 devChange = true;
846 }
James Feist4131aea2018-03-09 09:47:30 -0800847 }
848
849 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
850 }
James Feist6ebf9de2018-05-15 15:01:17 -0700851 if (devChange)
James Feist4131aea2018-03-09 09:47:30 -0800852 {
James Feist6ebf9de2018-05-15 15:01:17 -0700853 rescanBusses(io, busmap, dbusInterfaceMap, systemBus,
854 objServer);
James Feist4131aea2018-03-09 09:47:30 -0800855 }
James Feist6ebf9de2018-05-15 15:01:17 -0700856
James Feist4131aea2018-03-09 09:47:30 -0800857 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
858 watchI2cBusses);
859 };
860
861 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -0500862 // run the initial scan
James Feist6ebf9de2018-05-15 15:01:17 -0700863 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800864
James Feist3cb5fec2018-01-23 14:41:51 -0800865 io.run();
866 return 0;
867}