Matthew Barth | 70dbc58 | 2016-09-20 10:16:52 -0500 | [diff] [blame] | 1 | #include "oemhandler.hpp" |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 2 | #include <host-ipmid/ipmid-api.h> |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 3 | #include <fstream> |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <string.h> |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 6 | #include <systemd/sd-bus.h> |
Nan Li | 8a0807a | 2016-05-24 16:59:24 +0800 | [diff] [blame] | 7 | #include <endian.h> |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 8 | |
| 9 | void register_netfn_oem_partial_esel() __attribute__((constructor)); |
| 10 | |
Chris Austen | fcafccf | 2016-02-19 23:15:57 -0600 | [diff] [blame] | 11 | const char *g_esel_path = "/tmp/esel"; |
Chris Austen | 29a8e0f | 2015-10-30 11:44:38 -0500 | [diff] [blame] | 12 | uint16_t g_record_id = 0x0001; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 13 | |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 14 | /////////////////////////////////////////////////////////////////////////////// |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 15 | // For the First partial add eSEL the SEL Record ID and offset |
| 16 | // value should be 0x0000. The extended data needs to be in |
| 17 | // the form of an IPMI SEL Event Record, with Event sensor type |
| 18 | // of 0xDF and Event Message format of 0x04. The returned |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 19 | // Record ID should be used for all partial eSEL adds. |
| 20 | // |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 21 | // This function creates a /tmp/esel file to store the |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 22 | // incoming partial esel. It is the role of some other |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 23 | // function to commit the error log in to long term |
| 24 | // storage. Likely via the ipmi add_sel command. |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 25 | /////////////////////////////////////////////////////////////////////////////// |
Patrick Williams | 24fa5a9 | 2015-10-30 14:53:57 -0500 | [diff] [blame] | 26 | ipmi_ret_t ipmi_ibm_oem_partial_esel(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
Chris Austen | 17160ce | 2015-10-30 11:44:38 -0500 | [diff] [blame] | 27 | ipmi_request_t request, ipmi_response_t response, |
| 28 | ipmi_data_len_t data_len, ipmi_context_t context) |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 29 | { |
Nan Li | 8a0807a | 2016-05-24 16:59:24 +0800 | [diff] [blame] | 30 | uint8_t *reqptr = (uint8_t *) request; |
| 31 | esel_request_t esel_req; |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 32 | FILE *fp; |
Nan Li | b6c4c56 | 2016-03-30 17:06:13 +0800 | [diff] [blame] | 33 | int r = 0; |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 34 | uint8_t rlen; |
| 35 | ipmi_ret_t rc = IPMI_CC_OK; |
| 36 | const char *pio; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 37 | |
Nan Li | 8a0807a | 2016-05-24 16:59:24 +0800 | [diff] [blame] | 38 | esel_req.resid = le16toh((((uint16_t) reqptr[1]) << 8) + reqptr[0]); |
| 39 | esel_req.selrecord = le16toh((((uint16_t) reqptr[3]) << 8) + reqptr[2]); |
| 40 | esel_req.offset = le16toh((((uint16_t) reqptr[5]) << 8) + reqptr[4]); |
| 41 | esel_req.progress = reqptr[6]; |
Nan Li | b6c4c56 | 2016-03-30 17:06:13 +0800 | [diff] [blame] | 42 | |
Nan Li | 8a0807a | 2016-05-24 16:59:24 +0800 | [diff] [blame] | 43 | uint16_t used_res_id = get_sel_reserve_id(); |
Nan Li | b6c4c56 | 2016-03-30 17:06:13 +0800 | [diff] [blame] | 44 | |
| 45 | // According to IPMI spec, Reservation ID must be checked. |
Nan Li | 8a0807a | 2016-05-24 16:59:24 +0800 | [diff] [blame] | 46 | if ( used_res_id != esel_req.resid ) { |
Nan Li | b6c4c56 | 2016-03-30 17:06:13 +0800 | [diff] [blame] | 47 | // 0xc5 means Reservation Cancelled or Invalid Reservation ID. |
| 48 | printf("Used Reservation ID = %d\n", used_res_id); |
| 49 | rc = IPMI_CC_INVALID_RESERVATION_ID; |
| 50 | |
| 51 | // clean g_esel_path. |
| 52 | r = remove(g_esel_path); |
| 53 | if(r < 0) |
| 54 | fprintf(stderr, "Error deleting %s\n", g_esel_path); |
| 55 | |
| 56 | return rc; |
| 57 | } |
| 58 | |
| 59 | // OpenPOWER Host Interface spec says if RecordID and Offset are |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 60 | // 0 then then this is a new request |
Nan Li | 8a0807a | 2016-05-24 16:59:24 +0800 | [diff] [blame] | 61 | if (!esel_req.selrecord && !esel_req.offset) |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 62 | pio = "wb"; |
| 63 | else |
| 64 | pio = "rb+"; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 65 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 66 | rlen = (*data_len) - (uint8_t) (sizeof(esel_request_t)); |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 67 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 68 | printf("IPMI PARTIAL ESEL for %s Offset = %d Length = %d\n", |
Nan Li | 8a0807a | 2016-05-24 16:59:24 +0800 | [diff] [blame] | 69 | g_esel_path, esel_req.offset, rlen); |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 70 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 71 | if ((fp = fopen(g_esel_path, pio)) != NULL) { |
Nan Li | 8a0807a | 2016-05-24 16:59:24 +0800 | [diff] [blame] | 72 | fseek(fp, esel_req.offset, SEEK_SET); |
| 73 | fwrite(reqptr+(uint8_t) (sizeof(esel_request_t)), rlen, 1, fp); |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 74 | fclose(fp); |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 75 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 76 | *data_len = sizeof(g_record_id); |
| 77 | memcpy(response, &g_record_id, *data_len); |
| 78 | } else { |
| 79 | fprintf(stderr, "Error trying to perform %s for esel%s\n",pio, g_esel_path); |
| 80 | rc = IPMI_CC_INVALID; |
| 81 | *data_len = 0; |
| 82 | } |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 83 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 84 | // The first bit prepresents that this is the last partial packet |
| 85 | // coming down. If that is the case advance the record id so we |
| 86 | // don't overlap logs. This allows anyone to establish a log |
| 87 | // directory system. |
Nan Li | 8a0807a | 2016-05-24 16:59:24 +0800 | [diff] [blame] | 88 | if (esel_req.progress & 1 ) { |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 89 | g_record_id++; |
| 90 | } |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 91 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 92 | return rc; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 93 | } |
| 94 | |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 95 | // Prepare for FW Update. |
| 96 | // Execute needed commands to prepare the system for a fw update from the host. |
| 97 | ipmi_ret_t ipmi_ibm_oem_prep_fw_update(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 98 | ipmi_request_t request, ipmi_response_t response, |
| 99 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 100 | { |
| 101 | ipmi_ret_t ipmi_rc = IPMI_CC_OK; |
| 102 | *data_len = 0; |
| 103 | |
| 104 | int rc = 0; |
| 105 | std::ofstream rwfs_file; |
| 106 | const char *busname = "org.openbmc.control.Bmc"; |
| 107 | const char *objname = "/org/openbmc/control/bmc0"; |
| 108 | const char *iface = "org.openbmc.control.Bmc"; |
| 109 | sd_bus *bus = ipmid_get_sd_bus_connection(); |
| 110 | sd_bus_message *reply = NULL; |
| 111 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 112 | int r = 0; |
| 113 | |
| 114 | // Set one time flag |
| 115 | rc = system("fw_setenv openbmconce copy-files-to-ram copy-base-filesystem-to-ram"); |
| 116 | rc = WEXITSTATUS(rc); |
| 117 | if (rc != 0) { |
| 118 | fprintf(stderr, "fw_setenv openbmconce failed with rc=%d\n", rc); |
| 119 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 120 | } |
| 121 | |
| 122 | // Touch the image-rwfs file to perform an empty update to force the save |
| 123 | // in case we're already in ram and the flash is the same causing the ram files |
| 124 | // to not be copied back to flash |
| 125 | rwfs_file.open("/run/initramfs/image-rwfs", std::ofstream::out | std::ofstream::app); |
| 126 | rwfs_file.close(); |
| 127 | |
| 128 | // Reboot the BMC for settings to take effect |
| 129 | r = sd_bus_call_method(bus, busname, objname, iface, |
| 130 | "warmReset", &error, &reply, NULL); |
| 131 | if (r < 0) { |
| 132 | fprintf(stderr, "Failed to reset BMC: %s\n", strerror(-r)); |
| 133 | return -1; |
| 134 | } |
| 135 | printf("Warning: BMC is going down for reboot!\n"); |
| 136 | sd_bus_error_free(&error); |
| 137 | reply = sd_bus_message_unref(reply); |
| 138 | |
| 139 | return ipmi_rc; |
| 140 | } |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 141 | |
| 142 | void register_netfn_oem_partial_esel() |
| 143 | { |
Tom | cbfd6ec | 2016-09-14 17:45:55 +0530 | [diff] [blame^] | 144 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_OEM, IPMI_CMD_PESEL); |
| 145 | ipmi_register_callback(NETFUN_OEM, IPMI_CMD_PESEL, NULL, ipmi_ibm_oem_partial_esel, |
| 146 | SYSTEM_INTERFACE); |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 147 | |
| 148 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_OEM, IPMI_CMD_PREP_FW_UPDATE); |
Tom | cbfd6ec | 2016-09-14 17:45:55 +0530 | [diff] [blame^] | 149 | ipmi_register_callback(NETFUN_OEM, IPMI_CMD_PREP_FW_UPDATE, NULL, ipmi_ibm_oem_prep_fw_update, |
| 150 | SYSTEM_INTERFACE); |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 151 | |
Tom | cbfd6ec | 2016-09-14 17:45:55 +0530 | [diff] [blame^] | 152 | return; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 153 | } |