From aed1bdfe59fddd8ee59ff6bbfbcdabf89df808ff Mon Sep 17 00:00:00 2001 From: bigbiff Date: Tue, 3 Aug 2021 18:21:39 -0400 Subject: [PATCH] get_args: process /data/cache/command Change-Id: I744cdc5a72f53fd1224de242e30ff261e6104900 --- etc/init.rc | 2 +- install/get_args.cpp | 19 +------------------ openrecoveryscript.cpp | 2 +- partition.cpp | 10 +++++++++- partitionmanager.cpp | 23 ++++++++++++++++++++++- partitions.hpp | 3 ++- startupArgs.cpp | 20 +++++++++++++------- startupArgs.hpp | 1 + twrpinstall/include/twinstall/get_args.h | 1 - 9 files changed, 50 insertions(+), 31 deletions(-) diff --git a/etc/init.rc b/etc/init.rc index 362687f9..9c7d3483 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -41,7 +41,7 @@ on init mkdir /system mkdir /data - mkdir /cache + symlink /data/cache /cache mkdir /sideload mkdir /mnt/system mount tmpfs tmpfs /tmp diff --git a/install/get_args.cpp b/install/get_args.cpp index c1fcc965..92f6b100 100755 --- a/install/get_args.cpp +++ b/install/get_args.cpp @@ -1,8 +1,6 @@ #include "twinstall/get_args.h" std::string stage; -bool has_cache = false; -static constexpr const char* COMMAND_FILE = "/cache/recovery/command"; // command line args come from, in decreasing precedence: // - the actual command line @@ -12,6 +10,7 @@ std::vector args::get_args(const int *argc, char*** const argv) { CHECK_GT(*argc, 0); bootloader_message boot = {}; + std::string err; if (!read_bootloader_message(&boot, &err)) { LOG(ERROR) << err; @@ -54,22 +53,6 @@ std::vector args::get_args(const int *argc, char*** const argv) { } } - // --- if that doesn't work, try the command file (if we have /cache). - if (args.size() == 1 && has_cache) { - std::string content; - if (ensure_path_mounted(COMMAND_FILE) == 0 && - android::base::ReadFileToString(COMMAND_FILE, &content)) { - std::vector tokens = android::base::Split(content, "\n"); - // All the arguments in COMMAND_FILE are needed (unlike the BCB message, - // COMMAND_FILE doesn't use filename as the first argument). - for (auto it = tokens.begin(); it != tokens.end(); it++) { - // Skip empty and '\0'-filled tokens. - if (!it->empty() && (*it)[0] != '\0') args.push_back(std::move(*it)); - } - LOG(INFO) << "Got " << args.size() << " arguments from " << COMMAND_FILE; - } - } - // Write the arguments (excluding the filename in args[0]) back into the // bootloader control block. So the device will always boot into recovery to // finish the pending work, until finish_recovery() is called. diff --git a/openrecoveryscript.cpp b/openrecoveryscript.cpp index 4d1b146f..c2ff0feb 100755 --- a/openrecoveryscript.cpp +++ b/openrecoveryscript.cpp @@ -1,5 +1,5 @@ /* - Copyright 2003 to 2017 TeamWin + Copyright 2003 to 2021 TeamWin This file is part of TWRP/TeamWin Recovery Project. TWRP is free software: you can redistribute it and/or modify diff --git a/partition.cpp b/partition.cpp index 09b79088..3efa9bfa 100755 --- a/partition.cpp +++ b/partition.cpp @@ -1,5 +1,5 @@ /* - Copyright 2013 to 2020 TeamWin + Copyright 2013 to 2021 TeamWin This file is part of TWRP/TeamWin Recovery Project. TWRP is free software: you can redistribute it and/or modify @@ -1779,6 +1779,14 @@ bool TWPartition::Wipe_AndSec(void) { return true; } +bool TWPartition::Wipe_Data_Cache(void) { + if (!Mount(true)) + return false; + gui_msg(Msg("wiping=Wiping {1}")(Mount_Point + "/cache/")); + TWFunc::removeDir(Mount_Point + "/cache/", true); + return true; +} + bool TWPartition::Can_Repair() { if (Mount_Read_Only) return false; diff --git a/partitionmanager.cpp b/partitionmanager.cpp index 00fd1c2d..5e98b062 100755 --- a/partitionmanager.cpp +++ b/partitionmanager.cpp @@ -1,5 +1,5 @@ /* - Copyright 2014 to 2020 TeamWin + Copyright 2014 to 2021 TeamWin This file is part of TWRP/TeamWin Recovery Project. TWRP is free software: you can redistribute it and/or modify @@ -63,6 +63,7 @@ #include "twcommon.h" #include "partitions.hpp" #include "data.hpp" +#include "startupArgs.hpp" #include "twrp-functions.hpp" #include "fixContexts.hpp" #include "exclude.hpp" @@ -1401,6 +1402,16 @@ int TWPartitionManager::Wipe_By_Path(string Path) { if (Local_Path == "/system") Local_Path = Get_Android_Root_Path(); + if (Path == "/cache") { + TWPartition* cache = Find_Partition_By_Path("/cache"); + if (cache == nullptr) { + TWPartition* dat = Find_Partition_By_Path("/data"); + if (dat) { + dat->Wipe_Data_Cache(); + found = true; + } + } + } // Iterate through all partitions for (iter = Partitions.begin(); iter != Partitions.end(); iter++) { if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) { @@ -1766,6 +1777,16 @@ void TWPartitionManager::Post_Decrypt(const string& Block_Device) { TWPartition* dat = Find_Partition_By_Path("/data"); if (dat != NULL) { + // reparse for /cache/recovery/command + static constexpr const char* COMMAND_FILE = "/data/cache/command"; + if (TWFunc::Path_Exists(COMMAND_FILE)) { + startupArgs startup; + std::string content; + TWFunc::read_file(COMMAND_FILE, content); + std::vector args = {content}; + startup.processRecoveryArgs(args, 0); + } + DataManager::SetValue(TW_IS_DECRYPTED, 1); dat->Is_Decrypted = true; if (!Block_Device.empty()) { diff --git a/partitions.hpp b/partitions.hpp index 2a8ae7d5..936d99b9 100755 --- a/partitions.hpp +++ b/partitions.hpp @@ -1,5 +1,5 @@ /* - Copyright 2014 to 2017 TeamWin + Copyright 2014 to 2021 TeamWin This file is part of TWRP/TeamWin Recovery Project. TWRP is free software: you can redistribute it and/or modify @@ -130,6 +130,7 @@ public: bool Wipe(string New_File_System); // Wipes the partition bool Wipe(); // Wipes the partition bool Wipe_AndSec(); // Wipes android secure + bool Wipe_Data_Cache(); // Wipe /data/cache with devices that have no cache partition bool Can_Repair(); // Checks to see if we have everything needed to be able to repair the current file system uint64_t Get_Max_FileSize(); // get partition maxFileSie bool Repair(); // Repairs the current file system diff --git a/startupArgs.cpp b/startupArgs.cpp index 4fe6ec0c..f385e27f 100755 --- a/startupArgs.cpp +++ b/startupArgs.cpp @@ -1,5 +1,5 @@ /* - Copyright 2012-2020 TeamWin + Copyright 2012-2021 TeamWin This file is part of TWRP/TeamWin Recovery Project. TWRP is free software: you can redistribute it and/or modify @@ -24,6 +24,13 @@ void startupArgs::parse(int *argc, char ***argv) { LOGINFO("Startup Commands: "); for (index = 1; index < args.size(); index++) { + if (!processRecoveryArgs(args, index)) + break; + } + printf("\n"); +} + +bool startupArgs::processRecoveryArgs(std::vector args, int index) { if (args[index].find(RESCUE_PARTY) != std::string::npos) { gui_print("\n\n"); gui_msg(Msg(msg::kError, "rescue_party0=Android Rescue Party trigger! Possible solutions? Either:")); @@ -53,7 +60,7 @@ void startupArgs::parse(int *argc, char ***argv) { std::string ORSCommand = "install " + arg; SkipDecryption = arg.find("@") == 1; if (!OpenRecoveryScript::Insert_ORS_Command(ORSCommand)) - break; + return false; } } else if (args[index].find(SEND_INTENT) != std::string::npos) { std::string::size_type eq_pos = args[index].find("="); @@ -65,17 +72,16 @@ void startupArgs::parse(int *argc, char ***argv) { } } else if (args[index].find(WIPE_DATA) != std::string::npos) { if (!OpenRecoveryScript::Insert_ORS_Command("wipe data\n")) - break; + return false; } else if (args[index].find(WIPE_CACHE) != std::string::npos) { if (!OpenRecoveryScript::Insert_ORS_Command("wipe cache\n")) - break; + return false; } else if (args[index].find(NANDROID) != std::string::npos) { DataManager::SetValue(TW_BACKUP_NAME, gui_parse_text("{@auto_generate}")); if (!OpenRecoveryScript::Insert_ORS_Command("backup BSDCAE\n")) - break; + return false; } - } - printf("\n"); + return true; } bool startupArgs::Should_Skip_Decryption() { diff --git a/startupArgs.hpp b/startupArgs.hpp index f01a411c..d388f55d 100755 --- a/startupArgs.hpp +++ b/startupArgs.hpp @@ -45,6 +45,7 @@ public: bool Should_Skip_Decryption(); std::string Get_Intent(); bool Get_Fastboot_Mode(); + bool processRecoveryArgs(std::vector args, int index); private: bool SkipDecryption = false; diff --git a/twrpinstall/include/twinstall/get_args.h b/twrpinstall/include/twinstall/get_args.h index 712b7905..fb9bb432 100755 --- a/twrpinstall/include/twinstall/get_args.h +++ b/twrpinstall/include/twinstall/get_args.h @@ -4,7 +4,6 @@ #include #include #include - #include "recovery_utils/roots.h" #include "bootloader_message/include/bootloader_message/bootloader_message.h"