/*
 * oem_supermicro.c
 * Handle SuperMicro OEM command functions 
 *
 * Change history:
 *  12/06/2010 ARCress - included in source tree
 *
 *---------------------------------------------------------------------
 */
/*M*
Copyright (c) 2010 Kontron America, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

  a.. Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer. 
  b.. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution. 
  c.. Neither the name of Kontron nor the names of its contributors 
      may be used to endorse or promote products derived from this software 
      without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *M*/
#ifdef WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "ipmicmd.h"
#include "ievents.h"

#define SUPER_NETFN_OEM           0x30
#define SUPER_CMD_BMCSTATUS       0x70
#define SUPER_CMD_RESET_INTRUSION 0x03

#define SUPER_NETFN_OEMFW     0x3C  /*for SuperMicro/Peppercon*/
#define SUPER_CMD_OEMFWINFO   0x20

static char fdbg = 0;

int oem_supermicro_get_bmc_status(uchar *sts)
{
   int rv;
   int rlen;
   uchar rdata[16];
   uchar idata[16];
   uchar cc;

   if (sts == NULL) return(LAN_ERR_INVPARAM);
   idata[0] = 0xF0;  /* subfunction */
   idata[1] = 0x02;  /* action: get status */
   rlen = sizeof(rdata);
   rv = ipmi_cmdraw(SUPER_CMD_BMCSTATUS, SUPER_NETFN_OEM, 
		    BMC_SA, PUBLIC_BUS, BMC_LUN,
		    idata, 2, rdata, &rlen, &cc, fdbg);
   if ((rv == 0) && (cc != 0)) rv = cc;
   if (rv == 0) { *sts = rdata[0]; }
   return(rv);
}

int oem_supermicro_set_bmc_status(uchar sts)
{
   int rv;
   int rlen;
   uchar rdata[16];
   uchar idata[16];
   uchar cc;

   if (sts > 1) sts = 1;  /* actions: 0=disable, 1=enable, 2=status*/ 
   idata[0] = 0xF0;  /* subfunction */
   idata[1] = sts; 
   rlen = sizeof(rdata);
   rv = ipmi_cmdraw(SUPER_CMD_BMCSTATUS, SUPER_NETFN_OEM, 
		    BMC_SA, PUBLIC_BUS, BMC_LUN,
		    idata, 2, rdata, &rlen, &cc, fdbg);
   if ((rv == 0) && (cc != 0)) rv = cc;
   return(rv);
}

int oem_supermicro_get_health(char *pstr, int sz) 
{
   int rv;
   uchar bsts;
   char *str;

   rv = oem_supermicro_get_bmc_status(&bsts);
   if (rv == 0) {
	if (bsts == 0x01) str = "BMC status = enabled";
	else str = "BMC status = disabled";
	strncpy(pstr, str, sz);
   }
   return(rv);
}

/* 
 * oem_supermicro_get_firmware_info
 *
 * From post by ipmitool developer.
 * http://sourceforge.net/mailarchive/message.php?msg_name=49ABCCC3.4040004%40cern.ch
 *
 * Request
 * 0x3C - OEM network function
 * 0x20 - OEM cmd  (SUPER_CMD_OEMFWINFO)
 *
 * Response data:
 * 4 bytes - firmware major version (LSB first)
 * 4 bytes - firmware minor version (LSB first)
 * 4 bytes - firmware sub version (LSB first)
 * 4 bytes - firmware build number (LSB first)
 * 1 byte - hardware ID
 * ? bytes - firmware tag, null terminated string
 */
int oem_supermicro_get_firmware_info(uchar *info)
{
   int rv;
   int rlen;
   uchar rdata[32];
   uchar idata[16];
   uchar cc;

   if (info == NULL) return(LAN_ERR_INVPARAM);
   rlen = sizeof(rdata);
   rv = ipmi_cmdraw(SUPER_CMD_OEMFWINFO, SUPER_NETFN_OEM, 
		    BMC_SA, PUBLIC_BUS, BMC_LUN,
		    idata, 0, rdata, &rlen, &cc, fdbg);
   if ((rv == 0) && (cc != 0)) rv = cc;
   if (rv == 0) { memcpy(info,rdata,rlen); }
   return(rv);
}

int oem_supermicro_get_firmware_str(char *pstr, int sz)
{
   int rv;
   uchar info[32];
   uint32 fwmaj;
   uint32 fwmin;
   uint32 fwsub;
   uint32 fwbld;
   uchar hwid;
   rv = oem_supermicro_get_firmware_info(info);
   if (rv == 0) {
        fwmaj = info[0] + (info[1] << 8) + (info[2] << 16) + (info[3] << 24);
        fwmin = info[4] + (info[5] << 8) + (info[6] << 16) + (info[7] << 24);
        fwsub = info[8] + (info[9] << 8) + (info[10] << 16) + (info[11] << 24);
        fwbld = info[12] +(info[13] << 8) + (info[14] << 16) + (info[15] << 24);
	hwid = info[16];
        /*info[17] = fw tag string */
        snprintf(pstr,sz,"Firmware %d.%d.%d.%d HW %d %s\n",fwmaj,fwmin,fwsub,
			fwbld,hwid,&info[17]);
   }
   return(rv);
}


