blob: 22452d65c22ab6651b271aac7130ae351600821f [file] [log] [blame]
Chris Austen4d98c1e2015-10-13 14:33:50 -05001#include "oemhandler.h"
2#include <host-ipmid/ipmid-api.h>
Adriana Kobylak187bfce2016-03-04 11:55:43 -06003#include <fstream>
Chris Austen4d98c1e2015-10-13 14:33:50 -05004#include <stdio.h>
5#include <string.h>
Adriana Kobylak187bfce2016-03-04 11:55:43 -06006#include <systemd/sd-bus.h>
Chris Austen4d98c1e2015-10-13 14:33:50 -05007
8void register_netfn_oem_partial_esel() __attribute__((constructor));
9
Chris Austenfcafccf2016-02-19 23:15:57 -060010const char *g_esel_path = "/tmp/esel";
Chris Austen29a8e0f2015-10-30 11:44:38 -050011uint16_t g_record_id = 0x0001;
Chris Austen4d98c1e2015-10-13 14:33:50 -050012
13
14///////////////////////////////////////////////////////////////////////////////
Patrick Williams24fa5a92015-10-30 14:53:57 -050015// 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 Austen4d98c1e2015-10-13 14:33:50 -050019// Record ID should be used for all partial eSEL adds.
20//
Chris Austenba54afb2016-02-19 23:18:42 -060021// This function creates a /tmp/esel file to store the
Chris Austen4d98c1e2015-10-13 14:33:50 -050022// incoming partial esel. It is the role of some other
Patrick Williams24fa5a92015-10-30 14:53:57 -050023// function to commit the error log in to long term
24// storage. Likely via the ipmi add_sel command.
Chris Austen4d98c1e2015-10-13 14:33:50 -050025///////////////////////////////////////////////////////////////////////////////
Patrick Williams24fa5a92015-10-30 14:53:57 -050026ipmi_ret_t ipmi_ibm_oem_partial_esel(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
Chris Austen17160ce2015-10-30 11:44:38 -050027 ipmi_request_t request, ipmi_response_t response,
28 ipmi_data_len_t data_len, ipmi_context_t context)
Chris Austen4d98c1e2015-10-13 14:33:50 -050029{
Chris Austenba54afb2016-02-19 23:18:42 -060030 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 Austen4d98c1e2015-10-13 14:33:50 -050038
Chris Austenba54afb2016-02-19 23:18:42 -060039 // 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 Austen4d98c1e2015-10-13 14:33:50 -050045
Chris Austenba54afb2016-02-19 23:18:42 -060046 rlen = (*data_len) - (uint8_t) (sizeof(esel_request_t));
Chris Austen4d98c1e2015-10-13 14:33:50 -050047
Chris Austenba54afb2016-02-19 23:18:42 -060048 printf("IPMI PARTIAL ESEL for %s Offset = %d Length = %d\n",
49 g_esel_path, *offset, rlen);
Chris Austen4d98c1e2015-10-13 14:33:50 -050050
Chris Austenba54afb2016-02-19 23:18:42 -060051 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 Austen4d98c1e2015-10-13 14:33:50 -050055
Chris Austenba54afb2016-02-19 23:18:42 -060056 *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 Austen4d98c1e2015-10-13 14:33:50 -050063
Chris Austenba54afb2016-02-19 23:18:42 -060064 // 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 Austen4d98c1e2015-10-13 14:33:50 -050071
Chris Austenba54afb2016-02-19 23:18:42 -060072 return rc;
Chris Austen4d98c1e2015-10-13 14:33:50 -050073}
74
Adriana Kobylak187bfce2016-03-04 11:55:43 -060075// Prepare for FW Update.
76// Execute needed commands to prepare the system for a fw update from the host.
77ipmi_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 Austen4d98c1e2015-10-13 14:33:50 -0500121
122void register_netfn_oem_partial_esel()
123{
Chris Austenba54afb2016-02-19 23:18:42 -0600124 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 Kobylak187bfce2016-03-04 11:55:43 -0600126
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 Austenba54afb2016-02-19 23:18:42 -0600130 return;
Chris Austen4d98c1e2015-10-13 14:33:50 -0500131}