blob: c5a589cc60ee2fe5b0a44db3cea33ca0f67f60fa [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ctype.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070018#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <errno.h>
20#include <fcntl.h>
21#include <getopt.h>
22#include <limits.h>
23#include <linux/input.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070024#include <stdarg.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070028#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include <sys/types.h>
30#include <time.h>
31#include <unistd.h>
32
33#include "bootloader.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include "common.h"
35#include "cutils/properties.h"
Ken Sumrall6e4472a2011-03-07 23:37:27 -080036#include "cutils/android_reboot.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080037#include "install.h"
38#include "minui/minui.h"
39#include "minzip/DirUtil.h"
40#include "roots.h"
Doug Zongker28ce47c2011-10-28 10:33:05 -070041#include "ui.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070042#include "screen_ui.h"
Doug Zongkerdaefc1d2011-10-31 09:34:15 -070043#include "device.h"
Doug Zongker9270a202012-01-09 15:16:13 -080044#include "adb_install.h"
45extern "C" {
46#include "minadbd/adb.h"
47}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080048
Stephen Smalley779701d2012-02-09 14:13:23 -050049struct selabel_handle *sehandle;
50
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080051static const struct option OPTIONS[] = {
52 { "send_intent", required_argument, NULL, 's' },
53 { "update_package", required_argument, NULL, 'u' },
54 { "wipe_data", no_argument, NULL, 'w' },
55 { "wipe_cache", no_argument, NULL, 'c' },
Doug Zongker4bc98062010-09-03 11:00:13 -070056 { "show_text", no_argument, NULL, 't' },
Doug Zongkere5d5ac72012-04-12 11:01:22 -070057 { "just_exit", no_argument, NULL, 'x' },
Doug Zongker02ec6b82012-08-22 17:26:40 -070058 { "locale", required_argument, NULL, 'l' },
Doug Zongker988500b2009-10-06 14:41:38 -070059 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080060};
61
Doug Zongkerda1ebae2013-05-16 11:23:48 -070062#define LAST_LOG_FILE "/cache/recovery/last_log"
63
Doug Zongker6d0d7ac2013-07-09 13:34:55 -070064static const char *CACHE_LOG_DIR = "/cache/recovery";
Doug Zongkerd4208f92010-09-20 12:16:13 -070065static const char *COMMAND_FILE = "/cache/recovery/command";
66static const char *INTENT_FILE = "/cache/recovery/intent";
67static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070068static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -070069static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Michael Ward9d2629c2011-06-23 22:14:24 -070070static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070071static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070073static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070074static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080075
Doug Zongker211aebc2011-10-28 15:13:10 -070076RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070077char* locale = NULL;
Doug Zongker211aebc2011-10-28 15:13:10 -070078
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080079/*
80 * The recovery tool communicates with the main system through /cache files.
81 * /cache/recovery/command - INPUT - command line for tool, one arg per line
82 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
83 * /cache/recovery/intent - OUTPUT - intent that was passed in
84 *
85 * The arguments which may be supplied in the recovery.command file:
86 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070087 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080088 * --wipe_data - erase user data (and cache), then reboot
89 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080090 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -070091 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092 *
93 * After completing, we remove /cache/recovery/command and reboot.
94 * Arguments may also be supplied in the bootloader control block (BCB).
95 * These important scenarios must be safely restartable at any point:
96 *
97 * FACTORY RESET
98 * 1. user selects "factory reset"
99 * 2. main system writes "--wipe_data" to /cache/recovery/command
100 * 3. main system reboots into recovery
101 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
102 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700103 * 5. erase_volume() reformats /data
104 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800105 * 7. finish_recovery() erases BCB
106 * -- after this, rebooting will restart the main system --
107 * 8. main() calls reboot() to boot main system
108 *
109 * OTA INSTALL
110 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700111 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800112 * 3. main system reboots into recovery
113 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
114 * -- after this, rebooting will attempt to reinstall the update --
115 * 5. install_package() attempts to install the update
116 * NOTE: the package install must itself be restartable from any point
117 * 6. finish_recovery() erases BCB
118 * -- after this, rebooting will (try to) restart the main system --
119 * 7. ** if install failed **
120 * 7a. prompt_and_wait() shows an error icon and waits for the user
121 * 7b; the user reboots (pulling the battery, etc) into the main system
122 * 8. main() calls maybe_install_firmware_update()
123 * ** if the update contained radio/hboot firmware **:
124 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
125 * -- after this, rebooting will reformat cache & restart main system --
126 * 8b. m_i_f_u() writes firmware image into raw cache partition
127 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
128 * -- after this, rebooting will attempt to reinstall firmware --
129 * 8d. bootloader tries to flash firmware
130 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
131 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700132 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800133 * 8g. finish_recovery() erases BCB
134 * -- after this, rebooting will (try to) restart the main system --
135 * 9. main() calls reboot() to boot main system
136 */
137
138static const int MAX_ARG_LENGTH = 4096;
139static const int MAX_ARGS = 100;
140
Doug Zongkerd4208f92010-09-20 12:16:13 -0700141// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700142FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700143fopen_path(const char *path, const char *mode) {
144 if (ensure_path_mounted(path) != 0) {
145 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800146 return NULL;
147 }
148
149 // When writing, try to create the containing directory, if necessary.
150 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500151 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800152
153 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800154 return fp;
155}
156
157// close a file, log an error if the error indicator is set
158static void
159check_and_fclose(FILE *fp, const char *name) {
160 fflush(fp);
161 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
162 fclose(fp);
163}
164
165// command line args come from, in decreasing precedence:
166// - the actual command line
167// - the bootloader control block (one per line, after "recovery")
168// - the contents of COMMAND_FILE (one per line)
169static void
170get_args(int *argc, char ***argv) {
171 struct bootloader_message boot;
172 memset(&boot, 0, sizeof(boot));
173 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
174
175 if (boot.command[0] != 0 && boot.command[0] != 255) {
176 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
177 }
178
179 if (boot.status[0] != 0 && boot.status[0] != 255) {
180 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
181 }
182
183 // --- if arguments weren't supplied, look in the bootloader control block
184 if (*argc <= 1) {
185 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
186 const char *arg = strtok(boot.recovery, "\n");
187 if (arg != NULL && !strcmp(arg, "recovery")) {
188 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
189 (*argv)[0] = strdup(arg);
190 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
191 if ((arg = strtok(NULL, "\n")) == NULL) break;
192 (*argv)[*argc] = strdup(arg);
193 }
194 LOGI("Got arguments from boot message\n");
195 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
196 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
197 }
198 }
199
200 // --- if that doesn't work, try the command file
201 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700202 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800204 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800205 char *argv0 = (*argv)[0];
206 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
207 (*argv)[0] = argv0; // use the same program name
208
209 char buf[MAX_ARG_LENGTH];
210 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
211 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800212 token = strtok(buf, "\r\n");
213 if (token != NULL) {
214 (*argv)[*argc] = strdup(token); // Strip newline.
215 } else {
216 --*argc;
217 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800218 }
219
220 check_and_fclose(fp, COMMAND_FILE);
221 LOGI("Got arguments from %s\n", COMMAND_FILE);
222 }
223 }
224
225 // --> write the arguments we have back into the bootloader control block
226 // always boot into recovery after this (until finish_recovery() is called)
227 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
228 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
229 int i;
230 for (i = 1; i < *argc; ++i) {
231 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
232 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
233 }
234 set_bootloader_message(&boot);
235}
236
Doug Zongker34c98df2009-08-18 12:05:45 -0700237static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800238set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700239 struct bootloader_message boot;
240 memset(&boot, 0, sizeof(boot));
241 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
242 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
243 set_bootloader_message(&boot);
244}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800245
Doug Zongker2c3539e2010-09-29 13:21:30 -0700246// How much of the temp log we have copied to the copy in cache.
247static long tmplog_offset = 0;
248
249static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700250copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700251 FILE *log = fopen_path(destination, append ? "a" : "w");
252 if (log == NULL) {
253 LOGE("Can't open %s\n", destination);
254 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700255 FILE *tmplog = fopen(source, "r");
256 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700257 if (append) {
258 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
259 }
260 char buf[4096];
261 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
262 if (append) {
263 tmplog_offset = ftell(tmplog);
264 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700265 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700266 }
267 check_and_fclose(log, destination);
268 }
269}
270
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700271// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max
272// Overwrites any existing last_log.$max.
273static void
274rotate_last_logs(int max) {
275 char oldfn[256];
276 char newfn[256];
277
278 int i;
279 for (i = max-1; i >= 0; --i) {
280 snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i);
281 snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1);
282 // ignore errors
283 rename(oldfn, newfn);
284 }
285}
Doug Zongker2c3539e2010-09-29 13:21:30 -0700286
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700287static void
288copy_logs() {
289 // Copy logs to cache so the system can find out what happened.
290 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
291 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
292 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
293 chmod(LOG_FILE, 0600);
294 chown(LOG_FILE, 1000, 1000); // system user
295 chmod(LAST_LOG_FILE, 0640);
296 chmod(LAST_INSTALL_FILE, 0644);
297 sync();
298}
299
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300// clear the recovery command and prepare to boot a (hopefully working) system,
301// copy our log file to cache as well (for the system to read), and
302// record any intent we were asked to communicate back to the system.
303// this function is idempotent: call it as many times as you like.
304static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800305finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800306 // By this point, we're ready to return to the main system...
307 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700308 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000309 if (fp == NULL) {
310 LOGE("Can't open %s\n", INTENT_FILE);
311 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800312 fputs(send_intent, fp);
313 check_and_fclose(fp, INTENT_FILE);
314 }
315 }
316
Doug Zongker4f33e552012-08-23 13:16:12 -0700317 // Save the locale to cache, so if recovery is next started up
318 // without a --locale argument (eg, directly from the bootloader)
319 // it will use the last-known locale.
320 if (locale != NULL) {
321 LOGI("Saving locale \"%s\"\n", locale);
322 FILE* fp = fopen_path(LOCALE_FILE, "w");
323 fwrite(locale, 1, strlen(locale), fp);
324 fflush(fp);
325 fsync(fileno(fp));
326 check_and_fclose(fp, LOCALE_FILE);
327 }
328
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700329 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800330
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700331 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800332 struct bootloader_message boot;
333 memset(&boot, 0, sizeof(boot));
334 set_bootloader_message(&boot);
335
336 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700337 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
338 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800339 LOGW("Can't unlink %s\n", COMMAND_FILE);
340 }
341
Doug Zongkerd0181b82011-10-19 10:51:12 -0700342 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800343 sync(); // For good measure.
344}
345
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700346typedef struct _saved_log_file {
347 char* name;
348 struct stat st;
349 unsigned char* data;
350 struct _saved_log_file* next;
351} saved_log_file;
352
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800353static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700354erase_volume(const char *volume) {
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700355 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
356
Doug Zongker02ec6b82012-08-22 17:26:40 -0700357 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700358 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700359
360 saved_log_file* head = NULL;
361
362 if (is_cache) {
363 // If we're reformatting /cache, we load any
364 // "/cache/recovery/last*" files into memory, so we can restore
365 // them after the reformat.
366
367 ensure_path_mounted(volume);
368
369 DIR* d;
370 struct dirent* de;
371 d = opendir(CACHE_LOG_DIR);
372 if (d) {
373 char path[PATH_MAX];
374 strcpy(path, CACHE_LOG_DIR);
375 strcat(path, "/");
376 int path_len = strlen(path);
377 while ((de = readdir(d)) != NULL) {
378 if (strncmp(de->d_name, "last", 4) == 0) {
379 saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file));
380 strcpy(path+path_len, de->d_name);
381 p->name = strdup(path);
382 if (stat(path, &(p->st)) == 0) {
383 // truncate files to 512kb
384 if (p->st.st_size > (1 << 19)) {
385 p->st.st_size = 1 << 19;
386 }
387 p->data = (unsigned char*) malloc(p->st.st_size);
388 FILE* f = fopen(path, "rb");
389 fread(p->data, 1, p->st.st_size, f);
390 fclose(f);
391 p->next = head;
392 head = p;
393 } else {
394 free(p);
395 }
396 }
397 }
398 closedir(d);
399 } else {
400 if (errno != ENOENT) {
401 printf("opendir failed: %s\n", strerror(errno));
402 }
403 }
404 }
405
Doug Zongker211aebc2011-10-28 15:13:10 -0700406 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700407
Doug Zongkerd0181b82011-10-19 10:51:12 -0700408 ensure_path_unmounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700409 int result = format_volume(volume);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700410
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700411 if (is_cache) {
412 while (head) {
413 FILE* f = fopen_path(head->name, "wb");
414 if (f) {
415 fwrite(head->data, 1, head->st.st_size, f);
416 fclose(f);
417 chmod(head->name, head->st.st_mode);
418 chown(head->name, head->st.st_uid, head->st.st_gid);
419 }
420 free(head->name);
421 free(head->data);
422 saved_log_file* temp = head->next;
423 free(head);
424 head = temp;
425 }
426
Doug Zongker2c3539e2010-09-29 13:21:30 -0700427 // Any part of the log we'd copied to cache is now gone.
428 // Reset the pointer so we copy from the beginning of the temp
429 // log.
430 tmplog_offset = 0;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700431 copy_logs();
Doug Zongker2c3539e2010-09-29 13:21:30 -0700432 }
433
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700434 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800435}
436
Doug Zongker23ceeea2010-07-08 17:27:55 -0700437static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700438copy_sideloaded_package(const char* original_path) {
439 if (ensure_path_mounted(original_path) != 0) {
440 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700441 return NULL;
442 }
443
Doug Zongkerd4208f92010-09-20 12:16:13 -0700444 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700445 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
446 return NULL;
447 }
448
Doug Zongkerd4208f92010-09-20 12:16:13 -0700449 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700450 if (errno != EEXIST) {
451 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
452 return NULL;
453 }
454 }
455
Doug Zongkerd4208f92010-09-20 12:16:13 -0700456 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
457 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700458 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700459 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
460 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700461 return NULL;
462 }
463 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700464 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700465 return NULL;
466 }
467 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700468 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700469 return NULL;
470 }
471 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700472 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700473 return NULL;
474 }
475
Doug Zongkerd4208f92010-09-20 12:16:13 -0700476 char copy_path[PATH_MAX];
477 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700478 strcat(copy_path, "/package.zip");
479
Doug Zongker28ce47c2011-10-28 10:33:05 -0700480 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700481 if (buffer == NULL) {
482 LOGE("Failed to allocate buffer\n");
483 return NULL;
484 }
485
486 size_t read;
487 FILE* fin = fopen(original_path, "rb");
488 if (fin == NULL) {
489 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
490 return NULL;
491 }
492 FILE* fout = fopen(copy_path, "wb");
493 if (fout == NULL) {
494 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
495 return NULL;
496 }
497
498 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
499 if (fwrite(buffer, 1, read, fout) != read) {
500 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
501 return NULL;
502 }
503 }
504
505 free(buffer);
506
507 if (fclose(fout) != 0) {
508 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
509 return NULL;
510 }
511
512 if (fclose(fin) != 0) {
513 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
514 return NULL;
515 }
516
517 // "adb push" is happy to overwrite read-only files when it's
518 // running as root, but we'll try anyway.
519 if (chmod(copy_path, 0400) != 0) {
520 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
521 return NULL;
522 }
523
Doug Zongkerd4208f92010-09-20 12:16:13 -0700524 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700525}
526
Doug Zongker28ce47c2011-10-28 10:33:05 -0700527static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700528prepend_title(const char* const* headers) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700529 const char* title[] = { "Android system recovery <"
530 EXPAND(RECOVERY_API_VERSION) "e>",
531 "",
532 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800533
Doug Zongkerd6837852009-06-17 22:07:13 -0700534 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700535 // caller-provided headers.
Doug Zongkerd6837852009-06-17 22:07:13 -0700536 int count = 0;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700537 const char* const* p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700538 for (p = title; *p; ++p, ++count);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700539 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700540
Doug Zongker28ce47c2011-10-28 10:33:05 -0700541 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
542 const char** h = new_headers;
Doug Zongkerd6837852009-06-17 22:07:13 -0700543 for (p = title; *p; ++p, ++h) *h = *p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700544 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700545 *h = NULL;
546
Doug Zongkerf93d8162009-09-22 15:16:02 -0700547 return new_headers;
548}
549
550static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700551get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700552 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700553 // throw away keys pressed previously, so user doesn't
554 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700555 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700556
Doug Zongker211aebc2011-10-28 15:13:10 -0700557 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700558 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800559 int chosen_item = -1;
560
Doug Zongkerf93d8162009-09-22 15:16:02 -0700561 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700562 int key = ui->WaitKey();
563 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800564
Doug Zongker5cae4452011-01-25 13:15:30 -0800565 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700566 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800567 continue;
568 } else {
569 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700570 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700571 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800572 }
573 }
574
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700575 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700576
577 if (action < 0) {
578 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700579 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700580 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700581 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700582 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700583 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700584 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700585 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700586 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700587 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700588 chosen_item = selected;
589 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700590 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700591 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800592 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700593 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700594 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800595 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700596 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800597
Doug Zongker211aebc2011-10-28 15:13:10 -0700598 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700599 return chosen_item;
600}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800601
Doug Zongker8674a722010-09-15 11:08:23 -0700602static int compare_string(const void* a, const void* b) {
603 return strcmp(*(const char**)a, *(const char**)b);
604}
605
606static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700607update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700608 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700609 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700610
Doug Zongker8674a722010-09-15 11:08:23 -0700611 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700612 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700613 "",
614 NULL };
615 DIR* d;
616 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700617 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700618 if (d == NULL) {
619 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700620 if (unmount_when_done != NULL) {
621 ensure_path_unmounted(unmount_when_done);
622 }
Doug Zongker8674a722010-09-15 11:08:23 -0700623 return 0;
624 }
625
Doug Zongker28ce47c2011-10-28 10:33:05 -0700626 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700627
628 int d_size = 0;
629 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700630 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700631 int z_size = 1;
632 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700633 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700634 zips[0] = strdup("../");
635
636 while ((de = readdir(d)) != NULL) {
637 int name_len = strlen(de->d_name);
638
639 if (de->d_type == DT_DIR) {
640 // skip "." and ".." entries
641 if (name_len == 1 && de->d_name[0] == '.') continue;
642 if (name_len == 2 && de->d_name[0] == '.' &&
643 de->d_name[1] == '.') continue;
644
645 if (d_size >= d_alloc) {
646 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700647 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700648 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700649 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700650 strcpy(dirs[d_size], de->d_name);
651 dirs[d_size][name_len] = '/';
652 dirs[d_size][name_len+1] = '\0';
653 ++d_size;
654 } else if (de->d_type == DT_REG &&
655 name_len >= 4 &&
656 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
657 if (z_size >= z_alloc) {
658 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700659 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700660 }
661 zips[z_size++] = strdup(de->d_name);
662 }
663 }
664 closedir(d);
665
666 qsort(dirs, d_size, sizeof(char*), compare_string);
667 qsort(zips, z_size, sizeof(char*), compare_string);
668
669 // append dirs to the zips list
670 if (d_size + z_size + 1 > z_alloc) {
671 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700672 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700673 }
674 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
675 free(dirs);
676 z_size += d_size;
677 zips[z_size] = NULL;
678
679 int result;
680 int chosen_item = 0;
681 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700682 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700683
684 char* item = zips[chosen_item];
685 int item_len = strlen(item);
686 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700687 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700688 result = -1;
689 break;
690 } else if (item[item_len-1] == '/') {
691 // recurse down into a subdirectory
692 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700693 strlcpy(new_path, path, PATH_MAX);
694 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700695 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700696 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700697 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700698 if (result >= 0) break;
699 } else {
700 // selected a zip file: attempt to install it, and return
701 // the status to the caller.
702 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700703 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700704 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700705 strlcat(new_path, item, PATH_MAX);
706
Doug Zongker211aebc2011-10-28 15:13:10 -0700707 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700708 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700709 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700710 if (unmount_when_done != NULL) {
711 ensure_path_unmounted(unmount_when_done);
712 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700713 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700714 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700715 free(copy);
716 } else {
717 result = INSTALL_ERROR;
718 }
Doug Zongker8674a722010-09-15 11:08:23 -0700719 break;
720 }
721 } while (true);
722
723 int i;
724 for (i = 0; i < z_size; ++i) free(zips[i]);
725 free(zips);
726 free(headers);
727
Michael Ward9d2629c2011-06-23 22:14:24 -0700728 if (unmount_when_done != NULL) {
729 ensure_path_unmounted(unmount_when_done);
730 }
Doug Zongker8674a722010-09-15 11:08:23 -0700731 return result;
732}
733
Doug Zongkerf93d8162009-09-22 15:16:02 -0700734static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700735wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700736 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700737 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700738
Doug Zongkerf93d8162009-09-22 15:16:02 -0700739 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700740 const char* headers[] = { "Confirm wipe of all user data?",
741 " THIS CAN NOT BE UNDONE.",
742 "",
743 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700744 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700745 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800746
Doug Zongker28ce47c2011-10-28 10:33:05 -0700747 const char* items[] = { " No",
748 " No",
749 " No",
750 " No",
751 " No",
752 " No",
753 " No",
754 " Yes -- delete all user data", // [7]
755 " No",
756 " No",
757 " No",
758 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800759
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700760 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700761 if (chosen_item != 7) {
762 return;
763 }
764 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700765
Doug Zongker211aebc2011-10-28 15:13:10 -0700766 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700767 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700768 erase_volume("/data");
769 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700770 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700771}
772
773static void
Doug Zongker6c8553d2012-09-24 10:40:47 -0700774prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700775 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700776
777 for (;;) {
778 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700779 switch (status) {
780 case INSTALL_SUCCESS:
781 case INSTALL_NONE:
782 ui->SetBackground(RecoveryUI::NO_COMMAND);
783 break;
784
785 case INSTALL_ERROR:
786 case INSTALL_CORRUPT:
787 ui->SetBackground(RecoveryUI::ERROR);
788 break;
789 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700790 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700791
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700792 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700793
794 // device-specific code may take some action here. It may
795 // return one of the core actions handled in the switch
796 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700797 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700798
Doug Zongkerd0181b82011-10-19 10:51:12 -0700799 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700800 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700801 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700802 return;
803
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700804 case Device::WIPE_DATA:
805 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700806 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700807 break;
808
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700809 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700810 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700811 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700812 ui->Print("Cache wipe complete.\n");
813 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700814 break;
815
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700816 case Device::APPLY_EXT:
Doug Zongkerd9428e32011-12-13 16:03:28 -0800817 // Some packages expect /cache to be mounted (eg,
818 // standard incremental packages expect to use /cache
819 // as scratch space).
820 ensure_path_mounted(CACHE_ROOT);
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700821 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700822 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700823 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700824 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700825 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700826 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700827 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700828 }
829 }
Doug Zongker8674a722010-09-15 11:08:23 -0700830 if (status >= 0) {
831 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700832 ui->SetBackground(RecoveryUI::ERROR);
833 ui->Print("Installation aborted.\n");
834 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700835 return; // reboot if logs aren't visible
836 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700837 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700838 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700839 }
840 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700841
842 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700843 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700844 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700845 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700846 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700847 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700848 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700849 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700850 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700851 }
852 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700853 if (status >= 0) {
854 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700855 ui->SetBackground(RecoveryUI::ERROR);
856 ui->Print("Installation aborted.\n");
857 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700858 return; // reboot if logs aren't visible
859 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700860 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700861 }
862 }
863 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800864
865 case Device::APPLY_ADB_SIDELOAD:
866 ensure_path_mounted(CACHE_ROOT);
867 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
868 if (status >= 0) {
869 if (status != INSTALL_SUCCESS) {
870 ui->SetBackground(RecoveryUI::ERROR);
871 ui->Print("Installation aborted.\n");
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700872 copy_logs();
Doug Zongker9270a202012-01-09 15:16:13 -0800873 } else if (!ui->IsTextVisible()) {
874 return; // reboot if logs aren't visible
875 } else {
876 ui->Print("\nInstall from ADB complete.\n");
877 }
878 }
879 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800880 }
881 }
882}
883
884static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800885print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700886 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800887}
888
Doug Zongker02ec6b82012-08-22 17:26:40 -0700889static void
890load_locale_from_cache() {
891 FILE* fp = fopen_path(LOCALE_FILE, "r");
892 char buffer[80];
893 if (fp != NULL) {
894 fgets(buffer, sizeof(buffer), fp);
895 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700896 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700897 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
898 if (!isspace(buffer[i])) {
899 buffer[j++] = buffer[i];
900 }
901 }
902 buffer[j] = 0;
903 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700904 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700905 }
906}
907
Doug Zongker7c3ae452013-05-14 11:03:02 -0700908static RecoveryUI* gCurrentUI = NULL;
909
910void
911ui_print(const char* format, ...) {
912 char buffer[256];
913
914 va_list ap;
915 va_start(ap, format);
916 vsnprintf(buffer, sizeof(buffer), format, ap);
917 va_end(ap);
918
919 if (gCurrentUI != NULL) {
920 gCurrentUI->Print("%s", buffer);
921 } else {
922 fputs(buffer, stdout);
923 }
924}
925
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800926int
Oscar Montemayor05231562009-11-30 08:40:57 -0800927main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800928 time_t start = time(NULL);
929
930 // If these fail, there's not really anywhere to complain...
931 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
932 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800933
934 // If this binary is started with the single argument "--adbd",
935 // instead of being the normal recovery binary, it turns into kind
936 // of a stripped-down version of adbd that only supports the
937 // 'sideload' command. Note this must be a real argument, not
938 // anything in the command file or bootloader control block; the
939 // only way recovery should be run with this argument is when it
940 // starts a copy of itself from the apply_from_adb() function.
941 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
942 adb_main();
943 return 0;
944 }
945
Doug Zongker56c51052010-07-01 09:18:44 -0700946 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800947
Doug Zongkerd4208f92010-09-20 12:16:13 -0700948 load_volume_table();
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700949 ensure_path_mounted(LAST_LOG_FILE);
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700950 rotate_last_logs(10);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800951 get_args(&argc, &argv);
952
953 int previous_runs = 0;
954 const char *send_intent = NULL;
955 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700956 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700957 bool just_exit = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800958
959 int arg;
960 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
961 switch (arg) {
962 case 'p': previous_runs = atoi(optarg); break;
963 case 's': send_intent = optarg; break;
964 case 'u': update_package = optarg; break;
965 case 'w': wipe_data = wipe_cache = 1; break;
966 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700967 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700968 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700969 case 'l': locale = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800970 case '?':
971 LOGE("Invalid command argument\n");
972 continue;
973 }
974 }
975
Doug Zongker02ec6b82012-08-22 17:26:40 -0700976 if (locale == NULL) {
977 load_locale_from_cache();
978 }
979 printf("locale is [%s]\n", locale);
980
981 Device* device = make_device();
982 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -0700983 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700984
985 ui->Init();
Doug Zongker5fa8c232012-09-18 12:37:02 -0700986 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700987 ui->SetBackground(RecoveryUI::NONE);
988 if (show_text) ui->ShowText(true);
989
Stephen Smalley779701d2012-02-09 14:13:23 -0500990 struct selinux_opt seopts[] = {
991 { SELABEL_OPT_PATH, "/file_contexts" }
992 };
993
994 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
995
996 if (!sehandle) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700997 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500998 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500999
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001000 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -08001001
Doug Zongker56c51052010-07-01 09:18:44 -07001002 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001003 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -07001004 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001005 }
Doug Zongker9b125b02010-09-22 12:01:37 -07001006 printf("\n");
1007
1008 if (update_package) {
1009 // For backwards compatibility on the cache partition only, if
1010 // we're given an old 'root' path "CACHE:foo", change it to
1011 // "/cache/foo".
1012 if (strncmp(update_package, "CACHE:", 6) == 0) {
1013 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -07001014 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -07001015 strlcpy(modified_path, "/cache/", len);
1016 strlcat(modified_path, update_package+6, len);
1017 printf("(replacing path \"%s\" with \"%s\")\n",
1018 update_package, modified_path);
1019 update_package = modified_path;
1020 }
1021 }
1022 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001023
1024 property_list(print_property, NULL);
Doug Zongker56c51052010-07-01 09:18:44 -07001025 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001026
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001027 int status = INSTALL_SUCCESS;
1028
Doug Zongker540d57f2011-01-18 13:36:58 -08001029 if (update_package != NULL) {
Doug Zongkerd0181b82011-10-19 10:51:12 -07001030 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
1031 if (status == INSTALL_SUCCESS && wipe_cache) {
1032 if (erase_volume("/cache")) {
1033 LOGE("Cache wipe (requested by package) failed.");
1034 }
1035 }
Doug Zongker7c3ae452013-05-14 11:03:02 -07001036 if (status != INSTALL_SUCCESS) {
1037 ui->Print("Installation aborted.\n");
1038
1039 // If this is an eng or userdebug build, then automatically
1040 // turn the text display on if the script fails so the error
1041 // message is visible.
1042 char buffer[PROPERTY_VALUE_MAX+1];
1043 property_get("ro.build.fingerprint", buffer, "");
1044 if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {
1045 ui->ShowText(true);
1046 }
1047 }
Doug Zongkerb128f542009-06-18 15:07:14 -07001048 } else if (wipe_data) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001049 if (device->WipeData()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -07001050 if (erase_volume("/data")) status = INSTALL_ERROR;
1051 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001052 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -07001053 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -07001054 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001055 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001056 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -07001057 status = INSTALL_NONE; // No command specified
1058 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001059 }
1060
Doug Zongker02ec6b82012-08-22 17:26:40 -07001061 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
Doug Zongkerf24fd7e2013-07-02 11:43:25 -07001062 copy_logs();
Doug Zongker02ec6b82012-08-22 17:26:40 -07001063 ui->SetBackground(RecoveryUI::ERROR);
1064 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001065 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker6c8553d2012-09-24 10:40:47 -07001066 prompt_and_wait(device, status);
Doug Zongker8674a722010-09-15 11:08:23 -07001067 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001068
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001069 // Otherwise, get ready to boot the main system...
1070 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -07001071 ui->Print("Rebooting...\n");
Ken Sumrall6e4472a2011-03-07 23:37:27 -08001072 android_reboot(ANDROID_RB_RESTART, 0, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001073 return EXIT_SUCCESS;
1074}