2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
#include "recovery.h"
|
2018-04-26 01:47:04 +02:00
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
2016-06-02 20:16:50 +02:00
|
|
|
#include <inttypes.h>
|
2009-03-04 04:28:42 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <linux/input.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
Add ScreenRecoveryUI::ShowMenu().
From caller's PoV, RecoveryUI::{Start,Select,End}Menu should always be
used together, i.e. to show a menu and get user's selection. This CL
provides ShowMenu() as one-stop service (which is based on
get_menu_selection() from recovery.cpp).
Also move RecoveryUI::{Start,Select,End}Menu into ScreenRecoveryUI, with
a dropped access level from public to protected.
Due to the dependency on recovery / librecovery refactoring, will add
testcases in follow-up CLs.
Test: Build and boot into recovery image. Check the menus (main menu,
'View recovery logs', 'Wipe data/factory reset').
Change-Id: Ie17aa78144871a12affd6f9075e045f76608a0ba
2018-04-20 18:24:58 +02:00
|
|
|
#include <functional>
|
2019-04-27 07:56:56 +02:00
|
|
|
#include <iterator>
|
2017-02-10 09:13:30 +01:00
|
|
|
#include <memory>
|
2016-06-02 20:16:50 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2015-09-11 00:32:24 +02:00
|
|
|
|
2015-12-05 00:30:20 +01:00
|
|
|
#include <android-base/file.h>
|
2016-08-06 03:00:04 +02:00
|
|
|
#include <android-base/logging.h>
|
2016-02-06 03:25:58 +01:00
|
|
|
#include <android-base/parseint.h>
|
2016-09-24 00:30:55 +02:00
|
|
|
#include <android-base/properties.h>
|
2015-12-05 00:30:20 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2016-06-02 20:16:50 +02:00
|
|
|
#include <android-base/strings.h>
|
2016-09-24 00:30:55 +02:00
|
|
|
#include <cutils/properties.h> /* for property_list */
|
2019-05-10 19:40:59 +02:00
|
|
|
#include <fs_mgr/roots.h>
|
2016-09-09 05:10:11 +02:00
|
|
|
#include <ziparchive/zip_archive.h>
|
2016-02-17 21:21:52 +01:00
|
|
|
|
2019-04-27 07:56:56 +02:00
|
|
|
#include "bootloader_message/bootloader_message.h"
|
2019-03-23 00:08:52 +01:00
|
|
|
#include "install/adb_install.h"
|
2019-06-12 01:09:07 +02:00
|
|
|
#include "install/fuse_install.h"
|
2019-03-23 00:08:52 +01:00
|
|
|
#include "install/install.h"
|
|
|
|
#include "install/package.h"
|
2019-12-09 10:26:05 +01:00
|
|
|
#include "install/snapshot_utils.h"
|
2019-04-13 01:22:15 +02:00
|
|
|
#include "install/wipe_data.h"
|
2019-04-27 07:56:56 +02:00
|
|
|
#include "install/wipe_device.h"
|
2019-07-29 23:21:49 +02:00
|
|
|
#include "otautil/boot_state.h"
|
2017-10-06 16:43:41 +02:00
|
|
|
#include "otautil/error_code.h"
|
2018-04-26 03:59:40 +02:00
|
|
|
#include "otautil/paths.h"
|
2018-05-04 08:01:13 +02:00
|
|
|
#include "otautil/sysutil.h"
|
2018-08-20 22:40:47 +02:00
|
|
|
#include "recovery_ui/screen_ui.h"
|
|
|
|
#include "recovery_ui/ui.h"
|
2019-09-28 08:32:00 +02:00
|
|
|
#include "recovery_utils/battery_utils.h"
|
2019-10-01 20:55:36 +02:00
|
|
|
#include "recovery_utils/logging.h"
|
|
|
|
#include "recovery_utils/roots.h"
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2018-04-30 08:38:59 +02:00
|
|
|
static constexpr const char* COMMAND_FILE = "/cache/recovery/command";
|
|
|
|
static constexpr const char* LAST_KMSG_FILE = "/cache/recovery/last_kmsg";
|
|
|
|
static constexpr const char* LAST_LOG_FILE = "/cache/recovery/last_log";
|
|
|
|
static constexpr const char* LOCALE_FILE = "/cache/recovery/last_locale";
|
|
|
|
|
|
|
|
static constexpr const char* CACHE_ROOT = "/cache";
|
2011-10-29 00:13:10 +02:00
|
|
|
|
2019-04-13 01:22:15 +02:00
|
|
|
static bool save_current_log = false;
|
2016-11-03 19:37:15 +01:00
|
|
|
|
2009-03-04 04:28:42 +01:00
|
|
|
/*
|
|
|
|
* The recovery tool communicates with the main system through /cache files.
|
|
|
|
* /cache/recovery/command - INPUT - command line for tool, one arg per line
|
|
|
|
* /cache/recovery/log - OUTPUT - combined log file from recovery run(s)
|
|
|
|
*
|
|
|
|
* The arguments which may be supplied in the recovery.command file:
|
2010-09-20 21:16:13 +02:00
|
|
|
* --update_package=path - verify install an OTA package file
|
2019-06-25 22:59:39 +02:00
|
|
|
* --install_with_fuse - install the update package with FUSE. This allows installation of large
|
|
|
|
* packages on LP32 builds. Since the mmap will otherwise fail due to out of memory.
|
2009-03-04 04:28:42 +01:00
|
|
|
* --wipe_data - erase user data (and cache), then reboot
|
2018-04-27 19:44:04 +02:00
|
|
|
* --prompt_and_wipe_data - prompt the user that data is corrupt, with their consent erase user
|
|
|
|
* data (and cache), then reboot
|
2009-03-04 04:28:42 +01:00
|
|
|
* --wipe_cache - wipe cache (but not user data), then reboot
|
2018-04-27 19:44:04 +02:00
|
|
|
* --show_text - show the recovery text menu, used by some bootloader (e.g. http://b/36872519).
|
2009-11-30 17:40:57 +01:00
|
|
|
* --set_encrypted_filesystem=on|off - enables / diasables encrypted fs
|
2012-04-12 20:01:22 +02:00
|
|
|
* --just_exit - do nothing; exit and reboot
|
2009-03-04 04:28:42 +01:00
|
|
|
*
|
|
|
|
* After completing, we remove /cache/recovery/command and reboot.
|
|
|
|
* Arguments may also be supplied in the bootloader control block (BCB).
|
|
|
|
* These important scenarios must be safely restartable at any point:
|
|
|
|
*
|
|
|
|
* FACTORY RESET
|
|
|
|
* 1. user selects "factory reset"
|
|
|
|
* 2. main system writes "--wipe_data" to /cache/recovery/command
|
|
|
|
* 3. main system reboots into recovery
|
|
|
|
* 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"
|
|
|
|
* -- after this, rebooting will restart the erase --
|
2010-09-20 21:16:13 +02:00
|
|
|
* 5. erase_volume() reformats /data
|
|
|
|
* 6. erase_volume() reformats /cache
|
2019-05-15 22:59:39 +02:00
|
|
|
* 7. FinishRecovery() erases BCB
|
2009-03-04 04:28:42 +01:00
|
|
|
* -- after this, rebooting will restart the main system --
|
|
|
|
* 8. main() calls reboot() to boot main system
|
|
|
|
*
|
|
|
|
* OTA INSTALL
|
|
|
|
* 1. main system downloads OTA package to /cache/some-filename.zip
|
2010-09-22 21:01:37 +02:00
|
|
|
* 2. main system writes "--update_package=/cache/some-filename.zip"
|
2009-03-04 04:28:42 +01:00
|
|
|
* 3. main system reboots into recovery
|
|
|
|
* 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."
|
|
|
|
* -- after this, rebooting will attempt to reinstall the update --
|
2019-04-30 08:48:02 +02:00
|
|
|
* 5. InstallPackage() attempts to install the update
|
2009-03-04 04:28:42 +01:00
|
|
|
* NOTE: the package install must itself be restartable from any point
|
2019-05-15 22:59:39 +02:00
|
|
|
* 6. FinishRecovery() erases BCB
|
2009-03-04 04:28:42 +01:00
|
|
|
* -- after this, rebooting will (try to) restart the main system --
|
|
|
|
* 7. ** if install failed **
|
2019-04-30 08:48:02 +02:00
|
|
|
* 7a. PromptAndWait() shows an error icon and waits for the user
|
2016-12-14 07:29:49 +01:00
|
|
|
* 7b. the user reboots (pulling the battery, etc) into the main system
|
2009-03-04 04:28:42 +01:00
|
|
|
*/
|
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
static bool IsRoDebuggable() {
|
|
|
|
return android::base::GetBoolProperty("ro.debuggable", false);
|
2015-02-10 23:46:14 +01:00
|
|
|
}
|
|
|
|
|
2017-07-21 21:13:15 +02:00
|
|
|
// Clear the recovery command and prepare to boot a (hopefully working) system,
|
2016-03-24 19:50:34 +01:00
|
|
|
// copy our log file to cache as well (for the system to read). This function is
|
|
|
|
// idempotent: call it as many times as you like.
|
2019-05-15 22:59:39 +02:00
|
|
|
static void FinishRecovery(RecoveryUI* ui) {
|
2018-05-10 05:53:13 +02:00
|
|
|
std::string locale = ui->GetLocale();
|
2017-07-21 21:13:15 +02:00
|
|
|
// Save the locale to cache, so if recovery is next started up without a '--locale' argument
|
|
|
|
// (e.g., directly from the bootloader) it will use the last-known locale.
|
2019-05-15 22:59:39 +02:00
|
|
|
if (!locale.empty() && HasCache()) {
|
2017-07-21 21:13:15 +02:00
|
|
|
LOG(INFO) << "Saving locale \"" << locale << "\"";
|
|
|
|
if (ensure_path_mounted(LOCALE_FILE) != 0) {
|
|
|
|
LOG(ERROR) << "Failed to mount " << LOCALE_FILE;
|
|
|
|
} else if (!android::base::WriteStringToFile(locale, LOCALE_FILE)) {
|
|
|
|
PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE;
|
2012-08-23 22:16:12 +02:00
|
|
|
}
|
2017-07-21 21:13:15 +02:00
|
|
|
}
|
2012-08-23 22:16:12 +02:00
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
copy_logs(save_current_log);
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-07-21 21:13:15 +02:00
|
|
|
// Reset to normal system boot so recovery won't cycle indefinitely.
|
|
|
|
std::string err;
|
|
|
|
if (!clear_bootloader_message(&err)) {
|
|
|
|
LOG(ERROR) << "Failed to clear BCB message: " << err;
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-07-21 21:13:15 +02:00
|
|
|
// Remove the command file, so recovery won't repeat indefinitely.
|
2019-05-15 22:59:39 +02:00
|
|
|
if (HasCache()) {
|
2017-07-21 21:13:15 +02:00
|
|
|
if (ensure_path_mounted(COMMAND_FILE) != 0 || (unlink(COMMAND_FILE) && errno != ENOENT)) {
|
|
|
|
LOG(WARNING) << "Can't unlink " << COMMAND_FILE;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2017-07-21 21:13:15 +02:00
|
|
|
ensure_path_unmounted(CACHE_ROOT);
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-07-21 21:13:15 +02:00
|
|
|
sync(); // For good measure.
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2015-03-25 23:16:51 +01:00
|
|
|
static bool yes_no(Device* device, const char* question1, const char* question2) {
|
2018-05-02 00:56:05 +02:00
|
|
|
std::vector<std::string> headers{ question1, question2 };
|
|
|
|
std::vector<std::string> items{ " No", " Yes" };
|
2009-04-01 22:57:40 +02:00
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
size_t chosen_item = device->GetUI()->ShowMenu(
|
Add ScreenRecoveryUI::ShowMenu().
From caller's PoV, RecoveryUI::{Start,Select,End}Menu should always be
used together, i.e. to show a menu and get user's selection. This CL
provides ShowMenu() as one-stop service (which is based on
get_menu_selection() from recovery.cpp).
Also move RecoveryUI::{Start,Select,End}Menu into ScreenRecoveryUI, with
a dropped access level from public to protected.
Due to the dependency on recovery / librecovery refactoring, will add
testcases in follow-up CLs.
Test: Build and boot into recovery image. Check the menus (main menu,
'View recovery logs', 'Wipe data/factory reset').
Change-Id: Ie17aa78144871a12affd6f9075e045f76608a0ba
2018-04-20 18:24:58 +02:00
|
|
|
headers, items, 0, true,
|
|
|
|
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
|
|
|
|
return (chosen_item == 1);
|
2015-03-25 23:16:51 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 22:04:23 +01:00
|
|
|
static bool ask_to_wipe_data(Device* device) {
|
2018-10-26 00:22:07 +02:00
|
|
|
std::vector<std::string> headers{ "Wipe all user data?", " THIS CAN NOT BE UNDONE!" };
|
|
|
|
std::vector<std::string> items{ " Cancel", " Factory data reset" };
|
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
size_t chosen_item = device->GetUI()->ShowPromptWipeDataConfirmationMenu(
|
2018-10-26 00:22:07 +02:00
|
|
|
headers, items,
|
|
|
|
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
|
|
|
|
|
|
|
|
return (chosen_item == 1);
|
2016-12-19 22:04:23 +01:00
|
|
|
}
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2018-09-12 19:25:01 +02:00
|
|
|
static InstallResult prompt_and_wipe_data(Device* device) {
|
2017-08-14 08:48:55 +02:00
|
|
|
// Use a single string and let ScreenRecoveryUI handles the wrapping.
|
2018-10-17 00:13:09 +02:00
|
|
|
std::vector<std::string> wipe_data_menu_headers{
|
2017-08-14 08:48:55 +02:00
|
|
|
"Can't load Android system. Your data may be corrupt. "
|
|
|
|
"If you continue to get this message, you may need to "
|
|
|
|
"perform a factory data reset and erase all user data "
|
2017-03-23 20:32:40 +01:00
|
|
|
"stored on this device.",
|
2017-02-24 04:06:53 +01:00
|
|
|
};
|
2018-05-02 00:56:05 +02:00
|
|
|
// clang-format off
|
2018-10-17 00:13:09 +02:00
|
|
|
std::vector<std::string> wipe_data_menu_items {
|
2017-03-23 20:32:40 +01:00
|
|
|
"Try again",
|
|
|
|
"Factory data reset",
|
2017-02-24 04:06:53 +01:00
|
|
|
};
|
2018-05-02 00:56:05 +02:00
|
|
|
// clang-format on
|
2017-02-24 04:06:53 +01:00
|
|
|
for (;;) {
|
2019-05-15 22:59:39 +02:00
|
|
|
size_t chosen_item = device->GetUI()->ShowPromptWipeDataMenu(
|
2018-10-17 00:13:09 +02:00
|
|
|
wipe_data_menu_headers, wipe_data_menu_items,
|
Add ScreenRecoveryUI::ShowMenu().
From caller's PoV, RecoveryUI::{Start,Select,End}Menu should always be
used together, i.e. to show a menu and get user's selection. This CL
provides ShowMenu() as one-stop service (which is based on
get_menu_selection() from recovery.cpp).
Also move RecoveryUI::{Start,Select,End}Menu into ScreenRecoveryUI, with
a dropped access level from public to protected.
Due to the dependency on recovery / librecovery refactoring, will add
testcases in follow-up CLs.
Test: Build and boot into recovery image. Check the menus (main menu,
'View recovery logs', 'Wipe data/factory reset').
Change-Id: Ie17aa78144871a12affd6f9075e045f76608a0ba
2018-04-20 18:24:58 +02:00
|
|
|
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
|
2018-05-22 21:08:35 +02:00
|
|
|
// If ShowMenu() returned RecoveryUI::KeyError::INTERRUPTED, WaitKey() was interrupted.
|
|
|
|
if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
|
2018-09-12 19:25:01 +02:00
|
|
|
return INSTALL_KEY_INTERRUPTED;
|
2018-05-22 21:08:35 +02:00
|
|
|
}
|
2017-02-24 04:06:53 +01:00
|
|
|
if (chosen_item != 1) {
|
2018-09-12 19:25:01 +02:00
|
|
|
return INSTALL_SUCCESS; // Just reboot, no wipe; not a failure, user asked for it
|
2017-02-24 04:06:53 +01:00
|
|
|
}
|
2018-10-17 00:13:09 +02:00
|
|
|
|
2017-02-24 04:06:53 +01:00
|
|
|
if (ask_to_wipe_data(device)) {
|
2019-07-29 23:21:49 +02:00
|
|
|
CHECK(device->GetReason().has_value());
|
|
|
|
bool convert_fbe = device->GetReason().value() == "convert_fbe";
|
2019-04-13 01:22:15 +02:00
|
|
|
if (WipeData(device, convert_fbe)) {
|
2018-09-12 19:25:01 +02:00
|
|
|
return INSTALL_SUCCESS;
|
|
|
|
} else {
|
|
|
|
return INSTALL_ERROR;
|
|
|
|
}
|
2016-12-19 22:04:23 +01:00
|
|
|
}
|
2017-02-24 04:06:53 +01:00
|
|
|
}
|
2016-12-19 22:04:23 +01:00
|
|
|
}
|
|
|
|
|
2014-10-23 03:38:48 +02:00
|
|
|
static void choose_recovery_file(Device* device) {
|
2017-03-07 09:56:27 +01:00
|
|
|
std::vector<std::string> entries;
|
2019-05-15 22:59:39 +02:00
|
|
|
if (HasCache()) {
|
2017-03-07 09:56:27 +01:00
|
|
|
for (int i = 0; i < KEEP_LOG_COUNT; i++) {
|
|
|
|
auto add_to_entries = [&](const char* filename) {
|
|
|
|
std::string log_file(filename);
|
|
|
|
if (i > 0) {
|
|
|
|
log_file += "." + std::to_string(i);
|
|
|
|
}
|
2014-10-23 03:38:48 +02:00
|
|
|
|
2018-12-17 23:29:06 +01:00
|
|
|
if (ensure_path_mounted(log_file) == 0 && access(log_file.c_str(), R_OK) == 0) {
|
2017-03-07 09:56:27 +01:00
|
|
|
entries.push_back(std::move(log_file));
|
|
|
|
}
|
|
|
|
};
|
2014-12-09 20:26:40 +01:00
|
|
|
|
2017-03-07 09:56:27 +01:00
|
|
|
// Add LAST_LOG_FILE + LAST_LOG_FILE.x
|
|
|
|
add_to_entries(LAST_LOG_FILE);
|
2015-05-05 03:50:27 +02:00
|
|
|
|
2017-03-07 09:56:27 +01:00
|
|
|
// Add LAST_KMSG_FILE + LAST_KMSG_FILE.x
|
|
|
|
add_to_entries(LAST_KMSG_FILE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If cache partition is not found, view /tmp/recovery.log instead.
|
2018-04-26 03:59:40 +02:00
|
|
|
if (access(Paths::Get().temporary_log_file().c_str(), R_OK) == -1) {
|
2017-03-07 09:56:27 +01:00
|
|
|
return;
|
2016-08-17 21:02:46 +02:00
|
|
|
} else {
|
2018-04-26 03:59:40 +02:00
|
|
|
entries.push_back(Paths::Get().temporary_log_file());
|
2014-10-23 03:38:48 +02:00
|
|
|
}
|
2017-03-07 09:56:27 +01:00
|
|
|
}
|
2014-10-23 03:38:48 +02:00
|
|
|
|
2017-03-07 09:56:27 +01:00
|
|
|
entries.push_back("Back");
|
2015-05-06 21:40:05 +02:00
|
|
|
|
2018-05-02 00:56:05 +02:00
|
|
|
std::vector<std::string> headers{ "Select file to view" };
|
2014-10-23 03:38:48 +02:00
|
|
|
|
2018-05-02 00:56:05 +02:00
|
|
|
size_t chosen_item = 0;
|
2017-03-07 09:56:27 +01:00
|
|
|
while (true) {
|
2019-05-15 22:59:39 +02:00
|
|
|
chosen_item = device->GetUI()->ShowMenu(
|
2018-05-02 00:56:05 +02:00
|
|
|
headers, entries, chosen_item, true,
|
Add ScreenRecoveryUI::ShowMenu().
From caller's PoV, RecoveryUI::{Start,Select,End}Menu should always be
used together, i.e. to show a menu and get user's selection. This CL
provides ShowMenu() as one-stop service (which is based on
get_menu_selection() from recovery.cpp).
Also move RecoveryUI::{Start,Select,End}Menu into ScreenRecoveryUI, with
a dropped access level from public to protected.
Due to the dependency on recovery / librecovery refactoring, will add
testcases in follow-up CLs.
Test: Build and boot into recovery image. Check the menus (main menu,
'View recovery logs', 'Wipe data/factory reset').
Change-Id: Ie17aa78144871a12affd6f9075e045f76608a0ba
2018-04-20 18:24:58 +02:00
|
|
|
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
|
2018-05-22 21:08:35 +02:00
|
|
|
|
|
|
|
// Handle WaitKey() interrupt.
|
|
|
|
if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
|
|
|
|
break;
|
|
|
|
}
|
2017-03-07 09:56:27 +01:00
|
|
|
if (entries[chosen_item] == "Back") break;
|
2014-10-23 03:38:48 +02:00
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
device->GetUI()->ShowFile(entries[chosen_item]);
|
2017-03-07 09:56:27 +01:00
|
|
|
}
|
2014-10-23 03:38:48 +02:00
|
|
|
}
|
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
static void run_graphics_test(RecoveryUI* ui) {
|
2017-03-07 08:53:16 +01:00
|
|
|
// Switch to graphics screen.
|
|
|
|
ui->ShowText(false);
|
2016-04-15 01:49:04 +02:00
|
|
|
|
2017-03-07 08:53:16 +01:00
|
|
|
ui->SetProgressType(RecoveryUI::INDETERMINATE);
|
|
|
|
ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
|
|
|
|
sleep(1);
|
2016-04-15 01:49:04 +02:00
|
|
|
|
2017-03-07 08:53:16 +01:00
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
sleep(1);
|
2016-04-15 01:49:04 +02:00
|
|
|
|
2017-03-07 08:53:16 +01:00
|
|
|
ui->SetBackground(RecoveryUI::NO_COMMAND);
|
|
|
|
sleep(1);
|
2016-04-15 01:49:04 +02:00
|
|
|
|
2017-03-07 08:53:16 +01:00
|
|
|
ui->SetBackground(RecoveryUI::ERASING);
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
// Calling SetBackground() after SetStage() to trigger a redraw.
|
|
|
|
ui->SetStage(1, 3);
|
|
|
|
ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
|
|
|
|
sleep(1);
|
|
|
|
ui->SetStage(2, 3);
|
|
|
|
ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
|
|
|
|
sleep(1);
|
|
|
|
ui->SetStage(3, 3);
|
|
|
|
ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
ui->SetStage(-1, -1);
|
|
|
|
ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
|
|
|
|
|
|
|
|
ui->SetProgressType(RecoveryUI::DETERMINATE);
|
|
|
|
ui->ShowProgress(1.0, 10.0);
|
|
|
|
float fraction = 0.0;
|
|
|
|
for (size_t i = 0; i < 100; ++i) {
|
|
|
|
fraction += .01;
|
|
|
|
ui->SetProgress(fraction);
|
|
|
|
usleep(100000);
|
|
|
|
}
|
2016-04-15 01:49:04 +02:00
|
|
|
|
2017-03-07 08:53:16 +01:00
|
|
|
ui->ShowText(true);
|
2016-04-15 01:49:04 +02:00
|
|
|
}
|
|
|
|
|
2020-01-30 01:51:33 +01:00
|
|
|
static void WriteUpdateInProgress() {
|
|
|
|
std::string err;
|
|
|
|
if (!update_bootloader_message({ "--reason=update_in_progress" }, &err)) {
|
|
|
|
LOG(ERROR) << "Failed to WriteUpdateInProgress: " << err;
|
2020-01-28 02:37:39 +01:00
|
|
|
}
|
2020-01-30 01:51:33 +01:00
|
|
|
}
|
2020-01-28 02:37:39 +01:00
|
|
|
|
2020-01-30 01:51:33 +01:00
|
|
|
static bool AskToReboot(Device* device, Device::BuiltinAction chosen_action) {
|
2020-01-28 02:37:39 +01:00
|
|
|
bool is_non_ab = android::base::GetProperty("ro.boot.slot_suffix", "").empty();
|
|
|
|
bool is_virtual_ab = android::base::GetBoolProperty("ro.virtual_ab.enabled", false);
|
|
|
|
if (!is_non_ab && !is_virtual_ab) {
|
|
|
|
// Only prompt for non-A/B or Virtual A/B devices.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string header_text;
|
|
|
|
std::string item_text;
|
|
|
|
switch (chosen_action) {
|
|
|
|
case Device::REBOOT:
|
|
|
|
header_text = "reboot";
|
|
|
|
item_text = " Reboot system now";
|
|
|
|
break;
|
|
|
|
case Device::SHUTDOWN:
|
|
|
|
header_text = "power off";
|
|
|
|
item_text = " Power off";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
LOG(FATAL) << "Invalid chosen action " << chosen_action;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2020-01-30 01:51:33 +01:00
|
|
|
std::vector<std::string> headers{ "WARNING: Previous installation has failed.",
|
2020-01-28 02:37:39 +01:00
|
|
|
" Your device may fail to boot if you " + header_text +
|
|
|
|
" now.",
|
|
|
|
" Confirm reboot?" };
|
|
|
|
std::vector<std::string> items{ " Cancel", item_text };
|
|
|
|
|
|
|
|
size_t chosen_item = device->GetUI()->ShowMenu(
|
|
|
|
headers, items, 0, true /* menu_only */,
|
|
|
|
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
|
|
|
|
|
|
|
|
return (chosen_item == 1);
|
|
|
|
}
|
|
|
|
|
2019-04-30 08:48:02 +02:00
|
|
|
// Shows the recovery UI and waits for user input. Returns one of the device builtin actions, such
|
|
|
|
// as REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION means to take the default, which
|
|
|
|
// is to reboot or shutdown depending on if the --shutdown_after flag was passed to recovery.
|
|
|
|
static Device::BuiltinAction PromptAndWait(Device* device, InstallResult status) {
|
2019-05-15 22:59:39 +02:00
|
|
|
auto ui = device->GetUI();
|
2020-01-30 01:51:33 +01:00
|
|
|
bool update_in_progress = (device->GetReason().value_or("") == "update_in_progress");
|
2017-03-07 23:57:04 +01:00
|
|
|
for (;;) {
|
2019-05-15 22:59:39 +02:00
|
|
|
FinishRecovery(ui);
|
2017-03-07 23:57:04 +01:00
|
|
|
switch (status) {
|
|
|
|
case INSTALL_SUCCESS:
|
|
|
|
case INSTALL_NONE:
|
2019-04-30 08:48:02 +02:00
|
|
|
case INSTALL_SKIPPED:
|
|
|
|
case INSTALL_RETRY:
|
|
|
|
case INSTALL_KEY_INTERRUPTED:
|
2017-03-07 23:57:04 +01:00
|
|
|
ui->SetBackground(RecoveryUI::NO_COMMAND);
|
|
|
|
break;
|
2012-09-24 19:40:47 +02:00
|
|
|
|
2017-03-07 23:57:04 +01:00
|
|
|
case INSTALL_ERROR:
|
|
|
|
case INSTALL_CORRUPT:
|
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
break;
|
2019-04-30 08:48:02 +02:00
|
|
|
|
|
|
|
case INSTALL_REBOOT:
|
|
|
|
// All the reboots should have been handled prior to entering PromptAndWait() or immediately
|
|
|
|
// after installing a package.
|
|
|
|
LOG(FATAL) << "Invalid status code of INSTALL_REBOOT";
|
|
|
|
break;
|
2017-03-07 23:57:04 +01:00
|
|
|
}
|
|
|
|
ui->SetProgressType(RecoveryUI::EMPTY);
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2020-01-30 01:51:33 +01:00
|
|
|
std::vector<std::string> headers;
|
|
|
|
if (update_in_progress) {
|
|
|
|
headers = { "WARNING: Previous installation has failed.",
|
|
|
|
" Your device may fail to boot if you reboot or power off now." };
|
|
|
|
}
|
|
|
|
|
2018-05-02 00:56:05 +02:00
|
|
|
size_t chosen_item = ui->ShowMenu(
|
2020-01-30 01:51:33 +01:00
|
|
|
headers, device->GetMenuItems(), 0, false,
|
Add ScreenRecoveryUI::ShowMenu().
From caller's PoV, RecoveryUI::{Start,Select,End}Menu should always be
used together, i.e. to show a menu and get user's selection. This CL
provides ShowMenu() as one-stop service (which is based on
get_menu_selection() from recovery.cpp).
Also move RecoveryUI::{Start,Select,End}Menu into ScreenRecoveryUI, with
a dropped access level from public to protected.
Due to the dependency on recovery / librecovery refactoring, will add
testcases in follow-up CLs.
Test: Build and boot into recovery image. Check the menus (main menu,
'View recovery logs', 'Wipe data/factory reset').
Change-Id: Ie17aa78144871a12affd6f9075e045f76608a0ba
2018-04-20 18:24:58 +02:00
|
|
|
std::bind(&Device::HandleMenuKey, device, std::placeholders::_1, std::placeholders::_2));
|
2018-05-22 21:08:35 +02:00
|
|
|
// Handle Interrupt key
|
|
|
|
if (chosen_item == static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED)) {
|
|
|
|
return Device::KEY_INTERRUPTED;
|
|
|
|
}
|
2017-03-07 23:57:04 +01:00
|
|
|
// Device-specific code may take some action here. It may return one of the core actions
|
|
|
|
// handled in the switch statement below.
|
2018-05-22 21:08:35 +02:00
|
|
|
Device::BuiltinAction chosen_action =
|
|
|
|
(chosen_item == static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT))
|
|
|
|
? Device::REBOOT
|
|
|
|
: device->InvokeMenuItem(chosen_item);
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2017-03-07 23:57:04 +01:00
|
|
|
switch (chosen_action) {
|
recovery: report compliant reboot reason (Part Deux)
shutdown and reboot should have a corresponding sub-reason.
Adding:
"reboot,userrequested,fastboot"
"reboot,userrequested,recovery"
"reboot,userrequested,recovery,ui"
"shutdown,userrequested,fastboot"
"shutdown,userrequested,recovery"
"reboot,unknown#" (Can't happen, debug)
Test: manual, multiple targets, enter recovery, be able to exit recovery
Bug: 133326470
Change-Id: Ibfcb2a23158e8e99922e8053edd815fb592150f2
2019-05-20 19:36:16 +02:00
|
|
|
case Device::REBOOT_FROM_FASTBOOT: // Can not happen
|
|
|
|
case Device::SHUTDOWN_FROM_FASTBOOT: // Can not happen
|
2017-03-07 23:57:04 +01:00
|
|
|
case Device::NO_ACTION:
|
|
|
|
break;
|
2014-04-01 22:20:23 +02:00
|
|
|
|
2019-04-23 20:46:25 +02:00
|
|
|
case Device::ENTER_FASTBOOT:
|
|
|
|
case Device::ENTER_RECOVERY:
|
2017-03-07 23:57:04 +01:00
|
|
|
case Device::REBOOT_BOOTLOADER:
|
2019-04-23 20:46:25 +02:00
|
|
|
case Device::REBOOT_FASTBOOT:
|
|
|
|
case Device::REBOOT_RECOVERY:
|
2019-04-20 00:22:15 +02:00
|
|
|
case Device::REBOOT_RESCUE:
|
2017-03-07 23:57:04 +01:00
|
|
|
return chosen_action;
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2020-01-28 02:37:39 +01:00
|
|
|
case Device::REBOOT:
|
|
|
|
case Device::SHUTDOWN:
|
2020-01-30 01:51:33 +01:00
|
|
|
if (!ui->IsTextVisible()) {
|
|
|
|
return Device::REBOOT;
|
|
|
|
}
|
|
|
|
// okay to reboot; no need to ask.
|
|
|
|
if (!update_in_progress) {
|
|
|
|
return Device::REBOOT;
|
|
|
|
}
|
|
|
|
// An update might have been failed. Ask if user really wants to reboot.
|
|
|
|
if (AskToReboot(device, chosen_action)) {
|
2020-01-28 02:37:39 +01:00
|
|
|
return Device::REBOOT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-03-07 23:57:04 +01:00
|
|
|
case Device::WIPE_DATA:
|
2019-04-13 01:22:15 +02:00
|
|
|
save_current_log = true;
|
2017-03-07 23:57:04 +01:00
|
|
|
if (ui->IsTextVisible()) {
|
|
|
|
if (ask_to_wipe_data(device)) {
|
2019-04-13 01:22:15 +02:00
|
|
|
WipeData(device, false);
|
2017-03-07 23:57:04 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-04-13 01:22:15 +02:00
|
|
|
WipeData(device, false);
|
2017-03-07 23:57:04 +01:00
|
|
|
return Device::NO_ACTION;
|
|
|
|
}
|
|
|
|
break;
|
2009-09-23 00:16:02 +02:00
|
|
|
|
2019-04-13 01:22:15 +02:00
|
|
|
case Device::WIPE_CACHE: {
|
|
|
|
save_current_log = true;
|
|
|
|
std::function<bool()> confirm_func = [&device]() {
|
|
|
|
return yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!");
|
|
|
|
};
|
|
|
|
WipeCache(ui, ui->IsTextVisible() ? confirm_func : nullptr);
|
2017-03-07 23:57:04 +01:00
|
|
|
if (!ui->IsTextVisible()) return Device::NO_ACTION;
|
|
|
|
break;
|
2019-04-13 01:22:15 +02:00
|
|
|
}
|
2019-04-20 00:22:15 +02:00
|
|
|
|
2017-03-07 23:57:04 +01:00
|
|
|
case Device::APPLY_ADB_SIDELOAD:
|
2019-04-16 23:22:25 +02:00
|
|
|
case Device::APPLY_SDCARD:
|
|
|
|
case Device::ENTER_RESCUE: {
|
2019-04-13 01:22:15 +02:00
|
|
|
save_current_log = true;
|
2019-04-16 23:22:25 +02:00
|
|
|
|
2020-01-30 01:51:33 +01:00
|
|
|
update_in_progress = true;
|
|
|
|
WriteUpdateInProgress();
|
|
|
|
|
2019-04-16 23:22:25 +02:00
|
|
|
bool adb = true;
|
2019-04-20 00:22:15 +02:00
|
|
|
Device::BuiltinAction reboot_action;
|
2019-04-16 23:22:25 +02:00
|
|
|
if (chosen_action == Device::ENTER_RESCUE) {
|
|
|
|
// Switch to graphics screen.
|
|
|
|
ui->ShowText(false);
|
2019-04-22 21:18:14 +02:00
|
|
|
status = ApplyFromAdb(device, true /* rescue_mode */, &reboot_action);
|
2019-04-16 23:22:25 +02:00
|
|
|
} else if (chosen_action == Device::APPLY_ADB_SIDELOAD) {
|
2019-04-22 21:18:14 +02:00
|
|
|
status = ApplyFromAdb(device, false /* rescue_mode */, &reboot_action);
|
2019-03-18 23:03:33 +01:00
|
|
|
} else {
|
2019-04-16 23:22:25 +02:00
|
|
|
adb = false;
|
2019-06-12 01:09:07 +02:00
|
|
|
status = ApplyFromSdcard(device);
|
2019-03-18 23:03:33 +01:00
|
|
|
}
|
2016-04-15 01:49:04 +02:00
|
|
|
|
2019-04-20 00:22:15 +02:00
|
|
|
ui->Print("\nInstall from %s completed with status %d.\n", adb ? "ADB" : "SD card", status);
|
|
|
|
if (status == INSTALL_REBOOT) {
|
|
|
|
return reboot_action;
|
|
|
|
}
|
|
|
|
|
2020-01-30 01:51:33 +01:00
|
|
|
if (status == INSTALL_SUCCESS) {
|
|
|
|
update_in_progress = false;
|
|
|
|
if (!ui->IsTextVisible()) {
|
|
|
|
return Device::NO_ACTION; // reboot if logs aren't visible
|
|
|
|
}
|
|
|
|
} else {
|
2019-03-18 23:03:33 +01:00
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
ui->Print("Installation aborted.\n");
|
2019-05-15 22:59:39 +02:00
|
|
|
copy_logs(save_current_log);
|
2017-03-07 23:57:04 +01:00
|
|
|
}
|
|
|
|
break;
|
2019-03-18 23:03:33 +01:00
|
|
|
}
|
2015-07-30 23:43:27 +02:00
|
|
|
|
2017-03-07 23:57:04 +01:00
|
|
|
case Device::VIEW_RECOVERY_LOGS:
|
|
|
|
choose_recovery_file(device);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Device::RUN_GRAPHICS_TEST:
|
2019-05-15 22:59:39 +02:00
|
|
|
run_graphics_test(ui);
|
2017-03-07 23:57:04 +01:00
|
|
|
break;
|
|
|
|
|
2017-09-21 02:53:46 +02:00
|
|
|
case Device::RUN_LOCALE_TEST: {
|
|
|
|
ScreenRecoveryUI* screen_ui = static_cast<ScreenRecoveryUI*>(ui);
|
2018-05-08 07:50:33 +02:00
|
|
|
screen_ui->CheckBackgroundTextImages();
|
2017-09-21 02:53:46 +02:00
|
|
|
break;
|
|
|
|
}
|
2019-12-09 10:26:05 +01:00
|
|
|
|
2017-03-07 23:57:04 +01:00
|
|
|
case Device::MOUNT_SYSTEM:
|
2019-12-09 10:26:05 +01:00
|
|
|
// For Virtual A/B, set up the snapshot devices (if exist).
|
|
|
|
if (!CreateSnapshotPartitions()) {
|
|
|
|
ui->Print("Virtual A/B: snapshot partitions creation failed.\n");
|
|
|
|
break;
|
|
|
|
}
|
2019-05-10 19:40:59 +02:00
|
|
|
if (ensure_path_mounted_at(android::fs_mgr::GetSystemRoot(), "/mnt/system") != -1) {
|
2018-11-26 23:59:09 +01:00
|
|
|
ui->Print("Mounted /system.\n");
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2017-03-07 23:57:04 +01:00
|
|
|
break;
|
2018-05-22 21:08:35 +02:00
|
|
|
|
|
|
|
case Device::KEY_INTERRUPTED:
|
|
|
|
return Device::KEY_INTERRUPTED;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2017-03-07 23:57:04 +01:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2016-10-13 21:46:38 +02:00
|
|
|
static void print_property(const char* key, const char* name, void* /* cookie */) {
|
|
|
|
printf("%s=%s\n", key, name);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
|
|
|
|
2019-09-28 08:32:00 +02:00
|
|
|
static bool IsBatteryOk(int* required_battery_level) {
|
|
|
|
// GmsCore enters recovery mode to install package when having enough battery percentage.
|
|
|
|
// Normally, the threshold is 40% without charger and 20% with charger. So we check the battery
|
|
|
|
// level against a slightly lower limit.
|
|
|
|
constexpr int BATTERY_OK_PERCENTAGE = 20;
|
|
|
|
constexpr int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15;
|
|
|
|
|
|
|
|
auto battery_info = GetBatteryInfo();
|
|
|
|
*required_battery_level =
|
|
|
|
battery_info.charging ? BATTERY_WITH_CHARGER_OK_PERCENTAGE : BATTERY_OK_PERCENTAGE;
|
|
|
|
return battery_info.capacity >= *required_battery_level;
|
2016-02-17 21:21:52 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
// Set the retry count to |retry_count| in BCB.
|
2017-05-17 03:07:31 +02:00
|
|
|
static void set_retry_bootloader_message(int retry_count, const std::vector<std::string>& args) {
|
|
|
|
std::vector<std::string> options;
|
|
|
|
for (const auto& arg : args) {
|
|
|
|
if (!android::base::StartsWith(arg, "--retry_count")) {
|
|
|
|
options.push_back(arg);
|
2016-02-06 03:25:58 +01:00
|
|
|
}
|
2017-05-17 03:07:31 +02:00
|
|
|
}
|
2016-02-06 03:25:58 +01:00
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
// Update the retry counter in BCB.
|
|
|
|
options.push_back(android::base::StringPrintf("--retry_count=%d", retry_count));
|
2017-05-17 03:07:31 +02:00
|
|
|
std::string err;
|
|
|
|
if (!update_bootloader_message(options, &err)) {
|
|
|
|
LOG(ERROR) << err;
|
|
|
|
}
|
2016-02-06 03:25:58 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 23:04:08 +02:00
|
|
|
static bool bootreason_in_blacklist() {
|
2017-01-09 07:45:47 +01:00
|
|
|
std::string bootreason = android::base::GetProperty("ro.boot.bootreason", "");
|
|
|
|
if (!bootreason.empty()) {
|
2018-04-30 08:38:59 +02:00
|
|
|
// More bootreasons can be found in "system/core/bootstat/bootstat.cpp".
|
|
|
|
static const std::vector<std::string> kBootreasonBlacklist{
|
|
|
|
"kernel_panic",
|
|
|
|
"Panic",
|
|
|
|
};
|
|
|
|
for (const auto& str : kBootreasonBlacklist) {
|
|
|
|
if (android::base::EqualsIgnoreCase(str, bootreason)) return true;
|
2016-07-11 23:04:08 +02:00
|
|
|
}
|
2017-01-09 07:45:47 +01:00
|
|
|
}
|
|
|
|
return false;
|
2016-07-11 23:04:08 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 03:59:40 +02:00
|
|
|
static void log_failure_code(ErrorCode code, const std::string& update_package) {
|
|
|
|
std::vector<std::string> log_buffer = {
|
|
|
|
update_package,
|
|
|
|
"0", // install result
|
|
|
|
"error: " + std::to_string(code),
|
|
|
|
};
|
|
|
|
std::string log_content = android::base::Join(log_buffer, "\n");
|
|
|
|
const std::string& install_file = Paths::Get().temporary_install_file();
|
|
|
|
if (!android::base::WriteStringToFile(log_content, install_file)) {
|
|
|
|
PLOG(ERROR) << "Failed to write " << install_file;
|
|
|
|
}
|
2016-07-11 23:04:08 +02:00
|
|
|
|
2018-04-26 03:59:40 +02:00
|
|
|
// Also write the info into last_log.
|
|
|
|
LOG(INFO) << log_content;
|
2016-07-11 23:04:08 +02:00
|
|
|
}
|
|
|
|
|
2018-05-04 20:24:10 +02:00
|
|
|
Device::BuiltinAction start_recovery(Device* device, const std::vector<std::string>& args) {
|
2018-04-27 19:44:04 +02:00
|
|
|
static constexpr struct option OPTIONS[] = {
|
2018-07-28 07:09:12 +02:00
|
|
|
{ "fastboot", no_argument, nullptr, 0 },
|
2019-06-25 22:59:39 +02:00
|
|
|
{ "install_with_fuse", no_argument, nullptr, 0 },
|
2018-04-27 19:44:04 +02:00
|
|
|
{ "just_exit", no_argument, nullptr, 'x' },
|
|
|
|
{ "locale", required_argument, nullptr, 0 },
|
|
|
|
{ "prompt_and_wipe_data", no_argument, nullptr, 0 },
|
|
|
|
{ "reason", required_argument, nullptr, 0 },
|
2019-04-23 20:46:25 +02:00
|
|
|
{ "rescue", no_argument, nullptr, 0 },
|
2018-04-27 19:44:04 +02:00
|
|
|
{ "retry_count", required_argument, nullptr, 0 },
|
|
|
|
{ "security", no_argument, nullptr, 0 },
|
|
|
|
{ "show_text", no_argument, nullptr, 't' },
|
|
|
|
{ "shutdown_after", no_argument, nullptr, 0 },
|
|
|
|
{ "sideload", no_argument, nullptr, 0 },
|
|
|
|
{ "sideload_auto_reboot", no_argument, nullptr, 0 },
|
|
|
|
{ "update_package", required_argument, nullptr, 0 },
|
|
|
|
{ "wipe_ab", no_argument, nullptr, 0 },
|
|
|
|
{ "wipe_cache", no_argument, nullptr, 0 },
|
|
|
|
{ "wipe_data", no_argument, nullptr, 0 },
|
|
|
|
{ "wipe_package_size", required_argument, nullptr, 0 },
|
|
|
|
{ nullptr, 0, nullptr, 0 },
|
|
|
|
};
|
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
const char* update_package = nullptr;
|
2019-06-25 22:59:39 +02:00
|
|
|
bool install_with_fuse = false; // memory map the update package by default.
|
2017-11-29 02:23:06 +01:00
|
|
|
bool should_wipe_data = false;
|
|
|
|
bool should_prompt_and_wipe_data = false;
|
|
|
|
bool should_wipe_cache = false;
|
|
|
|
bool should_wipe_ab = false;
|
|
|
|
size_t wipe_package_size = 0;
|
|
|
|
bool sideload = false;
|
|
|
|
bool sideload_auto_reboot = false;
|
2019-04-23 20:46:25 +02:00
|
|
|
bool rescue = false;
|
2017-11-29 02:23:06 +01:00
|
|
|
bool just_exit = false;
|
|
|
|
bool shutdown_after = false;
|
|
|
|
int retry_count = 0;
|
|
|
|
bool security_update = false;
|
2018-05-03 02:15:03 +02:00
|
|
|
std::string locale;
|
2017-11-29 02:23:06 +01:00
|
|
|
|
2018-07-17 07:09:59 +02:00
|
|
|
auto args_to_parse = StringVectorToNullTerminatedArray(args);
|
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
int arg;
|
|
|
|
int option_index;
|
2018-07-17 07:09:59 +02:00
|
|
|
// Parse everything before the last element (which must be a nullptr). getopt_long(3) expects a
|
|
|
|
// null-terminated char* array, but without counting null as an arg (i.e. argv[argc] should be
|
|
|
|
// nullptr).
|
|
|
|
while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
|
2017-11-29 02:23:06 +01:00
|
|
|
&option_index)) != -1) {
|
|
|
|
switch (arg) {
|
|
|
|
case 't':
|
2018-05-04 20:24:10 +02:00
|
|
|
// Handled in recovery_main.cpp
|
2017-11-29 02:23:06 +01:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
just_exit = true;
|
|
|
|
break;
|
|
|
|
case 0: {
|
|
|
|
std::string option = OPTIONS[option_index].name;
|
2020-01-27 18:29:02 +01:00
|
|
|
if (option == "install_with_fuse") {
|
2019-06-25 22:59:39 +02:00
|
|
|
install_with_fuse = true;
|
2019-07-29 23:21:49 +02:00
|
|
|
} else if (option == "locale" || option == "fastboot" || option == "reason") {
|
2018-05-04 20:24:10 +02:00
|
|
|
// Handled in recovery_main.cpp
|
2018-04-27 19:44:04 +02:00
|
|
|
} else if (option == "prompt_and_wipe_data") {
|
|
|
|
should_prompt_and_wipe_data = true;
|
2019-04-23 20:46:25 +02:00
|
|
|
} else if (option == "rescue") {
|
|
|
|
rescue = true;
|
2018-04-27 19:44:04 +02:00
|
|
|
} else if (option == "retry_count") {
|
|
|
|
android::base::ParseInt(optarg, &retry_count, 0);
|
|
|
|
} else if (option == "security") {
|
|
|
|
security_update = true;
|
|
|
|
} else if (option == "sideload") {
|
|
|
|
sideload = true;
|
|
|
|
} else if (option == "sideload_auto_reboot") {
|
|
|
|
sideload = true;
|
|
|
|
sideload_auto_reboot = true;
|
|
|
|
} else if (option == "shutdown_after") {
|
|
|
|
shutdown_after = true;
|
|
|
|
} else if (option == "update_package") {
|
|
|
|
update_package = optarg;
|
|
|
|
} else if (option == "wipe_ab") {
|
2017-11-29 02:23:06 +01:00
|
|
|
should_wipe_ab = true;
|
2018-04-27 19:44:04 +02:00
|
|
|
} else if (option == "wipe_cache") {
|
|
|
|
should_wipe_cache = true;
|
|
|
|
} else if (option == "wipe_data") {
|
|
|
|
should_wipe_data = true;
|
2017-11-29 02:23:06 +01:00
|
|
|
} else if (option == "wipe_package_size") {
|
|
|
|
android::base::ParseUint(optarg, &wipe_package_size);
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2017-11-29 02:23:06 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '?':
|
|
|
|
LOG(ERROR) << "Invalid command argument";
|
|
|
|
continue;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2017-11-29 02:23:06 +01:00
|
|
|
}
|
2018-05-17 21:54:41 +02:00
|
|
|
optind = 1;
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2019-07-29 23:21:49 +02:00
|
|
|
printf("stage is [%s]\n", device->GetStage().value_or("").c_str());
|
|
|
|
printf("reason is [%s]\n", device->GetReason().value_or("").c_str());
|
2012-08-23 02:26:40 +02:00
|
|
|
|
2019-05-15 22:59:39 +02:00
|
|
|
auto ui = device->GetUI();
|
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
// Set background string to "installing security update" for security update,
|
|
|
|
// otherwise set it to "installing system update".
|
|
|
|
ui->SetSystemUpdateText(security_update);
|
2013-11-25 22:53:25 +01:00
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
int st_cur, st_max;
|
2019-07-29 23:21:49 +02:00
|
|
|
if (!device->GetStage().has_value() &&
|
|
|
|
sscanf(device->GetStage().value().c_str(), "%d/%d", &st_cur, &st_max) == 2) {
|
2017-11-29 02:23:06 +01:00
|
|
|
ui->SetStage(st_cur, st_max);
|
|
|
|
}
|
2013-11-25 22:53:25 +01:00
|
|
|
|
2018-05-07 20:21:10 +02:00
|
|
|
std::vector<std::string> title_lines =
|
2020-05-25 11:14:53 +02:00
|
|
|
android::base::Split(android::base::GetProperty("ro.build.fingerprint", ""), ":");
|
2018-05-07 20:21:10 +02:00
|
|
|
title_lines.insert(std::begin(title_lines), "Android Recovery");
|
|
|
|
ui->SetTitle(title_lines);
|
|
|
|
|
2018-05-22 21:08:35 +02:00
|
|
|
ui->ResetKeyInterruptStatus();
|
2017-11-29 02:23:06 +01:00
|
|
|
device->StartRecovery();
|
2010-02-02 00:59:12 +01:00
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
printf("Command:");
|
|
|
|
for (const auto& arg : args) {
|
|
|
|
printf(" \"%s\"", arg.c_str());
|
|
|
|
}
|
|
|
|
printf("\n\n");
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
property_list(print_property, nullptr);
|
|
|
|
printf("\n");
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2019-04-30 08:48:02 +02:00
|
|
|
InstallResult status = INSTALL_SUCCESS;
|
2019-04-20 00:22:15 +02:00
|
|
|
// next_action indicates the next target to reboot into upon finishing the install. It could be
|
|
|
|
// overridden to a different reboot target per user request.
|
|
|
|
Device::BuiltinAction next_action = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
if (update_package != nullptr) {
|
|
|
|
// It's not entirely true that we will modify the flash. But we want
|
|
|
|
// to log the update attempt since update_package is non-NULL.
|
2019-04-13 01:22:15 +02:00
|
|
|
save_current_log = true;
|
2016-04-30 00:37:05 +02:00
|
|
|
|
2019-09-28 08:32:00 +02:00
|
|
|
if (int required_battery_level; retry_count == 0 && !IsBatteryOk(&required_battery_level)) {
|
2018-04-26 19:40:36 +02:00
|
|
|
ui->Print("battery capacity is not enough for installing package: %d%% needed\n",
|
|
|
|
required_battery_level);
|
2019-09-28 08:32:00 +02:00
|
|
|
// Log the error code to last_install when installation skips due to low battery.
|
2017-11-29 02:23:06 +01:00
|
|
|
log_failure_code(kLowBattery, update_package);
|
|
|
|
status = INSTALL_SKIPPED;
|
2018-03-26 23:32:11 +02:00
|
|
|
} else if (retry_count == 0 && bootreason_in_blacklist()) {
|
2017-11-29 02:23:06 +01:00
|
|
|
// Skip update-on-reboot when bootreason is kernel_panic or similar
|
|
|
|
ui->Print("bootreason is in the blacklist; skip OTA installation\n");
|
|
|
|
log_failure_code(kBootreasonInBlacklist, update_package);
|
|
|
|
status = INSTALL_SKIPPED;
|
|
|
|
} else {
|
|
|
|
// It's a fresh update. Initialize the retry_count in the BCB to 1; therefore we can later
|
|
|
|
// identify the interrupted update due to unexpected reboots.
|
|
|
|
if (retry_count == 0) {
|
|
|
|
set_retry_bootloader_message(retry_count + 1, args);
|
|
|
|
}
|
|
|
|
|
2019-08-06 21:32:05 +02:00
|
|
|
bool should_use_fuse = false;
|
|
|
|
if (!SetupPackageMount(update_package, &should_use_fuse)) {
|
|
|
|
LOG(INFO) << "Failed to set up the package access, skipping installation";
|
|
|
|
status = INSTALL_ERROR;
|
|
|
|
} else if (install_with_fuse || should_use_fuse) {
|
2019-06-25 22:59:39 +02:00
|
|
|
LOG(INFO) << "Installing package " << update_package << " with fuse";
|
|
|
|
status = InstallWithFuseFromPath(update_package, ui);
|
|
|
|
} else if (auto memory_package = Package::CreateMemoryPackage(
|
|
|
|
update_package,
|
|
|
|
std::bind(&RecoveryUI::SetProgress, ui, std::placeholders::_1));
|
|
|
|
memory_package != nullptr) {
|
|
|
|
status = InstallPackage(memory_package.get(), update_package, should_wipe_cache,
|
|
|
|
retry_count, ui);
|
|
|
|
} else {
|
|
|
|
// We may fail to memory map the package on 32 bit builds for packages with 2GiB+ size.
|
|
|
|
// In such cases, we will try to install the package with fuse. This is not the default
|
|
|
|
// installation method because it introduces a layer of indirection from the kernel space.
|
|
|
|
LOG(WARNING) << "Failed to memory map package " << update_package
|
|
|
|
<< "; falling back to install with fuse";
|
|
|
|
status = InstallWithFuseFromPath(update_package, ui);
|
|
|
|
}
|
2017-11-29 02:23:06 +01:00
|
|
|
if (status != INSTALL_SUCCESS) {
|
|
|
|
ui->Print("Installation aborted.\n");
|
2018-04-30 08:38:59 +02:00
|
|
|
|
|
|
|
// When I/O error or bspatch/imgpatch error happens, reboot and retry installation
|
|
|
|
// RETRY_LIMIT times before we abandon this OTA update.
|
|
|
|
static constexpr int RETRY_LIMIT = 4;
|
2017-11-29 02:23:06 +01:00
|
|
|
if (status == INSTALL_RETRY && retry_count < RETRY_LIMIT) {
|
2019-05-15 22:59:39 +02:00
|
|
|
copy_logs(save_current_log);
|
2017-11-29 02:23:06 +01:00
|
|
|
retry_count += 1;
|
|
|
|
set_retry_bootloader_message(retry_count, args);
|
|
|
|
// Print retry count on screen.
|
|
|
|
ui->Print("Retry attempt %d\n", retry_count);
|
|
|
|
|
2019-04-29 20:23:16 +02:00
|
|
|
// Reboot back into recovery to retry the update.
|
2020-03-13 22:25:02 +01:00
|
|
|
Reboot("recovery");
|
2015-03-30 18:43:49 +02:00
|
|
|
}
|
2017-11-29 02:23:06 +01:00
|
|
|
// If this is an eng or userdebug build, then automatically
|
|
|
|
// turn the text display on if the script fails so the error
|
|
|
|
// message is visible.
|
2019-05-15 22:59:39 +02:00
|
|
|
if (IsRoDebuggable()) {
|
2017-11-29 02:23:06 +01:00
|
|
|
ui->ShowText(true);
|
2015-03-30 18:43:49 +02:00
|
|
|
}
|
2017-07-25 18:52:36 +02:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|
2017-11-29 02:23:06 +01:00
|
|
|
} else if (should_wipe_data) {
|
2019-04-13 01:22:15 +02:00
|
|
|
save_current_log = true;
|
2019-07-29 23:21:49 +02:00
|
|
|
CHECK(device->GetReason().has_value());
|
|
|
|
bool convert_fbe = device->GetReason().value() == "convert_fbe";
|
2019-04-13 01:22:15 +02:00
|
|
|
if (!WipeData(device, convert_fbe)) {
|
2017-11-29 02:23:06 +01:00
|
|
|
status = INSTALL_ERROR;
|
|
|
|
}
|
|
|
|
} else if (should_prompt_and_wipe_data) {
|
2018-09-17 23:32:47 +02:00
|
|
|
// Trigger the logging to capture the cause, even if user chooses to not wipe data.
|
2019-04-13 01:22:15 +02:00
|
|
|
save_current_log = true;
|
2018-09-17 23:32:47 +02:00
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
ui->ShowText(true);
|
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
2018-09-12 19:25:01 +02:00
|
|
|
status = prompt_and_wipe_data(device);
|
|
|
|
if (status != INSTALL_KEY_INTERRUPTED) {
|
|
|
|
ui->ShowText(false);
|
2017-11-29 02:23:06 +01:00
|
|
|
}
|
|
|
|
} else if (should_wipe_cache) {
|
2019-04-13 01:22:15 +02:00
|
|
|
save_current_log = true;
|
|
|
|
if (!WipeCache(ui, nullptr)) {
|
2017-11-29 02:23:06 +01:00
|
|
|
status = INSTALL_ERROR;
|
|
|
|
}
|
|
|
|
} else if (should_wipe_ab) {
|
2019-04-27 07:56:56 +02:00
|
|
|
if (!WipeAbDevice(device, wipe_package_size)) {
|
2017-11-29 02:23:06 +01:00
|
|
|
status = INSTALL_ERROR;
|
|
|
|
}
|
|
|
|
} else if (sideload) {
|
2019-04-20 00:22:15 +02:00
|
|
|
// 'adb reboot sideload' acts the same as user presses key combinations to enter the sideload
|
|
|
|
// mode. When 'sideload-auto-reboot' is used, text display will NOT be turned on by default. And
|
|
|
|
// it will reboot after sideload finishes even if there are errors. This is to enable automated
|
2017-11-29 02:23:06 +01:00
|
|
|
// testing.
|
2019-04-13 01:22:15 +02:00
|
|
|
save_current_log = true;
|
2017-11-29 02:23:06 +01:00
|
|
|
if (!sideload_auto_reboot) {
|
|
|
|
ui->ShowText(true);
|
|
|
|
}
|
2019-04-22 21:18:14 +02:00
|
|
|
status = ApplyFromAdb(device, false /* rescue_mode */, &next_action);
|
2017-11-29 02:23:06 +01:00
|
|
|
ui->Print("\nInstall from ADB complete (status: %d).\n", status);
|
|
|
|
if (sideload_auto_reboot) {
|
2019-04-20 00:22:15 +02:00
|
|
|
status = INSTALL_REBOOT;
|
2017-11-29 02:23:06 +01:00
|
|
|
ui->Print("Rebooting automatically.\n");
|
|
|
|
}
|
2019-04-23 20:46:25 +02:00
|
|
|
} else if (rescue) {
|
|
|
|
save_current_log = true;
|
2019-04-22 21:18:14 +02:00
|
|
|
status = ApplyFromAdb(device, true /* rescue_mode */, &next_action);
|
2019-04-23 20:46:25 +02:00
|
|
|
ui->Print("\nInstall from ADB complete (status: %d).\n", status);
|
2017-11-29 02:23:06 +01:00
|
|
|
} else if (!just_exit) {
|
|
|
|
// If this is an eng or userdebug build, automatically turn on the text display if no command
|
|
|
|
// is specified. Note that this should be called before setting the background to avoid
|
|
|
|
// flickering the background image.
|
2019-05-15 22:59:39 +02:00
|
|
|
if (IsRoDebuggable()) {
|
2017-11-29 02:23:06 +01:00
|
|
|
ui->ShowText(true);
|
|
|
|
}
|
|
|
|
status = INSTALL_NONE; // No command specified
|
|
|
|
ui->SetBackground(RecoveryUI::NO_COMMAND);
|
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {
|
|
|
|
ui->SetBackground(RecoveryUI::ERROR);
|
|
|
|
if (!ui->IsTextVisible()) {
|
|
|
|
sleep(5);
|
2012-08-23 02:26:40 +02:00
|
|
|
}
|
2017-11-29 02:23:06 +01:00
|
|
|
}
|
2015-03-30 18:43:49 +02:00
|
|
|
|
2019-04-20 00:22:15 +02:00
|
|
|
// Determine the next action.
|
|
|
|
// - If the state is INSTALL_REBOOT, device will reboot into the target as specified in
|
|
|
|
// `next_action`.
|
|
|
|
// - If the recovery menu is visible, prompt and wait for commands.
|
|
|
|
// - If the state is INSTALL_NONE, wait for commands (e.g. in user build, one manually boots
|
|
|
|
// into recovery to sideload a package or to wipe the device).
|
|
|
|
// - In all other cases, reboot the device. Therefore, normal users will observe the device
|
|
|
|
// rebooting a) immediately upon successful finish (INSTALL_SUCCESS); or b) an "error" screen
|
|
|
|
// for 5s followed by an automatic reboot.
|
|
|
|
if (status != INSTALL_REBOOT) {
|
|
|
|
if (status == INSTALL_NONE || ui->IsTextVisible()) {
|
2019-04-30 08:48:02 +02:00
|
|
|
auto temp = PromptAndWait(device, status);
|
2019-04-20 00:22:15 +02:00
|
|
|
if (temp != Device::NO_ACTION) {
|
|
|
|
next_action = temp;
|
|
|
|
}
|
2010-09-15 20:08:23 +02:00
|
|
|
}
|
2017-11-29 02:23:06 +01:00
|
|
|
}
|
2009-03-04 04:28:42 +01:00
|
|
|
|
2017-11-29 02:23:06 +01:00
|
|
|
// Save logs and clean up before rebooting or shutting down.
|
2019-05-15 22:59:39 +02:00
|
|
|
FinishRecovery(ui);
|
2014-04-01 22:20:23 +02:00
|
|
|
|
2019-04-20 00:22:15 +02:00
|
|
|
return next_action;
|
2009-03-04 04:28:42 +01:00
|
|
|
}
|