From f30dd3d81206fcfcce0404436fa55c997d03924e Mon Sep 17 00:00:00 2001 From: Steve Kondik Date: Sat, 19 Oct 2013 19:49:20 -0700 Subject: sr: Get a proper shell environment in recovery * Secure ADB support * Toybox applets * mksh * Various other tools Change-Id: I80b0e2aa5eb7142eaa9f157709f4e029077d8dfa --- recovery.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'recovery.cpp') diff --git a/recovery.cpp b/recovery.cpp index da962ba..5288dab 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -52,6 +52,10 @@ #include "fuse_sideload.h" #include "fuse_sdcard_provider.h" +extern "C" { +#include "recovery_cmds.h" +} + struct selabel_handle *sehandle; static const struct option OPTIONS[] = { @@ -987,6 +991,40 @@ load_locale_from_cache() { } } +static const char *key_src = "/data/misc/adb/adb_keys"; +static const char *key_dest = "/adb_keys"; + + +static void +setup_adbd() { + struct stat f; + // Mount /data and copy adb_keys to root if it exists + ensure_path_mounted("/data"); + if (stat(key_src, &f) == 0) { + FILE *file_src = fopen(key_src, "r"); + if (file_src == NULL) { + LOGE("Can't open %s\n", key_src); + } else { + FILE *file_dest = fopen(key_dest, "w"); + if (file_dest == NULL) { + LOGE("Can't open %s\n", key_dest); + } else { + char buf[4096]; + while (fgets(buf, sizeof(buf), file_src)) fputs(buf, file_dest); + check_and_fclose(file_dest, key_dest); + + // Enable secure adbd + property_set("ro.adb.secure", "1"); + } + check_and_fclose(file_src, key_src); + } + } + ensure_path_unmounted("/data"); + + // Trigger (re)start of adb daemon + property_set("service.adb.root", "1"); +} + static RecoveryUI* gCurrentUI = NULL; void @@ -1005,6 +1043,8 @@ ui_print(const char* format, ...) { } } +extern "C" int toybox_driver(int argc, char **argv); + int main(int argc, char **argv) { // If this binary is started with the single argument "--adbd", @@ -1019,6 +1059,37 @@ main(int argc, char **argv) { return 0; } + // Handle alternative invocations + char* command = argv[0]; + char* stripped = strrchr(argv[0], '/'); + if (stripped) + command = stripped + 1; + + if (strcmp(command, "recovery") != 0) { + struct recovery_cmd cmd = get_command(command); + if (cmd.name) + return cmd.main_func(argc, argv); + + if (!strcmp(command, "setup_adbd")) { + load_volume_table(); + setup_adbd(); + return 0; + } + if (strstr(argv[0], "start")) { + property_set("ctl.start", argv[1]); + return 0; + } + if (strstr(argv[0], "stop")) { + property_set("ctl.stop", argv[1]); + return 0; + } + return toybox_driver(argc, argv); + } + + // Clear umask for packages that copy files out to /tmp and then over + // to /system without properly setting all permissions (eg. gapps). + umask(0); + time_t start = time(NULL); // redirect_stdio should be called only in non-sideload mode. Otherwise -- cgit v1.1