blob: a84d8333ac7ab0234f2ed318d0817ab119e0334d [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>
Doug Zongker7c3ae452013-05-14 11:03:02 -070018#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
22#include <limits.h>
23#include <linux/input.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070024#include <stdarg.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070028#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include <sys/types.h>
30#include <time.h>
31#include <unistd.h>
32
33#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include "common.h"
35#include "cutils/properties.h"
Ken Sumrall6e4472a2011-03-07 23:37:27 -080036#include "cutils/android_reboot.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037#include "install.h"
38#include "minui/minui.h"
39#include "minzip/DirUtil.h"
40#include "roots.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070041#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070042#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070043#include "device.h"
Doug Zongker9270a202012-01-09 15:16:13 -080044#include "adb_install.h"
45extern "C" {
46#include "minadbd/adb.h"
47}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048
Stephen Smalley779701d2012-02-09 14:13:23 -050049struct selabel_handle *sehandle;
50
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051static const struct option OPTIONS[] = {
52 { "send_intent", required_argument, NULL, 's' },
53 { "update_package", required_argument, NULL, 'u' },
54 { "wipe_data", no_argument, NULL, 'w' },
55 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070056 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070057 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker02ec6b82012-08-22 17:26:40 -070058 { "locale", required_argument, NULL, 'l' },
Doug Zongker988500b2009-10-06 14:41:38 -070059 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060};
61
Doug Zongkerd4208f92010-09-20 12:16:13 -070062static const char *COMMAND_FILE = "/cache/recovery/command";
63static const char *INTENT_FILE = "/cache/recovery/intent";
64static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongker2c3539e2010-09-29 13:21:30 -070065static const char *LAST_LOG_FILE = "/cache/recovery/last_log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070066static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -070067static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Michael Ward9d2629c2011-06-23 22:14:24 -070068static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070069static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080070static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070071static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070072static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080073
Doug Zongker211aebc2011-10-28 15:13:10 -070074RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070075char* locale = NULL;
Doug Zongker211aebc2011-10-28 15:13:10 -070076
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077/*
78 * The recovery tool communicates with the main system through /cache files.
79 * /cache/recovery/command - INPUT - command line for tool, one arg per line
80 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
81 * /cache/recovery/intent - OUTPUT - intent that was passed in
82 *
83 * The arguments which may be supplied in the recovery.command file:
84 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070085 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086 * --wipe_data - erase user data (and cache), then reboot
87 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080088 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -070089 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080090 *
91 * After completing, we remove /cache/recovery/command and reboot.
92 * Arguments may also be supplied in the bootloader control block (BCB).
93 * These important scenarios must be safely restartable at any point:
94 *
95 * FACTORY RESET
96 * 1. user selects "factory reset"
97 * 2. main system writes "--wipe_data" to /cache/recovery/command
98 * 3. main system reboots into recovery
99 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
100 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700101 * 5. erase_volume() reformats /data
102 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800103 * 7. finish_recovery() erases BCB
104 * -- after this, rebooting will restart the main system --
105 * 8. main() calls reboot() to boot main system
106 *
107 * OTA INSTALL
108 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700109 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800110 * 3. main system reboots into recovery
111 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
112 * -- after this, rebooting will attempt to reinstall the update --
113 * 5. install_package() attempts to install the update
114 * NOTE: the package install must itself be restartable from any point
115 * 6. finish_recovery() erases BCB
116 * -- after this, rebooting will (try to) restart the main system --
117 * 7. ** if install failed **
118 * 7a. prompt_and_wait() shows an error icon and waits for the user
119 * 7b; the user reboots (pulling the battery, etc) into the main system
120 * 8. main() calls maybe_install_firmware_update()
121 * ** if the update contained radio/hboot firmware **:
122 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
123 * -- after this, rebooting will reformat cache & restart main system --
124 * 8b. m_i_f_u() writes firmware image into raw cache partition
125 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
126 * -- after this, rebooting will attempt to reinstall firmware --
127 * 8d. bootloader tries to flash firmware
128 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
129 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700130 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800131 * 8g. finish_recovery() erases BCB
132 * -- after this, rebooting will (try to) restart the main system --
133 * 9. main() calls reboot() to boot main system
134 */
135
136static const int MAX_ARG_LENGTH = 4096;
137static const int MAX_ARGS = 100;
138
Doug Zongkerd4208f92010-09-20 12:16:13 -0700139// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700140FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700141fopen_path(const char *path, const char *mode) {
142 if (ensure_path_mounted(path) != 0) {
143 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800144 return NULL;
145 }
146
147 // When writing, try to create the containing directory, if necessary.
148 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500149 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150
151 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152 return fp;
153}
154
155// close a file, log an error if the error indicator is set
156static void
157check_and_fclose(FILE *fp, const char *name) {
158 fflush(fp);
159 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
160 fclose(fp);
161}
162
163// command line args come from, in decreasing precedence:
164// - the actual command line
165// - the bootloader control block (one per line, after "recovery")
166// - the contents of COMMAND_FILE (one per line)
167static void
168get_args(int *argc, char ***argv) {
169 struct bootloader_message boot;
170 memset(&boot, 0, sizeof(boot));
171 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
172
173 if (boot.command[0] != 0 && boot.command[0] != 255) {
174 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
175 }
176
177 if (boot.status[0] != 0 && boot.status[0] != 255) {
178 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
179 }
180
181 // --- if arguments weren't supplied, look in the bootloader control block
182 if (*argc <= 1) {
183 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
184 const char *arg = strtok(boot.recovery, "\n");
185 if (arg != NULL && !strcmp(arg, "recovery")) {
186 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
187 (*argv)[0] = strdup(arg);
188 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
189 if ((arg = strtok(NULL, "\n")) == NULL) break;
190 (*argv)[*argc] = strdup(arg);
191 }
192 LOGI("Got arguments from boot message\n");
193 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
194 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
195 }
196 }
197
198 // --- if that doesn't work, try the command file
199 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700200 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800201 if (fp != NULL) {
202 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;
209 (*argv)[*argc] = strdup(strtok(buf, "\r\n")); // Strip newline.
210 }
211
212 check_and_fclose(fp, COMMAND_FILE);
213 LOGI("Got arguments from %s\n", COMMAND_FILE);
214 }
215 }
216
217 // --> write the arguments we have back into the bootloader control block
218 // always boot into recovery after this (until finish_recovery() is called)
219 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
220 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
221 int i;
222 for (i = 1; i < *argc; ++i) {
223 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
224 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
225 }
226 set_bootloader_message(&boot);
227}
228
Doug Zongker34c98df2009-08-18 12:05:45 -0700229static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800230set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700231 struct bootloader_message boot;
232 memset(&boot, 0, sizeof(boot));
233 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
234 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
235 set_bootloader_message(&boot);
236}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237
Doug Zongker2c3539e2010-09-29 13:21:30 -0700238// How much of the temp log we have copied to the copy in cache.
239static long tmplog_offset = 0;
240
241static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700242copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700243 FILE *log = fopen_path(destination, append ? "a" : "w");
244 if (log == NULL) {
245 LOGE("Can't open %s\n", destination);
246 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700247 FILE *tmplog = fopen(source, "r");
248 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700249 if (append) {
250 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
251 }
252 char buf[4096];
253 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
254 if (append) {
255 tmplog_offset = ftell(tmplog);
256 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700257 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700258 }
259 check_and_fclose(log, destination);
260 }
261}
262
263
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800264// clear the recovery command and prepare to boot a (hopefully working) system,
265// copy our log file to cache as well (for the system to read), and
266// record any intent we were asked to communicate back to the system.
267// this function is idempotent: call it as many times as you like.
268static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800269finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800270 // By this point, we're ready to return to the main system...
271 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700272 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000273 if (fp == NULL) {
274 LOGE("Can't open %s\n", INTENT_FILE);
275 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800276 fputs(send_intent, fp);
277 check_and_fclose(fp, INTENT_FILE);
278 }
279 }
280
Doug Zongker4f33e552012-08-23 13:16:12 -0700281 // Save the locale to cache, so if recovery is next started up
282 // without a --locale argument (eg, directly from the bootloader)
283 // it will use the last-known locale.
284 if (locale != NULL) {
285 LOGI("Saving locale \"%s\"\n", locale);
286 FILE* fp = fopen_path(LOCALE_FILE, "w");
287 fwrite(locale, 1, strlen(locale), fp);
288 fflush(fp);
289 fsync(fileno(fp));
290 check_and_fclose(fp, LOCALE_FILE);
291 }
292
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800293 // Copy logs to cache so the system can find out what happened.
Doug Zongkerd0181b82011-10-19 10:51:12 -0700294 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
295 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
296 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
297 chmod(LOG_FILE, 0600);
298 chown(LOG_FILE, 1000, 1000); // system user
Doug Zongker2c3539e2010-09-29 13:21:30 -0700299 chmod(LAST_LOG_FILE, 0640);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700300 chmod(LAST_INSTALL_FILE, 0644);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800301
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700302 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800303 struct bootloader_message boot;
304 memset(&boot, 0, sizeof(boot));
305 set_bootloader_message(&boot);
306
307 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700308 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
309 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800310 LOGW("Can't unlink %s\n", COMMAND_FILE);
311 }
312
Doug Zongkerd0181b82011-10-19 10:51:12 -0700313 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800314 sync(); // For good measure.
315}
316
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800317static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700318erase_volume(const char *volume) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700319 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700320 ui->SetProgressType(RecoveryUI::INDETERMINATE);
321 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700322
Doug Zongkerd0181b82011-10-19 10:51:12 -0700323 ensure_path_unmounted(volume);
324
Doug Zongker2c3539e2010-09-29 13:21:30 -0700325 if (strcmp(volume, "/cache") == 0) {
326 // Any part of the log we'd copied to cache is now gone.
327 // Reset the pointer so we copy from the beginning of the temp
328 // log.
329 tmplog_offset = 0;
330 }
331
Doug Zongkerd4208f92010-09-20 12:16:13 -0700332 return format_volume(volume);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800333}
334
Doug Zongker23ceeea2010-07-08 17:27:55 -0700335static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700336copy_sideloaded_package(const char* original_path) {
337 if (ensure_path_mounted(original_path) != 0) {
338 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700339 return NULL;
340 }
341
Doug Zongkerd4208f92010-09-20 12:16:13 -0700342 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700343 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
344 return NULL;
345 }
346
Doug Zongkerd4208f92010-09-20 12:16:13 -0700347 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700348 if (errno != EEXIST) {
349 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
350 return NULL;
351 }
352 }
353
Doug Zongkerd4208f92010-09-20 12:16:13 -0700354 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
355 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700356 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700357 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
358 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700359 return NULL;
360 }
361 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700362 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700363 return NULL;
364 }
365 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700366 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700367 return NULL;
368 }
369 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700370 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700371 return NULL;
372 }
373
Doug Zongkerd4208f92010-09-20 12:16:13 -0700374 char copy_path[PATH_MAX];
375 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700376 strcat(copy_path, "/package.zip");
377
Doug Zongker28ce47c2011-10-28 10:33:05 -0700378 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700379 if (buffer == NULL) {
380 LOGE("Failed to allocate buffer\n");
381 return NULL;
382 }
383
384 size_t read;
385 FILE* fin = fopen(original_path, "rb");
386 if (fin == NULL) {
387 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
388 return NULL;
389 }
390 FILE* fout = fopen(copy_path, "wb");
391 if (fout == NULL) {
392 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
393 return NULL;
394 }
395
396 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
397 if (fwrite(buffer, 1, read, fout) != read) {
398 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
399 return NULL;
400 }
401 }
402
403 free(buffer);
404
405 if (fclose(fout) != 0) {
406 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
407 return NULL;
408 }
409
410 if (fclose(fin) != 0) {
411 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
412 return NULL;
413 }
414
415 // "adb push" is happy to overwrite read-only files when it's
416 // running as root, but we'll try anyway.
417 if (chmod(copy_path, 0400) != 0) {
418 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
419 return NULL;
420 }
421
Doug Zongkerd4208f92010-09-20 12:16:13 -0700422 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700423}
424
Doug Zongker28ce47c2011-10-28 10:33:05 -0700425static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700426prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700427 const char* title[] = { "Android system recovery <"
428 EXPAND(RECOVERY_API_VERSION) "e>",
429 "",
430 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800431
Doug Zongkerd6837852009-06-17 22:07:13 -0700432 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700433 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700434 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700435 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700436 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700437 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700438
Doug Zongker28ce47c2011-10-28 10:33:05 -0700439 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
440 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700441 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700442 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700443 *h = NULL;
444
Doug Zongkerf93d8162009-09-22 15:16:02 -0700445 return new_headers;
446}
447
448static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700449get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700450 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700451 // throw away keys pressed previously, so user doesn't
452 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700453 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700454
Doug Zongker211aebc2011-10-28 15:13:10 -0700455 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700456 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800457 int chosen_item = -1;
458
Doug Zongkerf93d8162009-09-22 15:16:02 -0700459 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700460 int key = ui->WaitKey();
461 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800462
Doug Zongker5cae4452011-01-25 13:15:30 -0800463 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700464 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800465 continue;
466 } else {
467 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700468 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700469 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800470 }
471 }
472
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700473 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700474
475 if (action < 0) {
476 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700477 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700478 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700479 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700480 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700481 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700482 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700483 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700484 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700485 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700486 chosen_item = selected;
487 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700488 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700489 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800490 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700491 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700492 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800493 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700494 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495
Doug Zongker211aebc2011-10-28 15:13:10 -0700496 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700497 return chosen_item;
498}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800499
Doug Zongker8674a722010-09-15 11:08:23 -0700500static int compare_string(const void* a, const void* b) {
501 return strcmp(*(const char**)a, *(const char**)b);
502}
503
504static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700505update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700506 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700507 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700508
Doug Zongker8674a722010-09-15 11:08:23 -0700509 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700510 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700511 "",
512 NULL };
513 DIR* d;
514 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700515 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700516 if (d == NULL) {
517 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700518 if (unmount_when_done != NULL) {
519 ensure_path_unmounted(unmount_when_done);
520 }
Doug Zongker8674a722010-09-15 11:08:23 -0700521 return 0;
522 }
523
Doug Zongker28ce47c2011-10-28 10:33:05 -0700524 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700525
526 int d_size = 0;
527 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700528 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700529 int z_size = 1;
530 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700531 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700532 zips[0] = strdup("../");
533
534 while ((de = readdir(d)) != NULL) {
535 int name_len = strlen(de->d_name);
536
537 if (de->d_type == DT_DIR) {
538 // skip "." and ".." entries
539 if (name_len == 1 && de->d_name[0] == '.') continue;
540 if (name_len == 2 && de->d_name[0] == '.' &&
541 de->d_name[1] == '.') continue;
542
543 if (d_size >= d_alloc) {
544 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700545 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700546 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700547 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700548 strcpy(dirs[d_size], de->d_name);
549 dirs[d_size][name_len] = '/';
550 dirs[d_size][name_len+1] = '\0';
551 ++d_size;
552 } else if (de->d_type == DT_REG &&
553 name_len >= 4 &&
554 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
555 if (z_size >= z_alloc) {
556 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700557 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700558 }
559 zips[z_size++] = strdup(de->d_name);
560 }
561 }
562 closedir(d);
563
564 qsort(dirs, d_size, sizeof(char*), compare_string);
565 qsort(zips, z_size, sizeof(char*), compare_string);
566
567 // append dirs to the zips list
568 if (d_size + z_size + 1 > z_alloc) {
569 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700570 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700571 }
572 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
573 free(dirs);
574 z_size += d_size;
575 zips[z_size] = NULL;
576
577 int result;
578 int chosen_item = 0;
579 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700580 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700581
582 char* item = zips[chosen_item];
583 int item_len = strlen(item);
584 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700585 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700586 result = -1;
587 break;
588 } else if (item[item_len-1] == '/') {
589 // recurse down into a subdirectory
590 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700591 strlcpy(new_path, path, PATH_MAX);
592 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700593 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700594 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700595 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700596 if (result >= 0) break;
597 } else {
598 // selected a zip file: attempt to install it, and return
599 // the status to the caller.
600 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700601 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700602 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700603 strlcat(new_path, item, PATH_MAX);
604
Doug Zongker211aebc2011-10-28 15:13:10 -0700605 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700606 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700607 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700608 if (unmount_when_done != NULL) {
609 ensure_path_unmounted(unmount_when_done);
610 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700611 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700612 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700613 free(copy);
614 } else {
615 result = INSTALL_ERROR;
616 }
Doug Zongker8674a722010-09-15 11:08:23 -0700617 break;
618 }
619 } while (true);
620
621 int i;
622 for (i = 0; i < z_size; ++i) free(zips[i]);
623 free(zips);
624 free(headers);
625
Michael Ward9d2629c2011-06-23 22:14:24 -0700626 if (unmount_when_done != NULL) {
627 ensure_path_unmounted(unmount_when_done);
628 }
Doug Zongker8674a722010-09-15 11:08:23 -0700629 return result;
630}
631
Doug Zongkerf93d8162009-09-22 15:16:02 -0700632static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700633wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700634 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700635 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700636
Doug Zongkerf93d8162009-09-22 15:16:02 -0700637 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700638 const char* headers[] = { "Confirm wipe of all user data?",
639 " THIS CAN NOT BE UNDONE.",
640 "",
641 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700642 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700643 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800644
Doug Zongker28ce47c2011-10-28 10:33:05 -0700645 const char* items[] = { " No",
646 " No",
647 " No",
648 " No",
649 " No",
650 " No",
651 " No",
652 " Yes -- delete all user data", // [7]
653 " No",
654 " No",
655 " No",
656 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800657
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700658 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700659 if (chosen_item != 7) {
660 return;
661 }
662 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700663
Doug Zongker211aebc2011-10-28 15:13:10 -0700664 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700665 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700666 erase_volume("/data");
667 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700668 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700669}
670
671static void
Doug Zongker6c8553d2012-09-24 10:40:47 -0700672prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700673 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700674
675 for (;;) {
676 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700677 switch (status) {
678 case INSTALL_SUCCESS:
679 case INSTALL_NONE:
680 ui->SetBackground(RecoveryUI::NO_COMMAND);
681 break;
682
683 case INSTALL_ERROR:
684 case INSTALL_CORRUPT:
685 ui->SetBackground(RecoveryUI::ERROR);
686 break;
687 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700688 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700689
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700690 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700691
692 // device-specific code may take some action here. It may
693 // return one of the core actions handled in the switch
694 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700695 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700696
Doug Zongkerd0181b82011-10-19 10:51:12 -0700697 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700698 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700699 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700700 return;
701
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700702 case Device::WIPE_DATA:
703 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700704 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700705 break;
706
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700707 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700708 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700709 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700710 ui->Print("Cache wipe complete.\n");
711 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700712 break;
713
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700714 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800715 // Some packages expect /cache to be mounted (eg,
716 // standard incremental packages expect to use /cache
717 // as scratch space).
718 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700719 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700720 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700721 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700722 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700723 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700724 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700725 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700726 }
727 }
Doug Zongker8674a722010-09-15 11:08:23 -0700728 if (status >= 0) {
729 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700730 ui->SetBackground(RecoveryUI::ERROR);
731 ui->Print("Installation aborted.\n");
732 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700733 return; // reboot if logs aren't visible
734 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700735 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700736 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700737 }
738 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700739
740 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700741 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700742 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700743 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700744 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700745 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700746 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700747 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700748 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700749 }
750 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700751 if (status >= 0) {
752 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700753 ui->SetBackground(RecoveryUI::ERROR);
754 ui->Print("Installation aborted.\n");
755 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700756 return; // reboot if logs aren't visible
757 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700758 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700759 }
760 }
761 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800762
763 case Device::APPLY_ADB_SIDELOAD:
764 ensure_path_mounted(CACHE_ROOT);
765 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
766 if (status >= 0) {
767 if (status != INSTALL_SUCCESS) {
768 ui->SetBackground(RecoveryUI::ERROR);
769 ui->Print("Installation aborted.\n");
770 } else if (!ui->IsTextVisible()) {
771 return; // reboot if logs aren't visible
772 } else {
773 ui->Print("\nInstall from ADB complete.\n");
774 }
775 }
776 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800777 }
778 }
779}
780
781static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800782print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700783 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800784}
785
Doug Zongker02ec6b82012-08-22 17:26:40 -0700786static void
787load_locale_from_cache() {
788 FILE* fp = fopen_path(LOCALE_FILE, "r");
789 char buffer[80];
790 if (fp != NULL) {
791 fgets(buffer, sizeof(buffer), fp);
792 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700793 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700794 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
795 if (!isspace(buffer[i])) {
796 buffer[j++] = buffer[i];
797 }
798 }
799 buffer[j] = 0;
800 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700801 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700802 }
803}
804
Doug Zongker7c3ae452013-05-14 11:03:02 -0700805static RecoveryUI* gCurrentUI = NULL;
806
807void
808ui_print(const char* format, ...) {
809 char buffer[256];
810
811 va_list ap;
812 va_start(ap, format);
813 vsnprintf(buffer, sizeof(buffer), format, ap);
814 va_end(ap);
815
816 if (gCurrentUI != NULL) {
817 gCurrentUI->Print("%s", buffer);
818 } else {
819 fputs(buffer, stdout);
820 }
821}
822
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800823int
Oscar Montemayor05231562009-11-30 08:40:57 -0800824main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800825 time_t start = time(NULL);
826
827 // If these fail, there's not really anywhere to complain...
828 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
829 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800830
831 // If this binary is started with the single argument "--adbd",
832 // instead of being the normal recovery binary, it turns into kind
833 // of a stripped-down version of adbd that only supports the
834 // 'sideload' command. Note this must be a real argument, not
835 // anything in the command file or bootloader control block; the
836 // only way recovery should be run with this argument is when it
837 // starts a copy of itself from the apply_from_adb() function.
838 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
839 adb_main();
840 return 0;
841 }
842
Doug Zongker56c51052010-07-01 09:18:44 -0700843 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800844
Doug Zongkerd4208f92010-09-20 12:16:13 -0700845 load_volume_table();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800846 get_args(&argc, &argv);
847
848 int previous_runs = 0;
849 const char *send_intent = NULL;
850 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700851 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700852 bool just_exit = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800853
854 int arg;
855 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
856 switch (arg) {
857 case 'p': previous_runs = atoi(optarg); break;
858 case 's': send_intent = optarg; break;
859 case 'u': update_package = optarg; break;
860 case 'w': wipe_data = wipe_cache = 1; break;
861 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700862 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700863 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700864 case 'l': locale = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800865 case '?':
866 LOGE("Invalid command argument\n");
867 continue;
868 }
869 }
870
Doug Zongker02ec6b82012-08-22 17:26:40 -0700871 if (locale == NULL) {
872 load_locale_from_cache();
873 }
874 printf("locale is [%s]\n", locale);
875
876 Device* device = make_device();
877 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -0700878 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700879
880 ui->Init();
Doug Zongker5fa8c232012-09-18 12:37:02 -0700881 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700882 ui->SetBackground(RecoveryUI::NONE);
883 if (show_text) ui->ShowText(true);
884
Stephen Smalley779701d2012-02-09 14:13:23 -0500885 struct selinux_opt seopts[] = {
886 { SELABEL_OPT_PATH, "/file_contexts" }
887 };
888
889 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
890
891 if (!sehandle) {
892 fprintf(stderr, "Warning: No file_contexts\n");
Kenny Root038818c2012-04-08 11:03:01 -0700893 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500894 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500895
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700896 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800897
Doug Zongker56c51052010-07-01 09:18:44 -0700898 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800899 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700900 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800901 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700902 printf("\n");
903
904 if (update_package) {
905 // For backwards compatibility on the cache partition only, if
906 // we're given an old 'root' path "CACHE:foo", change it to
907 // "/cache/foo".
908 if (strncmp(update_package, "CACHE:", 6) == 0) {
909 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700910 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700911 strlcpy(modified_path, "/cache/", len);
912 strlcat(modified_path, update_package+6, len);
913 printf("(replacing path \"%s\" with \"%s\")\n",
914 update_package, modified_path);
915 update_package = modified_path;
916 }
917 }
918 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800919
920 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -0700921 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800922
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800923 int status = INSTALL_SUCCESS;
924
Doug Zongker540d57f2011-01-18 13:36:58 -0800925 if (update_package != NULL) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700926 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
927 if (status == INSTALL_SUCCESS && wipe_cache) {
928 if (erase_volume("/cache")) {
929 LOGE("Cache wipe (requested by package) failed.");
930 }
931 }
Doug Zongker7c3ae452013-05-14 11:03:02 -0700932 if (status != INSTALL_SUCCESS) {
933 ui->Print("Installation aborted.\n");
934
935 // If this is an eng or userdebug build, then automatically
936 // turn the text display on if the script fails so the error
937 // message is visible.
938 char buffer[PROPERTY_VALUE_MAX+1];
939 property_get("ro.build.fingerprint", buffer, "");
940 if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {
941 ui->ShowText(true);
942 }
943 }
Doug Zongkerb128f542009-06-18 15:07:14 -0700944 } else if (wipe_data) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700945 if (device->WipeData()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700946 if (erase_volume("/data")) status = INSTALL_ERROR;
947 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700948 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700949 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700950 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700951 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700952 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700953 status = INSTALL_NONE; // No command specified
954 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800955 }
956
Doug Zongker02ec6b82012-08-22 17:26:40 -0700957 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
958 ui->SetBackground(RecoveryUI::ERROR);
959 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700960 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker6c8553d2012-09-24 10:40:47 -0700961 prompt_and_wait(device, status);
Doug Zongker8674a722010-09-15 11:08:23 -0700962 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800963
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800964 // Otherwise, get ready to boot the main system...
965 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -0700966 ui->Print("Rebooting...\n");
Ken Sumrall6e4472a2011-03-07 23:37:27 -0800967 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800968 return EXIT_SUCCESS;
969}