bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 1 | /* |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 2 | Copyright 2013 to 2017 TeamWin |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 3 | 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 bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 30 | #include <vector> |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 31 | #include <fstream> |
| 32 | #include <sstream> |
| 33 | |
| 34 | #include "twadbstream.h" |
| 35 | #include "libtwadbbu.hpp" |
| 36 | |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 37 | bool twadbbu::Check_ADB_Backup_File(std::string fname) { |
| 38 | struct AdbBackupStreamHeader adbbuhdr; |
| 39 | uint32_t crc, adbbuhdrcrc; |
| 40 | unsigned char buf[MAX_ADB_READ]; |
| 41 | int bytes; |
| 42 | |
| 43 | int fd = open(fname.c_str(), O_RDONLY); |
| 44 | if (fd < 0) { |
| 45 | printf("Unable to open %s for reading: %s.\n", fname.c_str(), strerror(errno)); |
| 46 | close(fd); |
| 47 | return false; |
| 48 | } |
| 49 | bytes = read(fd, &buf, sizeof(buf)); |
| 50 | close(fd); |
| 51 | |
| 52 | if (memcpy(&adbbuhdr, buf, sizeof(adbbuhdr)) < 0) { |
| 53 | printf("Unable to memcpy: %s.\n", fname.c_str(), strerror(errno)); |
| 54 | return false; |
| 55 | } |
| 56 | adbbuhdrcrc = adbbuhdr.crc; |
| 57 | memset(&adbbuhdr.crc, 0, sizeof(adbbuhdr.crc)); |
| 58 | crc = crc32(0L, Z_NULL, 0); |
| 59 | crc = crc32(crc, (const unsigned char*) &adbbuhdr, sizeof(adbbuhdr)); |
| 60 | |
| 61 | return (crc == adbbuhdrcrc); |
| 62 | } |
| 63 | |
| 64 | std::vector<std::string> twadbbu::Get_ADB_Backup_Files(std::string fname) { |
| 65 | unsigned char buf[MAX_ADB_READ]; |
| 66 | struct AdbBackupControlType structcmd; |
| 67 | std::vector<std::string> adb_partitions; |
| 68 | |
| 69 | int fd = open(fname.c_str(), O_RDONLY); |
| 70 | if (fd < 0) { |
| 71 | printf("Unable to open %s for reading: %s\n", fname.c_str(), strerror(errno)); |
| 72 | close(fd); |
| 73 | return std::vector<std::string>(); |
| 74 | } |
| 75 | |
| 76 | while (1) { |
| 77 | std::string cmdstr; |
| 78 | int readbytes; |
| 79 | if (readbytes = read(fd, &buf, sizeof(buf)) > 0) { |
| 80 | memcpy(&structcmd, buf, sizeof(structcmd)); |
| 81 | assert(structcmd.type == TWENDADB || structcmd.type == TWIMG || structcmd.type == TWFN); |
| 82 | cmdstr = structcmd.type; |
| 83 | std::string cmdtype = cmdstr.substr(0, sizeof(structcmd.type) - 1); |
| 84 | if (cmdtype == TWENDADB) { |
| 85 | struct AdbBackupControlType endadb; |
| 86 | uint32_t crc, endadbcrc; |
| 87 | |
| 88 | memcpy(&endadb, buf, sizeof(endadb)); |
| 89 | endadbcrc = endadb.crc; |
| 90 | memset(&endadb.crc, 0, sizeof(endadb.crc)); |
| 91 | crc = crc32(0L, Z_NULL, 0); |
| 92 | crc = crc32(crc, (const unsigned char*) &endadb, sizeof(endadb)); |
| 93 | |
| 94 | if (crc == endadbcrc) { |
| 95 | break; |
| 96 | } |
| 97 | else { |
| 98 | printf("ADB TWENDADB crc header doesn't match\n"); |
| 99 | close(fd); |
| 100 | return std::vector<std::string>(); |
| 101 | } |
| 102 | } |
| 103 | else if (cmdtype == TWIMG || cmdtype == TWFN) { |
| 104 | struct twfilehdr twfilehdr; |
| 105 | uint32_t crc, twfilehdrcrc; |
| 106 | |
| 107 | memcpy(&twfilehdr, buf, sizeof(twfilehdr)); |
| 108 | twfilehdrcrc = twfilehdr.crc; |
| 109 | memset(&twfilehdr.crc, 0, sizeof(twfilehdr.crc)); |
| 110 | |
| 111 | crc = crc32(0L, Z_NULL, 0); |
| 112 | crc = crc32(crc, (const unsigned char*) &twfilehdr, sizeof(twfilehdr)); |
| 113 | if (crc == twfilehdrcrc) { |
| 114 | std::string adbfile = twfilehdr.name; |
| 115 | int pos = adbfile.find_last_of("/") + 1; |
| 116 | adbfile = adbfile.substr(pos, adbfile.size()); |
| 117 | adb_partitions.push_back(adbfile); |
| 118 | } |
| 119 | else { |
| 120 | printf("ADB crc header doesn't match\n"); |
| 121 | close(fd); |
| 122 | return std::vector<std::string>(); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | close(fd); |
| 128 | return adb_partitions; |
| 129 | } |
| 130 | |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 131 | bool twadbbu::Write_ADB_Stream_Header(uint64_t partition_count) { |
| 132 | struct AdbBackupStreamHeader twhdr; |
| 133 | int adb_control_bu_fd; |
| 134 | |
| 135 | memset(&twhdr, 0, sizeof(twhdr)); |
| 136 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 137 | if (adb_control_bu_fd < 0) { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 138 | printf("Cannot write to TW_ADB_BU_CONTROL: %s\n", strerror(errno)); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | |
| 142 | strncpy(twhdr.start_of_header, TWRP, sizeof(twhdr.start_of_header)); |
| 143 | strncpy(twhdr.type, TWSTREAMHDR, sizeof(twhdr.type)); |
| 144 | twhdr.partition_count = partition_count; |
| 145 | twhdr.version = ADB_BACKUP_VERSION; |
| 146 | memset(twhdr.space, 0, sizeof(twhdr.space)); |
| 147 | twhdr.crc = crc32(0L, Z_NULL, 0); |
| 148 | twhdr.crc = crc32(twhdr.crc, (const unsigned char*) &twhdr, sizeof(twhdr)); |
| 149 | if (write(adb_control_bu_fd, &twhdr, sizeof(twhdr)) < 0) { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 150 | printf("Cannot write to adb control channel: %s\n", strerror(errno)); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 151 | close(adb_control_bu_fd); |
| 152 | return false; |
| 153 | } |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool twadbbu::Write_ADB_Stream_Trailer() { |
| 158 | int adb_control_bu_fd; |
| 159 | struct AdbBackupControlType endadb; |
| 160 | |
| 161 | memset(&endadb, 0, sizeof(endadb)); |
| 162 | |
| 163 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY); |
| 164 | if (adb_control_bu_fd < 0) { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 165 | printf("Error opening adb_control_bu_fd: %s\n", strerror(errno)); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header)); |
| 169 | strncpy(endadb.type, TWENDADB, sizeof(endadb.type)); |
| 170 | endadb.crc = crc32(0L, Z_NULL, 0); |
| 171 | endadb.crc = crc32(endadb.crc, (const unsigned char*) &endadb, sizeof(endadb)); |
| 172 | if (write(adb_control_bu_fd, &endadb, sizeof(endadb)) < 0) { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 173 | printf("Cannot write to ADB control: %s\n", strerror(errno)); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 174 | close(adb_control_bu_fd); |
| 175 | return false; |
| 176 | } |
| 177 | close(adb_control_bu_fd); |
| 178 | return true; |
| 179 | } |
| 180 | |
| 181 | bool twadbbu::Write_TWFN(std::string Backup_FileName, uint64_t file_size, bool use_compression) { |
| 182 | int adb_control_bu_fd; |
| 183 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 184 | struct twfilehdr twfilehdr; |
| 185 | strncpy(twfilehdr.start_of_header, TWRP, sizeof(twfilehdr.start_of_header)); |
| 186 | strncpy(twfilehdr.type, TWFN, sizeof(twfilehdr.type)); |
| 187 | strncpy(twfilehdr.name, Backup_FileName.c_str(), sizeof(twfilehdr.name)); |
| 188 | twfilehdr.size = (file_size == 0 ? 1024 : file_size); |
| 189 | twfilehdr.compressed = use_compression; |
| 190 | twfilehdr.crc = crc32(0L, Z_NULL, 0); |
| 191 | twfilehdr.crc = crc32(twfilehdr.crc, (const unsigned char*) &twfilehdr, sizeof(twfilehdr)); |
| 192 | |
| 193 | printf("Sending TWFN to adb\n"); |
| 194 | if (write(adb_control_bu_fd, &twfilehdr, sizeof(twfilehdr)) < 1) { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 195 | printf("Cannot that write to adb_control_bu_fd: %s\n", strerror(errno)); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 196 | close(adb_control_bu_fd); |
| 197 | return false; |
| 198 | } |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 199 | fsync(adb_control_bu_fd); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 200 | close(adb_control_bu_fd); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | bool twadbbu::Write_TWIMG(std::string Backup_FileName, uint64_t file_size) { |
| 205 | int adb_control_bu_fd; |
| 206 | struct twfilehdr twimghdr; |
| 207 | |
| 208 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 209 | strncpy(twimghdr.start_of_header, TWRP, sizeof(twimghdr.start_of_header)); |
| 210 | strncpy(twimghdr.type, TWIMG, sizeof(twimghdr.type)); |
| 211 | twimghdr.size = file_size; |
| 212 | strncpy(twimghdr.name, Backup_FileName.c_str(), sizeof(twimghdr.name)); |
| 213 | twimghdr.crc = crc32(0L, Z_NULL, 0); |
| 214 | twimghdr.crc = crc32(twimghdr.crc, (const unsigned char*) &twimghdr, sizeof(twimghdr)); |
| 215 | printf("Sending TWIMG to adb\n"); |
| 216 | if (write(adb_control_bu_fd, &twimghdr, sizeof(twimghdr)) < 1) { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 217 | printf("Cannot write to adb control channel: %s\n", strerror(errno)); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | bool twadbbu::Write_TWEOF() { |
| 225 | struct AdbBackupControlType tweof; |
| 226 | int adb_control_bu_fd; |
| 227 | int errctr = 0; |
| 228 | |
| 229 | printf("opening TW_ADB_BU_CONTROL\n"); |
| 230 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 231 | while (adb_control_bu_fd < 0) { |
| 232 | printf("failed to open TW_ADB_BU_CONTROL. Retrying: %s\n", strerror(errno)); |
| 233 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 234 | usleep(10000); |
| 235 | errctr++; |
| 236 | if (errctr > ADB_BU_MAX_ERROR) { |
| 237 | printf("Cannot write to adb_control_bu_fd: %s.\n", strerror(errno)); |
| 238 | close(adb_control_bu_fd); |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | memset(&tweof, 0, sizeof(tweof)); |
| 243 | strncpy(tweof.start_of_header, TWRP, sizeof(tweof.start_of_header)); |
| 244 | strncpy(tweof.type, TWEOF, sizeof(tweof.type)); |
| 245 | tweof.crc = crc32(0L, Z_NULL, 0); |
| 246 | tweof.crc = crc32(tweof.crc, (const unsigned char*) &tweof, sizeof(tweof)); |
| 247 | printf("Sending TWEOF to adb backup\n"); |
| 248 | if (write(adb_control_bu_fd, &tweof, sizeof(tweof)) < 0) { |
| 249 | printf("Cannot write to adb_control_bu_fd: %s.\n", strerror(errno)); |
| 250 | close(adb_control_bu_fd); |
| 251 | return false; |
| 252 | } |
| 253 | close(adb_control_bu_fd); |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | bool twadbbu::Write_TWERROR() { |
| 258 | struct AdbBackupControlType twerror; |
| 259 | int adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 260 | |
| 261 | strncpy(twerror.start_of_header, TWRP, sizeof(twerror.start_of_header)); |
| 262 | strncpy(twerror.type, TWERROR, sizeof(twerror.type)); |
| 263 | memset(twerror.space, 0, sizeof(twerror.space)); |
| 264 | twerror.crc = crc32(0L, Z_NULL, 0); |
| 265 | twerror.crc = crc32(twerror.crc, (const unsigned char*) &twerror, sizeof(twerror)); |
| 266 | if (write(adb_control_bu_fd, &twerror, sizeof(twerror)) < 0) { |
bigbiff bigbiff | 19fb79c | 2016-09-05 21:04:51 -0400 | [diff] [blame^] | 267 | printf("Cannot write to adb control channel: %s\n", strerror(errno)); |
bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame] | 268 | return false; |
| 269 | } |
| 270 | close(adb_control_bu_fd); |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | bool twadbbu::Write_TWENDADB() { |
| 275 | struct AdbBackupControlType endadb; |
| 276 | int adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 277 | |
| 278 | memset(&endadb, 0, sizeof(endadb)); |
| 279 | strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header)); |
| 280 | strncpy(endadb.type, TWENDADB, sizeof(endadb.type)); |
| 281 | endadb.crc = crc32(0L, Z_NULL, 0); |
| 282 | endadb.crc = crc32(endadb.crc, (const unsigned char*) &endadb, sizeof(endadb)); |
| 283 | |
| 284 | printf("Sending TWENDADB to ADB Backup\n"); |
| 285 | if (write(adb_control_bu_fd, &endadb, sizeof(endadb)) < 1) { |
| 286 | printf("Cannot write to ADB_CONTROL_BU_FD: %s\n", strerror(errno)); |
| 287 | return false; |
| 288 | } |
| 289 | |
| 290 | close(adb_control_bu_fd); |
| 291 | return true; |
| 292 | } |