blob: d803cadf1738c1c3c97546b002755f09d8eb4949 [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 Zongkerc0441d12013-07-31 11:28:24 -070078char recovery_version[PROPERTY_VALUE_MAX+1];
Doug Zongker211aebc2011-10-28 15:13:10 -070079
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080080/*
81 * The recovery tool communicates with the main system through /cache files.
82 * /cache/recovery/command - INPUT - command line for tool, one arg per line
83 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
84 * /cache/recovery/intent - OUTPUT - intent that was passed in
85 *
86 * The arguments which may be supplied in the recovery.command file:
87 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070088 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080089 * --wipe_data - erase user data (and cache), then reboot
90 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080091 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -070092 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080093 *
94 * After completing, we remove /cache/recovery/command and reboot.
95 * Arguments may also be supplied in the bootloader control block (BCB).
96 * These important scenarios must be safely restartable at any point:
97 *
98 * FACTORY RESET
99 * 1. user selects "factory reset"
100 * 2. main system writes "--wipe_data" to /cache/recovery/command
101 * 3. main system reboots into recovery
102 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
103 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700104 * 5. erase_volume() reformats /data
105 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800106 * 7. finish_recovery() erases BCB
107 * -- after this, rebooting will restart the main system --
108 * 8. main() calls reboot() to boot main system
109 *
110 * OTA INSTALL
111 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700112 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800113 * 3. main system reboots into recovery
114 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
115 * -- after this, rebooting will attempt to reinstall the update --
116 * 5. install_package() attempts to install the update
117 * NOTE: the package install must itself be restartable from any point
118 * 6. finish_recovery() erases BCB
119 * -- after this, rebooting will (try to) restart the main system --
120 * 7. ** if install failed **
121 * 7a. prompt_and_wait() shows an error icon and waits for the user
122 * 7b; the user reboots (pulling the battery, etc) into the main system
123 * 8. main() calls maybe_install_firmware_update()
124 * ** if the update contained radio/hboot firmware **:
125 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
126 * -- after this, rebooting will reformat cache & restart main system --
127 * 8b. m_i_f_u() writes firmware image into raw cache partition
128 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
129 * -- after this, rebooting will attempt to reinstall firmware --
130 * 8d. bootloader tries to flash firmware
131 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
132 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700133 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800134 * 8g. finish_recovery() erases BCB
135 * -- after this, rebooting will (try to) restart the main system --
136 * 9. main() calls reboot() to boot main system
137 */
138
139static const int MAX_ARG_LENGTH = 4096;
140static const int MAX_ARGS = 100;
141
Doug Zongkerd4208f92010-09-20 12:16:13 -0700142// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700143FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700144fopen_path(const char *path, const char *mode) {
145 if (ensure_path_mounted(path) != 0) {
146 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800147 return NULL;
148 }
149
150 // When writing, try to create the containing directory, if necessary.
151 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500152 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800153
154 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800155 return fp;
156}
157
158// close a file, log an error if the error indicator is set
159static void
160check_and_fclose(FILE *fp, const char *name) {
161 fflush(fp);
162 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
163 fclose(fp);
164}
165
166// command line args come from, in decreasing precedence:
167// - the actual command line
168// - the bootloader control block (one per line, after "recovery")
169// - the contents of COMMAND_FILE (one per line)
170static void
171get_args(int *argc, char ***argv) {
172 struct bootloader_message boot;
173 memset(&boot, 0, sizeof(boot));
174 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
175
176 if (boot.command[0] != 0 && boot.command[0] != 255) {
177 LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command);
178 }
179
180 if (boot.status[0] != 0 && boot.status[0] != 255) {
181 LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status);
182 }
183
184 // --- if arguments weren't supplied, look in the bootloader control block
185 if (*argc <= 1) {
186 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
187 const char *arg = strtok(boot.recovery, "\n");
188 if (arg != NULL && !strcmp(arg, "recovery")) {
189 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
190 (*argv)[0] = strdup(arg);
191 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
192 if ((arg = strtok(NULL, "\n")) == NULL) break;
193 (*argv)[*argc] = strdup(arg);
194 }
195 LOGI("Got arguments from boot message\n");
196 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
197 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
198 }
199 }
200
201 // --- if that doesn't work, try the command file
202 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700203 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800204 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800205 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800206 char *argv0 = (*argv)[0];
207 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
208 (*argv)[0] = argv0; // use the same program name
209
210 char buf[MAX_ARG_LENGTH];
211 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
212 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800213 token = strtok(buf, "\r\n");
214 if (token != NULL) {
215 (*argv)[*argc] = strdup(token); // Strip newline.
216 } else {
217 --*argc;
218 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800219 }
220
221 check_and_fclose(fp, COMMAND_FILE);
222 LOGI("Got arguments from %s\n", COMMAND_FILE);
223 }
224 }
225
226 // --> write the arguments we have back into the bootloader control block
227 // always boot into recovery after this (until finish_recovery() is called)
228 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
229 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
230 int i;
231 for (i = 1; i < *argc; ++i) {
232 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
233 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
234 }
235 set_bootloader_message(&boot);
236}
237
Doug Zongker34c98df2009-08-18 12:05:45 -0700238static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800239set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700240 struct bootloader_message boot;
241 memset(&boot, 0, sizeof(boot));
242 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
243 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
244 set_bootloader_message(&boot);
245}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800246
Doug Zongker2c3539e2010-09-29 13:21:30 -0700247// How much of the temp log we have copied to the copy in cache.
248static long tmplog_offset = 0;
249
250static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700251copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700252 FILE *log = fopen_path(destination, append ? "a" : "w");
253 if (log == NULL) {
254 LOGE("Can't open %s\n", destination);
255 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700256 FILE *tmplog = fopen(source, "r");
257 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700258 if (append) {
259 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
260 }
261 char buf[4096];
262 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
263 if (append) {
264 tmplog_offset = ftell(tmplog);
265 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700266 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700267 }
268 check_and_fclose(log, destination);
269 }
270}
271
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700272// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max
273// Overwrites any existing last_log.$max.
274static void
275rotate_last_logs(int max) {
276 char oldfn[256];
277 char newfn[256];
278
279 int i;
280 for (i = max-1; i >= 0; --i) {
281 snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i);
282 snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1);
283 // ignore errors
284 rename(oldfn, newfn);
285 }
286}
Doug Zongker2c3539e2010-09-29 13:21:30 -0700287
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700288static void
289copy_logs() {
290 // Copy logs to cache so the system can find out what happened.
291 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
292 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
293 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
294 chmod(LOG_FILE, 0600);
295 chown(LOG_FILE, 1000, 1000); // system user
296 chmod(LAST_LOG_FILE, 0640);
297 chmod(LAST_INSTALL_FILE, 0644);
298 sync();
299}
300
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800301// clear the recovery command and prepare to boot a (hopefully working) system,
302// copy our log file to cache as well (for the system to read), and
303// record any intent we were asked to communicate back to the system.
304// this function is idempotent: call it as many times as you like.
305static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800306finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800307 // By this point, we're ready to return to the main system...
308 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700309 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000310 if (fp == NULL) {
311 LOGE("Can't open %s\n", INTENT_FILE);
312 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800313 fputs(send_intent, fp);
314 check_and_fclose(fp, INTENT_FILE);
315 }
316 }
317
Doug Zongker4f33e552012-08-23 13:16:12 -0700318 // Save the locale to cache, so if recovery is next started up
319 // without a --locale argument (eg, directly from the bootloader)
320 // it will use the last-known locale.
321 if (locale != NULL) {
322 LOGI("Saving locale \"%s\"\n", locale);
323 FILE* fp = fopen_path(LOCALE_FILE, "w");
324 fwrite(locale, 1, strlen(locale), fp);
325 fflush(fp);
326 fsync(fileno(fp));
327 check_and_fclose(fp, LOCALE_FILE);
328 }
329
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700330 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800331
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700332 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800333 struct bootloader_message boot;
334 memset(&boot, 0, sizeof(boot));
335 set_bootloader_message(&boot);
336
337 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700338 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
339 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800340 LOGW("Can't unlink %s\n", COMMAND_FILE);
341 }
342
Doug Zongkerd0181b82011-10-19 10:51:12 -0700343 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800344 sync(); // For good measure.
345}
346
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700347typedef struct _saved_log_file {
348 char* name;
349 struct stat st;
350 unsigned char* data;
351 struct _saved_log_file* next;
352} saved_log_file;
353
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800354static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700355erase_volume(const char *volume) {
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700356 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
357
Doug Zongker02ec6b82012-08-22 17:26:40 -0700358 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700359 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700360
361 saved_log_file* head = NULL;
362
363 if (is_cache) {
364 // If we're reformatting /cache, we load any
365 // "/cache/recovery/last*" files into memory, so we can restore
366 // them after the reformat.
367
368 ensure_path_mounted(volume);
369
370 DIR* d;
371 struct dirent* de;
372 d = opendir(CACHE_LOG_DIR);
373 if (d) {
374 char path[PATH_MAX];
375 strcpy(path, CACHE_LOG_DIR);
376 strcat(path, "/");
377 int path_len = strlen(path);
378 while ((de = readdir(d)) != NULL) {
379 if (strncmp(de->d_name, "last", 4) == 0) {
380 saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file));
381 strcpy(path+path_len, de->d_name);
382 p->name = strdup(path);
383 if (stat(path, &(p->st)) == 0) {
384 // truncate files to 512kb
385 if (p->st.st_size > (1 << 19)) {
386 p->st.st_size = 1 << 19;
387 }
388 p->data = (unsigned char*) malloc(p->st.st_size);
389 FILE* f = fopen(path, "rb");
390 fread(p->data, 1, p->st.st_size, f);
391 fclose(f);
392 p->next = head;
393 head = p;
394 } else {
395 free(p);
396 }
397 }
398 }
399 closedir(d);
400 } else {
401 if (errno != ENOENT) {
402 printf("opendir failed: %s\n", strerror(errno));
403 }
404 }
405 }
406
Doug Zongker211aebc2011-10-28 15:13:10 -0700407 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700408
Doug Zongkerd0181b82011-10-19 10:51:12 -0700409 ensure_path_unmounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700410 int result = format_volume(volume);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700411
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700412 if (is_cache) {
413 while (head) {
414 FILE* f = fopen_path(head->name, "wb");
415 if (f) {
416 fwrite(head->data, 1, head->st.st_size, f);
417 fclose(f);
418 chmod(head->name, head->st.st_mode);
419 chown(head->name, head->st.st_uid, head->st.st_gid);
420 }
421 free(head->name);
422 free(head->data);
423 saved_log_file* temp = head->next;
424 free(head);
425 head = temp;
426 }
427
Doug Zongker2c3539e2010-09-29 13:21:30 -0700428 // Any part of the log we'd copied to cache is now gone.
429 // Reset the pointer so we copy from the beginning of the temp
430 // log.
431 tmplog_offset = 0;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700432 copy_logs();
Doug Zongker2c3539e2010-09-29 13:21:30 -0700433 }
434
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700435 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800436}
437
Doug Zongker23ceeea2010-07-08 17:27:55 -0700438static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700439copy_sideloaded_package(const char* original_path) {
440 if (ensure_path_mounted(original_path) != 0) {
441 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700442 return NULL;
443 }
444
Doug Zongkerd4208f92010-09-20 12:16:13 -0700445 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700446 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
447 return NULL;
448 }
449
Doug Zongkerd4208f92010-09-20 12:16:13 -0700450 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700451 if (errno != EEXIST) {
452 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
453 return NULL;
454 }
455 }
456
Doug Zongkerd4208f92010-09-20 12:16:13 -0700457 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
458 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700459 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700460 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
461 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700462 return NULL;
463 }
464 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700465 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700466 return NULL;
467 }
468 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700469 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700470 return NULL;
471 }
472 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700473 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700474 return NULL;
475 }
476
Doug Zongkerd4208f92010-09-20 12:16:13 -0700477 char copy_path[PATH_MAX];
478 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700479 strcat(copy_path, "/package.zip");
480
Doug Zongker28ce47c2011-10-28 10:33:05 -0700481 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700482 if (buffer == NULL) {
483 LOGE("Failed to allocate buffer\n");
484 return NULL;
485 }
486
487 size_t read;
488 FILE* fin = fopen(original_path, "rb");
489 if (fin == NULL) {
490 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
491 return NULL;
492 }
493 FILE* fout = fopen(copy_path, "wb");
494 if (fout == NULL) {
495 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
496 return NULL;
497 }
498
499 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
500 if (fwrite(buffer, 1, read, fout) != read) {
501 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
502 return NULL;
503 }
504 }
505
506 free(buffer);
507
508 if (fclose(fout) != 0) {
509 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
510 return NULL;
511 }
512
513 if (fclose(fin) != 0) {
514 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
515 return NULL;
516 }
517
518 // "adb push" is happy to overwrite read-only files when it's
519 // running as root, but we'll try anyway.
520 if (chmod(copy_path, 0400) != 0) {
521 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
522 return NULL;
523 }
524
Doug Zongkerd4208f92010-09-20 12:16:13 -0700525 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700526}
527
Doug Zongker28ce47c2011-10-28 10:33:05 -0700528static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700529prepend_title(const char* const* headers) {
Doug Zongkerd6837852009-06-17 22:07:13 -0700530 // count the number of lines in our title, plus the
Doug Zongkerf93d8162009-09-22 15:16:02 -0700531 // caller-provided headers.
Doug Zongkerc0441d12013-07-31 11:28:24 -0700532 int count = 3; // our title has 3 lines
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700533 const char* const* p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700534 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700535
Doug Zongker28ce47c2011-10-28 10:33:05 -0700536 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
537 const char** h = new_headers;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700538 *(h++) = "Android system recovery <" EXPAND(RECOVERY_API_VERSION) "e>";
539 *(h++) = recovery_version;
540 *(h++) = "";
Doug Zongkerf93d8162009-09-22 15:16:02 -0700541 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700542 *h = NULL;
543
Doug Zongkerf93d8162009-09-22 15:16:02 -0700544 return new_headers;
545}
546
547static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700548get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700549 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700550 // throw away keys pressed previously, so user doesn't
551 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700552 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700553
Doug Zongker211aebc2011-10-28 15:13:10 -0700554 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700555 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800556 int chosen_item = -1;
557
Doug Zongkerf93d8162009-09-22 15:16:02 -0700558 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700559 int key = ui->WaitKey();
560 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800561
Doug Zongker5cae4452011-01-25 13:15:30 -0800562 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700563 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800564 continue;
565 } else {
566 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700567 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700568 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800569 }
570 }
571
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700572 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700573
574 if (action < 0) {
575 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700576 case Device::kHighlightUp:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700577 --selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700578 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700579 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700580 case Device::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700581 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700582 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700583 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700584 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700585 chosen_item = selected;
586 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700587 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700588 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800589 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700590 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700591 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800592 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700593 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800594
Doug Zongker211aebc2011-10-28 15:13:10 -0700595 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700596 return chosen_item;
597}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800598
Doug Zongker8674a722010-09-15 11:08:23 -0700599static int compare_string(const void* a, const void* b) {
600 return strcmp(*(const char**)a, *(const char**)b);
601}
602
603static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700604update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700605 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700606 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700607
Doug Zongker8674a722010-09-15 11:08:23 -0700608 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700609 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700610 "",
611 NULL };
612 DIR* d;
613 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700614 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700615 if (d == NULL) {
616 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700617 if (unmount_when_done != NULL) {
618 ensure_path_unmounted(unmount_when_done);
619 }
Doug Zongker8674a722010-09-15 11:08:23 -0700620 return 0;
621 }
622
Doug Zongker28ce47c2011-10-28 10:33:05 -0700623 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700624
625 int d_size = 0;
626 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700627 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700628 int z_size = 1;
629 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700630 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700631 zips[0] = strdup("../");
632
633 while ((de = readdir(d)) != NULL) {
634 int name_len = strlen(de->d_name);
635
636 if (de->d_type == DT_DIR) {
637 // skip "." and ".." entries
638 if (name_len == 1 && de->d_name[0] == '.') continue;
639 if (name_len == 2 && de->d_name[0] == '.' &&
640 de->d_name[1] == '.') continue;
641
642 if (d_size >= d_alloc) {
643 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700644 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700645 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700646 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700647 strcpy(dirs[d_size], de->d_name);
648 dirs[d_size][name_len] = '/';
649 dirs[d_size][name_len+1] = '\0';
650 ++d_size;
651 } else if (de->d_type == DT_REG &&
652 name_len >= 4 &&
653 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
654 if (z_size >= z_alloc) {
655 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700656 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700657 }
658 zips[z_size++] = strdup(de->d_name);
659 }
660 }
661 closedir(d);
662
663 qsort(dirs, d_size, sizeof(char*), compare_string);
664 qsort(zips, z_size, sizeof(char*), compare_string);
665
666 // append dirs to the zips list
667 if (d_size + z_size + 1 > z_alloc) {
668 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700669 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700670 }
671 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
672 free(dirs);
673 z_size += d_size;
674 zips[z_size] = NULL;
675
676 int result;
677 int chosen_item = 0;
678 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700679 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700680
681 char* item = zips[chosen_item];
682 int item_len = strlen(item);
683 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700684 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700685 result = -1;
686 break;
687 } else if (item[item_len-1] == '/') {
688 // recurse down into a subdirectory
689 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700690 strlcpy(new_path, path, PATH_MAX);
691 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700692 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700693 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700694 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700695 if (result >= 0) break;
696 } else {
697 // selected a zip file: attempt to install it, and return
698 // the status to the caller.
699 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700700 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700701 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700702 strlcat(new_path, item, PATH_MAX);
703
Doug Zongker211aebc2011-10-28 15:13:10 -0700704 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700705 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700706 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700707 if (unmount_when_done != NULL) {
708 ensure_path_unmounted(unmount_when_done);
709 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700710 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700711 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700712 free(copy);
713 } else {
714 result = INSTALL_ERROR;
715 }
Doug Zongker8674a722010-09-15 11:08:23 -0700716 break;
717 }
718 } while (true);
719
720 int i;
721 for (i = 0; i < z_size; ++i) free(zips[i]);
722 free(zips);
723 free(headers);
724
Michael Ward9d2629c2011-06-23 22:14:24 -0700725 if (unmount_when_done != NULL) {
726 ensure_path_unmounted(unmount_when_done);
727 }
Doug Zongker8674a722010-09-15 11:08:23 -0700728 return result;
729}
730
Doug Zongkerf93d8162009-09-22 15:16:02 -0700731static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700732wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700733 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700734 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700735
Doug Zongkerf93d8162009-09-22 15:16:02 -0700736 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700737 const char* headers[] = { "Confirm wipe of all user data?",
738 " THIS CAN NOT BE UNDONE.",
739 "",
740 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700741 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700742 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800743
Doug Zongker28ce47c2011-10-28 10:33:05 -0700744 const char* items[] = { " No",
745 " No",
746 " No",
747 " No",
748 " No",
749 " No",
750 " No",
751 " Yes -- delete all user data", // [7]
752 " No",
753 " No",
754 " No",
755 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800756
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700757 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700758 if (chosen_item != 7) {
759 return;
760 }
761 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700762
Doug Zongker211aebc2011-10-28 15:13:10 -0700763 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700764 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700765 erase_volume("/data");
766 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700767 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700768}
769
770static void
Doug Zongker6c8553d2012-09-24 10:40:47 -0700771prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700772 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700773
774 for (;;) {
775 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700776 switch (status) {
777 case INSTALL_SUCCESS:
778 case INSTALL_NONE:
779 ui->SetBackground(RecoveryUI::NO_COMMAND);
780 break;
781
782 case INSTALL_ERROR:
783 case INSTALL_CORRUPT:
784 ui->SetBackground(RecoveryUI::ERROR);
785 break;
786 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700787 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700788
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700789 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700790
791 // device-specific code may take some action here. It may
792 // return one of the core actions handled in the switch
793 // statement below.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700794 chosen_item = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700795
Doug Zongkerd0181b82011-10-19 10:51:12 -0700796 int wipe_cache;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700797 switch (chosen_item) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700798 case Device::REBOOT:
Doug Zongkerf93d8162009-09-22 15:16:02 -0700799 return;
800
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700801 case Device::WIPE_DATA:
802 wipe_data(ui->IsTextVisible(), device);
Doug Zongker211aebc2011-10-28 15:13:10 -0700803 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700804 break;
805
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700806 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700807 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700808 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700809 ui->Print("Cache wipe complete.\n");
810 if (!ui->IsTextVisible()) return;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700811 break;
812
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700813 case Device::APPLY_EXT:
814 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700815 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700816 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700817 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700818 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700819 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700820 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700821 }
822 }
Doug Zongker8674a722010-09-15 11:08:23 -0700823 if (status >= 0) {
824 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700825 ui->SetBackground(RecoveryUI::ERROR);
826 ui->Print("Installation aborted.\n");
827 } else if (!ui->IsTextVisible()) {
Doug Zongker8674a722010-09-15 11:08:23 -0700828 return; // reboot if logs aren't visible
829 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700830 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700831 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700832 }
833 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700834
835 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700836 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700837 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700838 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700839 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700840 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700841 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700842 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700843 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700844 }
845 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700846 if (status >= 0) {
847 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700848 ui->SetBackground(RecoveryUI::ERROR);
849 ui->Print("Installation aborted.\n");
850 } else if (!ui->IsTextVisible()) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700851 return; // reboot if logs aren't visible
852 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700853 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700854 }
855 }
856 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800857
858 case Device::APPLY_ADB_SIDELOAD:
Doug Zongker9270a202012-01-09 15:16:13 -0800859 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
860 if (status >= 0) {
861 if (status != INSTALL_SUCCESS) {
862 ui->SetBackground(RecoveryUI::ERROR);
863 ui->Print("Installation aborted.\n");
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700864 copy_logs();
Doug Zongker9270a202012-01-09 15:16:13 -0800865 } else if (!ui->IsTextVisible()) {
866 return; // reboot if logs aren't visible
867 } else {
868 ui->Print("\nInstall from ADB complete.\n");
869 }
870 }
871 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800872 }
873 }
874}
875
876static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800877print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700878 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800879}
880
Doug Zongker02ec6b82012-08-22 17:26:40 -0700881static void
882load_locale_from_cache() {
883 FILE* fp = fopen_path(LOCALE_FILE, "r");
884 char buffer[80];
885 if (fp != NULL) {
886 fgets(buffer, sizeof(buffer), fp);
887 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700888 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700889 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
890 if (!isspace(buffer[i])) {
891 buffer[j++] = buffer[i];
892 }
893 }
894 buffer[j] = 0;
895 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700896 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700897 }
898}
899
Doug Zongker7c3ae452013-05-14 11:03:02 -0700900static RecoveryUI* gCurrentUI = NULL;
901
902void
903ui_print(const char* format, ...) {
904 char buffer[256];
905
906 va_list ap;
907 va_start(ap, format);
908 vsnprintf(buffer, sizeof(buffer), format, ap);
909 va_end(ap);
910
911 if (gCurrentUI != NULL) {
912 gCurrentUI->Print("%s", buffer);
913 } else {
914 fputs(buffer, stdout);
915 }
916}
917
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800918int
Oscar Montemayor05231562009-11-30 08:40:57 -0800919main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800920 time_t start = time(NULL);
921
922 // If these fail, there's not really anywhere to complain...
923 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
924 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800925
926 // If this binary is started with the single argument "--adbd",
927 // instead of being the normal recovery binary, it turns into kind
928 // of a stripped-down version of adbd that only supports the
929 // 'sideload' command. Note this must be a real argument, not
930 // anything in the command file or bootloader control block; the
931 // only way recovery should be run with this argument is when it
932 // starts a copy of itself from the apply_from_adb() function.
933 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
934 adb_main();
935 return 0;
936 }
937
Doug Zongker56c51052010-07-01 09:18:44 -0700938 printf("Starting recovery on %s", ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800939
Doug Zongkerd4208f92010-09-20 12:16:13 -0700940 load_volume_table();
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700941 ensure_path_mounted(LAST_LOG_FILE);
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700942 rotate_last_logs(10);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800943 get_args(&argc, &argv);
944
945 int previous_runs = 0;
946 const char *send_intent = NULL;
947 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700948 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700949 bool just_exit = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800950
951 int arg;
952 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
953 switch (arg) {
954 case 'p': previous_runs = atoi(optarg); break;
955 case 's': send_intent = optarg; break;
956 case 'u': update_package = optarg; break;
957 case 'w': wipe_data = wipe_cache = 1; break;
958 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700959 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700960 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700961 case 'l': locale = optarg; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800962 case '?':
963 LOGE("Invalid command argument\n");
964 continue;
965 }
966 }
967
Doug Zongker02ec6b82012-08-22 17:26:40 -0700968 if (locale == NULL) {
969 load_locale_from_cache();
970 }
971 printf("locale is [%s]\n", locale);
972
973 Device* device = make_device();
974 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -0700975 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700976
977 ui->Init();
Doug Zongker5fa8c232012-09-18 12:37:02 -0700978 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700979 ui->SetBackground(RecoveryUI::NONE);
980 if (show_text) ui->ShowText(true);
981
Stephen Smalley779701d2012-02-09 14:13:23 -0500982 struct selinux_opt seopts[] = {
983 { SELABEL_OPT_PATH, "/file_contexts" }
984 };
985
986 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
987
988 if (!sehandle) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -0700989 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -0500990 }
Stephen Smalley779701d2012-02-09 14:13:23 -0500991
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700992 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -0800993
Doug Zongker56c51052010-07-01 09:18:44 -0700994 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800995 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -0700996 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800997 }
Doug Zongker9b125b02010-09-22 12:01:37 -0700998 printf("\n");
999
1000 if (update_package) {
1001 // For backwards compatibility on the cache partition only, if
1002 // we're given an old 'root' path "CACHE:foo", change it to
1003 // "/cache/foo".
1004 if (strncmp(update_package, "CACHE:", 6) == 0) {
1005 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -07001006 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -07001007 strlcpy(modified_path, "/cache/", len);
1008 strlcat(modified_path, update_package+6, len);
1009 printf("(replacing path \"%s\" with \"%s\")\n",
1010 update_package, modified_path);
1011 update_package = modified_path;
1012 }
1013 }
1014 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001015
1016 property_list(print_property, NULL);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001017 property_get("ro.build.display.id", recovery_version, "");
Doug Zongker56c51052010-07-01 09:18:44 -07001018 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001019
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001020 int status = INSTALL_SUCCESS;
1021
Doug Zongker540d57f2011-01-18 13:36:58 -08001022 if (update_package != NULL) {
Doug Zongkerd0181b82011-10-19 10:51:12 -07001023 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
1024 if (status == INSTALL_SUCCESS && wipe_cache) {
1025 if (erase_volume("/cache")) {
1026 LOGE("Cache wipe (requested by package) failed.");
1027 }
1028 }
Doug Zongker7c3ae452013-05-14 11:03:02 -07001029 if (status != INSTALL_SUCCESS) {
1030 ui->Print("Installation aborted.\n");
1031
1032 // If this is an eng or userdebug build, then automatically
1033 // turn the text display on if the script fails so the error
1034 // message is visible.
1035 char buffer[PROPERTY_VALUE_MAX+1];
1036 property_get("ro.build.fingerprint", buffer, "");
1037 if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {
1038 ui->ShowText(true);
1039 }
1040 }
Doug Zongkerb128f542009-06-18 15:07:14 -07001041 } else if (wipe_data) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001042 if (device->WipeData()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -07001043 if (erase_volume("/data")) status = INSTALL_ERROR;
1044 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001045 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -07001046 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -07001047 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001048 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001049 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -07001050 status = INSTALL_NONE; // No command specified
1051 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001052 }
1053
Doug Zongker02ec6b82012-08-22 17:26:40 -07001054 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
Doug Zongkerf24fd7e2013-07-02 11:43:25 -07001055 copy_logs();
Doug Zongker02ec6b82012-08-22 17:26:40 -07001056 ui->SetBackground(RecoveryUI::ERROR);
1057 }
Doug Zongker211aebc2011-10-28 15:13:10 -07001058 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker6c8553d2012-09-24 10:40:47 -07001059 prompt_and_wait(device, status);
Doug Zongker8674a722010-09-15 11:08:23 -07001060 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001061
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001062 // Otherwise, get ready to boot the main system...
1063 finish_recovery(send_intent);
Doug Zongker211aebc2011-10-28 15:13:10 -07001064 ui->Print("Rebooting...\n");
Doug Zongker3b5a9872013-09-03 14:29:54 -07001065 property_set(ANDROID_RB_PROPERTY, "reboot,");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001066 return EXIT_SUCCESS;
1067}