blob: f2386d7a84ce4626217c4af215cc62435afd2a51 [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_Troy85f44ed2013-01-09 18:42:36 +000093#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
94 EcryptFS_Password = "";
95#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040096}
97
98TWPartition::~TWPartition(void) {
99 // Do nothing
100}
101
Dees_Troy5bf43922012-09-07 16:07:55 -0400102bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
103 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
104 int line_len = Line.size(), index = 0, item_index = 0;
105 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400106 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400107 strncpy(full_line, Line.c_str(), line_len);
108
Dees_Troy51127312012-09-08 13:08:49 -0400109 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400110 if (full_line[index] <= 32)
111 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400112 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400113 Mount_Point = full_line;
114 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400115 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400116 index = Mount_Point.size();
117 while (index < line_len) {
118 while (index < line_len && full_line[index] == '\0')
119 index++;
120 if (index >= line_len)
121 continue;
122 ptr = full_line + index;
123 if (item_index == 0) {
124 // File System
125 Fstab_File_System = ptr;
126 Current_File_System = ptr;
127 item_index++;
128 } else if (item_index == 1) {
129 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400130 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400131 MTD_Name = ptr;
132 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400133 } else if (Fstab_File_System == "bml") {
134 if (Mount_Point == "/boot")
135 MTD_Name = "boot";
136 else if (Mount_Point == "/recovery")
137 MTD_Name = "recovery";
138 Primary_Block_Device = ptr;
139 if (*ptr != '/')
140 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 -0400141 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400142 if (Display_Error)
143 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
144 else
145 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
146 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400147 } else {
148 Primary_Block_Device = ptr;
149 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400150 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400151 item_index++;
152 } else if (item_index > 1) {
153 if (*ptr == '/') {
154 // Alternate Block Device
155 Alternate_Block_Device = ptr;
156 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
157 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
158 // Partition length
159 ptr += 7;
160 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400161 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
162 // Custom flags, save for later so that new values aren't overwritten by defaults
163 ptr += 6;
164 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000165 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400166 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
167 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400168 } else {
169 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400170 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400171 }
172 }
173 while (index < line_len && full_line[index] != '\0')
174 index++;
175 }
176
177 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
178 if (Display_Error)
179 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
180 else
181 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
182 return 0;
183 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400184 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400185 Setup_File_System(Display_Error);
186 if (Mount_Point == "/system") {
187 Display_Name = "System";
188 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400189 } else if (Mount_Point == "/data") {
190 Display_Name = "Data";
191 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400192 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400193#ifdef RECOVERY_SDCARD_ON_DATA
194 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400195 Is_Storage = true;
196 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000197 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400198 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
199 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400200 Symlink_Mount_Point = "/emmc";
201 } else {
202 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400203 Symlink_Mount_Point = "/sdcard";
204 }
Dees_Troy16b74352012-11-14 22:27:31 +0000205 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
206 Storage_Path = "/data/media/0";
207 Symlink_Path = Storage_Path;
208 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
209 UnMount(true);
210 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400211#endif
212#ifdef TW_INCLUDE_CRYPTO
213 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400214 char crypto_blkdev[255];
215 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
216 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400217 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400218 DataManager::SetValue(TW_IS_DECRYPTED, 1);
219 Is_Encrypted = true;
220 Is_Decrypted = true;
221 Decrypted_Block_Device = crypto_blkdev;
222 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
223 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 Is_Encrypted = true;
225 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800226 Can_Be_Mounted = false;
227 Current_File_System = "emmc";
228 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
230 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
231 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800232 } else {
233 // Filesystem is not encrypted and the mount
234 // succeeded, so get it back to the original
235 // unmounted state
236 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400237 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400238 #ifdef RECOVERY_SDCARD_ON_DATA
239 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
240 Recreate_Media_Folder();
241 #endif
242#else
243 #ifdef RECOVERY_SDCARD_ON_DATA
244 Recreate_Media_Folder();
245 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400246#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 } else if (Mount_Point == "/cache") {
248 Display_Name = "Cache";
249 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400250 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400251 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troyb46a6842012-09-25 11:06:46 -0400252 LOGI("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500253 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
254 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400255 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400257 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 Display_Name = "DataData";
259 Is_SubPartition = true;
260 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 DataManager::SetValue(TW_HAS_DATADATA, 1);
262 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400263 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 Display_Name = "SD-Ext";
265 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400266 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400267 } else if (Mount_Point == "/boot") {
268 Display_Name = "Boot";
269 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400270 }
271#ifdef TW_EXTERNAL_STORAGE_PATH
272 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
273 Is_Storage = true;
274 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400275 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400276 }
277#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 }
287#endif
288#ifdef TW_INTERNAL_STORAGE_PATH
289 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
290 Is_Storage = true;
291 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400292#ifndef RECOVERY_SDCARD_ON_DATA
293 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400294 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400295#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400296 }
297#else
298 if (Mount_Point == "/emmc") {
299 Is_Storage = true;
300 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400301#ifndef RECOVERY_SDCARD_ON_DATA
302 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400303 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400304#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400305 }
306#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400307 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400308 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400309 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 }
311
Dees_Troy51127312012-09-08 13:08:49 -0400312 // Process any custom flags
313 if (Flags.size() > 0)
314 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400315 return true;
316}
317
318bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
319 char flags[MAX_FSTAB_LINE_LENGTH];
320 int flags_len, index = 0;
321 char* ptr;
322
323 strcpy(flags, Flags.c_str());
324 flags_len = Flags.size();
325 for (index = 0; index < flags_len; index++) {
326 if (flags[index] == ';')
327 flags[index] = '\0';
328 }
329
330 index = 0;
331 while (index < flags_len) {
332 while (index < flags_len && flags[index] == '\0')
333 index++;
334 if (index >= flags_len)
335 continue;
336 ptr = flags + index;
337 if (strcmp(ptr, "removable") == 0) {
338 Removable = true;
339 } else if (strcmp(ptr, "storage") == 0) {
340 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400341 } else if (strcmp(ptr, "canbewiped") == 0) {
342 Can_Be_Wiped = true;
343 } else if (strcmp(ptr, "wipeingui") == 0) {
344 Can_Be_Wiped = true;
345 Wipe_Available_in_GUI = true;
346 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
347 Can_Be_Wiped = true;
348 Wipe_Available_in_GUI = true;
349 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400350 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
351 ptr += 13;
352 Is_SubPartition = true;
353 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000354 } else if (strcmp(ptr, "ignoreblkid") == 0) {
355 Ignore_Blkid = true;
Dees_Troy51127312012-09-08 13:08:49 -0400356 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
357 ptr += 8;
358 Symlink_Path = ptr;
359 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
360 ptr += 8;
361 Display_Name = ptr;
362 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
363 ptr += 10;
364 Format_Block_Size = atoi(ptr);
365 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
366 ptr += 7;
367 Length = atoi(ptr);
368 } else {
369 if (Display_Error)
370 LOGE("Unhandled flag: '%s'\n", ptr);
371 else
372 LOGI("Unhandled flag: '%s'\n", ptr);
373 }
374 while (index < flags_len && flags[index] != '\0')
375 index++;
376 }
377 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400378}
379
Dees_Troy5bf43922012-09-07 16:07:55 -0400380bool TWPartition::Is_File_System(string File_System) {
381 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400382 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400383 File_System == "ext4" ||
384 File_System == "vfat" ||
385 File_System == "ntfs" ||
386 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500387 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400388 File_System == "auto")
389 return true;
390 else
391 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400392}
393
Dees_Troy5bf43922012-09-07 16:07:55 -0400394bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400395 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400396 return true;
397 else
398 return false;
399}
400
Dees_Troy51127312012-09-08 13:08:49 -0400401bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400402 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400403 if (mkdir(Path.c_str(), 0777) == -1) {
404 if (Display_Error)
405 LOGE("Can not create '%s' folder.\n", Path.c_str());
406 else
407 LOGI("Can not create '%s' folder.\n", Path.c_str());
408 return false;
409 } else {
410 LOGI("Created '%s' folder.\n", Path.c_str());
411 return true;
412 }
413 }
414 return true;
415}
416
Dees_Troy5bf43922012-09-07 16:07:55 -0400417void TWPartition::Setup_File_System(bool Display_Error) {
418 struct statfs st;
419
420 Can_Be_Mounted = true;
421 Can_Be_Wiped = true;
422
Dees_Troy5bf43922012-09-07 16:07:55 -0400423 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400424 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400425 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
426 Backup_Name = Display_Name;
427 Backup_Method = FILES;
428}
429
430void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400431 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
432 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800433 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400434 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800435 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400436 Backup_Method = FLASH_UTILS;
437 else
Gary Peck82599a82012-11-21 16:23:12 -0800438 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 -0400439 if (Find_Partition_Size()) {
440 Used = Size;
441 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400444 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400445 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400446 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 }
448}
449
Dees_Troye58d5262012-09-21 12:27:57 -0400450void TWPartition::Setup_AndSec(void) {
451 Backup_Name = "and-sec";
452 Has_Android_Secure = true;
453 Symlink_Path = Mount_Point + "/.android_secure";
454 Symlink_Mount_Point = "/and-sec";
455 Backup_Path = Symlink_Mount_Point;
456 Make_Dir("/and-sec", true);
457 Recreate_AndSec_Folder();
458}
459
Dees_Troy5bf43922012-09-07 16:07:55 -0400460void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
461 char device[512], realDevice[512];
462
463 strcpy(device, Block.c_str());
464 memset(realDevice, 0, sizeof(realDevice));
465 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
466 {
467 strcpy(device, realDevice);
468 memset(realDevice, 0, sizeof(realDevice));
469 }
470
471 if (device[0] != '/') {
472 if (Display_Error)
473 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
474 else
475 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
476 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400478 Block = device;
479 return;
480 }
481}
482
Dees_Troy8e337f32012-10-13 22:07:49 -0400483void TWPartition::Mount_Storage_Retry(void) {
484 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500485 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400486 int retry_count = 5;
487 while (retry_count > 0 && !Mount(false)) {
488 usleep(500000);
489 retry_count--;
490 }
491 Mount(true);
492 }
493}
494
Dees_Troy38bd7602012-09-14 13:33:53 -0400495bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
496 FILE *fp = NULL;
497 char line[255];
498
499 fp = fopen("/proc/mtd", "rt");
500 if (fp == NULL) {
501 LOGE("Device does not support /proc/mtd\n");
502 return false;
503 }
504
505 while (fgets(line, sizeof(line), fp) != NULL)
506 {
507 char device[32], label[32];
508 unsigned long size = 0;
509 char* fstype = NULL;
510 int deviceId;
511
512 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
513
514 // Skip header and blank lines
515 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
516 continue;
517
518 // Strip off the trailing " from the label
519 label[strlen(label)-1] = '\0';
520
521 if (strcmp(label, MTD_Name.c_str()) == 0) {
522 // We found our device
523 // Strip off the trailing : from the device
524 device[strlen(device)-1] = '\0';
525 if (sscanf(device,"mtd%d", &deviceId) == 1) {
526 sprintf(device, "/dev/block/mtdblock%d", deviceId);
527 Primary_Block_Device = device;
528 }
529 }
530 }
531 fclose(fp);
532
533 return false;
534}
535
Dees_Troy51127312012-09-08 13:08:49 -0400536bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
537 struct statfs st;
538 string Local_Path = Mount_Point + "/.";
539
540 if (!Mount(Display_Error))
541 return false;
542
543 if (statfs(Local_Path.c_str(), &st) != 0) {
544 if (!Removable) {
545 if (Display_Error)
546 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
547 else
548 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
549 }
550 return false;
551 }
552 Size = (st.f_blocks * st.f_bsize);
553 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
554 Free = (st.f_bfree * st.f_bsize);
555 Backup_Size = Used;
556 return true;
557}
558
559bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400560 FILE* fp;
561 char command[255], line[512];
562 int include_block = 1;
563 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500564 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400565
566 if (!Mount(Display_Error))
567 return false;
568
Dees_Troy38bd7602012-09-14 13:33:53 -0400569 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400570 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500571 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400572 fp = fopen("/tmp/dfoutput.txt", "rt");
573 if (fp == NULL) {
574 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400575 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400576 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400577
578 while (fgets(line, sizeof(line), fp) != NULL)
579 {
580 unsigned long blocks, used, available;
581 char device[64];
582 char tmpString[64];
583
584 if (strncmp(line, "Filesystem", 10) == 0)
585 continue;
586 if (strlen(line) < min_len) {
587 include_block = 0;
588 continue;
589 }
590 if (include_block) {
591 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
592 } else {
593 // The device block string is so long that the df information is on the next line
594 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400595 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400596 while (tmpString[space_count] == 32)
597 space_count++;
598 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
599 }
600
601 // Adjust block size to byte size
602 Size = blocks * 1024ULL;
603 Used = used * 1024ULL;
604 Free = available * 1024ULL;
605 Backup_Size = Used;
606 }
607 fclose(fp);
608 return true;
609}
610
Dees_Troy5bf43922012-09-07 16:07:55 -0400611bool TWPartition::Find_Partition_Size(void) {
612 FILE* fp;
613 char line[512];
614 string tmpdevice;
615
616 // In this case, we'll first get the partitions we care about (with labels)
617 fp = fopen("/proc/partitions", "rt");
618 if (fp == NULL)
619 return false;
620
621 while (fgets(line, sizeof(line), fp) != NULL)
622 {
623 unsigned long major, minor, blocks;
624 char device[512];
625 char tmpString[64];
626
Dees_Troy63c8df72012-09-10 14:02:05 -0400627 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400628 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
629
630 tmpdevice = "/dev/block/";
631 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400632 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400633 // Adjust block size to byte size
634 Size = blocks * 1024ULL;
635 fclose(fp);
636 return true;
637 }
638 }
639 fclose(fp);
640 return false;
641}
642
Dees_Troy5bf43922012-09-07 16:07:55 -0400643bool TWPartition::Is_Mounted(void) {
644 if (!Can_Be_Mounted)
645 return false;
646
647 struct stat st1, st2;
648 string test_path;
649
650 // Check to see if the mount point directory exists
651 test_path = Mount_Point + "/.";
652 if (stat(test_path.c_str(), &st1) != 0) return false;
653
654 // Check to see if the directory above the mount point exists
655 test_path = Mount_Point + "/../.";
656 if (stat(test_path.c_str(), &st2) != 0) return false;
657
658 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
659 int ret = (st1.st_dev != st2.st_dev) ? true : false;
660
661 return ret;
662}
663
664bool TWPartition::Mount(bool Display_Error) {
665 if (Is_Mounted()) {
666 return true;
667 } else if (!Can_Be_Mounted) {
668 return false;
669 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400670
671 Find_Actual_Block_Device();
672
673 // Check the current file system before mounting
674 Check_FS_Type();
Dees_Troy22042032012-12-18 21:23:08 +0000675 if (Fstab_File_System == "yaffs2") {
676 // mount an MTD partition as a YAFFS2 filesystem.
677 mtd_scan_partitions();
678 const MtdPartition* partition;
679 partition = mtd_find_partition_by_name(MTD_Name.c_str());
680 if (partition == NULL) {
681 LOGE("Failed to find '%s' partition to mount at '%s'\n",
682 MTD_Name.c_str(), Mount_Point.c_str());
683 return false;
684 }
685 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
686 if (Display_Error)
687 LOGE("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
688 else
689 LOGI("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
690 return false;
691 } else
692 return true;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000693 } else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500694 string cmd = "/sbin/exfat-fuse " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000695 LOGI("cmd: %s\n", cmd.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500696 string result;
697 if (TWFunc::Exec_Cmd(cmd, result) != 0)
698 return false;
Dees_Troy22042032012-12-18 21:23:08 +0000699 } else if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400700 if (Display_Error)
701 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
702 else
703 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy9350b8d2012-09-27 12:38:38 -0400704 LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400705 return false;
706 } else {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000707#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troyc8bafa12013-01-10 15:43:00 +0000708 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false)) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000709 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
710 if (Display_Error)
711 LOGE("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
712 else
713 LOGI("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
714 } else {
715 LOGI("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
716 }
717 }
718#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400719 if (Removable)
720 Update_Size(Display_Error);
721
722 if (!Symlink_Mount_Point.empty()) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500723 mount(Symlink_Path.c_str(), Symlink_Mount_Point.c_str(), Fstab_File_System.c_str(), NULL, NULL);
Dees_Troy51127312012-09-08 13:08:49 -0400724 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400725 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400726 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400727 return true;
728}
729
730bool TWPartition::UnMount(bool Display_Error) {
731 if (Is_Mounted()) {
732 int never_unmount_system;
733
734 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
735 if (never_unmount_system == 1 && Mount_Point == "/system")
736 return true; // Never unmount system if you're not supposed to unmount it
737
Dees_Troyc8bafa12013-01-10 15:43:00 +0000738#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
739 if (EcryptFS_Password.size() > 0) {
740 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
741 if (Display_Error)
742 LOGE("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
743 else
744 LOGI("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
745 } else {
746 LOGI("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
747 }
748 }
749#endif
750
Dees_Troy38bd7602012-09-14 13:33:53 -0400751 if (!Symlink_Mount_Point.empty())
752 umount(Symlink_Mount_Point.c_str());
753
Dees_Troy5bf43922012-09-07 16:07:55 -0400754 if (umount(Mount_Point.c_str()) != 0) {
755 if (Display_Error)
756 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
757 else
758 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
759 return false;
760 } else
761 return true;
762 } else {
763 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 }
765}
766
Gary Peck43acadf2012-11-21 21:19:01 -0800767bool TWPartition::Wipe(string New_File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400768 if (!Can_Be_Wiped) {
769 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
770 return false;
771 }
772
Dees_Troyc51f1f92012-09-20 15:32:13 -0400773 if (Mount_Point == "/cache")
774 tmplog_offset = 0;
775
Dees_Troyce675462013-01-09 19:48:21 +0000776#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
777 if (Mount_Point == "/data" && Mount(false)) {
778 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
779 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
780 }
781#endif
782
Dees_Troy38bd7602012-09-14 13:33:53 -0400783 if (Has_Data_Media)
784 return Wipe_Data_Without_Wiping_Media();
785
786 int check;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800787 bool wiped = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400788 DataManager::GetValue(TW_RM_RF_VAR, check);
Gary Pecke8bc5d72012-12-21 06:45:25 -0800789
Dees_Troy38bd7602012-09-14 13:33:53 -0400790 if (check)
Gary Pecke8bc5d72012-12-21 06:45:25 -0800791 wiped = Wipe_RMRF();
792 else if (New_File_System == "ext4")
793 wiped = Wipe_EXT4();
794 else if (New_File_System == "ext2" || New_File_System == "ext3")
795 wiped = Wipe_EXT23(New_File_System);
796 else if (New_File_System == "vfat")
797 wiped = Wipe_FAT();
Dees_Troyce675462013-01-09 19:48:21 +0000798 else if (New_File_System == "exfat")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500799 wiped = Wipe_EXFAT();
Gary Pecke8bc5d72012-12-21 06:45:25 -0800800 else if (New_File_System == "yaffs2")
801 wiped = Wipe_MTD();
802 else {
803 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
804 return false;
805 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400806
Gary Pecke8bc5d72012-12-21 06:45:25 -0800807 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000808#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
809 if (Mount_Point == "/data" && Mount(false)) {
810 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
811 Make_Dir("/data/system", true);
812 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
813 }
814 }
815#endif
Gary Pecke8bc5d72012-12-21 06:45:25 -0800816 Setup_File_System(false);
817 if (Is_Encrypted && !Is_Decrypted) {
818 // just wiped an encrypted partition back to its unencrypted state
819 Is_Encrypted = false;
820 Is_Decrypted = false;
821 Decrypted_Block_Device = "";
822 if (Mount_Point == "/data") {
823 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
824 DataManager::SetValue(TW_IS_DECRYPTED, 0);
825 }
826 }
827 }
828 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400829}
830
Gary Peck43acadf2012-11-21 21:19:01 -0800831bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800832 if (Is_File_System(Current_File_System))
833 return Wipe(Current_File_System);
834 else
835 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -0800836}
837
Dees_Troye58d5262012-09-21 12:27:57 -0400838bool TWPartition::Wipe_AndSec(void) {
839 if (!Has_Android_Secure)
840 return false;
841
Dees_Troye58d5262012-09-21 12:27:57 -0400842 if (!Mount(true))
843 return false;
844
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500845 ui_print("Wiping .android_secure\n");
846 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -0400847 return true;
848}
849
Dees_Troy51a0e822012-09-05 15:24:24 -0400850bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400851 if (Backup_Method == FILES)
852 return Backup_Tar(backup_folder);
853 else if (Backup_Method == DD)
854 return Backup_DD(backup_folder);
855 else if (Backup_Method == FLASH_UTILS)
856 return Backup_Dump_Image(backup_folder);
857 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
858 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400859}
860
Dees_Troy43d8b002012-09-17 16:00:01 -0400861bool TWPartition::Check_MD5(string restore_folder) {
862 string Full_Filename;
863 char split_filename[512];
864 int index = 0;
865
866 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400867 if (!TWFunc::Path_Exists(Full_Filename)) {
868 // This is a split archive, we presume
869 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
870 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
871 if (TWFunc::Check_MD5(split_filename) == 0) {
872 LOGE("MD5 failed to match on '%s'.\n", split_filename);
873 return false;
874 }
875 index++;
876 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400877 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400878 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400879 } else {
880 // Single file archive
881 if (TWFunc::Check_MD5(Full_Filename) == 0) {
882 LOGE("MD5 failed to match on '%s'.\n", split_filename);
883 return false;
884 } else
885 return true;
886 }
887 return false;
888}
889
Dees_Troy51a0e822012-09-05 15:24:24 -0400890bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -0800891 size_t first_period, second_period;
892 string Restore_File_System;
893
894 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
895 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
896
897 // Parse backup filename to extract the file system before wiping
898 first_period = Backup_FileName.find(".");
899 if (first_period == string::npos) {
900 LOGE("Unable to find file system (first period).\n");
901 return false;
902 }
903 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
904 second_period = Restore_File_System.find(".");
905 if (second_period == string::npos) {
906 LOGE("Unable to find file system (second period).\n");
907 return false;
908 }
909 Restore_File_System.resize(second_period);
910 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
911
912 if (Is_File_System(Restore_File_System))
913 return Restore_Tar(restore_folder, Restore_File_System);
914 else if (Is_Image(Restore_File_System)) {
915 if (Restore_File_System == "emmc")
916 return Restore_DD(restore_folder);
917 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
918 return Restore_Flash_Image(restore_folder);
919 }
920
Dees_Troy38bd7602012-09-14 13:33:53 -0400921 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
922 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400923}
924
925string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400926 if (Backup_Method == NONE)
927 return "none";
928 else if (Backup_Method == FILES)
929 return "files";
930 else if (Backup_Method == DD)
931 return "dd";
932 else if (Backup_Method == FLASH_UTILS)
933 return "flash_utils";
934 else
935 return "undefined";
936 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400937}
938
939bool TWPartition::Decrypt(string Password) {
940 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400941 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400942 return 1;
943}
944
945bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400946 bool Save_Data_Media = Has_Data_Media;
947
948 if (!UnMount(true))
949 return false;
950
Dees_Troy38bd7602012-09-14 13:33:53 -0400951 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800952 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400953 Has_Data_Media = Save_Data_Media;
954 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
955 Recreate_Media_Folder();
956 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400957 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400958 return true;
959 } else {
960 Has_Data_Media = Save_Data_Media;
961 LOGE("Unable to format to remove encryption.\n");
962 return false;
963 }
964 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400965}
966
967void TWPartition::Check_FS_Type() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500968 string blkCommand, result;
969 string blkOutput;
Dees_Troy5bf43922012-09-07 16:07:55 -0400970 char* blk;
971 char* arg;
972 char* ptr;
973
Dees_Troy68cab492012-12-12 19:29:35 +0000974 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
975 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -0400976
Dees_Troy38bd7602012-09-14 13:33:53 -0400977 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400978 if (!Is_Present)
979 return;
Dees_Troy51127312012-09-08 13:08:49 -0400980
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500981 blkCommand = "blkid " + Actual_Block_Device;
982 TWFunc::Exec_Cmd(blkCommand, result);
983 std::stringstream line(result);
984 while (getline(line, blkOutput))
Dees_Troy5bf43922012-09-07 16:07:55 -0400985 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500986 blk = (char*) blkOutput.c_str();
987 ptr = (char*) blkOutput.c_str();
Dees_Troy63c8df72012-09-10 14:02:05 -0400988 while (*ptr > 32 && *ptr != ':') ptr++;
989 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400990 *ptr = 0;
991
992 // Increment by two, but verify that we don't hit a NULL
993 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400994 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400995
996 // Now, find the TYPE field
997 while (1)
998 {
999 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -04001000 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -04001001 if (*ptr != 0)
1002 {
1003 *ptr = 0;
1004 ptr++;
1005 }
1006
1007 if (strlen(arg) > 6)
1008 {
1009 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
1010 }
1011
1012 if (*ptr == 0)
1013 {
1014 arg = NULL;
1015 break;
1016 }
1017 }
1018
1019 if (arg && strlen(arg) > 7)
1020 {
1021 arg += 6; // Skip the TYPE=" portion
1022 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
1023 }
1024 else
1025 continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001026 if (Current_File_System != arg) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001027 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
1028 Current_File_System = arg;
1029 }
1030 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001031 return;
1032}
1033
Gary Peck43acadf2012-11-21 21:19:01 -08001034bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001035 if (!UnMount(true))
1036 return false;
1037
Dees_Troy43d8b002012-09-17 16:00:01 -04001038 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001039 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001040
1041 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
1042 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001043 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
1044 LOGI("mke2fs command: %s\n", command.c_str());
1045 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001046 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001047 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001048 ui_print("Done.\n");
1049 return true;
1050 } else {
1051 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1052 return false;
1053 }
1054 } else
1055 return Wipe_RMRF();
1056
1057 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001058}
1059
1060bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001061 if (!UnMount(true))
1062 return false;
1063
Dees_Troy43d8b002012-09-17 16:00:01 -04001064 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001065 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001066
1067 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
1068 Find_Actual_Block_Device();
1069 Command = "make_ext4fs";
1070 if (!Is_Decrypted && Length != 0) {
1071 // Only use length if we're not decrypted
1072 char len[32];
1073 sprintf(len, "%i", Length);
1074 Command += " -l ";
1075 Command += len;
1076 }
1077 Command += " " + Actual_Block_Device;
1078 LOGI("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001079 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001080 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001081 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001082 ui_print("Done.\n");
1083 return true;
1084 } else {
1085 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1086 return false;
1087 }
1088 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001089 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001090
1091 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001092}
1093
1094bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001095 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001096
Dees_Troy43d8b002012-09-17 16:00:01 -04001097 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001098 if (!UnMount(true))
1099 return false;
1100
1101 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1102 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001103 command = "mkdosfs " + Actual_Block_Device;
1104 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001105 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001106 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001107 ui_print("Done.\n");
1108 return true;
1109 } else {
1110 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1111 return false;
1112 }
1113 return true;
1114 }
1115 else
1116 return Wipe_RMRF();
1117
1118 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001119}
1120
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001121bool TWPartition::Wipe_EXFAT() {
1122 string command, result;
1123
1124 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1125 if (!UnMount(true))
1126 return false;
1127
1128 ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
1129 Find_Actual_Block_Device();
1130 command = "mkexfatfs " + Actual_Block_Device;
1131 if (TWFunc::Exec_Cmd(command, result) == 0) {
1132 Recreate_AndSec_Folder();
1133 ui_print("Done.\n");
1134 return true;
1135 } else {
1136 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1137 return false;
1138 }
1139 return true;
1140 }
1141 return false;
1142}
1143
Dees_Troy38bd7602012-09-14 13:33:53 -04001144bool TWPartition::Wipe_MTD() {
1145 if (!UnMount(true))
1146 return false;
1147
1148 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1149
1150 mtd_scan_partitions();
1151 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1152 if (mtd == NULL) {
1153 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1154 return false;
1155 }
1156
1157 MtdWriteContext* ctx = mtd_write_partition(mtd);
1158 if (ctx == NULL) {
1159 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1160 return false;
1161 }
1162 if (mtd_erase_blocks(ctx, -1) == -1) {
1163 mtd_write_close(ctx);
1164 LOGE("Failed to format '%s'", MTD_Name.c_str());
1165 return false;
1166 }
1167 if (mtd_write_close(ctx) != 0) {
1168 LOGE("Failed to close '%s'", MTD_Name.c_str());
1169 return false;
1170 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001171 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001172 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001173 ui_print("Done.\n");
1174 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001175}
1176
1177bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001178 if (!Mount(true))
1179 return false;
1180
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001181 ui_print("Removing all files under '%s'\n", Mount_Point.c_str());
1182 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001183 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001184 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001185}
1186
1187bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001188 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001189
1190 // This handles wiping data on devices with "sdcard" in /data/media
1191 if (!Mount(true))
1192 return false;
1193
1194 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001195
1196 DIR* d;
1197 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001198 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001199 struct dirent* de;
1200 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001201 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1202 // The media folder is the "internal sdcard"
1203 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1204 // the media folder for multi-user.
1205 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001206
1207 dir = "/data/";
1208 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001209 if (de->d_type == DT_DIR) {
1210 TWFunc::removeDir(dir, false);
1211 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
1212 if (!unlink(dir.c_str()))
1213 LOGI("Unable to unlink '%s'\n", dir.c_str());
1214 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001215 }
1216 closedir(d);
Dees_Troy16b74352012-11-14 22:27:31 +00001217 ui_print("Done.\n");
Dees_Troyce675462013-01-09 19:48:21 +00001218#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1219 if (Mount_Point == "/data" && Mount(false)) {
1220 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1221 Make_Dir("/data/system", true);
1222 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1223 }
1224 }
1225#endif
Dees_Troy16b74352012-11-14 22:27:31 +00001226 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001227 }
Dees_Troy16b74352012-11-14 22:27:31 +00001228 ui_print("Dirent failed to open /data, error!\n");
1229 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001230}
1231
1232bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001233 char back_name[255], split_index[5];
1234 string Full_FileName, Split_FileName, Tar_Args, Command;
1235 int use_compression, index, backup_count;
1236 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001237 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001238 twrpTar tar;
1239 vector <string> files;
Dees_Troy43d8b002012-09-17 16:00:01 -04001240
1241 if (!Mount(true))
1242 return false;
1243
Dees_Troy2c50e182012-09-26 20:05:28 -04001244 if (Backup_Path == "/and-sec") {
1245 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1246 ui_print("Backing up %s...\n", "Android Secure");
1247 } else {
1248 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1249 ui_print("Backing up %s...\n", Display_Name.c_str());
1250 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001251
1252 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001253
1254 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1255 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001256 Full_FileName = backup_folder + "/" + Backup_FileName;
1257 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001258 // This backup needs to be split into multiple archives
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001259 ui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001260 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001261 backup_count = tar.Split_Archive(back_name, Full_FileName);
1262 if (backup_count == -1) {
1263 LOGE("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001264 return false;
1265 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001266 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001267 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001268 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001269 if (use_compression) {
1270 if (tar.createTGZ(Backup_Path, Full_FileName) != 0)
1271 return false;
1272 string gzname = Full_FileName + ".gz";
1273 rename(gzname.c_str(), Full_FileName.c_str());
1274 }
1275 else {
1276 if (tar.create(Backup_Path, Full_FileName) != 0)
1277 return false;
1278 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001279 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1280 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1281 return false;
1282 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001283 }
1284 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001285}
1286
1287bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001288 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001289 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001290 int use_compression;
1291
Dees_Troyb46a6842012-09-25 11:06:46 -04001292 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001293 ui_print("Backing up %s...\n", Display_Name.c_str());
1294
1295 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1296 Backup_FileName = back_name;
1297
1298 Full_FileName = backup_folder + "/" + Backup_FileName;
1299
1300 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1301 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001302 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001303 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1304 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001305 return false;
1306 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001307 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001308}
1309
1310bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001311 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001312 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001313 int use_compression;
1314
Dees_Troyb46a6842012-09-25 11:06:46 -04001315 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001316 ui_print("Backing up %s...\n", Display_Name.c_str());
1317
1318 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1319 Backup_FileName = back_name;
1320
1321 Full_FileName = backup_folder + "/" + Backup_FileName;
1322
1323 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1324 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001325 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001326 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1327 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1328 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1329 return false;
1330 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001331 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001332}
1333
Gary Peck43acadf2012-11-21 21:19:01 -08001334bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1335 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001336 int index = 0;
1337 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001338
Dees_Troye58d5262012-09-21 12:27:57 -04001339 if (Has_Android_Secure) {
1340 ui_print("Wiping android secure...\n");
1341 if (!Wipe_AndSec())
1342 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001343 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001344 ui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001345 if (!Wipe(Restore_File_System))
1346 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001347 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001348
1349 if (!Mount(true))
1350 return false;
1351
Dees_Troyda8b55a2012-12-12 19:18:30 +00001352 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001353 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001354 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001355 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001356 if (!TWFunc::Path_Exists(Full_FileName)) {
1357 // Backup is multiple archives
1358 LOGI("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001359 sprintf(split_index, "%03i", index);
1360 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001361 while (TWFunc::Path_Exists(Full_FileName)) {
1362 index++;
1363 ui_print("Restoring archive %i...\n", index);
1364 LOGI("Restoring '%s'...\n", Full_FileName.c_str());
1365 twrpTar tar;
1366 if (tar.extract("/", Full_FileName) != 0)
1367 return false;
1368 sprintf(split_index, "%03i", index);
1369 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1370 }
1371 if (index == 0) {
1372 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1373 return false;
1374 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001375 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001376 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001377 twrpTar tar;
1378 if (tar.extract(Backup_Path, Full_FileName) != 0)
1379 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001380 }
1381 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001382}
1383
1384bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001385 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001386
Dees_Troyda8b55a2012-12-12 19:18:30 +00001387 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001388 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001389
1390 if (!Find_Partition_Size()) {
1391 LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
1392 return false;
1393 }
1394 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1395 if (backup_size > Size) {
1396 LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
1397 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1398 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1399 return false;
1400 }
1401
1402 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001403 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1404 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001405 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001406 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001407}
1408
1409bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001410 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001411
1412 ui_print("Restoring %s...\n", Display_Name.c_str());
1413 Full_FileName = restore_folder + "/" + Backup_FileName;
1414 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1415 Command = "erase_image " + MTD_Name;
1416 LOGI("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001417 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001418 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1419 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001420 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001421 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001422}
Dees_Troy5bf43922012-09-07 16:07:55 -04001423
1424bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001425 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001426
Dees_Troyab10ee22012-09-21 14:27:30 -04001427 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001428 return false;
1429
Dees_Troy0550cfb2012-10-13 11:56:13 -04001430 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001431 if (Removable || Is_Encrypted) {
1432 if (!Mount(false))
1433 return true;
1434 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001435 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001436
1437 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001438 if (!ret || Size == 0) {
1439 if (!Get_Size_Via_df(Display_Error)) {
1440 if (!Was_Already_Mounted)
1441 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001442 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001443 }
1444 }
Dees_Troy51127312012-09-08 13:08:49 -04001445
Dees_Troy5bf43922012-09-07 16:07:55 -04001446 if (Has_Data_Media) {
1447 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001448 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001449 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1450 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001451 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001452 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001453 int bak = (int)(Backup_Size / 1048576LLU);
1454 int total = (int)(Size / 1048576LLU);
1455 int us = (int)(Used / 1048576LLU);
1456 int fre = (int)(Free / 1048576LLU);
1457 int datmed = (int)(data_media_used / 1048576LLU);
1458 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 -04001459 } else {
1460 if (!Was_Already_Mounted)
1461 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001462 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001463 }
Dees_Troye58d5262012-09-21 12:27:57 -04001464 } else if (Has_Android_Secure) {
1465 if (Mount(Display_Error))
1466 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001467 else {
1468 if (!Was_Already_Mounted)
1469 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001470 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001471 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001472 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001473 if (!Was_Already_Mounted)
1474 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001475 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001476}
Dees_Troy38bd7602012-09-14 13:33:53 -04001477
1478void TWPartition::Find_Actual_Block_Device(void) {
1479 if (Is_Decrypted) {
1480 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001481 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001482 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001483 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001484 Is_Present = true;
1485 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001486 return;
1487 }
1488 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001489 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001490 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001491 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001492 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001493 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001494 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001495}
1496
1497void TWPartition::Recreate_Media_Folder(void) {
1498 string Command;
1499
1500 if (!Mount(true)) {
1501 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001502 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001503 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001504 LOGI("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001505 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1506 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001507 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001508}
Dees_Troye58d5262012-09-21 12:27:57 -04001509
1510void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001511 if (!Has_Android_Secure)
1512 return;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001513 LOGI("Creating .android_secure: %s\n", Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001514 if (!Mount(true)) {
1515 LOGE("Unable to recreate android secure folder.\n");
1516 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1517 LOGI("Recreating android secure folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001518 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1519 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1520 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001521 }
1522}