1+ #define _GNU_SOURCE
2+ #include < string.h>
3+
14#include " mbed.h"
25#include " FlashIAPBlockDevice.h"
36#include " FATFileSystem.h"
@@ -21,6 +24,8 @@ void apply_update(FILE *file, uint32_t address);
2124
2225int main ()
2326{
27+ printf (" SFU version %d\r\n " , VERSION);
28+
2429 FILE *file;
2530 sd.init ();
2631 int err = fs.mount (&sd);
@@ -57,12 +62,35 @@ void apply_update(FILE *file, uint32_t address)
5762 // Skip the first POST_APPLICATION_ADDR bytes
5863 long len = ftell (file) - POST_APPLICATION_ADDR;
5964 printf (" Firmware size is %ld bytes\r\n " , len);
60- fseek (file, POST_APPLICATION_ADDR, SEEK_SET);
65+
66+ if (len < 0 ) {
67+ return ;
68+ }
6169
6270 flash.init ();
6371
6472 const uint32_t page_size = flash.get_page_size ();
6573 char *page_buffer = new char [page_size];
74+
75+ fseek (file, 0 , SEEK_SET);
76+ int size_read = 0 ;
77+ while (size_read < POST_APPLICATION_ADDR) {
78+ memset (page_buffer, 0 , sizeof (char ) * page_size);
79+ size_read += fread (page_buffer, 1 , page_size, file);
80+ if (memmem ((const char *)page_buffer, page_size, " SFU version" , 11 ) != NULL ) {
81+ printf (" Signature string found\n " );
82+ break ;
83+ }
84+ }
85+ if (size_read >= POST_APPLICATION_ADDR) {
86+ printf (" OTA binary does not contain SFU, won't flash it\n " );
87+ delete[] page_buffer;
88+ flash.deinit ();
89+ return ;
90+ }
91+
92+ fseek (file, POST_APPLICATION_ADDR, SEEK_SET);
93+
6694 uint32_t addr = address;
6795 uint32_t next_sector = addr + flash.get_sector_size (addr);
6896 bool sector_erased = false ;
0 commit comments