[Feature][ZXW-452]merge P54U02 version

Only Configure: No
Affected branch: master
Affected module: unknow
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: Yes
Doc Update: No

Change-Id: I17e6795ab66e2b9d1cbbfec4b7c0028d666e177d
diff --git a/ap/lib/libcpnv/nvro.c b/ap/lib/libcpnv/nvro.c
index d65b151..a457ab1 100755
--- a/ap/lib/libcpnv/nvro.c
+++ b/ap/lib/libcpnv/nvro.c
@@ -13,6 +13,20 @@
 #include "flags_api.h"
 #include "zxicbasic_api.h"
 
+static int file_is_exist(const char *filename)
+{
+    FILE *file = fopen(filename, "r");
+
+    if (file)
+    {
+        fclose(file);
+        return 1;
+    }
+    else
+    {
+        return 0;
+    }
+}
 
 /*******************************************************************************
 * 功能描述:     copyfile
@@ -119,6 +133,13 @@
     return ret;
 }
 
+/*******************************************************************************
+* 功能描述:     nvrofs2挂载
+* 参数说明:     
+*   (传入参数)  rw,传入0进行只读挂载,传入非0进行可写挂载
+* 返 回 值:     0表示成功,非0表示失败
+* 其它说明:     
+*******************************************************************************/
 int nvrofs2_mount(int rw)
 {
     if (rw)
@@ -148,6 +169,14 @@
     return result;
 }
 
+/*******************************************************************************
+* 功能描述:     计算文件md5
+* 参数说明:     
+*   (传入参数)  file_in文件名
+*   (传出参数)  hash_value保存文件的md5(二进制),md5需要至少16个字节
+* 返 回 值:     0表示成功,非0表示失败
+* 其它说明:     
+*******************************************************************************/
 int calc_file_hash(const char *file_in, unsigned char *hash_value)
 {
     MD5_CTX ctx;
@@ -180,6 +209,14 @@
     return 0;
 }
 
+/*******************************************************************************
+* 功能描述:     计算文件md5,并保存到文件
+* 参数说明:     
+*   (传入参数)  file_in文件名
+*   (传入参数)  file_hash保存md5的文件
+* 返 回 值:     0表示成功,非0表示失败
+* 其它说明:     
+*******************************************************************************/
 int file_hash(const char *file_in, const char *file_hash)
 {
     unsigned char hash_value[16] = {0};
@@ -197,12 +234,21 @@
         return -1;
     return 0;
 }
+
+/*******************************************************************************
+* 功能描述:     检验文件md5是否一致
+* 参数说明:     
+*   (传入参数)  file_in文件名
+*   (传入参数)  file_hash md5文件
+* 返 回 值:     0表示校验一致,非0表示校验不一致
+* 其它说明:     
+*******************************************************************************/
 int file_hash_check(const char *file_in, const char *file_hash)
 {
     unsigned char hash_value[16] = {0};
     unsigned char *hash_str;
-    unsigned char  hash_str2[33] = {0};
-    int ret;
+    unsigned char  hash_str2[36] = {0};
+    int ret = -1;
     ssize_t ret_s;
 
     ret = calc_file_hash(file_in, hash_value);
@@ -212,18 +258,24 @@
     memset(hash_str2, 0, sizeof(hash_str2));
     ret_s = open_read_close(file_hash, hash_str2, 32);
     if (ret_s != 32)
-        return -1;
-    if (strcmp(hash_str, hash_str2) == 0)
     {
-        return 0;
+        ret = -1;
+        goto out;
     }
-    return -1;
+    if (strcmp(hash_str, hash_str2) == 0)
+        ret = 0; /* md5数据一致 */
+    else
+        ret = -1;  /* md5数据不一致 */
+out:
+    free(hash_str);
+    return ret;
 }
 
 unsigned int cpnv_NvroBackup(void)
 {
     char mtd_path[MAX_PATH] = {0};
     int ret;
+    unsigned int ret_u;
 
     ret = mtd_find("nvrofs2", mtd_path, DEVICE_MTD_BLOCK, MAX_PATH);
     if (ret < 0)
@@ -261,6 +313,61 @@
         printf("[error]cpnv file_hash\n");
         goto out_err;
     }
