Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 38bd760b authored by Dees_Troy's avatar Dees_Troy
Browse files

Add wipe functions, compiles in CM7, text shows in UI, zips install

Zip install works, had to move mincrypt code into TWRP to prevent
a crash when checking the zip signature.
Added wipe functions
Made it compile in CM7
Made text show up in console and logging
parent 32c8eb81
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -38,7 +38,8 @@ LOCAL_SRC_FILES += \
    mtdutils/mtdutils.c \
    twinstall.cpp \
    twmincrypt/twrsa.c \
    twmincrypt/twsha.c
    twmincrypt/twsha.c \
    twrp-functions.cpp

ifeq ($(TARGET_RECOVERY_REBOOT_SRC),)
  LOCAL_SRC_FILES += reboot.c
+29 −3
Original line number Diff line number Diff line
@@ -50,8 +50,6 @@ extern "C"

	void gui_notifyVarChange(const char *name, const char* value);

    int get_battery_level(void);

	int __system(const char *command);
}

@@ -761,8 +759,36 @@ int DataManager::GetMagicValue(const string varName, string& value)
    if (varName == "tw_battery")
    {
        char tmp[16];
		static char charging = ' ';
		static int lastVal = -1;
		static time_t nextSecCheck = 0;

		struct timeval curTime;
		gettimeofday(&curTime, NULL);
		if (curTime.tv_sec > nextSecCheck)
		{
			char cap_s[4];
			FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
			if (cap){
				fgets(cap_s, 4, cap);
				fclose(cap);
				lastVal = atoi(cap_s);
				if (lastVal > 100)  lastVal = 101;
				if (lastVal < 0)    lastVal = 0;
			}
			cap = fopen("/sys/class/power_supply/battery/status","rt");
			if (cap) {
				fgets(cap_s, 2, cap);
				fclose(cap);
				if (cap_s[0] == 'C')
					charging = '+';
				else
					charging = ' ';
			}
			nextSecCheck = curTime.tv_sec + 60;
		}

        sprintf(tmp, "%i%%", get_battery_level());
		sprintf(tmp, "%i%%%c", lastVal, charging);
        value = tmp;
        return 0;
    }
+0 −146
Original line number Diff line number Diff line
@@ -339,102 +339,6 @@ int usb_storage_disable(void)
    return 0;
}

void wipe_dalvik_cache()
{
        //ui_set_background(BACKGROUND_ICON_WIPE);
        ensure_path_mounted("/data");
        ensure_path_mounted("/cache");
        ui_print("\n-- Wiping Dalvik Cache Directories...\n");
        __system("rm -rf /data/dalvik-cache");
        ui_print("Cleaned: /data/dalvik-cache...\n");
        __system("rm -rf /cache/dalvik-cache");
        ui_print("Cleaned: /cache/dalvik-cache...\n");
        __system("rm -rf /cache/dc");
        ui_print("Cleaned: /cache/dc\n");

        struct stat st;
		LOGE("TODO: Re-implement wipe dalvik into Partition Manager!\n");
        if (1) //if (0 != stat(sde.blk, &st))
        {
            ui_print("/sd-ext not present, skipping\n");
        } else {
        	__system("mount /sd-ext");
    	    LOGI("Mounting /sd-ext\n");
    	    if (stat("/sd-ext/dalvik-cache",&st) == 0)
    	    {
                __system("rm -rf /sd-ext/dalvik-cache");
        	    ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
    	    }
        }
        ensure_path_unmounted("/data");
        ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
        //ui_set_background(BACKGROUND_ICON_MAIN);
        //if (!ui_text_visible()) return;
}

// BATTERY STATS
void wipe_battery_stats()
{
    ensure_path_mounted("/data");
    struct stat st;
    if (0 != stat("/data/system/batterystats.bin", &st))
    {
        ui_print("No Battery Stats Found. No Need To Wipe.\n");
    } else {
        //ui_set_background(BACKGROUND_ICON_WIPE);
        remove("/data/system/batterystats.bin");
        ui_print("Cleared: Battery Stats...\n");
        ensure_path_unmounted("/data");
    }
}

// ROTATION SETTINGS
void wipe_rotate_data()
{
    //ui_set_background(BACKGROUND_ICON_WIPE);
    ensure_path_mounted("/data");
    __system("rm -r /data/misc/akmd*");
    __system("rm -r /data/misc/rild*");
    ui_print("Cleared: Rotatation Data...\n");
    ensure_path_unmounted("/data");
}   

void fix_perms()
{
	ensure_path_mounted("/data");
	ensure_path_mounted("/system");
	//ui_show_progress(1,30);
    ui_print("\n-- Fixing Permissions\n");
	ui_print("This may take a few minutes.\n");
	__system("./sbin/fix_permissions.sh");
	ui_print("-- Done.\n\n");
	//ui_reset_progress();
}

