blob: 53e7ba6363710b57146a41429c3b120e0cd485ef [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition class for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040028#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040029#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040030#include <dirent.h>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031#include <iostream>
32#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "variables.h"
39#include "common.h"
40#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040041#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050043#include "twrpTar.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040044extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040045 #include "mtdutils/mtdutils.h"
46 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000047#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
48 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
49#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040050}
Dees_Troy51a0e822012-09-05 15:24:24 -040051
bigbiff bigbiff9c754052013-01-09 09:09:08 -050052using namespace std;
53
Dees_Troy51a0e822012-09-05 15:24:24 -040054TWPartition::TWPartition(void) {
55 Can_Be_Mounted = false;
56 Can_Be_Wiped = false;
57 Wipe_During_Factory_Reset = false;
58 Wipe_Available_in_GUI = false;
59 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040060 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040061 SubPartition_Of = "";
62 Symlink_Path = "";
63 Symlink_Mount_Point = "";
64 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040065 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040066 Actual_Block_Device = "";
67 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040068 Alternate_Block_Device = "";
69 Removable = false;
70 Is_Present = false;
71 Length = 0;
72 Size = 0;
73 Used = 0;
74 Free = 0;
75 Backup_Size = 0;
76 Can_Be_Encrypted = false;
77 Is_Encrypted = false;
78 Is_Decrypted = false;
79 Decrypted_Block_Device = "";
80 Display_Name = "";
81 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040082 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040083 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040084 Backup_Method = NONE;
85 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040086 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040087 Is_Storage = false;
88 Storage_Path = "";
89 Current_File_System = "";
90 Fstab_File_System = "";
91 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +000092 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +000093 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +000094#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
95 EcryptFS_Password = "";
96#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040097}
98
99TWPartition::~TWPartition(void) {
100 // Do nothing
101}
102
Dees_Troy5bf43922012-09-07 16:07:55 -0400103bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
104 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
105 int line_len = Line.size(), index = 0, item_index = 0;
106 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400107 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400108 strncpy(full_line, Line.c_str(), line_len);
109
Dees_Troy51127312012-09-08 13:08:49 -0400110 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400111 if (full_line[index] <= 32)
112 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400113 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400114 Mount_Point = full_line;
115 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400116 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400117 index = Mount_Point.size();
118 while (index < line_len) {
119 while (index < line_len && full_line[index] == '\0')
120 index++;
121 if (index >= line_len)
122 continue;
123 ptr = full_line + index;
124 if (item_index == 0) {
125 // File System
126 Fstab_File_System = ptr;
127 Current_File_System = ptr;
128 item_index++;
129 } else if (item_index == 1) {
130 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400131 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400132 MTD_Name = ptr;
133 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400134 } else if (Fstab_File_System == "bml") {
135 if (Mount_Point == "/boot")
136 MTD_Name = "boot";
137 else if (Mount_Point == "/recovery")
138 MTD_Name = "recovery";
139 Primary_Block_Device = ptr;
140 if (*ptr != '/')
141 LOGE("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400142 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400143 if (Display_Error)
144 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
145 else
146 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
147 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400148 } else {
149 Primary_Block_Device = ptr;
150 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400151 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400152 item_index++;
153 } else if (item_index > 1) {
154 if (*ptr == '/') {
155 // Alternate Block Device
156 Alternate_Block_Device = ptr;
157 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
158 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
159 // Partition length
160 ptr += 7;
161 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400162 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
163 // Custom flags, save for later so that new values aren't overwritten by defaults
164 ptr += 6;
165 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000166 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400167 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
168 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400169 } else {
170 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400171 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400172 }
173 }
174 while (index < line_len && full_line[index] != '\0')
175 index++;
176 }
177
178 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
179 if (Display_Error)
180 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
181 else
182 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
183 return 0;
184 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400185 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400186 Setup_File_System(Display_Error);
187 if (Mount_Point == "/system") {
188 Display_Name = "System";
189 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400190 } else if (Mount_Point == "/data") {
191 Display_Name = "Data";
192 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400193 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400194#ifdef RECOVERY_SDCARD_ON_DATA
195 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400196 Is_Storage = true;
197 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000198 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400199 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
200 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400201 Symlink_Mount_Point = "/emmc";
202 } else {
203 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400204 Symlink_Mount_Point = "/sdcard";
205 }
Dees_Troy16b74352012-11-14 22:27:31 +0000206 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
207 Storage_Path = "/data/media/0";
208 Symlink_Path = Storage_Path;
209 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
210 UnMount(true);
211 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400212#endif
213#ifdef TW_INCLUDE_CRYPTO
214 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400215 char crypto_blkdev[255];
216 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
217 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400218 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400219 DataManager::SetValue(TW_IS_DECRYPTED, 1);
220 Is_Encrypted = true;
221 Is_Decrypted = true;
222 Decrypted_Block_Device = crypto_blkdev;
223 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
224 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 Is_Encrypted = true;
226 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800227 Can_Be_Mounted = false;
228 Current_File_System = "emmc";
229 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400230 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
231 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
232 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800233 } else {
234 // Filesystem is not encrypted and the mount
235 // succeeded, so get it back to the original
236 // unmounted state
237 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400238 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400239 #ifdef RECOVERY_SDCARD_ON_DATA
240 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
241 Recreate_Media_Folder();
242 #endif
243#else
244 #ifdef RECOVERY_SDCARD_ON_DATA
245 Recreate_Media_Folder();
246 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400247#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400248 } else if (Mount_Point == "/cache") {
249 Display_Name = "Cache";
250 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400251 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400252 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troyb46a6842012-09-25 11:06:46 -0400253 LOGI("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500254 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
255 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400256 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400257 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400258 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 Display_Name = "DataData";
260 Is_SubPartition = true;
261 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 DataManager::SetValue(TW_HAS_DATADATA, 1);
263 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400264 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400265 Display_Name = "SD-Ext";
266 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400267 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400268 } else if (Mount_Point == "/boot") {
269 Display_Name = "Boot";
270 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400271 }
272#ifdef TW_EXTERNAL_STORAGE_PATH
273 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
274 Is_Storage = true;
275 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400276 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400277#else
278 if (Mount_Point == "/sdcard") {
279 Is_Storage = true;
280 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400281 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400282#ifndef RECOVERY_SDCARD_ON_DATA
283 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400284 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400285#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400286#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000287 // blkid cannot detect exfat so we force exfat at the start if exfat support is present
288 if (TWFunc::Path_Exists("/sbin/exfat-fuse") && (Fstab_File_System == "vfat" || Fstab_File_System == "auto")) {
289 Fstab_File_System = "exfat";
290 Current_File_System = Fstab_File_System;
291 }
292 }
Dees_Troy8170a922012-09-18 15:40:25 -0400293#ifdef TW_INTERNAL_STORAGE_PATH
294 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
295 Is_Storage = true;
296 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400297#ifndef RECOVERY_SDCARD_ON_DATA
298 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400299 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400300#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400301 }
302#else
303 if (Mount_Point == "/emmc") {
304 Is_Storage = true;
305 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400306#ifndef RECOVERY_SDCARD_ON_DATA
307 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400308 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400309#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400310 }
311#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400313 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400314 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400315 }
316
Dees_Troy51127312012-09-08 13:08:49 -0400317 // Process any custom flags
318 if (Flags.size() > 0)
319 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400320 return true;
321}
322
323bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
324 char flags[MAX_FSTAB_LINE_LENGTH];
325 int flags_len, index = 0;
326 char* ptr;
327
328 strcpy(flags, Flags.c_str());
329 flags_len = Flags.size();
330 for (index = 0; index < flags_len; index++) {
331 if (flags[index] == ';')
332 flags[index] = '\0';
333 }
334
335 index = 0;
336 while (index < flags_len) {
337 while (index < flags_len && flags[index] == '\0')
338 index++;
339 if (index >= flags_len)
340 continue;
341 ptr = flags + index;
342 if (strcmp(ptr, "removable") == 0) {
343 Removable = true;
344 } else if (strcmp(ptr, "storage") == 0) {
345 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400346 } else if (strcmp(ptr, "canbewiped") == 0) {
347 Can_Be_Wiped = true;
348 } else if (strcmp(ptr, "wipeingui") == 0) {
349 Can_Be_Wiped = true;
350 Wipe_Available_in_GUI = true;
351 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
352 Can_Be_Wiped = true;
353 Wipe_Available_in_GUI = true;
354 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400355 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000356 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400357 Is_SubPartition = true;
358 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000359 } else if (strcmp(ptr, "ignoreblkid") == 0) {
360 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000361 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
362 Retain_Layout_Version = true;
Dees_Troy51127312012-09-08 13:08:49 -0400363 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
364 ptr += 8;
365 Symlink_Path = ptr;
366 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
367 ptr += 8;
368 Display_Name = ptr;
369 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
370 ptr += 10;
371 Format_Block_Size = atoi(ptr);
372 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
373 ptr += 7;
374 Length = atoi(ptr);
375 } else {
376 if (Display_Error)
377 LOGE("Unhandled flag: '%s'\n", ptr);
378 else
379 LOGI("Unhandled flag: '%s'\n", ptr);
380 }
381 while (index < flags_len && flags[index] != '\0')
382 index++;
383 }
384 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400385}
386
Dees_Troy5bf43922012-09-07 16:07:55 -0400387bool TWPartition::Is_File_System(string File_System) {
388 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400389 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400390 File_System == "ext4" ||
391 File_System == "vfat" ||
392 File_System == "ntfs" ||
393 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500394 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400395 File_System == "auto")
396 return true;
397 else
398 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400399}
400
Dees_Troy5bf43922012-09-07 16:07:55 -0400401bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400402 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400403 return true;
404 else
405 return false;
406}
407
Dees_Troy51127312012-09-08 13:08:49 -0400408bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400409 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400410 if (mkdir(Path.c_str(), 0777) == -1) {
411 if (Display_Error)
412 LOGE("Can not create '%s' folder.\n", Path.c_str());
413 else
414 LOGI("Can not create '%s' folder.\n", Path.c_str());
415 return false;
416 } else {
417 LOGI("Created '%s' folder.\n", Path.c_str());
418 return true;
419 }
420 }
421 return true;
422}
423
Dees_Troy5bf43922012-09-07 16:07:55 -0400424void TWPartition::Setup_File_System(bool Display_Error) {
425 struct statfs st;
426
427 Can_Be_Mounted = true;
428 Can_Be_Wiped = true;
429
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400431 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400432 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
433 Backup_Name = Display_Name;
434 Backup_Method = FILES;
435}
436
437void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400438 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
439 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800440 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400441 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800442 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 Backup_Method = FLASH_UTILS;
444 else
Gary Peck82599a82012-11-21 16:23:12 -0800445 LOGI("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400446 if (Find_Partition_Size()) {
447 Used = Size;
448 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400449 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400450 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400451 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400452 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400453 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400454 }
455}
456
Dees_Troye58d5262012-09-21 12:27:57 -0400457void TWPartition::Setup_AndSec(void) {
458 Backup_Name = "and-sec";
459 Has_Android_Secure = true;
460 Symlink_Path = Mount_Point + "/.android_secure";
461 Symlink_Mount_Point = "/and-sec";
462 Backup_Path = Symlink_Mount_Point;
463 Make_Dir("/and-sec", true);
464 Recreate_AndSec_Folder();
465}
466
Dees_Troy5bf43922012-09-07 16:07:55 -0400467void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
468 char device[512], realDevice[512];
469
470 strcpy(device, Block.c_str());
471 memset(realDevice, 0, sizeof(realDevice));
472 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
473 {
474 strcpy(device, realDevice);
475 memset(realDevice, 0, sizeof(realDevice));
476 }
477
478 if (device[0] != '/') {
479 if (Display_Error)
480 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
481 else
482 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
483 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400484 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400485 Block = device;
486 return;
487 }
488}
489
Dees_Troy8e337f32012-10-13 22:07:49 -0400490void TWPartition::Mount_Storage_Retry(void) {
491 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500492 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400493 int retry_count = 5;
494 while (retry_count > 0 && !Mount(false)) {
495 usleep(500000);
496 retry_count--;
497 }
498 Mount(true);
499 }
500}
501
Dees_Troy38bd7602012-09-14 13:33:53 -0400502bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
503 FILE *fp = NULL;
504 char line[255];
505
506 fp = fopen("/proc/mtd", "rt");
507 if (fp == NULL) {
508 LOGE("Device does not support /proc/mtd\n");
509 return false;
510 }
511
512 while (fgets(line, sizeof(line), fp) != NULL)
513 {
514 char device[32], label[32];
515 unsigned long size = 0;
516 char* fstype = NULL;
517 int deviceId;
518
519 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
520
521 // Skip header and blank lines
522 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
523 continue;
524
525 // Strip off the trailing " from the label
526 label[strlen(label)-1] = '\0';
527
528 if (strcmp(label, MTD_Name.c_str()) == 0) {
529 // We found our device
530 // Strip off the trailing : from the device
531 device[strlen(device)-1] = '\0';
532 if (sscanf(device,"mtd%d", &deviceId) == 1) {
533 sprintf(device, "/dev/block/mtdblock%d", deviceId);
534 Primary_Block_Device = device;
535 }
536 }
537 }
538 fclose(fp);
539
540 return false;
541}
542
Dees_Troy51127312012-09-08 13:08:49 -0400543bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
544 struct statfs st;
545 string Local_Path = Mount_Point + "/.";
546
547 if (!Mount(Display_Error))
548 return false;
549
550 if (statfs(Local_Path.c_str(), &st) != 0) {
551 if (!Removable) {
552 if (Display_Error)
553 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
554 else
555 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
556 }
557 return false;
558 }
559 Size = (st.f_blocks * st.f_bsize);
560 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
561 Free = (st.f_bfree * st.f_bsize);
562 Backup_Size = Used;
563 return true;
564}
565
566bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400567 FILE* fp;
568 char command[255], line[512];
569 int include_block = 1;
570 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500571 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400572
573 if (!Mount(Display_Error))
574 return false;
575
Dees_Troy38bd7602012-09-14 13:33:53 -0400576 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400577 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500578 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400579 fp = fopen("/tmp/dfoutput.txt", "rt");
580 if (fp == NULL) {
581 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400582 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400583 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400584
585 while (fgets(line, sizeof(line), fp) != NULL)
586 {
587 unsigned long blocks, used, available;
588 char device[64];
589 char tmpString[64];
590
591 if (strncmp(line, "Filesystem", 10) == 0)
592 continue;
593 if (strlen(line) < min_len) {
594 include_block = 0;
595 continue;
596 }
597 if (include_block) {
598 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
599 } else {
600 // The device block string is so long that the df information is on the next line
601 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400602 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400603 while (tmpString[space_count] == 32)
604 space_count++;
605 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
606 }
607
608 // Adjust block size to byte size
609 Size = blocks * 1024ULL;
610 Used = used * 1024ULL;
611 Free = available * 1024ULL;
612 Backup_Size = Used;
613 }
614 fclose(fp);
615 return true;
616}
617
Dees_Troy5bf43922012-09-07 16:07:55 -0400618bool TWPartition::Find_Partition_Size(void) {
619 FILE* fp;
620 char line[512];
621 string tmpdevice;
622
623 // In this case, we'll first get the partitions we care about (with labels)
624 fp = fopen("/proc/partitions", "rt");
625 if (fp == NULL)
626 return false;
627
628 while (fgets(line, sizeof(line), fp) != NULL)
629 {
630 unsigned long major, minor, blocks;
631 char device[512];
632 char tmpString[64];
633
Dees_Troy63c8df72012-09-10 14:02:05 -0400634 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400635 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
636
637 tmpdevice = "/dev/block/";
638 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400639 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400640 // Adjust block size to byte size
641 Size = blocks * 1024ULL;
642 fclose(fp);
643 return true;
644 }
645 }
646 fclose(fp);
647 return false;
648}
649
Dees_Troy5bf43922012-09-07 16:07:55 -0400650bool TWPartition::Is_Mounted(void) {
651 if (!Can_Be_Mounted)
652 return false;
653
654 struct stat st1, st2;
655 string test_path;
656
657 // Check to see if the mount point directory exists
658 test_path = Mount_Point + "/.";
659 if (stat(test_path.c_str(), &st1) != 0) return false;
660
661 // Check to see if the directory above the mount point exists
662 test_path = Mount_Point + "/../.";
663 if (stat(test_path.c_str(), &st2) != 0) return false;
664
665 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
666 int ret = (st1.st_dev != st2.st_dev) ? true : false;
667
668 return ret;
669}
670
671bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000672 int exfat_mounted = 0;
673
Dees_Troy5bf43922012-09-07 16:07:55 -0400674 if (Is_Mounted()) {
675 return true;
676 } else if (!Can_Be_Mounted) {
677 return false;
678 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400679
680 Find_Actual_Block_Device();
681
682 // Check the current file system before mounting
683 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000684 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000685 string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000686 LOGI("cmd: %s\n", cmd.c_str());
687 string result;
688 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
689 LOGI("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
690 Current_File_System = "vfat";
691 } else {
692#ifdef TW_NO_EXFAT_FUSE
693 UnMount(false);
694 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
695 // Some kernels let us mount vfat as exfat which doesn't work out too well
696#else
697 exfat_mounted = 1;
698#endif
699 }
700 }
Dees_Troy22042032012-12-18 21:23:08 +0000701 if (Fstab_File_System == "yaffs2") {
702 // mount an MTD partition as a YAFFS2 filesystem.
703 mtd_scan_partitions();
704 const MtdPartition* partition;
705 partition = mtd_find_partition_by_name(MTD_Name.c_str());
706 if (partition == NULL) {
707 LOGE("Failed to find '%s' partition to mount at '%s'\n",
708 MTD_Name.c_str(), Mount_Point.c_str());
709 return false;
710 }
711 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
712 if (Display_Error)
713 LOGE("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
714 else
715 LOGI("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
716 return false;
717 } else
718 return true;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000719 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
720#ifdef TW_NO_EXFAT_FUSE
721 if (Current_File_System == "exfat") {
722 LOGI("Mounting exfat failed, trying vfat...\n");
723 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000724 if (Display_Error)
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000725 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000726 else
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000727 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
728 LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
729 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000730 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000731 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000732#endif
733 if (Display_Error)
734 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
735 else
736 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
737 LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
738 return false;
739#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000740 }
741#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000742 }
743#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
744 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
745 MetaEcfsFile += "/.MetaEcfsFile";
746 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
747 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
748 if (Display_Error)
749 LOGE("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
750 else
751 LOGI("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
752 } else {
753 LOGI("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
754 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400755 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000756 } else {
757 Is_Decrypted = false;
758 }
759#endif
760 if (Removable)
761 Update_Size(Display_Error);
762
763 if (!Symlink_Mount_Point.empty()) {
764 string Command, Result;
765 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
766 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400767 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400768 return true;
769}
770
771bool TWPartition::UnMount(bool Display_Error) {
772 if (Is_Mounted()) {
773 int never_unmount_system;
774
775 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
776 if (never_unmount_system == 1 && Mount_Point == "/system")
777 return true; // Never unmount system if you're not supposed to unmount it
778
Dees_Troyc8bafa12013-01-10 15:43:00 +0000779#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
780 if (EcryptFS_Password.size() > 0) {
781 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
782 if (Display_Error)
783 LOGE("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
784 else
785 LOGI("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
786 } else {
787 LOGI("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
788 }
789 }
790#endif
791
Dees_Troy38bd7602012-09-14 13:33:53 -0400792 if (!Symlink_Mount_Point.empty())
793 umount(Symlink_Mount_Point.c_str());
794
Dees_Troyb05ddee2013-01-28 20:24:50 +0000795 umount(Mount_Point.c_str());
796 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400797 if (Display_Error)
798 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
799 else
800 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
801 return false;
802 } else
803 return true;
804 } else {
805 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400806 }
807}
808
Gary Peck43acadf2012-11-21 21:19:01 -0800809bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000810 bool wiped = false, update_crypt = false;
811 int check;
812 string Layout_Filename = Mount_Point + "/.layout_version";
813
Dees_Troy38bd7602012-09-14 13:33:53 -0400814 if (!Can_Be_Wiped) {
815 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
816 return false;
817 }
818
Dees_Troyc51f1f92012-09-20 15:32:13 -0400819 if (Mount_Point == "/cache")
820 tmplog_offset = 0;
821
Dees_Troyce675462013-01-09 19:48:21 +0000822#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
823 if (Mount_Point == "/data" && Mount(false)) {
824 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
825 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
826 }
827#endif
828
Dees_Troy16c2b312013-01-15 16:51:18 +0000829 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
830 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
831 else
832 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400833
Dees_Troy16c2b312013-01-15 16:51:18 +0000834 if (Has_Data_Media) {
835 wiped = Wipe_Data_Without_Wiping_Media();
836 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800837
Dees_Troy16c2b312013-01-15 16:51:18 +0000838 DataManager::GetValue(TW_RM_RF_VAR, check);
839
840 if (check)
841 wiped = Wipe_RMRF();
842 else if (New_File_System == "ext4")
843 wiped = Wipe_EXT4();
844 else if (New_File_System == "ext2" || New_File_System == "ext3")
845 wiped = Wipe_EXT23(New_File_System);
846 else if (New_File_System == "vfat")
847 wiped = Wipe_FAT();
848 else if (New_File_System == "exfat")
849 wiped = Wipe_EXFAT();
850 else if (New_File_System == "yaffs2")
851 wiped = Wipe_MTD();
852 else {
853 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
854 unlink("/.layout_version");
855 return false;
856 }
857 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800858 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400859
Gary Pecke8bc5d72012-12-21 06:45:25 -0800860 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000861#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
862 if (Mount_Point == "/data" && Mount(false)) {
863 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
864 Make_Dir("/data/system", true);
865 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
866 }
867 }
868#endif
Dees_Troy16c2b312013-01-15 16:51:18 +0000869
Dees_Troy1c1ac442013-01-17 21:42:14 +0000870 if (Mount_Point == "/cache")
871 DataManager::Output_Version();
872
Dees_Troy16c2b312013-01-15 16:51:18 +0000873 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
874 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
875
876 if (update_crypt) {
877 Setup_File_System(false);
878 if (Is_Encrypted && !Is_Decrypted) {
879 // just wiped an encrypted partition back to its unencrypted state
880 Is_Encrypted = false;
881 Is_Decrypted = false;
882 Decrypted_Block_Device = "";
883 if (Mount_Point == "/data") {
884 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
885 DataManager::SetValue(TW_IS_DECRYPTED, 0);
886 }
Gary Pecke8bc5d72012-12-21 06:45:25 -0800887 }
888 }
889 }
890 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400891}
892
Gary Peck43acadf2012-11-21 21:19:01 -0800893bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800894 if (Is_File_System(Current_File_System))
895 return Wipe(Current_File_System);
896 else
897 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -0800898}
899
Dees_Troye58d5262012-09-21 12:27:57 -0400900bool TWPartition::Wipe_AndSec(void) {
901 if (!Has_Android_Secure)
902 return false;
903
Dees_Troye58d5262012-09-21 12:27:57 -0400904 if (!Mount(true))
905 return false;
906
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500907 ui_print("Wiping .android_secure\n");
908 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -0400909 return true;
910}
911
Dees_Troy51a0e822012-09-05 15:24:24 -0400912bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400913 if (Backup_Method == FILES)
914 return Backup_Tar(backup_folder);
915 else if (Backup_Method == DD)
916 return Backup_DD(backup_folder);
917 else if (Backup_Method == FLASH_UTILS)
918 return Backup_Dump_Image(backup_folder);
919 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
920 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400921}
922
Dees_Troy43d8b002012-09-17 16:00:01 -0400923bool TWPartition::Check_MD5(string restore_folder) {
924 string Full_Filename;
925 char split_filename[512];
926 int index = 0;
927
928 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400929 if (!TWFunc::Path_Exists(Full_Filename)) {
930 // This is a split archive, we presume
931 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
932 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
933 if (TWFunc::Check_MD5(split_filename) == 0) {
934 LOGE("MD5 failed to match on '%s'.\n", split_filename);
935 return false;
936 }
937 index++;
938 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400939 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400940 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400941 } else {
942 // Single file archive
943 if (TWFunc::Check_MD5(Full_Filename) == 0) {
944 LOGE("MD5 failed to match on '%s'.\n", split_filename);
945 return false;
946 } else
947 return true;
948 }
949 return false;
950}
951
Dees_Troy51a0e822012-09-05 15:24:24 -0400952bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -0800953 size_t first_period, second_period;
954 string Restore_File_System;
955
956 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
957 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
958
959 // Parse backup filename to extract the file system before wiping
960 first_period = Backup_FileName.find(".");
961 if (first_period == string::npos) {
962 LOGE("Unable to find file system (first period).\n");
963 return false;
964 }
965 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
966 second_period = Restore_File_System.find(".");
967 if (second_period == string::npos) {
968 LOGE("Unable to find file system (second period).\n");
969 return false;
970 }
971 Restore_File_System.resize(second_period);
972 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
973
974 if (Is_File_System(Restore_File_System))
975 return Restore_Tar(restore_folder, Restore_File_System);
976 else if (Is_Image(Restore_File_System)) {
977 if (Restore_File_System == "emmc")
978 return Restore_DD(restore_folder);
979 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
980 return Restore_Flash_Image(restore_folder);
981 }
982
Dees_Troy38bd7602012-09-14 13:33:53 -0400983 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
984 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400985}
986
987string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400988 if (Backup_Method == NONE)
989 return "none";
990 else if (Backup_Method == FILES)
991 return "files";
992 else if (Backup_Method == DD)
993 return "dd";
994 else if (Backup_Method == FLASH_UTILS)
995 return "flash_utils";
996 else
997 return "undefined";
998 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400999}
1000
1001bool TWPartition::Decrypt(string Password) {
1002 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001003 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001004 return 1;
1005}
1006
1007bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001008 bool Save_Data_Media = Has_Data_Media;
1009
1010 if (!UnMount(true))
1011 return false;
1012
Dees_Troy38bd7602012-09-14 13:33:53 -04001013 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001014 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001015 Has_Data_Media = Save_Data_Media;
1016 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1017 Recreate_Media_Folder();
1018 }
Dees_Troyb46a6842012-09-25 11:06:46 -04001019 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001020 return true;
1021 } else {
1022 Has_Data_Media = Save_Data_Media;
1023 LOGE("Unable to format to remove encryption.\n");
1024 return false;
1025 }
1026 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001027}
1028
1029void TWPartition::Check_FS_Type() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001030 string blkCommand, result;
1031 string blkOutput;
Dees_Troy5bf43922012-09-07 16:07:55 -04001032 char* blk;
1033 char* arg;
1034 char* ptr;
Dees_Troy4bb33ac2013-01-18 21:05:47 +00001035 int type_found = 0;
Dees_Troy5bf43922012-09-07 16:07:55 -04001036
Dees_Troy68cab492012-12-12 19:29:35 +00001037 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1038 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001039
Dees_Troy38bd7602012-09-14 13:33:53 -04001040 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001041 if (!Is_Present)
1042 return;
Dees_Troy51127312012-09-08 13:08:49 -04001043
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001044 blkCommand = "blkid " + Actual_Block_Device;
1045 TWFunc::Exec_Cmd(blkCommand, result);
1046 std::stringstream line(result);
1047 while (getline(line, blkOutput))
Dees_Troy5bf43922012-09-07 16:07:55 -04001048 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001049 blk = (char*) blkOutput.c_str();
1050 ptr = (char*) blkOutput.c_str();
Dees_Troy63c8df72012-09-10 14:02:05 -04001051 while (*ptr > 32 && *ptr != ':') ptr++;
1052 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001053 *ptr = 0;
1054
1055 // Increment by two, but verify that we don't hit a NULL
1056 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -04001057 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -04001058
1059 // Now, find the TYPE field
1060 while (1)
1061 {
1062 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -04001063 while (*ptr > 32) ptr++;
Dees_Troy4bb33ac2013-01-18 21:05:47 +00001064 if (*ptr != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001065 *ptr = 0;
1066 ptr++;
1067 }
1068
Dees_Troy4bb33ac2013-01-18 21:05:47 +00001069 if (strlen(arg) > 6) {
1070 if (memcmp(arg, "TYPE=\"", 6) == 0) {
1071 type_found = 1;
1072 break;
1073 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001074 }
1075
Dees_Troy4bb33ac2013-01-18 21:05:47 +00001076 if (*ptr == 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001077 arg = NULL;
1078 break;
1079 }
1080 }
1081
Dees_Troy4bb33ac2013-01-18 21:05:47 +00001082 if (type_found) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001083 arg += 6; // Skip the TYPE=" portion
1084 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
Dees_Troy4bb33ac2013-01-18 21:05:47 +00001085 if (Current_File_System != arg) {
1086 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
1087 Current_File_System = arg;
1088 }
1089 } else
Dees_Troy5bf43922012-09-07 16:07:55 -04001090 continue;
Dees_Troy5bf43922012-09-07 16:07:55 -04001091 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001092 return;
1093}
1094
Gary Peck43acadf2012-11-21 21:19:01 -08001095bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001096 if (!UnMount(true))
1097 return false;
1098
Dees_Troy43d8b002012-09-17 16:00:01 -04001099 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001100 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001101
1102 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
1103 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001104 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
1105 LOGI("mke2fs command: %s\n", command.c_str());
1106 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001107 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001108 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001109 ui_print("Done.\n");
1110 return true;
1111 } else {
1112 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1113 return false;
1114 }
1115 } else
1116 return Wipe_RMRF();
1117
1118 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001119}
1120
1121bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001122 if (!UnMount(true))
1123 return false;
1124
Dees_Troy43d8b002012-09-17 16:00:01 -04001125 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001126 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001127
1128 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
1129 Find_Actual_Block_Device();
1130 Command = "make_ext4fs";
1131 if (!Is_Decrypted && Length != 0) {
1132 // Only use length if we're not decrypted
1133 char len[32];
1134 sprintf(len, "%i", Length);
1135 Command += " -l ";
1136 Command += len;
1137 }
1138 Command += " " + Actual_Block_Device;
1139 LOGI("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001140 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001141 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001142 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001143 ui_print("Done.\n");
1144 return true;
1145 } else {
1146 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1147 return false;
1148 }
1149 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001150 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001151
1152 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001153}
1154
1155bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001156 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001157
Dees_Troy43d8b002012-09-17 16:00:01 -04001158 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001159 if (!UnMount(true))
1160 return false;
1161
1162 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1163 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001164 command = "mkdosfs " + Actual_Block_Device;
1165 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001166 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001167 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001168 ui_print("Done.\n");
1169 return true;
1170 } else {
1171 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1172 return false;
1173 }
1174 return true;
1175 }
1176 else
1177 return Wipe_RMRF();
1178
1179 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001180}
1181
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001182bool TWPartition::Wipe_EXFAT() {
1183 string command, result;
1184
1185 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1186 if (!UnMount(true))
1187 return false;
1188
1189 ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
1190 Find_Actual_Block_Device();
1191 command = "mkexfatfs " + Actual_Block_Device;
1192 if (TWFunc::Exec_Cmd(command, result) == 0) {
1193 Recreate_AndSec_Folder();
1194 ui_print("Done.\n");
1195 return true;
1196 } else {
1197 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1198 return false;
1199 }
1200 return true;
1201 }
1202 return false;
1203}
1204
Dees_Troy38bd7602012-09-14 13:33:53 -04001205bool TWPartition::Wipe_MTD() {
1206 if (!UnMount(true))
1207 return false;
1208
1209 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1210
1211 mtd_scan_partitions();
1212 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1213 if (mtd == NULL) {
1214 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1215 return false;
1216 }
1217
1218 MtdWriteContext* ctx = mtd_write_partition(mtd);
1219 if (ctx == NULL) {
1220 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1221 return false;
1222 }
1223 if (mtd_erase_blocks(ctx, -1) == -1) {
1224 mtd_write_close(ctx);
1225 LOGE("Failed to format '%s'", MTD_Name.c_str());
1226 return false;
1227 }
1228 if (mtd_write_close(ctx) != 0) {
1229 LOGE("Failed to close '%s'", MTD_Name.c_str());
1230 return false;
1231 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001232 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001233 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001234 ui_print("Done.\n");
1235 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001236}
1237
1238bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001239 if (!Mount(true))
1240 return false;
1241
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001242 ui_print("Removing all files under '%s'\n", Mount_Point.c_str());
1243 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001244 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001245 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001246}
1247
1248bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001249 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001250
1251 // This handles wiping data on devices with "sdcard" in /data/media
1252 if (!Mount(true))
1253 return false;
1254
1255 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001256
1257 DIR* d;
1258 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001259 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001260 struct dirent* de;
1261 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001262 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1263 // The media folder is the "internal sdcard"
1264 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1265 // the media folder for multi-user.
1266 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001267
1268 dir = "/data/";
1269 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001270 if (de->d_type == DT_DIR) {
1271 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001272 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +00001273 if (!unlink(dir.c_str()))
1274 LOGI("Unable to unlink '%s'\n", dir.c_str());
1275 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001276 }
1277 closedir(d);
Dees_Troy16b74352012-11-14 22:27:31 +00001278 ui_print("Done.\n");
1279 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001280 }
Dees_Troy16b74352012-11-14 22:27:31 +00001281 ui_print("Dirent failed to open /data, error!\n");
1282 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001283}
1284
1285bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001286 char back_name[255], split_index[5];
1287 string Full_FileName, Split_FileName, Tar_Args, Command;
1288 int use_compression, index, backup_count;
1289 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001290 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001291 twrpTar tar;
1292 vector <string> files;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001293
Dees_Troy43d8b002012-09-17 16:00:01 -04001294 if (!Mount(true))
1295 return false;
1296
Dees_Troy2c50e182012-09-26 20:05:28 -04001297 if (Backup_Path == "/and-sec") {
1298 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1299 ui_print("Backing up %s...\n", "Android Secure");
1300 } else {
1301 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1302 ui_print("Backing up %s...\n", Display_Name.c_str());
1303 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001304
1305 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001306
1307 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1308 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001309 Full_FileName = backup_folder + "/" + Backup_FileName;
1310 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001311 // This backup needs to be split into multiple archives
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001312 ui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001313 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001314 tar.setdir(back_name);
1315 tar.setfn(Full_FileName);
1316 backup_count = tar.splitArchiveThread();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001317 if (backup_count == -1) {
1318 LOGE("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001319 return false;
1320 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001321 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001322 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001323 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001324 if (use_compression) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001325 tar.setdir(Backup_Path);
1326 tar.setfn(Full_FileName);
1327 if (tar.createTarGZThread() != 0)
1328 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001329 string gzname = Full_FileName + ".gz";
1330 rename(gzname.c_str(), Full_FileName.c_str());
1331 }
1332 else {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001333 tar.setdir(Backup_Path);
1334 tar.setfn(Full_FileName);
1335 if (tar.createTarThread() != 0)
1336 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001337 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001338 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1339 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1340 return false;
1341 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001342 }
1343 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001344}
1345
1346bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001347 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001348 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001349 int use_compression;
1350
Dees_Troyb46a6842012-09-25 11:06:46 -04001351 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001352 ui_print("Backing up %s...\n", Display_Name.c_str());
1353
1354 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1355 Backup_FileName = back_name;
1356
1357 Full_FileName = backup_folder + "/" + Backup_FileName;
1358
1359 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1360 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001361 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001362 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1363 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001364 return false;
1365 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001366 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001367}
1368
1369bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001370 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001371 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001372 int use_compression;
1373
Dees_Troyb46a6842012-09-25 11:06:46 -04001374 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001375 ui_print("Backing up %s...\n", Display_Name.c_str());
1376
1377 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1378 Backup_FileName = back_name;
1379
1380 Full_FileName = backup_folder + "/" + Backup_FileName;
1381
1382 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1383 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001384 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001385 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1386 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1387 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1388 return false;
1389 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001390 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001391}
1392
Gary Peck43acadf2012-11-21 21:19:01 -08001393bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1394 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001395 int index = 0;
1396 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001397
Dees_Troye58d5262012-09-21 12:27:57 -04001398 if (Has_Android_Secure) {
1399 ui_print("Wiping android secure...\n");
1400 if (!Wipe_AndSec())
1401 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001402 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001403 ui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001404 if (!Wipe(Restore_File_System))
1405 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001406 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001407
1408 if (!Mount(true))
1409 return false;
1410
Dees_Troyda8b55a2012-12-12 19:18:30 +00001411 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001412 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001413 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001414 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001415 if (!TWFunc::Path_Exists(Full_FileName)) {
1416 // Backup is multiple archives
1417 LOGI("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001418 sprintf(split_index, "%03i", index);
1419 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001420 while (TWFunc::Path_Exists(Full_FileName)) {
1421 index++;
1422 ui_print("Restoring archive %i...\n", index);
1423 LOGI("Restoring '%s'...\n", Full_FileName.c_str());
1424 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001425 tar.setdir("/");
1426 tar.setfn(Full_FileName);
1427 if (tar.extractTarThread() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001428 return false;
1429 sprintf(split_index, "%03i", index);
1430 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1431 }
1432 if (index == 0) {
1433 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1434 return false;
1435 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001436 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001437 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001438 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001439 tar.setdir(Backup_Path);
1440 tar.setfn(Full_FileName);
1441 if (tar.extractTarThread() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001442 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001443 }
1444 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001445}
1446
1447bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001448 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001449
Dees_Troyda8b55a2012-12-12 19:18:30 +00001450 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001451 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001452
1453 if (!Find_Partition_Size()) {
1454 LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
1455 return false;
1456 }
1457 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1458 if (backup_size > Size) {
1459 LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
1460 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1461 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1462 return false;
1463 }
1464
1465 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001466 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1467 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001468 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001469 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001470}
1471
1472bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001473 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001474
1475 ui_print("Restoring %s...\n", Display_Name.c_str());
1476 Full_FileName = restore_folder + "/" + Backup_FileName;
1477 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1478 Command = "erase_image " + MTD_Name;
1479 LOGI("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001480 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001481 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1482 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001483 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001484 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001485}
Dees_Troy5bf43922012-09-07 16:07:55 -04001486
1487bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001488 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001489
Dees_Troyab10ee22012-09-21 14:27:30 -04001490 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001491 return false;
1492
Dees_Troy0550cfb2012-10-13 11:56:13 -04001493 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001494 if (Removable || Is_Encrypted) {
1495 if (!Mount(false))
1496 return true;
1497 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001498 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001499
1500 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001501 if (!ret || Size == 0) {
1502 if (!Get_Size_Via_df(Display_Error)) {
1503 if (!Was_Already_Mounted)
1504 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001505 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001506 }
1507 }
Dees_Troy51127312012-09-08 13:08:49 -04001508
Dees_Troy5bf43922012-09-07 16:07:55 -04001509 if (Has_Data_Media) {
1510 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001511 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001512 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1513 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001514 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001515 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001516 int bak = (int)(Backup_Size / 1048576LLU);
1517 int total = (int)(Size / 1048576LLU);
1518 int us = (int)(Used / 1048576LLU);
1519 int fre = (int)(Free / 1048576LLU);
1520 int datmed = (int)(data_media_used / 1048576LLU);
1521 LOGI("Data backup size is %iMB, size: %iMB, used: %iMB, free: %iMB, in data/media: %iMB.\n", bak, total, us, fre, datmed);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001522 } else {
1523 if (!Was_Already_Mounted)
1524 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001525 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001526 }
Dees_Troye58d5262012-09-21 12:27:57 -04001527 } else if (Has_Android_Secure) {
1528 if (Mount(Display_Error))
1529 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001530 else {
1531 if (!Was_Already_Mounted)
1532 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001533 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001534 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001535 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001536 if (!Was_Already_Mounted)
1537 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001538 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001539}
Dees_Troy38bd7602012-09-14 13:33:53 -04001540
1541void TWPartition::Find_Actual_Block_Device(void) {
1542 if (Is_Decrypted) {
1543 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001544 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001545 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001546 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001547 Is_Present = true;
1548 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001549 return;
1550 }
1551 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001552 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001553 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001554 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001555 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001556 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001557 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001558}
1559
1560void TWPartition::Recreate_Media_Folder(void) {
1561 string Command;
1562
1563 if (!Mount(true)) {
1564 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001565 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001566 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001567 LOGI("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001568 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1569 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001570 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001571}
Dees_Troye58d5262012-09-21 12:27:57 -04001572
1573void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001574 if (!Has_Android_Secure)
1575 return;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001576 LOGI("Creating .android_secure: %s\n", Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001577 if (!Mount(true)) {
1578 LOGE("Unable to recreate android secure folder.\n");
1579 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1580 LOGI("Recreating android secure folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001581 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1582 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1583 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001584 }
1585}