int oem_supermicro_reset_intrusion(uchar *state)
{
   int rv;
   int rlen;
   uchar rdata[32];
   uchar idata[16];
   uchar cc;
   if (state == NULL) return(LAN_ERR_INVPARAM);
   rlen = sizeof(rdata);
   rv = ipmi_cmdraw(SUPER_CMD_RESET_INTRUSION, SUPER_NETFN_OEM, 
		    BMC_SA, PUBLIC_BUS, BMC_LUN,
		    idata, 0, rdata, &rlen, &cc, fdbg);
   if ((rv == 0) && (cc != 0)) rv = cc;
   if (rv == 0) { memcpy(state,rdata,rlen); }
   return(rv);
}

/*
 * decode_sensor_supermicro
 * inputs:
 *    sdr     = the SDR buffer
 *    reading = the 3 or 4 bytes of data from GetSensorReading
 *    pstring = points to the output string buffer
 *    slen    = size of the output buffer
 * outputs:
 *    rv     = 0 if this sensor was successfully interpreted here,
 *             non-zero otherwise, to use default interpretations.
 *    pstring = contains the sensor reading interpretation string (if rv==0)
 */
int decode_sensor_supermicro(uchar *sdr,uchar *reading,char *pstring, int slen)
{
   int rv = -1;
   uchar stype;
   char *pstr = NULL;

   if (sdr == NULL || reading == NULL) return(rv);
   if (pstring == NULL || slen == 0) return(rv);
   if (sdr[3] != 0x02) return(rv);  /*skip if not compact sensor*/
   stype = sdr[12];
   switch(stype) {
        case 0xC0:      /* CPU Temp Sensor, EvTyp=0x70 */
	   //if (dbg) printf("supermicro %x sensor reading %x\n",stype,reading);
           rv = 0;
	   switch(reading[2]) {
           case 0x0000: pstr = "Low";    break;
           case 0x0001: pstr = "Medium"; break;
           case 0x0002: pstr = "High";   break;
           case 0x0004: pstr = "Overheat";   break;
           case 0x0007: pstr = "Not Installed";   break;
	   default:  rv = -1;  break;
	   }
           break;
        default:
           break;
   }
   if (rv == 0) strncpy(pstring, pstr, slen);
   return(rv);
}

/*
 * decode_sel_supermicro
 * inputs:
 *    evt    = the 16-byte IPMI SEL event
 *    outbuf = points to the output string buffer
 *    outsz  = size of the output buffer
 * outputs:
 *    rv     = 0 if this event was successfully interpreted here,
 *             non-zero otherwise, to use default interpretations.
 *    outbuf = will contain the interpreted event text string (if rv==0)
 */
int decode_sel_supermicro(uchar *evt, char *outbuf, int outsz, char fdesc,
                        char fdebug)
{
   int rv = -1;
   ushort id;
   uchar rectype;
   ulong timestamp;
   uchar sdr[MAX_BUFFER_SIZE];
   char sensdesc[36];
   char timestr[40]; 
   char mybuf[64]; 
   char *type_str = "";
   char *gstr = NULL;
   char *pstr = NULL;
   int sevid;
   ushort genid;
   uchar snum;
   int isdr = 0;
         
   sevid = SEV_INFO;
   id = evt[0] + (evt[1] << 8);
   rectype = evt[2];
   snum = evt[11];
   timestamp = evt[3] + (evt[4] << 8) + (evt[5] << 16) + (evt[6] << 24);
   genid = evt[7] | (evt[8] << 8);
   if (genid == 0x0033) gstr = "Bios";
   else gstr = "BMC ";
   if (rectype == 0x02) {
     sprintf(mybuf,"%02x [%02x %02x %02x]", evt[12],evt[13],evt[14],evt[15]);
     switch(evt[10]) {  /*sensor type*/
        case 0xC0:      /* CPU Temp Sensor */
	   switch(evt[13]) {  /*offset/data1*/
           case 0x02:   /* CPU Temp Sensor Overheat event offset */
		if (evt[12] & 0x80) {  /*EvTyp==0xF0 if deassert*/
		   pstr = "Overheat OK"; sevid = SEV_INFO; 
		} else {  /* EvTyp=0x70 assert */
		   pstr = "Overheat   "; sevid = SEV_MAJ; 
		}
		rv = 0;
		break;
	   default: break;
	   }
	   break;
	default:
	   break;
     }
   }
   if (rv == 0) {
         sensdesc[0] = 0;
         if (fdesc) {
           rv = get_sensor_tag(isdr,genid,snum,sensdesc,sdr,sizeof(sdr));
           if (fdebug) printf("get_sensor_tag(%x) rv = %d\n",snum,rv);
           if (rv != 0) rv = 0;
         }
         fmt_time(timestamp, timestr, sizeof(timestr));
         snprintf(outbuf,outsz, "%04x %s %s %s %s #%02x %s%s %s\n",
                        id,timestr,get_sev_str(sevid), gstr, type_str,
                        snum, sensdesc, /*sensor number, desc */
                        pstr,mybuf);    /*evt description*/
   }

   return(rv);
}

/* end oem_supermicro.c */
