blob: 2541e54d8a3346410d015a202e545030939c93f1 [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"
Ken Sumrall6e4472a2011-03-07 23:37:27 -080035#include "cutils/android_reboot.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080036#include "install.h"
37#include "minui/minui.h"
38#include "minzip/DirUtil.h"
39#include "roots.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070040#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070041#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070042#include "device.h"
Doug Zongker9270a202012-01-09 15:16:13 -080043#include "adb_install.h"
44extern "C" {
45#include "minadbd/adb.h"
46}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080047
Stephen Smalley779701d2012-02-09 14:13:23 -050048struct selabel_handle *sehandle;
49
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050static const struct option OPTIONS[] = {
51 { "send_intent", required_argument, NULL, 's' },
52 { "update_package", required_argument, NULL, 'u' },
53 { "wipe_data", no_argument, NULL, 'w' },
54 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070055 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070056 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker02ec6b82012-08-22 17:26:40 -070057 { "locale", required_argument, NULL, 'l' },
Doug Zongker988500b2009-10-06 14:41:38 -070058 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080059};
60
Doug Zongkerd4208f92010-09-20 12:16:13 -070061static const char *COMMAND_FILE = "/cache/recovery/command";
62static const char *INTENT_FILE = "/cache/recovery/intent";
63static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070064static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070065static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -070066static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Michael Ward9d2629c2011-06-23 22:14:24 -070067static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070068static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080069static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070070static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070071static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072
Doug Zongker211aebc2011-10-28 15:13:10 -070073RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070074char* locale = NULL;
Doug Zongker211aebc2011-10-28 15:13:10 -070075
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080076/*
77 * The recovery tool communicates with the main system through /cache files.
78 * /cache/recovery/command - INPUT - command line for tool, one arg per line
79 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
80 * /cache/recovery/intent - OUTPUT - intent that was passed in
81 *
82 * The arguments which may be supplied in the recovery.command file:
83 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070084 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080085 * --wipe_data - erase user data (and cache), then reboot
86 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080087 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -070088 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089 *
90 * After completing, we remove /cache/recovery/command and reboot.
91 * Arguments may also be supplied in the bootloader control block (BCB).
92 * These important scenarios must be safely restartable at any point:
93 *
94 * FACTORY RESET
95 * 1. user selects "factory reset"
96 * 2. main system writes "--wipe_data" to /cache/recovery/command
97 * 3. main system reboots into recovery
98 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
99 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700100 * 5. erase_volume() reformats /data
101 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800102 * 7. finish_recovery() erases BCB
103 * -- after this, rebooting will restart the main system --
104 * 8. main() calls reboot() to boot main system
105 *
106 * OTA INSTALL
107 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700108 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800109 * 3. main system reboots into recovery
110 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
111 * -- after this, rebooting will attempt to reinstall the update --
112 * 5. install_package() attempts to install the update
113 * NOTE: the package install must itself be restartable from any point
114 * 6. finish_recovery() erases BCB
115 * -- after this, rebooting will (try to) restart the main system --
116 * 7. ** if install failed **
117 * 7a. prompt_and_wait() shows an error icon and waits for the user
118 * 7b; the user reboots (pulling the battery, etc) into the main system
119 * 8. main() calls maybe_install_firmware_update()
120 * ** if the update contained radio/hboot firmware **:
121 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
122 * -- after this, rebooting will reformat cache & restart main system --
123 * 8b. m_i_f_u() writes firmware image into raw cache partition
124 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
125 * -- after this, rebooting will attempt to reinstall firmware --
126 * 8d. bootloader tries to flash firmware
127 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
128 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700129 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800130 * 8g. finish_recovery() erases BCB
131 * -- after this, rebooting will (try to) restart the main system --
132 * 9. main() calls reboot() to boot main system
133 */
134
135static const int MAX_ARG_LENGTH = 4096;
136static const int MAX_ARGS = 100;
137
Doug Zongkerd4208f92010-09-20 12:16:13 -0700138// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700139FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700140fopen_path(const char *path, const char *mode) {
141 if (ensure_path_mounted(path) != 0) {
142 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800143 return NULL;
144 }
145
146 // When writing, try to create the containing directory, if necessary.
147 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500148 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800149
150 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800151 return fp;
152}
153
154// close a file, log an error if the error indicator is set
155static void
156check_and_fclose(FILE *fp, const char *name) {
157 fflush(fp);
158 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
159 fclose(fp);
160}
161
162// command line args come from, in decreasing precedence:
163// - the actual command line
164// - the bootloader control block (one per line, after "recovery")
165// - the contents of COMMAND_FILE (one per line)
166static void
167get_args(int *argc, char ***argv) {
168 struct bootloader_message boot;
169 memset(&boot, 0, sizeof(boot));
170 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
171
172 if (boot.command[0] != 0 && boot.command[0] != 255) {
173 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
174 }
175
176 if (boot.status[0] != 0 && boot.status[0] != 255) {
177 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
178 }
179
180 // --- if arguments weren't supplied, look in the bootloader control block
181 if (*argc <= 1) {
182 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
183 const char *arg = strtok(boot.recovery, "\n");
184 if (arg != NULL && !strcmp(arg, "recovery")) {
185 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
186 (*argv)[0] = strdup(arg);
187 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
188 if ((arg = strtok(NULL, "\n")) == NULL) break;
189 (*argv)[*argc] = strdup(arg);
190 }
191 LOGI("Got arguments from boot message\n");
192 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
193 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
194 }
195 }
196
197 // --- if that doesn't work, try the command file
198 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700199 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800201 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800202 char *argv0 = (*argv)[0];
203 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
204 (*argv)[0] = argv0; // use the same program name
205
206 char buf[MAX_ARG_LENGTH];
207 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
208 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800209 token = strtok(buf, "\r\n");
210 if (token != NULL) {
211 (*argv)[*argc] = strdup(token); // Strip newline.
212 } else {
213 --*argc;
214 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800215 }
216
217 check_and_fclose(fp, COMMAND_FILE);
218 LOGI("Got arguments from %s\n", COMMAND_FILE);
219 }
220 }
221
222 // --> write the arguments we have back into the bootloader control block
223 // always boot into recovery after this (until finish_recovery() is called)
224 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
225 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
226 int i;
227 for (i = 1; i < *argc; ++i) {
228 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
229 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
230 }
231 set_bootloader_message(&boot);
232}
233
Doug Zongker34c98df2009-08-18 12:05:45 -0700234static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800235set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700236 struct bootloader_message boot;
237 memset(&boot, 0, sizeof(boot));
238 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
239 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
240 set_bootloader_message(&boot);
241}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800242
Doug Zongker2c3539e2010-09-29 13:21:30 -0700243// How much of the temp log we have copied to the copy in cache.
244static long tmplog_offset = 0;
245
246static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700247copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700248 FILE *log = fopen_path(destination, append ? "a" : "w");
249 if (log == NULL) {
250 LOGE("Can't open %s\n", destination);
251 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700252 FILE *tmplog = fopen(source, "r");
253 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700254 if (append) {
255 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
256 }
257 char buf[4096];
258 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
259 if (append) {
260 tmplog_offset = ftell(tmplog);
261 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700262 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700263 }
264 check_and_fclose(log, destination);
265 }
266}
267
268
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800269// clear the recovery command and prepare to boot a (hopefully working) system,
270// copy our log file to cache as well (for the system to read), and
271// record any intent we were asked to communicate back to the system.
272// this function is idempotent: call it as many times as you like.
273static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800274finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800275 // By this point, we're ready to return to the main system...
276 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700277 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000278 if (fp == NULL) {
279 LOGE("Can't open %s\n", INTENT_FILE);
280 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800281 fputs(send_intent, fp);
282 check_and_fclose(fp, INTENT_FILE);
283 }
284 }
285
Doug Zongker4f33e552012-08-23 13:16:12 -0700286 // Save the locale to cache, so if recovery is next started up
287 // without a --locale argument (eg, directly from the bootloader)
288 // it will use the last-known locale.
289 if (locale != NULL) {
290 LOGI("Saving locale \"%s\"\n", locale);
291 FILE* fp = fopen_path(LOCALE_FILE, "w");
292 fwrite(locale, 1, strlen(locale), fp);
293 fflush(fp);
294 fsync(fileno(fp));
295 check_and_fclose(fp, LOCALE_FILE);
296 }
297
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800298 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700299 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
300 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
301 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
302 chmod(LOG_FILE, 0600);
303 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700304 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700305 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700307 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800308 struct bootloader_message boot;
309 memset(&boot, 0, sizeof(boot));
310 set_bootloader_message(&boot);
311
312 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700313 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
314 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800315 LOGW("Can't unlink %s\n", COMMAND_FILE);
316 }
317
Doug Zongkerd0181b82011-10-19 10:51:12 -0700318 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800319 sync(); // For good measure.
320}
321
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800322static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700323erase_volume(const char *volume) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700324 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700325 ui->SetProgressType(RecoveryUI::INDETERMINATE);
326 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700327
Doug Zongkerd0181b82011-10-19 10:51:12 -0700328 ensure_path_unmounted(volume);
329
Doug Zongker2c3539e2010-09-29 13:21:30 -0700330 if (strcmp(volume, "/cache") == 0) {
331 // Any part of the log we'd copied to cache is now gone.
332 // Reset the pointer so we copy from the beginning of the temp
333 // log.
334 tmplog_offset = 0;
335 }
336
Doug Zongkerd4208f92010-09-20 12:16:13 -0700337 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800338}
339
Doug Zongker23ceeea2010-07-08 17:27:55 -0700340static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700341copy_sideloaded_package(const char* original_path) {
342 if (ensure_path_mounted(original_path) != 0) {
343 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700344 return NULL;
345 }
346
Doug Zongkerd4208f92010-09-20 12:16:13 -0700347 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700348 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
349 return NULL;
350 }
351
Doug Zongkerd4208f92010-09-20 12:16:13 -0700352 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700353 if (errno != EEXIST) {
354 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
355 return NULL;
356 }
357 }
358
Doug Zongkerd4208f92010-09-20 12:16:13 -0700359 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
360 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700361 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700362 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
363 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700364 return NULL;
365 }
366 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700367 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700368 return NULL;
369 }
370 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700371 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700372 return NULL;
373 }
374 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700375 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700376 return NULL;
377 }
378
Doug Zongkerd4208f92010-09-20 12:16:13 -0700379 char copy_path[PATH_MAX];
380 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700381 strcat(copy_path, "/package.zip");
382
Doug Zongker28ce47c2011-10-28 10:33:05 -0700383 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700384 if (buffer == NULL) {
385 LOGE("Failed to allocate buffer\n");
386 return NULL;
387 }
388
389 size_t read;
390 FILE* fin = fopen(original_path, "rb");
391 if (fin == NULL) {
392 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
393 return NULL;
394 }
395 FILE* fout = fopen(copy_path, "wb");
396 if (fout == NULL) {
397 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
398 return NULL;
399 }
400
401 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
402 if (fwrite(buffer, 1, read, fout) != read) {
403 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
404 return NULL;
405 }
406 }
407
408 free(buffer);
409
410 if (fclose(fout) != 0) {
411 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
412 return NULL;
413 }
414
415 if (fclose(fin) != 0) {
416 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
417 return NULL;
418 }
419
420 // "adb push" is happy to overwrite read-only files when it's
421 // running as root, but we'll try anyway.
422 if (chmod(copy_path, 0400) != 0) {
423 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
424 return NULL;
425 }
426
Doug Zongkerd4208f92010-09-20 12:16:13 -0700427 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700428}
429
Doug Zongker28ce47c2011-10-28 10:33:05 -0700430static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700431prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700432 const char* title[] = { "Android system recovery <"
433 EXPAND(RECOVERY_API_VERSION) "e>",
434 "",
435 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800436
Doug Zongkerd6837852009-06-17 22:07:13 -0700437 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700438 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700439 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700440 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700441 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700442 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700443
Doug Zongker28ce47c2011-10-28 10:33:05 -0700444 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
445 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700446 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700447 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700448 *h = NULL;
449
Doug Zongkerf93d8162009-09-22 15:16:02 -0700450 return new_headers;
451}
452
453static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700454get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700455 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700456 // throw away keys pressed previously, so user doesn't
457 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700458 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700459
Doug Zongker211aebc2011-10-28 15:13:10 -0700460 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700461 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800462 int chosen_item = -1;
463
Doug Zongkerf93d8162009-09-22 15:16:02 -0700464 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700465 int key = ui->WaitKey();
466 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800467
Doug Zongker5cae4452011-01-25 13:15:30 -0800468 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700469 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800470 continue;
471 } else {
472 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700473 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700474 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800475 }
476 }
477
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700478 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700479
480 if (action < 0) {
481 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700482 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700483 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700484 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700485 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700486 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700487 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700488 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700489 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700490 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700491 chosen_item = selected;
492 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700493 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700494 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700496 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700497 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800498 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700499 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800500
Doug Zongker211aebc2011-10-28 15:13:10 -0700501 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700502 return chosen_item;
503}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800504
Doug Zongker8674a722010-09-15 11:08:23 -0700505static int compare_string(const void* a, const void* b) {
506 return strcmp(*(const char**)a, *(const char**)b);
507}
508
509static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700510update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700511 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700512 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700513
Doug Zongker8674a722010-09-15 11:08:23 -0700514 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700515 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700516 "",
517 NULL };
518 DIR* d;
519 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700520 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700521 if (d == NULL) {
522 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700523 if (unmount_when_done != NULL) {
524 ensure_path_unmounted(unmount_when_done);
525 }
Doug Zongker8674a722010-09-15 11:08:23 -0700526 return 0;
527 }
528
Doug Zongker28ce47c2011-10-28 10:33:05 -0700529 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700530
531 int d_size = 0;
532 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700533 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700534 int z_size = 1;
535 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700536 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700537 zips[0] = strdup("../");
538
539 while ((de = readdir(d)) != NULL) {
540 int name_len = strlen(de->d_name);
541
542 if (de->d_type == DT_DIR) {
543 // skip "." and ".." entries
544 if (name_len == 1 && de->d_name[0] == '.') continue;
545 if (name_len == 2 && de->d_name[0] == '.' &&
546 de->d_name[1] == '.') continue;
547
548 if (d_size >= d_alloc) {
549 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700550 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700551 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700552 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700553 strcpy(dirs[d_size], de->d_name);
554 dirs[d_size][name_len] = '/';
555 dirs[d_size][name_len+1] = '\0';
556 ++d_size;
557 } else if (de->d_type == DT_REG &&
558 name_len >= 4 &&
559 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
560 if (z_size >= z_alloc) {
561 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700562 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700563 }
564 zips[z_size++] = strdup(de->d_name);
565 }
566 }
567 closedir(d);
568
569 qsort(dirs, d_size, sizeof(char*), compare_string);
570 qsort(zips, z_size, sizeof(char*), compare_string);
571
572 // append dirs to the zips list
573 if (d_size + z_size + 1 > z_alloc) {
574 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700575 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700576 }
577 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
578 free(dirs);
579 z_size += d_size;
580 zips[z_size] = NULL;
581
582 int result;
583 int chosen_item = 0;
584 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700585 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700586
587 char* item = zips[chosen_item];
588 int item_len = strlen(item);
589 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700590 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700591 result = -1;
592 break;
593 } else if (item[item_len-1] == '/') {
594 // recurse down into a subdirectory
595 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700596 strlcpy(new_path, path, PATH_MAX);
597 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700598 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700599 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700600 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700601 if (result >= 0) break;
602 } else {
603 // selected a zip file: attempt to install it, and return
604 // the status to the caller.
605 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700606 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700607 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700608 strlcat(new_path, item, PATH_MAX);
609
Doug Zongker211aebc2011-10-28 15:13:10 -0700610 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700611 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700612 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700613 if (unmount_when_done != NULL) {
614 ensure_path_unmounted(unmount_when_done);
615 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700616 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700617 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700618 free(copy);
619 } else {
620 result = INSTALL_ERROR;
621 }
Doug Zongker8674a722010-09-15 11:08:23 -0700622 break;
623 }
624 } while (true);
625
626 int i;
627 for (i = 0; i < z_size; ++i) free(zips[i]);
628 free(zips);
629 free(headers);
630
Michael Ward9d2629c2011-06-23 22:14:24 -0700631 if (unmount_when_done != NULL) {
632 ensure_path_unmounted(unmount_when_done);
633 }
Doug Zongker8674a722010-09-15 11:08:23 -0700634 return result;
635}
636
Doug Zongkerf93d8162009-09-22 15:16:02 -0700637static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700638wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700639 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700640 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700641
Doug Zongkerf93d8162009-09-22 15:16:02 -0700642 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700643 const char* headers[] = { "Confirm wipe of all user data?",
644 " THIS CAN NOT BE UNDONE.",
645 "",
646 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700647 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700648 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800649
Doug Zongker28ce47c2011-10-28 10:33:05 -0700650 const char* items[] = { " No",
651 " No",
652 " No",
653 " No",
654 " No",
655 " No",
656 " No",
657 " Yes -- delete all user data", // [7]
658 " No",
659 " No",
660 " No",
661 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800662
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700663 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700664 if (chosen_item != 7) {
665 return;
666 }
667 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700668
Doug Zongker211aebc2011-10-28 15:13:10 -0700669 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700670 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700671 erase_volume("/data");
672 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700673 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700674}
675
676static void
Doug Zongker6c8553d2012-09-24 10:40:47 -0700677prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700678 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700679
680 for (;;) {
681 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700682 switch (status) {
683 case INSTALL_SUCCESS:
684 case INSTALL_NONE:
685 ui->SetBackground(RecoveryUI::NO_COMMAND);
686 break;
687
688 case INSTALL_ERROR:
689 case INSTALL_CORRUPT:
690 ui->SetBackground(RecoveryUI::ERROR);
691 break;
692 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700693 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700694
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700695 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700696
697 // device-specific code may take some action here. It may
698 // return one of the core actions handled in the switch
699 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700700 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700701
Doug Zongkerd0181b82011-10-19 10:51:12 -0700702 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700703 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700704 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700705 return;
706
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700707 case Device::WIPE_DATA:
708 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700709 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700710 break;
711
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700712 case Device::WIPE_CACHE:
Doug Zongker02ec6b82012-08-22 17:26:40 -0700713 ui->ShowText(false);
Doug Zongker211aebc2011-10-28 15:13:10 -0700714 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700715 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700716 ui->Print("Cache wipe complete.\n");
717 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700718 break;
719
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700720 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800721 // Some packages expect /cache to be mounted (eg,
722 // standard incremental packages expect to use /cache
723 // as scratch space).
724 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700725 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700726 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700727 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700728 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700729 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700730 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700731 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700732 }
733 }
Doug Zongker8674a722010-09-15 11:08:23 -0700734 if (status >= 0) {
735 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700736 ui->SetBackground(RecoveryUI::ERROR);
737 ui->Print("Installation aborted.\n");
738 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700739 return; // reboot if logs aren't visible
740 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700741 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700742 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700743 }
744 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700745
746 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700747 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700748 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700749 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700750 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700751 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700752 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700753 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700754 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700755 }
756 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700757 if (status >= 0) {
758 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700759 ui->SetBackground(RecoveryUI::ERROR);
760 ui->Print("Installation aborted.\n");
761 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700762 return; // reboot if logs aren't visible
763 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700764 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700765 }
766 }
767 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800768
769 case Device::APPLY_ADB_SIDELOAD:
770 ensure_path_mounted(CACHE_ROOT);
771 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
772 if (status >= 0) {
773 if (status != INSTALL_SUCCESS) {
774 ui->SetBackground(RecoveryUI::ERROR);
775 ui->Print("Installation aborted.\n");
776 } else if (!ui->IsTextVisible()) {
777 return; // reboot if logs aren't visible
778 } else {
779 ui->Print("\nInstall from ADB complete.\n");
780 }
781 }
782 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800783 }
784 }
785}
786
787static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800788print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700789 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800790}
791
Doug Zongker02ec6b82012-08-22 17:26:40 -0700792static void
793load_locale_from_cache() {
794 FILE* fp = fopen_path(LOCALE_FILE, "r");
795 char buffer[80];
796 if (fp != NULL) {
797 fgets(buffer, sizeof(buffer), fp);
798 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700799 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700800 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
801 if (!isspace(buffer[i])) {
802 buffer[j++] = buffer[i];
803 }
804 }
805 buffer[j] = 0;
806 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700807 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700808 }
809}
810
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800811int
Oscar Montemayor05231562009-11-30 08:40:57 -0800812main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800813 time_t start = time(NULL);
814
815 // If these fail, there's not really anywhere to complain...
816 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
817 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800818
819 // If this binary is started with the single argument "--adbd",
820 // instead of being the normal recovery binary, it turns into kind
821 // of a stripped-down version of adbd that only supports the
822 // 'sideload' command. Note this must be a real argument, not
823 // anything in the command file or bootloader control block; the
824 // only way recovery should be run with this argument is when it
825 // starts a copy of itself from the apply_from_adb() function.
826 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
827 adb_main();
828 return 0;
829 }
830
Doug Zongker56c51052010-07-01 09:18:44 -0700831 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800832
Doug Zongkerd4208f92010-09-20 12:16:13 -0700833 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800834 get_args(&argc, &argv);
835
836 int previous_runs = 0;
837 const char *send_intent = NULL;
838 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700839 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700840 bool just_exit = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800841
842 int arg;
843 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
844 switch (arg) {
845 case 'p': previous_runs = atoi(optarg); break;
846 case 's': send_intent = optarg; break;
847 case 'u': update_package = optarg; break;
848 case 'w': wipe_data = wipe_cache = 1; break;
849 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700850 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700851 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700852 case 'l': locale = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800853 case '?':
854 LOGE("Invalid command argument\n");
855 continue;
856 }
857 }
858
Doug Zongker02ec6b82012-08-22 17:26:40 -0700859 if (locale == NULL) {
860 load_locale_from_cache();
861 }
862 printf("locale is [%s]\n", locale);
863
864 Device* device = make_device();
865 ui = device->GetUI();
866
867 ui->Init();
Doug Zongker5fa8c232012-09-18 12:37:02 -0700868 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700869 ui->SetBackground(RecoveryUI::NONE);
870 if (show_text) ui->ShowText(true);
871
Stephen Smalley779701d2012-02-09 14:13:23 -0500872 struct selinux_opt seopts[] = {
873 { SELABEL_OPT_PATH, "/file_contexts" }
874 };
875
876 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
877
878 if (!sehandle) {
879 fprintf(stderr, "Warning: No file_contexts\n");
Kenny Root038818c2012-04-08 11:03:01 -0700880 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500881 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500882
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700883 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800884
Doug Zongker56c51052010-07-01 09:18:44 -0700885 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800886 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700887 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800888 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700889 printf("\n");
890
891 if (update_package) {
892 // For backwards compatibility on the cache partition only, if
893 // we're given an old 'root' path "CACHE:foo", change it to
894 // "/cache/foo".
895 if (strncmp(update_package, "CACHE:", 6) == 0) {
896 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700897 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700898 strlcpy(modified_path, "/cache/", len);
899 strlcat(modified_path, update_package+6, len);
900 printf("(replacing path \"%s\" with \"%s\")\n",
901 update_package, modified_path);
902 update_package = modified_path;
903 }
904 }
905 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800906
907 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700908 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800909
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800910 int status = INSTALL_SUCCESS;
911
Doug Zongker540d57f2011-01-18 13:36:58 -0800912 if (update_package != NULL) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700913 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
914 if (status == INSTALL_SUCCESS && wipe_cache) {
915 if (erase_volume("/cache")) {
916 LOGE("Cache wipe (requested by package) failed.");
917 }
918 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700919 if (status != INSTALL_SUCCESS) ui->Print("Installation aborted.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700920 } else if (wipe_data) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700921 if (device->WipeData()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700922 if (erase_volume("/data")) status = INSTALL_ERROR;
923 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700924 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700925 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700926 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700927 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700928 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700929 status = INSTALL_NONE; // No command specified
930 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800931 }
932
Doug Zongker02ec6b82012-08-22 17:26:40 -0700933 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
934 ui->SetBackground(RecoveryUI::ERROR);
935 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700936 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker6c8553d2012-09-24 10:40:47 -0700937 prompt_and_wait(device, status);
Doug Zongker8674a722010-09-15 11:08:23 -0700938 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800939
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800940 // Otherwise, get ready to boot the main system...
941 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -0700942 ui->Print("Rebooting...\n");
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800943 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800944 return EXIT_SUCCESS;
945}