From 928a6613a56141c2df8ddedffd7795cf56d49c34 Mon Sep 17 00:00:00 2001 From: SnickerSun Date: Mon, 9 Sep 2024 02:13:46 +0800 Subject: [PATCH] =?utf8?q?=E6=B7=BB=E5=8A=A0resfile=E6=96=87=E4=BB=B6?= =?utf8?q?=E6=A0=A1=E9=AA=8C=E5=B7=A5=E5=85=B7=EF=BC=8C=E7=A1=AE=E8=AE=A4?= =?utf8?q?=E8=B5=84=E6=BA=90=E6=96=87=E4=BB=B6=E6=98=AF=E5=90=A6=E5=AE=8C?= =?utf8?q?=E6=95=B4=E3=80=82=E6=A0=A1=E9=AA=8C=E4=BE=9D=E6=8D=AE=20mkota.s?= =?utf8?q?h=20=E5=88=9B=E5=BB=BA=E7=9A=84=E6=96=87=E4=BB=B6=20/system/file?= =?utf8?q?schecksum.txt=20=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../drivers/peripheral/utils/CMakeLists.txt | 1 + .../peripheral/utils/custom_resfile_check.c | 234 ++++++++++++++++++ .../drivers/peripheral/utils/custom_utils.h | 12 +- .../peripheral/utils/custom_utils_at_cmd.c | 8 +- 4 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 code/sdk/drivers/peripheral/utils/custom_resfile_check.c diff --git a/code/sdk/drivers/peripheral/utils/CMakeLists.txt b/code/sdk/drivers/peripheral/utils/CMakeLists.txt index 66e88b4c8..f91087ff2 100644 --- a/code/sdk/drivers/peripheral/utils/CMakeLists.txt +++ b/code/sdk/drivers/peripheral/utils/CMakeLists.txt @@ -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 index 000000000..0255b3d5b --- /dev/null +++ b/code/sdk/drivers/peripheral/utils/custom_resfile_check.c @@ -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 +#include +#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 diff --git a/code/sdk/drivers/peripheral/utils/custom_utils.h b/code/sdk/drivers/peripheral/utils/custom_utils.h index e3f41cdee..54ec4014e 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils.h +++ b/code/sdk/drivers/peripheral/utils/custom_utils.h @@ -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" diff --git a/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c b/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c index 8125bdf88..be8844a0b 100644 --- a/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c +++ b/code/sdk/drivers/peripheral/utils/custom_utils_at_cmd.c @@ -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 *)" " }, }; -- 2.17.1