blob: b8b7a8db954ea10675a778e0df7091d64ebf508b [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.
Tom Josephb9ac6a42017-02-28 19:56:33 +0530185 unique_ptr<char[]> selData(new char[(debuglen*3) + 1]());
Adriana Kobylak2efb3e72017-02-06 21:43:59 -0600186 uint32_t i = 0;
187 for(i = 0; i < debuglen; i++)
188 {
189 sprintf(&selData[i*3], "%02x ", 0xFF & ((char*)debug)[i]);
190 }
Tom Josephb9ac6a42017-02-28 19:56:33 +0530191 selData[debuglen*3] = '\0';
192
Marri Devender Raod1a67532017-04-15 06:00:33 -0500193 using error = org::open_power::Error::Host::Event;
194 report<error>(error::ESEL(selData.get()));
Adriana Kobylak2efb3e72017-02-06 21:43:59 -0600195
Adriana Kobylak513d68e2017-02-15 11:36:28 -0600196 return 0;
Chris Austen41a4b312015-10-25 03:45:42 -0500197}
198
199
200void send_esel(uint16_t recordid) {
Joel Stanleyf19539e2015-11-25 17:24:05 +1030201 char *desc, *assoc;
Chris Austen41a4b312015-10-25 03:45:42 -0500202 const char *sev;
203 uint8_t *buffer = NULL;
Chris Austen6dc2e912016-02-20 01:00:06 -0600204 const char *path = "/tmp/esel";
Matthew Barth8b470052016-09-21 10:02:57 -0500205 ssize_t sz;
Nan Lidfe6e422016-05-13 21:46:26 +0800206 int r;
Chris Austen41a4b312015-10-25 03:45:42 -0500207
Chris Austen41a4b312015-10-25 03:45:42 -0500208 sz = getfilestream(path, &buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500209 if (sz == 0) {
210 printf("Error file does not exist %d\n",__LINE__);
Chris Austen41a4b312015-10-25 03:45:42 -0500211 return;
212 }
213
Chris Austen41a4b312015-10-25 03:45:42 -0500214 sev = create_esel_severity(buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500215 create_esel_association(buffer, &assoc);
Chris Austen41a4b312015-10-25 03:45:42 -0500216 create_esel_description(buffer, sev, &desc);
217
Nan Lidfe6e422016-05-13 21:46:26 +0800218 r = send_esel_to_dbus(desc, sev, assoc, buffer, sz);
219 if (r < 0) {
220 fprintf(stderr, "Failed to send esel to dbus\n");
221 }
Chris Austen41a4b312015-10-25 03:45:42 -0500222
Chris Austen41a4b312015-10-25 03:45:42 -0500223 free(assoc);
224 free(desc);
Chris Austen41a4b312015-10-25 03:45:42 -0500225 delete[] buffer;
226
Chris Austen41a4b312015-10-25 03:45:42 -0500227 return;
228}