Merge pull request #88 from williamli80/issue77

Returning correct value from handle_ipmid_command in ipmid.C
diff --git a/chassishandler.C b/chassishandler.C
index fca3c79..d5b3404 100644
--- a/chassishandler.C
+++ b/chassishandler.C
@@ -4,6 +4,15 @@
 #include <string.h>
 #include <stdint.h>
 
+
+//Defines
+#define SET_PARM_VERSION 1
+#define SET_PARM_BOOT_FLAGS_PERMANENT 0x40 //boot flags data1 7th bit on
+#define SET_PARM_BOOT_FLAGS_VALID_ONE_TIME   0x80 //boot flags data1 8th bit on
+#define SET_PARM_BOOT_FLAGS_VALID_PERMANENT  0xC0 //boot flags data1 7 & 8 bit on 
+
+
+
 // OpenBMC Chassis Manager dbus framework
 const char  *chassis_bus_name      =  "org.openbmc.control.Chassis";
 const char  *chassis_object_name   =  "/org/openbmc/control/chassis0";
@@ -92,7 +101,7 @@
     return r;
 }
 
-int dbus_get_property(char **buf)
+int dbus_get_property(const char *name, char **buf)
 {
     sd_bus_error error = SD_BUS_ERROR_NULL;
     sd_bus_message *m = NULL;
@@ -128,7 +137,7 @@
                            &m,                                         /* return message on success */
                            "ss",                                       /* input signature */
                            host_intf_name,                             /* first argument */
-                           "boot_flags");                              /* second argument */
+                           name);                                      /* second argument */
 
     if (r < 0) {
         fprintf(stderr, "Failed to issue method call: %s\n", error.message);
@@ -161,7 +170,7 @@
     return r;
 }
 
-int dbus_set_property(const char *buf)
+int dbus_set_property(const char * name, const char *value)
 {
     sd_bus_error error = SD_BUS_ERROR_NULL;
     sd_bus_message *m = NULL;
@@ -196,16 +205,16 @@
                            &m,                                         /* return message on success */
                            "ssv",                                      /* input signature */
                            host_intf_name,                             /* first argument */
-                           "boot_flags",                               /* second argument */
+                           name,                                       /* second argument */
                            "s",                                        /* third argument */
-                           buf);                                       /* fourth argument */
+                           value);                                     /* fourth argument */
 
     if (r < 0) {
         fprintf(stderr, "Failed to issue method call: %s\n", error.message);
         goto finish;
     }
 
-    printf("IPMID boot option property set: {%s}.\n", buf);
+    printf("IPMID boot option property set: {%s}.\n", value);
 
 finish:
     sd_bus_error_free(&error);
@@ -373,9 +382,6 @@
     return s->dbusname;
 }
 
-#define SET_PARM_VERSION 1
-#define SET_PARM_BOOT_FLAGS_VALID   0x80
-
 ipmi_ret_t ipmi_chassis_get_sys_boot_options(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)
@@ -391,7 +397,7 @@
     memset(resp,0,sizeof(*resp));
     resp->version   = SET_PARM_VERSION;
     resp->parm      = 5;
-    resp->data[0]   = SET_PARM_BOOT_FLAGS_VALID;
+    resp->data[0]   = SET_PARM_BOOT_FLAGS_VALID_ONE_TIME;
 
     *data_len = sizeof(*resp);
 
@@ -401,10 +407,11 @@
      */
     if (reqptr->parameter == 5) {
 
-        int r = dbus_get_property(&p);
+        /* Get the boot device */
+        int r = dbus_get_property("boot_flags",&p);
 
         if (r < 0) {
-            fprintf(stderr, "Dbus get property failed for get_sys_boot_options.\n");
+            fprintf(stderr, "Dbus get property(boot_flags) failed for get_sys_boot_options.\n");
             rc = IPMI_CC_UNSPECIFIED_ERROR;
 
         } else {
@@ -412,8 +419,33 @@
             s = get_ipmi_boot_option(p);
             resp->data[1] = (s << 2);
             rc = IPMI_CC_OK;
+
         }
 
+        if (p)
+        {
+          free(p);
+          p = NULL;
+        }
+
+        /* Get the boot policy */
+        r = dbus_get_property("boot_policy",&p);
+
+        if (r < 0) {
+            fprintf(stderr, "Dbus get property(boot_policy) failed for get_sys_boot_options.\n");
+            rc = IPMI_CC_UNSPECIFIED_ERROR;
+
+        } else {
+
+            printf("BootPolicy is[%s]", p); 
+            resp->data[0] = (strncmp(p,"ONETIME",strlen("ONETIME"))==0) ? 
+                            SET_PARM_BOOT_FLAGS_VALID_ONE_TIME:
+                            SET_PARM_BOOT_FLAGS_VALID_PERMANENT;
+            rc = IPMI_CC_OK;
+
+        }
+
+
     } else {
         fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
     }
