Support adb rescue wipe command

Support `adb rescue wipe` command on the host side. This command runs
under the rescue mode and wipes data (and cache/metadata).

Bug: 131037235
Test: run adb rescue wipe

Change-Id: Ib3a3f2d564cc19d0446540d616cc21489ba558c2
(cherry picked from commit c2265c5343)
This commit is contained in:
xunchang 2019-04-22 23:14:28 -07:00 committed by Tianjie Xu
parent fbeac4605c
commit 40bf0f0772
2 changed files with 37 additions and 6 deletions

View file

@ -904,12 +904,12 @@ static int adb_sideload_install(const char* filename, bool rescue_mode) {
}
buf[8] = '\0';
if (strcmp(kSideloadServiceExitSuccess, buf) == 0 ||
strcmp(kSideloadServiceExitFailure, buf) == 0) {
if (strcmp(kMinadbdServicesExitSuccess, buf) == 0 ||
strcmp(kMinadbdServicesExitFailure, buf) == 0) {
printf("\rTotal xfer: %.2fx%*s\n",
static_cast<double>(xfer) / (sb.st_size ? sb.st_size : 1),
static_cast<int>(strlen(filename) + 10), "");
if (strcmp(kSideloadServiceExitFailure, buf) == 0) {
if (strcmp(kMinadbdServicesExitFailure, buf) == 0) {
return 1;
}
return 0;
@ -961,6 +961,33 @@ static int adb_sideload_install(const char* filename, bool rescue_mode) {
}
}
static int adb_wipe_devices() {
auto wipe_devices_message_size = strlen(kMinadbdServicesExitSuccess);
std::string error;
unique_fd fd(adb_connect(
android::base::StringPrintf("rescue-wipe:userdata:%zu", wipe_devices_message_size),
&error));
if (fd < 0) {
fprintf(stderr, "adb: wipe device connection failed: %s\n", error.c_str());
return 1;
}
std::string message(wipe_devices_message_size, '\0');
if (!ReadFdExactly(fd, message.data(), wipe_devices_message_size)) {
fprintf(stderr, "adb: failed to read wipe result: %s\n", strerror(errno));
return 1;
}
if (message == kMinadbdServicesExitSuccess) {
return 0;
}
if (message != kMinadbdServicesExitFailure) {
fprintf(stderr, "adb: got unexpected message from rescue wipe %s\n", message.c_str());
}
return 1;
}
/**
* Run ppp in "notty" mode against a resource listed as the first parameter
* eg:
@ -1643,6 +1670,7 @@ int adb_commandline(int argc, const char** argv) {
} else if (!strcmp(argv[0], "rescue")) {
// adb rescue getprop <prop>
// adb rescue install <filename>
// adb rescue wipe userdata
if (argc != 3) error_exit("rescue requires two arguments");
if (!strcmp(argv[1], "getprop")) {
return adb_connect_command(android::base::StringPrintf("rescue-getprop:%s", argv[2]));
@ -1650,6 +1678,8 @@ int adb_commandline(int argc, const char** argv) {
if (adb_sideload_install(argv[2], true /* rescue_mode */) != 0) {
return 1;
}
} else if (!strcmp(argv[1], "wipe") && !strcmp(argv[2], "userdata")) {
return adb_wipe_devices();
} else {
error_exit("invalid rescue argument");
}

View file

@ -23,9 +23,10 @@ constexpr char kShellServiceArgRaw[] = "raw";
constexpr char kShellServiceArgPty[] = "pty";
constexpr char kShellServiceArgShellProtocol[] = "v2";
// Special flags sent by minadbd that indicate the end of sideload transfer and install result.
constexpr char kSideloadServiceExitSuccess[] = "DONEDONE";
constexpr char kSideloadServiceExitFailure[] = "FAILFAIL";
// Special flags sent by minadbd. They indicate the end of sideload transfer and the result of
// installation or wipe.
constexpr char kMinadbdServicesExitSuccess[] = "DONEDONE";
constexpr char kMinadbdServicesExitFailure[] = "FAILFAIL";
unique_fd create_service_thread(const char* service_name, std::function<void(unique_fd)> func);
#endif // SERVICES_H_