blob: 681604f2150f79235721e3f5f42ab7251ba7ddeb [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
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050038#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
40#include "common.h"
41#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050044#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040046extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040047 #include "mtdutils/mtdutils.h"
48 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000049#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
50 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
51#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040052}
Dees_Troy51a0e822012-09-05 15:24:24 -040053
bigbiff bigbiff9c754052013-01-09 09:09:08 -050054using namespace std;
55
Dees_Troy51a0e822012-09-05 15:24:24 -040056TWPartition::TWPartition(void) {
57 Can_Be_Mounted = false;
58 Can_Be_Wiped = false;
59 Wipe_During_Factory_Reset = false;
60 Wipe_Available_in_GUI = false;
61 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040062 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040063 SubPartition_Of = "";
64 Symlink_Path = "";
65 Symlink_Mount_Point = "";
66 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040067 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040068 Actual_Block_Device = "";
69 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040070 Alternate_Block_Device = "";
71 Removable = false;
72 Is_Present = false;
73 Length = 0;
74 Size = 0;
75 Used = 0;
76 Free = 0;
77 Backup_Size = 0;
78 Can_Be_Encrypted = false;
79 Is_Encrypted = false;
80 Is_Decrypted = false;
81 Decrypted_Block_Device = "";
82 Display_Name = "";
83 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040084 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040085 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040086 Backup_Method = NONE;
87 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040088 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040089 Is_Storage = false;
90 Storage_Path = "";
91 Current_File_System = "";
92 Fstab_File_System = "";
93 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +000094 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +000095 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +000096#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
97 EcryptFS_Password = "";
98#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040099}
100
101TWPartition::~TWPartition(void) {
102 // Do nothing
103}
104
Dees_Troy5bf43922012-09-07 16:07:55 -0400105bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
106 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
107 int line_len = Line.size(), index = 0, item_index = 0;
108 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400109 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400110 strncpy(full_line, Line.c_str(), line_len);
111
Dees_Troy51127312012-09-08 13:08:49 -0400112 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400113 if (full_line[index] <= 32)
114 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400115 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400116 Mount_Point = full_line;
117 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400118 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400119 index = Mount_Point.size();
120 while (index < line_len) {
121 while (index < line_len && full_line[index] == '\0')
122 index++;
123 if (index >= line_len)
124 continue;
125 ptr = full_line + index;
126 if (item_index == 0) {
127 // File System
128 Fstab_File_System = ptr;
129 Current_File_System = ptr;
130 item_index++;
131 } else if (item_index == 1) {
132 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400133 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400134 MTD_Name = ptr;
135 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400136 } else if (Fstab_File_System == "bml") {
137 if (Mount_Point == "/boot")
138 MTD_Name = "boot";
139 else if (Mount_Point == "/recovery")
140 MTD_Name = "recovery";
141 Primary_Block_Device = ptr;
142 if (*ptr != '/')
143 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 -0400144 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400145 if (Display_Error)
146 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
147 else
148 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
149 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400150 } else {
151 Primary_Block_Device = ptr;
152 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400153 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400154 item_index++;
155 } else if (item_index > 1) {
156 if (*ptr == '/') {
157 // Alternate Block Device
158 Alternate_Block_Device = ptr;
159 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
160 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
161 // Partition length
162 ptr += 7;
163 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400164 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
165 // Custom flags, save for later so that new values aren't overwritten by defaults
166 ptr += 6;
167 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000168 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400169 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
170 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400171 } else {
172 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400173 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400174 }
175 }
176 while (index < line_len && full_line[index] != '\0')
177 index++;
178 }
179
180 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
181 if (Display_Error)
182 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
183 else
184 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
185 return 0;
186 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400187 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400188 Setup_File_System(Display_Error);
189 if (Mount_Point == "/system") {
190 Display_Name = "System";
191 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400192 } else if (Mount_Point == "/data") {
193 Display_Name = "Data";
194 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400195 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400196#ifdef RECOVERY_SDCARD_ON_DATA
197 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400198 Is_Storage = true;
199 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000200 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400201 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
202 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400203 Symlink_Mount_Point = "/emmc";
204 } else {
205 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400206 Symlink_Mount_Point = "/sdcard";
207 }
Dees_Troy16b74352012-11-14 22:27:31 +0000208 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
209 Storage_Path = "/data/media/0";
210 Symlink_Path = Storage_Path;
211 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
212 UnMount(true);
213 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400214#endif
215#ifdef TW_INCLUDE_CRYPTO
216 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400217 char crypto_blkdev[255];
218 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
219 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400220 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400221 DataManager::SetValue(TW_IS_DECRYPTED, 1);
222 Is_Encrypted = true;
223 Is_Decrypted = true;
224 Decrypted_Block_Device = crypto_blkdev;
225 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
226 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 Is_Encrypted = true;
228 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800229 Can_Be_Mounted = false;
230 Current_File_System = "emmc";
231 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400232 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
233 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
234 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800235 } else {
236 // Filesystem is not encrypted and the mount
237 // succeeded, so get it back to the original
238 // unmounted state
239 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400240 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400241 #ifdef RECOVERY_SDCARD_ON_DATA
242 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
243 Recreate_Media_Folder();
244 #endif
245#else
246 #ifdef RECOVERY_SDCARD_ON_DATA
247 Recreate_Media_Folder();
248 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400249#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 } else if (Mount_Point == "/cache") {
251 Display_Name = "Cache";
252 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400253 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400254 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troyb46a6842012-09-25 11:06:46 -0400255 LOGI("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500256 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
257 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400258 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400260 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 Display_Name = "DataData";
262 Is_SubPartition = true;
263 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 DataManager::SetValue(TW_HAS_DATADATA, 1);
265 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400266 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400267 Display_Name = "SD-Ext";
268 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400269 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400270 } else if (Mount_Point == "/boot") {
271 Display_Name = "Boot";
272 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400273 }
274#ifdef TW_EXTERNAL_STORAGE_PATH
275 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
276 Is_Storage = true;
277 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400278 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400279#else
280 if (Mount_Point == "/sdcard") {
281 Is_Storage = true;
282 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400283 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400284#ifndef RECOVERY_SDCARD_ON_DATA
285 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400286 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400287#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400288#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000289 }
Dees_Troy8170a922012-09-18 15:40:25 -0400290#ifdef TW_INTERNAL_STORAGE_PATH
291 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
292 Is_Storage = true;
293 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400294#ifndef RECOVERY_SDCARD_ON_DATA
295 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400296 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400297#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400298 }
299#else
300 if (Mount_Point == "/emmc") {
301 Is_Storage = true;
302 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400303#ifndef RECOVERY_SDCARD_ON_DATA
304 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400305 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400306#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400307 }
308#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400309 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400310 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400311 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 }
313
Dees_Troy51127312012-09-08 13:08:49 -0400314 // Process any custom flags
315 if (Flags.size() > 0)
316 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400317 return true;
318}
319
320bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
321 char flags[MAX_FSTAB_LINE_LENGTH];
322 int flags_len, index = 0;
323 char* ptr;
324
325 strcpy(flags, Flags.c_str());
326 flags_len = Flags.size();
327 for (index = 0; index < flags_len; index++) {
328 if (flags[index] == ';')
329 flags[index] = '\0';
330 }
331
332 index = 0;
333 while (index < flags_len) {
334 while (index < flags_len && flags[index] == '\0')
335 index++;
336 if (index >= flags_len)
337 continue;
338 ptr = flags + index;
339 if (strcmp(ptr, "removable") == 0) {
340 Removable = true;
341 } else if (strcmp(ptr, "storage") == 0) {
342 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400343 } else if (strcmp(ptr, "canbewiped") == 0) {
344 Can_Be_Wiped = true;
345 } else if (strcmp(ptr, "wipeingui") == 0) {
346 Can_Be_Wiped = true;
347 Wipe_Available_in_GUI = true;
348 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
349 Can_Be_Wiped = true;
350 Wipe_Available_in_GUI = true;
351 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400352 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000353 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400354 Is_SubPartition = true;
355 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000356 } else if (strcmp(ptr, "ignoreblkid") == 0) {
357 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000358 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
359 Retain_Layout_Version = true;
Dees_Troy51127312012-09-08 13:08:49 -0400360 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
361 ptr += 8;
362 Symlink_Path = ptr;
363 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
364 ptr += 8;
365 Display_Name = ptr;
366 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
367 ptr += 10;
368 Format_Block_Size = atoi(ptr);
369 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
370 ptr += 7;
371 Length = atoi(ptr);
372 } else {
373 if (Display_Error)
374 LOGE("Unhandled flag: '%s'\n", ptr);
375 else
376 LOGI("Unhandled flag: '%s'\n", ptr);
377 }
378 while (index < flags_len && flags[index] != '\0')
379 index++;
380 }
381 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400382}
383
Dees_Troy5bf43922012-09-07 16:07:55 -0400384bool TWPartition::Is_File_System(string File_System) {
385 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400386 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400387 File_System == "ext4" ||
388 File_System == "vfat" ||
389 File_System == "ntfs" ||
390 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500391 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400392 File_System == "auto")
393 return true;
394 else
395 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400396}
397
Dees_Troy5bf43922012-09-07 16:07:55 -0400398bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400399 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400400 return true;
401 else
402 return false;
403}
404
Dees_Troy51127312012-09-08 13:08:49 -0400405bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400406 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400407 if (mkdir(Path.c_str(), 0777) == -1) {
408 if (Display_Error)
409 LOGE("Can not create '%s' folder.\n", Path.c_str());
410 else
411 LOGI("Can not create '%s' folder.\n", Path.c_str());
412 return false;
413 } else {
414 LOGI("Created '%s' folder.\n", Path.c_str());
415 return true;
416 }
417 }
418 return true;
419}
420
Dees_Troy5bf43922012-09-07 16:07:55 -0400421void TWPartition::Setup_File_System(bool Display_Error) {
422 struct statfs st;
423
424 Can_Be_Mounted = true;
425 Can_Be_Wiped = true;
426
Dees_Troy5bf43922012-09-07 16:07:55 -0400427 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400428 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400429 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
430 Backup_Name = Display_Name;
431 Backup_Method = FILES;
432}
433
434void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
436 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800437 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400438 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800439 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 Backup_Method = FLASH_UTILS;
441 else
Gary Peck82599a82012-11-21 16:23:12 -0800442 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 -0400443 if (Find_Partition_Size()) {
444 Used = Size;
445 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400446 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400447 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400448 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400449 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400450 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 }
452}
453
Dees_Troye58d5262012-09-21 12:27:57 -0400454void TWPartition::Setup_AndSec(void) {
455 Backup_Name = "and-sec";
456 Has_Android_Secure = true;
457 Symlink_Path = Mount_Point + "/.android_secure";
458 Symlink_Mount_Point = "/and-sec";
459 Backup_Path = Symlink_Mount_Point;
460 Make_Dir("/and-sec", true);
461 Recreate_AndSec_Folder();
462}
463
Dees_Troy5bf43922012-09-07 16:07:55 -0400464void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
465 char device[512], realDevice[512];
466
467 strcpy(device, Block.c_str());
468 memset(realDevice, 0, sizeof(realDevice));
469 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
470 {
471 strcpy(device, realDevice);
472 memset(realDevice, 0, sizeof(realDevice));
473 }
474
475 if (device[0] != '/') {
476 if (Display_Error)
477 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
478 else
479 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
480 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400482 Block = device;
483 return;
484 }
485}
486
Dees_Troy8e337f32012-10-13 22:07:49 -0400487void TWPartition::Mount_Storage_Retry(void) {
488 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500489 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400490 int retry_count = 5;
491 while (retry_count > 0 && !Mount(false)) {
492 usleep(500000);
493 retry_count--;
494 }
495 Mount(true);
496 }
497}
498
Dees_Troy38bd7602012-09-14 13:33:53 -0400499bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
500 FILE *fp = NULL;
501 char line[255];
502
503 fp = fopen("/proc/mtd", "rt");
504 if (fp == NULL) {
505 LOGE("Device does not support /proc/mtd\n");
506 return false;
507 }
508
509 while (fgets(line, sizeof(line), fp) != NULL)
510 {
511 char device[32], label[32];
512 unsigned long size = 0;
513 char* fstype = NULL;
514 int deviceId;
515
516 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
517
518 // Skip header and blank lines
519 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
520 continue;
521
522 // Strip off the trailing " from the label
523 label[strlen(label)-1] = '\0';
524
525 if (strcmp(label, MTD_Name.c_str()) == 0) {
526 // We found our device
527 // Strip off the trailing : from the device
528 device[strlen(device)-1] = '\0';
529 if (sscanf(device,"mtd%d", &deviceId) == 1) {
530 sprintf(device, "/dev/block/mtdblock%d", deviceId);
531 Primary_Block_Device = device;
532 }
533 }
534 }
535 fclose(fp);
536
537 return false;
538}
539
Dees_Troy51127312012-09-08 13:08:49 -0400540bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
541 struct statfs st;
542 string Local_Path = Mount_Point + "/.";
543
544 if (!Mount(Display_Error))
545 return false;
546
547 if (statfs(Local_Path.c_str(), &st) != 0) {
548 if (!Removable) {
549 if (Display_Error)
550 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
551 else
552 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
553 }
554 return false;
555 }
556 Size = (st.f_blocks * st.f_bsize);
557 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
558 Free = (st.f_bfree * st.f_bsize);
559 Backup_Size = Used;
560 return true;
561}
562
563bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400564 FILE* fp;
565 char command[255], line[512];
566 int include_block = 1;
567 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500568 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400569
570 if (!Mount(Display_Error))
571 return false;
572
Dees_Troy38bd7602012-09-14 13:33:53 -0400573 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400574 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500575 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400576 fp = fopen("/tmp/dfoutput.txt", "rt");
577 if (fp == NULL) {
578 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400579 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400580 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400581
582 while (fgets(line, sizeof(line), fp) != NULL)
583 {
584 unsigned long blocks, used, available;
585 char device[64];
586 char tmpString[64];
587
588 if (strncmp(line, "Filesystem", 10) == 0)
589 continue;
590 if (strlen(line) < min_len) {
591 include_block = 0;
592 continue;
593 }
594 if (include_block) {
595 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
596 } else {
597 // The device block string is so long that the df information is on the next line
598 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400599 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400600 while (tmpString[space_count] == 32)
601 space_count++;
602 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
603 }
604
605 // Adjust block size to byte size
606 Size = blocks * 1024ULL;
607 Used = used * 1024ULL;
608 Free = available * 1024ULL;
609 Backup_Size = Used;
610 }
611 fclose(fp);
612 return true;
613}
614
Dees_Troy5bf43922012-09-07 16:07:55 -0400615bool TWPartition::Find_Partition_Size(void) {
616 FILE* fp;
617 char line[512];
618 string tmpdevice;
619
igoriok87e3d932013-01-31 21:03:53 +0200620 fp = fopen("/proc/dumchar_info", "rt");
621 if (fp != NULL) {
622 while (fgets(line, sizeof(line), fp) != NULL)
623 {
624 char label[32], device[32];
625 unsigned long size = 0;
626
627 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
628
629 // Skip header, annotation and blank lines
630 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
631 continue;
632
633 tmpdevice = "/dev/";
634 tmpdevice += label;
635 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
636 Size = size;
637 fclose(fp);
638 return true;
639 }
640 }
641 }
642
Dees_Troy5bf43922012-09-07 16:07:55 -0400643 // In this case, we'll first get the partitions we care about (with labels)
644 fp = fopen("/proc/partitions", "rt");
645 if (fp == NULL)
646 return false;
647
648 while (fgets(line, sizeof(line), fp) != NULL)
649 {
650 unsigned long major, minor, blocks;
651 char device[512];
652 char tmpString[64];
653
Dees_Troy63c8df72012-09-10 14:02:05 -0400654 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400655 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
656
657 tmpdevice = "/dev/block/";
658 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400659 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400660 // Adjust block size to byte size
661 Size = blocks * 1024ULL;
662 fclose(fp);
663 return true;
664 }
665 }
666 fclose(fp);
667 return false;
668}
669
Dees_Troy5bf43922012-09-07 16:07:55 -0400670bool TWPartition::Is_Mounted(void) {
671 if (!Can_Be_Mounted)
672 return false;
673
674 struct stat st1, st2;
675 string test_path;
676
677 // Check to see if the mount point directory exists
678 test_path = Mount_Point + "/.";
679 if (stat(test_path.c_str(), &st1) != 0) return false;
680
681 // Check to see if the directory above the mount point exists
682 test_path = Mount_Point + "/../.";
683 if (stat(test_path.c_str(), &st2) != 0) return false;
684
685 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
686 int ret = (st1.st_dev != st2.st_dev) ? true : false;
687
688 return ret;
689}
690
691bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000692 int exfat_mounted = 0;
693
Dees_Troy5bf43922012-09-07 16:07:55 -0400694 if (Is_Mounted()) {
695 return true;
696 } else if (!Can_Be_Mounted) {
697 return false;
698 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400699
700 Find_Actual_Block_Device();
701
702 // Check the current file system before mounting
703 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000704 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000705 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 +0000706 LOGI("cmd: %s\n", cmd.c_str());
707 string result;
708 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
709 LOGI("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
710 Current_File_System = "vfat";
711 } else {
712#ifdef TW_NO_EXFAT_FUSE
713 UnMount(false);
714 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
715 // Some kernels let us mount vfat as exfat which doesn't work out too well
716#else
717 exfat_mounted = 1;
718#endif
719 }
720 }
Dees_Troy22042032012-12-18 21:23:08 +0000721 if (Fstab_File_System == "yaffs2") {
722 // mount an MTD partition as a YAFFS2 filesystem.
723 mtd_scan_partitions();
724 const MtdPartition* partition;
725 partition = mtd_find_partition_by_name(MTD_Name.c_str());
726 if (partition == NULL) {
727 LOGE("Failed to find '%s' partition to mount at '%s'\n",
728 MTD_Name.c_str(), Mount_Point.c_str());
729 return false;
730 }
731 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
732 if (Display_Error)
733 LOGE("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
734 else
735 LOGI("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
736 return false;
737 } else
738 return true;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000739 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
740#ifdef TW_NO_EXFAT_FUSE
741 if (Current_File_System == "exfat") {
742 LOGI("Mounting exfat failed, trying vfat...\n");
743 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000744 if (Display_Error)
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000745 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000746 else
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000747 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
748 LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
749 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000750 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000751 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000752#endif
753 if (Display_Error)
754 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
755 else
756 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
757 LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
758 return false;
759#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000760 }
761#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000762 }
763#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
764 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
765 MetaEcfsFile += "/.MetaEcfsFile";
766 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
767 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
768 if (Display_Error)
769 LOGE("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
770 else
771 LOGI("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
772 } else {
773 LOGI("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
774 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400775 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000776 } else {
777 Is_Decrypted = false;
778 }
779#endif
780 if (Removable)
781 Update_Size(Display_Error);
782
783 if (!Symlink_Mount_Point.empty()) {
784 string Command, Result;
785 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
786 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400787 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400788 return true;
789}
790
791bool TWPartition::UnMount(bool Display_Error) {
792 if (Is_Mounted()) {
793 int never_unmount_system;
794
795 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
796 if (never_unmount_system == 1 && Mount_Point == "/system")
797 return true; // Never unmount system if you're not supposed to unmount it
798
Dees_Troyc8bafa12013-01-10 15:43:00 +0000799#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
800 if (EcryptFS_Password.size() > 0) {
801 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
802 if (Display_Error)
803 LOGE("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
804 else
805 LOGI("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
806 } else {
807 LOGI("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
808 }
809 }
810#endif
811
Dees_Troy38bd7602012-09-14 13:33:53 -0400812 if (!Symlink_Mount_Point.empty())
813 umount(Symlink_Mount_Point.c_str());
814
Dees_Troyb05ddee2013-01-28 20:24:50 +0000815 umount(Mount_Point.c_str());
816 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400817 if (Display_Error)
818 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
819 else
820 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
821 return false;
822 } else
823 return true;
824 } else {
825 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826 }
827}
828
Gary Peck43acadf2012-11-21 21:19:01 -0800829bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000830 bool wiped = false, update_crypt = false;
831 int check;
832 string Layout_Filename = Mount_Point + "/.layout_version";
833
Dees_Troy38bd7602012-09-14 13:33:53 -0400834 if (!Can_Be_Wiped) {
835 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
836 return false;
837 }
838
Dees_Troyc51f1f92012-09-20 15:32:13 -0400839 if (Mount_Point == "/cache")
840 tmplog_offset = 0;
841
Dees_Troyce675462013-01-09 19:48:21 +0000842#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
843 if (Mount_Point == "/data" && Mount(false)) {
844 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
845 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
846 }
847#endif
848
Dees_Troy16c2b312013-01-15 16:51:18 +0000849 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
850 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
851 else
852 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400853
Dees_Troy16c2b312013-01-15 16:51:18 +0000854 if (Has_Data_Media) {
855 wiped = Wipe_Data_Without_Wiping_Media();
856 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800857
Dees_Troy16c2b312013-01-15 16:51:18 +0000858 DataManager::GetValue(TW_RM_RF_VAR, check);
859
860 if (check)
861 wiped = Wipe_RMRF();
862 else if (New_File_System == "ext4")
863 wiped = Wipe_EXT4();
864 else if (New_File_System == "ext2" || New_File_System == "ext3")
865 wiped = Wipe_EXT23(New_File_System);
866 else if (New_File_System == "vfat")
867 wiped = Wipe_FAT();
868 else if (New_File_System == "exfat")
869 wiped = Wipe_EXFAT();
870 else if (New_File_System == "yaffs2")
871 wiped = Wipe_MTD();
872 else {
873 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
874 unlink("/.layout_version");
875 return false;
876 }
877 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800878 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400879
Gary Pecke8bc5d72012-12-21 06:45:25 -0800880 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000881#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
882 if (Mount_Point == "/data" && Mount(false)) {
883 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
884 Make_Dir("/data/system", true);
885 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
886 }
887 }
888#endif
Dees_Troy16c2b312013-01-15 16:51:18 +0000889
Dees_Troy1c1ac442013-01-17 21:42:14 +0000890 if (Mount_Point == "/cache")
891 DataManager::Output_Version();
892
Dees_Troy16c2b312013-01-15 16:51:18 +0000893 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
894 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
895
896 if (update_crypt) {
897 Setup_File_System(false);
898 if (Is_Encrypted && !Is_Decrypted) {
899 // just wiped an encrypted partition back to its unencrypted state
900 Is_Encrypted = false;
901 Is_Decrypted = false;
902 Decrypted_Block_Device = "";
903 if (Mount_Point == "/data") {
904 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
905 DataManager::SetValue(TW_IS_DECRYPTED, 0);
906 }
Gary Pecke8bc5d72012-12-21 06:45:25 -0800907 }
908 }
909 }
910 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400911}
912
Gary Peck43acadf2012-11-21 21:19:01 -0800913bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800914 if (Is_File_System(Current_File_System))
915 return Wipe(Current_File_System);
916 else
917 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -0800918}
919
Dees_Troye58d5262012-09-21 12:27:57 -0400920bool TWPartition::Wipe_AndSec(void) {
921 if (!Has_Android_Secure)
922 return false;
923
Dees_Troye58d5262012-09-21 12:27:57 -0400924 if (!Mount(true))
925 return false;
926
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500927 ui_print("Wiping .android_secure\n");
928 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -0400929 return true;
930}
931
Dees_Troy51a0e822012-09-05 15:24:24 -0400932bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400933 if (Backup_Method == FILES)
934 return Backup_Tar(backup_folder);
935 else if (Backup_Method == DD)
936 return Backup_DD(backup_folder);
937 else if (Backup_Method == FLASH_UTILS)
938 return Backup_Dump_Image(backup_folder);
939 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
940 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400941}
942
Dees_Troy43d8b002012-09-17 16:00:01 -0400943bool TWPartition::Check_MD5(string restore_folder) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400944 string Full_Filename, md5file;
Dees_Troy43d8b002012-09-17 16:00:01 -0400945 char split_filename[512];
946 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500947 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400948
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400949 memset(split_filename, 0, sizeof(split_filename));
Dees_Troy43d8b002012-09-17 16:00:01 -0400950 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400951 if (!TWFunc::Path_Exists(Full_Filename)) {
952 // This is a split archive, we presume
953 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400954 LOGI("split_filename: %s\n", split_filename);
955 md5file = split_filename;
956 md5file += ".md5";
957 if (!TWFunc::Path_Exists(md5file)) {
958 LOGE("No md5 file found for '%s'.\n", split_filename);
959 LOGE("Please unselect Enable MD5 verification to restore.\n");
960 return false;
961 }
962 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400963 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500964 if (md5sum.verify_md5digest() != 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400965 LOGE("MD5 failed to match on '%s'.\n", split_filename);
966 return false;
967 }
968 index++;
969 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400970 md5sum.setfn(split_filename);
Dees_Troy43d8b002012-09-17 16:00:01 -0400971 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400972 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400973 } else {
974 // Single file archive
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400975 md5file = Full_Filename + ".md5";
976 if (!TWFunc::Path_Exists(md5file)) {
977 LOGE("No md5 file found for '%s'.\n", Full_Filename.c_str());
978 LOGE("Please unselect Enable MD5 verification to restore.\n");
979 return false;
980 }
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500981 md5sum.setfn(Full_Filename);
982 if (md5sum.verify_md5digest() != 0) {
bigbiff bigbiff65a4c732013-03-15 15:17:50 -0400983 LOGE("MD5 failed to match on '%s'.\n", Full_Filename.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400984 return false;
985 } else
986 return true;
987 }
988 return false;
989}
990
Dees_Troy51a0e822012-09-05 15:24:24 -0400991bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -0800992 size_t first_period, second_period;
993 string Restore_File_System;
994
995 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
996 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
997
998 // Parse backup filename to extract the file system before wiping
999 first_period = Backup_FileName.find(".");
1000 if (first_period == string::npos) {
1001 LOGE("Unable to find file system (first period).\n");
1002 return false;
1003 }
1004 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1005 second_period = Restore_File_System.find(".");
1006 if (second_period == string::npos) {
1007 LOGE("Unable to find file system (second period).\n");
1008 return false;
1009 }
1010 Restore_File_System.resize(second_period);
1011 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1012
1013 if (Is_File_System(Restore_File_System))
1014 return Restore_Tar(restore_folder, Restore_File_System);
1015 else if (Is_Image(Restore_File_System)) {
1016 if (Restore_File_System == "emmc")
1017 return Restore_DD(restore_folder);
1018 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1019 return Restore_Flash_Image(restore_folder);
1020 }
1021
Dees_Troy38bd7602012-09-14 13:33:53 -04001022 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
1023 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001024}
1025
1026string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001027 if (Backup_Method == NONE)
1028 return "none";
1029 else if (Backup_Method == FILES)
1030 return "files";
1031 else if (Backup_Method == DD)
1032 return "dd";
1033 else if (Backup_Method == FLASH_UTILS)
1034 return "flash_utils";
1035 else
1036 return "undefined";
1037 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001038}
1039
1040bool TWPartition::Decrypt(string Password) {
1041 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001042 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001043 return 1;
1044}
1045
1046bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001047 bool Save_Data_Media = Has_Data_Media;
1048
1049 if (!UnMount(true))
1050 return false;
1051
Dees_Troy38bd7602012-09-14 13:33:53 -04001052 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001053 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001054 Has_Data_Media = Save_Data_Media;
1055 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1056 Recreate_Media_Folder();
1057 }
Dees_Troyb46a6842012-09-25 11:06:46 -04001058 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001059 return true;
1060 } else {
1061 Has_Data_Media = Save_Data_Media;
1062 LOGE("Unable to format to remove encryption.\n");
1063 return false;
1064 }
1065 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001066}
1067
1068void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001069 const char* type;
1070 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001071
Dees_Troy68cab492012-12-12 19:29:35 +00001072 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1073 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001074
Dees_Troy38bd7602012-09-14 13:33:53 -04001075 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001076 if (!Is_Present)
1077 return;
Dees_Troy51127312012-09-08 13:08:49 -04001078
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001079 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1080 if (blkid_do_fullprobe(pr)) {
1081 blkid_free_probe(pr);
1082 LOGI("Can't probe device %s\n", Actual_Block_Device.c_str());
1083 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001084 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001085 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1086 blkid_free_probe(pr);
1087 LOGI("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
1088 return;
1089 }
1090 Current_File_System = type;
Dees_Troy51a0e822012-09-05 15:24:24 -04001091 return;
1092}
1093
Gary Peck43acadf2012-11-21 21:19:01 -08001094bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001095 if (!UnMount(true))
1096 return false;
1097
Dees_Troy43d8b002012-09-17 16:00:01 -04001098 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001099 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001100
1101 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
1102 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001103 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
1104 LOGI("mke2fs command: %s\n", command.c_str());
1105 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001106 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001107 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001108 ui_print("Done.\n");
1109 return true;
1110 } else {
1111 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1112 return false;
1113 }
1114 } else
1115 return Wipe_RMRF();
1116
1117 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001118}
1119
1120bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001121 if (!UnMount(true))
1122 return false;
1123
Dees_Troy43d8b002012-09-17 16:00:01 -04001124 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001125 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001126
1127 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
1128 Find_Actual_Block_Device();
1129 Command = "make_ext4fs";
1130 if (!Is_Decrypted && Length != 0) {
1131 // Only use length if we're not decrypted
1132 char len[32];
1133 sprintf(len, "%i", Length);
1134 Command += " -l ";
1135 Command += len;
1136 }
1137 Command += " " + Actual_Block_Device;
1138 LOGI("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001139 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001140 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001141 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001142 ui_print("Done.\n");
1143 return true;
1144 } else {
1145 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1146 return false;
1147 }
1148 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001149 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001150
1151 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001152}
1153
1154bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001155 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001156
Dees_Troy43d8b002012-09-17 16:00:01 -04001157 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001158 if (!UnMount(true))
1159 return false;
1160
1161 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1162 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001163 command = "mkdosfs " + Actual_Block_Device;
1164 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001165 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001166 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001167 ui_print("Done.\n");
1168 return true;
1169 } else {
1170 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1171 return false;
1172 }
1173 return true;
1174 }
1175 else
1176 return Wipe_RMRF();
1177
1178 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001179}
1180
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001181bool TWPartition::Wipe_EXFAT() {
1182 string command, result;
1183
1184 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1185 if (!UnMount(true))
1186 return false;
1187
1188 ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
1189 Find_Actual_Block_Device();
1190 command = "mkexfatfs " + Actual_Block_Device;
1191 if (TWFunc::Exec_Cmd(command, result) == 0) {
1192 Recreate_AndSec_Folder();
1193 ui_print("Done.\n");
1194 return true;
1195 } else {
1196 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1197 return false;
1198 }
1199 return true;
1200 }
1201 return false;
1202}
1203
Dees_Troy38bd7602012-09-14 13:33:53 -04001204bool TWPartition::Wipe_MTD() {
1205 if (!UnMount(true))
1206 return false;
1207
1208 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1209
1210 mtd_scan_partitions();
1211 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1212 if (mtd == NULL) {
1213 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1214 return false;
1215 }
1216
1217 MtdWriteContext* ctx = mtd_write_partition(mtd);
1218 if (ctx == NULL) {
1219 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1220 return false;
1221 }
1222 if (mtd_erase_blocks(ctx, -1) == -1) {
1223 mtd_write_close(ctx);
1224 LOGE("Failed to format '%s'", MTD_Name.c_str());
1225 return false;
1226 }
1227 if (mtd_write_close(ctx) != 0) {
1228 LOGE("Failed to close '%s'", MTD_Name.c_str());
1229 return false;
1230 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001231 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001232 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001233 ui_print("Done.\n");
1234 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001235}
1236
1237bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001238 if (!Mount(true))
1239 return false;
1240
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001241 ui_print("Removing all files under '%s'\n", Mount_Point.c_str());
1242 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001243 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001244 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001245}
1246
1247bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001248 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001249
1250 // This handles wiping data on devices with "sdcard" in /data/media
1251 if (!Mount(true))
1252 return false;
1253
1254 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001255
1256 DIR* d;
1257 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001258 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001259 struct dirent* de;
1260 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001261 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1262 // The media folder is the "internal sdcard"
1263 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1264 // the media folder for multi-user.
1265 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001266
1267 dir = "/data/";
1268 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001269 if (de->d_type == DT_DIR) {
1270 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001271 } 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 +00001272 if (!unlink(dir.c_str()))
1273 LOGI("Unable to unlink '%s'\n", dir.c_str());
1274 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001275 }
1276 closedir(d);
Dees_Troy16b74352012-11-14 22:27:31 +00001277 ui_print("Done.\n");
1278 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001279 }
Dees_Troy16b74352012-11-14 22:27:31 +00001280 ui_print("Dirent failed to open /data, error!\n");
1281 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001282}
1283
1284bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001285 char back_name[255], split_index[5];
1286 string Full_FileName, Split_FileName, Tar_Args, Command;
1287 int use_compression, index, backup_count;
1288 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001289 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001290 twrpTar tar;
1291 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001292
Dees_Troy43d8b002012-09-17 16:00:01 -04001293 if (!Mount(true))
1294 return false;
1295
Dees_Troy2c50e182012-09-26 20:05:28 -04001296 if (Backup_Path == "/and-sec") {
1297 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1298 ui_print("Backing up %s...\n", "Android Secure");
1299 } else {
1300 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1301 ui_print("Backing up %s...\n", Display_Name.c_str());
1302 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001303
1304 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001305
1306 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1307 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001308 Full_FileName = backup_folder + "/" + Backup_FileName;
1309 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001310 // This backup needs to be split into multiple archives
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001311 ui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001312 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001313 tar.setdir(back_name);
1314 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001315 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001316 if (backup_count == -1) {
1317 LOGE("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001318 return false;
1319 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001320 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001321 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001322 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001323 if (use_compression) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001324 tar.setdir(Backup_Path);
1325 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001326 if (tar.createTarGZFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001327 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001328 string gzname = Full_FileName + ".gz";
1329 rename(gzname.c_str(), Full_FileName.c_str());
1330 }
1331 else {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001332 tar.setdir(Backup_Path);
1333 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001334 if (tar.createTarFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001335 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001336 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001337 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1338 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1339 return false;
1340 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001341 }
1342 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001343}
1344
1345bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001346 char back_name[255], backup_size[32];
1347 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001348 int use_compression;
1349
igoriok87e3d932013-01-31 21:03:53 +02001350 sprintf(backup_size, "%llu", Backup_Size);
1351 DD_BS = backup_size;
1352
Dees_Troyb46a6842012-09-25 11:06:46 -04001353 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001354 ui_print("Backing up %s...\n", Display_Name.c_str());
1355
1356 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1357 Backup_FileName = back_name;
1358
1359 Full_FileName = backup_folder + "/" + Backup_FileName;
1360
igoriok87e3d932013-01-31 21:03:53 +02001361 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy43d8b002012-09-17 16:00:01 -04001362 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001363 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001364 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1365 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001366 return false;
1367 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001368 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001369}
1370
1371bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001372 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001373 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001374 int use_compression;
1375
Dees_Troyb46a6842012-09-25 11:06:46 -04001376 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001377 ui_print("Backing up %s...\n", Display_Name.c_str());
1378
1379 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1380 Backup_FileName = back_name;
1381
1382 Full_FileName = backup_folder + "/" + Backup_FileName;
1383
1384 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1385 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001386 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001387 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1388 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1389 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1390 return false;
1391 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001392 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001393}
1394
Gary Peck43acadf2012-11-21 21:19:01 -08001395bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1396 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001397 int index = 0;
1398 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001399
Dees_Troye58d5262012-09-21 12:27:57 -04001400 if (Has_Android_Secure) {
1401 ui_print("Wiping android secure...\n");
1402 if (!Wipe_AndSec())
1403 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001404 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001405 ui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001406 if (!Wipe(Restore_File_System))
1407 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001408 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001409
1410 if (!Mount(true))
1411 return false;
1412
Dees_Troyda8b55a2012-12-12 19:18:30 +00001413 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001414 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001415 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001416 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001417 if (!TWFunc::Path_Exists(Full_FileName)) {
1418 // Backup is multiple archives
1419 LOGI("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001420 sprintf(split_index, "%03i", index);
1421 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001422 while (TWFunc::Path_Exists(Full_FileName)) {
1423 index++;
1424 ui_print("Restoring archive %i...\n", index);
1425 LOGI("Restoring '%s'...\n", Full_FileName.c_str());
1426 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001427 tar.setdir("/");
1428 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001429 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001430 return false;
1431 sprintf(split_index, "%03i", index);
1432 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1433 }
1434 if (index == 0) {
1435 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1436 return false;
1437 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001438 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001439 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001440 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001441 tar.setdir(Backup_Path);
1442 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001443 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001444 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001445 }
1446 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001447}
1448
1449bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001450 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001451
Dees_Troyda8b55a2012-12-12 19:18:30 +00001452 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001453 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001454
1455 if (!Find_Partition_Size()) {
1456 LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
1457 return false;
1458 }
1459 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1460 if (backup_size > Size) {
1461 LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
1462 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1463 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1464 return false;
1465 }
1466
1467 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001468 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1469 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001470 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001471 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001472}
1473
1474bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001475 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001476
1477 ui_print("Restoring %s...\n", Display_Name.c_str());
1478 Full_FileName = restore_folder + "/" + Backup_FileName;
1479 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1480 Command = "erase_image " + MTD_Name;
1481 LOGI("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001482 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001483 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1484 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001485 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001486 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001487}
Dees_Troy5bf43922012-09-07 16:07:55 -04001488
1489bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001490 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001491
Dees_Troyab10ee22012-09-21 14:27:30 -04001492 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001493 return false;
1494
Dees_Troy0550cfb2012-10-13 11:56:13 -04001495 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001496 if (Removable || Is_Encrypted) {
1497 if (!Mount(false))
1498 return true;
1499 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001500 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001501
1502 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001503 if (!ret || Size == 0) {
1504 if (!Get_Size_Via_df(Display_Error)) {
1505 if (!Was_Already_Mounted)
1506 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001507 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001508 }
1509 }
Dees_Troy51127312012-09-08 13:08:49 -04001510
Dees_Troy5bf43922012-09-07 16:07:55 -04001511 if (Has_Data_Media) {
1512 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001513 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001514 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1515 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001516 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001517 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001518 int bak = (int)(Backup_Size / 1048576LLU);
1519 int total = (int)(Size / 1048576LLU);
1520 int us = (int)(Used / 1048576LLU);
1521 int fre = (int)(Free / 1048576LLU);
1522 int datmed = (int)(data_media_used / 1048576LLU);
1523 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 -04001524 } else {
1525 if (!Was_Already_Mounted)
1526 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001527 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001528 }
Dees_Troye58d5262012-09-21 12:27:57 -04001529 } else if (Has_Android_Secure) {
1530 if (Mount(Display_Error))
1531 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001532 else {
1533 if (!Was_Already_Mounted)
1534 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001535 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001536 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001537 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001538 if (!Was_Already_Mounted)
1539 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001540 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001541}
Dees_Troy38bd7602012-09-14 13:33:53 -04001542
1543void TWPartition::Find_Actual_Block_Device(void) {
1544 if (Is_Decrypted) {
1545 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001546 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001547 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001548 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001549 Is_Present = true;
1550 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001551 return;
1552 }
1553 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001554 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001555 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001556 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001557 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001558 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001559 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001560}
1561
1562void TWPartition::Recreate_Media_Folder(void) {
1563 string Command;
1564
1565 if (!Mount(true)) {
1566 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001567 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001568 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001569 LOGI("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001570 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1571 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001572 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001573}
Dees_Troye58d5262012-09-21 12:27:57 -04001574
1575void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001576 if (!Has_Android_Secure)
1577 return;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001578 LOGI("Creating .android_secure: %s\n", Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001579 if (!Mount(true)) {
1580 LOGE("Unable to recreate android secure folder.\n");
1581 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1582 LOGI("Recreating android secure folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001583 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1584 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1585 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001586 }
1587}