Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 378bfbfc authored by Tao Bao's avatar Tao Bao
Browse files

Allow entering rescue mode via recovery UI.

Only enabled on debuggable builds.

Bug: 128415917
Test: Sideload package on taimen.
Test: Choose "Enter rescue" from recovery UI.
Change-Id: I913dbdbcffd3179e6fa72ca862f74ca8f1364b02
Merged-In: I913dbdbcffd3179e6fa72ca862f74ca8f1364b02
(cherry picked from commit c6dc325e)
parent d0052c30
Loading
Loading
Loading
Loading
+22 −9
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@
#include <atomic>
#include <functional>
#include <map>
#include <vector>

#include <android-base/file.h>
#include <android-base/logging.h>
@@ -42,6 +43,7 @@
#include "fuse_sideload.h"
#include "install/install.h"
#include "minadbd_types.h"
#include "otautil/sysutil.h"
#include "recovery_ui/ui.h"

using CommandFunction = std::function<bool()>;
@@ -228,7 +230,7 @@ static void ListenAndExecuteMinadbdCommands(
//                               b11. exit the listening loop
//
static void CreateMinadbdServiceAndExecuteCommands(
    const std::map<MinadbdCommands, CommandFunction>& command_map) {
    const std::map<MinadbdCommands, CommandFunction>& command_map, bool rescue_mode) {
  signal(SIGPIPE, SIG_IGN);

  android::base::unique_fd recovery_socket;
@@ -245,9 +247,16 @@ static void CreateMinadbdServiceAndExecuteCommands(
  }
  if (child == 0) {
    recovery_socket.reset();
    execl("/system/bin/minadbd", "minadbd", "--socket_fd",
          std::to_string(minadbd_socket.release()).c_str(), nullptr);

    std::vector<std::string> minadbd_commands = {
      "/system/bin/minadbd",
      "--socket_fd",
      std::to_string(minadbd_socket.release()),
    };
    if (rescue_mode) {
      minadbd_commands.push_back("--rescue");
    }
    auto exec_args = StringVectorToNullTerminatedArray(minadbd_commands);
    execv(exec_args[0], exec_args.data());
    _exit(EXIT_FAILURE);
  }

@@ -280,7 +289,7 @@ static void CreateMinadbdServiceAndExecuteCommands(
  signal(SIGPIPE, SIG_DFL);
}

int apply_from_adb(RecoveryUI* ui) {
int ApplyFromAdb(RecoveryUI* ui, bool rescue_mode) {
  // Save the usb state to restore after the sideload operation.
  std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
  // Clean up state and stop adbd.
@@ -289,16 +298,20 @@ int apply_from_adb(RecoveryUI* ui) {
    return INSTALL_ERROR;
  }

  if (!rescue_mode) {
    ui->Print(
        "\n\nNow send the package you want to apply\n"
        "to the device with \"adb sideload <filename>\"...\n");
  } else {
    ui->Print("\n\nWaiting for rescue commands...\n");
  }

  int install_result = INSTALL_ERROR;
  std::map<MinadbdCommands, CommandFunction> command_map{
    { MinadbdCommands::kInstall, std::bind(&AdbInstallPackageHandler, ui, &install_result) },
  };

  CreateMinadbdServiceAndExecuteCommands(command_map);
  CreateMinadbdServiceAndExecuteCommands(command_map, rescue_mode);

  // Clean up before switching to the older state, for example setting the state
  // to none sets sys/class/android_usb/android0/enable to 0.
+1 −1
Original line number Diff line number Diff line
@@ -18,4 +18,4 @@

#include <recovery_ui/ui.h>

int apply_from_adb(RecoveryUI* ui);
int ApplyFromAdb(RecoveryUI* ui, bool rescue_mode);
+10 −2
Original line number Diff line number Diff line
@@ -31,10 +31,13 @@
#include "minadbd_services.h"
#include "minadbd_types.h"

using namespace std::string_literals;

int main(int argc, char** argv) {
  android::base::InitLogging(argv, &android::base::StderrLogger);
  // TODO(xunchang) implement a command parser
  if (argc != 3 || strcmp("--socket_fd", argv[1]) != 0) {
  if ((argc != 3 && argc != 4) || argv[1] != "--socket_fd"s ||
      (argc == 4 && argv[3] != "--rescue"s)) {
    LOG(ERROR) << "minadbd has invalid arguments, argc: " << argc;
    exit(kMinadbdArgumentsParsingError);
  }
@@ -50,7 +53,12 @@ int main(int argc, char** argv) {
  }
  SetMinadbdSocketFd(socket_fd);

  if (argc == 4) {
    SetMinadbdRescueMode(true);
    adb_device_banner = "rescue";
  } else {
    adb_device_banner = "sideload";
  }

  signal(SIGPIPE, SIG_IGN);

+6 −0
Original line number Diff line number Diff line
@@ -45,10 +45,16 @@
#include "sysdeps.h"

static int minadbd_socket = -1;
static bool rescue_mode = false;

void SetMinadbdSocketFd(int socket_fd) {
  minadbd_socket = socket_fd;
}

void SetMinadbdRescueMode(bool rescue) {
  rescue_mode = rescue;
}

static bool WriteCommandToFd(MinadbdCommands cmd, int fd) {
  char message[kMinadbdMessageSize];
  memcpy(message, kMinadbdCommandPrefix, strlen(kMinadbdStatusPrefix));
+2 −0
Original line number Diff line number Diff line
@@ -17,3 +17,5 @@
#pragma once

void SetMinadbdSocketFd(int socket_fd);

void SetMinadbdRescueMode(bool);
Loading