13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- #include <stdio.h>
17
16
#include <string.h>
18
17
19
18
#include "mbed_wait_api.h"
24
23
25
24
static flash_t flash_obj ;
26
25
27
- void OTA_GetImageInfo ( imginfo_t * info )
26
+ void OTA_ReadHeader ( uint32_t base , imginfo_t * img )
28
27
{
29
- uint32_t ver_hi , ver_lo ;
28
+ uint32_t epoch_hi , epoch_lo ;
30
29
31
- flash_ext_read_word (& flash_obj , info -> base + TAG_OFS , & info -> tag );
32
- flash_ext_read_word (& flash_obj , info -> base + VER_OFS , & ver_lo );
33
- flash_ext_read_word (& flash_obj , info -> base + VER_OFS + 4 , & ver_hi );
30
+ if (base != OTA_REGION1_BASE || base != OTA_REGION2_BASE ) {
31
+ return ;
32
+ }
33
+
34
+ flash_ext_read_word (& flash_obj , base + OTA_TAG_OFS , & img -> tag );
35
+ flash_ext_read_word (& flash_obj , base + OTA_VER_OFS , & img -> ver );
36
+ flash_ext_read_word (& flash_obj , base + OTA_EPOCH_OFS , & epoch_hi );
37
+ flash_ext_read_word (& flash_obj , base + OTA_EPOCH_OFS + 4 , & epoch_lo );
38
+ img -> timestamp = ((uint64_t )epoch_hi << 32 ) | (uint64_t ) epoch_lo ;
34
39
35
- if (info -> tag == TAG_DOWNLOAD ) {
36
- info -> ver = ((uint64_t )ver_hi << 32 ) | (uint64_t ) ver_lo ;
37
- } else {
38
- info -> ver = 0 ;
40
+ flash_ext_read_word (& flash_obj , base + OTA_SIZE_OFS , & img -> size );
41
+ flash_ext_stream_read (& flash_obj , base + OTA_HASH_OFS , 32 , img -> hash );
42
+ flash_ext_stream_read (& flash_obj , base + OTA_CAMPAIGN_OFS , 16 , img -> campaign );
43
+ flash_ext_read_word (& flash_obj , base + OTA_CRC32_OFS , & img -> crc32 );
44
+ }
45
+
46
+ bool OTA_CheckHeader (imginfo_t * img )
47
+ {
48
+ uint8_t * msg ;
49
+ uint32_t crc ;
50
+
51
+ msg = (uint8_t * )img ;
52
+ crc = crc32_get (msg , OTA_CRC32_LEN );
53
+ if (crc != img -> crc32 ) {
54
+ return false;
55
+ }
56
+
57
+ if ((img -> tag & OTA_TAG_CHIP_MSK ) != (OTA_TAG_ID & OTA_TAG_CHIP_MSK )) {
58
+ return false;
39
59
}
60
+
61
+ return true;
40
62
}
41
63
42
- uint32_t OTA_GetBase ( void )
64
+ void OTA_GetImageInfo ( uint32_t base , imginfo_t * img )
43
65
{
44
- static uint32_t ota_base = 0 ;
45
- imginfo_t region1 , region2 ;
66
+ OTA_ReadHeader (base , img );
46
67
47
- if (ota_base == OTA_REGION1 || ota_base == OTA_REGION2 ) {
48
- return ota_base ;
68
+ if (!OTA_CheckHeader (img )) {
69
+ img -> timestamp = 0 ;
70
+ img -> valid = false;
49
71
}
50
72
51
- region1 .base = OTA_REGION1 ;
52
- region2 .base = OTA_REGION2 ;
73
+ img -> valid = true;
74
+ }
75
+
76
+ uint32_t OTA_GetUpdateBase (void )
77
+ {
78
+ imginfo_t img1 , img2 ;
79
+
80
+ OTA_GetImageInfo (OTA_REGION1_BASE , & img1 );
81
+ OTA_GetImageInfo (OTA_REGION2_BASE , & img2 );
53
82
54
- OTA_GetImageInfo (& region1 );
55
- OTA_GetImageInfo (& region2 );
83
+ if (img1 .valid && img2 .valid ) {
84
+ if (img1 .timestamp < img2 .timestamp ) {
85
+ return OTA_REGION1_BASE ;
86
+ } else {
87
+ return OTA_REGION2_BASE ;
88
+ }
89
+ }
56
90
57
- if (region1 .ver >= region2 .ver ) {
58
- ota_base = region2 .base ;
59
- } else {
60
- ota_base = region1 .base ;
91
+ if (img1 .valid ) {
92
+ return OTA_REGION2_BASE ;
61
93
}
62
- return ota_base ;
94
+
95
+ return OTA_REGION1_BASE ;
63
96
}
64
97
65
- uint32_t OTA_MarkUpdateDone ( void )
98
+ uint32_t OTA_UpateHeader ( uint32_t base , imginfo_t * img )
66
99
{
67
- uint32_t addr = OTA_GetBase () + TAG_OFS ;
100
+ flash_ext_write_word (& flash_obj , base + OTA_TAG_OFS , img -> tag );
101
+ flash_ext_write_word (& flash_obj , base + OTA_VER_OFS , img -> ver );
102
+ flash_ext_write_word (& flash_obj , base + OTA_EPOCH_OFS , img -> timestamp >> 32 );
103
+ flash_ext_write_word (& flash_obj , base + OTA_EPOCH_OFS + 4 , (img -> timestamp << 32 ) >> 32 );
68
104
69
- return flash_ext_write_word (& flash_obj , addr , TAG_DOWNLOAD );
105
+ flash_ext_write_word (& flash_obj , base + OTA_SIZE_OFS , img -> size );
106
+ flash_ext_stream_write (& flash_obj , base + OTA_HASH_OFS , 32 , img -> hash );
107
+ flash_ext_stream_write (& flash_obj , base + OTA_CAMPAIGN_OFS , 16 , img -> campaign );
108
+ flash_ext_write_word (& flash_obj , base + OTA_CRC32_OFS , img -> crc32 );
109
+
110
+ return 0 ;
70
111
}
71
112
72
- uint32_t OTA_UpdateImage (uint32_t offset , uint32_t len , uint8_t * data )
113
+ uint32_t OTA_UpdateImage (uint32_t base , uint32_t offset , uint32_t len , uint8_t * data )
73
114
{
74
115
uint32_t addr , start , end , count , shift ;
75
116
uint8_t * pdata = data ;
76
117
uint8_t buf [FLASH_SECTOR_SIZE ];
77
118
78
- start = OTA_GetBase () + offset ;
119
+ start = base + offset ;
79
120
end = start + len ;
80
121
81
- if (data == NULL || start > FLASH_TOP || end > FLASH_TOP ) {
122
+ if (data == NULL ||
123
+ base != OTA_REGION1_BASE || base != OTA_REGION2_BASE ||
124
+ start > FLASH_TOP || end > FLASH_TOP ) {
82
125
return 0 ;
83
126
}
84
127
@@ -96,7 +139,6 @@ uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data)
96
139
}
97
140
98
141
while (addr < end ) {
99
- printf ("OTA: update addr=0x%lx, len=%ld\r\n" , addr , len );
100
142
count = MIN (FLASH_SECTOR_SIZE , end - addr );
101
143
flash_ext_erase_sector (& flash_obj , addr );
102
144
flash_ext_stream_write (& flash_obj , addr , count , pdata );
@@ -106,31 +148,28 @@ uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data)
106
148
return len ;
107
149
}
108
150
109
- uint32_t OTA_ReadImage (uint32_t offset , uint32_t len , uint8_t * data )
151
+ uint32_t OTA_ReadImage (uint32_t base , uint32_t offset , uint32_t len , uint8_t * data )
110
152
{
111
- uint32_t addr , endaddr ;
153
+ uint32_t start , end ;
112
154
113
- addr = OTA_GetBase () + offset ;
114
- endaddr = addr + len ;
155
+ start = base + offset ;
156
+ end = start + len ;
115
157
116
- if (data == NULL || addr > FLASH_TOP || endaddr > FLASH_TOP ) {
158
+ if (data == NULL ||
159
+ base != OTA_REGION1_BASE || base != OTA_REGION2_BASE ||
160
+ start > FLASH_TOP || end > FLASH_TOP ) {
117
161
return 0 ;
118
162
}
119
163
120
- printf ("OTA: read addr=0x%lx\r\n" , addr );
121
- return flash_ext_stream_read (& flash_obj , addr , len , data );
164
+ return flash_ext_stream_read (& flash_obj , start , len , data );
122
165
}
123
166
124
167
void OTA_ResetTarget (void )
125
168
{
126
169
__RTK_CTRL_WRITE32 (0x14 , 0x00000021 );
127
170
wait (1 );
128
171
129
- // write SCB->AIRCR
130
- HAL_WRITE32 (0xE000ED00 , 0x0C ,
131
- (0x5FA << 16 ) | // VECTKEY
132
- (HAL_READ32 (0xE000ED00 , 0x0C ) & (7 << 8 )) | // PRIGROUP
133
- (1 << 2 )); // SYSRESETREQ
172
+ NVIC_SystemReset ();
134
173
135
174
// not reached
136
175
while (1 );
0 commit comments