@@ -25,20 +25,32 @@ def status(msg):
25
25
26
26
def critical (msg ):
27
27
"""Print critical message to stderr"""
28
- sys .stderr .write ("factory.py: " )
29
- sys .stderr .write (msg )
30
- sys .stderr .write ("\n " )
31
-
28
+ sys .stdout .write ("factory.py: " )
29
+ sys .stdout .write (msg )
30
+ sys .stdout .write ("\n " )
31
+
32
+
33
+ def esptool_call (cmd ):
34
+ try :
35
+ esptool .main (cmd )
36
+ except SystemExit as e :
37
+ # Fetch sys.exit() without leaving the script
38
+ if e .code == 0 :
39
+ return True
40
+ else :
41
+ print (f"❌ esptool failed with exit code: { e .code } " )
42
+ return False
32
43
33
- def generateFactooryImage (source , target , env ):
44
+ def generateFactoryImage (source , target , env ):
34
45
status ("Generating factory image for serial flashing" )
35
46
36
47
app_offset = 0x10000
37
48
app_image = env .subst ("$BUILD_DIR/${PROGNAME}.bin" )
38
49
39
50
# Set fs_offset = 0 to disable LittleFS image generation
40
51
# Set fs_offset to the correct offset from the partition to generate a LittleFS image
41
- fs_offset = 0
52
+ fs_offset = 0x0
53
+ # fs_offset = 0x3E0000
42
54
fs_image = env .subst ("$BUILD_DIR/littlefs.bin" )
43
55
44
56
safeboot_offset = 0x10000
@@ -47,15 +59,10 @@ def generateFactooryImage(source, target, env):
47
59
if safeboot_image == "" :
48
60
safeboot_project = env .GetProjectOption ("custom_safeboot_dir" , "" )
49
61
if safeboot_project != "" :
50
- status (
51
- "Building SafeBoot image for board %s from %s"
52
- % (env .get ("BOARD" ), safeboot_project )
53
- )
62
+ status ("Building SafeBoot image for board %s from %s" % (env .get ("BOARD" ), safeboot_project ))
54
63
if not os .path .isdir (safeboot_project ):
55
64
raise Exception ("SafeBoot project not found: %s" % safeboot_project )
56
- env .Execute (
57
- "SAFEBOOT_BOARD=%s pio run -e safeboot -d %s" % (env .get ("BOARD" ), safeboot_project )
58
- )
65
+ env .Execute ("SAFEBOOT_BOARD=%s pio run -e safeboot -d %s" % (env .get ("BOARD" ), safeboot_project ))
59
66
safeboot_image = join (safeboot_project , ".pio/build/safeboot/firmware.bin" )
60
67
if not os .path .isfile (safeboot_image ):
61
68
raise Exception ("SafeBoot image not found: %s" % safeboot_image )
@@ -65,10 +72,7 @@ def generateFactooryImage(source, target, env):
65
72
if safeboot_url != "" :
66
73
safeboot_image = env .subst ("$BUILD_DIR/safeboot.bin" )
67
74
if not os .path .isfile (safeboot_image ):
68
- status (
69
- "Downloading SafeBoot image from %s to %s"
70
- % (safeboot_url , safeboot_image )
71
- )
75
+ status ("Downloading SafeBoot image from %s to %s" % (safeboot_url , safeboot_image ))
72
76
response = requests .get (safeboot_url )
73
77
if response .status_code != 200 :
74
78
raise Exception ("Download error: %d" % response .status_code )
@@ -98,14 +102,14 @@ def generateFactooryImage(source, target, env):
98
102
cmd = [
99
103
"--chip" ,
100
104
chip ,
101
- "merge_bin " ,
105
+ "merge-bin " ,
102
106
"-o" ,
103
107
factory_image ,
104
- "--flash_mode " ,
108
+ "--flash-mode " ,
105
109
flash_mode ,
106
- "--flash_freq " ,
110
+ "--flash-freq " ,
107
111
flash_freq ,
108
- "--flash_size " ,
112
+ "--flash-size " ,
109
113
flash_size ,
110
114
]
111
115
@@ -117,6 +121,9 @@ def generateFactooryImage(source, target, env):
117
121
if fw_size > max_size :
118
122
raise Exception ("Firmware binary too large: %d > %d" % (fw_size , max_size ))
119
123
124
+ status ("Generating factory image at: %s" % factory_image )
125
+ status ("You can flash it with:\n > esptool.py write_flash 0x0 %s" % factory_image )
126
+
120
127
status (" Offset | File" )
121
128
for section in sections :
122
129
sect_adr , sect_file = section .split (" " , 1 )
@@ -138,11 +145,10 @@ def generateFactooryImage(source, target, env):
138
145
status (f" - { hex (fs_offset )} | { fs_image } " )
139
146
cmd += [hex (fs_offset ), fs_image ]
140
147
141
- status ("Using esptool.py arguments: %s" % " " .join (cmd ))
142
-
143
- esptool .main (cmd )
148
+ # status("Using esptool.py arguments: %s" % " ".join(cmd))
144
149
145
- status ("Factory image generated! You can flash it with:\n > esptool.py write_flash 0x0 %s" % factory_image )
150
+ esptool_call (["version" ])
151
+ esptool_call (cmd )
146
152
147
153
148
- env .AddPostAction ("$BUILD_DIR/${PROGNAME}.bin" , generateFactooryImage )
154
+ env .AddPostAction ("$BUILD_DIR/${PROGNAME}.bin" , generateFactoryImage )
0 commit comments