Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 1 | #include "oemhandler.h" |
| 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> |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 7 | |
| 8 | void register_netfn_oem_partial_esel() __attribute__((constructor)); |
| 9 | |
Chris Austen | fcafccf | 2016-02-19 23:15:57 -0600 | [diff] [blame] | 10 | const char *g_esel_path = "/tmp/esel"; |
Chris Austen | 29a8e0f | 2015-10-30 11:44:38 -0500 | [diff] [blame] | 11 | uint16_t g_record_id = 0x0001; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 12 | |
| 13 | |
| 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 | { |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 30 | esel_request_t *reqptr = (esel_request_t*) request; |
| 31 | FILE *fp; |
| 32 | // TODO: Issue 5: This is not endian-safe. |
| 33 | short *recid = (short*) &reqptr->selrecordls; |
| 34 | short *offset = (short*) &reqptr->offsetls; |
| 35 | uint8_t rlen; |
| 36 | ipmi_ret_t rc = IPMI_CC_OK; |
| 37 | const char *pio; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 38 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 39 | // OpenPOWER Host Interface spec says if RecordID and Offset are |
| 40 | // 0 then then this is a new request |
| 41 | if (!*recid && !*offset) |
| 42 | pio = "wb"; |
| 43 | else |
| 44 | pio = "rb+"; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 45 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 46 | rlen = (*data_len) - (uint8_t) (sizeof(esel_request_t)); |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 47 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 48 | printf("IPMI PARTIAL ESEL for %s Offset = %d Length = %d\n", |
| 49 | g_esel_path, *offset, rlen); |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 50 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 51 | if ((fp = fopen(g_esel_path, pio)) != NULL) { |
| 52 | fseek(fp, *offset, SEEK_SET); |
| 53 | fwrite(reqptr+1,rlen,1,fp); |
| 54 | fclose(fp); |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 55 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 56 | *data_len = sizeof(g_record_id); |
| 57 | memcpy(response, &g_record_id, *data_len); |
| 58 | } else { |
| 59 | fprintf(stderr, "Error trying to perform %s for esel%s\n",pio, g_esel_path); |
| 60 | rc = IPMI_CC_INVALID; |
| 61 | *data_len = 0; |
| 62 | } |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 63 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 64 | // The first bit prepresents that this is the last partial packet |
| 65 | // coming down. If that is the case advance the record id so we |
| 66 | // don't overlap logs. This allows anyone to establish a log |
| 67 | // directory system. |
| 68 | if (reqptr->progress & 1 ) { |
| 69 | g_record_id++; |
| 70 | } |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 71 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 72 | return rc; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 75 | // Prepare for FW Update. |
| 76 | // Execute needed commands to prepare the system for a fw update from the host. |
| 77 | ipmi_ret_t ipmi_ibm_oem_prep_fw_update(ipmi_netfn_t netfn, ipmi_cmd_t cmd, |
| 78 | ipmi_request_t request, ipmi_response_t response, |
| 79 | ipmi_data_len_t data_len, ipmi_context_t context) |
| 80 | { |
| 81 | ipmi_ret_t ipmi_rc = IPMI_CC_OK; |
| 82 | *data_len = 0; |
| 83 | |
| 84 | int rc = 0; |
| 85 | std::ofstream rwfs_file; |
| 86 | const char *busname = "org.openbmc.control.Bmc"; |
| 87 | const char *objname = "/org/openbmc/control/bmc0"; |
| 88 | const char *iface = "org.openbmc.control.Bmc"; |
| 89 | sd_bus *bus = ipmid_get_sd_bus_connection(); |
| 90 | sd_bus_message *reply = NULL; |
| 91 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 92 | int r = 0; |
| 93 | |
| 94 | // Set one time flag |
| 95 | rc = system("fw_setenv openbmconce copy-files-to-ram copy-base-filesystem-to-ram"); |
| 96 | rc = WEXITSTATUS(rc); |
| 97 | if (rc != 0) { |
| 98 | fprintf(stderr, "fw_setenv openbmconce failed with rc=%d\n", rc); |
| 99 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 100 | } |
| 101 | |
| 102 | // Touch the image-rwfs file to perform an empty update to force the save |
| 103 | // in case we're already in ram and the flash is the same causing the ram files |
| 104 | // to not be copied back to flash |
| 105 | rwfs_file.open("/run/initramfs/image-rwfs", std::ofstream::out | std::ofstream::app); |
| 106 | rwfs_file.close(); |
| 107 | |
| 108 | // Reboot the BMC for settings to take effect |
| 109 | r = sd_bus_call_method(bus, busname, objname, iface, |
| 110 | "warmReset", &error, &reply, NULL); |
| 111 | if (r < 0) { |
| 112 | fprintf(stderr, "Failed to reset BMC: %s\n", strerror(-r)); |
| 113 | return -1; |
| 114 | } |
| 115 | printf("Warning: BMC is going down for reboot!\n"); |
| 116 | sd_bus_error_free(&error); |
| 117 | reply = sd_bus_message_unref(reply); |
| 118 | |
| 119 | return ipmi_rc; |
| 120 | } |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 121 | |
| 122 | void register_netfn_oem_partial_esel() |
| 123 | { |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 124 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_OEM, IPMI_CMD_PESEL); |
| 125 | ipmi_register_callback(NETFUN_OEM, IPMI_CMD_PESEL, NULL, ipmi_ibm_oem_partial_esel); |
Adriana Kobylak | 187bfce | 2016-03-04 11:55:43 -0600 | [diff] [blame] | 126 | |
| 127 | printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n", NETFUN_OEM, IPMI_CMD_PREP_FW_UPDATE); |
| 128 | ipmi_register_callback(NETFUN_OEM, IPMI_CMD_PREP_FW_UPDATE, NULL, ipmi_ibm_oem_prep_fw_update); |
| 129 | |
Chris Austen | ba54afb | 2016-02-19 23:18:42 -0600 | [diff] [blame] | 130 | return; |
Chris Austen | 4d98c1e | 2015-10-13 14:33:50 -0500 | [diff] [blame] | 131 | } |