blob: 0074b64336268c0b3c866719943cfb50a7e335e8 [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
Jerry Zhang6a648042018-05-04 11:24:10 -070017#include "recovery.h"
Tao Bao2ac56af2018-04-25 16:47:04 -070018
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080019#include <ctype.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070020#include <dirent.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080021#include <errno.h>
22#include <fcntl.h>
23#include <getopt.h>
Tao Bao862a4c12016-06-02 11:16:50 -070024#include <inttypes.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080025#include <limits.h>
Tao Bao862a4c12016-06-02 11:16:50 -070026#include <linux/fs.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080027#include <linux/input.h>
Doug Zongker7c3ae452013-05-14 11:03:02 -070028#include <stdarg.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
Doug Zongker23ceeea2010-07-08 17:27:55 -070032#include <sys/stat.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080033#include <sys/types.h>
Tao Baocdcf28f2016-01-13 15:05:20 -080034#include <sys/wait.h>
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080035#include <unistd.h>
36
Tao Baoc4a18ef2017-02-10 00:13:30 -080037#include <algorithm>
Tao Bao3aec6962018-04-20 09:24:58 -070038#include <functional>
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>
Elliott Hughescb220402016-09-23 15:30:55 -070051#include <cutils/properties.h> /* for property_list */
Yifan Hong056538c2018-07-11 17:04:12 -070052#include <healthhalutils/HealthHalUtils.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070053#include <ziparchive/zip_archive.h>
Yabin Cui99281df2016-02-17 12:21:52 -080054
Tao Bao75238632015-05-27 14:46:17 -070055#include "adb_install.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080056#include "common.h"
Tao Bao75238632015-05-27 14:46:17 -070057#include "device.h"
David Andersonedee8362018-05-16 13:43:22 -070058#include "fsck_unshare_blocks.h"
Tao Bao75238632015-05-27 14:46:17 -070059#include "fuse_sdcard_provider.h"
60#include "fuse_sideload.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080061#include "install.h"
Jerry Zhangcadf4ed2018-05-02 16:56:00 -070062#include "logging.h"
Tao Bao17054c02018-05-03 22:41:23 -070063#include "otautil/dirutil.h"
Tao Bao1fc5bf32017-10-06 07:43:41 -070064#include "otautil/error_code.h"
Tao Bao641fa972018-04-25 18:59:40 -070065#include "otautil/paths.h"
Tao Bao2c526392018-05-03 23:01:13 -070066#include "otautil/sysutil.h"
xunchangf07ed2e2019-02-25 14:14:01 -080067#include "package.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080068#include "roots.h"
Doug Zongker211aebc2011-10-28 15:13:10 -070069#include "screen_ui.h"
Tianjie Xue113e4d2016-10-21 17:46:13 -070070#include "ui.h"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080071
Tao Baoaac9d9f2018-04-29 23:38:59 -070072static constexpr const char* CACHE_LOG_DIR = "/cache/recovery";
73static constexpr const char* COMMAND_FILE = "/cache/recovery/command";
Tao Baoaac9d9f2018-04-29 23:38:59 -070074static constexpr const char* LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
75static constexpr const char* LAST_LOG_FILE = "/cache/recovery/last_log";
76static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale";
Tianjie Xu06e57ac2016-07-11 14:04:08 -070077
Tao Baoaac9d9f2018-04-29 23:38:59 -070078static constexpr const char* CACHE_ROOT = "/cache";
79static constexpr const char* DATA_ROOT = "/data";
80static constexpr const char* METADATA_ROOT = "/metadata";
81static constexpr const char* SDCARD_ROOT = "/sdcard";
Nick Kralevicha9ad0322014-10-22 18:38:48 -070082
Tao Baobd0ddcd2017-05-04 13:03:18 -070083// We define RECOVERY_API_VERSION in Android.mk, which will be picked up by build system and packed
84// into target_files.zip. Assert the version defined in code and in Android.mk are consistent.
85static_assert(kRecoveryApiVersion == RECOVERY_API_VERSION, "Mismatching recovery API versions.");
86
Tao Baoac9d94d2016-11-03 11:37:15 -070087bool modified_flash = false;
Tao Baoa8d72bc2016-12-25 18:46:50 -080088std::string stage;
Tao Baoac9d94d2016-11-03 11:37:15 -070089const char* reason = nullptr;
Tao Baoac9d94d2016-11-03 11:37:15 -070090
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080091/*
92 * The recovery tool communicates with the main system through /cache files.
93 * /cache/recovery/command - INPUT - command line for tool, one arg per line
94 * /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080095 *
96 * The arguments which may be supplied in the recovery.command file:
Doug Zongkerd4208f92010-09-20 12:16:13 -070097 * --update_package=path - verify install an OTA package file
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -080098 * --wipe_data - erase user data (and cache), then reboot
Tao Baof9f17342018-04-27 10:44:04 -070099 * --prompt_and_wipe_data - prompt the user that data is corrupt, with their consent erase user
100 * data (and cache), then reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800101 * --wipe_cache - wipe cache (but not user data), then reboot
Tao Baof9f17342018-04-27 10:44:04 -0700102 * --show_text - show the recovery text menu, used by some bootloader (e.g. http://b/36872519).
Oscar Montemayor05231562009-11-30 08:40:57 -0800103 * --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700104 * --just_exit - do nothing; exit and reboot
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800105 *
106 * After completing, we remove /cache/recovery/command and reboot.
107 * Arguments may also be supplied in the bootloader control block (BCB).
108 * These important scenarios must be safely restartable at any point:
109 *
110 * FACTORY RESET
111 * 1. user selects "factory reset"
112 * 2. main system writes "--wipe_data" to /cache/recovery/command
113 * 3. main system reboots into recovery
114 * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
115 * -- after this, rebooting will restart the erase --
Doug Zongkerd4208f92010-09-20 12:16:13 -0700116 * 5. erase_volume() reformats /data
117 * 6. erase_volume() reformats /cache
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800118 * 7. finish_recovery() erases BCB
119 * -- after this, rebooting will restart the main system --
120 * 8. main() calls reboot() to boot main system
121 *
122 * OTA INSTALL
123 * 1. main system downloads OTA package to /cache/some-filename.zip
Doug Zongker9b125b02010-09-22 12:01:37 -0700124 * 2. main system writes "--update_package=/cache/some-filename.zip"
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800125 * 3. main system reboots into recovery
126 * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
127 * -- after this, rebooting will attempt to reinstall the update --
128 * 5. install_package() attempts to install the update
129 * NOTE: the package install must itself be restartable from any point
130 * 6. finish_recovery() erases BCB
131 * -- after this, rebooting will (try to) restart the main system --
132 * 7. ** if install failed **
133 * 7a. prompt_and_wait() shows an error icon and waits for the user
Tao Baoc0336392016-12-13 22:29:49 -0800134 * 7b. the user reboots (pulling the battery, etc) into the main system
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800135 */
136
Elliott Hughesf14af802015-02-10 14:46:14 -0800137bool is_ro_debuggable() {
Elliott Hughescb220402016-09-23 15:30:55 -0700138 return android::base::GetBoolProperty("ro.debuggable", false);
Elliott Hughesf14af802015-02-10 14:46:14 -0800139}
140
Tao Bao2292db82016-12-13 21:53:31 -0800141// Set the BCB to reboot back into recovery (it won't resume the install from
142// sdcard though).
143static void set_sdcard_update_bootloader_message() {
144 std::vector<std::string> options;
145 std::string err;
146 if (!update_bootloader_message(options, &err)) {
147 LOG(ERROR) << "Failed to set BCB message: " << err;
148 }
Doug Zongker34c98df2009-08-18 12:05:45 -0700149}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800150
Tao Baoec579032017-07-21 12:13:15 -0700151// Clear the recovery command and prepare to boot a (hopefully working) system,
Tianjie Xuc14d95d2016-03-24 11:50:34 -0700152// copy our log file to cache as well (for the system to read). This function is
153// idempotent: call it as many times as you like.
Tao Bao551d2c32018-05-09 20:53:13 -0700154static void finish_recovery() {
155 std::string locale = ui->GetLocale();
Tao Baoec579032017-07-21 12:13:15 -0700156 // Save the locale to cache, so if recovery is next started up without a '--locale' argument
157 // (e.g., directly from the bootloader) it will use the last-known locale.
158 if (!locale.empty() && has_cache) {
159 LOG(INFO) << "Saving locale \"" << locale << "\"";
160 if (ensure_path_mounted(LOCALE_FILE) != 0) {
161 LOG(ERROR) << "Failed to mount " << LOCALE_FILE;
162 } else if (!android::base::WriteStringToFile(locale, LOCALE_FILE)) {
163 PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE;
Doug Zongker4f33e552012-08-23 13:16:12 -0700164 }
Tao Baoec579032017-07-21 12:13:15 -0700165 }
Doug Zongker4f33e552012-08-23 13:16:12 -0700166
Jerry Zhangcadf4ed2018-05-02 16:56:00 -0700167 copy_logs(modified_flash, has_cache);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168
Tao Baoec579032017-07-21 12:13:15 -0700169 // Reset to normal system boot so recovery won't cycle indefinitely.
170 std::string err;
171 if (!clear_bootloader_message(&err)) {
172 LOG(ERROR) << "Failed to clear BCB message: " << err;
173 }
174
175 // Remove the command file, so recovery won't repeat indefinitely.
176 if (has_cache) {
177 if (ensure_path_mounted(COMMAND_FILE) != 0 || (unlink(COMMAND_FILE) && errno != ENOENT)) {
178 LOG(WARNING) << "Can't unlink " << COMMAND_FILE;
Yabin Cui8b309f62016-06-24 18:22:02 -0700179 }
Tao Baoec579032017-07-21 12:13:15 -0700180 ensure_path_unmounted(CACHE_ROOT);
181 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800182
Tao Baoec579032017-07-21 12:13:15 -0700183 sync(); // For good measure.
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800184}
185
Tao Bao3f5a3822016-12-13 11:14:37 -0800186struct saved_log_file {
187 std::string name;
188 struct stat sb;
189 std::string data;
190};
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700191
Elliott Hughes945548e2015-06-05 17:59:56 -0700192static bool erase_volume(const char* volume) {
Tao Bao3f5a3822016-12-13 11:14:37 -0800193 bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);
194 bool is_data = (strcmp(volume, DATA_ROOT) == 0);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700195
Tao Bao3f5a3822016-12-13 11:14:37 -0800196 ui->SetBackground(RecoveryUI::ERASING);
197 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700198
Tao Bao3f5a3822016-12-13 11:14:37 -0800199 std::vector<saved_log_file> log_files;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700200
Tao Bao3f5a3822016-12-13 11:14:37 -0800201 if (is_cache) {
202 // If we're reformatting /cache, we load any past logs
203 // (i.e. "/cache/recovery/last_*") and the current log
204 // ("/cache/recovery/log") into memory, so we can restore them after
205 // the reformat.
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700206
Tao Bao3f5a3822016-12-13 11:14:37 -0800207 ensure_path_mounted(volume);
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700208
Tao Bao3f5a3822016-12-13 11:14:37 -0800209 struct dirent* de;
210 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(CACHE_LOG_DIR), closedir);
211 if (d) {
212 while ((de = readdir(d.get())) != nullptr) {
213 if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) {
214 std::string path = android::base::StringPrintf("%s/%s", CACHE_LOG_DIR, de->d_name);
215
216 struct stat sb;
217 if (stat(path.c_str(), &sb) == 0) {
218 // truncate files to 512kb
219 if (sb.st_size > (1 << 19)) {
220 sb.st_size = 1 << 19;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700221 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800222
223 std::string data(sb.st_size, '\0');
Tianjie Xude6735e2017-07-10 15:13:33 -0700224 FILE* f = fopen(path.c_str(), "rbe");
Tao Bao3f5a3822016-12-13 11:14:37 -0800225 fread(&data[0], 1, data.size(), f);
226 fclose(f);
227
228 log_files.emplace_back(saved_log_file{ path, sb, data });
229 }
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700230 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800231 }
Paul Lawrenced0db3372015-11-05 13:38:40 -0800232 } else {
Tao Bao3f5a3822016-12-13 11:14:37 -0800233 if (errno != ENOENT) {
234 PLOG(ERROR) << "Failed to opendir " << CACHE_LOG_DIR;
235 }
Paul Lawrenced0db3372015-11-05 13:38:40 -0800236 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800237 }
Doug Zongkerd0181b82011-10-19 10:51:12 -0700238
Tao Bao3f5a3822016-12-13 11:14:37 -0800239 ui->Print("Formatting %s...\n", volume);
240
241 ensure_path_unmounted(volume);
242
243 int result;
Tao Bao3f5a3822016-12-13 11:14:37 -0800244 if (is_data && reason && strcmp(reason, "convert_fbe") == 0) {
Tao Bao406a6ff2018-04-30 10:05:57 -0700245 static constexpr const char* CONVERT_FBE_DIR = "/tmp/convert_fbe";
246 static constexpr const char* CONVERT_FBE_FILE = "/tmp/convert_fbe/convert_fbe";
247 // Create convert_fbe breadcrumb file to signal init to convert to file based encryption, not
248 // full disk encryption.
Tao Bao3f5a3822016-12-13 11:14:37 -0800249 if (mkdir(CONVERT_FBE_DIR, 0700) != 0) {
Tao Bao406a6ff2018-04-30 10:05:57 -0700250 PLOG(ERROR) << "Failed to mkdir " << CONVERT_FBE_DIR;
251 return false;
Tao Bao3f5a3822016-12-13 11:14:37 -0800252 }
Tianjie Xude6735e2017-07-10 15:13:33 -0700253 FILE* f = fopen(CONVERT_FBE_FILE, "wbe");
Tao Bao3f5a3822016-12-13 11:14:37 -0800254 if (!f) {
Tao Bao406a6ff2018-04-30 10:05:57 -0700255 PLOG(ERROR) << "Failed to convert to file encryption";
256 return false;
Tao Bao3f5a3822016-12-13 11:14:37 -0800257 }
258 fclose(f);
259 result = format_volume(volume, CONVERT_FBE_DIR);
260 remove(CONVERT_FBE_FILE);
261 rmdir(CONVERT_FBE_DIR);
262 } else {
263 result = format_volume(volume);
264 }
265
266 if (is_cache) {
267 // Re-create the log dir and write back the log entries.
268 if (ensure_path_mounted(CACHE_LOG_DIR) == 0 &&
Tao Baoac3d1ed2017-07-23 00:01:02 -0700269 mkdir_recursively(CACHE_LOG_DIR, 0777, false, sehandle) == 0) {
Tao Bao3f5a3822016-12-13 11:14:37 -0800270 for (const auto& log : log_files) {
271 if (!android::base::WriteStringToFile(log.data, log.name, log.sb.st_mode, log.sb.st_uid,
272 log.sb.st_gid)) {
273 PLOG(ERROR) << "Failed to write to " << log.name;
Doug Zongker6d0d7ac2013-07-09 13:34:55 -0700274 }
Tao Bao3f5a3822016-12-13 11:14:37 -0800275 }
276 } else {
277 PLOG(ERROR) << "Failed to mount / create " << CACHE_LOG_DIR;
Doug Zongker2c3539e2010-09-29 13:21:30 -0700278 }
279
Tao Bao3f5a3822016-12-13 11:14:37 -0800280 // Any part of the log we'd copied to cache is now gone.
281 // Reset the pointer so we copy from the beginning of the temp
282 // log.
Jerry Zhangcadf4ed2018-05-02 16:56:00 -0700283 reset_tmplog_offset();
284 copy_logs(modified_flash, has_cache);
Tao Bao3f5a3822016-12-13 11:14:37 -0800285 }
286
287 return (result == 0);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800288}
289
Hridya Valsarajue4ef4532018-08-31 11:57:51 -0700290// Sets the usb config to 'state'
291bool SetUsbConfig(const std::string& state) {
292 android::base::SetProperty("sys.usb.config", state);
293 return android::base::WaitForProperty("sys.usb.state", state);
294}
295
Tao Baoc4a18ef2017-02-10 00:13:30 -0800296// Returns the selected filename, or an empty string.
297static std::string browse_directory(const std::string& path, Device* device) {
Yifan Hongd81b8e32018-12-17 14:29:06 -0800298 ensure_path_mounted(path);
Doug Zongker8674a722010-09-15 11:08:23 -0700299
Tao Baoc4a18ef2017-02-10 00:13:30 -0800300 std::unique_ptr<DIR, decltype(&closedir)> d(opendir(path.c_str()), closedir);
301 if (!d) {
302 PLOG(ERROR) << "error opening " << path;
303 return "";
304 }
Doug Zongkerc18eeb82010-09-21 16:49:26 -0700305
Tao Baoc4a18ef2017-02-10 00:13:30 -0800306 std::vector<std::string> dirs;
Tao Bao1fe1afe2018-05-01 15:56:05 -0700307 std::vector<std::string> entries{ "../" }; // "../" is always the first entry.
Tao Baoc4a18ef2017-02-10 00:13:30 -0800308
309 dirent* de;
310 while ((de = readdir(d.get())) != nullptr) {
311 std::string name(de->d_name);
312
313 if (de->d_type == DT_DIR) {
314 // Skip "." and ".." entries.
315 if (name == "." || name == "..") continue;
316 dirs.push_back(name + "/");
317 } else if (de->d_type == DT_REG && android::base::EndsWithIgnoreCase(name, ".zip")) {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700318 entries.push_back(name);
Tao Baoc4a18ef2017-02-10 00:13:30 -0800319 }
320 }
321
322 std::sort(dirs.begin(), dirs.end());
Tao Bao1fe1afe2018-05-01 15:56:05 -0700323 std::sort(entries.begin(), entries.end());
Tao Baoc4a18ef2017-02-10 00:13:30 -0800324
Tao Bao1fe1afe2018-05-01 15:56:05 -0700325 // Append dirs to the entries list.
326 entries.insert(entries.end(), dirs.begin(), dirs.end());
Tao Baoc4a18ef2017-02-10 00:13:30 -0800327
Tao Bao1fe1afe2018-05-01 15:56:05 -0700328 std::vector<std::string> headers{ "Choose a package to install:", path };
Tao Baoc4a18ef2017-02-10 00:13:30 -0800329
Tao Bao1fe1afe2018-05-01 15:56:05 -0700330 size_t chosen_item = 0;
Tao Baoc4a18ef2017-02-10 00:13:30 -0800331 while (true) {
Tao Bao3aec6962018-04-20 09:24:58 -0700332 chosen_item = ui->ShowMenu(
333 headers, entries, chosen_item, true,
334 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
Tao Baoc4a18ef2017-02-10 00:13:30 -0800335
Jerry Zhangb76af932018-05-22 12:08:35 -0700336 // Return if WaitKey() was interrupted.
337 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
338 return "";
339 }
340
Tao Bao1fe1afe2018-05-01 15:56:05 -0700341 const std::string& item = entries[chosen_item];
Tao Baoc4a18ef2017-02-10 00:13:30 -0800342 if (chosen_item == 0) {
343 // Go up but continue browsing (if the caller is browse_directory).
344 return "";
Doug Zongker8674a722010-09-15 11:08:23 -0700345 }
346
Tao Baoc4a18ef2017-02-10 00:13:30 -0800347 std::string new_path = path + "/" + item;
348 if (new_path.back() == '/') {
349 // Recurse down into a subdirectory.
350 new_path.pop_back();
351 std::string result = browse_directory(new_path, device);
352 if (!result.empty()) return result;
353 } else {
354 // Selected a zip file: return the path to the caller.
355 return new_path;
Doug Zongker8674a722010-09-15 11:08:23 -0700356 }
Tao Baoc4a18ef2017-02-10 00:13:30 -0800357 }
Doug Zongker8674a722010-09-15 11:08:23 -0700358
Tao Baoc4a18ef2017-02-10 00:13:30 -0800359 // Unreachable.
Doug Zongker8674a722010-09-15 11:08:23 -0700360}
361
Elliott Hughes30694c92015-03-25 15:16:51 -0700362static bool yes_no(Device* device, const char* question1, const char* question2) {
Tao Bao1fe1afe2018-05-01 15:56:05 -0700363 std::vector<std::string> headers{ question1, question2 };
364 std::vector<std::string> items{ " No", " Yes" };
Doug Zongkerddd6a282009-06-09 12:22:33 -0700365
Tao Bao1fe1afe2018-05-01 15:56:05 -0700366 size_t chosen_item = ui->ShowMenu(
Tao Bao3aec6962018-04-20 09:24:58 -0700367 headers, items, 0, true,
368 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
369 return (chosen_item == 1);
Elliott Hughes30694c92015-03-25 15:16:51 -0700370}
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800371
Paul Crowley08404b42016-12-19 13:04:23 -0800372static bool ask_to_wipe_data(Device* device) {
Tianjie Xu1a0a30a2018-10-25 15:22:07 -0700373 std::vector<std::string> headers{ "Wipe all user data?", " THIS CAN NOT BE UNDONE!" };
374 std::vector<std::string> items{ " Cancel", " Factory data reset" };
375
376 size_t chosen_item = ui->ShowPromptWipeDataConfirmationMenu(
377 headers, items,
378 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
379
380 return (chosen_item == 1);
Paul Crowley08404b42016-12-19 13:04:23 -0800381}
Doug Zongker1066d2c2009-04-01 13:57:40 -0700382
Paul Crowley08404b42016-12-19 13:04:23 -0800383// Return true on success.
384static bool wipe_data(Device* device) {
Tao Bao682c34b2015-04-07 17:16:35 -0700385 modified_flash = true;
386
Doug Zongker211aebc2011-10-28 15:13:10 -0700387 ui->Print("\n-- Wiping data...\n");
Paul Crowley3b4d5162016-06-08 13:51:41 -0700388 bool success = device->PreWipeData();
389 if (success) {
390 success &= erase_volume(DATA_ROOT);
391 if (has_cache) {
392 success &= erase_volume(CACHE_ROOT);
393 }
394 if (volume_for_mount_point(METADATA_ROOT) != nullptr) {
395 success &= erase_volume(METADATA_ROOT);
396 }
397 }
398 if (success) {
399 success &= device->PostWipeData();
400 }
Elliott Hughes945548e2015-06-05 17:59:56 -0700401 ui->Print("Data wipe %s.\n", success ? "complete" : "failed");
402 return success;
Doug Zongkerf93d8162009-09-22 15:16:02 -0700403}
404
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -0700405static InstallResult prompt_and_wipe_data(Device* device) {
Tao Bao2bbc6d62017-08-13 23:48:55 -0700406 // Use a single string and let ScreenRecoveryUI handles the wrapping.
Tianjie Xub99e6062018-10-16 15:13:09 -0700407 std::vector<std::string> wipe_data_menu_headers{
Tao Bao2bbc6d62017-08-13 23:48:55 -0700408 "Can't load Android system. Your data may be corrupt. "
409 "If you continue to get this message, you may need to "
410 "perform a factory data reset and erase all user data "
Paul Crowley31ac0c62017-03-23 12:32:40 -0700411 "stored on this device.",
Tao Baofc5499f2017-02-23 19:06:53 -0800412 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700413 // clang-format off
Tianjie Xub99e6062018-10-16 15:13:09 -0700414 std::vector<std::string> wipe_data_menu_items {
Paul Crowley31ac0c62017-03-23 12:32:40 -0700415 "Try again",
416 "Factory data reset",
Tao Baofc5499f2017-02-23 19:06:53 -0800417 };
Tao Bao1fe1afe2018-05-01 15:56:05 -0700418 // clang-format on
Tao Baofc5499f2017-02-23 19:06:53 -0800419 for (;;) {
Tianjie Xub99e6062018-10-16 15:13:09 -0700420 size_t chosen_item = ui->ShowPromptWipeDataMenu(
421 wipe_data_menu_headers, wipe_data_menu_items,
Tao Bao3aec6962018-04-20 09:24:58 -0700422 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
Jerry Zhangb76af932018-05-22 12:08:35 -0700423 // If ShowMenu() returned RecoveryUI::KeyError::INTERRUPTED, WaitKey() was interrupted.
424 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -0700425 return INSTALL_KEY_INTERRUPTED;
Jerry Zhangb76af932018-05-22 12:08:35 -0700426 }
Tao Baofc5499f2017-02-23 19:06:53 -0800427 if (chosen_item != 1) {
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -0700428 return INSTALL_SUCCESS; // Just reboot, no wipe; not a failure, user asked for it
Paul Crowley08404b42016-12-19 13:04:23 -0800429 }
Tianjie Xub99e6062018-10-16 15:13:09 -0700430
Tao Baofc5499f2017-02-23 19:06:53 -0800431 if (ask_to_wipe_data(device)) {
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -0700432 if (wipe_data(device)) {
433 return INSTALL_SUCCESS;
434 } else {
435 return INSTALL_ERROR;
436 }
Tao Baofc5499f2017-02-23 19:06:53 -0800437 }
438 }
Paul Crowley08404b42016-12-19 13:04:23 -0800439}
440
Tao Baoe39a9bc2015-03-31 12:19:05 -0700441// Return true on success.
442static bool wipe_cache(bool should_confirm, Device* device) {
Tao Bao26112e52016-02-25 12:29:40 -0800443 if (!has_cache) {
444 ui->Print("No /cache partition found.\n");
445 return false;
446 }
447
Elliott Hughes30694c92015-03-25 15:16:51 -0700448 if (should_confirm && !yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!")) {
Tao Baoe39a9bc2015-03-31 12:19:05 -0700449 return false;
Elliott Hughes30694c92015-03-25 15:16:51 -0700450 }
451
Tao Bao682c34b2015-04-07 17:16:35 -0700452 modified_flash = true;
453
Elliott Hughes30694c92015-03-25 15:16:51 -0700454 ui->Print("\n-- Wiping cache...\n");
Elliott Hughes945548e2015-06-05 17:59:56 -0700455 bool success = erase_volume("/cache");
456 ui->Print("Cache wipe %s.\n", success ? "complete" : "failed");
457 return success;
Elliott Hughes30694c92015-03-25 15:16:51 -0700458}
459
Tao Bao1b2a98b2017-03-24 10:45:34 -0700460// Secure-wipe a given partition. It uses BLKSECDISCARD, if supported. Otherwise, it goes with
461// BLKDISCARD (if device supports BLKDISCARDZEROES) or BLKZEROOUT.
Tao Bao862a4c12016-06-02 11:16:50 -0700462static bool secure_wipe_partition(const std::string& partition) {
Tao Bao1b2a98b2017-03-24 10:45:34 -0700463 android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(partition.c_str(), O_WRONLY)));
464 if (fd == -1) {
465 PLOG(ERROR) << "Failed to open \"" << partition << "\"";
466 return false;
467 }
468
469 uint64_t range[2] = { 0, 0 };
470 if (ioctl(fd, BLKGETSIZE64, &range[1]) == -1 || range[1] == 0) {
471 PLOG(ERROR) << "Failed to get partition size";
472 return false;
473 }
474 LOG(INFO) << "Secure-wiping \"" << partition << "\" from " << range[0] << " to " << range[1];
475
476 LOG(INFO) << " Trying BLKSECDISCARD...";
477 if (ioctl(fd, BLKSECDISCARD, &range) == -1) {
478 PLOG(WARNING) << " Failed";
479
480 // Use BLKDISCARD if it zeroes out blocks, otherwise use BLKZEROOUT.
481 unsigned int zeroes;
482 if (ioctl(fd, BLKDISCARDZEROES, &zeroes) == 0 && zeroes != 0) {
483 LOG(INFO) << " Trying BLKDISCARD...";
484 if (ioctl(fd, BLKDISCARD, &range) == -1) {
485 PLOG(ERROR) << " Failed";
Tao Bao862a4c12016-06-02 11:16:50 -0700486 return false;
Tao Bao1b2a98b2017-03-24 10:45:34 -0700487 }
488 } else {
489 LOG(INFO) << " Trying BLKZEROOUT...";
490 if (ioctl(fd, BLKZEROOUT, &range) == -1) {
491 PLOG(ERROR) << " Failed";
Tao Bao862a4c12016-06-02 11:16:50 -0700492 return false;
Tao Bao1b2a98b2017-03-24 10:45:34 -0700493 }
Tao Bao862a4c12016-06-02 11:16:50 -0700494 }
Tao Bao1b2a98b2017-03-24 10:45:34 -0700495 }
Tao Bao862a4c12016-06-02 11:16:50 -0700496
Tao Bao1b2a98b2017-03-24 10:45:34 -0700497 LOG(INFO) << " Done";
498 return true;
Tao Bao862a4c12016-06-02 11:16:50 -0700499}
500
xunchang55e3d222019-03-11 11:28:41 -0700501static std::unique_ptr<Package> ReadWipePackage(size_t wipe_package_size) {
xunchange0d991c2019-03-05 14:50:51 -0800502 if (wipe_package_size == 0) {
503 LOG(ERROR) << "wipe_package_size is zero";
xunchang55e3d222019-03-11 11:28:41 -0700504 return nullptr;
xunchange0d991c2019-03-05 14:50:51 -0800505 }
506
507 std::string wipe_package;
508 std::string err_str;
509 if (!read_wipe_package(&wipe_package, wipe_package_size, &err_str)) {
510 PLOG(ERROR) << "Failed to read wipe package" << err_str;
xunchang55e3d222019-03-11 11:28:41 -0700511 return nullptr;
xunchange0d991c2019-03-05 14:50:51 -0800512 }
xunchang55e3d222019-03-11 11:28:41 -0700513
514 return Package::CreateMemoryPackage(
515 std::vector<uint8_t>(wipe_package.begin(), wipe_package.end()), nullptr);
xunchange0d991c2019-03-05 14:50:51 -0800516}
517
518// Checks if the wipe package matches expectation. If the check passes, reads the list of
519// partitions to wipe from the package. Checks include
Yabin Cuifd99a312016-06-09 14:09:39 -0700520// 1. verify the package.
521// 2. check metadata (ota-type, pre-device and serial number if having one).
xunchang55e3d222019-03-11 11:28:41 -0700522static bool CheckWipePackage(Package* wipe_package) {
523 if (!verify_package(wipe_package)) {
xunchange0d991c2019-03-05 14:50:51 -0800524 LOG(ERROR) << "Failed to verify package";
525 return false;
526 }
Yabin Cuifd99a312016-06-09 14:09:39 -0700527
xunchang55e3d222019-03-11 11:28:41 -0700528 ZipArchiveHandle zip = wipe_package->GetZipArchiveHandle();
529 if (!zip) {
530 LOG(ERROR) << "Failed to get ZipArchiveHandle";
xunchange0d991c2019-03-05 14:50:51 -0800531 return false;
532 }
533
534 std::map<std::string, std::string> metadata;
535 if (!ReadMetadataFromPackage(zip, &metadata)) {
536 LOG(ERROR) << "Failed to parse metadata in the zip file";
537 return false;
538 }
539
xunchang55e3d222019-03-11 11:28:41 -0700540 return CheckPackageMetadata(metadata, OtaType::BRICK) == 0;
xunchange0d991c2019-03-05 14:50:51 -0800541}
542
xunchang55e3d222019-03-11 11:28:41 -0700543std::vector<std::string> GetWipePartitionList(Package* wipe_package) {
544 ZipArchiveHandle zip = wipe_package->GetZipArchiveHandle();
545 if (!zip) {
546 LOG(ERROR) << "Failed to get ZipArchiveHandle";
xunchange0d991c2019-03-05 14:50:51 -0800547 return {};
548 }
549
550 static constexpr const char* RECOVERY_WIPE_ENTRY_NAME = "recovery.wipe";
551
552 std::string partition_list_content;
553 ZipString path(RECOVERY_WIPE_ENTRY_NAME);
554 ZipEntry entry;
555 if (FindEntry(zip, path, &entry) == 0) {
556 uint32_t length = entry.uncompressed_length;
557 partition_list_content = std::string(length, '\0');
558 if (auto err = ExtractToMemory(
559 zip, &entry, reinterpret_cast<uint8_t*>(partition_list_content.data()), length);
560 err != 0) {
561 LOG(ERROR) << "Failed to extract " << RECOVERY_WIPE_ENTRY_NAME << ": "
562 << ErrorCodeString(err);
xunchange0d991c2019-03-05 14:50:51 -0800563 return {};
Yabin Cuifd99a312016-06-09 14:09:39 -0700564 }
xunchange0d991c2019-03-05 14:50:51 -0800565 } else {
566 LOG(INFO) << "Failed to find " << RECOVERY_WIPE_ENTRY_NAME
567 << ", falling back to use the partition list on device.";
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700568
xunchange0d991c2019-03-05 14:50:51 -0800569 static constexpr const char* RECOVERY_WIPE_ON_DEVICE = "/etc/recovery.wipe";
570 if (!android::base::ReadFileToString(RECOVERY_WIPE_ON_DEVICE, &partition_list_content)) {
571 PLOG(ERROR) << "failed to read \"" << RECOVERY_WIPE_ON_DEVICE << "\"";
xunchange0d991c2019-03-05 14:50:51 -0800572 return {};
Yabin Cuifd99a312016-06-09 14:09:39 -0700573 }
xunchange0d991c2019-03-05 14:50:51 -0800574 }
Tianjie Xu93b5bf22018-10-25 10:39:01 -0700575
xunchange0d991c2019-03-05 14:50:51 -0800576 std::vector<std::string> result;
577 std::vector<std::string> lines = android::base::Split(partition_list_content, "\n");
578 for (const std::string& line : lines) {
579 std::string partition = android::base::Trim(line);
580 // Ignore '#' comment or empty lines.
581 if (android::base::StartsWith(partition, "#") || partition.empty()) {
582 continue;
583 }
584 result.push_back(line);
585 }
Yabin Cuifd99a312016-06-09 14:09:39 -0700586
xunchange0d991c2019-03-05 14:50:51 -0800587 return result;
Yabin Cuifd99a312016-06-09 14:09:39 -0700588}
589
Tao Baoaac9d9f2018-04-29 23:38:59 -0700590// Wipes the current A/B device, with a secure wipe of all the partitions in RECOVERY_WIPE.
Yabin Cuifd99a312016-06-09 14:09:39 -0700591static bool wipe_ab_device(size_t wipe_package_size) {
Tao Baoaac9d9f2018-04-29 23:38:59 -0700592 ui->SetBackground(RecoveryUI::ERASING);
593 ui->SetProgressType(RecoveryUI::INDETERMINATE);
Tao Bao862a4c12016-06-02 11:16:50 -0700594
xunchang55e3d222019-03-11 11:28:41 -0700595 auto wipe_package = ReadWipePackage(wipe_package_size);
596 if (!wipe_package) {
597 LOG(ERROR) << "Failed to open wipe package";
xunchange0d991c2019-03-05 14:50:51 -0800598 return false;
599 }
600
xunchang55e3d222019-03-11 11:28:41 -0700601 if (!CheckWipePackage(wipe_package.get())) {
Tao Baoaac9d9f2018-04-29 23:38:59 -0700602 LOG(ERROR) << "Failed to verify wipe package";
603 return false;
604 }
xunchange0d991c2019-03-05 14:50:51 -0800605
xunchang55e3d222019-03-11 11:28:41 -0700606 auto partition_list = GetWipePartitionList(wipe_package.get());
xunchange0d991c2019-03-05 14:50:51 -0800607 if (partition_list.empty()) {
608 LOG(ERROR) << "Empty wipe ab partition list";
Tao Baoaac9d9f2018-04-29 23:38:59 -0700609 return false;
610 }
611
xunchange0d991c2019-03-05 14:50:51 -0800612 for (const auto& partition : partition_list) {
Tao Baoaac9d9f2018-04-29 23:38:59 -0700613 // Proceed anyway even if it fails to wipe some partition.
614 secure_wipe_partition(partition);
615 }
616 return true;
Tao Bao862a4c12016-06-02 11:16:50 -0700617}
618
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700619static void choose_recovery_file(Device* device) {
Tao Bao08fc6be2017-03-07 00:56:27 -0800620 std::vector<std::string> entries;
621 if (has_cache) {
622 for (int i = 0; i < KEEP_LOG_COUNT; i++) {
623 auto add_to_entries = [&](const char* filename) {
624 std::string log_file(filename);
625 if (i > 0) {
626 log_file += "." + std::to_string(i);
Tao Baobef39712015-05-04 18:50:27 -0700627 }
Tao Bao08fc6be2017-03-07 00:56:27 -0800628
Yifan Hongd81b8e32018-12-17 14:29:06 -0800629 if (ensure_path_mounted(log_file) == 0 && access(log_file.c_str(), R_OK) == 0) {
Tao Bao08fc6be2017-03-07 00:56:27 -0800630 entries.push_back(std::move(log_file));
631 }
632 };
633
634 // Add LAST_LOG_FILE + LAST_LOG_FILE.x
635 add_to_entries(LAST_LOG_FILE);
636
637 // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x
638 add_to_entries(LAST_KMSG_FILE);
639 }
640 } else {
641 // If cache partition is not found, view /tmp/recovery.log instead.
Tao Bao641fa972018-04-25 18:59:40 -0700642 if (access(Paths::Get().temporary_log_file().c_str(), R_OK) == -1) {
Tao Bao08fc6be2017-03-07 00:56:27 -0800643 return;
Tianjie Xua54f75e2016-08-17 12:02:46 -0700644 } else {
Tao Bao641fa972018-04-25 18:59:40 -0700645 entries.push_back(Paths::Get().temporary_log_file());
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700646 }
Tao Bao08fc6be2017-03-07 00:56:27 -0800647 }
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700648
Tao Bao08fc6be2017-03-07 00:56:27 -0800649 entries.push_back("Back");
Elliott Hughesc0491632015-05-06 12:40:05 -0700650
Tao Bao1fe1afe2018-05-01 15:56:05 -0700651 std::vector<std::string> headers{ "Select file to view" };
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700652
Tao Bao1fe1afe2018-05-01 15:56:05 -0700653 size_t chosen_item = 0;
Tao Bao08fc6be2017-03-07 00:56:27 -0800654 while (true) {
Tao Bao3aec6962018-04-20 09:24:58 -0700655 chosen_item = ui->ShowMenu(
Tao Bao1fe1afe2018-05-01 15:56:05 -0700656 headers, entries, chosen_item, true,
Tao Bao3aec6962018-04-20 09:24:58 -0700657 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
Jerry Zhangb76af932018-05-22 12:08:35 -0700658
659 // Handle WaitKey() interrupt.
660 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
661 break;
662 }
Tao Bao08fc6be2017-03-07 00:56:27 -0800663 if (entries[chosen_item] == "Back") break;
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700664
Tao Bao1d156b92018-05-02 12:43:18 -0700665 ui->ShowFile(entries[chosen_item]);
Tao Bao08fc6be2017-03-07 00:56:27 -0800666 }
Nick Kralevicha9ad0322014-10-22 18:38:48 -0700667}
668
Tao Baodb7e8982017-03-06 23:53:16 -0800669static void run_graphics_test() {
670 // Switch to graphics screen.
671 ui->ShowText(false);
Elliott Hughes498cda62016-04-14 16:49:04 -0700672
Tao Baodb7e8982017-03-06 23:53:16 -0800673 ui->SetProgressType(RecoveryUI::INDETERMINATE);
674 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
675 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700676
Tao Baodb7e8982017-03-06 23:53:16 -0800677 ui->SetBackground(RecoveryUI::ERROR);
678 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700679
Tao Baodb7e8982017-03-06 23:53:16 -0800680 ui->SetBackground(RecoveryUI::NO_COMMAND);
681 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700682
Tao Baodb7e8982017-03-06 23:53:16 -0800683 ui->SetBackground(RecoveryUI::ERASING);
684 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700685
Tao Baodb7e8982017-03-06 23:53:16 -0800686 // Calling SetBackground() after SetStage() to trigger a redraw.
687 ui->SetStage(1, 3);
688 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
689 sleep(1);
690 ui->SetStage(2, 3);
691 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
692 sleep(1);
693 ui->SetStage(3, 3);
694 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
695 sleep(1);
Elliott Hughes498cda62016-04-14 16:49:04 -0700696
Tao Baodb7e8982017-03-06 23:53:16 -0800697 ui->SetStage(-1, -1);
698 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
Elliott Hughes498cda62016-04-14 16:49:04 -0700699
Tao Baodb7e8982017-03-06 23:53:16 -0800700 ui->SetProgressType(RecoveryUI::DETERMINATE);
701 ui->ShowProgress(1.0, 10.0);
702 float fraction = 0.0;
703 for (size_t i = 0; i < 100; ++i) {
704 fraction += .01;
705 ui->SetProgress(fraction);
706 usleep(100000);
707 }
708
709 ui->ShowText(true);
Elliott Hughes498cda62016-04-14 16:49:04 -0700710}
711
Tao Baocdcf28f2016-01-13 15:05:20 -0800712// How long (in seconds) we wait for the fuse-provided package file to
713// appear, before timing out.
714#define SDCARD_INSTALL_TIMEOUT 10
715
Tao Bao145d8612015-03-25 15:51:15 -0700716static int apply_from_sdcard(Device* device, bool* wipe_cache) {
Tao Bao682c34b2015-04-07 17:16:35 -0700717 modified_flash = true;
718
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000719 if (ensure_path_mounted(SDCARD_ROOT) != 0) {
720 ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT);
721 return INSTALL_ERROR;
722 }
723
Tao Baoc4a18ef2017-02-10 00:13:30 -0800724 std::string path = browse_directory(SDCARD_ROOT, device);
725 if (path.empty()) {
Elliott Hughes018ed312015-04-08 16:51:36 -0700726 ui->Print("\n-- No package file selected.\n");
caozhiyuanb4effb92015-06-10 16:46:38 +0800727 ensure_path_unmounted(SDCARD_ROOT);
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000728 return INSTALL_ERROR;
729 }
730
Tao Baoc4a18ef2017-02-10 00:13:30 -0800731 ui->Print("\n-- Install %s ...\n", path.c_str());
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000732 set_sdcard_update_bootloader_message();
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000733
Tao Baocdcf28f2016-01-13 15:05:20 -0800734 // We used to use fuse in a thread as opposed to a process. Since accessing
735 // through fuse involves going from kernel to userspace to kernel, it leads
736 // to deadlock when a page fault occurs. (Bug: 26313124)
737 pid_t child;
738 if ((child = fork()) == 0) {
Tao Baoc4a18ef2017-02-10 00:13:30 -0800739 bool status = start_sdcard_fuse(path.c_str());
Tao Baocdcf28f2016-01-13 15:05:20 -0800740
741 _exit(status ? EXIT_SUCCESS : EXIT_FAILURE);
742 }
743
744 // FUSE_SIDELOAD_HOST_PATHNAME will start to exist once the fuse in child
745 // process is ready.
746 int result = INSTALL_ERROR;
747 int status;
748 bool waited = false;
749 for (int i = 0; i < SDCARD_INSTALL_TIMEOUT; ++i) {
750 if (waitpid(child, &status, WNOHANG) == -1) {
751 result = INSTALL_ERROR;
752 waited = true;
753 break;
754 }
755
756 struct stat sb;
757 if (stat(FUSE_SIDELOAD_HOST_PATHNAME, &sb) == -1) {
758 if (errno == ENOENT && i < SDCARD_INSTALL_TIMEOUT-1) {
759 sleep(1);
760 continue;
761 } else {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700762 LOG(ERROR) << "Timed out waiting for the fuse-provided package.";
Tao Baocdcf28f2016-01-13 15:05:20 -0800763 result = INSTALL_ERROR;
764 kill(child, SIGKILL);
765 break;
766 }
767 }
768
Tao Bao641fa972018-04-25 18:59:40 -0700769 result = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, false, 0 /*retry_count*/);
Tao Baocdcf28f2016-01-13 15:05:20 -0800770 break;
771 }
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000772
Tao Baocdcf28f2016-01-13 15:05:20 -0800773 if (!waited) {
774 // Calling stat() on this magic filename signals the fuse
775 // filesystem to shut down.
776 struct stat sb;
777 stat(FUSE_SIDELOAD_HOST_EXIT_PATHNAME, &sb);
778
779 waitpid(child, &status, 0);
780 }
781
782 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700783 LOG(ERROR) << "Error exit from the fuse process: " << WEXITSTATUS(status);
Tao Baocdcf28f2016-01-13 15:05:20 -0800784 }
785
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000786 ensure_path_unmounted(SDCARD_ROOT);
Tao Baocdcf28f2016-01-13 15:05:20 -0800787 return result;
Christian Poetzsch4ec58a42015-02-19 10:42:39 +0000788}
789
Tao Bao50dd5322017-03-07 14:57:04 -0800790// Returns REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION means to take the default,
791// which is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery.
792static Device::BuiltinAction prompt_and_wait(Device* device, int status) {
793 for (;;) {
Tao Bao551d2c32018-05-09 20:53:13 -0700794 finish_recovery();
Tao Bao50dd5322017-03-07 14:57:04 -0800795 switch (status) {
796 case INSTALL_SUCCESS:
797 case INSTALL_NONE:
798 ui->SetBackground(RecoveryUI::NO_COMMAND);
799 break;
Doug Zongker6c8553d2012-09-24 10:40:47 -0700800
Tao Bao50dd5322017-03-07 14:57:04 -0800801 case INSTALL_ERROR:
802 case INSTALL_CORRUPT:
803 ui->SetBackground(RecoveryUI::ERROR);
804 break;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800805 }
Tao Bao50dd5322017-03-07 14:57:04 -0800806 ui->SetProgressType(RecoveryUI::EMPTY);
807
Tao Bao1fe1afe2018-05-01 15:56:05 -0700808 size_t chosen_item = ui->ShowMenu(
809 {}, device->GetMenuItems(), 0, false,
Tao Bao3aec6962018-04-20 09:24:58 -0700810 std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
Jerry Zhangb76af932018-05-22 12:08:35 -0700811 // Handle Interrupt key
812 if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
813 return Device::KEY_INTERRUPTED;
814 }
Tao Bao50dd5322017-03-07 14:57:04 -0800815 // Device-specific code may take some action here. It may return one of the core actions
816 // handled in the switch statement below.
Jerry Zhangb76af932018-05-22 12:08:35 -0700817 Device::BuiltinAction chosen_action =
818 (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT))
819 ? Device::REBOOT
820 : device->InvokeMenuItem(chosen_item);
Tao Bao50dd5322017-03-07 14:57:04 -0800821
822 bool should_wipe_cache = false;
823 switch (chosen_action) {
824 case Device::NO_ACTION:
825 break;
826
827 case Device::REBOOT:
828 case Device::SHUTDOWN:
829 case Device::REBOOT_BOOTLOADER:
Hridya Valsaraju20c81b32018-07-27 22:09:12 -0700830 case Device::ENTER_FASTBOOT:
831 case Device::ENTER_RECOVERY:
Tao Bao50dd5322017-03-07 14:57:04 -0800832 return chosen_action;
833
834 case Device::WIPE_DATA:
835 if (ui->IsTextVisible()) {
836 if (ask_to_wipe_data(device)) {
837 wipe_data(device);
838 }
839 } else {
840 wipe_data(device);
841 return Device::NO_ACTION;
842 }
843 break;
844
845 case Device::WIPE_CACHE:
846 wipe_cache(ui->IsTextVisible(), device);
847 if (!ui->IsTextVisible()) return Device::NO_ACTION;
848 break;
849
850 case Device::APPLY_ADB_SIDELOAD:
851 case Device::APPLY_SDCARD:
852 {
853 bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD);
854 if (adb) {
Tao Bao641fa972018-04-25 18:59:40 -0700855 status = apply_from_adb(&should_wipe_cache);
Tao Bao50dd5322017-03-07 14:57:04 -0800856 } else {
857 status = apply_from_sdcard(device, &should_wipe_cache);
858 }
859
860 if (status == INSTALL_SUCCESS && should_wipe_cache) {
861 if (!wipe_cache(false, device)) {
862 status = INSTALL_ERROR;
863 }
864 }
865
866 if (status != INSTALL_SUCCESS) {
867 ui->SetBackground(RecoveryUI::ERROR);
868 ui->Print("Installation aborted.\n");
Jerry Zhangcadf4ed2018-05-02 16:56:00 -0700869 copy_logs(modified_flash, has_cache);
Tao Bao50dd5322017-03-07 14:57:04 -0800870 } else if (!ui->IsTextVisible()) {
871 return Device::NO_ACTION; // reboot if logs aren't visible
872 } else {
873 ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card");
874 }
875 }
876 break;
877
878 case Device::VIEW_RECOVERY_LOGS:
879 choose_recovery_file(device);
880 break;
881
882 case Device::RUN_GRAPHICS_TEST:
883 run_graphics_test();
884 break;
885
Tianjie Xu29d55752017-09-20 17:53:46 -0700886 case Device::RUN_LOCALE_TEST: {
887 ScreenRecoveryUI* screen_ui = static_cast<ScreenRecoveryUI*>(ui);
Tao Bao39c49182018-05-07 22:50:33 -0700888 screen_ui->CheckBackgroundTextImages();
Tianjie Xu29d55752017-09-20 17:53:46 -0700889 break;
890 }
Tao Bao50dd5322017-03-07 14:57:04 -0800891 case Device::MOUNT_SYSTEM:
Jiyong Park8b7af4c2018-06-01 11:58:54 +0900892 // the system partition is mounted at /mnt/system
Yifan Hongd81b8e32018-12-17 14:29:06 -0800893 if (ensure_path_mounted_at(get_system_root(), "/mnt/system") != -1) {
Yifan Hong49327802018-11-26 14:59:09 -0800894 ui->Print("Mounted /system.\n");
Tao Bao50dd5322017-03-07 14:57:04 -0800895 }
896 break;
Jerry Zhangb76af932018-05-22 12:08:35 -0700897
898 case Device::KEY_INTERRUPTED:
899 return Device::KEY_INTERRUPTED;
Tao Bao50dd5322017-03-07 14:57:04 -0800900 }
901 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800902}
903
Tao Bao99f0d9e2016-10-13 12:46:38 -0700904static void print_property(const char* key, const char* name, void* /* cookie */) {
905 printf("%s=%s\n", key, name);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800906}
907
Tao Baoac9d94d2016-11-03 11:37:15 -0700908void ui_print(const char* format, ...) {
909 std::string buffer;
Doug Zongker7c3ae452013-05-14 11:03:02 -0700910 va_list ap;
911 va_start(ap, format);
Tao Baoac9d94d2016-11-03 11:37:15 -0700912 android::base::StringAppendV(&buffer, format, ap);
Doug Zongker7c3ae452013-05-14 11:03:02 -0700913 va_end(ap);
914
Tao Baoac9d94d2016-11-03 11:37:15 -0700915 if (ui != nullptr) {
916 ui->Print("%s", buffer.c_str());
Doug Zongker7c3ae452013-05-14 11:03:02 -0700917 } else {
Tao Baoac9d94d2016-11-03 11:37:15 -0700918 fputs(buffer.c_str(), stdout);
Doug Zongker7c3ae452013-05-14 11:03:02 -0700919 }
920}
921
Tao Bao6d90a9d2018-04-26 10:40:36 -0700922static bool is_battery_ok(int* required_battery_level) {
Yifan Honge8e4c402017-11-08 14:56:03 -0800923 using android::hardware::health::V1_0::BatteryStatus;
Yifan Hong056538c2018-07-11 17:04:12 -0700924 using android::hardware::health::V2_0::get_health_service;
925 using android::hardware::health::V2_0::IHealth;
Yifan Honge8e4c402017-11-08 14:56:03 -0800926 using android::hardware::health::V2_0::Result;
927 using android::hardware::health::V2_0::toString;
Yabin Cui99281df2016-02-17 12:21:52 -0800928
Yifan Hong056538c2018-07-11 17:04:12 -0700929 android::sp<IHealth> health = get_health_service();
Yifan Honge8e4c402017-11-08 14:56:03 -0800930
Tao Bao6d90a9d2018-04-26 10:40:36 -0700931 static constexpr int BATTERY_READ_TIMEOUT_IN_SEC = 10;
Yifan Honge8e4c402017-11-08 14:56:03 -0800932 int wait_second = 0;
933 while (true) {
934 auto charge_status = BatteryStatus::UNKNOWN;
Yifan Hong056538c2018-07-11 17:04:12 -0700935
936 if (health == nullptr) {
937 LOG(WARNING) << "no health implementation is found, assuming defaults";
938 } else {
939 health
940 ->getChargeStatus([&charge_status](auto res, auto out_status) {
941 if (res == Result::SUCCESS) {
942 charge_status = out_status;
943 }
944 })
945 .isOk(); // should not have transport error
946 }
Yifan Honge8e4c402017-11-08 14:56:03 -0800947
948 // Treat unknown status as charged.
949 bool charged = (charge_status != BatteryStatus::DISCHARGING &&
950 charge_status != BatteryStatus::NOT_CHARGING);
951
952 Result res = Result::UNKNOWN;
953 int32_t capacity = INT32_MIN;
Yifan Hong056538c2018-07-11 17:04:12 -0700954 if (health != nullptr) {
955 health
956 ->getCapacity([&res, &capacity](auto out_res, auto out_capacity) {
957 res = out_res;
958 capacity = out_capacity;
959 })
960 .isOk(); // should not have transport error
961 }
Yifan Honge8e4c402017-11-08 14:56:03 -0800962
Yifan Hong056538c2018-07-11 17:04:12 -0700963 LOG(INFO) << "charge_status " << toString(charge_status) << ", charged " << charged
964 << ", status " << toString(res) << ", capacity " << capacity;
Yifan Honge8e4c402017-11-08 14:56:03 -0800965 // At startup, the battery drivers in devices like N5X/N6P take some time to load
966 // the battery profile. Before the load finishes, it reports value 50 as a fake
967 // capacity. BATTERY_READ_TIMEOUT_IN_SEC is set that the battery drivers are expected
968 // to finish loading the battery profile earlier than 10 seconds after kernel startup.
969 if (res == Result::SUCCESS && capacity == 50) {
970 if (wait_second < BATTERY_READ_TIMEOUT_IN_SEC) {
971 sleep(1);
972 wait_second++;
973 continue;
974 }
975 }
976 // If we can't read battery percentage, it may be a device without battery. In this
977 // situation, use 100 as a fake battery percentage.
978 if (res != Result::SUCCESS) {
979 capacity = 100;
980 }
Tao Bao6d90a9d2018-04-26 10:40:36 -0700981
982 // GmsCore enters recovery mode to install package when having enough battery percentage.
983 // Normally, the threshold is 40% without charger and 20% with charger. So we should check
984 // battery with a slightly lower limitation.
985 static constexpr int BATTERY_OK_PERCENTAGE = 20;
986 static constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15;
987 *required_battery_level = charged ? BATTERY_WITH_CHARGER_OK_PERCENTAGE : BATTERY_OK_PERCENTAGE;
988 return capacity >= *required_battery_level;
989 }
Yabin Cui99281df2016-02-17 12:21:52 -0800990}
991
Tianjie Xu99b73be2017-11-28 17:23:06 -0800992// Set the retry count to |retry_count| in BCB.
Tianjie Xu72449c92017-05-16 18:07:31 -0700993static void set_retry_bootloader_message(int retry_count, const std::vector<std::string>& args) {
994 std::vector<std::string> options;
995 for (const auto& arg : args) {
996 if (!android::base::StartsWith(arg, "--retry_count")) {
997 options.push_back(arg);
Tianjie Xu3c62b672016-02-05 18:25:58 -0800998 }
Tianjie Xu72449c92017-05-16 18:07:31 -0700999 }
Tianjie Xu3c62b672016-02-05 18:25:58 -08001000
Tianjie Xu99b73be2017-11-28 17:23:06 -08001001 // Update the retry counter in BCB.
1002 options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count));
Tianjie Xu72449c92017-05-16 18:07:31 -07001003 std::string err;
1004 if (!update_bootloader_message(options, &err)) {
1005 LOG(ERROR) << err;
1006 }
Tianjie Xu3c62b672016-02-05 18:25:58 -08001007}
1008
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001009static bool bootreason_in_blacklist() {
Tao Baoefc35592017-01-08 22:45:47 -08001010 std::string bootreason = android::base::GetProperty("ro.boot.bootreason", "");
1011 if (!bootreason.empty()) {
Tao Baoaac9d9f2018-04-29 23:38:59 -07001012 // More bootreasons can be found in "system/core/bootstat/bootstat.cpp".
1013 static const std::vector<std::string> kBootreasonBlacklist{
1014 "kernel_panic",
1015 "Panic",
1016 };
1017 for (const auto& str : kBootreasonBlacklist) {
1018 if (android::base::EqualsIgnoreCase(str, bootreason)) return true;
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001019 }
Tao Baoefc35592017-01-08 22:45:47 -08001020 }
1021 return false;
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001022}
1023
Tao Bao641fa972018-04-25 18:59:40 -07001024static void log_failure_code(ErrorCode code, const std::string& update_package) {
1025 std::vector<std::string> log_buffer = {
1026 update_package,
1027 "0", // install result
1028 "error: " + std::to_string(code),
1029 };
1030 std::string log_content = android::base::Join(log_buffer, "\n");
1031 const std::string& install_file = Paths::Get().temporary_install_file();
1032 if (!android::base::WriteStringToFile(log_content, install_file)) {
1033 PLOG(ERROR) << "Failed to write " << install_file;
1034 }
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001035
Tao Bao641fa972018-04-25 18:59:40 -07001036 // Also write the info into last_log.
1037 LOG(INFO) << log_content;
Tianjie Xu06e57ac2016-07-11 14:04:08 -07001038}
1039
Jerry Zhang6a648042018-05-04 11:24:10 -07001040Device::BuiltinAction start_recovery(Device* device, const std::vector<std::string>& args) {
Tao Baof9f17342018-04-27 10:44:04 -07001041 static constexpr struct option OPTIONS[] = {
Hridya Valsaraju20c81b32018-07-27 22:09:12 -07001042 { "fastboot", no_argument, nullptr, 0 },
David Andersonedee8362018-05-16 13:43:22 -07001043 { "fsck_unshare_blocks", no_argument, nullptr, 0 },
Tao Baof9f17342018-04-27 10:44:04 -07001044 { "just_exit", no_argument, nullptr, 'x' },
1045 { "locale", required_argument, nullptr, 0 },
1046 { "prompt_and_wipe_data", no_argument, nullptr, 0 },
1047 { "reason", required_argument, nullptr, 0 },
1048 { "retry_count", required_argument, nullptr, 0 },
1049 { "security", no_argument, nullptr, 0 },
1050 { "show_text", no_argument, nullptr, 't' },
1051 { "shutdown_after", no_argument, nullptr, 0 },
1052 { "sideload", no_argument, nullptr, 0 },
1053 { "sideload_auto_reboot", no_argument, nullptr, 0 },
1054 { "update_package", required_argument, nullptr, 0 },
1055 { "wipe_ab", no_argument, nullptr, 0 },
1056 { "wipe_cache", no_argument, nullptr, 0 },
1057 { "wipe_data", no_argument, nullptr, 0 },
1058 { "wipe_package_size", required_argument, nullptr, 0 },
1059 { nullptr, 0, nullptr, 0 },
1060 };
1061
Tianjie Xu99b73be2017-11-28 17:23:06 -08001062 const char* update_package = nullptr;
1063 bool should_wipe_data = false;
1064 bool should_prompt_and_wipe_data = false;
1065 bool should_wipe_cache = false;
1066 bool should_wipe_ab = false;
1067 size_t wipe_package_size = 0;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001068 bool sideload = false;
1069 bool sideload_auto_reboot = false;
1070 bool just_exit = false;
1071 bool shutdown_after = false;
David Andersonedee8362018-05-16 13:43:22 -07001072 bool fsck_unshare_blocks = false;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001073 int retry_count = 0;
1074 bool security_update = false;
Jerry Zhang2dea53e2018-05-02 17:15:03 -07001075 std::string locale;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001076
Tao Bao1700cc42018-07-16 22:09:59 -07001077 auto args_to_parse = StringVectorToNullTerminatedArray(args);
1078
Tianjie Xu99b73be2017-11-28 17:23:06 -08001079 int arg;
1080 int option_index;
Tao Bao1700cc42018-07-16 22:09:59 -07001081 // Parse everything before the last element (which must be a nullptr). getopt_long(3) expects a
1082 // null-terminated char* array, but without counting null as an arg (i.e. argv[argc] should be
1083 // nullptr).
1084 while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
Tianjie Xu99b73be2017-11-28 17:23:06 -08001085 &option_index)) != -1) {
1086 switch (arg) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001087 case 't':
Jerry Zhang6a648042018-05-04 11:24:10 -07001088 // Handled in recovery_main.cpp
Tianjie Xu99b73be2017-11-28 17:23:06 -08001089 break;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001090 case 'x':
1091 just_exit = true;
1092 break;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001093 case 0: {
1094 std::string option = OPTIONS[option_index].name;
David Andersonedee8362018-05-16 13:43:22 -07001095 if (option == "fsck_unshare_blocks") {
1096 fsck_unshare_blocks = true;
Hridya Valsaraju20c81b32018-07-27 22:09:12 -07001097 } else if (option == "locale" || option == "fastboot") {
Jerry Zhang6a648042018-05-04 11:24:10 -07001098 // Handled in recovery_main.cpp
Tianjie Xu99b73be2017-11-28 17:23:06 -08001099 } else if (option == "prompt_and_wipe_data") {
1100 should_prompt_and_wipe_data = true;
Tao Baof9f17342018-04-27 10:44:04 -07001101 } else if (option == "reason") {
1102 reason = optarg;
1103 } else if (option == "retry_count") {
1104 android::base::ParseInt(optarg, &retry_count, 0);
1105 } else if (option == "security") {
1106 security_update = true;
1107 } else if (option == "sideload") {
1108 sideload = true;
1109 } else if (option == "sideload_auto_reboot") {
1110 sideload = true;
1111 sideload_auto_reboot = true;
1112 } else if (option == "shutdown_after") {
1113 shutdown_after = true;
1114 } else if (option == "update_package") {
1115 update_package = optarg;
1116 } else if (option == "wipe_ab") {
1117 should_wipe_ab = true;
1118 } else if (option == "wipe_cache") {
1119 should_wipe_cache = true;
1120 } else if (option == "wipe_data") {
1121 should_wipe_data = true;
1122 } else if (option == "wipe_package_size") {
1123 android::base::ParseUint(optarg, &wipe_package_size);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001124 }
1125 break;
1126 }
1127 case '?':
1128 LOG(ERROR) << "Invalid command argument";
1129 continue;
Doug Zongker9270a202012-01-09 15:16:13 -08001130 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001131 }
Jerry Zhang49fd5d22018-05-17 12:54:41 -07001132 optind = 1;
Doug Zongker9270a202012-01-09 15:16:13 -08001133
Tianjie Xu99b73be2017-11-28 17:23:06 -08001134 printf("stage is [%s]\n", stage.c_str());
1135 printf("reason is [%s]\n", reason);
Doug Zongker02ec6b82012-08-22 17:26:40 -07001136
Tianjie Xu99b73be2017-11-28 17:23:06 -08001137 // Set background string to "installing security update" for security update,
1138 // otherwise set it to "installing system update".
1139 ui->SetSystemUpdateText(security_update);
1140
1141 int st_cur, st_max;
1142 if (!stage.empty() && sscanf(stage.c_str(), "%d/%d", &st_cur, &st_max) == 2) {
1143 ui->SetStage(st_cur, st_max);
1144 }
1145
Jerry Zhang0e577ee2018-05-07 11:21:10 -07001146 std::vector<std::string> title_lines =
1147 android::base::Split(android::base::GetProperty("ro.bootimage.build.fingerprint", ""), ":");
1148 title_lines.insert(std::begin(title_lines), "Android Recovery");
1149 ui->SetTitle(title_lines);
1150
Jerry Zhangb76af932018-05-22 12:08:35 -07001151 ui->ResetKeyInterruptStatus();
Tianjie Xu99b73be2017-11-28 17:23:06 -08001152 device->StartRecovery();
1153
1154 printf("Command:");
1155 for (const auto& arg : args) {
1156 printf(" \"%s\"", arg.c_str());
1157 }
1158 printf("\n\n");
1159
1160 property_list(print_property, nullptr);
1161 printf("\n");
1162
1163 ui->Print("Supported API: %d\n", kRecoveryApiVersion);
1164
1165 int status = INSTALL_SUCCESS;
1166
1167 if (update_package != nullptr) {
1168 // It's not entirely true that we will modify the flash. But we want
1169 // to log the update attempt since update_package is non-NULL.
1170 modified_flash = true;
1171
Tao Bao6d90a9d2018-04-26 10:40:36 -07001172 int required_battery_level;
1173 if (retry_count == 0 && !is_battery_ok(&required_battery_level)) {
1174 ui->Print("battery capacity is not enough for installing package: %d%% needed\n",
1175 required_battery_level);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001176 // Log the error code to last_install when installation skips due to
1177 // low battery.
1178 log_failure_code(kLowBattery, update_package);
1179 status = INSTALL_SKIPPED;
Tianjie Xua6f49bd2018-03-26 14:32:11 -07001180 } else if (retry_count == 0 && bootreason_in_blacklist()) {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001181 // Skip update-on-reboot when bootreason is kernel_panic or similar
1182 ui->Print("bootreason is in the blacklist; skip OTA installation\n");
1183 log_failure_code(kBootreasonInBlacklist, update_package);
1184 status = INSTALL_SKIPPED;
Dmitri Plotnikov8706a982017-04-18 08:28:26 -07001185 } else {
Tianjie Xu99b73be2017-11-28 17:23:06 -08001186 // It's a fresh update. Initialize the retry_count in the BCB to 1; therefore we can later
1187 // identify the interrupted update due to unexpected reboots.
1188 if (retry_count == 0) {
1189 set_retry_bootloader_message(retry_count + 1, args);
Tao Bao7022f332017-07-25 09:52:36 -07001190 }
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001191
Tao Bao641fa972018-04-25 18:59:40 -07001192 status = install_package(update_package, &should_wipe_cache, true, retry_count);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001193 if (status == INSTALL_SUCCESS && should_wipe_cache) {
1194 wipe_cache(false, device);
1195 }
1196 if (status != INSTALL_SUCCESS) {
1197 ui->Print("Installation aborted.\n");
Tao Baoaac9d9f2018-04-29 23:38:59 -07001198
1199 // When I/O error or bspatch/imgpatch error happens, reboot and retry installation
1200 // RETRY_LIMIT times before we abandon this OTA update.
1201 static constexpr int RETRY_LIMIT = 4;
Tianjie Xu99b73be2017-11-28 17:23:06 -08001202 if (status == INSTALL_RETRY && retry_count < RETRY_LIMIT) {
Jerry Zhangcadf4ed2018-05-02 16:56:00 -07001203 copy_logs(modified_flash, has_cache);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001204 retry_count += 1;
1205 set_retry_bootloader_message(retry_count, args);
1206 // Print retry count on screen.
1207 ui->Print("Retry attempt %d\n", retry_count);
1208
1209 // Reboot and retry the update
1210 if (!reboot("reboot,recovery")) {
1211 ui->Print("Reboot failed\n");
1212 } else {
1213 while (true) {
1214 pause();
1215 }
1216 }
Tianjie Xud9d16292017-04-20 18:08:21 -07001217 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001218 // If this is an eng or userdebug build, then automatically
1219 // turn the text display on if the script fails so the error
1220 // message is visible.
1221 if (is_ro_debuggable()) {
1222 ui->ShowText(true);
Tao Baoc679f932015-03-30 09:43:49 -07001223 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001224 }
Doug Zongker8674a722010-09-15 11:08:23 -07001225 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001226 } else if (should_wipe_data) {
1227 if (!wipe_data(device)) {
1228 status = INSTALL_ERROR;
Doug Zongkerb1d12632014-03-18 10:32:12 -07001229 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001230 } else if (should_prompt_and_wipe_data) {
Tao Baoa5bbcb92018-09-17 14:32:47 -07001231 // Trigger the logging to capture the cause, even if user chooses to not wipe data.
1232 modified_flash = true;
1233
Tianjie Xu99b73be2017-11-28 17:23:06 -08001234 ui->ShowText(true);
1235 ui->SetBackground(RecoveryUI::ERROR);
Hridya Valsarajueb6f13a2018-09-12 10:25:01 -07001236 status = prompt_and_wipe_data(device);
1237 if (status != INSTALL_KEY_INTERRUPTED) {
1238 ui->ShowText(false);
Tao Bao75238632015-05-27 14:46:17 -07001239 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001240 } else if (should_wipe_cache) {
1241 if (!wipe_cache(false, device)) {
1242 status = INSTALL_ERROR;
1243 }
1244 } else if (should_wipe_ab) {
1245 if (!wipe_ab_device(wipe_package_size)) {
1246 status = INSTALL_ERROR;
1247 }
1248 } else if (sideload) {
1249 // 'adb reboot sideload' acts the same as user presses key combinations
1250 // to enter the sideload mode. When 'sideload-auto-reboot' is used, text
1251 // display will NOT be turned on by default. And it will reboot after
1252 // sideload finishes even if there are errors. Unless one turns on the
1253 // text display during the installation. This is to enable automated
1254 // testing.
1255 if (!sideload_auto_reboot) {
1256 ui->ShowText(true);
1257 }
Tao Bao641fa972018-04-25 18:59:40 -07001258 status = apply_from_adb(&should_wipe_cache);
Tianjie Xu99b73be2017-11-28 17:23:06 -08001259 if (status == INSTALL_SUCCESS && should_wipe_cache) {
1260 if (!wipe_cache(false, device)) {
1261 status = INSTALL_ERROR;
1262 }
1263 }
1264 ui->Print("\nInstall from ADB complete (status: %d).\n", status);
1265 if (sideload_auto_reboot) {
1266 ui->Print("Rebooting automatically.\n");
1267 }
David Andersonedee8362018-05-16 13:43:22 -07001268 } else if (fsck_unshare_blocks) {
1269 if (!do_fsck_unshare_blocks()) {
1270 status = INSTALL_ERROR;
1271 }
Tianjie Xu99b73be2017-11-28 17:23:06 -08001272 } else if (!just_exit) {
1273 // If this is an eng or userdebug build, automatically turn on the text display if no command
1274 // is specified. Note that this should be called before setting the background to avoid
1275 // flickering the background image.
1276 if (is_ro_debuggable()) {
1277 ui->ShowText(true);
1278 }
1279 status = INSTALL_NONE; // No command specified
1280 ui->SetBackground(RecoveryUI::NO_COMMAND);
1281 }
1282
1283 if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
1284 ui->SetBackground(RecoveryUI::ERROR);
1285 if (!ui->IsTextVisible()) {
1286 sleep(5);
1287 }
1288 }
1289
1290 Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
1291 // 1. If the recovery menu is visible, prompt and wait for commands.
1292 // 2. If the state is INSTALL_NONE, wait for commands. (i.e. In user build, manually reboot into
1293 // recovery to sideload a package.)
1294 // 3. sideload_auto_reboot is an option only available in user-debug build, reboot the device
1295 // without waiting.
1296 // 4. In all other cases, reboot the device. Therefore, normal users will observe the device
1297 // reboot after it shows the "error" screen for 5s.
1298 if ((status == INSTALL_NONE && !sideload_auto_reboot) || ui->IsTextVisible()) {
1299 Device::BuiltinAction temp = prompt_and_wait(device, status);
1300 if (temp != Device::NO_ACTION) {
1301 after = temp;
1302 }
1303 }
1304
1305 // Save logs and clean up before rebooting or shutting down.
Tao Bao551d2c32018-05-09 20:53:13 -07001306 finish_recovery();
Tianjie Xu99b73be2017-11-28 17:23:06 -08001307
Jerry Zhang6a648042018-05-04 11:24:10 -07001308 return after;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001309}