1
+ #define _GNU_SOURCE
2
+ #include < string.h>
3
+
1
4
#include " mbed.h"
2
5
#include " FlashIAPBlockDevice.h"
3
6
#include " FATFileSystem.h"
@@ -21,6 +24,8 @@ void apply_update(FILE *file, uint32_t address);
21
24
22
25
int main ()
23
26
{
27
+ printf (" SFU version %d\r\n " , VERSION);
28
+
24
29
FILE *file;
25
30
sd.init ();
26
31
int err = fs.mount (&sd);
@@ -57,12 +62,35 @@ void apply_update(FILE *file, uint32_t address)
57
62
// Skip the first POST_APPLICATION_ADDR bytes
58
63
long len = ftell (file) - POST_APPLICATION_ADDR;
59
64
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
+ }
61
69
62
70
flash.init ();
63
71
64
72
const uint32_t page_size = flash.get_page_size ();
65
73
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
+
66
94
uint32_t addr = address;
67
95
uint32_t next_sector = addr + flash.get_sector_size (addr);
68
96
bool sector_erased = false ;
0 commit comments