1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- #include <stdio.h>
1716#include <string.h>
1817
1918#include "mbed_wait_api.h"
2423
2524static flash_t flash_obj ;
2625
27- void OTA_GetImageInfo ( imginfo_t * info )
26+ void OTA_ReadHeader ( uint32_t base , imginfo_t * img )
2827{
29- uint32_t ver_hi , ver_lo ;
28+ uint32_t epoch_hi , epoch_lo ;
3029
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 ;
3439
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;
3959 }
60+
61+ return true;
4062}
4163
42- uint32_t OTA_GetBase ( void )
64+ void OTA_GetImageInfo ( uint32_t base , imginfo_t * img )
4365{
44- static uint32_t ota_base = 0 ;
45- imginfo_t region1 , region2 ;
66+ OTA_ReadHeader (base , img );
4667
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;
4971 }
5072
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 );
5382
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+ }
5690
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 ;
6193 }
62- return ota_base ;
94+
95+ return OTA_REGION1_BASE ;
6396}
6497
65- uint32_t OTA_MarkUpdateDone ( void )
98+ uint32_t OTA_UpateHeader ( uint32_t base , imginfo_t * img )
6699{
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 );
68104
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 ;
70111}
71112
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 )
73114{
74115 uint32_t addr , start , end , count , shift ;
75116 uint8_t * pdata = data ;
76117 uint8_t buf [FLASH_SECTOR_SIZE ];
77118
78- start = OTA_GetBase () + offset ;
119+ start = base + offset ;
79120 end = start + len ;
80121
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 ) {
82125 return 0 ;
83126 }
84127
@@ -96,7 +139,6 @@ uint32_t OTA_UpdateImage(uint32_t offset, uint32_t len, uint8_t *data)
96139 }
97140
98141 while (addr < end ) {
99- printf ("OTA: update addr=0x%lx, len=%ld\r\n" , addr , len );
100142 count = MIN (FLASH_SECTOR_SIZE , end - addr );
101143 flash_ext_erase_sector (& flash_obj , addr );
102144 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)
106148 return len ;
107149}
108150
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 )
110152{
111- uint32_t addr , endaddr ;
153+ uint32_t start , end ;
112154
113- addr = OTA_GetBase () + offset ;
114- endaddr = addr + len ;
155+ start = base + offset ;
156+ end = start + len ;
115157
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 ) {
117161 return 0 ;
118162 }
119163
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 );
122165}
123166
124167void OTA_ResetTarget (void )
125168{
126169 __RTK_CTRL_WRITE32 (0x14 , 0x00000021 );
127170 wait (1 );
128171
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 ();
134173
135174 // not reached
136175 while (1 );
0 commit comments