diff options
Diffstat (limited to 'adbbu')
-rw-r--r-- | adbbu/Android.mk | 48 | ||||
-rw-r--r-- | adbbu/adbbumain.cpp | 95 | ||||
-rw-r--r-- | adbbu/libtwadbbu.cpp | 307 | ||||
-rw-r--r-- | adbbu/libtwadbbu.hpp | 51 | ||||
-rw-r--r-- | adbbu/twadbstream.h | 112 | ||||
-rw-r--r-- | adbbu/twrpback.cpp | 935 | ||||
-rw-r--r-- | adbbu/twrpback.hpp | 61 |
7 files changed, 1609 insertions, 0 deletions
diff --git a/adbbu/Android.mk b/adbbu/Android.mk new file mode 100644 index 000000000..8f8dbd0fb --- /dev/null +++ b/adbbu/Android.mk @@ -0,0 +1,48 @@ +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_MODULE := libtwadbbu +LOCAL_MODULE_TAGS := optional +LOCAL_CFLAGS = -fno-strict-aliasing -D_LARGFILE_SOURCE #-D_DEBUG_ADB_BACKUP +LOCAL_C_INCLUDES += bionic external/zlib +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0) + LOCAL_C_INCLUDES += external/stlport/stlport +endif + +LOCAL_SRC_FILES = \ + libtwadbbu.cpp \ + twrpback.cpp + +LOCAL_SHARED_LIBRARIES += libz libc libstdc++ libtwrpdigest + +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0) + LOCAL_SHARED_LIBRARIES += libstlport +else + LOCAL_SHARED_LIBRARIES += libc++ +endif + +include $(BUILD_SHARED_LIBRARY) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + adbbumain.cpp + +LOCAL_SHARED_LIBRARIES += libstdc++ libz libtwadbbu + +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0) + LOCAL_C_INCLUDES += external/stlport/stlport + LOCAL_SHARED_LIBRARIES += libstlport +else + LOCAL_SHARED_LIBRARIES += libc++ +endif + +LOCAL_C_INCLUDES += bionic external/zlib +LOCAL_CFLAGS:= -c -W +LOCAL_MODULE:= twrpbu +LOCAL_MODULE_STEM := bu +LOCAL_MODULE_TAGS:= eng +LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES +LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin +include $(BUILD_EXECUTABLE) + diff --git a/adbbu/adbbumain.cpp b/adbbu/adbbumain.cpp new file mode 100644 index 000000000..bd96b2040 --- /dev/null +++ b/adbbu/adbbumain.cpp @@ -0,0 +1,95 @@ +/* + Copyright 2013 to 2017 TeamWin + 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <ctype.h> +#include <string> +#include <sstream> +#include <algorithm> + +#include "twrpback.hpp" +#include "twadbstream.h" + + +int main(int argc, char **argv) { + int index; + size_t pos = 0; + bool ret = false; + size_t maxpos = strlen(TWRPARG) + 2; + std::string command; + twrpback tw; + + tw.adblogwrite("Starting adb backup and restore\n"); + command = argv[1]; + for (index = 2; index < argc; index++) { + command = command + " " + argv[index]; + } + + pos = command.find(TWRP_BACKUP_ARG); + if (pos == std::string::npos || pos > (maxpos + strlen(TWRP_BACKUP_ARG) + 1)) { + pos = command.find(TWRP_RESTORE_ARG); + } + if (pos == std::string::npos || pos > maxpos + strlen(TWRP_STREAM_ARG) + 1) { + pos = command.find(TWRP_STREAM_ARG); + } + + tw.adblogwrite("command: " + command + "\n"); + command.erase(0, pos); + command.erase(std::remove(command.begin(), command.end(), '\''), command.end()); + + if (command.substr(0, sizeof(TWRP_BACKUP_ARG) - 1) == TWRP_BACKUP_ARG) { + tw.adblogwrite("Starting adb backup\n"); + if (isdigit(*argv[1])) + tw.adbd_fd = atoi(argv[1]); + else + tw.adbd_fd = 1; + ret = tw.backup(command); + } + else if (command.substr(0, sizeof(TWRP_RESTORE_ARG) - 1) == TWRP_RESTORE_ARG) { + tw.adblogwrite("Starting adb restore\n"); + if (isdigit(*argv[1])) + tw.adbd_fd = atoi(argv[1]); + else + tw.adbd_fd = 0; + ret = tw.restore(); + } + else if (command.substr(0, sizeof(TWRP_STREAM_ARG) - 1) == TWRP_STREAM_ARG) { + tw.setStreamFileName(argv[3]); + tw.threadStream(); + ret = true; + } + if (ret) + tw.adblogwrite("Adb backup/restore completed\n"); + else + tw.adblogwrite("Adb backup/restore failed\n"); + + if (unlink(TW_ADB_BU_CONTROL) < 0) { + std::stringstream str; + str << strerror(errno); + tw.adblogwrite("Unable to remove TW_ADB_BU_CONTROL: " + str.str()); + } + unlink(TW_ADB_TWRP_CONTROL); + if (ret) + return 0; + else + return -1; +} diff --git a/adbbu/libtwadbbu.cpp b/adbbu/libtwadbbu.cpp new file mode 100644 index 000000000..39803b0ce --- /dev/null +++ b/adbbu/libtwadbbu.cpp @@ -0,0 +1,307 @@ +/* + Copyright 2013 to 2017 TeamWin + 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <zlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/select.h> +#include <sys/time.h> +#include <string> +#include <vector> +#include <fstream> +#include <sstream> +#include <assert.h> + +#include "twadbstream.h" +#include "libtwadbbu.hpp" +#include "twrpback.hpp" + +bool twadbbu::Check_ADB_Backup_File(std::string fname) { + struct AdbBackupStreamHeader adbbuhdr; + uint32_t crc, adbbuhdrcrc; + unsigned char buf[MAX_ADB_READ]; + int bytes; + + int fd = open(fname.c_str(), O_RDONLY); + if (fd < 0) { + printf("Unable to open %s for reading: %s.\n", fname.c_str(), strerror(errno)); + close(fd); + return false; + } + bytes = read(fd, &buf, sizeof(buf)); + close(fd); + + if (memcpy(&adbbuhdr, buf, sizeof(adbbuhdr)) == NULL) { + printf("Unable to memcpy: %s (%s).\n", fname.c_str(), strerror(errno)); + return false; + } + adbbuhdrcrc = adbbuhdr.crc; + memset(&adbbuhdr.crc, 0, sizeof(adbbuhdr.crc)); + crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (const unsigned char*) &adbbuhdr, sizeof(adbbuhdr)); + + return (crc == adbbuhdrcrc); +} + +std::vector<std::string> twadbbu::Get_ADB_Backup_Files(std::string fname) { + unsigned char buf[MAX_ADB_READ]; + struct AdbBackupControlType structcmd; + std::vector<std::string> adb_partitions; + + int fd = open(fname.c_str(), O_RDONLY); + if (fd < 0) { + printf("Unable to open %s for reading: %s\n", fname.c_str(), strerror(errno)); + close(fd); + return std::vector<std::string>(); + } + + while (true) { + std::string cmdstr; + int readbytes; + if ((readbytes = read(fd, &buf, sizeof(buf))) > 0) { + memcpy(&structcmd, buf, sizeof(structcmd)); + assert(structcmd.type == TWENDADB || structcmd.type == TWIMG || structcmd.type == TWFN); + cmdstr = structcmd.type; + std::string cmdtype = cmdstr.substr(0, sizeof(structcmd.type) - 1); + if (cmdtype == TWENDADB) { + struct AdbBackupControlType endadb; + uint32_t crc, endadbcrc; + + memcpy(&endadb, buf, sizeof(endadb)); + endadbcrc = endadb.crc; + memset(&endadb.crc, 0, sizeof(endadb.crc)); + crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (const unsigned char*) &endadb, sizeof(endadb)); + + if (crc == endadbcrc) { + break; + } + else { + printf("ADB TWENDADB crc header doesn't match\n"); + close(fd); + return std::vector<std::string>(); + } + } + else if (cmdtype == TWIMG || cmdtype == TWFN) { + struct twfilehdr twfilehdr; + uint32_t crc, twfilehdrcrc; + + memcpy(&twfilehdr, buf, sizeof(twfilehdr)); + twfilehdrcrc = twfilehdr.crc; + memset(&twfilehdr.crc, 0, sizeof(twfilehdr.crc)); + + crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (const unsigned char*) &twfilehdr, sizeof(twfilehdr)); + if (crc == twfilehdrcrc) { + std::string adbfile = twfilehdr.name; + int pos = adbfile.find_last_of("/") + 1; + adbfile = adbfile.substr(pos, adbfile.size()); + adb_partitions.push_back(adbfile); + } + else { + printf("ADB crc header doesn't match\n"); + close(fd); + return std::vector<std::string>(); + } + } + } + } + close(fd); + return adb_partitions; +} + +bool twadbbu::Write_ADB_Stream_Header(uint64_t partition_count) { + struct AdbBackupStreamHeader twhdr; + int adb_control_bu_fd; + + memset(&twhdr, 0, sizeof(twhdr)); + adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); + if (adb_control_bu_fd < 0) { + printf("Cannot write to TW_ADB_BU_CONTROL: %s\n", strerror(errno)); + return false; + } + + strncpy(twhdr.start_of_header, TWRP, sizeof(twhdr.start_of_header)); + strncpy(twhdr.type, TWSTREAMHDR, sizeof(twhdr.type)); + twhdr.partition_count = partition_count; + twhdr.version = ADB_BACKUP_VERSION; + memset(twhdr.space, 0, sizeof(twhdr.space)); + twhdr.crc = crc32(0L, Z_NULL, 0); + twhdr.crc = crc32(twhdr.crc, (const unsigned char*) &twhdr, sizeof(twhdr)); + if (write(adb_control_bu_fd, &twhdr, sizeof(twhdr)) < 0) { + printf("Cannot write to adb control channel: %s\n", strerror(errno)); + close(adb_control_bu_fd); + return false; + } + return true; +} + +bool twadbbu::Write_ADB_Stream_Trailer() { + int adb_control_bu_fd; + struct AdbBackupControlType endadb; + + memset(&endadb, 0, sizeof(endadb)); + + adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY); + if (adb_control_bu_fd < 0) { + printf("Error opening adb_control_bu_fd: %s\n", strerror(errno)); + return false; + } + strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header)); + strncpy(endadb.type, TWENDADB, sizeof(endadb.type)); + endadb.crc = crc32(0L, Z_NULL, 0); + endadb.crc = crc32(endadb.crc, (const unsigned char*) &endadb, sizeof(endadb)); + if (write(adb_control_bu_fd, &endadb, sizeof(endadb)) < 0) { + printf("Cannot write to ADB control: %s\n", strerror(errno)); + close(adb_control_bu_fd); + return false; + } + close(adb_control_bu_fd); + return true; +} + +bool twadbbu::Write_TWFN(std::string Backup_FileName, uint64_t file_size, bool use_compression) { + int adb_control_bu_fd; + adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); + struct twfilehdr twfilehdr; + strncpy(twfilehdr.start_of_header, TWRP, sizeof(twfilehdr.start_of_header)); + strncpy(twfilehdr.type, TWFN, sizeof(twfilehdr.type)); + strncpy(twfilehdr.name, Backup_FileName.c_str(), sizeof(twfilehdr.name)); + twfilehdr.size = (file_size == 0 ? 1024 : file_size); + twfilehdr.compressed = use_compression; + twfilehdr.crc = crc32(0L, Z_NULL, 0); + twfilehdr.crc = crc32(twfilehdr.crc, (const unsigned char*) &twfilehdr, sizeof(twfilehdr)); + + printf("Sending TWFN to adb\n"); + if (write(adb_control_bu_fd, &twfilehdr, sizeof(twfilehdr)) < 1) { + printf("Cannot that write to adb_control_bu_fd: %s\n", strerror(errno)); + close(adb_control_bu_fd); + return false; + } + fsync(adb_control_bu_fd); + close(adb_control_bu_fd); + return true; +} + +bool twadbbu::Write_TWIMG(std::string Backup_FileName, uint64_t file_size) { + int adb_control_bu_fd; + struct twfilehdr twimghdr; + + adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); + strncpy(twimghdr.start_of_header, TWRP, sizeof(twimghdr.start_of_header)); + strncpy(twimghdr.type, TWIMG, sizeof(twimghdr.type)); + twimghdr.size = file_size; + strncpy(twimghdr.name, Backup_FileName.c_str(), sizeof(twimghdr.name)); + twimghdr.crc = crc32(0L, Z_NULL, 0); + twimghdr.crc = crc32(twimghdr.crc, (const unsigned char*) &twimghdr, sizeof(twimghdr)); + printf("Sending TWIMG to adb\n"); + if (write(adb_control_bu_fd, &twimghdr, sizeof(twimghdr)) < 1) { + printf("Cannot write to adb control channel: %s\n", strerror(errno)); + return false; + } + + return true; +} + +bool twadbbu::Write_TWEOF() { + struct AdbBackupControlType tweof; + int adb_control_bu_fd; + int errctr = 0; + + printf("opening TW_ADB_BU_CONTROL\n"); + adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); + while (adb_control_bu_fd < 0) { + printf("failed to open TW_ADB_BU_CONTROL. Retrying: %s\n", strerror(errno)); + adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); + usleep(10000); + errctr++; + if (errctr > ADB_BU_MAX_ERROR) { + printf("Cannot write to adb_control_bu_fd: %s.\n", strerror(errno)); + close(adb_control_bu_fd); + return false; + } + } + memset(&tweof, 0, sizeof(tweof)); + strncpy(tweof.start_of_header, TWRP, sizeof(tweof.start_of_header)); + strncpy(tweof.type, TWEOF, sizeof(tweof.type)); + tweof.crc = crc32(0L, Z_NULL, 0); + tweof.crc = crc32(tweof.crc, (const unsigned char*) &tweof, sizeof(tweof)); + printf("Sending TWEOF to adb backup\n"); + if (write(adb_control_bu_fd, &tweof, sizeof(tweof)) < 0) { + printf("Cannot write to adb_control_bu_fd: %s.\n", strerror(errno)); + close(adb_control_bu_fd); + return false; + } + close(adb_control_bu_fd); + return true; +} + +bool twadbbu::Write_TWERROR() { + struct AdbBackupControlType twerror; + int adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); + + strncpy(twerror.start_of_header, TWRP, sizeof(twerror.start_of_header)); + strncpy(twerror.type, TWERROR, sizeof(twerror.type)); + memset(twerror.space, 0, sizeof(twerror.space)); + twerror.crc = crc32(0L, Z_NULL, 0); + twerror.crc = crc32(twerror.crc, (const unsigned char*) &twerror, sizeof(twerror)); + if (write(adb_control_bu_fd, &twerror, sizeof(twerror)) < 0) { + printf("Cannot write to adb control channel: %s\n", strerror(errno)); + return false; + } + close(adb_control_bu_fd); + return true; +} + +bool twadbbu::Write_TWENDADB() { + struct AdbBackupControlType endadb; + int adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); + + memset(&endadb, 0, sizeof(endadb)); + strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header)); + strncpy(endadb.type, TWENDADB, sizeof(endadb.type)); + endadb.crc = crc32(0L, Z_NULL, 0); + endadb.crc = crc32(endadb.crc, (const unsigned char*) &endadb, sizeof(endadb)); + + printf("Sending TWENDADB to ADB Backup\n"); + if (write(adb_control_bu_fd, &endadb, sizeof(endadb)) < 1) { + printf("Cannot write to ADB_CONTROL_BU_FD: %s\n", strerror(errno)); + return false; + } + + close(adb_control_bu_fd); + return true; +} + +bool twadbbu::Write_TWDATA(FILE* adbd_fp) { + struct AdbBackupControlType data_block; + memset(&data_block, 0, sizeof(data_block)); + strncpy(data_block.start_of_header, TWRP, sizeof(data_block.start_of_header)); + strncpy(data_block.type, TWDATA, sizeof(data_block.type)); + data_block.crc = crc32(0L, Z_NULL, 0); + data_block.crc = crc32(data_block.crc, (const unsigned char*) &data_block, sizeof(data_block)); + if (fwrite(&data_block, 1, sizeof(data_block), adbd_fp) != sizeof(data_block)) { + return false; + } + return true; +} diff --git a/adbbu/libtwadbbu.hpp b/adbbu/libtwadbbu.hpp new file mode 100644 index 000000000..9244bb54d --- /dev/null +++ b/adbbu/libtwadbbu.hpp @@ -0,0 +1,51 @@ +/* + Copyright 2013 to 2017 TeamWin + 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/>. +*/ +#ifndef _LIBTWADBBU_HPP +#define _LIBTWADBBU_HPP + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <string.h> +#include <fcntl.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/select.h> +#include <sys/time.h> +#include <string> +#include <vector> +#include <fstream> +#include <sstream> + +#include "twadbstream.h" + +class twadbbu { +public: + static bool Check_ADB_Backup_File(std::string fname); //Check if file is ADB Backup file + static std::vector<std::string> Get_ADB_Backup_Files(std::string fname); //List ADB Files in String Vector + static bool Write_ADB_Stream_Header(uint64_t partition_count); //Write ADB Stream Header to stream + static bool Write_ADB_Stream_Trailer(); //Write ADB Stream Trailer to stream + static bool Write_TWFN(std::string Backup_FileName, uint64_t file_size, bool use_compression); //Write a tar image to stream + static bool Write_TWIMG(std::string Backup_FileName, uint64_t file_size); //Write a partition image to stream + static bool Write_TWEOF(); //Write ADB End-Of-File marker to stream + static bool Write_TWERROR(); //Write error message occurred to stream + static bool Write_TWENDADB(); //Write ADB End-Of-Stream command to stream + static bool Write_TWDATA(FILE* adbd_fp); //Write TWDATA separator +}; + +#endif //__LIBTWADBBU_HPP diff --git a/adbbu/twadbstream.h b/adbbu/twadbstream.h new file mode 100644 index 000000000..bef463cf8 --- /dev/null +++ b/adbbu/twadbstream.h @@ -0,0 +1,112 @@ +/* + 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/>. +*/ + +#ifndef __TWADBSTREAM_H +#define __TWADBSTREAM_H + +#define TWRPARG "--twrp" +#define TWRP_BACKUP_ARG "backup" +#define TWRP_RESTORE_ARG "restore" +#define TWRP_STREAM_ARG "stream" +#define TW_ADB_BACKUP "/tmp/twadbbackup" //FIFO for adb backup +#define TW_ADB_RESTORE "/tmp/twadbrestore" //FIFO for adb restore +#define TW_ADB_BU_CONTROL "/tmp/twadbbucontrol" //FIFO for sending control from TWRP to ADB Backup +#define TW_ADB_TWRP_CONTROL "/tmp/twadbtwrpcontrol" //FIFO for sending control from ADB Backup to TWRP +#define TWRP "TWRP" //Magic Value +#define ADB_BU_MAX_ERROR 20 //Max amount of errors for while loops +#define ADB_BACKUP_OP "adbbackup" +#define ADB_RESTORE_OP "adbrestore" + +//ADB Backup Control Commands +#define TWSTREAMHDR "twstreamheader" //TWRP Parititon Count Control +#define TWFN "twfilename" //TWRP Filename Control +#define TWIMG "twimage" //TWRP Image name Control +#define TWEOF "tweof" //End of File for Image/File +#define MD5TRAILER "md5trailer" //Image/File MD5 Trailer +#define TWDATA "twdatablock" // twrp adb backup data block header +#define TWMD5 "twverifymd5" //This command is compared to the md5trailer by ORS to verify transfer +#define TWENDADB "twendadb" //End Protocol +#define TWERROR "twerror" //Send error +#define ADB_BACKUP_VERSION 3 //Backup Version +#define DATA_MAX_CHUNK_SIZE 1048576 //Maximum size between each data header +#define MAX_ADB_READ 512 //align with default tar size for amount to read fom adb stream + +/* +structs for adb backup need to align to 512 bytes for reading 512 +bytes at a time +Each struct contains a crc field so that when we are checking for commands +and the crc doesn't match we still assume it's data matching the command +struct but not really a command +*/ + +/* stream format: + | TW ADB Backup Header | + | TW File Stream Header | + | File Data | + | File/Image MD5 Trailer | + | TW File Stream Header | + | File Data | + | File/Image MD5 Trailer | + | etc... | +*/ + +//determine whether struct is 512 bytes, if not fail compilation +#define ADBSTRUCT_STATIC_ASSERT(structure) typedef char adb_assertion[( !!(structure) )*2-1 ] + +//generic cmd structure to align fields for sending commands to and from adb backup +struct AdbBackupControlType { + char start_of_header[8]; //stores the magic value #define TWRP + char type[16]; //stores the type of command, TWENDADB, TWCNT, TWEOF, TWMD5, TWDATA and TWERROR + uint32_t crc; //stores the zlib 32 bit crc of the AdbBackupControlType struct to allow for making sure we are processing metadata + char space[484]; //stores space to align the struct to 512 bytes + + //return a C++ string while not reading outside the type char array + std::string get_type() { + return std::string(type, strnlen(type, sizeof(type)-1)); + } +}; + +//general info for file metadata stored in adb backup header +struct twfilehdr { + char start_of_header[8]; //stores the magic value #define TWRP + char type[16]; //stores the type of file header, TWFN or TWIMG + uint64_t size; //stores the size of the file contained after this header in the backup file + uint64_t compressed; //stores whether the file is compressed or not. 1 == compressed and 0 == uncompressed + uint32_t crc; //stores the zlib 32 bit crc of the twfilehdr struct to allow for making sure we are processing metadata + char name[468]; //stores the filename of the file +}; + +//md5 for files stored as a trailer to files in the adb backup file to check +//that they are restored correctly +struct AdbBackupFileTrailer { + char start_of_trailer[8]; //stores the magic value #define TWRP + char type[16]; //stores the AdbBackupFileTrailer type MD5TRAILER + uint32_t crc; //stores the zlib 32 bit crc of the AdbBackupFileTrailer struct to allow for making sure we are processing metadata + uint32_t ident; //stores crc to determine if header is encapsulated in stream as data + char md5[40]; //stores the md5 computation of the file + char space[440]; //stores space to align the struct to 512 bytes +}; + +//info for version and number of partitions backed up +struct AdbBackupStreamHeader { + char start_of_header[8]; //stores the magic value #define TWRP + char type[16]; //stores the AdbBackupStreamHeader value TWCNT + uint64_t partition_count; //stores the number of partitions to restore in the stream + uint64_t version; //stores the version of adb backup. increment ADB_BACKUP_VERSION each time the metadata is updated + uint32_t crc; //stores the zlib 32 bit crc of the AdbBackupStreamHeader struct to allow for making sure we are processing metadata + char space[468]; //stores space to align the struct to 512 bytes +}; + +#endif //__TWADBSTREAM_H diff --git a/adbbu/twrpback.cpp b/adbbu/twrpback.cpp new file mode 100644 index 000000000..b3de76db6 --- /dev/null +++ b/adbbu/twrpback.cpp @@ -0,0 +1,935 @@ +/* + Copyright 2013 to 2017 TeamWin + 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 <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/select.h> +#include <sys/time.h> +#include <zlib.h> +#include <ctype.h> +#include <semaphore.h> +#include <string> +#include <sstream> +#include <fstream> +#include <algorithm> +#include <utils/threads.h> +#include <pthread.h> + +#include "twadbstream.h" +#include "twrpback.hpp" +#include "libtwadbbu.hpp" +#include "../twrpDigest/twrpDigest.hpp" +#include "../twrpDigest/twrpMD5.hpp" +#include "../twrpAdbBuFifo.hpp" + +twrpback::twrpback(void) { + adbd_fp = NULL; + read_fd = 0; + write_fd = 0; + adb_control_twrp_fd = 0; + adb_control_bu_fd = 0; + adb_read_fd = 0; + adb_write_fd = 0; + ors_fd = 0; + debug_adb_fd = 0; + firstPart = true; + createFifos(); + adbloginit(); +} + +twrpback::~twrpback(void) { + adblogfile.close(); + closeFifos(); +} + +void twrpback::printErrMsg(std::string msg, int errNum) { + std::stringstream str; + str << strerror(errNum); + adblogwrite(msg + " " + str.str() + "\n"); +} + +void twrpback::createFifos(void) { + if (mkfifo(TW_ADB_BU_CONTROL, 0666) < 0) { + std::string msg = "Unable to create TW_ADB_BU_CONTROL fifo: "; + printErrMsg(msg, errno); + } + if (mkfifo(TW_ADB_TWRP_CONTROL, 0666) < 0) { + std::string msg = "Unable to create TW_ADB_TWRP_CONTROL fifo: "; + printErrMsg(msg, errno); + unlink(TW_ADB_BU_CONTROL); + } +} + +void twrpback::closeFifos(void) { + if (unlink(TW_ADB_BU_CONTROL) < 0) { + std::string msg = "Unable to remove TW_ADB_BU_CONTROL: "; + printErrMsg(msg, errno); + } + if (unlink(TW_ADB_TWRP_CONTROL) < 0) { + std::string msg = "Unable to remove TW_ADB_TWRP_CONTROL: "; + printErrMsg(msg, errno); + } +} + +void twrpback::adbloginit(void) { + adblogfile.open("/tmp/adb.log", std::fstream::app); +} + +void twrpback::adblogwrite(std::string writemsg) { + adblogfile << writemsg << std::flush; +} + +void twrpback::close_backup_fds() { + if (ors_fd > 0) + close(ors_fd); + if (write_fd > 0) + close(write_fd); + if (adb_read_fd > 0) + close(adb_read_fd); + if (adb_control_bu_fd > 0) + close(adb_control_bu_fd); + #ifdef _DEBUG_ADB_BACKUP + if (debug_adb_fd > 0) + close(debug_adb_fd); + #endif + if (adbd_fp != NULL) + fclose(adbd_fp); + if (access(TW_ADB_BACKUP, F_OK) == 0) + unlink(TW_ADB_BACKUP); +} + +void twrpback::close_restore_fds() { + if (ors_fd > 0) + close(ors_fd); + if (write_fd > 0) + close(write_fd); + if (adb_control_bu_fd > 0) + close(adb_control_bu_fd); + if (adb_control_twrp_fd > 0) + close(adb_control_twrp_fd); + if (adbd_fp != NULL) + fclose(adbd_fp); + if (access(TW_ADB_RESTORE, F_OK) == 0) + unlink(TW_ADB_RESTORE); + #ifdef _DEBUG_ADB_BACKUP + if (debug_adb_fd > 0) + close(debug_adb_fd); + #endif +} + +bool twrpback::backup(std::string command) { + twrpMD5 digest; + int bytes = 0, errctr = 0; + char adbReadStream[MAX_ADB_READ]; + uint64_t totalbytes = 0, dataChunkBytes = 0, fileBytes = 0; + uint64_t md5fnsize = 0; + struct AdbBackupControlType endadb; + + //ADBSTRUCT_STATIC_ASSERT(sizeof(endadb) == MAX_ADB_READ); + + bool writedata = true; + bool compressed = false; + bool firstDataPacket = true; + + adbd_fp = fdopen(adbd_fd, "w"); + if (adbd_fp == NULL) { + adblogwrite("Unable to open adb_fp\n"); + return false; + } + + if (mkfifo(TW_ADB_BACKUP, 0666) < 0) { + adblogwrite("Unable to create TW_ADB_BACKUP fifo\n"); + return false; + } + + adblogwrite("opening TW_ADB_FIFO\n"); + + write_fd = open(TW_ADB_FIFO, O_WRONLY); + while (write_fd < 0) { + write_fd = open(TW_ADB_FIFO, O_WRONLY); + usleep(10000); + errctr++; + if (errctr > ADB_BU_MAX_ERROR) { + std::string msg = "Unable to open TW_ADB_FIFO"; + printErrMsg(msg, errno); + close_backup_fds(); + return false; + } + } + + memset(operation, 0, sizeof(operation)); + if (snprintf(operation, sizeof(operation), "adbbackup %s", command.c_str()) >= (int)sizeof(operation)) { + adblogwrite("Operation too big to write to ORS_INPUT_FILE\n"); + close_backup_fds(); + return false; + } + if (write(write_fd, operation, sizeof(operation)) != sizeof(operation)) { + adblogwrite("Unable to write to ORS_INPUT_FILE\n"); + close_backup_fds(); + return false; + } + + memset(&adbReadStream, 0, sizeof(adbReadStream)); + memset(&cmd, 0, sizeof(cmd)); + + adblogwrite("opening TW_ADB_BU_CONTROL\n"); + adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_RDONLY | O_NONBLOCK); + if (adb_control_bu_fd < 0) { + adblogwrite("Unable to open TW_ADB_BU_CONTROL for reading.\n"); + close_backup_fds(); + return false; + } + + adblogwrite("opening TW_ADB_BACKUP\n"); + adb_read_fd = open(TW_ADB_BACKUP, O_RDONLY | O_NONBLOCK); + if (adb_read_fd < 0) { + adblogwrite("Unable to open TW_ADB_BACKUP for reading.\n"); + close_backup_fds(); + return false; + } + + //loop until TWENDADB sent + while (true) { + if (read(adb_control_bu_fd, &cmd, sizeof(cmd)) > 0) { + struct AdbBackupControlType structcmd; + + memcpy(&structcmd, cmd, sizeof(cmd)); + std::string cmdtype = structcmd.get_type(); + + //we received an error, exit and unlink + if (cmdtype == TWERROR) { + writedata = false; + adblogwrite("Error received. Quitting...\n"); + close_backup_fds(); + return false; + } + //we received the end of adb backup stream so we should break the loop + else if (cmdtype == TWENDADB) { + writedata = false; + adblogwrite("Recieved TWENDADB\n"); + memcpy(&endadb, cmd, sizeof(cmd)); + std::stringstream str; + str << totalbytes; + adblogwrite(str.str() + " total bytes written\n"); + break; + } + //we recieved the TWSTREAMHDR structure metadata to write to adb + else if (cmdtype == TWSTREAMHDR) { + writedata = false; + adblogwrite("writing TWSTREAMHDR\n"); + if (fwrite(cmd, 1, sizeof(cmd), adbd_fp) != sizeof(cmd)) { + std::string msg = "Error writing TWSTREAMHDR to adbd"; + printErrMsg(msg, errno); + close_backup_fds(); + return false; + } + fflush(adbd_fp); + } + //we will be writing an image from TWRP + else if (cmdtype == TWIMG) { + struct twfilehdr twimghdr; + + adblogwrite("writing TWIMG\n"); + digest.init(); + memset(&twimghdr, 0, sizeof(twimghdr)); + memcpy(&twimghdr, cmd, sizeof(cmd)); + md5fnsize = twimghdr.size; + compressed = false; + + #ifdef _DEBUG_ADB_BACKUP + std::string debug_fname = "/data/media/"; + debug_fname.append(basename(twimghdr.name)); + debug_fname.append("-backup.img"); + debug_adb_fd = open(debug_fname.c_str(), O_WRONLY | O_CREAT, 0666); + adblogwrite("Opening adb debug tar\n"); + #endif + + if (fwrite(cmd, 1, sizeof(cmd), adbd_fp) != sizeof(cmd)) { + adblogwrite("Error writing TWIMG to adbd\n"); + close_backup_fds(); + return false; + } + fflush(adbd_fp); + writedata = true; + } + //we will be writing a tar from TWRP + else if (cmdtype == TWFN) { + struct twfilehdr twfilehdr; + + adblogwrite("writing TWFN\n"); + digest.init(); + + //ADBSTRUCT_STATIC_ASSERT(sizeof(twfilehdr) == MAX_ADB_READ); + + memset(&twfilehdr, 0, sizeof(twfilehdr)); + memcpy(&twfilehdr, cmd, sizeof(cmd)); + md5fnsize = twfilehdr.size; + + compressed = twfilehdr.compressed == 1 ? true: false; + + #ifdef _DEBUG_ADB_BACKUP + std::string debug_fname = "/data/media/"; + debug_fname.append(basename(twfilehdr.name)); + debug_fname.append("-backup.tar"); + debug_adb_fd = open(debug_fname.c_str(), O_WRONLY | O_CREAT, 0666); + adblogwrite("Opening adb debug tar\n"); + #endif + + if (fwrite(cmd, 1, sizeof(cmd), adbd_fp) != sizeof(cmd)) { + adblogwrite("Error writing TWFN to adbd\n"); + close_backup_fds(); + return false; + } + fflush(adbd_fp); + writedata = true; + } + /* + We received the command that we are done with the file stream. + We will flush the remaining data stream. + Update md5 and write final results to adb stream. + If we need padding because the total bytes are not a multiple + of 512, we pad the end with 0s to we reach 512. + We also write the final md5 to the adb stream. + */ + else if (cmdtype == TWEOF) { + adblogwrite("received TWEOF\n"); + while ((bytes = read(adb_read_fd, &adbReadStream, sizeof(adbReadStream)) != 0)) { + totalbytes += bytes; + fileBytes += bytes; + dataChunkBytes += bytes; + + char *writeAdbReadStream = new char [bytes]; + memcpy(writeAdbReadStream, adbReadStream, bytes); + + digest.update((unsigned char *) writeAdbReadStream, bytes); + if (fwrite(writeAdbReadStream, 1, bytes, adbd_fp) < 0) { + std::string msg = "Cannot write to adbd stream: "; + printErrMsg(msg, errno); + } + #if defined(_DEBUG_ADB_BACKUP) + if (write(debug_adb_fd, writeAdbReadStream, bytes) < 1) { + std::string msg = "Cannot write to ADB_CONTROL_READ_FD: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + #endif + fflush(adbd_fp); + delete [] writeAdbReadStream; + memset(adbReadStream, 0, sizeof(adbReadStream)); + } + + if (fileBytes % DATA_MAX_CHUNK_SIZE != 0) { + int64_t count = fileBytes / DATA_MAX_CHUNK_SIZE + 1; + uint64_t ceilingBytes = count * DATA_MAX_CHUNK_SIZE; + char padding[ceilingBytes - fileBytes]; + int paddingBytes = sizeof(padding); + memset(padding, 0, paddingBytes); + std::stringstream paddingStr; + paddingStr << paddingBytes; + adblogwrite("writing padding to stream: " + paddingStr.str() + " bytes\n"); + if (fwrite(padding, 1, paddingBytes, adbd_fp) != sizeof(padding)) { + adblogwrite("Error writing padding to adbd\n"); + close_backup_fds(); + return false; + } + #if defined(_DEBUG_ADB_BACKUP) + if (write(debug_adb_fd, padding, paddingBytes) < 1) { + std::string msg = "Cannot write to ADB_CONTROL_READ_FD: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + #endif + totalbytes += paddingBytes; + digest.update((unsigned char *) padding, paddingBytes); + fflush(adbd_fp); + } + + AdbBackupFileTrailer md5trailer; + + memset(&md5trailer, 0, sizeof(md5trailer)); + + std::string md5string = digest.return_digest_string(); + + strncpy(md5trailer.start_of_trailer, TWRP, sizeof(md5trailer.start_of_trailer)); + strncpy(md5trailer.type, MD5TRAILER, sizeof(md5trailer.type)); + strncpy(md5trailer.md5, md5string.c_str(), sizeof(md5trailer.md5)); + + md5trailer.crc = crc32(0L, Z_NULL, 0); + md5trailer.crc = crc32(md5trailer.crc, (const unsigned char*) &md5trailer, sizeof(md5trailer)); + + md5trailer.ident = crc32(0L, Z_NULL, 0); + md5trailer.ident = crc32(md5trailer.ident, (const unsigned char*) &md5trailer, sizeof(md5trailer)); + md5trailer.ident = crc32(md5trailer.ident, (const unsigned char*) &md5fnsize, sizeof(md5fnsize)); + + if (fwrite(&md5trailer, 1, sizeof(md5trailer), adbd_fp) != sizeof(md5trailer)) { + adblogwrite("Error writing md5trailer to adbd\n"); + close_backup_fds(); + return false; + } + fflush(adbd_fp); + writedata = false; + firstDataPacket = true; + fileBytes = 0; + } + memset(&cmd, 0, sizeof(cmd)); + dataChunkBytes = 0; + } + //If we are to write data because of a new file stream, lets write all the data. + //This will allow us to not write data after a command structure has been written + //to the adb stream. + //If the stream is compressed, we need to always write the data. + if (writedata || compressed) { + while ((bytes = read(adb_read_fd, &adbReadStream, sizeof(adbReadStream))) > 0) { + if (firstDataPacket) { + if (!twadbbu::Write_TWDATA(adbd_fp)) { + close_backup_fds(); + return false; + } + fileBytes += MAX_ADB_READ; + fflush(adbd_fp); + firstDataPacket = false; + dataChunkBytes += sizeof(adbReadStream); + } + char *writeAdbReadStream = new char [bytes]; + memcpy(writeAdbReadStream, adbReadStream, bytes); + + digest.update((unsigned char *) writeAdbReadStream, bytes); + + totalbytes += bytes; + fileBytes += bytes; + dataChunkBytes += bytes; + + if (fwrite(writeAdbReadStream, 1, bytes, adbd_fp) != (unsigned long long)bytes) { + adblogwrite("Error writing backup data to adbd\n"); + close_backup_fds(); + return false; + } + #ifdef _DEBUG_ADB_BACKUP + if (write(debug_adb_fd, writeAdbReadStream, bytes) < 1) { + std::string msg = "Cannot write to ADB_CONTROL_READ_FD: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + #endif + fflush(adbd_fp); + delete [] writeAdbReadStream; + + memset(&adbReadStream, 0, sizeof(adbReadStream)); + + if (dataChunkBytes == DATA_MAX_CHUNK_SIZE) { + dataChunkBytes = 0; + firstDataPacket = true; + } + else if (dataChunkBytes > (DATA_MAX_CHUNK_SIZE - sizeof(adbReadStream))) { + int bytesLeft = DATA_MAX_CHUNK_SIZE - dataChunkBytes; + char extraData[bytesLeft]; + + memset(&extraData, 0, bytesLeft); + while ((bytes = read(adb_read_fd, &extraData, bytesLeft)) != 0) { + if (bytes > 0) { + totalbytes += bytes; + fileBytes += bytes; + dataChunkBytes += bytes; + + bytesLeft -= bytes; + char *writeAdbReadStream = new char [bytes]; + memcpy(writeAdbReadStream, extraData, bytes); + + digest.update((unsigned char *) writeAdbReadStream, bytes); + if (fwrite(writeAdbReadStream, 1, bytes, adbd_fp) < 0) { + std::string msg = "Cannot write to adbd stream: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + #ifdef _DEBUG_ADB_BACKUP + if (write(debug_adb_fd, writeAdbReadStream, bytes) < 1) { + std::string msg = "Cannot write to ADB_CONTROL_READ_FD: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + #endif + fflush(adbd_fp); + delete [] writeAdbReadStream; + } + memset(&extraData, 0, bytesLeft); + if (bytesLeft == 0) { + break; + } + } + + fflush(adbd_fp); + dataChunkBytes = 0; + firstDataPacket = true; + } + } + } + } + + //Write the final end adb structure to the adb stream + if (fwrite(&endadb, 1, sizeof(endadb), adbd_fp) != sizeof(endadb)) { + adblogwrite("Error writing endadb to adbd\n"); + close_backup_fds(); + return false; + } + fflush(adbd_fp); + close_backup_fds(); + return true; +} + +bool twrpback::restore(void) { + twrpMD5 digest; + char cmd[MAX_ADB_READ]; + char readAdbStream[MAX_ADB_READ]; + struct AdbBackupControlType structcmd; + int errctr = 0; + uint64_t totalbytes = 0, dataChunkBytes = 0; + uint64_t md5fnsize = 0, fileBytes = 0; + bool read_from_adb; + bool md5sumdata; + bool compressed, tweofrcvd, extraData; + + read_from_adb = true; + + signal(SIGPIPE, SIG_IGN); + signal(SIGHUP, SIG_IGN); + + adbd_fp = fdopen(adbd_fd, "r"); + if (adbd_fp == NULL) { + adblogwrite("Unable to open adb_fp\n"); + close_restore_fds(); + return false; + } + + if(mkfifo(TW_ADB_RESTORE, 0666)) { + adblogwrite("Unable to create TW_ADB_RESTORE fifo\n"); + close_restore_fds(); + return false; + } + + adblogwrite("opening TW_ADB_FIFO\n"); + write_fd = open(TW_ADB_FIFO, O_WRONLY); + + while (write_fd < 0) { + write_fd = open(TW_ADB_FIFO, O_WRONLY); + errctr++; + if (errctr > ADB_BU_MAX_ERROR) { + std::string msg = "Unable to open TW_ADB_FIFO."; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + } + + memset(operation, 0, sizeof(operation)); + sprintf(operation, "adbrestore"); + if (write(write_fd, operation, sizeof(operation)) != sizeof(operation)) { + adblogwrite("Unable to write to TW_ADB_FIFO\n"); + close_restore_fds(); + return false; + } + + memset(&readAdbStream, 0, sizeof(readAdbStream)); + memset(&cmd, 0, sizeof(cmd)); + + adblogwrite("opening TW_ADB_BU_CONTROL\n"); + adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_RDONLY | O_NONBLOCK); + if (adb_control_bu_fd < 0) { + std::string msg = "Unable to open TW_ADB_BU_CONTROL for writing."; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + + adblogwrite("opening TW_ADB_TWRP_CONTROL\n"); + adb_control_twrp_fd = open(TW_ADB_TWRP_CONTROL, O_WRONLY | O_NONBLOCK); + if (adb_control_twrp_fd < 0) { + std::string msg = "Unable to open TW_ADB_TWRP_CONTROL for writing. Retrying..."; + printErrMsg(msg, errno); + while (adb_control_twrp_fd < 0) { + adb_control_twrp_fd = open(TW_ADB_TWRP_CONTROL, O_WRONLY | O_NONBLOCK); + usleep(10000); + errctr++; + if (errctr > ADB_BU_MAX_ERROR) { + adblogwrite("Unable to open TW_ADB_TWRP_CONTROL\n"); + close_backup_fds(); + return false; + } + } + } + + //Loop until we receive TWENDADB from TWRP + while (true) { + memset(&cmd, 0, sizeof(cmd)); + if (read(adb_control_bu_fd, &cmd, sizeof(cmd)) > 0) { + struct AdbBackupControlType structcmd; + memcpy(&structcmd, cmd, sizeof(cmd)); + std::string cmdtype = structcmd.get_type(); + + //If we receive TWEOF from TWRP close adb data fifo + if (cmdtype == TWEOF) { + adblogwrite("Received TWEOF\n"); + read_from_adb = true; + tweofrcvd = true; + close(adb_write_fd); + } + //Break when TWRP sends TWENDADB + else if (cmdtype == TWENDADB) { + adblogwrite("Received TWENDADB\n"); + break; + } + //we received an error, exit and unlink + else if (cmdtype == TWERROR) { + adblogwrite("Error received. Quitting...\n"); + close_restore_fds(); + return false; + } + } + //If we should read from the adb stream, write commands and data to TWRP + if (read_from_adb) { + int readbytes; + if ((readbytes = fread(readAdbStream, 1, sizeof(readAdbStream), adbd_fp)) == sizeof(readAdbStream)) { + memcpy(&structcmd, readAdbStream, sizeof(readAdbStream)); + std::string cmdtype = structcmd.get_type(); + + //Tell TWRP we have read the entire adb stream + if (cmdtype == TWENDADB) { + struct AdbBackupControlType endadb; + uint32_t crc, endadbcrc; + + md5sumdata = false; + memset(&endadb, 0, sizeof(endadb)); + memcpy(&endadb, readAdbStream, sizeof(readAdbStream)); + endadbcrc = endadb.crc; + memset(&endadb.crc, 0, sizeof(endadb.crc)); + crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (const unsigned char*) &endadb, sizeof(endadb)); + + if (crc == endadbcrc) { + adblogwrite("sending TWENDADB\n"); + if (write(adb_control_twrp_fd, &endadb, sizeof(endadb)) < 1) { + std::string msg = "Cannot write to ADB_CONTROL_READ_FD: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + read_from_adb = false; + } + else { + adblogwrite("ADB TWENDADB crc header doesn't match\n"); + close_restore_fds(); + return false; + } + } + //Send TWRP partition metadata + else if (cmdtype == TWSTREAMHDR) { + struct AdbBackupStreamHeader cnthdr; + uint32_t crc, cnthdrcrc; + + //ADBSTRUCT_STATIC_ASSERT(sizeof(cnthdr) == MAX_ADB_READ); + + md5sumdata = false; + memset(&cnthdr, 0, sizeof(cnthdr)); + memcpy(&cnthdr, readAdbStream, sizeof(readAdbStream)); + cnthdrcrc = cnthdr.crc; + memset(&cnthdr.crc, 0, sizeof(cnthdr.crc)); + crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (const unsigned char*) &cnthdr, sizeof(cnthdr)); + + if (crc == cnthdrcrc) { + adblogwrite("Restoring TWSTREAMHDR\n"); + if (write(adb_control_twrp_fd, readAdbStream, sizeof(readAdbStream)) < 0) { + std::string msg = "Cannot write to adb_control_twrp_fd: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + } + else { + adblogwrite("ADB TWSTREAMHDR crc header doesn't match\n"); + close_restore_fds(); + return false; + } + } + //Tell TWRP we are sending a partition image + else if (cmdtype == TWIMG) { + struct twfilehdr twimghdr; + uint32_t crc, twimghdrcrc; + md5sumdata = false; + fileBytes = 0; + read_from_adb = true; + dataChunkBytes = 0; + extraData = false; + + digest.init(); + adblogwrite("Restoring TWIMG\n"); + memset(&twimghdr, 0, sizeof(twimghdr)); + memcpy(&twimghdr, readAdbStream, sizeof(readAdbStream)); + md5fnsize = twimghdr.size; + twimghdrcrc = twimghdr.crc; + memset(&twimghdr.crc, 0, sizeof(twimghdr.crc)); + + crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (const unsigned char*) &twimghdr, sizeof(twimghdr)); + if (crc == twimghdrcrc) { + if (write(adb_control_twrp_fd, readAdbStream, sizeof(readAdbStream)) < 1) { + std::string msg = "Cannot write to adb_control_twrp_fd: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + } + else { + adblogwrite("ADB TWIMG crc header doesn't match\n"); + close_restore_fds(); + return false; + } + + #ifdef _DEBUG_ADB_BACKUP + std::string debug_fname = "/data/media/"; + debug_fname.append(basename(twimghdr.name)); + debug_fname.append("-restore.img"); + adblogwrite("image: " + debug_fname + "\n"); + debug_adb_fd = open(debug_fname.c_str(), O_WRONLY | O_CREAT, 0666); + adblogwrite("Opened restore image\n"); + #endif + + adblogwrite("opening TW_ADB_RESTORE\n"); + adb_write_fd = open(TW_ADB_RESTORE, O_WRONLY); + } + //Tell TWRP we are sending a tar stream + else if (cmdtype == TWFN) { + struct twfilehdr twfilehdr; + uint32_t crc, twfilehdrcrc; + fileBytes = 0; + md5sumdata = false; + read_from_adb = true; + dataChunkBytes = 0; + extraData = false; + + digest.init(); + adblogwrite("Restoring TWFN\n"); + memset(&twfilehdr, 0, sizeof(twfilehdr)); + memcpy(&twfilehdr, readAdbStream, sizeof(readAdbStream)); + md5fnsize = twfilehdr.size; + twfilehdrcrc = twfilehdr.crc; + memset(&twfilehdr.crc, 0, sizeof(twfilehdr.crc)); + + crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (const unsigned char*) &twfilehdr, sizeof(twfilehdr)); + + if (crc == twfilehdrcrc) { + if (write(adb_control_twrp_fd, readAdbStream, sizeof(readAdbStream)) < 1) { + std::string msg = "Cannot write to adb_control_twrp_fd: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + } + else { + adblogwrite("ADB TWFN crc header doesn't match\n"); + close_restore_fds(); + return false; + } + + #ifdef _DEBUG_ADB_BACKUP + std::string debug_fname = "/data/media/"; + debug_fname.append(basename(twfilehdr.name)); + debug_fname.append("-restore.tar"); + adblogwrite("tar: " + debug_fname + "\n"); + debug_adb_fd = open(debug_fname.c_str(), O_WRONLY | O_CREAT, 0666); + adblogwrite("Opened restore tar\n"); + #endif + + compressed = twfilehdr.compressed == 1 ? true: false; + adblogwrite("opening TW_ADB_RESTORE\n"); + adb_write_fd = open(TW_ADB_RESTORE, O_WRONLY); + } + else if (cmdtype == MD5TRAILER) { + if (fileBytes >= md5fnsize) + close(adb_write_fd); + if (tweofrcvd) { + read_from_adb = true; + tweofrcvd = false; + } + else + read_from_adb = false; //don't read from adb until TWRP sends TWEOF + md5sumdata = false; + if (!checkMD5Trailer(readAdbStream, md5fnsize, &digest)) { + close_restore_fds(); + break; + } + continue; + } + //Send the tar or partition image md5 to TWRP + else if (cmdtype == TWDATA) { + dataChunkBytes += sizeof(readAdbStream); + while (true) { + if ((readbytes = fread(readAdbStream, 1, sizeof(readAdbStream), adbd_fp)) != sizeof(readAdbStream)) { + close_restore_fds(); + return false; + } + memcpy(&structcmd, readAdbStream, sizeof(readAdbStream)); + std::string cmdtype = structcmd.get_type(); + + dataChunkBytes += readbytes; + totalbytes += readbytes; + fileBytes += readbytes; + + if (cmdtype == MD5TRAILER) { + if (fileBytes >= md5fnsize) + close(adb_write_fd); + if (tweofrcvd) { + tweofrcvd = false; + read_from_adb = true; + } + else + read_from_adb = false; //don't read from adb until TWRP sends TWEOF + if (!checkMD5Trailer(readAdbStream, md5fnsize, &digest)) { + close_restore_fds(); + break; + } + break; + } + + digest.update((unsigned char*)readAdbStream, readbytes); + + read_from_adb = true; + + #ifdef _DEBUG_ADB_BACKUP + if (write(debug_adb_fd, readAdbStream, sizeof(readAdbStream)) < 0) { + std::string msg = "Cannot write to ADB_CONTROL_READ_FD: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + #endif + + if (write(adb_write_fd, readAdbStream, sizeof(readAdbStream)) < 0) { + std::string msg = "Cannot write to TWRP ADB FIFO: "; + md5sumdata = true; + printErrMsg(msg, errno); + adblogwrite("end of stream reached.\n"); + break; + } + + if (dataChunkBytes == DATA_MAX_CHUNK_SIZE) { + dataChunkBytes = 0; + md5sumdata = false; + break; + } + } + } + else if (md5sumdata) { + digest.update((unsigned char*)readAdbStream, sizeof(readAdbStream)); + md5sumdata = true; + } + } + } + } + std::stringstream str; + str << totalbytes; + close_restore_fds(); + adblogwrite(str.str() + " bytes restored from adbbackup\n"); + return true; +} + +void twrpback::streamFileForTWRP(void) { + adblogwrite("streamFileForTwrp" + streamFn + "\n"); +} + +void twrpback::setStreamFileName(std::string fn) { + streamFn = fn; + adbd_fd = open(fn.c_str(), O_RDONLY); + if (adbd_fd < 0) { + adblogwrite("Unable to open adb_fd\n"); + close(adbd_fd); + return; + } + restore(); +} + +void twrpback::threadStream(void) { + pthread_t thread; + ThreadPtr streamPtr = &twrpback::streamFileForTWRP; + PThreadPtr p = *(PThreadPtr*)&streamPtr; + pthread_create(&thread, NULL, p, this); + pthread_join(thread, NULL); +} + +bool twrpback::checkMD5Trailer(char readAdbStream[], uint64_t md5fnsize, twrpMD5 *digest) { + struct AdbBackupFileTrailer md5tr; + uint32_t crc, md5trcrc, md5ident, md5identmatch; + + //ADBSTRUCT_STATIC_ASSERT(sizeof(md5tr) == MAX_ADB_READ); + memcpy(&md5tr, readAdbStream, MAX_ADB_READ); + md5ident = md5tr.ident; + + memset(&md5tr.ident, 0, sizeof(md5tr.ident)); + + md5identmatch = crc32(0L, Z_NULL, 0); + md5identmatch = crc32(md5identmatch, (const unsigned char*) &md5tr, sizeof(md5tr)); + md5identmatch = crc32(md5identmatch, (const unsigned char*) &md5fnsize, sizeof(md5fnsize)); + + if (md5identmatch == md5ident) { + adblogwrite("checking MD5TRAILER\n"); + md5trcrc = md5tr.crc; + memset(&md5tr.crc, 0, sizeof(md5tr.crc)); + crc = crc32(0L, Z_NULL, 0); + crc = crc32(crc, (const unsigned char*) &md5tr, sizeof(md5tr)); + if (crc == md5trcrc) { + if (write(adb_control_twrp_fd, &md5tr, sizeof(md5tr)) < 1) { + std::string msg = "Cannot write to adb_control_twrp_fd: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + } + else { + adblogwrite("ADB MD5TRAILER crc header doesn't match\n"); + close_restore_fds(); + return false; + } + + AdbBackupFileTrailer md5; + + memset(&md5, 0, sizeof(md5)); + strncpy(md5.start_of_trailer, TWRP, sizeof(md5.start_of_trailer)); + strncpy(md5.type, TWMD5, sizeof(md5.type)); + std::string md5string = digest->return_digest_string(); + strncpy(md5.md5, md5string.c_str(), sizeof(md5.md5)); + + adblogwrite("sending MD5 verification: " + md5string + "\n"); + if (write(adb_control_twrp_fd, &md5, sizeof(md5)) < 1) { + std::string msg = "Cannot write to adb_control_twrp_fd: "; + printErrMsg(msg, errno); + close_restore_fds(); + return false; + } + return true; + } + return false; +} diff --git a/adbbu/twrpback.hpp b/adbbu/twrpback.hpp new file mode 100644 index 000000000..edc162651 --- /dev/null +++ b/adbbu/twrpback.hpp @@ -0,0 +1,61 @@ +/* + Copyright 2013 to 2017 TeamWin + 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/>. +*/ + +#ifndef _TWRPBACK_HPP +#define _TWRPBACK_HPP + +#include <fstream> +#include "../twrpDigest/twrpMD5.hpp" + +class twrpback { +public: + int adbd_fd; // adbd data stream + twrpback(void); + virtual ~twrpback(void); + bool backup(std::string command); // adb backup stream + bool restore(void); // adb restore stream + void adblogwrite(std::string writemsg); // adb debugging log function + void createFifos(void); // create fifos needed for adb backup + void closeFifos(void); // close created fifos + void streamFileForTWRP(void); // stream file to twrp via bu + void setStreamFileName(std::string fn); // tell adb backup what file to load on storage + void threadStream(void); // thread bu for streaming + +private: + int read_fd; // ors input fd + int write_fd; // ors operation fd + int ors_fd; // ors output fd + int adb_control_twrp_fd; // fd for bu to twrp communication + int adb_control_bu_fd; // fd for twrp to bu communication + int adb_read_fd; // adb read data stream + int adb_write_fd; // adb write data stream + int debug_adb_fd; // fd to write debug tars + bool firstPart; // first partition in the stream + FILE *adbd_fp; // file pointer for adb stream + char cmd[512]; // store result of commands + char operation[512]; // operation to send to ors + std::ofstream adblogfile; // adb stream log file + std::string streamFn; + typedef void (twrpback::*ThreadPtr)(void); + typedef void* (*PThreadPtr)(void *); + void adbloginit(void); // setup adb log stream file + void close_backup_fds(); // close backup resources + void close_restore_fds(); // close restore resources + bool checkMD5Trailer(char adbReadStream[], uint64_t md5fnsize, twrpMD5* digest); // Check MD5 Trailer + void printErrMsg(std::string msg, int errNum); // print error msg to adb log +}; + +#endif // _TWRPBACK_HPP |