blob: bf4027529caf8614b9a9ba02f1fb83e325458f71 [file] [log] [blame]
Vishwanatha Subbanna07655062017-07-14 20:31:57 +05301#include "config.h"
Patrick Venture02261c02018-10-31 15:16:23 -07002
3#include "oemhandler.hpp"
4
Tom Joseph246bc0d2017-10-17 16:08:38 +05305#include "elog-errors.hpp"
Matt Spinler34aca012018-09-25 11:21:06 -05006#include "local_users.hpp"
Patrick Venture02261c02018-10-31 15:16:23 -07007
8#include <endian.h>
Chris Austen4d98c1e2015-10-13 14:33:50 -05009#include <host-ipmid/ipmid-api.h>
10#include <stdio.h>
11#include <string.h>
Vishwanatha Subbanna07655062017-07-14 20:31:57 +053012#include <systemd/sd-bus.h>
Patrick Venture02261c02018-10-31 15:16:23 -070013
14#include <fstream>
15#include <functional>
Vishwanatha Subbanna07655062017-07-14 20:31:57 +053016#include <host-interface.hpp>
Patrick Venture02261c02018-10-31 15:16:23 -070017#include <host-ipmid/ipmid-host-cmd.hpp>
18#include <memory>
Tom Joseph3503fdc2019-01-10 11:25:27 +053019#include <org/open_power/Host/error.hpp>
Tom Joseph246bc0d2017-10-17 16:08:38 +053020#include <org/open_power/OCC/Metrics/error.hpp>
Patrick Venture02261c02018-10-31 15:16:23 -070021#include <sdbusplus/bus.hpp>
Chris Austen4d98c1e2015-10-13 14:33:50 -050022
Andrew Jeffery8fb3f032018-07-27 16:45:44 +093023void register_netfn_ibm_oem_commands() __attribute__((constructor));
Chris Austen4d98c1e2015-10-13 14:33:50 -050024
Patrick Venture02261c02018-10-31 15:16:23 -070025const char* g_esel_path = "/tmp/esel";
Chris Austen29a8e0f2015-10-30 11:44:38 -050026uint16_t g_record_id = 0x0001;
Tom Joseph246bc0d2017-10-17 16:08:38 +053027using namespace phosphor::logging;
28constexpr auto occMetricsType = 0xDD;
29
Tom Josephb61b1072019-01-28 12:32:52 +053030extern const ObjectIDMap invSensors;
31const std::map<uint8_t, Entry::Level> severityMap{
32 {0x10, Entry::Level::Warning}, // Recoverable error
33 {0x20, Entry::Level::Warning}, // Predictive error
34 {0x40, Entry::Level::Error}, // Unrecoverable error
35 {0x50, Entry::Level::Error}, // Critical error
36 {0x60, Entry::Level::Error}, // Error from a diagnostic test
37 {0x70, Entry::Level::Warning}, // Recoverable symptom
38 {0xFF, Entry::Level::Error}, // Unknown error
39};
40
41Entry::Level mapSeverity(const std::string& eSELData)
42{
43 constexpr size_t severityOffset = 0x4A;
44
45 if (eSELData.size() > severityOffset)
46 {
47 // Dive in to the IBM log to find the severity
48 uint8_t sev = 0xF0 & eSELData[severityOffset];
49
50 auto find = severityMap.find(sev);
51 if (find != severityMap.end())
52 {
53 return find->second;
54 }
55 }
56
57 // Default to Entry::Level::Error if a matching is not found.
58 return Entry::Level::Error;
59}
60
61std::string mapCalloutAssociation(const std::string& eSELData)
62{
63 auto rec = reinterpret_cast<const SELEventRecord*>(&eSELData[0]);
64 uint8_t sensor = rec->sensorNum;
65
66 /*
67 * Search the sensor number to inventory path mapping to figure out the
68 * inventory associated with the ESEL.
69 */
70 auto found = std::find_if(invSensors.begin(), invSensors.end(),
71 [&sensor](const auto& iter) {
72 return (iter.second.sensorID == sensor);
73 });
74 if (found != invSensors.end())
75 {
76 return found->first;
77 }
78
79 return {};
80}
81
Tom Joseph246bc0d2017-10-17 16:08:38 +053082std::string readESEL(const char* fileName)
83{
Patrick Venture02261c02018-10-31 15:16:23 -070084 std::string content{};
Tom Joseph246bc0d2017-10-17 16:08:38 +053085
86 std::ifstream handle(fileName);
87
88 if (handle.fail())
89 {
Patrick Venture02261c02018-10-31 15:16:23 -070090 log<level::ERR>("Failed to open eSEL", entry("FILENAME=%s", fileName));
Tom Joseph246bc0d2017-10-17 16:08:38 +053091 return content;
92 }
93
94 handle.seekg(0, std::ios::end);
95 content.resize(handle.tellg());
96 handle.seekg(0, std::ios::beg);
97 handle.read(&content[0], content.size());
98 handle.close();
99
100 return content;
101}
102
103void createOCCLogEntry(const std::string& eSELData)
104{
105 // Each byte in eSEL is formatted as %02x with a space between bytes and
106 // insert '/0' at the end of the character array.
107 constexpr auto byteSeperator = 3;
108
Patrick Venture02261c02018-10-31 15:16:23 -0700109 std::unique_ptr<char[]> data(
110 new char[(eSELData.size() * byteSeperator) + 1]());
Tom Joseph246bc0d2017-10-17 16:08:38 +0530111
112 for (size_t i = 0; i < eSELData.size(); i++)
113 {
114 sprintf(&data[i * byteSeperator], "%02x ", eSELData[i]);
115 }
116 data[eSELData.size() * byteSeperator] = '\0';
117
Patrick Venture02261c02018-10-31 15:16:23 -0700118 using error = sdbusplus::org::open_power::OCC::Metrics::Error::Event;
Tom Joseph246bc0d2017-10-17 16:08:38 +0530119 using metadata = org::open_power::OCC::Metrics::Event;
120
121 report<error>(metadata::ESEL(data.get()));
122}
123
Tom Joseph3503fdc2019-01-10 11:25:27 +0530124void createHostEntry(const std::string& eSELData)
125{
126 // Each byte in eSEL is formatted as %02x with a space between bytes and
127 // insert '/0' at the end of the character array.
Tom Joseph3503fdc2019-01-10 11:25:27 +0530128 constexpr auto byteSeperator = 3;
129
Tom Josephb61b1072019-01-28 12:32:52 +0530130 auto sev = mapSeverity(eSELData);
131 auto inventoryPath = mapCalloutAssociation(eSELData);
Tom Joseph3503fdc2019-01-10 11:25:27 +0530132
Tom Josephb61b1072019-01-28 12:32:52 +0530133 if (!inventoryPath.empty())
Tom Joseph3503fdc2019-01-10 11:25:27 +0530134 {
Tom Josephb61b1072019-01-28 12:32:52 +0530135 std::unique_ptr<char[]> data(
136 new char[(eSELData.size() * byteSeperator) + 1]());
137
138 for (size_t i = 0; i < eSELData.size(); i++)
139 {
140 sprintf(&data[i * byteSeperator], "%02x ", eSELData[i]);
141 }
142 data[eSELData.size() * byteSeperator] = '\0';
143
144 using hosterror = sdbusplus::org::open_power::Host::Error::Event;
145 using hostmetadata = org::open_power::Host::Event;
146
147 report<hosterror>(
148 sev, hostmetadata::ESEL(data.get()),
149 hostmetadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Tom Joseph3503fdc2019-01-10 11:25:27 +0530150 }
Tom Joseph3503fdc2019-01-10 11:25:27 +0530151}
152
Chris Austen4d98c1e2015-10-13 14:33:50 -0500153///////////////////////////////////////////////////////////////////////////////
Patrick Williams24fa5a92015-10-30 14:53:57 -0500154// For the First partial add eSEL the SEL Record ID and offset
155// value should be 0x0000. The extended data needs to be in
156// the form of an IPMI SEL Event Record, with Event sensor type
157// of 0xDF and Event Message format of 0x04. The returned
Chris Austen4d98c1e2015-10-13 14:33:50 -0500158// Record ID should be used for all partial eSEL adds.
159//
Chris Austenba54afb2016-02-19 23:18:42 -0600160// This function creates a /tmp/esel file to store the
Chris Austen4d98c1e2015-10-13 14:33:50 -0500161// incoming partial esel. It is the role of some other
Patrick Williams24fa5a92015-10-30 14:53:57 -0500162// function to commit the error log in to long term
163// storage. Likely via the ipmi add_sel command.
Chris Austen4d98c1e2015-10-13 14:33:50 -0500164///////////////////////////////////////////////////////////////////////////////
Patrick Williams24fa5a92015-10-30 14:53:57 -0500165ipmi_ret_t ipmi_ibm_oem_partial_esel(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture02261c02018-10-31 15:16:23 -0700166 ipmi_request_t request,
167 ipmi_response_t response,
168 ipmi_data_len_t data_len,
169 ipmi_context_t context)
Chris Austen4d98c1e2015-10-13 14:33:50 -0500170{
Patrick Venture02261c02018-10-31 15:16:23 -0700171 uint8_t* reqptr = (uint8_t*)request;
172 esel_request_t esel_req;
173 FILE* fp;
174 int r = 0;
175 uint8_t rlen;
176 ipmi_ret_t rc = IPMI_CC_OK;
177 const char* pio;
Chris Austen4d98c1e2015-10-13 14:33:50 -0500178
Patrick Venture02261c02018-10-31 15:16:23 -0700179 esel_req.resid = le16toh((((uint16_t)reqptr[1]) << 8) + reqptr[0]);
180 esel_req.selrecord = le16toh((((uint16_t)reqptr[3]) << 8) + reqptr[2]);
181 esel_req.offset = le16toh((((uint16_t)reqptr[5]) << 8) + reqptr[4]);
182 esel_req.progress = reqptr[6];
Nan Lib6c4c562016-03-30 17:06:13 +0800183
Patrick Venture02261c02018-10-31 15:16:23 -0700184 // According to IPMI spec, Reservation ID must be checked.
185 if (!checkSELReservation(esel_req.resid))
186 {
187 // 0xc5 means Reservation Cancelled or Invalid Reservation ID.
188 printf("Used Reservation ID = %d\n", esel_req.resid);
189 rc = IPMI_CC_INVALID_RESERVATION_ID;
Nan Lib6c4c562016-03-30 17:06:13 +0800190
Patrick Venture02261c02018-10-31 15:16:23 -0700191 // clean g_esel_path.
192 r = remove(g_esel_path);
193 if (r < 0)
194 fprintf(stderr, "Error deleting %s\n", g_esel_path);
Nan Lib6c4c562016-03-30 17:06:13 +0800195
Patrick Venture02261c02018-10-31 15:16:23 -0700196 return rc;
197 }
Nan Lib6c4c562016-03-30 17:06:13 +0800198
199 // OpenPOWER Host Interface spec says if RecordID and Offset are
Patrick Venture02261c02018-10-31 15:16:23 -0700200 // 0 then then this is a new request
201 if (!esel_req.selrecord && !esel_req.offset)
202 pio = "wb";
203 else
204 pio = "rb+";
Chris Austen4d98c1e2015-10-13 14:33:50 -0500205
Patrick Venture02261c02018-10-31 15:16:23 -0700206 rlen = (*data_len) - (uint8_t)(sizeof(esel_request_t));
Chris Austen4d98c1e2015-10-13 14:33:50 -0500207
Patrick Venture02261c02018-10-31 15:16:23 -0700208 if ((fp = fopen(g_esel_path, pio)) != NULL)
Tom Joseph246bc0d2017-10-17 16:08:38 +0530209 {
Patrick Venture02261c02018-10-31 15:16:23 -0700210 fseek(fp, esel_req.offset, SEEK_SET);
211 fwrite(reqptr + (uint8_t)(sizeof(esel_request_t)), rlen, 1, fp);
212 fclose(fp);
Chris Austen4d98c1e2015-10-13 14:33:50 -0500213
Patrick Venture02261c02018-10-31 15:16:23 -0700214 *data_len = sizeof(g_record_id);
215 memcpy(response, &g_record_id, *data_len);
216 }
217 else
218 {
219 fprintf(stderr, "Error trying to perform %s for esel%s\n", pio,
220 g_esel_path);
221 rc = IPMI_CC_INVALID;
222 *data_len = 0;
223 }
Tom Joseph246bc0d2017-10-17 16:08:38 +0530224
Patrick Venture02261c02018-10-31 15:16:23 -0700225 // The first bit presents that this is the last partial packet
226 // coming down. If that is the case advance the record id so we
227 // don't overlap logs. This allows anyone to establish a log
228 // directory system.
229 if (esel_req.progress & 1)
230 {
231 g_record_id++;
Tom Joseph246bc0d2017-10-17 16:08:38 +0530232
Patrick Venture02261c02018-10-31 15:16:23 -0700233 auto eSELData = readESEL(g_esel_path);
234
235 if (eSELData.empty())
236 {
237 return IPMI_CC_UNSPECIFIED_ERROR;
238 }
239
240 // If the eSEL record type is OCC metrics, then create the OCC log
241 // entry.
242 if (eSELData[2] == occMetricsType)
243 {
244 createOCCLogEntry(eSELData);
245 }
Tom Joseph3503fdc2019-01-10 11:25:27 +0530246 else
247 {
248 createHostEntry(eSELData);
249 }
Tom Joseph246bc0d2017-10-17 16:08:38 +0530250 }
251
252 return rc;
Chris Austen4d98c1e2015-10-13 14:33:50 -0500253}
254
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600255// Prepare for FW Update.
256// Execute needed commands to prepare the system for a fw update from the host.
257ipmi_ret_t ipmi_ibm_oem_prep_fw_update(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Patrick Venture02261c02018-10-31 15:16:23 -0700258 ipmi_request_t request,
259 ipmi_response_t response,
260 ipmi_data_len_t data_len,
261 ipmi_context_t context)
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600262{
263 ipmi_ret_t ipmi_rc = IPMI_CC_OK;
264 *data_len = 0;
265
266 int rc = 0;
267 std::ofstream rwfs_file;
Patrick Venture02261c02018-10-31 15:16:23 -0700268 const char* busname = "org.openbmc.control.Bmc";
269 const char* objname = "/org/openbmc/control/bmc0";
270 const char* iface = "org.openbmc.control.Bmc";
271 sd_bus* bus = ipmid_get_sd_bus_connection();
272 sd_bus_message* reply = NULL;
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600273 sd_bus_error error = SD_BUS_ERROR_NULL;
274 int r = 0;
275
276 // Set one time flag
Patrick Venture02261c02018-10-31 15:16:23 -0700277 rc = system(
278 "fw_setenv openbmconce copy-files-to-ram copy-base-filesystem-to-ram");
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600279 rc = WEXITSTATUS(rc);
Patrick Venture02261c02018-10-31 15:16:23 -0700280 if (rc != 0)
281 {
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600282 fprintf(stderr, "fw_setenv openbmconce failed with rc=%d\n", rc);
Andrew Geisslerd9296052017-06-15 14:57:25 -0500283 return IPMI_CC_UNSPECIFIED_ERROR;
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600284 }
285
286 // Touch the image-rwfs file to perform an empty update to force the save
Patrick Venture02261c02018-10-31 15:16:23 -0700287 // in case we're already in ram and the flash is the same causing the ram
288 // files to not be copied back to flash
289 rwfs_file.open("/run/initramfs/image-rwfs",
290 std::ofstream::out | std::ofstream::app);
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600291 rwfs_file.close();
292
293 // Reboot the BMC for settings to take effect
Patrick Venture02261c02018-10-31 15:16:23 -0700294 r = sd_bus_call_method(bus, busname, objname, iface, "warmReset", &error,
295 &reply, NULL);
296 if (r < 0)
297 {
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600298 fprintf(stderr, "Failed to reset BMC: %s\n", strerror(-r));
299 return -1;
300 }
301 printf("Warning: BMC is going down for reboot!\n");
302 sd_bus_error_free(&error);
303 reply = sd_bus_message_unref(reply);
304
305 return ipmi_rc;
306}
Chris Austen4d98c1e2015-10-13 14:33:50 -0500307
Matt Spinler32884712018-09-28 13:56:20 -0500308ipmi_ret_t ipmi_ibm_oem_reset_bmc_auth(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
309 ipmi_request_t request,
310 ipmi_response_t response,
311 ipmi_data_len_t data_len,
312 ipmi_context_t context)
Adriana Kobylak81e23102018-08-09 14:44:19 -0500313{
Matt Spinler34aca012018-09-25 11:21:06 -0500314 ipmi_ret_t rc;
315
316 rc = local::users::enableUsers();
317
318 return rc;
Adriana Kobylak81e23102018-08-09 14:44:19 -0500319}
320
Patrick Venture02261c02018-10-31 15:16:23 -0700321namespace
322{
Vishwanatha Subbanna07655062017-07-14 20:31:57 +0530323// Storage to keep the object alive during process life
324std::unique_ptr<open_power::host::command::Host> opHost
Patrick Venture02261c02018-10-31 15:16:23 -0700325 __attribute__((init_priority(101)));
Vishwanatha Subbanna07655062017-07-14 20:31:57 +0530326std::unique_ptr<sdbusplus::server::manager::manager> objManager
Patrick Venture02261c02018-10-31 15:16:23 -0700327 __attribute__((init_priority(101)));
328} // namespace
Vishwanatha Subbanna07655062017-07-14 20:31:57 +0530329
Andrew Jeffery8fb3f032018-07-27 16:45:44 +0930330void register_netfn_ibm_oem_commands()
Chris Austen4d98c1e2015-10-13 14:33:50 -0500331{
Patrick Venture02261c02018-10-31 15:16:23 -0700332 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_IBM_OEM,
333 IPMI_CMD_PESEL);
334 ipmi_register_callback(NETFUN_IBM_OEM, IPMI_CMD_PESEL, NULL,
335 ipmi_ibm_oem_partial_esel, SYSTEM_INTERFACE);
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600336
Patrick Venture02261c02018-10-31 15:16:23 -0700337 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_OEM,
338 IPMI_CMD_PREP_FW_UPDATE);
339 ipmi_register_callback(NETFUN_OEM, IPMI_CMD_PREP_FW_UPDATE, NULL,
340 ipmi_ibm_oem_prep_fw_update, SYSTEM_INTERFACE);
Adriana Kobylak187bfce2016-03-04 11:55:43 -0600341
Matt Spinler32884712018-09-28 13:56:20 -0500342 ipmi_register_callback(NETFUN_IBM_OEM, IPMI_CMD_RESET_BMC_AUTH, NULL,
343 ipmi_ibm_oem_reset_bmc_auth, SYSTEM_INTERFACE);
Adriana Kobylak81e23102018-08-09 14:44:19 -0500344
Vishwanatha Subbanna07655062017-07-14 20:31:57 +0530345 // Create new object on the bus
346 auto objPath = std::string{CONTROL_HOST_OBJ_MGR} + '/' + HOST_NAME + '0';
347
348 // Add sdbusplus ObjectManager.
349 auto& sdbusPlusHandler = ipmid_get_sdbus_plus_handler();
350 objManager = std::make_unique<sdbusplus::server::manager::manager>(
Patrick Venture02261c02018-10-31 15:16:23 -0700351 *sdbusPlusHandler, CONTROL_HOST_OBJ_MGR);
Vishwanatha Subbanna07655062017-07-14 20:31:57 +0530352
353 opHost = std::make_unique<open_power::host::command::Host>(
Patrick Venture02261c02018-10-31 15:16:23 -0700354 *sdbusPlusHandler, objPath.c_str());
Vishwanatha Subbanna07655062017-07-14 20:31:57 +0530355
356 // Service for this is provided by phosphor layer systemcmdintf
357 // and this will be as part of that.
Tomcbfd6ec2016-09-14 17:45:55 +0530358 return;
Chris Austen4d98c1e2015-10-13 14:33:50 -0500359}