blob: 2b41d5677be8c246502101e794cc491408fd2b5d [file] [log] [blame]
Chris Austen41a4b312015-10-25 03:45:42 -05001#include <stdint.h>
2#include <cstdlib>
3#include <cstring>
4#include <fstream>
5#include <iostream>
6#include <algorithm>
7#include <vector>
8#include <memory>
9#include <systemd/sd-bus.h>
Sergey Solomineb9b8142016-08-23 09:07:28 -050010#include <mapper.h>
Saqib Khand33a4af2017-02-20 15:23:27 -060011#include <phosphor-logging/elog.hpp>
Tomd700e762016-09-20 18:24:13 +053012#include "host-ipmid/ipmid-api.h"
Tom Joseph448e74e2017-07-24 23:08:56 +053013#include "elog-errors.hpp"
14#include "error-HostEvent.hpp"
Chris Austen41a4b312015-10-25 03:45:42 -050015#include "sensorhandler.h"
Tomd700e762016-09-20 18:24:13 +053016#include "storagehandler.h"
Tom Joseph448e74e2017-07-24 23:08:56 +053017#include "types.hpp"
Deepak Kodihalli3d230482018-04-03 07:00:45 -050018#include "xyz/openbmc_project/Logging/Entry/server.hpp"
Tomd700e762016-09-20 18:24:13 +053019
Chris Austen41a4b312015-10-25 03:45:42 -050020
21using namespace std;
Adriana Kobylak2efb3e72017-02-06 21:43:59 -060022using namespace phosphor::logging;
Deepak Kodihalli3d230482018-04-03 07:00:45 -050023using namespace sdbusplus::xyz::openbmc_project::Logging::server;
Tom Joseph448e74e2017-07-24 23:08:56 +053024extern const ipmi::sensor::InvObjectIDMap invSensors;
Chris Austen41a4b312015-10-25 03:45:42 -050025
Chris Austen41a4b312015-10-25 03:45:42 -050026//////////////////////////
27struct esel_section_headers_t {
28 uint8_t sectionid[2];
29 uint8_t sectionlength[2];
30 uint8_t version;
31 uint8_t subsectiontype;
32 uint8_t compid;
33};
34
35struct severity_values_t {
36 uint8_t type;
Deepak Kodihalli3d230482018-04-03 07:00:45 -050037 Entry::Level level;
Chris Austen41a4b312015-10-25 03:45:42 -050038};
39
40
41const std::vector<severity_values_t> g_sev_desc = {
Deepak Kodihalli3d230482018-04-03 07:00:45 -050042 {0x10, Entry::Level::Warning}, // recoverable error
43 {0x20, Entry::Level::Warning}, // predictive error
44 // TODO via github issue 3066 : map level below to Level::Unrecoverable
45 {0x40, Entry::Level::Error}, // unrecoverable error
46 // TODO via github issue 3066 : map level below to Level::Critical
47 {0x50, Entry::Level::Error}, // critical error
48 {0x60, Entry::Level::Error}, // error from a diagnostic test
49 {0x70, Entry::Level::Warning}, // recoverable symptom
50 {0xFF, Entry::Level::Error}, //unknown error
Chris Austen41a4b312015-10-25 03:45:42 -050051};
52
Deepak Kodihalli3d230482018-04-03 07:00:45 -050053Entry::Level sev_lookup(uint8_t n) {
Chris Austen41a4b312015-10-25 03:45:42 -050054 auto i = std::find_if(std::begin(g_sev_desc), std::end(g_sev_desc),
55 [n](auto p){ return p.type == n || p.type == 0xFF; });
Deepak Kodihalli3d230482018-04-03 07:00:45 -050056 return i->level;
Chris Austen41a4b312015-10-25 03:45:42 -050057}
58
59
60
61
62int find_sensor_type_string(uint8_t sensor_number, char **s) {
63
64 dbus_interface_t a;
65 const char *p;
Matthew Barth56181052017-01-23 09:36:29 -060066 int r;
Chris Austen41a4b312015-10-25 03:45:42 -050067
Emily Shaffer2ae09b92017-04-05 15:09:41 -070068 r = find_openbmc_path(sensor_number, &a);
Chris Austen41a4b312015-10-25 03:45:42 -050069
70 if ((r < 0) || (a.bus[0] == 0)) {
71 // Just make a generic message for errors that
Gunnar Mills8991dd62017-10-25 17:11:29 -050072 // occur on sensors that don't exist
Matthew Barth56181052017-01-23 09:36:29 -060073 r = asprintf(s, "Unknown Sensor (0x%02x)", sensor_number);
Chris Austen41a4b312015-10-25 03:45:42 -050074 } else {
75
76 if ((p = strrchr (a.path, '/')) == NULL) {
77 p = "/Unknown Sensor";
78 }
79
Matthew Barth56181052017-01-23 09:36:29 -060080 *s = strdup(p+1);
Chris Austen41a4b312015-10-25 03:45:42 -050081 }
82
83 return 0;
84}
85
86
87size_t getfilestream(const char *fn, uint8_t **buffer) {
88
89 FILE *fp;
Matthew Barth8b470052016-09-21 10:02:57 -050090 ssize_t size = 0;
Nan Lidfe6e422016-05-13 21:46:26 +080091 int r;
Chris Austen41a4b312015-10-25 03:45:42 -050092
93 if ((fp = fopen(fn, "rb")) != NULL) {
94
Nan Lidfe6e422016-05-13 21:46:26 +080095 r = fseek(fp, 0, SEEK_END);
96 if (r) {
97 fprintf(stderr,"Fseek failed\n");
98 goto fclose_fp;
99 }
100
Chris Austen41a4b312015-10-25 03:45:42 -0500101 size = ftell(fp);
Nan Lidfe6e422016-05-13 21:46:26 +0800102 if (size == -1L) {
103 fprintf(stderr,"Ftell failed for %s\n", strerror(errno));
104 size = 0;
105 goto fclose_fp;
106 }
107
108 r = fseek(fp, 0, SEEK_SET);
109 if (r) {
110 fprintf(stderr,"Fseek failed\n");
111 size = 0;
112 goto fclose_fp;
113 }
Chris Austen41a4b312015-10-25 03:45:42 -0500114
Chris Austen41a4b312015-10-25 03:45:42 -0500115 *buffer = new uint8_t [size];
116
Nan Lidfe6e422016-05-13 21:46:26 +0800117 r = fread(*buffer, 1, size, fp);
118 if ( r != size) {
119 size = 0;
120 fprintf(stderr,"Fread failed\n");
121 }
122
123fclose_fp:
Chris Austen41a4b312015-10-25 03:45:42 -0500124 fclose(fp);
125 }
126
Matthew Barth8b470052016-09-21 10:02:57 -0500127 return static_cast<size_t>(size);
Chris Austen41a4b312015-10-25 03:45:42 -0500128}
129
130
Deepak Kodihalli3d230482018-04-03 07:00:45 -0500131Entry::Level create_esel_severity(const uint8_t *buffer) {
Chris Austen41a4b312015-10-25 03:45:42 -0500132
133 uint8_t severity;
134 // Dive in to the IBM log to find the severity
135 severity = (0xF0 & buffer[0x4A]);
136
137 return sev_lookup(severity);
138}
139
Tom Joseph448e74e2017-07-24 23:08:56 +0530140int create_esel_association(const uint8_t *buffer, std::string& inventoryPath)
141{
Chris Austen41a4b312015-10-25 03:45:42 -0500142 ipmi_add_sel_request_t *p;
Chris Austen41a4b312015-10-25 03:45:42 -0500143 uint8_t sensor;
144
145 p = ( ipmi_add_sel_request_t *) buffer;
146
147 sensor = p->sensornumber;
148
Tom Joseph448e74e2017-07-24 23:08:56 +0530149 inventoryPath = {};
Chris Austen41a4b312015-10-25 03:45:42 -0500150
Tom Joseph448e74e2017-07-24 23:08:56 +0530151 /*
152 * Search the sensor number to inventory path mapping to figure out the
153 * inventory associated with the ESEL.
154 */
155 for (auto const &iter : invSensors)
156 {
157 if (iter.second.sensorID == sensor)
158 {
159 inventoryPath = iter.first;
160 break;
161 }
162 }
Chris Austen41a4b312015-10-25 03:45:42 -0500163
164 return 0;
165}
166
167
168
Deepak Kodihalli3d230482018-04-03 07:00:45 -0500169int create_esel_description(const uint8_t *buffer, Entry::Level level,
170 char **message) {
Chris Austen41a4b312015-10-25 03:45:42 -0500171
172
173 ipmi_add_sel_request_t *p;
Chris Austen41a4b312015-10-25 03:45:42 -0500174 char *m;
Matthew Barth56181052017-01-23 09:36:29 -0600175 int r;
Chris Austen41a4b312015-10-25 03:45:42 -0500176
177 p = ( ipmi_add_sel_request_t *) buffer;
178
179 find_sensor_type_string(p->sensornumber,&m);
180
Deepak Kodihalli3d230482018-04-03 07:00:45 -0500181 r = asprintf(message, "A %s has experienced an error of level %d",
182 m, static_cast<uint32_t>(level) );
Matthew Barth56181052017-01-23 09:36:29 -0600183 if (r == -1) {
184 fprintf(stderr,
185 "Failed to allocate memory for ESEL description\n");
186 }
Chris Austen41a4b312015-10-25 03:45:42 -0500187
188 free(m);
189
190 return 0;
191}
192
193
Tom Joseph448e74e2017-07-24 23:08:56 +0530194int send_esel_to_dbus(const char *desc,
Deepak Kodihalli3d230482018-04-03 07:00:45 -0500195 Entry::Level level,
Tom Joseph448e74e2017-07-24 23:08:56 +0530196 const std::string& inventoryPath,
197 uint8_t *debug,
198 size_t debuglen)
199{
Chris Austen41a4b312015-10-25 03:45:42 -0500200
Adriana Kobylak2efb3e72017-02-06 21:43:59 -0600201 // Allocate enough space to represent the data in hex separated by spaces,
202 // to mimic how IPMI would display the data.
Tom Josephb9ac6a42017-02-28 19:56:33 +0530203 unique_ptr<char[]> selData(new char[(debuglen*3) + 1]());
Adriana Kobylak2efb3e72017-02-06 21:43:59 -0600204 uint32_t i = 0;
205 for(i = 0; i < debuglen; i++)
206 {
207 sprintf(&selData[i*3], "%02x ", 0xFF & ((char*)debug)[i]);
208 }
Tom Josephb9ac6a42017-02-28 19:56:33 +0530209 selData[debuglen*3] = '\0';
210
Marri Devender Raob0b395b2017-10-24 10:14:14 -0500211 using error = sdbusplus::org::open_power::Host::Error::Event;
212 using metadata = org::open_power::Host::Event;
Tom Joseph448e74e2017-07-24 23:08:56 +0530213
Deepak Kodihalli3d230482018-04-03 07:00:45 -0500214 report<error>(level,
215 metadata::ESEL(selData.get()),
Tom Joseph448e74e2017-07-24 23:08:56 +0530216 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Adriana Kobylak2efb3e72017-02-06 21:43:59 -0600217
Adriana Kobylak513d68e2017-02-15 11:36:28 -0600218 return 0;
Chris Austen41a4b312015-10-25 03:45:42 -0500219}
220
221
222void send_esel(uint16_t recordid) {
Tom Joseph448e74e2017-07-24 23:08:56 +0530223 char *desc;
Chris Austen41a4b312015-10-25 03:45:42 -0500224 uint8_t *buffer = NULL;
Chris Austen6dc2e912016-02-20 01:00:06 -0600225 const char *path = "/tmp/esel";
Matthew Barth8b470052016-09-21 10:02:57 -0500226 ssize_t sz;
Nan Lidfe6e422016-05-13 21:46:26 +0800227 int r;
Tom Joseph448e74e2017-07-24 23:08:56 +0530228 std::string inventoryPath;
Chris Austen41a4b312015-10-25 03:45:42 -0500229
Chris Austen41a4b312015-10-25 03:45:42 -0500230 sz = getfilestream(path, &buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500231 if (sz == 0) {
232 printf("Error file does not exist %d\n",__LINE__);
Chris Austen41a4b312015-10-25 03:45:42 -0500233 return;
234 }
235
Deepak Kodihalli3d230482018-04-03 07:00:45 -0500236 auto sev = create_esel_severity(buffer);
Tom Joseph448e74e2017-07-24 23:08:56 +0530237 create_esel_association(buffer, inventoryPath);
Chris Austen41a4b312015-10-25 03:45:42 -0500238 create_esel_description(buffer, sev, &desc);
239
Tom Joseph448e74e2017-07-24 23:08:56 +0530240 r = send_esel_to_dbus(desc, sev, inventoryPath, buffer, sz);
Nan Lidfe6e422016-05-13 21:46:26 +0800241 if (r < 0) {
242 fprintf(stderr, "Failed to send esel to dbus\n");
243 }
Chris Austen41a4b312015-10-25 03:45:42 -0500244
Chris Austen41a4b312015-10-25 03:45:42 -0500245 free(desc);
Chris Austen41a4b312015-10-25 03:45:42 -0500246 delete[] buffer;
247
Chris Austen41a4b312015-10-25 03:45:42 -0500248 return;
249}
Tom Josephb647d5b2017-10-31 17:25:33 +0530250
251std::string readESEL(const char* fileName)
252{
253 std::string content;
254 std::ifstream handle(fileName);
255
256 if (handle.fail())
257 {
258 log<level::ERR>("Failed to open eSEL", entry("FILENAME=%s", fileName));
259 return content;
260 }
261
262 handle.seekg(0, std::ios::end);
263 content.resize(handle.tellg());
264 handle.seekg(0, std::ios::beg);
265 handle.read(&content[0], content.size());
266 handle.close();
267
268 return content;
269}
270
271void createProcedureLogEntry(uint8_t procedureNum)
272{
273 // Read the eSEL data from the file.
274 static constexpr auto eSELFile = "/tmp/esel";
275 auto eSELData = readESEL(eSELFile);
276
277 // Each byte in eSEL is formatted as %02x with a space between bytes and
278 // insert '/0' at the end of the character array.
279 static constexpr auto byteSeparator = 3;
280 std::unique_ptr<char[]> data(new char[
281 (eSELData.size() * byteSeparator) + 1]());
282
283 for (size_t i = 0; i < eSELData.size(); i++)
284 {
285 sprintf(&data[i * byteSeparator], "%02x ", eSELData[i]);
286 }
287 data[eSELData.size() * byteSeparator] = '\0';
288
289 using error = sdbusplus::org::open_power::Host::Error::MaintenanceProcedure;
290 using metadata = org::open_power::Host::MaintenanceProcedure;
291
292 report<error>(metadata::ESEL(data.get()),
293 metadata::PROCEDURE(static_cast<uint32_t>(procedureNum)));
294}