blob: d2b3eb5168c23677ff067cc2a9fefffdeb0f25d1 [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>
Tao Bao862a4c12016-06-02 11:16:50 -070022#include <inttypes.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080023#include <limits.h>
Tao Bao862a4c12016-06-02 11:16:50 -070024#include <linux/fs.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <linux/input.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070026#include <stdarg.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Patrick Tjincd055ee2014-12-09 11:26:40 -080030#include <sys/klog.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070031#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080032#include <sys/types.h>
Tao Baocdcf28f2016-01-13 15:05:20 -080033#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080034#include <time.h>
35#include <unistd.h>
36
Tao Baoc4a18ef2017-02-10 00:13:30 -080037#include <algorithm>
Tao Bao04ca4262015-09-10 15:32:24 -070038#include <chrono>
Tao Baoc4a18ef2017-02-10 00:13:30 -080039#include <memory>
Tao Bao862a4c12016-06-02 11:16:50 -070040#include <string>
41#include <vector>
Tao Bao04ca4262015-09-10 15:32:24 -070042
Elliott Hughes4b166f02015-12-04 15:30:20 -080043#include <android-base/file.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070044#include <android-base/logging.h>
Tianjie Xu3c62b672016-02-05 18:25:58 -080045#include <android-base/parseint.h>
Elliott Hughescb220402016-09-23 15:30:55 -070046#include <android-base/properties.h>
Elliott Hughes4b166f02015-12-04 15:30:20 -080047#include <android-base/stringprintf.h>
Tao Bao862a4c12016-06-02 11:16:50 -070048#include <android-base/strings.h>
49#include <android-base/unique_fd.h>
Yabin Cui8b309f62016-06-24 18:22:02 -070050#include <bootloader_message/bootloader_message.h>
Tao Bao75238632015-05-27 14:46:17 -070051#include <cutils/android_reboot.h>
Elliott Hughescb220402016-09-23 15:30:55 -070052#include <cutils/properties.h> /* for property_list */
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070053#include <healthd/BatteryMonitor.h>
Mark Salyzyna4f701a2016-03-09 14:58:16 -080054#include <private/android_logger.h> /* private pmsg functions */
Tao Baoe1905ad2017-03-22 14:57:04 -070055#include <private/android_filesystem_config.h> /* for AID_SYSTEM */
Jeff Vander Stoepe35926e2017-06-14 15:30:39 -070056#include <selinux/android.h>
Elliott Hughes4bbd5bf2016-04-01 18:24:39 -070057#include <selinux/label.h>
58#include <selinux/selinux.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070059#include <ziparchive/zip_archive.h>
Yabin Cui99281df2016-02-17 12:21:52 -080060
Tao Bao75238632015-05-27 14:46:17 -070061#include "adb_install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080062#include "common.h"
Tao Bao75238632015-05-27 14:46:17 -070063#include "device.h"
64#include "fuse_sdcard_provider.h"
65#include "fuse_sideload.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080066#include "install.h"
Josh Gaoacb2a2f2016-08-26 18:24:34 -070067#include "minadbd/minadbd.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068#include "minui/minui.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070069#include "otautil/DirUtil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070070#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070071#include "otautil/paths.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080072#include "roots.h"
Tianjie Xue113e4d2016-10-21 17:46:13 -070073#include "rotate_logs.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070074#include "screen_ui.h"
Sen Jiangd5304492016-12-09 16:20:49 -080075#include "stub_ui.h"
Tianjie Xue113e4d2016-10-21 17:46:13 -070076#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080077
Tao Baoaac9d9f2018-04-29 23:38:59 -070078static constexpr const char* CACHE_LOG_DIR = "/cache/recovery";
79static constexpr const char* COMMAND_FILE = "/cache/recovery/command";
80static constexpr const char* LOG_FILE = "/cache/recovery/log";
81static constexpr const char* LAST_INSTALL_FILE = "/cache/recovery/last_install";
82static constexpr const char* LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
83static constexpr const char* LAST_LOG_FILE = "/cache/recovery/last_log";
84static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale";
Tianjie Xu06e57ac2016-07-11 14:04:08 -070085
Tao Baoaac9d9f2018-04-29 23:38:59 -070086static constexpr const char* CACHE_ROOT = "/cache";
87static constexpr const char* DATA_ROOT = "/data";
88static constexpr const char* METADATA_ROOT = "/metadata";
89static constexpr const char* SDCARD_ROOT = "/sdcard";
Nick Kralevicha9ad0322014-10-22 18:38:48 -070090
Tao Baobd0ddcd2017-05-04 13:03:18 -070091// We define RECOVERY_API_VERSION in Android.mk, which will be picked up by build system and packed
92// into target_files.zip. Assert the version defined in code and in Android.mk are consistent.
93static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions.");
94
Tao Baoac9d94d2016-11-03 11:37:15 -070095static std::string locale;
Dan Albert8584fcf2016-10-27 03:08:08 +000096static bool has_cache = false;
Tao Baoc0319b62016-10-13 15:17:04 -070097
Tao Baoac9d94d2016-11-03 11:37:15 -070098RecoveryUI* ui = nullptr;
99bool modified_flash = false;
Tao Baoa8d72bc2016-12-25 18:46:50 -0800100std::string stage;
Tao Baoac9d94d2016-11-03 11:37:15 -0700101const char* reason = nullptr;
102struct selabel_handle* sehandle;
103
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800104/*
105 * The recovery tool communicates with the main system through /cache files.
106 * /cache/recovery/command - INPUT - command line for tool, one arg per line
107 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800108 *
109 * The arguments which may be supplied in the recovery.command file:
Doug Zongkerd4208f92010-09-20 12:16:13 -0700110 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800111 * --wipe_data - erase user data (and cache), then reboot
Tao Baof9f17342018-04-27 10:44:04 -0700112 * --prompt_and_wipe_data - prompt the user that data is corrupt, with their consent erase user
113 * data (and cache), then reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800114 * --wipe_cache - wipe cache (but not user data), then reboot
Tao Baof9f17342018-04-27 10:44:04 -0700115 * --show_text - show the recovery text menu, used by some bootloader (e.g. http://b/36872519).
Oscar Montemayor05231562009-11-30 08:40:57 -0800116 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700117 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118 *
119 * After completing, we remove /cache/recovery/command and reboot.
120 * Arguments may also be supplied in the bootloader control block (BCB).
121 * These important scenarios must be safely restartable at any point:
122 *
123 * FACTORY RESET
124 * 1. user selects "factory reset"
125 * 2. main system writes "--wipe_data" to /cache/recovery/command
126 * 3. main system reboots into recovery
127 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
128 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700129 * 5. erase_volume() reformats /data
130 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800131 * 7. finish_recovery() erases BCB
132 * -- after this, rebooting will restart the main system --
133 * 8. main() calls reboot() to boot main system
134 *
135 * OTA INSTALL
136 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700137 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800138 * 3. main system reboots into recovery
139 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
140 * -- after this, rebooting will attempt to reinstall the update --
141 * 5. install_package() attempts to install the update
142 * NOTE: the package install must itself be restartable from any point
143 * 6. finish_recovery() erases BCB
144 * -- after this, rebooting will (try to) restart the main system --
145 * 7. ** if install failed **
146 * 7a. prompt_and_wait() shows an error icon and waits for the user
Tao Baoc0336392016-12-13 22:29:49 -0800147 * 7b. the user reboots (pulling the battery, etc) into the main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800148 */
149
Tao Baoac3d1ed2017-07-23 00:01:02 -0700150// Open a given path, mounting partitions as necessary.
Tao Bao641fa972018-04-25 18:59:40 -0700151FILE* fopen_path(const std::string& path, const char* mode) {
152 if (ensure_path_mounted(path.c_str()) != 0) {
Tao Baoac3d1ed2017-07-23 00:01:02 -0700153 LOG(ERROR) << "Can't mount " << path;
154 return nullptr;
155 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156
Tao Baoac3d1ed2017-07-23 00:01:02 -0700157 // When writing, try to create the containing directory, if necessary. Use generous permissions,
158 // the system (init.rc) will reset them.
159 if (strchr("wa", mode[0])) {
160 mkdir_recursively(path, 0777, true, sehandle);
161 }
Tao Bao641fa972018-04-25 18:59:40 -0700162 return fopen(path.c_str(), mode);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800163}
164
Tao Bao04ca4262015-09-10 15:32:24 -0700165// close a file, log an error if the error indicator is set
Tao Bao641fa972018-04-25 18:59:40 -0700166static void check_and_fclose(FILE* fp, const std::string& name) {
167 fflush(fp);
168 if (fsync(fileno(fp)) == -1) {
169 PLOG(ERROR) << "Failed to fsync " << name;
170 }
171 if (ferror(fp)) {
172 PLOG(ERROR) << "Error in " << name;
173 }
174 fclose(fp);
Tao Bao04ca4262015-09-10 15:32:24 -0700175}
176
Elliott Hughesf14af802015-02-10 14:46:14 -0800177bool is_ro_debuggable() {
Elliott Hughescb220402016-09-23 15:30:55 -0700178 return android::base::GetBoolProperty("ro.debuggable", false);
Elliott Hughesf14af802015-02-10 14:46:14 -0800179}
180
Dmitri Plotnikov8706a982017-04-18 08:28:26 -0700181bool reboot(const std::string& command) {
182 std::string cmd = command;
183 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
184 cmd += ",quiescent";
185 }
186 return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd);
187}
188
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700189static void redirect_stdio(const char* filename) {
Tao Bao04ca4262015-09-10 15:32:24 -0700190 int pipefd[2];
191 if (pipe(pipefd) == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700192 PLOG(ERROR) << "pipe failed";
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700193
Tao Bao04ca4262015-09-10 15:32:24 -0700194 // Fall back to traditional logging mode without timestamps.
195 // If these fail, there's not really anywhere to complain...
196 freopen(filename, "a", stdout); setbuf(stdout, NULL);
197 freopen(filename, "a", stderr); setbuf(stderr, NULL);
198
199 return;
200 }
201
202 pid_t pid = fork();
203 if (pid == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700204 PLOG(ERROR) << "fork failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700205
206 // Fall back to traditional logging mode without timestamps.
207 // If these fail, there's not really anywhere to complain...
208 freopen(filename, "a", stdout); setbuf(stdout, NULL);
209 freopen(filename, "a", stderr); setbuf(stderr, NULL);
210
211 return;
212 }
213
214 if (pid == 0) {
215 /// Close the unused write end.
216 close(pipefd[1]);
217
218 auto start = std::chrono::steady_clock::now();
219
220 // Child logger to actually write to the log file.
Tianjie Xude6735e2017-07-10 15:13:33 -0700221 FILE* log_fp = fopen(filename, "ae");
Tao Bao04ca4262015-09-10 15:32:24 -0700222 if (log_fp == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700223 PLOG(ERROR) << "fopen \"" << filename << "\" failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700224 close(pipefd[0]);
Tao Bao3da88012017-02-03 13:09:23 -0800225 _exit(EXIT_FAILURE);
Tao Bao04ca4262015-09-10 15:32:24 -0700226 }
227
228 FILE* pipe_fp = fdopen(pipefd[0], "r");
229 if (pipe_fp == nullptr) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700230 PLOG(ERROR) << "fdopen failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700231 check_and_fclose(log_fp, filename);
232 close(pipefd[0]);
Tao Bao3da88012017-02-03 13:09:23 -0800233 _exit(EXIT_FAILURE);
Tao Bao04ca4262015-09-10 15:32:24 -0700234 }
235
236 char* line = nullptr;
237 size_t len = 0;
238 while (getline(&line, &len, pipe_fp) != -1) {
239 auto now = std::chrono::steady_clock::now();
240 double duration = std::chrono::duration_cast<std::chrono::duration<double>>(
241 now - start).count();
242 if (line[0] == '\n') {
243 fprintf(log_fp, "[%12.6lf]\n", duration);
244 } else {
245 fprintf(log_fp, "[%12.6lf] %s", duration, line);
246 }
247 fflush(log_fp);
248 }
249
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700250 PLOG(ERROR) << "getline failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700251
252 free(line);
253 check_and_fclose(log_fp, filename);
254 close(pipefd[0]);
Tao Bao3da88012017-02-03 13:09:23 -0800255 _exit(EXIT_FAILURE);
Tao Bao04ca4262015-09-10 15:32:24 -0700256 } else {
257 // Redirect stdout/stderr to the logger process.
258 // Close the unused read end.
259 close(pipefd[0]);
260
261 setbuf(stdout, nullptr);
262 setbuf(stderr, nullptr);
263
264 if (dup2(pipefd[1], STDOUT_FILENO) == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700265 PLOG(ERROR) << "dup2 stdout failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700266 }
267 if (dup2(pipefd[1], STDERR_FILENO) == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700268 PLOG(ERROR) << "dup2 stderr failed";
Tao Bao04ca4262015-09-10 15:32:24 -0700269 }
270
271 close(pipefd[1]);
272 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800273}
274
275// command line args come from, in decreasing precedence:
276// - the actual command line
277// - the bootloader control block (one per line, after "recovery")
278// - the contents of COMMAND_FILE (one per line)
Tao Baof0ed1592016-12-02 11:32:19 -0800279static std::vector<std::string> get_args(const int argc, char** const argv) {
280 CHECK_GT(argc, 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800281
Tao Baof0ed1592016-12-02 11:32:19 -0800282 bootloader_message boot = {};
283 std::string err;
284 if (!read_bootloader_message(&boot, &err)) {
285 LOG(ERROR) << err;
286 // If fails, leave a zeroed bootloader_message.
287 boot = {};
288 }
Tao Baoa8d72bc2016-12-25 18:46:50 -0800289 stage = std::string(boot.stage);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800290
Tao Baof0ed1592016-12-02 11:32:19 -0800291 if (boot.command[0] != 0) {
292 std::string boot_command = std::string(boot.command, sizeof(boot.command));
293 LOG(INFO) << "Boot command: " << boot_command;
294 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800295
Tao Baof0ed1592016-12-02 11:32:19 -0800296 if (boot.status[0] != 0) {
297 std::string boot_status = std::string(boot.status, sizeof(boot.status));
298 LOG(INFO) << "Boot status: " << boot_status;
299 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800300
Tao Baof0ed1592016-12-02 11:32:19 -0800301 std::vector<std::string> args(argv, argv + argc);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800302
Tao Baof0ed1592016-12-02 11:32:19 -0800303 // --- if arguments weren't supplied, look in the bootloader control block
Tao Bao570af9d2017-01-09 10:29:59 -0800304 if (args.size() == 1) {
Tao Baof0ed1592016-12-02 11:32:19 -0800305 boot.recovery[sizeof(boot.recovery) - 1] = '\0'; // Ensure termination
306 std::string boot_recovery(boot.recovery);
307 std::vector<std::string> tokens = android::base::Split(boot_recovery, "\n");
308 if (!tokens.empty() && tokens[0] == "recovery") {
309 for (auto it = tokens.begin() + 1; it != tokens.end(); it++) {
310 // Skip empty and '\0'-filled tokens.
311 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
312 }
313 LOG(INFO) << "Got " << args.size() << " arguments from boot message";
314 } else if (boot.recovery[0] != 0) {
315 LOG(ERROR) << "Bad boot message: \"" << boot_recovery << "\"";
316 }
317 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800318
Tao Baof0ed1592016-12-02 11:32:19 -0800319 // --- if that doesn't work, try the command file (if we have /cache).
Tao Bao570af9d2017-01-09 10:29:59 -0800320 if (args.size() == 1 && has_cache) {
Tao Baof0ed1592016-12-02 11:32:19 -0800321 std::string content;
Tao Bao7d34fa12016-12-08 18:10:48 -0800322 if (ensure_path_mounted(COMMAND_FILE) == 0 &&
323 android::base::ReadFileToString(COMMAND_FILE, &content)) {
Tao Baof0ed1592016-12-02 11:32:19 -0800324 std::vector<std::string> tokens = android::base::Split(content, "\n");
Tao Bao7d34fa12016-12-08 18:10:48 -0800325 // All the arguments in COMMAND_FILE are needed (unlike the BCB message,
326 // COMMAND_FILE doesn't use filename as the first argument).
327 for (auto it = tokens.begin(); it != tokens.end(); it++) {
Tao Baof0ed1592016-12-02 11:32:19 -0800328 // Skip empty and '\0'-filled tokens.
329 if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it));
330 }
331 LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800332 }
Tao Baof0ed1592016-12-02 11:32:19 -0800333 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800334
Tao Baof0ed1592016-12-02 11:32:19 -0800335 // Write the arguments (excluding the filename in args[0]) back into the
336 // bootloader control block. So the device will always boot into recovery to
337 // finish the pending work, until finish_recovery() is called.
338 std::vector<std::string> options(args.cbegin() + 1, args.cend());
Tao Bao2292db82016-12-13 21:53:31 -0800339 if (!update_bootloader_message(options, &err)) {
340 LOG(ERROR) << "Failed to set BCB message: " << err;
Tao Baof0ed1592016-12-02 11:32:19 -0800341 }
342
343 return args;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800344}
345
Tao Bao2292db82016-12-13 21:53:31 -0800346// Set the BCB to reboot back into recovery (it won't resume the install from
347// sdcard though).
348static void set_sdcard_update_bootloader_message() {
349 std::vector<std::string> options;
350 std::string err;
351 if (!update_bootloader_message(options, &err)) {
352 LOG(ERROR) << "Failed to set BCB message: " << err;
353 }
Doug Zongker34c98df2009-08-18 12:05:45 -0700354}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800355
Tao Baobef39712015-05-04 18:50:27 -0700356// Read from kernel log into buffer and write out to file.
357static void save_kernel_log(const char* destination) {
358 int klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0);
Patrick Tjincd055ee2014-12-09 11:26:40 -0800359 if (klog_buf_len <= 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700360 PLOG(ERROR) << "Error getting klog size";
Patrick Tjincd055ee2014-12-09 11:26:40 -0800361 return;
362 }
363
Tao Baobef39712015-05-04 18:50:27 -0700364 std::string buffer(klog_buf_len, 0);
365 int n = klogctl(KLOG_READ_ALL, &buffer[0], klog_buf_len);
366 if (n == -1) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700367 PLOG(ERROR) << "Error in reading klog";
Patrick Tjincd055ee2014-12-09 11:26:40 -0800368 return;
369 }
Tao Baobef39712015-05-04 18:50:27 -0700370 buffer.resize(n);
371 android::base::WriteStringToFile(buffer, destination);
Patrick Tjincd055ee2014-12-09 11:26:40 -0800372}
373
Tao Bao641fa972018-04-25 18:59:40 -0700374// Writes content to the current pmsg session.
375static ssize_t __pmsg_write(const std::string& filename, const std::string& buf) {
376 return __android_log_pmsg_file_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filename.c_str(),
377 buf.data(), buf.size());
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800378}
379
Tao Bao641fa972018-04-25 18:59:40 -0700380static void copy_log_file_to_pmsg(const std::string& source, const std::string& destination) {
381 std::string content;
382 android::base::ReadFileToString(source, &content);
383 __pmsg_write(destination, content);
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800384}
385
Tao Baof0124322015-04-11 02:04:11 +0000386// How much of the temp log we have copied to the copy in cache.
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700387static off_t tmplog_offset = 0;
Tao Baof0124322015-04-11 02:04:11 +0000388
Tao Bao641fa972018-04-25 18:59:40 -0700389static void copy_log_file(const std::string& source, const std::string& destination, bool append) {
Tianjie Xude6735e2017-07-10 15:13:33 -0700390 FILE* dest_fp = fopen_path(destination, append ? "ae" : "we");
391 if (dest_fp == nullptr) {
392 PLOG(ERROR) << "Can't open " << destination;
393 } else {
Tao Bao641fa972018-04-25 18:59:40 -0700394 FILE* source_fp = fopen(source.c_str(), "re");
Tianjie Xude6735e2017-07-10 15:13:33 -0700395 if (source_fp != nullptr) {
396 if (append) {
397 fseeko(source_fp, tmplog_offset, SEEK_SET); // Since last write
398 }
399 char buf[4096];
400 size_t bytes;
401 while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
402 fwrite(buf, 1, bytes, dest_fp);
403 }
404 if (append) {
405 tmplog_offset = ftello(source_fp);
406 }
407 check_and_fclose(source_fp, source);
Doug Zongker2c3539e2010-09-29 13:21:30 -0700408 }
Tianjie Xude6735e2017-07-10 15:13:33 -0700409 check_and_fclose(dest_fp, destination);
410 }
Doug Zongker2c3539e2010-09-29 13:21:30 -0700411}
412
Tao Bao682c34b2015-04-07 17:16:35 -0700413static void copy_logs() {
Tao Bao641fa972018-04-25 18:59:40 -0700414 // We only rotate and record the log of the current session if there are actual attempts to modify
415 // the flash, such as wipes, installs from BCB or menu selections. This is to avoid unnecessary
416 // rotation (and possible deletion) of log files, if it does not do anything loggable.
417 if (!modified_flash) {
418 return;
419 }
Tao Bao682c34b2015-04-07 17:16:35 -0700420
Tao Bao641fa972018-04-25 18:59:40 -0700421 // Always write to pmsg, this allows the OTA logs to be caught in `logcat -L`.
422 copy_log_file_to_pmsg(Paths::Get().temporary_log_file(), LAST_LOG_FILE);
423 copy_log_file_to_pmsg(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE);
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800424
Tao Bao641fa972018-04-25 18:59:40 -0700425 // We can do nothing for now if there's no /cache partition.
426 if (!has_cache) {
427 return;
428 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800429
Tao Bao641fa972018-04-25 18:59:40 -0700430 ensure_path_mounted(LAST_LOG_FILE);
431 ensure_path_mounted(LAST_KMSG_FILE);
432 rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
Tao Bao682c34b2015-04-07 17:16:35 -0700433
Tao Bao641fa972018-04-25 18:59:40 -0700434 // Copy logs to cache so the system can find out what happened.
435 copy_log_file(Paths::Get().temporary_log_file(), LOG_FILE, true);
436 copy_log_file(Paths::Get().temporary_log_file(), LAST_LOG_FILE, false);
437 copy_log_file(Paths::Get().temporary_install_file(), LAST_INSTALL_FILE, false);
438 save_kernel_log(LAST_KMSG_FILE);
439 chmod(LOG_FILE, 0600);
440 chown(LOG_FILE, AID_SYSTEM, AID_SYSTEM);
441 chmod(LAST_KMSG_FILE, 0600);
442 chown(LAST_KMSG_FILE, AID_SYSTEM, AID_SYSTEM);
443 chmod(LAST_LOG_FILE, 0640);
444 chmod(LAST_INSTALL_FILE, 0644);
445 sync();
Doug Zongkerf24fd7e2013-07-02 11:43:25 -0700446}
447
Tao Baoec579032017-07-21 12:13:15 -0700448// Clear the recovery command and prepare to boot a (hopefully working) system,
Tianjie Xuc14d95d2016-03-24 11:50:34 -0700449// copy our log file to cache as well (for the system to read). This function is
450// idempotent: call it as many times as you like.
Tao Baoac9d94d2016-11-03 11:37:15 -0700451static void finish_recovery() {
Tao Baoec579032017-07-21 12:13:15 -0700452 // Save the locale to cache, so if recovery is next started up without a '--locale' argument
453 // (e.g., directly from the bootloader) it will use the last-known locale.
454 if (!locale.empty() && has_cache) {
455 LOG(INFO) << "Saving locale \"" << locale << "\"";
456 if (ensure_path_mounted(LOCALE_FILE) != 0) {
457 LOG(ERROR) << "Failed to mount " << LOCALE_FILE;
458 } else if (!android::base::WriteStringToFile(locale, LOCALE_FILE)) {
459 PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE;
Doug Zongker4f33e552012-08-23 13:16:12 -0700460 }
Tao Baoec579032017-07-21 12:13:15 -0700461 }
Doug Zongker4f33e552012-08-23 13:16:12 -0700462
Tao Baoec579032017-07-21 12:13:15 -0700463 copy_logs();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800464
Tao Baoec579032017-07-21 12:13:15 -0700465 // Reset to normal system boot so recovery won't cycle indefinitely.
466 std::string err;
467 if (!clear_bootloader_message(&err)) {
468 LOG(ERROR) << "Failed to clear BCB message: " << err;
469 }
470
471 // Remove the command file, so recovery won't repeat indefinitely.
472 if (has_cache) {
473 if (ensure_path_mounted(COMMAND_FILE) != 0 || (unlink(COMMAND_FILE) && errno != ENOENT)) {
474 LOG(WARNING) << "Can't unlink " << COMMAND_FILE;
Yabin Cui8b309f62016-06-24 18:22:02 -0700475 }
Tao Baoec579032017-07-21 12:13:15 -0700476 ensure_path_unmounted(CACHE_ROOT);
477 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800478
Tao Baoec579032017-07-21 12:13:15 -0700479 sync(); // For good measure.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800480}
481
Tao Bao3f5a3822016-12-13 11:14:37 -0800482struct saved_log_file {
483 std::string name;
484 struct stat sb;
485 std::string data;
486};
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700487
Elliott Hughes945548e2015-06-05 17:59:56 -0700488static bool erase_volume(const char* volume) {
Tao Bao3f5a3822016-12-13 11:14:37 -0800489 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
490 bool is_data = (strcmp(volume, DATA_ROOT) == 0);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700491
Tao Bao3f5a3822016-12-13 11:14:37 -0800492 ui->SetBackground(RecoveryUI::ERASING);
493 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700494
Tao Bao3f5a3822016-12-13 11:14:37 -0800495 std::vector<saved_log_file> log_files;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700496
Tao Bao3f5a3822016-12-13 11:14:37 -0800497 if (is_cache) {
498 // If we're reformatting /cache, we load any past logs
499 // (i.e. "/cache/recovery/last_*") and the current log
500 // ("/cache/recovery/log") into memory, so we can restore them after
501 // the reformat.
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700502
Tao Bao3f5a3822016-12-13 11:14:37 -0800503 ensure_path_mounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700504
Tao Bao3f5a3822016-12-13 11:14:37 -0800505 struct dirent* de;
506 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(CACHE_LOG_DIR), closedir);
507 if (d) {
508 while ((de = readdir(d.get())) != nullptr) {
509 if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) {
510 std::string path = android::base::StringPrintf("%s/%s", CACHE_LOG_DIR, de->d_name);
511
512 struct stat sb;
513 if (stat(path.c_str(), &sb) == 0) {
514 // truncate files to 512kb
515 if (sb.st_size > (1 << 19)) {
516 sb.st_size = 1 << 19;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700517 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800518
519 std::string data(sb.st_size, '\0');
Tianjie Xude6735e2017-07-10 15:13:33 -0700520 FILE* f = fopen(path.c_str(), "rbe");
Tao Bao3f5a3822016-12-13 11:14:37 -0800521 fread(&data[0], 1, data.size(), f);
522 fclose(f);
523
524 log_files.emplace_back(saved_log_file{ path, sb, data });
525 }
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700526 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800527 }
Paul Lawrenced0db3372015-11-05 13:38:40 -0800528 } else {
Tao Bao3f5a3822016-12-13 11:14:37 -0800529 if (errno != ENOENT) {
530 PLOG(ERROR) << "Failed to opendir " << CACHE_LOG_DIR;
531 }
Paul Lawrenced0db3372015-11-05 13:38:40 -0800532 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800533 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700534
Tao Bao3f5a3822016-12-13 11:14:37 -0800535 ui->Print("Formatting %s...\n", volume);
536
537 ensure_path_unmounted(volume);
538
539 int result;
Tao Bao3f5a3822016-12-13 11:14:37 -0800540 if (is_data && reason && strcmp(reason, "convert_fbe") == 0) {
Tao Bao406a6ff2018-04-30 10:05:57 -0700541 static constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe";
542 static constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe";
543 // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not
544 // full disk encryption.
Tao Bao3f5a3822016-12-13 11:14:37 -0800545 if (mkdir(CONVERT_FBE_DIR, 0700) != 0) {
Tao Bao406a6ff2018-04-30 10:05:57 -0700546 PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR;
547 return false;
Tao Bao3f5a3822016-12-13 11:14:37 -0800548 }
Tianjie Xude6735e2017-07-10 15:13:33 -0700549 FILE* f = fopen(CONVERT_FBE_FILE, "wbe");
Tao Bao3f5a3822016-12-13 11:14:37 -0800550 if (!f) {
Tao Bao406a6ff2018-04-30 10:05:57 -0700551 PLOG(ERROR) << "Failed to convert to file encryption";
552 return false;
Tao Bao3f5a3822016-12-13 11:14:37 -0800553 }
554 fclose(f);
555 result = format_volume(volume, CONVERT_FBE_DIR);
556 remove(CONVERT_FBE_FILE);
557 rmdir(CONVERT_FBE_DIR);
558 } else {
559 result = format_volume(volume);
560 }
561
562 if (is_cache) {
563 // Re-create the log dir and write back the log entries.
564 if (ensure_path_mounted(CACHE_LOG_DIR) == 0 &&
Tao Baoac3d1ed2017-07-23 00:01:02 -0700565 mkdir_recursively(CACHE_LOG_DIR, 0777, false, sehandle) == 0) {
Tao Bao3f5a3822016-12-13 11:14:37 -0800566 for (const auto& log : log_files) {
567 if (!android::base::WriteStringToFile(log.data, log.name, log.sb.st_mode, log.sb.st_uid,
568 log.sb.st_gid)) {
569 PLOG(ERROR) << "Failed to write to " << log.name;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700570 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800571 }
572 } else {
573 PLOG(ERROR) << "Failed to mount / create " << CACHE_LOG_DIR;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700574 }
575
Tao Bao3f5a3822016-12-13 11:14:37 -0800576 // Any part of the log we'd copied to cache is now gone.
577 // Reset the pointer so we copy from the beginning of the temp
578 // log.
579 tmplog_offset = 0;
580 copy_logs();
581 }
582
583 return (result == 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800584}
585
Tao Baofc5499f2017-02-23 19:06:53 -0800586// Display a menu with the specified 'headers' and 'items'. Device specific HandleMenuKey() may
587// return a positive number beyond the given range. Caller sets 'menu_only' to true to ensure only
Tao Bao50dd5322017-03-07 14:57:04 -0800588// a menu item gets selected. 'initial_selection' controls the initial cursor location. Returns the
589// (non-negative) chosen item number, or -1 if timed out waiting for input.
Tao Baofc5499f2017-02-23 19:06:53 -0800590static int get_menu_selection(const char* const* headers, const char* const* items, bool menu_only,
591 int initial_selection, Device* device) {
592 // Throw away keys pressed previously, so user doesn't accidentally trigger menu items.
593 ui->FlushKeys();
Doug Zongkerf93d8162009-09-22 15:16:02 -0700594
Tao Baofc5499f2017-02-23 19:06:53 -0800595 ui->StartMenu(headers, items, initial_selection);
Tao Bao50dd5322017-03-07 14:57:04 -0800596
Tao Baofc5499f2017-02-23 19:06:53 -0800597 int selected = initial_selection;
598 int chosen_item = -1;
Tao Baofc5499f2017-02-23 19:06:53 -0800599 while (chosen_item < 0) {
600 int key = ui->WaitKey();
Tao Bao50dd5322017-03-07 14:57:04 -0800601 if (key == -1) { // WaitKey() timed out.
Tao Baofc5499f2017-02-23 19:06:53 -0800602 if (ui->WasTextEverVisible()) {
603 continue;
604 } else {
Tao Bao50dd5322017-03-07 14:57:04 -0800605 LOG(INFO) << "Timed out waiting for key input; rebooting.";
Tao Baofc5499f2017-02-23 19:06:53 -0800606 ui->EndMenu();
Tao Bao50dd5322017-03-07 14:57:04 -0800607 return -1;
Tao Baofc5499f2017-02-23 19:06:53 -0800608 }
Doug Zongkerf93d8162009-09-22 15:16:02 -0700609 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800610
Tao Baofc5499f2017-02-23 19:06:53 -0800611 bool visible = ui->IsTextVisible();
612 int action = device->HandleMenuKey(key, visible);
613
614 if (action < 0) {
615 switch (action) {
616 case Device::kHighlightUp:
617 selected = ui->SelectMenu(--selected);
618 break;
619 case Device::kHighlightDown:
620 selected = ui->SelectMenu(++selected);
621 break;
622 case Device::kInvokeItem:
623 chosen_item = selected;
624 break;
625 case Device::kNoAction:
626 break;
627 }
628 } else if (!menu_only) {
629 chosen_item = action;
630 }
631 }
632
633 ui->EndMenu();
634 return chosen_item;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700635}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800636
Tao Baoc4a18ef2017-02-10 00:13:30 -0800637// Returns the selected filename, or an empty string.
638static std::string browse_directory(const std::string& path, Device* device) {
639 ensure_path_mounted(path.c_str());
Doug Zongker8674a722010-09-15 11:08:23 -0700640
Tao Baoc4a18ef2017-02-10 00:13:30 -0800641 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path.c_str()), closedir);
642 if (!d) {
643 PLOG(ERROR) << "error opening " << path;
644 return "";
645 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700646
Tao Baoc4a18ef2017-02-10 00:13:30 -0800647 std::vector<std::string> dirs;
648 std::vector<std::string> zips = { "../" }; // "../" is always the first entry.
649
650 dirent* de;
651 while ((de = readdir(d.get())) != nullptr) {
652 std::string name(de->d_name);
653
654 if (de->d_type == DT_DIR) {
655 // Skip "." and ".." entries.
656 if (name == "." || name == "..") continue;
657 dirs.push_back(name + "/");
658 } else if (de->d_type == DT_REG && android::base::EndsWithIgnoreCase(name, ".zip")) {
659 zips.push_back(name);
660 }
661 }
662
663 std::sort(dirs.begin(), dirs.end());
664 std::sort(zips.begin(), zips.end());
665
666 // Append dirs to the zips list.
667 zips.insert(zips.end(), dirs.begin(), dirs.end());
668
669 const char* entries[zips.size() + 1];
670 entries[zips.size()] = nullptr;
671 for (size_t i = 0; i < zips.size(); i++) {
672 entries[i] = zips[i].c_str();
673 }
674
675 const char* headers[] = { "Choose a package to install:", path.c_str(), nullptr };
676
677 int chosen_item = 0;
678 while (true) {
Tao Baofc5499f2017-02-23 19:06:53 -0800679 chosen_item = get_menu_selection(headers, entries, true, chosen_item, device);
Tao Baoc4a18ef2017-02-10 00:13:30 -0800680
681 const std::string& item = zips[chosen_item];
682 if (chosen_item == 0) {
683 // Go up but continue browsing (if the caller is browse_directory).
684 return "";
Doug Zongker8674a722010-09-15 11:08:23 -0700685 }
686
Tao Baoc4a18ef2017-02-10 00:13:30 -0800687 std::string new_path = path + "/" + item;
688 if (new_path.back() == '/') {
689 // Recurse down into a subdirectory.
690 new_path.pop_back();
691 std::string result = browse_directory(new_path, device);
692 if (!result.empty()) return result;
693 } else {
694 // Selected a zip file: return the path to the caller.
695 return new_path;
Doug Zongker8674a722010-09-15 11:08:23 -0700696 }
Tao Baoc4a18ef2017-02-10 00:13:30 -0800697 }
Doug Zongker8674a722010-09-15 11:08:23 -0700698
Tao Baoc4a18ef2017-02-10 00:13:30 -0800699 // Unreachable.
Doug Zongker8674a722010-09-15 11:08:23 -0700700}
701
Elliott Hughes30694c92015-03-25 15:16:51 -0700702static bool yes_no(Device* device, const char* question1, const char* question2) {
Elliott Hughes8fd86d72015-04-13 14:36:02 -0700703 const char* headers[] = { question1, question2, NULL };
Elliott Hughes30694c92015-03-25 15:16:51 -0700704 const char* items[] = { " No", " Yes", NULL };
Doug Zongkerddd6a282009-06-09 12:22:33 -0700705
Tao Baofc5499f2017-02-23 19:06:53 -0800706 int chosen_item = get_menu_selection(headers, items, true, 0, device);
Elliott Hughes30694c92015-03-25 15:16:51 -0700707 return (chosen_item == 1);
708}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800709
Paul Crowley08404b42016-12-19 13:04:23 -0800710static bool ask_to_wipe_data(Device* device) {
711 return yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!");
712}
Doug Zongker1066d2c2009-04-01 13:57:40 -0700713
Paul Crowley08404b42016-12-19 13:04:23 -0800714// Return true on success.
715static bool wipe_data(Device* device) {
Tao Bao682c34b2015-04-07 17:16:35 -0700716 modified_flash = true;
717
Doug Zongker211aebc2011-10-28 15:13:10 -0700718 ui->Print("\n-- Wiping data...\n");
Paul Crowley3b4d5162016-06-08 13:51:41 -0700719 bool success = device->PreWipeData();
720 if (success) {
721 success &= erase_volume(DATA_ROOT);
722 if (has_cache) {
723 success &= erase_volume(CACHE_ROOT);
724 }
725 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
726 success &= erase_volume(METADATA_ROOT);
727 }
728 }
729 if (success) {
730 success &= device->PostWipeData();
731 }
Elliott Hughes945548e2015-06-05 17:59:56 -0700732 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
733 return success;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700734}
735
Paul Crowley08404b42016-12-19 13:04:23 -0800736static bool prompt_and_wipe_data(Device* device) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700737 // Use a single string and let ScreenRecoveryUI handles the wrapping.
Tao Baofc5499f2017-02-23 19:06:53 -0800738 const char* const headers[] = {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700739 "Can't load Android system. Your data may be corrupt. "
740 "If you continue to get this message, you may need to "
741 "perform a factory data reset and erase all user data "
Paul Crowley31ac0c62017-03-23 12:32:40 -0700742 "stored on this device.",
Tao Bao2bbc6d62017-08-13 23:48:55 -0700743 nullptr
Tao Baofc5499f2017-02-23 19:06:53 -0800744 };
745 const char* const items[] = {
Paul Crowley31ac0c62017-03-23 12:32:40 -0700746 "Try again",
747 "Factory data reset",
Tao Baofc5499f2017-02-23 19:06:53 -0800748 NULL
749 };
750 for (;;) {
751 int chosen_item = get_menu_selection(headers, items, true, 0, device);
752 if (chosen_item != 1) {
753 return true; // Just reboot, no wipe; not a failure, user asked for it
Paul Crowley08404b42016-12-19 13:04:23 -0800754 }
Tao Baofc5499f2017-02-23 19:06:53 -0800755 if (ask_to_wipe_data(device)) {
756 return wipe_data(device);
757 }
758 }
Paul Crowley08404b42016-12-19 13:04:23 -0800759}
760
Tao Baoe39a9bc2015-03-31 12:19:05 -0700761// Return true on success.
762static bool wipe_cache(bool should_confirm, Device* device) {
Tao Bao26112e52016-02-25 12:29:40 -0800763 if (!has_cache) {
764 ui->Print("No /cache partition found.\n");
765 return false;
766 }
767
Elliott Hughes30694c92015-03-25 15:16:51 -0700768 if (should_confirm && !yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!")) {
Tao Baoe39a9bc2015-03-31 12:19:05 -0700769 return false;
Elliott Hughes30694c92015-03-25 15:16:51 -0700770 }
771
Tao Bao682c34b2015-04-07 17:16:35 -0700772 modified_flash = true;
773
Elliott Hughes30694c92015-03-25 15:16:51 -0700774 ui->Print("\n-- Wiping cache...\n");
Elliott Hughes945548e2015-06-05 17:59:56 -0700775 bool success = erase_volume("/cache");
776 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
777 return success;
Elliott Hughes30694c92015-03-25 15:16:51 -0700778}
779
Tao Bao1b2a98b2017-03-24 10:45:34 -0700780// Secure-wipe a given partition. It uses BLKSECDISCARD, if supported. Otherwise, it goes with
781// BLKDISCARD (if device supports BLKDISCARDZEROES) or BLKZEROOUT.
Tao Bao862a4c12016-06-02 11:16:50 -0700782static bool secure_wipe_partition(const std::string& partition) {
Tao Bao1b2a98b2017-03-24 10:45:34 -0700783 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY)));
784 if (fd == -1) {
785 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
786 return false;
787 }
788
789 uint64_t range[2] = { 0, 0 };
790 if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) {
791 PLOG(ERROR) << "Failed to get partition size";
792 return false;
793 }
794 LOG(INFO) << "Secure-wiping \"" << partition << "\" from " << range[0] << " to " << range[1];
795
796 LOG(INFO) << " Trying BLKSECDISCARD...";
797 if (ioctl(fd, BLKSECDISCARD, &range) == -1) {
798 PLOG(WARNING) << " Failed";
799
800 // Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT.
801 unsigned int zeroes;
802 if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) {
803 LOG(INFO) << " Trying BLKDISCARD...";
804 if (ioctl(fd, BLKDISCARD, &range) == -1) {
805 PLOG(ERROR) << " Failed";
Tao Bao862a4c12016-06-02 11:16:50 -0700806 return false;
Tao Bao1b2a98b2017-03-24 10:45:34 -0700807 }
808 } else {
809 LOG(INFO) << " Trying BLKZEROOUT...";
810 if (ioctl(fd, BLKZEROOUT, &range) == -1) {
811 PLOG(ERROR) << " Failed";
Tao Bao862a4c12016-06-02 11:16:50 -0700812 return false;
Tao Bao1b2a98b2017-03-24 10:45:34 -0700813 }
Tao Bao862a4c12016-06-02 11:16:50 -0700814 }
Tao Bao1b2a98b2017-03-24 10:45:34 -0700815 }
Tao Bao862a4c12016-06-02 11:16:50 -0700816
Tao Bao1b2a98b2017-03-24 10:45:34 -0700817 LOG(INFO) << " Done";
818 return true;
Tao Bao862a4c12016-06-02 11:16:50 -0700819}
820
Yabin Cuifd99a312016-06-09 14:09:39 -0700821// Check if the wipe package matches expectation:
822// 1. verify the package.
823// 2. check metadata (ota-type, pre-device and serial number if having one).
824static bool check_wipe_package(size_t wipe_package_size) {
825 if (wipe_package_size == 0) {
826 LOG(ERROR) << "wipe_package_size is zero";
827 return false;
828 }
829 std::string wipe_package;
830 std::string err_str;
831 if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) {
832 PLOG(ERROR) << "Failed to read wipe package";
833 return false;
834 }
835 if (!verify_package(reinterpret_cast<const unsigned char*>(wipe_package.data()),
836 wipe_package.size())) {
837 LOG(ERROR) << "Failed to verify package";
838 return false;
839 }
840
841 // Extract metadata
842 ZipArchiveHandle zip;
Tao Baoefc35592017-01-08 22:45:47 -0800843 int err = OpenArchiveFromMemory(static_cast<void*>(&wipe_package[0]), wipe_package.size(),
844 "wipe_package", &zip);
Yabin Cuifd99a312016-06-09 14:09:39 -0700845 if (err != 0) {
846 LOG(ERROR) << "Can't open wipe package : " << ErrorCodeString(err);
847 return false;
848 }
849 std::string metadata;
Tao Bao1b2a98b2017-03-24 10:45:34 -0700850 if (!read_metadata_from_package(zip, &metadata)) {
Yabin Cuifd99a312016-06-09 14:09:39 -0700851 CloseArchive(zip);
852 return false;
853 }
854 CloseArchive(zip);
855
856 // Check metadata
857 std::vector<std::string> lines = android::base::Split(metadata, "\n");
858 bool ota_type_matched = false;
859 bool device_type_matched = false;
860 bool has_serial_number = false;
861 bool serial_number_matched = false;
862 for (const auto& line : lines) {
863 if (line == "ota-type=BRICK") {
864 ota_type_matched = true;
865 } else if (android::base::StartsWith(line, "pre-device=")) {
866 std::string device_type = line.substr(strlen("pre-device="));
Tao Baoefc35592017-01-08 22:45:47 -0800867 std::string real_device_type = android::base::GetProperty("ro.build.product", "");
Yabin Cuifd99a312016-06-09 14:09:39 -0700868 device_type_matched = (device_type == real_device_type);
869 } else if (android::base::StartsWith(line, "serialno=")) {
870 std::string serial_no = line.substr(strlen("serialno="));
Tao Baoefc35592017-01-08 22:45:47 -0800871 std::string real_serial_no = android::base::GetProperty("ro.serialno", "");
Yabin Cuifd99a312016-06-09 14:09:39 -0700872 has_serial_number = true;
873 serial_number_matched = (serial_no == real_serial_no);
874 }
875 }
876 return ota_type_matched && device_type_matched && (!has_serial_number || serial_number_matched);
877}
878
Tao Baoaac9d9f2018-04-29 23:38:59 -0700879// Wipes the current A/B device, with a secure wipe of all the partitions in RECOVERY_WIPE.
Yabin Cuifd99a312016-06-09 14:09:39 -0700880static bool wipe_ab_device(size_t wipe_package_size) {
Tao Baoaac9d9f2018-04-29 23:38:59 -0700881 ui->SetBackground(RecoveryUI::ERASING);
882 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Tao Bao862a4c12016-06-02 11:16:50 -0700883
Tao Baoaac9d9f2018-04-29 23:38:59 -0700884 if (!check_wipe_package(wipe_package_size)) {
885 LOG(ERROR) << "Failed to verify wipe package";
886 return false;
887 }
888 static constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe";
889 std::string partition_list;
890 if (!android::base::ReadFileToString(RECOVERY_WIPE, &partition_list)) {
891 LOG(ERROR) << "failed to read \"" << RECOVERY_WIPE << "\"";
892 return false;
893 }
894
895 std::vector<std::string> lines = android::base::Split(partition_list, "\n");
896 for (const std::string& line : lines) {
897 std::string partition = android::base::Trim(line);
898 // Ignore '#' comment or empty lines.
899 if (android::base::StartsWith(partition, "#") || partition.empty()) {
900 continue;
Tao Bao862a4c12016-06-02 11:16:50 -0700901 }
902
Tao Baoaac9d9f2018-04-29 23:38:59 -0700903 // Proceed anyway even if it fails to wipe some partition.
904 secure_wipe_partition(partition);
905 }
906 return true;
Tao Bao862a4c12016-06-02 11:16:50 -0700907}
908
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700909static void choose_recovery_file(Device* device) {
Tao Bao08fc6be2017-03-07 00:56:27 -0800910 std::vector<std::string> entries;
911 if (has_cache) {
912 for (int i = 0; i < KEEP_LOG_COUNT; i++) {
913 auto add_to_entries = [&](const char* filename) {
914 std::string log_file(filename);
915 if (i > 0) {
916 log_file += "." + std::to_string(i);
Tao Baobef39712015-05-04 18:50:27 -0700917 }
Tao Bao08fc6be2017-03-07 00:56:27 -0800918
919 if (ensure_path_mounted(log_file.c_str()) == 0 && access(log_file.c_str(), R_OK) == 0) {
920 entries.push_back(std::move(log_file));
921 }
922 };
923
924 // Add LAST_LOG_FILE + LAST_LOG_FILE.x
925 add_to_entries(LAST_LOG_FILE);
926
927 // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x
928 add_to_entries(LAST_KMSG_FILE);
929 }
930 } else {
931 // If cache partition is not found, view /tmp/recovery.log instead.
Tao Bao641fa972018-04-25 18:59:40 -0700932 if (access(Paths::Get().temporary_log_file().c_str(), R_OK) == -1) {
Tao Bao08fc6be2017-03-07 00:56:27 -0800933 return;
Tianjie Xua54f75e2016-08-17 12:02:46 -0700934 } else {
Tao Bao641fa972018-04-25 18:59:40 -0700935 entries.push_back(Paths::Get().temporary_log_file());
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700936 }
Tao Bao08fc6be2017-03-07 00:56:27 -0800937 }
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700938
Tao Bao08fc6be2017-03-07 00:56:27 -0800939 entries.push_back("Back");
Elliott Hughesc0491632015-05-06 12:40:05 -0700940
Tao Bao08fc6be2017-03-07 00:56:27 -0800941 std::vector<const char*> menu_entries(entries.size());
942 std::transform(entries.cbegin(), entries.cend(), menu_entries.begin(),
943 [](const std::string& entry) { return entry.c_str(); });
944 menu_entries.push_back(nullptr);
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700945
Tao Bao08fc6be2017-03-07 00:56:27 -0800946 const char* headers[] = { "Select file to view", nullptr };
Elliott Hughes8de52072015-04-08 20:06:50 -0700947
Tao Bao08fc6be2017-03-07 00:56:27 -0800948 int chosen_item = 0;
949 while (true) {
Tao Baofc5499f2017-02-23 19:06:53 -0800950 chosen_item = get_menu_selection(headers, menu_entries.data(), true, chosen_item, device);
Tao Bao08fc6be2017-03-07 00:56:27 -0800951 if (entries[chosen_item] == "Back") break;
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700952
Tao Bao08fc6be2017-03-07 00:56:27 -0800953 ui->ShowFile(entries[chosen_item].c_str());
954 }
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700955}
956
Tao Baodb7e8982017-03-06 23:53:16 -0800957static void run_graphics_test() {
958 // Switch to graphics screen.
959 ui->ShowText(false);
Elliott Hughes498cda62016-04-14 16:49:04 -0700960
Tao Baodb7e8982017-03-06 23:53:16 -0800961 ui->SetProgressType(RecoveryUI::INDETERMINATE);
962 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
963 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700964
Tao Baodb7e8982017-03-06 23:53:16 -0800965 ui->SetBackground(RecoveryUI::ERROR);
966 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700967
Tao Baodb7e8982017-03-06 23:53:16 -0800968 ui->SetBackground(RecoveryUI::NO_COMMAND);
969 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700970
Tao Baodb7e8982017-03-06 23:53:16 -0800971 ui->SetBackground(RecoveryUI::ERASING);
972 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700973
Tao Baodb7e8982017-03-06 23:53:16 -0800974 // Calling SetBackground() after SetStage() to trigger a redraw.
975 ui->SetStage(1, 3);
976 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
977 sleep(1);
978 ui->SetStage(2, 3);
979 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
980 sleep(1);
981 ui->SetStage(3, 3);
982 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
983 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700984
Tao Baodb7e8982017-03-06 23:53:16 -0800985 ui->SetStage(-1, -1);
986 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Elliott Hughes498cda62016-04-14 16:49:04 -0700987
Tao Baodb7e8982017-03-06 23:53:16 -0800988 ui->SetProgressType(RecoveryUI::DETERMINATE);
989 ui->ShowProgress(1.0, 10.0);
990 float fraction = 0.0;
991 for (size_t i = 0; i < 100; ++i) {
992 fraction += .01;
993 ui->SetProgress(fraction);
994 usleep(100000);
995 }
996
997 ui->ShowText(true);
Elliott Hughes498cda62016-04-14 16:49:04 -0700998}
999
Tao Baocdcf28f2016-01-13 15:05:20 -08001000// How long (in seconds) we wait for the fuse-provided package file to
1001// appear, before timing out.
1002#define SDCARD_INSTALL_TIMEOUT 10
1003
Tao Bao145d8612015-03-25 15:51:15 -07001004static int apply_from_sdcard(Device* device, bool* wipe_cache) {
Tao Bao682c34b2015-04-07 17:16:35 -07001005 modified_flash = true;
1006
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001007 if (ensure_path_mounted(SDCARD_ROOT) != 0) {
1008 ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT);
1009 return INSTALL_ERROR;
1010 }
1011
Tao Baoc4a18ef2017-02-10 00:13:30 -08001012 std::string path = browse_directory(SDCARD_ROOT, device);
1013 if (path.empty()) {
Elliott Hughes018ed312015-04-08 16:51:36 -07001014 ui->Print("\n-- No package file selected.\n");
caozhiyuanb4effb92015-06-10 16:46:38 +08001015 ensure_path_unmounted(SDCARD_ROOT);
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001016 return INSTALL_ERROR;
1017 }
1018
Tao Baoc4a18ef2017-02-10 00:13:30 -08001019 ui->Print("\n-- Install %s ...\n", path.c_str());
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001020 set_sdcard_update_bootloader_message();
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001021
Tao Baocdcf28f2016-01-13 15:05:20 -08001022 // We used to use fuse in a thread as opposed to a process. Since accessing
1023 // through fuse involves going from kernel to userspace to kernel, it leads
1024 // to deadlock when a page fault occurs. (Bug: 26313124)
1025 pid_t child;
1026 if ((child = fork()) == 0) {
Tao Baoc4a18ef2017-02-10 00:13:30 -08001027 bool status = start_sdcard_fuse(path.c_str());
Tao Baocdcf28f2016-01-13 15:05:20 -08001028
1029 _exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
1030 }
1031
1032 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child
1033 // process is ready.
1034 int result = INSTALL_ERROR;
1035 int status;
1036 bool waited = false;
1037 for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
1038 if (waitpid(child, &status, WNOHANG) == -1) {
1039 result = INSTALL_ERROR;
1040 waited = true;
1041 break;
1042 }
1043
1044 struct stat sb;
1045 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) {
1046 if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
1047 sleep(1);
1048 continue;
1049 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001050 LOG(ERROR) << "Timed out waiting for the fuse-provided package.";
Tao Baocdcf28f2016-01-13 15:05:20 -08001051 result = INSTALL_ERROR;
1052 kill(child, SIGKILL);
1053 break;
1054 }
1055 }
1056
Tao Bao641fa972018-04-25 18:59:40 -07001057 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/);
Tao Baocdcf28f2016-01-13 15:05:20 -08001058 break;
1059 }
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001060
Tao Baocdcf28f2016-01-13 15:05:20 -08001061 if (!waited) {
1062 // Calling stat() on this magic filename signals the fuse
1063 // filesystem to shut down.
1064 struct stat sb;
1065 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
1066
1067 waitpid(child, &status, 0);
1068 }
1069
1070 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001071 LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
Tao Baocdcf28f2016-01-13 15:05:20 -08001072 }
1073
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001074 ensure_path_unmounted(SDCARD_ROOT);
Tao Baocdcf28f2016-01-13 15:05:20 -08001075 return result;
Christian Poetzsch4ec58a42015-02-19 10:42:39 +00001076}
1077
Tao Bao50dd5322017-03-07 14:57:04 -08001078// Returns REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION means to take the default,
1079// which is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery.
1080static Device::BuiltinAction prompt_and_wait(Device* device, int status) {
1081 for (;;) {
1082 finish_recovery();
1083 switch (status) {
1084 case INSTALL_SUCCESS:
1085 case INSTALL_NONE:
1086 ui->SetBackground(RecoveryUI::NO_COMMAND);
1087 break;
Doug Zongker6c8553d2012-09-24 10:40:47 -07001088
Tao Bao50dd5322017-03-07 14:57:04 -08001089 case INSTALL_ERROR:
1090 case INSTALL_CORRUPT:
1091 ui->SetBackground(RecoveryUI::ERROR);
1092 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001093 }
Tao Bao50dd5322017-03-07 14:57:04 -08001094 ui->SetProgressType(RecoveryUI::EMPTY);
1095
1096 int chosen_item = get_menu_selection(nullptr, device->GetMenuItems(), false, 0, device);
1097
1098 // Device-specific code may take some action here. It may return one of the core actions
1099 // handled in the switch statement below.
1100 Device::BuiltinAction chosen_action =
1101 (chosen_item == -1) ? Device::REBOOT : device->InvokeMenuItem(chosen_item);
1102
1103 bool should_wipe_cache = false;
1104 switch (chosen_action) {
1105 case Device::NO_ACTION:
1106 break;
1107
1108 case Device::REBOOT:
1109 case Device::SHUTDOWN:
1110 case Device::REBOOT_BOOTLOADER:
1111 return chosen_action;
1112
1113 case Device::WIPE_DATA:
1114 if (ui->IsTextVisible()) {
1115 if (ask_to_wipe_data(device)) {
1116 wipe_data(device);
1117 }
1118 } else {
1119 wipe_data(device);
1120 return Device::NO_ACTION;
1121 }
1122 break;
1123
1124 case Device::WIPE_CACHE:
1125 wipe_cache(ui->IsTextVisible(), device);
1126 if (!ui->IsTextVisible()) return Device::NO_ACTION;
1127 break;
1128
1129 case Device::APPLY_ADB_SIDELOAD:
1130 case Device::APPLY_SDCARD:
1131 {
1132 bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD);
1133 if (adb) {
Tao Bao641fa972018-04-25 18:59:40 -07001134 status = apply_from_adb(&should_wipe_cache);
Tao Bao50dd5322017-03-07 14:57:04 -08001135 } else {
1136 status = apply_from_sdcard(device, &should_wipe_cache);
1137 }
1138
1139 if (status == INSTALL_SUCCESS && should_wipe_cache) {
1140 if (!wipe_cache(false, device)) {
1141 status = INSTALL_ERROR;
1142 }
1143 }
1144
1145 if (status != INSTALL_SUCCESS) {
1146 ui->SetBackground(RecoveryUI::ERROR);
1147 ui->Print("Installation aborted.\n");
1148 copy_logs();
1149 } else if (!ui->IsTextVisible()) {
1150 return Device::NO_ACTION; // reboot if logs aren't visible
1151 } else {
1152 ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card");
1153 }
1154 }
1155 break;
1156
1157 case Device::VIEW_RECOVERY_LOGS:
1158 choose_recovery_file(device);
1159 break;
1160
1161 case Device::RUN_GRAPHICS_TEST:
1162 run_graphics_test();
1163 break;
1164
Tianjie Xu29d55752017-09-20 17:53:46 -07001165 case Device::RUN_LOCALE_TEST: {
1166 ScreenRecoveryUI* screen_ui = static_cast<ScreenRecoveryUI*>(ui);
1167 screen_ui->CheckBackgroundTextImages(locale);
1168 break;
1169 }
Tao Bao50dd5322017-03-07 14:57:04 -08001170 case Device::MOUNT_SYSTEM:
1171 // For a system image built with the root directory (i.e. system_root_image == "true"), we
1172 // mount it to /system_root, and symlink /system to /system_root/system to make adb shell
1173 // work (the symlink is created through the build system). (Bug: 22855115)
1174 if (android::base::GetBoolProperty("ro.build.system_root_image", false)) {
1175 if (ensure_path_mounted_at("/", "/system_root") != -1) {
1176 ui->Print("Mounted /system.\n");
1177 }
1178 } else {
1179 if (ensure_path_mounted("/system") != -1) {
1180 ui->Print("Mounted /system.\n");
1181 }
1182 }
1183 break;
1184 }
1185 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001186}
1187
Tao Bao99f0d9e2016-10-13 12:46:38 -07001188static void print_property(const char* key, const char* name, void* /* cookie */) {
1189 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001190}
1191
Tao Baoac9d94d2016-11-03 11:37:15 -07001192static std::string load_locale_from_cache() {
1193 if (ensure_path_mounted(LOCALE_FILE) != 0) {
1194 LOG(ERROR) << "Can't mount " << LOCALE_FILE;
1195 return "";
Doug Zongker02ec6b82012-08-22 17:26:40 -07001196 }
Tao Baoac9d94d2016-11-03 11:37:15 -07001197
1198 std::string content;
1199 if (!android::base::ReadFileToString(LOCALE_FILE, &content)) {
1200 PLOG(ERROR) << "Can't read " << LOCALE_FILE;
1201 return "";
1202 }
1203
1204 return android::base::Trim(content);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001205}
1206
Tao Baoac9d94d2016-11-03 11:37:15 -07001207void ui_print(const char* format, ...) {
1208 std::string buffer;
Doug Zongker7c3ae452013-05-14 11:03:02 -07001209 va_list ap;
1210 va_start(ap, format);
Tao Baoac9d94d2016-11-03 11:37:15 -07001211 android::base::StringAppendV(&buffer, format, ap);
Doug Zongker7c3ae452013-05-14 11:03:02 -07001212 va_end(ap);
1213
Tao Baoac9d94d2016-11-03 11:37:15 -07001214 if (ui != nullptr) {
1215 ui->Print("%s", buffer.c_str());
Doug Zongker7c3ae452013-05-14 11:03:02 -07001216 } else {
Tao Baoac9d94d2016-11-03 11:37:15 -07001217 fputs(buffer.c_str(), stdout);
Doug Zongker7c3ae452013-05-14 11:03:02 -07001218 }
1219}
1220
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001221static constexpr char log_characters[] = "VDIWEF";
1222
Tao Bao99f0d9e2016-10-13 12:46:38 -07001223void UiLogger(android::base::LogId /* id */, android::base::LogSeverity severity,
1224 const char* /* tag */, const char* /* file */, unsigned int /* line */,
1225 const char* message) {
1226 if (severity >= android::base::ERROR && ui != nullptr) {
1227 ui->Print("E:%s\n", message);
1228 } else {
1229 fprintf(stdout, "%c:%s\n", log_characters[severity], message);
1230 }
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001231}
1232
Tao Baof2ea6d72018-04-26 10:40:36 -07001233static bool is_battery_ok(int* required_battery_level) {
1234 struct healthd_config healthd_config = {
1235 .batteryStatusPath = android::String8(android::String8::kEmptyString),
1236 .batteryHealthPath = android::String8(android::String8::kEmptyString),
1237 .batteryPresentPath = android::String8(android::String8::kEmptyString),
1238 .batteryCapacityPath = android::String8(android::String8::kEmptyString),
1239 .batteryVoltagePath = android::String8(android::String8::kEmptyString),
1240 .batteryTemperaturePath = android::String8(android::String8::kEmptyString),
1241 .batteryTechnologyPath = android::String8(android::String8::kEmptyString),
1242 .batteryCurrentNowPath = android::String8(android::String8::kEmptyString),
1243 .batteryCurrentAvgPath = android::String8(android::String8::kEmptyString),
1244 .batteryChargeCounterPath = android::String8(android::String8::kEmptyString),
1245 .batteryFullChargePath = android::String8(android::String8::kEmptyString),
1246 .batteryCycleCountPath = android::String8(android::String8::kEmptyString),
1247 .energyCounter = nullptr,
1248 .boot_min_cap = 0,
1249 .screen_on = nullptr
1250 };
1251 healthd_board_init(&healthd_config);
Yabin Cui99281df2016-02-17 12:21:52 -08001252
Tao Baof2ea6d72018-04-26 10:40:36 -07001253 android::BatteryMonitor monitor;
1254 monitor.init(&healthd_config);
Yabin Cui99281df2016-02-17 12:21:52 -08001255
Tao Baof2ea6d72018-04-26 10:40:36 -07001256 static constexpr int BATTERY_READ_TIMEOUT_IN_SEC = 10;
1257 int wait_second = 0;
1258 while (true) {
1259 int charge_status = monitor.getChargeStatus();
1260 // Treat unknown status as charged.
1261 bool charged = (charge_status != android::BATTERY_STATUS_DISCHARGING &&
1262 charge_status != android::BATTERY_STATUS_NOT_CHARGING);
1263 android::BatteryProperty capacity;
1264 android::status_t status = monitor.getProperty(android::BATTERY_PROP_CAPACITY, &capacity);
1265 ui_print("charge_status %d, charged %d, status %d, capacity %" PRId64 "\n", charge_status,
1266 charged, status, capacity.valueInt64);
1267 // At startup, the battery drivers in devices like N5X/N6P take some time to load
1268 // the battery profile. Before the load finishes, it reports value 50 as a fake
1269 // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected
1270 // to finish loading the battery profile earlier than 10 seconds after kernel startup.
1271 if (status == 0 && capacity.valueInt64 == 50) {
1272 if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) {
1273 sleep(1);
1274 wait_second++;
1275 continue;
1276 }
Yabin Cui99281df2016-02-17 12:21:52 -08001277 }
Tao Baof2ea6d72018-04-26 10:40:36 -07001278 // If we can't read battery percentage, it may be a device without battery. In this situation,
1279 // use 100 as a fake battery percentage.
1280 if (status != 0) {
1281 capacity.valueInt64 = 100;
1282 }
1283
1284 // GmsCore enters recovery mode to install package when having enough battery percentage.
1285 // Normally, the threshold is 40% without charger and 20% with charger. So we should check
1286 // battery with a slightly lower limitation.
1287 static constexpr int BATTERY_OK_PERCENTAGE = 20;
1288 static constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15;
1289 *required_battery_level = charged ? BATTERY_WITH_CHARGER_OK_PERCENTAGE : BATTERY_OK_PERCENTAGE;
1290 return capacity.valueInt64 >= *required_battery_level;
1291 }
Yabin Cui99281df2016-02-17 12:21:52 -08001292}
1293
Tianjie Xu99b73be2017-11-28 17:23:06 -08001294// Set the retry count to |retry_count| in BCB.
Tianjie Xu72449c92017-05-16 18:07:31 -07001295static void set_retry_bootloader_message(int retry_count, const std::vector<std::string>& args) {
1296 std::vector<std::string> options;
1297 for (const auto& arg : args) {
1298 if (!android::base::StartsWith(arg, "--retry_count")) {
1299 options.push_back(arg);
Tianjie Xu3c62b672016-02-05 18:25:58 -08001300 }
Tianjie Xu72449c92017-05-16 18:07:31 -07001301 }
Tianjie Xu3c62b672016-02-05 18:25:58 -08001302
Tianjie Xu99b73be2017-11-28 17:23:06 -08001303 // Update the retry counter in BCB.
1304 options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count));
Tianjie Xu72449c92017-05-16 18:07:31 -07001305 std::string err;
1306 if (!update_bootloader_message(options, &err)) {
1307 LOG(ERROR) << err;
1308 }
Tianjie Xu3c62b672016-02-05 18:25:58 -08001309}
1310
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001311static bool bootreason_in_blacklist() {
Tao Baoefc35592017-01-08 22:45:47 -08001312 std::string bootreason = android::base::GetProperty("ro.boot.bootreason", "");
1313 if (!bootreason.empty()) {
Tao Baoaac9d9f2018-04-29 23:38:59 -07001314 // More bootreasons can be found in "system/core/bootstat/bootstat.cpp".
1315 static const std::vector<std::string> kBootreasonBlacklist{
1316 "kernel_panic",
1317 "Panic",
1318 };
1319 for (const auto& str : kBootreasonBlacklist) {
1320 if (android::base::EqualsIgnoreCase(str, bootreason)) return true;
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001321 }
Tao Baoefc35592017-01-08 22:45:47 -08001322 }
1323 return false;
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001324}
1325
Tao Bao641fa972018-04-25 18:59:40 -07001326static void log_failure_code(ErrorCode code, const std::string& update_package) {
1327 std::vector<std::string> log_buffer = {
1328 update_package,
1329 "0", // install result
1330 "error: " + std::to_string(code),
1331 };
1332 std::string log_content = android::base::Join(log_buffer, "\n");
1333 const std::string& install_file = Paths::Get().temporary_install_file();
1334 if (!android::base::WriteStringToFile(log_content, install_file)) {
1335 PLOG(ERROR) << "Failed to write " << install_file;
1336 }
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001337
Tao Bao641fa972018-04-25 18:59:40 -07001338 // Also write the info into last_log.
1339 LOG(INFO) << log_content;
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001340}
1341
Yabin Cui99281df2016-02-17 12:21:52 -08001342int main(int argc, char **argv) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001343 // We don't have logcat yet under recovery; so we'll print error on screen and
1344 // log to stdout (which is redirected to recovery.log) as we used to do.
1345 android::base::InitLogging(argv, &UiLogger);
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -07001346
Tianjie Xu99b73be2017-11-28 17:23:06 -08001347 // Take last pmsg contents and rewrite it to the current pmsg session.
1348 static const char filter[] = "recovery/";
1349 // Do we need to rotate?
1350 bool doRotate = false;
Tianjie Xue113e4d2016-10-21 17:46:13 -07001351
Tianjie Xu99b73be2017-11-28 17:23:06 -08001352 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &doRotate);
1353 // Take action to refresh pmsg contents
1354 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &doRotate);
Mark Salyzyna4f701a2016-03-09 14:58:16 -08001355
Tianjie Xu99b73be2017-11-28 17:23:06 -08001356 // If this binary is started with the single argument "--adbd",
1357 // instead of being the normal recovery binary, it turns into kind
1358 // of a stripped-down version of adbd that only supports the
1359 // 'sideload' command. Note this must be a real argument, not
1360 // anything in the command file or bootloader control block; the
1361 // only way recovery should be run with this argument is when it
1362 // starts a copy of itself from the apply_from_adb() function.
1363 if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {
1364 minadbd_main();
1365 return 0;
1366 }
1367
1368 time_t start = time(nullptr);
1369
1370 // redirect_stdio should be called only in non-sideload mode. Otherwise
1371 // we may have two logger instances with different timestamps.
Tao Bao641fa972018-04-25 18:59:40 -07001372 redirect_stdio(Paths::Get().temporary_log_file().c_str());
Tianjie Xu99b73be2017-11-28 17:23:06 -08001373
1374 printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));
1375
1376 load_volume_table();
1377 has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
1378
1379 std::vector<std::string> args = get_args(argc, argv);
1380 std::vector<char*> args_to_parse(args.size());
1381 std::transform(args.cbegin(), args.cend(), args_to_parse.begin(),
1382 [](const std::string& arg) { return const_cast<char*>(arg.c_str()); });
1383
Tao Baof9f17342018-04-27 10:44:04 -07001384 static constexpr struct option OPTIONS[] = {
1385 { "just_exit", no_argument, nullptr, 'x' },
1386 { "locale", required_argument, nullptr, 0 },
1387 { "prompt_and_wipe_data", no_argument, nullptr, 0 },
1388 { "reason", required_argument, nullptr, 0 },
1389 { "retry_count", required_argument, nullptr, 0 },
1390 { "security", no_argument, nullptr, 0 },
1391 { "show_text", no_argument, nullptr, 't' },
1392 { "shutdown_after", no_argument, nullptr, 0 },
1393 { "sideload", no_argument, nullptr, 0 },
1394 { "sideload_auto_reboot", no_argument, nullptr, 0 },
1395 { "update_package", required_argument, nullptr, 0 },
1396 { "wipe_ab", no_argument, nullptr, 0 },
1397 { "wipe_cache", no_argument, nullptr, 0 },
1398 { "wipe_data", no_argument, nullptr, 0 },
1399 { "wipe_package_size", required_argument, nullptr, 0 },
1400 { nullptr, 0, nullptr, 0 },
1401 };
1402
Tianjie Xu99b73be2017-11-28 17:23:06 -08001403 const char* update_package = nullptr;
1404 bool should_wipe_data = false;
1405 bool should_prompt_and_wipe_data = false;
1406 bool should_wipe_cache = false;
1407 bool should_wipe_ab = false;
1408 size_t wipe_package_size = 0;
1409 bool show_text = false;
1410 bool sideload = false;
1411 bool sideload_auto_reboot = false;
1412 bool just_exit = false;
1413 bool shutdown_after = false;
1414 int retry_count = 0;
1415 bool security_update = false;
1416
1417 int arg;
1418 int option_index;
1419 while ((arg = getopt_long(args_to_parse.size(), args_to_parse.data(), "", OPTIONS,
1420 &option_index)) != -1) {
1421 switch (arg) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001422 case 't':
1423 show_text = true;
1424 break;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001425 case 'x':
1426 just_exit = true;
1427 break;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001428 case 0: {
1429 std::string option = OPTIONS[option_index].name;
Tao Baof9f17342018-04-27 10:44:04 -07001430 if (option == "locale") {
1431 locale = optarg;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001432 } else if (option == "prompt_and_wipe_data") {
1433 should_prompt_and_wipe_data = true;
Tao Baof9f17342018-04-27 10:44:04 -07001434 } else if (option == "reason") {
1435 reason = optarg;
1436 } else if (option == "retry_count") {
1437 android::base::ParseInt(optarg, &retry_count, 0);
1438 } else if (option == "security") {
1439 security_update = true;
1440 } else if (option == "sideload") {
1441 sideload = true;
1442 } else if (option == "sideload_auto_reboot") {
1443 sideload = true;
1444 sideload_auto_reboot = true;
1445 } else if (option == "shutdown_after") {
1446 shutdown_after = true;
1447 } else if (option == "update_package") {
1448 update_package = optarg;
1449 } else if (option == "wipe_ab") {
1450 should_wipe_ab = true;
1451 } else if (option == "wipe_cache") {
1452 should_wipe_cache = true;
1453 } else if (option == "wipe_data") {
1454 should_wipe_data = true;
1455 } else if (option == "wipe_package_size") {
1456 android::base::ParseUint(optarg, &wipe_package_size);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001457 }
1458 break;
1459 }
1460 case '?':
1461 LOG(ERROR) << "Invalid command argument";
1462 continue;
Doug Zongker9270a202012-01-09 15:16:13 -08001463 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001464 }
Doug Zongker9270a202012-01-09 15:16:13 -08001465
Tianjie Xu99b73be2017-11-28 17:23:06 -08001466 if (locale.empty()) {
1467 if (has_cache) {
1468 locale = load_locale_from_cache();
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001469 }
1470
Tao Baoac9d94d2016-11-03 11:37:15 -07001471 if (locale.empty()) {
Tao Baoaac9d9f2018-04-29 23:38:59 -07001472 static constexpr const char* DEFAULT_LOCALE = "en-US";
Tianjie Xu99b73be2017-11-28 17:23:06 -08001473 locale = DEFAULT_LOCALE;
Doug Zongker02ec6b82012-08-22 17:26:40 -07001474 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001475 }
Tao Baoac9d94d2016-11-03 11:37:15 -07001476
Tianjie Xu99b73be2017-11-28 17:23:06 -08001477 printf("locale is [%s]\n", locale.c_str());
1478 printf("stage is [%s]\n", stage.c_str());
1479 printf("reason is [%s]\n", reason);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001480
Tianjie Xu99b73be2017-11-28 17:23:06 -08001481 Device* device = make_device();
1482 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
1483 printf("Quiescent recovery mode.\n");
1484 ui = new StubRecoveryUI();
1485 } else {
1486 ui = device->GetUI();
1487
1488 if (!ui->Init(locale)) {
1489 printf("Failed to initialize UI, use stub UI instead.\n");
1490 ui = new StubRecoveryUI();
1491 }
1492 }
1493
1494 // Set background string to "installing security update" for security update,
1495 // otherwise set it to "installing system update".
1496 ui->SetSystemUpdateText(security_update);
1497
1498 int st_cur, st_max;
1499 if (!stage.empty() && sscanf(stage.c_str(), "%d/%d", &st_cur, &st_max) == 2) {
1500 ui->SetStage(st_cur, st_max);
1501 }
1502
1503 ui->SetBackground(RecoveryUI::NONE);
1504 if (show_text) ui->ShowText(true);
1505
1506 sehandle = selinux_android_file_context_handle();
1507 selinux_android_set_sehandle(sehandle);
1508 if (!sehandle) {
1509 ui->Print("Warning: No file_contexts\n");
1510 }
1511
1512 device->StartRecovery();
1513
1514 printf("Command:");
1515 for (const auto& arg : args) {
1516 printf(" \"%s\"", arg.c_str());
1517 }
1518 printf("\n\n");
1519
1520 property_list(print_property, nullptr);
1521 printf("\n");
1522
1523 ui->Print("Supported API: %d\n", kRecoveryApiVersion);
1524
1525 int status = INSTALL_SUCCESS;
1526
1527 if (update_package != nullptr) {
1528 // It's not entirely true that we will modify the flash. But we want
1529 // to log the update attempt since update_package is non-NULL.
1530 modified_flash = true;
1531
Tao Baof2ea6d72018-04-26 10:40:36 -07001532 int required_battery_level;
1533 if (retry_count == 0 && !is_battery_ok(&required_battery_level)) {
1534 ui->Print("battery capacity is not enough for installing package: %d%% needed\n",
1535 required_battery_level);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001536 // Log the error code to last_install when installation skips due to
1537 // low battery.
1538 log_failure_code(kLowBattery, update_package);
1539 status = INSTALL_SKIPPED;
Tianjie Xua6f49bd2018-03-26 14:32:11 -07001540 } else if (retry_count == 0 && bootreason_in_blacklist()) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001541 // Skip update-on-reboot when bootreason is kernel_panic or similar
1542 ui->Print("bootreason is in the blacklist; skip OTA installation\n");
1543 log_failure_code(kBootreasonInBlacklist, update_package);
1544 status = INSTALL_SKIPPED;
Dmitri Plotnikov8706a982017-04-18 08:28:26 -07001545 } else {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001546 // It's a fresh update. Initialize the retry_count in the BCB to 1; therefore we can later
1547 // identify the interrupted update due to unexpected reboots.
1548 if (retry_count == 0) {
1549 set_retry_bootloader_message(retry_count + 1, args);
Tao Bao7022f332017-07-25 09:52:36 -07001550 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001551
Tao Bao641fa972018-04-25 18:59:40 -07001552 status = install_package(update_package, &should_wipe_cache, true, retry_count);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001553 if (status == INSTALL_SUCCESS && should_wipe_cache) {
1554 wipe_cache(false, device);
1555 }
1556 if (status != INSTALL_SUCCESS) {
1557 ui->Print("Installation aborted.\n");
Tao Baoaac9d9f2018-04-29 23:38:59 -07001558
1559 // When I/O error or bspatch/imgpatch error happens, reboot and retry installation
1560 // RETRY_LIMIT times before we abandon this OTA update.
1561 static constexpr int RETRY_LIMIT = 4;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001562 if (status == INSTALL_RETRY && retry_count < RETRY_LIMIT) {
1563 copy_logs();
1564 retry_count += 1;
1565 set_retry_bootloader_message(retry_count, args);
1566 // Print retry count on screen.
1567 ui->Print("Retry attempt %d\n", retry_count);
1568
1569 // Reboot and retry the update
1570 if (!reboot("reboot,recovery")) {
1571 ui->Print("Reboot failed\n");
1572 } else {
1573 while (true) {
1574 pause();
1575 }
1576 }
Tianjie Xud9d16292017-04-20 18:08:21 -07001577 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001578 // If this is an eng or userdebug build, then automatically
1579 // turn the text display on if the script fails so the error
1580 // message is visible.
1581 if (is_ro_debuggable()) {
1582 ui->ShowText(true);
Tao Baoc679f932015-03-30 09:43:49 -07001583 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001584 }
Doug Zongker8674a722010-09-15 11:08:23 -07001585 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001586 } else if (should_wipe_data) {
1587 if (!wipe_data(device)) {
1588 status = INSTALL_ERROR;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001589 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001590 } else if (should_prompt_and_wipe_data) {
1591 ui->ShowText(true);
1592 ui->SetBackground(RecoveryUI::ERROR);
1593 if (!prompt_and_wipe_data(device)) {
1594 status = INSTALL_ERROR;
Tao Bao75238632015-05-27 14:46:17 -07001595 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001596 ui->ShowText(false);
1597 } else if (should_wipe_cache) {
1598 if (!wipe_cache(false, device)) {
1599 status = INSTALL_ERROR;
1600 }
1601 } else if (should_wipe_ab) {
1602 if (!wipe_ab_device(wipe_package_size)) {
1603 status = INSTALL_ERROR;
1604 }
1605 } else if (sideload) {
1606 // 'adb reboot sideload' acts the same as user presses key combinations
1607 // to enter the sideload mode. When 'sideload-auto-reboot' is used, text
1608 // display will NOT be turned on by default. And it will reboot after
1609 // sideload finishes even if there are errors. Unless one turns on the
1610 // text display during the installation. This is to enable automated
1611 // testing.
1612 if (!sideload_auto_reboot) {
1613 ui->ShowText(true);
1614 }
Tao Bao641fa972018-04-25 18:59:40 -07001615 status = apply_from_adb(&should_wipe_cache);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001616 if (status == INSTALL_SUCCESS && should_wipe_cache) {
1617 if (!wipe_cache(false, device)) {
1618 status = INSTALL_ERROR;
1619 }
1620 }
1621 ui->Print("\nInstall from ADB complete (status: %d).\n", status);
1622 if (sideload_auto_reboot) {
1623 ui->Print("Rebooting automatically.\n");
1624 }
1625 } else if (!just_exit) {
1626 // If this is an eng or userdebug build, automatically turn on the text display if no command
1627 // is specified. Note that this should be called before setting the background to avoid
1628 // flickering the background image.
1629 if (is_ro_debuggable()) {
1630 ui->ShowText(true);
1631 }
1632 status = INSTALL_NONE; // No command specified
1633 ui->SetBackground(RecoveryUI::NO_COMMAND);
1634 }
1635
1636 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
1637 ui->SetBackground(RecoveryUI::ERROR);
1638 if (!ui->IsTextVisible()) {
1639 sleep(5);
1640 }
1641 }
1642
1643 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
1644 // 1. If the recovery menu is visible, prompt and wait for commands.
1645 // 2. If the state is INSTALL_NONE, wait for commands. (i.e. In user build, manually reboot into
1646 // recovery to sideload a package.)
1647 // 3. sideload_auto_reboot is an option only available in user-debug build, reboot the device
1648 // without waiting.
1649 // 4. In all other cases, reboot the device. Therefore, normal users will observe the device
1650 // reboot after it shows the "error" screen for 5s.
1651 if ((status == INSTALL_NONE && !sideload_auto_reboot) || ui->IsTextVisible()) {
1652 Device::BuiltinAction temp = prompt_and_wait(device, status);
1653 if (temp != Device::NO_ACTION) {
1654 after = temp;
1655 }
1656 }
1657
1658 // Save logs and clean up before rebooting or shutting down.
1659 finish_recovery();
1660
1661 switch (after) {
1662 case Device::SHUTDOWN:
1663 ui->Print("Shutting down...\n");
1664 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,");
1665 break;
1666
1667 case Device::REBOOT_BOOTLOADER:
1668 ui->Print("Rebooting to bootloader...\n");
1669 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
1670 break;
1671
1672 default:
1673 ui->Print("Rebooting...\n");
1674 reboot("reboot,");
1675 break;
1676 }
1677 while (true) {
1678 pause();
1679 }
1680 // Should be unreachable.
1681 return EXIT_SUCCESS;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001682}