Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error during unlocking caused by 'fastboot getvar token' returning 'GetVar Variabe Not found' on newer devices #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/com/xiaomitool/v2/adb/AdbUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,22 @@ public static String parseFastbootVar(String var, String output) {
return m.group(1).trim();
}

public static String parseOemToken(String output) {
if (output == null) {
return null;
}
Pattern pattern = Pattern.compile("\\s*" + "token" + "\\s*:\\s*([^\\n]+)");
Matcher m = pattern.matcher(output);
if (!m.find()) {
return null;
}
String first = m.group(1).trim();
if (!m.find()) {
return first;
}
return first + m.group(1).trim();
}

public static String parseFastbootOemInfo(List<String> output) {
Pattern pattern = Pattern.compile("Device unlocked:\\s*(\\w+)", Pattern.CASE_INSENSITIVE);
for (String line : output) {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/xiaomitool/v2/adb/FastbootCommons.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public static List<String> devices() {
return command_list("devices");
}

public static String getUnlockToken(String device) {
String token = getvar("token", device);
if (token != null) {
return token;
} else {
return oemGetToken(device);
}
}

public static List<String> getvars(String device) {
FastbootRunner runner = command_fast("getvar all", device, DEFAULT_TIMEOUT);
if (runner == null) {
Expand All @@ -95,6 +104,17 @@ public static String getvar(String var, String device) {
return AdbUtils.parseFastbootVar(var, runner.getOutputString());
}

public static String oemGetToken(String device) {
FastbootRunner runner = command_fast("oem get_token", device, DEFAULT_TIMEOUT);
if (runner == null) {
return null;
}
if (runner.getExitValue() != 0) {
return "";
}
return AdbUtils.parseOemToken(runner.getOutputString());
}

public static List<String> oemDeviceInfo(String device) {
return command_list("oem device-info", device);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public void run(ProcedureRunner runner) throws InstallException, RMessage, Inter
throw new InstallException("Login is required for this action. Please login with your Xiaomi account", InstallException.Code.INFO_RETRIVE_FAILED);
}
}
String token = FastbootCommons.getvar("token", device.getSerial());
String token = FastbootCommons.getUnlockToken(device.getSerial());
Thread.sleep(400);
if (token == null) {
throw new InstallException("Failed to get the device unlock token", InstallException.Code.INFO_RETRIVE_FAILED, FastbootCommons.getLastError(device.getSerial()));
Expand Down Expand Up @@ -274,7 +274,7 @@ public void run(ProcedureRunner runner) throws InstallException, RMessage, Inter
}
Log.info("Unlock request confirmation success");
while (true) {
token = FastbootCommons.getvar("token", device.getSerial());
token = FastbootCommons.getUnlockToken(device.getSerial());
if (token == null) {
throw new InstallException("Failed to get the device unlock token", InstallException.Code.INFO_RETRIVE_FAILED, FastbootCommons.getLastError(device.getSerial()));
}
Expand Down