Skip to content

Commit e39e59e

Browse files
committed
First semi-manual run for google pixel 3a, no terminal required
1 parent 72b51bb commit e39e59e

File tree

2 files changed

+103
-13
lines changed

2 files changed

+103
-13
lines changed

scripts/lineageos-on-pixel3a.py

Lines changed: 100 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""Script to flash a google pixel 3a.
22
33
Example usage:
4-
5-
4+
poetry run python scripts/lineageos-on-pixel3a.py --recovery images/lineage-19.1-20220830-recovery-sargo.img --image images/lineage-19.1-20220830-nightly-sargo-signed.zip
65
"""
76
import click
87
from subprocess import call
@@ -14,7 +13,86 @@
1413
@click.option('--recovery', help='Path to the recovery file to flash. (Can be TWRP)')
1514
@click.option('--image', help='Path to the lineage os image to flash.')
1615
def install_lineage_os(recovery: str, image: str):
17-
unlocking_bootloader_result = unlock_bootloader()
16+
# Steps 1: Unlock the bootloader
17+
#unlock_result = unlocking_bootloader_result = unlock_bootloader()
18+
#if not unlock_result:
19+
# click.echo("Unlocking the bootloader failed. Exiting.")
20+
# return False
21+
22+
# Step 2: Temporarily booting a custom recovery using fastboot
23+
boot_recovery_result = boot_recovery(recovery)
24+
if not boot_recovery_result:
25+
click.echo("Flashing recovery failed. Exiting.")
26+
return False
27+
28+
# Step 3: Installing LineageOS from recovery
29+
install_result = install_os(image)
30+
if not install_result:
31+
click.echo("Installing LineageOS failed. Exiting.")
32+
return False
33+
34+
click.echo(
35+
"Installing lineageOS was successful! Have fun with your device! :)")
36+
37+
38+
def install_os(image: str):
39+
"""Installing LineageOS from recovery with the image filepath given in image."""
40+
# reboot into fastboot mode
41+
reboot_res = reboot_device_into_bootloader()
42+
if reboot_res == 4:
43+
click.echo("Booting into bootloader failed. Exiting.")
44+
return False
45+
# needs screen interaction here
46+
confirmed = click.confirm("Select 'Boot into Recovery' on your smartphone screen. Wait until you are in recovery, then confirm",
47+
default=True, abort=False, prompt_suffix=': ', show_default=True, err=False)
48+
49+
# automatic factory reset
50+
#factory_reset_result = run_fastboot_command(cmd=["erase", "cache"])
51+
#click.echo(f"{factory_reset_result}")
52+
#factory_reset_result = run_fastboot_command(cmd=["erase", "userdata"])
53+
#click.echo(f"{factory_reset_result}")
54+
55+
# manual factory reset
56+
confirmed = click.confirm(
57+
"Now tap Factory Reset, then Format data / factory reset and continue with the formatting process. This will remove encryption and delete all files stored in the internal storage, as well as format your cache partition (if you have one). Confirm if you are done",
58+
default=True, abort=False, prompt_suffix=': ', show_default=True, err=False
59+
)
60+
61+
# sideload linageos image with adb
62+
confirmed = click.confirm(
63+
"On the device, select “Apply Update”, then “Apply from ADB” to begin sideload. Then confirm here",
64+
default=True, abort=False, prompt_suffix=': ', show_default=True, err=False
65+
)
66+
click.echo("\nRunning: adb sideload <image>")
67+
if call(f'adb sideload {image}', shell=True) < 0:
68+
click.echo("*** Sideloading image failed! ***")
69+
return False
70+
71+
click.echo("Flashing finished. Now press 'back' (arrow) and then 'Reboot system now' to finish the installation.")
72+
return True
73+
74+
75+
def boot_recovery(recovery: str):
76+
"""
77+
Temporarily booting a custom recovery using fastboot.
78+
79+
Using the recovery found in the path 'recovery'.
80+
"""
81+
# reboot into fastboot mode
82+
reboot_res = reboot_device_into_bootloader()
83+
if reboot_res == 4:
84+
click.echo("Unlocking the bootloader failed. Exiting.")
85+
return False
86+
# needs screen interaction here
87+
confirmed = click.confirm("Select 'Restart bootloader' on your smartphone screen. Then confirm",
88+
default=True, abort=False, prompt_suffix=': ', show_default=True, err=False)
89+
# list devices
90+
devices = run_fastboot_command(cmd=["devices"])
91+
click.echo(f"Found: {devices}")
92+
# flash the recovery (temporarily)
93+
flash_result = run_fastboot_command(cmd=["flash", "boot", recovery])
94+
click.echo("Recovery flashed successfully.")
95+
return True
1896

1997

2098
def reboot_device():
@@ -27,12 +105,12 @@ def reboot_device():
27105

28106

29107
def reboot_device_into_bootloader():
30-
"""Reboot the connected device back into fastboot."""
31-
click.echo("\nRunning: fastboot reboot-bootloader")
32-
if call('fastboot reboot bootloader', shell=True) < 0:
108+
"""Reboot the connected device into fastboot."""
109+
click.echo("\nRunning: adb reboot bootloader")
110+
if call('adb reboot bootloader', shell=True) < 0:
33111
click.echo("*** Reboot-bootloader command failed! ***")
34112
return 4
35-
return 0
113+
return 0
36114

37115

38116
def unlock_bootloader():
@@ -42,20 +120,31 @@ def unlock_bootloader():
42120
if reboot_res == 4:
43121
click.echo("Unlocking the bootloader failed. Exiting.")
44122
return False
123+
# needs screen interaction here
124+
confirmed = click.confirm("Select 'Restart bootloader' on your smartphone screen. Then confirm",
125+
default=True, abort=False, prompt_suffix=': ', show_default=True, err=False)
45126
# list devices
46-
devices = run_fastboot_command(cmd="devices")
127+
devices = run_fastboot_command(cmd=["devices"])
47128
click.echo(f"Found: {devices}")
48129
# actually unlock the bootloader
49-
unlock_res = run_fastboot_command(cmd="flashing unlock")
130+
unlock_res = run_fastboot_command(cmd=["flashing", "unlock"])
50131
click.echo(f"{unlock_res}")
132+
click.confirm("At this point the device may display on-screen prompts which will require interaction to continue the process of unlocking the bootloader. Please take whatever actions the device asks you to to proceed.",
133+
default=True, abort=False, prompt_suffix=': ', show_default=True, err=False
134+
)
51135
# reboot device
52136
reboot_res = reboot_device()
53137
if reboot_res == 4:
54138
click.echo("Unlocking the bootloader failed while final reboot. Exiting.")
55139
return False
56140
click.echo("Bootloader is now unlocked!")
57-
click.echo(">>Since the device resets completely, you will need to re-enable USB debugging to continue.")
58-
141+
click.echo(
142+
">>Since the device resets completely, you will need to re-enable USB debugging to continue.")
143+
confirmed = click.confirm(
144+
"Confirm to continue",
145+
default=True, abort=False, prompt_suffix=': ', show_default=True, err=False
146+
)
147+
return confirmed
59148

60149

61150
if __name__ == '__main__':

scripts/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Reusable functions for flashing."""
2+
from typing import List
23
from subprocess import check_output, STDOUT
34

45

5-
def run_fastboot_command(cmd: str) -> str:
6+
def run_fastboot_command(cmd: List[str]) -> str:
67
"""Run a fastboot command and return the result as string."""
7-
return check_output(['fastboot', cmd], stderr=STDOUT).decode()
8+
return check_output(['fastboot'] + cmd, stderr=STDOUT).decode()

0 commit comments

Comments
 (0)