Skip to content

Commit

Permalink
Add an option to one click flash current booted TWRP ramdisk
Browse files Browse the repository at this point in the history
Change-Id: I12303696698a5feaa43401bcac9e30d5b3b99a0c
  • Loading branch information
epicX67 committed Nov 4, 2021
1 parent 4979a37 commit 45e96a4
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 54 deletions.
4 changes: 3 additions & 1 deletion data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,8 @@ initSHRPVars(&mConst, &mData, &mPersist);
mPersist.SetValue(TW_TIME_ZONE_GUISEL, "CST6;CDT,M3.2.0,M11.1.0");
mPersist.SetValue(TW_TIME_ZONE_GUIOFFSET, "0");
mPersist.SetValue(TW_TIME_ZONE_GUIDST, "1");
mPersist.SetValue(TW_AUTO_REFLASHTWRP_VAR, "0");

mData.SetValue(TW_ACTION_BUSY, "0");
mData.SetValue("tw_wipe_cache", "0");
mData.SetValue("tw_wipe_dalvik", "0");
Expand Down Expand Up @@ -1200,4 +1202,4 @@ void DataManager::LoadTWRPFolderInfo(void)
string mainPath = GetCurrentStoragePath();
SetValue(TW_RECOVERY_FOLDER_VAR, TWFunc::Check_For_TwrpFolder());
mBackingFile = mainPath + GetStrValue(TW_RECOVERY_FOLDER_VAR) + '/' + TW_SETTINGS_FILE;
}
}
18 changes: 18 additions & 0 deletions gui/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ GUIAction::GUIAction(xml_node<>* node)
ADD_ACTION(installapp);
ADD_ACTION(uninstalltwrpsystemapp);
ADD_ACTION(repackimage);
ADD_ACTION(reflashtwrp);
ADD_ACTION(fixabrecoverybootloop);
ADD_ACTION(applycustomtwrpfolder);
#ifndef TW_EXCLUDE_NANO
Expand Down Expand Up @@ -2263,6 +2264,23 @@ int GUIAction::repackimage(std::string arg __unused)
return 0;
}

