blob: 44b00ab4c3709470ee71cc4634be346485b02d51 [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
708 if (EcryptFS_Password.size() > 0) {
709 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_Troy38bd7602012-09-14 13:33:53 -0400738 if (!Symlink_Mount_Point.empty())
739 umount(Symlink_Mount_Point.c_str());
740
Dees_Troy5bf43922012-09-07 16:07:55 -0400741 if (umount(Mount_Point.c_str()) != 0) {
742 if (Display_Error)
743 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
744 else
745 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
746 return false;
747 } else
748 return true;
749 } else {
750 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400751 }
752}
753
Gary Peck43acadf2012-11-21 21:19:01 -0800754bool TWPartition::Wipe(string New_File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400755 if (!Can_Be_Wiped) {
756 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
757 return false;
758 }
759
Dees_Troyc51f1f92012-09-20 15:32:13 -0400760 if (Mount_Point == "/cache")
761 tmplog_offset = 0;
762
Dees_Troyce675462013-01-09 19:48:21 +0000763#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
764 if (Mount_Point == "/data" && Mount(false)) {
765 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
766 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
767 }
768#endif
769
Dees_Troy38bd7602012-09-14 13:33:53 -0400770 if (Has_Data_Media)
771 return Wipe_Data_Without_Wiping_Media();
772
773 int check;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800774 bool wiped = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400775 DataManager::GetValue(TW_RM_RF_VAR, check);
Gary Pecke8bc5d72012-12-21 06:45:25 -0800776
Dees_Troy38bd7602012-09-14 13:33:53 -0400777 if (check)
Gary Pecke8bc5d72012-12-21 06:45:25 -0800778 wiped = Wipe_RMRF();
779 else if (New_File_System == "ext4")
780 wiped = Wipe_EXT4();
781 else if (New_File_System == "ext2" || New_File_System == "ext3")
782 wiped = Wipe_EXT23(New_File_System);
783 else if (New_File_System == "vfat")
784 wiped = Wipe_FAT();
Dees_Troyce675462013-01-09 19:48:21 +0000785 else if (New_File_System == "exfat")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500786 wiped = Wipe_EXFAT();
Gary Pecke8bc5d72012-12-21 06:45:25 -0800787 else if (New_File_System == "yaffs2")
788 wiped = Wipe_MTD();
789 else {
790 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
791 return false;
792 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400793
Gary Pecke8bc5d72012-12-21 06:45:25 -0800794 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000795#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
796 if (Mount_Point == "/data" && Mount(false)) {
797 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
798 Make_Dir("/data/system", true);
799 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
800 }
801 }
802#endif
Gary Pecke8bc5d72012-12-21 06:45:25 -0800803 Setup_File_System(false);
804 if (Is_Encrypted && !Is_Decrypted) {
805 // just wiped an encrypted partition back to its unencrypted state
806 Is_Encrypted = false;
807 Is_Decrypted = false;
808 Decrypted_Block_Device = "";
809 if (Mount_Point == "/data") {
810 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
811 DataManager::SetValue(TW_IS_DECRYPTED, 0);
812 }
813 }
814 }
815 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400816}
817
Gary Peck43acadf2012-11-21 21:19:01 -0800818bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800819 if (Is_File_System(Current_File_System))
820 return Wipe(Current_File_System);
821 else
822 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -0800823}
824
Dees_Troye58d5262012-09-21 12:27:57 -0400825bool TWPartition::Wipe_AndSec(void) {
826 if (!Has_Android_Secure)
827 return false;
828
Dees_Troye58d5262012-09-21 12:27:57 -0400829 if (!Mount(true))
830 return false;
831
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500832 ui_print("Wiping .android_secure\n");
833 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -0400834 return true;
835}
836
Dees_Troy51a0e822012-09-05 15:24:24 -0400837bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400838 if (Backup_Method == FILES)
839 return Backup_Tar(backup_folder);
840 else if (Backup_Method == DD)
841 return Backup_DD(backup_folder);
842 else if (Backup_Method == FLASH_UTILS)
843 return Backup_Dump_Image(backup_folder);
844 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
845 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400846}
847
Dees_Troy43d8b002012-09-17 16:00:01 -0400848bool TWPartition::Check_MD5(string restore_folder) {
849 string Full_Filename;
850 char split_filename[512];
851 int index = 0;
852
853 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400854 if (!TWFunc::Path_Exists(Full_Filename)) {
855 // This is a split archive, we presume
856 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
857 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
858 if (TWFunc::Check_MD5(split_filename) == 0) {
859 LOGE("MD5 failed to match on '%s'.\n", split_filename);
860 return false;
861 }
862 index++;
863 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400864 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400865 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400866 } else {
867 // Single file archive
868 if (TWFunc::Check_MD5(Full_Filename) == 0) {
869 LOGE("MD5 failed to match on '%s'.\n", split_filename);
870 return false;
871 } else
872 return true;
873 }
874 return false;
875}
876
Dees_Troy51a0e822012-09-05 15:24:24 -0400877bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -0800878 size_t first_period, second_period;
879 string Restore_File_System;
880
881 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
882 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
883
884 // Parse backup filename to extract the file system before wiping
885 first_period = Backup_FileName.find(".");
886 if (first_period == string::npos) {
887 LOGE("Unable to find file system (first period).\n");
888 return false;
889 }
890 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
891 second_period = Restore_File_System.find(".");
892 if (second_period == string::npos) {
893 LOGE("Unable to find file system (second period).\n");
894 return false;
895 }
896 Restore_File_System.resize(second_period);
897 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
898
899 if (Is_File_System(Restore_File_System))
900 return Restore_Tar(restore_folder, Restore_File_System);
901 else if (Is_Image(Restore_File_System)) {
902 if (Restore_File_System == "emmc")
903 return Restore_DD(restore_folder);
904 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
905 return Restore_Flash_Image(restore_folder);
906 }
907
Dees_Troy38bd7602012-09-14 13:33:53 -0400908 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
909 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400910}
911
912string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400913 if (Backup_Method == NONE)
914 return "none";
915 else if (Backup_Method == FILES)
916 return "files";
917 else if (Backup_Method == DD)
918 return "dd";
919 else if (Backup_Method == FLASH_UTILS)
920 return "flash_utils";
921 else
922 return "undefined";
923 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400924}
925
926bool TWPartition::Decrypt(string Password) {
927 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400928 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400929 return 1;
930}
931
932bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400933 bool Save_Data_Media = Has_Data_Media;
934
935 if (!UnMount(true))
936 return false;
937
Dees_Troy38bd7602012-09-14 13:33:53 -0400938 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800939 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400940 Has_Data_Media = Save_Data_Media;
941 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
942 Recreate_Media_Folder();
943 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400944 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400945 return true;
946 } else {
947 Has_Data_Media = Save_Data_Media;
948 LOGE("Unable to format to remove encryption.\n");
949 return false;
950 }
951 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400952}
953
954void TWPartition::Check_FS_Type() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500955 string blkCommand, result;
956 string blkOutput;
Dees_Troy5bf43922012-09-07 16:07:55 -0400957 char* blk;
958 char* arg;
959 char* ptr;
960
Dees_Troy68cab492012-12-12 19:29:35 +0000961 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
962 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -0400963
Dees_Troy38bd7602012-09-14 13:33:53 -0400964 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400965 if (!Is_Present)
966 return;
Dees_Troy51127312012-09-08 13:08:49 -0400967
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500968 blkCommand = "blkid " + Actual_Block_Device;
969 TWFunc::Exec_Cmd(blkCommand, result);
970 std::stringstream line(result);
971 while (getline(line, blkOutput))
Dees_Troy5bf43922012-09-07 16:07:55 -0400972 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500973 blk = (char*) blkOutput.c_str();
974 ptr = (char*) blkOutput.c_str();
Dees_Troy63c8df72012-09-10 14:02:05 -0400975 while (*ptr > 32 && *ptr != ':') ptr++;
976 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400977 *ptr = 0;
978
979 // Increment by two, but verify that we don't hit a NULL
980 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400981 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400982
983 // Now, find the TYPE field
984 while (1)
985 {
986 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400987 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400988 if (*ptr != 0)
989 {
990 *ptr = 0;
991 ptr++;
992 }
993
994 if (strlen(arg) > 6)
995 {
996 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
997 }
998
999 if (*ptr == 0)
1000 {
1001 arg = NULL;
1002 break;
1003 }
1004 }
1005
1006 if (arg && strlen(arg) > 7)
1007 {
1008 arg += 6; // Skip the TYPE=" portion
1009 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
1010 }
1011 else
1012 continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001013 if (Current_File_System != arg) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001014 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
1015 Current_File_System = arg;
1016 }
1017 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001018 return;
1019}
1020
Gary Peck43acadf2012-11-21 21:19:01 -08001021bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001022 if (!UnMount(true))
1023 return false;
1024
Dees_Troy43d8b002012-09-17 16:00:01 -04001025 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001026 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001027
1028 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
1029 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001030 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
1031 LOGI("mke2fs command: %s\n", command.c_str());
1032 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001033 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001034 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001035 ui_print("Done.\n");
1036 return true;
1037 } else {
1038 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1039 return false;
1040 }
1041 } else
1042 return Wipe_RMRF();
1043
1044 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001045}
1046
1047bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001048 if (!UnMount(true))
1049 return false;
1050
Dees_Troy43d8b002012-09-17 16:00:01 -04001051 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001052 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001053
1054 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
1055 Find_Actual_Block_Device();
1056 Command = "make_ext4fs";
1057 if (!Is_Decrypted && Length != 0) {
1058 // Only use length if we're not decrypted
1059 char len[32];
1060 sprintf(len, "%i", Length);
1061 Command += " -l ";
1062 Command += len;
1063 }
1064 Command += " " + Actual_Block_Device;
1065 LOGI("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001066 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001067 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001068 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001069 ui_print("Done.\n");
1070 return true;
1071 } else {
1072 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1073 return false;
1074 }
1075 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001076 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001077
1078 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001079}
1080
1081bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001082 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001083
Dees_Troy43d8b002012-09-17 16:00:01 -04001084 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001085 if (!UnMount(true))
1086 return false;
1087
1088 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1089 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001090 command = "mkdosfs " + Actual_Block_Device;
1091 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001092 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001093 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001094 ui_print("Done.\n");
1095 return true;
1096 } else {
1097 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1098 return false;
1099 }
1100 return true;
1101 }
1102 else
1103 return Wipe_RMRF();
1104
1105 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001106}
1107
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001108bool TWPartition::Wipe_EXFAT() {
1109 string command, result;
1110
1111 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1112 if (!UnMount(true))
1113 return false;
1114
1115 ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
1116 Find_Actual_Block_Device();
1117 command = "mkexfatfs " + Actual_Block_Device;
1118 if (TWFunc::Exec_Cmd(command, result) == 0) {
1119 Recreate_AndSec_Folder();
1120 ui_print("Done.\n");
1121 return true;
1122 } else {
1123 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1124 return false;
1125 }
1126 return true;
1127 }
1128 return false;
1129}
1130
Dees_Troy38bd7602012-09-14 13:33:53 -04001131bool TWPartition::Wipe_MTD() {
1132 if (!UnMount(true))
1133 return false;
1134
1135 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1136
1137 mtd_scan_partitions();
1138 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1139 if (mtd == NULL) {
1140 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1141 return false;
1142 }
1143
1144 MtdWriteContext* ctx = mtd_write_partition(mtd);
1145 if (ctx == NULL) {
1146 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1147 return false;
1148 }
1149 if (mtd_erase_blocks(ctx, -1) == -1) {
1150 mtd_write_close(ctx);
1151 LOGE("Failed to format '%s'", MTD_Name.c_str());
1152 return false;
1153 }
1154 if (mtd_write_close(ctx) != 0) {
1155 LOGE("Failed to close '%s'", MTD_Name.c_str());
1156 return false;
1157 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001158 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001159 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001160 ui_print("Done.\n");
1161 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001162}
1163
1164bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001165 if (!Mount(true))
1166 return false;
1167
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001168 ui_print("Removing all files under '%s'\n", Mount_Point.c_str());
1169 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001170 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001171 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001172}
1173
1174bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001175 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001176
1177 // This handles wiping data on devices with "sdcard" in /data/media
1178 if (!Mount(true))
1179 return false;
1180
1181 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001182
1183 DIR* d;
1184 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001185 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001186 struct dirent* de;
1187 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001188 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1189 // The media folder is the "internal sdcard"
1190 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1191 // the media folder for multi-user.
1192 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001193
1194 dir = "/data/";
1195 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001196 if (de->d_type == DT_DIR) {
1197 TWFunc::removeDir(dir, false);
1198 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
1199 if (!unlink(dir.c_str()))
1200 LOGI("Unable to unlink '%s'\n", dir.c_str());
1201 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001202 }
1203 closedir(d);
Dees_Troy16b74352012-11-14 22:27:31 +00001204 ui_print("Done.\n");
Dees_Troyce675462013-01-09 19:48:21 +00001205#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1206 if (Mount_Point == "/data" && Mount(false)) {
1207 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1208 Make_Dir("/data/system", true);
1209 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1210 }
1211 }
1212#endif
Dees_Troy16b74352012-11-14 22:27:31 +00001213 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001214 }
Dees_Troy16b74352012-11-14 22:27:31 +00001215 ui_print("Dirent failed to open /data, error!\n");
1216 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001217}
1218
1219bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001220 char back_name[255], split_index[5];
1221 string Full_FileName, Split_FileName, Tar_Args, Command;
1222 int use_compression, index, backup_count;
1223 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001224 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001225 twrpTar tar;
1226 vector <string> files;
Dees_Troy43d8b002012-09-17 16:00:01 -04001227
1228 if (!Mount(true))
1229 return false;
1230
Dees_Troy2c50e182012-09-26 20:05:28 -04001231 if (Backup_Path == "/and-sec") {
1232 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1233 ui_print("Backing up %s...\n", "Android Secure");
1234 } else {
1235 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1236 ui_print("Backing up %s...\n", Display_Name.c_str());
1237 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001238
1239 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001240
1241 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1242 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001243 Full_FileName = backup_folder + "/" + Backup_FileName;
1244 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001245 // This backup needs to be split into multiple archives
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001246 ui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001247 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001248 backup_count = tar.Split_Archive(back_name, Full_FileName);
1249 if (backup_count == -1) {
1250 LOGE("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001251 return false;
1252 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001253 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001254 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001255 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001256 if (use_compression) {
1257 if (tar.createTGZ(Backup_Path, Full_FileName) != 0)
1258 return false;
1259 string gzname = Full_FileName + ".gz";
1260 rename(gzname.c_str(), Full_FileName.c_str());
1261 }
1262 else {
1263 if (tar.create(Backup_Path, Full_FileName) != 0)
1264 return false;
1265 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001266 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1267 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1268 return false;
1269 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001270 }
1271 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001272}
1273
1274bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001275 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001276 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001277 int use_compression;
1278
Dees_Troyb46a6842012-09-25 11:06:46 -04001279 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001280 ui_print("Backing up %s...\n", Display_Name.c_str());
1281
1282 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1283 Backup_FileName = back_name;
1284
1285 Full_FileName = backup_folder + "/" + Backup_FileName;
1286
1287 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1288 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001289 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001290 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1291 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001292 return false;
1293 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001294 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001295}
1296
1297bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001298 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001299 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001300 int use_compression;
1301
Dees_Troyb46a6842012-09-25 11:06:46 -04001302 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001303 ui_print("Backing up %s...\n", Display_Name.c_str());
1304
1305 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1306 Backup_FileName = back_name;
1307
1308 Full_FileName = backup_folder + "/" + Backup_FileName;
1309
1310 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1311 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001312 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001313 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1314 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1315 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1316 return false;
1317 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001318 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001319}
1320
Gary Peck43acadf2012-11-21 21:19:01 -08001321bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1322 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001323 int index = 0;
1324 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001325
Dees_Troye58d5262012-09-21 12:27:57 -04001326 if (Has_Android_Secure) {
1327 ui_print("Wiping android secure...\n");
1328 if (!Wipe_AndSec())
1329 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001330 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001331 ui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001332 if (!Wipe(Restore_File_System))
1333 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001334 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001335
1336 if (!Mount(true))
1337 return false;
1338
Dees_Troyda8b55a2012-12-12 19:18:30 +00001339 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001340 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001341 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001342 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001343 if (!TWFunc::Path_Exists(Full_FileName)) {
1344 // Backup is multiple archives
1345 LOGI("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001346 sprintf(split_index, "%03i", index);
1347 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001348 while (TWFunc::Path_Exists(Full_FileName)) {
1349 index++;
1350 ui_print("Restoring archive %i...\n", index);
1351 LOGI("Restoring '%s'...\n", Full_FileName.c_str());
1352 twrpTar tar;
1353 if (tar.extract("/", Full_FileName) != 0)
1354 return false;
1355 sprintf(split_index, "%03i", index);
1356 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1357 }
1358 if (index == 0) {
1359 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1360 return false;
1361 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001362 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001363 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001364 twrpTar tar;
1365 if (tar.extract(Backup_Path, Full_FileName) != 0)
1366 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001367 }
1368 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001369}
1370
1371bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001372 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001373
Dees_Troyda8b55a2012-12-12 19:18:30 +00001374 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001375 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001376
1377 if (!Find_Partition_Size()) {
1378 LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
1379 return false;
1380 }
1381 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1382 if (backup_size > Size) {
1383 LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
1384 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1385 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1386 return false;
1387 }
1388
1389 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001390 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1391 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001392 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001393 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001394}
1395
1396bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001397 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001398
1399 ui_print("Restoring %s...\n", Display_Name.c_str());
1400 Full_FileName = restore_folder + "/" + Backup_FileName;
1401 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1402 Command = "erase_image " + MTD_Name;
1403 LOGI("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001404 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001405 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1406 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001407 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001408 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001409}
Dees_Troy5bf43922012-09-07 16:07:55 -04001410
1411bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001412 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001413
Dees_Troyab10ee22012-09-21 14:27:30 -04001414 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001415 return false;
1416
Dees_Troy0550cfb2012-10-13 11:56:13 -04001417 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001418 if (Removable || Is_Encrypted) {
1419 if (!Mount(false))
1420 return true;
1421 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001422 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001423
1424 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001425 if (!ret || Size == 0) {
1426 if (!Get_Size_Via_df(Display_Error)) {
1427 if (!Was_Already_Mounted)
1428 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001429 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001430 }
1431 }
Dees_Troy51127312012-09-08 13:08:49 -04001432
Dees_Troy5bf43922012-09-07 16:07:55 -04001433 if (Has_Data_Media) {
1434 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001435 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001436 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1437 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001438 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001439 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001440 int bak = (int)(Backup_Size / 1048576LLU);
1441 int total = (int)(Size / 1048576LLU);
1442 int us = (int)(Used / 1048576LLU);
1443 int fre = (int)(Free / 1048576LLU);
1444 int datmed = (int)(data_media_used / 1048576LLU);
1445 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 -04001446 } else {
1447 if (!Was_Already_Mounted)
1448 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001449 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001450 }
Dees_Troye58d5262012-09-21 12:27:57 -04001451 } else if (Has_Android_Secure) {
1452 if (Mount(Display_Error))
1453 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001454 else {
1455 if (!Was_Already_Mounted)
1456 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001457 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001458 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001459 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001460 if (!Was_Already_Mounted)
1461 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001462 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001463}
Dees_Troy38bd7602012-09-14 13:33:53 -04001464
1465void TWPartition::Find_Actual_Block_Device(void) {
1466 if (Is_Decrypted) {
1467 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001468 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001469 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001470 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001471 Is_Present = true;
1472 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001473 return;
1474 }
1475 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001476 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001477 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001478 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001479 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001480 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001481 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001482}
1483
1484void TWPartition::Recreate_Media_Folder(void) {
1485 string Command;
1486
1487 if (!Mount(true)) {
1488 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001489 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001490 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001491 LOGI("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001492 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1493 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001494 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001495}
Dees_Troye58d5262012-09-21 12:27:57 -04001496
1497void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001498 if (!Has_Android_Secure)
1499 return;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001500 LOGI("Creating .android_secure: %s\n", Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001501 if (!Mount(true)) {
1502 LOGE("Unable to recreate android secure folder.\n");
1503 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1504 LOGI("Recreating android secure folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001505 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1506 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1507 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001508 }
1509}