summaryrefslogtreecommitdiffstats
path: root/adbbu/libtwadbbu.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--adbbu/libtwadbbu.cpp112
1 files changed, 104 insertions, 8 deletions
diff --git a/adbbu/libtwadbbu.cpp b/adbbu/libtwadbbu.cpp
index cdc2170ba..a13ecb2c3 100644
--- a/adbbu/libtwadbbu.cpp
+++ b/adbbu/libtwadbbu.cpp
@@ -1,5 +1,5 @@
/*
- Copyright 2013 to 2016 TeamWin
+ 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
@@ -27,12 +27,107 @@
#include <sys/select.h>
#include <sys/time.h>
#include <string>
+#include <vector>
#include <fstream>
#include <sstream>
#include "twadbstream.h"
#include "libtwadbbu.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)) < 0) {
+ printf("Unable to memcpy: %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 (1) {
+ 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;
@@ -40,7 +135,7 @@ bool twadbbu::Write_ADB_Stream_Header(uint64_t partition_count) {
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");
+ printf("Cannot write to TW_ADB_BU_CONTROL: %s\n", strerror(errno));
return false;
}
@@ -52,7 +147,7 @@ bool twadbbu::Write_ADB_Stream_Header(uint64_t partition_count) {
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");
+ printf("Cannot write to adb control channel: %s\n", strerror(errno));
close(adb_control_bu_fd);
return false;
}
@@ -67,7 +162,7 @@ bool twadbbu::Write_ADB_Stream_Trailer() {
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");
+ printf("Error opening adb_control_bu_fd: %s\n", strerror(errno));
return false;
}
strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header));
@@ -75,7 +170,7 @@ bool twadbbu::Write_ADB_Stream_Trailer() {
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");
+ printf("Cannot write to ADB control: %s\n", strerror(errno));
close(adb_control_bu_fd);
return false;
}
@@ -97,10 +192,11 @@ bool twadbbu::Write_TWFN(std::string Backup_FileName, uint64_t file_size, bool u
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");
+ 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;
}
@@ -118,7 +214,7 @@ bool twadbbu::Write_TWIMG(std::string Backup_FileName, uint64_t file_size) {
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");
+ printf("Cannot write to adb control channel: %s\n", strerror(errno));
return false;
}
@@ -168,7 +264,7 @@ bool twadbbu::Write_TWERROR() {
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");
+ printf("Cannot write to adb control channel: %s\n", strerror(errno));
return false;
}
close(adb_control_bu_fd);