Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 1 | #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 Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 10 | #include <mapper.h> |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 11 | #include "host-ipmid/ipmid-api.h" |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 12 | #include "sensorhandler.h" |
Tom | d700e76 | 2016-09-20 18:24:13 +0530 | [diff] [blame] | 13 | #include "storagehandler.h" |
| 14 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 15 | |
| 16 | using namespace std; |
| 17 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 18 | ////////////////////////// |
| 19 | struct 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 | |
| 27 | struct severity_values_t { |
| 28 | uint8_t type; |
| 29 | const char *description; |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | const 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 | |
| 43 | const 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 | |
| 52 | int 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 | |
| 77 | size_t getfilestream(const char *fn, uint8_t **buffer) { |
| 78 | |
| 79 | FILE *fp; |
Matthew Barth | 8b47005 | 2016-09-21 10:02:57 -0500 | [diff] [blame] | 80 | ssize_t size = 0; |
Nan Li | dfe6e42 | 2016-05-13 21:46:26 +0800 | [diff] [blame] | 81 | int r; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 82 | |
| 83 | if ((fp = fopen(fn, "rb")) != NULL) { |
| 84 | |
Nan Li | dfe6e42 | 2016-05-13 21:46:26 +0800 | [diff] [blame] | 85 | r = fseek(fp, 0, SEEK_END); |
| 86 | if (r) { |
| 87 | fprintf(stderr,"Fseek failed\n"); |
| 88 | goto fclose_fp; |
| 89 | } |
| 90 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 91 | size = ftell(fp); |
Nan Li | dfe6e42 | 2016-05-13 21:46:26 +0800 | [diff] [blame] | 92 | 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 Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 104 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 105 | *buffer = new uint8_t [size]; |
| 106 | |
Nan Li | dfe6e42 | 2016-05-13 21:46:26 +0800 | [diff] [blame] | 107 | r = fread(*buffer, 1, size, fp); |
| 108 | if ( r != size) { |
| 109 | size = 0; |
| 110 | fprintf(stderr,"Fread failed\n"); |
| 111 | } |
| 112 | |
| 113 | fclose_fp: |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 114 | fclose(fp); |
| 115 | } |
| 116 | |
Matthew Barth | 8b47005 | 2016-09-21 10:02:57 -0500 | [diff] [blame] | 117 | return static_cast<size_t>(size); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
| 121 | const 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 | |
| 130 | int 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 Austen | 6dc2e91 | 2016-02-20 01:00:06 -0600 | [diff] [blame] | 142 | // Simply no associations if the sensor can not be found |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 143 | if (strlen(dbusint.path) < 1) { |
Chris Austen | 6dc2e91 | 2016-02-20 01:00:06 -0600 | [diff] [blame] | 144 | printf("Sensor 0x%x not found\n", sensor); |
| 145 | memset(dbusint.path,0,sizeof(dbusint.path)); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | asprintf(m, "%s", dbusint.path); |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | int create_esel_description(const uint8_t *buffer, const char *sev, char **message) { |
| 156 | |
| 157 | |
| 158 | ipmi_add_sel_request_t *p; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 159 | 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 | |
| 173 | int 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 178 | uint16_t x; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 179 | int r; |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 180 | const char *object_name = "/org/openbmc/records/events"; |
| 181 | char *bus_name = NULL; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 182 | |
Chris Austen | 30195fa | 2015-11-13 14:39:19 -0600 | [diff] [blame] | 183 | mbus = ipmid_get_sd_bus_connection(); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 184 | 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 Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 189 | r = sd_bus_message_new_method_call(mbus,&m, |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 190 | bus_name, |
| 191 | object_name, |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 192 | "org.openbmc.recordlog", |
| 193 | "acceptHostMessage"); |
| 194 | if (r < 0) { |
| 195 | fprintf(stderr, "Failed to add the method object: %s\n", strerror(-r)); |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 196 | goto finish; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 197 | } |
| 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 202 | goto finish; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 203 | } |
| 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 208 | goto finish; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 209 | } |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 210 | // 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 Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 213 | fprintf(stderr, "Failed to call the method: %s %s\n", __FUNCTION__, strerror(-r)); |
| 214 | goto finish; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 215 | } |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 216 | r = sd_bus_message_read(reply, "q", &x); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 217 | if (r < 0) { |
Chris Austen | 7cc3332 | 2015-11-11 00:20:22 -0600 | [diff] [blame] | 218 | fprintf(stderr, "Failed to get a rc from the method: %s\n", strerror(-r)); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Chris Austen | 169395e | 2015-12-02 20:56:15 -0600 | [diff] [blame] | 221 | finish: |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 222 | sd_bus_error_free(&error); |
vishwa | 1eaea4f | 2016-02-26 11:57:40 -0600 | [diff] [blame] | 223 | m = sd_bus_message_unref(m); |
| 224 | reply = sd_bus_message_unref(reply); |
Sergey Solomin | eb9b814 | 2016-08-23 09:07:28 -0500 | [diff] [blame] | 225 | free (bus_name); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 226 | return r; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | |
| 230 | void send_esel(uint16_t recordid) { |
Joel Stanley | f19539e | 2015-11-25 17:24:05 +1030 | [diff] [blame] | 231 | char *desc, *assoc; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 232 | const char *sev; |
| 233 | uint8_t *buffer = NULL; |
Chris Austen | 6dc2e91 | 2016-02-20 01:00:06 -0600 | [diff] [blame] | 234 | const char *path = "/tmp/esel"; |
Matthew Barth | 8b47005 | 2016-09-21 10:02:57 -0500 | [diff] [blame] | 235 | ssize_t sz; |
Nan Li | dfe6e42 | 2016-05-13 21:46:26 +0800 | [diff] [blame] | 236 | int r; |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 237 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 238 | sz = getfilestream(path, &buffer); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 239 | if (sz == 0) { |
| 240 | printf("Error file does not exist %d\n",__LINE__); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 244 | sev = create_esel_severity(buffer); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 245 | create_esel_association(buffer, &assoc); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 246 | create_esel_description(buffer, sev, &desc); |
| 247 | |
Nan Li | dfe6e42 | 2016-05-13 21:46:26 +0800 | [diff] [blame] | 248 | 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 Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 252 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 253 | free(assoc); |
| 254 | free(desc); |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 255 | delete[] buffer; |
| 256 | |
Chris Austen | 41a4b31 | 2015-10-25 03:45:42 -0500 | [diff] [blame] | 257 | return; |
| 258 | } |