+    ret = copyfile("/mnt/nvrofs/nvroc0wall.bin", "/mnt/nvrofs2/nvroc0wall.bin");
+    if (ret != 0)
+    {
+        printf("[error]cpnv nvrofs2 copyfile\n");
+        goto out_err;
+    }
+    ret = file_hash("/mnt/nvrofs2/nvroc0wall.bin", "/mnt/nvrofs2/nvroc0wall.bin.hash");
+    if (ret != 0)
+    {
+        printf("[error]cpnv file_hash\n");
+        goto out_err;
+    }
+    ret = nvrofs2_umount();
+    if (ret < 0)
+    {
+        printf("[error]cpnv nvrofs2_umount\n");
+        goto out_err;
+    }
+    ret = flags_set_nvroflag(NVRO_BACKED_UP);
+    if (ret != 0)
+    {
+        printf("[error]cpnv NVRO_BACKED_UP\n");
+        return CPNV_ERROR;
+    }
+    ret = nvrofs2_mount(0);
+    if (ret != 0)
+    {
+        printf("[error]cpnv nvrofs2_mount ro\n");
+        goto out_err;
+    }
+    ret_u = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);
+    if (ret_u != CPNV_OK)
+    {
+        printf("[error]cpnv nvrofs Attr 1\n");
+        goto out_err;
+    }
+    ret = copyfile("/mnt/nvrofs2/nvroall.bin.hash", "/mnt/nvrofs/nvroall.bin.hash");
+    if (ret != 0)
+    {
+        printf("[error]cpnv nvroall.bin.hash copyfile\n");
+        goto out_err;
+    }
+    ret = copyfile("/mnt/nvrofs2/nvroc0wall.bin.hash", "/mnt/nvrofs/nvroc0wall.bin.hash");
+    if (ret != 0)
+    {
+        printf("[error]cpnv nvroc0wall.bin.hash copyfile\n");
+        goto out_err;
+    }
+    ret_u = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 0);
+    if (ret_u != CPNV_OK)
+    {
+        printf("[error]cpnv nvrofs Attr 0\n");
+        goto out_err;
+    }
+
     ret = nvrofs2_umount();
     if (ret < 0)
     {
@@ -268,12 +375,6 @@
         return CPNV_ERROR;
     }
 
-    ret = flags_set_nvroflag(NVRO_BACKED_UP);
-    if (ret != 0)
-    {
-        printf("[error]cpnv NVRO_BACKED_UP\n");
-        return CPNV_ERROR;
-    }
     return CPNV_OK;
 
 out_err:
@@ -282,6 +383,33 @@
     return CPNV_ERROR;
 }
 
+unsigned int cpnv_nvro_check(int flag)
+{
+    int ret;
+
+    if (!file_is_exist("/mnt/nvrofs/nvroall.bin.hash"))
+    {
+        printf("[error]cpnv nvro nvroall.bin.hash not found\n");
+        return CPNV_ERROR;
+    }
+    ret = file_hash_check("/mnt/nvrofs/nvroall.bin", "/mnt/nvrofs/nvroall.bin.hash");
+    if (ret != 0)
+    {
+        printf("[error]cpnv nvro hash check\n");
+        return CPNV_ERROR;
+    }
+    if (flag == 2)
+    {
+        ret = file_hash_check("/mnt/nvrofs/nvroc0wall.bin", "/mnt/nvrofs/nvroc0wall.bin.hash");
+        if (ret != 0)
+        {
+            printf("[error]cpnv nvro hash check\n");
+            return CPNV_ERROR;
+        }
+    }
+    return CPNV_OK;
+}
+
 unsigned int cpnv_NvroRestore(void)
 {
     int ret;
@@ -294,12 +422,16 @@
         printf("[error]cpnv_NvroRestore nvro flag error\n");
         return CPNV_ERROR;
     }
-    nvrofs2_umount();
     ret = nvrofs2_mount(0);
     if (ret != 0)
     {
-        printf("[error]cpnv nvrofs2_mount\n");
-        return CPNV_ERROR;
+        nvrofs2_umount();
+        ret = nvrofs2_mount(0);
+        if (ret != 0)
+        {
+            printf("[error]cpnv nvrofs2_mount\n");
+            return CPNV_ERROR;
+        }
     }
     ret = file_hash_check("/mnt/nvrofs2/nvroall.bin", "/mnt/nvrofs2/nvroall.bin.hash");
     if (ret != 0)
@@ -307,6 +439,12 @@
         printf("[error]cpnv file_hash_check\n");
         goto out_err;
     }
+    ret = file_hash_check("/mnt/nvrofs2/nvroc0wall.bin", "/mnt/nvrofs2/nvroc0wall.bin.hash");
+    if (ret != 0)
+    {
+        printf("[error]cpnv file_hash_check\n");
+        goto out_err;
+    }
     ret_u = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 1);
     if (ret_u != CPNV_OK)
     {
@@ -319,7 +457,25 @@
         printf("[error]cpnv nvrofs2 restore copyfile\n");
         goto out_err;
     }
-	ret_u = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 0);
+    ret = copyfile("/mnt/nvrofs2/nvroall.bin.hash", "/mnt/nvrofs/nvroall.bin.hash");
+    if (ret != 0)
+    {
+        printf("[error]cpnv nvrofs2 restore hash copyfile\n");
+        goto out_err;
+    }
+    ret = copyfile("/mnt/nvrofs2/nvroc0wall.bin", "/mnt/nvrofs/nvroc0wall.bin");
+    if (ret != 0)
+    {
+        printf("[error]cpnv nvrofs2 restore copyfile\n");
+        goto out_err;
+    }
+    ret = copyfile("/mnt/nvrofs2/nvroc0wall.bin.hash", "/mnt/nvrofs/nvroc0wall.bin.hash");
+    if (ret != 0)
+    {
+        printf("[error]cpnv nvrofs2 restore hash copyfile\n");
+        goto out_err;
+    }
+    ret_u = cpnv_ChangeFsPartitionAttr(FS_NVROFS, 0);
     if (ret_u != CPNV_OK)
     {
         printf("[error]cpnv nvrofs Attr 0\n");