blob: 47328b05bcfc2dde8515c82e662267d17130379a [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>
Tomd700e762016-09-20 18:24:13 +053011#include "host-ipmid/ipmid-api.h"
Chris Austen41a4b312015-10-25 03:45:42 -050012#include "sensorhandler.h"
Tomd700e762016-09-20 18:24:13 +053013#include "storagehandler.h"
14
Chris Austen41a4b312015-10-25 03:45:42 -050015
16using namespace std;
17
Chris Austen41a4b312015-10-25 03:45:42 -050018//////////////////////////
19struct esel_section_headers_t {
20 uint8_t sectionid[2];
21 uint8_t sectionlength[2];
22 uint8_t version;
23 uint8_t subsectiontype;
24 uint8_t compid;
25};
26
27struct severity_values_t {
28 uint8_t type;
29 const char *description;
30};
31
32
33const std::vector<severity_values_t> g_sev_desc = {
34 {0x10, "recoverable error"},
35 {0x20, "predictive error"},
36 {0x40, "unrecoverable error"},
37 {0x50, "critical error"},
38 {0x60, "error from a diagnostic test"},
39 {0x70, "recovered symptom "},
40 {0xFF, "Unknown"},
41};
42
43const char* sev_lookup(uint8_t n) {
44 auto i = std::find_if(std::begin(g_sev_desc), std::end(g_sev_desc),
45 [n](auto p){ return p.type == n || p.type == 0xFF; });
46 return i->description;
47}
48
49
50
51
52int find_sensor_type_string(uint8_t sensor_number, char **s) {
53
54 dbus_interface_t a;
55 const char *p;
56 char r;
57
58 r = find_openbmc_path("SENSOR", sensor_number, &a);
59
60 if ((r < 0) || (a.bus[0] == 0)) {
61 // Just make a generic message for errors that
62 // occur on sensors that dont exist
63 asprintf(s, "Unknown Sensor (0x%02x)", sensor_number);
64 } else {
65
66 if ((p = strrchr (a.path, '/')) == NULL) {
67 p = "/Unknown Sensor";
68 }
69
70 asprintf(s, "%s", p+1);
71 }
72
73 return 0;
74}
75
76
77size_t getfilestream(const char *fn, uint8_t **buffer) {
78
79 FILE *fp;
Matthew Barth8b470052016-09-21 10:02:57 -050080 ssize_t size = 0;
Nan Lidfe6e422016-05-13 21:46:26 +080081 int r;
Chris Austen41a4b312015-10-25 03:45:42 -050082
83 if ((fp = fopen(fn, "rb")) != NULL) {
84
Nan Lidfe6e422016-05-13 21:46:26 +080085 r = fseek(fp, 0, SEEK_END);
86 if (r) {
87 fprintf(stderr,"Fseek failed\n");
88 goto fclose_fp;
89 }
90
Chris Austen41a4b312015-10-25 03:45:42 -050091 size = ftell(fp);
Nan Lidfe6e422016-05-13 21:46:26 +080092 if (size == -1L) {
93 fprintf(stderr,"Ftell failed for %s\n", strerror(errno));
94 size = 0;
95 goto fclose_fp;
96 }
97
98 r = fseek(fp, 0, SEEK_SET);
99 if (r) {
100 fprintf(stderr,"Fseek failed\n");
101 size = 0;
102 goto fclose_fp;
103 }
Chris Austen41a4b312015-10-25 03:45:42 -0500104
Chris Austen41a4b312015-10-25 03:45:42 -0500105 *buffer = new uint8_t [size];
106
Nan Lidfe6e422016-05-13 21:46:26 +0800107 r = fread(*buffer, 1, size, fp);
108 if ( r != size) {
109 size = 0;
110 fprintf(stderr,"Fread failed\n");
111 }
112
113fclose_fp:
Chris Austen41a4b312015-10-25 03:45:42 -0500114 fclose(fp);
115 }
116
Matthew Barth8b470052016-09-21 10:02:57 -0500117 return static_cast<size_t>(size);
Chris Austen41a4b312015-10-25 03:45:42 -0500118}
119
120
121const char *create_esel_severity(const uint8_t *buffer) {
122
123 uint8_t severity;
124 // Dive in to the IBM log to find the severity
125 severity = (0xF0 & buffer[0x4A]);
126
127 return sev_lookup(severity);
128}
129
130int create_esel_association(const uint8_t *buffer, char **m) {
131
132 ipmi_add_sel_request_t *p;
133 dbus_interface_t dbusint;
134 uint8_t sensor;
135
136 p = ( ipmi_add_sel_request_t *) buffer;
137
138 sensor = p->sensornumber;
139
140 find_openbmc_path("SENSOR", sensor, &dbusint);
141
Chris Austen6dc2e912016-02-20 01:00:06 -0600142 // Simply no associations if the sensor can not be found
Chris Austen41a4b312015-10-25 03:45:42 -0500143 if (strlen(dbusint.path) < 1) {
Chris Austen6dc2e912016-02-20 01:00:06 -0600144 printf("Sensor 0x%x not found\n", sensor);
145 memset(dbusint.path,0,sizeof(dbusint.path));
Chris Austen41a4b312015-10-25 03:45:42 -0500146 }
147
148 asprintf(m, "%s", dbusint.path);
149
150 return 0;
151}
152
153
154
155int create_esel_description(const uint8_t *buffer, const char *sev, char **message) {
156
157
158 ipmi_add_sel_request_t *p;
Chris Austen41a4b312015-10-25 03:45:42 -0500159 char *m;
160
161 p = ( ipmi_add_sel_request_t *) buffer;
162
163 find_sensor_type_string(p->sensornumber,&m);
164
165 asprintf(message, "A %s has experienced a %s", m, sev );
166
167 free(m);
168
169 return 0;
170}
171
172
173int send_esel_to_dbus(const char *desc, const char *sev, const char *details, uint8_t *debug, size_t debuglen) {
174
175 sd_bus *mbus = NULL;
176 sd_bus_error error = SD_BUS_ERROR_NULL;
177 sd_bus_message *reply = NULL, *m=NULL;
Chris Austen169395e2015-12-02 20:56:15 -0600178 uint16_t x;
Chris Austen41a4b312015-10-25 03:45:42 -0500179 int r;
Sergey Solomineb9b8142016-08-23 09:07:28 -0500180 const char *object_name = "/org/openbmc/records/events";
181 char *bus_name = NULL;
Chris Austen41a4b312015-10-25 03:45:42 -0500182
Chris Austen30195fa2015-11-13 14:39:19 -0600183 mbus = ipmid_get_sd_bus_connection();
Sergey Solomineb9b8142016-08-23 09:07:28 -0500184 r = mapper_get_service(mbus, object_name, &bus_name);
185 if (r < 0) {
186 fprintf(stderr, "Failed to get connection, return value: %s.\n", strerror(-r));
187 goto finish;
188 }
Chris Austen41a4b312015-10-25 03:45:42 -0500189 r = sd_bus_message_new_method_call(mbus,&m,
Sergey Solomineb9b8142016-08-23 09:07:28 -0500190 bus_name,
191 object_name,
Chris Austen41a4b312015-10-25 03:45:42 -0500192 "org.openbmc.recordlog",
193 "acceptHostMessage");
194 if (r < 0) {
195 fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600196 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500197 }
198
199 r = sd_bus_message_append(m, "sss", desc, sev, details);
200 if (r < 0) {
201 fprintf(stderr, "Failed add the message strings : %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600202 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500203 }
204
205 r = sd_bus_message_append_array(m, 'y', debug, debuglen);
206 if (r < 0) {
207 fprintf(stderr, "Failed to add the raw array of bytes: %s\n", strerror(-r));
Chris Austen169395e2015-12-02 20:56:15 -0600208 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500209 }
Chris Austen41a4b312015-10-25 03:45:42 -0500210 // Call the IPMI responder on the bus so the message can be sent to the CEC
211 r = sd_bus_call(mbus, m, 0, &error, &reply);
212 if (r < 0) {
Chris Austen169395e2015-12-02 20:56:15 -0600213 fprintf(stderr, "Failed to call the method: %s %s\n", __FUNCTION__, strerror(-r));
214 goto finish;
Chris Austen41a4b312015-10-25 03:45:42 -0500215 }
Chris Austen169395e2015-12-02 20:56:15 -0600216 r = sd_bus_message_read(reply, "q", &x);
Chris Austen41a4b312015-10-25 03:45:42 -0500217 if (r < 0) {
Chris Austen7cc33322015-11-11 00:20:22 -0600218 fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r));
Chris Austen41a4b312015-10-25 03:45:42 -0500219 }
220
Chris Austen169395e2015-12-02 20:56:15 -0600221finish:
Chris Austen41a4b312015-10-25 03:45:42 -0500222 sd_bus_error_free(&error);
vishwa1eaea4f2016-02-26 11:57:40 -0600223 m = sd_bus_message_unref(m);
224 reply = sd_bus_message_unref(reply);
Sergey Solomineb9b8142016-08-23 09:07:28 -0500225 free (bus_name);
Chris Austen41a4b312015-10-25 03:45:42 -0500226 return r;
Chris Austen41a4b312015-10-25 03:45:42 -0500227}
228
229
230void send_esel(uint16_t recordid) {
Joel Stanleyf19539e2015-11-25 17:24:05 +1030231 char *desc, *assoc;
Chris Austen41a4b312015-10-25 03:45:42 -0500232 const char *sev;
233 uint8_t *buffer = NULL;
Chris Austen6dc2e912016-02-20 01:00:06 -0600234 const char *path = "/tmp/esel";
Matthew Barth8b470052016-09-21 10:02:57 -0500235 ssize_t sz;
Nan Lidfe6e422016-05-13 21:46:26 +0800236 int r;
Chris Austen41a4b312015-10-25 03:45:42 -0500237
Chris Austen41a4b312015-10-25 03:45:42 -0500238 sz = getfilestream(path, &buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500239 if (sz == 0) {
240 printf("Error file does not exist %d\n",__LINE__);
Chris Austen41a4b312015-10-25 03:45:42 -0500241 return;
242 }
243
Chris Austen41a4b312015-10-25 03:45:42 -0500244 sev = create_esel_severity(buffer);
Chris Austen41a4b312015-10-25 03:45:42 -0500245 create_esel_association(buffer, &assoc);
Chris Austen41a4b312015-10-25 03:45:42 -0500246 create_esel_description(buffer, sev, &desc);
247
Nan Lidfe6e422016-05-13 21:46:26 +0800248 r = send_esel_to_dbus(desc, sev, assoc, buffer, sz);
249 if (r < 0) {
250 fprintf(stderr, "Failed to send esel to dbus\n");
251 }
Chris Austen41a4b312015-10-25 03:45:42 -0500252
Chris Austen41a4b312015-10-25 03:45:42 -0500253 free(assoc);
254 free(desc);
Chris Austen41a4b312015-10-25 03:45:42 -0500255 delete[] buffer;
256
Chris Austen41a4b312015-10-25 03:45:42 -0500257 return;
258}