blob: e1a2a9678b115c5f10497987c3f55001140b948b [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"
Doug Zongker945fc682014-07-10 10:50:39 -070047#include "fuse_sideload.h"
48#include "fuse_sdcard_provider.h"
Doug Zongker9270a202012-01-09 15:16:13 -080049}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080050
Stephen Smalley779701d2012-02-09 14:13:23 -050051struct selabel_handle *sehandle;
52
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080053static const struct option OPTIONS[] = {
54 { "send_intent", required_argument, NULL, 's' },
55 { "update_package", required_argument, NULL, 'u' },
56 { "wipe_data", no_argument, NULL, 'w' },
57 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070058 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070059 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker02ec6b82012-08-22 17:26:40 -070060 { "locale", required_argument, NULL, 'l' },
Doug Zongkerc87bab12013-11-25 13:53:25 -080061 { "stages", required_argument, NULL, 'g' },
Doug Zongkerb1d12632014-03-18 10:32:12 -070062 { "shutdown_after", no_argument, NULL, 'p' },
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -070063 { "reason", required_argument, NULL, 'r' },
Doug Zongker988500b2009-10-06 14:41:38 -070064 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080065};
66
Doug Zongkerda1ebae2013-05-16 11:23:48 -070067#define LAST_LOG_FILE "/cache/recovery/last_log"
68
Doug Zongker6d0d7ac2013-07-09 13:34:55 -070069static const char *CACHE_LOG_DIR = "/cache/recovery";
Doug Zongkerd4208f92010-09-20 12:16:13 -070070static const char *COMMAND_FILE = "/cache/recovery/command";
71static const char *INTENT_FILE = "/cache/recovery/intent";
72static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070073static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -070074static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Michael Ward9d2629c2011-06-23 22:14:24 -070075static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070076static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070078static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079
Nick Kralevicha9ad0322014-10-22 18:38:48 -070080#define KEEP_LOG_COUNT 10
81
Doug Zongker211aebc2011-10-28 15:13:10 -070082RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070083char* locale = NULL;
Doug Zongkerc0441d12013-07-31 11:28:24 -070084char recovery_version[PROPERTY_VALUE_MAX+1];
Doug Zongkerc87bab12013-11-25 13:53:25 -080085char* stage = NULL;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -070086char* reason = NULL;
Doug Zongker211aebc2011-10-28 15:13:10 -070087
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080088/*
89 * The recovery tool communicates with the main system through /cache files.
90 * /cache/recovery/command - INPUT - command line for tool, one arg per line
91 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
92 * /cache/recovery/intent - OUTPUT - intent that was passed in
93 *
94 * The arguments which may be supplied in the recovery.command file:
95 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070096 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080097 * --wipe_data - erase user data (and cache), then reboot
98 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080099 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700100 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101 *
102 * After completing, we remove /cache/recovery/command and reboot.
103 * Arguments may also be supplied in the bootloader control block (BCB).
104 * These important scenarios must be safely restartable at any point:
105 *
106 * FACTORY RESET
107 * 1. user selects "factory reset"
108 * 2. main system writes "--wipe_data" to /cache/recovery/command
109 * 3. main system reboots into recovery
110 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
111 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700112 * 5. erase_volume() reformats /data
113 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800114 * 7. finish_recovery() erases BCB
115 * -- after this, rebooting will restart the main system --
116 * 8. main() calls reboot() to boot main system
117 *
118 * OTA INSTALL
119 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700120 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800121 * 3. main system reboots into recovery
122 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
123 * -- after this, rebooting will attempt to reinstall the update --
124 * 5. install_package() attempts to install the update
125 * NOTE: the package install must itself be restartable from any point
126 * 6. finish_recovery() erases BCB
127 * -- after this, rebooting will (try to) restart the main system --
128 * 7. ** if install failed **
129 * 7a. prompt_and_wait() shows an error icon and waits for the user
130 * 7b; the user reboots (pulling the battery, etc) into the main system
131 * 8. main() calls maybe_install_firmware_update()
132 * ** if the update contained radio/hboot firmware **:
133 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
134 * -- after this, rebooting will reformat cache & restart main system --
135 * 8b. m_i_f_u() writes firmware image into raw cache partition
136 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
137 * -- after this, rebooting will attempt to reinstall firmware --
138 * 8d. bootloader tries to flash firmware
139 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
140 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700141 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800142 * 8g. finish_recovery() erases BCB
143 * -- after this, rebooting will (try to) restart the main system --
144 * 9. main() calls reboot() to boot main system
145 */
146
147static const int MAX_ARG_LENGTH = 4096;
148static const int MAX_ARGS = 100;
149
Doug Zongkerd4208f92010-09-20 12:16:13 -0700150// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700151FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700152fopen_path(const char *path, const char *mode) {
153 if (ensure_path_mounted(path) != 0) {
154 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155 return NULL;
156 }
157
158 // When writing, try to create the containing directory, if necessary.
159 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500160 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161
162 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163 return fp;
164}
165
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700166static void redirect_stdio(const char* filename) {
167 // If these fail, there's not really anywhere to complain...
168 freopen(filename, "a", stdout); setbuf(stdout, NULL);
169 freopen(filename, "a", stderr); setbuf(stderr, NULL);
170}
171
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800172// close a file, log an error if the error indicator is set
173static void
174check_and_fclose(FILE *fp, const char *name) {
175 fflush(fp);
176 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
177 fclose(fp);
178}
179
180// command line args come from, in decreasing precedence:
181// - the actual command line
182// - the bootloader control block (one per line, after "recovery")
183// - the contents of COMMAND_FILE (one per line)
184static void
185get_args(int *argc, char ***argv) {
186 struct bootloader_message boot;
187 memset(&boot, 0, sizeof(boot));
188 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Doug Zongkerc87bab12013-11-25 13:53:25 -0800189 stage = strndup(boot.stage, sizeof(boot.stage));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190
191 if (boot.command[0] != 0 && boot.command[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700192 LOGI("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800193 }
194
195 if (boot.status[0] != 0 && boot.status[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700196 LOGI("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800197 }
198
199 // --- if arguments weren't supplied, look in the bootloader control block
200 if (*argc <= 1) {
201 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
202 const char *arg = strtok(boot.recovery, "\n");
203 if (arg != NULL && !strcmp(arg, "recovery")) {
204 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
205 (*argv)[0] = strdup(arg);
206 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
207 if ((arg = strtok(NULL, "\n")) == NULL) break;
208 (*argv)[*argc] = strdup(arg);
209 }
210 LOGI("Got arguments from boot message\n");
211 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
212 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
213 }
214 }
215
216 // --- if that doesn't work, try the command file
217 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700218 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800219 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800220 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800221 char *argv0 = (*argv)[0];
222 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
223 (*argv)[0] = argv0; // use the same program name
224
225 char buf[MAX_ARG_LENGTH];
226 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
227 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800228 token = strtok(buf, "\r\n");
229 if (token != NULL) {
230 (*argv)[*argc] = strdup(token); // Strip newline.
231 } else {
232 --*argc;
233 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800234 }
235
236 check_and_fclose(fp, COMMAND_FILE);
237 LOGI("Got arguments from %s\n", COMMAND_FILE);
238 }
239 }
240
241 // --> write the arguments we have back into the bootloader control block
242 // always boot into recovery after this (until finish_recovery() is called)
243 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
244 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
245 int i;
246 for (i = 1; i < *argc; ++i) {
247 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
248 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
249 }
250 set_bootloader_message(&boot);
251}
252
Doug Zongker34c98df2009-08-18 12:05:45 -0700253static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800254set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700255 struct bootloader_message boot;
256 memset(&boot, 0, sizeof(boot));
257 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
258 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
259 set_bootloader_message(&boot);
260}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800261
Doug Zongker2c3539e2010-09-29 13:21:30 -0700262// How much of the temp log we have copied to the copy in cache.
263static long tmplog_offset = 0;
264
265static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700266copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700267 FILE *log = fopen_path(destination, append ? "a" : "w");
268 if (log == NULL) {
269 LOGE("Can't open %s\n", destination);
270 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700271 FILE *tmplog = fopen(source, "r");
272 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700273 if (append) {
274 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
275 }
276 char buf[4096];
277 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
278 if (append) {
279 tmplog_offset = ftell(tmplog);
280 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700281 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700282 }
283 check_and_fclose(log, destination);
284 }
285}
286
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700287// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max
288// Overwrites any existing last_log.$max.
289static void
290rotate_last_logs(int max) {
291 char oldfn[256];
292 char newfn[256];
293
294 int i;
295 for (i = max-1; i >= 0; --i) {
296 snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i);
297 snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1);
298 // ignore errors
299 rename(oldfn, newfn);
300 }
301}
Doug Zongker2c3539e2010-09-29 13:21:30 -0700302
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700303static void
304copy_logs() {
305 // Copy logs to cache so the system can find out what happened.
306 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
307 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
308 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
309 chmod(LOG_FILE, 0600);
310 chown(LOG_FILE, 1000, 1000); // system user
311 chmod(LAST_LOG_FILE, 0640);
312 chmod(LAST_INSTALL_FILE, 0644);
313 sync();
314}
315
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800316// clear the recovery command and prepare to boot a (hopefully working) system,
317// copy our log file to cache as well (for the system to read), and
318// record any intent we were asked to communicate back to the system.
319// this function is idempotent: call it as many times as you like.
320static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800321finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800322 // By this point, we're ready to return to the main system...
323 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700324 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000325 if (fp == NULL) {
326 LOGE("Can't open %s\n", INTENT_FILE);
327 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800328 fputs(send_intent, fp);
329 check_and_fclose(fp, INTENT_FILE);
330 }
331 }
332
Doug Zongker4f33e552012-08-23 13:16:12 -0700333 // Save the locale to cache, so if recovery is next started up
334 // without a --locale argument (eg, directly from the bootloader)
335 // it will use the last-known locale.
336 if (locale != NULL) {
337 LOGI("Saving locale \"%s\"\n", locale);
338 FILE* fp = fopen_path(LOCALE_FILE, "w");
339 fwrite(locale, 1, strlen(locale), fp);
340 fflush(fp);
341 fsync(fileno(fp));
342 check_and_fclose(fp, LOCALE_FILE);
343 }
344
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700345 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800346
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700347 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800348 struct bootloader_message boot;
349 memset(&boot, 0, sizeof(boot));
350 set_bootloader_message(&boot);
351
352 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700353 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
354 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800355 LOGW("Can't unlink %s\n", COMMAND_FILE);
356 }
357
Doug Zongkerd0181b82011-10-19 10:51:12 -0700358 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800359 sync(); // For good measure.
360}
361
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700362typedef struct _saved_log_file {
363 char* name;
364 struct stat st;
365 unsigned char* data;
366 struct _saved_log_file* next;
367} saved_log_file;
368
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800369static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700370erase_volume(const char *volume) {
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700371 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
372
Doug Zongker02ec6b82012-08-22 17:26:40 -0700373 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700374 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700375
376 saved_log_file* head = NULL;
377
378 if (is_cache) {
379 // If we're reformatting /cache, we load any
380 // "/cache/recovery/last*" files into memory, so we can restore
381 // them after the reformat.
382
383 ensure_path_mounted(volume);
384
385 DIR* d;
386 struct dirent* de;
387 d = opendir(CACHE_LOG_DIR);
388 if (d) {
389 char path[PATH_MAX];
390 strcpy(path, CACHE_LOG_DIR);
391 strcat(path, "/");
392 int path_len = strlen(path);
393 while ((de = readdir(d)) != NULL) {
394 if (strncmp(de->d_name, "last", 4) == 0) {
395 saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file));
396 strcpy(path+path_len, de->d_name);
397 p->name = strdup(path);
398 if (stat(path, &(p->st)) == 0) {
399 // truncate files to 512kb
400 if (p->st.st_size > (1 << 19)) {
401 p->st.st_size = 1 << 19;
402 }
403 p->data = (unsigned char*) malloc(p->st.st_size);
404 FILE* f = fopen(path, "rb");
405 fread(p->data, 1, p->st.st_size, f);
406 fclose(f);
407 p->next = head;
408 head = p;
409 } else {
410 free(p);
411 }
412 }
413 }
414 closedir(d);
415 } else {
416 if (errno != ENOENT) {
417 printf("opendir failed: %s\n", strerror(errno));
418 }
419 }
420 }
421
Doug Zongker211aebc2011-10-28 15:13:10 -0700422 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700423
Doug Zongkerd0181b82011-10-19 10:51:12 -0700424 ensure_path_unmounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700425 int result = format_volume(volume);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700426
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700427 if (is_cache) {
428 while (head) {
429 FILE* f = fopen_path(head->name, "wb");
430 if (f) {
431 fwrite(head->data, 1, head->st.st_size, f);
432 fclose(f);
433 chmod(head->name, head->st.st_mode);
434 chown(head->name, head->st.st_uid, head->st.st_gid);
435 }
436 free(head->name);
437 free(head->data);
438 saved_log_file* temp = head->next;
439 free(head);
440 head = temp;
441 }
442
Doug Zongker2c3539e2010-09-29 13:21:30 -0700443 // Any part of the log we'd copied to cache is now gone.
444 // Reset the pointer so we copy from the beginning of the temp
445 // log.
446 tmplog_offset = 0;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700447 copy_logs();
Doug Zongker2c3539e2010-09-29 13:21:30 -0700448 }
449
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700450 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800451}
452
Doug Zongker28ce47c2011-10-28 10:33:05 -0700453static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700454prepend_title(const char* const* headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700455 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700456 // caller-provided headers.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700457 int count = 3; // our title has 3 lines
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700458 const char* const* p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700459 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700460
Doug Zongker28ce47c2011-10-28 10:33:05 -0700461 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
462 const char** h = new_headers;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700463 *(h++) = "Android system recovery <" EXPAND(RECOVERY_API_VERSION) "e>";
464 *(h++) = recovery_version;
465 *(h++) = "";
Doug Zongkerf93d8162009-09-22 15:16:02 -0700466 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700467 *h = NULL;
468
Doug Zongkerf93d8162009-09-22 15:16:02 -0700469 return new_headers;
470}
471
472static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700473get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700474 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700475 // throw away keys pressed previously, so user doesn't
476 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700477 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700478
Doug Zongker211aebc2011-10-28 15:13:10 -0700479 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700480 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800481 int chosen_item = -1;
482
Doug Zongkerf93d8162009-09-22 15:16:02 -0700483 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700484 int key = ui->WaitKey();
485 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800486
Doug Zongker5cae4452011-01-25 13:15:30 -0800487 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700488 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800489 continue;
490 } else {
491 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700492 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700493 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800494 }
495 }
496
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700497 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700498
499 if (action < 0) {
500 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700501 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700502 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700503 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700504 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700505 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700506 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700507 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700508 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700509 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700510 chosen_item = selected;
511 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700512 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700513 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800514 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700515 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700516 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800517 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700518 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800519
Doug Zongker211aebc2011-10-28 15:13:10 -0700520 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700521 return chosen_item;
522}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800523
Doug Zongker8674a722010-09-15 11:08:23 -0700524static int compare_string(const void* a, const void* b) {
525 return strcmp(*(const char**)a, *(const char**)b);
526}
527
Doug Zongker93950222014-07-08 14:10:23 -0700528// Returns a malloc'd path, or NULL.
529static char*
530browse_directory(const char* path, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700531 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700532
Doug Zongker8674a722010-09-15 11:08:23 -0700533 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700534 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700535 "",
536 NULL };
537 DIR* d;
538 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700539 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700540 if (d == NULL) {
541 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongker93950222014-07-08 14:10:23 -0700542 return NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700543 }
544
Doug Zongker28ce47c2011-10-28 10:33:05 -0700545 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700546
547 int d_size = 0;
548 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700549 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700550 int z_size = 1;
551 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700552 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700553 zips[0] = strdup("../");
554
555 while ((de = readdir(d)) != NULL) {
556 int name_len = strlen(de->d_name);
557
558 if (de->d_type == DT_DIR) {
559 // skip "." and ".." entries
560 if (name_len == 1 && de->d_name[0] == '.') continue;
561 if (name_len == 2 && de->d_name[0] == '.' &&
562 de->d_name[1] == '.') continue;
563
564 if (d_size >= d_alloc) {
565 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700566 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700567 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700568 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700569 strcpy(dirs[d_size], de->d_name);
570 dirs[d_size][name_len] = '/';
571 dirs[d_size][name_len+1] = '\0';
572 ++d_size;
573 } else if (de->d_type == DT_REG &&
574 name_len >= 4 &&
575 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
576 if (z_size >= z_alloc) {
577 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700578 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700579 }
580 zips[z_size++] = strdup(de->d_name);
581 }
582 }
583 closedir(d);
584
585 qsort(dirs, d_size, sizeof(char*), compare_string);
586 qsort(zips, z_size, sizeof(char*), compare_string);
587
588 // append dirs to the zips list
589 if (d_size + z_size + 1 > z_alloc) {
590 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700591 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700592 }
593 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
594 free(dirs);
595 z_size += d_size;
596 zips[z_size] = NULL;
597
Doug Zongker93950222014-07-08 14:10:23 -0700598 char* result;
Doug Zongker8674a722010-09-15 11:08:23 -0700599 int chosen_item = 0;
Doug Zongker93950222014-07-08 14:10:23 -0700600 while (true) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700601 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700602
603 char* item = zips[chosen_item];
604 int item_len = strlen(item);
605 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700606 // go up but continue browsing (if the caller is update_directory)
Doug Zongker93950222014-07-08 14:10:23 -0700607 result = NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700608 break;
609 }
Doug Zongker93950222014-07-08 14:10:23 -0700610
611 char new_path[PATH_MAX];
612 strlcpy(new_path, path, PATH_MAX);
613 strlcat(new_path, "/", PATH_MAX);
614 strlcat(new_path, item, PATH_MAX);
615
616 if (item[item_len-1] == '/') {
617 // recurse down into a subdirectory
618 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
619 result = browse_directory(new_path, device);
620 if (result) break;
621 } else {
622 // selected a zip file: return the malloc'd path to the caller.
623 result = strdup(new_path);
624 break;
625 }
626 }
Doug Zongker8674a722010-09-15 11:08:23 -0700627
628 int i;
629 for (i = 0; i < z_size; ++i) free(zips[i]);
630 free(zips);
631 free(headers);
632
633 return result;
634}
635
Doug Zongkerf93d8162009-09-22 15:16:02 -0700636static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700637wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700638 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700639 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700640
Doug Zongkerf93d8162009-09-22 15:16:02 -0700641 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700642 const char* headers[] = { "Confirm wipe of all user data?",
643 " THIS CAN NOT BE UNDONE.",
644 "",
645 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700646 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700647 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800648
Doug Zongker28ce47c2011-10-28 10:33:05 -0700649 const char* items[] = { " No",
650 " No",
651 " No",
652 " No",
653 " No",
654 " No",
655 " No",
656 " Yes -- delete all user data", // [7]
657 " No",
658 " No",
659 " No",
660 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800661
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700662 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700663 if (chosen_item != 7) {
664 return;
665 }
666 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700667
Doug Zongker211aebc2011-10-28 15:13:10 -0700668 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700669 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700670 erase_volume("/data");
671 erase_volume("/cache");
Andres Moralesee193872014-08-05 19:49:09 -0700672 erase_persistent_partition();
Doug Zongker211aebc2011-10-28 15:13:10 -0700673 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700674}
675
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700676static void file_to_ui(const char* fn) {
677 FILE *fp = fopen_path(fn, "re");
678 if (fp == NULL) {
679 ui->Print(" Unable to open %s: %s\n", fn, strerror(errno));
680 return;
681 }
682 char line[1024];
683 int ct = 0;
684 redirect_stdio("/dev/null");
685 while(fgets(line, sizeof(line), fp) != NULL) {
686 ui->Print("%s", line);
687 ct++;
688 if (ct % 30 == 0) {
689 // give the user time to glance at the entries
690 ui->WaitKey();
691 }
692 }
693 redirect_stdio(TEMPORARY_LOG_FILE);
694 fclose(fp);
695}
696
697static void choose_recovery_file(Device* device) {
698 int i;
699 static const char** title_headers = NULL;
700 char *filename;
701 const char* headers[] = { "Select file to view",
702 "",
703 NULL };
704 char* entries[KEEP_LOG_COUNT + 2];
705 memset(entries, 0, sizeof(entries));
706
707 for (i = 0; i < KEEP_LOG_COUNT; i++) {
708 char *filename;
709 if (asprintf(&filename, (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i) == -1) {
710 // memory allocation failure - return early. Should never happen.
711 return;
712 }
713 if ((ensure_path_mounted(filename) != 0) || (access(filename, R_OK) == -1)) {
714 free(filename);
715 entries[i+1] = NULL;
716 break;
717 }
718 entries[i+1] = filename;
719 }
720
721 entries[0] = strdup("Go back");
722 title_headers = prepend_title((const char**)headers);
723
724 while(1) {
725 int chosen_item = get_menu_selection(title_headers, entries, 1, 0, device);
726 if (chosen_item == 0) break;
727 file_to_ui(entries[chosen_item]);
728 }
729
730 for (i = 0; i < KEEP_LOG_COUNT + 1; i++) {
731 free(entries[i]);
732 }
733}
734
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700735// Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION
736// means to take the default, which is to reboot or shutdown depending
737// on if the --shutdown_after flag was passed to recovery.
738static Device::BuiltinAction
Doug Zongker6c8553d2012-09-24 10:40:47 -0700739prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700740 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700741
742 for (;;) {
743 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700744 switch (status) {
745 case INSTALL_SUCCESS:
746 case INSTALL_NONE:
747 ui->SetBackground(RecoveryUI::NO_COMMAND);
748 break;
749
750 case INSTALL_ERROR:
751 case INSTALL_CORRUPT:
752 ui->SetBackground(RecoveryUI::ERROR);
753 break;
754 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700755 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700756
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700757 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700758
759 // device-specific code may take some action here. It may
760 // return one of the core actions handled in the switch
761 // statement below.
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700762 Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700763
Doug Zongker93950222014-07-08 14:10:23 -0700764 int wipe_cache = 0;
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700765 switch (chosen_action) {
766 case Device::NO_ACTION:
767 break;
768
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700769 case Device::REBOOT:
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700770 case Device::SHUTDOWN:
771 case Device::REBOOT_BOOTLOADER:
772 return chosen_action;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700773
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700774 case Device::WIPE_DATA:
775 wipe_data(ui->IsTextVisible(), device);
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700776 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700777 break;
778
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700779 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700780 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700781 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700782 ui->Print("Cache wipe complete.\n");
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700783 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700784 break;
785
Doug Zongker93950222014-07-08 14:10:23 -0700786 case Device::APPLY_EXT: {
787 ensure_path_mounted(SDCARD_ROOT);
788 char* path = browse_directory(SDCARD_ROOT, device);
789 if (path == NULL) {
790 ui->Print("\n-- No package file selected.\n", path);
791 break;
792 }
793
794 ui->Print("\n-- Install %s ...\n", path);
795 set_sdcard_update_bootloader_message();
Doug Zongker945fc682014-07-10 10:50:39 -0700796 void* token = start_sdcard_fuse(path);
Doug Zongker93950222014-07-08 14:10:23 -0700797
Doug Zongker945fc682014-07-10 10:50:39 -0700798 int status = install_package(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache,
799 TEMPORARY_INSTALL_FILE, false);
800
801 finish_sdcard_fuse(token);
802 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker93950222014-07-08 14:10:23 -0700803
Doug Zongkerd0181b82011-10-19 10:51:12 -0700804 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700805 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700806 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700807 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700808 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700809 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700810 }
811 }
Doug Zongker945fc682014-07-10 10:50:39 -0700812
Doug Zongker8674a722010-09-15 11:08:23 -0700813 if (status >= 0) {
814 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700815 ui->SetBackground(RecoveryUI::ERROR);
816 ui->Print("Installation aborted.\n");
817 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700818 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker8674a722010-09-15 11:08:23 -0700819 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700820 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700821 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700822 }
823 break;
Doug Zongker93950222014-07-08 14:10:23 -0700824 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700825
826 case Device::APPLY_CACHE:
Doug Zongker93950222014-07-08 14:10:23 -0700827 ui->Print("\nAPPLY_CACHE is deprecated.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700828 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800829
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700830 case Device::READ_RECOVERY_LASTLOG:
831 choose_recovery_file(device);
832 break;
833
Doug Zongker9270a202012-01-09 15:16:13 -0800834 case Device::APPLY_ADB_SIDELOAD:
Doug Zongker9270a202012-01-09 15:16:13 -0800835 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
836 if (status >= 0) {
837 if (status != INSTALL_SUCCESS) {
838 ui->SetBackground(RecoveryUI::ERROR);
839 ui->Print("Installation aborted.\n");
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700840 copy_logs();
Doug Zongker9270a202012-01-09 15:16:13 -0800841 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700842 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker9270a202012-01-09 15:16:13 -0800843 } else {
844 ui->Print("\nInstall from ADB complete.\n");
845 }
846 }
847 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800848 }
849 }
850}
851
852static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800853print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700854 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800855}
856
Doug Zongker02ec6b82012-08-22 17:26:40 -0700857static void
858load_locale_from_cache() {
859 FILE* fp = fopen_path(LOCALE_FILE, "r");
860 char buffer[80];
861 if (fp != NULL) {
862 fgets(buffer, sizeof(buffer), fp);
863 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700864 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700865 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
866 if (!isspace(buffer[i])) {
867 buffer[j++] = buffer[i];
868 }
869 }
870 buffer[j] = 0;
871 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700872 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700873 }
874}
875
Doug Zongker7c3ae452013-05-14 11:03:02 -0700876static RecoveryUI* gCurrentUI = NULL;
877
878void
879ui_print(const char* format, ...) {
880 char buffer[256];
881
882 va_list ap;
883 va_start(ap, format);
884 vsnprintf(buffer, sizeof(buffer), format, ap);
885 va_end(ap);
886
887 if (gCurrentUI != NULL) {
888 gCurrentUI->Print("%s", buffer);
889 } else {
890 fputs(buffer, stdout);
891 }
892}
893
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800894int
Oscar Montemayor05231562009-11-30 08:40:57 -0800895main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800896 time_t start = time(NULL);
897
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700898 redirect_stdio(TEMPORARY_LOG_FILE);
Doug Zongker9270a202012-01-09 15:16:13 -0800899
900 // If this binary is started with the single argument "--adbd",
901 // instead of being the normal recovery binary, it turns into kind
902 // of a stripped-down version of adbd that only supports the
903 // 'sideload' command. Note this must be a real argument, not
904 // anything in the command file or bootloader control block; the
905 // only way recovery should be run with this argument is when it
906 // starts a copy of itself from the apply_from_adb() function.
907 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
908 adb_main();
909 return 0;
910 }
911
Doug Zongker19a8e242014-01-21 09:25:41 -0800912 printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800913
Doug Zongkerd4208f92010-09-20 12:16:13 -0700914 load_volume_table();
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700915 ensure_path_mounted(LAST_LOG_FILE);
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700916 rotate_last_logs(KEEP_LOG_COUNT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800917 get_args(&argc, &argv);
918
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800919 const char *send_intent = NULL;
920 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700921 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700922 bool just_exit = false;
Doug Zongkerb1d12632014-03-18 10:32:12 -0700923 bool shutdown_after = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800924
925 int arg;
926 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
927 switch (arg) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800928 case 's': send_intent = optarg; break;
929 case 'u': update_package = optarg; break;
930 case 'w': wipe_data = wipe_cache = 1; break;
931 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700932 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700933 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700934 case 'l': locale = optarg; break;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800935 case 'g': {
936 if (stage == NULL || *stage == '\0') {
937 char buffer[20] = "1/";
938 strncat(buffer, optarg, sizeof(buffer)-3);
939 stage = strdup(buffer);
940 }
941 break;
942 }
Doug Zongkerb1d12632014-03-18 10:32:12 -0700943 case 'p': shutdown_after = true; break;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -0700944 case 'r': reason = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800945 case '?':
946 LOGE("Invalid command argument\n");
947 continue;
948 }
949 }
950
Doug Zongker02ec6b82012-08-22 17:26:40 -0700951 if (locale == NULL) {
952 load_locale_from_cache();
953 }
954 printf("locale is [%s]\n", locale);
Doug Zongker0d32f252014-02-13 15:07:56 -0800955 printf("stage is [%s]\n", stage);
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -0700956 printf("reason is [%s]\n", reason);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700957
958 Device* device = make_device();
959 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -0700960 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700961
Doug Zongker5fa8c232012-09-18 12:37:02 -0700962 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700963 ui->Init();
Doug Zongkerc87bab12013-11-25 13:53:25 -0800964
965 int st_cur, st_max;
966 if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {
967 ui->SetStage(st_cur, st_max);
968 }
969
Doug Zongker02ec6b82012-08-22 17:26:40 -0700970 ui->SetBackground(RecoveryUI::NONE);
971 if (show_text) ui->ShowText(true);
972
Stephen Smalley779701d2012-02-09 14:13:23 -0500973 struct selinux_opt seopts[] = {
974 { SELABEL_OPT_PATH, "/file_contexts" }
975 };
976
977 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
978
979 if (!sehandle) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700980 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500981 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500982
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700983 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800984
Doug Zongker56c51052010-07-01 09:18:44 -0700985 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800986 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700987 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800988 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700989 printf("\n");
990
991 if (update_package) {
992 // For backwards compatibility on the cache partition only, if
993 // we're given an old 'root' path "CACHE:foo", change it to
994 // "/cache/foo".
995 if (strncmp(update_package, "CACHE:", 6) == 0) {
996 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700997 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700998 strlcpy(modified_path, "/cache/", len);
999 strlcat(modified_path, update_package+6, len);
1000 printf("(replacing path \"%s\" with \"%s\")\n",
1001 update_package, modified_path);
1002 update_package = modified_path;
1003 }
1004 }
1005 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001006
1007 property_list(print_property, NULL);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001008 property_get("ro.build.display.id", recovery_version, "");
Doug Zongker56c51052010-07-01 09:18:44 -07001009 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001010
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001011 int status = INSTALL_SUCCESS;
1012
Doug Zongker540d57f2011-01-18 13:36:58 -08001013 if (update_package != NULL) {
Doug Zongker075ad802014-06-26 15:35:51 -07001014 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE, true);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001015 if (status == INSTALL_SUCCESS && wipe_cache) {
1016 if (erase_volume("/cache")) {
1017 LOGE("Cache wipe (requested by package) failed.");
1018 }
1019 }
Doug Zongker7c3ae452013-05-14 11:03:02 -07001020 if (status != INSTALL_SUCCESS) {
1021 ui->Print("Installation aborted.\n");
1022
1023 // If this is an eng or userdebug build, then automatically
1024 // turn the text display on if the script fails so the error
1025 // message is visible.
1026 char buffer[PROPERTY_VALUE_MAX+1];
1027 property_get("ro.build.fingerprint", buffer, "");
1028 if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {
1029 ui->ShowText(true);
1030 }
1031 }
Doug Zongkerb128f542009-06-18 15:07:14 -07001032 } else if (wipe_data) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001033 if (device->WipeData()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -07001034 if (erase_volume("/data")) status = INSTALL_ERROR;
1035 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Andres Moralesee193872014-08-05 19:49:09 -07001036 if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001037 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -07001038 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -07001039 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001040 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001041 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -07001042 status = INSTALL_NONE; // No command specified
1043 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001044 }
1045
Doug Zongker02ec6b82012-08-22 17:26:40 -07001046 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
Doug Zongkerf24fd7e2013-07-02 11:43:25 -07001047 copy_logs();
Doug Zongker02ec6b82012-08-22 17:26:40 -07001048 ui->SetBackground(RecoveryUI::ERROR);
1049 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001050 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
Doug Zongker211aebc2011-10-28 15:13:10 -07001051 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001052 Device::BuiltinAction temp = prompt_and_wait(device, status);
1053 if (temp != Device::NO_ACTION) after = temp;
Doug Zongker8674a722010-09-15 11:08:23 -07001054 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001055
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001056 // Save logs and clean up before rebooting or shutting down.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001057 finish_recovery(send_intent);
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001058
1059 switch (after) {
1060 case Device::SHUTDOWN:
1061 ui->Print("Shutting down...\n");
1062 property_set(ANDROID_RB_PROPERTY, "shutdown,");
1063 break;
1064
1065 case Device::REBOOT_BOOTLOADER:
1066 ui->Print("Rebooting to bootloader...\n");
1067 property_set(ANDROID_RB_PROPERTY, "reboot,bootloader");
1068 break;
1069
1070 default:
1071 ui->Print("Rebooting...\n");
1072 property_set(ANDROID_RB_PROPERTY, "reboot,");
1073 break;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001074 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001075 sleep(5); // should reboot before this finishes
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001076 return EXIT_SUCCESS;
1077}