@@ -106,14 +106,17 @@ def adb_reboot_bootloader(bin_path: Path) -> TerminalResponse:
106106 """Reboot the device into bootloader and return success."""
107107 for line in run_command ("adb reboot bootloader" , bin_path ):
108108 yield line
109- sleep (1 )
109+ # wait for the bootloader to become available
110+ for line in fastboot_wait_for_bootloader (bin_path = bin_path ):
111+ yield line
110112
111113
112114@add_logging ("Rebooting device into download mode with adb." )
113115def adb_reboot_download (bin_path : Path ) -> TerminalResponse :
114116 """Reboot the device into download mode of samsung devices and return success."""
115117 for line in run_command ("adb reboot download" , bin_path ):
116118 yield line
119+ yield heimdall_wait_for_download_available (bin_path = bin_path )
117120
118121
119122@add_logging ("Sideload the target to device with adb." )
@@ -128,6 +131,22 @@ def activate_sideload(bin_path: Path) -> TerminalResponse:
128131 """Activate sideload with adb shell in twrp."""
129132 for line in run_command ("adb shell twrp sideload" , bin_path ):
130133 yield line
134+ for line in adb_wait_for_sideload (bin_path = bin_path ):
135+ yield line
136+
137+
138+ @add_logging ("Wait for recovery" )
139+ def adb_wait_for_recovery (bin_path : Path ) -> TerminalResponse :
140+ """Use adb to wait for the recovery to become available."""
141+ for line in run_command ("adb wait-for-recovery" , bin_path ):
142+ yield line
143+
144+
145+ @add_logging ("Wait for sideload" )
146+ def adb_wait_for_sideload (bin_path : Path ) -> TerminalResponse :
147+ """Use adb to wait for the sideload to become available."""
148+ for line in run_command ("adb wait-for-sideload" , bin_path ):
149+ yield line
131150
132151
133152def adb_twrp_copy_partitions (bin_path : Path , config_path : Path ) -> TerminalResponse :
@@ -137,7 +156,6 @@ def adb_twrp_copy_partitions(bin_path: Path, config_path: Path) -> TerminalRespo
137156 for line in activate_sideload (bin_path ):
138157 yield line
139158 # now sideload the script
140- sleep (5 )
141159 logger .info ("Sideload the copy_partitions script" )
142160 for line in adb_sideload (
143161 bin_path = bin_path ,
@@ -148,7 +166,6 @@ def adb_twrp_copy_partitions(bin_path: Path, config_path: Path) -> TerminalRespo
148166 # reboot into the bootloader again
149167 for line in adb_reboot_bootloader (bin_path ):
150168 yield line
151- sleep (7 )
152169 # Copy partitions end #
153170 yield True
154171
@@ -180,7 +197,6 @@ def adb_twrp_wipe_and_install(
180197 Only works for twrp recovery.
181198 """
182199 logger .info ("Wipe and format data with twrp, then install os image." )
183- sleep (7 )
184200 # now perform a factory reset
185201 for line in adb_twrp_format_data (bin_path ):
186202 yield line
@@ -197,7 +213,6 @@ def adb_twrp_wipe_and_install(
197213 for line in activate_sideload (bin_path = bin_path ):
198214 yield line
199215 # now flash os image
200- sleep (5 )
201216 logger .info ("Sideload and install os image." )
202217 for line in adb_sideload (bin_path = bin_path , target = target ):
203218 yield line
@@ -226,13 +241,11 @@ def adb_twrp_wipe_and_install(
226241 # reboot into the bootloader again
227242 for line in adb_reboot_bootloader (bin_path ):
228243 yield line
229- sleep (3 )
230244 # boot to TWRP again
231245 for line in fastboot_flash_recovery (
232246 bin_path = bin_path , recovery = recovery , is_ab = is_ab
233247 ):
234248 yield line
235- sleep (7 )
236249 else :
237250 # if not an a/b-device just stay in twrp
238251 pass
@@ -249,14 +262,13 @@ def adb_twrp_install_addons(
249262 Only works for twrp recovery.
250263 """
251264 logger .info ("Install addons with twrp." )
252- sleep (5 )
265+ sleep (0. 5 )
253266 logger .info ("Sideload and install addons." )
254267 for addon in addons :
255268 # activate sideload
256269 logger .info ("Activate sideload." )
257270 for line in activate_sideload (bin_path = bin_path ):
258271 yield line
259- sleep (5 )
260272 # now flash os image
261273 for line in adb_sideload (bin_path = bin_path , target = addon ):
262274 yield line
@@ -267,7 +279,6 @@ def adb_twrp_install_addons(
267279 # reboot into the bootloader again
268280 for line in adb_reboot_bootloader (bin_path = bin_path ):
269281 yield line
270- sleep (3 )
271282 # switch active boot partition
272283 for line in fastboot_switch_partition (bin_path = bin_path ):
273284 yield line
@@ -285,6 +296,13 @@ def adb_twrp_install_addons(
285296 yield line
286297
287298
299+ @add_logging ("Wait for bootloader" )
300+ def fastboot_wait_for_bootloader (bin_path : Path ) -> TerminalResponse :
301+ """Use adb to wait for the bootloader to become available."""
302+ for line in run_command ("fastboot devices" , bin_path ):
303+ yield line
304+
305+
288306@add_logging ("Switch active boot partitions." , return_if_fail = True )
289307def fastboot_switch_partition (bin_path : Path ) -> TerminalResponse :
290308 """Switch the active boot partition with fastboot."""
@@ -336,10 +354,14 @@ def fastboot_flash_recovery(
336354 logger .info ("Boot custom recovery with fastboot." )
337355 for line in run_command (f"fastboot boot { recovery } " , bin_path ):
338356 yield line
357+ for line in adb_wait_for_recovery (bin_path = bin_path ):
358+ yield line
339359 else :
340360 logger .info ("Flash custom recovery with fastboot." )
341361 for line in run_command (f"fastboot flash recovery { recovery } " , bin_path ):
342362 yield line
363+ for line in adb_wait_for_recovery (bin_path = bin_path ):
364+ yield line
343365 if (type (line ) == bool ) and not line :
344366 logger .error ("Flashing recovery failed." )
345367 yield False
@@ -349,6 +371,8 @@ def fastboot_flash_recovery(
349371 logger .info ("Boot into TWRP with fastboot." )
350372 for line in run_command ("fastboot reboot recovery" , bin_path ):
351373 yield line
374+ for line in adb_wait_for_recovery (bin_path = bin_path ):
375+ yield line
352376
353377
354378def fastboot_flash_boot (bin_path : Path , recovery : str ) -> TerminalResponse :
@@ -365,13 +389,25 @@ def fastboot_flash_boot(bin_path: Path, recovery: str) -> TerminalResponse:
365389 logger .info ("Boot into TWRP with fastboot." )
366390 for line in run_command ("fastboot reboot" , bin_path ):
367391 yield line
392+ for line in adb_wait_for_recovery (bin_path = bin_path ):
393+ yield line
368394 if (type (line ) == bool ) and not line :
369395 logger .error ("Booting recovery failed." )
370396 yield False
371397 else :
372398 yield True
373399
374400
401+ def heimdall_wait_for_download_available (bin_path : Path ) -> bool :
402+ """Use heimdall detect to wait for download mode to become available on the device."""
403+ logger .info ("Wait for download mode to become available." )
404+ while True :
405+ sleep (1 )
406+ for line in run_command ("heimdall detect" , bin_path = bin_path ):
407+ if (type (line ) == bool ) and line :
408+ return True
409+
410+
375411@add_logging ("Flash custom recovery with heimdall." )
376412def heimdall_flash_recovery (bin_path : Path , recovery : str ) -> TerminalResponse :
377413 """Temporarily, flash custom recovery with heimdall."""
0 commit comments