Incorrect check for changed NVRAM fields fixed.

This fixes b/8949242.
The original version, if the old data is FOO_ and the new data is FOO,
this check will conclude that they are identical and will not write FOO
to NVRAM, therefore I added an extra memcmp to make sure this doesn't
happen.

Change-Id: Iab070c756c33d751ca82594d5f587500b05cc867
diff --git a/libupgrade/hmx_upgrade_nvram.c b/libupgrade/hmx_upgrade_nvram.c
index 9e8bb57..ecd198e 100644
--- a/libupgrade/hmx_upgrade_nvram.c
+++ b/libupgrade/hmx_upgrade_nvram.c
@@ -207,9 +207,9 @@
 /*******************************************************************/
 /************************ static funtions **************************/
 /*******************************************************************/
-/*  *********************************************************************0
-    *  HMX_NVRAM_Read(buffer,offset,length)0
-    *  0
+/*  *********************************************************************
+    *  drv_NVRAM_Read(buffer,offset,length)
+    *
     *  Read data from the NVRAM device0
     *
     *  Input parameters:
@@ -234,7 +234,7 @@
 }
 
 /*  *********************************************************************
-    *  HMX_NVRAM_Write(buffer,offset,length)
+    *  drv_NVRAM_Write(buffer,offset,length)
     *
     *  Write data to the NVRAM device
     *
@@ -340,7 +340,7 @@
 }
 
 /*  *********************************************************************
-    *  HMX_NVRAM_Delete(name)
+    *  drv_NVRAM_Delete(name)
     *
     *  Delete an environment variable
     *
@@ -367,48 +367,65 @@
 }
 
 /*  *********************************************************************
-    *  HMX_NVRAM_GetEnv(name)
+    *  drv_NVRAM_GetEnv(name)
     *
-    *  Retrieve the value of an environment variable
+    *  Retrieve the value of an environment variable starting from an offset.
     *
     *  Input parameters:
+    *      partition - name of the nvram partition
     *      name - name of environment variable to find
+    *      offset - relative offset to where the variable starts
+    *      data - where to retrieve the value of the variable
+    *      readLen - maximum length of the returned data
+    *      pDataLen - the actual size of the data, may be smaller than readLen
     *
     *  Return value:
-    *      value, or NULL if variable is not found
+    *      0 if ok
+    *      else error code
     ********************************************************************* */
 static DRV_Error drv_NVRAM_GetEnv(HMX_NVRAM_PARTITION_E partition,
                                   const unsigned char *name,
                                   const unsigned int offset,
-                                  unsigned char *value, unsigned int readLen)
+                                  unsigned char *data,
+                                  unsigned int readLen,
+                                  unsigned int *pDataLen)
 {
-  NVRAM_EVNVAR_T        *env;
+  NVRAM_EVNVAR_T *env;
   unsigned int  len;
 
-  memset(value, 0, readLen);
-  env = drv_NVRAM_FindEnv(partition, name);
-  if (env != NULL) {
-    if (readLen > env->valueLen - offset)
-      len = env->valueLen - offset;
-    else
-      len = readLen;
+  if (data == NULL)
+    return DRV_ERR_INVALID_PARAMETER;
 
-    memcpy(value, env->value+offset, len);
-  }
+  memset(data, 0, readLen);
+  env = drv_NVRAM_FindEnv(partition, name);
+
+  if (env == NULL)
+    return DRV_ERR_INVALID_PARAMETER;
+  if (readLen > env->valueLen - offset)
+    len = env->valueLen - offset;
+  else
+    len = readLen;
+
+  memcpy(data, env->value+offset, len);
+  if (pDataLen != NULL)
+    *pDataLen = len;
 
   return DRV_OK;
 }
 
 /*  *********************************************************************
-    *  HMX_NVRAM_GetLength(name)
+    *  drv_NVRAM_GetLength(name)
     *
     *  Retrieve the value of an environment variable
     *
     *  Input parameters:
+    *      partition - the nvram partition for the environment variable
     *      name - name of environment variable to find
+    *      pLen - pointer to where the length should be returned
     *
     *  Return value:
-    *      value, or NULL if variable is not found
+    *      0 if ok
+    *      else error code
     ********************************************************************* */
 static DRV_Error drv_NVRAM_GetLength(HMX_NVRAM_PARTITION_E partition,
                                      const unsigned char *name,
@@ -426,7 +443,7 @@
 }
 
 /*  *********************************************************************
-    *  HMX_NVRAM_SetEnv(name,value,flags)
+    *  drv_NVRAM_SetEnv(name,value,flags)
     *
     *  Set the value of an environment variable
     *
@@ -475,12 +492,14 @@
 
 
 /*  *********************************************************************
-    *  HMX_NVRAM_Load()
+    *  drv_NVRAM_LoadByAddress()
     *
     *  Load the environment from the NVRAM device.
     *
     *  Input parameters:
-    *      nothing
+    *      partition - the name of the NVRAM partition
+    *      address - the address to load from
+    *      size - the size of nvram
     *
     *  Return value:
     *      0 if ok
@@ -590,17 +609,15 @@
 
 error:
   free(buffer);
-
   return retval;
-
 }
 
 static DRV_Error drv_NVRAM_Load(HMX_NVRAM_PARTITION_E partition)
 {
-  DRV_Error     drv_error;
+  DRV_Error drv_error;
 
-  drv_error  = drv_NVRAM_LoadByAddress(partition, s_nvram_offset[partition],
-                                       s_nvram_size[partition]);
+  drv_error = drv_NVRAM_LoadByAddress(partition, s_nvram_offset[partition],
+                                      s_nvram_size[partition]);
   if (drv_error != DRV_OK) {
     DEBUG_ERR("[HMX_NVRAM_Load] error(%d) loading partition (%d)\n", drv_error, partition);
     if (s_nvram_backup_offset[partition] != 0) {
@@ -725,7 +742,7 @@
     return DRV_OK;
   }
   SEM_Release(s_nvramSema);
-  return 1;
+  return DRV_ERR;
 }
 
 
@@ -753,28 +770,31 @@
 
 DRV_Error HMX_NVRAM_Read(HMX_NVRAM_PARTITION_E partition, unsigned char *pName,
                          unsigned int offset, unsigned char *pValue,
-                         unsigned int ulSize)
+                         unsigned int ulSize, unsigned int *pLen)
 {
-  DRV_Error     drv_error;
+  DRV_Error drv_error;
+
+  if (pValue == NULL)
+    return DRV_ERR_INVALID_PARAMETER;
 
   SEM_Get(s_nvramSema);
   memset(pValue, 0, ulSize);
-  drv_error = drv_NVRAM_GetEnv(partition, pName, offset, pValue, ulSize);
+  drv_error = drv_NVRAM_GetEnv(partition, pName, offset, pValue, ulSize, pLen);
   SEM_Release(s_nvramSema);
   return drv_error;
 }
 
-DRV_Error HMX_NVRAM_GetField(NVRAM_FIELD_T field, unsigned int offset,
-                             void *data, int nDataSize)
+DRV_Error HMX_NVRAM_GetFieldAndSize(NVRAM_FIELD_T field, unsigned int offset,
+                                    void *data, int nDataSize, unsigned int *pDataSize)
 {
   DRV_Error errCode = DRV_ERR;
-  unsigned char         szFileName[MAX_NVRAM_FILENAME_LENGTH];
+  unsigned char szFileName[MAX_NVRAM_FILENAME_LENGTH];
   HMX_NVRAM_PARTITION_E partition;
-  int                                   nvramHandle;
-  unsigned int                          defaultSize;
+  int nvramHandle;
+  unsigned int defaultSize;
 
-  if( data == NULL || field == NVRAM_FIELD_DUMMY ) {
-    return 2;
+  if (data == NULL || field == NVRAM_FIELD_DUMMY) {
+    return DRV_ERR_INVALID_PARAMETER;
   }
 
   errCode = drv_NVRAM_GetFieldInfo(field, &partition, szFileName, &defaultSize);
@@ -790,7 +810,7 @@
     case HMX_NVRAM_PARTITION_RO :
     case HMX_NVRAM_PARTITION_RW :
       if (defaultSize == 0) {
-        errCode = HMX_NVRAM_Read(partition, szFileName, offset, data, nDataSize);
+        errCode = HMX_NVRAM_Read(partition, szFileName, offset, data, nDataSize, pDataSize);
         if (errCode != DRV_OK) {
           DEBUG_ERR("[HMX_NVRAM_GetField] field(%d)-%s error(%08X) : "
                     "HMX_NVRAM_Read\n", field, szFileName, errCode);
@@ -807,7 +827,7 @@
           DEBUG_ERR("[HMX_NVRAM_GetField] malloc\n");
           return 3;
         }
-        errCode = HMX_NVRAM_Read(partition, szFileName, 0, pBuf, defaultSize);
+        errCode = HMX_NVRAM_Read(partition, szFileName, 0, pBuf, defaultSize, pDataSize);
         if (errCode != DRV_OK) {
           memset(pBuf, 0, defaultSize);
         }
@@ -818,36 +838,44 @@
     default :
       DEBUG_ERR("[HMX_NVRAM_GetField] DI_ERR_INVALID_PARAM field(%d)-%s "
                 "error(%08X) : HMX_NVRAM_Write\n", field, szFileName, errCode);
-      return 2;
+      return DRV_ERR_INVALID_PARAMETER;
   }
 
   return errCode;
 }
 
-DRV_Error HMX_NVRAM_SetField(NVRAM_FIELD_T field, unsigned int offset, void *data, int nDataSize)
+DRV_Error HMX_NVRAM_GetField(NVRAM_FIELD_T field, unsigned int offset,
+                             void *data, int nDataSize)
+{
+  return HMX_NVRAM_GetFieldAndSize(field, offset, data, nDataSize, NULL);
+}
+
+DRV_Error HMX_NVRAM_SetField(NVRAM_FIELD_T field, unsigned int offset,
+                             void *data, int nDataSize)
 {
   DRV_Error errCode = DRV_ERR;
-  unsigned char         szFileName[MAX_NVRAM_FILENAME_LENGTH];
+  unsigned char szFileName[MAX_NVRAM_FILENAME_LENGTH];
   HMX_NVRAM_PARTITION_E partition;
-  int                                   nvramHandle;
-  unsigned int                          defaultSize;
-  unsigned char                         *pBuf;
-  unsigned int                          systemId;
+  int nvramHandle;
+  unsigned int defaultSize;
+  unsigned char *pBuf;
+  unsigned int systemId;
+  unsigned int pLen = 0;
 
-  if( data == NULL || field == NVRAM_FIELD_DUMMY ) {
-    return 2;
+  if (data == NULL || field == NVRAM_FIELD_DUMMY) {
+    return DRV_ERR_INVALID_PARAMETER;
   }
 
   pBuf = malloc(nDataSize);
   if (pBuf == NULL) {
-    return 5;
+    return DRV_ERR_OUTOFMEMORY;
   }
 
-  errCode = HMX_NVRAM_GetField(field, offset, pBuf, nDataSize);
+  errCode = HMX_NVRAM_GetFieldAndSize(field, offset, pBuf, nDataSize, &pLen);
   if (errCode == DRV_OK) {
-    if (memcmp(pBuf, data, nDataSize) == 0) {
+    if (memcmp(pBuf, data, nDataSize) == 0 && pLen == nDataSize) {
       free(pBuf);
-      return 0;
+      return DRV_OK;
     }
   }
   free(pBuf);
@@ -893,9 +921,9 @@
         pBuf = malloc(defaultSize);
         if (pBuf == NULL) {
           DEBUG_ERR("[HMX_NVRAM_SetField] malloc\n");
-          return 3;
+          return DRV_ERR_INITIALIZATION;
         }
-        errCode = HMX_NVRAM_Read(partition, szFileName, 0, pBuf, defaultSize);
+        errCode = HMX_NVRAM_Read(partition, szFileName, 0, pBuf, defaultSize, NULL);
         if (errCode != DRV_OK) {
           memset(pBuf, 0, defaultSize);
         }
@@ -913,7 +941,7 @@
     default :
       DEBUG_ERR("[HMX_NVRAM_SetField] DI_ERR_INVALID_PARAM field(%d)-%s "
                 "error(%08X) : HMX_NVRAM_Write\n", field, szFileName, errCode);
-      return 1;
+      return DRV_ERR_INVALID_PARAMETER;
   }
 
   return errCode;
@@ -922,14 +950,14 @@
 DRV_Error HMX_NVRAM_GetLength(NVRAM_FIELD_T field, int *pLen)
 {
   DRV_Error errCode = DRV_ERR;
-  unsigned char         szFileName[MAX_NVRAM_FILENAME_LENGTH];
+  unsigned char szFileName[MAX_NVRAM_FILENAME_LENGTH];
   HMX_NVRAM_PARTITION_E partition;
-  int                                   nvramHandle;
-  unsigned int                          defaultSize;
-  unsigned char                         *pBuf;
+  int nvramHandle;
+  unsigned int defaultSize;
+  unsigned char *pBuf;
 
-  if( pLen == NULL) {
-    return 2;
+  if (pLen == NULL) {
+    return DRV_ERR_INVALID_PARAMETER;
   }
 
   *pLen = 0;
@@ -946,9 +974,10 @@
   return DRV_OK;
 }
 
-DRV_Error       HMX_NVRAM_Remove(HMX_NVRAM_PARTITION_E partition, unsigned char *pName)
+DRV_Error HMX_NVRAM_Remove(HMX_NVRAM_PARTITION_E partition, 
+                           unsigned char *pName)
 {
-  int   result;
+  int result;
 
   SEM_Get(s_nvramSema);
   result = drv_NVRAM_Delete(partition, pName);
diff --git a/libupgrade/hmx_upgrade_nvram.h b/libupgrade/hmx_upgrade_nvram.h
index 9e77f5a..0b2fd01 100644
--- a/libupgrade/hmx_upgrade_nvram.h
+++ b/libupgrade/hmx_upgrade_nvram.h
@@ -150,7 +150,8 @@
                                 unsigned char *pValue, unsigned int ulSize);
 DRV_Error       HMX_NVRAM_Read(HMX_NVRAM_PARTITION_E partition,
                                unsigned char *pName, unsigned int offset,
-                               unsigned char *pValue, unsigned int ulSize);
+                               unsigned char *pValue, unsigned int ulSize,
+                               unsigned int *pLen);
 DRV_Error       HMX_NVRAM_GetField(NVRAM_FIELD_T field, unsigned int offset,
                                    void *data, int nDataSize);
 DRV_Error       HMX_NVRAM_SetField(NVRAM_FIELD_T field, unsigned int offset,