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