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