blob: 5469edfc5ba2f0dd550274642bfbd68a7d720d50 [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
417 size_t length = *fruBytesIter & 0x3f;
418 fruBytesIter += 1;
419
Vijay Khemka5d5de442018-11-07 10:51:25 -0800420 /* Checking for length if field present */
421 if (length == 0)
422 {
423 result[std::string(area) + "_" + field] =
424 std::string("Null");
425 continue;
426 }
427
428 /* Checking for last byte C1 to indicate that no more
429 * field to be read */
430 if (length == 1)
431 {
432 break;
433 }
434
James Feist3cb5fec2018-01-23 14:41:51 -0800435 if (fruBytesIter >= fruBytes.end())
436 {
437 return false;
438 }
439
440 result[std::string(area) + "_" + field] =
441 std::string(fruBytesIter, fruBytesIter + length);
442 fruBytesIter += length;
443 if (fruBytesIter >= fruBytes.end())
444 {
445 std::cerr << "Warning Fru Length Mismatch:\n ";
James Feista465ccc2019-02-08 12:51:01 -0800446 for (auto& c : fruBytes)
James Feist3cb5fec2018-01-23 14:41:51 -0800447 {
448 std::cerr << c;
449 }
450 std::cerr << "\n";
451 if (DEBUG)
452 {
James Feista465ccc2019-02-08 12:51:01 -0800453 for (auto& keyPair : result)
James Feist3cb5fec2018-01-23 14:41:51 -0800454 {
455 std::cerr << keyPair.first << " : "
456 << keyPair.second << "\n";
457 }
458 }
459 return false;
460 }
461 }
462 }
463 }
464
465 return true;
466}
467
468void AddFruObjectToDbus(
James Feist9eb0b582018-04-27 12:15:46 -0700469 std::shared_ptr<sdbusplus::asio::connection> dbusConn,
James Feista465ccc2019-02-08 12:51:01 -0800470 std::vector<char>& device, sdbusplus::asio::object_server& objServer,
471 boost::container::flat_map<
472 std::pair<size_t, size_t>,
473 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
James Feist13b86d62018-05-29 11:24:35 -0700474 uint32_t bus, uint32_t address)
James Feist3cb5fec2018-01-23 14:41:51 -0800475{
476 boost::container::flat_map<std::string, std::string> formattedFru;
477 if (!formatFru(device, formattedFru))
478 {
479 std::cerr << "failed to format fru for device at bus " << std::hex
480 << bus << "address " << address << "\n";
481 return;
482 }
483 auto productNameFind = formattedFru.find("BOARD_PRODUCT_NAME");
484 std::string productName;
485 if (productNameFind == formattedFru.end())
486 {
487 productNameFind = formattedFru.find("PRODUCT_PRODUCT_NAME");
488 }
489 if (productNameFind != formattedFru.end())
490 {
491 productName = productNameFind->second;
James Feist3f8a2782018-02-12 09:24:42 -0800492 std::regex illegalObject("[^A-Za-z0-9_]");
493 productName = std::regex_replace(productName, illegalObject, "_");
James Feist3cb5fec2018-01-23 14:41:51 -0800494 }
495 else
496 {
497 productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT);
498 UNKNOWN_BUS_OBJECT_COUNT++;
499 }
500
James Feist918e18c2018-02-13 15:51:07 -0800501 productName = "/xyz/openbmc_project/FruDevice/" + productName;
James Feist918e18c2018-02-13 15:51:07 -0800502 // avoid duplicates by checking to see if on a mux
James Feist79e9c0b2018-03-15 15:21:17 -0700503 if (bus > 0)
James Feist918e18c2018-02-13 15:51:07 -0800504 {
James Feist79e9c0b2018-03-15 15:21:17 -0700505 size_t index = 0;
James Feista465ccc2019-02-08 12:51:01 -0800506 for (auto const& busIface : dbusInterfaceMap)
James Feist918e18c2018-02-13 15:51:07 -0800507 {
James Feist9eb0b582018-04-27 12:15:46 -0700508 if ((busIface.second->get_object_path() == productName))
James Feist918e18c2018-02-13 15:51:07 -0800509 {
James Feist9eb0b582018-04-27 12:15:46 -0700510 if (isMuxBus(bus) && address == busIface.first.second)
James Feist79e9c0b2018-03-15 15:21:17 -0700511 {
512 continue;
513 }
514 // add underscore _index for the same object path on dbus
515 std::string strIndex = std::to_string(index);
516 if (index > 0)
517 {
518 productName.substr(0, productName.size() - strIndex.size());
519 }
520 else
521 {
522 productName += "_";
523 }
524 productName += std::to_string(index++);
James Feist918e18c2018-02-13 15:51:07 -0800525 }
526 }
527 }
James Feist3cb5fec2018-01-23 14:41:51 -0800528
James Feist9eb0b582018-04-27 12:15:46 -0700529 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
530 objServer.add_interface(productName, "xyz.openbmc_project.FruDevice");
531 dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface;
532
James Feista465ccc2019-02-08 12:51:01 -0800533 for (auto& property : formattedFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800534 {
James Feist9eb0b582018-04-27 12:15:46 -0700535
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700536 std::regex_replace(property.second.begin(), property.second.begin(),
537 property.second.end(), NON_ASCII_REGEX, "_");
James Feist9eb0b582018-04-27 12:15:46 -0700538 if (property.second.empty())
539 {
540 continue;
541 }
542 std::string key =
543 std::regex_replace(property.first, NON_ASCII_REGEX, "_");
544 if (!iface->register_property(key, property.second + '\0'))
545 {
546 std::cerr << "illegal key: " << key << "\n";
547 }
Jae Hyun Yoo3936e7a2018-03-23 17:26:16 -0700548 if (DEBUG)
549 {
550 std::cout << property.first << ": " << property.second << "\n";
551 }
James Feist3cb5fec2018-01-23 14:41:51 -0800552 }
James Feist2a9d6db2018-04-27 15:48:28 -0700553
554 // baseboard will be 0, 0
James Feist13b86d62018-05-29 11:24:35 -0700555 iface->register_property("BUS", bus);
556 iface->register_property("ADDRESS", address);
James Feist2a9d6db2018-04-27 15:48:28 -0700557
James Feist9eb0b582018-04-27 12:15:46 -0700558 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800559}
560
James Feista465ccc2019-02-08 12:51:01 -0800561static bool readBaseboardFru(std::vector<char>& baseboardFru)
James Feist3cb5fec2018-01-23 14:41:51 -0800562{
563 // try to read baseboard fru from file
564 std::ifstream baseboardFruFile(BASEBOARD_FRU_LOCATION, std::ios::binary);
565 if (baseboardFruFile.good())
566 {
567 baseboardFruFile.seekg(0, std::ios_base::end);
568 std::streampos fileSize = baseboardFruFile.tellg();
569 baseboardFru.resize(fileSize);
570 baseboardFruFile.seekg(0, std::ios_base::beg);
571 baseboardFruFile.read(baseboardFru.data(), fileSize);
572 }
573 else
574 {
575 return false;
576 }
577 return true;
578}
579
James Feista465ccc2019-02-08 12:51:01 -0800580bool writeFru(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru)
James Feistb49ffc32018-05-02 11:10:43 -0700581{
582 boost::container::flat_map<std::string, std::string> tmp;
583 if (fru.size() > MAX_FRU_SIZE)
584 {
585 std::cerr << "Invalid fru.size() during writeFru\n";
586 return false;
587 }
588 // verify legal fru by running it through fru parsing logic
James Feista465ccc2019-02-08 12:51:01 -0800589 if (!formatFru(reinterpret_cast<const std::vector<char>&>(fru), tmp))
James Feistb49ffc32018-05-02 11:10:43 -0700590 {
591 std::cerr << "Invalid fru format during writeFru\n";
592 return false;
593 }
594 // baseboard fru
595 if (bus == 0 && address == 0)
596 {
597 std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary);
598 if (!file.good())
599 {
600 std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION
601 << "\n";
James Feistddb78302018-09-06 11:45:42 -0700602 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700603 return false;
604 }
James Feista465ccc2019-02-08 12:51:01 -0800605 file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
James Feistb49ffc32018-05-02 11:10:43 -0700606 return file.good();
607 }
608 else
609 {
610 std::string i2cBus = "/dev/i2c-" + std::to_string(bus);
611
612 int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC);
613 if (file < 0)
614 {
615 std::cerr << "unable to open i2c device " << i2cBus << "\n";
James Feistddb78302018-09-06 11:45:42 -0700616 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700617 return false;
618 }
619 if (ioctl(file, I2C_SLAVE_FORCE, address) < 0)
620 {
621 std::cerr << "unable to set device address\n";
622 close(file);
James Feistddb78302018-09-06 11:45:42 -0700623 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700624 return false;
625 }
626
627 constexpr const size_t RETRY_MAX = 2;
628 uint16_t index = 0;
629 size_t retries = RETRY_MAX;
630 while (index < fru.size())
631 {
632 if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) &&
633 (retries == RETRY_MAX))
634 {
635 // The 4K EEPROM only uses the A2 and A1 device address bits
636 // with the third bit being a memory page address bit.
637 if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0)
638 {
639 std::cerr << "unable to set device address\n";
640 close(file);
James Feistddb78302018-09-06 11:45:42 -0700641 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700642 return false;
643 }
644 }
645
646 if (i2c_smbus_write_byte_data(file, index & 0xFF, fru[index]) < 0)
647 {
648 if (!retries--)
649 {
650 std::cerr << "error writing fru: " << strerror(errno)
651 << "\n";
652 close(file);
James Feistddb78302018-09-06 11:45:42 -0700653 throw DBusInternalError();
James Feistb49ffc32018-05-02 11:10:43 -0700654 return false;
655 }
656 }
657 else
658 {
659 retries = RETRY_MAX;
660 index++;
661 }
662 // most eeproms require 5-10ms between writes
663 std::this_thread::sleep_for(std::chrono::milliseconds(10));
664 }
665 close(file);
666 return true;
667 }
668}
669
James Feist9eb0b582018-04-27 12:15:46 -0700670void rescanBusses(
James Feista465ccc2019-02-08 12:51:01 -0800671 boost::asio::io_service& io, BusMap& busMap,
672 boost::container::flat_map<
673 std::pair<size_t, size_t>,
674 std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap,
675 std::shared_ptr<sdbusplus::asio::connection>& systemBus,
676 sdbusplus::asio::object_server& objServer)
James Feist918e18c2018-02-13 15:51:07 -0800677{
James Feist6ebf9de2018-05-15 15:01:17 -0700678 static boost::asio::deadline_timer timer(io);
679 timer.expires_from_now(boost::posix_time::seconds(1));
James Feist918e18c2018-02-13 15:51:07 -0800680
Gunnar Mills6f0ae942018-08-31 12:38:03 -0500681 // setup an async wait in case we get flooded with requests
James Feista465ccc2019-02-08 12:51:01 -0800682 timer.async_wait([&](const boost::system::error_code& ec) {
James Feist4131aea2018-03-09 09:47:30 -0800683 auto devDir = fs::path("/dev/");
684 auto matchString = std::string("i2c*");
685 std::vector<fs::path> i2cBuses;
James Feist918e18c2018-02-13 15:51:07 -0800686
James Feista3c180a2018-08-09 16:06:04 -0700687 if (!findFiles(devDir, matchString, i2cBuses))
James Feist918e18c2018-02-13 15:51:07 -0800688 {
James Feist4131aea2018-03-09 09:47:30 -0800689 std::cerr << "unable to find i2c devices\n";
690 return;
James Feist918e18c2018-02-13 15:51:07 -0800691 }
James Feist4131aea2018-03-09 09:47:30 -0800692 // scanning muxes in reverse order seems to have adverse effects
693 // sorting this list seems to be a fix for that
694 std::sort(i2cBuses.begin(), i2cBuses.end());
James Feist4131aea2018-03-09 09:47:30 -0800695
James Feist6ebf9de2018-05-15 15:01:17 -0700696 busMap.clear();
697 auto scan = std::make_shared<FindDevicesWithCallback>(
698 i2cBuses, io, busMap, [&]() {
James Feista465ccc2019-02-08 12:51:01 -0800699 for (auto& busIface : dbusInterfaceMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700700 {
701 objServer.remove_interface(busIface.second);
702 }
James Feist4131aea2018-03-09 09:47:30 -0800703
James Feist6ebf9de2018-05-15 15:01:17 -0700704 dbusInterfaceMap.clear();
705 UNKNOWN_BUS_OBJECT_COUNT = 0;
James Feist4131aea2018-03-09 09:47:30 -0800706
James Feist6ebf9de2018-05-15 15:01:17 -0700707 // todo, get this from a more sensable place
708 std::vector<char> baseboardFru;
709 if (readBaseboardFru(baseboardFru))
710 {
711 boost::container::flat_map<int, std::vector<char>>
712 baseboardDev;
713 baseboardDev.emplace(0, baseboardFru);
714 busMap[0] = std::make_shared<DeviceMap>(baseboardDev);
715 }
James Feista465ccc2019-02-08 12:51:01 -0800716 for (auto& devicemap : busMap)
James Feist6ebf9de2018-05-15 15:01:17 -0700717 {
James Feista465ccc2019-02-08 12:51:01 -0800718 for (auto& device : *devicemap.second)
James Feist6ebf9de2018-05-15 15:01:17 -0700719 {
720 AddFruObjectToDbus(systemBus, device.second, objServer,
721 dbusInterfaceMap, devicemap.first,
722 device.first);
723 }
724 }
725 });
726 scan->run();
727 });
James Feist918e18c2018-02-13 15:51:07 -0800728}
729
James Feista465ccc2019-02-08 12:51:01 -0800730int main(int argc, char** argv)
James Feist3cb5fec2018-01-23 14:41:51 -0800731{
732 auto devDir = fs::path("/dev/");
733 auto matchString = std::string("i2c*");
734 std::vector<fs::path> i2cBuses;
735
James Feista3c180a2018-08-09 16:06:04 -0700736 if (!findFiles(devDir, matchString, i2cBuses))
James Feist3cb5fec2018-01-23 14:41:51 -0800737 {
738 std::cerr << "unable to find i2c devices\n";
739 return 1;
740 }
James Feist3cb5fec2018-01-23 14:41:51 -0800741
742 boost::asio::io_service io;
James Feist9eb0b582018-04-27 12:15:46 -0700743 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
744 auto objServer = sdbusplus::asio::object_server(systemBus);
Vijay Khemka065f6d92018-12-18 10:37:47 -0800745 systemBus->request_name("xyz.openbmc_project.FruDevice");
James Feist3cb5fec2018-01-23 14:41:51 -0800746
James Feist6ebf9de2018-05-15 15:01:17 -0700747 // this is a map with keys of pair(bus number, address) and values of
748 // the object on dbus
James Feist3cb5fec2018-01-23 14:41:51 -0800749 boost::container::flat_map<std::pair<size_t, size_t>,
James Feist9eb0b582018-04-27 12:15:46 -0700750 std::shared_ptr<sdbusplus::asio::dbus_interface>>
751 dbusInterfaceMap;
James Feist2a9d6db2018-04-27 15:48:28 -0700752 BusMap busmap;
James Feist3cb5fec2018-01-23 14:41:51 -0800753
James Feist9eb0b582018-04-27 12:15:46 -0700754 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
755 objServer.add_interface("/xyz/openbmc_project/FruDevice",
756 "xyz.openbmc_project.FruDeviceManager");
James Feist3cb5fec2018-01-23 14:41:51 -0800757
758 iface->register_method("ReScan", [&]() {
James Feist6ebf9de2018-05-15 15:01:17 -0700759 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feist3cb5fec2018-01-23 14:41:51 -0800760 });
James Feist2a9d6db2018-04-27 15:48:28 -0700761
762 iface->register_method(
James Feista465ccc2019-02-08 12:51:01 -0800763 "GetRawFru", [&](const uint8_t& bus, const uint8_t& address) {
James Feist2a9d6db2018-04-27 15:48:28 -0700764 auto deviceMap = busmap.find(bus);
765 if (deviceMap == busmap.end())
766 {
James Feistddb78302018-09-06 11:45:42 -0700767 throw std::invalid_argument("Invalid Bus.");
James Feist2a9d6db2018-04-27 15:48:28 -0700768 }
769 auto device = deviceMap->second->find(address);
770 if (device == deviceMap->second->end())
771 {
James Feistddb78302018-09-06 11:45:42 -0700772 throw std::invalid_argument("Invalid Address.");
James Feist2a9d6db2018-04-27 15:48:28 -0700773 }
James Feista465ccc2019-02-08 12:51:01 -0800774 std::vector<uint8_t>& ret =
775 reinterpret_cast<std::vector<uint8_t>&>(device->second);
James Feist2a9d6db2018-04-27 15:48:28 -0700776 return ret;
777 });
James Feistb49ffc32018-05-02 11:10:43 -0700778
779 iface->register_method("WriteFru", [&](const uint8_t bus,
780 const uint8_t address,
James Feista465ccc2019-02-08 12:51:01 -0800781 const std::vector<uint8_t>& data) {
James Feistb49ffc32018-05-02 11:10:43 -0700782 if (!writeFru(bus, address, data))
783 {
James Feistddb78302018-09-06 11:45:42 -0700784 throw std::invalid_argument("Invalid Arguments.");
James Feistb49ffc32018-05-02 11:10:43 -0700785 return;
786 }
787 // schedule rescan on success
788 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feistb49ffc32018-05-02 11:10:43 -0700789 });
James Feist9eb0b582018-04-27 12:15:46 -0700790 iface->initialize();
James Feist3cb5fec2018-01-23 14:41:51 -0800791
James Feist9eb0b582018-04-27 12:15:46 -0700792 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feista465ccc2019-02-08 12:51:01 -0800793 [&](sdbusplus::message::message& message) {
James Feist918e18c2018-02-13 15:51:07 -0800794 std::string objectName;
James Feist9eb0b582018-04-27 12:15:46 -0700795 boost::container::flat_map<
James Feista465ccc2019-02-08 12:51:01 -0800796 std::string,
797 std::variant<std::string, bool, int64_t, uint64_t, double>>
James Feist9eb0b582018-04-27 12:15:46 -0700798 values;
799 message.read(objectName, values);
James Feist918e18c2018-02-13 15:51:07 -0800800 auto findPgood = values.find("pgood");
801 if (findPgood != values.end())
802 {
James Feist6ebf9de2018-05-15 15:01:17 -0700803
804 rescanBusses(io, busmap, dbusInterfaceMap, systemBus,
805 objServer);
James Feist918e18c2018-02-13 15:51:07 -0800806 }
James Feist918e18c2018-02-13 15:51:07 -0800807 };
James Feist9eb0b582018-04-27 12:15:46 -0700808
809 sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match(
James Feista465ccc2019-02-08 12:51:01 -0800810 static_cast<sdbusplus::bus::bus&>(*systemBus),
James Feist9eb0b582018-04-27 12:15:46 -0700811 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
Kuiying Wang1dc43102018-05-09 15:09:29 +0800812 "namespace='/xyz/openbmc_project/Chassis/Control/"
813 "power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist9eb0b582018-04-27 12:15:46 -0700814 eventHandler);
815
James Feist4131aea2018-03-09 09:47:30 -0800816 int fd = inotify_init();
817 int wd = inotify_add_watch(fd, I2C_DEV_LOCATION,
818 IN_CREATE | IN_MOVED_TO | IN_DELETE);
819 std::array<char, 4096> readBuffer;
820 std::string pendingBuffer;
821 // monitor for new i2c devices
822 boost::asio::posix::stream_descriptor dirWatch(io, fd);
823 std::function<void(const boost::system::error_code, std::size_t)>
James Feista465ccc2019-02-08 12:51:01 -0800824 watchI2cBusses = [&](const boost::system::error_code& ec,
James Feist4131aea2018-03-09 09:47:30 -0800825 std::size_t bytes_transferred) {
826 if (ec)
827 {
828 std::cout << "Callback Error " << ec << "\n";
829 return;
830 }
831 pendingBuffer += std::string(readBuffer.data(), bytes_transferred);
832 bool devChange = false;
833 while (pendingBuffer.size() > sizeof(inotify_event))
834 {
James Feista465ccc2019-02-08 12:51:01 -0800835 const inotify_event* iEvent =
836 reinterpret_cast<const inotify_event*>(
James Feist4131aea2018-03-09 09:47:30 -0800837 pendingBuffer.data());
838 switch (iEvent->mask)
839 {
James Feist9eb0b582018-04-27 12:15:46 -0700840 case IN_CREATE:
841 case IN_MOVED_TO:
842 case IN_DELETE:
843 if (boost::starts_with(std::string(iEvent->name),
844 "i2c"))
845 {
846 devChange = true;
847 }
James Feist4131aea2018-03-09 09:47:30 -0800848 }
849
850 pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len);
851 }
James Feist6ebf9de2018-05-15 15:01:17 -0700852 if (devChange)
James Feist4131aea2018-03-09 09:47:30 -0800853 {
James Feist6ebf9de2018-05-15 15:01:17 -0700854 rescanBusses(io, busmap, dbusInterfaceMap, systemBus,
855 objServer);
James Feist4131aea2018-03-09 09:47:30 -0800856 }
James Feist6ebf9de2018-05-15 15:01:17 -0700857
James Feist4131aea2018-03-09 09:47:30 -0800858 dirWatch.async_read_some(boost::asio::buffer(readBuffer),
859 watchI2cBusses);
860 };
861
862 dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses);
Gunnar Millsb3e42fe2018-06-13 15:48:27 -0500863 // run the initial scan
James Feist6ebf9de2018-05-15 15:01:17 -0700864 rescanBusses(io, busmap, dbusInterfaceMap, systemBus, objServer);
James Feist918e18c2018-02-13 15:51:07 -0800865
James Feist3cb5fec2018-01-23 14:41:51 -0800866 io.run();
867 return 0;
868}