blob: 1e1d9a952c771123ba53e028f680ed2d36b4f7fe [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ctype.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <getopt.h>
21#include <limits.h>
22#include <linux/input.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070026#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
Doug Zongker8674a722010-09-15 11:08:23 -070030#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080031
32#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include "common.h"
34#include "cutils/properties.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040035#ifdef ANDROID_RB_RESTART
Ken Sumrall6e4472a2011-03-07 23:37:27 -080036#include "cutils/android_reboot.h"
Dees_Troy38bd7602012-09-14 13:33:53 -040037#else
38#include <sys/reboot.h>
39#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080040#include "install.h"
41#include "minui/minui.h"
42#include "minzip/DirUtil.h"
43#include "roots.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070044#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070045#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070046#include "device.h"
Doug Zongker9270a202012-01-09 15:16:13 -080047#include "adb_install.h"
48extern "C" {
49#include "minadbd/adb.h"
50}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
Dees_Troy7d15c252012-09-05 20:47:21 -040052extern "C" {
Dees_Troy7d15c252012-09-05 20:47:21 -040053#include "data.h"
54#include "gui/gui.h"
55}
56#include "partitions.hpp"
Dees_Troy51127312012-09-08 13:08:49 -040057#include "variables.h"
Dees_Troy812660f2012-09-20 09:55:17 -040058#include "openrecoveryscript.hpp"
Dees_Troya58bead2012-09-27 09:49:29 -040059#include "twrp-functions.hpp"
Dees_Troy7d15c252012-09-05 20:47:21 -040060
Dees_Troy5bf43922012-09-07 16:07:55 -040061TWPartitionManager PartitionManager;
Dees_Troy7d15c252012-09-05 20:47:21 -040062
Stephen Smalley779701d2012-02-09 14:13:23 -050063struct selabel_handle *sehandle;
64
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065static const struct option OPTIONS[] = {
66 { "send_intent", required_argument, NULL, 's' },
67 { "update_package", required_argument, NULL, 'u' },
68 { "wipe_data", no_argument, NULL, 'w' },
69 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070070 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070071 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker988500b2009-10-06 14:41:38 -070072 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073};
74
Doug Zongkerd4208f92010-09-20 12:16:13 -070075static const char *COMMAND_FILE = "/cache/recovery/command";
76static const char *INTENT_FILE = "/cache/recovery/intent";
77static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070078static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070079static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Michael Ward9d2629c2011-06-23 22:14:24 -070080static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070081static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080082static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070083static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070084static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085
Doug Zongker211aebc2011-10-28 15:13:10 -070086RecoveryUI* ui = NULL;
87
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080088/*
89 * The recovery tool communicates with the main system through /cache files.
90 * /cache/recovery/command - INPUT - command line for tool, one arg per line
91 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
92 * /cache/recovery/intent - OUTPUT - intent that was passed in
93 *
94 * The arguments which may be supplied in the recovery.command file:
95 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070096 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097 * --wipe_data - erase user data (and cache), then reboot
98 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080099 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700100 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101 *
102 * After completing, we remove /cache/recovery/command and reboot.
103 * Arguments may also be supplied in the bootloader control block (BCB).
104 * These important scenarios must be safely restartable at any point:
105 *
106 * FACTORY RESET
107 * 1. user selects "factory reset"
108 * 2. main system writes "--wipe_data" to /cache/recovery/command
109 * 3. main system reboots into recovery
110 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
111 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700112 * 5. erase_volume() reformats /data
113 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800114 * 7. finish_recovery() erases BCB
115 * -- after this, rebooting will restart the main system --
116 * 8. main() calls reboot() to boot main system
117 *
118 * OTA INSTALL
119 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700120 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121 * 3. main system reboots into recovery
122 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
123 * -- after this, rebooting will attempt to reinstall the update --
124 * 5. install_package() attempts to install the update
125 * NOTE: the package install must itself be restartable from any point
126 * 6. finish_recovery() erases BCB
127 * -- after this, rebooting will (try to) restart the main system --
128 * 7. ** if install failed **
129 * 7a. prompt_and_wait() shows an error icon and waits for the user
130 * 7b; the user reboots (pulling the battery, etc) into the main system
131 * 8. main() calls maybe_install_firmware_update()
132 * ** if the update contained radio/hboot firmware **:
133 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
134 * -- after this, rebooting will reformat cache & restart main system --
135 * 8b. m_i_f_u() writes firmware image into raw cache partition
136 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
137 * -- after this, rebooting will attempt to reinstall firmware --
138 * 8d. bootloader tries to flash firmware
139 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
140 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700141 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800142 * 8g. finish_recovery() erases BCB
143 * -- after this, rebooting will (try to) restart the main system --
144 * 9. main() calls reboot() to boot main system
145 */
146
147static const int MAX_ARG_LENGTH = 4096;
148static const int MAX_ARGS = 100;
149
Doug Zongkerd4208f92010-09-20 12:16:13 -0700150// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700151FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700152fopen_path(const char *path, const char *mode) {
153 if (ensure_path_mounted(path) != 0) {
154 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155 return NULL;
156 }
157
158 // When writing, try to create the containing directory, if necessary.
159 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500160 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161
162 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163 return fp;
164}
165
166// close a file, log an error if the error indicator is set
167static void
168check_and_fclose(FILE *fp, const char *name) {
169 fflush(fp);
170 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
171 fclose(fp);
172}
173
174// command line args come from, in decreasing precedence:
175// - the actual command line
176// - the bootloader control block (one per line, after "recovery")
177// - the contents of COMMAND_FILE (one per line)
178static void
179get_args(int *argc, char ***argv) {
180 struct bootloader_message boot;
181 memset(&boot, 0, sizeof(boot));
182 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
183
184 if (boot.command[0] != 0 && boot.command[0] != 255) {
185 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
186 }
187
188 if (boot.status[0] != 0 && boot.status[0] != 255) {
189 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
190 }
191
192 // --- if arguments weren't supplied, look in the bootloader control block
193 if (*argc <= 1) {
194 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
195 const char *arg = strtok(boot.recovery, "\n");
196 if (arg != NULL && !strcmp(arg, "recovery")) {
197 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
198 (*argv)[0] = strdup(arg);
199 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
200 if ((arg = strtok(NULL, "\n")) == NULL) break;
201 (*argv)[*argc] = strdup(arg);
202 }
203 LOGI("Got arguments from boot message\n");
204 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
205 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
206 }
207 }
208
209 // --- if that doesn't work, try the command file
210 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700211 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212 if (fp != NULL) {
213 char *argv0 = (*argv)[0];
214 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
215 (*argv)[0] = argv0; // use the same program name
216
217 char buf[MAX_ARG_LENGTH];
218 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
219 if (!fgets(buf, sizeof(buf), fp)) break;
220 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
221 }
222
223 check_and_fclose(fp, COMMAND_FILE);
224 LOGI("Got arguments from %s\n", COMMAND_FILE);
225 }
226 }
227
228 // --> write the arguments we have back into the bootloader control block
229 // always boot into recovery after this (until finish_recovery() is called)
230 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
231 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
232 int i;
233 for (i = 1; i < *argc; ++i) {
234 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
235 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
236 }
237 set_bootloader_message(&boot);
238}
239
Doug Zongker34c98df2009-08-18 12:05:45 -0700240static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800241set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700242 struct bootloader_message boot;
243 memset(&boot, 0, sizeof(boot));
244 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
245 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
246 set_bootloader_message(&boot);
247}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800248
Doug Zongker2c3539e2010-09-29 13:21:30 -0700249// How much of the temp log we have copied to the copy in cache.
Dees_Troy7d15c252012-09-05 20:47:21 -0400250//static long tmplog_offset = 0;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700251
252static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700253copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700254 FILE *log = fopen_path(destination, append ? "a" : "w");
255 if (log == NULL) {
256 LOGE("Can't open %s\n", destination);
257 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700258 FILE *tmplog = fopen(source, "r");
259 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700260 if (append) {
261 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
262 }
263 char buf[4096];
264 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
265 if (append) {
266 tmplog_offset = ftell(tmplog);
267 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700268 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700269 }
270 check_and_fclose(log, destination);
271 }
272}
273
274
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275// clear the recovery command and prepare to boot a (hopefully working) system,
276// copy our log file to cache as well (for the system to read), and
277// record any intent we were asked to communicate back to the system.
278// this function is idempotent: call it as many times as you like.
279static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800280finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800281 // By this point, we're ready to return to the main system...
282 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700283 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000284 if (fp == NULL) {
285 LOGE("Can't open %s\n", INTENT_FILE);
286 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800287 fputs(send_intent, fp);
288 check_and_fclose(fp, INTENT_FILE);
289 }
290 }
291
292 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700293 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
294 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
295 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
296 chmod(LOG_FILE, 0600);
297 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700298 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700299 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700301 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800302 struct bootloader_message boot;
303 memset(&boot, 0, sizeof(boot));
304 set_bootloader_message(&boot);
305
306 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700307 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
308 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800309 LOGW("Can't unlink %s\n", COMMAND_FILE);
310 }
311
Doug Zongkerd0181b82011-10-19 10:51:12 -0700312 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800313 sync(); // For good measure.
314}
315
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800316static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700317erase_volume(const char *volume) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700318 ui->SetBackground(RecoveryUI::INSTALLING);
319 ui->SetProgressType(RecoveryUI::INDETERMINATE);
320 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700321
Doug Zongkerd0181b82011-10-19 10:51:12 -0700322 ensure_path_unmounted(volume);
323
Doug Zongker2c3539e2010-09-29 13:21:30 -0700324 if (strcmp(volume, "/cache") == 0) {
325 // Any part of the log we'd copied to cache is now gone.
326 // Reset the pointer so we copy from the beginning of the temp
327 // log.
328 tmplog_offset = 0;
329 }
330
Doug Zongkerd4208f92010-09-20 12:16:13 -0700331 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800332}
333
Doug Zongker23ceeea2010-07-08 17:27:55 -0700334static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700335copy_sideloaded_package(const char* original_path) {
336 if (ensure_path_mounted(original_path) != 0) {
337 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700338 return NULL;
339 }
340
Doug Zongkerd4208f92010-09-20 12:16:13 -0700341 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700342 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
343 return NULL;
344 }
345
Doug Zongkerd4208f92010-09-20 12:16:13 -0700346 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700347 if (errno != EEXIST) {
348 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
349 return NULL;
350 }
351 }
352
Doug Zongkerd4208f92010-09-20 12:16:13 -0700353 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
354 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700355 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700356 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
357 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700358 return NULL;
359 }
360 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700361 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700362 return NULL;
363 }
364 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700365 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700366 return NULL;
367 }
368 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700369 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700370 return NULL;
371 }
372
Doug Zongkerd4208f92010-09-20 12:16:13 -0700373 char copy_path[PATH_MAX];
374 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700375 strcat(copy_path, "/package.zip");
376
Doug Zongker28ce47c2011-10-28 10:33:05 -0700377 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700378 if (buffer == NULL) {
379 LOGE("Failed to allocate buffer\n");
380 return NULL;
381 }
382
383 size_t read;
384 FILE* fin = fopen(original_path, "rb");
385 if (fin == NULL) {
386 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
387 return NULL;
388 }
389 FILE* fout = fopen(copy_path, "wb");
390 if (fout == NULL) {
391 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
392 return NULL;
393 }
394
395 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
396 if (fwrite(buffer, 1, read, fout) != read) {
397 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
398 return NULL;
399 }
400 }
401
402 free(buffer);
403
404 if (fclose(fout) != 0) {
405 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
406 return NULL;
407 }
408
409 if (fclose(fin) != 0) {
410 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
411 return NULL;
412 }
413
414 // "adb push" is happy to overwrite read-only files when it's
415 // running as root, but we'll try anyway.
416 if (chmod(copy_path, 0400) != 0) {
417 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
418 return NULL;
419 }
420
Doug Zongkerd4208f92010-09-20 12:16:13 -0700421 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700422}
423
Doug Zongker28ce47c2011-10-28 10:33:05 -0700424static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700425prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700426 const char* title[] = { "Android system recovery <"
427 EXPAND(RECOVERY_API_VERSION) "e>",
428 "",
429 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800430
Doug Zongkerd6837852009-06-17 22:07:13 -0700431 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700432 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700433 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700434 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700435 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700436 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700437
Doug Zongker28ce47c2011-10-28 10:33:05 -0700438 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
439 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700440 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700441 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700442 *h = NULL;
443
Doug Zongkerf93d8162009-09-22 15:16:02 -0700444 return new_headers;
445}
446
447static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700448get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700449 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700450 // throw away keys pressed previously, so user doesn't
451 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700452 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700453
Doug Zongker211aebc2011-10-28 15:13:10 -0700454 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700455 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800456 int chosen_item = -1;
457
Doug Zongkerf93d8162009-09-22 15:16:02 -0700458 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700459 int key = ui->WaitKey();
460 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800461
Doug Zongker5cae4452011-01-25 13:15:30 -0800462 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700463 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800464 continue;
465 } else {
466 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700467 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700468 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800469 }
470 }
471
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700472 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700473
474 if (action < 0) {
475 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700476 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700477 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700478 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700479 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700480 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700481 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700482 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700483 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700484 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700485 chosen_item = selected;
486 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700487 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700488 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800489 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700490 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700491 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800492 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700493 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800494
Doug Zongker211aebc2011-10-28 15:13:10 -0700495 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700496 return chosen_item;
497}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800498
Doug Zongker8674a722010-09-15 11:08:23 -0700499static int compare_string(const void* a, const void* b) {
500 return strcmp(*(const char**)a, *(const char**)b);
501}
502
503static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700504update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700505 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700506 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700507
Doug Zongker8674a722010-09-15 11:08:23 -0700508 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700509 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700510 "",
511 NULL };
512 DIR* d;
513 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700514 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700515 if (d == NULL) {
516 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700517 if (unmount_when_done != NULL) {
518 ensure_path_unmounted(unmount_when_done);
519 }
Doug Zongker8674a722010-09-15 11:08:23 -0700520 return 0;
521 }
522
Doug Zongker28ce47c2011-10-28 10:33:05 -0700523 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700524
525 int d_size = 0;
526 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700527 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700528 int z_size = 1;
529 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700530 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700531 zips[0] = strdup("../");
532
533 while ((de = readdir(d)) != NULL) {
534 int name_len = strlen(de->d_name);
535
536 if (de->d_type == DT_DIR) {
537 // skip "." and ".." entries
538 if (name_len == 1 && de->d_name[0] == '.') continue;
539 if (name_len == 2 && de->d_name[0] == '.' &&
540 de->d_name[1] == '.') continue;
541
542 if (d_size >= d_alloc) {
543 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700544 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700545 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700546 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700547 strcpy(dirs[d_size], de->d_name);
548 dirs[d_size][name_len] = '/';
549 dirs[d_size][name_len+1] = '\0';
550 ++d_size;
551 } else if (de->d_type == DT_REG &&
552 name_len >= 4 &&
553 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
554 if (z_size >= z_alloc) {
555 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700556 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700557 }
558 zips[z_size++] = strdup(de->d_name);
559 }
560 }
561 closedir(d);
562
563 qsort(dirs, d_size, sizeof(char*), compare_string);
564 qsort(zips, z_size, sizeof(char*), compare_string);
565
566 // append dirs to the zips list
567 if (d_size + z_size + 1 > z_alloc) {
568 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700569 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700570 }
571 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
572 free(dirs);
573 z_size += d_size;
574 zips[z_size] = NULL;
575
576 int result;
577 int chosen_item = 0;
578 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700579 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700580
581 char* item = zips[chosen_item];
582 int item_len = strlen(item);
583 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700584 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700585 result = -1;
586 break;
587 } else if (item[item_len-1] == '/') {
588 // recurse down into a subdirectory
589 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700590 strlcpy(new_path, path, PATH_MAX);
591 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700592 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700593 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700594 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700595 if (result >= 0) break;
596 } else {
597 // selected a zip file: attempt to install it, and return
598 // the status to the caller.
599 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700600 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700601 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700602 strlcat(new_path, item, PATH_MAX);
603
Doug Zongker211aebc2011-10-28 15:13:10 -0700604 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700605 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700606 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700607 if (unmount_when_done != NULL) {
608 ensure_path_unmounted(unmount_when_done);
609 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700610 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700611 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700612 free(copy);
613 } else {
614 result = INSTALL_ERROR;
615 }
Doug Zongker8674a722010-09-15 11:08:23 -0700616 break;
617 }
618 } while (true);
619
620 int i;
621 for (i = 0; i < z_size; ++i) free(zips[i]);
622 free(zips);
623 free(headers);
624
Michael Ward9d2629c2011-06-23 22:14:24 -0700625 if (unmount_when_done != NULL) {
626 ensure_path_unmounted(unmount_when_done);
627 }
Doug Zongker8674a722010-09-15 11:08:23 -0700628 return result;
629}
630
Doug Zongkerf93d8162009-09-22 15:16:02 -0700631static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700632wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700633 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700634 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700635
Doug Zongkerf93d8162009-09-22 15:16:02 -0700636 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700637 const char* headers[] = { "Confirm wipe of all user data?",
638 " THIS CAN NOT BE UNDONE.",
639 "",
640 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700641 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700642 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800643
Doug Zongker28ce47c2011-10-28 10:33:05 -0700644 const char* items[] = { " No",
645 " No",
646 " No",
647 " No",
648 " No",
649 " No",
650 " No",
651 " Yes -- delete all user data", // [7]
652 " No",
653 " No",
654 " No",
655 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800656
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700657 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700658 if (chosen_item != 7) {
659 return;
660 }
661 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700662
Doug Zongker211aebc2011-10-28 15:13:10 -0700663 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700664 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700665 erase_volume("/data");
666 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700667 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700668}
669
670static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700671prompt_and_wait(Device* device) {
672 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700673
674 for (;;) {
675 finish_recovery(NULL);
Doug Zongker211aebc2011-10-28 15:13:10 -0700676 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700677
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700678 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700679
680 // device-specific code may take some action here. It may
681 // return one of the core actions handled in the switch
682 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700683 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700684
Michael Ward9d2629c2011-06-23 22:14:24 -0700685 int status;
Doug Zongkerd0181b82011-10-19 10:51:12 -0700686 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700687 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700688 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700689 return;
690
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700691 case Device::WIPE_DATA:
692 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700693 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700694 break;
695
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700696 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700697 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700698 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700699 ui->Print("Cache wipe complete.\n");
700 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700701 break;
702
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700703 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800704 // Some packages expect /cache to be mounted (eg,
705 // standard incremental packages expect to use /cache
706 // as scratch space).
707 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700708 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700709 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700710 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700711 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700712 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700713 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700714 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700715 }
716 }
Doug Zongker8674a722010-09-15 11:08:23 -0700717 if (status >= 0) {
718 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700719 ui->SetBackground(RecoveryUI::ERROR);
720 ui->Print("Installation aborted.\n");
721 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700722 return; // reboot if logs aren't visible
723 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700724 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700725 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700726 }
727 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700728
729 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700730 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700731 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700732 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700733 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700734 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700735 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700736 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700737 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700738 }
739 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700740 if (status >= 0) {
741 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700742 ui->SetBackground(RecoveryUI::ERROR);
743 ui->Print("Installation aborted.\n");
744 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700745 return; // reboot if logs aren't visible
746 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700747 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700748 }
749 }
750 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800751
752 case Device::APPLY_ADB_SIDELOAD:
753 ensure_path_mounted(CACHE_ROOT);
754 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
755 if (status >= 0) {
756 if (status != INSTALL_SUCCESS) {
757 ui->SetBackground(RecoveryUI::ERROR);
758 ui->Print("Installation aborted.\n");
759 } else if (!ui->IsTextVisible()) {
760 return; // reboot if logs aren't visible
761 } else {
762 ui->Print("\nInstall from ADB complete.\n");
763 }
764 }
765 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800766 }
767 }
768}
769
770static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800771print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700772 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800773}
774
775int
Oscar Montemayor05231562009-11-30 08:40:57 -0800776main(int argc, char **argv) {
Dees_Troycfb63ae2012-09-19 14:30:17 -0400777 // Recovery needs to install world-readable files, so clear umask
778 // set by init
779 umask(0);
780
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800781 time_t start = time(NULL);
782
783 // If these fail, there's not really anywhere to complain...
784 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
785 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800786
787 // If this binary is started with the single argument "--adbd",
788 // instead of being the normal recovery binary, it turns into kind
789 // of a stripped-down version of adbd that only supports the
790 // 'sideload' command. Note this must be a real argument, not
791 // anything in the command file or bootloader control block; the
792 // only way recovery should be run with this argument is when it
793 // starts a copy of itself from the apply_from_adb() function.
Dees_Troy9a4b5692012-09-19 15:09:45 -0400794 if (argc == 3 && strcmp(argv[1], "--adbd") == 0) {
795 adb_main(argv[2]);
Doug Zongker9270a202012-01-09 15:16:13 -0800796 return 0;
797 }
798
Dees_Troy51127312012-09-08 13:08:49 -0400799 printf("Starting TWRP %s on %s", TW_VERSION_STR, ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800800
Dees_Troy32c8eb82012-09-11 15:28:06 -0400801 Device* device = make_device();
802 ui = device->GetUI();
Doug Zongker211aebc2011-10-28 15:13:10 -0700803
Dees_Troy7d15c252012-09-05 20:47:21 -0400804 //ui->Init();
805 //ui->SetBackground(RecoveryUI::NONE);
Dees_Troy7c2dec82012-09-26 09:49:14 -0400806 //load_volume_table();
Dees_Troy7d15c252012-09-05 20:47:21 -0400807
808 // Load default values to set DataManager constants and handle ifdefs
809 DataManager_LoadDefaults();
810 printf("Starting the UI...");
Dees_Troy51127312012-09-08 13:08:49 -0400811 gui_init();
Dees_Troyce2fe772012-09-28 12:34:33 -0400812 printf("=> Installing busybox into /sbin\n");
813 system("/sbin/bbinstall.sh"); // Let's install busybox
Dees_Troy7d15c252012-09-05 20:47:21 -0400814 printf("=> Linking mtab\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400815 system("ln -s /proc/mounts /etc/mtab"); // And link mtab for mke2fs
Dees_Troy7d15c252012-09-05 20:47:21 -0400816 printf("=> Processing recovery.fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400817 if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400818 LOGE("Failing out of recovery due to problem with recovery.fstab.\n");
819 //return -1;
820 }
Dees_Troy8170a922012-09-18 15:40:25 -0400821 PartitionManager.Output_Partition_Logging();
Dees_Troy7d15c252012-09-05 20:47:21 -0400822 // Load up all the resources
823 gui_loadResources();
824
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800825 get_args(&argc, &argv);
826
827 int previous_runs = 0;
828 const char *send_intent = NULL;
829 const char *update_package = NULL;
830 int wipe_data = 0, wipe_cache = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700831 bool just_exit = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800832
833 int arg;
834 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
835 switch (arg) {
836 case 'p': previous_runs = atoi(optarg); break;
837 case 's': send_intent = optarg; break;
838 case 'u': update_package = optarg; break;
839 case 'w': wipe_data = wipe_cache = 1; break;
840 case 'c': wipe_cache = 1; break;
Doug Zongker211aebc2011-10-28 15:13:10 -0700841 case 't': ui->ShowText(true); break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700842 case 'x': just_exit = true; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800843 case '?':
844 LOGE("Invalid command argument\n");
845 continue;
846 }
847 }
848
Stephen Smalley779701d2012-02-09 14:13:23 -0500849#ifdef HAVE_SELINUX
850 struct selinux_opt seopts[] = {
851 { SELABEL_OPT_PATH, "/file_contexts" }
852 };
853
854 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
855
856 if (!sehandle) {
857 fprintf(stderr, "Warning: No file_contexts\n");
Kenny Root038818c2012-04-08 11:03:01 -0700858 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500859 }
860#endif
861
Dees_Troy7d15c252012-09-05 20:47:21 -0400862 //device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800863
Doug Zongker56c51052010-07-01 09:18:44 -0700864 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800865 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700866 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800867 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700868 printf("\n");
869
870 if (update_package) {
871 // For backwards compatibility on the cache partition only, if
872 // we're given an old 'root' path "CACHE:foo", change it to
873 // "/cache/foo".
874 if (strncmp(update_package, "CACHE:", 6) == 0) {
875 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700876 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700877 strlcpy(modified_path, "/cache/", len);
878 strlcat(modified_path, update_package+6, len);
879 printf("(replacing path \"%s\" with \"%s\")\n",
880 update_package, modified_path);
881 update_package = modified_path;
882 }
883 }
884 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800885
886 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700887 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800888
Dees_Troy7d15c252012-09-05 20:47:21 -0400889 // Check for and run startup script if script exists
Dees_Troya58bead2012-09-27 09:49:29 -0400890 TWFunc::check_and_run_script("/sbin/runatboot.sh", "boot");
891 TWFunc::check_and_run_script("/sbin/postrecoveryboot.sh", "boot");
Dees_Troy7d15c252012-09-05 20:47:21 -0400892
893#ifdef TW_INCLUDE_INJECTTWRP
894 // Back up TWRP Ramdisk if needed:
895 LOGI("Backing up TWRP ramdisk...\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400896 system("injecttwrp --backup /tmp/backup_recovery_ramdisk.img");
Dees_Troy7d15c252012-09-05 20:47:21 -0400897 LOGI("Backup of TWRP ramdisk done.\n");
898#endif
899
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800900 int status = INSTALL_SUCCESS;
901
Doug Zongker540d57f2011-01-18 13:36:58 -0800902 if (update_package != NULL) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400903 gui_console_only();
Doug Zongkerd0181b82011-10-19 10:51:12 -0700904 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
905 if (status == INSTALL_SUCCESS && wipe_cache) {
906 if (erase_volume("/cache")) {
907 LOGE("Cache wipe (requested by package) failed.");
908 }
909 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700910 if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700911 } else if (wipe_data) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400912 gui_console_only();
Dees_Troy5bf43922012-09-07 16:07:55 -0400913 if (PartitionManager.Factory_Reset()) status = INSTALL_ERROR;
Dees_Troy7d15c252012-09-05 20:47:21 -0400914 //if (device->WipeData()) status = INSTALL_ERROR;
915 //if (erase_volume("/data")) status = INSTALL_ERROR;
916 //if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700917 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700918 } else if (wipe_cache) {
Dees_Troy7d15c252012-09-05 20:47:21 -0400919 gui_console_only();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700920 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700921 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700922 } else if (!just_exit) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800923 status = INSTALL_ERROR; // No command specified
924 }
925
Dees_Troy7d15c252012-09-05 20:47:21 -0400926 //if (status != INSTALL_SUCCESS) ui->SetBackground(RecoveryUI::ERROR);
927 if (status != INSTALL_SUCCESS /*|| ui->IsTextVisible()*/) {
Dees_Troy812660f2012-09-20 09:55:17 -0400928 finish_recovery(NULL);
Dees_Troy7d15c252012-09-05 20:47:21 -0400929 DataManager_ReadSettingsFile();
Dees_Troy812660f2012-09-20 09:55:17 -0400930 if (DataManager_GetIntValue(TW_IS_ENCRYPTED) == 0 && OpenRecoveryScript::check_for_script_file()) {
931 gui_console_only();
932 OpenRecoveryScript::run_script_file();
933 if (1 || OpenRecoveryScript::run_script_file() != 0) {
934 // There was an error, boot the recovery
935 gui_start();
936 } else {
937 usleep(2000000); // Sleep for 2 seconds before rebooting
938 }
939 } else
940 gui_start();
Dees_Troy7d15c252012-09-05 20:47:21 -0400941 //prompt_and_wait(device);
Doug Zongker8674a722010-09-15 11:08:23 -0700942 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800943
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800944 // Otherwise, get ready to boot the main system...
945 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -0700946 ui->Print("Rebooting...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400947#ifdef ANDROID_RB_RESTART
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800948 android_reboot(ANDROID_RB_RESTART, 0, 0);
Dees_Troy38bd7602012-09-14 13:33:53 -0400949#else
950 reboot(RB_AUTOBOOT);
951#endif
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800952 return EXIT_SUCCESS;
953}