出问题的函数如下:
1 char *calc_file_md5(const char* fw_name) 2 { 3 FRESULT result; 4 FIL File; 5 uint32_t BytesRead = 0,offset = 0,file_size = 0; 6 MD5_CTX context; //MD5 7 unsigned char digest[16]; 8 uint8_t i = 0; 9 10 result = f_open(&File,fw_name,FA_OPEN_EXISTING|FA_READ); //打开文件 11 if(FR_OK != result) 12 { 13 DEBUG_LOG(DEBUG_FW,("Not Find %s\n",fw_name)); 14 return 0; 15 } 16 file_size = File.obj.objsize;//结束地址为起始地址加上文件大小。 17 MD5Init(&context); 18 while(1) 19 { 20 f_lseek(&File,offset); 21 result = f_read(&File,file_buffer,2048,&BytesRead); 22 if(result || BytesRead==0) 23 { 24 DEBUG_LOG(DEBUG_FW,("read %s error!\n",fw_name)); 25 return 0; 26 } 27 MD5Update(&context, file_buffer, BytesRead); 28 29 offset+=BytesRead; 30 if(offset==file_size) 31 break; 32 } 33 34 MD5Final(digest, &context); 35 f_close(&File); 36 for (i = 0; i < 16; i++) 37 rt_sprintf(cal_md5_str+(i*2),"%02X",digest[i]); 38 39 DEBUG_LOG(DEBUG_FW,("cal_md5:%s\n",cal_md5_str)); 40 41 return cal_md5_str; 42 }