blob: 2bbf48ee1673b173e2a36f759e4774c1015e18bb [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>
Patrick Venture5739ac32018-10-16 19:17:41 -070019#include <phosphor-logging/log.hpp>
Ratan Gupta0b77cfa2017-01-31 17:32:31 +053020#include <sdbusplus/server.hpp>
Patrick Venturec9508db2018-10-16 17:18:43 -070021#include <sstream>
22#include <vector>
Vishwa4be4b7a2015-10-31 22:55:50 -050023
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060024using namespace ipmi::vpd;
Patrick Venture5739ac32018-10-16 19:17:41 -070025using namespace phosphor::logging;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060026
Ratan Guptacb0d4e52016-12-22 19:05:57 +053027extern const FruMap frus;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -060028extern const std::map<Path, InterfaceMap> extras;
Ratan Guptacb0d4e52016-12-22 19:05:57 +053029
vishwac93d6d42015-12-16 11:55:16 -060030//----------------------------------------------------------------
31// Constructor
32//----------------------------------------------------------------
33ipmi_fru::ipmi_fru(const uint8_t fruid, const ipmi_fru_area_type type,
Patrick Venturec9508db2018-10-16 17:18:43 -070034 sd_bus* bus_type, bool bmc_fru)
vishwac93d6d42015-12-16 11:55:16 -060035{
36 iv_fruid = fruid;
37 iv_type = type;
38 iv_bmc_fru = bmc_fru;
39 iv_bus_type = bus_type;
40 iv_valid = false;
41 iv_data = NULL;
42 iv_present = false;
43
Patrick Venturec9508db2018-10-16 17:18:43 -070044 if (iv_type == IPMI_FRU_AREA_INTERNAL_USE)
vishwac93d6d42015-12-16 11:55:16 -060045 {
46 iv_name = "INTERNAL_";
47 }
Patrick Venturec9508db2018-10-16 17:18:43 -070048 else if (iv_type == IPMI_FRU_AREA_CHASSIS_INFO)
vishwac93d6d42015-12-16 11:55:16 -060049 {
50 iv_name = "CHASSIS_";
51 }
Patrick Venturec9508db2018-10-16 17:18:43 -070052 else if (iv_type == IPMI_FRU_AREA_BOARD_INFO)
vishwac93d6d42015-12-16 11:55:16 -060053 {
54 iv_name = "BOARD_";
55 }
Patrick Venturec9508db2018-10-16 17:18:43 -070056 else if (iv_type == IPMI_FRU_AREA_PRODUCT_INFO)
vishwac93d6d42015-12-16 11:55:16 -060057 {
58 iv_name = "PRODUCT_";
59 }
Patrick Venturec9508db2018-10-16 17:18:43 -070060 else if (iv_type == IPMI_FRU_AREA_MULTI_RECORD)
vishwac93d6d42015-12-16 11:55:16 -060061 {
62 iv_name = "MULTI_";
63 }
64 else
65 {
66 iv_name = IPMI_FRU_AREA_TYPE_MAX;
Patrick Venturec9508db2018-10-16 17:18:43 -070067 fprintf(stderr, "ERROR: Invalid Area type :[%d]\n", iv_type);
vishwac93d6d42015-12-16 11:55:16 -060068 }
69}
70
71//-----------------------------------------------------
72// For a FRU area type, accepts the data and updates
73// area specific data.
74//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -070075void ipmi_fru::set_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -060076{
77 iv_len = len;
78 iv_data = new uint8_t[len];
79 memcpy(iv_data, data, len);
80}
81
82//-----------------------------------------------------
83// Sets the dbus parameters
84//-----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -070085void ipmi_fru::update_dbus_paths(const char* bus_name, const char* obj_path,
86 const char* intf_name)
vishwac93d6d42015-12-16 11:55:16 -060087{
88 iv_bus_name = bus_name;
89 iv_obj_path = obj_path;
90 iv_intf_name = intf_name;
91}
92
93//-------------------
94// Destructor
95//-------------------
96ipmi_fru::~ipmi_fru()
97{
Patrick Venturec9508db2018-10-16 17:18:43 -070098 if (iv_data != NULL)
vishwac93d6d42015-12-16 11:55:16 -060099 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700100 delete[] iv_data;
vishwac93d6d42015-12-16 11:55:16 -0600101 iv_data = NULL;
102 }
vishwac93d6d42015-12-16 11:55:16 -0600103}
104
Vishwa4be4b7a2015-10-31 22:55:50 -0500105//------------------------------------------------
vishwaf3ca3522015-12-02 10:35:13 -0600106// Takes the pointer to stream of bytes and length
vishwac93d6d42015-12-16 11:55:16 -0600107// and returns the 8 bit checksum
108// This algo is per IPMI V2.0 spec
Vishwa4be4b7a2015-10-31 22:55:50 -0500109//-------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700110unsigned char calculate_crc(const unsigned char* data, size_t len)
Vishwa4be4b7a2015-10-31 22:55:50 -0500111{
112 char crc = 0;
vishwac93d6d42015-12-16 11:55:16 -0600113 size_t byte = 0;
Vishwa4be4b7a2015-10-31 22:55:50 -0500114
Patrick Venturec9508db2018-10-16 17:18:43 -0700115 for (byte = 0; byte < len; byte++)
Vishwa4be4b7a2015-10-31 22:55:50 -0500116 {
117 crc += *data++;
118 }
vishwaf3ca3522015-12-02 10:35:13 -0600119
Patrick Venturec9508db2018-10-16 17:18:43 -0700120 return (-crc);
Vishwa4be4b7a2015-10-31 22:55:50 -0500121}
122
123//---------------------------------------------------------------------
124// Accepts a fru area offset in commom hdr and tells which area it is.
125//---------------------------------------------------------------------
vishwac93d6d42015-12-16 11:55:16 -0600126ipmi_fru_area_type get_fru_area_type(uint8_t area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500127{
128 ipmi_fru_area_type type = IPMI_FRU_AREA_TYPE_MAX;
129
Patrick Venturec9508db2018-10-16 17:18:43 -0700130 switch (area_offset)
Vishwa4be4b7a2015-10-31 22:55:50 -0500131 {
132 case IPMI_FRU_INTERNAL_OFFSET:
133 type = IPMI_FRU_AREA_INTERNAL_USE;
134 break;
135
136 case IPMI_FRU_CHASSIS_OFFSET:
137 type = IPMI_FRU_AREA_CHASSIS_INFO;
138 break;
139
140 case IPMI_FRU_BOARD_OFFSET:
141 type = IPMI_FRU_AREA_BOARD_INFO;
142 break;
143
144 case IPMI_FRU_PRODUCT_OFFSET:
145 type = IPMI_FRU_AREA_PRODUCT_INFO;
146 break;
147
148 case IPMI_FRU_MULTI_OFFSET:
149 type = IPMI_FRU_AREA_MULTI_RECORD;
150 break;
151
152 default:
153 type = IPMI_FRU_AREA_TYPE_MAX;
154 }
155
156 return type;
157}
158
vishwac93d6d42015-12-16 11:55:16 -0600159///-----------------------------------------------
160// Validates the data for crc and mandatory fields
161///-----------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700162int verify_fru_data(const uint8_t* data, const size_t len)
vishwac93d6d42015-12-16 11:55:16 -0600163{
164 uint8_t checksum = 0;
165 int rc = -1;
166
167 // Validate for first byte to always have a value of [1]
Patrick Venturec9508db2018-10-16 17:18:43 -0700168 if (data[0] != IPMI_FRU_HDR_BYTE_ZERO)
vishwac93d6d42015-12-16 11:55:16 -0600169 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700170 fprintf(stderr, "Invalid entry:[%d] in byte-0\n", data[0]);
vishwac93d6d42015-12-16 11:55:16 -0600171 return rc;
172 }
173#ifdef __IPMI_DEBUG__
174 else
175 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700176 printf("SUCCESS: Validated [0x%X] in entry_1 of fru_data\n", data[0]);
vishwac93d6d42015-12-16 11:55:16 -0600177 }
178#endif
179
180 // See if the calculated CRC matches with the embedded one.
181 // CRC to be calculated on all except the last one that is CRC itself.
182 checksum = calculate_crc(data, len - 1);
Patrick Venturec9508db2018-10-16 17:18:43 -0700183 if (checksum != data[len - 1])
vishwac93d6d42015-12-16 11:55:16 -0600184 {
185#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700186 fprintf(stderr,
187 "Checksum mismatch."
vishwac93d6d42015-12-16 11:55:16 -0600188 " Calculated:[0x%X], Embedded:[0x%X]\n",
189 checksum, data[len]);
190#endif
191 return rc;
192 }
193#ifdef __IPMI_DEBUG__
194 else
195 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700196 printf("SUCCESS: Checksum matches:[0x%X]\n", checksum);
vishwac93d6d42015-12-16 11:55:16 -0600197 }
198#endif
199
200 return EXIT_SUCCESS;
201}
202
Vishwa4be4b7a2015-10-31 22:55:50 -0500203//------------------------------------------------------------------------
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530204// Gets the value of the key from the fru dictionary of the given section.
205// FRU dictionary is parsed fru data for all the sections.
206//------------------------------------------------------------------------
207
Patrick Venturec9508db2018-10-16 17:18:43 -0700208std::string getFRUValue(const std::string& section, const std::string& key,
209 const std::string& delimiter, IPMIFruInfo& fruData)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530210{
211
212 auto minIndexValue = 0;
213 auto maxIndexValue = 0;
214 std::string fruValue = "";
215 if (section == "Board")
216 {
217 minIndexValue = OPENBMC_VPD_KEY_BOARD_MFG_DATE;
218 maxIndexValue = OPENBMC_VPD_KEY_BOARD_MAX;
219 }
220 else if (section == "Product")
221 {
222 minIndexValue = OPENBMC_VPD_KEY_PRODUCT_MFR;
223 maxIndexValue = OPENBMC_VPD_KEY_PRODUCT_MAX;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530224 }
225 else if (section == "Chassis")
226 {
227 minIndexValue = OPENBMC_VPD_KEY_CHASSIS_TYPE;
228 maxIndexValue = OPENBMC_VPD_KEY_CHASSIS_MAX;
229 }
230
231 auto first = fruData.cbegin() + minIndexValue;
232 auto last = first + (maxIndexValue - minIndexValue) + 1;
233
Patrick Venturec9508db2018-10-16 17:18:43 -0700234 auto itr =
235 std::find_if(first, last, [&key](auto& e) { return key == e.first; });
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530236
237 if (itr != last)
238 {
239 fruValue = itr->second;
240 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530241
Patrick Venturec9508db2018-10-16 17:18:43 -0700242 // if the key is custom property then the value could be in two formats.
243 // 1) custom field 2 = "value".
244 // 2) custom field 2 = "key:value".
245 // if delimiter length = 0 i.e custom field 2 = "value"
Ratan Guptacdde23d2017-02-15 09:29:54 +0530246
247 constexpr auto customProp = "Custom Field";
248 if (key.find(customProp) != std::string::npos)
249 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700250 if (delimiter.length() > 0)
251 {
252 size_t delimiterpos = fruValue.find(delimiter);
253 if (delimiterpos != std::string::npos)
254 fruValue = fruValue.substr(delimiterpos + 1);
255 }
Ratan Guptacdde23d2017-02-15 09:29:54 +0530256 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530257 return fruValue;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530258}
Patrick Venturec9508db2018-10-16 17:18:43 -0700259// Get the inventory service from the mapper.
260auto getService(sdbusplus::bus::bus& bus, const std::string& intf,
261 const std::string& path)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530262{
Patrick Venturec9508db2018-10-16 17:18:43 -0700263 auto mapperCall =
264 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
265 "/xyz/openbmc_project/object_mapper",
266 "xyz.openbmc_project.ObjectMapper", "GetObject");
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530267
268 mapperCall.append(path);
269 mapperCall.append(std::vector<std::string>({intf}));
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530270 std::map<std::string, std::vector<std::string>> mapperResponse;
Patrick Venture5739ac32018-10-16 19:17:41 -0700271
272 try
273 {
274 auto mapperResponseMsg = bus.call(mapperCall);
275 mapperResponseMsg.read(mapperResponse);
276 }
277 catch (const sdbusplus::exception::SdBusError& ex)
278 {
279 log<level::ERR>("Exception from sdbus call",
280 entry("WHAT=%s", ex.what()));
281 throw;
282 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530283
284 if (mapperResponse.begin() == mapperResponse.end())
285 {
286 throw std::runtime_error("ERROR in reading the mapper response");
287 }
288
289 return mapperResponse.begin()->first;
290}
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530291
292//------------------------------------------------------------------------
Vishwa4be4b7a2015-10-31 22:55:50 -0500293// Takes FRU data, invokes Parser for each fru record area and updates
294// Inventory
295//------------------------------------------------------------------------
Patrick Williams21eb0432017-03-31 11:40:48 -0500296int ipmi_update_inventory(fru_area_vec_t& area_vec, sd_bus* bus_sd)
Vishwa4be4b7a2015-10-31 22:55:50 -0500297{
vishwac93d6d42015-12-16 11:55:16 -0600298 // Generic error reporter
Vishwa4be4b7a2015-10-31 22:55:50 -0500299 int rc = 0;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530300 uint8_t fruid = 0;
301 IPMIFruInfo fruData;
Vishwa4be4b7a2015-10-31 22:55:50 -0500302
Vishwa4be4b7a2015-10-31 22:55:50 -0500303 // For each FRU area, extract the needed data , get it parsed and update
304 // the Inventory.
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530305 for (const auto& fruArea : area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500306 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530307 fruid = fruArea->get_fruid();
Vishwa4be4b7a2015-10-31 22:55:50 -0500308 // Fill the container with information
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530309 rc = parse_fru_area((fruArea)->get_type(), (void*)(fruArea)->get_data(),
310 (fruArea)->get_len(), fruData);
311 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500312 {
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530313 std::cerr << "ERROR parsing FRU records\n";
314 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500315 }
316 } // END walking the vector of areas and updating
317
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530318 // For each Fru we have the list of instances which needs to be updated.
319 // Each instance object implements certain interfaces.
320 // Each Interface is having Dbus properties.
321 // Each Dbus Property would be having metaData(eg section,VpdPropertyName).
322
323 // Here we are just printing the object,interface and the properties.
324 // which needs to be called with the new inventory manager implementation.
Patrick Williams49a6fcd2017-04-18 11:49:07 -0500325 sdbusplus::bus::bus bus{bus_sd};
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530326 using namespace std::string_literals;
327 static const auto intf = "xyz.openbmc_project.Inventory.Manager"s;
Brad Bishopec7648d2017-02-23 11:30:16 -0500328 static const auto path = "/xyz/openbmc_project/inventory"s;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530329 std::string service;
330 try
331 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700332 service = getService(bus, intf, path);
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530333 }
Patrick Venture5739ac32018-10-16 19:17:41 -0700334 catch (const std::exception& e)
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530335 {
336 std::cerr << e.what() << "\n";
337 return -1;
338 }
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530339
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530340 auto iter = frus.find(fruid);
341 if (iter == frus.end())
342 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530343 std::cerr << "ERROR Unable to get the fru info for FRU="
344 << static_cast<int>(fruid) << "\n";
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530345 return -1;
346 }
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530347
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530348 auto& instanceList = iter->second;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530349 if (instanceList.size() <= 0)
350 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530351 std::cout << "Object List empty for this FRU="
352 << static_cast<int>(fruid) << "\n";
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530353 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530354
355 ObjectMap objects;
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530356 for (auto& instance : instanceList)
357 {
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530358 InterfaceMap interfaces;
Ratan Guptac19c0542018-02-04 23:24:44 +0530359 const auto& extrasIter = extras.find(instance.path);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530360
Ratan Guptac19c0542018-02-04 23:24:44 +0530361 for (auto& interfaceList : instance.interfaces)
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530362 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700363 PropertyMap props; // store all the properties
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530364 for (auto& properties : interfaceList.second)
365 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500366 std::string value;
367 decltype(auto) pdata = properties.second;
Ratan Guptacdde23d2017-02-15 09:29:54 +0530368
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500369 if (!pdata.section.empty() && !pdata.property.empty())
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530370 {
Patrick Williamsaeb726d2017-06-01 19:00:43 -0500371 value = getFRUValue(pdata.section, pdata.property,
372 pdata.delimiter, fruData);
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530373 }
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530374 props.emplace(std::move(properties.first), std::move(value));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530375 }
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600376 // Check and update extra properties
Patrick Venturec9508db2018-10-16 17:18:43 -0700377 if (extras.end() != extrasIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600378 {
379 const auto& propsIter =
380 (extrasIter->second).find(interfaceList.first);
Patrick Venturec9508db2018-10-16 17:18:43 -0700381 if ((extrasIter->second).end() != propsIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600382 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700383 for (const auto& map : propsIter->second)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600384 {
385 props.emplace(map.first, map.second);
386 }
387 }
388 }
389 interfaces.emplace(std::move(interfaceList.first),
390 std::move(props));
Ratan Guptacb0d4e52016-12-22 19:05:57 +0530391 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500392
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600393 // Call the inventory manager
Ratan Guptac19c0542018-02-04 23:24:44 +0530394 sdbusplus::message::object_path path = instance.path;
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600395 // Check and update extra properties
Patrick Venturec9508db2018-10-16 17:18:43 -0700396 if (extras.end() != extrasIter)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600397 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700398 for (const auto& entry : extrasIter->second)
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600399 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700400 if (interfaces.end() == interfaces.find(entry.first))
Deepak Kodihalli788bd1f2017-02-20 01:21:59 -0600401 {
402 interfaces.emplace(entry.first, entry.second);
403 }
404 }
405 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700406 objects.emplace(path, interfaces);
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530407 }
408
Patrick Venturec9508db2018-10-16 17:18:43 -0700409 auto pimMsg = bus.new_method_call(service.c_str(), path.c_str(),
410 intf.c_str(), "Notify");
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530411 pimMsg.append(std::move(objects));
Patrick Venture5739ac32018-10-16 19:17:41 -0700412
413 try
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530414 {
Patrick Venture5739ac32018-10-16 19:17:41 -0700415 auto inventoryMgrResponseMsg = bus.call(pimMsg);
416 }
417 catch (const sdbusplus::exception::SdBusError& ex)
418 {
419 log<level::ERR>("Error in notify call", entry("WHAT=%s", ex.what()));
Ratan Gupta0fc20ed2017-02-06 19:59:12 +0530420 return -1;
Ratan Gupta0b77cfa2017-01-31 17:32:31 +0530421 }
Patrick Venture5739ac32018-10-16 19:17:41 -0700422
Vishwa4be4b7a2015-10-31 22:55:50 -0500423 return rc;
424}
425
vishwac93d6d42015-12-16 11:55:16 -0600426///----------------------------------------------------
427// Checks if a particular fru area is populated or not
428///----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700429bool remove_invalid_area(const std::unique_ptr<ipmi_fru>& fru_area)
Vishwa4be4b7a2015-10-31 22:55:50 -0500430{
Deepak Kodihalli89ededd2017-02-11 01:59:32 -0600431 // Filter the ones that are empty
Patrick Venturec9508db2018-10-16 17:18:43 -0700432 if (!(fru_area->get_len()))
vishwac93d6d42015-12-16 11:55:16 -0600433 {
434 return true;
435 }
436 return false;
437}
Vishwa4be4b7a2015-10-31 22:55:50 -0500438
vishwac93d6d42015-12-16 11:55:16 -0600439///----------------------------------------------------------------------------------
440// Populates various FRU areas
441// @prereq : This must be called only after validating common header.
442///----------------------------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700443int ipmi_populate_fru_areas(uint8_t* fru_data, const size_t data_len,
444 fru_area_vec_t& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600445{
vishwac93d6d42015-12-16 11:55:16 -0600446 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500447
vishwac93d6d42015-12-16 11:55:16 -0600448 // Now walk the common header and see if the file size has atleast the last
449 // offset mentioned by the common_hdr. If the file size is less than the
450 // offset of any if the fru areas mentioned in the common header, then we do
451 // not have a complete file.
Patrick Venturec9508db2018-10-16 17:18:43 -0700452 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
453 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600454 {
Yi Li75c2d462016-04-11 16:57:46 +0800455 rc = -1;
vishwac93d6d42015-12-16 11:55:16 -0600456 // Actual offset in the payload is the offset mentioned in common header
Gunnar Millsc19b8132018-06-14 08:56:17 -0500457 // multiplied by 8. Common header is always the first 8 bytes.
Patrick Venture50ddfe52018-10-16 17:16:32 -0700458 size_t area_offset = fru_data[fru_entry] * IPMI_EIGHT_BYTES;
Patrick Venturec9508db2018-10-16 17:18:43 -0700459 if (area_offset && (data_len < (area_offset + 2)))
vishwac93d6d42015-12-16 11:55:16 -0600460 {
461 // Our file size is less than what it needs to be. +2 because we are
462 // using area len that is at 2 byte off area_offset
Patrick Venturec9508db2018-10-16 17:18:43 -0700463 fprintf(stderr, "fru file is incomplete. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600464 return rc;
465 }
Patrick Venturec9508db2018-10-16 17:18:43 -0700466 else if (area_offset)
vishwac93d6d42015-12-16 11:55:16 -0600467 {
468 // Read 2 bytes to know the actual size of area.
469 uint8_t area_hdr[2] = {0};
Patrick Venturec9508db2018-10-16 17:18:43 -0700470 memcpy(area_hdr, &((uint8_t*)fru_data)[area_offset],
471 sizeof(area_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500472
vishwac93d6d42015-12-16 11:55:16 -0600473 // Size of this area will be the 2nd byte in the fru area header.
Patrick Venturec9508db2018-10-16 17:18:43 -0700474 size_t area_len = area_hdr[1] * IPMI_EIGHT_BYTES;
vishwac93d6d42015-12-16 11:55:16 -0600475 uint8_t area_data[area_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500476
Patrick Williams3365ec82016-08-17 17:45:18 -0500477 printf("fru data size:[%zd], area offset:[%zd], area_size:[%zd]\n",
Patrick Venturec9508db2018-10-16 17:18:43 -0700478 data_len, area_offset, area_len);
Vishwa4be4b7a2015-10-31 22:55:50 -0500479
vishwac93d6d42015-12-16 11:55:16 -0600480 // See if we really have that much buffer. We have area offset amd
481 // from there, the actual len.
Patrick Venturec9508db2018-10-16 17:18:43 -0700482 if (data_len < (area_len + area_offset))
vishwac93d6d42015-12-16 11:55:16 -0600483 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700484 fprintf(stderr, "Incomplete Fru file.. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600485 return rc;
486 }
487
488 // Save off the data.
Patrick Venturec9508db2018-10-16 17:18:43 -0700489 memcpy(area_data, &((uint8_t*)fru_data)[area_offset], area_len);
vishwac93d6d42015-12-16 11:55:16 -0600490
491 // Validate the crc
492 rc = verify_fru_data(area_data, area_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700493 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600494 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700495 fprintf(stderr, "Error validating fru area. offset:[%zd]\n",
496 area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600497 return rc;
498 }
499 else
500 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700501 printf("Successfully verified area checksum. offset:[%zd]\n",
502 area_offset);
vishwac93d6d42015-12-16 11:55:16 -0600503 }
504
505 // We already have a vector that is passed to us containing all
506 // of the fields populated. Update the data portion now.
Patrick Venturec9508db2018-10-16 17:18:43 -0700507 for (auto& iter : fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600508 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700509 if ((iter)->get_type() == get_fru_area_type(fru_entry))
vishwac93d6d42015-12-16 11:55:16 -0600510 {
511 (iter)->set_data(area_data, area_len);
512 }
513 }
514 } // If we have fru data present
Patrick Venturec9508db2018-10-16 17:18:43 -0700515 } // Walk common_hdr
vishwac93d6d42015-12-16 11:55:16 -0600516
517 // Not all the fields will be populated in a fru data. Mostly all cases will
518 // not have more than 2 or 3.
519 fru_area_vec.erase(std::remove_if(fru_area_vec.begin(), fru_area_vec.end(),
Patrick Venturec9508db2018-10-16 17:18:43 -0700520 remove_invalid_area),
521 fru_area_vec.end());
vishwac93d6d42015-12-16 11:55:16 -0600522
523 return EXIT_SUCCESS;
524}
525
526///---------------------------------------------------------
527// Validates the fru data per ipmi common header constructs.
528// Returns with updated common_hdr and also file_size
529//----------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700530int ipmi_validate_common_hdr(const uint8_t* fru_data, const size_t data_len)
vishwac93d6d42015-12-16 11:55:16 -0600531{
532 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500533
534 uint8_t common_hdr[sizeof(struct common_header)] = {0};
Patrick Venturec9508db2018-10-16 17:18:43 -0700535 if (data_len >= sizeof(common_hdr))
Vishwa4be4b7a2015-10-31 22:55:50 -0500536 {
vishwac93d6d42015-12-16 11:55:16 -0600537 memcpy(common_hdr, fru_data, sizeof(common_hdr));
Vishwa4be4b7a2015-10-31 22:55:50 -0500538 }
539 else
540 {
Patrick Williams3365ec82016-08-17 17:45:18 -0500541 fprintf(stderr, "Incomplete fru data file. Size:[%zd]\n", data_len);
vishwac93d6d42015-12-16 11:55:16 -0600542 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500543 }
544
vishwac93d6d42015-12-16 11:55:16 -0600545 // Verify the crc and size
546 rc = verify_fru_data(common_hdr, sizeof(common_hdr));
Patrick Venturec9508db2018-10-16 17:18:43 -0700547 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500548 {
vishwac93d6d42015-12-16 11:55:16 -0600549 fprintf(stderr, "Failed to validate common header\n");
550 return rc;
Vishwa4be4b7a2015-10-31 22:55:50 -0500551 }
552
vishwac93d6d42015-12-16 11:55:16 -0600553 return EXIT_SUCCESS;
554}
Vishwa4be4b7a2015-10-31 22:55:50 -0500555
vishwac93d6d42015-12-16 11:55:16 -0600556//------------------------------------------------------------
557// Cleanup routine
558//------------------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700559int cleanup_error(FILE* fru_fp, fru_area_vec_t& fru_area_vec)
vishwac93d6d42015-12-16 11:55:16 -0600560{
Patrick Venturec9508db2018-10-16 17:18:43 -0700561 if (fru_fp != NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500562 {
vishwac93d6d42015-12-16 11:55:16 -0600563 fclose(fru_fp);
564 fru_fp = NULL;
565 }
vishwaf3ca3522015-12-02 10:35:13 -0600566
Patrick Venturec9508db2018-10-16 17:18:43 -0700567 if (!(fru_area_vec.empty()))
Vishwa4be4b7a2015-10-31 22:55:50 -0500568 {
vishwac93d6d42015-12-16 11:55:16 -0600569 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500570 }
vishwaf3ca3522015-12-02 10:35:13 -0600571
Patrick Venturec9508db2018-10-16 17:18:43 -0700572 return -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500573}
574
575///-----------------------------------------------------
576// Accepts the filename and validates per IPMI FRU spec
577//----------------------------------------------------
Patrick Venturec9508db2018-10-16 17:18:43 -0700578int ipmi_validate_fru_area(const uint8_t fruid, const char* fru_file_name,
579 sd_bus* bus_type, const bool bmc_fru)
Vishwa4be4b7a2015-10-31 22:55:50 -0500580{
vishwac93d6d42015-12-16 11:55:16 -0600581 size_t data_len = 0;
582 size_t bytes_read = 0;
583 int rc = -1;
Vishwa4be4b7a2015-10-31 22:55:50 -0500584
vishwac93d6d42015-12-16 11:55:16 -0600585 // Vector that holds individual IPMI FRU AREAs. Although MULTI and INTERNAL
586 // are not used, keeping it here for completeness.
587 fru_area_vec_t fru_area_vec;
Yi Li75c2d462016-04-11 16:57:46 +0800588
Patrick Venturec9508db2018-10-16 17:18:43 -0700589 for (uint8_t fru_entry = IPMI_FRU_INTERNAL_OFFSET;
590 fru_entry < (sizeof(struct common_header) - 2); fru_entry++)
vishwac93d6d42015-12-16 11:55:16 -0600591 {
592 // Create an object and push onto a vector.
Patrick Venturec9508db2018-10-16 17:18:43 -0700593 std::unique_ptr<ipmi_fru> fru_area = std::make_unique<ipmi_fru>(
594 fruid, get_fru_area_type(fru_entry), bus_type, bmc_fru);
vishwac93d6d42015-12-16 11:55:16 -0600595
596 // Physically being present
vishwa2f5a3cf2016-05-30 02:25:21 -0500597 bool present = access(fru_file_name, F_OK) == 0;
vishwac93d6d42015-12-16 11:55:16 -0600598 fru_area->set_present(present);
599
vishwac93d6d42015-12-16 11:55:16 -0600600 fru_area_vec.emplace_back(std::move(fru_area));
601 }
602
Patrick Venturec9508db2018-10-16 17:18:43 -0700603 FILE* fru_fp = fopen(fru_file_name, "rb");
604 if (fru_fp == NULL)
Vishwa4be4b7a2015-10-31 22:55:50 -0500605 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700606 fprintf(stderr, "ERROR: opening:[%s]\n", fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500607 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600608 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500609 }
610
vishwac93d6d42015-12-16 11:55:16 -0600611 // Get the size of the file to see if it meets minimum requirement
Patrick Venturec9508db2018-10-16 17:18:43 -0700612 if (fseek(fru_fp, 0, SEEK_END))
Vishwa4be4b7a2015-10-31 22:55:50 -0500613 {
614 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600615 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500616 }
617
vishwac93d6d42015-12-16 11:55:16 -0600618 // Allocate a buffer to hold entire file content
619 data_len = ftell(fru_fp);
620 uint8_t fru_data[data_len] = {0};
Vishwa4be4b7a2015-10-31 22:55:50 -0500621
vishwac93d6d42015-12-16 11:55:16 -0600622 rewind(fru_fp);
623 bytes_read = fread(fru_data, data_len, 1, fru_fp);
Patrick Venturec9508db2018-10-16 17:18:43 -0700624 if (bytes_read != 1)
Vishwa4be4b7a2015-10-31 22:55:50 -0500625 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700626 fprintf(stderr, "Failed reading fru data. Bytes_read=[%zd]\n",
627 bytes_read);
Vishwa4be4b7a2015-10-31 22:55:50 -0500628 perror("Error:");
vishwac93d6d42015-12-16 11:55:16 -0600629 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500630 }
Vishwa4be4b7a2015-10-31 22:55:50 -0500631
vishwac93d6d42015-12-16 11:55:16 -0600632 // We are done reading.
633 fclose(fru_fp);
634 fru_fp = NULL;
635
636 rc = ipmi_validate_common_hdr(fru_data, data_len);
Patrick Venturec9508db2018-10-16 17:18:43 -0700637 if (rc < 0)
Vishwa4be4b7a2015-10-31 22:55:50 -0500638 {
vishwac93d6d42015-12-16 11:55:16 -0600639 return cleanup_error(fru_fp, fru_area_vec);
640 }
641
Patrick Venturec9508db2018-10-16 17:18:43 -0700642 // Now that we validated the common header, populate various fru sections if
643 // we have them here.
vishwac93d6d42015-12-16 11:55:16 -0600644 rc = ipmi_populate_fru_areas(fru_data, data_len, fru_area_vec);
Patrick Venturec9508db2018-10-16 17:18:43 -0700645 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600646 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700647 fprintf(stderr, "Populating FRU areas failed for:[%d]\n", fruid);
vishwac93d6d42015-12-16 11:55:16 -0600648 return cleanup_error(fru_fp, fru_area_vec);
Vishwa4be4b7a2015-10-31 22:55:50 -0500649 }
650 else
651 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700652 printf("SUCCESS: Populated FRU areas for:[%s]\n", fru_file_name);
Vishwa4be4b7a2015-10-31 22:55:50 -0500653 }
654
vishwac93d6d42015-12-16 11:55:16 -0600655#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700656 for (auto& iter : fru_area_vec)
Vishwa4be4b7a2015-10-31 22:55:50 -0500657 {
Patrick Venturec9508db2018-10-16 17:18:43 -0700658 printf("FRU ID : [%d]\n", (iter)->get_fruid());
659 printf("AREA NAME : [%s]\n", (iter)->get_name());
660 printf("TYPE : [%d]\n", (iter)->get_type());
661 printf("LEN : [%d]\n", (iter)->get_len());
vishwac93d6d42015-12-16 11:55:16 -0600662 printf("BUS NAME : [%s]\n", (iter)->get_bus_name());
663 printf("OBJ PATH : [%s]\n", (iter)->get_obj_path());
664 printf("INTF NAME :[%s]\n", (iter)->get_intf_name());
Vishwa4be4b7a2015-10-31 22:55:50 -0500665 }
vishwac93d6d42015-12-16 11:55:16 -0600666#endif
667
668 // If the vector is populated with everything, then go ahead and update the
669 // inventory.
Patrick Venturec9508db2018-10-16 17:18:43 -0700670 if (!(fru_area_vec.empty()))
vishwac93d6d42015-12-16 11:55:16 -0600671 {
672
673#ifdef __IPMI_DEBUG__
Patrick Venturec9508db2018-10-16 17:18:43 -0700674 printf("\n SIZE of vector is : [%d] \n", fru_area_vec.size());
vishwac93d6d42015-12-16 11:55:16 -0600675#endif
Patrick Williams21eb0432017-03-31 11:40:48 -0500676 rc = ipmi_update_inventory(fru_area_vec, bus_type);
Patrick Venturec9508db2018-10-16 17:18:43 -0700677 if (rc < 0)
vishwac93d6d42015-12-16 11:55:16 -0600678 {
679 fprintf(stderr, "Error updating inventory\n");
680 }
681 }
682
683 // we are done with all that we wanted to do. This will do the job of
684 // calling any destructors too.
685 fru_area_vec.clear();
Vishwa4be4b7a2015-10-31 22:55:50 -0500686
687 return rc;
688}