Merge pull request #83 from ratagupt/146621

Adding Boot Policy
diff --git a/apphandler.C b/apphandler.C
index fc6c811..71dba66 100644
--- a/apphandler.C
+++ b/apphandler.C
@@ -10,6 +10,10 @@
 
 void register_netfn_app_functions() __attribute__((constructor));
 
+#define DEVICE_FW1 2
+#define DEVICE_FW2 3
+#define DEVICE_AUX 11
+
 //---------------------------------------------------------------------
 // Called by Host on seeing a SMS_ATN bit set. Return a hardcoded 
 // value of 0x2 indicating we need Host read some data.
@@ -91,23 +95,105 @@
     return rc;
 }
 
+
+typedef struct
+{
+    char major;
+    char minor;
+    uint16_t d[2];
+} rev_t;
+
+
+/* Currently only supports the vx.x-x-[-x] format Will return -1 if not in  */
+/* the format this routine knows how to parse                               */
+/* version = v0.6-19-gf363f61-dirty                                         */
+/*            ^ ^ ^^          ^                                             */
+/*            | |  |----------|-- additional details                        */
+/*            | |---------------- Minor                                     */
+/*            |------------------ Major                                     */
+/* Additional details : If the option group exists it will force Auxiliary  */
+/* Firmware Revision Information 4th byte to 1 indicating the build was     */
+/* derived with additional edits                                            */
+int convert_version(const char *p, rev_t *rev)
+{
+    char *s, *token;
+    char hexbyte[5];
+    int l;
+    uint16_t commits;
+
+    if (*p != 'v')
+        return -1;
+    p++;
+
+    s = strdup(p);
+    token = strtok(s,".-");
+
+    rev->major = (int8_t) atoi(token);
+
+    token = strtok(NULL, ".-");
+    rev->minor = (int8_t) atoi(token);
+
+    // Capture the number of commits on top of the minor tag.
+    // I'm using BE format like the ipmi spec asked for
+    token = strtok(NULL,".-");
+
+    if (token) {
+        commits = (int16_t) atoi(token);
+        rev->d[0] = (commits>>8) | (commits<<8);
+
+        // commit number we skip
+        token = strtok(NULL,".-");
+
+    } else {
+        rev->d[0] = 0;
+    }
+
+    // Any value of the optional parameter forces it to 1
+    if (token)
+        token = strtok(NULL,".-");
+
+    rev->d[1] = (token != NULL) ? 1 : 0;
+
+    free(s);
+    return 0;
+}
+
 ipmi_ret_t ipmi_app_get_device_id(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
                              ipmi_request_t request, ipmi_response_t response,
                              ipmi_data_len_t data_len, ipmi_context_t context)
 {
     ipmi_ret_t rc = IPMI_CC_OK;
+    const char  *busname = "org.openbmc.Inventory";
+    const char  *objname = "/org/openbmc/inventory/system/chassis/motherboard/bmc";
+    const char  *iface   = "org.openbmc.InventoryItem";
+    char *ver = NULL;
+    int r;
+    rev_t rev = {0};
 
     // TODO:
     // This value is the IANA number assigned to "IBM Platform Firmware
     // Division", which is also used by our service processor.  We may want
     // a different number or at least a different version?
-    uint8_t str[] = {0x00, 0, 1, 1,2, 0xD, 0x41, 0xA7, 0x00, 0x43, 0x40};
+    uint8_t dev_id[] = {0, 0, 0, 0, 2, 0xD, 0x41, 0xA7, 0x00, 0x43, 0x40, 0, 0, 0, 0};
 
     // Data length
-    *data_len = sizeof(str);
+    *data_len = sizeof(dev_id);
+
+    r = sd_bus_get_property_string(bus,busname,objname,iface,"version", NULL, &ver);
+    if ( r < 0 ) {
+        fprintf(stderr, "Failed to obtain version property: %s\n", strerror(-r));
+    } else {
+        r = convert_version(ver, &rev);
+        if( r >= 0 ) {
+            // bit7 identifies state of SDR repository, hence the mask
+            dev_id[DEVICE_FW1] |= 0x7F & rev.major;
+            dev_id[DEVICE_FW2] = rev.minor;
+            memcpy(&dev_id[DEVICE_AUX], rev.d, 4);
+        }
+    }
 
     // Pack the actual response
-    memcpy(response, &str, *data_len);
+    memcpy(response, &dev_id, *data_len);
     return rc;
 }
 
diff --git a/ipmid-api.h b/ipmid-api.h
index e635528..cf3eaab 100644
--- a/ipmid-api.h
+++ b/ipmid-api.h
@@ -58,6 +58,8 @@
 void ipmi_register_callback(ipmi_netfn_t, ipmi_cmd_t,
                                        ipmi_context_t, ipmid_callback_t);
 
+unsigned short get_sel_reserve_id(void);
+
 // These are the command network functions, the response
 // network functions are the function + 1. So to determine
 // the proper network function which issued the command
