添加resfile文件校验工具,确认资源文件是否完整。校验依据 mkota.sh 创建的文件 /system/fileschecksum.txt 。 915/1 915/head 916/target/1 917/target/1
authorSnickerSun <snickersun@163.com>
Sun, 8 Sep 2024 18:13:46 +0000 (02:13 +0800)
committerSnickerSun <snickersun@163.com>
Sun, 8 Sep 2024 18:13:46 +0000 (02:13 +0800)
code/sdk/drivers/peripheral/utils/CMakeLists.txt
code/sdk/drivers/peripheral/utils/custom_resfile_check.c [new file with mode: 0644]
code/sdk/drivers/peripheral/utils/custom_utils.h
code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c

index 66e88b4..f91087f 100644 (file)
@@ -11,6 +11,7 @@ set(SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/custom_runtime.c
     ${CMAKE_CURRENT_SOURCE_DIR}/pdu.c
     ${CMAKE_CURRENT_SOURCE_DIR}/microtar.c
+    ${CMAKE_CURRENT_SOURCE_DIR}/custom_resfile_check.c
 )
 
 set(PUBLIC_HEADER
diff --git a/code/sdk/drivers/peripheral/utils/custom_resfile_check.c b/code/sdk/drivers/peripheral/utils/custom_resfile_check.c
new file mode 100644 (file)
index 0000000..0255b3d
--- /dev/null
@@ -0,0 +1,234 @@
+/**
+ * Copyright (c) @CompanyNameMagicTag 2023-2023. All rights reserved. \n
+ *
+ * Description: Button driver source file \n
+ * Author: @CompanyNameTag \n
+ * History: \n
+ * 2023-02-24, Create file. \n
+ */
+
+#include "pinctrl.h"
+#include "button_porting.h"
+#include "platform_core.h"
+#include "gpio.h"
+#include "gpio_porting.h"
+#include "debug_print.h"
+#include "rtc.h"
+#include "timer.h"
+#include "systick.h"
+#include "chip_core_irq.h"
+#include "pwm.h"
+#include "pwm_porting.h"
+#include "pinctrl.h"
+#include "errcode.h"
+#include "los_base.h"
+
+#include <dirent.h>
+#include <unistd.h>
+#include "custom_runtime.h"
+#include "pm_definition.h"
+
+
+#include "sys/time.h"
+#include "time.h"
+
+#include "custom_utils.h"
+#include "broadcast_feature.h"
+#include "mbedtls/md5.h"
+#include "mbedtls/error.h"
+
+
+#define MAX_FILE_NAME_LEN 256
+#define MAX_FILE_CHECKSUM_STR_LEN (256+64)
+#define RESFILE_CHECKSUM_FILE "/system/fileschecksum.txt"
+
+// #define ENABLE_RESFILECHECK_LOG
+#ifdef ENABLE_RESFILECHECK_LOG
+#define LOG_RESCHECK  printf
+#else
+#define LOG_RESCHECK
+#endif
+/**
+ * 输入: 
+ *      /music/ring/ring.mp3,523621,8c0b93aae3b2ee12e6cf58336a092c43
+*/
+static bool split_argument(char* buf,int inputlen , char* outfilename, int max_outfile_namelen,uint32_t *outfilesize ,char *md5sum_buf, uint32_t nax_md5sum_buf_len)
+{
+    char *tokbuf=NULL;
+    char *pchStrTmpIn = NULL;
+    tokbuf=strtok_r(buf,",",&pchStrTmpIn);
+    if(tokbuf!=NULL)
+    {
+        int filenamelen=strlen(tokbuf);
+        memcpy_s(outfilename,max_outfile_namelen,tokbuf,filenamelen);
+        outfilename[filenamelen]=0;
+    }else{
+        return false;
+    }
+    tokbuf=strtok_r(NULL,",",&pchStrTmpIn);
+    if(tokbuf!=NULL)
+    {
+        *outfilesize=atoi(tokbuf);
+    }else{
+        return false;
+    }
+    tokbuf=strtok_r(NULL,",",&pchStrTmpIn);
+    if(tokbuf!=NULL)
+    {
+        int md5sum_size=strlen(tokbuf);
+        memcpy_s(md5sum_buf,nax_md5sum_buf_len,tokbuf,md5sum_size);
+        md5sum_buf[md5sum_size]=0;
+        // printf("checksum %s ,size %d \n",tokbuf,md5sum_size);
+    }else{
+        return false;
+    }
+    
+    return true;
+}
+/**
+ * 资源文件校验码保存在文件 /system/fileschecksum.txt 中
+ * 读出每一行,输入本函数,检测文件是否正常。
+ * 输入: 
+ *      /music/ring/ring.mp3,523621,8c0b93aae3b2ee12e6cf58336a092c43
+ * 输出:
+ *      outputfile 显示到UI中使用 ,文件错误或者缺失的时候用到。
+ * return true 正确
+ *        false 文件错误,或者缺失 
+*/
+bool check_file(char* buffer,int inputlen,char* outputfile,int outputsize)
+{
+    unsigned char md5sum_str[64]={0};
+    unsigned char current_md5sum_str[64]={0};
+    unsigned char current_md5sum[16]={0};
+    int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
+    mbedtls_md5_context ctx;
+    char* filename=malloc(MAX_FILE_NAME_LEN);
+    char* checksumbuf=malloc(MAX_FILE_NAME_LEN);
+
+    uint32_t filesize=0;
+    uint32_t current_filesize=0;
+    if ( buffer == NULL || outputfile == NULL)
+    {
+        return false;
+    }
+
+    // printf("check %s\n",buffer);
+    if (false == split_argument(buffer,inputlen,(char*)filename,MAX_FILE_NAME_LEN,&filesize,(char*)md5sum_str,64))
+    {
+        printf("splite_argument %s failed !\r\n",buffer);
+        goto freebuf;
+    }
+    memcpy_s(outputfile,outputsize,filename,strlen((char*)filename));
+
+    // printf("===---filename %s, size %d,checksum %s\n",filename,filesize,md5sum_str);
+    FILE *file = NULL;
+    file = fopen((char*)filename, "rb");
+    if (file == NULL) {
+        goto freebuf;
+    }
+
+    mbedtls_md5_init( &ctx );
+    if( ( ret = mbedtls_md5_starts( &ctx ) ) != 0 )
+        goto fileread_error;
+        
+    uint32_t filesize_readed = fread(checksumbuf,1,sizeof(checksumbuf),file); 
+    while (filesize_readed > 0 )
+    {
+        current_filesize+=filesize_readed;
+        if( ( ret = mbedtls_md5_update( &ctx,(unsigned char*) checksumbuf, filesize_readed ) ) != 0 )
+            goto fileread_error;
+        filesize_readed = fread(checksumbuf,1,sizeof(checksumbuf),file); 
+    }
+    fclose(file);
+    if( ( ret = mbedtls_md5_finish( &ctx, (unsigned char*)current_md5sum ) ) != 0 )
+        goto md5_error;
+
+    sprintf((char*)current_md5sum_str,"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
+    current_md5sum[0],current_md5sum[1],current_md5sum[2],current_md5sum[3],
+    current_md5sum[4],current_md5sum[5],current_md5sum[6],current_md5sum[7],
+    current_md5sum[8],current_md5sum[9],current_md5sum[10],current_md5sum[11],
+    current_md5sum[12],current_md5sum[13],current_md5sum[14],current_md5sum[15]);
+    // printf("===+++filename %s, current_filesize %d,current_md5sum %s\n",filename,current_filesize,current_md5sum_str);
+    int cmpret=memcmp(md5sum_str,current_md5sum_str,32);
+    int cmpsize=(filesize == current_filesize);
+    LOG_RESCHECK("filename %s, filesize=compare(%d,%d)=%d,md5sum compare(%s,%s)=%d\n",
+            filename,
+            filesize,current_filesize,cmpsize,
+            md5sum_str,current_md5sum_str,cmpret);
+    if ( cmpret != 0 || cmpsize != 1 )
+    {
+        LOG_RESCHECK("check failed!\r\n");
+        return false;
+    }else{
+        LOG_RESCHECK("check pass\n");
+        return true;
+    }
+fileread_error:
+    fclose(file);
+md5_error:
+    mbedtls_md5_free( &ctx );
+freebuf:
+    free(filename);
+    free(checksumbuf);
+    return false;
+}
+
+void resfile_checksum_thread(void)
+{
+
+    printf("resfile start check\r\n");
+    char* readbuf=malloc(MAX_FILE_CHECKSUM_STR_LEN);
+    if (readbuf == NULL )
+    {
+        printf("malloc fail, skip resfile check\r\n");
+        return ;
+    }
+    char* filename=malloc(MAX_FILE_NAME_LEN);
+    if (filename == NULL )
+    {
+        free(readbuf);
+        printf("malloc fail, skip resfile check\r\n");
+        return ;
+    }
+    FILE * file=fopen(RESFILE_CHECKSUM_FILE,"rb");
+
+    if (file == NULL)
+    {
+        printf("checksum list file %s not exist,skip resfile check\r\n",RESFILE_CHECKSUM_FILE);
+        free(readbuf);
+        free(filename);
+        return ;
+    }
+    memset(readbuf,0,MAX_FILE_CHECKSUM_STR_LEN);
+    char tmp;
+    int i=0;
+    while(fread(&tmp,1,1,file)>0)
+    {
+        if(i>=MAX_FILE_CHECKSUM_STR_LEN)
+        {
+            break;
+        }
+        if(tmp == '\n')
+        {
+            readbuf[i]=tmp;
+            memset(filename,0,MAX_FILE_NAME_LEN);
+            if (check_file(readbuf,i+1,filename,MAX_FILE_NAME_LEN) == false)
+            {
+                printf("resfile %s is modified or deleted!\r\n",filename);
+            }
+            i=0;
+            memset(readbuf,0,MAX_FILE_CHECKSUM_STR_LEN);
+            continue;
+
+        }else{
+            readbuf[i]=tmp;
+        }
+        i++;
+    }
+    
+    fclose(file);
+    free(readbuf);
+    free(filename);
+    printf("resfile check done\r\n");
+
+}
\ No newline at end of file
index e3f41cd..54ec401 100644 (file)
@@ -283,7 +283,17 @@ errcode_t uapi_stop_audio_test(void);
 
 void start_save_file_thread(void);
 
-
+/**
+ * 资源文件校验码保存在文件 /system/fileschecksum.txt 中
+ * 读出每一行,输入本函数,检测文件是否正常。
+ * 输入: 
+ *      /music/ring/ring.mp3,523621,8c0b93aae3b2ee12e6cf58336a092c43
+ * 输出:
+ *      outputfile 显示到UI中使用 ,文件错误或者缺失的时候用到。
+ * return true 正确
+ *        false 文件错误,或者缺失 
+*/
+bool check_file(char* buffer,int inputlen,char* outputfile,int max_outputsize);
 
 #include "custom_utilscpp.h"
 
index 8125bdf..be8844a 100644 (file)
@@ -1177,7 +1177,12 @@ static uint32_t custom_get_sleep(uint8_t *para, uint32_t paraLen, char *strCommo
     return ERRCODE_SUCC;
 }
 
-
+static uint32_t custom_resfiles_checksum(uint8_t *para, uint32_t paraLen, char *strCommonCmd)
+{
+    wstp_print("custom_resfiles_checksum\n");
+    resfile_checksum_thread();
+    return ERRCODE_SUCC;
+}
 static const at_cmd_table_t g_custom_utils_cmd[] = {
     { (char *)"AT^CUSTOM_SN",      custom_utils_setsn, (char *)"123456789ABCDEF2345678" },
     { (char *)"AT^CUSTOM_SN_REQ",  custom_utils_getsn, (char *)" " },
@@ -1257,6 +1262,7 @@ static const at_cmd_table_t g_custom_utils_cmd[] = {
     { (char *)"AT^BLE_TEST",      BLE_TEST, (char *)" " },
     { (char *)"AT^SETVOLUME",      custom_volume_test, (char *)" " },
     { (char *)"AT^CYWEESAVEKEY",      custom_cywee_save_mac_key, (char *)" " },
+    { (char *)"AT^RESFILECHECK",      custom_resfiles_checksum, (char *)" " },
 };