WXK
2024-12-20 51135221cd73a2b3a6ce4b5ec906396d5a33b4c7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (c) 2020-2021 Shanghai Panchip Microelectronics Co.,Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 */
 
#include "flash_manager.h"
 
static uint32_t m_erased_area;
 
void fm_status_refresh(void)
{
    m_erased_area = FLASH_AREA_IMAGE_START;
}
 
void fm_write_flash(unsigned int addr, unsigned char *buf, unsigned int len)
{    
    if ((addr + len) <= m_erased_area) { /* means the specified flash has be erased before */
        FMC_WriteStream(FLCTL, addr, buf, len); /* todo chao*/
    } else {
        if (FMC_EraseSector(FLCTL, m_erased_area) == 0) {
            m_erased_area += SECTOR_SIZE;
            FMC_WriteStream(FLCTL, addr, buf, len);
        } else {
            ASSERT;
        }
    }    
}
 
int fm_read_flash(unsigned int addr, unsigned char *buf, unsigned int len)
{
    return FMC_ReadStream(FLCTL, addr, CMD_FAST_READ, buf, len);
}
 
static void check_sum_add(uint32_t *init_val, uint8_t *val, uint16_t len)
{
    for (uint16_t i =0 ; i < len; i++) {
        *init_val += *(val + i);
    }
}
 
int fm_image_make_invalid(uint32_t im_addr)
{
    return FMC_EraseSector(FLCTL, im_addr);
}
 
static uint8_t read_value[256];
 
void fm_image_move(uint32_t src_addr, uint32_t dst_addr)
{
    uint32_t current_index = 0;
    image_header_t header;
    
    /* fm write to image area, should handle one time fm_status_refresh */
    fm_status_refresh();
    
    /* move header */
    fm_read_flash(src_addr, (unsigned char *)&header, sizeof(image_header_t)); 
    fm_write_flash(dst_addr, (unsigned char *)&header, sizeof(image_header_t));
    src_addr += sizeof(image_header_t);
    dst_addr += sizeof(image_header_t);
    
    header.ih_img_size += 40; /*smp signed related*/
    /* move completed image*/
    while(current_index < header.ih_img_size) {
        if (current_index + 256 <= header.ih_img_size){ /* remaining packets over than 256 bytes */
            fm_read_flash(src_addr + current_index, read_value, 256);
            fm_write_flash(dst_addr+ current_index, read_value, 256);
            current_index += 256;
        } 
        else {
            fm_read_flash(src_addr + current_index, read_value, header.ih_img_size - current_index); /* remaining packets less than 256 bytes */
            fm_write_flash(dst_addr+ current_index, read_value, header.ih_img_size - current_index);
            current_index = header.ih_img_size;
        }
    }
    /* escape move at next time, should be make it invalid */
    fm_image_make_invalid(src_addr);
}
 
bool fm_image_completed_check(uint32_t im_addr)
{
    
    uint32_t check_sum = 0;
    uint32_t current_index = 0;
    image_header_t header;
    
    fm_read_flash(im_addr, (unsigned char *)&header, sizeof(image_header_t));
    
    if (header.ih_img_size > FLASH_IMAGE_MAX_SIZE  || header.ih_img_size <= 256) /* image size should be obtained a basic rules */
        return false;
    
    im_addr += sizeof(image_header_t);
    
    while(current_index < header.ih_img_size) {
        if (current_index + 256 <= header.ih_img_size){ /* remaining packets over than 256 bytes */
            fm_read_flash(im_addr + current_index, read_value, 256);
            current_index += 256;
            check_sum_add(&check_sum, read_value, 256);
        } 
        else {
            fm_read_flash(im_addr + current_index, read_value, header.ih_img_size - current_index); /* remaining packets less than 256 bytes */
            check_sum_add(&check_sum, read_value, header.ih_img_size - current_index);
            current_index = header.ih_img_size;
        }
    }
    
    if (header.checksum == check_sum) {
        return true;
    } else 
        return false;
}
 
#if false
const uint8_t flash_image[] = {
0x1, 0x2, 0x3, 0x4,
0x1, 0x1, 0x0, 0x0,
0x7B, 0x5, 0x0, 0x0,
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0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