int get_battery_level(void)
{
    static int lastVal = -1;
    static time_t nextSecCheck = 0;

    struct timeval curTime;
    gettimeofday(&curTime, NULL);
    if (curTime.tv_sec > nextSecCheck)
    {
        char cap_s[4];
        FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
        if (cap)
        {
            fgets(cap_s, 4, cap);
            fclose(cap);
            lastVal = atoi(cap_s);
            if (lastVal > 100)  lastVal = 101;
            if (lastVal < 0)    lastVal = 0;
        }
        nextSecCheck = curTime.tv_sec + 60;
    }
    return lastVal;
}

void update_tz_environment_variables() {
    setenv("TZ", DataManager_GetStrValue(TW_TIME_ZONE_VAR), 1);
    tzset();
@@ -478,56 +382,6 @@ void run_script(const char *str1, const char *str2, const char *str3, const char
		//if (!ui_text_visible()) return;
}

void install_htc_dumlock(void)
{
	struct statfs fs1, fs2;
	int need_libs = 0;

	ui_print("Installing HTC Dumlock to system...\n");
	ensure_path_mounted("/system");
	__system("cp /res/htcd/htcdumlocksys /system/bin/htcdumlock && chmod 755 /system/bin/htcdumlock");
	if (statfs("/system/bin/flash_image", &fs1) != 0) {
		ui_print("Installing flash_image...\n");
		__system("cp /res/htcd/flash_imagesys /system/bin/flash_image && chmod 755 /system/bin/flash_image");
		need_libs = 1;
	} else
		ui_print("flash_image is already installed, skipping...\n");
	if (statfs("/system/bin/dump_image", &fs2) != 0) {
		ui_print("Installing dump_image...\n");
		__system("cp /res/htcd/dump_imagesys /system/bin/dump_image && chmod 755 /system/bin/dump_image");
		need_libs = 1;
	} else
		ui_print("dump_image is already installed, skipping...\n");
	if (need_libs) {
		ui_print("Installing libs needed for flash_image and dump_image...\n");
		__system("cp /res/htcd/libbmlutils.so /system/lib && chmod 755 /system/lib/libbmlutils.so");
		__system("cp /res/htcd/libflashutils.so /system/lib && chmod 755 /system/lib/libflashutils.so");
		__system("cp /res/htcd/libmmcutils.so /system/lib && chmod 755 /system/lib/libmmcutils.so");
		__system("cp /res/htcd/libmtdutils.so /system/lib && chmod 755 /system/lib/libmtdutils.so");
	}
	ui_print("Installing HTC Dumlock app...\n");
	ensure_path_mounted("/data");
	mkdir("/data/app", 0777);
	__system("rm /data/app/com.teamwin.htcdumlock*");
	__system("cp /res/htcd/HTCDumlock.apk /data/app/com.teamwin.htcdumlock.apk");
	sync();
	ui_print("HTC Dumlock is installed.\n");
}

void htc_dumlock_restore_original_boot(void)
{
	ui_print("Restoring original boot...\n");
	__system("htcdumlock restore");
	ui_print("Original boot restored.\n");
}

void htc_dumlock_reflash_recovery_to_boot(void)
{
	ui_print("Reflashing recovery to boot...\n");
	__system("htcdumlock recovery noreboot");
	ui_print("Recovery is flashed to boot.\n");
}

void check_and_run_script(const char* script_file, const char* display_name)
{
	// Check for and run startup script if script exists
+0 −10
Original line number Diff line number Diff line
@@ -8,22 +8,12 @@ int __system(const char *command);
FILE * __popen(const char *program, const char *type);
int __pclose(FILE *iop);

void wipe_dalvik_cache();
void wipe_battery_stats();
void wipe_rotate_data();

static long tmplog_offset = 0;

void update_tz_environment_variables();

void fix_perms();

void run_script(const char *str1, const char *str2, const char *str3, const char *str4, const char *str5, const char *str6, const char *str7, int request_confirm);

void install_htc_dumlock(void);
void htc_dumlock_restore_original_boot(void);
void htc_dumlock_reflash_recovery_to_boot(void);

void check_and_run_script(const char* script_file, const char* display_name);
int check_backup_name(int show_error);
void twfinish_recovery(const char *send_intent);
+34 −39
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <string>
#include <sstream>
#include "../partitions.hpp"
#include "../twrp-functions.hpp"

extern "C" {
#include "../common.h"
@@ -31,7 +32,6 @@ extern "C" {
#include "../twinstall.h"

int TWinstall_zip(const char* path, int* wipe_cache);
void fix_perms();
void wipe_dalvik_cache(void);
int check_backup_name(int show_error);
void wipe_battery_stats(void);
@@ -667,62 +667,57 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
            operation_start("Format");
            DataManager::SetValue("tw_partition", arg);

			int ret_val = 0;
			int ret_val = false;

			if (simulate) {
				simulate_progress_bar();
			} else {
				if (arg == "data")
					PartitionManager.Factory_Reset();
					ret_val = PartitionManager.Factory_Reset();
				else if (arg == "battery")
					wipe_battery_stats();
					ret_val = PartitionManager.Wipe_Battery_Stats();
				else if (arg == "rotate")
					wipe_rotate_data();
					ret_val = PartitionManager.Wipe_Rotate_Data();
				else if (arg == "dalvik")
					wipe_dalvik_cache();
					ret_val = PartitionManager.Wipe_Dalvik_Cache();
				else if (arg == "DATAMEDIA") {
					LOGE("TODO: Implement formatting of datamedia device!\n");
					ret_val = 1; //format_data_media();
					int has_datamedia, dual_storage;

					DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
					DataManager::GetValue(TW_HAS_DUAL_STORAGE, dual_storage);
					if (has_datamedia && !dual_storage) {
						system("umount /sdcard");
						system("mount /data/media /sdcard");
					}
					ret_val = PartitionManager.Format_Data();
				} else if (arg == "INTERNAL") {
					int has_datamedia, dual_storage;

					DataManager::GetValue(TW_HAS_DATA_MEDIA, has_datamedia);
					if (has_datamedia) {
						PartitionManager.Mount_By_Path("/data", 1);
						__system("rm -rf /data/media");
						__system("cd /data && mkdir media && chmod 775 media");
						DataManager::GetValue(TW_HAS_DUAL_STORAGE, dual_storage);
						if (!dual_storage) {
							system("umount /sdcard");
							system("mount /data/media /sdcard");
						}
						ret_val = PartitionManager.Wipe_Media_From_Data();
					} else {
						ret_val = 0;
						LOGE("Wipe not implemented yet!\n");
						ret_val = PartitionManager.Wipe_By_Path(DataManager::GetSettingsStoragePath());
					}
				} else if (arg == "EXTERNAL") {
					ret_val = 0;
					LOGE("Wipe not implemented yet!\n");
					string External_Path;

					DataManager::GetValue(TW_EXTERNAL_PATH, External_Path);
					ret_val = PartitionManager.Wipe_By_Path(External_Path);
				} else
					PartitionManager.Wipe_By_Path(arg);
					ret_val = PartitionManager.Wipe_By_Path(arg);

				if (arg == "/sdcard") {
					PartitionManager.Mount_By_Path("/sdcard", 1);
					mkdir("/sdcard/TWRP", 0777);
				if (arg == DataManager::GetSettingsStoragePath()) {
					// If we wiped the settings storage path, recreate the TWRP folder and dump the settings
					string Storage_Path = DataManager::GetSettingsStoragePath();

					if (PartitionManager.Mount_By_Path(Storage_Path, true)) {
						LOGI("Making TWRP folder and saving settings.\n");
						Storage_Path += "/TWRP";
						mkdir(Storage_Path.c_str(), 0777);
						DataManager::Flush();
					} else {
						LOGE("Unable to recreate TWRP folder and save settings.\n");
					}
				}
			}
			PartitionManager.Update_System_Details();
			if (ret_val != 0)
				ret_val = 1;
			if (ret_val)
				ret_val = 0; // 0 is success
			else
				ret_val = 1; // 1 is failure
            operation_end(ret_val, simulate);
            return 0;
        }
@@ -770,7 +765,7 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
			if (simulate) {
				simulate_progress_bar();
			} else
				fix_perms();
				PartitionManager.Fix_Permissions();

			LOGI("fix permissions DONE!\n");
			operation_end(0, simulate);
@@ -870,7 +865,7 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
			if (simulate) {
				simulate_progress_bar();
			} else
				install_htc_dumlock();
				TWFunc::install_htc_dumlock();

			operation_end(0, simulate);
			return 0;
@@ -881,7 +876,7 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
			if (simulate) {
				simulate_progress_bar();
			} else
				htc_dumlock_restore_original_boot();
				TWFunc::htc_dumlock_restore_original_boot();

			operation_end(0, simulate);
			return 0;
@@ -892,7 +887,7 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */)
			if (simulate) {
				simulate_progress_bar();
			} else
				htc_dumlock_reflash_recovery_to_boot();
				TWFunc::htc_dumlock_reflash_recovery_to_boot();

			operation_end(0, simulate);
			return 0;
Loading