blob: 4a7a90023067dbd4fdf5d68a16ba5477d27450b9 [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_Troy38bd7602012-09-14 13:33:53 -0400763 if (Has_Data_Media)
764 return Wipe_Data_Without_Wiping_Media();
765
766 int check;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800767 bool wiped = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400768 DataManager::GetValue(TW_RM_RF_VAR, check);
Gary Pecke8bc5d72012-12-21 06:45:25 -0800769
Dees_Troy38bd7602012-09-14 13:33:53 -0400770 if (check)
Gary Pecke8bc5d72012-12-21 06:45:25 -0800771 wiped = Wipe_RMRF();
772 else if (New_File_System == "ext4")
773 wiped = Wipe_EXT4();
774 else if (New_File_System == "ext2" || New_File_System == "ext3")
775 wiped = Wipe_EXT23(New_File_System);
776 else if (New_File_System == "vfat")
777 wiped = Wipe_FAT();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500778 if (New_File_System == "exfat")
779 wiped = Wipe_EXFAT();
Gary Pecke8bc5d72012-12-21 06:45:25 -0800780 else if (New_File_System == "yaffs2")
781 wiped = Wipe_MTD();
782 else {
783 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
784 return false;
785 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400786
Gary Pecke8bc5d72012-12-21 06:45:25 -0800787 if (wiped) {
788 Setup_File_System(false);
789 if (Is_Encrypted && !Is_Decrypted) {
790 // just wiped an encrypted partition back to its unencrypted state
791 Is_Encrypted = false;
792 Is_Decrypted = false;
793 Decrypted_Block_Device = "";
794 if (Mount_Point == "/data") {
795 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
796 DataManager::SetValue(TW_IS_DECRYPTED, 0);
797 }
798 }
799 }
800 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400801}
802
Gary Peck43acadf2012-11-21 21:19:01 -0800803bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800804 if (Is_File_System(Current_File_System))
805 return Wipe(Current_File_System);
806 else
807 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -0800808}
809
Dees_Troye58d5262012-09-21 12:27:57 -0400810bool TWPartition::Wipe_AndSec(void) {
811 if (!Has_Android_Secure)
812 return false;
813
Dees_Troye58d5262012-09-21 12:27:57 -0400814 if (!Mount(true))
815 return false;
816
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500817 ui_print("Wiping .android_secure\n");
818 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -0400819 return true;
820}
821
Dees_Troy51a0e822012-09-05 15:24:24 -0400822bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400823 if (Backup_Method == FILES)
824 return Backup_Tar(backup_folder);
825 else if (Backup_Method == DD)
826 return Backup_DD(backup_folder);
827 else if (Backup_Method == FLASH_UTILS)
828 return Backup_Dump_Image(backup_folder);
829 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
830 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400831}
832
Dees_Troy43d8b002012-09-17 16:00:01 -0400833bool TWPartition::Check_MD5(string restore_folder) {
834 string Full_Filename;
835 char split_filename[512];
836 int index = 0;
837
838 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400839 if (!TWFunc::Path_Exists(Full_Filename)) {
840 // This is a split archive, we presume
841 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
842 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
843 if (TWFunc::Check_MD5(split_filename) == 0) {
844 LOGE("MD5 failed to match on '%s'.\n", split_filename);
845 return false;
846 }
847 index++;
848 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400849 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400850 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400851 } else {
852 // Single file archive
853 if (TWFunc::Check_MD5(Full_Filename) == 0) {
854 LOGE("MD5 failed to match on '%s'.\n", split_filename);
855 return false;
856 } else
857 return true;
858 }
859 return false;
860}
861
Dees_Troy51a0e822012-09-05 15:24:24 -0400862bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -0800863 size_t first_period, second_period;
864 string Restore_File_System;
865
866 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
867 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
868
869 // Parse backup filename to extract the file system before wiping
870 first_period = Backup_FileName.find(".");
871 if (first_period == string::npos) {
872 LOGE("Unable to find file system (first period).\n");
873 return false;
874 }
875 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
876 second_period = Restore_File_System.find(".");
877 if (second_period == string::npos) {
878 LOGE("Unable to find file system (second period).\n");
879 return false;
880 }
881 Restore_File_System.resize(second_period);
882 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
883
884 if (Is_File_System(Restore_File_System))
885 return Restore_Tar(restore_folder, Restore_File_System);
886 else if (Is_Image(Restore_File_System)) {
887 if (Restore_File_System == "emmc")
888 return Restore_DD(restore_folder);
889 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
890 return Restore_Flash_Image(restore_folder);
891 }
892
Dees_Troy38bd7602012-09-14 13:33:53 -0400893 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
894 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400895}
896
897string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400898 if (Backup_Method == NONE)
899 return "none";
900 else if (Backup_Method == FILES)
901 return "files";
902 else if (Backup_Method == DD)
903 return "dd";
904 else if (Backup_Method == FLASH_UTILS)
905 return "flash_utils";
906 else
907 return "undefined";
908 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400909}
910
911bool TWPartition::Decrypt(string Password) {
912 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400913 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400914 return 1;
915}
916
917bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400918 bool Save_Data_Media = Has_Data_Media;
919
920 if (!UnMount(true))
921 return false;
922
Dees_Troy38bd7602012-09-14 13:33:53 -0400923 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800924 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400925 Has_Data_Media = Save_Data_Media;
926 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
927 Recreate_Media_Folder();
928 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400929 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400930 return true;
931 } else {
932 Has_Data_Media = Save_Data_Media;
933 LOGE("Unable to format to remove encryption.\n");
934 return false;
935 }
936 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400937}
938
939void TWPartition::Check_FS_Type() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500940 string blkCommand, result;
941 string blkOutput;
Dees_Troy5bf43922012-09-07 16:07:55 -0400942 char* blk;
943 char* arg;
944 char* ptr;
945
Dees_Troy68cab492012-12-12 19:29:35 +0000946 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
947 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -0400948
Dees_Troy38bd7602012-09-14 13:33:53 -0400949 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400950 if (!Is_Present)
951 return;
Dees_Troy51127312012-09-08 13:08:49 -0400952
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500953 blkCommand = "blkid " + Actual_Block_Device;
954 TWFunc::Exec_Cmd(blkCommand, result);
955 std::stringstream line(result);
956 while (getline(line, blkOutput))
Dees_Troy5bf43922012-09-07 16:07:55 -0400957 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500958 blk = (char*) blkOutput.c_str();
959 ptr = (char*) blkOutput.c_str();
Dees_Troy63c8df72012-09-10 14:02:05 -0400960 while (*ptr > 32 && *ptr != ':') ptr++;
961 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400962 *ptr = 0;
963
964 // Increment by two, but verify that we don't hit a NULL
965 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400966 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400967
968 // Now, find the TYPE field
969 while (1)
970 {
971 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400972 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400973 if (*ptr != 0)
974 {
975 *ptr = 0;
976 ptr++;
977 }
978
979 if (strlen(arg) > 6)
980 {
981 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
982 }
983
984 if (*ptr == 0)
985 {
986 arg = NULL;
987 break;
988 }
989 }
990
991 if (arg && strlen(arg) > 7)
992 {
993 arg += 6; // Skip the TYPE=" portion
994 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
995 }
996 else
997 continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500998 if (Current_File_System != arg) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400999 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
1000 Current_File_System = arg;
1001 }
1002 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001003 return;
1004}
1005
Gary Peck43acadf2012-11-21 21:19:01 -08001006bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001007 if (!UnMount(true))
1008 return false;
1009
Dees_Troy43d8b002012-09-17 16:00:01 -04001010 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001011 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001012
1013 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
1014 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001015 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
1016 LOGI("mke2fs command: %s\n", command.c_str());
1017 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001018 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001019 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001020 ui_print("Done.\n");
1021 return true;
1022 } else {
1023 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1024 return false;
1025 }
1026 } else
1027 return Wipe_RMRF();
1028
1029 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001030}
1031
1032bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001033 if (!UnMount(true))
1034 return false;
1035
Dees_Troy43d8b002012-09-17 16:00:01 -04001036 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001037 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001038
1039 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
1040 Find_Actual_Block_Device();
1041 Command = "make_ext4fs";
1042 if (!Is_Decrypted && Length != 0) {
1043 // Only use length if we're not decrypted
1044 char len[32];
1045 sprintf(len, "%i", Length);
1046 Command += " -l ";
1047 Command += len;
1048 }
1049 Command += " " + Actual_Block_Device;
1050 LOGI("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001051 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001052 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001053 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001054 ui_print("Done.\n");
1055 return true;
1056 } else {
1057 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1058 return false;
1059 }
1060 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001061 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001062
1063 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001064}
1065
1066bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001067 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001068
Dees_Troy43d8b002012-09-17 16:00:01 -04001069 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001070 if (!UnMount(true))
1071 return false;
1072
1073 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1074 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001075 command = "mkdosfs " + Actual_Block_Device;
1076 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001077 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001078 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001079 ui_print("Done.\n");
1080 return true;
1081 } else {
1082 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1083 return false;
1084 }
1085 return true;
1086 }
1087 else
1088 return Wipe_RMRF();
1089
1090 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001091}
1092
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001093bool TWPartition::Wipe_EXFAT() {
1094 string command, result;
1095
1096 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1097 if (!UnMount(true))
1098 return false;
1099
1100 ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
1101 Find_Actual_Block_Device();
1102 command = "mkexfatfs " + Actual_Block_Device;
1103 if (TWFunc::Exec_Cmd(command, result) == 0) {
1104 Recreate_AndSec_Folder();
1105 ui_print("Done.\n");
1106 return true;
1107 } else {
1108 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1109 return false;
1110 }
1111 return true;
1112 }
1113 return false;
1114}
1115
Dees_Troy38bd7602012-09-14 13:33:53 -04001116bool TWPartition::Wipe_MTD() {
1117 if (!UnMount(true))
1118 return false;
1119
1120 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1121
1122 mtd_scan_partitions();
1123 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1124 if (mtd == NULL) {
1125 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1126 return false;
1127 }
1128
1129 MtdWriteContext* ctx = mtd_write_partition(mtd);
1130 if (ctx == NULL) {
1131 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1132 return false;
1133 }
1134 if (mtd_erase_blocks(ctx, -1) == -1) {
1135 mtd_write_close(ctx);
1136 LOGE("Failed to format '%s'", MTD_Name.c_str());
1137 return false;
1138 }
1139 if (mtd_write_close(ctx) != 0) {
1140 LOGE("Failed to close '%s'", MTD_Name.c_str());
1141 return false;
1142 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001143 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001144 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001145 ui_print("Done.\n");
1146 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001147}
1148
1149bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001150 if (!Mount(true))
1151 return false;
1152
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001153 ui_print("Removing all files under '%s'\n", Mount_Point.c_str());
1154 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001155 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001156 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001157}
1158
1159bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001160 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001161
1162 // This handles wiping data on devices with "sdcard" in /data/media
1163 if (!Mount(true))
1164 return false;
1165
1166 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001167
1168 DIR* d;
1169 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001170 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001171 struct dirent* de;
1172 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001173 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1174 // The media folder is the "internal sdcard"
1175 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1176 // the media folder for multi-user.
1177 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001178
1179 dir = "/data/";
1180 dir.append(de->d_name);
1181 TWFunc::removeDir(dir, false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001182 }
1183 closedir(d);
Dees_Troy16b74352012-11-14 22:27:31 +00001184 ui_print("Done.\n");
1185 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001186 }
Dees_Troy16b74352012-11-14 22:27:31 +00001187 ui_print("Dirent failed to open /data, error!\n");
1188 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001189}
1190
1191bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001192 char back_name[255], split_index[5];
1193 string Full_FileName, Split_FileName, Tar_Args, Command;
1194 int use_compression, index, backup_count;
1195 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001196 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001197 twrpTar tar;
1198 vector <string> files;
Dees_Troy43d8b002012-09-17 16:00:01 -04001199
1200 if (!Mount(true))
1201 return false;
1202
Dees_Troy2c50e182012-09-26 20:05:28 -04001203 if (Backup_Path == "/and-sec") {
1204 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1205 ui_print("Backing up %s...\n", "Android Secure");
1206 } else {
1207 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1208 ui_print("Backing up %s...\n", Display_Name.c_str());
1209 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001210
1211 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001212
1213 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1214 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001215 Full_FileName = backup_folder + "/" + Backup_FileName;
1216 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001217 // This backup needs to be split into multiple archives
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001218 ui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001219 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001220 backup_count = tar.Split_Archive(back_name, Full_FileName);
1221 if (backup_count == -1) {
1222 LOGE("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001223 return false;
1224 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001225 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001226 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001227 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001228 if (use_compression) {
1229 if (tar.createTGZ(Backup_Path, Full_FileName) != 0)
1230 return false;
1231 string gzname = Full_FileName + ".gz";
1232 rename(gzname.c_str(), Full_FileName.c_str());
1233 }
1234 else {
1235 if (tar.create(Backup_Path, Full_FileName) != 0)
1236 return false;
1237 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001238 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1239 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1240 return false;
1241 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001242 }
1243 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001244}
1245
1246bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001247 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001248 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001249 int use_compression;
1250
Dees_Troyb46a6842012-09-25 11:06:46 -04001251 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001252 ui_print("Backing up %s...\n", Display_Name.c_str());
1253
1254 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1255 Backup_FileName = back_name;
1256
1257 Full_FileName = backup_folder + "/" + Backup_FileName;
1258
1259 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1260 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001261 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001262 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1263 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001264 return false;
1265 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001266 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001267}
1268
1269bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001270 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001271 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001272 int use_compression;
1273
Dees_Troyb46a6842012-09-25 11:06:46 -04001274 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001275 ui_print("Backing up %s...\n", Display_Name.c_str());
1276
1277 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1278 Backup_FileName = back_name;
1279
1280 Full_FileName = backup_folder + "/" + Backup_FileName;
1281
1282 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1283 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001284 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001285 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1286 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1287 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1288 return false;
1289 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001290 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001291}
1292
Gary Peck43acadf2012-11-21 21:19:01 -08001293bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1294 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001295 int index = 0;
1296 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001297
Dees_Troye58d5262012-09-21 12:27:57 -04001298 if (Has_Android_Secure) {
1299 ui_print("Wiping android secure...\n");
1300 if (!Wipe_AndSec())
1301 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001302 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001303 ui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001304 if (!Wipe(Restore_File_System))
1305 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001306 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001307
1308 if (!Mount(true))
1309 return false;
1310
Dees_Troyda8b55a2012-12-12 19:18:30 +00001311 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001312 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001313 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001314 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001315 if (!TWFunc::Path_Exists(Full_FileName)) {
1316 // Backup is multiple archives
1317 LOGI("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001318 sprintf(split_index, "%03i", index);
1319 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001320 while (TWFunc::Path_Exists(Full_FileName)) {
1321 index++;
1322 ui_print("Restoring archive %i...\n", index);
1323 LOGI("Restoring '%s'...\n", Full_FileName.c_str());
1324 twrpTar tar;
1325 if (tar.extract("/", Full_FileName) != 0)
1326 return false;
1327 sprintf(split_index, "%03i", index);
1328 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1329 }
1330 if (index == 0) {
1331 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1332 return false;
1333 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001334 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001335 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001336 twrpTar tar;
1337 if (tar.extract(Backup_Path, Full_FileName) != 0)
1338 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001339 }
1340 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001341}
1342
1343bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001344 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001345
Dees_Troyda8b55a2012-12-12 19:18:30 +00001346 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001347 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001348
1349 if (!Find_Partition_Size()) {
1350 LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
1351 return false;
1352 }
1353 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1354 if (backup_size > Size) {
1355 LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
1356 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1357 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1358 return false;
1359 }
1360
1361 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001362 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1363 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001364 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001365 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001366}
1367
1368bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001369 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001370
1371 ui_print("Restoring %s...\n", Display_Name.c_str());
1372 Full_FileName = restore_folder + "/" + Backup_FileName;
1373 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1374 Command = "erase_image " + MTD_Name;
1375 LOGI("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001376 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001377 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1378 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001379 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001380 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001381}
Dees_Troy5bf43922012-09-07 16:07:55 -04001382
1383bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001384 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001385
Dees_Troyab10ee22012-09-21 14:27:30 -04001386 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001387 return false;
1388
Dees_Troy0550cfb2012-10-13 11:56:13 -04001389 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001390 if (Removable || Is_Encrypted) {
1391 if (!Mount(false))
1392 return true;
1393 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001394 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001395
1396 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001397 if (!ret || Size == 0) {
1398 if (!Get_Size_Via_df(Display_Error)) {
1399 if (!Was_Already_Mounted)
1400 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001401 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001402 }
1403 }
Dees_Troy51127312012-09-08 13:08:49 -04001404
Dees_Troy5bf43922012-09-07 16:07:55 -04001405 if (Has_Data_Media) {
1406 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001407 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001408 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1409 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001410 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001411 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001412 int bak = (int)(Backup_Size / 1048576LLU);
1413 int total = (int)(Size / 1048576LLU);
1414 int us = (int)(Used / 1048576LLU);
1415 int fre = (int)(Free / 1048576LLU);
1416 int datmed = (int)(data_media_used / 1048576LLU);
1417 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 -04001418 } else {
1419 if (!Was_Already_Mounted)
1420 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001421 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001422 }
Dees_Troye58d5262012-09-21 12:27:57 -04001423 } else if (Has_Android_Secure) {
1424 if (Mount(Display_Error))
1425 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001426 else {
1427 if (!Was_Already_Mounted)
1428 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001429 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001430 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001431 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001432 if (!Was_Already_Mounted)
1433 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001434 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001435}
Dees_Troy38bd7602012-09-14 13:33:53 -04001436
1437void TWPartition::Find_Actual_Block_Device(void) {
1438 if (Is_Decrypted) {
1439 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001440 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001441 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001442 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001443 Is_Present = true;
1444 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001445 return;
1446 }
1447 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001448 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001449 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001450 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001451 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001452 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001453 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001454}
1455
1456void TWPartition::Recreate_Media_Folder(void) {
1457 string Command;
1458
1459 if (!Mount(true)) {
1460 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001461 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001462 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001463 LOGI("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001464 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1465 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001466 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001467}
Dees_Troye58d5262012-09-21 12:27:57 -04001468
1469void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001470 if (!Has_Android_Secure)
1471 return;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001472 LOGI("Creating .android_secure: %s\n", Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001473 if (!Mount(true)) {
1474 LOGE("Unable to recreate android secure folder.\n");
1475 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1476 LOGI("Recreating android secure folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001477 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1478 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1479 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001480 }
1481}