blob: e7bc3559f2ab91d5c33feae6a83f341873f87cec [file] [log] [blame]
Koushik Duttad632c0d2010-08-16 09:22:18 -07001#include <ctype.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <getopt.h>
5#include <limits.h>
6#include <linux/input.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/reboot.h>
11#include <sys/types.h>
12#include <time.h>
13#include <unistd.h>
14
15#include <sys/wait.h>
16#include <sys/limits.h>
17#include <dirent.h>
18#include <sys/stat.h>
19
20#include <signal.h>
21#include <sys/wait.h>
Koushik Duttaf0e31b82010-08-17 16:55:38 -070022#include <sys/types.h>
23#include <sys/stat.h>
24#include <stdio.h>
25#include <errno.h>
26#include <stdlib.h>
27#include <sys/mount.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <unistd.h>
Koushik Duttad632c0d2010-08-16 09:22:18 -070031
Koushik Duttaf0e31b82010-08-17 16:55:38 -070032#include <limits.h>
Koushik Duttad632c0d2010-08-16 09:22:18 -070033
Koushik Duttaf0e31b82010-08-17 16:55:38 -070034#include <cutils/properties.h>
35
36#define RECOVERY_MODE_FILE "/data/.recovery_mode"
Koushik Duttad632c0d2010-08-16 09:22:18 -070037#define UPDATE_BINARY BOARD_HIJACK_RECOVERY_PATH"/update-binary"
38#define UPDATE_PACKAGE BOARD_HIJACK_RECOVERY_PATH"/recovery.zip"
Koushik Duttad632c0d2010-08-16 09:22:18 -070039
Koushik Duttad632c0d2010-08-16 09:22:18 -070040int
Koushik Duttaf0e31b82010-08-17 16:55:38 -070041exec_and_wait(char** argp)
Koushik Duttad632c0d2010-08-16 09:22:18 -070042{
43 pid_t pid;
44 sig_t intsave, quitsave;
45 sigset_t mask, omask;
46 int pstat;
Koushik Duttaf0e31b82010-08-17 16:55:38 -070047
Koushik Duttad632c0d2010-08-16 09:22:18 -070048 sigemptyset(&mask);
49 sigaddset(&mask, SIGCHLD);
50 sigprocmask(SIG_BLOCK, &mask, &omask);
51 switch (pid = vfork()) {
52 case -1: /* error */
53 sigprocmask(SIG_SETMASK, &omask, NULL);
54 return(-1);
55 case 0: /* child */
56 sigprocmask(SIG_SETMASK, &omask, NULL);
Koushik Duttaf0e31b82010-08-17 16:55:38 -070057 execve(argp[0], argp, environ);
Koushik Duttad632c0d2010-08-16 09:22:18 -070058 _exit(127);
59 }
60
61 intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN);
62 quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
63 pid = waitpid(pid, (int *)&pstat, 0);
64 sigprocmask(SIG_SETMASK, &omask, NULL);
65 (void)bsd_signal(SIGINT, intsave);
66 (void)bsd_signal(SIGQUIT, quitsave);
67 return (pid == -1 ? -1 : pstat);
68}
69
Koushik Duttaf0e31b82010-08-17 16:55:38 -070070typedef char* string;
71
Koushik Duttad632c0d2010-08-16 09:22:18 -070072int main(int argc, char** argv) {
73 char* hijacked_executable = argv[0];
74 struct stat info;
75
76 if (NULL != strstr(hijacked_executable, "hijack")) {
77 // no op
Koushik Duttaf0e31b82010-08-17 16:55:38 -070078 if (argc >= 2) {
79 if (strcmp("sh", argv[1]) == 0) {
80 return ash_main(argc - 1, argv + 1);
81 }
82 if (strcmp("mount", argv[1]) == 0) {
83 printf("mount!\n");
84 return mount_main(argc - 1, argv + 1);
85 }
86 if (strcmp("umount", argv[1]) == 0) {
87 printf("umount!\n");
88 return umount_main(argc - 1, argv + 1);
89 }
90 }
Koushik Duttad632c0d2010-08-16 09:22:18 -070091 printf("Hijack!\n");
92 return 0;
93 }
94
95 char cmd[PATH_MAX];
96 if (0 == stat(RECOVERY_MODE_FILE, &info)) {
97 remove(RECOVERY_MODE_FILE);
Koushik Duttad632c0d2010-08-16 09:22:18 -070098 sprintf(cmd, "%s 2 0 %s", UPDATE_BINARY, UPDATE_PACKAGE);
Koushik Duttaf0e31b82010-08-17 16:55:38 -070099
100 char* remount_root_args[] = { "/system/bin/hijack", "mount", "-orw,remount", "/", NULL };
101 //mount_main(3, remount_root_args);
102 //__system("/system/bin/hijack mount -orw,remount /");
103 exec_and_wait(remount_root_args);
104
105 mkdir("/preinstall", S_IRWXU);
106 mkdir("/tmp", S_IRWXU);
107 mkdir("/res", S_IRWXU);
108 mkdir("/res/images", S_IRWXU);
109 remove("/etc");
110 mkdir("/etc", S_IRWXU);
111 rename("/sbin/adbd", "/sbin/adbd.old");
112 property_set("ctl.stop", "runtime");
113 property_set("ctl.stop", "zygote");
114 property_set("persist.service.adb.enable", "1");
115
116 char* mount_preinstall_args[] = { "/system/bin/hijack", "mount", "/dev/block/preinstall", "/preinstall", NULL };
117 //mount_main(3, mount_preinstall_args);
118 //__system("/system/bin/hijack mount /dev/block/preinstall /preinstall");
119 exec_and_wait(mount_preinstall_args);
120
121 char* umount_args[] = { "/system/bin/hijack", "umount", "-l", "/system", NULL };
122 exec_and_wait(umount_args);
123
124 char* updater_args[] = { UPDATE_BINARY, "2", "0", UPDATE_PACKAGE, NULL };
125 return exec_and_wait(updater_args);
Koushik Duttad632c0d2010-08-16 09:22:18 -0700126 }
127
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700128 char real_executable[PATH_MAX];
129 sprintf(real_executable, "%s.bin", hijacked_executable);
130 string* argp = (string*)malloc(sizeof(string) * (argc + 1));
Koushik Duttad632c0d2010-08-16 09:22:18 -0700131 int i;
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700132 for (i = 0; i < argc; i++) {
133 argp[i]=argv[i];
Koushik Duttad632c0d2010-08-16 09:22:18 -0700134 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700135 argp[argc] = NULL;
Koushik Duttad632c0d2010-08-16 09:22:18 -0700136
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700137 argp[0] = real_executable;
138
139 return exec_and_wait(argp);
Koushik Duttad632c0d2010-08-16 09:22:18 -0700140}