1 #include "autoconf.h"
2 #if !defined(CONFIG_PLATFORM_8195A) && !defined(CONFIG_PLATFORM_8711B)
3 #include <flash/stm32_flash.h>
4 #if defined(STM32F2XX)
5 #include <stm32f2xx_flash.h>
6 #elif defined(STM32F4XX)
7 #include <stm32f4xx_flash.h>
8 #elif defined(STM32f1xx)
9 #include <stm32f10x_flash.h>
10 #endif
11 #include "cloud_updater.h"
12 #else
13 #include "flash_api.h"
14 #include "device_lock.h"
15 #endif
16
17 #define BUF_LEN 512
18 #define CONFIG_MD5_USE_SSL
19
20 #ifdef CONFIG_MD5_USE_SSL
21 #include "md5.h"
22 #define file_md5_context md5_context
23 #define file_md5_init(ctx) md5_init(ctx)
24 #define file_md5_starts(ctx) md5_starts(ctx)
25 #define file_md5_update(ctx, input, len) md5_update(ctx, input, len)
26 #define file_md5_finish(ctx, output) md5_finish(ctx, output)
27 #define file_md5_free(ctx) md5_free(ctx)
28 #else
29 #include "rom_md5.h"
30 #define file_md5_context md5_ctx
31 #define file_md5_init(ctx) rt_md5_init(ctx)
32 #define file_md5_starts(ctx)
33 #define file_md5_update(ctx, input, len) rt_md5_append(ctx, input, len)
34 #define file_md5_finish(ctx, output) rt_md5_final(output, ctx)
35 #define file_md5_free(ctx)
36 #endif
37
file_check_sum(uint32_t addr,uint32_t image_len,u8 * check_sum)38 int file_check_sum(uint32_t addr, uint32_t image_len, u8* check_sum)
39 {
40 int ret = -1 ;
41 flash_t flash;
42 file_md5_context md5;
43 unsigned char buf[BUF_LEN];
44 uint32_t read_addr = addr;
45 int len = 0;
46
47 file_md5_init(&md5);
48 file_md5_starts(&md5);
49
50 while(len < image_len){
51 if ((image_len-len) >= BUF_LEN){
52 device_mutex_lock(RT_DEV_LOCK_FLASH);
53 flash_stream_read(&flash, read_addr, BUF_LEN, buf);
54 device_mutex_unlock(RT_DEV_LOCK_FLASH);
55 file_md5_update(&md5, buf, BUF_LEN);
56 }else{
57 device_mutex_lock(RT_DEV_LOCK_FLASH);
58 flash_stream_read(&flash, read_addr, (image_len-len), buf);
59 device_mutex_unlock(RT_DEV_LOCK_FLASH);
60 file_md5_update(&md5, buf, (image_len-len));
61 }
62 len += BUF_LEN;
63 read_addr = read_addr + BUF_LEN;
64 }
65
66 file_md5_finish(&md5, check_sum);
67 file_md5_free(&md5);
68 }
69