Skip to content
Permalink
Browse files

ADB: Add adb backup for TWRP.

Functionality for client side to backup
tar and image streams over adbd to the client under backup.ab.

Using adb backup on the client side you can backup the partitions
TWRP knows about.

On the client side you can do the following:
adb backup -f <filename> --twrp <options> where options are
--compress: compress data
system: backup system
cache: backup cache
data: backup data
boot: backup boot
etc for each partition.

You can string multiple options,
i.e. adb backup -f <filename> --twrp --compress cache system data

adb backup in TWRP will take any option corresponding
to TWRP fstab partitions, e.g. efs boot as well.

If you do not specify the filename with the -f option,
adb will backup your data to a filename backup.ab on the client.
You can then rename the file and encrypt it with desktop tools.

If you don't want to use command line arguments:
adb backup --twrp

will bring up the gui and allow you to choose partitions
from the backup page.

To restore the backup use the following convention:
adb restore <filename>

Structures are used to store metadata in binary inside
of the file itself. If the metadata structure is modified,
update the adb version so that it will invalidate older
backups and not cause issues on restore. When restoring,
we currently do not support picking specific partitions.
It's all or nothing.

Change-Id: Idb92c37fc9801dc8d89ed2a4570e9d12e76facf8
  • Loading branch information
bigbiff authored and Dees-Troy committed Aug 1, 2016
1 parent 0ac2293 commit ce8f83c48d200106ff61ad530c863b15c16949d9
@@ -100,7 +100,8 @@ LOCAL_C_INCLUDES += \
system/vold \
system/extras/ext4_utils \
system/core/adb \
system/core/libsparse
system/core/libsparse \
external/zlib

LOCAL_C_INCLUDES += bionic external/openssl/include $(LOCAL_PATH)/libmincrypt/includes
ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
@@ -111,7 +112,7 @@ LOCAL_STATIC_LIBRARIES :=
LOCAL_SHARED_LIBRARIES :=

LOCAL_STATIC_LIBRARIES += libguitwrp
LOCAL_SHARED_LIBRARIES += libz libc libcutils libstdc++ libtar libblkid libminuitwrp libminadbd libmtdutils libminzip libaosprecovery
LOCAL_SHARED_LIBRARIES += libz libc libcutils libstdc++ libtar libblkid libminuitwrp libminadbd libmtdutils libminzip libaosprecovery libtwadbbu
LOCAL_SHARED_LIBRARIES += libcrecovery

ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
@@ -588,6 +589,7 @@ include $(commands_recovery_local_path)/injecttwrp/Android.mk \
$(commands_recovery_local_path)/etc/Android.mk \
$(commands_recovery_local_path)/toybox/Android.mk \
$(commands_recovery_local_path)/simg2img/Android.mk \
$(commands_recovery_local_path)/adbbu/Android.mk \
$(commands_recovery_local_path)/libpixelflinger/Android.mk

ifeq ($(TW_INCLUDE_CRYPTO), true)
@@ -0,0 +1,45 @@
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES:= \
twrpback.cpp \
../twrpDigest.cpp \
../digest/md5.c
LOCAL_SHARED_LIBRARIES += libstdc++ libz
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)

include $(CLEAR_VARS)
LOCAL_MODULE := libtwadbbu
LOCAL_MODULE_TAGS := optional
LOCAL_CFLAGS = -D_FILE_OFFSET_BITS=64 -DMTP_DEVICE -DMTP_HOST -fno-strict-aliasing
LOCAL_C_INCLUDES += $(LOCAL_PATH) bionic frameworks/base/include system/core/include bionic/libc/private/
ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
LOCAL_C_INCLUDES += external/stlport/stlport
endif

LOCAL_SRC_FILES = \
libtwadbbu.cpp

LOCAL_SHARED_LIBRARIES += libz libc libstdc++

ifeq ($(shell test $(PLATFORM_SDK_VERSION) -lt 23; echo $$?),0)
LOCAL_SHARED_LIBRARIES += libstlport
else
LOCAL_SHARED_LIBRARIES += libc++
endif

include $(BUILD_SHARED_LIBRARY)
@@ -0,0 +1,196 @@
/*
Copyright 2013 to 2016 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 <fstream>
#include <sstream>

#include "twadbstream.h"
#include "libtwadbbu.hpp"

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.\n");
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\n");
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\n");
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.\n");
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\n");
close(adb_control_bu_fd);
return false;
}
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\n");
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");
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;
}
@@ -0,0 +1,44 @@
/*
Copyright 2013 to 2016 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 <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <string>
#include <fstream>
#include <sstream>

#include "twadbstream.h"
#include "twrpback.hpp"

class twadbbu {
public:
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
};

1 comment on commit ce8f83c

@q996467794

This comment has been minimized.

Copy link

@q996467794 q996467794 commented on ce8f83c Aug 29, 2016

Failure cause subpartitions

Please sign in to comment.
You can’t perform that action at this time.