Vishwanatha Subbanna | 0765506 | 2017-07-14 20:31:57 +0530 | [diff] [blame] | 1 | #include "config.h" |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 2 | |
| 3 | #include "oemhandler.hpp" |
| 4 | |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 5 | #include "elog-errors.hpp" |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 6 | |
| 7 | #include <endian.h> |
William A. Kennington III | 822eaf6 | 2019-02-12 15:20:06 -0800 | [diff] [blame] | 8 | #include <ipmid/api.h> |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <string.h> |
Vishwanatha Subbanna | 0765506 | 2017-07-14 20:31:57 +0530 | [diff] [blame] | 11 | #include <systemd/sd-bus.h> |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 12 | |
| 13 | #include <fstream> |
| 14 | #include <functional> |
Vishwanatha Subbanna | 0765506 | 2017-07-14 20:31:57 +0530 | [diff] [blame] | 15 | #include <host-interface.hpp> |
William A. Kennington III | 822eaf6 | 2019-02-12 15:20:06 -0800 | [diff] [blame] | 16 | #include <ipmid-host/cmd.hpp> |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 17 | #include <memory> |
Tom Joseph | 3503fdc | 2019-01-10 11:25:27 +0530 | [diff] [blame] | 18 | #include <org/open_power/Host/error.hpp> |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 19 | #include <org/open_power/OCC/Metrics/error.hpp> |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 20 | #include <sdbusplus/bus.hpp> |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 21 | #include <sdbusplus/exception.hpp> |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 22 | |
Andrew Jeffery | 8fb3f03 | 2018-07-27 16:45:44 +0930 | [diff] [blame] | 23 | void register_netfn_ibm_oem_commands() __attribute__((constructor)); |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 24 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 25 | const char* g_esel_path = "/tmp/esel"; |
Chris Austen | 29a8e0f | 2015-10-30 11:44:38 -0500 | [diff] [blame] | 26 | uint16_t g_record_id = 0x0001; |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 27 | using namespace phosphor::logging; |
| 28 | constexpr auto occMetricsType = 0xDD; |
| 29 | |
Tom Joseph | b61b107 | 2019-01-28 12:32:52 +0530 | [diff] [blame] | 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 | |
Patrick Williams | a0a221f | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 82 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 83 | const std::string& interface) |
| 84 | { |
| 85 | auto method = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, MAPPER_IFACE, |
| 86 | "GetObject"); |
| 87 | |
| 88 | method.append(path); |
| 89 | method.append(std::vector<std::string>({interface})); |
| 90 | |
| 91 | std::map<std::string, std::vector<std::string>> response; |
| 92 | |
| 93 | try |
| 94 | { |
| 95 | auto reply = bus.call(method); |
| 96 | |
| 97 | reply.read(response); |
| 98 | if (response.empty()) |
| 99 | { |
| 100 | log<level::ERR>("Error in mapper response for getting service name", |
| 101 | entry("PATH=%s", path.c_str()), |
| 102 | entry("INTERFACE=%s", interface.c_str())); |
| 103 | return std::string{}; |
| 104 | } |
| 105 | } |
Patrick Williams | a0a221f | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 106 | catch (const sdbusplus::exception_t& e) |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 107 | { |
| 108 | log<level::ERR>("Error in mapper method call", |
| 109 | entry("ERROR=%s", e.what())); |
| 110 | return std::string{}; |
| 111 | } |
| 112 | |
| 113 | return response.begin()->first; |
| 114 | } |
| 115 | |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 116 | std::string readESEL(const char* fileName) |
| 117 | { |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 118 | std::string content{}; |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 119 | |
| 120 | std::ifstream handle(fileName); |
| 121 | |
| 122 | if (handle.fail()) |
| 123 | { |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 124 | log<level::ERR>("Failed to open eSEL", entry("FILENAME=%s", fileName)); |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 125 | return content; |
| 126 | } |
| 127 | |
| 128 | handle.seekg(0, std::ios::end); |
| 129 | content.resize(handle.tellg()); |
| 130 | handle.seekg(0, std::ios::beg); |
| 131 | handle.read(&content[0], content.size()); |
| 132 | handle.close(); |
| 133 | |
| 134 | return content; |
| 135 | } |
| 136 | |
| 137 | void createOCCLogEntry(const std::string& eSELData) |
| 138 | { |
| 139 | // Each byte in eSEL is formatted as %02x with a space between bytes and |
| 140 | // insert '/0' at the end of the character array. |
| 141 | constexpr auto byteSeperator = 3; |
| 142 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 143 | std::unique_ptr<char[]> data( |
| 144 | new char[(eSELData.size() * byteSeperator) + 1]()); |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 145 | |
| 146 | for (size_t i = 0; i < eSELData.size(); i++) |
| 147 | { |
| 148 | sprintf(&data[i * byteSeperator], "%02x ", eSELData[i]); |
| 149 | } |
| 150 | data[eSELData.size() * byteSeperator] = '\0'; |
| 151 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 152 | using error = sdbusplus::org::open_power::OCC::Metrics::Error::Event; |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 153 | using metadata = org::open_power::OCC::Metrics::Event; |
| 154 | |
| 155 | report<error>(metadata::ESEL(data.get())); |
| 156 | } |
| 157 | |
Tom Joseph | 3503fdc | 2019-01-10 11:25:27 +0530 | [diff] [blame] | 158 | void createHostEntry(const std::string& eSELData) |
| 159 | { |
| 160 | // Each byte in eSEL is formatted as %02x with a space between bytes and |
| 161 | // insert '/0' at the end of the character array. |
Tom Joseph | 3503fdc | 2019-01-10 11:25:27 +0530 | [diff] [blame] | 162 | constexpr auto byteSeperator = 3; |
| 163 | |
Tom Joseph | b61b107 | 2019-01-28 12:32:52 +0530 | [diff] [blame] | 164 | auto sev = mapSeverity(eSELData); |
| 165 | auto inventoryPath = mapCalloutAssociation(eSELData); |
Tom Joseph | 3503fdc | 2019-01-10 11:25:27 +0530 | [diff] [blame] | 166 | |
Tom Joseph | b61b107 | 2019-01-28 12:32:52 +0530 | [diff] [blame] | 167 | if (!inventoryPath.empty()) |
Tom Joseph | 3503fdc | 2019-01-10 11:25:27 +0530 | [diff] [blame] | 168 | { |
Tom Joseph | b61b107 | 2019-01-28 12:32:52 +0530 | [diff] [blame] | 169 | std::unique_ptr<char[]> data( |
| 170 | new char[(eSELData.size() * byteSeperator) + 1]()); |
| 171 | |
| 172 | for (size_t i = 0; i < eSELData.size(); i++) |
| 173 | { |
| 174 | sprintf(&data[i * byteSeperator], "%02x ", eSELData[i]); |
| 175 | } |
| 176 | data[eSELData.size() * byteSeperator] = '\0'; |
| 177 | |
| 178 | using hosterror = sdbusplus::org::open_power::Host::Error::Event; |
| 179 | using hostmetadata = org::open_power::Host::Event; |
| 180 | |
| 181 | report<hosterror>( |
| 182 | sev, hostmetadata::ESEL(data.get()), |
| 183 | hostmetadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str())); |
Tom Joseph | 3503fdc | 2019-01-10 11:25:27 +0530 | [diff] [blame] | 184 | } |
Tom Joseph | 3503fdc | 2019-01-10 11:25:27 +0530 | [diff] [blame] | 185 | } |
| 186 | |
Adriana Kobylak | 080503e | 2023-01-05 13:44:25 -0600 | [diff] [blame^] | 187 | /** @brief Helper function to do a graceful restart (reboot) of the BMC. |
| 188 | @return 0 on success, -1 on error |
| 189 | */ |
| 190 | int rebootBMC() |
| 191 | { |
| 192 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 193 | auto service = getService(bus, stateBmcPath, stateBmcIntf); |
| 194 | if (service.empty()) |
| 195 | { |
| 196 | log<level::ERR>("Error getting the service name to reboot the BMC."); |
| 197 | return -1; |
| 198 | } |
| 199 | std::variant<std::string> reboot = |
| 200 | "xyz.openbmc_project.State.BMC.Transition.Reboot"; |
| 201 | auto method = bus.new_method_call(service.c_str(), stateBmcPath, |
| 202 | propertiesIntf, "Set"); |
| 203 | method.append(stateBmcIntf, "RequestedBMCTransition", reboot); |
| 204 | try |
| 205 | { |
| 206 | bus.call_noreply(method); |
| 207 | } |
| 208 | catch (const sdbusplus::exception_t& e) |
| 209 | { |
| 210 | log<level::ERR>("Error calling to reboot the BMC.", |
| 211 | entry("ERROR=%s", e.what())); |
| 212 | return -1; |
| 213 | } |
| 214 | return 0; |
| 215 | } |
| 216 | |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 217 | /////////////////////////////////////////////////////////////////////////////// |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 218 | // For the First partial add eSEL the SEL Record ID and offset |
| 219 | // value should be 0x0000. The extended data needs to be in |
| 220 | // the form of an IPMI SEL Event Record, with Event sensor type |
| 221 | // of 0xDF and Event Message format of 0x04. The returned |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 222 | // Record ID should be used for all partial eSEL adds. |
| 223 | // |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 224 | // This function creates a /tmp/esel file to store the |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 225 | // incoming partial esel. It is the role of some other |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 226 | // function to commit the error log in to long term |
| 227 | // storage. Likely via the ipmi add_sel command. |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 228 | /////////////////////////////////////////////////////////////////////////////// |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 229 | ipmi_ret_t ipmi_ibm_oem_partial_esel(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 230 | ipmi_request_t request, |
| 231 | ipmi_response_t response, |
| 232 | ipmi_data_len_t data_len, |
| 233 | ipmi_context_t context) |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 234 | { |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 235 | uint8_t* reqptr = (uint8_t*)request; |
| 236 | esel_request_t esel_req; |
| 237 | FILE* fp; |
| 238 | int r = 0; |
| 239 | uint8_t rlen; |
| 240 | ipmi_ret_t rc = IPMI_CC_OK; |
| 241 | const char* pio; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 242 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 243 | esel_req.resid = le16toh((((uint16_t)reqptr[1]) << 8) + reqptr[0]); |
| 244 | esel_req.selrecord = le16toh((((uint16_t)reqptr[3]) << 8) + reqptr[2]); |
| 245 | esel_req.offset = le16toh((((uint16_t)reqptr[5]) << 8) + reqptr[4]); |
| 246 | esel_req.progress = reqptr[6]; |
Nan Li | b6c4c56 | 2016-03-30 17:06:13 +0800 | [diff] [blame] | 247 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 248 | // According to IPMI spec, Reservation ID must be checked. |
| 249 | if (!checkSELReservation(esel_req.resid)) |
| 250 | { |
| 251 | // 0xc5 means Reservation Cancelled or Invalid Reservation ID. |
| 252 | printf("Used Reservation ID = %d\n", esel_req.resid); |
| 253 | rc = IPMI_CC_INVALID_RESERVATION_ID; |
Nan Li | b6c4c56 | 2016-03-30 17:06:13 +0800 | [diff] [blame] | 254 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 255 | // clean g_esel_path. |
| 256 | r = remove(g_esel_path); |
| 257 | if (r < 0) |
| 258 | fprintf(stderr, "Error deleting %s\n", g_esel_path); |
Nan Li | b6c4c56 | 2016-03-30 17:06:13 +0800 | [diff] [blame] | 259 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 260 | return rc; |
| 261 | } |
Nan Li | b6c4c56 | 2016-03-30 17:06:13 +0800 | [diff] [blame] | 262 | |
| 263 | // OpenPOWER Host Interface spec says if RecordID and Offset are |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 264 | // 0 then then this is a new request |
| 265 | if (!esel_req.selrecord && !esel_req.offset) |
| 266 | pio = "wb"; |
| 267 | else |
| 268 | pio = "rb+"; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 269 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 270 | rlen = (*data_len) - (uint8_t)(sizeof(esel_request_t)); |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 271 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 272 | if ((fp = fopen(g_esel_path, pio)) != NULL) |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 273 | { |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 274 | fseek(fp, esel_req.offset, SEEK_SET); |
| 275 | fwrite(reqptr + (uint8_t)(sizeof(esel_request_t)), rlen, 1, fp); |
| 276 | fclose(fp); |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 277 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 278 | *data_len = sizeof(g_record_id); |
| 279 | memcpy(response, &g_record_id, *data_len); |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | fprintf(stderr, "Error trying to perform %s for esel%s\n", pio, |
| 284 | g_esel_path); |
| 285 | rc = IPMI_CC_INVALID; |
| 286 | *data_len = 0; |
| 287 | } |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 288 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 289 | // The first bit presents that this is the last partial packet |
| 290 | // coming down. If that is the case advance the record id so we |
| 291 | // don't overlap logs. This allows anyone to establish a log |
| 292 | // directory system. |
| 293 | if (esel_req.progress & 1) |
| 294 | { |
| 295 | g_record_id++; |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 296 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 297 | auto eSELData = readESEL(g_esel_path); |
| 298 | |
| 299 | if (eSELData.empty()) |
| 300 | { |
| 301 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 302 | } |
| 303 | |
| 304 | // If the eSEL record type is OCC metrics, then create the OCC log |
| 305 | // entry. |
| 306 | if (eSELData[2] == occMetricsType) |
| 307 | { |
| 308 | createOCCLogEntry(eSELData); |
| 309 | } |
Tom Joseph | 3503fdc | 2019-01-10 11:25:27 +0530 | [diff] [blame] | 310 | else |
| 311 | { |
| 312 | createHostEntry(eSELData); |
| 313 | } |
Tom Joseph | 246bc0d | 2017-10-17 16:08:38 +0530 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | return rc; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 317 | } |
| 318 | |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 319 | // Prepare for FW Update. |
| 320 | // Execute needed commands to prepare the system for a fw update from the host. |
| 321 | ipmi_ret_t ipmi_ibm_oem_prep_fw_update(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 322 | ipmi_request_t request, |
| 323 | ipmi_response_t response, |
| 324 | ipmi_data_len_t data_len, |
| 325 | ipmi_context_t context) |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 326 | { |
| 327 | ipmi_ret_t ipmi_rc = IPMI_CC_OK; |
| 328 | *data_len = 0; |
| 329 | |
| 330 | int rc = 0; |
| 331 | std::ofstream rwfs_file; |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 332 | |
| 333 | // Set one time flag |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 334 | rc = system( |
| 335 | "fw_setenv openbmconce copy-files-to-ram copy-base-filesystem-to-ram"); |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 336 | rc = WEXITSTATUS(rc); |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 337 | if (rc != 0) |
| 338 | { |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 339 | fprintf(stderr, "fw_setenv openbmconce failed with rc=%d\n", rc); |
Andrew Geissler | d929605 | 2017-06-15 14:57:25 -0500 | [diff] [blame] | 340 | return IPMI_CC_UNSPECIFIED_ERROR; |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // Touch the image-rwfs file to perform an empty update to force the save |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 344 | // in case we're already in ram and the flash is the same causing the ram |
| 345 | // files to not be copied back to flash |
| 346 | rwfs_file.open("/run/initramfs/image-rwfs", |
| 347 | std::ofstream::out | std::ofstream::app); |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 348 | rwfs_file.close(); |
| 349 | |
| 350 | // Reboot the BMC for settings to take effect |
Adriana Kobylak | 080503e | 2023-01-05 13:44:25 -0600 | [diff] [blame^] | 351 | rc = rebootBMC(); |
| 352 | if (rc < 0) |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 353 | { |
Adriana Kobylak | 080503e | 2023-01-05 13:44:25 -0600 | [diff] [blame^] | 354 | fprintf(stderr, "Failed to reset BMC: %s\n", strerror(-rc)); |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 355 | return -1; |
| 356 | } |
| 357 | printf("Warning: BMC is going down for reboot!\n"); |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 358 | |
| 359 | return ipmi_rc; |
| 360 | } |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 361 | |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 362 | ipmi_ret_t ipmi_ibm_oem_bmc_factory_reset(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 363 | ipmi_request_t request, |
| 364 | ipmi_response_t response, |
| 365 | ipmi_data_len_t data_len, |
| 366 | ipmi_context_t context) |
Adriana Kobylak | 81e2310 | 2018-08-09 14:44:19 -0500 | [diff] [blame] | 367 | { |
Patrick Williams | a0a221f | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 368 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Andrew Geissler | 64354b6 | 2019-09-20 13:39:24 -0500 | [diff] [blame] | 369 | |
| 370 | // Since this is a one way command (i.e. the host is requesting a power |
| 371 | // off of itself and a reboot of the BMC) we can exceed the 5 second |
| 372 | // IPMI timeout. Testing has shown that the power off can take up to |
| 373 | // 10 seconds so give it at least 15 |
| 374 | constexpr auto powerOffWait = std::chrono::seconds(15); |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 375 | constexpr auto setFactoryWait = std::chrono::seconds(3); |
Matt Spinler | 34aca01 | 2018-09-25 11:21:06 -0500 | [diff] [blame] | 376 | |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 377 | // Power Off Chassis |
| 378 | auto service = getService(bus, stateChassisPath, stateChassisIntf); |
| 379 | if (service.empty()) |
| 380 | { |
| 381 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 382 | } |
Patrick Williams | 6195010 | 2020-05-13 17:49:58 -0500 | [diff] [blame] | 383 | std::variant<std::string> off = |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 384 | "xyz.openbmc_project.State.Chassis.Transition.Off"; |
| 385 | auto method = bus.new_method_call(service.c_str(), stateChassisPath, |
| 386 | propertiesIntf, "Set"); |
| 387 | method.append(stateChassisIntf, "RequestedPowerTransition", off); |
| 388 | try |
| 389 | { |
| 390 | bus.call_noreply(method); |
| 391 | } |
Patrick Williams | a0a221f | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 392 | catch (const sdbusplus::exception_t& e) |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 393 | { |
| 394 | log<level::ERR>("Error powering off the chassis", |
| 395 | entry("ERROR=%s", e.what())); |
| 396 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 397 | } |
Matt Spinler | 34aca01 | 2018-09-25 11:21:06 -0500 | [diff] [blame] | 398 | |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 399 | // Wait a few seconds for the chassis to power off |
| 400 | std::this_thread::sleep_for(powerOffWait); |
| 401 | |
| 402 | // Set Factory Reset |
| 403 | method = bus.new_method_call(bmcUpdaterServiceName, softwarePath, |
| 404 | factoryResetIntf, "Reset"); |
| 405 | try |
| 406 | { |
| 407 | bus.call_noreply(method); |
| 408 | } |
Patrick Williams | a0a221f | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 409 | catch (const sdbusplus::exception_t& e) |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 410 | { |
| 411 | log<level::ERR>("Error setting factory reset", |
| 412 | entry("ERROR=%s", e.what())); |
| 413 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 414 | } |
| 415 | |
| 416 | // Wait a few seconds for service that sets the reset env variable to |
| 417 | // complete before the BMC is rebooted |
| 418 | std::this_thread::sleep_for(setFactoryWait); |
| 419 | |
| 420 | // Reboot BMC |
Adriana Kobylak | 080503e | 2023-01-05 13:44:25 -0600 | [diff] [blame^] | 421 | auto rc = rebootBMC(); |
| 422 | if (rc < 0) |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 423 | { |
Adriana Kobylak | 080503e | 2023-01-05 13:44:25 -0600 | [diff] [blame^] | 424 | log<level::ALERT>("The BMC needs to be manually rebooted to complete " |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 425 | "the factory reset."); |
| 426 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 427 | } |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 428 | |
| 429 | return IPMI_CC_OK; |
Adriana Kobylak | 81e2310 | 2018-08-09 14:44:19 -0500 | [diff] [blame] | 430 | } |
| 431 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 432 | namespace |
| 433 | { |
Vishwanatha Subbanna | 0765506 | 2017-07-14 20:31:57 +0530 | [diff] [blame] | 434 | // Storage to keep the object alive during process life |
| 435 | std::unique_ptr<open_power::host::command::Host> opHost |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 436 | __attribute__((init_priority(101))); |
Patrick Williams | a0a221f | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 437 | std::unique_ptr<sdbusplus::server::manager_t> objManager |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 438 | __attribute__((init_priority(101))); |
| 439 | } // namespace |
Vishwanatha Subbanna | 0765506 | 2017-07-14 20:31:57 +0530 | [diff] [blame] | 440 | |
Andrew Jeffery | 8fb3f03 | 2018-07-27 16:45:44 +0930 | [diff] [blame] | 441 | void register_netfn_ibm_oem_commands() |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 442 | { |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 443 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_IBM_OEM, |
| 444 | IPMI_CMD_PESEL); |
| 445 | ipmi_register_callback(NETFUN_IBM_OEM, IPMI_CMD_PESEL, NULL, |
| 446 | ipmi_ibm_oem_partial_esel, SYSTEM_INTERFACE); |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 447 | |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 448 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_OEM, |
| 449 | IPMI_CMD_PREP_FW_UPDATE); |
| 450 | ipmi_register_callback(NETFUN_OEM, IPMI_CMD_PREP_FW_UPDATE, NULL, |
| 451 | ipmi_ibm_oem_prep_fw_update, SYSTEM_INTERFACE); |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 452 | |
Adriana Kobylak | 81c34df | 2018-11-01 11:44:16 -0500 | [diff] [blame] | 453 | ipmi_register_callback(NETFUN_IBM_OEM, IPMI_CMD_BMC_FACTORY_RESET, NULL, |
| 454 | ipmi_ibm_oem_bmc_factory_reset, SYSTEM_INTERFACE); |
Adriana Kobylak | 81e2310 | 2018-08-09 14:44:19 -0500 | [diff] [blame] | 455 | |
Vishwanatha Subbanna | 0765506 | 2017-07-14 20:31:57 +0530 | [diff] [blame] | 456 | // Create new object on the bus |
| 457 | auto objPath = std::string{CONTROL_HOST_OBJ_MGR} + '/' + HOST_NAME + '0'; |
| 458 | |
| 459 | // Add sdbusplus ObjectManager. |
| 460 | auto& sdbusPlusHandler = ipmid_get_sdbus_plus_handler(); |
Patrick Williams | a0a221f | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 461 | objManager = std::make_unique<sdbusplus::server::manager_t>( |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 462 | *sdbusPlusHandler, CONTROL_HOST_OBJ_MGR); |
Vishwanatha Subbanna | 0765506 | 2017-07-14 20:31:57 +0530 | [diff] [blame] | 463 | |
| 464 | opHost = std::make_unique<open_power::host::command::Host>( |
Patrick Venture | 02261c0 | 2018-10-31 15:16:23 -0700 | [diff] [blame] | 465 | *sdbusPlusHandler, objPath.c_str()); |
Vishwanatha Subbanna | 0765506 | 2017-07-14 20:31:57 +0530 | [diff] [blame] | 466 | |
| 467 | // Service for this is provided by phosphor layer systemcmdintf |
| 468 | // and this will be as part of that. |
Tom | cbfd6ec | 2016-09-14 17:45:55 +0530 | [diff] [blame] | 469 | return; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 470 | } |