blob: 176ac7a070b6f5ca77346639c63653ff72f95f41 [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>
Patrick Tjincd055ee2014-12-09 11:26:40 -080028#include <sys/klog.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070029#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080030#include <sys/types.h>
31#include <time.h>
32#include <unistd.h>
33
34#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include "common.h"
36#include "cutils/properties.h"
Ken Sumrall6e4472a2011-03-07 23:37:27 -080037#include "cutils/android_reboot.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080038#include "install.h"
39#include "minui/minui.h"
40#include "minzip/DirUtil.h"
41#include "roots.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070042#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070043#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070044#include "device.h"
Doug Zongker9270a202012-01-09 15:16:13 -080045#include "adb_install.h"
46extern "C" {
47#include "minadbd/adb.h"
Doug Zongker945fc682014-07-10 10:50:39 -070048#include "fuse_sideload.h"
49#include "fuse_sdcard_provider.h"
Doug Zongker9270a202012-01-09 15:16:13 -080050}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051
Stephen Smalley779701d2012-02-09 14:13:23 -050052struct selabel_handle *sehandle;
53
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080054static const struct option OPTIONS[] = {
55 { "send_intent", required_argument, NULL, 's' },
56 { "update_package", required_argument, NULL, 'u' },
57 { "wipe_data", no_argument, NULL, 'w' },
58 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070059 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070060 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker02ec6b82012-08-22 17:26:40 -070061 { "locale", required_argument, NULL, 'l' },
Doug Zongkerc87bab12013-11-25 13:53:25 -080062 { "stages", required_argument, NULL, 'g' },
Doug Zongkerb1d12632014-03-18 10:32:12 -070063 { "shutdown_after", no_argument, NULL, 'p' },
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -070064 { "reason", required_argument, NULL, 'r' },
Doug Zongker988500b2009-10-06 14:41:38 -070065 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066};
67
Doug Zongkerda1ebae2013-05-16 11:23:48 -070068#define LAST_LOG_FILE "/cache/recovery/last_log"
69
Doug Zongker6d0d7ac2013-07-09 13:34:55 -070070static const char *CACHE_LOG_DIR = "/cache/recovery";
Doug Zongkerd4208f92010-09-20 12:16:13 -070071static const char *COMMAND_FILE = "/cache/recovery/command";
72static const char *INTENT_FILE = "/cache/recovery/intent";
73static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070074static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -070075static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Michael Ward9d2629c2011-06-23 22:14:24 -070076static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070077static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080078static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070079static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Patrick Tjincd055ee2014-12-09 11:26:40 -080080static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
81#define KLOG_DEFAULT_LEN (64 * 1024)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080082
Nick Kralevicha9ad0322014-10-22 18:38:48 -070083#define KEEP_LOG_COUNT 10
84
Doug Zongker211aebc2011-10-28 15:13:10 -070085RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070086char* locale = NULL;
Doug Zongkerc0441d12013-07-31 11:28:24 -070087char recovery_version[PROPERTY_VALUE_MAX+1];
Doug Zongkerc87bab12013-11-25 13:53:25 -080088char* stage = NULL;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -070089char* reason = NULL;
Doug Zongker211aebc2011-10-28 15:13:10 -070090
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091/*
92 * The recovery tool communicates with the main system through /cache files.
93 * /cache/recovery/command - INPUT - command line for tool, one arg per line
94 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
95 * /cache/recovery/intent - OUTPUT - intent that was passed in
96 *
97 * The arguments which may be supplied in the recovery.command file:
98 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070099 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800100 * --wipe_data - erase user data (and cache), then reboot
101 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -0800102 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700103 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104 *
105 * After completing, we remove /cache/recovery/command and reboot.
106 * Arguments may also be supplied in the bootloader control block (BCB).
107 * These important scenarios must be safely restartable at any point:
108 *
109 * FACTORY RESET
110 * 1. user selects "factory reset"
111 * 2. main system writes "--wipe_data" to /cache/recovery/command
112 * 3. main system reboots into recovery
113 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
114 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700115 * 5. erase_volume() reformats /data
116 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800117 * 7. finish_recovery() erases BCB
118 * -- after this, rebooting will restart the main system --
119 * 8. main() calls reboot() to boot main system
120 *
121 * OTA INSTALL
122 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700123 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800124 * 3. main system reboots into recovery
125 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
126 * -- after this, rebooting will attempt to reinstall the update --
127 * 5. install_package() attempts to install the update
128 * NOTE: the package install must itself be restartable from any point
129 * 6. finish_recovery() erases BCB
130 * -- after this, rebooting will (try to) restart the main system --
131 * 7. ** if install failed **
132 * 7a. prompt_and_wait() shows an error icon and waits for the user
133 * 7b; the user reboots (pulling the battery, etc) into the main system
134 * 8. main() calls maybe_install_firmware_update()
135 * ** if the update contained radio/hboot firmware **:
136 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
137 * -- after this, rebooting will reformat cache & restart main system --
138 * 8b. m_i_f_u() writes firmware image into raw cache partition
139 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
140 * -- after this, rebooting will attempt to reinstall firmware --
141 * 8d. bootloader tries to flash firmware
142 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
143 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700144 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800145 * 8g. finish_recovery() erases BCB
146 * -- after this, rebooting will (try to) restart the main system --
147 * 9. main() calls reboot() to boot main system
148 */
149
150static const int MAX_ARG_LENGTH = 4096;
151static const int MAX_ARGS = 100;
152
Doug Zongkerd4208f92010-09-20 12:16:13 -0700153// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700154FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700155fopen_path(const char *path, const char *mode) {
156 if (ensure_path_mounted(path) != 0) {
157 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158 return NULL;
159 }
160
161 // When writing, try to create the containing directory, if necessary.
162 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500163 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800164
165 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800166 return fp;
167}
168
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700169static void redirect_stdio(const char* filename) {
170 // If these fail, there's not really anywhere to complain...
171 freopen(filename, "a", stdout); setbuf(stdout, NULL);
172 freopen(filename, "a", stderr); setbuf(stderr, NULL);
173}
174
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175// close a file, log an error if the error indicator is set
176static void
177check_and_fclose(FILE *fp, const char *name) {
178 fflush(fp);
179 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
180 fclose(fp);
181}
182
183// command line args come from, in decreasing precedence:
184// - the actual command line
185// - the bootloader control block (one per line, after "recovery")
186// - the contents of COMMAND_FILE (one per line)
187static void
188get_args(int *argc, char ***argv) {
189 struct bootloader_message boot;
190 memset(&boot, 0, sizeof(boot));
191 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Doug Zongkerc87bab12013-11-25 13:53:25 -0800192 stage = strndup(boot.stage, sizeof(boot.stage));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800193
194 if (boot.command[0] != 0 && boot.command[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700195 LOGI("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800196 }
197
198 if (boot.status[0] != 0 && boot.status[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700199 LOGI("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800200 }
201
202 // --- if arguments weren't supplied, look in the bootloader control block
203 if (*argc <= 1) {
204 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
205 const char *arg = strtok(boot.recovery, "\n");
206 if (arg != NULL && !strcmp(arg, "recovery")) {
207 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
208 (*argv)[0] = strdup(arg);
209 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
210 if ((arg = strtok(NULL, "\n")) == NULL) break;
211 (*argv)[*argc] = strdup(arg);
212 }
213 LOGI("Got arguments from boot message\n");
214 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
215 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
216 }
217 }
218
219 // --- if that doesn't work, try the command file
220 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700221 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800222 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800223 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800224 char *argv0 = (*argv)[0];
225 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
226 (*argv)[0] = argv0; // use the same program name
227
228 char buf[MAX_ARG_LENGTH];
229 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
230 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800231 token = strtok(buf, "\r\n");
232 if (token != NULL) {
233 (*argv)[*argc] = strdup(token); // Strip newline.
234 } else {
235 --*argc;
236 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800237 }
238
239 check_and_fclose(fp, COMMAND_FILE);
240 LOGI("Got arguments from %s\n", COMMAND_FILE);
241 }
242 }
243
244 // --> write the arguments we have back into the bootloader control block
245 // always boot into recovery after this (until finish_recovery() is called)
246 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
247 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
248 int i;
249 for (i = 1; i < *argc; ++i) {
250 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
251 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
252 }
253 set_bootloader_message(&boot);
254}
255
Doug Zongker34c98df2009-08-18 12:05:45 -0700256static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800257set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700258 struct bootloader_message boot;
259 memset(&boot, 0, sizeof(boot));
260 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
261 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
262 set_bootloader_message(&boot);
263}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800264
Patrick Tjincd055ee2014-12-09 11:26:40 -0800265// read from kernel log into buffer and write out to file
266static void
267save_kernel_log(const char *destination) {
268 int n;
269 char *buffer;
270 int klog_buf_len;
271 FILE *log;
272
273 klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
274 if (klog_buf_len <= 0) {
275 LOGE("Error getting klog size (%s), using default\n", strerror(errno));
276 klog_buf_len = KLOG_DEFAULT_LEN;
277 }
278
279 buffer = (char *)malloc(klog_buf_len);
280 if (!buffer) {
281 LOGE("Can't alloc %d bytes for klog buffer\n", klog_buf_len);
282 return;
283 }
284
285 n = klogctl(KLOG_READ_ALL, buffer, klog_buf_len);
286 if (n < 0) {
287 LOGE("Error in reading klog (%s)\n", strerror(errno));
288 free(buffer);
289 return;
290 }
291
292 log = fopen_path(destination, "w");
293 if (log == NULL) {
294 LOGE("Can't open %s\n", destination);
295 free(buffer);
296 return;
297 }
298 fwrite(buffer, n, 1, log);
299 check_and_fclose(log, destination);
300 free(buffer);
301}
302
Doug Zongker2c3539e2010-09-29 13:21:30 -0700303// How much of the temp log we have copied to the copy in cache.
304static long tmplog_offset = 0;
305
306static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700307copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700308 FILE *log = fopen_path(destination, append ? "a" : "w");
309 if (log == NULL) {
310 LOGE("Can't open %s\n", destination);
311 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700312 FILE *tmplog = fopen(source, "r");
313 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700314 if (append) {
315 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
316 }
317 char buf[4096];
318 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
319 if (append) {
320 tmplog_offset = ftell(tmplog);
321 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700322 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700323 }
324 check_and_fclose(log, destination);
325 }
326}
327
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700328// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max
329// Overwrites any existing last_log.$max.
330static void
331rotate_last_logs(int max) {
332 char oldfn[256];
333 char newfn[256];
334
335 int i;
336 for (i = max-1; i >= 0; --i) {
337 snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i);
338 snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1);
339 // ignore errors
340 rename(oldfn, newfn);
341 }
342}
Doug Zongker2c3539e2010-09-29 13:21:30 -0700343
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700344static void
345copy_logs() {
346 // Copy logs to cache so the system can find out what happened.
347 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
348 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
349 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
Patrick Tjincd055ee2014-12-09 11:26:40 -0800350 save_kernel_log(LAST_KMSG_FILE);
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700351 chmod(LOG_FILE, 0600);
352 chown(LOG_FILE, 1000, 1000); // system user
Patrick Tjincd055ee2014-12-09 11:26:40 -0800353 chmod(LAST_KMSG_FILE, 0600);
354 chown(LAST_KMSG_FILE, 1000, 1000); // system user
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700355 chmod(LAST_LOG_FILE, 0640);
356 chmod(LAST_INSTALL_FILE, 0644);
357 sync();
358}
359
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800360// clear the recovery command and prepare to boot a (hopefully working) system,
361// copy our log file to cache as well (for the system to read), and
362// record any intent we were asked to communicate back to the system.
363// this function is idempotent: call it as many times as you like.
364static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800365finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800366 // By this point, we're ready to return to the main system...
367 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700368 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000369 if (fp == NULL) {
370 LOGE("Can't open %s\n", INTENT_FILE);
371 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800372 fputs(send_intent, fp);
373 check_and_fclose(fp, INTENT_FILE);
374 }
375 }
376
Doug Zongker4f33e552012-08-23 13:16:12 -0700377 // Save the locale to cache, so if recovery is next started up
378 // without a --locale argument (eg, directly from the bootloader)
379 // it will use the last-known locale.
380 if (locale != NULL) {
381 LOGI("Saving locale \"%s\"\n", locale);
382 FILE* fp = fopen_path(LOCALE_FILE, "w");
383 fwrite(locale, 1, strlen(locale), fp);
384 fflush(fp);
385 fsync(fileno(fp));
386 check_and_fclose(fp, LOCALE_FILE);
387 }
388
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700389 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800390
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700391 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800392 struct bootloader_message boot;
393 memset(&boot, 0, sizeof(boot));
394 set_bootloader_message(&boot);
395
396 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700397 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
398 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800399 LOGW("Can't unlink %s\n", COMMAND_FILE);
400 }
401
Doug Zongkerd0181b82011-10-19 10:51:12 -0700402 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800403 sync(); // For good measure.
404}
405
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700406typedef struct _saved_log_file {
407 char* name;
408 struct stat st;
409 unsigned char* data;
410 struct _saved_log_file* next;
411} saved_log_file;
412
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800413static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700414erase_volume(const char *volume) {
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700415 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
416
Doug Zongker02ec6b82012-08-22 17:26:40 -0700417 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700418 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700419
420 saved_log_file* head = NULL;
421
422 if (is_cache) {
423 // If we're reformatting /cache, we load any
424 // "/cache/recovery/last*" files into memory, so we can restore
425 // them after the reformat.
426
427 ensure_path_mounted(volume);
428
429 DIR* d;
430 struct dirent* de;
431 d = opendir(CACHE_LOG_DIR);
432 if (d) {
433 char path[PATH_MAX];
434 strcpy(path, CACHE_LOG_DIR);
435 strcat(path, "/");
436 int path_len = strlen(path);
437 while ((de = readdir(d)) != NULL) {
438 if (strncmp(de->d_name, "last", 4) == 0) {
439 saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file));
440 strcpy(path+path_len, de->d_name);
441 p->name = strdup(path);
442 if (stat(path, &(p->st)) == 0) {
443 // truncate files to 512kb
444 if (p->st.st_size > (1 << 19)) {
445 p->st.st_size = 1 << 19;
446 }
447 p->data = (unsigned char*) malloc(p->st.st_size);
448 FILE* f = fopen(path, "rb");
449 fread(p->data, 1, p->st.st_size, f);
450 fclose(f);
451 p->next = head;
452 head = p;
453 } else {
454 free(p);
455 }
456 }
457 }
458 closedir(d);
459 } else {
460 if (errno != ENOENT) {
461 printf("opendir failed: %s\n", strerror(errno));
462 }
463 }
464 }
465
Doug Zongker211aebc2011-10-28 15:13:10 -0700466 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700467
Doug Zongkerd0181b82011-10-19 10:51:12 -0700468 ensure_path_unmounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700469 int result = format_volume(volume);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700470
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700471 if (is_cache) {
472 while (head) {
473 FILE* f = fopen_path(head->name, "wb");
474 if (f) {
475 fwrite(head->data, 1, head->st.st_size, f);
476 fclose(f);
477 chmod(head->name, head->st.st_mode);
478 chown(head->name, head->st.st_uid, head->st.st_gid);
479 }
480 free(head->name);
481 free(head->data);
482 saved_log_file* temp = head->next;
483 free(head);
484 head = temp;
485 }
486
Doug Zongker2c3539e2010-09-29 13:21:30 -0700487 // Any part of the log we'd copied to cache is now gone.
488 // Reset the pointer so we copy from the beginning of the temp
489 // log.
490 tmplog_offset = 0;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700491 copy_logs();
Doug Zongker2c3539e2010-09-29 13:21:30 -0700492 }
493
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700494 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800495}
496
Doug Zongker28ce47c2011-10-28 10:33:05 -0700497static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700498prepend_title(const char* const* headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700499 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700500 // caller-provided headers.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700501 int count = 3; // our title has 3 lines
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700502 const char* const* p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700503 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700504
Doug Zongker28ce47c2011-10-28 10:33:05 -0700505 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
506 const char** h = new_headers;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700507 *(h++) = "Android system recovery <" EXPAND(RECOVERY_API_VERSION) "e>";
508 *(h++) = recovery_version;
509 *(h++) = "";
Doug Zongkerf93d8162009-09-22 15:16:02 -0700510 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700511 *h = NULL;
512
Doug Zongkerf93d8162009-09-22 15:16:02 -0700513 return new_headers;
514}
515
516static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700517get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700518 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700519 // throw away keys pressed previously, so user doesn't
520 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700521 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700522
Doug Zongker211aebc2011-10-28 15:13:10 -0700523 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700524 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800525 int chosen_item = -1;
526
Doug Zongkerf93d8162009-09-22 15:16:02 -0700527 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700528 int key = ui->WaitKey();
529 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800530
Doug Zongker5cae4452011-01-25 13:15:30 -0800531 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700532 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800533 continue;
534 } else {
535 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700536 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700537 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800538 }
539 }
540
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700541 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700542
543 if (action < 0) {
544 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700545 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700546 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700547 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700548 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700549 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700550 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700551 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700552 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700553 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700554 chosen_item = selected;
555 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700556 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700557 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800558 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700559 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700560 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800561 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700562 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800563
Doug Zongker211aebc2011-10-28 15:13:10 -0700564 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700565 return chosen_item;
566}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800567
Doug Zongker8674a722010-09-15 11:08:23 -0700568static int compare_string(const void* a, const void* b) {
569 return strcmp(*(const char**)a, *(const char**)b);
570}
571
Doug Zongker93950222014-07-08 14:10:23 -0700572// Returns a malloc'd path, or NULL.
573static char*
574browse_directory(const char* path, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700575 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700576
Doug Zongker8674a722010-09-15 11:08:23 -0700577 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700578 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700579 "",
580 NULL };
581 DIR* d;
582 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700583 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700584 if (d == NULL) {
585 LOGE("error opening %s: %s\n", path, strerror(errno));
Doug Zongker93950222014-07-08 14:10:23 -0700586 return NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700587 }
588
Doug Zongker28ce47c2011-10-28 10:33:05 -0700589 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700590
591 int d_size = 0;
592 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700593 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700594 int z_size = 1;
595 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700596 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700597 zips[0] = strdup("../");
598
599 while ((de = readdir(d)) != NULL) {
600 int name_len = strlen(de->d_name);
601
602 if (de->d_type == DT_DIR) {
603 // skip "." and ".." entries
604 if (name_len == 1 && de->d_name[0] == '.') continue;
605 if (name_len == 2 && de->d_name[0] == '.' &&
606 de->d_name[1] == '.') continue;
607
608 if (d_size >= d_alloc) {
609 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700610 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700611 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700612 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700613 strcpy(dirs[d_size], de->d_name);
614 dirs[d_size][name_len] = '/';
615 dirs[d_size][name_len+1] = '\0';
616 ++d_size;
617 } else if (de->d_type == DT_REG &&
618 name_len >= 4 &&
619 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
620 if (z_size >= z_alloc) {
621 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700622 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700623 }
624 zips[z_size++] = strdup(de->d_name);
625 }
626 }
627 closedir(d);
628
629 qsort(dirs, d_size, sizeof(char*), compare_string);
630 qsort(zips, z_size, sizeof(char*), compare_string);
631
632 // append dirs to the zips list
633 if (d_size + z_size + 1 > z_alloc) {
634 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700635 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700636 }
637 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
638 free(dirs);
639 z_size += d_size;
640 zips[z_size] = NULL;
641
Doug Zongker93950222014-07-08 14:10:23 -0700642 char* result;
Doug Zongker8674a722010-09-15 11:08:23 -0700643 int chosen_item = 0;
Doug Zongker93950222014-07-08 14:10:23 -0700644 while (true) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700645 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700646
647 char* item = zips[chosen_item];
648 int item_len = strlen(item);
649 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700650 // go up but continue browsing (if the caller is update_directory)
Doug Zongker93950222014-07-08 14:10:23 -0700651 result = NULL;
Doug Zongker8674a722010-09-15 11:08:23 -0700652 break;
653 }
Doug Zongker93950222014-07-08 14:10:23 -0700654
655 char new_path[PATH_MAX];
656 strlcpy(new_path, path, PATH_MAX);
657 strlcat(new_path, "/", PATH_MAX);
658 strlcat(new_path, item, PATH_MAX);
659
660 if (item[item_len-1] == '/') {
661 // recurse down into a subdirectory
662 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
663 result = browse_directory(new_path, device);
664 if (result) break;
665 } else {
666 // selected a zip file: return the malloc'd path to the caller.
667 result = strdup(new_path);
668 break;
669 }
670 }
Doug Zongker8674a722010-09-15 11:08:23 -0700671
672 int i;
673 for (i = 0; i < z_size; ++i) free(zips[i]);
674 free(zips);
675 free(headers);
676
677 return result;
678}
679
Doug Zongkerf93d8162009-09-22 15:16:02 -0700680static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700681wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700682 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700683 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700684
Doug Zongkerf93d8162009-09-22 15:16:02 -0700685 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700686 const char* headers[] = { "Confirm wipe of all user data?",
687 " THIS CAN NOT BE UNDONE.",
688 "",
689 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700690 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700691 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800692
Doug Zongker28ce47c2011-10-28 10:33:05 -0700693 const char* items[] = { " No",
694 " No",
695 " No",
696 " No",
697 " No",
698 " No",
699 " No",
700 " Yes -- delete all user data", // [7]
701 " No",
702 " No",
703 " No",
704 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800705
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700706 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700707 if (chosen_item != 7) {
708 return;
709 }
710 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700711
Doug Zongker211aebc2011-10-28 15:13:10 -0700712 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700713 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700714 erase_volume("/data");
715 erase_volume("/cache");
Andres Moralesee193872014-08-05 19:49:09 -0700716 erase_persistent_partition();
Doug Zongker211aebc2011-10-28 15:13:10 -0700717 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700718}
719
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700720static void file_to_ui(const char* fn) {
721 FILE *fp = fopen_path(fn, "re");
722 if (fp == NULL) {
723 ui->Print(" Unable to open %s: %s\n", fn, strerror(errno));
724 return;
725 }
726 char line[1024];
727 int ct = 0;
728 redirect_stdio("/dev/null");
729 while(fgets(line, sizeof(line), fp) != NULL) {
730 ui->Print("%s", line);
731 ct++;
732 if (ct % 30 == 0) {
733 // give the user time to glance at the entries
734 ui->WaitKey();
735 }
736 }
737 redirect_stdio(TEMPORARY_LOG_FILE);
738 fclose(fp);
739}
740
741static void choose_recovery_file(Device* device) {
Patrick Tjincd055ee2014-12-09 11:26:40 -0800742 unsigned int i;
743 unsigned int n;
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700744 static const char** title_headers = NULL;
745 char *filename;
746 const char* headers[] = { "Select file to view",
747 "",
748 NULL };
Patrick Tjincd055ee2014-12-09 11:26:40 -0800749 // "Go back" + LAST_KMSG_FILE + KEEP_LOG_COUNT + terminating NULL entry
750 char* entries[KEEP_LOG_COUNT + 3];
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700751 memset(entries, 0, sizeof(entries));
752
Patrick Tjincd055ee2014-12-09 11:26:40 -0800753 n = 0;
754 entries[n++] = strdup("Go back");
755
756 // Add kernel kmsg file if available
757 if ((ensure_path_mounted(LAST_KMSG_FILE) == 0) && (access(LAST_KMSG_FILE, R_OK) == 0)) {
758 entries[n++] = strdup(LAST_KMSG_FILE);
759 }
760
761 // Add LAST_LOG_FILE + LAST_LOG_FILE.x
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700762 for (i = 0; i < KEEP_LOG_COUNT; i++) {
763 char *filename;
764 if (asprintf(&filename, (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i) == -1) {
765 // memory allocation failure - return early. Should never happen.
766 return;
767 }
768 if ((ensure_path_mounted(filename) != 0) || (access(filename, R_OK) == -1)) {
769 free(filename);
Patrick Tjincd055ee2014-12-09 11:26:40 -0800770 entries[n++] = NULL;
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700771 break;
772 }
Patrick Tjincd055ee2014-12-09 11:26:40 -0800773 entries[n++] = filename;
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700774 }
775
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700776 title_headers = prepend_title((const char**)headers);
777
778 while(1) {
779 int chosen_item = get_menu_selection(title_headers, entries, 1, 0, device);
780 if (chosen_item == 0) break;
781 file_to_ui(entries[chosen_item]);
782 }
783
Patrick Tjincd055ee2014-12-09 11:26:40 -0800784 for (i = 0; i < (sizeof(entries) / sizeof(*entries)); i++) {
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700785 free(entries[i]);
786 }
787}
788
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700789// Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION
790// means to take the default, which is to reboot or shutdown depending
791// on if the --shutdown_after flag was passed to recovery.
792static Device::BuiltinAction
Doug Zongker6c8553d2012-09-24 10:40:47 -0700793prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700794 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700795
796 for (;;) {
797 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700798 switch (status) {
799 case INSTALL_SUCCESS:
800 case INSTALL_NONE:
801 ui->SetBackground(RecoveryUI::NO_COMMAND);
802 break;
803
804 case INSTALL_ERROR:
805 case INSTALL_CORRUPT:
806 ui->SetBackground(RecoveryUI::ERROR);
807 break;
808 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700809 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700810
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700811 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700812
813 // device-specific code may take some action here. It may
814 // return one of the core actions handled in the switch
815 // statement below.
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700816 Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700817
Doug Zongker93950222014-07-08 14:10:23 -0700818 int wipe_cache = 0;
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700819 switch (chosen_action) {
820 case Device::NO_ACTION:
821 break;
822
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700823 case Device::REBOOT:
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700824 case Device::SHUTDOWN:
825 case Device::REBOOT_BOOTLOADER:
826 return chosen_action;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700827
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700828 case Device::WIPE_DATA:
829 wipe_data(ui->IsTextVisible(), device);
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700830 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700831 break;
832
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700833 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700834 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700835 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700836 ui->Print("Cache wipe complete.\n");
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700837 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700838 break;
839
Doug Zongker93950222014-07-08 14:10:23 -0700840 case Device::APPLY_EXT: {
841 ensure_path_mounted(SDCARD_ROOT);
842 char* path = browse_directory(SDCARD_ROOT, device);
843 if (path == NULL) {
844 ui->Print("\n-- No package file selected.\n", path);
845 break;
846 }
847
848 ui->Print("\n-- Install %s ...\n", path);
849 set_sdcard_update_bootloader_message();
Doug Zongker945fc682014-07-10 10:50:39 -0700850 void* token = start_sdcard_fuse(path);
Doug Zongker93950222014-07-08 14:10:23 -0700851
Doug Zongker945fc682014-07-10 10:50:39 -0700852 int status = install_package(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache,
853 TEMPORARY_INSTALL_FILE, false);
854
855 finish_sdcard_fuse(token);
856 ensure_path_unmounted(SDCARD_ROOT);
Doug Zongker93950222014-07-08 14:10:23 -0700857
Doug Zongkerd0181b82011-10-19 10:51:12 -0700858 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700859 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700860 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700861 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700862 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700863 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700864 }
865 }
Doug Zongker945fc682014-07-10 10:50:39 -0700866
Doug Zongker8674a722010-09-15 11:08:23 -0700867 if (status >= 0) {
868 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700869 ui->SetBackground(RecoveryUI::ERROR);
870 ui->Print("Installation aborted.\n");
871 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700872 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker8674a722010-09-15 11:08:23 -0700873 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700874 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700875 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700876 }
877 break;
Doug Zongker93950222014-07-08 14:10:23 -0700878 }
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700879
880 case Device::APPLY_CACHE:
Doug Zongker93950222014-07-08 14:10:23 -0700881 ui->Print("\nAPPLY_CACHE is deprecated.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700882 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800883
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700884 case Device::READ_RECOVERY_LASTLOG:
885 choose_recovery_file(device);
886 break;
887
Doug Zongker9270a202012-01-09 15:16:13 -0800888 case Device::APPLY_ADB_SIDELOAD:
Doug Zongker9270a202012-01-09 15:16:13 -0800889 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
890 if (status >= 0) {
891 if (status != INSTALL_SUCCESS) {
892 ui->SetBackground(RecoveryUI::ERROR);
893 ui->Print("Installation aborted.\n");
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700894 copy_logs();
Doug Zongker9270a202012-01-09 15:16:13 -0800895 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700896 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker9270a202012-01-09 15:16:13 -0800897 } else {
898 ui->Print("\nInstall from ADB complete.\n");
899 }
900 }
901 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800902 }
903 }
904}
905
906static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800907print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700908 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800909}
910
Doug Zongker02ec6b82012-08-22 17:26:40 -0700911static void
912load_locale_from_cache() {
913 FILE* fp = fopen_path(LOCALE_FILE, "r");
914 char buffer[80];
915 if (fp != NULL) {
916 fgets(buffer, sizeof(buffer), fp);
917 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700918 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700919 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
920 if (!isspace(buffer[i])) {
921 buffer[j++] = buffer[i];
922 }
923 }
924 buffer[j] = 0;
925 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700926 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700927 }
928}
929
Doug Zongker7c3ae452013-05-14 11:03:02 -0700930static RecoveryUI* gCurrentUI = NULL;
931
932void
933ui_print(const char* format, ...) {
934 char buffer[256];
935
936 va_list ap;
937 va_start(ap, format);
938 vsnprintf(buffer, sizeof(buffer), format, ap);
939 va_end(ap);
940
941 if (gCurrentUI != NULL) {
942 gCurrentUI->Print("%s", buffer);
943 } else {
944 fputs(buffer, stdout);
945 }
946}
947
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800948int
Oscar Montemayor05231562009-11-30 08:40:57 -0800949main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800950 time_t start = time(NULL);
951
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700952 redirect_stdio(TEMPORARY_LOG_FILE);
Doug Zongker9270a202012-01-09 15:16:13 -0800953
954 // If this binary is started with the single argument "--adbd",
955 // instead of being the normal recovery binary, it turns into kind
956 // of a stripped-down version of adbd that only supports the
957 // 'sideload' command. Note this must be a real argument, not
958 // anything in the command file or bootloader control block; the
959 // only way recovery should be run with this argument is when it
960 // starts a copy of itself from the apply_from_adb() function.
961 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
962 adb_main();
963 return 0;
964 }
965
Doug Zongker19a8e242014-01-21 09:25:41 -0800966 printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800967
Doug Zongkerd4208f92010-09-20 12:16:13 -0700968 load_volume_table();
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700969 ensure_path_mounted(LAST_LOG_FILE);
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700970 rotate_last_logs(KEEP_LOG_COUNT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800971 get_args(&argc, &argv);
972
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800973 const char *send_intent = NULL;
974 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700975 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700976 bool just_exit = false;
Doug Zongkerb1d12632014-03-18 10:32:12 -0700977 bool shutdown_after = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800978
979 int arg;
980 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
981 switch (arg) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800982 case 's': send_intent = optarg; break;
983 case 'u': update_package = optarg; break;
984 case 'w': wipe_data = wipe_cache = 1; break;
985 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700986 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700987 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700988 case 'l': locale = optarg; break;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800989 case 'g': {
990 if (stage == NULL || *stage == '\0') {
991 char buffer[20] = "1/";
992 strncat(buffer, optarg, sizeof(buffer)-3);
993 stage = strdup(buffer);
994 }
995 break;
996 }
Doug Zongkerb1d12632014-03-18 10:32:12 -0700997 case 'p': shutdown_after = true; break;
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -0700998 case 'r': reason = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800999 case '?':
1000 LOGE("Invalid command argument\n");
1001 continue;
1002 }
1003 }
1004
Doug Zongker02ec6b82012-08-22 17:26:40 -07001005 if (locale == NULL) {
1006 load_locale_from_cache();
1007 }
1008 printf("locale is [%s]\n", locale);
Doug Zongker0d32f252014-02-13 15:07:56 -08001009 printf("stage is [%s]\n", stage);
Jeff Sharkeya6e13ae2014-09-24 11:46:17 -07001010 printf("reason is [%s]\n", reason);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001011
1012 Device* device = make_device();
1013 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -07001014 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -07001015
Doug Zongker5fa8c232012-09-18 12:37:02 -07001016 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001017 ui->Init();
Doug Zongkerc87bab12013-11-25 13:53:25 -08001018
1019 int st_cur, st_max;
1020 if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {
1021 ui->SetStage(st_cur, st_max);
1022 }
1023
Doug Zongker02ec6b82012-08-22 17:26:40 -07001024 ui->SetBackground(RecoveryUI::NONE);
1025 if (show_text) ui->ShowText(true);
1026
Stephen Smalley779701d2012-02-09 14:13:23 -05001027 struct selinux_opt seopts[] = {
1028 { SELABEL_OPT_PATH, "/file_contexts" }
1029 };
1030
1031 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
1032
1033 if (!sehandle) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001034 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -05001035 }
Stephen Smalley779701d2012-02-09 14:13:23 -05001036
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001037 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -08001038
Doug Zongker56c51052010-07-01 09:18:44 -07001039 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001040 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -07001041 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001042 }
Doug Zongker9b125b02010-09-22 12:01:37 -07001043 printf("\n");
1044
1045 if (update_package) {
1046 // For backwards compatibility on the cache partition only, if
1047 // we're given an old 'root' path "CACHE:foo", change it to
1048 // "/cache/foo".
1049 if (strncmp(update_package, "CACHE:", 6) == 0) {
1050 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -07001051 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -07001052 strlcpy(modified_path, "/cache/", len);
1053 strlcat(modified_path, update_package+6, len);
1054 printf("(replacing path \"%s\" with \"%s\")\n",
1055 update_package, modified_path);
1056 update_package = modified_path;
1057 }
1058 }
1059 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001060
1061 property_list(print_property, NULL);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001062 property_get("ro.build.display.id", recovery_version, "");
Doug Zongker56c51052010-07-01 09:18:44 -07001063 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001064
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001065 int status = INSTALL_SUCCESS;
1066
Doug Zongker540d57f2011-01-18 13:36:58 -08001067 if (update_package != NULL) {
Doug Zongker075ad802014-06-26 15:35:51 -07001068 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE, true);
Doug Zongkerd0181b82011-10-19 10:51:12 -07001069 if (status == INSTALL_SUCCESS && wipe_cache) {
1070 if (erase_volume("/cache")) {
1071 LOGE("Cache wipe (requested by package) failed.");
1072 }
1073 }
Doug Zongker7c3ae452013-05-14 11:03:02 -07001074 if (status != INSTALL_SUCCESS) {
1075 ui->Print("Installation aborted.\n");
1076
1077 // If this is an eng or userdebug build, then automatically
1078 // turn the text display on if the script fails so the error
1079 // message is visible.
1080 char buffer[PROPERTY_VALUE_MAX+1];
1081 property_get("ro.build.fingerprint", buffer, "");
1082 if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {
1083 ui->ShowText(true);
1084 }
1085 }
Doug Zongkerb128f542009-06-18 15:07:14 -07001086 } else if (wipe_data) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001087 if (device->WipeData()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -07001088 if (erase_volume("/data")) status = INSTALL_ERROR;
1089 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Andres Moralesee193872014-08-05 19:49:09 -07001090 if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001091 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -07001092 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -07001093 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001094 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001095 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -07001096 status = INSTALL_NONE; // No command specified
1097 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001098 }
1099
Doug Zongker02ec6b82012-08-22 17:26:40 -07001100 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
Doug Zongkerf24fd7e2013-07-02 11:43:25 -07001101 copy_logs();
Doug Zongker02ec6b82012-08-22 17:26:40 -07001102 ui->SetBackground(RecoveryUI::ERROR);
1103 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001104 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
Doug Zongker211aebc2011-10-28 15:13:10 -07001105 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001106 Device::BuiltinAction temp = prompt_and_wait(device, status);
1107 if (temp != Device::NO_ACTION) after = temp;
Doug Zongker8674a722010-09-15 11:08:23 -07001108 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001109
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001110 // Save logs and clean up before rebooting or shutting down.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001111 finish_recovery(send_intent);
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001112
1113 switch (after) {
1114 case Device::SHUTDOWN:
1115 ui->Print("Shutting down...\n");
1116 property_set(ANDROID_RB_PROPERTY, "shutdown,");
1117 break;
1118
1119 case Device::REBOOT_BOOTLOADER:
1120 ui->Print("Rebooting to bootloader...\n");
1121 property_set(ANDROID_RB_PROPERTY, "reboot,bootloader");
1122 break;
1123
1124 default:
1125 ui->Print("Rebooting...\n");
1126 property_set(ANDROID_RB_PROPERTY, "reboot,");
1127 break;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001128 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001129 sleep(5); // should reboot before this finishes
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001130 return EXIT_SUCCESS;
1131}