James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
Brad Bishop | 1fb9f3f | 2020-08-28 08:15:13 -0400 | [diff] [blame] | 16 | /// \file FruDevice.cpp |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 17 | |
Patrick Venture | a49dc33 | 2019-10-26 08:32:02 -0700 | [diff] [blame] | 18 | #include "Utils.hpp" |
| 19 | |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 20 | #include <errno.h> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 21 | #include <fcntl.h> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 22 | #include <sys/inotify.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 25 | #include <boost/algorithm/string/predicate.hpp> |
Brad Bishop | 82c84bb | 2020-08-26 14:12:50 -0400 | [diff] [blame] | 26 | #include <boost/asio/deadline_timer.hpp> |
| 27 | #include <boost/asio/io_service.hpp> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 28 | #include <boost/container/flat_map.hpp> |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 29 | #include <nlohmann/json.hpp> |
| 30 | #include <sdbusplus/asio/connection.hpp> |
| 31 | #include <sdbusplus/asio/object_server.hpp> |
| 32 | |
| 33 | #include <array> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 34 | #include <chrono> |
| 35 | #include <ctime> |
Patrick Venture | e375400 | 2019-08-06 09:39:12 -0700 | [diff] [blame] | 36 | #include <filesystem> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 37 | #include <fstream> |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 38 | #include <functional> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 39 | #include <future> |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 40 | #include <iomanip> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 41 | #include <iostream> |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 42 | #include <limits> |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 43 | #include <regex> |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 44 | #include <set> |
| 45 | #include <sstream> |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 46 | #include <string> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 47 | #include <thread> |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 48 | #include <utility> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 49 | #include <variant> |
Patrick Venture | c274f2f | 2019-10-26 09:27:23 -0700 | [diff] [blame] | 50 | #include <vector> |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 51 | |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 52 | extern "C" |
| 53 | { |
James Feist | 3b86098 | 2018-10-02 14:34:07 -0700 | [diff] [blame] | 54 | #include <i2c/smbus.h> |
| 55 | #include <linux/i2c-dev.h> |
| 56 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 57 | |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 58 | namespace fs = std::filesystem; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 59 | static constexpr bool DEBUG = false; |
| 60 | static size_t UNKNOWN_BUS_OBJECT_COUNT = 0; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 61 | constexpr size_t MAX_FRU_SIZE = 512; |
| 62 | constexpr size_t MAX_EEPROM_PAGE_INDEX = 255; |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 63 | constexpr size_t busTimeoutSeconds = 5; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 64 | constexpr size_t fruBlockSize = 8; // FRU areas are measured in 8-byte blocks |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 65 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 66 | constexpr const char* blacklistPath = PACKAGE_DIR "blacklist.json"; |
| 67 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 68 | const static constexpr char* BASEBOARD_FRU_LOCATION = |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 69 | "/etc/fru/baseboard.fru.bin"; |
| 70 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 71 | const static constexpr char* I2C_DEV_LOCATION = "/dev"; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 72 | |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 73 | enum class resCodes |
| 74 | { |
| 75 | resOK, |
| 76 | resWarn, |
| 77 | resErr |
| 78 | }; |
| 79 | |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 80 | enum class fruAreas |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 81 | { |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 82 | fruAreaInternal = 0, |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 83 | fruAreaChassis, |
| 84 | fruAreaBoard, |
| 85 | fruAreaProduct, |
| 86 | fruAreaMultirecord |
| 87 | }; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 88 | inline fruAreas operator++(fruAreas& x) |
| 89 | { |
| 90 | return x = static_cast<fruAreas>(std::underlying_type<fruAreas>::type(x) + |
| 91 | 1); |
| 92 | } |
| 93 | |
| 94 | static const std::vector<std::string> FRU_AREA_NAMES = { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 95 | "INTERNAL", "CHASSIS", "BOARD", "PRODUCT", "MULTIRECORD"}; |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 96 | const static std::regex NON_ASCII_REGEX("[^\x01-\x7f]"); |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 97 | using DeviceMap = boost::container::flat_map<int, std::vector<uint8_t>>; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 98 | using BusMap = boost::container::flat_map<int, std::shared_ptr<DeviceMap>>; |
| 99 | |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 100 | static std::set<size_t> busBlacklist; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 101 | struct FindDevicesWithCallback; |
| 102 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 103 | static BusMap busMap; |
| 104 | |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 105 | static bool powerIsOn = false; |
| 106 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 107 | static boost::container::flat_map< |
| 108 | std::pair<size_t, size_t>, std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 109 | foundDevices; |
| 110 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 111 | static boost::container::flat_map<size_t, std::set<size_t>> failedAddresses; |
| 112 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 113 | boost::asio::io_service io; |
| 114 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 115 | auto objServer = sdbusplus::asio::object_server(systemBus); |
| 116 | |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 117 | static const std::vector<std::string> CHASSIS_FRU_AREAS = {"PART_NUMBER", |
| 118 | "SERIAL_NUMBER"}; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 119 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 120 | static const std::vector<std::string> BOARD_FRU_AREAS = { |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 121 | "MANUFACTURER", "PRODUCT_NAME", "SERIAL_NUMBER", "PART_NUMBER", |
| 122 | "FRU_VERSION_ID"}; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 123 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 124 | static const std::vector<std::string> PRODUCT_FRU_AREAS = { |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 125 | "MANUFACTURER", "PRODUCT_NAME", "PART_NUMBER", "VERSION", |
| 126 | "SERIAL_NUMBER", "ASSET_TAG", "FRU_VERSION_ID"}; |
| 127 | |
| 128 | static const std::string FRU_CUSTOM_FIELD_NAME = "INFO_AM"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 129 | |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 130 | static inline unsigned int getHeaderAreaFieldOffset(fruAreas area) |
| 131 | { |
| 132 | return static_cast<unsigned int>(area) + 1; |
| 133 | } |
| 134 | static inline const std::string& getFruAreaName(fruAreas area) |
| 135 | { |
| 136 | return FRU_AREA_NAMES[static_cast<unsigned int>(area)]; |
| 137 | } |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 138 | bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData); |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 139 | uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| 140 | std::vector<uint8_t>::const_iterator end); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 141 | bool updateFRUProperty( |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 142 | const std::string& assetTag, uint32_t bus, uint32_t address, |
| 143 | std::string propertyName, |
| 144 | boost::container::flat_map< |
| 145 | std::pair<size_t, size_t>, |
| 146 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 147 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 148 | using ReadBlockFunc = |
| 149 | std::function<int64_t(int flag, int file, uint16_t address, uint16_t offset, |
| 150 | uint8_t length, uint8_t* outBuf)>; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 151 | |
| 152 | // Read and validate FRU contents, given the flag required for raw i2c, the open |
| 153 | // file handle, a read method, and a helper string for failures. |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 154 | std::vector<uint8_t> readFRUContents(int flag, int file, uint16_t address, |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 155 | ReadBlockFunc readBlock, |
| 156 | const std::string& errorHelp) |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 157 | { |
| 158 | std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData; |
| 159 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 160 | if (readBlock(flag, file, address, 0x0, 0x8, blockData.data()) < 0) |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 161 | { |
| 162 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 163 | return {}; |
| 164 | } |
| 165 | |
| 166 | // check the header checksum |
| 167 | if (!validateHeader(blockData)) |
| 168 | { |
| 169 | if (DEBUG) |
| 170 | { |
| 171 | std::cerr << "Illegal header " << errorHelp << "\n"; |
| 172 | } |
| 173 | |
| 174 | return {}; |
| 175 | } |
| 176 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 177 | std::vector<uint8_t> device; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 178 | device.insert(device.end(), blockData.begin(), blockData.begin() + 8); |
| 179 | |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 180 | bool hasMultiRecords = false; |
Brad Bishop | ef3cdc9 | 2020-10-01 10:36:21 -0400 | [diff] [blame] | 181 | size_t fruLength = fruBlockSize; // At least FRU header is present |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 182 | for (fruAreas area = fruAreas::fruAreaInternal; |
| 183 | area <= fruAreas::fruAreaMultirecord; ++area) |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 184 | { |
Patrick Venture | f2ad76b | 2019-10-30 08:49:27 -0700 | [diff] [blame] | 185 | // Offset value can be 255. |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 186 | unsigned int areaOffset = device[getHeaderAreaFieldOffset(area)]; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 187 | if (areaOffset == 0) |
| 188 | { |
| 189 | continue; |
| 190 | } |
| 191 | |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 192 | // MultiRecords are different. area is not tracking section, it's |
| 193 | // walking the common header. |
| 194 | if (area == fruAreas::fruAreaMultirecord) |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 195 | { |
| 196 | hasMultiRecords = true; |
| 197 | break; |
| 198 | } |
| 199 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 200 | areaOffset *= fruBlockSize; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 201 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 202 | if (readBlock(flag, file, address, static_cast<uint16_t>(areaOffset), |
| 203 | 0x2, blockData.data()) < 0) |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 204 | { |
| 205 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 206 | return {}; |
| 207 | } |
| 208 | |
Patrick Venture | f2ad76b | 2019-10-30 08:49:27 -0700 | [diff] [blame] | 209 | // Ignore data type (blockData is already unsigned). |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 210 | size_t length = blockData[1] * fruBlockSize; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 211 | areaOffset += length; |
| 212 | fruLength = (areaOffset > fruLength) ? areaOffset : fruLength; |
| 213 | } |
| 214 | |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 215 | if (hasMultiRecords) |
| 216 | { |
| 217 | // device[area count] is the index to the last area because the 0th |
| 218 | // entry is not an offset in the common header. |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 219 | unsigned int areaOffset = |
| 220 | device[getHeaderAreaFieldOffset(fruAreas::fruAreaMultirecord)]; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 221 | areaOffset *= fruBlockSize; |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 222 | |
| 223 | // the multi-area record header is 5 bytes long. |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 224 | constexpr size_t multiRecordHeaderSize = 5; |
| 225 | constexpr uint8_t multiRecordEndOfListMask = 0x80; |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 226 | |
| 227 | // Sanity hard-limit to 64KB. |
| 228 | while (areaOffset < std::numeric_limits<uint16_t>::max()) |
| 229 | { |
| 230 | // In multi-area, the area offset points to the 0th record, each |
| 231 | // record has 3 bytes of the header we care about. |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 232 | if (readBlock(flag, file, address, |
| 233 | static_cast<uint16_t>(areaOffset), 0x3, |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 234 | blockData.data()) < 0) |
| 235 | { |
| 236 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 237 | return {}; |
| 238 | } |
| 239 | |
| 240 | // Ok, let's check the record length, which is in bytes (unsigned, |
| 241 | // up to 255, so blockData should hold uint8_t not char) |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 242 | size_t recordLength = blockData[2]; |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 243 | areaOffset += (recordLength + multiRecordHeaderSize); |
| 244 | fruLength = (areaOffset > fruLength) ? areaOffset : fruLength; |
| 245 | |
| 246 | // If this is the end of the list bail. |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 247 | if ((blockData[1] & multiRecordEndOfListMask)) |
Patrick Venture | e26395d | 2019-10-29 14:05:16 -0700 | [diff] [blame] | 248 | { |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 254 | // You already copied these first 8 bytes (the ipmi fru header size) |
Brad Bishop | ef3cdc9 | 2020-10-01 10:36:21 -0400 | [diff] [blame] | 255 | fruLength -= std::min(fruBlockSize, fruLength); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 256 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 257 | int readOffset = fruBlockSize; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 258 | |
| 259 | while (fruLength > 0) |
| 260 | { |
Brad Bishop | ef3cdc9 | 2020-10-01 10:36:21 -0400 | [diff] [blame] | 261 | size_t requestLength = |
| 262 | std::min(static_cast<size_t>(I2C_SMBUS_BLOCK_MAX), fruLength); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 263 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 264 | if (readBlock(flag, file, address, static_cast<uint16_t>(readOffset), |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 265 | static_cast<uint8_t>(requestLength), |
| 266 | blockData.data()) < 0) |
| 267 | { |
| 268 | std::cerr << "failed to read " << errorHelp << "\n"; |
| 269 | return {}; |
| 270 | } |
| 271 | |
| 272 | device.insert(device.end(), blockData.begin(), |
| 273 | blockData.begin() + requestLength); |
| 274 | |
| 275 | readOffset += requestLength; |
Brad Bishop | ef3cdc9 | 2020-10-01 10:36:21 -0400 | [diff] [blame] | 276 | fruLength -= std::min(requestLength, fruLength); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return device; |
| 280 | } |
| 281 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 282 | // Given a bus/address, produce the path in sysfs for an eeprom. |
| 283 | static std::string getEepromPath(size_t bus, size_t address) |
| 284 | { |
| 285 | std::stringstream output; |
| 286 | output << "/sys/bus/i2c/devices/" << bus << "-" << std::right |
| 287 | << std::setfill('0') << std::setw(4) << std::hex << address |
| 288 | << "/eeprom"; |
| 289 | return output.str(); |
| 290 | } |
| 291 | |
| 292 | static bool hasEepromFile(size_t bus, size_t address) |
| 293 | { |
| 294 | auto path = getEepromPath(bus, address); |
| 295 | try |
| 296 | { |
| 297 | return fs::exists(path); |
| 298 | } |
| 299 | catch (...) |
| 300 | { |
| 301 | return false; |
| 302 | } |
| 303 | } |
| 304 | |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 305 | static int64_t readFromEeprom(int flag __attribute__((unused)), int fd, |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 306 | uint16_t address __attribute__((unused)), |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 307 | uint16_t offset, uint8_t len, uint8_t* buf) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 308 | { |
| 309 | auto result = lseek(fd, offset, SEEK_SET); |
| 310 | if (result < 0) |
| 311 | { |
| 312 | std::cerr << "failed to seek\n"; |
| 313 | return -1; |
| 314 | } |
| 315 | |
Patrick Venture | 3ac8e4f | 2019-10-28 13:07:21 -0700 | [diff] [blame] | 316 | return read(fd, buf, len); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 317 | } |
| 318 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 319 | static int busStrToInt(const std::string& busName) |
| 320 | { |
| 321 | auto findBus = busName.rfind("-"); |
| 322 | if (findBus == std::string::npos) |
| 323 | { |
| 324 | return -1; |
| 325 | } |
| 326 | return std::stoi(busName.substr(findBus + 1)); |
| 327 | } |
| 328 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 329 | static int getRootBus(size_t bus) |
| 330 | { |
| 331 | auto ec = std::error_code(); |
| 332 | auto path = std::filesystem::read_symlink( |
| 333 | std::filesystem::path("/sys/bus/i2c/devices/i2c-" + |
| 334 | std::to_string(bus) + "/mux_device"), |
| 335 | ec); |
| 336 | if (ec) |
| 337 | { |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | std::string filename = path.filename(); |
| 342 | auto findBus = filename.find("-"); |
| 343 | if (findBus == std::string::npos) |
| 344 | { |
| 345 | return -1; |
| 346 | } |
| 347 | return std::stoi(filename.substr(0, findBus)); |
| 348 | } |
| 349 | |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 350 | static bool isMuxBus(size_t bus) |
| 351 | { |
Ed Tanous | 072e25d | 2018-12-16 21:45:20 -0800 | [diff] [blame] | 352 | return is_symlink(std::filesystem::path( |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 353 | "/sys/bus/i2c/devices/i2c-" + std::to_string(bus) + "/mux_device")); |
| 354 | } |
| 355 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 356 | static void makeProbeInterface(size_t bus, size_t address) |
| 357 | { |
| 358 | if (isMuxBus(bus)) |
| 359 | { |
| 360 | return; // the mux buses are random, no need to publish |
| 361 | } |
| 362 | auto [it, success] = foundDevices.emplace( |
| 363 | std::make_pair(bus, address), |
| 364 | objServer.add_interface( |
| 365 | "/xyz/openbmc_project/FruDevice/" + std::to_string(bus) + "_" + |
| 366 | std::to_string(address), |
| 367 | "xyz.openbmc_project.Inventory.Item.I2CDevice")); |
| 368 | if (!success) |
| 369 | { |
| 370 | return; // already added |
| 371 | } |
| 372 | it->second->register_property("Bus", bus); |
| 373 | it->second->register_property("Address", address); |
| 374 | it->second->initialize(); |
| 375 | } |
| 376 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 377 | static int isDevice16Bit(int file) |
| 378 | { |
| 379 | /* Get first byte */ |
| 380 | int byte1 = i2c_smbus_read_byte_data(file, 0); |
| 381 | if (byte1 < 0) |
| 382 | { |
| 383 | return byte1; |
| 384 | } |
| 385 | /* Read 7 more bytes, it will read same first byte in case of |
| 386 | * 8 bit but it will read next byte in case of 16 bit |
| 387 | */ |
| 388 | for (int i = 0; i < 7; i++) |
| 389 | { |
| 390 | int byte2 = i2c_smbus_read_byte_data(file, 0); |
| 391 | if (byte2 < 0) |
| 392 | { |
| 393 | return byte2; |
| 394 | } |
| 395 | if (byte2 != byte1) |
| 396 | { |
| 397 | return 1; |
| 398 | } |
| 399 | } |
| 400 | return 0; |
| 401 | } |
| 402 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 403 | // Issue an I2C transaction to first write to_slave_buf_len bytes,then read |
| 404 | // from_slave_buf_len bytes. |
| 405 | static int i2c_smbus_write_then_read(int file, uint16_t address, |
| 406 | uint8_t* toSlaveBuf, uint8_t toSlaveBufLen, |
| 407 | uint8_t* fromSlaveBuf, |
| 408 | uint8_t fromSlaveBufLen) |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 409 | { |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 410 | if (toSlaveBuf == NULL || toSlaveBufLen == 0 || fromSlaveBuf == NULL || |
| 411 | fromSlaveBufLen == 0) |
| 412 | { |
| 413 | return -1; |
| 414 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 415 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 416 | #define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2 |
| 417 | struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT]; |
| 418 | struct i2c_rdwr_ioctl_data rdwr; |
| 419 | |
| 420 | msgs[0].addr = address; |
| 421 | msgs[0].flags = 0; |
| 422 | msgs[0].len = toSlaveBufLen; |
| 423 | msgs[0].buf = toSlaveBuf; |
| 424 | msgs[1].addr = address; |
| 425 | msgs[1].flags = I2C_M_RD; |
| 426 | msgs[1].len = fromSlaveBufLen; |
| 427 | msgs[1].buf = fromSlaveBuf; |
| 428 | |
| 429 | rdwr.msgs = msgs; |
| 430 | rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT; |
| 431 | |
| 432 | int ret = ioctl(file, I2C_RDWR, &rdwr); |
| 433 | |
| 434 | return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? ret : -1; |
| 435 | } |
| 436 | |
| 437 | static int64_t readBlockData(int flag, int file, uint16_t address, |
| 438 | uint16_t offset, uint8_t len, uint8_t* buf) |
| 439 | { |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 440 | if (flag == 0) |
| 441 | { |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 442 | return i2c_smbus_read_i2c_block_data(file, static_cast<uint8_t>(offset), |
| 443 | len, buf); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 446 | offset = htobe16(offset); |
| 447 | return i2c_smbus_write_then_read( |
| 448 | file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len); |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 449 | } |
| 450 | |
James Feist | 24bae7a | 2019-04-03 09:50:56 -0700 | [diff] [blame] | 451 | bool validateHeader(const std::array<uint8_t, I2C_SMBUS_BLOCK_MAX>& blockData) |
| 452 | { |
| 453 | // ipmi spec format version number is currently at 1, verify it |
| 454 | if (blockData[0] != 0x1) |
| 455 | { |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | // verify pad is set to 0 |
| 460 | if (blockData[6] != 0x0) |
| 461 | { |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | // verify offsets are 0, or don't point to another offset |
| 466 | std::set<uint8_t> foundOffsets; |
| 467 | for (int ii = 1; ii < 6; ii++) |
| 468 | { |
| 469 | if (blockData[ii] == 0) |
| 470 | { |
| 471 | continue; |
| 472 | } |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 473 | auto inserted = foundOffsets.insert(blockData[ii]); |
| 474 | if (!inserted.second) |
James Feist | 24bae7a | 2019-04-03 09:50:56 -0700 | [diff] [blame] | 475 | { |
| 476 | return false; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | // validate checksum |
| 481 | size_t sum = 0; |
| 482 | for (int jj = 0; jj < 7; jj++) |
| 483 | { |
| 484 | sum += blockData[jj]; |
| 485 | } |
| 486 | sum = (256 - sum) & 0xFF; |
| 487 | |
| 488 | if (sum != blockData[7]) |
| 489 | { |
| 490 | return false; |
| 491 | } |
| 492 | return true; |
| 493 | } |
| 494 | |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 495 | // TODO: This code is very similar to the non-eeprom version and can be merged |
| 496 | // with some tweaks. |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 497 | static std::vector<uint8_t> processEeprom(int bus, int address) |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 498 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 499 | auto path = getEepromPath(bus, address); |
| 500 | |
| 501 | int file = open(path.c_str(), O_RDONLY); |
| 502 | if (file < 0) |
| 503 | { |
| 504 | std::cerr << "Unable to open eeprom file: " << path << "\n"; |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 505 | return {}; |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 508 | std::string errorMessage = "eeprom at " + std::to_string(bus) + |
| 509 | " address " + std::to_string(address); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 510 | std::vector<uint8_t> device = readFRUContents( |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 511 | 0, file, static_cast<uint16_t>(address), readFromEeprom, errorMessage); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 512 | |
| 513 | close(file); |
| 514 | return device; |
| 515 | } |
| 516 | |
| 517 | std::set<int> findI2CEeproms(int i2cBus, std::shared_ptr<DeviceMap> devices) |
| 518 | { |
| 519 | std::set<int> foundList; |
| 520 | |
| 521 | std::string path = "/sys/bus/i2c/devices/i2c-" + std::to_string(i2cBus); |
| 522 | |
| 523 | // For each file listed under the i2c device |
| 524 | // NOTE: This should be faster than just checking for each possible address |
| 525 | // path. |
| 526 | for (const auto& p : fs::directory_iterator(path)) |
| 527 | { |
| 528 | const std::string node = p.path().string(); |
| 529 | std::smatch m; |
| 530 | bool found = |
| 531 | std::regex_match(node, m, std::regex(".+\\d+-([0-9abcdef]+$)")); |
| 532 | |
| 533 | if (!found) |
| 534 | { |
| 535 | continue; |
| 536 | } |
| 537 | if (m.size() != 2) |
| 538 | { |
| 539 | std::cerr << "regex didn't capture\n"; |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | std::ssub_match subMatch = m[1]; |
| 544 | std::string addressString = subMatch.str(); |
| 545 | |
| 546 | std::size_t ignored; |
| 547 | const int hexBase = 16; |
| 548 | int address = std::stoi(addressString, &ignored, hexBase); |
| 549 | |
| 550 | const std::string eeprom = node + "/eeprom"; |
| 551 | |
| 552 | try |
| 553 | { |
| 554 | if (!fs::exists(eeprom)) |
| 555 | { |
| 556 | continue; |
| 557 | } |
| 558 | } |
| 559 | catch (...) |
| 560 | { |
| 561 | continue; |
| 562 | } |
| 563 | |
| 564 | // There is an eeprom file at this address, it may have invalid |
| 565 | // contents, but we found it. |
| 566 | foundList.insert(address); |
| 567 | |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 568 | std::vector<uint8_t> device = processEeprom(i2cBus, address); |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 569 | if (!device.empty()) |
| 570 | { |
| 571 | devices->emplace(address, device); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | return foundList; |
| 576 | } |
| 577 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 578 | int getBusFRUs(int file, int first, int last, int bus, |
Patrick Venture | 4f47fe6 | 2019-08-08 16:30:38 -0700 | [diff] [blame] | 579 | std::shared_ptr<DeviceMap> devices) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 580 | { |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 581 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 582 | std::future<int> future = std::async(std::launch::async, [&]() { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 583 | // NOTE: When reading the devices raw on the bus, it can interfere with |
| 584 | // the driver's ability to operate, therefore read eeproms first before |
| 585 | // scanning for devices without drivers. Several experiments were run |
| 586 | // and it was determined that if there were any devices on the bus |
| 587 | // before the eeprom was hit and read, the eeprom driver wouldn't open |
| 588 | // while the bus device was open. An experiment was not performed to see |
| 589 | // if this issue was resolved if the i2c bus device was closed, but |
| 590 | // hexdumps of the eeprom later were successful. |
| 591 | |
| 592 | // Scan for i2c eeproms loaded on this bus. |
| 593 | std::set<int> skipList = findI2CEeproms(bus, devices); |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 594 | std::set<size_t>& failedItems = failedAddresses[bus]; |
| 595 | |
| 596 | std::set<size_t>* rootFailures = nullptr; |
| 597 | int rootBus = getRootBus(bus); |
| 598 | |
| 599 | if (rootBus >= 0) |
| 600 | { |
| 601 | rootFailures = &(failedAddresses[rootBus]); |
| 602 | } |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 603 | |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 604 | constexpr int startSkipSlaveAddr = 0; |
| 605 | constexpr int endSkipSlaveAddr = 12; |
| 606 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 607 | for (int ii = first; ii <= last; ii++) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 608 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 609 | if (skipList.find(ii) != skipList.end()) |
| 610 | { |
| 611 | continue; |
| 612 | } |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 613 | // skipping since no device is present in this range |
| 614 | if (ii >= startSkipSlaveAddr && ii <= endSkipSlaveAddr) |
| 615 | { |
| 616 | continue; |
| 617 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 618 | // Set slave address |
Rajashekar Gade Reddy | fa8d322 | 2020-05-13 08:27:03 +0530 | [diff] [blame] | 619 | if (ioctl(file, I2C_SLAVE, ii) < 0) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 620 | { |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 621 | std::cerr << "device at bus " << bus << " address " << ii |
Patrick Venture | 5d8b61d | 2019-08-06 12:36:10 -0700 | [diff] [blame] | 622 | << " busy\n"; |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 623 | continue; |
| 624 | } |
| 625 | // probe |
| 626 | else if (i2c_smbus_read_byte(file) < 0) |
| 627 | { |
| 628 | continue; |
| 629 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 630 | |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 631 | if (DEBUG) |
| 632 | { |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 633 | std::cout << "something at bus " << bus << " addr " << ii |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 634 | << "\n"; |
| 635 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 636 | |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 637 | makeProbeInterface(bus, ii); |
| 638 | |
James Feist | 7972bb9 | 2019-11-13 15:59:24 -0800 | [diff] [blame] | 639 | if (failedItems.find(ii) != failedItems.end()) |
| 640 | { |
| 641 | // if we failed to read it once, unlikely we can read it later |
| 642 | continue; |
| 643 | } |
| 644 | |
| 645 | if (rootFailures != nullptr) |
| 646 | { |
| 647 | if (rootFailures->find(ii) != rootFailures->end()) |
| 648 | { |
| 649 | continue; |
| 650 | } |
| 651 | } |
| 652 | |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 653 | /* Check for Device type if it is 8 bit or 16 bit */ |
| 654 | int flag = isDevice16Bit(file); |
| 655 | if (flag < 0) |
| 656 | { |
| 657 | std::cerr << "failed to read bus " << bus << " address " << ii |
| 658 | << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 659 | if (powerIsOn) |
| 660 | { |
| 661 | failedItems.insert(ii); |
| 662 | } |
Vijay Khemka | 2d681f6 | 2018-11-06 15:51:00 -0800 | [diff] [blame] | 663 | continue; |
| 664 | } |
| 665 | |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 666 | std::string errorMessage = |
| 667 | "bus " + std::to_string(bus) + " address " + std::to_string(ii); |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 668 | std::vector<uint8_t> device = |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 669 | readFRUContents(flag, file, static_cast<uint16_t>(ii), |
Xiang Liu | 2801a70 | 2020-01-20 14:29:34 -0800 | [diff] [blame] | 670 | readBlockData, errorMessage); |
Patrick Venture | baba767 | 2019-10-26 09:26:41 -0700 | [diff] [blame] | 671 | if (device.empty()) |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 672 | { |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 673 | continue; |
| 674 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 675 | |
Patrick Venture | 786f179 | 2019-08-05 16:33:44 -0700 | [diff] [blame] | 676 | devices->emplace(ii, device); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 677 | } |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 678 | return 1; |
| 679 | }); |
| 680 | std::future_status status = |
| 681 | future.wait_for(std::chrono::seconds(busTimeoutSeconds)); |
| 682 | if (status == std::future_status::timeout) |
| 683 | { |
| 684 | std::cerr << "Error reading bus " << bus << "\n"; |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 685 | if (powerIsOn) |
| 686 | { |
| 687 | busBlacklist.insert(bus); |
| 688 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 689 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 690 | return -1; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 691 | } |
| 692 | |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 693 | close(file); |
James Feist | 26c27ad | 2018-07-25 15:09:40 -0700 | [diff] [blame] | 694 | return future.get(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 697 | void loadBlacklist(const char* path) |
| 698 | { |
| 699 | std::ifstream blacklistStream(path); |
| 700 | if (!blacklistStream.good()) |
| 701 | { |
| 702 | // File is optional. |
| 703 | std::cerr << "Cannot open blacklist file.\n\n"; |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | nlohmann::json data = |
| 708 | nlohmann::json::parse(blacklistStream, nullptr, false); |
| 709 | if (data.is_discarded()) |
| 710 | { |
| 711 | std::cerr << "Illegal blacklist file detected, cannot validate JSON, " |
| 712 | "exiting\n"; |
| 713 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | // It's expected to have at least one field, "buses" that is an array of the |
| 717 | // buses by integer. Allow for future options to exclude further aspects, |
| 718 | // such as specific addresses or ranges. |
| 719 | if (data.type() != nlohmann::json::value_t::object) |
| 720 | { |
| 721 | std::cerr << "Illegal blacklist, expected to read dictionary\n"; |
| 722 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | // If buses field is missing, that's fine. |
| 726 | if (data.count("buses") == 1) |
| 727 | { |
| 728 | // Parse the buses array after a little validation. |
| 729 | auto buses = data.at("buses"); |
| 730 | if (buses.type() != nlohmann::json::value_t::array) |
| 731 | { |
| 732 | // Buses field present but invalid, therefore this is an error. |
| 733 | std::cerr << "Invalid contents for blacklist buses field\n"; |
| 734 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | // Catch exception here for type mis-match. |
| 738 | try |
| 739 | { |
| 740 | for (const auto& bus : buses) |
| 741 | { |
| 742 | busBlacklist.insert(bus.get<size_t>()); |
| 743 | } |
| 744 | } |
| 745 | catch (const nlohmann::detail::type_error& e) |
| 746 | { |
| 747 | // Type mis-match is a critical error. |
| 748 | std::cerr << "Invalid bus type: " << e.what() << "\n"; |
| 749 | std::exit(EXIT_FAILURE); |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
| 753 | return; |
| 754 | } |
| 755 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 756 | static void FindI2CDevices(const std::vector<fs::path>& i2cBuses, |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 757 | BusMap& busmap) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 758 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 759 | for (auto& i2cBus : i2cBuses) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 760 | { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 761 | int bus = busStrToInt(i2cBus); |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 762 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 763 | if (bus < 0) |
| 764 | { |
| 765 | std::cerr << "Cannot translate " << i2cBus << " to int\n"; |
| 766 | continue; |
| 767 | } |
James Feist | 444830e | 2019-04-05 08:38:16 -0700 | [diff] [blame] | 768 | if (busBlacklist.find(bus) != busBlacklist.end()) |
| 769 | { |
| 770 | continue; // skip previously failed busses |
| 771 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 772 | |
James Feist | 0c3980a | 2019-12-19 11:09:27 -0800 | [diff] [blame] | 773 | int rootBus = getRootBus(bus); |
| 774 | if (busBlacklist.find(rootBus) != busBlacklist.end()) |
| 775 | { |
| 776 | continue; |
| 777 | } |
| 778 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 779 | auto file = open(i2cBus.c_str(), O_RDWR); |
| 780 | if (file < 0) |
| 781 | { |
| 782 | std::cerr << "unable to open i2c device " << i2cBus.string() |
| 783 | << "\n"; |
| 784 | continue; |
| 785 | } |
| 786 | unsigned long funcs = 0; |
| 787 | |
| 788 | if (ioctl(file, I2C_FUNCS, &funcs) < 0) |
| 789 | { |
| 790 | std::cerr |
Patrick Venture | 98e0cf3 | 2019-08-02 11:11:03 -0700 | [diff] [blame] | 791 | << "Error: Could not get the adapter functionality matrix bus " |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 792 | << bus << "\n"; |
| 793 | continue; |
| 794 | } |
| 795 | if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) || |
| 796 | !(I2C_FUNC_SMBUS_READ_I2C_BLOCK)) |
| 797 | { |
| 798 | std::cerr << "Error: Can't use SMBus Receive Byte command bus " |
| 799 | << bus << "\n"; |
| 800 | continue; |
| 801 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 802 | auto& device = busmap[bus]; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 803 | device = std::make_shared<DeviceMap>(); |
| 804 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 805 | // i2cdetect by default uses the range 0x03 to 0x77, as |
| 806 | // this is what we have tested with, use this range. Could be |
| 807 | // changed in future. |
| 808 | if (DEBUG) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 809 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 810 | std::cerr << "Scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 811 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 812 | |
| 813 | // fd is closed in this function in case the bus locks up |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 814 | getBusFRUs(file, 0x03, 0x77, bus, device); |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 815 | |
| 816 | if (DEBUG) |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 817 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 818 | std::cerr << "Done scanning bus " << bus << "\n"; |
James Feist | c95cb14 | 2018-02-26 10:41:42 -0800 | [diff] [blame] | 819 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 820 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 821 | } |
| 822 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 823 | // this class allows an async response after all i2c devices are discovered |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 824 | struct FindDevicesWithCallback : |
| 825 | std::enable_shared_from_this<FindDevicesWithCallback> |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 826 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 827 | FindDevicesWithCallback(const std::vector<fs::path>& i2cBuses, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 828 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 829 | std::function<void(void)>&& callback) : |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 830 | _i2cBuses(i2cBuses), |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 831 | _busMap(busmap), _callback(std::move(callback)) |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 832 | {} |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 833 | ~FindDevicesWithCallback() |
| 834 | { |
| 835 | _callback(); |
| 836 | } |
| 837 | void run() |
| 838 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 839 | FindI2CDevices(_i2cBuses, _busMap); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 840 | } |
| 841 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 842 | const std::vector<fs::path>& _i2cBuses; |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 843 | BusMap& _busMap; |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 844 | std::function<void(void)> _callback; |
| 845 | }; |
| 846 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 847 | static const std::tm intelEpoch(void) |
| 848 | { |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 849 | std::tm val = {}; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 850 | val.tm_year = 1996 - 1900; |
| 851 | return val; |
| 852 | } |
| 853 | |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 854 | static char sixBitToChar(uint8_t val) |
| 855 | { |
| 856 | return static_cast<char>((val & 0x3f) + ' '); |
| 857 | } |
| 858 | |
| 859 | /* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */ |
| 860 | static const char bcdHighChars[] = { |
| 861 | ' ', '-', '.', 'X', 'X', 'X', |
| 862 | }; |
| 863 | |
| 864 | static char bcdPlusToChar(uint8_t val) |
| 865 | { |
| 866 | val &= 0xf; |
| 867 | return (val < 10) ? static_cast<char>(val + '0') : bcdHighChars[val - 10]; |
| 868 | } |
| 869 | |
| 870 | enum class DecodeState |
| 871 | { |
| 872 | ok, |
| 873 | end, |
| 874 | err, |
| 875 | }; |
| 876 | |
| 877 | enum FRUDataEncoding |
| 878 | { |
| 879 | binary = 0x0, |
| 880 | bcdPlus = 0x1, |
| 881 | sixBitASCII = 0x2, |
| 882 | languageDependent = 0x3, |
| 883 | }; |
| 884 | |
| 885 | /* Decode FRU data into a std::string, given an input iterator and end. If the |
| 886 | * state returned is fruDataOk, then the resulting string is the decoded FRU |
| 887 | * data. The input iterator is advanced past the data consumed. |
| 888 | * |
| 889 | * On fruDataErr, we have lost synchronisation with the length bytes, so the |
| 890 | * iterator is no longer usable. |
| 891 | */ |
| 892 | static std::pair<DecodeState, std::string> |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 893 | decodeFRUData(std::vector<uint8_t>::const_iterator& iter, |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 894 | const std::vector<uint8_t>::const_iterator& end) |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 895 | { |
| 896 | std::string value; |
| 897 | unsigned int i; |
| 898 | |
| 899 | /* we need at least one byte to decode the type/len header */ |
| 900 | if (iter == end) |
| 901 | { |
| 902 | std::cerr << "Truncated FRU data\n"; |
| 903 | return make_pair(DecodeState::err, value); |
| 904 | } |
| 905 | |
| 906 | uint8_t c = *(iter++); |
| 907 | |
| 908 | /* 0xc1 is the end marker */ |
| 909 | if (c == 0xc1) |
| 910 | { |
| 911 | return make_pair(DecodeState::end, value); |
| 912 | } |
| 913 | |
| 914 | /* decode type/len byte */ |
| 915 | uint8_t type = static_cast<uint8_t>(c >> 6); |
| 916 | uint8_t len = static_cast<uint8_t>(c & 0x3f); |
| 917 | |
| 918 | /* we should have at least len bytes of data available overall */ |
| 919 | if (iter + len > end) |
| 920 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 921 | std::cerr << "FRU data field extends past end of FRU area data\n"; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 922 | return make_pair(DecodeState::err, value); |
| 923 | } |
| 924 | |
| 925 | switch (type) |
| 926 | { |
| 927 | case FRUDataEncoding::binary: |
Andrei Kartashev | c994c02 | 2020-08-10 18:51:49 +0300 | [diff] [blame] | 928 | { |
| 929 | std::stringstream ss; |
| 930 | ss << std::hex << std::setfill('0'); |
| 931 | for (i = 0; i < len; i++, iter++) |
| 932 | { |
| 933 | uint8_t val = static_cast<uint8_t>(*iter); |
| 934 | ss << std::setw(2) << static_cast<int>(val); |
| 935 | } |
| 936 | value = ss.str(); |
| 937 | break; |
| 938 | } |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 939 | case FRUDataEncoding::languageDependent: |
| 940 | /* For language-code dependent encodings, assume 8-bit ASCII */ |
| 941 | value = std::string(iter, iter + len); |
| 942 | iter += len; |
| 943 | break; |
| 944 | |
| 945 | case FRUDataEncoding::bcdPlus: |
| 946 | value = std::string(); |
| 947 | for (i = 0; i < len; i++, iter++) |
| 948 | { |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 949 | uint8_t val = *iter; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 950 | value.push_back(bcdPlusToChar(val >> 4)); |
| 951 | value.push_back(bcdPlusToChar(val & 0xf)); |
| 952 | } |
| 953 | break; |
| 954 | |
| 955 | case FRUDataEncoding::sixBitASCII: |
| 956 | { |
Andrei Kartashev | e707de6 | 2020-08-10 18:33:29 +0300 | [diff] [blame] | 957 | unsigned int accum = 0; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 958 | unsigned int accumBitLen = 0; |
| 959 | value = std::string(); |
| 960 | for (i = 0; i < len; i++, iter++) |
| 961 | { |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 962 | accum |= *iter << accumBitLen; |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 963 | accumBitLen += 8; |
| 964 | while (accumBitLen >= 6) |
| 965 | { |
| 966 | value.push_back(sixBitToChar(accum & 0x3f)); |
| 967 | accum >>= 6; |
| 968 | accumBitLen -= 6; |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | break; |
| 973 | } |
| 974 | |
| 975 | return make_pair(DecodeState::ok, value); |
| 976 | } |
| 977 | |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 978 | static void checkLang(uint8_t lang) |
| 979 | { |
| 980 | // If Lang is not English then the encoding is defined as 2-byte UNICODE, |
| 981 | // but we don't support that. |
| 982 | if (lang && lang != 25) |
| 983 | { |
| 984 | std::cerr << "Warning: language other then English is not " |
| 985 | "supported \n"; |
| 986 | } |
| 987 | } |
| 988 | |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 989 | resCodes formatFRU(const std::vector<uint8_t>& fruBytes, |
| 990 | boost::container::flat_map<std::string, std::string>& result) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 991 | { |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 992 | resCodes ret = resCodes::resOK; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 993 | if (fruBytes.size() <= fruBlockSize) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 994 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 995 | std::cerr << "Error: trying to parse empty FRU \n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 996 | return resCodes::resErr; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 997 | } |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 998 | result["Common_Format_Version"] = |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 999 | std::to_string(static_cast<int>(*fruBytes.begin())); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1000 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1001 | const std::vector<std::string>* fruAreaFieldNames; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1002 | |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1003 | // Don't parse Internal and Multirecord areas |
| 1004 | for (fruAreas area = fruAreas::fruAreaChassis; |
| 1005 | area <= fruAreas::fruAreaProduct; ++area) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1006 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1007 | |
| 1008 | size_t offset = *(fruBytes.begin() + getHeaderAreaFieldOffset(area)); |
| 1009 | if (offset == 0) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1010 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1011 | continue; |
| 1012 | } |
| 1013 | offset *= fruBlockSize; |
| 1014 | std::vector<uint8_t>::const_iterator fruBytesIter = |
| 1015 | fruBytes.begin() + offset; |
| 1016 | if (fruBytesIter + fruBlockSize >= fruBytes.end()) |
| 1017 | { |
| 1018 | std::cerr << "Not enough data to parse \n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1019 | return resCodes::resErr; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1020 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1021 | // check for format version 1 |
| 1022 | if (*fruBytesIter != 0x01) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1023 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1024 | std::cerr << "Unexpected version " << *fruBytesIter << "\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1025 | return resCodes::resErr; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1026 | } |
| 1027 | ++fruBytesIter; |
| 1028 | uint8_t fruAreaSize = *fruBytesIter * fruBlockSize; |
| 1029 | std::vector<uint8_t>::const_iterator fruBytesIterEndArea = |
| 1030 | fruBytes.begin() + offset + fruAreaSize - 1; |
| 1031 | ++fruBytesIter; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1032 | |
Andrei Kartashev | 272bafd | 2020-10-09 12:05:32 +0300 | [diff] [blame] | 1033 | uint8_t fruComputedChecksum = |
| 1034 | calculateChecksum(fruBytes.begin() + offset, fruBytesIterEndArea); |
| 1035 | if (fruComputedChecksum != *fruBytesIterEndArea) |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1036 | { |
Andrei Kartashev | 272bafd | 2020-10-09 12:05:32 +0300 | [diff] [blame] | 1037 | std::stringstream ss; |
| 1038 | ss << std::hex << std::setfill('0'); |
| 1039 | ss << "Checksum error in FRU area " << getFruAreaName(area) << "\n"; |
| 1040 | ss << "\tComputed checksum: 0x" << std::setw(2) |
| 1041 | << static_cast<int>(fruComputedChecksum) << "\n"; |
| 1042 | ss << "\tThe read checksum: 0x" << std::setw(2) |
| 1043 | << static_cast<int>(*fruBytesIterEndArea) << "\n"; |
| 1044 | std::cerr << ss.str(); |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1045 | ret = resCodes::resWarn; |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1046 | } |
| 1047 | |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1048 | switch (area) |
| 1049 | { |
| 1050 | case fruAreas::fruAreaChassis: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1051 | { |
| 1052 | result["CHASSIS_TYPE"] = |
| 1053 | std::to_string(static_cast<int>(*fruBytesIter)); |
| 1054 | fruBytesIter += 1; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1055 | fruAreaFieldNames = &CHASSIS_FRU_AREAS; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1056 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1057 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1058 | case fruAreas::fruAreaBoard: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1059 | { |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1060 | uint8_t lang = *fruBytesIter; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1061 | result["BOARD_LANGUAGE_CODE"] = |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1062 | std::to_string(static_cast<int>(lang)); |
| 1063 | checkLang(lang); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1064 | fruBytesIter += 1; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1065 | |
| 1066 | unsigned int minutes = *fruBytesIter | |
| 1067 | *(fruBytesIter + 1) << 8 | |
| 1068 | *(fruBytesIter + 2) << 16; |
| 1069 | std::tm fruTime = intelEpoch(); |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 1070 | std::time_t timeValue = std::mktime(&fruTime); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1071 | timeValue += minutes * 60; |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 1072 | fruTime = *std::gmtime(&timeValue); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1073 | |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 1074 | // Tue Nov 20 23:08:00 2018 |
| 1075 | char timeString[32] = {0}; |
| 1076 | auto bytes = std::strftime(timeString, sizeof(timeString), |
Patrick Venture | fff050a | 2019-08-13 11:44:30 -0700 | [diff] [blame] | 1077 | "%Y-%m-%d - %H:%M:%S", &fruTime); |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 1078 | if (bytes == 0) |
| 1079 | { |
| 1080 | std::cerr << "invalid time string encountered\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1081 | return resCodes::resErr; |
Patrick Venture | e0e6f5f | 2019-08-12 19:00:11 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | result["BOARD_MANUFACTURE_DATE"] = std::string(timeString); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1085 | fruBytesIter += 3; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1086 | fruAreaFieldNames = &BOARD_FRU_AREAS; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1087 | break; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1088 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1089 | case fruAreas::fruAreaProduct: |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1090 | { |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1091 | uint8_t lang = *fruBytesIter; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1092 | result["PRODUCT_LANGUAGE_CODE"] = |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1093 | std::to_string(static_cast<int>(lang)); |
| 1094 | checkLang(lang); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1095 | fruBytesIter += 1; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1096 | fruAreaFieldNames = &PRODUCT_FRU_AREAS; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1097 | break; |
| 1098 | } |
| 1099 | default: |
| 1100 | { |
| 1101 | std::cerr << "Internal error: unexpected FRU area index: " |
| 1102 | << static_cast<int>(area) << " \n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1103 | return resCodes::resErr; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | size_t fieldIndex = 0; |
| 1107 | DecodeState state; |
| 1108 | do |
| 1109 | { |
| 1110 | auto res = decodeFRUData(fruBytesIter, fruBytesIterEndArea); |
| 1111 | state = res.first; |
| 1112 | std::string value = res.second; |
| 1113 | std::string name; |
| 1114 | if (fieldIndex < fruAreaFieldNames->size()) |
| 1115 | { |
| 1116 | name = std::string(getFruAreaName(area)) + "_" + |
| 1117 | fruAreaFieldNames->at(fieldIndex); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1118 | } |
| 1119 | else |
| 1120 | { |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1121 | name = |
| 1122 | std::string(getFruAreaName(area)) + "_" + |
| 1123 | FRU_CUSTOM_FIELD_NAME + |
| 1124 | std::to_string(fieldIndex - fruAreaFieldNames->size() + 1); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1125 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1126 | |
| 1127 | if (state == DecodeState::ok) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1128 | { |
Ed Tanous | 2147e67 | 2019-02-27 13:59:56 -0800 | [diff] [blame] | 1129 | // Strip non null characters from the end |
| 1130 | value.erase(std::find_if(value.rbegin(), value.rend(), |
| 1131 | [](char ch) { return ch != 0; }) |
| 1132 | .base(), |
| 1133 | value.end()); |
| 1134 | |
Jeremy Kerr | 4e6a62f | 2020-06-09 11:26:40 +0800 | [diff] [blame] | 1135 | result[name] = std::move(value); |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1136 | ++fieldIndex; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1137 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1138 | else if (state == DecodeState::err) |
| 1139 | { |
| 1140 | std::cerr << "Error while parsing " << name << "\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1141 | ret = resCodes::resWarn; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1142 | // Cancel decoding if failed to parse any of mandatory |
| 1143 | // fields |
| 1144 | if (fieldIndex < fruAreaFieldNames->size()) |
| 1145 | { |
| 1146 | std::cerr << "Failed to parse mandatory field \n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1147 | return resCodes::resErr; |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1148 | } |
| 1149 | } |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1150 | else |
| 1151 | { |
| 1152 | if (fieldIndex < fruAreaFieldNames->size()) |
| 1153 | { |
| 1154 | std::cerr << "Mandatory fields absent in FRU area " |
| 1155 | << getFruAreaName(area) << " after " << name |
| 1156 | << "\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1157 | ret = resCodes::resWarn; |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1158 | } |
| 1159 | } |
Andrei Kartashev | d7b6659 | 2020-08-13 15:33:51 +0300 | [diff] [blame] | 1160 | } while (state == DecodeState::ok); |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1161 | for (; fruBytesIter < fruBytesIterEndArea; fruBytesIter++) |
| 1162 | { |
| 1163 | uint8_t c = *fruBytesIter; |
| 1164 | if (c) |
| 1165 | { |
| 1166 | std::cerr << "Non-zero byte after EndOfFields in FRU area " |
| 1167 | << getFruAreaName(area) << "\n"; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1168 | ret = resCodes::resWarn; |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1169 | break; |
| 1170 | } |
| 1171 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1172 | } |
| 1173 | |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1174 | return ret; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1177 | std::vector<uint8_t>& getFRUInfo(const uint8_t& bus, const uint8_t& address) |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1178 | { |
| 1179 | auto deviceMap = busMap.find(bus); |
| 1180 | if (deviceMap == busMap.end()) |
| 1181 | { |
| 1182 | throw std::invalid_argument("Invalid Bus."); |
| 1183 | } |
| 1184 | auto device = deviceMap->second->find(address); |
| 1185 | if (device == deviceMap->second->end()) |
| 1186 | { |
| 1187 | throw std::invalid_argument("Invalid Address."); |
| 1188 | } |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1189 | std::vector<uint8_t>& ret = device->second; |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1190 | |
| 1191 | return ret; |
| 1192 | } |
| 1193 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1194 | void AddFRUObjectToDbus( |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1195 | std::vector<uint8_t>& device, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1196 | boost::container::flat_map< |
| 1197 | std::pair<size_t, size_t>, |
| 1198 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 1199 | uint32_t bus, uint32_t address) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1200 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1201 | boost::container::flat_map<std::string, std::string> formattedFRU; |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1202 | resCodes res = formatFRU(device, formattedFRU); |
| 1203 | if (res == resCodes::resErr) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1204 | { |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1205 | std::cerr << "failed to parse FRU for device at bus " << bus |
Patrick Venture | b755c83 | 2019-08-07 11:09:14 -0700 | [diff] [blame] | 1206 | << " address " << address << "\n"; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1207 | return; |
| 1208 | } |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1209 | else if (res == resCodes::resWarn) |
| 1210 | { |
| 1211 | std::cerr << "there were warnings while parsing FRU for device at bus " |
| 1212 | << bus << " address " << address << "\n"; |
| 1213 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1214 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1215 | auto productNameFind = formattedFRU.find("BOARD_PRODUCT_NAME"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1216 | std::string productName; |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1217 | // Not found under Board section or an empty string. |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1218 | if (productNameFind == formattedFRU.end() || |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1219 | productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1220 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1221 | productNameFind = formattedFRU.find("PRODUCT_PRODUCT_NAME"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1222 | } |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1223 | // Found under Product section and not an empty string. |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1224 | if (productNameFind != formattedFRU.end() && |
Patrick Venture | 96cdaef | 2019-07-30 13:30:52 -0700 | [diff] [blame] | 1225 | !productNameFind->second.empty()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1226 | { |
| 1227 | productName = productNameFind->second; |
James Feist | 3f8a278 | 2018-02-12 09:24:42 -0800 | [diff] [blame] | 1228 | std::regex illegalObject("[^A-Za-z0-9_]"); |
| 1229 | productName = std::regex_replace(productName, illegalObject, "_"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1230 | } |
| 1231 | else |
| 1232 | { |
| 1233 | productName = "UNKNOWN" + std::to_string(UNKNOWN_BUS_OBJECT_COUNT); |
| 1234 | UNKNOWN_BUS_OBJECT_COUNT++; |
| 1235 | } |
| 1236 | |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1237 | productName = "/xyz/openbmc_project/FruDevice/" + productName; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1238 | // avoid duplicates by checking to see if on a mux |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1239 | if (bus > 0) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1240 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1241 | int highest = -1; |
| 1242 | bool found = false; |
| 1243 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1244 | for (auto const& busIface : dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1245 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1246 | std::string path = busIface.second->get_object_path(); |
| 1247 | if (std::regex_match(path, std::regex(productName + "(_\\d+|)$"))) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1248 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1249 | if (isMuxBus(bus) && address == busIface.first.second && |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1250 | (getFRUInfo(static_cast<uint8_t>(busIface.first.first), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1251 | static_cast<uint8_t>(busIface.first.second)) == |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1252 | getFRUInfo(static_cast<uint8_t>(bus), |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1253 | static_cast<uint8_t>(address)))) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1254 | { |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1255 | // This device is already added to the lower numbered bus, |
| 1256 | // do not replicate it. |
| 1257 | return; |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1258 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1259 | |
| 1260 | // Check if the match named has extra information. |
| 1261 | found = true; |
| 1262 | std::smatch base_match; |
| 1263 | |
| 1264 | bool match = std::regex_match( |
| 1265 | path, base_match, std::regex(productName + "_(\\d+)$")); |
| 1266 | if (match) |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1267 | { |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1268 | if (base_match.size() == 2) |
| 1269 | { |
| 1270 | std::ssub_match base_sub_match = base_match[1]; |
| 1271 | std::string base = base_sub_match.str(); |
| 1272 | |
| 1273 | int value = std::stoi(base); |
| 1274 | highest = (value > highest) ? value : highest; |
| 1275 | } |
James Feist | 79e9c0b | 2018-03-15 15:21:17 -0700 | [diff] [blame] | 1276 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1277 | } |
Patrick Venture | 015fb0a | 2019-08-16 09:33:31 -0700 | [diff] [blame] | 1278 | } // end searching objects |
| 1279 | |
| 1280 | if (found) |
| 1281 | { |
| 1282 | // We found something with the same name. If highest was still -1, |
| 1283 | // it means this new entry will be _0. |
| 1284 | productName += "_"; |
| 1285 | productName += std::to_string(++highest); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1288 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1289 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1290 | objServer.add_interface(productName, "xyz.openbmc_project.FruDevice"); |
| 1291 | dbusInterfaceMap[std::pair<size_t, size_t>(bus, address)] = iface; |
| 1292 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1293 | for (auto& property : formattedFRU) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1294 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1295 | |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 1296 | std::regex_replace(property.second.begin(), property.second.begin(), |
| 1297 | property.second.end(), NON_ASCII_REGEX, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1298 | if (property.second.empty() && property.first != "PRODUCT_ASSET_TAG") |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1299 | { |
| 1300 | continue; |
| 1301 | } |
| 1302 | std::string key = |
| 1303 | std::regex_replace(property.first, NON_ASCII_REGEX, "_"); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1304 | |
| 1305 | if (property.first == "PRODUCT_ASSET_TAG") |
| 1306 | { |
| 1307 | std::string propertyName = property.first; |
| 1308 | iface->register_property( |
| 1309 | key, property.second + '\0', |
| 1310 | [bus, address, propertyName, |
| 1311 | &dbusInterfaceMap](const std::string& req, std::string& resp) { |
| 1312 | if (strcmp(req.c_str(), resp.c_str())) |
| 1313 | { |
| 1314 | // call the method which will update |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1315 | if (updateFRUProperty(req, bus, address, propertyName, |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1316 | dbusInterfaceMap)) |
| 1317 | { |
| 1318 | resp = req; |
| 1319 | } |
| 1320 | else |
| 1321 | { |
| 1322 | throw std::invalid_argument( |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1323 | "FRU property update failed."); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | return 1; |
| 1327 | }); |
| 1328 | } |
| 1329 | else if (!iface->register_property(key, property.second + '\0')) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1330 | { |
| 1331 | std::cerr << "illegal key: " << key << "\n"; |
| 1332 | } |
Jae Hyun Yoo | 3936e7a | 2018-03-23 17:26:16 -0700 | [diff] [blame] | 1333 | if (DEBUG) |
| 1334 | { |
| 1335 | std::cout << property.first << ": " << property.second << "\n"; |
| 1336 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1337 | } |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1338 | |
| 1339 | // baseboard will be 0, 0 |
James Feist | 13b86d6 | 2018-05-29 11:24:35 -0700 | [diff] [blame] | 1340 | iface->register_property("BUS", bus); |
| 1341 | iface->register_property("ADDRESS", address); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1342 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1343 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1346 | static bool readBaseboardFRU(std::vector<uint8_t>& baseboardFRU) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1347 | { |
| 1348 | // try to read baseboard fru from file |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1349 | std::ifstream baseboardFRUFile(BASEBOARD_FRU_LOCATION, std::ios::binary); |
| 1350 | if (baseboardFRUFile.good()) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1351 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1352 | baseboardFRUFile.seekg(0, std::ios_base::end); |
| 1353 | size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg()); |
| 1354 | baseboardFRU.resize(fileSize); |
| 1355 | baseboardFRUFile.seekg(0, std::ios_base::beg); |
| 1356 | baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()), |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1357 | fileSize); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1358 | } |
| 1359 | else |
| 1360 | { |
| 1361 | return false; |
| 1362 | } |
| 1363 | return true; |
| 1364 | } |
| 1365 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1366 | bool writeFRU(uint8_t bus, uint8_t address, const std::vector<uint8_t>& fru) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1367 | { |
| 1368 | boost::container::flat_map<std::string, std::string> tmp; |
| 1369 | if (fru.size() > MAX_FRU_SIZE) |
| 1370 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1371 | std::cerr << "Invalid fru.size() during writeFRU\n"; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1372 | return false; |
| 1373 | } |
| 1374 | // verify legal fru by running it through fru parsing logic |
Andrei Kartashev | b45324a | 2020-10-14 17:01:47 +0300 | [diff] [blame^] | 1375 | if (formatFRU(fru, tmp) != resCodes::resOK) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1376 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1377 | std::cerr << "Invalid fru format during writeFRU\n"; |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1378 | return false; |
| 1379 | } |
| 1380 | // baseboard fru |
| 1381 | if (bus == 0 && address == 0) |
| 1382 | { |
| 1383 | std::ofstream file(BASEBOARD_FRU_LOCATION, std::ios_base::binary); |
| 1384 | if (!file.good()) |
| 1385 | { |
| 1386 | std::cerr << "Error opening file " << BASEBOARD_FRU_LOCATION |
| 1387 | << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1388 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1389 | return false; |
| 1390 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1391 | file.write(reinterpret_cast<const char*>(fru.data()), fru.size()); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1392 | return file.good(); |
| 1393 | } |
| 1394 | else |
| 1395 | { |
Patrick Venture | c50e1ff | 2019-08-06 10:22:28 -0700 | [diff] [blame] | 1396 | if (hasEepromFile(bus, address)) |
| 1397 | { |
| 1398 | auto path = getEepromPath(bus, address); |
| 1399 | int eeprom = open(path.c_str(), O_RDWR | O_CLOEXEC); |
| 1400 | if (eeprom < 0) |
| 1401 | { |
| 1402 | std::cerr << "unable to open i2c device " << path << "\n"; |
| 1403 | throw DBusInternalError(); |
| 1404 | return false; |
| 1405 | } |
| 1406 | |
| 1407 | ssize_t writtenBytes = write(eeprom, fru.data(), fru.size()); |
| 1408 | if (writtenBytes < 0) |
| 1409 | { |
| 1410 | std::cerr << "unable to write to i2c device " << path << "\n"; |
| 1411 | close(eeprom); |
| 1412 | throw DBusInternalError(); |
| 1413 | return false; |
| 1414 | } |
| 1415 | |
| 1416 | close(eeprom); |
| 1417 | return true; |
| 1418 | } |
| 1419 | |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1420 | std::string i2cBus = "/dev/i2c-" + std::to_string(bus); |
| 1421 | |
| 1422 | int file = open(i2cBus.c_str(), O_RDWR | O_CLOEXEC); |
| 1423 | if (file < 0) |
| 1424 | { |
| 1425 | std::cerr << "unable to open i2c device " << i2cBus << "\n"; |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1426 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1427 | return false; |
| 1428 | } |
| 1429 | if (ioctl(file, I2C_SLAVE_FORCE, address) < 0) |
| 1430 | { |
| 1431 | std::cerr << "unable to set device address\n"; |
| 1432 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1433 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1434 | return false; |
| 1435 | } |
| 1436 | |
| 1437 | constexpr const size_t RETRY_MAX = 2; |
| 1438 | uint16_t index = 0; |
| 1439 | size_t retries = RETRY_MAX; |
| 1440 | while (index < fru.size()) |
| 1441 | { |
| 1442 | if ((index && ((index % (MAX_EEPROM_PAGE_INDEX + 1)) == 0)) && |
| 1443 | (retries == RETRY_MAX)) |
| 1444 | { |
| 1445 | // The 4K EEPROM only uses the A2 and A1 device address bits |
| 1446 | // with the third bit being a memory page address bit. |
| 1447 | if (ioctl(file, I2C_SLAVE_FORCE, ++address) < 0) |
| 1448 | { |
| 1449 | std::cerr << "unable to set device address\n"; |
| 1450 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1451 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1452 | return false; |
| 1453 | } |
| 1454 | } |
| 1455 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1456 | if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index), |
| 1457 | fru[index]) < 0) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1458 | { |
| 1459 | if (!retries--) |
| 1460 | { |
| 1461 | std::cerr << "error writing fru: " << strerror(errno) |
| 1462 | << "\n"; |
| 1463 | close(file); |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1464 | throw DBusInternalError(); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1465 | return false; |
| 1466 | } |
| 1467 | } |
| 1468 | else |
| 1469 | { |
| 1470 | retries = RETRY_MAX; |
| 1471 | index++; |
| 1472 | } |
| 1473 | // most eeproms require 5-10ms between writes |
| 1474 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
| 1475 | } |
| 1476 | close(file); |
| 1477 | return true; |
| 1478 | } |
| 1479 | } |
| 1480 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1481 | void rescanOneBus( |
| 1482 | BusMap& busmap, uint8_t busNum, |
| 1483 | boost::container::flat_map< |
| 1484 | std::pair<size_t, size_t>, |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1485 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap, |
| 1486 | bool dbusCall) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1487 | { |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1488 | for (auto& [pair, interface] : foundDevices) |
| 1489 | { |
| 1490 | if (pair.first == static_cast<size_t>(busNum)) |
| 1491 | { |
| 1492 | objServer.remove_interface(interface); |
| 1493 | foundDevices.erase(pair); |
| 1494 | } |
| 1495 | } |
| 1496 | |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1497 | fs::path busPath = fs::path("/dev/i2c-" + std::to_string(busNum)); |
| 1498 | if (!fs::exists(busPath)) |
| 1499 | { |
| 1500 | if (dbusCall) |
| 1501 | { |
| 1502 | std::cerr << "Unable to access i2c bus " << static_cast<int>(busNum) |
| 1503 | << "\n"; |
| 1504 | throw std::invalid_argument("Invalid Bus."); |
| 1505 | } |
| 1506 | return; |
| 1507 | } |
| 1508 | |
| 1509 | std::vector<fs::path> i2cBuses; |
| 1510 | i2cBuses.emplace_back(busPath); |
| 1511 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1512 | auto scan = std::make_shared<FindDevicesWithCallback>( |
| 1513 | i2cBuses, busmap, [busNum, &busmap, &dbusInterfaceMap]() { |
| 1514 | for (auto& busIface : dbusInterfaceMap) |
| 1515 | { |
| 1516 | if (busIface.first.first == static_cast<size_t>(busNum)) |
| 1517 | { |
| 1518 | objServer.remove_interface(busIface.second); |
| 1519 | } |
| 1520 | } |
Wludzik, Jozef | c1136b7 | 2020-06-24 11:58:32 +0200 | [diff] [blame] | 1521 | auto found = busmap.find(busNum); |
| 1522 | if (found == busmap.end() || found->second == nullptr) |
| 1523 | { |
| 1524 | return; |
| 1525 | } |
| 1526 | for (auto& device : *(found->second)) |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1527 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1528 | AddFRUObjectToDbus(device.second, dbusInterfaceMap, |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1529 | static_cast<uint32_t>(busNum), device.first); |
| 1530 | } |
| 1531 | }); |
| 1532 | scan->run(); |
| 1533 | } |
| 1534 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1535 | void rescanBusses( |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1536 | BusMap& busmap, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1537 | boost::container::flat_map< |
| 1538 | std::pair<size_t, size_t>, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1539 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1540 | { |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1541 | static boost::asio::deadline_timer timer(io); |
| 1542 | timer.expires_from_now(boost::posix_time::seconds(1)); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1543 | |
Gunnar Mills | 6f0ae94 | 2018-08-31 12:38:03 -0500 | [diff] [blame] | 1544 | // setup an async wait in case we get flooded with requests |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1545 | timer.async_wait([&](const boost::system::error_code&) { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1546 | auto devDir = fs::path("/dev/"); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1547 | std::vector<fs::path> i2cBuses; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1548 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1549 | boost::container::flat_map<size_t, fs::path> busPaths; |
| 1550 | if (!getI2cDevicePaths(devDir, busPaths)) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1551 | { |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1552 | std::cerr << "unable to find i2c devices\n"; |
| 1553 | return; |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1554 | } |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 1555 | |
| 1556 | for (auto busPath : busPaths) |
| 1557 | { |
| 1558 | i2cBuses.emplace_back(busPath.second); |
| 1559 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1560 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1561 | busmap.clear(); |
James Feist | 8a98392 | 2019-10-24 17:11:56 -0700 | [diff] [blame] | 1562 | for (auto& [pair, interface] : foundDevices) |
| 1563 | { |
| 1564 | objServer.remove_interface(interface); |
| 1565 | } |
| 1566 | foundDevices.clear(); |
| 1567 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1568 | auto scan = |
| 1569 | std::make_shared<FindDevicesWithCallback>(i2cBuses, busmap, [&]() { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1570 | for (auto& busIface : dbusInterfaceMap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1571 | { |
| 1572 | objServer.remove_interface(busIface.second); |
| 1573 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1574 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1575 | dbusInterfaceMap.clear(); |
| 1576 | UNKNOWN_BUS_OBJECT_COUNT = 0; |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 1577 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1578 | // todo, get this from a more sensable place |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1579 | std::vector<uint8_t> baseboardFRU; |
| 1580 | if (readBaseboardFRU(baseboardFRU)) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1581 | { |
Andrei Kartashev | 1c5b706 | 2020-08-19 14:47:30 +0300 | [diff] [blame] | 1582 | boost::container::flat_map<int, std::vector<uint8_t>> |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1583 | baseboardDev; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1584 | baseboardDev.emplace(0, baseboardFRU); |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1585 | busmap[0] = std::make_shared<DeviceMap>(baseboardDev); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1586 | } |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1587 | for (auto& devicemap : busmap) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1588 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1589 | for (auto& device : *devicemap.second) |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1590 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1591 | AddFRUObjectToDbus(device.second, dbusInterfaceMap, |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1592 | devicemap.first, device.first); |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1593 | } |
| 1594 | } |
| 1595 | }); |
| 1596 | scan->run(); |
| 1597 | }); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1600 | // Calculate new checksum for fru info area |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1601 | uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter, |
| 1602 | std::vector<uint8_t>::const_iterator end) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1603 | { |
| 1604 | constexpr int checksumMod = 256; |
| 1605 | constexpr uint8_t modVal = 0xFF; |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1606 | int sum = std::accumulate(iter, end, 0); |
| 1607 | int checksum = (checksumMod - sum) & modVal; |
| 1608 | return static_cast<uint8_t>(checksum); |
| 1609 | } |
| 1610 | uint8_t calculateChecksum(std::vector<uint8_t>& fruAreaData) |
| 1611 | { |
| 1612 | return calculateChecksum(fruAreaData.begin(), fruAreaData.end()); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | // Update new fru area length & |
| 1616 | // Update checksum at new checksum location |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1617 | // Return the offset of the area checksum byte |
| 1618 | static unsigned int updateFRUAreaLenAndChecksum(std::vector<uint8_t>& fruData, |
| 1619 | size_t fruAreaStart, |
| 1620 | size_t fruAreaEndOfFieldsOffset, |
| 1621 | size_t fruAreaEndOffset) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1622 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1623 | size_t traverseFRUAreaIndex = fruAreaEndOfFieldsOffset - fruAreaStart; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1624 | |
| 1625 | // fill zeros for any remaining unused space |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1626 | std::fill(fruData.begin() + fruAreaEndOfFieldsOffset, |
| 1627 | fruData.begin() + fruAreaEndOffset, 0); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1628 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1629 | size_t mod = traverseFRUAreaIndex % fruBlockSize; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1630 | size_t checksumLoc; |
| 1631 | if (!mod) |
| 1632 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1633 | traverseFRUAreaIndex += (fruBlockSize); |
| 1634 | checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - 1); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1635 | } |
| 1636 | else |
| 1637 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1638 | traverseFRUAreaIndex += (fruBlockSize - mod); |
| 1639 | checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - mod - 1); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1640 | } |
| 1641 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1642 | size_t newFRUAreaLen = (traverseFRUAreaIndex / fruBlockSize) + |
| 1643 | ((traverseFRUAreaIndex % fruBlockSize) != 0); |
| 1644 | size_t fruAreaLengthLoc = fruAreaStart + 1; |
| 1645 | fruData[fruAreaLengthLoc] = static_cast<uint8_t>(newFRUAreaLen); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1646 | |
| 1647 | // Calculate new checksum |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1648 | std::vector<uint8_t> finalFRUData; |
| 1649 | std::copy_n(fruData.begin() + fruAreaStart, checksumLoc - fruAreaStart, |
| 1650 | std::back_inserter(finalFRUData)); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1651 | |
Andrei Kartashev | 2a7e395 | 2020-09-02 20:32:26 +0300 | [diff] [blame] | 1652 | fruData[checksumLoc] = calculateChecksum(finalFRUData); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1653 | return checksumLoc; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1654 | } |
| 1655 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1656 | static ssize_t getFieldLength(uint8_t fruFieldTypeLenValue) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1657 | { |
| 1658 | constexpr uint8_t typeLenMask = 0x3F; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1659 | constexpr uint8_t endOfFields = 0xC1; |
| 1660 | if (fruFieldTypeLenValue == endOfFields) |
| 1661 | { |
| 1662 | return -1; |
| 1663 | } |
| 1664 | else |
| 1665 | { |
| 1666 | return fruFieldTypeLenValue & typeLenMask; |
| 1667 | } |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | // Details with example of Asset Tag Update |
| 1671 | // To find location of Product Info Area asset tag as per FRU specification |
| 1672 | // 1. Find product Info area starting offset (*8 - as header will be in |
| 1673 | // multiple of 8 bytes). |
| 1674 | // 2. Skip 3 bytes of product info area (like format version, area length, |
| 1675 | // and language code). |
| 1676 | // 3. Traverse manufacturer name, product name, product version, & product |
| 1677 | // serial number, by reading type/length code to reach the Asset Tag. |
| 1678 | // 4. Update the Asset Tag, reposition the product Info area in multiple of |
| 1679 | // 8 bytes. Update the Product area length and checksum. |
| 1680 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1681 | bool updateFRUProperty( |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1682 | const std::string& updatePropertyReq, uint32_t bus, uint32_t address, |
| 1683 | std::string propertyName, |
| 1684 | boost::container::flat_map< |
| 1685 | std::pair<size_t, size_t>, |
| 1686 | std::shared_ptr<sdbusplus::asio::dbus_interface>>& dbusInterfaceMap) |
| 1687 | { |
| 1688 | size_t updatePropertyReqLen = updatePropertyReq.length(); |
| 1689 | if (updatePropertyReqLen == 1 || updatePropertyReqLen > 63) |
| 1690 | { |
| 1691 | std::cerr |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1692 | << "FRU field data cannot be of 1 char or more than 63 chars. " |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1693 | "Invalid Length " |
| 1694 | << updatePropertyReqLen << "\n"; |
| 1695 | return false; |
| 1696 | } |
| 1697 | |
| 1698 | std::vector<uint8_t> fruData; |
| 1699 | try |
| 1700 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1701 | fruData = getFRUInfo(static_cast<uint8_t>(bus), |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1702 | static_cast<uint8_t>(address)); |
| 1703 | } |
| 1704 | catch (std::invalid_argument& e) |
| 1705 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1706 | std::cerr << "Failure getting FRU Info" << e.what() << "\n"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1707 | return false; |
| 1708 | } |
| 1709 | |
| 1710 | if (fruData.empty()) |
| 1711 | { |
| 1712 | return false; |
| 1713 | } |
| 1714 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1715 | const std::vector<std::string>* fruAreaFieldNames; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1716 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1717 | uint8_t fruAreaOffsetFieldValue = 0; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1718 | size_t offset = 0; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1719 | std::string areaName = propertyName.substr(0, propertyName.find("_")); |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1720 | std::string propertyNamePrefix = areaName + "_"; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1721 | auto it = std::find(FRU_AREA_NAMES.begin(), FRU_AREA_NAMES.end(), areaName); |
| 1722 | if (it == FRU_AREA_NAMES.end()) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1723 | { |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1724 | std::cerr << "Can't parse area name for property " << propertyName |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1725 | << " \n"; |
| 1726 | return false; |
| 1727 | } |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1728 | fruAreas fruAreaToUpdate = |
| 1729 | static_cast<fruAreas>(it - FRU_AREA_NAMES.begin()); |
| 1730 | fruAreaOffsetFieldValue = |
| 1731 | fruData[getHeaderAreaFieldOffset(fruAreaToUpdate)]; |
| 1732 | switch (fruAreaToUpdate) |
| 1733 | { |
| 1734 | case fruAreas::fruAreaChassis: |
| 1735 | offset = 3; // chassis part number offset. Skip fixed first 3 bytes |
| 1736 | fruAreaFieldNames = &CHASSIS_FRU_AREAS; |
| 1737 | break; |
| 1738 | case fruAreas::fruAreaBoard: |
| 1739 | offset = 6; // board manufacturer offset. Skip fixed first 6 bytes |
| 1740 | fruAreaFieldNames = &BOARD_FRU_AREAS; |
| 1741 | break; |
| 1742 | case fruAreas::fruAreaProduct: |
| 1743 | // Manufacturer name offset. Skip fixed first 3 product fru bytes |
| 1744 | // i.e. version, area length and language code |
| 1745 | offset = 3; |
| 1746 | fruAreaFieldNames = &PRODUCT_FRU_AREAS; |
| 1747 | break; |
| 1748 | default: |
| 1749 | std::cerr << "Don't know how to handle property " << propertyName |
| 1750 | << " \n"; |
| 1751 | return false; |
| 1752 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1753 | if (fruAreaOffsetFieldValue == 0) |
| 1754 | { |
| 1755 | std::cerr << "FRU Area for " << propertyName << " not present \n"; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1756 | return false; |
| 1757 | } |
| 1758 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1759 | size_t fruAreaStart = fruAreaOffsetFieldValue * fruBlockSize; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1760 | size_t fruAreaSize = fruData[fruAreaStart + 1] * fruBlockSize; |
| 1761 | size_t fruAreaEnd = fruAreaStart + fruAreaSize; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1762 | size_t fruDataIter = fruAreaStart + offset; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1763 | size_t fruUpdateFieldLoc = fruDataIter; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1764 | size_t skipToFRUUpdateField = 0; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1765 | ssize_t fieldLength; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1766 | |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1767 | bool found = false; |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1768 | for (auto& field : *fruAreaFieldNames) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1769 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1770 | skipToFRUUpdateField++; |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1771 | if (propertyName == propertyNamePrefix + field) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1772 | { |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1773 | found = true; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1774 | break; |
| 1775 | } |
| 1776 | } |
Andrei Kartashev | 6cfb775 | 2020-08-10 19:50:33 +0300 | [diff] [blame] | 1777 | if (!found) |
| 1778 | { |
| 1779 | std::size_t pos = propertyName.find(FRU_CUSTOM_FIELD_NAME); |
| 1780 | if (pos == std::string::npos) |
| 1781 | { |
| 1782 | std::cerr << "PropertyName doesn't exist in FRU Area Vectors: " |
| 1783 | << propertyName << "\n"; |
| 1784 | return false; |
| 1785 | } |
| 1786 | std::string fieldNumStr = |
| 1787 | propertyName.substr(pos + FRU_CUSTOM_FIELD_NAME.length()); |
| 1788 | size_t fieldNum = std::stoi(fieldNumStr); |
| 1789 | if (fieldNum == 0) |
| 1790 | { |
| 1791 | std::cerr << "PropertyName not recognized: " << propertyName |
| 1792 | << "\n"; |
| 1793 | return false; |
| 1794 | } |
| 1795 | skipToFRUUpdateField += fieldNum; |
| 1796 | } |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1797 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1798 | for (size_t i = 1; i < skipToFRUUpdateField; i++) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1799 | { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1800 | fieldLength = getFieldLength(fruData[fruDataIter]); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1801 | if (fieldLength < 0) |
| 1802 | { |
| 1803 | break; |
| 1804 | } |
| 1805 | fruDataIter += 1 + fieldLength; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1806 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1807 | fruUpdateFieldLoc = fruDataIter; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1808 | |
| 1809 | // Push post update fru field bytes to a vector |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1810 | fieldLength = getFieldLength(fruData[fruUpdateFieldLoc]); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1811 | if (fieldLength < 0) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1812 | { |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1813 | std::cerr << "Property " << propertyName << " not present \n"; |
| 1814 | return false; |
| 1815 | } |
| 1816 | fruDataIter += 1 + fieldLength; |
| 1817 | size_t restFRUFieldsLoc = fruDataIter; |
| 1818 | size_t endOfFieldsLoc = 0; |
| 1819 | while ((fieldLength = getFieldLength(fruData[fruDataIter])) >= 0) |
| 1820 | { |
| 1821 | if (fruDataIter >= (fruAreaStart + fruAreaSize)) |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1822 | { |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1823 | fruDataIter = fruAreaStart + fruAreaSize; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1824 | break; |
| 1825 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1826 | fruDataIter += 1 + fieldLength; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1827 | } |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1828 | endOfFieldsLoc = fruDataIter; |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1829 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1830 | std::vector<uint8_t> restFRUAreaFieldsData; |
| 1831 | std::copy_n(fruData.begin() + restFRUFieldsLoc, |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1832 | endOfFieldsLoc - restFRUFieldsLoc + 1, |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1833 | std::back_inserter(restFRUAreaFieldsData)); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1834 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1835 | // Push post update fru areas if any |
| 1836 | unsigned int nextFRUAreaLoc = 0; |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1837 | for (fruAreas nextFRUArea = fruAreas::fruAreaInternal; |
| 1838 | nextFRUArea <= fruAreas::fruAreaMultirecord; ++nextFRUArea) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1839 | { |
| 1840 | unsigned int fruAreaLoc = |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1841 | fruData[getHeaderAreaFieldOffset(nextFRUArea)] * fruBlockSize; |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1842 | if ((fruAreaLoc > endOfFieldsLoc) && |
| 1843 | ((nextFRUAreaLoc == 0) || (fruAreaLoc < nextFRUAreaLoc))) |
| 1844 | { |
| 1845 | nextFRUAreaLoc = fruAreaLoc; |
| 1846 | } |
| 1847 | } |
| 1848 | std::vector<uint8_t> restFRUAreasData; |
| 1849 | if (nextFRUAreaLoc) |
| 1850 | { |
| 1851 | std::copy_n(fruData.begin() + nextFRUAreaLoc, |
| 1852 | fruData.size() - nextFRUAreaLoc, |
| 1853 | std::back_inserter(restFRUAreasData)); |
| 1854 | } |
| 1855 | |
| 1856 | // check FRU area size |
| 1857 | size_t fruAreaDataSize = |
| 1858 | ((fruUpdateFieldLoc - fruAreaStart + 1) + restFRUAreaFieldsData.size()); |
| 1859 | size_t fruAreaAvailableSize = fruAreaSize - fruAreaDataSize; |
| 1860 | if ((updatePropertyReqLen + 1) > fruAreaAvailableSize) |
| 1861 | { |
| 1862 | |
| 1863 | #ifdef ENABLE_FRU_AREA_RESIZE |
| 1864 | size_t newFRUAreaSize = fruAreaDataSize + updatePropertyReqLen + 1; |
| 1865 | // round size to 8-byte blocks |
| 1866 | newFRUAreaSize = |
| 1867 | ((newFRUAreaSize - 1) / fruBlockSize + 1) * fruBlockSize; |
| 1868 | size_t newFRUDataSize = fruData.size() + newFRUAreaSize - fruAreaSize; |
| 1869 | fruData.resize(newFRUDataSize); |
| 1870 | fruAreaSize = newFRUAreaSize; |
| 1871 | fruAreaEnd = fruAreaStart + fruAreaSize; |
| 1872 | #else |
| 1873 | std::cerr << "FRU field length: " << updatePropertyReqLen + 1 |
| 1874 | << " should not be greater than available FRU area size: " |
| 1875 | << fruAreaAvailableSize << "\n"; |
| 1876 | return false; |
| 1877 | #endif // ENABLE_FRU_AREA_RESIZE |
| 1878 | } |
| 1879 | |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1880 | // write new requested property field length and data |
| 1881 | constexpr uint8_t newTypeLenMask = 0xC0; |
| 1882 | fruData[fruUpdateFieldLoc] = |
| 1883 | static_cast<uint8_t>(updatePropertyReqLen | newTypeLenMask); |
| 1884 | fruUpdateFieldLoc++; |
| 1885 | std::copy(updatePropertyReq.begin(), updatePropertyReq.end(), |
| 1886 | fruData.begin() + fruUpdateFieldLoc); |
| 1887 | |
| 1888 | // Copy remaining data to main fru area - post updated fru field vector |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1889 | restFRUFieldsLoc = fruUpdateFieldLoc + updatePropertyReqLen; |
| 1890 | size_t fruAreaDataEnd = restFRUFieldsLoc + restFRUAreaFieldsData.size(); |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1891 | std::copy(restFRUAreaFieldsData.begin(), restFRUAreaFieldsData.end(), |
| 1892 | fruData.begin() + restFRUFieldsLoc); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1893 | |
| 1894 | // Update final fru with new fru area length and checksum |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1895 | unsigned int nextFRUAreaNewLoc = updateFRUAreaLenAndChecksum( |
| 1896 | fruData, fruAreaStart, fruAreaDataEnd, fruAreaEnd); |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1897 | |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1898 | #ifdef ENABLE_FRU_AREA_RESIZE |
| 1899 | ++nextFRUAreaNewLoc; |
| 1900 | ssize_t nextFRUAreaOffsetDiff = |
| 1901 | (nextFRUAreaNewLoc - nextFRUAreaLoc) / fruBlockSize; |
| 1902 | // Append rest FRU Areas if size changed and there were other sections after |
| 1903 | // updated one |
| 1904 | if (nextFRUAreaOffsetDiff && nextFRUAreaLoc) |
| 1905 | { |
| 1906 | std::copy(restFRUAreasData.begin(), restFRUAreasData.end(), |
| 1907 | fruData.begin() + nextFRUAreaNewLoc); |
| 1908 | // Update Common Header |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1909 | for (int fruArea = fruAreaInternal; fruArea <= fruAreaMultirecord; |
| 1910 | fruArea++) |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1911 | { |
Andrei Kartashev | 9f0f2d1 | 2020-08-20 04:24:37 +0300 | [diff] [blame] | 1912 | unsigned int fruAreaOffsetField = getHeaderAreaFieldOffset(fruArea); |
Andrei Kartashev | 6b3d4c5 | 2020-08-10 19:24:17 +0300 | [diff] [blame] | 1913 | size_t curFRUAreaOffset = fruData[fruAreaOffsetField]; |
| 1914 | if (curFRUAreaOffset > fruAreaOffsetFieldValue) |
| 1915 | { |
| 1916 | fruData[fruAreaOffsetField] = static_cast<int8_t>( |
| 1917 | curFRUAreaOffset + nextFRUAreaOffsetDiff); |
| 1918 | } |
| 1919 | } |
| 1920 | // Calculate new checksum |
| 1921 | std::vector<uint8_t> headerFRUData; |
| 1922 | std::copy_n(fruData.begin(), 7, std::back_inserter(headerFRUData)); |
| 1923 | size_t checksumVal = calculateChecksum(headerFRUData); |
| 1924 | fruData[7] = static_cast<uint8_t>(checksumVal); |
| 1925 | // fill zeros if FRU Area size decreased |
| 1926 | if (nextFRUAreaOffsetDiff < 0) |
| 1927 | { |
| 1928 | std::fill(fruData.begin() + nextFRUAreaNewLoc + |
| 1929 | restFRUAreasData.size(), |
| 1930 | fruData.end(), 0); |
| 1931 | } |
| 1932 | } |
| 1933 | #else |
| 1934 | // this is to avoid "unused variable" warning |
| 1935 | (void)nextFRUAreaNewLoc; |
| 1936 | #endif // ENABLE_FRU_AREA_RESIZE |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1937 | if (fruData.empty()) |
| 1938 | { |
| 1939 | return false; |
| 1940 | } |
| 1941 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1942 | if (!writeFRU(static_cast<uint8_t>(bus), static_cast<uint8_t>(address), |
Joshi-Mansi | d73b744 | 2020-03-27 19:10:21 +0530 | [diff] [blame] | 1943 | fruData)) |
| 1944 | { |
| 1945 | return false; |
| 1946 | } |
| 1947 | |
| 1948 | // Rescan the bus so that GetRawFru dbus-call fetches updated values |
| 1949 | rescanBusses(busMap, dbusInterfaceMap); |
| 1950 | return true; |
| 1951 | } |
| 1952 | |
James Feist | 9813279 | 2019-07-09 13:29:09 -0700 | [diff] [blame] | 1953 | int main() |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1954 | { |
| 1955 | auto devDir = fs::path("/dev/"); |
James Feist | c9dff1b | 2019-02-13 13:33:13 -0800 | [diff] [blame] | 1956 | auto matchString = std::string(R"(i2c-\d+$)"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1957 | std::vector<fs::path> i2cBuses; |
| 1958 | |
James Feist | a3c180a | 2018-08-09 16:06:04 -0700 | [diff] [blame] | 1959 | if (!findFiles(devDir, matchString, i2cBuses)) |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1960 | { |
| 1961 | std::cerr << "unable to find i2c devices\n"; |
| 1962 | return 1; |
| 1963 | } |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1964 | |
Patrick Venture | 11f1ff4 | 2019-08-01 10:42:12 -0700 | [diff] [blame] | 1965 | // check for and load blacklist with initial buses. |
| 1966 | loadBlacklist(blacklistPath); |
| 1967 | |
Vijay Khemka | 065f6d9 | 2018-12-18 10:37:47 -0800 | [diff] [blame] | 1968 | systemBus->request_name("xyz.openbmc_project.FruDevice"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1969 | |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 1970 | // this is a map with keys of pair(bus number, address) and values of |
| 1971 | // the object on dbus |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1972 | boost::container::flat_map<std::pair<size_t, size_t>, |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1973 | std::shared_ptr<sdbusplus::asio::dbus_interface>> |
| 1974 | dbusInterfaceMap; |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1975 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 1976 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 1977 | objServer.add_interface("/xyz/openbmc_project/FruDevice", |
| 1978 | "xyz.openbmc_project.FruDeviceManager"); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 1979 | |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1980 | iface->register_method("ReScan", |
| 1981 | [&]() { rescanBusses(busMap, dbusInterfaceMap); }); |
James Feist | 2a9d6db | 2018-04-27 15:48:28 -0700 | [diff] [blame] | 1982 | |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1983 | iface->register_method("ReScanBus", [&](uint8_t bus) { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 1984 | rescanOneBus(busMap, bus, dbusInterfaceMap, true); |
Cheng C Yang | d5b87fb | 2020-01-23 10:10:45 +0800 | [diff] [blame] | 1985 | }); |
| 1986 | |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1987 | iface->register_method("GetRawFru", getFRUInfo); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1988 | |
| 1989 | iface->register_method("WriteFru", [&](const uint8_t bus, |
| 1990 | const uint8_t address, |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 1991 | const std::vector<uint8_t>& data) { |
Andrei Kartashev | 2f0de17 | 2020-08-13 14:29:40 +0300 | [diff] [blame] | 1992 | if (!writeFRU(bus, address, data)) |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1993 | { |
James Feist | ddb7830 | 2018-09-06 11:45:42 -0700 | [diff] [blame] | 1994 | throw std::invalid_argument("Invalid Arguments."); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1995 | return; |
| 1996 | } |
| 1997 | // schedule rescan on success |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 1998 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | b49ffc3 | 2018-05-02 11:10:43 -0700 | [diff] [blame] | 1999 | }); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 2000 | iface->initialize(); |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 2001 | |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 2002 | std::function<void(sdbusplus::message::message & message)> eventHandler = |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 2003 | [&](sdbusplus::message::message& message) { |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 2004 | std::string objectName; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 2005 | boost::container::flat_map< |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 2006 | std::string, |
| 2007 | std::variant<std::string, bool, int64_t, uint64_t, double>> |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 2008 | values; |
| 2009 | message.read(objectName, values); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 2010 | auto findState = values.find("CurrentHostState"); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 2011 | if (findState != values.end()) |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 2012 | { |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 2013 | powerIsOn = boost::ends_with( |
| 2014 | std::get<std::string>(findState->second), "Running"); |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 2015 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 2016 | |
James Feist | cb5661d | 2020-06-12 15:05:01 -0700 | [diff] [blame] | 2017 | if (powerIsOn) |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 2018 | { |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 2019 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 2020 | } |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 2021 | }; |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 2022 | |
| 2023 | sdbusplus::bus::match::match powerMatch = sdbusplus::bus::match::match( |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 2024 | static_cast<sdbusplus::bus::bus&>(*systemBus), |
James Feist | 7bcd3f2 | 2019-03-18 16:04:04 -0700 | [diff] [blame] | 2025 | "type='signal',interface='org.freedesktop.DBus.Properties',path='/xyz/" |
James Feist | 0eeb79c | 2019-10-09 13:35:29 -0700 | [diff] [blame] | 2026 | "openbmc_project/state/" |
| 2027 | "host0',arg0='xyz.openbmc_project.State.Host'", |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 2028 | eventHandler); |
| 2029 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 2030 | int fd = inotify_init(); |
James Feist | 0eb4035 | 2019-04-09 14:44:04 -0700 | [diff] [blame] | 2031 | inotify_add_watch(fd, I2C_DEV_LOCATION, |
| 2032 | IN_CREATE | IN_MOVED_TO | IN_DELETE); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 2033 | std::array<char, 4096> readBuffer; |
| 2034 | std::string pendingBuffer; |
| 2035 | // monitor for new i2c devices |
| 2036 | boost::asio::posix::stream_descriptor dirWatch(io, fd); |
| 2037 | std::function<void(const boost::system::error_code, std::size_t)> |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 2038 | watchI2cBusses = [&](const boost::system::error_code& ec, |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 2039 | std::size_t bytes_transferred) { |
| 2040 | if (ec) |
| 2041 | { |
| 2042 | std::cout << "Callback Error " << ec << "\n"; |
| 2043 | return; |
| 2044 | } |
| 2045 | pendingBuffer += std::string(readBuffer.data(), bytes_transferred); |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 2046 | while (pendingBuffer.size() > sizeof(inotify_event)) |
| 2047 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 2048 | const inotify_event* iEvent = |
| 2049 | reinterpret_cast<const inotify_event*>( |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 2050 | pendingBuffer.data()); |
| 2051 | switch (iEvent->mask) |
| 2052 | { |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 2053 | case IN_CREATE: |
| 2054 | case IN_MOVED_TO: |
| 2055 | case IN_DELETE: |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 2056 | std::string name(iEvent->name); |
| 2057 | if (boost::starts_with(name, "i2c")) |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 2058 | { |
James Feist | 5d7f469 | 2020-06-17 18:22:33 -0700 | [diff] [blame] | 2059 | int bus = busStrToInt(name); |
| 2060 | if (bus < 0) |
| 2061 | { |
| 2062 | std::cerr << "Could not parse bus " << name |
| 2063 | << "\n"; |
| 2064 | continue; |
| 2065 | } |
| 2066 | rescanOneBus(busMap, static_cast<uint8_t>(bus), |
| 2067 | dbusInterfaceMap, false); |
James Feist | 9eb0b58 | 2018-04-27 12:15:46 -0700 | [diff] [blame] | 2068 | } |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
| 2071 | pendingBuffer.erase(0, sizeof(inotify_event) + iEvent->len); |
| 2072 | } |
James Feist | 6ebf9de | 2018-05-15 15:01:17 -0700 | [diff] [blame] | 2073 | |
James Feist | 4131aea | 2018-03-09 09:47:30 -0800 | [diff] [blame] | 2074 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), |
| 2075 | watchI2cBusses); |
| 2076 | }; |
| 2077 | |
| 2078 | dirWatch.async_read_some(boost::asio::buffer(readBuffer), watchI2cBusses); |
Gunnar Mills | b3e42fe | 2018-06-13 15:48:27 -0500 | [diff] [blame] | 2079 | // run the initial scan |
James Feist | 5cb0608 | 2019-10-24 17:11:13 -0700 | [diff] [blame] | 2080 | rescanBusses(busMap, dbusInterfaceMap); |
James Feist | 918e18c | 2018-02-13 15:51:07 -0800 | [diff] [blame] | 2081 | |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 2082 | io.run(); |
| 2083 | return 0; |
| 2084 | } |