1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/*
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 "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;
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;
}
|