int GUIAction::reflashtwrp(std::string arg __unused)
{
int op_status = 1;
twrpRepacker repacker;

operation_start("Repack Image");
if (!simulate)
{
if (!repacker.Flash_Current_Twrp())
goto exit;
} else
simulate_progress_bar();
op_status = 0;
exit:
operation_end(op_status);
return 0;
}
int GUIAction::fixabrecoverybootloop(std::string arg __unused)
{
int op_status = 1;
Expand Down
1 change: 1 addition & 0 deletions gui/objects.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ class GUIAction : public GUIObject, public ActionObject
int installapp(std::string arg);
int uninstalltwrpsystemapp(std::string arg);
int repackimage(std::string arg);
int reflashtwrp(std::string arg);
int fixabrecoverybootloop(std::string arg);
int enableadb(std::string arg);
int enablefastboot(std::string arg);
Expand Down
6 changes: 6 additions & 0 deletions gui/theme/shrp_portrait_hdpi/languages/en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,12 @@
<string name="install_complete">Install Complete</string>
<string name="unpack_error">Error unpacking image.</string>
<string name="repack_error">Error repacking image.</string>
<string name="modified_ramdisk_error">ramdisk files have been modified, unable to create ramdisk to flash, fastboot boot twrp and try this option again or use the Install Recovery Ramdisk option.</string>
<string name="create_ramdisk_error">failed to create ramdisk to flash.</string>
<string name="reflash_twrp">Flash current TWRP</string>
<string name="reflash_twrp_confirm">Flash current TWRP?</string>
<string name="reflashing_twrp">Flashing TWRP...</string>
<string name="reflash_twrp_complete">Done flashing TWRP</string>
<string name="unpacking_image">Unpacking {1}...</string>
<string name="repacking_image">Repacking {1}...</string>
<string name="repack_image_hdr">Select Image</string>
Expand Down
7 changes: 7 additions & 0 deletions gui/theme/shrp_portrait_hdpi/pages/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@
<listitem name="{@zip_sig_chk=Zip signature verification}">
<data variable="tw_signed_zip_verify"/>
</listitem>
<listitem name="{@auto_reflashtwrp_chk=Automatically Reflash TWRP after flashing a ROM}">
<conditions>
<condition var1="tw_has_boot_slots" var2="1"/>
<condition var1="tw_has_repack_tools" var2="1"/>
</conditions>
<data variable="tw_auto_reflashtwrp"/>
</listitem>
</listbox>

<button style="c_queue">
Expand Down
7 changes: 7 additions & 0 deletions gui/theme/shrp_portrait_hdpi/pages/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@
<listitem name="{@unmount_sys_install=Unmount System before installing a ZIP}">
<data variable="tw_unmount_system"/>
</listitem>
<listitem name="{@auto_reflashtwrp_chk=Automatically Reflash TWRP after flashing a ROM}">
<conditions>
<condition var1="tw_has_boot_slots" var2="1"/>
<condition var1="tw_has_repack_tools" var2="1"/>
</conditions>
<data variable="tw_auto_reflashtwrp"/>
</listitem>
</listbox>

<button style="cnfrmBtn">
Expand Down
50 changes: 50 additions & 0 deletions gui/theme/shrp_portrait_hdpi/pages/tweaks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,56 @@
</action>
</page>

<page name="c_addons_native">
<template name="navCommon"/>
<template name="c_status_bar"/>

<text style="pageTitle">
<placement x="%c_hawk_base_column%" y="%rPos3%"/>
<text>{@c_tweaks=Tweaks}</text>
</text>
<text style="pageSubTitle">
<placement x="%c_hawk_base_column%" y="%rPos12%"/>
<text>{@c_tweaks_info=Collection of tweaks}</text>
</text>


<listbox style="customListbox">
<placement x="%c_hawk_base_column%" y="%rPos18%" w="%file_selector_default_w%" h="1100"/>
<icon selected="fileImg2" unselected="fileImg2"/>
<listitem name="{@reflash_twrp=Flash Current TWRP}">
<conditions>
<condition var1="tw_has_boot_slots" var2="1"/>
<condition var1="tw_has_repack_tools" var2="1"/>
</conditions>
<actions>
<action function="set">tw_repack_kernel=0</action>
<action function="set">tw_back=c_addons_native</action>
<action function="set">tw_action=reflashtwrp</action>
<action function="set">tw_text1={@reflash_twrp_confirm=Flash Current TWRP?}</action>
<action function="set">tw_action_text1={@reflashing_twrp=Flashing TWRP...}</action>
<action function="set">tw_complete_text1={@reflash_twrp_complete=Done Flashing TWRP}</action>
<action function="set">tw_slider_text={@swipe_to_confirm=Swipe to Confirm}</action>
<action function="page">confirm_action</action>
</actions>
</listitem>
</listbox>


<action>
<touch key="back"/>
<actions>
<action function="page">c_refresh</action>
</actions>
</action>
<action>
<touch key="home"/>
<actions>
<action function="page">c_refresh</action>
</actions>
</action>
</page>

<page name="tweakConfirm">
<template name="overlayPage"/>

Expand Down
50 changes: 46 additions & 4 deletions twrpRepacker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,14 @@ bool twrpRepacker::Repack_Image_And_Flash(const std::string& Target_Image, const
if (!Backup_Image_For_Repack(part, REPACK_ORIG_DIR, Repack_Options.Backup_First, gui_lookup("repack", "Repack")))
return false;
DataManager::SetProgress(.25);
gui_msg(Msg("unpacking_image=Unpacking {1}...")(Target_Image));
image_ramdisk_format = Unpack_Image(Target_Image, REPACK_NEW_DIR, true);
if (Repack_Options.Type == REPLACE_RAMDISK_UNPACKED) {
if (!Prepare_Empty_Folder(REPACK_NEW_DIR))
return false;
image_ramdisk_format = "gzip";
} else {
gui_msg(Msg("unpacking_image=Unpacking {1}...")(Target_Image));
image_ramdisk_format = Unpack_Image(Target_Image, REPACK_NEW_DIR, true);
}
if (image_ramdisk_format.empty())
return false;
DataManager::SetProgress(.5);
Expand All @@ -142,6 +148,16 @@ bool twrpRepacker::Repack_Image_And_Flash(const std::string& Target_Image, const
LOGERR("Failed to copy ramdisk\n");
return false;
}
} else if (Repack_Options.Type == REPLACE_RAMDISK_UNPACKED) {
if (TWFunc::copy_file(Target_Image, REPACK_ORIG_DIR "ramdisk.cpio", 0644)) {
LOGERR("Failed to copy ramdisk\n");
return false;
}
if (TWFunc::copy_file(Target_Image, REPACK_NEW_DIR "ramdisk.cpio", 0644)) {
LOGERR("Failed to copy ramdisk\n");
return false;
}
path = REPACK_ORIG_DIR;
} else if (Repack_Options.Type == REPLACE_RAMDISK) {
// Repack the ramdisk
if (TWFunc::copy_file(REPACK_NEW_DIR "ramdisk.cpio", REPACK_ORIG_DIR "ramdisk.cpio", 0644)) {
Expand Down Expand Up @@ -192,7 +208,7 @@ bool twrpRepacker::Repack_Image_And_Flash(const std::string& Target_Image, const
}
DataManager::SetProgress(1);
TWFunc::removeDir(REPACK_ORIG_DIR, false);
if (part->Is_SlotSelect() && Repack_Options.Type == REPLACE_RAMDISK) {
if (part->Is_SlotSelect()) { if (Repack_Options.Type == REPLACE_RAMDISK || Repack_Options.Type == REPLACE_RAMDISK_UNPACKED) {
LOGINFO("Switching slots to flash ramdisk to both partitions\n");
string Current_Slot = PartitionManager.Get_Active_Slot_Display();
if (Current_Slot == "A")
Expand Down Expand Up @@ -241,7 +257,33 @@ bool twrpRepacker::Repack_Image_And_Flash(const std::string& Target_Image, const
}
DataManager::SetProgress(1);
TWFunc::removeDir(REPACK_ORIG_DIR, false);
}
}}
TWFunc::removeDir(REPACK_NEW_DIR, false);
return true;
}

bool twrpRepacker::Flash_Current_Twrp() {
if (!TWFunc::Path_Exists("/ramdisk-files.txt")) {
LOGERR("can not find ramdisk-files.txt");
return false;
}
Repack_Options_struct Repack_Options;
Repack_Options.Disable_Verity = false;
Repack_Options.Disable_Force_Encrypt = false;
Repack_Options.Type = REPLACE_RAMDISK_UNPACKED;
Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
std::string verifyfiles = "cd / && sha256sum --status -c ramdisk-files.sha256sum";
if (TWFunc::Exec_Cmd(verifyfiles) != 0) {
gui_msg(Msg(msg::kError, "modified_ramdisk_error=ramdisk files have been modified, unable to create ramdisk to flash, fastboot boot twrp and try this option again or use the Install Recovery Ramdisk option."));
return false;
}
std::string command = "cd / && /system/bin/cpio -H newc -o < ramdisk-files.txt > /tmp/currentramdisk.cpio && /system/bin/gzip -f /tmp/currentramdisk.cpio";
if (TWFunc::Exec_Cmd(command) != 0) {
gui_msg(Msg(msg::kError, "create_ramdisk_error=failed to create ramdisk to flash."));
return false;
}
if (!Repack_Image_And_Flash("/tmp/currentramdisk.cpio.gz", Repack_Options))
return false;
else
return true;
}
98 changes: 50 additions & 48 deletions twrpRepacker.hpp
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
/*
Copyright 2014 to 2020 TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TWRP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/

#include <string>
#include "partitions.hpp"

#ifndef TWRP_REPACKER
#define TWRP_REPACKER

enum Repack_Type {
REPLACE_NONE = 0,
REPLACE_RAMDISK = 1,
REPLACE_KERNEL = 2,
};

struct Repack_Options_struct {
Repack_Type Type;
bool Backup_First;
bool Disable_Verity;
bool Disable_Force_Encrypt;
};

class twrpRepacker {
public:
bool Backup_Image_For_Repack(TWPartition* Part, const std::string& Temp_Folder_Destination, const bool Create_Backup, const std::string& Backup_Name); // Prepares an image for repacking by unpacking it to the temp folder destination
std::string Unpack_Image(const std::string& Source_Path, const std::string& Temp_Folder_Destination, const bool Copy_Source, const bool Create_Destination = true); // Prepares an image for repacking by unpacking it to the temp folder destination and return the ramdisk format
bool Repack_Image_And_Flash(const std::string& Target_Image, const struct Repack_Options_struct& Repack_Options); // Repacks the boot image with a new kernel or a new ramdisk
private:
bool Prepare_Empty_Folder(const std::string& Folder); // Creates an empty folder at Folder. If the folder already exists, the folder is deleted, then created
std::string original_ramdisk_format; // Ramdisk format of boot partition
std::string image_ramdisk_format; // Ramdisk format of boot image to repack from
};
#endif // TWRP_REPACKER
/*
Copyright 2014 to 2020 TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TWRP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/

#include <string>
#include "partitions.hpp"

#ifndef TWRP_REPACKER
#define TWRP_REPACKER

enum Repack_Type {
REPLACE_NONE = 0,
REPLACE_RAMDISK = 1,
REPLACE_KERNEL = 2,
REPLACE_RAMDISK_UNPACKED = 3,
};

struct Repack_Options_struct {
Repack_Type Type;
bool Backup_First;
bool Disable_Verity;
bool Disable_Force_Encrypt;
};

class twrpRepacker {
public:
bool Backup_Image_For_Repack(TWPartition* Part, const std::string& Temp_Folder_Destination, const bool Create_Backup, const std::string& Backup_Name); // Prepares an image for repacking by unpacking it to the temp folder destination
std::string Unpack_Image(const std::string& Source_Path, const std::string& Temp_Folder_Destination, const bool Copy_Source, const bool Create_Destination = true); // Prepares an image for repacking by unpacking it to the temp folder destination and return the ramdisk format
bool Repack_Image_And_Flash(const std::string& Target_Image, const struct Repack_Options_struct& Repack_Options); // Repacks the boot image with a new kernel or a new ramdisk
bool Flash_Current_Twrp();
private:
bool Prepare_Empty_Folder(const std::string& Folder); // Creates an empty folder at Folder. If the folder already exists, the folder is deleted, then created
std::string original_ramdisk_format; // Ramdisk format of boot partition
std::string image_ramdisk_format; // Ramdisk format of boot image to repack from
};
#endif // TWRP_REPACKER
8 changes: 7 additions & 1 deletion twrpinstall/twinstall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "gui/pages.hpp"
#include "twinstall.h"
#include "installcommand.h"
#include "../twrpRepacker.hpp"
extern "C" {
#include "gui/gui.h"
}
Expand Down Expand Up @@ -242,7 +243,7 @@ static int Run_Update_Binary(const char *path, int* wipe_cache, zip_type ztype)
}

int TWinstall_zip(const char* path, int* wipe_cache, bool check_for_digest) {
int ret_val, zip_verify = 1, unmount_system = 1;
int ret_val, zip_verify = 1, unmount_system = 1, reflashtwrp = 0;

gui_msg(Msg("installing_zip=Installing zip file '{1}'")(path));
if (strlen(path) < 9 || strncmp(path, "/sideload", 9) != 0) {
Expand Down Expand Up @@ -350,6 +351,11 @@ int TWinstall_zip(const char* path, int* wipe_cache, bool check_for_digest) {
gui_warn("mount_vab_partitions=Devices on super may not mount until rebooting recovery.");
}
gui_warn("flash_ab_reboot=To flash additional zips, please reboot recovery to switch to the updated slot.");
DataManager::GetValue(TW_AUTO_REFLASHTWRP_VAR, reflashtwrp);
if (reflashtwrp) {
twrpRepacker repacker;
repacker.Flash_Current_Twrp();
}
} else {
std::string binary_name("ui.xml");
ZipEntry binary_entry;
Expand Down
1 change: 1 addition & 0 deletions variables.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
#define TW_NO_SHA2 "tw_no_sha2"
#define TW_UNMOUNT_SYSTEM "tw_unmount_system"
#define TW_IS_SUPER "tw_is_super"
#define TW_AUTO_REFLASHTWRP_VAR "tw_auto_reflashtwrp"

#define TW_HAS_DEVICEAB "tw_has_deviceab"
#define TW_ACTIVE_SLOT_INSTALL "tw_active_slot_install"
Expand Down

0 comments on commit 45e96a4

Please sign in to comment.