@@ -455,13 +487,25 @@
 
         } else {
 
-            int r = dbus_set_property(s);
+            int r = dbus_set_property("boot_flags",s);
 
             if (r < 0) {
-                fprintf(stderr, "Dbus set property failed for set_sys_boot_options.\n");
+                fprintf(stderr, "Dbus set property(boot_flags) failed for set_sys_boot_options.\n");
                 rc = IPMI_CC_UNSPECIFIED_ERROR;
             }
         }
+      
+        /* setting the boot policy */
+        s = (char *)(((reqptr->data[0] & SET_PARM_BOOT_FLAGS_PERMANENT) == 
+                       SET_PARM_BOOT_FLAGS_PERMANENT) ?"PERMANENT":"ONETIME");
+
+        printf ( "\nBoot Policy is %s",s); 
+        int r = dbus_set_property("boot_policy",s);
+
+        if (r < 0) {
+            fprintf(stderr, "Dbus set property(boot_policy) failed for set_sys_boot_options.\n");
+            rc = IPMI_CC_UNSPECIFIED_ERROR;
+        }
 
     } else {
         fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
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 9009cd6..7354958 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
@@ -345,8 +350,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--)
         {
@@ -355,6 +360,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",
@@ -363,6 +369,7 @@
             // Wipe the memory allocated for this particular entry.
             free(handler_list[num_handlers]);
         }
+
         // Done with all registration.
         free(handler_list);
     }
@@ -569,6 +576,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));
@@ -606,6 +618,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/storageaddsel.C b/storageaddsel.C
index cb90aab..3343772 100644
--- a/storageaddsel.C
+++ b/storageaddsel.C
@@ -80,16 +80,39 @@
 
 	FILE *fp;
 	size_t size = 0;
+	int r;
 
 	if ((fp = fopen(fn, "rb")) != NULL) {
 
-		fseek(fp, 0, SEEK_END);
+		r = fseek(fp, 0, SEEK_END);
+		if (r) {
+			fprintf(stderr,"Fseek failed\n");
+			goto fclose_fp;
+		}
+
 		size = ftell(fp);
-		fseek(fp, 0, SEEK_SET);
+		if (size == -1L) {
+			fprintf(stderr,"Ftell failed for %s\n", strerror(errno));
+			size = 0;
+			goto fclose_fp;
+		}
+
+		r = fseek(fp, 0, SEEK_SET);
+		if (r) {
+			fprintf(stderr,"Fseek failed\n");
+			size = 0;
+			goto fclose_fp;
+		}
 
 		*buffer = new uint8_t [size];
 
-		fread(*buffer, 1, size, fp);
+		r = fread(*buffer, 1, size, fp);
+		if ( r != size) {
+			size = 0;
+			fprintf(stderr,"Fread failed\n");
+		}
+
+fclose_fp:
 		fclose(fp);
 	}
 
@@ -205,6 +228,7 @@
 	uint8_t *buffer = NULL;
 	const char *path = "/tmp/esel";
 	size_t sz;
+	int r;
 
 	sz = getfilestream(path, &buffer);
 	if (sz == 0) {
@@ -216,7 +240,10 @@
 	create_esel_association(buffer, &assoc);
 	create_esel_description(buffer, sev, &desc);
 
-	send_esel_to_dbus(desc, sev, assoc, buffer, sz);
+	r = send_esel_to_dbus(desc, sev, assoc, buffer, sz);
+	if (r < 0) {
+		fprintf(stderr, "Failed to send esel to dbus\n");
+	}
 
 	free(assoc);
 	free(desc);
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);