@@ -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,9 @@ 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 )
200+ for line in adb_wait_for_recovery (bin_path ):
201+ yield line
202+
184203 # now perform a factory reset
185204 for line in adb_twrp_format_data (bin_path ):
186205 yield line
@@ -197,7 +216,6 @@ def adb_twrp_wipe_and_install(
197216 for line in activate_sideload (bin_path = bin_path ):
198217 yield line
199218 # now flash os image
200- sleep (5 )
201219 logger .info ("Sideload and install os image." )
202220 for line in adb_sideload (bin_path = bin_path , target = target ):
203221 yield line
@@ -226,13 +244,11 @@ def adb_twrp_wipe_and_install(
226244 # reboot into the bootloader again
227245 for line in adb_reboot_bootloader (bin_path ):
228246 yield line
229- sleep (3 )
230247 # boot to TWRP again
231248 for line in fastboot_flash_recovery (
232249 bin_path = bin_path , recovery = recovery , is_ab = is_ab
233250 ):
234251 yield line
235- sleep (7 )
236252 else :
237253 # if not an a/b-device just stay in twrp
238254 pass
@@ -249,14 +265,13 @@ def adb_twrp_install_addons(
249265 Only works for twrp recovery.
250266 """
251267 logger .info ("Install addons with twrp." )
252- sleep (5 )
268+ sleep (0. 5 )
253269 logger .info ("Sideload and install addons." )
254270 for addon in addons :
255271 # activate sideload
256272 logger .info ("Activate sideload." )
257273 for line in activate_sideload (bin_path = bin_path ):
258274 yield line
259- sleep (5 )
260275 # now flash os image
261276 for line in adb_sideload (bin_path = bin_path , target = addon ):
262277 yield line
@@ -267,7 +282,6 @@ def adb_twrp_install_addons(
267282 # reboot into the bootloader again
268283 for line in adb_reboot_bootloader (bin_path = bin_path ):
269284 yield line
270- sleep (3 )
271285 # switch active boot partition
272286 for line in fastboot_switch_partition (bin_path = bin_path ):
273287 yield line
@@ -285,6 +299,13 @@ def adb_twrp_install_addons(
285299 yield line
286300
287301
302+ @add_logging ("Wait for bootloader" )
303+ def fastboot_wait_for_bootloader (bin_path : Path ) -> TerminalResponse :
304+ """Use adb to wait for the bootloader to become available."""
305+ for line in run_command ("fastboot devices" , bin_path ):
306+ yield line
307+
308+
288309@add_logging ("Switch active boot partitions." , return_if_fail = True )
289310def fastboot_switch_partition (bin_path : Path ) -> TerminalResponse :
290311 """Switch the active boot partition with fastboot."""
@@ -336,10 +357,14 @@ def fastboot_flash_recovery(
336357 logger .info ("Boot custom recovery with fastboot." )
337358 for line in run_command (f"fastboot boot { recovery } " , bin_path ):
338359 yield line
360+ for line in adb_wait_for_recovery (bin_path = bin_path ):
361+ yield line
339362 else :
340363 logger .info ("Flash custom recovery with fastboot." )
341364 for line in run_command (f"fastboot flash recovery { recovery } " , bin_path ):
342365 yield line
366+ for line in adb_wait_for_recovery (bin_path = bin_path ):
367+ yield line
343368 if (type (line ) == bool ) and not line :
344369 logger .error ("Flashing recovery failed." )
345370 yield False
@@ -349,6 +374,8 @@ def fastboot_flash_recovery(
349374 logger .info ("Boot into TWRP with fastboot." )
350375 for line in run_command ("fastboot reboot recovery" , bin_path ):
351376 yield line
377+ for line in adb_wait_for_recovery (bin_path = bin_path ):
378+ yield line
352379
353380
354381def fastboot_flash_boot (bin_path : Path , recovery : str ) -> TerminalResponse :
@@ -365,13 +392,25 @@ def fastboot_flash_boot(bin_path: Path, recovery: str) -> TerminalResponse:
365392 logger .info ("Boot into TWRP with fastboot." )
366393 for line in run_command ("fastboot reboot" , bin_path ):
367394 yield line
395+ for line in adb_wait_for_recovery (bin_path = bin_path ):
396+ yield line
368397 if (type (line ) == bool ) and not line :
369398 logger .error ("Booting recovery failed." )
370399 yield False
371400 else :
372401 yield True
373402
374403
404+ def heimdall_wait_for_download_available (bin_path : Path ) -> bool :
405+ """Use heimdall detect to wait for download mode to become available on the device."""
406+ logger .info ("Wait for download mode to become available." )
407+ while True :
408+ sleep (1 )
409+ for line in run_command ("heimdall detect" , bin_path = bin_path ):
410+ if (type (line ) == bool ) and line :
411+ return True
412+
413+
375414@add_logging ("Flash custom recovery with heimdall." )
376415def heimdall_flash_recovery (bin_path : Path , recovery : str ) -> TerminalResponse :
377416 """Temporarily, flash custom recovery with heimdall."""
0 commit comments