blob: db34c5ac242171d50459ab355629ca8319731965 [file] [log] [blame]
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001/*
2 Copyright 2013 to 2017 TeamWin
3 This file is part of TWRP/TeamWin Recovery Project.
4
5 TWRP is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 TWRP is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with TWRP. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#define __STDC_FORMAT_MACROS 1
20#include <string>
21#include <sys/wait.h>
22#include <sys/stat.h>
23#include <pthread.h>
24#include <zlib.h>
25#include <inttypes.h>
26#include "twrpAdbBuFifo.hpp"
27#include "twcommon.h"
28#include "data.hpp"
29#include "variables.h"
30#include "partitions.hpp"
31#include "twrp-functions.hpp"
32#include "gui/gui.hpp"
33#include "gui/objects.hpp"
34#include "gui/pages.hpp"
35#include "adbbu/twadbstream.h"
36#include "adbbu/libtwadbbu.hpp"
37
38twrpAdbBuFifo::twrpAdbBuFifo(void) {
39 unlink(TW_ADB_FIFO);
40}
41
42bool twrpAdbBuFifo::Check_Adb_Fifo_For_Events(void) {
43 char cmd[512];
44
45 memset(&cmd, 0, sizeof(cmd));
46
47 if (read(adb_fifo_fd, &cmd, sizeof(cmd)) > 0) {
48 LOGINFO("adb backup cmd: %s\n", cmd);
49 std::string cmdcheck(cmd);
50 cmdcheck = cmdcheck.substr(0, strlen(ADB_BACKUP_OP));
51 std::string Options(cmd);
52 Options = Options.substr(strlen(ADB_BACKUP_OP) + 1, strlen(cmd));
53 if (cmdcheck == ADB_BACKUP_OP)
54 return Backup_ADB_Command(Options);
55 else {
56 return Restore_ADB_Backup();
57 }
58 }
59
60 return true;
61}
62
63bool twrpAdbBuFifo::start(void) {
64 LOGINFO("Starting Adb Backup FIFO\n");
65 unlink(TW_ADB_FIFO);
66 if (mkfifo(TW_ADB_FIFO, 06660) != 0) {
67 LOGINFO("Unable to mkfifo %s\n", TW_ADB_FIFO);
68 return false;
69 }
70 adb_fifo_fd = open(TW_ADB_FIFO, O_RDONLY | O_NONBLOCK);
71 if (adb_fifo_fd < 0) {
72 LOGERR("Unable to open TW_ADB_FIFO for reading.\n");
73 close(adb_fifo_fd);
74 return false;
75 }
76 while (true) {
77 Check_Adb_Fifo_For_Events();
78 usleep(8000);
79 }
80 //Shouldn't get here but cleanup anwyay
81 close(adb_fifo_fd);
82 return true;
83}
84
85pthread_t twrpAdbBuFifo::threadAdbBuFifo(void) {
86 pthread_t thread;
87 ThreadPtr adbfifo = &twrpAdbBuFifo::start;
88 PThreadPtr p = *(PThreadPtr*)&adbfifo;
89 pthread_create(&thread, NULL, p, this);
90 return thread;
91}
92
93bool twrpAdbBuFifo::Backup_ADB_Command(std::string Options) {
94 std::vector<std::string> args;
95 std::string Backup_List;
96 bool adbbackup = true, ret = false;
97 std::string rmopt = "--";
98
99 std::replace(Options.begin(), Options.end(), ':', ' ');
100 args = TWFunc::Split_String(Options, " ");
101
102 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 0);
103 DataManager::SetValue(TW_SKIP_DIGEST_GENERATE_VAR, 0);
104
105 if (args[1].compare("--twrp") != 0) {
106 gui_err("twrp_adbbu_option=--twrp option is required to enable twrp adb backup");
107 sleep(2);
108 return false;
109 }
110
111 for (unsigned i = 2; i < args.size(); i++) {
112 int compress;
113
114 std::string::size_type size = args[i].find(rmopt);
115 if (size != std::string::npos)
116 args[i].erase(size, rmopt.length());
117
118 if (args[i].compare("compress") == 0) {
119 gui_msg("compression_on=Compression is on");
120 DataManager::SetValue(TW_USE_COMPRESSION_VAR, 1);
121 continue;
122 }
123 DataManager::GetValue(TW_USE_COMPRESSION_VAR, compress);
124 gui_print("%s\n", args[i].c_str());
125 std::string path;
126 path = "/" + args[i];
127 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
128 if (part) {
129 Backup_List += path;
130 Backup_List += ";";
131 }
132 else {
133 gui_msg(Msg(msg::kError, "partition_not_found=path: {1} not found in partition list")(path));
134 return false;
135 }
136 }
137
138 if (Backup_List.empty()) {
139 DataManager::GetValue("tw_backup_list", Backup_List);
140 if (Backup_List.empty()) {
141 gui_err("no_partition_selected=No partitions selected for backup.");
142 return false;
143 }
144 }
145 else
146 DataManager::SetValue("tw_backup_list", Backup_List);
147
148 DataManager::SetValue("tw_action", "clear");
149 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
150 DataManager::SetValue("tw_action_text2", "");
151 gui_changePage("action_page");
152
153 ret = PartitionManager.Run_Backup(adbbackup);
154 DataManager::SetValue(TW_BACKUP_NAME, gui_lookup("auto_generate", "(Auto Generate)"));
155 if (!ret) {
156 gui_err("backup_fail=Backup failed");
157 return false;
158 }
159 gui_msg("backup_complete=Backup Complete");
160 DataManager::SetValue("ui_progress", 100);
161 sleep(2); //give time for user to see messages on console
162 gui_changePage("main");
163 return true;
164}
165
166bool twrpAdbBuFifo::Restore_ADB_Backup(void) {
167 int partition_count = 0;
168 std::string Restore_Name;
169 std::size_t pos = 0;
170 struct AdbBackupFileTrailer adbmd5;
171 struct PartitionSettings part_settings;
172 int adb_control_twrp_fd, adb_write_fd, systemro;
173 int adb_control_bu_fd, ret = 0;
174 char cmd[512];
175
176 part_settings.total_restore_size = 0;
177
178 PartitionManager.Mount_All_Storage();
179 DataManager::SetValue(TW_SKIP_DIGEST_CHECK_VAR, 0);
180 LOGINFO("opening TW_ADB_BU_CONTROL\n");
181 adb_control_bu_fd = open(TW_ADB_BU_CONTROL, O_WRONLY | O_NONBLOCK);
182 LOGINFO("opening TW_ADB_TWRP_CONTROL\n");
183 adb_control_twrp_fd = open(TW_ADB_TWRP_CONTROL, O_RDONLY | O_NONBLOCK);
184 memset(&adbmd5, 0, sizeof(adbmd5));
185
186 DataManager::SetValue("tw_action", "clear");
187 DataManager::SetValue("tw_action_text1", gui_lookup("running_recovery_commands", "Running Recovery Commands"));
188 DataManager::SetValue("tw_action_text2", "");
189 gui_changePage("action_page");
190
191 while (1) {
192 memset(&cmd, 0, sizeof(cmd));
193 if (read(adb_control_twrp_fd, cmd, sizeof(cmd)) > 0) {
194 struct AdbBackupControlType cmdstruct;
195
196 memset(&cmdstruct, 0, sizeof(cmdstruct));
197 memcpy(&cmdstruct, cmd, sizeof(cmdstruct));
198 std::string cmdstr(cmdstruct.type);
199 std::string cmdtype = cmdstr.substr(0, sizeof(cmdstruct.type) - 1);
200 if (cmdtype == TWSTREAMHDR) {
201 struct AdbBackupStreamHeader twhdr;
202 memcpy(&twhdr, cmd, sizeof(cmd));
203 LOGINFO("ADB Partition count: %" PRIu64 "\n", twhdr.partition_count);
204 LOGINFO("ADB version: %" PRIu64 "\n", twhdr.version);
205 if (twhdr.version != ADB_BACKUP_VERSION) {
206 LOGERR("Incompatible adb backup version!\n");
207 break;
208 }
209 partition_count = twhdr.partition_count;
210 }
211 else if (cmdtype == MD5TRAILER) {
212 LOGINFO("Restoring MD5TRAILER\n");
213 memcpy(&adbmd5, cmd, sizeof(cmd));
214 }
215 else if (cmdtype == TWMD5) {
216 struct AdbBackupFileTrailer md5check;
217 LOGINFO("Restoring TWMD5\n");
218
219 memset(&md5check, 0, sizeof(md5check));
220 memcpy(&md5check, cmd, sizeof(cmd));
221 if (strcmp(md5check.md5, adbmd5.md5) != 0) {
222 LOGERR("md5 doesn't match!\n");
223 LOGERR("file md5: %s\n", adbmd5.md5);
224 LOGERR("check md5: %s\n", md5check.md5);
225 ret = false;
226 break;
227 }
228 else {
229 LOGINFO("adbrestore md5 matches\n");
230 LOGINFO("adbmd5.md5: %s\n", adbmd5.md5);
231 LOGINFO("md5check.md5: %s\n", md5check.md5);
232 }
233 }
234 else if (cmdtype == TWENDADB) {
235 LOGINFO("received TWENDADB\n");
236 break;
237 }
238 else {
239 struct twfilehdr twimghdr;
240 memcpy(&twimghdr, cmd, sizeof(cmd));
241 std::string cmdstr(twimghdr.type);
242 Restore_Name = twimghdr.name;
243 part_settings.total_restore_size = twimghdr.size;
244 if (cmdtype == TWIMG) {
245 LOGINFO("ADB Type: %s\n", twimghdr.type);
246 LOGINFO("ADB Restore_Name: %s\n", Restore_Name.c_str());
247 LOGINFO("ADB Restore_size: %" PRIu64 "\n", part_settings.total_restore_size);
248 string compression = (twimghdr.compressed == 1) ? "compressed" : "uncompressed";
249 LOGINFO("ADB compression: %s\n", compression.c_str());
250 std::string Backup_FileName;
251 std::size_t pos = Restore_Name.find_last_of("/");
252 std::string path = "/" + Restore_Name.substr(pos, Restore_Name.size());
253 pos = path.find_first_of(".");
254 path = path.substr(0, pos);
255 if (path.substr(0,1).compare("//")) {
256 path = path.substr(1, path.size());
257 }
258
259 pos = Restore_Name.find_last_of("/");
260 Backup_FileName = Restore_Name.substr(pos + 1, Restore_Name.size());
261 part_settings.Part = PartitionManager.Find_Partition_By_Path(path);
262 part_settings.Backup_Folder = path;
263 PartitionManager.Set_Restore_Files(path);
264 part_settings.partition_count = partition_count;
265 part_settings.adbbackup = true;
266 part_settings.adb_compression = twimghdr.compressed;
267 part_settings.PM_Method = PM_RESTORE;
268 ProgressTracking progress(part_settings.total_restore_size);
269 part_settings.progress = &progress;
270 if (!PartitionManager.Restore_Partition(&part_settings)) {
271 LOGERR("ADB Restore failed.\n");
272 return false;
273 }
274 }
275 else if (cmdtype == TWFN) {
276 LOGINFO("ADB Type: %s\n", twimghdr.type);
277 LOGINFO("ADB Restore_Name: %s\n", Restore_Name.c_str());
278 LOGINFO("ADB Restore_size: %" PRIi64 "\n", part_settings.total_restore_size);
279 string compression = (twimghdr.compressed == 1) ? "compressed" : "uncompressed";
280 LOGINFO("ADB compression: %s\n", compression.c_str());
281 std::string Backup_FileName;
282 std::size_t pos = Restore_Name.find_last_of("/");
283 std::string path = "/" + Restore_Name.substr(pos, Restore_Name.size());
284 pos = path.find_first_of(".");
285 path = path.substr(0, pos);
286 if (path.substr(0,1).compare("//")) {
287 path = path.substr(1, path.size());
288 }
289
290 pos = Restore_Name.find_last_of("/");
291 Backup_FileName = Restore_Name.substr(pos + 1, Restore_Name.size());
292 pos = Restore_Name.find_last_of("/");
293 part_settings.Part = PartitionManager.Find_Partition_By_Path(path);
294 part_settings.Part->Set_Backup_FileName(Backup_FileName);
295 PartitionManager.Set_Restore_Files(path);
296
297 if (path.compare("/system") == 0) {
298 if (part_settings.Part->Is_Read_Only()) {
299 struct AdbBackupControlType twerror;
300 strncpy(twerror.start_of_header, TWRP, sizeof(twerror.start_of_header));
301 strncpy(twerror.type, TWERROR, sizeof(twerror.type));
302 memset(twerror.space, 0, sizeof(twerror.space));
303 twerror.crc = crc32(0L, Z_NULL, 0);
304 twerror.crc = crc32(twerror.crc, (const unsigned char*) &twerror, sizeof(twerror));
305 if (write(adb_control_bu_fd, &twerror, sizeof(twerror)) < 0) {
306 LOGERR("Cannot write to ADB_CONTROL_BU_FD: %s\n", strerror(errno));
307 }
308 gui_msg(Msg(msg::kError, "restore_read_only=Cannot restore {1} -- mounted read only.")(part_settings.Part->Backup_Display_Name));
309 return false;
310
311 }
312 }
313 part_settings.partition_count = partition_count;
314 part_settings.adbbackup = true;
315 part_settings.adb_compression = twimghdr.compressed;
316 part_settings.total_restore_size += part_settings.Part->Get_Restore_Size(&part_settings);
317 part_settings.PM_Method = PM_RESTORE;
318 ProgressTracking progress(part_settings.total_restore_size);
319 part_settings.progress = &progress;
320 if (!PartitionManager.Restore_Partition(&part_settings)) {
321 LOGERR("ADB Restore failed.\n");
322 return false;
323 }
324 }
325 }
326 }
327 }
328 gui_msg("restore_complete=Restore Complete");
329
330 if (!twadbbu::Write_TWENDADB())
331 ret = false;
332 sleep(2); //give time for user to see messages on console
333 DataManager::SetValue("ui_progress", 100);
334 gui_changePage("main");
335 return ret;
336}