blob: 7cc126f4bb9ea1ee0ce88246114be4bedc9e216b [file] [log] [blame]
Dees Troy3be70a82013-10-22 14:25:12 +00001/*
bigbiffee7b7ff2020-03-23 15:08:27 -04002 Copyright 2014 to 2020 TeamWin
Dees Troy3be70a82013-10-22 14:25:12 +00003 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*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <sys/stat.h>
23#include <sys/vfs.h>
24#include <unistd.h>
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060025#include <map>
Dees_Troy5bf43922012-09-07 16:07:55 -040026#include <vector>
Dees_Troy63c8df72012-09-10 14:02:05 -040027#include <dirent.h>
28#include <time.h>
Dees_Troy8170a922012-09-18 15:40:25 -040029#include <errno.h>
30#include <fcntl.h>
bigbiff25d25b92020-06-19 16:07:38 -040031#include <grp.h>
32#include <pwd.h>
bigbiffce8f83c2015-12-12 18:30:21 -050033#include <zlib.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050034#include <iostream>
35#include <iomanip>
Ethan Yonker8613dc02014-09-11 09:28:20 -050036#include <sys/wait.h>
Ethan Yonker483e9f42016-01-11 22:21:18 -060037#include <linux/fs.h>
38#include <sys/mount.h>
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060039
bigbiffee7b7ff2020-03-23 15:08:27 -040040
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060041#include <sys/poll.h>
42#include <sys/socket.h>
43#include <linux/types.h>
44#include <linux/netlink.h>
bigbiffee7b7ff2020-03-23 15:08:27 -040045#include <android-base/chrono_utils.h>
46#include <android-base/file.h>
47#include <android-base/logging.h>
bigbiff7ba75002020-04-11 20:47:09 -040048#include <android-base/properties.h>
bigbiffee7b7ff2020-03-23 15:08:27 -040049#include <android-base/strings.h>
50#include <fstab/fstab.h>
51#include <fs_avb/fs_avb.h>
52#include <fs_mgr.h>
53#include <fs_mgr_dm_linear.h>
54#include <fs_mgr_overlayfs.h>
55#include <libgsi/libgsi.h>
56#include <liblp/liblp.h>
Ethan Yonker6e8c27a2016-12-22 17:55:57 -060057
Dees_Troy51a0e822012-09-05 15:24:24 -040058#include "variables.h"
Dees_Troy2673cec2013-04-02 20:22:16 +000059#include "twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040060#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040061#include "data.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040062#include "twrp-functions.hpp"
Ethan Yonkerb5fab762016-01-28 14:03:33 -060063#include "fixContexts.hpp"
Ethan Yonker3fdcda42016-11-30 12:29:37 -060064#include "exclude.hpp"
Ethan Yonker4b94cfd2014-12-11 10:00:45 -060065#include "set_metadata.h"
bigbiff7abc5fe2015-01-17 16:53:12 -050066#include "tw_atomic.hpp"
Ethan Yonker74db1572015-10-28 12:44:49 -050067#include "gui/gui.hpp"
Ethan Yonker472f5062016-02-25 13:47:30 -060068#include "progresstracking.hpp"
bigbiff bigbiff56cf5642016-08-19 17:43:45 -040069#include "twrpDigestDriver.hpp"
bigbiffad58e1b2020-07-06 20:24:34 -040070#include "twrpRepacker.hpp"
bigbiffce8f83c2015-12-12 18:30:21 -050071#include "adbbu/libtwadbbu.hpp"
Dees_Troy38bd7602012-09-14 13:33:53 -040072
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040073#ifdef TW_HAS_MTP
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -050074#ifdef TW_HAS_LEGACY_MTP
75#include "mtp/legacy/mtp_MtpServer.hpp"
76#include "mtp/legacy/twrpMtp.hpp"
77#include "mtp/legacy/MtpMessage.hpp"
78#else
79#include "mtp/ffs/mtp_MtpServer.hpp"
80#include "mtp/ffs/twrpMtp.hpp"
81#include "mtp/ffs/MtpMessage.hpp"
82#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040083#endif
84
Dees Troy6f6441d2014-01-23 02:07:03 +000085extern "C" {
86 #include "cutils/properties.h"
Ethan Yonker74db1572015-10-28 12:44:49 -050087 #include "gui/gui.h"
Dees Troy6f6441d2014-01-23 02:07:03 +000088}
89
Dees_Troy5bf43922012-09-07 16:07:55 -040090#ifdef TW_INCLUDE_CRYPTO
bigbiff7ba75002020-04-11 20:47:09 -040091#include "crypto/fde/cryptfs.h"
92#include "gui/rapidxml.hpp"
93#include "gui/pages.hpp"
94#ifdef TW_INCLUDE_FBE
95#include "crypto/ext4crypt/Decrypt.h"
96#ifdef TW_INCLUDE_FBE_METADATA_DECRYPT
97 #ifdef USE_FSCRYPT
98 #include "crypto/fscrypt/MetadataCrypt.h"
99 #else
100 #include "crypto/ext4crypt/MetadataCrypt.h"
bigbiffadc599e2020-05-28 19:36:30 +0000101 #endif
bigbiff7ba75002020-04-11 20:47:09 -0400102#endif
103#endif
104#ifdef TW_CRYPTO_USE_SYSTEM_VOLD
105#include "crypto/vold_decrypt/vold_decrypt.h"
106#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400107#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400108
Ethan Yonker1b190162016-12-05 15:25:19 -0600109#ifdef AB_OTA_UPDATER
110#include <hardware/hardware.h>
111#include <hardware/boot_control.h>
112#endif
113
bigbiffee7b7ff2020-03-23 15:08:27 -0400114using android::fs_mgr::Fstab;
115using android::fs_mgr::FstabEntry;
Ethan Yonker6277c792014-09-15 14:54:30 -0500116
bigbiffee7b7ff2020-03-23 15:08:27 -0400117extern bool datamedia;
118
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500119TWPartitionManager::TWPartitionManager(void) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -0400120 mtp_was_enabled = false;
Ethan Yonker726a0202014-12-16 20:01:38 -0600121 mtp_write_fd = -1;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600122 uevent_pfd.fd = -1;
bigbiff7abc5fe2015-01-17 16:53:12 -0500123 stop_backup.set_value(0);
Ethan Yonker1b190162016-12-05 15:25:19 -0600124#ifdef AB_OTA_UPDATER
125 char slot_suffix[PROPERTY_VALUE_MAX];
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500126 property_get("ro.boot.slot_suffix", slot_suffix, "error");
127 if (strcmp(slot_suffix, "error") == 0)
128 property_get("ro.boot.slot", slot_suffix, "error");
Ethan Yonker1b190162016-12-05 15:25:19 -0600129 Active_Slot_Display = "";
Ethan Yonkerfefe5912017-09-30 22:22:13 -0500130 if (strcmp(slot_suffix, "_a") == 0 || strcmp(slot_suffix, "a") == 0)
Ethan Yonker1b190162016-12-05 15:25:19 -0600131 Set_Active_Slot("A");
132 else
133 Set_Active_Slot("B");
134#endif
bigbiff bigbiff34684ff2013-12-01 21:03:45 -0500135}
136
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100137int TWPartitionManager::Process_Fstab(string Fstab_Filename, bool Display_Error, bool Sar_Detect) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400138 FILE *fstabFile;
139 char fstab_line[MAX_FSTAB_LINE_LENGTH];
Dees Troy02a64532014-03-19 15:23:32 +0000140 TWPartition* settings_partition = NULL;
Matt Mowerbf4efa32014-04-14 23:25:26 -0500141 TWPartition* andsec_partition = NULL;
Ethan Yonker726a0202014-12-16 20:01:38 -0600142 unsigned int storageid = 1 << 16; // upper 16 bits are for physical storage device, we pretend to have only one
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600143 std::map<string, Flags_Map> twrp_flags;
144
145 fstabFile = fopen("/etc/twrp.flags", "rt");
146 if (fstabFile != NULL) {
147 LOGINFO("reading /etc/twrp.flags\n");
148 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
149 if (fstab_line[0] != '/')
150 continue;
151
152 size_t line_size = strlen(fstab_line);
153 if (fstab_line[line_size - 1] != '\n')
154 fstab_line[line_size] = '\n';
155 Flags_Map line_flags;
156 line_flags.Primary_Block_Device = "";
157 line_flags.Alternate_Block_Device = "";
158 line_flags.fstab_line = (char*)malloc(MAX_FSTAB_LINE_LENGTH);
159 if (!line_flags.fstab_line) {
160 LOGERR("malloc error on line_flags.fstab_line\n");
161 return false;
162 }
163 memcpy(line_flags.fstab_line, fstab_line, MAX_FSTAB_LINE_LENGTH);
164 bool found_separator = false;
165 char *fs_loc = NULL;
166 char *block_loc = NULL;
167 char *flags_loc = NULL;
168 size_t index, item_index = 0;
169 for (index = 0; index < line_size; index++) {
170 if (fstab_line[index] <= 32) {
171 fstab_line[index] = '\0';
172 found_separator = true;
173 } else if (found_separator) {
174 if (item_index == 0) {
175 fs_loc = fstab_line + index;
176 } else if (item_index == 1) {
177 block_loc = fstab_line + index;
178 } else if (item_index > 1) {
179 char *ptr = fstab_line + index;
180 if (*ptr == '/') {
181 line_flags.Alternate_Block_Device = ptr;
182 } else if (strlen(ptr) > strlen("flags=") && strncmp(ptr, "flags=", strlen("flags=")) == 0) {
183 flags_loc = ptr;
184 // Once we find the flags=, we're done scanning the line
185 break;
186 }
187 }
188 found_separator = false;
189 item_index++;
190 }
191 }
192 if (block_loc)
193 line_flags.Primary_Block_Device = block_loc;
194 if (fs_loc)
195 line_flags.File_System = fs_loc;
196 if (flags_loc)
197 line_flags.Flags = flags_loc;
198 string Mount_Point = fstab_line;
199 twrp_flags[Mount_Point] = line_flags;
200 memset(fstab_line, 0, sizeof(fstab_line));
201 }
202 fclose(fstabFile);
203 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400204
205 fstabFile = fopen(Fstab_Filename.c_str(), "rt");
206 if (fstabFile == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +0000207 LOGERR("Critical Error: Unable to open fstab at '%s'.\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400208 return false;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600209 } else
210 LOGINFO("Reading %s\n", Fstab_Filename.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400211
212 while (fgets(fstab_line, sizeof(fstab_line), fstabFile) != NULL) {
bigbiffee7b7ff2020-03-23 15:08:27 -0400213 bool isSuper = Is_Super_Partition(fstab_line);
214
215 if (!isSuper && fstab_line[0] != '/')
Dees_Troy5bf43922012-09-07 16:07:55 -0400216 continue;
217
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600218 if (strstr(fstab_line, "swap"))
219 continue; // Skip swap in recovery
220
221 size_t line_size = strlen(fstab_line);
222 if (fstab_line[line_size - 1] != '\n')
223 fstab_line[line_size] = '\n';
Dees_Troy2a923582012-09-20 12:13:34 -0400224
Matt Mower2b2dd152016-04-26 11:24:08 -0500225 TWPartition* partition = new TWPartition();
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100226 if (partition->Process_Fstab_Line(fstab_line, Display_Error, &twrp_flags, Sar_Detect))
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500227 Partitions.push_back(partition);
Matt Mower72c87ce2016-04-26 14:34:56 -0500228 else
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 delete partition;
Matt Mower2b2dd152016-04-26 11:24:08 -0500230
231 memset(fstab_line, 0, sizeof(fstab_line));
Dees_Troy5bf43922012-09-07 16:07:55 -0400232 }
233 fclose(fstabFile);
Matt Mower72c87ce2016-04-26 14:34:56 -0500234
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600235 if (twrp_flags.size() > 0) {
236 LOGINFO("Processing remaining twrp.flags\n");
237 // Add any items from twrp.flags that did not exist in the recovery.fstab
238 for (std::map<string, Flags_Map>::iterator mapit=twrp_flags.begin(); mapit!=twrp_flags.end(); mapit++) {
239 if (Find_Partition_By_Path(mapit->first) == NULL) {
240 TWPartition* partition = new TWPartition();
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100241 if (partition->Process_Fstab_Line(mapit->second.fstab_line, Display_Error, NULL, Sar_Detect))
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600242 Partitions.push_back(partition);
243 else
244 delete partition;
245 }
246 if (mapit->second.fstab_line)
247 free(mapit->second.fstab_line);
248 mapit->second.fstab_line = NULL;
249 }
250 }
251 LOGINFO("Done processing fstab files\n");
252
Matt Mower72c87ce2016-04-26 14:34:56 -0500253 std::vector<TWPartition*>::iterator iter;
254 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Chaosmasterf6e42ce2020-01-27 00:17:04 +0100255 if (Sar_Detect) {
256 if ((*iter)->Mount_Point == "/s")
257 return true;
258 else
259 continue;
260 }
261
Matt Mower72c87ce2016-04-26 14:34:56 -0500262 (*iter)->Partition_Post_Processing(Display_Error);
263
264 if ((*iter)->Is_Storage) {
265 ++storageid;
266 (*iter)->MTP_Storage_ID = storageid;
267 }
268
269 if (!settings_partition && (*iter)->Is_Settings_Storage && (*iter)->Is_Present)
270 settings_partition = (*iter);
271 else
272 (*iter)->Is_Settings_Storage = false;
273
274 if (!andsec_partition && (*iter)->Has_Android_Secure && (*iter)->Is_Present)
275 andsec_partition = (*iter);
276 else
277 (*iter)->Has_Android_Secure = false;
278 }
279
Ethan Yonker6277c792014-09-15 14:54:30 -0500280 if (!datamedia && !settings_partition && Find_Partition_By_Path("/sdcard") == NULL && Find_Partition_By_Path("/internal_sd") == NULL && Find_Partition_By_Path("/internal_sdcard") == NULL && Find_Partition_By_Path("/emmc") == NULL) {
281 // Attempt to automatically identify /data/media emulated storage devices
282 TWPartition* Dat = Find_Partition_By_Path("/data");
283 if (Dat) {
284 LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
285 datamedia = true;
that9e0593e2014-10-08 00:01:24 +0200286 Dat->Setup_Data_Media();
Ethan Yonker6277c792014-09-15 14:54:30 -0500287 settings_partition = Dat;
Ethan Yonker726a0202014-12-16 20:01:38 -0600288 // Since /data was not considered a storage partition earlier, we still need to assign an MTP ID
289 ++storageid;
290 Dat->MTP_Storage_ID = storageid;
Ethan Yonker6277c792014-09-15 14:54:30 -0500291 }
292 }
Dees Troy02a64532014-03-19 15:23:32 +0000293 if (!settings_partition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500294 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
295 if ((*iter)->Is_Storage) {
Dees Troy02a64532014-03-19 15:23:32 +0000296 settings_partition = (*iter);
Dees_Troya13d74f2013-03-24 08:54:55 -0500297 break;
298 }
299 }
Dees Troy02a64532014-03-19 15:23:32 +0000300 if (!settings_partition)
Dees_Troy2673cec2013-04-02 20:22:16 +0000301 LOGERR("Unable to locate storage partition for storing settings file.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500302 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 if (!Write_Fstab()) {
304 if (Display_Error)
Dees_Troy2673cec2013-04-02 20:22:16 +0000305 LOGERR("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400306 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000307 LOGINFO("Error creating fstab\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400308 }
Matt Mowered71fa32014-04-16 13:21:47 -0500309
Matt Mowerbf4efa32014-04-14 23:25:26 -0500310 if (andsec_partition) {
311 Setup_Android_Secure_Location(andsec_partition);
Matt Mowered71fa32014-04-16 13:21:47 -0500312 } else if (settings_partition) {
Matt Mowerbf4efa32014-04-14 23:25:26 -0500313 Setup_Android_Secure_Location(settings_partition);
314 }
Matt Mowered71fa32014-04-16 13:21:47 -0500315 if (settings_partition) {
316 Setup_Settings_Storage_Partition(settings_partition);
317 }
bigbiff7ba75002020-04-11 20:47:09 -0400318
319 Update_System_Details();
320 UnMount_Main_Partitions();
321#ifdef AB_OTA_UPDATER
322 DataManager::SetValue("tw_active_slot", Get_Active_Slot_Display());
323#endif
324 setup_uevent();
325 return true;
326}
327
328int TWPartitionManager::Write_Fstab(void) {
329 FILE *fp;
330 std::vector<TWPartition*>::iterator iter;
331 string Line;
332
333 fp = fopen("/etc/fstab", "w");
334 if (fp == NULL) {
335 LOGINFO("Can not open /etc/fstab.\n");
336 return false;
337 }
338 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
339 if ((*iter)->Can_Be_Mounted) {
340 Line = (*iter)->Actual_Block_Device + " " + (*iter)->Mount_Point + " " + (*iter)->Current_File_System + " rw 0 0\n";
341 fputs(Line.c_str(), fp);
342 }
343 // Handle subpartition tracking
344 if ((*iter)->Is_SubPartition) {
345 TWPartition* ParentPartition = Find_Partition_By_Path((*iter)->SubPartition_Of);
346 if (ParentPartition)
347 ParentPartition->Has_SubPartition = true;
348 else
349 LOGERR("Unable to locate parent partition '%s' of '%s'\n", (*iter)->SubPartition_Of.c_str(), (*iter)->Mount_Point.c_str());
350 }
351 }
352 fclose(fp);
353 return true;
354}
355
356void TWPartitionManager::Decrypt_Data() {
357 #ifdef TW_INCLUDE_CRYPTO
Ethan Yonkercceebb82014-11-18 10:17:59 -0600358 TWPartition* Decrypt_Data = Find_Partition_By_Path("/data");
359 if (Decrypt_Data && Decrypt_Data->Is_Encrypted && !Decrypt_Data->Is_Decrypted) {
Ethan Yonker93382822018-11-01 15:25:31 -0500360 if (!Decrypt_Data->Key_Directory.empty() && Mount_By_Path(Decrypt_Data->Key_Directory, false)) {
361#ifdef TW_INCLUDE_FBE_METADATA_DECRYPT
bigbiff7ba75002020-04-11 20:47:09 -0400362#ifdef USE_FSCRYPT
363 if (fscrypt_mount_metadata_encrypted(Decrypt_Data->Actual_Block_Device, Decrypt_Data->Mount_Point, false)) {
364 std::string crypto_blkdev = android::base::GetProperty("ro.crypto.fs_crypto_blkdev", "error");
365 LOGINFO("Successfully decrypted metadata encrypted data partition with new block device: '%s'\n", crypto_blkdev.c_str());
366#else
Ethan Yonker93382822018-11-01 15:25:31 -0500367 if (e4crypt_mount_metadata_encrypted(Decrypt_Data->Mount_Point, false, Decrypt_Data->Key_Directory, Decrypt_Data->Actual_Block_Device, &Decrypt_Data->Decrypted_Block_Device)) {
bigbiff7ba75002020-04-11 20:47:09 -0400368 LOGINFO("Successfully decrypted metadata encrypted data partition with new block device: '%s'\n",
369 Decrypt_Data->Decrypted_Block_Device.c_str());
370#endif
Ethan Yonker93382822018-11-01 15:25:31 -0500371 property_set("ro.crypto.state", "encrypted");
372 Decrypt_Data->Is_Decrypted = true; // Needed to make the mount function work correctly
373 int retry_count = 10;
374 while (!Decrypt_Data->Mount(false) && --retry_count)
375 usleep(500);
376 if (Decrypt_Data->Mount(false)) {
bigbiff bigbiff0be03b32019-08-27 20:50:31 -0400377 if (!Decrypt_Data->Decrypt_FBE_DE()) {
mauronofriofc79aa62019-11-23 23:13:04 +0100378 char wrappedvalue[PROPERTY_VALUE_MAX];
379 property_get("fbe.data.wrappedkey", wrappedvalue, "");
380 std::string wrappedkeyvalue(wrappedvalue);
381 if (wrappedkeyvalue == "true") {
bigbiff bigbiff0be03b32019-08-27 20:50:31 -0400382 LOGERR("Unable to decrypt FBE device\n");
mauronofriofc79aa62019-11-23 23:13:04 +0100383 } else {
384 LOGINFO("Trying wrapped key.\n");
385 property_set("fbe.data.wrappedkey", "true");
386 if (!Decrypt_Data->Decrypt_FBE_DE()) {
387 LOGERR("Unable to decrypt FBE device\n");
388 }
bigbiff bigbiff0be03b32019-08-27 20:50:31 -0400389 }
390 }
391
Ethan Yonker93382822018-11-01 15:25:31 -0500392 } else {
393 LOGINFO("Failed to mount data after metadata decrypt\n");
394 }
395 } else {
396 LOGINFO("Unable to decrypt metadata encryption\n");
397 }
398#else
399 LOGERR("Metadata FBE decrypt support not present in this TWRP\n");
400#endif
401 }
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600402 if (Decrypt_Data->Is_FBE) {
403 if (DataManager::GetIntValue(TW_CRYPTO_PWTYPE) == 0) {
404 if (Decrypt_Device("!") == 0) {
405 gui_msg("decrypt_success=Successfully decrypted with default password.");
406 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
407 } else {
408 gui_err("unable_to_decrypt=Unable to decrypt with default password.");
409 }
Ethan Yonkercceebb82014-11-18 10:17:59 -0600410 }
411 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -0600412 int password_type = cryptfs_get_password_type();
413 if (password_type == CRYPT_TYPE_DEFAULT) {
414 LOGINFO("Device is encrypted with the default password, attempting to decrypt.\n");
415 if (Decrypt_Device("default_password") == 0) {
416 gui_msg("decrypt_success=Successfully decrypted with default password.");
417 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
418 } else {
419 gui_err("unable_to_decrypt=Unable to decrypt with default password.");
420 }
421 } else {
422 DataManager::SetValue("TW_CRYPTO_TYPE", password_type);
423 }
Ethan Yonkercceebb82014-11-18 10:17:59 -0600424 }
425 }
bigbiffadc599e2020-05-28 19:36:30 +0000426 if (Decrypt_Data && (!Decrypt_Data->Is_Encrypted || Decrypt_Data->Is_Decrypted) && Decrypt_Data->Mount(false)) {
Ethan Yonker66a19492015-12-10 10:19:45 -0600427 Decrypt_Adopted();
428 }
Ethan Yonkercceebb82014-11-18 10:17:59 -0600429#endif
Dees_Troy51a0e822012-09-05 15:24:24 -0400430}
431
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500432void TWPartitionManager::Setup_Settings_Storage_Partition(TWPartition* Part) {
Ethan Yonkerc05c5982014-03-13 09:19:56 -0500433 DataManager::SetValue("tw_settings_path", Part->Storage_Path);
434 DataManager::SetValue("tw_storage_path", Part->Storage_Path);
435 LOGINFO("Settings storage is '%s'\n", Part->Storage_Path.c_str());
436}
437
Matt Mowerbf4efa32014-04-14 23:25:26 -0500438void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
439 if (Part->Has_Android_Secure)
440 Part->Setup_AndSec();
Ethan Yonker6277c792014-09-15 14:54:30 -0500441 else if (!datamedia)
Matt Mowerbf4efa32014-04-14 23:25:26 -0500442 Part->Setup_AndSec();
Matt Mowerbf4efa32014-04-14 23:25:26 -0500443}
444
Dees_Troy8170a922012-09-18 15:40:25 -0400445void TWPartitionManager::Output_Partition_Logging(void) {
446 std::vector<TWPartition*>::iterator iter;
447
448 printf("\n\nPartition Logs:\n");
449 for (iter = Partitions.begin(); iter != Partitions.end(); iter++)
450 Output_Partition((*iter));
451}
452
453void TWPartitionManager::Output_Partition(TWPartition* Part) {
454 unsigned long long mb = 1048576;
455
Gary Peck004d48b2012-11-21 16:28:18 -0800456 printf("%s | %s | Size: %iMB", Part->Mount_Point.c_str(), Part->Actual_Block_Device.c_str(), (int)(Part->Size / mb));
Dees_Troy8170a922012-09-18 15:40:25 -0400457 if (Part->Can_Be_Mounted) {
Gary Peck004d48b2012-11-21 16:28:18 -0800458 printf(" Used: %iMB Free: %iMB Backup Size: %iMB", (int)(Part->Used / mb), (int)(Part->Free / mb), (int)(Part->Backup_Size / mb));
Dees_Troy8170a922012-09-18 15:40:25 -0400459 }
Gary Peck004d48b2012-11-21 16:28:18 -0800460 printf("\n Flags: ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500461 if (Part->Can_Be_Mounted)
462 printf("Can_Be_Mounted ");
Gary Peck004d48b2012-11-21 16:28:18 -0800463 if (Part->Can_Be_Wiped)
464 printf("Can_Be_Wiped ");
Hashcodedabfd492013-08-29 22:45:30 -0700465 if (Part->Use_Rm_Rf)
466 printf("Use_Rm_Rf ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500467 if (Part->Can_Be_Backed_Up)
468 printf("Can_Be_Backed_Up ");
Gary Peck004d48b2012-11-21 16:28:18 -0800469 if (Part->Wipe_During_Factory_Reset)
470 printf("Wipe_During_Factory_Reset ");
471 if (Part->Wipe_Available_in_GUI)
472 printf("Wipe_Available_in_GUI ");
473 if (Part->Is_SubPartition)
474 printf("Is_SubPartition ");
475 if (Part->Has_SubPartition)
476 printf("Has_SubPartition ");
477 if (Part->Removable)
478 printf("Removable ");
479 if (Part->Is_Present)
480 printf("IsPresent ");
481 if (Part->Can_Be_Encrypted)
482 printf("Can_Be_Encrypted ");
483 if (Part->Is_Encrypted)
484 printf("Is_Encrypted ");
485 if (Part->Is_Decrypted)
486 printf("Is_Decrypted ");
487 if (Part->Has_Data_Media)
488 printf("Has_Data_Media ");
Dees_Troy83bd4832013-05-04 12:39:56 +0000489 if (Part->Can_Encrypt_Backup)
490 printf("Can_Encrypt_Backup ");
491 if (Part->Use_Userdata_Encryption)
492 printf("Use_Userdata_Encryption ");
Gary Peck004d48b2012-11-21 16:28:18 -0800493 if (Part->Has_Android_Secure)
494 printf("Has_Android_Secure ");
495 if (Part->Is_Storage)
496 printf("Is_Storage ");
Dees_Troya13d74f2013-03-24 08:54:55 -0500497 if (Part->Is_Settings_Storage)
498 printf("Is_Settings_Storage ");
Dees_Troy68cab492012-12-12 19:29:35 +0000499 if (Part->Ignore_Blkid)
500 printf("Ignore_Blkid ");
Ethan Yonker253368a2014-11-25 15:00:52 -0600501 if (Part->Mount_To_Decrypt)
502 printf("Mount_To_Decrypt ");
Ethan Yonker96af84a2015-01-05 14:58:36 -0600503 if (Part->Can_Flash_Img)
504 printf("Can_Flash_Img ");
Ethan Yonker66a19492015-12-10 10:19:45 -0600505 if (Part->Is_Adopted_Storage)
506 printf("Is_Adopted_Storage ");
Ethan Yonker1b190162016-12-05 15:25:19 -0600507 if (Part->SlotSelect)
508 printf("SlotSelect ");
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600509 if (Part->Mount_Read_Only)
510 printf("Mount_Read_Only ");
Gary Peck004d48b2012-11-21 16:28:18 -0800511 printf("\n");
512 if (!Part->SubPartition_Of.empty())
513 printf(" SubPartition_Of: %s\n", Part->SubPartition_Of.c_str());
514 if (!Part->Symlink_Path.empty())
515 printf(" Symlink_Path: %s\n", Part->Symlink_Path.c_str());
516 if (!Part->Symlink_Mount_Point.empty())
517 printf(" Symlink_Mount_Point: %s\n", Part->Symlink_Mount_Point.c_str());
518 if (!Part->Primary_Block_Device.empty())
519 printf(" Primary_Block_Device: %s\n", Part->Primary_Block_Device.c_str());
520 if (!Part->Alternate_Block_Device.empty())
521 printf(" Alternate_Block_Device: %s\n", Part->Alternate_Block_Device.c_str());
522 if (!Part->Decrypted_Block_Device.empty())
523 printf(" Decrypted_Block_Device: %s\n", Part->Decrypted_Block_Device.c_str());
dianlujitao4879b372018-12-03 18:45:47 +0800524 if (!Part->Crypto_Key_Location.empty())
Ethan Yonker253368a2014-11-25 15:00:52 -0600525 printf(" Crypto_Key_Location: %s\n", Part->Crypto_Key_Location.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800526 if (Part->Length != 0)
527 printf(" Length: %i\n", Part->Length);
528 if (!Part->Display_Name.empty())
529 printf(" Display_Name: %s\n", Part->Display_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500530 if (!Part->Storage_Name.empty())
531 printf(" Storage_Name: %s\n", Part->Storage_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800532 if (!Part->Backup_Path.empty())
533 printf(" Backup_Path: %s\n", Part->Backup_Path.c_str());
534 if (!Part->Backup_Name.empty())
535 printf(" Backup_Name: %s\n", Part->Backup_Name.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500536 if (!Part->Backup_Display_Name.empty())
537 printf(" Backup_Display_Name: %s\n", Part->Backup_Display_Name.c_str());
Gary Peck004d48b2012-11-21 16:28:18 -0800538 if (!Part->Backup_FileName.empty())
539 printf(" Backup_FileName: %s\n", Part->Backup_FileName.c_str());
540 if (!Part->Storage_Path.empty())
541 printf(" Storage_Path: %s\n", Part->Storage_Path.c_str());
542 if (!Part->Current_File_System.empty())
543 printf(" Current_File_System: %s\n", Part->Current_File_System.c_str());
544 if (!Part->Fstab_File_System.empty())
545 printf(" Fstab_File_System: %s\n", Part->Fstab_File_System.c_str());
546 if (Part->Format_Block_Size != 0)
Ethan Yonkerc798c9c2015-10-09 11:15:26 -0500547 printf(" Format_Block_Size: %lu\n", Part->Format_Block_Size);
Dees_Troy094207a2012-09-26 12:00:39 -0400548 if (!Part->MTD_Name.empty())
549 printf(" MTD_Name: %s\n", Part->MTD_Name.c_str());
Matt Mower029a82d2017-01-08 13:12:38 -0600550 printf(" Backup_Method: %s\n", Part->Backup_Method_By_Name().c_str());
Hashcode62bd9e02013-11-19 21:59:42 -0800551 if (Part->Mount_Flags || !Part->Mount_Options.empty())
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600552 printf(" Mount_Flags: %i, Mount_Options: %s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -0600553 if (Part->MTP_Storage_ID)
554 printf(" MTP_Storage_ID: %i\n", Part->MTP_Storage_ID);
Ethan Yonker93382822018-11-01 15:25:31 -0500555 if (!Part->Key_Directory.empty())
556 printf(" Metadata Key Directory: %s\n", Part->Key_Directory.c_str());
Ethan Yonker6277c792014-09-15 14:54:30 -0500557 printf("\n");
Dees_Troy8170a922012-09-18 15:40:25 -0400558}
559
Dees_Troy51a0e822012-09-05 15:24:24 -0400560int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400561 std::vector<TWPartition*>::iterator iter;
562 int ret = false;
563 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400564 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400565
bigbiffd58ba182020-03-23 10:02:29 -0400566 if (Local_Path == "/tmp" || Local_Path == "/")
Dees_Troy43d8b002012-09-17 16:00:01 -0400567 return true;
568
Dees_Troy5bf43922012-09-07 16:07:55 -0400569 // Iterate through all partitions
570 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400571 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400572 ret = (*iter)->Mount(Display_Error);
573 found = true;
Dees_Troy51127312012-09-08 13:08:49 -0400574 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400575 (*iter)->Mount(Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400576 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400577 }
578 if (found) {
579 return ret;
580 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500581 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Dees_Troy5bf43922012-09-07 16:07:55 -0400582 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000583 LOGINFO("Mount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400584 }
585 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400586}
587
Dees_Troy51a0e822012-09-05 15:24:24 -0400588int TWPartitionManager::UnMount_By_Path(string Path, bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400589 std::vector<TWPartition*>::iterator iter;
590 int ret = false;
591 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400592 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy51127312012-09-08 13:08:49 -0400593
594 // Iterate through all partitions
595 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400596 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400597 ret = (*iter)->UnMount(Display_Error);
598 found = true;
599 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
600 (*iter)->UnMount(Display_Error);
601 }
602 }
603 if (found) {
604 return ret;
605 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500606 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Dees_Troy51127312012-09-08 13:08:49 -0400607 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +0000608 LOGINFO("UnMount: Unable to find partition for path '%s'\n", Local_Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400609 }
610 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400611}
612
Dees_Troy51a0e822012-09-05 15:24:24 -0400613int TWPartitionManager::Is_Mounted_By_Path(string Path) {
Dees_Troy51127312012-09-08 13:08:49 -0400614 TWPartition* Part = Find_Partition_By_Path(Path);
615
616 if (Part)
617 return Part->Is_Mounted();
618 else
Dees_Troy2673cec2013-04-02 20:22:16 +0000619 LOGINFO("Is_Mounted: Unable to find partition for path '%s'\n", Path.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400620 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400621}
622
Dees_Troy5bf43922012-09-07 16:07:55 -0400623int TWPartitionManager::Mount_Current_Storage(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -0400624 string current_storage_path = DataManager::GetCurrentStoragePath();
625
626 if (Mount_By_Path(current_storage_path, Display_Error)) {
627 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
628 if (FreeStorage)
629 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
630 return true;
631 }
632 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400633}
634
Dees_Troy5bf43922012-09-07 16:07:55 -0400635int TWPartitionManager::Mount_Settings_Storage(bool Display_Error) {
636 return Mount_By_Path(DataManager::GetSettingsStoragePath(), Display_Error);
637}
638
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600639TWPartition* TWPartitionManager::Find_Partition_By_Path(const string& Path) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400640 std::vector<TWPartition*>::iterator iter;
Dees_Troy38bd7602012-09-14 13:33:53 -0400641 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy5bf43922012-09-07 16:07:55 -0400642
dianlujitaob76a73a2020-04-30 20:33:20 +0800643 if (Local_Path == "/system")
644 Local_Path = Get_Android_Root_Path();
Dees_Troy5bf43922012-09-07 16:07:55 -0400645 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -0400646 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path))
Dees_Troy5bf43922012-09-07 16:07:55 -0400647 return (*iter);
648 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400649 return NULL;
650}
651
Ethan Yonker6e8c27a2016-12-22 17:55:57 -0600652TWPartition* TWPartitionManager::Find_Partition_By_Block_Device(const string& Block_Device) {
653 std::vector<TWPartition*>::iterator iter;
654
655 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
656 if ((*iter)->Primary_Block_Device == Block_Device || (!(*iter)->Actual_Block_Device.empty() && (*iter)->Actual_Block_Device == Block_Device))
657 return (*iter);
658 }
659 return NULL;
660}
661
Ethan Yonker53796e72019-01-11 22:49:52 -0600662int TWPartitionManager::Check_Backup_Name(const std::string& Backup_Name, bool Display_Error, bool Must_Be_Unique) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400663 // Check the backup name to ensure that it is the correct size and contains only valid characters
664 // and that a backup with that name doesn't already exist
665 char backup_name[MAX_BACKUP_NAME_LEN];
666 char backup_loc[255], tw_image_dir[255];
667 int copy_size;
668 int index, cur_char;
Ethan Yonker53796e72019-01-11 22:49:52 -0600669 string Backup_Loc;
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400670
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400671 copy_size = Backup_Name.size();
672 // Check size
673 if (copy_size > MAX_BACKUP_NAME_LEN) {
674 if (Display_Error)
Ethan Yonker74db1572015-10-28 12:44:49 -0500675 gui_err("backup_name_len=Backup name is too long.");
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400676 return -2;
677 }
678
679 // Check each character
680 strncpy(backup_name, Backup_Name.c_str(), copy_size);
Dees_Troya13d74f2013-03-24 08:54:55 -0500681 if (copy_size == 1 && strncmp(backup_name, "0", 1) == 0)
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400682 return 0; // A "0" (zero) means to use the current timestamp for the backup name
683 for (index=0; index<copy_size; index++) {
684 cur_char = (int)backup_name[index];
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500685 if (cur_char == 32 || (cur_char >= 48 && cur_char <= 57) || (cur_char >= 65 && cur_char <= 91) || cur_char == 93 || cur_char == 95 || (cur_char >= 97 && cur_char <= 123) || cur_char == 125 || cur_char == 45 || cur_char == 46) {
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400686 // These are valid characters
687 // Numbers
688 // Upper case letters
689 // Lower case letters
690 // Space
691 // and -_.{}[]
692 } else {
693 if (Display_Error)
Ethan Yonker4adc33e2016-01-22 16:07:11 -0600694 gui_msg(Msg(msg::kError, "backup_name_invalid=Backup name '{1}' contains invalid character: '{1}'")(Backup_Name)((char)cur_char));
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400695 return -3;
696 }
697 }
698
Ethan Yonker53796e72019-01-11 22:49:52 -0600699 if (Must_Be_Unique) {
700 // Check to make sure that a backup with this name doesn't already exist
701 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Loc);
702 strcpy(backup_loc, Backup_Loc.c_str());
703 sprintf(tw_image_dir,"%s/%s", backup_loc, Backup_Name.c_str());
704 if (TWFunc::Path_Exists(tw_image_dir)) {
705 if (Display_Error)
706 gui_err("backup_name_exists=A backup with that name already exists!");
bigbiffce8f83c2015-12-12 18:30:21 -0500707
Ethan Yonker53796e72019-01-11 22:49:52 -0600708 return -4;
709 }
710 // Backup is unique
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400711 }
Ethan Yonker53796e72019-01-11 22:49:52 -0600712 // No problems found
Dees_Troyc9ff7a32012-09-27 10:09:41 -0400713 return 0;
714}
715
bigbiffce8f83c2015-12-12 18:30:21 -0500716bool TWPartitionManager::Backup_Partition(PartitionSettings *part_settings) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400717 time_t start, stop;
Matt Mower23d8aae2017-01-06 14:30:33 -0600718 int use_compression;
bigbiffce8f83c2015-12-12 18:30:21 -0500719 string backup_log = part_settings->Backup_Folder + "/recovery.log";
Dees_Troy43d8b002012-09-17 16:00:01 -0400720
bigbiffce8f83c2015-12-12 18:30:21 -0500721 if (part_settings->Part == NULL)
Dees_Troy43d8b002012-09-17 16:00:01 -0400722 return true;
723
Dees_Troy093b7642012-09-21 15:59:38 -0400724 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -0400725
Tom Hite5a926722014-09-15 01:31:03 +0000726 TWFunc::SetPerformanceMode(true);
Dees_Troy43d8b002012-09-17 16:00:01 -0400727 time(&start);
728
bigbiffce8f83c2015-12-12 18:30:21 -0500729 if (part_settings->Part->Backup(part_settings, &tar_fork_pid)) {
Ethan Yonkercd00a8b2017-07-06 10:23:30 -0500730 sync();
731 sync();
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400732 string Full_Filename = part_settings->Backup_Folder + "/" + part_settings->Part->Backup_FileName;
Ethan Yonkercd00a8b2017-07-06 10:23:30 -0500733 if (!part_settings->adbbackup && part_settings->generate_digest) {
734 if (!twrpDigestDriver::Make_Digest(Full_Filename))
735 goto backup_error;
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400736 }
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400737
bigbiffce8f83c2015-12-12 18:30:21 -0500738 if (part_settings->Part->Has_SubPartition) {
Dees_Troy8170a922012-09-18 15:40:25 -0400739 std::vector<TWPartition*>::iterator subpart;
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400740 TWPartition *parentPart = part_settings->Part;
Dees_Troy8170a922012-09-18 15:40:25 -0400741
742 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400743 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) {
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500744 part_settings->Part = *subpart;
bigbiffce8f83c2015-12-12 18:30:21 -0500745 if (!(*subpart)->Backup(part_settings, &tar_fork_pid)) {
Ethan Yonkercd00a8b2017-07-06 10:23:30 -0500746 goto backup_error;
Tom Hite5a926722014-09-15 01:31:03 +0000747 }
Dees_Troy2727b992013-08-14 20:09:30 +0000748 sync();
749 sync();
bigbiff bigbifff5955b12019-05-18 17:28:58 -0400750 string Full_Filename = part_settings->Backup_Folder + "/" + part_settings->Part->Backup_FileName;
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400751 if (!part_settings->adbbackup && part_settings->generate_digest) {
752 if (!twrpDigestDriver::Make_Digest(Full_Filename)) {
Ethan Yonkercd00a8b2017-07-06 10:23:30 -0500753 goto backup_error;
bigbiffce8f83c2015-12-12 18:30:21 -0500754 }
Tom Hite5a926722014-09-15 01:31:03 +0000755 }
Dees_Troy8170a922012-09-18 15:40:25 -0400756 }
757 }
758 }
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400759
Dees_Troy43d8b002012-09-17 16:00:01 -0400760 time(&stop);
that203bcc92015-05-09 17:27:33 +0200761 int backup_time = (int) difftime(stop, start);
Dees_Troy2673cec2013-04-02 20:22:16 +0000762 LOGINFO("Partition Backup time: %d\n", backup_time);
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -0400763
bigbiffce8f83c2015-12-12 18:30:21 -0500764 if (part_settings->Part->Backup_Method == BM_FILES) {
765 part_settings->file_time += backup_time;
Dees_Troy43d8b002012-09-17 16:00:01 -0400766 } else {
bigbiffce8f83c2015-12-12 18:30:21 -0500767 part_settings->img_time += backup_time;
768
Dees_Troy43d8b002012-09-17 16:00:01 -0400769 }
Tom Hite5a926722014-09-15 01:31:03 +0000770
Matt Mower02899552016-12-30 18:13:51 -0600771 TWFunc::SetPerformanceMode(false);
Ethan Yonkercd00a8b2017-07-06 10:23:30 -0500772 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400773 }
Ethan Yonkercd00a8b2017-07-06 10:23:30 -0500774backup_error:
775 Clean_Backup_Folder(part_settings->Backup_Folder);
776 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
777 tw_set_default_metadata(backup_log.c_str());
778 TWFunc::SetPerformanceMode(false);
779 return false;
bigbiff7abc5fe2015-01-17 16:53:12 -0500780}
781
bigbiffbf1d6722015-02-14 16:25:59 -0500782void TWPartitionManager::Clean_Backup_Folder(string Backup_Folder) {
783 DIR *d = opendir(Backup_Folder.c_str());
784 struct dirent *p;
785 int r;
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400786 vector<string> ext;
787
788 //extensions we should delete when cleaning
789 ext.push_back("win");
790 ext.push_back("md5");
791 ext.push_back("sha2");
792 ext.push_back("info");
bigbiffbf1d6722015-02-14 16:25:59 -0500793
Ethan Yonker74db1572015-10-28 12:44:49 -0500794 gui_msg("backup_clean=Backup Failed. Cleaning Backup Folder.");
bigbiffbf1d6722015-02-14 16:25:59 -0500795
796 if (d == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500797 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Backup_Folder)(strerror(errno)));
bigbiffbf1d6722015-02-14 16:25:59 -0500798 return;
799 }
800
Matt Mower2b18a532015-02-20 16:58:05 -0600801 while ((p = readdir(d))) {
bigbiffbf1d6722015-02-14 16:25:59 -0500802 if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, ".."))
803 continue;
804
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500805 string path = Backup_Folder + "/" + p->d_name;
bigbiffbf1d6722015-02-14 16:25:59 -0500806
807 size_t dot = path.find_last_of(".") + 1;
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400808 for (vector<string>::const_iterator i = ext.begin(); i != ext.end(); ++i) {
809 if (path.substr(dot) == *i) {
810 r = unlink(path.c_str());
811 if (r != 0)
812 LOGINFO("Unable to unlink '%s: %s'\n", path.c_str(), strerror(errno));
bigbiffbf1d6722015-02-14 16:25:59 -0500813 }
814 }
815 }
816 closedir(d);
817}
818
Ethan Yonker472f5062016-02-25 13:47:30 -0600819int TWPartitionManager::Check_Backup_Cancel() {
820 return stop_backup.get_value();
821}
822
bigbiff7abc5fe2015-01-17 16:53:12 -0500823int TWPartitionManager::Cancel_Backup() {
824 string Backup_Folder, Backup_Name, Full_Backup_Path;
825
826 stop_backup.set_value(1);
827
828 if (tar_fork_pid != 0) {
829 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
830 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, Backup_Folder);
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500831 Full_Backup_Path = Backup_Folder + "/" + Backup_Name;
bigbiff7abc5fe2015-01-17 16:53:12 -0500832 LOGINFO("Killing pid: %d\n", tar_fork_pid);
833 kill(tar_fork_pid, SIGUSR2);
834 while (kill(tar_fork_pid, 0) == 0) {
835 usleep(1000);
836 }
837 LOGINFO("Backup_Run stopped and returning false, backup cancelled.\n");
838 LOGINFO("Removing directory %s\n", Full_Backup_Path.c_str());
839 TWFunc::removeDir(Full_Backup_Path, false);
840 tar_fork_pid = 0;
841 }
842
843 return 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400844}
845
bigbiffce8f83c2015-12-12 18:30:21 -0500846int TWPartitionManager::Run_Backup(bool adbbackup) {
847 PartitionSettings part_settings;
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400848 int partition_count = 0, disable_free_space_check = 0, skip_digest = 0;
bigbiffce8f83c2015-12-12 18:30:21 -0500849 string Backup_Name, Backup_List, backup_path;
Matt Mower23d8aae2017-01-06 14:30:33 -0600850 unsigned long long total_bytes = 0, free_space = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -0400851 TWPartition* storage = NULL;
852 struct tm *t;
Matt Mower23d8aae2017-01-06 14:30:33 -0600853 time_t seconds, total_start, total_stop;
Dees_Troya13d74f2013-03-24 08:54:55 -0500854 size_t start_pos = 0, end_pos = 0;
bigbiff7abc5fe2015-01-17 16:53:12 -0500855 stop_backup.set_value(0);
Dees_Troy43d8b002012-09-17 16:00:01 -0400856 seconds = time(0);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500857 t = localtime(&seconds);
Dees_Troy43d8b002012-09-17 16:00:01 -0400858
bigbiffce8f83c2015-12-12 18:30:21 -0500859 part_settings.img_bytes_remaining = 0;
860 part_settings.file_bytes_remaining = 0;
861 part_settings.img_time = 0;
862 part_settings.file_time = 0;
863 part_settings.img_bytes = 0;
864 part_settings.file_bytes = 0;
865 part_settings.PM_Method = PM_BACKUP;
866
bigbiffce8f83c2015-12-12 18:30:21 -0500867 part_settings.adbbackup = adbbackup;
Dees_Troy43d8b002012-09-17 16:00:01 -0400868 time(&total_start);
869
870 Update_System_Details();
871
872 if (!Mount_Current_Storage(true))
873 return false;
874
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400875 DataManager::GetValue(TW_SKIP_DIGEST_GENERATE_VAR, skip_digest);
876 if (skip_digest == 0)
877 part_settings.generate_digest = true;
Dees_Troyc5865ab2012-09-24 15:08:04 -0400878 else
bigbiff bigbiff56cf5642016-08-19 17:43:45 -0400879 part_settings.generate_digest = false;
Dees_Troy4a2a1262012-09-18 09:33:47 -0400880
bigbiffce8f83c2015-12-12 18:30:21 -0500881 DataManager::GetValue(TW_BACKUPS_FOLDER_VAR, part_settings.Backup_Folder);
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500882 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
883 if (Backup_Name == gui_lookup("curr_date", "(Current Date)")) {
884 Backup_Name = TWFunc::Get_Current_Date();
885 } else if (Backup_Name == gui_lookup("auto_generate", "(Auto Generate)") || Backup_Name == "0" || Backup_Name.empty()) {
Dees Troyb21cc642013-09-10 17:36:41 +0000886 TWFunc::Auto_Generate_Backup_Name();
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500887 DataManager::GetValue(TW_BACKUP_NAME, Backup_Name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400888 }
bigbiffce8f83c2015-12-12 18:30:21 -0500889
Ethan Yonkerdcf2b672016-09-13 14:41:53 -0500890 LOGINFO("Backup Name is: '%s'\n", Backup_Name.c_str());
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500891 part_settings.Backup_Folder = part_settings.Backup_Folder + "/" + Backup_Name;
bigbiffce8f83c2015-12-12 18:30:21 -0500892
Ethan Yonkere080c1f2016-09-19 13:50:25 -0500893 LOGINFO("Backup_Folder is: '%s'\n", part_settings.Backup_Folder.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400894
Dees_Troy2673cec2013-04-02 20:22:16 +0000895 LOGINFO("Calculating backup details...\n");
Dees_Troya13d74f2013-03-24 08:54:55 -0500896 DataManager::GetValue("tw_backup_list", Backup_List);
bigbiff7ba75002020-04-11 20:47:09 -0400897 LOGINFO("Backup_List: %s\n", Backup_List.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -0500898 if (!Backup_List.empty()) {
899 end_pos = Backup_List.find(";", start_pos);
900 while (end_pos != string::npos && start_pos < Backup_List.size()) {
901 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
bigbiff7ba75002020-04-11 20:47:09 -0400902 LOGINFO("backup_path: %s\n", backup_path.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -0500903 part_settings.Part = Find_Partition_By_Path(backup_path);
904 if (part_settings.Part != NULL) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500905 partition_count++;
bigbiffce8f83c2015-12-12 18:30:21 -0500906 if (part_settings.Part->Backup_Method == BM_FILES)
907 part_settings.file_bytes += part_settings.Part->Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -0500908 else
bigbiffce8f83c2015-12-12 18:30:21 -0500909 part_settings.img_bytes += part_settings.Part->Backup_Size;
910 if (part_settings.Part->Has_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500911 std::vector<TWPartition*>::iterator subpart;
912
913 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiffce8f83c2015-12-12 18:30:21 -0500914 if ((*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == part_settings.Part->Mount_Point) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500915 partition_count++;
bigbiffce8f83c2015-12-12 18:30:21 -0500916 if ((*subpart)->Backup_Method == BM_FILES)
917 part_settings.file_bytes += (*subpart)->Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -0500918 else
bigbiffce8f83c2015-12-12 18:30:21 -0500919 part_settings.img_bytes += (*subpart)->Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -0500920 }
921 }
Dees_Troy8170a922012-09-18 15:40:25 -0400922 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500923 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500924 gui_msg(Msg(msg::kError, "unable_to_locate_partition=Unable to locate '{1}' partition for backup calculations.")(backup_path));
Dees_Troy8170a922012-09-18 15:40:25 -0400925 }
Dees_Troya13d74f2013-03-24 08:54:55 -0500926 start_pos = end_pos + 1;
927 end_pos = Backup_List.find(";", start_pos);
Dees_Troy43d8b002012-09-17 16:00:01 -0400928 }
929 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400930
931 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -0500932 gui_msg("no_partition_selected=No partitions selected for backup.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400933 return false;
934 }
bigbiffce8f83c2015-12-12 18:30:21 -0500935 if (adbbackup) {
936 if (twadbbu::Write_ADB_Stream_Header(partition_count) == false) {
937 return false;
938 }
939 }
940 total_bytes = part_settings.file_bytes + part_settings.img_bytes;
Ethan Yonker472f5062016-02-25 13:47:30 -0600941 ProgressTracking progress(total_bytes);
bigbiffce8f83c2015-12-12 18:30:21 -0500942 part_settings.progress = &progress;
943
Ethan Yonker74db1572015-10-28 12:44:49 -0500944 gui_msg(Msg("total_partitions_backup= * Total number of partitions to back up: {1}")(partition_count));
945 gui_msg(Msg("total_backup_size= * Total size of all data: {1}MB")(total_bytes / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400946 storage = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
947 if (storage != NULL) {
948 free_space = storage->Free;
Ethan Yonker74db1572015-10-28 12:44:49 -0500949 gui_msg(Msg("available_space= * Available space: {1}MB")(free_space / 1024 / 1024));
Dees_Troy43d8b002012-09-17 16:00:01 -0400950 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500951 gui_err("unable_locate_storage=Unable to locate storage device.");
Dees_Troy43d8b002012-09-17 16:00:01 -0400952 return false;
953 }
bigbiffbf1d6722015-02-14 16:25:59 -0500954
Matt Mowerbfccfb82016-04-25 23:22:31 -0500955 DataManager::GetValue(TW_DISABLE_FREE_SPACE_VAR, disable_free_space_check);
bigbiffce8f83c2015-12-12 18:30:21 -0500956
957 if (adbbackup)
958 disable_free_space_check = true;
959
bigbiffbf1d6722015-02-14 16:25:59 -0500960 if (!disable_free_space_check) {
961 if (free_space - (32 * 1024 * 1024) < total_bytes) {
962 // We require an extra 32MB just in case
Ethan Yonker74db1572015-10-28 12:44:49 -0500963 gui_err("no_space=Not enough free space on storage.");
bigbiffbf1d6722015-02-14 16:25:59 -0500964 return false;
965 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400966 }
bigbiffce8f83c2015-12-12 18:30:21 -0500967 part_settings.img_bytes_remaining = part_settings.img_bytes;
968 part_settings.file_bytes_remaining = part_settings.file_bytes;
Dees_Troy43d8b002012-09-17 16:00:01 -0400969
Ethan Yonker74db1572015-10-28 12:44:49 -0500970 gui_msg("backup_started=[BACKUP STARTED]");
bigbiff bigbiffcdd97c72019-03-29 19:12:33 -0400971
972 int is_decrypted = 0;
973 int is_encrypted = 0;
974
975 DataManager::GetValue(TW_IS_DECRYPTED, is_decrypted);
976 DataManager::GetValue(TW_IS_ENCRYPTED, is_encrypted);
977 if (!adbbackup || (!is_encrypted || (is_encrypted && is_decrypted))) {
978 gui_msg(Msg("backup_folder= * Backup Folder: {1}")(part_settings.Backup_Folder));
979 if (!TWFunc::Recursive_Mkdir(part_settings.Backup_Folder)) {
980 gui_err("fail_backup_folder=Failed to make backup folder.");
981 return false;
982 }
Dees_Troyd4b22b02013-01-18 17:17:58 +0000983 }
984
Dees_Troy2673cec2013-04-02 20:22:16 +0000985 DataManager::SetProgress(0.0);
Dees_Troy093b7642012-09-21 15:59:38 -0400986
Dees_Troya13d74f2013-03-24 08:54:55 -0500987 start_pos = 0;
988 end_pos = Backup_List.find(";", start_pos);
989 while (end_pos != string::npos && start_pos < Backup_List.size()) {
bigbiff7abc5fe2015-01-17 16:53:12 -0500990 if (stop_backup.get_value() != 0)
991 return -1;
Dees_Troya13d74f2013-03-24 08:54:55 -0500992 backup_path = Backup_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -0500993 part_settings.Part = Find_Partition_By_Path(backup_path);
994 if (part_settings.Part != NULL) {
995 if (!Backup_Partition(&part_settings))
Dees_Troya13d74f2013-03-24 08:54:55 -0500996 return false;
997 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -0500998 gui_msg(Msg(msg::kError, "unable_to_locate_partition=Unable to locate '{1}' partition for backup calculations.")(backup_path));
Dees_Troya13d74f2013-03-24 08:54:55 -0500999 }
1000 start_pos = end_pos + 1;
1001 end_pos = Backup_List.find(";", start_pos);
1002 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001003
1004 // Average BPS
bigbiffce8f83c2015-12-12 18:30:21 -05001005 if (part_settings.img_time == 0)
1006 part_settings.img_time = 1;
1007 if (part_settings.file_time == 0)
1008 part_settings.file_time = 1;
1009 int img_bps = (int)part_settings.img_bytes / (int)part_settings.img_time;
1010 unsigned long long file_bps = part_settings.file_bytes / (int)part_settings.file_time;
Dees_Troy43d8b002012-09-17 16:00:01 -04001011
bigbiffce8f83c2015-12-12 18:30:21 -05001012 if (part_settings.file_bytes != 0)
Ethan Yonker472f5062016-02-25 13:47:30 -06001013 gui_msg(Msg("avg_backup_fs=Average backup rate for file systems: {1} MB/sec")(file_bps / (1024 * 1024)));
bigbiffce8f83c2015-12-12 18:30:21 -05001014 if (part_settings.img_bytes != 0)
Ethan Yonker472f5062016-02-25 13:47:30 -06001015 gui_msg(Msg("avg_backup_img=Average backup rate for imaged drives: {1} MB/sec")(img_bps / (1024 * 1024)));
Dees_Troy43d8b002012-09-17 16:00:01 -04001016
1017 time(&total_stop);
1018 int total_time = (int) difftime(total_stop, total_start);
bigbiffce8f83c2015-12-12 18:30:21 -05001019
1020 uint64_t actual_backup_size;
Ethan Yonker3fdcda42016-11-30 12:29:37 -06001021 if (!adbbackup) {
1022 TWExclude twe;
1023 actual_backup_size = twe.Get_Folder_Size(part_settings.Backup_Folder);
1024 } else
bigbiffce8f83c2015-12-12 18:30:21 -05001025 actual_backup_size = part_settings.file_bytes + part_settings.img_bytes;
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001026 actual_backup_size /= (1024LLU * 1024LLU);
Dees_Troy43d8b002012-09-17 16:00:01 -04001027
bigbiffce8f83c2015-12-12 18:30:21 -05001028 int prev_img_bps = 0, use_compression = 0;
1029 unsigned long long prev_file_bps = 0;
Matt Mower173cdb92016-12-31 02:49:19 -06001030 DataManager::GetValue(TW_BACKUP_AVG_IMG_RATE, prev_img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -04001031 img_bps += (prev_img_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001032 img_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -04001033
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001034 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy093b7642012-09-21 15:59:38 -04001035 if (use_compression)
1036 DataManager::GetValue(TW_BACKUP_AVG_FILE_COMP_RATE, prev_file_bps);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001037 else
Dees_Troy093b7642012-09-21 15:59:38 -04001038 DataManager::GetValue(TW_BACKUP_AVG_FILE_RATE, prev_file_bps);
1039 file_bps += (prev_file_bps * 4);
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001040 file_bps /= 5;
Dees_Troy093b7642012-09-21 15:59:38 -04001041
Matt Mowerfb1c4ff2014-04-16 13:43:36 -05001042 DataManager::SetValue(TW_BACKUP_AVG_IMG_RATE, img_bps);
Dees_Troy093b7642012-09-21 15:59:38 -04001043 if (use_compression)
1044 DataManager::SetValue(TW_BACKUP_AVG_FILE_COMP_RATE, file_bps);
1045 else
1046 DataManager::SetValue(TW_BACKUP_AVG_FILE_RATE, file_bps);
1047
Ethan Yonker74db1572015-10-28 12:44:49 -05001048 gui_msg(Msg("total_backed_size=[{1} MB TOTAL BACKED UP]")(actual_backup_size));
Dees_Troy43d8b002012-09-17 16:00:01 -04001049 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001050 UnMount_Main_Partitions();
Ethan Yonker56db1c42015-12-20 22:49:00 -06001051 gui_msg(Msg(msg::kHighlight, "backup_completed=[BACKUP COMPLETED IN {1} SECONDS]")(total_time)); // the end
Ethan Yonkere080c1f2016-09-19 13:50:25 -05001052 string backup_log = part_settings.Backup_Folder + "/recovery.log";
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001053 TWFunc::copy_file("/tmp/recovery.log", backup_log, 0644);
Ethan Yonker4b94cfd2014-12-11 10:00:45 -06001054 tw_set_default_metadata(backup_log.c_str());
bigbiffce8f83c2015-12-12 18:30:21 -05001055
1056 if (part_settings.adbbackup) {
1057 if (twadbbu::Write_ADB_Stream_Trailer() == false) {
1058 return false;
1059 }
1060 }
1061 part_settings.adbbackup = false;
1062 DataManager::SetValue("tw_enable_adb_backup", 0);
1063
Dees_Troy3f5c4e82013-02-01 15:16:59 +00001064 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001065}
1066
bigbiffce8f83c2015-12-12 18:30:21 -05001067bool TWPartitionManager::Restore_Partition(PartitionSettings *part_settings) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001068 time_t Start, Stop;
bigbiffce8f83c2015-12-12 18:30:21 -05001069
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04001070 if (part_settings->adbbackup) {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001071 std::string partName = part_settings->Part->Backup_Name + "." + part_settings->Part->Current_File_System + ".win";
1072 LOGINFO("setting backup name: %s\n", partName.c_str());
bigbiff bigbiffb5ecaad2017-03-20 18:53:53 -04001073 part_settings->Part->Set_Backup_FileName(part_settings->Part->Backup_Name + "." + part_settings->Part->Current_File_System + ".win");
1074 }
1075
Tom Hite5a926722014-09-15 01:31:03 +00001076 TWFunc::SetPerformanceMode(true);
bigbiffce8f83c2015-12-12 18:30:21 -05001077
Dees_Troy4a2a1262012-09-18 09:33:47 -04001078 time(&Start);
Ethan Yonker472f5062016-02-25 13:47:30 -06001079
bigbiffce8f83c2015-12-12 18:30:21 -05001080 if (!part_settings->Part->Restore(part_settings)) {
Tom Hite5a926722014-09-15 01:31:03 +00001081 TWFunc::SetPerformanceMode(false);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001082 return false;
Tom Hite5a926722014-09-15 01:31:03 +00001083 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001084 if (part_settings->Part->Has_SubPartition && !part_settings->adbbackup) {
Dees_Troy8170a922012-09-18 15:40:25 -04001085 std::vector<TWPartition*>::iterator subpart;
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001086 TWPartition *parentPart = part_settings->Part;
Dees_Troy8170a922012-09-18 15:40:25 -04001087
1088 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001089 part_settings->Part = *subpart;
1090 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) {
1091 part_settings->Part = (*subpart);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001092 part_settings->Part->Set_Backup_FileName(part_settings->Part->Backup_Name + "." + part_settings->Part->Current_File_System + ".win");
bigbiffce8f83c2015-12-12 18:30:21 -05001093 if (!(*subpart)->Restore(part_settings)) {
Tom Hite5a926722014-09-15 01:31:03 +00001094 TWFunc::SetPerformanceMode(false);
Dees_Troy8170a922012-09-18 15:40:25 -04001095 return false;
Tom Hite5a926722014-09-15 01:31:03 +00001096 }
Dees_Troy8170a922012-09-18 15:40:25 -04001097 }
1098 }
1099 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001100 time(&Stop);
Tom Hite5a926722014-09-15 01:31:03 +00001101 TWFunc::SetPerformanceMode(false);
bigbiffce8f83c2015-12-12 18:30:21 -05001102 gui_msg(Msg("restore_part_done=[{1} done ({2} seconds)]")(part_settings->Part->Backup_Display_Name)((int)difftime(Stop, Start)));
1103
Dees_Troy4a2a1262012-09-18 09:33:47 -04001104 return true;
1105}
1106
Ethan Yonker472f5062016-02-25 13:47:30 -06001107int TWPartitionManager::Run_Restore(const string& Restore_Name) {
bigbiffce8f83c2015-12-12 18:30:21 -05001108 PartitionSettings part_settings;
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04001109 int check_digest;
bigbiffce8f83c2015-12-12 18:30:21 -05001110
Dees_Troy4a2a1262012-09-18 09:33:47 -04001111 time_t rStart, rStop;
1112 time(&rStart);
Dees_Troya13d74f2013-03-24 08:54:55 -05001113 string Restore_List, restore_path;
1114 size_t start_pos = 0, end_pos;
bigbiffce8f83c2015-12-12 18:30:21 -05001115
Ethan Yonkerdcf2b672016-09-13 14:41:53 -05001116 part_settings.Backup_Folder = Restore_Name;
bigbiffce8f83c2015-12-12 18:30:21 -05001117 part_settings.Part = NULL;
1118 part_settings.partition_count = 0;
1119 part_settings.total_restore_size = 0;
1120 part_settings.adbbackup = false;
1121 part_settings.PM_Method = PM_RESTORE;
Dees_Troy43d8b002012-09-17 16:00:01 -04001122
Ethan Yonker74db1572015-10-28 12:44:49 -05001123 gui_msg("restore_started=[RESTORE STARTED]");
1124 gui_msg(Msg("restore_folder=Restore folder: '{1}'")(Restore_Name));
Dees_Troy43d8b002012-09-17 16:00:01 -04001125
Dees_Troy4a2a1262012-09-18 09:33:47 -04001126 if (!Mount_Current_Storage(true))
1127 return false;
1128
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04001129 DataManager::GetValue(TW_SKIP_DIGEST_CHECK_VAR, check_digest);
1130 if (check_digest > 0) {
1131 // Check Digest files first before restoring to ensure that all of them match before starting a restore
1132 TWFunc::GUI_Operation_Text(TW_VERIFY_DIGEST_TEXT, gui_parse_text("{@verifying_digest}"));
1133 gui_msg("verifying_digest=Verifying Digest");
Dees_Troya13d74f2013-03-24 08:54:55 -05001134 } else {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04001135 gui_msg("skip_digest=Skipping Digest check based on user setting.");
Dees_Troya13d74f2013-03-24 08:54:55 -05001136 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001137 gui_msg("calc_restore=Calculating restore details...");
Dees_Troya13d74f2013-03-24 08:54:55 -05001138 DataManager::GetValue("tw_restore_selected", Restore_List);
bigbiffce8f83c2015-12-12 18:30:21 -05001139
Dees_Troya13d74f2013-03-24 08:54:55 -05001140 if (!Restore_List.empty()) {
1141 end_pos = Restore_List.find(";", start_pos);
1142 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1143 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -05001144 part_settings.Part = Find_Partition_By_Path(restore_path);
1145 if (part_settings.Part != NULL) {
bigbiffce8f83c2015-12-12 18:30:21 -05001146 if (part_settings.Part->Mount_Read_Only) {
1147 gui_msg(Msg(msg::kError, "restore_read_only=Cannot restore {1} -- mounted read only.")(part_settings.Part->Backup_Display_Name));
Ethan Yonkereb32b1f2015-05-18 10:23:03 -05001148 return false;
1149 }
bigbiffce8f83c2015-12-12 18:30:21 -05001150
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04001151 string Full_Filename = part_settings.Backup_Folder + "/" + part_settings.Part->Backup_FileName;
1152
1153 if (check_digest > 0 && !twrpDigestDriver::Check_Digest(Full_Filename))
Dees_Troya13d74f2013-03-24 08:54:55 -05001154 return false;
bigbiffce8f83c2015-12-12 18:30:21 -05001155 part_settings.partition_count++;
1156 part_settings.total_restore_size += part_settings.Part->Get_Restore_Size(&part_settings);
1157 if (part_settings.Part->Has_SubPartition) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001158 TWPartition *parentPart = part_settings.Part;
Dees_Troya13d74f2013-03-24 08:54:55 -05001159 std::vector<TWPartition*>::iterator subpart;
1160
1161 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
bigbiff bigbiff8fd4b092016-08-30 20:48:53 -04001162 part_settings.Part = *subpart;
1163 if ((*subpart)->Is_SubPartition && (*subpart)->SubPartition_Of == parentPart->Mount_Point) {
bigbiff bigbiff56cf5642016-08-19 17:43:45 -04001164 if (check_digest > 0 && !twrpDigestDriver::Check_Digest(Full_Filename))
Dees_Troya13d74f2013-03-24 08:54:55 -05001165 return false;
bigbiffce8f83c2015-12-12 18:30:21 -05001166 part_settings.total_restore_size += (*subpart)->Get_Restore_Size(&part_settings);
Dees_Troya13d74f2013-03-24 08:54:55 -05001167 }
1168 }
1169 }
1170 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001171 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001172 }
1173 start_pos = end_pos + 1;
1174 end_pos = Restore_List.find(";", start_pos);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001175 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001176 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001177
bigbiffce8f83c2015-12-12 18:30:21 -05001178 if (part_settings.partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001179 gui_err("no_part_restore=No partitions selected for restore.");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001180 return false;
1181 }
1182
bigbiffce8f83c2015-12-12 18:30:21 -05001183 gui_msg(Msg("restore_part_count=Restoring {1} partitions...")(part_settings.partition_count));
1184 gui_msg(Msg("total_restore_size=Total restore size is {1}MB")(part_settings.total_restore_size / 1048576));
Dees_Troy2673cec2013-04-02 20:22:16 +00001185 DataManager::SetProgress(0.0);
bigbiffce8f83c2015-12-12 18:30:21 -05001186 ProgressTracking progress(part_settings.total_restore_size);
1187 part_settings.progress = &progress;
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001188
Dees_Troya13d74f2013-03-24 08:54:55 -05001189 start_pos = 0;
1190 if (!Restore_List.empty()) {
1191 end_pos = Restore_List.find(";", start_pos);
1192 while (end_pos != string::npos && start_pos < Restore_List.size()) {
1193 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
bigbiffce8f83c2015-12-12 18:30:21 -05001194
1195 part_settings.Part = Find_Partition_By_Path(restore_path);
1196 if (part_settings.Part != NULL) {
1197 part_settings.partition_count++;
bigbiffce8f83c2015-12-12 18:30:21 -05001198 if (!Restore_Partition(&part_settings))
Dees_Troya13d74f2013-03-24 08:54:55 -05001199 return false;
1200 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001201 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05001202 }
1203 start_pos = end_pos + 1;
1204 end_pos = Restore_List.find(";", start_pos);
1205 }
1206 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001207 TWFunc::GUI_Operation_Text(TW_UPDATE_SYSTEM_DETAILS_TEXT, gui_parse_text("{@updating_system_details}"));
Captain Throwback9d6feb52018-07-27 10:05:24 -04001208 UnMount_By_Path(Get_Android_Root_Path(), false);
Dees_Troy43d8b002012-09-17 16:00:01 -04001209 Update_System_Details();
Dees_Troyd0384ef2012-10-12 12:15:42 -04001210 UnMount_Main_Partitions();
Dees_Troy4a2a1262012-09-18 09:33:47 -04001211 time(&rStop);
Matt Mower3c366972015-12-25 19:28:31 -06001212 gui_msg(Msg(msg::kHighlight, "restore_completed=[RESTORE COMPLETED IN {1} SECONDS]")((int)difftime(rStop,rStart)));
Ethan Yonker1b7a31b2014-07-03 15:09:22 -05001213 DataManager::SetValue("tw_file_progress", "");
bigbiffce8f83c2015-12-12 18:30:21 -05001214
Dees_Troy63c8df72012-09-10 14:02:05 -04001215 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001216}
1217
1218void TWPartitionManager::Set_Restore_Files(string Restore_Name) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001219 // Start with the default values
Dees_Troya13d74f2013-03-24 08:54:55 -05001220 string Restore_List;
Dees_Troy83bd4832013-05-04 12:39:56 +00001221 bool get_date = true, check_encryption = true;
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001222 bool adbbackup = false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001223
1224 DataManager::SetValue("tw_restore_encrypted", 0);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001225 if (twadbbu::Check_ADB_Backup_File(Restore_Name)) {
1226 vector<string> adb_files;
1227 adb_files = twadbbu::Get_ADB_Backup_Files(Restore_Name);
1228 for (unsigned int i = 0; i < adb_files.size(); ++i) {
1229 string adb_restore_file = adb_files.at(i);
1230 std::size_t pos = adb_restore_file.find_first_of(".");
1231 std::string path = "/" + adb_restore_file.substr(0, pos);
1232 Restore_List = path + ";";
1233 TWPartition* Part = Find_Partition_By_Path(path);
1234 Part->Backup_FileName = TWFunc::Get_Filename(adb_restore_file);
1235 adbbackup = true;
1236 }
1237 DataManager::SetValue("tw_enable_adb_backup", 1);
Dees_Troy63c8df72012-09-10 14:02:05 -04001238 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001239 else {
1240 DIR* d;
1241 d = opendir(Restore_Name.c_str());
1242 if (d == NULL)
Dees_Troy63c8df72012-09-10 14:02:05 -04001243 {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001244 gui_msg(Msg(msg::kError, "error_opening_strerr=Error opening: '{1}' ({2})")(Restore_Name)(strerror(errno)));
1245 return;
Dees_Troy63c8df72012-09-10 14:02:05 -04001246 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001247
1248 struct dirent* de;
1249 while ((de = readdir(d)) != NULL)
Dees_Troy63c8df72012-09-10 14:02:05 -04001250 {
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001251 // Strip off three components
1252 char str[256];
1253 char* label;
1254 char* fstype = NULL;
1255 char* extn = NULL;
1256 char* ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -04001257
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001258 strcpy(str, de->d_name);
1259 if (strlen(str) <= 2)
1260 continue;
Dees_Troy83bd4832013-05-04 12:39:56 +00001261
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001262 if (get_date) {
1263 char file_path[255];
1264 struct stat st;
1265
1266 strcpy(file_path, Restore_Name.c_str());
1267 strcat(file_path, "/");
1268 strcat(file_path, str);
1269 stat(file_path, &st);
1270 string backup_date = ctime((const time_t*)(&st.st_mtime));
1271 DataManager::SetValue(TW_RESTORE_FILE_DATE, backup_date);
1272 get_date = false;
Dees_Troy83bd4832013-05-04 12:39:56 +00001273 }
Dees_Troy63c8df72012-09-10 14:02:05 -04001274
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001275 label = str;
1276 ptr = label;
1277 while (*ptr && *ptr != '.') ptr++;
1278 if (*ptr == '.')
1279 {
1280 *ptr = 0x00;
1281 ptr++;
1282 fstype = ptr;
1283 }
1284 while (*ptr && *ptr != '.') ptr++;
1285 if (*ptr == '.')
1286 {
1287 *ptr = 0x00;
1288 ptr++;
1289 extn = ptr;
1290 }
Dees_Troy63c8df72012-09-10 14:02:05 -04001291
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001292 if (fstype == NULL || extn == NULL || strcmp(fstype, "log") == 0) continue;
1293 int extnlength = strlen(extn);
1294 if (extnlength != 3 && extnlength != 6) continue;
1295 if (extnlength >= 3 && strncmp(extn, "win", 3) != 0) continue;
1296 //if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
Dees_Troy63c8df72012-09-10 14:02:05 -04001297
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001298 if (check_encryption) {
1299 string filename = Restore_Name + "/";
1300 filename += de->d_name;
1301 if (TWFunc::Get_File_Type(filename) == 2) {
1302 LOGINFO("'%s' is encrypted\n", filename.c_str());
1303 DataManager::SetValue("tw_restore_encrypted", 1);
1304 }
1305 }
1306 if (extnlength == 6 && strncmp(extn, "win000", 6) != 0) continue;
1307
1308 TWPartition* Part = Find_Partition_By_Path(label);
1309 if (Part == NULL)
1310 {
1311 gui_msg(Msg(msg::kError, "unable_locate_part_backup_name=Unable to locate partition by backup name: '{1}'")(label));
1312 continue;
1313 }
1314
1315 Part->Backup_FileName = de->d_name;
1316 if (strlen(extn) > 3) {
1317 Part->Backup_FileName.resize(Part->Backup_FileName.size() - strlen(extn) + 3);
1318 }
1319
dianlujitaob76a73a2020-04-30 20:33:20 +08001320 if (!Part->Is_SubPartition) {
1321 if (Part->Backup_Path == Get_Android_Root_Path())
1322 Restore_List += "/system;";
1323 else
1324 Restore_List += Part->Backup_Path + ";";
1325 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001326 }
1327 closedir(d);
Dees_Troy63c8df72012-09-10 14:02:05 -04001328 }
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04001329
1330 if (adbbackup) {
1331 Restore_List = "ADB_Backup;";
1332 adbbackup = false;
1333 }
Dees_Troy63c8df72012-09-10 14:02:05 -04001334
Dees_Troya13d74f2013-03-24 08:54:55 -05001335 // Set the final value
1336 DataManager::SetValue("tw_restore_list", Restore_List);
1337 DataManager::SetValue("tw_restore_selected", Restore_List);
Dees_Troy51a0e822012-09-05 15:24:24 -04001338 return;
1339}
1340
1341int TWPartitionManager::Wipe_By_Path(string Path) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001342 std::vector<TWPartition*>::iterator iter;
Mohd Farazfdd49bb2020-04-21 18:56:44 +05301343 std::vector < TWPartition * >::iterator iter1;
Dees_Troy63c8df72012-09-10 14:02:05 -04001344 int ret = false;
1345 bool found = false;
Dees_Troy38bd7602012-09-14 13:33:53 -04001346 string Local_Path = TWFunc::Get_Root_Path(Path);
Dees_Troy63c8df72012-09-10 14:02:05 -04001347
dianlujitaob76a73a2020-04-30 20:33:20 +08001348 if (Local_Path == "/system")
1349 Local_Path = Get_Android_Root_Path();
Dees_Troy63c8df72012-09-10 14:02:05 -04001350 // Iterate through all partitions
1351 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy657c3092012-09-10 20:32:10 -04001352 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
Mohd Farazfdd49bb2020-04-21 18:56:44 +05301353 // iterate through all partitions since some legacy devices uses other partitions as vendor causes issues while wiping
1354 (*iter)->Find_Actual_Block_Device();
1355 for (iter1 = Partitions.begin (); iter1 != Partitions.end (); iter1++)
1356 {
1357 (*iter1)->Find_Actual_Block_Device();
1358 if ((*iter)->Actual_Block_Device == (*iter1)->Actual_Block_Device && (*iter)->Mount_Point != (*iter1)->Mount_Point)
1359 (*iter1)->UnMount(false);
1360 }
Dees_Troye58d5262012-09-21 12:27:57 -04001361 if (Path == "/and-sec")
1362 ret = (*iter)->Wipe_AndSec();
1363 else
1364 ret = (*iter)->Wipe();
Dees_Troy63c8df72012-09-10 14:02:05 -04001365 found = true;
1366 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1367 (*iter)->Wipe();
1368 }
1369 }
1370 if (found) {
1371 return ret;
1372 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001373 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Dees_Troy63c8df72012-09-10 14:02:05 -04001374 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001375}
1376
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001377int TWPartitionManager::Wipe_By_Path(string Path, string New_File_System) {
1378 std::vector<TWPartition*>::iterator iter;
1379 int ret = false;
1380 bool found = false;
1381 string Local_Path = TWFunc::Get_Root_Path(Path);
1382
1383 // Iterate through all partitions
1384 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1385 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1386 if (Path == "/and-sec")
1387 ret = (*iter)->Wipe_AndSec();
1388 else
1389 ret = (*iter)->Wipe(New_File_System);
1390 found = true;
1391 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1392 (*iter)->Wipe(New_File_System);
1393 }
1394 }
1395 if (found) {
1396 return ret;
1397 } else
Ethan Yonker74db1572015-10-28 12:44:49 -05001398 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001399 return false;
1400}
1401
Dees_Troy51a0e822012-09-05 15:24:24 -04001402int TWPartitionManager::Factory_Reset(void) {
Dees_Troy63c8df72012-09-10 14:02:05 -04001403 std::vector<TWPartition*>::iterator iter;
1404 int ret = true;
1405
1406 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001407 if ((*iter)->Wipe_During_Factory_Reset && (*iter)->Is_Present) {
Ethan Yonker89583ef2015-08-26 09:01:59 -05001408#ifdef TW_OEM_BUILD
1409 if ((*iter)->Mount_Point == "/data") {
1410 if (!(*iter)->Wipe_Encryption())
1411 ret = false;
1412 } else {
1413#endif
1414 if (!(*iter)->Wipe())
1415 ret = false;
1416#ifdef TW_OEM_BUILD
1417 }
1418#endif
Dees_Troy094207a2012-09-26 12:00:39 -04001419 } else if ((*iter)->Has_Android_Secure) {
1420 if (!(*iter)->Wipe_AndSec())
1421 ret = false;
Dees_Troy63c8df72012-09-10 14:02:05 -04001422 }
1423 }
bigbiffad58e1b2020-07-06 20:24:34 -04001424 TWFunc::check_and_run_script("/system/bin/factoryreset.sh", "Factory Reset Script");
Dees_Troy63c8df72012-09-10 14:02:05 -04001425 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -04001426}
1427
Dees_Troy38bd7602012-09-14 13:33:53 -04001428int TWPartitionManager::Wipe_Dalvik_Cache(void) {
1429 struct stat st;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001430 vector <string> dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001431
1432 if (!Mount_By_Path("/data", true))
1433 return false;
1434
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001435 dir.push_back("/data/dalvik-cache");
bigbiff bigbiff19874f12019-01-08 20:06:57 -05001436
bigbiff25d25b92020-06-19 16:07:38 -04001437 std::string cacheDir = TWFunc::get_log_dir();
1438 if (cacheDir == CACHE_LOGS_DIR) {
1439 if (!PartitionManager.Mount_By_Path(CACHE_LOGS_DIR, false)) {
1440 LOGINFO("Unable to mount %s for wiping cache.\n", CACHE_LOGS_DIR);
bigbiff bigbiff19874f12019-01-08 20:06:57 -05001441 }
1442 dir.push_back(cacheDir + "dalvik-cache");
1443 dir.push_back(cacheDir + "/dc");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001444 }
Ethan Yonkerff2b7882016-12-10 09:04:41 -06001445
Dees_Troy38bd7602012-09-14 13:33:53 -04001446 TWPartition* sdext = Find_Partition_By_Path("/sd-ext");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001447 if (sdext && sdext->Is_Present && sdext->Mount(false))
1448 {
1449 if (stat("/sd-ext/dalvik-cache", &st) == 0)
1450 {
Ethan Yonkerff2b7882016-12-10 09:04:41 -06001451 dir.push_back("/sd-ext/dalvik-cache");
1452 }
1453 }
1454
bigbiff25d25b92020-06-19 16:07:38 -04001455 if (cacheDir == CACHE_LOGS_DIR) {
bigbiff bigbiff19874f12019-01-08 20:06:57 -05001456 gui_msg("wiping_cache_dalvik=Wiping Dalvik Cache Directories...");
1457 } else {
1458 gui_msg("wiping_dalvik=Wiping Dalvik Directory...");
1459 }
Ethan Yonkerff2b7882016-12-10 09:04:41 -06001460 for (unsigned i = 0; i < dir.size(); ++i) {
1461 if (stat(dir.at(i).c_str(), &st) == 0) {
1462 TWFunc::removeDir(dir.at(i), false);
1463 gui_msg(Msg("cleaned=Cleaned: {1}...")(dir.at(i)));
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001464 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02001465 }
bigbiff bigbiff19874f12019-01-08 20:06:57 -05001466
bigbiff25d25b92020-06-19 16:07:38 -04001467 if (cacheDir == CACHE_LOGS_DIR) {
bigbiff bigbiff19874f12019-01-08 20:06:57 -05001468 gui_msg("cache_dalvik_done=-- Dalvik Cache Directories Wipe Complete!");
1469 } else {
1470 gui_msg("dalvik_done=-- Dalvik Directory Wipe Complete!");
1471 }
1472
Dees_Troy38bd7602012-09-14 13:33:53 -04001473 return true;
1474}
1475
1476int TWPartitionManager::Wipe_Rotate_Data(void) {
1477 if (!Mount_By_Path("/data", true))
1478 return false;
1479
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001480 unlink("/data/misc/akmd*");
1481 unlink("/data/misc/rild*");
Dees_Troy2673cec2013-04-02 20:22:16 +00001482 gui_print("Rotation data wiped.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001483 return true;
1484}
1485
1486int TWPartitionManager::Wipe_Battery_Stats(void) {
1487 struct stat st;
1488
1489 if (!Mount_By_Path("/data", true))
1490 return false;
1491
1492 if (0 != stat("/data/system/batterystats.bin", &st)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001493 gui_print("No Battery Stats Found. No Need To Wipe.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001494 } else {
1495 remove("/data/system/batterystats.bin");
Dees_Troy2673cec2013-04-02 20:22:16 +00001496 gui_print("Cleared battery stats.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001497 }
1498 return true;
1499}
1500
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001501int TWPartitionManager::Wipe_Android_Secure(void) {
1502 std::vector<TWPartition*>::iterator iter;
1503 int ret = false;
1504 bool found = false;
1505
1506 // Iterate through all partitions
1507 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1508 if ((*iter)->Has_Android_Secure) {
1509 ret = (*iter)->Wipe_AndSec();
1510 found = true;
1511 }
1512 }
1513 if (found) {
1514 return ret;
1515 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001516 gui_err("no_andsec=No android secure partitions found.");
Dees_Troy2ff5a8d2012-09-26 14:53:02 -04001517 }
1518 return false;
1519}
1520
Dees_Troy38bd7602012-09-14 13:33:53 -04001521int TWPartitionManager::Format_Data(void) {
1522 TWPartition* dat = Find_Partition_By_Path("/data");
1523
1524 if (dat != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001525 return dat->Wipe_Encryption();
1526 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001527 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001528 return false;
1529 }
1530 return false;
1531}
1532
1533int TWPartitionManager::Wipe_Media_From_Data(void) {
1534 TWPartition* dat = Find_Partition_By_Path("/data");
1535
1536 if (dat != NULL) {
1537 if (!dat->Has_Data_Media) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001538 LOGERR("This device does not have /data/media\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001539 return false;
1540 }
1541 if (!dat->Mount(true))
1542 return false;
1543
Ethan Yonker74db1572015-10-28 12:44:49 -05001544 gui_msg("wiping_datamedia=Wiping internal storage -- /data/media...");
Ethan Yonker726a0202014-12-16 20:01:38 -06001545 Remove_MTP_Storage(dat->MTP_Storage_ID);
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001546 TWFunc::removeDir("/data/media", false);
xiaolu9416f4f2015-06-04 08:22:23 +08001547 dat->Recreate_Media_Folder();
Ethan Yonker726a0202014-12-16 20:01:38 -06001548 Add_MTP_Storage(dat->MTP_Storage_ID);
Dees_Troy38bd7602012-09-14 13:33:53 -04001549 return true;
1550 } else {
Matt Mower3c366972015-12-25 19:28:31 -06001551 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")("/data"));
Dees_Troy38bd7602012-09-14 13:33:53 -04001552 return false;
1553 }
1554 return false;
1555}
1556
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001557int TWPartitionManager::Repair_By_Path(string Path, bool Display_Error) {
1558 std::vector<TWPartition*>::iterator iter;
1559 int ret = false;
1560 bool found = false;
1561 string Local_Path = TWFunc::Get_Root_Path(Path);
1562
1563 if (Local_Path == "/tmp" || Local_Path == "/")
1564 return true;
1565
1566 // Iterate through all partitions
1567 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1568 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1569 ret = (*iter)->Repair();
1570 found = true;
1571 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1572 (*iter)->Repair();
1573 }
1574 }
1575 if (found) {
1576 return ret;
1577 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001578 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Ethan Yonker87c7bac2014-05-25 21:41:08 -05001579 } else {
1580 LOGINFO("Repair: Unable to find partition for path '%s'\n", Local_Path.c_str());
1581 }
1582 return false;
1583}
1584
Ethan Yonkera2719152015-05-28 09:44:41 -05001585int TWPartitionManager::Resize_By_Path(string Path, bool Display_Error) {
1586 std::vector<TWPartition*>::iterator iter;
1587 int ret = false;
1588 bool found = false;
1589 string Local_Path = TWFunc::Get_Root_Path(Path);
1590
1591 if (Local_Path == "/tmp" || Local_Path == "/")
1592 return true;
1593
1594 // Iterate through all partitions
1595 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1596 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
1597 ret = (*iter)->Resize();
1598 found = true;
1599 } else if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Local_Path) {
1600 (*iter)->Resize();
1601 }
1602 }
1603 if (found) {
1604 return ret;
1605 } else if (Display_Error) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001606 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Local_Path));
Ethan Yonkera2719152015-05-28 09:44:41 -05001607 } else {
1608 LOGINFO("Resize: Unable to find partition for path '%s'\n", Local_Path.c_str());
1609 }
1610 return false;
1611}
1612
Dees_Troy51a0e822012-09-05 15:24:24 -04001613void TWPartitionManager::Update_System_Details(void) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001614 std::vector<TWPartition*>::iterator iter;
Dees_Troy51127312012-09-08 13:08:49 -04001615 int data_size = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001616
Ethan Yonker74db1572015-10-28 12:44:49 -05001617 gui_msg("update_part_details=Updating partition details...");
Dees_Troy5bf43922012-09-07 16:07:55 -04001618 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Ethan Yonker1b190162016-12-05 15:25:19 -06001619 (*iter)->Update_Size(true);
Dees_Troy51127312012-09-08 13:08:49 -04001620 if ((*iter)->Can_Be_Mounted) {
Captain Throwback9d6feb52018-07-27 10:05:24 -04001621 if ((*iter)->Mount_Point == Get_Android_Root_Path()) {
Dees_Troy51127312012-09-08 13:08:49 -04001622 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1623 DataManager::SetValue(TW_BACKUP_SYSTEM_SIZE, backup_display_size);
Ethan Yonker76bbd3a2019-05-10 10:50:04 -05001624 TWFunc::Is_TWRP_App_In_System();
Dees_Troy51127312012-09-08 13:08:49 -04001625 } else if ((*iter)->Mount_Point == "/data" || (*iter)->Mount_Point == "/datadata") {
1626 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
1627 } else if ((*iter)->Mount_Point == "/cache") {
1628 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1629 DataManager::SetValue(TW_BACKUP_CACHE_SIZE, backup_display_size);
1630 } else if ((*iter)->Mount_Point == "/sd-ext") {
1631 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1632 DataManager::SetValue(TW_BACKUP_SDEXT_SIZE, backup_display_size);
1633 if ((*iter)->Backup_Size == 0) {
1634 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 0);
1635 DataManager::SetValue(TW_BACKUP_SDEXT_VAR, 0);
1636 } else
1637 DataManager::SetValue(TW_HAS_SDEXT_PARTITION, 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001638 } else if ((*iter)->Has_Android_Secure) {
Dees_Troy8170a922012-09-18 15:40:25 -04001639 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troye58d5262012-09-21 12:27:57 -04001640 DataManager::SetValue(TW_BACKUP_ANDSEC_SIZE, backup_display_size);
Dees_Troy8170a922012-09-18 15:40:25 -04001641 if ((*iter)->Backup_Size == 0) {
1642 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 0);
1643 DataManager::SetValue(TW_BACKUP_ANDSEC_VAR, 0);
1644 } else
1645 DataManager::SetValue(TW_HAS_ANDROID_SECURE, 1);
Dees_Troy2c50e182012-09-26 20:05:28 -04001646 } else if ((*iter)->Mount_Point == "/boot") {
1647 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1648 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1649 if ((*iter)->Backup_Size == 0) {
1650 DataManager::SetValue("tw_has_boot_partition", 0);
1651 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1652 } else
1653 DataManager::SetValue("tw_has_boot_partition", 1);
Dees_Troy51127312012-09-08 13:08:49 -04001654 }
Dees_Troyaa9cc402012-10-13 12:14:05 -04001655 } else {
1656 // Handle unmountable partitions in case we reset defaults
1657 if ((*iter)->Mount_Point == "/boot") {
1658 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1659 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
1660 if ((*iter)->Backup_Size == 0) {
1661 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
1662 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
1663 } else
1664 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
1665 } else if ((*iter)->Mount_Point == "/recovery") {
1666 int backup_display_size = (int)((*iter)->Backup_Size / 1048576LLU);
1667 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
1668 if ((*iter)->Backup_Size == 0) {
1669 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
1670 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
1671 } else
1672 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
Gary Peck82599a82012-11-21 16:23:12 -08001673 } else if ((*iter)->Mount_Point == "/data") {
1674 data_size += (int)((*iter)->Backup_Size / 1048576LLU);
Dees_Troyaa9cc402012-10-13 12:14:05 -04001675 }
Dees_Troy51127312012-09-08 13:08:49 -04001676 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001677 }
Ethan Yonker74db1572015-10-28 12:44:49 -05001678 gui_msg("update_part_details_done=...done");
Dees_Troy51127312012-09-08 13:08:49 -04001679 DataManager::SetValue(TW_BACKUP_DATA_SIZE, data_size);
1680 string current_storage_path = DataManager::GetCurrentStoragePath();
1681 TWPartition* FreeStorage = Find_Partition_By_Path(current_storage_path);
Dees_Troy8170a922012-09-18 15:40:25 -04001682 if (FreeStorage != NULL) {
1683 // Attempt to mount storage
bigbiff25d25b92020-06-19 16:07:38 -04001684 if (!FreeStorage->Mount(false)) {
1685 gui_msg(Msg(msg::kWarning, "unable_to_mount_storage=Unable to mount storage"));
1686 DataManager::SetValue(TW_STORAGE_FREE_SIZE, 0);
1687 } else {
1688 DataManager::SetValue(TW_STORAGE_FREE_SIZE, (int)(FreeStorage->Free / 1048576LLU));
Dees_Troy8170a922012-09-18 15:40:25 -04001689 }
1690 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00001691 LOGINFO("Unable to find storage partition '%s'.\n", current_storage_path.c_str());
Dees_Troy8170a922012-09-18 15:40:25 -04001692 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001693 if (!Write_Fstab())
Dees_Troy2673cec2013-04-02 20:22:16 +00001694 LOGERR("Error creating fstab\n");
Dees_Troy51a0e822012-09-05 15:24:24 -04001695 return;
1696}
1697
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001698void TWPartitionManager::Post_Decrypt(const string& Block_Device) {
1699 TWPartition* dat = Find_Partition_By_Path("/data");
bigbiff7ba75002020-04-11 20:47:09 -04001700#ifdef USE_FSCRYPT
1701 dat->Set_Block_Device("/dev/block/mapper/userdata");
1702#endif
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001703 if (dat != NULL) {
1704 DataManager::SetValue(TW_IS_DECRYPTED, 1);
1705 dat->Is_Decrypted = true;
1706 if (!Block_Device.empty()) {
1707 dat->Decrypted_Block_Device = Block_Device;
1708 gui_msg(Msg("decrypt_success_dev=Data successfully decrypted, new block device: '{1}'")(Block_Device));
1709 } else {
1710 gui_msg("decrypt_success_nodev=Data successfully decrypted");
1711 }
1712 dat->Setup_File_System(false);
bigbiffadc599e2020-05-28 19:36:30 +00001713 dat->Current_File_System = dat->Fstab_File_System; // Needed if we're ignoring blkid because encrypted devices start out as emmc
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001714
bigbiffadc599e2020-05-28 19:36:30 +00001715 // Sleep for a bit so that the device will be ready
1716 sleep(1);
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001717 if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
1718 dat->Storage_Path = "/data/media/0";
1719 dat->Symlink_Path = dat->Storage_Path;
1720 DataManager::SetValue("tw_storage_path", "/data/media/0");
1721 DataManager::SetValue("tw_settings_path", "/data/media/0");
1722 dat->UnMount(false);
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001723 }
1724 Update_System_Details();
nkk71ffb02bd2017-06-04 23:12:16 +03001725 Output_Partition(dat);
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001726 UnMount_Main_Partitions();
1727 } else
1728 LOGERR("Unable to locate data partition.\n");
1729}
1730
bigbiffadc599e2020-05-28 19:36:30 +00001731int TWPartitionManager::Decrypt_Device(string Password) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001732#ifdef TW_INCLUDE_CRYPTO
Ethan Yonkerddb63e22017-01-19 14:01:57 -06001733 char crypto_state[PROPERTY_VALUE_MAX], crypto_blkdev[PROPERTY_VALUE_MAX];
Ethan Yonker253368a2014-11-25 15:00:52 -06001734 std::vector<TWPartition*>::iterator iter;
Dees_Troy5bf43922012-09-07 16:07:55 -04001735
Ethan Yonker253368a2014-11-25 15:00:52 -06001736 // Mount any partitions that need to be mounted for decrypt
1737 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1738 if ((*iter)->Mount_To_Decrypt) {
1739 (*iter)->Mount(true);
1740 }
a39552696ff55ce2013-01-08 16:14:56 +00001741 }
oshmouna82a7542018-04-10 17:45:55 +02001742 property_set("twrp.mount_to_decrypt", "1");
a39552696ff55ce2013-01-08 16:14:56 +00001743
Ethan Yonkera1de1492015-11-04 11:58:27 -06001744 property_get("ro.crypto.state", crypto_state, "error");
1745 if (strcmp(crypto_state, "error") == 0) {
1746 property_set("ro.crypto.state", "encrypted");
1747 // Sleep for a bit so that services can start if needed
1748 sleep(1);
1749 }
1750
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001751 if (DataManager::GetIntValue(TW_IS_FBE)) {
1752#ifdef TW_INCLUDE_FBE
1753 if (!Mount_By_Path("/data", true)) // /data has to be mounted for FBE
1754 return -1;
1755 int retry_count = 10;
1756 while (!TWFunc::Path_Exists("/data/system/users/gatekeeper.password.key") && --retry_count)
bigbiffadc599e2020-05-28 19:36:30 +00001757 usleep(2000); // A small sleep is needed after mounting /data to ensure reliable decrypt... maybe because of DE?
1758 int user_id = DataManager::GetIntValue("tw_decrypt_user_id");
1759 LOGINFO("Decrypting FBE for user %i\n", user_id);
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001760 if (Decrypt_User(user_id, Password)) {
bigbiffadc599e2020-05-28 19:36:30 +00001761 Post_Decrypt("");
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001762 return 0;
1763 }
1764#else
1765 LOGERR("FBE support is not present\n");
1766#endif
1767 return -1;
1768 }
1769
Ethan Yonkerddb63e22017-01-19 14:01:57 -06001770 int pwret = -1;
1771 pid_t pid = fork();
1772 if (pid < 0) {
1773 LOGERR("fork failed\n");
1774 return -1;
1775 } else if (pid == 0) {
1776 // Child process
1777 char cPassword[255];
1778 strcpy(cPassword, Password.c_str());
1779 int ret = cryptfs_check_passwd(cPassword);
1780 exit(ret);
1781 } else {
1782 // Parent
1783 int status;
1784 if (TWFunc::Wait_For_Child_Timeout(pid, &status, "Decrypt", 30))
1785 pwret = -1;
1786 else
1787 pwret = WEXITSTATUS(status) ? -1 : 0;
1788 }
a39552696ff55ce2013-01-08 16:14:56 +00001789
nkk7171c6c502017-01-05 23:55:05 +02001790#ifdef TW_CRYPTO_USE_SYSTEM_VOLD
1791 if (pwret != 0) {
1792 pwret = vold_decrypt(Password);
nkk717d1222a2017-11-06 17:27:21 +02001793 switch (pwret) {
1794 case VD_SUCCESS:
1795 break;
1796 case VD_ERR_MISSING_VDC:
1797 gui_msg(Msg(msg::kError, "decrypt_data_vold_os_missing=Missing files needed for vold decrypt: {1}")("/system/bin/vdc"));
1798 break;
1799 case VD_ERR_MISSING_VOLD:
1800 gui_msg(Msg(msg::kError, "decrypt_data_vold_os_missing=Missing files needed for vold decrypt: {1}")("/system/bin/vold"));
1801 break;
1802 }
nkk7171c6c502017-01-05 23:55:05 +02001803 }
1804#endif // TW_CRYPTO_USE_SYSTEM_VOLD
1805
Ethan Yonker253368a2014-11-25 15:00:52 -06001806 // Unmount any partitions that were needed for decrypt
1807 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1808 if ((*iter)->Mount_To_Decrypt) {
1809 (*iter)->UnMount(false);
1810 }
1811 }
oshmouna82a7542018-04-10 17:45:55 +02001812 property_set("twrp.mount_to_decrypt", "0");
Ethan Yonker253368a2014-11-25 15:00:52 -06001813
a39552696ff55ce2013-01-08 16:14:56 +00001814 if (pwret != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001815 gui_err("fail_decrypt=Failed to decrypt data.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001816 return -1;
1817 }
a39552696ff55ce2013-01-08 16:14:56 +00001818
Dees_Troy5bf43922012-09-07 16:07:55 -04001819 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
1820 if (strcmp(crypto_blkdev, "error") == 0) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001821 LOGERR("Error retrieving decrypted data block device.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -04001822 } else {
Ethan Yonkerbd7492d2016-12-07 13:55:01 -06001823 Post_Decrypt(crypto_blkdev);
Dees_Troy5bf43922012-09-07 16:07:55 -04001824 }
1825 return 0;
1826#else
Ethan Yonker74db1572015-10-28 12:44:49 -05001827 gui_err("no_crypto_support=No crypto support was compiled into this build.");
Dees_Troy5bf43922012-09-07 16:07:55 -04001828 return -1;
1829#endif
Dees_Troy51a0e822012-09-05 15:24:24 -04001830 return 1;
Dees_Troy51127312012-09-08 13:08:49 -04001831}
1832
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001833int TWPartitionManager::Fix_Contexts(void) {
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001834 std::vector<TWPartition*>::iterator iter;
1835 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1836 if ((*iter)->Has_Data_Media) {
1837 if ((*iter)->Mount(true)) {
1838 if (fixContexts::fixDataMediaContexts((*iter)->Mount_Point) != 0)
1839 return -1;
1840 }
1841 }
1842 }
Dees_Troy1a650e62012-10-19 20:54:32 -04001843 UnMount_Main_Partitions();
Ethan Yonker74db1572015-10-28 12:44:49 -05001844 gui_msg("done=Done.");
Ethan Yonkerb5fab762016-01-28 14:03:33 -06001845 return 0;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04001846}
1847
Ethan Yonker66a19492015-12-10 10:19:45 -06001848TWPartition* TWPartitionManager::Find_Next_Storage(string Path, bool Exclude_Data_Media) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001849 std::vector<TWPartition*>::iterator iter = Partitions.begin();
1850
1851 if (!Path.empty()) {
1852 string Search_Path = TWFunc::Get_Root_Path(Path);
1853 for (; iter != Partitions.end(); iter++) {
Matt Mower9a561dd2016-02-22 21:25:51 -06001854 if ((*iter)->Mount_Point == Search_Path) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001855 iter++;
1856 break;
1857 }
1858 }
1859 }
1860
1861 for (; iter != Partitions.end(); iter++) {
Ethan Yonker66a19492015-12-10 10:19:45 -06001862 if (Exclude_Data_Media && (*iter)->Has_Data_Media) {
1863 // do nothing, do not return this type of partition
1864 } else if ((*iter)->Is_Storage && (*iter)->Is_Present) {
Ethan Yonker47360be2014-04-01 10:34:34 -05001865 return (*iter);
1866 }
1867 }
1868
1869 return NULL;
1870}
1871
Dees_Troyd21618c2012-10-14 18:48:49 -04001872int TWPartitionManager::Open_Lun_File(string Partition_Path, string Lun_File) {
Dees_Troyd21618c2012-10-14 18:48:49 -04001873 TWPartition* Part = Find_Partition_By_Path(Partition_Path);
1874
1875 if (Part == NULL) {
Ethan Yonker74db1572015-10-28 12:44:49 -05001876 LOGINFO("Unable to locate '%s' for USB storage mode.", Partition_Path.c_str());
1877 gui_msg(Msg(msg::kError, "unable_find_part_path=Unable to find partition for path '{1}'")(Partition_Path));
Dees_Troyd21618c2012-10-14 18:48:49 -04001878 return false;
1879 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001880 LOGINFO("USB mount '%s', '%s' > '%s'\n", Partition_Path.c_str(), Part->Actual_Block_Device.c_str(), Lun_File.c_str());
1881 if (!Part->UnMount(true) || !Part->Is_Present)
Dees_Troyd21618c2012-10-14 18:48:49 -04001882 return false;
1883
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001884 if (TWFunc::write_to_file(Lun_File, Part->Actual_Block_Device)) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001885 LOGERR("Unable to write to ums lunfile '%s': (%s)\n", Lun_File.c_str(), strerror(errno));
Dees_Troyd21618c2012-10-14 18:48:49 -04001886 return false;
1887 }
Dees_Troyd21618c2012-10-14 18:48:49 -04001888 return true;
1889}
1890
Dees_Troy8170a922012-09-18 15:40:25 -04001891int TWPartitionManager::usb_storage_enable(void) {
Dees_Troy8170a922012-09-18 15:40:25 -04001892 char lun_file[255];
Dees_Troyd21618c2012-10-14 18:48:49 -04001893 bool has_multiple_lun = false;
Dees_Troy8170a922012-09-18 15:40:25 -04001894
Ethan Yonker47360be2014-04-01 10:34:34 -05001895 string Lun_File_str = CUSTOM_LUN_FILE;
1896 size_t found = Lun_File_str.find("%");
1897 if (found != string::npos) {
1898 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
1899 if (TWFunc::Path_Exists(lun_file))
1900 has_multiple_lun = true;
1901 }
Ethan Yonker726a0202014-12-16 20:01:38 -06001902 mtp_was_enabled = TWFunc::Toggle_MTP(false); // Must disable MTP for USB Storage
Ethan Yonker47360be2014-04-01 10:34:34 -05001903 if (!has_multiple_lun) {
1904 LOGINFO("Device doesn't have multiple lun files, mount current storage\n");
1905 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
1906 if (TWFunc::Get_Root_Path(DataManager::GetCurrentStoragePath()) == "/data") {
Ethan Yonker66a19492015-12-10 10:19:45 -06001907 TWPartition* Mount = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001908 if (Mount) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001909 if (!Open_Lun_File(Mount->Mount_Point, lun_file)) {
1910 goto error_handle;
1911 }
Ethan Yonker47360be2014-04-01 10:34:34 -05001912 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001913 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001914 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001915 }
1916 } else if (!Open_Lun_File(DataManager::GetCurrentStoragePath(), lun_file)) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001917 goto error_handle;
Dees_Troy8170a922012-09-18 15:40:25 -04001918 }
Dees_Troy8170a922012-09-18 15:40:25 -04001919 } else {
Ethan Yonker47360be2014-04-01 10:34:34 -05001920 LOGINFO("Device has multiple lun files\n");
1921 TWPartition* Mount1;
1922 TWPartition* Mount2;
Dees_Troy8170a922012-09-18 15:40:25 -04001923 sprintf(lun_file, CUSTOM_LUN_FILE, 0);
Ethan Yonker66a19492015-12-10 10:19:45 -06001924 Mount1 = Find_Next_Storage("", true);
Ethan Yonker47360be2014-04-01 10:34:34 -05001925 if (Mount1) {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001926 if (!Open_Lun_File(Mount1->Mount_Point, lun_file)) {
1927 goto error_handle;
1928 }
Matt Moweree717062014-04-26 01:46:46 -05001929 sprintf(lun_file, CUSTOM_LUN_FILE, 1);
Ethan Yonker66a19492015-12-10 10:19:45 -06001930 Mount2 = Find_Next_Storage(Mount1->Mount_Point, true);
Matt Mower1fdcdb72016-01-25 20:53:47 -06001931 if (Mount2 && Mount2->Mount_Point != Mount1->Mount_Point) {
Matt Moweree717062014-04-26 01:46:46 -05001932 Open_Lun_File(Mount2->Mount_Point, lun_file);
Ethan Yonker47360be2014-04-01 10:34:34 -05001933 }
1934 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05001935 gui_err("unable_locate_storage=Unable to locate storage device.");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001936 goto error_handle;
Ethan Yonker47360be2014-04-01 10:34:34 -05001937 }
Dees_Troy8170a922012-09-18 15:40:25 -04001938 }
Matt Mowerb6ef4782014-11-06 02:24:36 -06001939 property_set("sys.storage.ums_enabled", "1");
HandyMenny37d42992014-10-26 22:15:59 +01001940 property_set("sys.usb.config", "mass_storage,adb");
Dees_Troy8170a922012-09-18 15:40:25 -04001941 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001942error_handle:
1943 if (mtp_was_enabled)
1944 if (!Enable_MTP())
1945 Disable_MTP();
1946 return false;
Dees_Troy8170a922012-09-18 15:40:25 -04001947}
1948
1949int TWPartitionManager::usb_storage_disable(void) {
Dees_Troy2673cec2013-04-02 20:22:16 +00001950 int index, ret;
1951 char lun_file[255], ch[2] = {0, 0};
1952 string str = ch;
Dees_Troy8170a922012-09-18 15:40:25 -04001953
1954 for (index=0; index<2; index++) {
1955 sprintf(lun_file, CUSTOM_LUN_FILE, index);
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06001956 ret = TWFunc::write_to_file(lun_file, str);
Dees_Troy2673cec2013-04-02 20:22:16 +00001957 if (ret < 0) {
1958 break;
Dees_Troy8170a922012-09-18 15:40:25 -04001959 }
Dees_Troy8170a922012-09-18 15:40:25 -04001960 }
Dees_Troye58d5262012-09-21 12:27:57 -04001961 Mount_All_Storage();
1962 Update_System_Details();
Dees_Troycfd73ef2012-10-12 16:52:00 -04001963 UnMount_Main_Partitions();
Matt Mowerd9cb9062013-12-14 20:34:45 -06001964 property_set("sys.storage.ums_enabled", "0");
HandyMenny37d42992014-10-26 22:15:59 +01001965 property_set("sys.usb.config", "adb");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04001966 if (mtp_was_enabled)
1967 if (!Enable_MTP())
1968 Disable_MTP();
Dees_Troy2673cec2013-04-02 20:22:16 +00001969 if (ret < 0 && index == 0) {
1970 LOGERR("Unable to write to ums lunfile '%s'.", lun_file);
1971 return false;
1972 } else {
1973 return true;
1974 }
Dees_Troy8170a922012-09-18 15:40:25 -04001975 return true;
Dees_Troy812660f2012-09-20 09:55:17 -04001976}
1977
1978void TWPartitionManager::Mount_All_Storage(void) {
1979 std::vector<TWPartition*>::iterator iter;
1980
1981 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
1982 if ((*iter)->Is_Storage)
1983 (*iter)->Mount(false);
1984 }
Dees_Troy2c50e182012-09-26 20:05:28 -04001985}
Dees_Troy9350b8d2012-09-27 12:38:38 -04001986
Dees_Troyd0384ef2012-10-12 12:15:42 -04001987void TWPartitionManager::UnMount_Main_Partitions(void) {
1988 // Unmounts system and data if data is not data/media
1989 // Also unmounts boot if boot is mountable
Dees_Troy2673cec2013-04-02 20:22:16 +00001990 LOGINFO("Unmounting main partitions...\n");
Dees_Troyd0384ef2012-10-12 12:15:42 -04001991
1992 TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
1993
Captain Throwback9d6feb52018-07-27 10:05:24 -04001994 UnMount_By_Path(Get_Android_Root_Path(), true);
Ethan Yonker6277c792014-09-15 14:54:30 -05001995 if (!datamedia)
1996 UnMount_By_Path("/data", true);
1997
Dees_Troyd0384ef2012-10-12 12:15:42 -04001998 if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
1999 Boot_Partition->UnMount(true);
2000}
2001
Dees_Troy9350b8d2012-09-27 12:38:38 -04002002int TWPartitionManager::Partition_SDCard(void) {
Matt Mower23d8aae2017-01-06 14:30:33 -06002003 char temp[255];
Ethan Yonker483e9f42016-01-11 22:21:18 -06002004 string Storage_Path, Command, Device, fat_str, ext_str, start_loc, end_loc, ext_format, sd_path, tmpdevice;
Dees_Troy9350b8d2012-09-27 12:38:38 -04002005 int ext, swap, total_size = 0, fat_size;
Dees_Troy9350b8d2012-09-27 12:38:38 -04002006
Ethan Yonker74db1572015-10-28 12:44:49 -05002007 gui_msg("start_partition_sd=Partitioning SD Card...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06002008
2009 // Locate and validate device to partition
2010 TWPartition* SDCard = Find_Partition_By_Path(DataManager::GetCurrentStoragePath());
2011
Ethan Yonker66a19492015-12-10 10:19:45 -06002012 if (SDCard->Is_Adopted_Storage)
2013 SDCard->Revert_Adopted();
2014
Ethan Yonker1eff6cd2014-09-15 13:30:42 -05002015 if (SDCard == NULL || !SDCard->Removable || SDCard->Has_Data_Media) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002016 gui_err("partition_sd_locate=Unable to locate device to partition.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04002017 return false;
2018 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06002019
2020 // Unmount everything
Dees_Troy9350b8d2012-09-27 12:38:38 -04002021 if (!SDCard->UnMount(true))
2022 return false;
2023 TWPartition* SDext = Find_Partition_By_Path("/sd-ext");
2024 if (SDext != NULL) {
2025 if (!SDext->UnMount(true))
2026 return false;
2027 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06002028 char* swappath = getenv("SWAPPATH");
2029 if (swappath != NULL) {
2030 LOGINFO("Unmounting swap at '%s'\n", swappath);
2031 umount(swappath);
2032 }
Vojtech Bocek05534202013-09-11 08:11:56 +02002033
Ethan Yonker483e9f42016-01-11 22:21:18 -06002034 // Determine block device
2035 if (SDCard->Alternate_Block_Device.empty()) {
2036 SDCard->Find_Actual_Block_Device();
2037 Device = SDCard->Actual_Block_Device;
2038 // Just use the root block device
2039 Device.resize(strlen("/dev/block/mmcblkX"));
2040 } else {
2041 Device = SDCard->Alternate_Block_Device;
2042 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04002043
2044 // Find the size of the block device:
Ethan Yonker483e9f42016-01-11 22:21:18 -06002045 total_size = (int)(TWFunc::IOCTL_Get_Block_Size(Device.c_str()) / (1048576));
Dees_Troy9350b8d2012-09-27 12:38:38 -04002046
2047 DataManager::GetValue("tw_sdext_size", ext);
2048 DataManager::GetValue("tw_swap_size", swap);
2049 DataManager::GetValue("tw_sdpart_file_system", ext_format);
2050 fat_size = total_size - ext - swap;
Ethan Yonker483e9f42016-01-11 22:21:18 -06002051 LOGINFO("sd card mount point %s block device is '%s', sdcard size is: %iMB, fat size: %iMB, ext size: %iMB, ext system: '%s', swap size: %iMB\n", DataManager::GetCurrentStoragePath().c_str(), Device.c_str(), total_size, fat_size, ext, ext_format.c_str(), swap);
2052
2053 // Determine partition sizes
2054 if (swap == 0 && ext == 0) {
2055 fat_str = "-0";
2056 } else {
2057 memset(temp, 0, sizeof(temp));
2058 sprintf(temp, "%i", fat_size);
2059 fat_str = temp;
2060 fat_str += "MB";
2061 }
2062 if (swap == 0) {
2063 ext_str = "-0";
2064 } else {
2065 memset(temp, 0, sizeof(temp));
2066 sprintf(temp, "%i", ext);
2067 ext_str = "+";
2068 ext_str += temp;
2069 ext_str += "MB";
2070 }
2071
Dees_Troy9350b8d2012-09-27 12:38:38 -04002072 if (ext + swap > total_size) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002073 gui_err("ext_swap_size=EXT + Swap size is larger than sdcard size.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04002074 return false;
2075 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06002076
Ethan Yonker74db1572015-10-28 12:44:49 -05002077 gui_msg("remove_part_table=Removing partition table...");
Ethan Yonker483e9f42016-01-11 22:21:18 -06002078 Command = "sgdisk --zap-all " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002079 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002080 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002081 gui_err("unable_rm_part=Unable to remove partition table.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04002082 Update_System_Details();
2083 return false;
2084 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002085 gui_msg(Msg("create_part=Creating {1} partition...")("FAT32"));
Captain Throwback9643a802016-05-27 11:44:51 -04002086 Command = "sgdisk --new=0:0:" + fat_str + " --change-name=0:\"Microsoft basic data\" --typecode=0:EBD0A0A2-B9E5-4433-87C0-68B6B72699C7 " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002087 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002088 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002089 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("FAT32"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04002090 return false;
2091 }
2092 if (ext > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002093 gui_msg(Msg("create_part=Creating {1} partition...")("EXT"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06002094 Command = "sgdisk --new=0:0:" + ext_str + " --change-name=0:\"Linux filesystem\" " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002095 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002096 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002097 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("EXT"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04002098 Update_System_Details();
2099 return false;
2100 }
2101 }
2102 if (swap > 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002103 gui_msg(Msg("create_part=Creating {1} partition...")("swap"));
Ethan Yonker483e9f42016-01-11 22:21:18 -06002104 Command = "sgdisk --new=0:0:-0 --change-name=0:\"Linux swap\" --typecode=0:0657FD6D-A4AB-43C4-84E5-0933C84B4F4F " + Device;
Dees_Troy2673cec2013-04-02 20:22:16 +00002105 LOGINFO("Command is: '%s'\n", Command.c_str());
Vojtech Bocek05534202013-09-11 08:11:56 +02002106 if (TWFunc::Exec_Cmd(Command) != 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002107 gui_msg(Msg(msg::kError, "unable_to_create_part=Unable to create {1} partition.")("swap"));
Dees_Troy9350b8d2012-09-27 12:38:38 -04002108 Update_System_Details();
2109 return false;
2110 }
2111 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06002112
2113 // Convert GPT to MBR
2114 Command = "sgdisk --gpttombr " + Device;
2115 if (TWFunc::Exec_Cmd(Command) != 0)
2116 LOGINFO("Failed to covert partition GPT to MBR\n");
2117
2118 // Tell the kernel to rescan the partition table
2119 int fd = open(Device.c_str(), O_RDONLY);
2120 ioctl(fd, BLKRRPART, 0);
2121 close(fd);
2122
2123 string format_device = Device;
2124 if (Device.substr(0, 17) == "/dev/block/mmcblk")
2125 format_device += "p";
2126
2127 // Format new partitions to proper file system
2128 if (fat_size > 0) {
2129 Command = "mkfs.fat " + format_device + "1";
2130 TWFunc::Exec_Cmd(Command);
2131 }
Dees_Troy9350b8d2012-09-27 12:38:38 -04002132 if (ext > 0) {
2133 if (SDext == NULL) {
Ethan Yonker483e9f42016-01-11 22:21:18 -06002134 Command = "mke2fs -t " + ext_format + " -m 0 " + format_device + "2";
2135 gui_msg(Msg("format_sdext_as=Formatting sd-ext as {1}...")(ext_format));
2136 LOGINFO("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str());
2137 TWFunc::Exec_Cmd(Command);
2138 } else {
2139 SDext->Wipe(ext_format);
Dees_Troy9350b8d2012-09-27 12:38:38 -04002140 }
Ethan Yonker483e9f42016-01-11 22:21:18 -06002141 }
2142 if (swap > 0) {
2143 Command = "mkswap " + format_device;
2144 if (ext > 0)
2145 Command += "3";
2146 else
2147 Command += "2";
Vojtech Bocek05534202013-09-11 08:11:56 +02002148 TWFunc::Exec_Cmd(Command);
Dees_Troy9350b8d2012-09-27 12:38:38 -04002149 }
2150
Ethan Yonker483e9f42016-01-11 22:21:18 -06002151 // recreate TWRP folder and rewrite settings - these will be gone after sdcard is partitioned
2152 if (SDCard->Mount(true)) {
2153 string TWRP_Folder = SDCard->Mount_Point + "/TWRP";
2154 mkdir(TWRP_Folder.c_str(), 0777);
2155 DataManager::Flush();
2156 }
2157
Dees_Troy9350b8d2012-09-27 12:38:38 -04002158 Update_System_Details();
Ethan Yonker74db1572015-10-28 12:44:49 -05002159 gui_msg("part_complete=Partitioning complete.");
Dees_Troy9350b8d2012-09-27 12:38:38 -04002160 return true;
bigbiff bigbiffa0f8a592012-10-09 21:01:03 -04002161}
Dees_Troya13d74f2013-03-24 08:54:55 -05002162
2163void TWPartitionManager::Get_Partition_List(string ListType, std::vector<PartitionList> *Partition_List) {
2164 std::vector<TWPartition*>::iterator iter;
2165 if (ListType == "mount") {
2166 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002167 if ((*iter)->Can_Be_Mounted) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002168 struct PartitionList part;
2169 part.Display_Name = (*iter)->Display_Name;
2170 part.Mount_Point = (*iter)->Mount_Point;
2171 part.selected = (*iter)->Is_Mounted();
2172 Partition_List->push_back(part);
2173 }
2174 }
2175 } else if (ListType == "storage") {
2176 char free_space[255];
2177 string Current_Storage = DataManager::GetCurrentStoragePath();
2178 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2179 if ((*iter)->Is_Storage) {
2180 struct PartitionList part;
2181 sprintf(free_space, "%llu", (*iter)->Free / 1024 / 1024);
2182 part.Display_Name = (*iter)->Storage_Name + " (";
2183 part.Display_Name += free_space;
2184 part.Display_Name += "MB)";
2185 part.Mount_Point = (*iter)->Storage_Path;
2186 if ((*iter)->Storage_Path == Current_Storage)
2187 part.selected = 1;
2188 else
2189 part.selected = 0;
2190 Partition_List->push_back(part);
2191 }
2192 }
2193 } else if (ListType == "backup") {
2194 char backup_size[255];
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002195 unsigned long long Backup_Size;
Dees_Troya13d74f2013-03-24 08:54:55 -05002196 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002197 if ((*iter)->Can_Be_Backed_Up && !(*iter)->Is_SubPartition && (*iter)->Is_Present) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002198 struct PartitionList part;
Dees_Troy9e0b71c2013-04-08 13:35:37 +00002199 Backup_Size = (*iter)->Backup_Size;
2200 if ((*iter)->Has_SubPartition) {
2201 std::vector<TWPartition*>::iterator subpart;
2202
2203 for (subpart = Partitions.begin(); subpart != Partitions.end(); subpart++) {
2204 if ((*subpart)->Is_SubPartition && (*subpart)->Can_Be_Backed_Up && (*subpart)->Is_Present && (*subpart)->SubPartition_Of == (*iter)->Mount_Point)
2205 Backup_Size += (*subpart)->Backup_Size;
2206 }
2207 }
2208 sprintf(backup_size, "%llu", Backup_Size / 1024 / 1024);
Dees_Troya13d74f2013-03-24 08:54:55 -05002209 part.Display_Name = (*iter)->Backup_Display_Name + " (";
2210 part.Display_Name += backup_size;
2211 part.Display_Name += "MB)";
2212 part.Mount_Point = (*iter)->Backup_Path;
2213 part.selected = 0;
2214 Partition_List->push_back(part);
2215 }
2216 }
2217 } else if (ListType == "restore") {
2218 string Restore_List, restore_path;
2219 TWPartition* restore_part = NULL;
2220
2221 DataManager::GetValue("tw_restore_list", Restore_List);
2222 if (!Restore_List.empty()) {
2223 size_t start_pos = 0, end_pos = Restore_List.find(";", start_pos);
2224 while (end_pos != string::npos && start_pos < Restore_List.size()) {
2225 restore_path = Restore_List.substr(start_pos, end_pos - start_pos);
bigbiff bigbiff19fb79c2016-09-05 21:04:51 -04002226 struct PartitionList part;
2227 if (restore_path.compare("ADB_Backup") == 0) {
2228 part.Display_Name = "ADB Backup";
2229 part.Mount_Point = "ADB Backup";
2230 part.selected = 1;
2231 Partition_List->push_back(part);
2232 break;
2233 }
Dees_Troy59df9262013-06-19 14:53:57 -05002234 if ((restore_part = Find_Partition_By_Path(restore_path)) != NULL) {
Dees Troy4159aed2014-02-28 17:24:43 +00002235 if ((restore_part->Backup_Name == "recovery" && !restore_part->Can_Be_Backed_Up) || restore_part->Is_SubPartition) {
Dees_Troya13d74f2013-03-24 08:54:55 -05002236 // Don't allow restore of recovery (causes problems on some devices)
Dees_Troy59df9262013-06-19 14:53:57 -05002237 // Don't add subpartitions to the list of items
Dees_Troya13d74f2013-03-24 08:54:55 -05002238 } else {
Dees_Troya13d74f2013-03-24 08:54:55 -05002239 part.Display_Name = restore_part->Backup_Display_Name;
2240 part.Mount_Point = restore_part->Backup_Path;
2241 part.selected = 1;
2242 Partition_List->push_back(part);
2243 }
2244 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002245 gui_msg(Msg(msg::kError, "restore_unable_locate=Unable to locate '{1}' partition for restoring.")(restore_path));
Dees_Troya13d74f2013-03-24 08:54:55 -05002246 }
2247 start_pos = end_pos + 1;
2248 end_pos = Restore_List.find(";", start_pos);
2249 }
2250 }
2251 } else if (ListType == "wipe") {
2252 struct PartitionList dalvik;
Ethan Yonker74db1572015-10-28 12:44:49 -05002253 dalvik.Display_Name = gui_parse_text("{@dalvik}");
Dees_Troya13d74f2013-03-24 08:54:55 -05002254 dalvik.Mount_Point = "DALVIK";
2255 dalvik.selected = 0;
2256 Partition_List->push_back(dalvik);
2257 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2258 if ((*iter)->Wipe_Available_in_GUI && !(*iter)->Is_SubPartition) {
2259 struct PartitionList part;
2260 part.Display_Name = (*iter)->Display_Name;
2261 part.Mount_Point = (*iter)->Mount_Point;
2262 part.selected = 0;
2263 Partition_List->push_back(part);
2264 }
2265 if ((*iter)->Has_Android_Secure) {
2266 struct PartitionList part;
2267 part.Display_Name = (*iter)->Backup_Display_Name;
2268 part.Mount_Point = (*iter)->Backup_Path;
2269 part.selected = 0;
2270 Partition_List->push_back(part);
2271 }
Dees_Troy74fb2e92013-04-15 14:35:47 +00002272 if ((*iter)->Has_Data_Media) {
2273 struct PartitionList datamedia;
2274 datamedia.Display_Name = (*iter)->Storage_Name;
2275 datamedia.Mount_Point = "INTERNAL";
2276 datamedia.selected = 0;
2277 Partition_List->push_back(datamedia);
2278 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002279 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002280 } else if (ListType == "flashimg") {
2281 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2282 if ((*iter)->Can_Flash_Img && (*iter)->Is_Present) {
2283 struct PartitionList part;
2284 part.Display_Name = (*iter)->Backup_Display_Name;
2285 part.Mount_Point = (*iter)->Backup_Path;
2286 part.selected = 0;
2287 Partition_List->push_back(part);
2288 }
2289 }
Ethan Yonker53796e72019-01-11 22:49:52 -06002290 if (DataManager::GetIntValue("tw_has_repack_tools") != 0 && DataManager::GetIntValue("tw_has_boot_slots") != 0) {
2291 TWPartition* boot = Find_Partition_By_Path("/boot");
2292 if (boot) {
2293 // Allow flashing kernels and ramdisks
2294 struct PartitionList repack_ramdisk;
2295 repack_ramdisk.Display_Name = gui_lookup("install_twrp_ramdisk", "Install Recovery Ramdisk");
2296 repack_ramdisk.Mount_Point = "/repack_ramdisk";
2297 repack_ramdisk.selected = 0;
2298 Partition_List->push_back(repack_ramdisk);
2299 /*struct PartitionList repack_kernel; For now let's leave repacking kernels under advanced only
2300 repack_kernel.Display_Name = gui_lookup("install_kernel", "Install Kernel");
2301 repack_kernel.Mount_Point = "/repack_kernel";
2302 repack_kernel.selected = 0;
2303 Partition_List->push_back(repack_kernel);*/
2304 }
2305 }
Dees_Troya13d74f2013-03-24 08:54:55 -05002306 } else {
Dees_Troy2673cec2013-04-02 20:22:16 +00002307 LOGERR("Unknown list type '%s' requested for TWPartitionManager::Get_Partition_List\n", ListType.c_str());
Dees_Troya13d74f2013-03-24 08:54:55 -05002308 }
2309}
2310
2311int TWPartitionManager::Fstab_Processed(void) {
2312 return Partitions.size();
2313}
Dees_Troyd93bda52013-07-03 19:55:19 +00002314
2315void TWPartitionManager::Output_Storage_Fstab(void) {
2316 std::vector<TWPartition*>::iterator iter;
2317 char storage_partition[255];
bigbiff bigbiffe4bdb152019-03-23 18:33:17 -04002318 std::string Temp;
bigbiff25d25b92020-06-19 16:07:38 -04002319 std::string cacheDir = TWFunc::get_log_dir();
bigbiff bigbiffe4bdb152019-03-23 18:33:17 -04002320
2321 if (cacheDir.empty()) {
2322 LOGINFO("Unable to find cache directory\n");
2323 return;
2324 }
bigbiff bigbiff19874f12019-01-08 20:06:57 -05002325
bigbiff25d25b92020-06-19 16:07:38 -04002326 std::string storageFstab = TWFunc::get_log_dir() + "recovery/storage.fstab";
bigbiff bigbiff19874f12019-01-08 20:06:57 -05002327 FILE *fp = fopen(storageFstab.c_str(), "w");
Dees_Troyd93bda52013-07-03 19:55:19 +00002328
2329 if (fp == NULL) {
bigbiff bigbiff19874f12019-01-08 20:06:57 -05002330 gui_msg(Msg(msg::kError, "unable_to_open=Unable to open '{1}'.")(storageFstab));
Dees_Troyd93bda52013-07-03 19:55:19 +00002331 return;
2332 }
2333
2334 // Iterate through all partitions
2335 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2336 if ((*iter)->Is_Storage) {
2337 Temp = (*iter)->Storage_Path + ";" + (*iter)->Storage_Name + ";\n";
2338 strcpy(storage_partition, Temp.c_str());
2339 fwrite(storage_partition, sizeof(storage_partition[0]), strlen(storage_partition) / sizeof(storage_partition[0]), fp);
2340 }
2341 }
2342 fclose(fp);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +02002343}
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002344
2345TWPartition *TWPartitionManager::Get_Default_Storage_Partition()
2346{
2347 TWPartition *res = NULL;
2348 for (std::vector<TWPartition*>::iterator iter = Partitions.begin(); iter != Partitions.end(); ++iter) {
Matt Mowera8a89d12016-12-30 18:10:37 -06002349 if (!(*iter)->Is_Storage)
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002350 continue;
2351
Matt Mowera8a89d12016-12-30 18:10:37 -06002352 if ((*iter)->Is_Settings_Storage)
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002353 return *iter;
2354
Matt Mowera8a89d12016-12-30 18:10:37 -06002355 if (!res)
Vojtech Bocek93cb1ef2014-05-12 15:41:52 +02002356 res = *iter;
2357 }
2358 return res;
2359}
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002360
2361bool TWPartitionManager::Enable_MTP(void) {
2362#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002363 if (mtppid) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002364 gui_err("mtp_already_enabled=MTP already enabled");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002365 return true;
2366 }
Ethan Yonker726a0202014-12-16 20:01:38 -06002367
2368 int mtppipe[2];
2369
2370 if (pipe(mtppipe) < 0) {
2371 LOGERR("Error creating MTP pipe\n");
2372 return false;
2373 }
2374
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002375 char old_value[PROPERTY_VALUE_MAX];
Matt Mowere07f0102017-02-23 17:40:00 -06002376 property_get("sys.usb.config", old_value, "");
2377 if (strcmp(old_value, "mtp,adb") != 0) {
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002378 char vendor[PROPERTY_VALUE_MAX];
2379 char product[PROPERTY_VALUE_MAX];
2380 property_set("sys.usb.config", "none");
2381 property_get("usb.vendor", vendor, "18D1");
2382 property_get("usb.product.mtpadb", product, "4EE2");
2383 string vendorstr = vendor;
2384 string productstr = product;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002385 TWFunc::write_to_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2386 TWFunc::write_to_file("/sys/class/android_usb/android0/idProduct", productstr);
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002387 property_set("sys.usb.config", "mtp,adb");
2388 }
Matt Mower653a1702017-02-16 10:38:52 -06002389 /* To enable MTP debug, use the twrp command line feature:
Ethan Yonker6d154c42014-09-03 14:19:43 -05002390 * twrp set tw_mtp_debug 1
2391 */
2392 twrpMtp *mtp = new twrpMtp(DataManager::GetIntValue("tw_mtp_debug"));
Ethan Yonker1b039202015-01-30 10:08:48 -06002393 mtppid = mtp->forkserver(mtppipe);
2394 if (mtppid) {
2395 close(mtppipe[0]); // Host closes read side
2396 mtp_write_fd = mtppipe[1];
2397 DataManager::SetValue("tw_mtp_enabled", 1);
2398 Add_All_MTP_Storage();
2399 return true;
Ethan Yonker726a0202014-12-16 20:01:38 -06002400 } else {
2401 close(mtppipe[0]);
2402 close(mtppipe[1]);
Ethan Yonker74db1572015-10-28 12:44:49 -05002403 gui_err("mtp_fail=Failed to enable MTP");
Ethan Yonker1b039202015-01-30 10:08:48 -06002404 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002405 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002406#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002407 gui_err("no_mtp=MTP support not included");
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002408#endif
2409 DataManager::SetValue("tw_mtp_enabled", 0);
2410 return false;
2411}
2412
Ethan Yonker1b039202015-01-30 10:08:48 -06002413void TWPartitionManager::Add_All_MTP_Storage(void) {
2414#ifdef TW_HAS_MTP
2415 std::vector<TWPartition*>::iterator iter;
2416
2417 if (!mtppid)
2418 return; // MTP is not enabled
2419
2420 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2421 if ((*iter)->Is_Storage && (*iter)->Is_Present && (*iter)->Mount(false))
2422 Add_Remove_MTP_Storage((*iter), MTP_MESSAGE_ADD_STORAGE);
2423 }
2424#else
2425 return;
2426#endif
2427}
2428
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002429bool TWPartitionManager::Disable_MTP(void) {
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002430 char old_value[PROPERTY_VALUE_MAX];
bigbiff bigbiffaf32bb92018-12-18 18:39:53 -05002431 property_set("sys.usb.ffs.mtp.ready", "0");
Matt Mowere07f0102017-02-23 17:40:00 -06002432 property_get("sys.usb.config", old_value, "");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002433 if (strcmp(old_value, "adb") != 0) {
2434 char vendor[PROPERTY_VALUE_MAX];
2435 char product[PROPERTY_VALUE_MAX];
2436 property_set("sys.usb.config", "none");
2437 property_get("usb.vendor", vendor, "18D1");
Matt Mowere07f0102017-02-23 17:40:00 -06002438 property_get("usb.product.adb", product, "D001");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002439 string vendorstr = vendor;
2440 string productstr = product;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002441 TWFunc::write_to_file("/sys/class/android_usb/android0/idVendor", vendorstr);
2442 TWFunc::write_to_file("/sys/class/android_usb/android0/idProduct", productstr);
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002443 usleep(2000);
2444 }
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002445#ifdef TW_HAS_MTP
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002446 if (mtppid) {
Ethan Yonker8613dc02014-09-11 09:28:20 -05002447 LOGINFO("Disabling MTP\n");
2448 int status;
2449 kill(mtppid, SIGKILL);
Ethan Yonker8dfa7772014-09-04 21:48:41 -05002450 mtppid = 0;
Ethan Yonker8613dc02014-09-11 09:28:20 -05002451 // We don't care about the exit value, but this prevents a zombie process
2452 waitpid(mtppid, &status, 0);
Ethan Yonker726a0202014-12-16 20:01:38 -06002453 close(mtp_write_fd);
2454 mtp_write_fd = -1;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002455 }
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002456#endif
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002457 property_set("sys.usb.config", "adb");
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002458#ifdef TW_HAS_MTP
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002459 DataManager::SetValue("tw_mtp_enabled", 0);
2460 return true;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002461#endif
Ethan Yonkerdf7abac2014-12-29 10:48:17 -06002462 return false;
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -04002463}
Ethan Yonker726a0202014-12-16 20:01:38 -06002464
2465TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {
2466 std::vector<TWPartition*>::iterator iter;
2467
2468 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2469 if ((*iter)->MTP_Storage_ID == Storage_ID)
2470 return (*iter);
2471 }
2472 return NULL;
2473}
2474
2475bool TWPartitionManager::Add_Remove_MTP_Storage(TWPartition* Part, int message_type) {
2476#ifdef TW_HAS_MTP
2477 struct mtpmsg mtp_message;
2478
2479 if (!mtppid)
2480 return false; // MTP is disabled
2481
2482 if (mtp_write_fd < 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002483 LOGINFO("MTP: mtp_write_fd is not set\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002484 return false;
2485 }
2486
2487 if (Part) {
Ethan Yonker4bfabab2014-12-29 09:15:37 -06002488 if (Part->MTP_Storage_ID == 0)
2489 return false;
Ethan Yonker726a0202014-12-16 20:01:38 -06002490 if (message_type == MTP_MESSAGE_REMOVE_STORAGE) {
2491 mtp_message.message_type = MTP_MESSAGE_REMOVE_STORAGE; // Remove
2492 LOGINFO("sending message to remove %i\n", Part->MTP_Storage_ID);
2493 mtp_message.storage_id = Part->MTP_Storage_ID;
2494 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002495 LOGINFO("error sending message to remove storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002496 return false;
2497 } else {
2498 LOGINFO("Message sent, remove storage ID: %i\n", Part->MTP_Storage_ID);
2499 return true;
2500 }
2501 } else if (message_type == MTP_MESSAGE_ADD_STORAGE && Part->Is_Mounted()) {
2502 mtp_message.message_type = MTP_MESSAGE_ADD_STORAGE; // Add
2503 mtp_message.storage_id = Part->MTP_Storage_ID;
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002504 if (Part->Storage_Path.size() >= sizeof(mtp_message.path)) {
2505 LOGERR("Storage path '%s' too large for mtpmsg\n", Part->Storage_Path.c_str());
2506 return false;
2507 }
2508 strcpy(mtp_message.path, Part->Storage_Path.c_str());
2509 if (Part->Storage_Name.size() >= sizeof(mtp_message.display)) {
2510 LOGERR("Storage name '%s' too large for mtpmsg\n", Part->Storage_Name.c_str());
2511 return false;
2512 }
2513 strcpy(mtp_message.display, Part->Storage_Name.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002514 mtp_message.maxFileSize = Part->Get_Max_FileSize();
Ethan Yonker1b039202015-01-30 10:08:48 -06002515 LOGINFO("sending message to add %i '%s' '%s'\n", mtp_message.storage_id, mtp_message.path, mtp_message.display);
Ethan Yonker726a0202014-12-16 20:01:38 -06002516 if (write(mtp_write_fd, &mtp_message, sizeof(mtp_message)) <= 0) {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002517 LOGINFO("error sending message to add storage %i\n", Part->MTP_Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002518 return false;
2519 } else {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002520 LOGINFO("Message sent, add storage ID: %i '%s'\n", Part->MTP_Storage_ID, mtp_message.path);
Ethan Yonker726a0202014-12-16 20:01:38 -06002521 return true;
2522 }
2523 } else {
2524 LOGERR("Unknown MTP message type: %i\n", message_type);
2525 }
2526 } else {
2527 // This hopefully never happens as the error handling should
2528 // occur in the calling function.
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002529 LOGINFO("TWPartitionManager::Add_Remove_MTP_Storage NULL partition given\n");
Ethan Yonker726a0202014-12-16 20:01:38 -06002530 }
2531 return true;
2532#else
Ethan Yonker74db1572015-10-28 12:44:49 -05002533 gui_err("no_mtp=MTP support not included");
Ethan Yonker726a0202014-12-16 20:01:38 -06002534 DataManager::SetValue("tw_mtp_enabled", 0);
2535 return false;
2536#endif
2537}
2538
2539bool TWPartitionManager::Add_MTP_Storage(string Mount_Point) {
2540#ifdef TW_HAS_MTP
2541 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2542 if (Part) {
2543 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2544 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002545 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002546 }
2547#endif
2548 return false;
2549}
2550
2551bool TWPartitionManager::Add_MTP_Storage(unsigned int Storage_ID) {
2552#ifdef TW_HAS_MTP
2553 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2554 if (Part) {
2555 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_ADD_STORAGE);
2556 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002557 LOGINFO("TWFunc::Add_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002558 }
2559#endif
2560 return false;
2561}
2562
2563bool TWPartitionManager::Remove_MTP_Storage(string Mount_Point) {
2564#ifdef TW_HAS_MTP
2565 TWPartition* Part = PartitionManager.Find_Partition_By_Path(Mount_Point);
2566 if (Part) {
2567 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2568 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002569 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for '%s'\n", Mount_Point.c_str());
Ethan Yonker726a0202014-12-16 20:01:38 -06002570 }
2571#endif
2572 return false;
2573}
2574
2575bool TWPartitionManager::Remove_MTP_Storage(unsigned int Storage_ID) {
2576#ifdef TW_HAS_MTP
2577 TWPartition* Part = PartitionManager.Find_Partition_By_MTP_Storage_ID(Storage_ID);
2578 if (Part) {
2579 return PartitionManager.Add_Remove_MTP_Storage(Part, MTP_MESSAGE_REMOVE_STORAGE);
2580 } else {
Ethan Yonkerd9ff3c52015-01-21 21:51:20 -06002581 LOGINFO("TWFunc::Remove_MTP_Storage unable to locate partition for %i\n", Storage_ID);
Ethan Yonker726a0202014-12-16 20:01:38 -06002582 }
2583#endif
2584 return false;
2585}
Ethan Yonker96af84a2015-01-05 14:58:36 -06002586
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002587bool TWPartitionManager::Flash_Image(string& path, string& filename) {
bigbiffad58e1b2020-07-06 20:24:34 -04002588 twrpRepacker repacker;
Matt Mower23d8aae2017-01-06 14:30:33 -06002589 int partition_count = 0;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002590 TWPartition* flash_part = NULL;
bigbiffce8f83c2015-12-12 18:30:21 -05002591 string Flash_List, flash_path, full_filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002592 size_t start_pos = 0, end_pos = 0;
2593
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002594 full_filename = path + "/" + filename;
Ethan Yonker96af84a2015-01-05 14:58:36 -06002595
bigbiffce8f83c2015-12-12 18:30:21 -05002596 gui_msg("image_flash_start=[IMAGE FLASH STARTED]");
2597 gui_msg(Msg("img_to_flash=Image to flash: '{1}'")(full_filename));
2598
2599 if (!TWFunc::Path_Exists(full_filename)) {
2600 if (!Mount_By_Path(full_filename, true)) {
Ethan Yonkerb78fbdf2016-01-15 16:46:35 -06002601 return false;
2602 }
bigbiffce8f83c2015-12-12 18:30:21 -05002603 if (!TWFunc::Path_Exists(full_filename)) {
2604 gui_msg(Msg(msg::kError, "unable_to_locate=Unable to locate {1}.")(full_filename));
Ethan Yonkerb78fbdf2016-01-15 16:46:35 -06002605 return false;
2606 }
2607 }
Ethan Yonker96af84a2015-01-05 14:58:36 -06002608
Ethan Yonker53796e72019-01-11 22:49:52 -06002609 DataManager::GetValue("tw_flash_partition", Flash_List);
2610 Repack_Type repack = REPLACE_NONE;
2611 if (Flash_List == "/repack_ramdisk;") {
2612 repack = REPLACE_RAMDISK;
2613 } else if (Flash_List == "/repack_kernel;") {
2614 repack = REPLACE_KERNEL;
2615 }
2616 if (repack != REPLACE_NONE) {
2617 Repack_Options_struct Repack_Options;
2618 Repack_Options.Type = repack;
2619 Repack_Options.Disable_Verity = false;
2620 Repack_Options.Disable_Force_Encrypt = false;
2621 Repack_Options.Backup_First = DataManager::GetIntValue("tw_repack_backup_first") != 0;
bigbiffad58e1b2020-07-06 20:24:34 -04002622 return repacker.Repack_Image_And_Flash(full_filename, Repack_Options);
Ethan Yonker53796e72019-01-11 22:49:52 -06002623 }
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002624 PartitionSettings part_settings;
2625 part_settings.Backup_Folder = path;
2626 unsigned long long total_bytes = TWFunc::Get_File_Size(full_filename);
2627 ProgressTracking progress(total_bytes);
2628 part_settings.progress = &progress;
2629 part_settings.adbbackup = false;
2630 part_settings.PM_Method = PM_RESTORE;
Ethan Yonker74db1572015-10-28 12:44:49 -05002631 gui_msg("calc_restore=Calculating restore details...");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002632 if (!Flash_List.empty()) {
2633 end_pos = Flash_List.find(";", start_pos);
2634 while (end_pos != string::npos && start_pos < Flash_List.size()) {
2635 flash_path = Flash_List.substr(start_pos, end_pos - start_pos);
2636 flash_part = Find_Partition_By_Path(flash_path);
2637 if (flash_part != NULL) {
2638 partition_count++;
2639 if (partition_count > 1) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002640 gui_err("too_many_flash=Too many partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002641 return false;
2642 }
2643 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002644 gui_msg(Msg(msg::kError, "flash_unable_locate=Unable to locate '{1}' partition for flashing.")(flash_path));
Ethan Yonker96af84a2015-01-05 14:58:36 -06002645 return false;
2646 }
2647 start_pos = end_pos + 1;
2648 end_pos = Flash_List.find(";", start_pos);
2649 }
2650 }
2651
2652 if (partition_count == 0) {
Ethan Yonker74db1572015-10-28 12:44:49 -05002653 gui_err("no_part_flash=No partitions selected for flashing.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002654 return false;
2655 }
2656
2657 DataManager::SetProgress(0.0);
2658 if (flash_part) {
Ethan Yonkere080c1f2016-09-19 13:50:25 -05002659 flash_part->Backup_FileName = filename;
2660 if (!flash_part->Flash_Image(&part_settings))
Ethan Yonker96af84a2015-01-05 14:58:36 -06002661 return false;
2662 } else {
Ethan Yonker74db1572015-10-28 12:44:49 -05002663 gui_err("invalid_flash=Invalid flash partition specified.");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002664 return false;
2665 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002666 gui_highlight("flash_done=IMAGE FLASH COMPLETED]");
Ethan Yonker96af84a2015-01-05 14:58:36 -06002667 return true;
2668}
Ethan Yonker74db1572015-10-28 12:44:49 -05002669
2670void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value) {
2671 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2672 if (part) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002673 if (part->Is_Adopted_Storage) {
2674 part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data");
2675 part->Backup_Display_Name = part->Display_Name;
2676 part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage");
2677 } else {
2678 part->Display_Name = gui_lookup(resource_name, default_value);
2679 part->Backup_Display_Name = part->Display_Name;
2680 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002681 }
2682}
2683
2684void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value, const char* storage_resource_name, const char* storage_default_value) {
2685 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2686 if (part) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002687 if (part->Is_Adopted_Storage) {
Ethan Yonker01f4e032017-02-03 15:30:52 -06002688 part->Backup_Display_Name = part->Display_Name + " - " + gui_lookup("data_backup", "Data (excl. storage)");
Ethan Yonker66a19492015-12-10 10:19:45 -06002689 part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data");
Ethan Yonker66a19492015-12-10 10:19:45 -06002690 part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage");
2691 } else {
2692 part->Display_Name = gui_lookup(resource_name, default_value);
2693 part->Backup_Display_Name = part->Display_Name;
2694 if (part->Is_Storage)
2695 part->Storage_Name = gui_lookup(storage_resource_name, storage_default_value);
2696 }
Ethan Yonker74db1572015-10-28 12:44:49 -05002697 }
2698}
2699
Ethan Yonker01f4e032017-02-03 15:30:52 -06002700void TWPartitionManager::Translate_Partition(const char* path, const char* resource_name, const char* default_value, const char* storage_resource_name, const char* storage_default_value, const char* backup_name, const char* backup_default) {
2701 TWPartition* part = PartitionManager.Find_Partition_By_Path(path);
2702 if (part) {
2703 if (part->Is_Adopted_Storage) {
2704 part->Backup_Display_Name = part->Display_Name + " - " + gui_lookup(backup_name, backup_default);
2705 part->Display_Name = part->Display_Name + " - " + gui_lookup("data", "Data");
2706 part->Storage_Name = part->Storage_Name + " - " + gui_lookup("adopted_storage", "Adopted Storage");
2707 } else {
2708 part->Display_Name = gui_lookup(resource_name, default_value);
2709 part->Backup_Display_Name = gui_lookup(backup_name, backup_default);
2710 if (part->Is_Storage)
2711 part->Storage_Name = gui_lookup(storage_resource_name, storage_default_value);
2712 }
2713 }
2714}
2715
Ethan Yonker74db1572015-10-28 12:44:49 -05002716void TWPartitionManager::Translate_Partition_Display_Names() {
Ethan Yonker66a19492015-12-10 10:19:45 -06002717 LOGINFO("Translating partition display names\n");
Ethan Yonker74db1572015-10-28 12:44:49 -05002718 Translate_Partition("/system", "system", "System");
2719 Translate_Partition("/system_image", "system_image", "System Image");
2720 Translate_Partition("/vendor", "vendor", "Vendor");
2721 Translate_Partition("/vendor_image", "vendor_image", "Vendor Image");
2722 Translate_Partition("/cache", "cache", "Cache");
Ethan Yonker74db1572015-10-28 12:44:49 -05002723 Translate_Partition("/boot", "boot", "Boot");
2724 Translate_Partition("/recovery", "recovery", "Recovery");
2725 if (!datamedia) {
Ethan Yonker01f4e032017-02-03 15:30:52 -06002726 Translate_Partition("/data", "data", "Data", "internal", "Internal Storage");
Ethan Yonker74db1572015-10-28 12:44:49 -05002727 Translate_Partition("/sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2728 Translate_Partition("/internal_sd", "sdcard", "SDCard", "sdcard", "SDCard");
2729 Translate_Partition("/internal_sdcard", "sdcard", "SDCard", "sdcard", "SDCard");
2730 Translate_Partition("/emmc", "sdcard", "SDCard", "sdcard", "SDCard");
Ethan Yonker01f4e032017-02-03 15:30:52 -06002731 } else {
2732 Translate_Partition("/data", "data", "Data", "internal", "Internal Storage", "data_backup", "Data (excl. storage)");
Ethan Yonker74db1572015-10-28 12:44:49 -05002733 }
Ethan Yonker01f4e032017-02-03 15:30:52 -06002734 Translate_Partition("/external_sd", "microsd", "Micro SDCard", "microsd", "Micro SDCard", "data_backup", "Data (excl. storage)");
2735 Translate_Partition("/external_sdcard", "microsd", "Micro SDCard", "microsd", "Micro SDCard", "data_backup", "Data (excl. storage)");
Ethan Yonker74db1572015-10-28 12:44:49 -05002736 Translate_Partition("/usb-otg", "usbotg", "USB OTG", "usbotg", "USB OTG");
2737 Translate_Partition("/sd-ext", "sdext", "SD-EXT");
2738
2739 // Android secure is a special case
2740 TWPartition* part = PartitionManager.Find_Partition_By_Path("/and-sec");
2741 if (part)
2742 part->Backup_Display_Name = gui_lookup("android_secure", "Android Secure");
2743
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002744 std::vector<TWPartition*>::iterator sysfs;
2745 for (sysfs = Partitions.begin(); sysfs != Partitions.end(); sysfs++) {
2746 if (!(*sysfs)->Sysfs_Entry.empty()) {
2747 Translate_Partition((*sysfs)->Mount_Point.c_str(), "autostorage", "Storage", "autostorage", "Storage");
2748 }
2749 }
2750
Ethan Yonker74db1572015-10-28 12:44:49 -05002751 // This updates the text on all of the storage selection buttons in the GUI
2752 DataManager::SetBackupFolder();
2753}
Ethan Yonker66a19492015-12-10 10:19:45 -06002754
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002755bool TWPartitionManager::Decrypt_Adopted() {
Ethan Yonker66a19492015-12-10 10:19:45 -06002756#ifdef TW_INCLUDE_CRYPTO
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002757 bool ret = false;
Ethan Yonker66a19492015-12-10 10:19:45 -06002758 if (!Mount_By_Path("/data", false)) {
2759 LOGERR("Cannot decrypt adopted storage because /data will not mount\n");
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002760 return false;
Ethan Yonker66a19492015-12-10 10:19:45 -06002761 }
2762 LOGINFO("Decrypt adopted storage starting\n");
2763 char* xmlFile = PageManager::LoadFileToBuffer("/data/system/storage.xml", NULL);
2764 xml_document<> *doc = NULL;
2765 xml_node<>* volumes = NULL;
Ethan Yonker66a19492015-12-10 10:19:45 -06002766 string Primary_Storage_UUID = "";
2767 if (xmlFile != NULL) {
2768 LOGINFO("successfully loaded storage.xml\n");
2769 doc = new xml_document<>();
2770 doc->parse<0>(xmlFile);
2771 volumes = doc->first_node("volumes");
2772 if (volumes) {
2773 xml_attribute<>* psuuid = volumes->first_attribute("primaryStorageUuid");
2774 if (psuuid) {
2775 Primary_Storage_UUID = psuuid->value();
2776 }
2777 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002778 } else {
2779 LOGINFO("No /data/system/storage.xml for adopted storage\n");
2780 return false;
Ethan Yonker66a19492015-12-10 10:19:45 -06002781 }
2782 std::vector<TWPartition*>::iterator adopt;
2783 for (adopt = Partitions.begin(); adopt != Partitions.end(); adopt++) {
2784 if ((*adopt)->Removable && (*adopt)->Is_Present) {
2785 if ((*adopt)->Decrypt_Adopted() == 0) {
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002786 ret = true;
Ethan Yonker66a19492015-12-10 10:19:45 -06002787 if (volumes) {
2788 xml_node<>* volume = volumes->first_node("volume");
2789 while (volume) {
2790 xml_attribute<>* guid = volume->first_attribute("partGuid");
2791 if (guid) {
2792 string GUID = (*adopt)->Adopted_GUID.c_str();
2793 GUID.insert(8, "-");
2794 GUID.insert(13, "-");
2795 GUID.insert(18, "-");
2796 GUID.insert(23, "-");
2797
2798 if (strcasecmp(GUID.c_str(), guid->value()) == 0) {
2799 xml_attribute<>* attr = volume->first_attribute("nickname");
nkk71ffb02bd2017-06-04 23:12:16 +03002800 if (attr && attr->value() && strlen(attr->value()) > 0) {
Ethan Yonker66a19492015-12-10 10:19:45 -06002801 (*adopt)->Storage_Name = attr->value();
2802 (*adopt)->Display_Name = (*adopt)->Storage_Name;
2803 (*adopt)->Backup_Display_Name = (*adopt)->Storage_Name;
2804 LOGINFO("storage name from storage.xml is '%s'\n", attr->value());
2805 }
2806 attr = volume->first_attribute("fsUuid");
2807 if (attr && !Primary_Storage_UUID.empty() && strcmp(Primary_Storage_UUID.c_str(), attr->value()) == 0) {
2808 TWPartition* Dat = Find_Partition_By_Path("/data");
2809 if (Dat) {
2810 LOGINFO("Internal storage is found on adopted storage '%s'\n", (*adopt)->Display_Name.c_str());
2811 LOGINFO("Changing '%s' to point to '%s'\n", Dat->Symlink_Mount_Point.c_str(), (*adopt)->Storage_Path.c_str());
2812 (*adopt)->Symlink_Mount_Point = Dat->Symlink_Mount_Point;
2813 Dat->Symlink_Mount_Point = "";
2814 // Toggle mounts to ensure that the symlink mount point (probably /sdcard) is mounted to the right location
2815 Dat->UnMount(false);
2816 Dat->Mount(false);
2817 (*adopt)->UnMount(false);
2818 (*adopt)->Mount(false);
Ethan Yonker66a19492015-12-10 10:19:45 -06002819 }
2820 }
2821 break;
2822 }
2823 }
2824 volume = volume->next_sibling("volume");
2825 }
2826 }
nkk71ffb02bd2017-06-04 23:12:16 +03002827 Update_System_Details();
2828 Output_Partition((*adopt));
Ethan Yonker66a19492015-12-10 10:19:45 -06002829 }
2830 }
2831 }
2832 if (xmlFile) {
2833 doc->clear();
2834 delete doc;
2835 free(xmlFile);
2836 }
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002837 return ret;
Ethan Yonker66a19492015-12-10 10:19:45 -06002838#else
2839 LOGINFO("Decrypt_Adopted: no crypto support\n");
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002840 return false;
Ethan Yonker66a19492015-12-10 10:19:45 -06002841#endif
2842}
Ethan Yonkerfcf3f242016-02-16 12:30:26 -06002843
2844void TWPartitionManager::Remove_Partition_By_Path(string Path) {
2845 std::vector<TWPartition*>::iterator iter;
2846 string Local_Path = TWFunc::Get_Root_Path(Path);
2847
2848 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2849 if ((*iter)->Mount_Point == Local_Path || (!(*iter)->Symlink_Mount_Point.empty() && (*iter)->Symlink_Mount_Point == Local_Path)) {
2850 LOGINFO("Found and erasing '%s' from partition list\n", Local_Path.c_str());
2851 Partitions.erase(iter);
2852 return;
2853 }
2854 }
2855}
Ethan Yonker1b190162016-12-05 15:25:19 -06002856
2857void TWPartitionManager::Set_Active_Slot(const string& Slot) {
2858 if (Slot != "A" && Slot != "B") {
2859 LOGERR("Set_Active_Slot invalid slot '%s'\n", Slot.c_str());
2860 return;
2861 }
2862 if (Active_Slot_Display == Slot)
2863 return;
2864 LOGINFO("Setting active slot %s\n", Slot.c_str());
2865#ifdef AB_OTA_UPDATER
2866 if (!Active_Slot_Display.empty()) {
2867 const hw_module_t *hw_module;
2868 boot_control_module_t *module;
2869 int ret;
2870 ret = hw_get_module("bootctrl", &hw_module);
2871 if (ret != 0) {
2872 LOGERR("Error getting bootctrl module.\n");
2873 } else {
2874 module = (boot_control_module_t*) hw_module;
2875 module->init(module);
2876 int slot_number = 0;
2877 if (Slot == "B")
2878 slot_number = 1;
2879 if (module->setActiveBootSlot(module, slot_number))
2880 gui_msg(Msg(msg::kError, "unable_set_boot_slot=Error changing bootloader boot slot to {1}")(Slot));
2881 }
2882 DataManager::SetValue("tw_active_slot", Slot); // Doing this outside of this if block may result in a seg fault because the DataManager may not be ready yet
2883 }
2884#else
2885 LOGERR("Boot slot feature not present\n");
2886#endif
2887 Active_Slot_Display = Slot;
2888 if (Fstab_Processed())
2889 Update_System_Details();
2890}
2891string TWPartitionManager::Get_Active_Slot_Suffix() {
2892 if (Active_Slot_Display == "A")
2893 return "_a";
2894 return "_b";
2895}
2896string TWPartitionManager::Get_Active_Slot_Display() {
2897 return Active_Slot_Display;
2898}
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002899
Captain Throwback9d6feb52018-07-27 10:05:24 -04002900string TWPartitionManager::Get_Android_Root_Path() {
Chaosmasterf6e42ce2020-01-27 00:17:04 +01002901 if (property_get_bool("ro.twrp.sar", false))
dianlujitao58f1a632020-01-16 23:03:56 +08002902 return "/system_root";
2903 return "/system";
Captain Throwback9d6feb52018-07-27 10:05:24 -04002904}
2905
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002906void TWPartitionManager::Remove_Uevent_Devices(const string& Mount_Point) {
2907 std::vector<TWPartition*>::iterator iter;
2908
2909 for (iter = Partitions.begin(); iter != Partitions.end(); ) {
2910 if ((*iter)->Is_SubPartition && (*iter)->SubPartition_Of == Mount_Point) {
2911 TWPartition *part = *iter;
2912 LOGINFO("%s was removed by uevent data\n", (*iter)->Mount_Point.c_str());
2913 (*iter)->UnMount(false);
2914 rmdir((*iter)->Mount_Point.c_str());
2915 iter = Partitions.erase(iter);
2916 delete part;
2917 } else {
2918 iter++;
2919 }
2920 }
2921}
2922
2923void TWPartitionManager::Handle_Uevent(const Uevent_Block_Data& uevent_data) {
2924 std::vector<TWPartition*>::iterator iter;
2925
2926 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
2927 if (!(*iter)->Sysfs_Entry.empty()) {
2928 string device;
2929 size_t wildcard = (*iter)->Sysfs_Entry.find("*");
2930 if (wildcard != string::npos) {
2931 device = (*iter)->Sysfs_Entry.substr(0, wildcard);
2932 } else {
2933 device = (*iter)->Sysfs_Entry;
2934 }
2935 if (device == uevent_data.sysfs_path.substr(0, device.size())) {
2936 // Found a match
2937 if (uevent_data.action == "add") {
2938 (*iter)->Primary_Block_Device = "/dev/block/" + uevent_data.block_device;
2939 (*iter)->Alternate_Block_Device = (*iter)->Primary_Block_Device;
2940 (*iter)->Is_Present = true;
2941 LOGINFO("Found a match '%s' '%s'\n", uevent_data.block_device.c_str(), device.c_str());
2942 if (!Decrypt_Adopted()) {
2943 LOGINFO("No adopted storage so finding actual block device\n");
2944 (*iter)->Find_Actual_Block_Device();
2945 }
2946 return;
2947 } else if (uevent_data.action == "remove") {
2948 (*iter)->Is_Present = false;
2949 (*iter)->Primary_Block_Device = "";
2950 (*iter)->Actual_Block_Device = "";
2951 Remove_Uevent_Devices((*iter)->Mount_Point);
2952 return;
2953 }
2954 }
2955 }
2956 }
bigbiffee7b7ff2020-03-23 15:08:27 -04002957
2958 if (!PartitionManager.Get_Super_Status())
2959 LOGINFO("Found no matching fstab entry for uevent device '%s' - %s\n", uevent_data.sysfs_path.c_str(), uevent_data.action.c_str());
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06002960}
2961
2962void TWPartitionManager::setup_uevent() {
2963 struct sockaddr_nl nls;
2964
2965 if (uevent_pfd.fd >= 0) {
2966 LOGINFO("uevent already set up\n");
2967 return;
2968 }
2969
2970 // Open hotplug event netlink socket
2971 memset(&nls,0,sizeof(struct sockaddr_nl));
2972 nls.nl_family = AF_NETLINK;
2973 nls.nl_pid = getpid();
2974 nls.nl_groups = -1;
2975 uevent_pfd.events = POLLIN;
2976 uevent_pfd.fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
2977 if (uevent_pfd.fd==-1) {
2978 LOGERR("uevent not root\n");
2979 return;
2980 }
2981
2982 // Listen to netlink socket
2983 if (::bind(uevent_pfd.fd, (struct sockaddr *) &nls, sizeof(struct sockaddr_nl)) < 0) {
2984 LOGERR("Bind failed\n");
2985 return;
2986 }
2987 set_select_fd();
2988 Coldboot();
2989}
2990
2991Uevent_Block_Data TWPartitionManager::get_event_block_values(char *buf, int len) {
2992 Uevent_Block_Data ret;
2993 ret.subsystem = "";
2994 char *ptr = buf;
2995 const char *end = buf + len;
2996
2997 buf[len - 1] = '\0';
2998 while (ptr < end) {
2999 if (strncmp(ptr, "ACTION=", strlen("ACTION=")) == 0) {
3000 ptr += strlen("ACTION=");
3001 ret.action = ptr;
3002 } else if (strncmp(ptr, "SUBSYSTEM=", strlen("SUBSYSTEM=")) == 0) {
3003 ptr += strlen("SUBSYSTEM=");
3004 ret.subsystem = ptr;
3005 } else if (strncmp(ptr, "DEVTYPE=", strlen("DEVTYPE=")) == 0) {
3006 ptr += strlen("DEVTYPE=");
3007 ret.type = ptr;
3008 } else if (strncmp(ptr, "DEVPATH=", strlen("DEVPATH=")) == 0) {
3009 ptr += strlen("DEVPATH=");
3010 ret.sysfs_path += ptr;
3011 } else if (strncmp(ptr, "DEVNAME=", strlen("DEVNAME=")) == 0) {
3012 ptr += strlen("DEVNAME=");
3013 ret.block_device += ptr;
3014 } else if (strncmp(ptr, "MAJOR=", strlen("MAJOR=")) == 0) {
3015 ptr += strlen("MAJOR=");
3016 ret.major = atoi(ptr);
3017 } else if (strncmp(ptr, "MINOR=", strlen("MINOR=")) == 0) {
3018 ptr += strlen("MINOR=");
3019 ret.minor = atoi(ptr);
3020 }
3021 ptr += strlen(ptr) + 1;
3022 }
3023 return ret;
3024}
3025
3026void TWPartitionManager::read_uevent() {
3027 char buf[1024];
3028
3029 int len = recv(uevent_pfd.fd, buf, sizeof(buf), MSG_DONTWAIT);
3030 if (len == -1) {
bigbiffd58ba182020-03-23 10:02:29 -04003031 LOGERR("recv error on uevent\n");
Ethan Yonker6e8c27a2016-12-22 17:55:57 -06003032 return;
3033 }
3034 /*int i = 0; // Print all uevent output for test /debug
3035 while (i<len) {
3036 printf("%s\n", buf+i);
3037 i += strlen(buf+i)+1;
3038 }*/
3039 Uevent_Block_Data uevent_data = get_event_block_values(buf, len);
3040 if (uevent_data.subsystem == "block" && uevent_data.type == "disk") {
3041 PartitionManager.Handle_Uevent(uevent_data);
3042 }
3043}
3044
3045void TWPartitionManager::close_uevent() {
3046 if (uevent_pfd.fd > 0)
3047 close(uevent_pfd.fd);
3048 uevent_pfd.fd = -1;
3049}
3050
3051void TWPartitionManager::Add_Partition(TWPartition* Part) {
3052 Partitions.push_back(Part);
3053}
3054
3055void TWPartitionManager::Coldboot_Scan(std::vector<string> *sysfs_entries, const string& Path, int depth) {
3056 string Real_Path = Path;
3057 char real_path[PATH_MAX];
3058 if (realpath(Path.c_str(), &real_path[0])) {
3059 string Real_Path = real_path;
3060 std::vector<string>::iterator iter;
3061 for (iter = sysfs_entries->begin(); iter != sysfs_entries->end(); iter++) {
3062 if (Real_Path.find((*iter)) != string::npos) {
3063 string Write_Path = Real_Path + "/uevent";
3064 if (TWFunc::Path_Exists(Write_Path)) {
3065 const char* write_val = "add\n";
3066 TWFunc::write_to_file(Write_Path, write_val);
3067 break;
3068 }
3069 }
3070 }
3071 }
3072
3073 DIR* d = opendir(Path.c_str());
3074 if (d != NULL) {
3075 struct dirent* de;
3076 while ((de = readdir(d)) != NULL) {
3077 if (de->d_name[0] == '.' || (de->d_type != DT_DIR && depth > 0))
3078 continue;
3079 if (strlen(de->d_name) >= 4 && (strncmp(de->d_name, "ram", 3) == 0 || strncmp(de->d_name, "loop", 4) == 0))
3080 continue;
3081
3082 string item = Path + "/";
3083 item.append(de->d_name);
3084 Coldboot_Scan(sysfs_entries, item, depth + 1);
3085 }
3086 closedir(d);
3087 }
3088}
3089
3090void TWPartitionManager::Coldboot() {
3091 std::vector<TWPartition*>::iterator iter;
3092 std::vector<string> sysfs_entries;
3093
3094 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
3095 if (!(*iter)->Sysfs_Entry.empty()) {
3096 size_t wildcard_pos = (*iter)->Sysfs_Entry.find("*");
3097 if (wildcard_pos == string::npos)
3098 wildcard_pos = (*iter)->Sysfs_Entry.size();
3099 sysfs_entries.push_back((*iter)->Sysfs_Entry.substr(0, wildcard_pos));
3100 }
3101 }
3102
3103 if (sysfs_entries.size() > 0)
3104 Coldboot_Scan(&sysfs_entries, "/sys/block", 0);
3105}
Ethan Yonker53796e72019-01-11 22:49:52 -06003106
3107bool TWPartitionManager::Prepare_Empty_Folder(const std::string& Folder) {
3108 if (TWFunc::Path_Exists(Folder))
3109 TWFunc::removeDir(Folder, false);
3110 return TWFunc::Recursive_Mkdir(Folder);
3111}
3112
bigbiffee7b7ff2020-03-23 15:08:27 -04003113bool TWPartitionManager::Prepare_Super_Volume(TWPartition* twrpPart) {
3114 Fstab fstab;
3115 std::string bare_partition_name;
3116
3117 if (twrpPart->Get_Mount_Point() == "/system_root")
3118 bare_partition_name = "system";
3119 else
3120 bare_partition_name = TWFunc::Remove_Beginning_Slash(twrpPart->Get_Mount_Point());
3121
3122 LOGINFO("Trying to prepare %s from super partition\n", bare_partition_name.c_str());
3123
3124 std::string blk_device_partition;
3125#ifdef AB_OTA_UPDATER
3126 blk_device_partition = bare_partition_name + PartitionManager.Get_Active_Slot_Suffix();
3127#else
3128 blk_device_partition = bare_partition_name;
3129#endif
3130
3131 FstabEntry fstabEntry = {
3132 .blk_device = blk_device_partition,
3133 .mount_point = twrpPart->Get_Mount_Point(),
3134 .fs_type = twrpPart->Current_File_System,
3135 .fs_mgr_flags.logical = twrpPart->Is_Super,
3136 };
3137
3138 fstab.emplace_back(fstabEntry);
3139 if (!fs_mgr_update_logical_partition(&fstabEntry)) {
3140 LOGERR("unable to update logical partition: %s\n", twrpPart->Get_Mount_Point().c_str());
3141 return false;
3142 }
3143
bigbiff32cbabe2020-04-12 19:16:19 -04003144 while (access(fstabEntry.blk_device.c_str(), F_OK) != 0) {
3145 usleep(100);
3146 }
3147
bigbiffee7b7ff2020-03-23 15:08:27 -04003148 twrpPart->Set_Block_Device(fstabEntry.blk_device);
3149 twrpPart->Update_Size(true);
3150 twrpPart->Change_Mount_Read_Only(true);
3151 twrpPart->Set_Can_Be_Backed_Up(false);
3152 twrpPart->Set_Can_Be_Wiped(false);
3153 return true;
3154}
3155
3156bool TWPartitionManager::Prepare_All_Super_Volumes() {
3157 bool status = true;
3158 std::vector<TWPartition*>::iterator iter;
3159
3160 for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
3161 if ((*iter)->Is_Super) {
3162 if (!Prepare_Super_Volume(*iter)) {
3163 status = false;
3164 }
3165 PartitionManager.Output_Partition(*iter);
3166 }
3167 }
3168 Update_System_Details();
3169 return status;
3170}
3171
3172bool TWPartitionManager::Is_Super_Partition(const char* fstab_line) {
3173 std::vector<std::string> super_partition_list = {"system", "vendor", "odm", "product", "system_ext"};
3174
3175 for (auto&& fstab_partition_check: super_partition_list) {
3176 if (strncmp(fstab_line, fstab_partition_check.c_str(), fstab_partition_check.size()) == 0) {
3177 DataManager::SetValue(TW_IS_SUPER, "1");
3178 return true;
3179 }
3180 }
3181 return false;
3182}
3183
3184std::string TWPartitionManager::Get_Super_Partition() {
3185 int slot_number = Get_Active_Slot_Display() == "A" ? 0 : 1;
3186 std::string super_device = fs_mgr_get_super_partition_name(slot_number);
3187 return "/dev/block/by-name/" + super_device;
3188}
3189
3190void TWPartitionManager::Setup_Super_Devices() {
3191 std::string superPart = Get_Super_Partition();
3192 android::fs_mgr::CreateLogicalPartitions(superPart);
bigbiff7ba75002020-04-11 20:47:09 -04003193}
3194
3195void TWPartitionManager::Setup_Super_Partition() {
bigbiffee7b7ff2020-03-23 15:08:27 -04003196 TWPartition* superPartition = new TWPartition();
bigbiff7ba75002020-04-11 20:47:09 -04003197 std::string superPart = Get_Super_Partition();
3198
3199 superPartition->Backup_Path = "/super";
3200 superPartition->Mount_Point = "/super";
bigbiffee7b7ff2020-03-23 15:08:27 -04003201 superPartition->Actual_Block_Device = superPart;
3202 superPartition->Alternate_Block_Device = superPart;
3203 superPartition->Backup_Display_Name = "super";
3204 superPartition->Can_Flash_Img = true;
bigbiffee7b7ff2020-03-23 15:08:27 -04003205 superPartition->Current_File_System = "emmc";
3206 superPartition->Can_Be_Backed_Up = true;
3207 superPartition->Is_Present = true;
3208 superPartition->Is_SubPartition = false;
bigbiff7ba75002020-04-11 20:47:09 -04003209 superPartition->Setup_Image();
bigbiffee7b7ff2020-03-23 15:08:27 -04003210 Add_Partition(superPartition);
3211 PartitionManager.Output_Partition(superPartition);
3212 Update_System_Details();
3213}
3214
3215bool TWPartitionManager::Get_Super_Status() {
3216 return access(Get_Super_Partition().c_str(), F_OK) == 0;
3217}
bigbiff25d25b92020-06-19 16:07:38 -04003218
3219bool TWPartitionManager::Recreate_Logs_Dir() {
3220#ifdef TW_INCLUDE_FBE
3221 struct passwd pd;
3222 struct passwd *pwdptr = &pd;
3223 struct passwd *tempPd;
3224 char pwdBuf[512];
3225 int uid = 0, gid = 0;
3226
3227 if ((getpwnam_r("system", pwdptr, pwdBuf, sizeof(pwdBuf), &tempPd)) != 0) {
3228 LOGERR("unable to get system user id\n");
3229 return false;
3230 } else {
3231 struct group grp;
3232 struct group *grpptr = &grp;
3233 struct group *tempGrp;
3234 char grpBuf[512];
3235
3236 if ((getgrnam_r("cache", grpptr, grpBuf, sizeof(grpBuf), &tempGrp)) != 0) {
3237 LOGERR("unable to get cache group id\n");
3238 return false;
3239 } else {
3240 uid = pd.pw_uid;
3241 gid = grp.gr_gid;
3242 std::string abLogsRecoveryDir(DATA_LOGS_DIR);
3243 abLogsRecoveryDir += "/recovery/";
3244
3245 if (!TWFunc::Create_Dir_Recursive(abLogsRecoveryDir, S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP, uid, gid)) {
3246 LOGERR("Unable to recreate %s\n", abLogsRecoveryDir.c_str());
3247 return false;
3248 }
3249 if (setfilecon(abLogsRecoveryDir.c_str(), "u:object_r:cache_file:s0") != 0) {
3250 LOGERR("Unable to set contexts for %s\n", abLogsRecoveryDir.c_str());
3251 return false;
3252 }
3253 }
3254 }
3255#endif
3256 return true;
3257}