blob: 371fa6cb8ac9abb14f129d80e3351c6635d218b5 [file] [log] [blame]
Patrick Venturec9508db2018-10-16 17:18:43 -07001#include "fru-area.hpp"
2#include "frup.hpp"
3#include "types.hpp"
4
Vishwa4be4b7a2015-10-31 22:55:50 -05005#include <dlfcn.h>
6#include <errno.h>
Patrick Venturec9508db2018-10-16 17:18:43 -07007#include <host-ipmid/ipmid-api.h>
8#include <mapper.h>
Vishwa4be4b7a2015-10-31 22:55:50 -05009#include <stdio.h>
Patrick Venturec9508db2018-10-16 17:18:43 -070010#include <stdlib.h>
Vishwa4be4b7a2015-10-31 22:55:50 -050011#include <systemd/sd-bus.h>
Chris Austenb45c4cb2015-11-01 06:34:56 -060012#include <unistd.h>
Patrick Venturec9508db2018-10-16 17:18:43 -070013
14#include <algorithm>
15#include <exception>
16#include <fstream>
vishwac93d6d42015-12-16 11:55:16 -060017#include <iostream>
18#include <memory>
Ratan Gupta0b77cfa2017-01-31 17:32:31 +053019#include <sdbusplus/server.hpp>
Patrick Venturec9508db2018-10-16 17:18:43 -070020#include <sstream>
21#include <vector>
Vishwa4be4b7a2015-10-31 22:55:50 -050022
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060023using namespace ipmi::vpd;
24
Ratan Guptacb0d4e52016-12-22 19:05:57 +053025extern const FruMap frus;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060026extern const std::map<Path, InterfaceMap> extras;
Ratan Guptacb0d4e52016-12-22 19:05:57 +053027
vishwac93d6d42015-12-16 11:55:16 -060028//----------------------------------------------------------------
29// Constructor
30//----------------------------------------------------------------
31ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type,
Patrick Venturec9508db2018-10-16 17:18:43 -070032 sd_bus* bus_type, bool bmc_fru)
vishwac93d6d42015-12-16 11:55:16 -060033{
34 iv_fruid = fruid;
35 iv_type = type;
36 iv_bmc_fru = bmc_fru;
37 iv_bus_type = bus_type;
38 iv_valid = false;
39 iv_data = NULL;
40 iv_present = false;
41
Patrick Venturec9508db2018-10-16 17:18:43 -070042 if (iv_type == IPMI_FRU_AREA_INTERNAL_USE)
vishwac93d6d42015-12-16 11:55:16 -060043 {
44 iv_name = "INTERNAL_";
45 }
Patrick Venturec9508db2018-10-16 17:18:43 -070046 else if (iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
vishwac93d6d42015-12-16 11:55:16 -060047 {
48 iv_name = "CHASSIS_";
49 }
Patrick Venturec9508db2018-10-16 17:18:43 -070050 else if (iv_type == IPMI_FRU_AREA_BOARD_INFO)
vishwac93d6d42015-12-16 11:55:16 -060051 {
52 iv_name = "BOARD_";
53 }
Patrick Venturec9508db2018-10-16 17:18:43 -070054 else if (iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
vishwac93d6d42015-12-16 11:55:16 -060055 {
56 iv_name = "PRODUCT_";
57 }
Patrick Venturec9508db2018-10-16 17:18:43 -070058 else if (iv_type == IPMI_FRU_AREA_MULTI_RECORD)
vishwac93d6d42015-12-16 11:55:16 -060059 {
60 iv_name = "MULTI_";
61 }
62 else
63 {
64 iv_name = IPMI_FRU_AREA_TYPE_MAX;
Patrick Venturec9508db2018-10-16 17:18:43 -070065 fprintf(stderr, "ERROR: Invalid Area type :[%d]\n", iv_type);
vishwac93d6d42015-12-16 11:55:16 -060066 }
67}
68
69//-----------------------------------------------------
70// For a FRU area type, accepts the data and updates
71// area specific data.
72//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -070073void ipmi_fru::set_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -060074{
75 iv_len = len;
76 iv_data = new uint8_t[len];
77 memcpy(iv_data, data, len);
78}
79
80//-----------------------------------------------------
81// Sets the dbus parameters
82//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -070083void ipmi_fru::update_dbus_paths(const char* bus_name, const char* obj_path,
84 const char* intf_name)
vishwac93d6d42015-12-16 11:55:16 -060085{
86 iv_bus_name = bus_name;
87 iv_obj_path = obj_path;
88 iv_intf_name = intf_name;
89}
90
91//-------------------
92// Destructor
93//-------------------
94ipmi_fru::~ipmi_fru()
95{
Patrick Venturec9508db2018-10-16 17:18:43 -070096 if (iv_data != NULL)
vishwac93d6d42015-12-16 11:55:16 -060097 {
Patrick Venturec9508db2018-10-16 17:18:43 -070098 delete[] iv_data;
vishwac93d6d42015-12-16 11:55:16 -060099 iv_data = NULL;
100 }
vishwac93d6d42015-12-16 11:55:16 -0600101}
102
Vishwa4be4b7a2015-10-31 22:55:50 -0500103//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600104// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600105// and returns the 8 bit checksum
106// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500107//-------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700108unsigned char calculate_crc(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500109{
110 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600111 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500112
Patrick Venturec9508db2018-10-16 17:18:43 -0700113 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500114 {
115 crc += *data++;
116 }
vishwaf3ca3522015-12-02 10:35:13 -0600117
Patrick Venturec9508db2018-10-16 17:18:43 -0700118 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500119}
120
121//---------------------------------------------------------------------
122// Accepts a fru area offset in commom hdr and tells which area it is.
123//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600124ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500125{
126 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
127
Patrick Venturec9508db2018-10-16 17:18:43 -0700128 switch (area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500129 {
130 case IPMI_FRU_INTERNAL_OFFSET:
131 type = IPMI_FRU_AREA_INTERNAL_USE;
132 break;
133
134 case IPMI_FRU_CHASSIS_OFFSET:
135 type = IPMI_FRU_AREA_CHASSIS_INFO;
136 break;
137
138 case IPMI_FRU_BOARD_OFFSET:
139 type = IPMI_FRU_AREA_BOARD_INFO;
140 break;
141
142 case IPMI_FRU_PRODUCT_OFFSET:
143 type = IPMI_FRU_AREA_PRODUCT_INFO;
144 break;
145
146 case IPMI_FRU_MULTI_OFFSET:
147 type = IPMI_FRU_AREA_MULTI_RECORD;
148 break;
149
150 default:
151 type = IPMI_FRU_AREA_TYPE_MAX;
152 }
153
154 return type;
155}
156
vishwac93d6d42015-12-16 11:55:16 -0600157///-----------------------------------------------
158// Validates the data for crc and mandatory fields
159///-----------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700160int verify_fru_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600161{
162 uint8_t checksum = 0;
163 int rc = -1;
164
165 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700166 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600167 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700168 fprintf(stderr, "Invalid entry:[%d] in byte-0\n", data[0]);
vishwac93d6d42015-12-16 11:55:16 -0600169 return rc;
170 }
171#ifdef __IPMI_DEBUG__
172 else
173 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700174 printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n", data[0]);
vishwac93d6d42015-12-16 11:55:16 -0600175 }
176#endif
177
178 // See if the calculated CRC matches with the embedded one.
179 // CRC to be calculated on all except the last one that is CRC itself.
180 checksum = calculate_crc(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700181 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600182 {
183#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700184 fprintf(stderr,
185 "Checksum mismatch."
vishwac93d6d42015-12-16 11:55:16 -0600186 " Calculated:[0x%X], Embedded:[0x%X]\n",
187 checksum, data[len]);
188#endif
189 return rc;
190 }
191#ifdef __IPMI_DEBUG__
192 else
193 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700194 printf("SUCCESS: Checksum matches:[0x%X]\n", checksum);
vishwac93d6d42015-12-16 11:55:16 -0600195 }
196#endif
197
198 return EXIT_SUCCESS;
199}
200
Vishwa4be4b7a2015-10-31 22:55:50 -0500201//------------------------------------------------------------------------
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530202// Gets the value of the key from the fru dictionary of the given section.
203// FRU dictionary is parsed fru data for all the sections.
204//------------------------------------------------------------------------
205
Patrick Venturec9508db2018-10-16 17:18:43 -0700206std::string getFRUValue(const std::string& section, const std::string& key,
207 const std::string& delimiter, IPMIFruInfo& fruData)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530208{
209
210 auto minIndexValue = 0;
211 auto maxIndexValue = 0;
212 std::string fruValue = "";
213 if (section == "Board")
214 {
215 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
216 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
217 }
218 else if (section == "Product")
219 {
220 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
221 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530222 }
223 else if (section == "Chassis")
224 {
225 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
226 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
227 }
228
229 auto first = fruData.cbegin() + minIndexValue;
230 auto last = first + (maxIndexValue - minIndexValue) + 1;
231
Patrick Venturec9508db2018-10-16 17:18:43 -0700232 auto itr =
233 std::find_if(first, last, [&key](auto& e) { return key == e.first; });
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530234
235 if (itr != last)
236 {
237 fruValue = itr->second;
238 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530239
Patrick Venturec9508db2018-10-16 17:18:43 -0700240 // if the key is custom property then the value could be in two formats.
241 // 1) custom field 2 = "value".
242 // 2) custom field 2 = "key:value".
243 // if delimiter length = 0 i.e custom field 2 = "value"
Ratan Guptacdde23d2017-02-15 09:29:54 +0530244
245 constexpr auto customProp = "Custom Field";
246 if (key.find(customProp) != std::string::npos)
247 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700248 if (delimiter.length() > 0)
249 {
250 size_t delimiterpos = fruValue.find(delimiter);
251 if (delimiterpos != std::string::npos)
252 fruValue = fruValue.substr(delimiterpos + 1);
253 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530254 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530255 return fruValue;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530256}
Patrick Venturec9508db2018-10-16 17:18:43 -0700257// Get the inventory service from the mapper.
258auto getService(sdbusplus::bus::bus& bus, const std::string& intf,
259 const std::string& path)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530260{
Patrick Venturec9508db2018-10-16 17:18:43 -0700261 auto mapperCall =
262 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
263 "/xyz/openbmc_project/object_mapper",
264 "xyz.openbmc_project.ObjectMapper", "GetObject");
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530265
266 mapperCall.append(path);
267 mapperCall.append(std::vector<std::string>({intf}));
268
269 auto mapperResponseMsg = bus.call(mapperCall);
270 if (mapperResponseMsg.is_method_error())
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530271 {
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530272 throw std::runtime_error("ERROR in mapper call");
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530273 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530274
275 std::map<std::string, std::vector<std::string>> mapperResponse;
276 mapperResponseMsg.read(mapperResponse);
277
278 if (mapperResponse.begin() == mapperResponse.end())
279 {
280 throw std::runtime_error("ERROR in reading the mapper response");
281 }
282
283 return mapperResponse.begin()->first;
284}
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530285
286//------------------------------------------------------------------------
Vishwa4be4b7a2015-10-31 22:55:50 -0500287// Takes FRU data, invokes Parser for each fru record area and updates
288// Inventory
289//------------------------------------------------------------------------
Patrick Williams21eb0432017-03-31 11:40:48 -0500290int ipmi_update_inventory(fru_area_vec_t& area_vec, sd_bus* bus_sd)
Vishwa4be4b7a2015-10-31 22:55:50 -0500291{
vishwac93d6d42015-12-16 11:55:16 -0600292 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500293 int rc = 0;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530294 uint8_t fruid = 0;
295 IPMIFruInfo fruData;
Vishwa4be4b7a2015-10-31 22:55:50 -0500296
Vishwa4be4b7a2015-10-31 22:55:50 -0500297 // For each FRU area, extract the needed data , get it parsed and update
298 // the Inventory.
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530299 for (const auto& fruArea : area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500300 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530301 fruid = fruArea->get_fruid();
Vishwa4be4b7a2015-10-31 22:55:50 -0500302 // Fill the container with information
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530303 rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(),
304 (fruArea)->get_len(), fruData);
305 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500306 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530307 std::cerr << "ERROR parsing FRU records\n";
308 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500309 }
310 } // END walking the vector of areas and updating
311
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530312 // For each Fru we have the list of instances which needs to be updated.
313 // Each instance object implements certain interfaces.
314 // Each Interface is having Dbus properties.
315 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
316
317 // Here we are just printing the object,interface and the properties.
318 // which needs to be called with the new inventory manager implementation.
Patrick Williams49a6fcd2017-04-18 11:49:07 -0500319 sdbusplus::bus::bus bus{bus_sd};
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530320 using namespace std::string_literals;
321 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
Brad Bishopec7648d2017-02-23 11:30:16 -0500322 static const auto path = "/xyz/openbmc_project/inventory"s;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530323 std::string service;
324 try
325 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700326 service = getService(bus, intf, path);
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530327 }
328 catch (const std::runtime_error& e)
329 {
330 std::cerr << e.what() << "\n";
331 return -1;
332 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530333
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530334 auto iter = frus.find(fruid);
335 if (iter == frus.end())
336 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530337 std::cerr << "ERROR Unable to get the fru info for FRU="
338 << static_cast<int>(fruid) << "\n";
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530339 return -1;
340 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530341
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530342 auto& instanceList = iter->second;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530343 if (instanceList.size() <= 0)
344 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530345 std::cout << "Object List empty for this FRU="
346 << static_cast<int>(fruid) << "\n";
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530347 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530348
349 ObjectMap objects;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530350 for (auto& instance : instanceList)
351 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530352 InterfaceMap interfaces;
Ratan Guptac19c0542018-02-04 23:24:44 +0530353 const auto& extrasIter = extras.find(instance.path);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530354
Ratan Guptac19c0542018-02-04 23:24:44 +0530355 for (auto& interfaceList : instance.interfaces)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530356 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700357 PropertyMap props; // store all the properties
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530358 for (auto& properties : interfaceList.second)
359 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500360 std::string value;
361 decltype(auto) pdata = properties.second;
Ratan Guptacdde23d2017-02-15 09:29:54 +0530362
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500363 if (!pdata.section.empty() && !pdata.property.empty())
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530364 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500365 value = getFRUValue(pdata.section, pdata.property,
366 pdata.delimiter, fruData);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530367 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530368 props.emplace(std::move(properties.first), std::move(value));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530369 }
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600370 // Check and update extra properties
Patrick Venturec9508db2018-10-16 17:18:43 -0700371 if (extras.end() != extrasIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600372 {
373 const auto& propsIter =
374 (extrasIter->second).find(interfaceList.first);
Patrick Venturec9508db2018-10-16 17:18:43 -0700375 if ((extrasIter->second).end() != propsIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600376 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700377 for (const auto& map : propsIter->second)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600378 {
379 props.emplace(map.first, map.second);
380 }
381 }
382 }
383 interfaces.emplace(std::move(interfaceList.first),
384 std::move(props));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530385 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500386
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600387 // Call the inventory manager
Ratan Guptac19c0542018-02-04 23:24:44 +0530388 sdbusplus::message::object_path path = instance.path;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600389 // Check and update extra properties
Patrick Venturec9508db2018-10-16 17:18:43 -0700390 if (extras.end() != extrasIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600391 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700392 for (const auto& entry : extrasIter->second)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600393 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700394 if (interfaces.end() == interfaces.find(entry.first))
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600395 {
396 interfaces.emplace(entry.first, entry.second);
397 }
398 }
399 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700400 objects.emplace(path, interfaces);
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530401 }
402
Patrick Venturec9508db2018-10-16 17:18:43 -0700403 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
404 intf.c_str(), "Notify");
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530405 pimMsg.append(std::move(objects));
406 auto inventoryMgrResponseMsg = bus.call(pimMsg);
407 if (inventoryMgrResponseMsg.is_method_error())
408 {
409 std::cerr << "Error in notify call\n";
410 return -1;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530411 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500412 return rc;
413}
414
vishwac93d6d42015-12-16 11:55:16 -0600415///----------------------------------------------------
416// Checks if a particular fru area is populated or not
417///----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700418bool remove_invalid_area(const std::unique_ptr<ipmi_fru>& fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500419{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600420 // Filter the ones that are empty
Patrick Venturec9508db2018-10-16 17:18:43 -0700421 if (!(fru_area->get_len()))
vishwac93d6d42015-12-16 11:55:16 -0600422 {
423 return true;
424 }
425 return false;
426}
Vishwa4be4b7a2015-10-31 22:55:50 -0500427
vishwac93d6d42015-12-16 11:55:16 -0600428///----------------------------------------------------------------------------------
429// Populates various FRU areas
430// @prereq : This must be called only after validating common header.
431///----------------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700432int ipmi_populate_fru_areas(uint8_t* fru_data, const size_t data_len,
433 fru_area_vec_t& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600434{
vishwac93d6d42015-12-16 11:55:16 -0600435 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500436
vishwac93d6d42015-12-16 11:55:16 -0600437 // Now walk the common header and see if the file size has atleast the last
438 // offset mentioned by the common_hdr. If the file size is less than the
439 // offset of any if the fru areas mentioned in the common header, then we do
440 // not have a complete file.
Patrick Venturec9508db2018-10-16 17:18:43 -0700441 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
442 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600443 {
Yi Li75c2d462016-04-11 16:57:46 +0800444 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600445 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500446 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Venture50ddfe52018-10-16 17:16:32 -0700447 size_t area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
Patrick Venturec9508db2018-10-16 17:18:43 -0700448 if (area_offset && (data_len < (area_offset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600449 {
450 // Our file size is less than what it needs to be. +2 because we are
451 // using area len that is at 2 byte off area_offset
Patrick Venturec9508db2018-10-16 17:18:43 -0700452 fprintf(stderr, "fru file is incomplete. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600453 return rc;
454 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700455 else if (area_offset)
vishwac93d6d42015-12-16 11:55:16 -0600456 {
457 // Read 2 bytes to know the actual size of area.
458 uint8_t area_hdr[2] = {0};
Patrick Venturec9508db2018-10-16 17:18:43 -0700459 memcpy(area_hdr, &((uint8_t*)fru_data)[area_offset],
460 sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500461
vishwac93d6d42015-12-16 11:55:16 -0600462 // Size of this area will be the 2nd byte in the fru area header.
Patrick Venturec9508db2018-10-16 17:18:43 -0700463 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
vishwac93d6d42015-12-16 11:55:16 -0600464 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500465
Patrick Williams3365ec82016-08-17 17:45:18 -0500466 printf("fru data size:[%zd], area offset:[%zd], area_size:[%zd]\n",
Patrick Venturec9508db2018-10-16 17:18:43 -0700467 data_len, area_offset, area_len);
Vishwa4be4b7a2015-10-31 22:55:50 -0500468
vishwac93d6d42015-12-16 11:55:16 -0600469 // See if we really have that much buffer. We have area offset amd
470 // from there, the actual len.
Patrick Venturec9508db2018-10-16 17:18:43 -0700471 if (data_len < (area_len + area_offset))
vishwac93d6d42015-12-16 11:55:16 -0600472 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700473 fprintf(stderr, "Incomplete Fru file.. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600474 return rc;
475 }
476
477 // Save off the data.
Patrick Venturec9508db2018-10-16 17:18:43 -0700478 memcpy(area_data, &((uint8_t*)fru_data)[area_offset], area_len);
vishwac93d6d42015-12-16 11:55:16 -0600479
480 // Validate the crc
481 rc = verify_fru_data(area_data, area_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700482 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600483 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700484 fprintf(stderr, "Error validating fru area. offset:[%zd]\n",
485 area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600486 return rc;
487 }
488 else
489 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700490 printf("Successfully verified area checksum. offset:[%zd]\n",
491 area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600492 }
493
494 // We already have a vector that is passed to us containing all
495 // of the fields populated. Update the data portion now.
Patrick Venturec9508db2018-10-16 17:18:43 -0700496 for (auto& iter : fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600497 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700498 if ((iter)->get_type() == get_fru_area_type(fru_entry))
vishwac93d6d42015-12-16 11:55:16 -0600499 {
500 (iter)->set_data(area_data, area_len);
501 }
502 }
503 } // If we have fru data present
Patrick Venturec9508db2018-10-16 17:18:43 -0700504 } // Walk common_hdr
vishwac93d6d42015-12-16 11:55:16 -0600505
506 // Not all the fields will be populated in a fru data. Mostly all cases will
507 // not have more than 2 or 3.
508 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
Patrick Venturec9508db2018-10-16 17:18:43 -0700509 remove_invalid_area),
510 fru_area_vec.end());
vishwac93d6d42015-12-16 11:55:16 -0600511
512 return EXIT_SUCCESS;
513}
514
515///---------------------------------------------------------
516// Validates the fru data per ipmi common header constructs.
517// Returns with updated common_hdr and also file_size
518//----------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700519int ipmi_validate_common_hdr(const uint8_t* fru_data, const size_t data_len)
vishwac93d6d42015-12-16 11:55:16 -0600520{
521 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500522
523 uint8_t common_hdr[sizeof(struct common_header)] = {0};
Patrick Venturec9508db2018-10-16 17:18:43 -0700524 if (data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500525 {
vishwac93d6d42015-12-16 11:55:16 -0600526 memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500527 }
528 else
529 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500530 fprintf(stderr, "Incomplete fru data file. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600531 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500532 }
533
vishwac93d6d42015-12-16 11:55:16 -0600534 // Verify the crc and size
535 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
Patrick Venturec9508db2018-10-16 17:18:43 -0700536 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500537 {
vishwac93d6d42015-12-16 11:55:16 -0600538 fprintf(stderr, "Failed to validate common header\n");
539 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500540 }
541
vishwac93d6d42015-12-16 11:55:16 -0600542 return EXIT_SUCCESS;
543}
Vishwa4be4b7a2015-10-31 22:55:50 -0500544
vishwac93d6d42015-12-16 11:55:16 -0600545//------------------------------------------------------------
546// Cleanup routine
547//------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700548int cleanup_error(FILE* fru_fp, fru_area_vec_t& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600549{
Patrick Venturec9508db2018-10-16 17:18:43 -0700550 if (fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500551 {
vishwac93d6d42015-12-16 11:55:16 -0600552 fclose(fru_fp);
553 fru_fp = NULL;
554 }
vishwaf3ca3522015-12-02 10:35:13 -0600555
Patrick Venturec9508db2018-10-16 17:18:43 -0700556 if (!(fru_area_vec.empty()))
Vishwa4be4b7a2015-10-31 22:55:50 -0500557 {
vishwac93d6d42015-12-16 11:55:16 -0600558 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500559 }
vishwaf3ca3522015-12-02 10:35:13 -0600560
Patrick Venturec9508db2018-10-16 17:18:43 -0700561 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500562}
563
564///-----------------------------------------------------
565// Accepts the filename and validates per IPMI FRU spec
566//----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700567int ipmi_validate_fru_area(const uint8_t fruid, const char* fru_file_name,
568 sd_bus* bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500569{
vishwac93d6d42015-12-16 11:55:16 -0600570 size_t data_len = 0;
571 size_t bytes_read = 0;
572 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500573
vishwac93d6d42015-12-16 11:55:16 -0600574 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
575 // are not used, keeping it here for completeness.
576 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800577
Patrick Venturec9508db2018-10-16 17:18:43 -0700578 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
579 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600580 {
581 // Create an object and push onto a vector.
Patrick Venturec9508db2018-10-16 17:18:43 -0700582 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>(
583 fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
vishwac93d6d42015-12-16 11:55:16 -0600584
585 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500586 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600587 fru_area->set_present(present);
588
vishwac93d6d42015-12-16 11:55:16 -0600589 fru_area_vec.emplace_back(std::move(fru_area));
590 }
591
Patrick Venturec9508db2018-10-16 17:18:43 -0700592 FILE* fru_fp = fopen(fru_file_name, "rb");
593 if (fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500594 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700595 fprintf(stderr, "ERROR: opening:[%s]\n", fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500596 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600597 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500598 }
599
vishwac93d6d42015-12-16 11:55:16 -0600600 // Get the size of the file to see if it meets minimum requirement
Patrick Venturec9508db2018-10-16 17:18:43 -0700601 if (fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500602 {
603 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600604 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500605 }
606
vishwac93d6d42015-12-16 11:55:16 -0600607 // Allocate a buffer to hold entire file content
608 data_len = ftell(fru_fp);
609 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500610
vishwac93d6d42015-12-16 11:55:16 -0600611 rewind(fru_fp);
612 bytes_read = fread(fru_data, data_len, 1, fru_fp);
Patrick Venturec9508db2018-10-16 17:18:43 -0700613 if (bytes_read != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500614 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700615 fprintf(stderr, "Failed reading fru data. Bytes_read=[%zd]\n",
616 bytes_read);
Vishwa4be4b7a2015-10-31 22:55:50 -0500617 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600618 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500619 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500620
vishwac93d6d42015-12-16 11:55:16 -0600621 // We are done reading.
622 fclose(fru_fp);
623 fru_fp = NULL;
624
625 rc = ipmi_validate_common_hdr(fru_data, data_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700626 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500627 {
vishwac93d6d42015-12-16 11:55:16 -0600628 return cleanup_error(fru_fp, fru_area_vec);
629 }
630
Patrick Venturec9508db2018-10-16 17:18:43 -0700631 // Now that we validated the common header, populate various fru sections if
632 // we have them here.
vishwac93d6d42015-12-16 11:55:16 -0600633 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700634 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600635 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700636 fprintf(stderr, "Populating FRU areas failed for:[%d]\n", fruid);
vishwac93d6d42015-12-16 11:55:16 -0600637 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500638 }
639 else
640 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700641 printf("SUCCESS: Populated FRU areas for:[%s]\n", fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500642 }
643
vishwac93d6d42015-12-16 11:55:16 -0600644#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700645 for (auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500646 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700647 printf("FRU ID : [%d]\n", (iter)->get_fruid());
648 printf("AREA NAME : [%s]\n", (iter)->get_name());
649 printf("TYPE : [%d]\n", (iter)->get_type());
650 printf("LEN : [%d]\n", (iter)->get_len());
vishwac93d6d42015-12-16 11:55:16 -0600651 printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
652 printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
653 printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500654 }
vishwac93d6d42015-12-16 11:55:16 -0600655#endif
656
657 // If the vector is populated with everything, then go ahead and update the
658 // inventory.
Patrick Venturec9508db2018-10-16 17:18:43 -0700659 if (!(fru_area_vec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600660 {
661
662#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700663 printf("\n SIZE of vector is : [%d] \n", fru_area_vec.size());
vishwac93d6d42015-12-16 11:55:16 -0600664#endif
Patrick Williams21eb0432017-03-31 11:40:48 -0500665 rc = ipmi_update_inventory(fru_area_vec, bus_type);
Patrick Venturec9508db2018-10-16 17:18:43 -0700666 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600667 {
668 fprintf(stderr, "Error updating inventory\n");
669 }
670 }
671
672 // we are done with all that we wanted to do. This will do the job of
673 // calling any destructors too.
674 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500675
676 return rc;
677}