@@ -91,6 +93,7 @@
     IPMI_CC_OK = 0x00,
     IPMI_DCMI_CC_NO_ACTIVE_POWER_LIMIT = 0x80,
     IPMI_CC_INVALID = 0xC1,
+    IPMI_CC_INVALID_RESERVATION_ID = 0xC5,
     IPMI_CC_PARM_OUT_OF_RANGE = 0xC9,
     IPMI_CC_SENSOR_INVALID = 0xCB,
     IPMI_CC_RESPONSE_ERROR = 0xCE,
diff --git a/ipmid.C b/ipmid.C
index 728ba0b..063ded6 100644
--- a/ipmid.C
+++ b/ipmid.C
@@ -26,8 +26,6 @@
   fprintf(stderr, "    mask : 0xFF - Print all trace\n");
 }
 
-
-
 const char * DBUS_INTF = "org.openbmc.HostIpmi";
 
 const char * FILTER = "type='signal',interface='org.openbmc.HostIpmi',member='ReceivedMessage'";
@@ -39,6 +37,13 @@
 // Global data structure that contains the IPMI command handler's registrations.
 std::map<ipmi_fn_cmd_t, ipmi_fn_context_t> g_ipmid_router_map;
 
+// IPMI Spec, shared Reservation ID.
+unsigned short g_sel_reserve = 0xFFFF;
+
+unsigned short get_sel_reserve_id(void)
+{
+    return g_sel_reserve;
+}
 
 #ifndef HEXDUMP_COLS
 #define HEXDUMP_COLS 16
@@ -341,8 +346,8 @@
         handler_fqdn += "/";
 
         num_handlers = scandir(ipmi_lib_path, &handler_list, handler_select, alphasort);
-	if (num_handlers < 0)
-		return;
+        if (num_handlers < 0)
+            return;
 
         while(num_handlers--)
         {
@@ -351,6 +356,7 @@
             printf("Registering handler:[%s]\n",handler_fqdn.c_str());
 
             lib_handler = dlopen(handler_fqdn.c_str(), RTLD_NOW);
+
             if(lib_handler == NULL)
             {
                 fprintf(stderr,"ERROR opening [%s]: %s\n",
@@ -359,6 +365,7 @@
             // Wipe the memory allocated for this particular entry.
             free(handler_list[num_handlers]);
         }
+
         // Done with all registration.
         free(handler_list);
     }
@@ -565,6 +572,11 @@
 
     r = find_openbmc_path("SENSOR", number, &a);
 
+    if (r < 0) {
+        fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
+        return 0;
+    }
+
     r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
     if (r < 0) {
         fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
@@ -602,6 +614,11 @@
 
     r = find_openbmc_path("SENSOR", number, &a);
 
+    if (r < 0) {
+        fprintf(stderr, "Failed to find Sensor 0x%02x\n", number);
+        return 0;
+    }
+
     r = sd_bus_message_new_method_call(bus,&m,a.bus,a.path,a.interface,method);
     if (r < 0) {
         fprintf(stderr, "Failed to create a method call: %s", strerror(-r));
diff --git a/sensorhandler.C b/sensorhandler.C
index bb14e7a..39de660 100644
--- a/sensorhandler.C
+++ b/sensorhandler.C
@@ -180,6 +180,11 @@
 
     r = find_openbmc_path("SENSOR", reqptr->sennum, &a);
 
+    if (r < 0) {
+        fprintf(stderr, "Failed to find Sensor 0x%02x\n", reqptr->sennum);
+        return IPMI_CC_SENSOR_INVALID;
+    }
+
     type = find_sensor(reqptr->sennum);
 
     fprintf(stderr, "Bus: %s, Path: %s, Interface: %s\n", a.bus, a.path, a.interface);
diff --git a/storagehandler.C b/storagehandler.C
index 020a0c9..9622ed9 100644
--- a/storagehandler.C
+++ b/storagehandler.C
@@ -13,7 +13,7 @@
 
 
 unsigned int   g_sel_time    = 0xFFFFFFFF;
-unsigned short g_sel_reserve = 0x1;
+extern unsigned short g_sel_reserve;
 
 ipmi_ret_t ipmi_storage_wildcard(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
                               ipmi_request_t request, ipmi_response_t response,
@@ -100,17 +100,19 @@
     return rc;
 }
 
-
-
 ipmi_ret_t ipmi_storage_reserve_sel(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
                               ipmi_request_t request, ipmi_response_t response,
                               ipmi_data_len_t data_len, ipmi_context_t context)
 {
+    unsigned short res_id;
 
     ipmi_ret_t rc = IPMI_CC_OK;
 
-    printf("IPMI Handling RESERVE-SEL 0x%04x\n", g_sel_reserve);
+    // IPMI spec, Reservation ID, the value simply increases against each execution of reserve_sel command.
+    if( ++g_sel_reserve == 0)
+        g_sel_reserve = 1;
 
+    printf("IPMI Handling RESERVE-SEL 0x%04x\n", g_sel_reserve);
 
     *data_len = sizeof(g_sel_reserve);