6
6
import os
7
7
from os .path import isdir , isfile , join , dirname
8
8
from shutil import copyfile
9
+ from hashlib import md5
9
10
10
11
from SCons .Script import DefaultEnvironment
11
12
21
22
if False == isfile (main_c ):
22
23
copyfile (join (FRAMEWORK_DIR , "template" , "main.c" ), main_c )
23
24
24
- GFH_Generator = join (FRAMEWORK_DIR ,
25
- "tools" ,
26
- "GFH_Generator.exe" )
27
- FOTA_Generator = join (FRAMEWORK_DIR ,
28
- "tools" ,
29
- "FOTA_Generator.exe" )
30
-
25
+ def fota_crc16 (data :bytearray , length ):
26
+ crc = 0
27
+ for i in range (length ):
28
+ data_byte = data [i ] & 0xff
29
+ crc = crc ^ (data_byte << 8 )
30
+ for _ in range (8 ):
31
+ if (crc & 0x8000 ):
32
+ crc = (crc << 1 ) ^ 0x1021
33
+ else :
34
+ crc = crc << 1
35
+ return crc & 0xffff
31
36
32
37
def gen_bin_file (target , source , env ):
33
38
cmd = ["$OBJCOPY" ]
@@ -38,15 +43,61 @@ def gen_bin_file(target, source, env):
38
43
cmd .extend (["-O" , "binary" ])
39
44
cmd .append (target_elf .get_abspath ())
40
45
cmd .append (temp_firm )
41
- env .Execute (env .VerboseAction (" " .join (cmd ), "Elf to Binary" ))
42
-
43
- cmd_gfh = [GFH_Generator ]
44
- cmd_gfh .append (temp_firm )
45
- cmd_gfh .append (target_firm .get_abspath ())
46
- env .Execute (env .VerboseAction (" " .join (cmd_gfh ), "Adding GFH Header" ))
47
-
46
+ env .Execute (env .VerboseAction (" " .join (cmd ), " " ))
47
+
48
+ GFH_Header = bytearray ([
49
+ 0x4D , 0x4D , 0x4D , 0x01 , 0x40 , 0x00 , 0x00 , 0x00 ,
50
+ 0x46 , 0x49 , 0x4C , 0x45 , 0x5F , 0x49 , 0x4E , 0x46 ,
51
+ 0x4F , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x00 , 0x00 ,
52
+ 0x00 , 0x70 , 0x07 , 0x00 , 0x00 , 0x00 , 0x2E , 0x10 ,
53
+ 0x00 , 0x00 , 0x00 , 0x00 , 0xFF , 0xFF , 0xFF , 0xFF ,
54
+ 0x40 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
55
+ 0x40 , 0x00 , 0x00 , 0x00 , 0x03 , 0x00 , 0x00 , 0x00 ,
56
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x22 ,
57
+ ])
58
+ firm_size = (os .path .getsize (temp_firm ) + 64 ).to_bytes (4 , "little" )
59
+ GFH_Header [0x20 :0x23 ] = firm_size [0 :3 ]
60
+
61
+ with open (target_firm .get_abspath (), "wb" ) as out_firm :
62
+ with open (temp_firm , "rb" ) as in_firm :
63
+ out_firm .write (GFH_Header )
64
+ out_firm .write (in_firm .read ())
65
+ in_firm .close ()
66
+ out_firm .close ()
48
67
os .remove (temp_firm )
49
68
69
+ def gen_fota_file (target , source , env ):
70
+ (fota_firm , ) = target
71
+ (firm_bin , ) = source
72
+ # 0x1c : Filesize
73
+ # 0x4c : CRC16
74
+ FOTA_Header = bytearray ([
75
+ 0x53 , 0x49 , 0x57 , 0x49 , 0x5F , 0x41 , 0x50 , 0x50 ,
76
+ 0x5F , 0x46 , 0x4F , 0x54 , 0x41 , 0x5F , 0x55 , 0x50 ,
77
+ 0x44 , 0x41 , 0x54 , 0x45 , 0x00 , 0x00 , 0x01 , 0x00 ,
78
+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 ,
79
+ 0x10 , 0x00 , 0x00 , 0x00 , 0xFF , 0xFF , 0xFF , 0xFF ,
80
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
81
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
82
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
83
+ 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
84
+ 0xFF , 0xFF , 0xFF , 0xFF , 0x00 , 0x00 , 0x00 , 0x00
85
+ ])
86
+ firm_size = os .path .getsize (firm_bin .get_abspath ()).to_bytes (4 , "little" )
87
+ FOTA_Header [0x1c :0x1f ] = firm_size [0 :3 ]
88
+ crc = fota_crc16 (FOTA_Header , len (FOTA_Header ) - 4 ).to_bytes (4 , "little" )
89
+ FOTA_Header [0x4c :0x4f ] = crc [0 :3 ]
90
+ hash = md5 ()
91
+ hash .update (FOTA_Header )
92
+ with open (firm_bin .get_abspath (), "rb" ) as in_firm :
93
+ in_firm_data = in_firm .read ()
94
+ hash .update (in_firm_data )
95
+ with open (fota_firm .get_abspath (), "wb" ) as out_firm :
96
+ out_firm .write (FOTA_Header )
97
+ out_firm .write (in_firm_data )
98
+ out_firm .write (hash .digest ())
99
+ out_firm .close ()
100
+ in_firm .close ()
50
101
51
102
# Setup ENV
52
103
env .Append (
@@ -110,11 +161,7 @@ def gen_bin_file(target, source, env):
110
161
suffix = ".bin"
111
162
),
112
163
BinToFOTA = Builder (
113
- action = env .VerboseAction (" " .join ([
114
- FOTA_Generator ,
115
- "$SOURCES" ,
116
- "$TARGET"
117
- ]), "Creating FOTA firmware $TARGET" ),
164
+ action = env .VerboseAction (gen_fota_file , "Generating FOTA firmware $TARGET" ),
118
165
suffix = ".bin"
119
166
)
120
167
)
@@ -133,13 +180,11 @@ def gen_bin_file(target, source, env):
133
180
# copy CCFLAGS to ASFLAGS (-x assembler-with-cpp mode)
134
181
env .Append (ASFLAGS = env .get ("CCFLAGS" , [])[:])
135
182
136
-
137
183
def load_siwilib_debug ():
138
184
for i , libs in enumerate (env ["LIBS" ]):
139
185
if libs == "siwisdk" :
140
186
env ["LIBS" ][i ] = "siwisdk_debug"
141
187
142
-
143
188
if board .get ("build.siwilib" ) == "debug" :
144
189
load_siwilib_debug ()
145
190
0 commit comments