blob: 2b5332e00eef2d2df876fe564d9c7741f3216adf [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" {
Dan Albertf3a57262015-02-19 13:21:14 -080046#include "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
Doug Zongker211aebc2011-10-28 15:13:10 -070080RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070081char* locale = NULL;
Doug Zongkerc0441d12013-07-31 11:28:24 -070082char recovery_version[PROPERTY_VALUE_MAX+1];
Doug Zongkerc87bab12013-11-25 13:53:25 -080083char* stage = NULL;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -070084char* reason = NULL;
Doug Zongker211aebc2011-10-28 15:13:10 -070085
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080086/*
87 * The recovery tool communicates with the main system through /cache files.
88 * /cache/recovery/command - INPUT - command line for tool, one arg per line
89 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
90 * /cache/recovery/intent - OUTPUT - intent that was passed in
91 *
92 * The arguments which may be supplied in the recovery.command file:
93 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070094 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095 * --wipe_data - erase user data (and cache), then reboot
96 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080097 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -070098 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080099 *
100 * After completing, we remove /cache/recovery/command and reboot.
101 * Arguments may also be supplied in the bootloader control block (BCB).
102 * These important scenarios must be safely restartable at any point:
103 *
104 * FACTORY RESET
105 * 1. user selects "factory reset"
106 * 2. main system writes "--wipe_data" to /cache/recovery/command
107 * 3. main system reboots into recovery
108 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
109 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700110 * 5. erase_volume() reformats /data
111 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112 * 7. finish_recovery() erases BCB
113 * -- after this, rebooting will restart the main system --
114 * 8. main() calls reboot() to boot main system
115 *
116 * OTA INSTALL
117 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700118 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800119 * 3. main system reboots into recovery
120 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
121 * -- after this, rebooting will attempt to reinstall the update --
122 * 5. install_package() attempts to install the update
123 * NOTE: the package install must itself be restartable from any point
124 * 6. finish_recovery() erases BCB
125 * -- after this, rebooting will (try to) restart the main system --
126 * 7. ** if install failed **
127 * 7a. prompt_and_wait() shows an error icon and waits for the user
128 * 7b; the user reboots (pulling the battery, etc) into the main system
129 * 8. main() calls maybe_install_firmware_update()
130 * ** if the update contained radio/hboot firmware **:
131 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
132 * -- after this, rebooting will reformat cache & restart main system --
133 * 8b. m_i_f_u() writes firmware image into raw cache partition
134 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
135 * -- after this, rebooting will attempt to reinstall firmware --
136 * 8d. bootloader tries to flash firmware
137 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
138 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700139 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800140 * 8g. finish_recovery() erases BCB
141 * -- after this, rebooting will (try to) restart the main system --
142 * 9. main() calls reboot() to boot main system
143 */
144
145static const int MAX_ARG_LENGTH = 4096;
146static const int MAX_ARGS = 100;
147
Doug Zongkerd4208f92010-09-20 12:16:13 -0700148// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700149FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700150fopen_path(const char *path, const char *mode) {
151 if (ensure_path_mounted(path) != 0) {
152 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800153 return NULL;
154 }
155
156 // When writing, try to create the containing directory, if necessary.
157 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500158 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800159
160 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800161 return fp;
162}
163
Elliott Hughesf14af802015-02-10 14:46:14 -0800164bool is_ro_debuggable() {
165 char value[PROPERTY_VALUE_MAX+1];
166 return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1');
167}
168
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800169// close a file, log an error if the error indicator is set
170static void
171check_and_fclose(FILE *fp, const char *name) {
172 fflush(fp);
173 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
174 fclose(fp);
175}
176
177// command line args come from, in decreasing precedence:
178// - the actual command line
179// - the bootloader control block (one per line, after "recovery")
180// - the contents of COMMAND_FILE (one per line)
181static void
182get_args(int *argc, char ***argv) {
183 struct bootloader_message boot;
184 memset(&boot, 0, sizeof(boot));
185 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Doug Zongkerc87bab12013-11-25 13:53:25 -0800186 stage = strndup(boot.stage, sizeof(boot.stage));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187
188 if (boot.command[0] != 0 && boot.command[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700189 LOGI("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800190 }
191
192 if (boot.status[0] != 0 && boot.status[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700193 LOGI("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800194 }
195
196 // --- if arguments weren't supplied, look in the bootloader control block
197 if (*argc <= 1) {
198 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
199 const char *arg = strtok(boot.recovery, "\n");
200 if (arg != NULL && !strcmp(arg, "recovery")) {
201 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
202 (*argv)[0] = strdup(arg);
203 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
204 if ((arg = strtok(NULL, "\n")) == NULL) break;
205 (*argv)[*argc] = strdup(arg);
206 }
207 LOGI("Got arguments from boot message\n");
208 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
209 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
210 }
211 }
212
213 // --- if that doesn't work, try the command file
214 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700215 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800216 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800217 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218 char *argv0 = (*argv)[0];
219 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
220 (*argv)[0] = argv0; // use the same program name
221
222 char buf[MAX_ARG_LENGTH];
223 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
224 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800225 token = strtok(buf, "\r\n");
226 if (token != NULL) {
227 (*argv)[*argc] = strdup(token); // Strip newline.
228 } else {
229 --*argc;
230 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800231 }
232
233 check_and_fclose(fp, COMMAND_FILE);
234 LOGI("Got arguments from %s\n", COMMAND_FILE);
235 }
236 }
237
238 // --> write the arguments we have back into the bootloader control block
239 // always boot into recovery after this (until finish_recovery() is called)
240 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
241 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
242 int i;
243 for (i = 1; i < *argc; ++i) {
244 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
245 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
246 }
247 set_bootloader_message(&boot);
248}
249
Doug Zongker34c98df2009-08-18 12:05:45 -0700250static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800251set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700252 struct bootloader_message boot;
253 memset(&boot, 0, sizeof(boot));
254 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
255 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
256 set_bootloader_message(&boot);
257}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800258
Doug Zongker2c3539e2010-09-29 13:21:30 -0700259// How much of the temp log we have copied to the copy in cache.
260static long tmplog_offset = 0;
261
262static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700263copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700264 FILE *log = fopen_path(destination, append ? "a" : "w");
265 if (log == NULL) {
266 LOGE("Can't open %s\n", destination);
267 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700268 FILE *tmplog = fopen(source, "r");
269 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700270 if (append) {
271 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
272 }
273 char buf[4096];
274 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
275 if (append) {
276 tmplog_offset = ftell(tmplog);
277 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700278 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700279 }
280 check_and_fclose(log, destination);
281 }
282}
283
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700284// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max
285// Overwrites any existing last_log.$max.
286static void
287rotate_last_logs(int max) {
288 char oldfn[256];
289 char newfn[256];
290
291 int i;
292 for (i = max-1; i >= 0; --i) {
293 snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i);
294 snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1);
295 // ignore errors
296 rename(oldfn, newfn);
297 }
298}
Doug Zongker2c3539e2010-09-29 13:21:30 -0700299
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700300static void
301copy_logs() {
302 // Copy logs to cache so the system can find out what happened.
303 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
304 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
305 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
306 chmod(LOG_FILE, 0600);
307 chown(LOG_FILE, 1000, 1000); // system user
308 chmod(LAST_LOG_FILE, 0640);
309 chmod(LAST_INSTALL_FILE, 0644);
310 sync();
311}
312
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800313// clear the recovery command and prepare to boot a (hopefully working) system,
314// copy our log file to cache as well (for the system to read), and
315// record any intent we were asked to communicate back to the system.
316// this function is idempotent: call it as many times as you like.
317static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800318finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800319 // By this point, we're ready to return to the main system...
320 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700321 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000322 if (fp == NULL) {
323 LOGE("Can't open %s\n", INTENT_FILE);
324 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800325 fputs(send_intent, fp);
326 check_and_fclose(fp, INTENT_FILE);
327 }
328 }
329
Doug Zongker4f33e552012-08-23 13:16:12 -0700330 // Save the locale to cache, so if recovery is next started up
331 // without a --locale argument (eg, directly from the bootloader)
332 // it will use the last-known locale.
333 if (locale != NULL) {
334 LOGI("Saving locale \"%s\"\n", locale);
335 FILE* fp = fopen_path(LOCALE_FILE, "w");
336 fwrite(locale, 1, strlen(locale), fp);
337 fflush(fp);
338 fsync(fileno(fp));
339 check_and_fclose(fp, LOCALE_FILE);
340 }
341
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700342 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800343
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700344 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800345 struct bootloader_message boot;
346 memset(&boot, 0, sizeof(boot));
347 set_bootloader_message(&boot);
348
349 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700350 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
351 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800352 LOGW("Can't unlink %s\n", COMMAND_FILE);
353 }
354
Doug Zongkerd0181b82011-10-19 10:51:12 -0700355 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800356 sync(); // For good measure.
357}
358
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700359typedef struct _saved_log_file {
360 char* name;
361 struct stat st;
362 unsigned char* data;
363 struct _saved_log_file* next;
364} saved_log_file;
365
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800366static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700367erase_volume(const char *volume) {
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700368 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
369
Doug Zongker02ec6b82012-08-22 17:26:40 -0700370 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700371 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700372
373 saved_log_file* head = NULL;
374
375 if (is_cache) {
376 // If we're reformatting /cache, we load any
377 // "/cache/recovery/last*" files into memory, so we can restore
378 // them after the reformat.
379
380 ensure_path_mounted(volume);
381
382 DIR* d;
383 struct dirent* de;
384 d = opendir(CACHE_LOG_DIR);
385 if (d) {
386 char path[PATH_MAX];
387 strcpy(path, CACHE_LOG_DIR);
388 strcat(path, "/");
389 int path_len = strlen(path);
390 while ((de = readdir(d)) != NULL) {
391 if (strncmp(de->d_name, "last", 4) == 0) {
392 saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file));
393 strcpy(path+path_len, de->d_name);
394 p->name = strdup(path);
395 if (stat(path, &(p->st)) == 0) {
396 // truncate files to 512kb
397 if (p->st.st_size > (1 << 19)) {
398 p->st.st_size = 1 << 19;
399 }
400 p->data = (unsigned char*) malloc(p->st.st_size);
401 FILE* f = fopen(path, "rb");
402 fread(p->data, 1, p->st.st_size, f);
403 fclose(f);
404 p->next = head;
405 head = p;
406 } else {
407 free(p);
408 }
409 }
410 }
411 closedir(d);
412 } else {
413 if (errno != ENOENT) {
414 printf("opendir failed: %s\n", strerror(errno));
415 }
416 }
417 }
418
Doug Zongker211aebc2011-10-28 15:13:10 -0700419 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700420
Doug Zongkerd0181b82011-10-19 10:51:12 -0700421 ensure_path_unmounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700422 int result = format_volume(volume);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700423
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700424 if (is_cache) {
425 while (head) {
426 FILE* f = fopen_path(head->name, "wb");
427 if (f) {
428 fwrite(head->data, 1, head->st.st_size, f);
429 fclose(f);
430 chmod(head->name, head->st.st_mode);
431 chown(head->name, head->st.st_uid, head->st.st_gid);
432 }
433 free(head->name);
434 free(head->data);
435 saved_log_file* temp = head->next;
436 free(head);
437 head = temp;
438 }
439
Doug Zongker2c3539e2010-09-29 13:21:30 -0700440 // Any part of the log we'd copied to cache is now gone.
441 // Reset the pointer so we copy from the beginning of the temp
442 // log.
443 tmplog_offset = 0;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700444 copy_logs();
Doug Zongker2c3539e2010-09-29 13:21:30 -0700445 }
446
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700447 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800448}
449
Doug Zongker28ce47c2011-10-28 10:33:05 -0700450static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700451prepend_title(const char* const* headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700452 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700453 // caller-provided headers.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700454 int count = 3; // our title has 3 lines
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700455 const char* const* p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700456 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700457
Doug Zongker28ce47c2011-10-28 10:33:05 -0700458 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
459 const char** h = new_headers;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700460 *(h++) = "Android system recovery <" EXPAND(RECOVERY_API_VERSION) "e>";
461 *(h++) = recovery_version;
462 *(h++) = "";
Doug Zongkerf93d8162009-09-22 15:16:02 -0700463 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700464 *h = NULL;
465
Doug Zongkerf93d8162009-09-22 15:16:02 -0700466 return new_headers;
467}
468
469static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700470get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700471 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700472 // throw away keys pressed previously, so user doesn't
473 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700474 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700475
Doug Zongker211aebc2011-10-28 15:13:10 -0700476 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700477 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800478 int chosen_item = -1;
479
Doug Zongkerf93d8162009-09-22 15:16:02 -0700480 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700481 int key = ui->WaitKey();
482 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800483
Doug Zongker5cae4452011-01-25 13:15:30 -0800484 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700485 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800486 continue;
487 } else {
488 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700489 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700490 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800491 }
492 }
493
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700494 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700495
496 if (action < 0) {
497 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700498 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700499 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700500 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700501 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700502 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700503 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700504 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700505 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700506 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700507 chosen_item = selected;
508 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700509 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700510 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800511 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700512 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700513 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800514 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700515 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800516
Doug Zongker211aebc2011-10-28 15:13:10 -0700517 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700518 return chosen_item;
519}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800520
Doug Zongker8674a722010-09-15 11:08:23 -0700521static int compare_string(const void* a, const void* b) {
522 return strcmp(*(const char**)a, *(const char**)b);
523}
524
Doug Zongker93950222014-07-08 14:10:23 -0700525// Returns a malloc'd path, or NULL.
526static char*
527browse_directory(const char* path, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700528 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700529
Doug Zongker8674a722010-09-15 11:08:23 -0700530 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700531 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700532 "",
533 NULL };
534 DIR* d;
535 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700536 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700537 if (d == NULL) {
538 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongker93950222014-07-08 14:10:23 -0700539 return NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700540 }
541
Doug Zongker28ce47c2011-10-28 10:33:05 -0700542 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700543
544 int d_size = 0;
545 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700546 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700547 int z_size = 1;
548 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700549 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700550 zips[0] = strdup("../");
551
552 while ((de = readdir(d)) != NULL) {
553 int name_len = strlen(de->d_name);
554
555 if (de->d_type == DT_DIR) {
556 // skip "." and ".." entries
557 if (name_len == 1 && de->d_name[0] == '.') continue;
558 if (name_len == 2 && de->d_name[0] == '.' &&
559 de->d_name[1] == '.') continue;
560
561 if (d_size >= d_alloc) {
562 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700563 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700564 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700565 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700566 strcpy(dirs[d_size], de->d_name);
567 dirs[d_size][name_len] = '/';
568 dirs[d_size][name_len+1] = '\0';
569 ++d_size;
570 } else if (de->d_type == DT_REG &&
571 name_len >= 4 &&
572 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
573 if (z_size >= z_alloc) {
574 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700575 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700576 }
577 zips[z_size++] = strdup(de->d_name);
578 }
579 }
580 closedir(d);
581
582 qsort(dirs, d_size, sizeof(char*), compare_string);
583 qsort(zips, z_size, sizeof(char*), compare_string);
584
585 // append dirs to the zips list
586 if (d_size + z_size + 1 > z_alloc) {
587 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700588 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700589 }
590 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
591 free(dirs);
592 z_size += d_size;
593 zips[z_size] = NULL;
594
Doug Zongker93950222014-07-08 14:10:23 -0700595 char* result;
Doug Zongker8674a722010-09-15 11:08:23 -0700596 int chosen_item = 0;
Doug Zongker93950222014-07-08 14:10:23 -0700597 while (true) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700598 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700599
600 char* item = zips[chosen_item];
601 int item_len = strlen(item);
602 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700603 // go up but continue browsing (if the caller is update_directory)
Doug Zongker93950222014-07-08 14:10:23 -0700604 result = NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700605 break;
606 }
Doug Zongker93950222014-07-08 14:10:23 -0700607
608 char new_path[PATH_MAX];
609 strlcpy(new_path, path, PATH_MAX);
610 strlcat(new_path, "/", PATH_MAX);
611 strlcat(new_path, item, PATH_MAX);
612
613 if (item[item_len-1] == '/') {
614 // recurse down into a subdirectory
615 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
616 result = browse_directory(new_path, device);
617 if (result) break;
618 } else {
619 // selected a zip file: return the malloc'd path to the caller.
620 result = strdup(new_path);
621 break;
622 }
623 }
Doug Zongker8674a722010-09-15 11:08:23 -0700624
625 int i;
626 for (i = 0; i < z_size; ++i) free(zips[i]);
627 free(zips);
628 free(headers);
629
630 return result;
631}
632
Doug Zongkerf93d8162009-09-22 15:16:02 -0700633static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700634wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700635 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700636 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700637
Doug Zongkerf93d8162009-09-22 15:16:02 -0700638 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700639 const char* headers[] = { "Confirm wipe of all user data?",
640 " THIS CAN NOT BE UNDONE.",
641 "",
642 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700643 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700644 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800645
Doug Zongker28ce47c2011-10-28 10:33:05 -0700646 const char* items[] = { " No",
647 " No",
648 " No",
649 " No",
650 " No",
651 " No",
652 " No",
653 " Yes -- delete all user data", // [7]
654 " No",
655 " No",
656 " No",
657 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800658
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700659 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700660 if (chosen_item != 7) {
661 return;
662 }
663 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700664
Doug Zongker211aebc2011-10-28 15:13:10 -0700665 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700666 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700667 erase_volume("/data");
668 erase_volume("/cache");
Andres Moralesee193872014-08-05 19:49:09 -0700669 erase_persistent_partition();
Doug Zongker211aebc2011-10-28 15:13:10 -0700670 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700671}
672
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700673// Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION
674// means to take the default, which is to reboot or shutdown depending
675// on if the --shutdown_after flag was passed to recovery.
676static Device::BuiltinAction
Doug Zongker6c8553d2012-09-24 10:40:47 -0700677prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700678 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700679
680 for (;;) {
681 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700682 switch (status) {
683 case INSTALL_SUCCESS:
684 case INSTALL_NONE:
685 ui->SetBackground(RecoveryUI::NO_COMMAND);
686 break;
687
688 case INSTALL_ERROR:
689 case INSTALL_CORRUPT:
690 ui->SetBackground(RecoveryUI::ERROR);
691 break;
692 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700693 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700694
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700695 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700696
697 // device-specific code may take some action here. It may
698 // return one of the core actions handled in the switch
699 // statement below.
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700700 Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700701
Doug Zongker93950222014-07-08 14:10:23 -0700702 int wipe_cache = 0;
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700703 switch (chosen_action) {
704 case Device::NO_ACTION:
705 break;
706
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700707 case Device::REBOOT:
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700708 case Device::SHUTDOWN:
709 case Device::REBOOT_BOOTLOADER:
710 return chosen_action;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700711
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700712 case Device::WIPE_DATA:
713 wipe_data(ui->IsTextVisible(), device);
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700714 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700715 break;
716
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700717 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700718 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700719 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700720 ui->Print("Cache wipe complete.\n");
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700721 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700722 break;
723
Doug Zongker93950222014-07-08 14:10:23 -0700724 case Device::APPLY_EXT: {
725 ensure_path_mounted(SDCARD_ROOT);
726 char* path = browse_directory(SDCARD_ROOT, device);
727 if (path == NULL) {
728 ui->Print("\n-- No package file selected.\n", path);
729 break;
730 }
731
732 ui->Print("\n-- Install %s ...\n", path);
733 set_sdcard_update_bootloader_message();
Doug Zongker945fc682014-07-10 10:50:39 -0700734 void* token = start_sdcard_fuse(path);
Doug Zongker93950222014-07-08 14:10:23 -0700735
Doug Zongker945fc682014-07-10 10:50:39 -0700736 int status = install_package(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache,
737 TEMPORARY_INSTALL_FILE, false);
738
739 finish_sdcard_fuse(token);
740 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker93950222014-07-08 14:10:23 -0700741
Doug Zongkerd0181b82011-10-19 10:51:12 -0700742 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700743 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700744 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700745 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700746 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700747 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700748 }
749 }
Doug Zongker945fc682014-07-10 10:50:39 -0700750
Doug Zongker8674a722010-09-15 11:08:23 -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()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700756 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker8674a722010-09-15 11:08:23 -0700757 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700758 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700759 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700760 }
761 break;
Doug Zongker93950222014-07-08 14:10:23 -0700762 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700763
764 case Device::APPLY_CACHE:
Doug Zongker93950222014-07-08 14:10:23 -0700765 ui->Print("\nAPPLY_CACHE is deprecated.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700766 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800767
768 case Device::APPLY_ADB_SIDELOAD:
Doug Zongker9270a202012-01-09 15:16:13 -0800769 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
770 if (status >= 0) {
771 if (status != INSTALL_SUCCESS) {
772 ui->SetBackground(RecoveryUI::ERROR);
773 ui->Print("Installation aborted.\n");
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700774 copy_logs();
Doug Zongker9270a202012-01-09 15:16:13 -0800775 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700776 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker9270a202012-01-09 15:16:13 -0800777 } else {
778 ui->Print("\nInstall from ADB complete.\n");
779 }
780 }
781 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800782 }
783 }
784}
785
786static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800787print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700788 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800789}
790
Doug Zongker02ec6b82012-08-22 17:26:40 -0700791static void
792load_locale_from_cache() {
793 FILE* fp = fopen_path(LOCALE_FILE, "r");
794 char buffer[80];
795 if (fp != NULL) {
796 fgets(buffer, sizeof(buffer), fp);
797 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700798 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700799 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
800 if (!isspace(buffer[i])) {
801 buffer[j++] = buffer[i];
802 }
803 }
804 buffer[j] = 0;
805 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700806 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700807 }
808}
809
Doug Zongker7c3ae452013-05-14 11:03:02 -0700810static RecoveryUI* gCurrentUI = NULL;
811
812void
813ui_print(const char* format, ...) {
814 char buffer[256];
815
816 va_list ap;
817 va_start(ap, format);
818 vsnprintf(buffer, sizeof(buffer), format, ap);
819 va_end(ap);
820
821 if (gCurrentUI != NULL) {
822 gCurrentUI->Print("%s", buffer);
823 } else {
824 fputs(buffer, stdout);
825 }
826}
827
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800828int
Oscar Montemayor05231562009-11-30 08:40:57 -0800829main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800830 time_t start = time(NULL);
831
832 // If these fail, there's not really anywhere to complain...
833 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
834 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800835
836 // If this binary is started with the single argument "--adbd",
837 // instead of being the normal recovery binary, it turns into kind
838 // of a stripped-down version of adbd that only supports the
839 // 'sideload' command. Note this must be a real argument, not
840 // anything in the command file or bootloader control block; the
841 // only way recovery should be run with this argument is when it
842 // starts a copy of itself from the apply_from_adb() function.
843 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
Dan Albertf3a57262015-02-19 13:21:14 -0800844 adb_main(0, DEFAULT_ADB_PORT);
Doug Zongker9270a202012-01-09 15:16:13 -0800845 return 0;
846 }
847
Doug Zongker19a8e242014-01-21 09:25:41 -0800848 printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800849
Doug Zongkerd4208f92010-09-20 12:16:13 -0700850 load_volume_table();
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700851 ensure_path_mounted(LAST_LOG_FILE);
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700852 rotate_last_logs(10);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800853 get_args(&argc, &argv);
854
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800855 const char *send_intent = NULL;
856 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700857 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700858 bool just_exit = false;
Doug Zongkerb1d12632014-03-18 10:32:12 -0700859 bool shutdown_after = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800860
861 int arg;
862 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
863 switch (arg) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800864 case 's': send_intent = optarg; break;
865 case 'u': update_package = optarg; break;
866 case 'w': wipe_data = wipe_cache = 1; break;
867 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700868 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700869 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700870 case 'l': locale = optarg; break;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800871 case 'g': {
872 if (stage == NULL || *stage == '\0') {
873 char buffer[20] = "1/";
874 strncat(buffer, optarg, sizeof(buffer)-3);
875 stage = strdup(buffer);
876 }
877 break;
878 }
Doug Zongkerb1d12632014-03-18 10:32:12 -0700879 case 'p': shutdown_after = true; break;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -0700880 case 'r': reason = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800881 case '?':
882 LOGE("Invalid command argument\n");
883 continue;
884 }
885 }
886
Doug Zongker02ec6b82012-08-22 17:26:40 -0700887 if (locale == NULL) {
888 load_locale_from_cache();
889 }
890 printf("locale is [%s]\n", locale);
Doug Zongker0d32f252014-02-13 15:07:56 -0800891 printf("stage is [%s]\n", stage);
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -0700892 printf("reason is [%s]\n", reason);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700893
894 Device* device = make_device();
895 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -0700896 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700897
Doug Zongker5fa8c232012-09-18 12:37:02 -0700898 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700899 ui->Init();
Doug Zongkerc87bab12013-11-25 13:53:25 -0800900
901 int st_cur, st_max;
902 if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {
903 ui->SetStage(st_cur, st_max);
904 }
905
Doug Zongker02ec6b82012-08-22 17:26:40 -0700906 ui->SetBackground(RecoveryUI::NONE);
907 if (show_text) ui->ShowText(true);
908
Stephen Smalley779701d2012-02-09 14:13:23 -0500909 struct selinux_opt seopts[] = {
910 { SELABEL_OPT_PATH, "/file_contexts" }
911 };
912
913 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
914
915 if (!sehandle) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700916 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500917 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500918
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700919 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800920
Doug Zongker56c51052010-07-01 09:18:44 -0700921 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800922 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700923 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800924 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700925 printf("\n");
926
927 if (update_package) {
928 // For backwards compatibility on the cache partition only, if
929 // we're given an old 'root' path "CACHE:foo", change it to
930 // "/cache/foo".
931 if (strncmp(update_package, "CACHE:", 6) == 0) {
932 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700933 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -0700934 strlcpy(modified_path, "/cache/", len);
935 strlcat(modified_path, update_package+6, len);
936 printf("(replacing path \"%s\" with \"%s\")\n",
937 update_package, modified_path);
938 update_package = modified_path;
939 }
940 }
941 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800942
943 property_list(print_property, NULL);
Doug Zongkerc0441d12013-07-31 11:28:24 -0700944 property_get("ro.build.display.id", recovery_version, "");
Doug Zongker56c51052010-07-01 09:18:44 -0700945 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800946
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800947 int status = INSTALL_SUCCESS;
948
Doug Zongker540d57f2011-01-18 13:36:58 -0800949 if (update_package != NULL) {
Doug Zongker075ad802014-06-26 15:35:51 -0700950 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE, true);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700951 if (status == INSTALL_SUCCESS && wipe_cache) {
952 if (erase_volume("/cache")) {
953 LOGE("Cache wipe (requested by package) failed.");
954 }
955 }
Doug Zongker7c3ae452013-05-14 11:03:02 -0700956 if (status != INSTALL_SUCCESS) {
957 ui->Print("Installation aborted.\n");
958
959 // If this is an eng or userdebug build, then automatically
960 // turn the text display on if the script fails so the error
961 // message is visible.
Elliott Hughesf14af802015-02-10 14:46:14 -0800962 if (is_ro_debuggable()) {
Doug Zongker7c3ae452013-05-14 11:03:02 -0700963 ui->ShowText(true);
964 }
965 }
Doug Zongkerb128f542009-06-18 15:07:14 -0700966 } else if (wipe_data) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700967 if (device->WipeData()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700968 if (erase_volume("/data")) status = INSTALL_ERROR;
969 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Andres Moralesee193872014-08-05 19:49:09 -0700970 if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700971 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -0700972 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700973 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -0700974 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700975 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -0700976 status = INSTALL_NONE; // No command specified
977 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800978 }
979
Doug Zongker02ec6b82012-08-22 17:26:40 -0700980 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700981 copy_logs();
Doug Zongker02ec6b82012-08-22 17:26:40 -0700982 ui->SetBackground(RecoveryUI::ERROR);
983 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700984 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
Doug Zongker211aebc2011-10-28 15:13:10 -0700985 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700986 Device::BuiltinAction temp = prompt_and_wait(device, status);
987 if (temp != Device::NO_ACTION) after = temp;
Doug Zongker8674a722010-09-15 11:08:23 -0700988 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800989
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700990 // Save logs and clean up before rebooting or shutting down.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800991 finish_recovery(send_intent);
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700992
993 switch (after) {
994 case Device::SHUTDOWN:
995 ui->Print("Shutting down...\n");
996 property_set(ANDROID_RB_PROPERTY, "shutdown,");
997 break;
998
999 case Device::REBOOT_BOOTLOADER:
1000 ui->Print("Rebooting to bootloader...\n");
1001 property_set(ANDROID_RB_PROPERTY, "reboot,bootloader");
1002 break;
1003
1004 default:
1005 ui->Print("Rebooting...\n");
1006 property_set(ANDROID_RB_PROPERTY, "reboot,");
1007 break;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001008 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001009 sleep(5); // should reboot before this finishes
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001010 return EXIT_SUCCESS;
1011}