blob: dea892ca8ac0f8087ad5a86f5012d910035eaac3 [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>
12#include <phosphor-logging/elog-errors-HostEvent.hpp>
Tomd700e762016-09-20 18:24:13 +053013#include "host-ipmid/ipmid-api.h"
Chris Austen41a4b312015-10-25 03:45:42 -050014#include "sensorhandler.h"
Tomd700e762016-09-20 18:24:13 +053015#include "storagehandler.h"
16
Chris Austen41a4b312015-10-25 03:45:42 -050017
18using namespace std;
Adriana Kobylak2efb3e72017-02-06 21:43:59 -060019using namespace phosphor::logging;
Chris Austen41a4b312015-10-25 03:45:42 -050020
Chris Austen41a4b312015-10-25 03:45:42 -050021//////////////////////////
22struct esel_section_headers_t {
23 uint8_t sectionid[2];
24 uint8_t sectionlength[2];
25 uint8_t version;
26 uint8_t subsectiontype;
27 uint8_t compid;
28};
29
30struct severity_values_t {
31 uint8_t type;
32 const char *description;
33};
34
35
36const std::vector<severity_values_t> g_sev_desc = {
37 {0x10, "recoverable error"},
38 {0x20, "predictive error"},
39 {0x40, "unrecoverable error"},
40 {0x50, "critical error"},
41 {0x60, "error from a diagnostic test"},
42 {0x70, "recovered symptom "},
43 {0xFF, "Unknown"},
44};
45
46const char* sev_lookup(uint8_t n) {
47 auto i = std::find_if(std::begin(g_sev_desc), std::end(g_sev_desc),
48 [n](auto p){ return p.type == n || p.type == 0xFF; });
49 return i->description;
50}
51
52
53
54
55int find_sensor_type_string(uint8_t sensor_number, char **s) {
56
57 dbus_interface_t a;
58 const char *p;
Matthew Barth56181052017-01-23 09:36:29 -060059 int r;
Chris Austen41a4b312015-10-25 03:45:42 -050060
61 r = find_openbmc_path("SENSOR", sensor_number, &a);
62
63 if ((r < 0) || (a.bus[0] == 0)) {
64 // Just make a generic message for errors that
65 // occur on sensors that dont exist
Matthew Barth56181052017-01-23 09:36:29 -060066 r = asprintf(s, "Unknown Sensor (0x%02x)", sensor_number);
Chris Austen41a4b312015-10-25 03:45:42 -050067 } else {
68
69 if ((p = strrchr (a.path, '/')) == NULL) {
70 p = "/Unknown Sensor";
71 }
72
Matthew Barth56181052017-01-23 09:36:29 -060073 *s = strdup(p+1);
Chris Austen41a4b312015-10-25 03:45:42 -050074 }
75
76 return 0;
77}
78
79
80size_t getfilestream(const char *fn, uint8_t **buffer) {
81
82 FILE *fp;
Matthew Barth8b470052016-09-21 10:02:57 -050083 ssize_t size = 0;
Nan Lidfe6e422016-05-13 21:46:26 +080084 int r;
Chris Austen41a4b312015-10-25 03:45:42 -050085
86 if ((fp = fopen(fn, "rb")) != NULL) {
87
Nan Lidfe6e422016-05-13 21:46:26 +080088 r = fseek(fp, 0, SEEK_END);
89 if (r) {
90 fprintf(stderr,"Fseek failed\n");
91 goto fclose_fp;
92 }
93
Chris Austen41a4b312015-10-25 03:45:42 -050094 size = ftell(fp);
Nan Lidfe6e422016-05-13 21:46:26 +080095 if (size == -1L) {
96 fprintf(stderr,"Ftell failed for %s\n", strerror(errno));
97 size = 0;
98 goto fclose_fp;
99 }
100
101 r = fseek(fp, 0, SEEK_SET);
102 if (r) {
103 fprintf(stderr,"Fseek failed\n");
104 size = 0;
105 goto fclose_fp;
106 }
Chris Austen41a4b312015-10-25 03:45:42 -0500107
Chris Austen41a4b312015-10-25 03:45:42 -0500108 *buffer = new uint8_t [size];
109
Nan Lidfe6e422016-05-13 21:46:26 +0800110 r = fread(*buffer, 1, size, fp);
111 if ( r != size) {
112 size = 0;
113 fprintf(stderr,"Fread failed\n");
114 }
115
116fclose_fp:
Chris Austen41a4b312015-10-25 03:45:42 -0500117 fclose(fp);
118 }
119
Matthew Barth8b470052016-09-21 10:02:57 -0500120 return static_cast<size_t>(size);
Chris Austen41a4b312015-10-25 03:45:42 -0500121}
122
123
124const char *create_esel_severity(const uint8_t *buffer) {
125
126 uint8_t severity;
127 // Dive in to the IBM log to find the severity
128 severity = (0xF0 & buffer[0x4A]);
129
130 return sev_lookup(severity);
131}
132
133int create_esel_association(const uint8_t *buffer, char **m) {
134
135 ipmi_add_sel_request_t *p;
136 dbus_interface_t dbusint;
137 uint8_t sensor;
138
139 p = ( ipmi_add_sel_request_t *) buffer;
140
141 sensor = p->sensornumber;
142
143 find_openbmc_path("SENSOR", sensor, &dbusint);
144
Chris Austen6dc2e912016-02-20 01:00:06 -0600145 // Simply no associations if the sensor can not be found
Chris Austen41a4b312015-10-25 03:45:42 -0500146 if (strlen(dbusint.path) < 1) {
Chris Austen6dc2e912016-02-20 01:00:06 -0600147 printf("Sensor 0x%x not found\n", sensor);
148 memset(dbusint.path,0,sizeof(dbusint.path));
Chris Austen41a4b312015-10-25 03:45:42 -0500149 }
150
Matthew Barth56181052017-01-23 09:36:29 -0600151 *m = strdup(dbusint.path);
Chris Austen41a4b312015-10-25 03:45:42 -0500152
153 return 0;
154}
155
156
157
158int create_esel_description(const uint8_t *buffer, const char *sev, char **message) {
159
160
161 ipmi_add_sel_request_t *p;
Chris Austen41a4b312015-10-25 03:45:42 -0500162 char *m;
Matthew Barth56181052017-01-23 09:36:29 -0600163 int r;
Chris Austen41a4b312015-10-25 03:45:42 -0500164
165 p = ( ipmi_add_sel_request_t *) buffer;
166
167 find_sensor_type_string(p->sensornumber,&m);
168
Matthew Barth56181052017-01-23 09:36:29 -0600169 r = asprintf(message, "A %s has experienced a %s", m, sev );
170 if (r == -1) {
171 fprintf(stderr,
172 "Failed to allocate memory for ESEL description\n");
173 }
Chris Austen41a4b312015-10-25 03:45:42 -0500174
175 free(m);
176
177 return 0;
178}
179
180
181int send_esel_to_dbus(const char *desc, const char *sev, const char *details, uint8_t *debug, size_t debuglen) {
182
Adriana Kobylak2efb3e72017-02-06 21:43:59 -0600183 // Allocate enough space to represent the data in hex separated by spaces,
184 // to mimic how IPMI would display the data.
185 unique_ptr<char[]> selData(new char[debuglen*3]());
186 uint32_t i = 0;
187 for(i = 0; i < debuglen; i++)
188 {
189 sprintf(&selData[i*3], "%02x ", 0xFF & ((char*)debug)[i]);
190 }
191 log<level::INFO>("Received Host Event", entry("ESEL=%s", selData.get()));
192
193 try
194 {
195 elog<org::open_power::Error::Host::Event>(
196 prev_entry<org::open_power::Error::Host::Event::ESEL>());
197 }
198 catch (elogException<org::open_power::Error::Host::Event>& e)
199 {
200 commit(e.name());
201 }
202
Adriana Kobylak513d68e2017-02-15 11:36:28 -0600203 return 0;
Chris Austen41a4b312015-10-25 03:45:42 -0500204}
205
206
207void send_esel(uint16_t recordid) {
Joel Stanleyf19539e2015-11-25 17:24:05 +1030208 char *desc, *assoc;
Chris Austen41a4b312015-10-25 03:45:42 -0500209 const char *sev;
210 uint8_t *buffer = NULL;
Chris Austen6dc2e912016-02-20 01:00:06 -0600211 const char *path = "/tmp/esel";
Matthew Barth8b470052016-09-21 10:02:57 -0500212 ssize_t sz;
Nan Lidfe6e422016-05-13 21:46:26 +0800213 int r;
Chris Austen41a4b312015-10-25 03:45:42 -0500214
Chris Austen41a4b312015-10-25 03:45:42 -0500215 sz = getfilestream(path, &buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500216 if (sz == 0) {
217 printf("Error file does not exist %d\n",__LINE__);
Chris Austen41a4b312015-10-25 03:45:42 -0500218 return;
219 }
220
Chris Austen41a4b312015-10-25 03:45:42 -0500221 sev = create_esel_severity(buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500222 create_esel_association(buffer, &assoc);
Chris Austen41a4b312015-10-25 03:45:42 -0500223 create_esel_description(buffer, sev, &desc);
224
Nan Lidfe6e422016-05-13 21:46:26 +0800225 r = send_esel_to_dbus(desc, sev, assoc, buffer, sz);
226 if (r < 0) {
227 fprintf(stderr, "Failed to send esel to dbus\n");
228 }
Chris Austen41a4b312015-10-25 03:45:42 -0500229
Chris Austen41a4b312015-10-25 03:45:42 -0500230 free(assoc);
231 free(desc);
Chris Austen41a4b312015-10-25 03:45:42 -0500232 delete[] buffer;
233
Chris Austen41a4b312015-10-25 03:45:42 -0500234 return;
235}