blob: 09af5f8b176eafcbc79a7e3eac3b1b74b3a43094 [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 Zongkerc87bab12013-11-25 13:53:25 -080059 { "stages", required_argument, NULL, 'g' },
Doug Zongkerb1d12632014-03-18 10:32:12 -070060 { "shutdown_after", no_argument, NULL, 'p' },
Doug Zongker988500b2009-10-06 14:41:38 -070061 { NULL, 0, NULL, 0 },
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080062};
63
Doug Zongkerda1ebae2013-05-16 11:23:48 -070064#define LAST_LOG_FILE "/cache/recovery/last_log"
65
Doug Zongker6d0d7ac2013-07-09 13:34:55 -070066static const char *CACHE_LOG_DIR = "/cache/recovery";
Doug Zongkerd4208f92010-09-20 12:16:13 -070067static const char *COMMAND_FILE = "/cache/recovery/command";
68static const char *INTENT_FILE = "/cache/recovery/intent";
69static const char *LOG_FILE = "/cache/recovery/log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070070static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";
Doug Zongker8b240cc2012-08-29 15:19:29 -070071static const char *LOCALE_FILE = "/cache/recovery/last_locale";
Michael Ward9d2629c2011-06-23 22:14:24 -070072static const char *CACHE_ROOT = "/cache";
Doug Zongkerd4208f92010-09-20 12:16:13 -070073static const char *SDCARD_ROOT = "/sdcard";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080074static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";
Doug Zongkerd0181b82011-10-19 10:51:12 -070075static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";
Doug Zongkerd4208f92010-09-20 12:16:13 -070076static const char *SIDELOAD_TEMP_DIR = "/tmp/sideload";
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077
Doug Zongker211aebc2011-10-28 15:13:10 -070078RecoveryUI* ui = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -070079char* locale = NULL;
Doug Zongkerc0441d12013-07-31 11:28:24 -070080char recovery_version[PROPERTY_VALUE_MAX+1];
Doug Zongkerc87bab12013-11-25 13:53:25 -080081char* stage = NULL;
Doug Zongker211aebc2011-10-28 15:13:10 -070082
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080083/*
84 * The recovery tool communicates with the main system through /cache files.
85 * /cache/recovery/command - INPUT - command line for tool, one arg per line
86 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
87 * /cache/recovery/intent - OUTPUT - intent that was passed in
88 *
89 * The arguments which may be supplied in the recovery.command file:
90 * --send_intent=anystring - write the text out to recovery.intent
Doug Zongkerd4208f92010-09-20 12:16:13 -070091 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080092 * --wipe_data - erase user data (and cache), then reboot
93 * --wipe_cache - wipe cache (but not user data), then reboot
Oscar Montemayor05231562009-11-30 08:40:57 -080094 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -070095 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080096 *
97 * After completing, we remove /cache/recovery/command and reboot.
98 * Arguments may also be supplied in the bootloader control block (BCB).
99 * These important scenarios must be safely restartable at any point:
100 *
101 * FACTORY RESET
102 * 1. user selects "factory reset"
103 * 2. main system writes "--wipe_data" to /cache/recovery/command
104 * 3. main system reboots into recovery
105 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
106 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700107 * 5. erase_volume() reformats /data
108 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800109 * 7. finish_recovery() erases BCB
110 * -- after this, rebooting will restart the main system --
111 * 8. main() calls reboot() to boot main system
112 *
113 * OTA INSTALL
114 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700115 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800116 * 3. main system reboots into recovery
117 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
118 * -- after this, rebooting will attempt to reinstall the update --
119 * 5. install_package() attempts to install the update
120 * NOTE: the package install must itself be restartable from any point
121 * 6. finish_recovery() erases BCB
122 * -- after this, rebooting will (try to) restart the main system --
123 * 7. ** if install failed **
124 * 7a. prompt_and_wait() shows an error icon and waits for the user
125 * 7b; the user reboots (pulling the battery, etc) into the main system
126 * 8. main() calls maybe_install_firmware_update()
127 * ** if the update contained radio/hboot firmware **:
128 * 8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"
129 * -- after this, rebooting will reformat cache & restart main system --
130 * 8b. m_i_f_u() writes firmware image into raw cache partition
131 * 8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"
132 * -- after this, rebooting will attempt to reinstall firmware --
133 * 8d. bootloader tries to flash firmware
134 * 8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")
135 * -- after this, rebooting will reformat cache & restart main system --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700136 * 8f. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800137 * 8g. finish_recovery() erases BCB
138 * -- after this, rebooting will (try to) restart the main system --
139 * 9. main() calls reboot() to boot main system
140 */
141
142static const int MAX_ARG_LENGTH = 4096;
143static const int MAX_ARGS = 100;
144
Doug Zongkerd4208f92010-09-20 12:16:13 -0700145// open a given path, mounting partitions as necessary
Doug Zongker469243e2011-04-12 09:28:10 -0700146FILE*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700147fopen_path(const char *path, const char *mode) {
148 if (ensure_path_mounted(path) != 0) {
149 LOGE("Can't mount %s\n", path);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150 return NULL;
151 }
152
153 // When writing, try to create the containing directory, if necessary.
154 // Use generous permissions, the system (init.rc) will reset them.
Stephen Smalley779701d2012-02-09 14:13:23 -0500155 if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156
157 FILE *fp = fopen(path, mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800158 return fp;
159}
160
161// close a file, log an error if the error indicator is set
162static void
163check_and_fclose(FILE *fp, const char *name) {
164 fflush(fp);
165 if (ferror(fp)) LOGE("Error in %s\n(%s)\n", name, strerror(errno));
166 fclose(fp);
167}
168
169// command line args come from, in decreasing precedence:
170// - the actual command line
171// - the bootloader control block (one per line, after "recovery")
172// - the contents of COMMAND_FILE (one per line)
173static void
174get_args(int *argc, char ***argv) {
175 struct bootloader_message boot;
176 memset(&boot, 0, sizeof(boot));
177 get_bootloader_message(&boot); // this may fail, leaving a zeroed structure
Doug Zongkerc87bab12013-11-25 13:53:25 -0800178 stage = strndup(boot.stage, sizeof(boot.stage));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800179
180 if (boot.command[0] != 0 && boot.command[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700181 LOGI("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182 }
183
184 if (boot.status[0] != 0 && boot.status[0] != 255) {
Mark Salyzynf3bb31c2014-03-14 09:39:48 -0700185 LOGI("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800186 }
187
188 // --- if arguments weren't supplied, look in the bootloader control block
189 if (*argc <= 1) {
190 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
191 const char *arg = strtok(boot.recovery, "\n");
192 if (arg != NULL && !strcmp(arg, "recovery")) {
193 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
194 (*argv)[0] = strdup(arg);
195 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
196 if ((arg = strtok(NULL, "\n")) == NULL) break;
197 (*argv)[*argc] = strdup(arg);
198 }
199 LOGI("Got arguments from boot message\n");
200 } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {
201 LOGE("Bad boot message\n\"%.20s\"\n", boot.recovery);
202 }
203 }
204
205 // --- if that doesn't work, try the command file
206 if (*argc <= 1) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700207 FILE *fp = fopen_path(COMMAND_FILE, "r");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800208 if (fp != NULL) {
Jin Feng93ffa752013-06-04 17:46:24 +0800209 char *token;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800210 char *argv0 = (*argv)[0];
211 *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);
212 (*argv)[0] = argv0; // use the same program name
213
214 char buf[MAX_ARG_LENGTH];
215 for (*argc = 1; *argc < MAX_ARGS; ++*argc) {
216 if (!fgets(buf, sizeof(buf), fp)) break;
Jin Feng93ffa752013-06-04 17:46:24 +0800217 token = strtok(buf, "\r\n");
218 if (token != NULL) {
219 (*argv)[*argc] = strdup(token); // Strip newline.
220 } else {
221 --*argc;
222 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800223 }
224
225 check_and_fclose(fp, COMMAND_FILE);
226 LOGI("Got arguments from %s\n", COMMAND_FILE);
227 }
228 }
229
230 // --> write the arguments we have back into the bootloader control block
231 // always boot into recovery after this (until finish_recovery() is called)
232 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
233 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
234 int i;
235 for (i = 1; i < *argc; ++i) {
236 strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));
237 strlcat(boot.recovery, "\n", sizeof(boot.recovery));
238 }
239 set_bootloader_message(&boot);
240}
241
Doug Zongker34c98df2009-08-18 12:05:45 -0700242static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800243set_sdcard_update_bootloader_message() {
Doug Zongker34c98df2009-08-18 12:05:45 -0700244 struct bootloader_message boot;
245 memset(&boot, 0, sizeof(boot));
246 strlcpy(boot.command, "boot-recovery", sizeof(boot.command));
247 strlcpy(boot.recovery, "recovery\n", sizeof(boot.recovery));
248 set_bootloader_message(&boot);
249}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800250
Doug Zongker2c3539e2010-09-29 13:21:30 -0700251// How much of the temp log we have copied to the copy in cache.
252static long tmplog_offset = 0;
253
254static void
Doug Zongkerd0181b82011-10-19 10:51:12 -0700255copy_log_file(const char* source, const char* destination, int append) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700256 FILE *log = fopen_path(destination, append ? "a" : "w");
257 if (log == NULL) {
258 LOGE("Can't open %s\n", destination);
259 } else {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700260 FILE *tmplog = fopen(source, "r");
261 if (tmplog != NULL) {
Doug Zongker2c3539e2010-09-29 13:21:30 -0700262 if (append) {
263 fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write
264 }
265 char buf[4096];
266 while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);
267 if (append) {
268 tmplog_offset = ftell(tmplog);
269 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700270 check_and_fclose(tmplog, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700271 }
272 check_and_fclose(log, destination);
273 }
274}
275
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700276// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max
277// Overwrites any existing last_log.$max.
278static void
279rotate_last_logs(int max) {
280 char oldfn[256];
281 char newfn[256];
282
283 int i;
284 for (i = max-1; i >= 0; --i) {
285 snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i);
286 snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1);
287 // ignore errors
288 rename(oldfn, newfn);
289 }
290}
Doug Zongker2c3539e2010-09-29 13:21:30 -0700291
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700292static void
293copy_logs() {
294 // Copy logs to cache so the system can find out what happened.
295 copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);
296 copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);
297 copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);
298 chmod(LOG_FILE, 0600);
299 chown(LOG_FILE, 1000, 1000); // system user
300 chmod(LAST_LOG_FILE, 0640);
301 chmod(LAST_INSTALL_FILE, 0644);
302 sync();
303}
304
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800305// clear the recovery command and prepare to boot a (hopefully working) system,
306// copy our log file to cache as well (for the system to read), and
307// record any intent we were asked to communicate back to the system.
308// this function is idempotent: call it as many times as you like.
309static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800310finish_recovery(const char *send_intent) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800311 // By this point, we're ready to return to the main system...
312 if (send_intent != NULL) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700313 FILE *fp = fopen_path(INTENT_FILE, "w");
Jay Freeman (saurik)619ec2f2008-11-17 01:56:05 +0000314 if (fp == NULL) {
315 LOGE("Can't open %s\n", INTENT_FILE);
316 } else {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800317 fputs(send_intent, fp);
318 check_and_fclose(fp, INTENT_FILE);
319 }
320 }
321
Doug Zongker4f33e552012-08-23 13:16:12 -0700322 // Save the locale to cache, so if recovery is next started up
323 // without a --locale argument (eg, directly from the bootloader)
324 // it will use the last-known locale.
325 if (locale != NULL) {
326 LOGI("Saving locale \"%s\"\n", locale);
327 FILE* fp = fopen_path(LOCALE_FILE, "w");
328 fwrite(locale, 1, strlen(locale), fp);
329 fflush(fp);
330 fsync(fileno(fp));
331 check_and_fclose(fp, LOCALE_FILE);
332 }
333
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700334 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800335
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700336 // Reset to normal system boot so recovery won't cycle indefinitely.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800337 struct bootloader_message boot;
338 memset(&boot, 0, sizeof(boot));
339 set_bootloader_message(&boot);
340
341 // Remove the command file, so recovery won't repeat indefinitely.
Doug Zongkerd4208f92010-09-20 12:16:13 -0700342 if (ensure_path_mounted(COMMAND_FILE) != 0 ||
343 (unlink(COMMAND_FILE) && errno != ENOENT)) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800344 LOGW("Can't unlink %s\n", COMMAND_FILE);
345 }
346
Doug Zongkerd0181b82011-10-19 10:51:12 -0700347 ensure_path_unmounted(CACHE_ROOT);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800348 sync(); // For good measure.
349}
350
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700351typedef struct _saved_log_file {
352 char* name;
353 struct stat st;
354 unsigned char* data;
355 struct _saved_log_file* next;
356} saved_log_file;
357
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800358static int
Doug Zongkerd4208f92010-09-20 12:16:13 -0700359erase_volume(const char *volume) {
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700360 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
361
Doug Zongker02ec6b82012-08-22 17:26:40 -0700362 ui->SetBackground(RecoveryUI::ERASING);
Doug Zongker211aebc2011-10-28 15:13:10 -0700363 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700364
365 saved_log_file* head = NULL;
366
367 if (is_cache) {
368 // If we're reformatting /cache, we load any
369 // "/cache/recovery/last*" files into memory, so we can restore
370 // them after the reformat.
371
372 ensure_path_mounted(volume);
373
374 DIR* d;
375 struct dirent* de;
376 d = opendir(CACHE_LOG_DIR);
377 if (d) {
378 char path[PATH_MAX];
379 strcpy(path, CACHE_LOG_DIR);
380 strcat(path, "/");
381 int path_len = strlen(path);
382 while ((de = readdir(d)) != NULL) {
383 if (strncmp(de->d_name, "last", 4) == 0) {
384 saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file));
385 strcpy(path+path_len, de->d_name);
386 p->name = strdup(path);
387 if (stat(path, &(p->st)) == 0) {
388 // truncate files to 512kb
389 if (p->st.st_size > (1 << 19)) {
390 p->st.st_size = 1 << 19;
391 }
392 p->data = (unsigned char*) malloc(p->st.st_size);
393 FILE* f = fopen(path, "rb");
394 fread(p->data, 1, p->st.st_size, f);
395 fclose(f);
396 p->next = head;
397 head = p;
398 } else {
399 free(p);
400 }
401 }
402 }
403 closedir(d);
404 } else {
405 if (errno != ENOENT) {
406 printf("opendir failed: %s\n", strerror(errno));
407 }
408 }
409 }
410
Doug Zongker211aebc2011-10-28 15:13:10 -0700411 ui->Print("Formatting %s...\n", volume);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700412
Doug Zongkerd0181b82011-10-19 10:51:12 -0700413 ensure_path_unmounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700414 int result = format_volume(volume);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700415
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700416 if (is_cache) {
417 while (head) {
418 FILE* f = fopen_path(head->name, "wb");
419 if (f) {
420 fwrite(head->data, 1, head->st.st_size, f);
421 fclose(f);
422 chmod(head->name, head->st.st_mode);
423 chown(head->name, head->st.st_uid, head->st.st_gid);
424 }
425 free(head->name);
426 free(head->data);
427 saved_log_file* temp = head->next;
428 free(head);
429 head = temp;
430 }
431
Doug Zongker2c3539e2010-09-29 13:21:30 -0700432 // Any part of the log we'd copied to cache is now gone.
433 // Reset the pointer so we copy from the beginning of the temp
434 // log.
435 tmplog_offset = 0;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700436 copy_logs();
Doug Zongker2c3539e2010-09-29 13:21:30 -0700437 }
438
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700439 return result;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800440}
441
Doug Zongker23ceeea2010-07-08 17:27:55 -0700442static char*
Doug Zongkerd4208f92010-09-20 12:16:13 -0700443copy_sideloaded_package(const char* original_path) {
444 if (ensure_path_mounted(original_path) != 0) {
445 LOGE("Can't mount %s\n", original_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700446 return NULL;
447 }
448
Doug Zongkerd4208f92010-09-20 12:16:13 -0700449 if (ensure_path_mounted(SIDELOAD_TEMP_DIR) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700450 LOGE("Can't mount %s\n", SIDELOAD_TEMP_DIR);
451 return NULL;
452 }
453
Doug Zongkerd4208f92010-09-20 12:16:13 -0700454 if (mkdir(SIDELOAD_TEMP_DIR, 0700) != 0) {
Doug Zongker23ceeea2010-07-08 17:27:55 -0700455 if (errno != EEXIST) {
456 LOGE("Can't mkdir %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
457 return NULL;
458 }
459 }
460
Doug Zongkerd4208f92010-09-20 12:16:13 -0700461 // verify that SIDELOAD_TEMP_DIR is exactly what we expect: a
462 // directory, owned by root, readable and writable only by root.
Doug Zongker23ceeea2010-07-08 17:27:55 -0700463 struct stat st;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700464 if (stat(SIDELOAD_TEMP_DIR, &st) != 0) {
465 LOGE("failed to stat %s (%s)\n", SIDELOAD_TEMP_DIR, strerror(errno));
Doug Zongker23ceeea2010-07-08 17:27:55 -0700466 return NULL;
467 }
468 if (!S_ISDIR(st.st_mode)) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700469 LOGE("%s isn't a directory\n", SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700470 return NULL;
471 }
472 if ((st.st_mode & 0777) != 0700) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700473 LOGE("%s has perms %o\n", SIDELOAD_TEMP_DIR, st.st_mode);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700474 return NULL;
475 }
476 if (st.st_uid != 0) {
Doug Zongkerd4208f92010-09-20 12:16:13 -0700477 LOGE("%s owned by %lu; not root\n", SIDELOAD_TEMP_DIR, st.st_uid);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700478 return NULL;
479 }
480
Doug Zongkerd4208f92010-09-20 12:16:13 -0700481 char copy_path[PATH_MAX];
482 strcpy(copy_path, SIDELOAD_TEMP_DIR);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700483 strcat(copy_path, "/package.zip");
484
Doug Zongker28ce47c2011-10-28 10:33:05 -0700485 char* buffer = (char*)malloc(BUFSIZ);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700486 if (buffer == NULL) {
487 LOGE("Failed to allocate buffer\n");
488 return NULL;
489 }
490
491 size_t read;
492 FILE* fin = fopen(original_path, "rb");
493 if (fin == NULL) {
494 LOGE("Failed to open %s (%s)\n", original_path, strerror(errno));
495 return NULL;
496 }
497 FILE* fout = fopen(copy_path, "wb");
498 if (fout == NULL) {
499 LOGE("Failed to open %s (%s)\n", copy_path, strerror(errno));
500 return NULL;
501 }
502
503 while ((read = fread(buffer, 1, BUFSIZ, fin)) > 0) {
504 if (fwrite(buffer, 1, read, fout) != read) {
505 LOGE("Short write of %s (%s)\n", copy_path, strerror(errno));
506 return NULL;
507 }
508 }
509
510 free(buffer);
511
512 if (fclose(fout) != 0) {
513 LOGE("Failed to close %s (%s)\n", copy_path, strerror(errno));
514 return NULL;
515 }
516
517 if (fclose(fin) != 0) {
518 LOGE("Failed to close %s (%s)\n", original_path, strerror(errno));
519 return NULL;
520 }
521
522 // "adb push" is happy to overwrite read-only files when it's
523 // running as root, but we'll try anyway.
524 if (chmod(copy_path, 0400) != 0) {
525 LOGE("Failed to chmod %s (%s)\n", copy_path, strerror(errno));
526 return NULL;
527 }
528
Doug Zongkerd4208f92010-09-20 12:16:13 -0700529 return strdup(copy_path);
Doug Zongker23ceeea2010-07-08 17:27:55 -0700530}
531
Doug Zongker28ce47c2011-10-28 10:33:05 -0700532static const char**
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700533prepend_title(const char* const* headers) {
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 Zongkerc0441d12013-07-31 11:28:24 -0700536 int count = 3; // our title has 3 lines
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700537 const char* const* p;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700538 for (p = headers; *p; ++p, ++count);
Doug Zongkerd6837852009-06-17 22:07:13 -0700539
Doug Zongker28ce47c2011-10-28 10:33:05 -0700540 const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));
541 const char** h = new_headers;
Doug Zongkerc0441d12013-07-31 11:28:24 -0700542 *(h++) = "Android system recovery <" EXPAND(RECOVERY_API_VERSION) "e>";
543 *(h++) = recovery_version;
544 *(h++) = "";
Doug Zongkerf93d8162009-09-22 15:16:02 -0700545 for (p = headers; *p; ++p, ++h) *h = *p;
Doug Zongkerd6837852009-06-17 22:07:13 -0700546 *h = NULL;
547
Doug Zongkerf93d8162009-09-22 15:16:02 -0700548 return new_headers;
549}
550
551static int
Doug Zongker28ce47c2011-10-28 10:33:05 -0700552get_menu_selection(const char* const * headers, const char* const * items,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700553 int menu_only, int initial_selection, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700554 // throw away keys pressed previously, so user doesn't
555 // accidentally trigger menu items.
Doug Zongker211aebc2011-10-28 15:13:10 -0700556 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700557
Doug Zongker211aebc2011-10-28 15:13:10 -0700558 ui->StartMenu(headers, items, initial_selection);
Doug Zongker8674a722010-09-15 11:08:23 -0700559 int selected = initial_selection;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800560 int chosen_item = -1;
561
Doug Zongkerf93d8162009-09-22 15:16:02 -0700562 while (chosen_item < 0) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700563 int key = ui->WaitKey();
564 int visible = ui->IsTextVisible();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800565
Doug Zongker5cae4452011-01-25 13:15:30 -0800566 if (key == -1) { // ui_wait_key() timed out
Doug Zongker211aebc2011-10-28 15:13:10 -0700567 if (ui->WasTextEverVisible()) {
Doug Zongker5cae4452011-01-25 13:15:30 -0800568 continue;
569 } else {
570 LOGI("timed out waiting for key input; rebooting.\n");
Doug Zongker211aebc2011-10-28 15:13:10 -0700571 ui->EndMenu();
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700572 return 0; // XXX fixme
Doug Zongker5cae4452011-01-25 13:15:30 -0800573 }
574 }
575
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700576 int action = device->HandleMenuKey(key, visible);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700577
578 if (action < 0) {
579 switch (action) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700580 case Device::kHighlightUp:
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::kHighlightDown:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700585 ++selected;
Doug Zongker211aebc2011-10-28 15:13:10 -0700586 selected = ui->SelectMenu(selected);
Doug Zongkerddd6a282009-06-09 12:22:33 -0700587 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700588 case Device::kInvokeItem:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700589 chosen_item = selected;
590 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700591 case Device::kNoAction:
Doug Zongkerddd6a282009-06-09 12:22:33 -0700592 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800593 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700594 } else if (!menu_only) {
Doug Zongkerddd6a282009-06-09 12:22:33 -0700595 chosen_item = action;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800596 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700597 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800598
Doug Zongker211aebc2011-10-28 15:13:10 -0700599 ui->EndMenu();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700600 return chosen_item;
601}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800602
Doug Zongker8674a722010-09-15 11:08:23 -0700603static int compare_string(const void* a, const void* b) {
604 return strcmp(*(const char**)a, *(const char**)b);
605}
606
607static int
Doug Zongkerd0181b82011-10-19 10:51:12 -0700608update_directory(const char* path, const char* unmount_when_done,
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700609 int* wipe_cache, Device* device) {
Michael Ward9d2629c2011-06-23 22:14:24 -0700610 ensure_path_mounted(path);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700611
Doug Zongker8674a722010-09-15 11:08:23 -0700612 const char* MENU_HEADERS[] = { "Choose a package to install:",
Doug Zongkerd4208f92010-09-20 12:16:13 -0700613 path,
Doug Zongker8674a722010-09-15 11:08:23 -0700614 "",
615 NULL };
616 DIR* d;
617 struct dirent* de;
Doug Zongkerd4208f92010-09-20 12:16:13 -0700618 d = opendir(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700619 if (d == NULL) {
620 LOGE("error opening %s: %s\n", path, strerror(errno));
Michael Ward9d2629c2011-06-23 22:14:24 -0700621 if (unmount_when_done != NULL) {
622 ensure_path_unmounted(unmount_when_done);
623 }
Doug Zongker8674a722010-09-15 11:08:23 -0700624 return 0;
625 }
626
Doug Zongker28ce47c2011-10-28 10:33:05 -0700627 const char** headers = prepend_title(MENU_HEADERS);
Doug Zongker8674a722010-09-15 11:08:23 -0700628
629 int d_size = 0;
630 int d_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700631 char** dirs = (char**)malloc(d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700632 int z_size = 1;
633 int z_alloc = 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700634 char** zips = (char**)malloc(z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700635 zips[0] = strdup("../");
636
637 while ((de = readdir(d)) != NULL) {
638 int name_len = strlen(de->d_name);
639
640 if (de->d_type == DT_DIR) {
641 // skip "." and ".." entries
642 if (name_len == 1 && de->d_name[0] == '.') continue;
643 if (name_len == 2 && de->d_name[0] == '.' &&
644 de->d_name[1] == '.') continue;
645
646 if (d_size >= d_alloc) {
647 d_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700648 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700649 }
Doug Zongker28ce47c2011-10-28 10:33:05 -0700650 dirs[d_size] = (char*)malloc(name_len + 2);
Doug Zongker8674a722010-09-15 11:08:23 -0700651 strcpy(dirs[d_size], de->d_name);
652 dirs[d_size][name_len] = '/';
653 dirs[d_size][name_len+1] = '\0';
654 ++d_size;
655 } else if (de->d_type == DT_REG &&
656 name_len >= 4 &&
657 strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {
658 if (z_size >= z_alloc) {
659 z_alloc *= 2;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700660 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700661 }
662 zips[z_size++] = strdup(de->d_name);
663 }
664 }
665 closedir(d);
666
667 qsort(dirs, d_size, sizeof(char*), compare_string);
668 qsort(zips, z_size, sizeof(char*), compare_string);
669
670 // append dirs to the zips list
671 if (d_size + z_size + 1 > z_alloc) {
672 z_alloc = d_size + z_size + 1;
Doug Zongker28ce47c2011-10-28 10:33:05 -0700673 zips = (char**)realloc(zips, z_alloc * sizeof(char*));
Doug Zongker8674a722010-09-15 11:08:23 -0700674 }
675 memcpy(zips + z_size, dirs, d_size * sizeof(char*));
676 free(dirs);
677 z_size += d_size;
678 zips[z_size] = NULL;
679
680 int result;
681 int chosen_item = 0;
682 do {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700683 chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700684
685 char* item = zips[chosen_item];
686 int item_len = strlen(item);
687 if (chosen_item == 0) { // item 0 is always "../"
Michael Ward9d2629c2011-06-23 22:14:24 -0700688 // go up but continue browsing (if the caller is update_directory)
Doug Zongker8674a722010-09-15 11:08:23 -0700689 result = -1;
690 break;
691 } else if (item[item_len-1] == '/') {
692 // recurse down into a subdirectory
693 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700694 strlcpy(new_path, path, PATH_MAX);
695 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700696 strlcat(new_path, item, PATH_MAX);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700697 new_path[strlen(new_path)-1] = '\0'; // truncate the trailing '/'
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700698 result = update_directory(new_path, unmount_when_done, wipe_cache, device);
Doug Zongker8674a722010-09-15 11:08:23 -0700699 if (result >= 0) break;
700 } else {
701 // selected a zip file: attempt to install it, and return
702 // the status to the caller.
703 char new_path[PATH_MAX];
Doug Zongkerd4208f92010-09-20 12:16:13 -0700704 strlcpy(new_path, path, PATH_MAX);
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700705 strlcat(new_path, "/", PATH_MAX);
Doug Zongker8674a722010-09-15 11:08:23 -0700706 strlcat(new_path, item, PATH_MAX);
707
Doug Zongker211aebc2011-10-28 15:13:10 -0700708 ui->Print("\n-- Install %s ...\n", path);
Doug Zongker8674a722010-09-15 11:08:23 -0700709 set_sdcard_update_bootloader_message();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700710 char* copy = copy_sideloaded_package(new_path);
Michael Ward9d2629c2011-06-23 22:14:24 -0700711 if (unmount_when_done != NULL) {
712 ensure_path_unmounted(unmount_when_done);
713 }
Doug Zongkerd4208f92010-09-20 12:16:13 -0700714 if (copy) {
Doug Zongkerd0181b82011-10-19 10:51:12 -0700715 result = install_package(copy, wipe_cache, TEMPORARY_INSTALL_FILE);
Doug Zongkerd4208f92010-09-20 12:16:13 -0700716 free(copy);
717 } else {
718 result = INSTALL_ERROR;
719 }
Doug Zongker8674a722010-09-15 11:08:23 -0700720 break;
721 }
722 } while (true);
723
724 int i;
725 for (i = 0; i < z_size; ++i) free(zips[i]);
726 free(zips);
727 free(headers);
728
Michael Ward9d2629c2011-06-23 22:14:24 -0700729 if (unmount_when_done != NULL) {
730 ensure_path_unmounted(unmount_when_done);
731 }
Doug Zongker8674a722010-09-15 11:08:23 -0700732 return result;
733}
734
Doug Zongkerf93d8162009-09-22 15:16:02 -0700735static void
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700736wipe_data(int confirm, Device* device) {
Doug Zongkerf93d8162009-09-22 15:16:02 -0700737 if (confirm) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700738 static const char** title_headers = NULL;
Doug Zongkerddd6a282009-06-09 12:22:33 -0700739
Doug Zongkerf93d8162009-09-22 15:16:02 -0700740 if (title_headers == NULL) {
Doug Zongker28ce47c2011-10-28 10:33:05 -0700741 const char* headers[] = { "Confirm wipe of all user data?",
742 " THIS CAN NOT BE UNDONE.",
743 "",
744 NULL };
Doug Zongker8674a722010-09-15 11:08:23 -0700745 title_headers = prepend_title((const char**)headers);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700746 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800747
Doug Zongker28ce47c2011-10-28 10:33:05 -0700748 const char* items[] = { " No",
749 " No",
750 " No",
751 " No",
752 " No",
753 " No",
754 " No",
755 " Yes -- delete all user data", // [7]
756 " No",
757 " No",
758 " No",
759 NULL };
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800760
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700761 int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700762 if (chosen_item != 7) {
763 return;
764 }
765 }
Doug Zongker1066d2c2009-04-01 13:57:40 -0700766
Doug Zongker211aebc2011-10-28 15:13:10 -0700767 ui->Print("\n-- Wiping data...\n");
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700768 device->WipeData();
Doug Zongkerd4208f92010-09-20 12:16:13 -0700769 erase_volume("/data");
770 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700771 ui->Print("Data wipe complete.\n");
Doug Zongkerf93d8162009-09-22 15:16:02 -0700772}
773
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700774// Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION
775// means to take the default, which is to reboot or shutdown depending
776// on if the --shutdown_after flag was passed to recovery.
777static Device::BuiltinAction
Doug Zongker6c8553d2012-09-24 10:40:47 -0700778prompt_and_wait(Device* device, int status) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700779 const char* const* headers = prepend_title(device->GetMenuHeaders());
Doug Zongkerf93d8162009-09-22 15:16:02 -0700780
781 for (;;) {
782 finish_recovery(NULL);
Doug Zongker6c8553d2012-09-24 10:40:47 -0700783 switch (status) {
784 case INSTALL_SUCCESS:
785 case INSTALL_NONE:
786 ui->SetBackground(RecoveryUI::NO_COMMAND);
787 break;
788
789 case INSTALL_ERROR:
790 case INSTALL_CORRUPT:
791 ui->SetBackground(RecoveryUI::ERROR);
792 break;
793 }
Doug Zongker211aebc2011-10-28 15:13:10 -0700794 ui->SetProgressType(RecoveryUI::EMPTY);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700795
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700796 int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700797
798 // device-specific code may take some action here. It may
799 // return one of the core actions handled in the switch
800 // statement below.
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700801 Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item);
Doug Zongkerf93d8162009-09-22 15:16:02 -0700802
Doug Zongkerd0181b82011-10-19 10:51:12 -0700803 int wipe_cache;
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700804 switch (chosen_action) {
805 case Device::NO_ACTION:
806 break;
807
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700808 case Device::REBOOT:
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700809 case Device::SHUTDOWN:
810 case Device::REBOOT_BOOTLOADER:
811 return chosen_action;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700812
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700813 case Device::WIPE_DATA:
814 wipe_data(ui->IsTextVisible(), device);
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700815 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700816 break;
817
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700818 case Device::WIPE_CACHE:
Doug Zongker211aebc2011-10-28 15:13:10 -0700819 ui->Print("\n-- Wiping cache...\n");
Doug Zongkerd4208f92010-09-20 12:16:13 -0700820 erase_volume("/cache");
Doug Zongker211aebc2011-10-28 15:13:10 -0700821 ui->Print("Cache wipe complete.\n");
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700822 if (!ui->IsTextVisible()) return Device::NO_ACTION;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700823 break;
824
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700825 case Device::APPLY_EXT:
826 status = update_directory(SDCARD_ROOT, SDCARD_ROOT, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700827 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700828 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700829 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700830 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700831 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700832 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700833 }
834 }
Doug Zongker8674a722010-09-15 11:08:23 -0700835 if (status >= 0) {
836 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700837 ui->SetBackground(RecoveryUI::ERROR);
838 ui->Print("Installation aborted.\n");
839 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700840 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker8674a722010-09-15 11:08:23 -0700841 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700842 ui->Print("\nInstall from sdcard complete.\n");
Doug Zongker8674a722010-09-15 11:08:23 -0700843 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700844 }
845 break;
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700846
847 case Device::APPLY_CACHE:
Michael Ward9d2629c2011-06-23 22:14:24 -0700848 // Don't unmount cache at the end of this.
Doug Zongkerdaefc1d2011-10-31 09:34:15 -0700849 status = update_directory(CACHE_ROOT, NULL, &wipe_cache, device);
Doug Zongkerd0181b82011-10-19 10:51:12 -0700850 if (status == INSTALL_SUCCESS && wipe_cache) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700851 ui->Print("\n-- Wiping cache (at package request)...\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700852 if (erase_volume("/cache")) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700853 ui->Print("Cache wipe failed.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700854 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700855 ui->Print("Cache wipe complete.\n");
Doug Zongkerd0181b82011-10-19 10:51:12 -0700856 }
857 }
Michael Ward9d2629c2011-06-23 22:14:24 -0700858 if (status >= 0) {
859 if (status != INSTALL_SUCCESS) {
Doug Zongker211aebc2011-10-28 15:13:10 -0700860 ui->SetBackground(RecoveryUI::ERROR);
861 ui->Print("Installation aborted.\n");
862 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700863 return Device::NO_ACTION; // reboot if logs aren't visible
Michael Ward9d2629c2011-06-23 22:14:24 -0700864 } else {
Doug Zongker211aebc2011-10-28 15:13:10 -0700865 ui->Print("\nInstall from cache complete.\n");
Michael Ward9d2629c2011-06-23 22:14:24 -0700866 }
867 }
868 break;
Doug Zongker9270a202012-01-09 15:16:13 -0800869
870 case Device::APPLY_ADB_SIDELOAD:
Doug Zongker9270a202012-01-09 15:16:13 -0800871 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);
872 if (status >= 0) {
873 if (status != INSTALL_SUCCESS) {
874 ui->SetBackground(RecoveryUI::ERROR);
875 ui->Print("Installation aborted.\n");
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700876 copy_logs();
Doug Zongker9270a202012-01-09 15:16:13 -0800877 } else if (!ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -0700878 return Device::NO_ACTION; // reboot if logs aren't visible
Doug Zongker9270a202012-01-09 15:16:13 -0800879 } else {
880 ui->Print("\nInstall from ADB complete.\n");
881 }
882 }
883 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800884 }
885 }
886}
887
888static void
Oscar Montemayor05231562009-11-30 08:40:57 -0800889print_property(const char *key, const char *name, void *cookie) {
Doug Zongker56c51052010-07-01 09:18:44 -0700890 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800891}
892
Doug Zongker02ec6b82012-08-22 17:26:40 -0700893static void
894load_locale_from_cache() {
895 FILE* fp = fopen_path(LOCALE_FILE, "r");
896 char buffer[80];
897 if (fp != NULL) {
898 fgets(buffer, sizeof(buffer), fp);
899 int j = 0;
Doug Zongker5fa8c232012-09-18 12:37:02 -0700900 unsigned int i;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700901 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
902 if (!isspace(buffer[i])) {
903 buffer[j++] = buffer[i];
904 }
905 }
906 buffer[j] = 0;
907 locale = strdup(buffer);
Devin Kim6016d082012-10-08 09:47:37 -0700908 check_and_fclose(fp, LOCALE_FILE);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700909 }
910}
911
Doug Zongker7c3ae452013-05-14 11:03:02 -0700912static RecoveryUI* gCurrentUI = NULL;
913
914void
915ui_print(const char* format, ...) {
916 char buffer[256];
917
918 va_list ap;
919 va_start(ap, format);
920 vsnprintf(buffer, sizeof(buffer), format, ap);
921 va_end(ap);
922
923 if (gCurrentUI != NULL) {
924 gCurrentUI->Print("%s", buffer);
925 } else {
926 fputs(buffer, stdout);
927 }
928}
929
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800930int
Oscar Montemayor05231562009-11-30 08:40:57 -0800931main(int argc, char **argv) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800932 time_t start = time(NULL);
933
934 // If these fail, there's not really anywhere to complain...
935 freopen(TEMPORARY_LOG_FILE, "a", stdout); setbuf(stdout, NULL);
936 freopen(TEMPORARY_LOG_FILE, "a", stderr); setbuf(stderr, NULL);
Doug Zongker9270a202012-01-09 15:16:13 -0800937
938 // If this binary is started with the single argument "--adbd",
939 // instead of being the normal recovery binary, it turns into kind
940 // of a stripped-down version of adbd that only supports the
941 // 'sideload' command. Note this must be a real argument, not
942 // anything in the command file or bootloader control block; the
943 // only way recovery should be run with this argument is when it
944 // starts a copy of itself from the apply_from_adb() function.
945 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
946 adb_main();
947 return 0;
948 }
949
Doug Zongker19a8e242014-01-21 09:25:41 -0800950 printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800951
Doug Zongkerd4208f92010-09-20 12:16:13 -0700952 load_volume_table();
Doug Zongkerda1ebae2013-05-16 11:23:48 -0700953 ensure_path_mounted(LAST_LOG_FILE);
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700954 rotate_last_logs(10);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800955 get_args(&argc, &argv);
956
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800957 const char *send_intent = NULL;
958 const char *update_package = NULL;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700959 int wipe_data = 0, wipe_cache = 0, show_text = 0;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700960 bool just_exit = false;
Doug Zongkerb1d12632014-03-18 10:32:12 -0700961 bool shutdown_after = false;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800962
963 int arg;
964 while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {
965 switch (arg) {
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800966 case 's': send_intent = optarg; break;
967 case 'u': update_package = optarg; break;
968 case 'w': wipe_data = wipe_cache = 1; break;
969 case 'c': wipe_cache = 1; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700970 case 't': show_text = 1; break;
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700971 case 'x': just_exit = true; break;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700972 case 'l': locale = optarg; break;
Doug Zongkerc87bab12013-11-25 13:53:25 -0800973 case 'g': {
974 if (stage == NULL || *stage == '\0') {
975 char buffer[20] = "1/";
976 strncat(buffer, optarg, sizeof(buffer)-3);
977 stage = strdup(buffer);
978 }
979 break;
980 }
Doug Zongkerb1d12632014-03-18 10:32:12 -0700981 case 'p': shutdown_after = true; break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800982 case '?':
983 LOGE("Invalid command argument\n");
984 continue;
985 }
986 }
987
Doug Zongker02ec6b82012-08-22 17:26:40 -0700988 if (locale == NULL) {
989 load_locale_from_cache();
990 }
991 printf("locale is [%s]\n", locale);
Doug Zongker0d32f252014-02-13 15:07:56 -0800992 printf("stage is [%s]\n", stage);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700993
994 Device* device = make_device();
995 ui = device->GetUI();
Doug Zongker7c3ae452013-05-14 11:03:02 -0700996 gCurrentUI = ui;
Doug Zongker02ec6b82012-08-22 17:26:40 -0700997
Doug Zongker5fa8c232012-09-18 12:37:02 -0700998 ui->SetLocale(locale);
Doug Zongker02ec6b82012-08-22 17:26:40 -0700999 ui->Init();
Doug Zongkerc87bab12013-11-25 13:53:25 -08001000
1001 int st_cur, st_max;
1002 if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {
1003 ui->SetStage(st_cur, st_max);
1004 }
1005
Doug Zongker02ec6b82012-08-22 17:26:40 -07001006 ui->SetBackground(RecoveryUI::NONE);
1007 if (show_text) ui->ShowText(true);
1008
Stephen Smalley779701d2012-02-09 14:13:23 -05001009 struct selinux_opt seopts[] = {
1010 { SELABEL_OPT_PATH, "/file_contexts" }
1011 };
1012
1013 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
1014
1015 if (!sehandle) {
Doug Zongkerfafc85b2013-07-09 12:29:45 -07001016 ui->Print("Warning: No file_contexts\n");
Stephen Smalley779701d2012-02-09 14:13:23 -05001017 }
Stephen Smalley779701d2012-02-09 14:13:23 -05001018
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001019 device->StartRecovery();
Doug Zongkerefa1bab2010-02-01 15:59:12 -08001020
Doug Zongker56c51052010-07-01 09:18:44 -07001021 printf("Command:");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001022 for (arg = 0; arg < argc; arg++) {
Doug Zongker56c51052010-07-01 09:18:44 -07001023 printf(" \"%s\"", argv[arg]);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001024 }
Doug Zongker9b125b02010-09-22 12:01:37 -07001025 printf("\n");
1026
1027 if (update_package) {
1028 // For backwards compatibility on the cache partition only, if
1029 // we're given an old 'root' path "CACHE:foo", change it to
1030 // "/cache/foo".
1031 if (strncmp(update_package, "CACHE:", 6) == 0) {
1032 int len = strlen(update_package) + 10;
Doug Zongker28ce47c2011-10-28 10:33:05 -07001033 char* modified_path = (char*)malloc(len);
Doug Zongker9b125b02010-09-22 12:01:37 -07001034 strlcpy(modified_path, "/cache/", len);
1035 strlcat(modified_path, update_package+6, len);
1036 printf("(replacing path \"%s\" with \"%s\")\n",
1037 update_package, modified_path);
1038 update_package = modified_path;
1039 }
1040 }
1041 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001042
1043 property_list(print_property, NULL);
Doug Zongkerc0441d12013-07-31 11:28:24 -07001044 property_get("ro.build.display.id", recovery_version, "");
Doug Zongker56c51052010-07-01 09:18:44 -07001045 printf("\n");
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001046
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001047 int status = INSTALL_SUCCESS;
1048
Doug Zongker540d57f2011-01-18 13:36:58 -08001049 if (update_package != NULL) {
Doug Zongkerd0181b82011-10-19 10:51:12 -07001050 status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE);
1051 if (status == INSTALL_SUCCESS && wipe_cache) {
1052 if (erase_volume("/cache")) {
1053 LOGE("Cache wipe (requested by package) failed.");
1054 }
1055 }
Doug Zongker7c3ae452013-05-14 11:03:02 -07001056 if (status != INSTALL_SUCCESS) {
1057 ui->Print("Installation aborted.\n");
1058
1059 // If this is an eng or userdebug build, then automatically
1060 // turn the text display on if the script fails so the error
1061 // message is visible.
1062 char buffer[PROPERTY_VALUE_MAX+1];
1063 property_get("ro.build.fingerprint", buffer, "");
1064 if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {
1065 ui->ShowText(true);
1066 }
1067 }
Doug Zongkerb128f542009-06-18 15:07:14 -07001068 } else if (wipe_data) {
Doug Zongkerdaefc1d2011-10-31 09:34:15 -07001069 if (device->WipeData()) status = INSTALL_ERROR;
Doug Zongkerd4208f92010-09-20 12:16:13 -07001070 if (erase_volume("/data")) status = INSTALL_ERROR;
1071 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001072 if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n");
Doug Zongkerb128f542009-06-18 15:07:14 -07001073 } else if (wipe_cache) {
Doug Zongkerd4208f92010-09-20 12:16:13 -07001074 if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;
Doug Zongker211aebc2011-10-28 15:13:10 -07001075 if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n");
Doug Zongkere5d5ac72012-04-12 11:01:22 -07001076 } else if (!just_exit) {
Doug Zongker02ec6b82012-08-22 17:26:40 -07001077 status = INSTALL_NONE; // No command specified
1078 ui->SetBackground(RecoveryUI::NO_COMMAND);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001079 }
1080
Doug Zongker02ec6b82012-08-22 17:26:40 -07001081 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
Doug Zongkerf24fd7e2013-07-02 11:43:25 -07001082 copy_logs();
Doug Zongker02ec6b82012-08-22 17:26:40 -07001083 ui->SetBackground(RecoveryUI::ERROR);
1084 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001085 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
Doug Zongker211aebc2011-10-28 15:13:10 -07001086 if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001087 Device::BuiltinAction temp = prompt_and_wait(device, status);
1088 if (temp != Device::NO_ACTION) after = temp;
Doug Zongker8674a722010-09-15 11:08:23 -07001089 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001090
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001091 // Save logs and clean up before rebooting or shutting down.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001092 finish_recovery(send_intent);
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001093
1094 switch (after) {
1095 case Device::SHUTDOWN:
1096 ui->Print("Shutting down...\n");
1097 property_set(ANDROID_RB_PROPERTY, "shutdown,");
1098 break;
1099
1100 case Device::REBOOT_BOOTLOADER:
1101 ui->Print("Rebooting to bootloader...\n");
1102 property_set(ANDROID_RB_PROPERTY, "reboot,bootloader");
1103 break;
1104
1105 default:
1106 ui->Print("Rebooting...\n");
1107 property_set(ANDROID_RB_PROPERTY, "reboot,");
1108 break;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001109 }
Doug Zongker8d9d3d52014-04-01 13:20:23 -07001110 sleep(5); // should reboot before this finishes
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001111 return EXIT_SUCCESS;
1112}