bigbiff | ce8f83c | 2015-12-12 18:30:21 -0500 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2013 to 2016 TeamWin |
| 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> |
| 30 | #include <fstream> |
| 31 | #include <sstream> |
| 32 | |
| 33 | #include "twadbstream.h" |
| 34 | #include "libtwadbbu.hpp" |
| 35 | |
| 36 | bool twadbbu::Write_ADB_Stream_Header(uint64_t partition_count) { |
| 37 | struct AdbBackupStreamHeader twhdr; |
| 38 | int adb_control_bu_fd; |
| 39 | |
| 40 | memset(&twhdr, 0, sizeof(twhdr)); |
| 41 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 42 | if (adb_control_bu_fd < 0) { |
| 43 | printf("Cannot write to TW_ADB_BU_CONTROL.\n"); |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | strncpy(twhdr.start_of_header, TWRP, sizeof(twhdr.start_of_header)); |
| 48 | strncpy(twhdr.type, TWSTREAMHDR, sizeof(twhdr.type)); |
| 49 | twhdr.partition_count = partition_count; |
| 50 | twhdr.version = ADB_BACKUP_VERSION; |
| 51 | memset(twhdr.space, 0, sizeof(twhdr.space)); |
| 52 | twhdr.crc = crc32(0L, Z_NULL, 0); |
| 53 | twhdr.crc = crc32(twhdr.crc, (const unsigned char*) &twhdr, sizeof(twhdr)); |
| 54 | if (write(adb_control_bu_fd, &twhdr, sizeof(twhdr)) < 0) { |
| 55 | printf("Cannot write to adb control channel\n"); |
| 56 | close(adb_control_bu_fd); |
| 57 | return false; |
| 58 | } |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | bool twadbbu::Write_ADB_Stream_Trailer() { |
| 63 | int adb_control_bu_fd; |
| 64 | struct AdbBackupControlType endadb; |
| 65 | |
| 66 | memset(&endadb, 0, sizeof(endadb)); |
| 67 | |
| 68 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY); |
| 69 | if (adb_control_bu_fd < 0) { |
| 70 | printf("Error opening adb_control_bu_fd\n"); |
| 71 | return false; |
| 72 | } |
| 73 | strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header)); |
| 74 | strncpy(endadb.type, TWENDADB, sizeof(endadb.type)); |
| 75 | endadb.crc = crc32(0L, Z_NULL, 0); |
| 76 | endadb.crc = crc32(endadb.crc, (const unsigned char*) &endadb, sizeof(endadb)); |
| 77 | if (write(adb_control_bu_fd, &endadb, sizeof(endadb)) < 0) { |
| 78 | printf("Cannot write to ADB control.\n"); |
| 79 | close(adb_control_bu_fd); |
| 80 | return false; |
| 81 | } |
| 82 | close(adb_control_bu_fd); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | bool twadbbu::Write_TWFN(std::string Backup_FileName, uint64_t file_size, bool use_compression) { |
| 87 | int adb_control_bu_fd; |
| 88 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 89 | struct twfilehdr twfilehdr; |
| 90 | strncpy(twfilehdr.start_of_header, TWRP, sizeof(twfilehdr.start_of_header)); |
| 91 | strncpy(twfilehdr.type, TWFN, sizeof(twfilehdr.type)); |
| 92 | strncpy(twfilehdr.name, Backup_FileName.c_str(), sizeof(twfilehdr.name)); |
| 93 | twfilehdr.size = (file_size == 0 ? 1024 : file_size); |
| 94 | twfilehdr.compressed = use_compression; |
| 95 | twfilehdr.crc = crc32(0L, Z_NULL, 0); |
| 96 | twfilehdr.crc = crc32(twfilehdr.crc, (const unsigned char*) &twfilehdr, sizeof(twfilehdr)); |
| 97 | |
| 98 | printf("Sending TWFN to adb\n"); |
| 99 | if (write(adb_control_bu_fd, &twfilehdr, sizeof(twfilehdr)) < 1) { |
| 100 | printf("Cannot that write to adb_control_bu_fd\n"); |
| 101 | close(adb_control_bu_fd); |
| 102 | return false; |
| 103 | } |
| 104 | close(adb_control_bu_fd); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | bool twadbbu::Write_TWIMG(std::string Backup_FileName, uint64_t file_size) { |
| 109 | int adb_control_bu_fd; |
| 110 | struct twfilehdr twimghdr; |
| 111 | |
| 112 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 113 | strncpy(twimghdr.start_of_header, TWRP, sizeof(twimghdr.start_of_header)); |
| 114 | strncpy(twimghdr.type, TWIMG, sizeof(twimghdr.type)); |
| 115 | twimghdr.size = file_size; |
| 116 | strncpy(twimghdr.name, Backup_FileName.c_str(), sizeof(twimghdr.name)); |
| 117 | twimghdr.crc = crc32(0L, Z_NULL, 0); |
| 118 | twimghdr.crc = crc32(twimghdr.crc, (const unsigned char*) &twimghdr, sizeof(twimghdr)); |
| 119 | printf("Sending TWIMG to adb\n"); |
| 120 | if (write(adb_control_bu_fd, &twimghdr, sizeof(twimghdr)) < 1) { |
| 121 | printf("Cannot write to adb control channel\n"); |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool twadbbu::Write_TWEOF() { |
| 129 | struct AdbBackupControlType tweof; |
| 130 | int adb_control_bu_fd; |
| 131 | int errctr = 0; |
| 132 | |
| 133 | printf("opening TW_ADB_BU_CONTROL\n"); |
| 134 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 135 | while (adb_control_bu_fd < 0) { |
| 136 | printf("failed to open TW_ADB_BU_CONTROL. Retrying: %s\n", strerror(errno)); |
| 137 | adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 138 | usleep(10000); |
| 139 | errctr++; |
| 140 | if (errctr > ADB_BU_MAX_ERROR) { |
| 141 | printf("Cannot write to adb_control_bu_fd: %s.\n", strerror(errno)); |
| 142 | close(adb_control_bu_fd); |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | memset(&tweof, 0, sizeof(tweof)); |
| 147 | strncpy(tweof.start_of_header, TWRP, sizeof(tweof.start_of_header)); |
| 148 | strncpy(tweof.type, TWEOF, sizeof(tweof.type)); |
| 149 | tweof.crc = crc32(0L, Z_NULL, 0); |
| 150 | tweof.crc = crc32(tweof.crc, (const unsigned char*) &tweof, sizeof(tweof)); |
| 151 | printf("Sending TWEOF to adb backup\n"); |
| 152 | if (write(adb_control_bu_fd, &tweof, sizeof(tweof)) < 0) { |
| 153 | printf("Cannot write to adb_control_bu_fd: %s.\n", strerror(errno)); |
| 154 | close(adb_control_bu_fd); |
| 155 | return false; |
| 156 | } |
| 157 | close(adb_control_bu_fd); |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | bool twadbbu::Write_TWERROR() { |
| 162 | struct AdbBackupControlType twerror; |
| 163 | int adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 164 | |
| 165 | strncpy(twerror.start_of_header, TWRP, sizeof(twerror.start_of_header)); |
| 166 | strncpy(twerror.type, TWERROR, sizeof(twerror.type)); |
| 167 | memset(twerror.space, 0, sizeof(twerror.space)); |
| 168 | twerror.crc = crc32(0L, Z_NULL, 0); |
| 169 | twerror.crc = crc32(twerror.crc, (const unsigned char*) &twerror, sizeof(twerror)); |
| 170 | if (write(adb_control_bu_fd, &twerror, sizeof(twerror)) < 0) { |
| 171 | printf("Cannot write to adb control channel"); |
| 172 | return false; |
| 173 | } |
| 174 | close(adb_control_bu_fd); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | bool twadbbu::Write_TWENDADB() { |
| 179 | struct AdbBackupControlType endadb; |
| 180 | int adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK); |
| 181 | |
| 182 | memset(&endadb, 0, sizeof(endadb)); |
| 183 | strncpy(endadb.start_of_header, TWRP, sizeof(endadb.start_of_header)); |
| 184 | strncpy(endadb.type, TWENDADB, sizeof(endadb.type)); |
| 185 | endadb.crc = crc32(0L, Z_NULL, 0); |
| 186 | endadb.crc = crc32(endadb.crc, (const unsigned char*) &endadb, sizeof(endadb)); |
| 187 | |
| 188 | printf("Sending TWENDADB to ADB Backup\n"); |
| 189 | if (write(adb_control_bu_fd, &endadb, sizeof(endadb)) < 1) { |
| 190 | printf("Cannot write to ADB_CONTROL_BU_FD: %s\n", strerror(errno)); |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | close(adb_control_bu_fd); |
| 195 | return true; |
| 196 | } |