blob: 0c7f355fcfd16331ae49d31aabeda65d4c8d4ee6 [file] [log] [blame]
bigbiffce8f83c2015-12-12 18:30:21 -05001/*
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002 Copyright 2013 to 2017 TeamWin
bigbiffce8f83c2015-12-12 18:30:21 -05003 TWRP is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
7
8 TWRP is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
15*/
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <unistd.h>
21#include <string.h>
22#include <fcntl.h>
23#include <errno.h>
24#include <zlib.h>
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <sys/select.h>
28#include <sys/time.h>
29#include <string>
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040030#include <vector>
bigbiffce8f83c2015-12-12 18:30:21 -050031#include <fstream>
32#include <sstream>
33
34#include "twadbstream.h"
35#include "libtwadbbu.hpp"
bigbiff bigbiffadcb4d82017-09-25 10:51:56 -040036#include "twrpback.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050037
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -040038bool twadbbu::Check_ADB_Backup_File(std::string fname) {
39 struct AdbBackupStreamHeader adbbuhdr;
40 uint32_t crc, adbbuhdrcrc;
41 unsigned char buf[MAX_ADB_READ];
42 int bytes;
43
44 int fd = open(fname.c_str(), O_RDONLY);
45 if (fd < 0) {
46 printf("Unable to open %s for reading: %s.\n", fname.c_str(), strerror(errno));
47 close(fd);
48 return false;
49 }
50 bytes = read(fd, &buf, sizeof(buf));
51 close(fd);
52
53 if (memcpy(&adbbuhdr, buf, sizeof(adbbuhdr)) < 0) {
54 printf("Unable to memcpy: %s.\n", fname.c_str(), strerror(errno));
55 return false;
56 }
57 adbbuhdrcrc = adbbuhdr.crc;
58 memset(&adbbuhdr.crc, 0, sizeof(adbbuhdr.crc));
59 crc = crc32(0L, Z_NULL, 0);
60 crc = crc32(crc, (const unsigned char*) &adbbuhdr, sizeof(adbbuhdr));
61
62 return (crc == adbbuhdrcrc);
63}
64
65std::vector<std::string> twadbbu::Get_ADB_Backup_Files(std::string fname) {
66 unsigned char buf[MAX_ADB_READ];
67 struct AdbBackupControlType structcmd;
68 std::vector<std::string> adb_partitions;
69
70 int fd = open(fname.c_str(), O_RDONLY);
71 if (fd < 0) {
72 printf("Unable to open %s for reading: %s\n", fname.c_str(), strerror(errno));
73 close(fd);
74 return std::vector<std::string>();
75 }
76
77 while (1) {
78 std::string cmdstr;
79 int readbytes;
80 if (readbytes = read(fd, &buf, sizeof(buf)) > 0) {
81 memcpy(&structcmd, buf, sizeof(structcmd));
82 assert(structcmd.type == TWENDADB || structcmd.type == TWIMG || structcmd.type == TWFN);
83 cmdstr = structcmd.type;
84 std::string cmdtype = cmdstr.substr(0, sizeof(structcmd.type) - 1);
85 if (cmdtype == TWENDADB) {
86 struct AdbBackupControlType endadb;
87 uint32_t crc, endadbcrc;
88
89 memcpy(&endadb, buf, sizeof(endadb));
90 endadbcrc = endadb.crc;
91 memset(&endadb.crc, 0, sizeof(endadb.crc));
92 crc = crc32(0L, Z_NULL, 0);
93 crc = crc32(crc, (const unsigned char*) &endadb, sizeof(endadb));
94
95 if (crc == endadbcrc) {
96 break;
97 }
98 else {
99 printf("ADB TWENDADB crc header doesn't match\n");
100 close(fd);
101 return std::vector<std::string>();
102 }
103 }
104 else if (cmdtype == TWIMG || cmdtype == TWFN) {
105 struct twfilehdr twfilehdr;
106 uint32_t crc, twfilehdrcrc;
107
108 memcpy(&twfilehdr, buf, sizeof(twfilehdr));
109 twfilehdrcrc = twfilehdr.crc;
110 memset(&twfilehdr.crc, 0, sizeof(twfilehdr.crc));
111
112 crc = crc32(0L, Z_NULL, 0);
113 crc = crc32(crc, (const unsigned char*) &twfilehdr, sizeof(twfilehdr));
114 if (crc == twfilehdrcrc) {
115 std::string adbfile = twfilehdr.name;
116 int pos = adbfile.find_last_of("/") + 1;
117 adbfile = adbfile.substr(pos, adbfile.size());
118 adb_partitions.push_back(adbfile);
119 }
120 else {
121 printf("ADB crc header doesn't match\n");
122 close(fd);
123 return std::vector<std::string>();
124 }
125 }
126 }
127 }
128 close(fd);
129 return adb_partitions;
130}
131
bigbiffce8f83c2015-12-12 18:30:21 -0500132bool twadbbu::Write_ADB_Stream_Header(uint64_t partition_count) {
133 struct AdbBackupStreamHeader twhdr;
134 int adb_control_bu_fd;
135
136 memset(&twhdr, 0, sizeof(twhdr));
137 adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
138 if (adb_control_bu_fd < 0) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400139 printf("Cannot write to TW_ADB_BU_CONTROL: %s\n", strerror(errno));
bigbiffce8f83c2015-12-12 18:30:21 -0500140 return false;
141 }
142
143 strncpy(twhdr.start_of_header, TWRP, sizeof(twhdr.start_of_header));
144 strncpy(twhdr.type, TWSTREAMHDR, sizeof(twhdr.type));
145 twhdr.partition_count = partition_count;
146 twhdr.version = ADB_BACKUP_VERSION;
147 memset(twhdr.space, 0, sizeof(twhdr.space));
148 twhdr.crc = crc32(0L, Z_NULL, 0);
149 twhdr.crc = crc32(twhdr.crc, (const unsigned char*) &twhdr, sizeof(twhdr));
150 if (write(adb_control_bu_fd, &twhdr, sizeof(twhdr)) < 0) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400151 printf("Cannot write to adb control channel: %s\n", strerror(errno));
bigbiffce8f83c2015-12-12 18:30:21 -0500152 close(adb_control_bu_fd);
153 return false;
154 }
155 return true;
156}
157
158bool twadbbu::Write_ADB_Stream_Trailer() {
159 int adb_control_bu_fd;
160 struct AdbBackupControlType endadb;
161
162 memset(&endadb, 0, sizeof(endadb));
163
164 adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY);
165 if (adb_control_bu_fd < 0) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400166 printf("Error opening adb_control_bu_fd: %s\n", strerror(errno));
bigbiffce8f83c2015-12-12 18:30:21 -0500167 return false;
168 }
169 strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header));
170 strncpy(endadb.type, TWENDADB, sizeof(endadb.type));
171 endadb.crc = crc32(0L, Z_NULL, 0);
172 endadb.crc = crc32(endadb.crc, (const unsigned char*) &endadb, sizeof(endadb));
173 if (write(adb_control_bu_fd, &endadb, sizeof(endadb)) < 0) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400174 printf("Cannot write to ADB control: %s\n", strerror(errno));
bigbiffce8f83c2015-12-12 18:30:21 -0500175 close(adb_control_bu_fd);
176 return false;
177 }
178 close(adb_control_bu_fd);
179 return true;
180}
181
182bool twadbbu::Write_TWFN(std::string Backup_FileName, uint64_t file_size, bool use_compression) {
183 int adb_control_bu_fd;
184 adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
185 struct twfilehdr twfilehdr;
186 strncpy(twfilehdr.start_of_header, TWRP, sizeof(twfilehdr.start_of_header));
187 strncpy(twfilehdr.type, TWFN, sizeof(twfilehdr.type));
188 strncpy(twfilehdr.name, Backup_FileName.c_str(), sizeof(twfilehdr.name));
189 twfilehdr.size = (file_size == 0 ? 1024 : file_size);
190 twfilehdr.compressed = use_compression;
191 twfilehdr.crc = crc32(0L, Z_NULL, 0);
192 twfilehdr.crc = crc32(twfilehdr.crc, (const unsigned char*) &twfilehdr, sizeof(twfilehdr));
193
194 printf("Sending TWFN to adb\n");
195 if (write(adb_control_bu_fd, &twfilehdr, sizeof(twfilehdr)) < 1) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400196 printf("Cannot that write to adb_control_bu_fd: %s\n", strerror(errno));
bigbiffce8f83c2015-12-12 18:30:21 -0500197 close(adb_control_bu_fd);
198 return false;
199 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400200 fsync(adb_control_bu_fd);
bigbiffce8f83c2015-12-12 18:30:21 -0500201 close(adb_control_bu_fd);
202 return true;
203}
204
205bool twadbbu::Write_TWIMG(std::string Backup_FileName, uint64_t file_size) {
206 int adb_control_bu_fd;
207 struct twfilehdr twimghdr;
208
209 adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
210 strncpy(twimghdr.start_of_header, TWRP, sizeof(twimghdr.start_of_header));
211 strncpy(twimghdr.type, TWIMG, sizeof(twimghdr.type));
212 twimghdr.size = file_size;
213 strncpy(twimghdr.name, Backup_FileName.c_str(), sizeof(twimghdr.name));
214 twimghdr.crc = crc32(0L, Z_NULL, 0);
215 twimghdr.crc = crc32(twimghdr.crc, (const unsigned char*) &twimghdr, sizeof(twimghdr));
216 printf("Sending TWIMG to adb\n");
217 if (write(adb_control_bu_fd, &twimghdr, sizeof(twimghdr)) < 1) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400218 printf("Cannot write to adb control channel: %s\n", strerror(errno));
bigbiffce8f83c2015-12-12 18:30:21 -0500219 return false;
220 }
221
222 return true;
223}
224
225bool twadbbu::Write_TWEOF() {
226 struct AdbBackupControlType tweof;
227 int adb_control_bu_fd;
228 int errctr = 0;
229
230 printf("opening TW_ADB_BU_CONTROL\n");
231 adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
232 while (adb_control_bu_fd < 0) {
233 printf("failed to open TW_ADB_BU_CONTROL. Retrying: %s\n", strerror(errno));
234 adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
235 usleep(10000);
236 errctr++;
237 if (errctr > ADB_BU_MAX_ERROR) {
238 printf("Cannot write to adb_control_bu_fd: %s.\n", strerror(errno));
239 close(adb_control_bu_fd);
240 return false;
241 }
242 }
243 memset(&tweof, 0, sizeof(tweof));
244 strncpy(tweof.start_of_header, TWRP, sizeof(tweof.start_of_header));
245 strncpy(tweof.type, TWEOF, sizeof(tweof.type));
246 tweof.crc = crc32(0L, Z_NULL, 0);
247 tweof.crc = crc32(tweof.crc, (const unsigned char*) &tweof, sizeof(tweof));
248 printf("Sending TWEOF to adb backup\n");
249 if (write(adb_control_bu_fd, &tweof, sizeof(tweof)) < 0) {
250 printf("Cannot write to adb_control_bu_fd: %s.\n", strerror(errno));
251 close(adb_control_bu_fd);
252 return false;
253 }
254 close(adb_control_bu_fd);
255 return true;
256}
257
258bool twadbbu::Write_TWERROR() {
259 struct AdbBackupControlType twerror;
260 int adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
261
262 strncpy(twerror.start_of_header, TWRP, sizeof(twerror.start_of_header));
263 strncpy(twerror.type, TWERROR, sizeof(twerror.type));
264 memset(twerror.space, 0, sizeof(twerror.space));
265 twerror.crc = crc32(0L, Z_NULL, 0);
266 twerror.crc = crc32(twerror.crc, (const unsigned char*) &twerror, sizeof(twerror));
267 if (write(adb_control_bu_fd, &twerror, sizeof(twerror)) < 0) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -0400268 printf("Cannot write to adb control channel: %s\n", strerror(errno));
bigbiffce8f83c2015-12-12 18:30:21 -0500269 return false;
270 }
271 close(adb_control_bu_fd);
272 return true;
273}
274
275bool twadbbu::Write_TWENDADB() {
276 struct AdbBackupControlType endadb;
277 int adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
278
279 memset(&endadb, 0, sizeof(endadb));
280 strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header));
281 strncpy(endadb.type, TWENDADB, sizeof(endadb.type));
282 endadb.crc = crc32(0L, Z_NULL, 0);
283 endadb.crc = crc32(endadb.crc, (const unsigned char*) &endadb, sizeof(endadb));
284
285 printf("Sending TWENDADB to ADB Backup\n");
286 if (write(adb_control_bu_fd, &endadb, sizeof(endadb)) < 1) {
287 printf("Cannot write to ADB_CONTROL_BU_FD: %s\n", strerror(errno));
288 return false;
289 }
290
291 close(adb_control_bu_fd);
292 return true;
293}
bigbiff bigbiffadcb4d82017-09-25 10:51:56 -0400294
295bool twadbbu::Write_TWDATA(FILE* adbd_fp) {
296 struct AdbBackupControlType data_block;
297 memset(&data_block, 0, sizeof(data_block));
298 strncpy(data_block.start_of_header, TWRP, sizeof(data_block.start_of_header));
299 strncpy(data_block.type, TWDATA, sizeof(data_block.type));
300 data_block.crc = crc32(0L, Z_NULL, 0);
301 data_block.crc = crc32(data_block.crc, (const unsigned char*) &data_block, sizeof(data_block));
302 if (fwrite(&data_block, 1, sizeof(data_block), adbd_fp) != sizeof(data_block)) {
303 return false;
304 }
305 return true;
306}