1 #include "config.h" 2 3 #include "oemhandler.hpp" 4 5 #include "elog-errors.hpp" 6 #include "local_users.hpp" 7 8 #include <endian.h> 9 #include <host-ipmid/ipmid-api.h> 10 #include <stdio.h> 11 #include <string.h> 12 #include <systemd/sd-bus.h> 13 14 #include <fstream> 15 #include <functional> 16 #include <host-interface.hpp> 17 #include <host-ipmid/ipmid-host-cmd.hpp> 18 #include <memory> 19 #include <org/open_power/Host/error.hpp> 20 #include <org/open_power/OCC/Metrics/error.hpp> 21 #include <sdbusplus/bus.hpp> 22 23 void register_netfn_ibm_oem_commands() __attribute__((constructor)); 24 25 const char* g_esel_path = "/tmp/esel"; 26 uint16_t g_record_id = 0x0001; 27 using namespace phosphor::logging; 28 constexpr auto occMetricsType = 0xDD; 29 30 extern const ObjectIDMap invSensors; 31 const std::map<uint8_t, Entry::Level> severityMap{ 32 {0x10, Entry::Level::Warning}, // Recoverable error 33 {0x20, Entry::Level::Warning}, // Predictive error 34 {0x40, Entry::Level::Error}, // Unrecoverable error 35 {0x50, Entry::Level::Error}, // Critical error 36 {0x60, Entry::Level::Error}, // Error from a diagnostic test 37 {0x70, Entry::Level::Warning}, // Recoverable symptom 38 {0xFF, Entry::Level::Error}, // Unknown error 39 }; 40 41 Entry::Level mapSeverity(const std::string& eSELData) 42 { 43 constexpr size_t severityOffset = 0x4A; 44 45 if (eSELData.size() > severityOffset) 46 { 47 // Dive in to the IBM log to find the severity 48 uint8_t sev = 0xF0 & eSELData[severityOffset]; 49 50 auto find = severityMap.find(sev); 51 if (find != severityMap.end()) 52 { 53 return find->second; 54 } 55 } 56 57 // Default to Entry::Level::Error if a matching is not found. 58 return Entry::Level::Error; 59 } 60 61 std::string mapCalloutAssociation(const std::string& eSELData) 62 { 63 auto rec = reinterpret_cast<const SELEventRecord*>(&eSELData[0]); 64 uint8_t sensor = rec->sensorNum; 65 66 /* 67 * Search the sensor number to inventory path mapping to figure out the 68 * inventory associated with the ESEL. 69 */ 70 auto found = std::find_if(invSensors.begin(), invSensors.end(), 71 [&sensor](const auto& iter) { 72 return (iter.second.sensorID == sensor); 73 }); 74 if (found != invSensors.end()) 75 { 76 return found->first; 77 } 78 79 return {}; 80 } 81 82 std::string readESEL(const char* fileName) 83 { 84 std::string content{}; 85 86 std::ifstream handle(fileName); 87 88 if (handle.fail()) 89 { 90 log<level::ERR>("Failed to open eSEL", entry("FILENAME=%s", fileName)); 91 return content; 92 } 93 94 handle.seekg(0, std::ios::end); 95 content.resize(handle.tellg()); 96 handle.seekg(0, std::ios::beg); 97 handle.read(&content[0], content.size()); 98 handle.close(); 99 100 return content; 101 } 102 103 void createOCCLogEntry(const std::string& eSELData) 104 { 105 // Each byte in eSEL is formatted as %02x with a space between bytes and 106 // insert '/0' at the end of the character array. 107 constexpr auto byteSeperator = 3; 108 109 std::unique_ptr<char[]> data( 110 new char[(eSELData.size() * byteSeperator) + 1]()); 111 112 for (size_t i = 0; i < eSELData.size(); i++) 113 { 114 sprintf(&data[i * byteSeperator], "%02x ", eSELData[i]); 115 } 116 data[eSELData.size() * byteSeperator] = '\0'; 117 118 using error = sdbusplus::org::open_power::OCC::Metrics::Error::Event; 119 using metadata = org::open_power::OCC::Metrics::Event; 120 121 report<error>(metadata::ESEL(data.get())); 122 } 123 124 void createHostEntry(const std::string& eSELData) 125 { 126 // Each byte in eSEL is formatted as %02x with a space between bytes and 127 // insert '/0' at the end of the character array. 128 constexpr auto byteSeperator = 3; 129 130 auto sev = mapSeverity(eSELData); 131 auto inventoryPath = mapCalloutAssociation(eSELData); 132 133 if (!inventoryPath.empty()) 134 { 135 std::unique_ptr<char[]> data( 136 new char[(eSELData.size() * byteSeperator) + 1]()); 137 138 for (size_t i = 0; i < eSELData.size(); i++) 139 { 140 sprintf(&data[i * byteSeperator], "%02x ", eSELData[i]); 141 } 142 data[eSELData.size() * byteSeperator] = '\0'; 143 144 using hosterror = sdbusplus::org::open_power::Host::Error::Event; 145 using hostmetadata = org::open_power::Host::Event; 146 147 report<hosterror>( 148 sev, hostmetadata::ESEL(data.get()), 149 hostmetadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str())); 150 } 151 } 152 153 /////////////////////////////////////////////////////////////////////////////// 154 // For the First partial add eSEL the SEL Record ID and offset 155 // value should be 0x0000. The extended data needs to be in 156 // the form of an IPMI SEL Event Record, with Event sensor type 157 // of 0xDF and Event Message format of 0x04. The returned 158 // Record ID should be used for all partial eSEL adds. 159 // 160 // This function creates a /tmp/esel file to store the 161 // incoming partial esel. It is the role of some other 162 // function to commit the error log in to long term 163 // storage. Likely via the ipmi add_sel command. 164 /////////////////////////////////////////////////////////////////////////////// 165 ipmi_ret_t ipmi_ibm_oem_partial_esel(ipmi_netfn_t netfn, ipmi_cmd_t cmd, 166 ipmi_request_t request, 167 ipmi_response_t response, 168 ipmi_data_len_t data_len, 169 ipmi_context_t context) 170 { 171 uint8_t* reqptr = (uint8_t*)request; 172 esel_request_t esel_req; 173 FILE* fp; 174 int r = 0; 175 uint8_t rlen; 176 ipmi_ret_t rc = IPMI_CC_OK; 177 const char* pio; 178 179 esel_req.resid = le16toh((((uint16_t)reqptr[1]) << 8) + reqptr[0]); 180 esel_req.selrecord = le16toh((((uint16_t)reqptr[3]) << 8) + reqptr[2]); 181 esel_req.offset = le16toh((((uint16_t)reqptr[5]) << 8) + reqptr[4]); 182 esel_req.progress = reqptr[6]; 183 184 // According to IPMI spec, Reservation ID must be checked. 185 if (!checkSELReservation(esel_req.resid)) 186 { 187 // 0xc5 means Reservation Cancelled or Invalid Reservation ID. 188 printf("Used Reservation ID = %d\n", esel_req.resid); 189 rc = IPMI_CC_INVALID_RESERVATION_ID; 190 191 // clean g_esel_path. 192 r = remove(g_esel_path); 193 if (r < 0) 194 fprintf(stderr, "Error deleting %s\n", g_esel_path); 195 196 return rc; 197 } 198 199 // OpenPOWER Host Interface spec says if RecordID and Offset are 200 // 0 then then this is a new request 201 if (!esel_req.selrecord && !esel_req.offset) 202 pio = "wb"; 203 else 204 pio = "rb+"; 205 206 rlen = (*data_len) - (uint8_t)(sizeof(esel_request_t)); 207 208 if ((fp = fopen(g_esel_path, pio)) != NULL) 209 { 210 fseek(fp, esel_req.offset, SEEK_SET); 211 fwrite(reqptr + (uint8_t)(sizeof(esel_request_t)), rlen, 1, fp); 212 fclose(fp); 213 214 *data_len = sizeof(g_record_id); 215 memcpy(response, &g_record_id, *data_len); 216 } 217 else 218 { 219 fprintf(stderr, "Error trying to perform %s for esel%s\n", pio, 220 g_esel_path); 221 rc = IPMI_CC_INVALID; 222 *data_len = 0; 223 } 224 225 // The first bit presents that this is the last partial packet 226 // coming down. If that is the case advance the record id so we 227 // don't overlap logs. This allows anyone to establish a log 228 // directory system. 229 if (esel_req.progress & 1) 230 { 231 g_record_id++; 232 233 auto eSELData = readESEL(g_esel_path); 234 235 if (eSELData.empty()) 236 { 237 return IPMI_CC_UNSPECIFIED_ERROR; 238 } 239 240 // If the eSEL record type is OCC metrics, then create the OCC log 241 // entry. 242 if (eSELData[2] == occMetricsType) 243 { 244 createOCCLogEntry(eSELData); 245 } 246 else 247 { 248 createHostEntry(eSELData); 249 } 250 } 251 252 return rc; 253 } 254 255 // Prepare for FW Update. 256 // Execute needed commands to prepare the system for a fw update from the host. 257 ipmi_ret_t ipmi_ibm_oem_prep_fw_update(ipmi_netfn_t netfn, ipmi_cmd_t cmd, 258 ipmi_request_t request, 259 ipmi_response_t response, 260 ipmi_data_len_t data_len, 261 ipmi_context_t context) 262 { 263 ipmi_ret_t ipmi_rc = IPMI_CC_OK; 264 *data_len = 0; 265 266 int rc = 0; 267 std::ofstream rwfs_file; 268 const char* busname = "org.openbmc.control.Bmc"; 269 const char* objname = "/org/openbmc/control/bmc0"; 270 const char* iface = "org.openbmc.control.Bmc"; 271 sd_bus* bus = ipmid_get_sd_bus_connection(); 272 sd_bus_message* reply = NULL; 273 sd_bus_error error = SD_BUS_ERROR_NULL; 274 int r = 0; 275 276 // Set one time flag 277 rc = system( 278 "fw_setenv openbmconce copy-files-to-ram copy-base-filesystem-to-ram"); 279 rc = WEXITSTATUS(rc); 280 if (rc != 0) 281 { 282 fprintf(stderr, "fw_setenv openbmconce failed with rc=%d\n", rc); 283 return IPMI_CC_UNSPECIFIED_ERROR; 284 } 285 286 // Touch the image-rwfs file to perform an empty update to force the save 287 // in case we're already in ram and the flash is the same causing the ram 288 // files to not be copied back to flash 289 rwfs_file.open("/run/initramfs/image-rwfs", 290 std::ofstream::out | std::ofstream::app); 291 rwfs_file.close(); 292 293 // Reboot the BMC for settings to take effect 294 r = sd_bus_call_method(bus, busname, objname, iface, "warmReset", &error, 295 &reply, NULL); 296 if (r < 0) 297 { 298 fprintf(stderr, "Failed to reset BMC: %s\n", strerror(-r)); 299 return -1; 300 } 301 printf("Warning: BMC is going down for reboot!\n"); 302 sd_bus_error_free(&error); 303 reply = sd_bus_message_unref(reply); 304 305 return ipmi_rc; 306 } 307 308 ipmi_ret_t ipmi_ibm_oem_reset_bmc_auth(ipmi_netfn_t netfn, ipmi_cmd_t cmd, 309 ipmi_request_t request, 310 ipmi_response_t response, 311 ipmi_data_len_t data_len, 312 ipmi_context_t context) 313 { 314 ipmi_ret_t rc; 315 316 rc = local::users::enableUsers(); 317 318 return rc; 319 } 320 321 namespace 322 { 323 // Storage to keep the object alive during process life 324 std::unique_ptr<open_power::host::command::Host> opHost 325 __attribute__((init_priority(101))); 326 std::unique_ptr<sdbusplus::server::manager::manager> objManager 327 __attribute__((init_priority(101))); 328 } // namespace 329 330 void register_netfn_ibm_oem_commands() 331 { 332 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_IBM_OEM, 333 IPMI_CMD_PESEL); 334 ipmi_register_callback(NETFUN_IBM_OEM, IPMI_CMD_PESEL, NULL, 335 ipmi_ibm_oem_partial_esel, SYSTEM_INTERFACE); 336 337 printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_OEM, 338 IPMI_CMD_PREP_FW_UPDATE); 339 ipmi_register_callback(NETFUN_OEM, IPMI_CMD_PREP_FW_UPDATE, NULL, 340 ipmi_ibm_oem_prep_fw_update, SYSTEM_INTERFACE); 341 342 ipmi_register_callback(NETFUN_IBM_OEM, IPMI_CMD_RESET_BMC_AUTH, NULL, 343 ipmi_ibm_oem_reset_bmc_auth, SYSTEM_INTERFACE); 344 345 // Create new object on the bus 346 auto objPath = std::string{CONTROL_HOST_OBJ_MGR} + '/' + HOST_NAME + '0'; 347 348 // Add sdbusplus ObjectManager. 349 auto& sdbusPlusHandler = ipmid_get_sdbus_plus_handler(); 350 objManager = std::make_unique<sdbusplus::server::manager::manager>( 351 *sdbusPlusHandler, CONTROL_HOST_OBJ_MGR); 352 353 opHost = std::make_unique<open_power::host::command::Host>( 354 *sdbusPlusHandler, objPath.c_str()); 355 356 // Service for this is provided by phosphor layer systemcmdintf 357 // and this will be as part of that. 358 return; 359 } 360