From 51a0e82eb29a6dfc79f93479883383fbdbf8bcc2 Mon Sep 17 00:00:00 2001 From: Dees_Troy Date: Wed, 5 Sep 2012 15:24:24 -0400 Subject: TWRP-ify AOSP code Pull in most TWRP sources Stub out partition management code Make it compile -- probably will not boot Kind of a mess but have to start somewhere --- injecttwrp/Android.mk | 13 ++ injecttwrp/injecttwrp.c | 421 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 434 insertions(+) create mode 100644 injecttwrp/Android.mk create mode 100644 injecttwrp/injecttwrp.c (limited to 'injecttwrp') diff --git a/injecttwrp/Android.mk b/injecttwrp/Android.mk new file mode 100644 index 000000000..9c39c297c --- /dev/null +++ b/injecttwrp/Android.mk @@ -0,0 +1,13 @@ +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +ifeq ($(TW_INCLUDE_INJECTTWRP), true) + LOCAL_SRC_FILES:= \ + injecttwrp.c + LOCAL_CFLAGS:= -g -c -W + LOCAL_MODULE:=injecttwrp + LOCAL_MODULE_TAGS:= eng + LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES + LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin + include $(BUILD_EXECUTABLE) +endif diff --git a/injecttwrp/injecttwrp.c b/injecttwrp/injecttwrp.c new file mode 100644 index 000000000..0daaf5696 --- /dev/null +++ b/injecttwrp/injecttwrp.c @@ -0,0 +1,421 @@ +/* + * This binary allows you to back up the second (recovery) ramdisk on + * typical Samsung boot images and to inject a new second ramdisk into + * an existing boot image. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program 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 this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + * + * The code was written from scratch by Dees_Troy dees_troy at + * yahoo + * + * Copyright (c) 2012 + */ +#include +#include +#include +#include +#include + +#define INJECT_USE_TMP 1 + +int scan_file_for_data(char *filename, unsigned char *data, int data_size, unsigned long start_location, unsigned long *data_address) { + FILE *pFile; + unsigned long lSize; + unsigned char *buffer, *last_needle = NULL; + unsigned char needle[data_size]; + size_t result; + int i, return_val = 0; + + pFile = fopen(filename, "rb"); + if(pFile==NULL){ + printf("Unabled to open file '%s'.\nFailed\n", filename); + return -1; + } + + fseek (pFile , 0 , SEEK_END); + lSize = ftell(pFile); + rewind(pFile); + + buffer = (unsigned char*)malloc(sizeof(unsigned char) * lSize); + if(buffer == NULL){ + printf("Memory allocation error on '%s'!\nFailed\n", filename); + return -1; + } + + result = fread(buffer, 1, lSize, pFile); + if (result != lSize) { + printf("Error reading file '%s'\nFailed\n", filename); + return -1; + } + + for (i=0; i 5) + arg_error = 1; + else { + if ((argc == 2 || argc == 3) && (strcmp(argv[1], "-b") == 0 || strcmp(argv[1], "--backup") == 0)) { + // Backup existing boot image + printf("Dumping boot image...\n"); +#ifdef INJECT_USE_TMP + system("dump_image boot /tmp/original_boot.img"); + strcpy(boot_image, "/tmp/original_boot.img"); + + if (argc == 2) + strcpy(backup_image, "/tmp/recovery_ramdisk.img"); + else + strcpy(backup_image, argv[2]); +#else + system("mount /cache"); + system("dump_image boot /cache/original_boot.img"); + strcpy(boot_image, "/cache/original_boot.img"); + + if (argc == 2) + strcpy(backup_image, "/cache/recovery_ramdisk.img"); + else + strcpy(backup_image, argv[2]); +#endif + + // Check if this is a normal Android image or a Samsung image + return_val = scan_file_for_data(boot_image, ®ular_check, 8, 0, &address2); + if (return_val >= 0) { + printf("This is not a properly formatted Samsung boot image!\nFailed\n"); + return 1; + } + + // Find the ramdisk + return_val = find_gzip_recovery_ramdisk(boot_image, &address2); + if (return_val < 0) { + return 1; + } + + backup_recovery_ramdisk(boot_image, address2, backup_image); + return 0; + } else { + // Inject new ramdisk + if (strcmp(argv[1], "-d") == 0 || strcmp(argv[1], "--dump") == 0) { + printf("Dumping boot image...\n"); +#ifdef INJECT_USE_TMP + system("dump_image boot /tmp/original_boot.img"); + strcpy(boot_image, "/tmp/original_boot.img"); +#else + system("mount /cache"); + system("dump_image boot /cache/original_boot.img"); + strcpy(boot_image, "/cache/original_boot.img"); +#endif + delete_ind = -1; + } else + strcpy(boot_image, argv[1]); + + // Check if this is a normal Android image or a Samsung image + return_val = scan_file_for_data(boot_image, ®ular_check, 8, 0, &address2); + if (return_val >= 0) { + printf("This is not a properly formatted Samsung boot image!\nFailed\n"); + return 1; + } + + // Find the ramdisk + return_val = find_gzip_recovery_ramdisk(boot_image, &address2); + if (return_val < 0) { + return 1; + } + + // Write the new image + write_new_ramdisk(boot_image, argv[2], address2, argv[3]); + + // Delete --dump image if needed + if (delete_ind) { + printf("Deleting dumped boot image from /cache\n"); + system("rm /cache/original_boot.img"); + } + + if (argc == 5 && (strcmp(argv[4], "-f") == 0 || strcmp(argv[4], "--flash") == 0)) { + char command[512]; + + printf("Flashing new image...\n"); + system("erase_image boot"); // Needed because flash_image checks the header and the header sometimes is the same while the ramdisks are different + strcpy(command, "flash_image boot "); + strcat(command, argv[3]); + system(command); + printf("Flash complete.\n"); + } + return 0; + } + } + + if (arg_error) { + printf("Invalid arguments supplied.\n"); + printf("Usage:\n\n"); + printf("Backup existing recovery ramdisk (requires dump_image):\n"); + printf("injecttwrp --backup [optionalbackuplocation.img]\n\n"); + printf("Inject new recovery ramdisk:\n"); + printf("injecttwrp originalboot.img ramdisk-recovery.img outputboot.img\n"); + printf("injecttwrp --dump ramdisk-recovery.img outputboot.img [--flash]\n"); + printf("--dump will use dump_image to dump your existing boot image\n"); + printf("--flash will use flash_image to flash the new boot image\n\n"); + printf("NOTE: dump_image, erase_image, and flash_image must already be installed!\n"); + return 0; + } + + return 0; +} -- cgit v1.2.3