blob: f5173b13d887a517671035f01a222f0f80d4e6b9 [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>
Dees_Troy51a0e822012-09-05 15:24:24 -040031
Dees_Troy657c3092012-09-10 20:32:10 -040032#ifdef TW_INCLUDE_CRYPTO
33 #include "cutils/properties.h"
34#endif
35
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
37#include "common.h"
38#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
Dees_Troy9df963c2012-09-26 08:58:12 -040041#include "makelist.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040043 #include "mtdutils/mtdutils.h"
44 #include "mtdutils/mounts.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040045}
Dees_Troy51a0e822012-09-05 15:24:24 -040046
47TWPartition::TWPartition(void) {
48 Can_Be_Mounted = false;
49 Can_Be_Wiped = false;
50 Wipe_During_Factory_Reset = false;
51 Wipe_Available_in_GUI = false;
52 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040053 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040054 SubPartition_Of = "";
55 Symlink_Path = "";
56 Symlink_Mount_Point = "";
57 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040058 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040059 Actual_Block_Device = "";
60 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040061 Alternate_Block_Device = "";
62 Removable = false;
63 Is_Present = false;
64 Length = 0;
65 Size = 0;
66 Used = 0;
67 Free = 0;
68 Backup_Size = 0;
69 Can_Be_Encrypted = false;
70 Is_Encrypted = false;
71 Is_Decrypted = false;
72 Decrypted_Block_Device = "";
73 Display_Name = "";
74 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040075 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040076 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040077 Backup_Method = NONE;
78 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040079 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040080 Is_Storage = false;
81 Storage_Path = "";
82 Current_File_System = "";
83 Fstab_File_System = "";
84 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +000085 Ignore_Blkid = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040086}
87
88TWPartition::~TWPartition(void) {
89 // Do nothing
90}
91
Dees_Troy5bf43922012-09-07 16:07:55 -040092bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
93 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
94 int line_len = Line.size(), index = 0, item_index = 0;
95 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -040096 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -040097
98 strncpy(full_line, Line.c_str(), line_len);
99
Dees_Troy51127312012-09-08 13:08:49 -0400100 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 if (full_line[index] <= 32)
102 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400103 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400104 Mount_Point = full_line;
105 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400106 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400107 index = Mount_Point.size();
108 while (index < line_len) {
109 while (index < line_len && full_line[index] == '\0')
110 index++;
111 if (index >= line_len)
112 continue;
113 ptr = full_line + index;
114 if (item_index == 0) {
115 // File System
116 Fstab_File_System = ptr;
117 Current_File_System = ptr;
118 item_index++;
119 } else if (item_index == 1) {
120 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400121 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400122 MTD_Name = ptr;
123 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400124 } else if (Fstab_File_System == "bml") {
125 if (Mount_Point == "/boot")
126 MTD_Name = "boot";
127 else if (Mount_Point == "/recovery")
128 MTD_Name = "recovery";
129 Primary_Block_Device = ptr;
130 if (*ptr != '/')
131 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 -0400132 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400133 if (Display_Error)
134 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
135 else
136 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
137 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400138 } else {
139 Primary_Block_Device = ptr;
140 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400141 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400142 item_index++;
143 } else if (item_index > 1) {
144 if (*ptr == '/') {
145 // Alternate Block Device
146 Alternate_Block_Device = ptr;
147 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
148 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
149 // Partition length
150 ptr += 7;
151 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400152 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
153 // Custom flags, save for later so that new values aren't overwritten by defaults
154 ptr += 6;
155 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000156 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400157 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
158 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400159 } else {
160 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400161 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400162 }
163 }
164 while (index < line_len && full_line[index] != '\0')
165 index++;
166 }
167
168 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
169 if (Display_Error)
170 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
171 else
172 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
173 return 0;
174 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400175 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400176 Setup_File_System(Display_Error);
177 if (Mount_Point == "/system") {
178 Display_Name = "System";
179 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400180 } else if (Mount_Point == "/data") {
181 Display_Name = "Data";
182 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400183 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400184#ifdef RECOVERY_SDCARD_ON_DATA
185 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400186 Is_Storage = true;
187 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000188 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400189 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
190 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400191 Symlink_Mount_Point = "/emmc";
192 } else {
193 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400194 Symlink_Mount_Point = "/sdcard";
195 }
Dees_Troy16b74352012-11-14 22:27:31 +0000196 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
197 Storage_Path = "/data/media/0";
198 Symlink_Path = Storage_Path;
199 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
200 UnMount(true);
201 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400202#endif
203#ifdef TW_INCLUDE_CRYPTO
204 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400205 char crypto_blkdev[255];
206 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
207 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400208 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400209 DataManager::SetValue(TW_IS_DECRYPTED, 1);
210 Is_Encrypted = true;
211 Is_Decrypted = true;
212 Decrypted_Block_Device = crypto_blkdev;
213 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
214 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 Is_Encrypted = true;
216 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800217 Can_Be_Mounted = false;
218 Current_File_System = "emmc";
219 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
221 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
222 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800223 } else {
224 // Filesystem is not encrypted and the mount
225 // succeeded, so get it back to the original
226 // unmounted state
227 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400228 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400229 #ifdef RECOVERY_SDCARD_ON_DATA
230 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
231 Recreate_Media_Folder();
232 #endif
233#else
234 #ifdef RECOVERY_SDCARD_ON_DATA
235 Recreate_Media_Folder();
236 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400237#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400238 } else if (Mount_Point == "/cache") {
239 Display_Name = "Cache";
240 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400241 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400242 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
243 string Recreate_Command = "cd /cache && mkdir recovery";
Dees_Troyb46a6842012-09-25 11:06:46 -0400244 LOGI("Recreating /cache/recovery folder.\n");
Dees_Troyce2fe772012-09-28 12:34:33 -0400245 system(Recreate_Command.c_str());
Dees_Troyb46a6842012-09-25 11:06:46 -0400246 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400248 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400249 Display_Name = "DataData";
250 Is_SubPartition = true;
251 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 DataManager::SetValue(TW_HAS_DATADATA, 1);
253 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400254 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 Display_Name = "SD-Ext";
256 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400257 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400258 } else if (Mount_Point == "/boot") {
259 Display_Name = "Boot";
260 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400261 }
262#ifdef TW_EXTERNAL_STORAGE_PATH
263 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
264 Is_Storage = true;
265 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400266 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400267 }
268#else
269 if (Mount_Point == "/sdcard") {
270 Is_Storage = true;
271 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400272 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400273#ifndef RECOVERY_SDCARD_ON_DATA
274 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400275 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400276#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400277 }
278#endif
279#ifdef TW_INTERNAL_STORAGE_PATH
280 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
281 Is_Storage = true;
282 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400283#ifndef RECOVERY_SDCARD_ON_DATA
284 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400285 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400286#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400287 }
288#else
289 if (Mount_Point == "/emmc") {
290 Is_Storage = true;
291 Storage_Path = "/emmc";
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#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400298 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400299 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400300 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400301 }
302
Dees_Troy51127312012-09-08 13:08:49 -0400303 // Process any custom flags
304 if (Flags.size() > 0)
305 Process_Flags(Flags, Display_Error);
306
307 return true;
308}
309
310bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
311 char flags[MAX_FSTAB_LINE_LENGTH];
312 int flags_len, index = 0;
313 char* ptr;
314
315 strcpy(flags, Flags.c_str());
316 flags_len = Flags.size();
317 for (index = 0; index < flags_len; index++) {
318 if (flags[index] == ';')
319 flags[index] = '\0';
320 }
321
322 index = 0;
323 while (index < flags_len) {
324 while (index < flags_len && flags[index] == '\0')
325 index++;
326 if (index >= flags_len)
327 continue;
328 ptr = flags + index;
329 if (strcmp(ptr, "removable") == 0) {
330 Removable = true;
331 } else if (strcmp(ptr, "storage") == 0) {
332 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400333 } else if (strcmp(ptr, "canbewiped") == 0) {
334 Can_Be_Wiped = true;
335 } else if (strcmp(ptr, "wipeingui") == 0) {
336 Can_Be_Wiped = true;
337 Wipe_Available_in_GUI = true;
338 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
339 Can_Be_Wiped = true;
340 Wipe_Available_in_GUI = true;
341 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400342 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
343 ptr += 13;
344 Is_SubPartition = true;
345 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000346 } else if (strcmp(ptr, "ignoreblkid") == 0) {
347 Ignore_Blkid = true;
Dees_Troy51127312012-09-08 13:08:49 -0400348 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
349 ptr += 8;
350 Symlink_Path = ptr;
351 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
352 ptr += 8;
353 Display_Name = ptr;
354 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
355 ptr += 10;
356 Format_Block_Size = atoi(ptr);
357 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
358 ptr += 7;
359 Length = atoi(ptr);
360 } else {
361 if (Display_Error)
362 LOGE("Unhandled flag: '%s'\n", ptr);
363 else
364 LOGI("Unhandled flag: '%s'\n", ptr);
365 }
366 while (index < flags_len && flags[index] != '\0')
367 index++;
368 }
369 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400370}
371
Dees_Troy5bf43922012-09-07 16:07:55 -0400372bool TWPartition::Is_File_System(string File_System) {
373 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400374 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400375 File_System == "ext4" ||
376 File_System == "vfat" ||
377 File_System == "ntfs" ||
378 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500379 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400380 File_System == "auto")
381 return true;
382 else
383 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400384}
385
Dees_Troy5bf43922012-09-07 16:07:55 -0400386bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400387 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400388 return true;
389 else
390 return false;
391}
392
Dees_Troy51127312012-09-08 13:08:49 -0400393bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400394 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400395 if (mkdir(Path.c_str(), 0777) == -1) {
396 if (Display_Error)
397 LOGE("Can not create '%s' folder.\n", Path.c_str());
398 else
399 LOGI("Can not create '%s' folder.\n", Path.c_str());
400 return false;
401 } else {
402 LOGI("Created '%s' folder.\n", Path.c_str());
403 return true;
404 }
405 }
406 return true;
407}
408
Dees_Troy5bf43922012-09-07 16:07:55 -0400409void TWPartition::Setup_File_System(bool Display_Error) {
410 struct statfs st;
411
412 Can_Be_Mounted = true;
413 Can_Be_Wiped = true;
414
Dees_Troy5bf43922012-09-07 16:07:55 -0400415 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400416 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400417 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
418 Backup_Name = Display_Name;
419 Backup_Method = FILES;
420}
421
422void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400423 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
424 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800425 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400426 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800427 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400428 Backup_Method = FLASH_UTILS;
429 else
Gary Peck82599a82012-11-21 16:23:12 -0800430 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 -0400431 if (Find_Partition_Size()) {
432 Used = Size;
433 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400434 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400436 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400437 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400438 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400439 }
440}
441
Dees_Troye58d5262012-09-21 12:27:57 -0400442void TWPartition::Setup_AndSec(void) {
443 Backup_Name = "and-sec";
444 Has_Android_Secure = true;
445 Symlink_Path = Mount_Point + "/.android_secure";
446 Symlink_Mount_Point = "/and-sec";
447 Backup_Path = Symlink_Mount_Point;
448 Make_Dir("/and-sec", true);
449 Recreate_AndSec_Folder();
450}
451
Dees_Troy5bf43922012-09-07 16:07:55 -0400452void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
453 char device[512], realDevice[512];
454
455 strcpy(device, Block.c_str());
456 memset(realDevice, 0, sizeof(realDevice));
457 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
458 {
459 strcpy(device, realDevice);
460 memset(realDevice, 0, sizeof(realDevice));
461 }
462
463 if (device[0] != '/') {
464 if (Display_Error)
465 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
466 else
467 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
468 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400469 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400470 Block = device;
471 return;
472 }
473}
474
Dees_Troy8e337f32012-10-13 22:07:49 -0400475void TWPartition::Mount_Storage_Retry(void) {
476 // On some devices, storage doesn't want to mount right away, retry and sleep
477 if (!Mount(false)) {
478 int retry_count = 5;
479 while (retry_count > 0 && !Mount(false)) {
480 usleep(500000);
481 retry_count--;
482 }
483 Mount(true);
484 }
485}
486
Dees_Troy38bd7602012-09-14 13:33:53 -0400487bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
488 FILE *fp = NULL;
489 char line[255];
490
491 fp = fopen("/proc/mtd", "rt");
492 if (fp == NULL) {
493 LOGE("Device does not support /proc/mtd\n");
494 return false;
495 }
496
497 while (fgets(line, sizeof(line), fp) != NULL)
498 {
499 char device[32], label[32];
500 unsigned long size = 0;
501 char* fstype = NULL;
502 int deviceId;
503
504 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
505
506 // Skip header and blank lines
507 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
508 continue;
509
510 // Strip off the trailing " from the label
511 label[strlen(label)-1] = '\0';
512
513 if (strcmp(label, MTD_Name.c_str()) == 0) {
514 // We found our device
515 // Strip off the trailing : from the device
516 device[strlen(device)-1] = '\0';
517 if (sscanf(device,"mtd%d", &deviceId) == 1) {
518 sprintf(device, "/dev/block/mtdblock%d", deviceId);
519 Primary_Block_Device = device;
520 }
521 }
522 }
523 fclose(fp);
524
525 return false;
526}
527
Dees_Troy51127312012-09-08 13:08:49 -0400528bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
529 struct statfs st;
530 string Local_Path = Mount_Point + "/.";
531
532 if (!Mount(Display_Error))
533 return false;
534
535 if (statfs(Local_Path.c_str(), &st) != 0) {
536 if (!Removable) {
537 if (Display_Error)
538 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
539 else
540 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
541 }
542 return false;
543 }
544 Size = (st.f_blocks * st.f_bsize);
545 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
546 Free = (st.f_bfree * st.f_bsize);
547 Backup_Size = Used;
548 return true;
549}
550
551bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400552 FILE* fp;
553 char command[255], line[512];
554 int include_block = 1;
555 unsigned int min_len;
556
557 if (!Mount(Display_Error))
558 return false;
559
Dees_Troy38bd7602012-09-14 13:33:53 -0400560 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400561 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400562 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400563 fp = fopen("/tmp/dfoutput.txt", "rt");
564 if (fp == NULL) {
565 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400566 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400567 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400568
569 while (fgets(line, sizeof(line), fp) != NULL)
570 {
571 unsigned long blocks, used, available;
572 char device[64];
573 char tmpString[64];
574
575 if (strncmp(line, "Filesystem", 10) == 0)
576 continue;
577 if (strlen(line) < min_len) {
578 include_block = 0;
579 continue;
580 }
581 if (include_block) {
582 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
583 } else {
584 // The device block string is so long that the df information is on the next line
585 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400586 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400587 while (tmpString[space_count] == 32)
588 space_count++;
589 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
590 }
591
592 // Adjust block size to byte size
593 Size = blocks * 1024ULL;
594 Used = used * 1024ULL;
595 Free = available * 1024ULL;
596 Backup_Size = Used;
597 }
598 fclose(fp);
599 return true;
600}
601
Dees_Troy5bf43922012-09-07 16:07:55 -0400602bool TWPartition::Find_Partition_Size(void) {
603 FILE* fp;
604 char line[512];
605 string tmpdevice;
606
607 // In this case, we'll first get the partitions we care about (with labels)
608 fp = fopen("/proc/partitions", "rt");
609 if (fp == NULL)
610 return false;
611
612 while (fgets(line, sizeof(line), fp) != NULL)
613 {
614 unsigned long major, minor, blocks;
615 char device[512];
616 char tmpString[64];
617
Dees_Troy63c8df72012-09-10 14:02:05 -0400618 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400619 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
620
621 tmpdevice = "/dev/block/";
622 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400623 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400624 // Adjust block size to byte size
625 Size = blocks * 1024ULL;
626 fclose(fp);
627 return true;
628 }
629 }
630 fclose(fp);
631 return false;
632}
633
Dees_Troy5bf43922012-09-07 16:07:55 -0400634bool TWPartition::Is_Mounted(void) {
635 if (!Can_Be_Mounted)
636 return false;
637
638 struct stat st1, st2;
639 string test_path;
640
641 // Check to see if the mount point directory exists
642 test_path = Mount_Point + "/.";
643 if (stat(test_path.c_str(), &st1) != 0) return false;
644
645 // Check to see if the directory above the mount point exists
646 test_path = Mount_Point + "/../.";
647 if (stat(test_path.c_str(), &st2) != 0) return false;
648
649 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
650 int ret = (st1.st_dev != st2.st_dev) ? true : false;
651
652 return ret;
653}
654
655bool TWPartition::Mount(bool Display_Error) {
656 if (Is_Mounted()) {
657 return true;
658 } else if (!Can_Be_Mounted) {
659 return false;
660 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400661
662 Find_Actual_Block_Device();
663
664 // Check the current file system before mounting
665 Check_FS_Type();
666
Dees_Troy22042032012-12-18 21:23:08 +0000667 if (Fstab_File_System == "yaffs2") {
668 // mount an MTD partition as a YAFFS2 filesystem.
669 mtd_scan_partitions();
670 const MtdPartition* partition;
671 partition = mtd_find_partition_by_name(MTD_Name.c_str());
672 if (partition == NULL) {
673 LOGE("Failed to find '%s' partition to mount at '%s'\n",
674 MTD_Name.c_str(), Mount_Point.c_str());
675 return false;
676 }
677 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
678 if (Display_Error)
679 LOGE("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
680 else
681 LOGI("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
682 return false;
683 } else
684 return true;
685 } 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 -0400686 if (Display_Error)
687 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
688 else
689 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy9350b8d2012-09-27 12:38:38 -0400690 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 -0400691 return false;
692 } else {
693 if (Removable)
694 Update_Size(Display_Error);
695
696 if (!Symlink_Mount_Point.empty()) {
697 string Command;
698
699 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400700 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400701 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400702 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400703 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400704 return true;
705}
706
707bool TWPartition::UnMount(bool Display_Error) {
708 if (Is_Mounted()) {
709 int never_unmount_system;
710
711 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
712 if (never_unmount_system == 1 && Mount_Point == "/system")
713 return true; // Never unmount system if you're not supposed to unmount it
714
Dees_Troy38bd7602012-09-14 13:33:53 -0400715 if (!Symlink_Mount_Point.empty())
716 umount(Symlink_Mount_Point.c_str());
717
Dees_Troy5bf43922012-09-07 16:07:55 -0400718 if (umount(Mount_Point.c_str()) != 0) {
719 if (Display_Error)
720 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
721 else
722 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
723 return false;
724 } else
725 return true;
726 } else {
727 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400728 }
729}
730
Gary Peck43acadf2012-11-21 21:19:01 -0800731bool TWPartition::Wipe(string New_File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400732 if (!Can_Be_Wiped) {
733 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
734 return false;
735 }
736
Dees_Troyc51f1f92012-09-20 15:32:13 -0400737 if (Mount_Point == "/cache")
738 tmplog_offset = 0;
739
Dees_Troy38bd7602012-09-14 13:33:53 -0400740 if (Has_Data_Media)
741 return Wipe_Data_Without_Wiping_Media();
742
743 int check;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800744 bool wiped = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400745 DataManager::GetValue(TW_RM_RF_VAR, check);
Gary Pecke8bc5d72012-12-21 06:45:25 -0800746
Dees_Troy38bd7602012-09-14 13:33:53 -0400747 if (check)
Gary Pecke8bc5d72012-12-21 06:45:25 -0800748 wiped = Wipe_RMRF();
749 else if (New_File_System == "ext4")
750 wiped = Wipe_EXT4();
751 else if (New_File_System == "ext2" || New_File_System == "ext3")
752 wiped = Wipe_EXT23(New_File_System);
753 else if (New_File_System == "vfat")
754 wiped = Wipe_FAT();
755 else if (New_File_System == "yaffs2")
756 wiped = Wipe_MTD();
757 else {
758 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
759 return false;
760 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400761
Gary Pecke8bc5d72012-12-21 06:45:25 -0800762 if (wiped) {
763 Setup_File_System(false);
764 if (Is_Encrypted && !Is_Decrypted) {
765 // just wiped an encrypted partition back to its unencrypted state
766 Is_Encrypted = false;
767 Is_Decrypted = false;
768 Decrypted_Block_Device = "";
769 if (Mount_Point == "/data") {
770 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
771 DataManager::SetValue(TW_IS_DECRYPTED, 0);
772 }
773 }
774 }
775 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400776}
777
Gary Peck43acadf2012-11-21 21:19:01 -0800778bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800779 if (Is_File_System(Current_File_System))
780 return Wipe(Current_File_System);
781 else
782 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -0800783}
784
Dees_Troye58d5262012-09-21 12:27:57 -0400785bool TWPartition::Wipe_AndSec(void) {
786 if (!Has_Android_Secure)
787 return false;
788
789 char cmd[512];
790
791 if (!Mount(true))
792 return false;
793
794 ui_print("Using rm -rf on .android_secure\n");
795 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
796
797 LOGI("rm -rf command is: '%s'\n", cmd);
798 system(cmd);
799 return true;
800}
801
Dees_Troy51a0e822012-09-05 15:24:24 -0400802bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400803 if (Backup_Method == FILES)
804 return Backup_Tar(backup_folder);
805 else if (Backup_Method == DD)
806 return Backup_DD(backup_folder);
807 else if (Backup_Method == FLASH_UTILS)
808 return Backup_Dump_Image(backup_folder);
809 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
810 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400811}
812
Dees_Troy43d8b002012-09-17 16:00:01 -0400813bool TWPartition::Check_MD5(string restore_folder) {
814 string Full_Filename;
815 char split_filename[512];
816 int index = 0;
817
818 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400819 if (!TWFunc::Path_Exists(Full_Filename)) {
820 // This is a split archive, we presume
821 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
822 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
823 if (TWFunc::Check_MD5(split_filename) == 0) {
824 LOGE("MD5 failed to match on '%s'.\n", split_filename);
825 return false;
826 }
827 index++;
828 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400829 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400830 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400831 } else {
832 // Single file archive
833 if (TWFunc::Check_MD5(Full_Filename) == 0) {
834 LOGE("MD5 failed to match on '%s'.\n", split_filename);
835 return false;
836 } else
837 return true;
838 }
839 return false;
840}
841
Dees_Troy51a0e822012-09-05 15:24:24 -0400842bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -0800843 size_t first_period, second_period;
844 string Restore_File_System;
845
846 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
847 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
848
849 // Parse backup filename to extract the file system before wiping
850 first_period = Backup_FileName.find(".");
851 if (first_period == string::npos) {
852 LOGE("Unable to find file system (first period).\n");
853 return false;
854 }
855 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
856 second_period = Restore_File_System.find(".");
857 if (second_period == string::npos) {
858 LOGE("Unable to find file system (second period).\n");
859 return false;
860 }
861 Restore_File_System.resize(second_period);
862 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
863
864 if (Is_File_System(Restore_File_System))
865 return Restore_Tar(restore_folder, Restore_File_System);
866 else if (Is_Image(Restore_File_System)) {
867 if (Restore_File_System == "emmc")
868 return Restore_DD(restore_folder);
869 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
870 return Restore_Flash_Image(restore_folder);
871 }
872
Dees_Troy38bd7602012-09-14 13:33:53 -0400873 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
874 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400875}
876
877string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400878 if (Backup_Method == NONE)
879 return "none";
880 else if (Backup_Method == FILES)
881 return "files";
882 else if (Backup_Method == DD)
883 return "dd";
884 else if (Backup_Method == FLASH_UTILS)
885 return "flash_utils";
886 else
887 return "undefined";
888 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400889}
890
891bool TWPartition::Decrypt(string Password) {
892 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400893 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400894 return 1;
895}
896
897bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400898 bool Save_Data_Media = Has_Data_Media;
899
900 if (!UnMount(true))
901 return false;
902
Dees_Troy38bd7602012-09-14 13:33:53 -0400903 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800904 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400905 Has_Data_Media = Save_Data_Media;
906 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
907 Recreate_Media_Folder();
908 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400909 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400910 return true;
911 } else {
912 Has_Data_Media = Save_Data_Media;
913 LOGE("Unable to format to remove encryption.\n");
914 return false;
915 }
916 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400917}
918
919void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400920 FILE *fp;
921 string blkCommand;
922 char blkOutput[255];
923 char* blk;
924 char* arg;
925 char* ptr;
926
Dees_Troy68cab492012-12-12 19:29:35 +0000927 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
928 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -0400929
Dees_Troy38bd7602012-09-14 13:33:53 -0400930 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400931 if (!Is_Present)
932 return;
Dees_Troy51127312012-09-08 13:08:49 -0400933
Dees_Troy8170a922012-09-18 15:40:25 -0400934 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
935 system("rm /tmp/blkidoutput.txt");
936
937 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400938 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400939 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400940 if (fp == NULL)
941 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400942 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
943 {
944 blk = blkOutput;
945 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400946 while (*ptr > 32 && *ptr != ':') ptr++;
947 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400948 *ptr = 0;
949
950 // Increment by two, but verify that we don't hit a NULL
951 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400952 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400953
954 // Now, find the TYPE field
955 while (1)
956 {
957 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400958 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400959 if (*ptr != 0)
960 {
961 *ptr = 0;
962 ptr++;
963 }
964
965 if (strlen(arg) > 6)
966 {
967 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
968 }
969
970 if (*ptr == 0)
971 {
972 arg = NULL;
973 break;
974 }
975 }
976
977 if (arg && strlen(arg) > 7)
978 {
979 arg += 6; // Skip the TYPE=" portion
980 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
981 }
982 else
983 continue;
984
Dees_Troy63c8df72012-09-10 14:02:05 -0400985 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400986 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
987 Current_File_System = arg;
988 }
989 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400990 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400991 return;
992}
993
Gary Peck43acadf2012-11-21 21:19:01 -0800994bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400995 if (!UnMount(true))
996 return false;
997
Dees_Troy43d8b002012-09-17 16:00:01 -0400998 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400999 char command[512];
1000
1001 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
1002 Find_Actual_Block_Device();
Gary Peck43acadf2012-11-21 21:19:01 -08001003 sprintf(command, "mke2fs -t %s -m 0 %s", File_System.c_str(), Actual_Block_Device.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001004 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -04001005 if (system(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001006 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001007 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001008 ui_print("Done.\n");
1009 return true;
1010 } else {
1011 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1012 return false;
1013 }
1014 } else
1015 return Wipe_RMRF();
1016
1017 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001018}
1019
1020bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001021 if (!UnMount(true))
1022 return false;
1023
Dees_Troy43d8b002012-09-17 16:00:01 -04001024 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001025 string Command;
1026
1027 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
1028 Find_Actual_Block_Device();
1029 Command = "make_ext4fs";
1030 if (!Is_Decrypted && Length != 0) {
1031 // Only use length if we're not decrypted
1032 char len[32];
1033 sprintf(len, "%i", Length);
1034 Command += " -l ";
1035 Command += len;
1036 }
1037 Command += " " + Actual_Block_Device;
1038 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001039 if (system(Command.c_str()) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001040 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001041 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001042 ui_print("Done.\n");
1043 return true;
1044 } else {
1045 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1046 return false;
1047 }
1048 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001049 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001050
1051 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001052}
1053
1054bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001055 char command[512];
1056
Dees_Troy43d8b002012-09-17 16:00:01 -04001057 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001058 if (!UnMount(true))
1059 return false;
1060
1061 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1062 Find_Actual_Block_Device();
1063 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -04001064 if (system(command) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001065 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001066 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001067 ui_print("Done.\n");
1068 return true;
1069 } else {
1070 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1071 return false;
1072 }
1073 return true;
1074 }
1075 else
1076 return Wipe_RMRF();
1077
1078 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001079}
1080
Dees_Troy38bd7602012-09-14 13:33:53 -04001081bool TWPartition::Wipe_MTD() {
1082 if (!UnMount(true))
1083 return false;
1084
1085 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1086
1087 mtd_scan_partitions();
1088 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1089 if (mtd == NULL) {
1090 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1091 return false;
1092 }
1093
1094 MtdWriteContext* ctx = mtd_write_partition(mtd);
1095 if (ctx == NULL) {
1096 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1097 return false;
1098 }
1099 if (mtd_erase_blocks(ctx, -1) == -1) {
1100 mtd_write_close(ctx);
1101 LOGE("Failed to format '%s'", MTD_Name.c_str());
1102 return false;
1103 }
1104 if (mtd_write_close(ctx) != 0) {
1105 LOGE("Failed to close '%s'", MTD_Name.c_str());
1106 return false;
1107 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001108 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001109 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001110 ui_print("Done.\n");
1111 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001112}
1113
1114bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001115 char cmd[512];
1116
1117 if (!Mount(true))
1118 return false;
1119
Dees_Troye58d5262012-09-21 12:27:57 -04001120 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1121 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001122
1123 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001124 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001125 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001126 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001127}
1128
1129bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001130 char cmd[256];
1131
1132 // This handles wiping data on devices with "sdcard" in /data/media
1133 if (!Mount(true))
1134 return false;
1135
1136 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001137
1138 DIR* d;
1139 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001140 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001141 struct dirent* de;
1142 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001143 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1144 // The media folder is the "internal sdcard"
1145 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1146 // the media folder for multi-user.
1147 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
Dees_Troy38bd7602012-09-14 13:33:53 -04001148
1149 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001150 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001151 }
1152 closedir(d);
Dees_Troy16b74352012-11-14 22:27:31 +00001153 ui_print("Done.\n");
1154 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001155 }
Dees_Troy16b74352012-11-14 22:27:31 +00001156 ui_print("Dirent failed to open /data, error!\n");
1157 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001158}
1159
1160bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001161 char back_name[255], split_index[5];
1162 string Full_FileName, Split_FileName, Tar_Args, Command;
1163 int use_compression, index, backup_count;
1164 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001165 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001166
1167 if (!Mount(true))
1168 return false;
1169
Dees_Troy2c50e182012-09-26 20:05:28 -04001170 if (Backup_Path == "/and-sec") {
1171 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1172 ui_print("Backing up %s...\n", "Android Secure");
1173 } else {
1174 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1175 ui_print("Backing up %s...\n", Display_Name.c_str());
1176 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001177
1178 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1179 if (use_compression)
1180 Tar_Args = "-cz";
1181 else
1182 Tar_Args = "-c";
1183
1184 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1185 Backup_FileName = back_name;
1186
Dees_Troy43d8b002012-09-17 16:00:01 -04001187 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1188 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001189 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001190 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001191 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001192 if (backup_count < 1) {
1193 LOGE("Error generating file list!\n");
1194 return false;
1195 }
1196 for (index=0; index<backup_count; index++) {
1197 sprintf(split_index, "%03i", index);
1198 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1199 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1200 LOGI("Backup command: '%s'\n", Command.c_str());
1201 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1202 system(Command.c_str()); // sending backup command formed earlier above
1203
Dees_Troy7c2dec82012-09-26 09:49:14 -04001204 file_size = TWFunc::Get_File_Size(Full_FileName);
1205 if (file_size == 0) {
1206 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str()); // oh noes! file size is 0, abort! abort!
Dees_Troy4a2a1262012-09-18 09:33:47 -04001207 return false;
1208 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001209 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001210 }
1211 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1212 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001213 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001214 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001215 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001216 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001217 else
Dees_Troye58d5262012-09-21 12:27:57 -04001218 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001219 LOGI("Backup command: '%s'\n", Command.c_str());
1220 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001221 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1222 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1223 return false;
1224 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001225 }
1226 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001227}
1228
1229bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001230 char back_name[255];
1231 string Full_FileName, Command;
1232 int use_compression;
1233
Dees_Troyb46a6842012-09-25 11:06:46 -04001234 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001235 ui_print("Backing up %s...\n", Display_Name.c_str());
1236
1237 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1238 Backup_FileName = back_name;
1239
1240 Full_FileName = backup_folder + "/" + Backup_FileName;
1241
1242 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1243 LOGI("Backup command: '%s'\n", Command.c_str());
1244 system(Command.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001245 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1246 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001247 return false;
1248 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001249 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001250}
1251
1252bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001253 char back_name[255];
1254 string Full_FileName, Command;
1255 int use_compression;
1256
Dees_Troyb46a6842012-09-25 11:06:46 -04001257 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001258 ui_print("Backing up %s...\n", Display_Name.c_str());
1259
1260 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1261 Backup_FileName = back_name;
1262
1263 Full_FileName = backup_folder + "/" + Backup_FileName;
1264
1265 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1266 LOGI("Backup command: '%s'\n", Command.c_str());
1267 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001268 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1269 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1270 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1271 return false;
1272 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001273 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001274}
1275
Gary Peck43acadf2012-11-21 21:19:01 -08001276bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1277 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001278 int index = 0;
1279 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001280
Dees_Troye58d5262012-09-21 12:27:57 -04001281 if (Has_Android_Secure) {
1282 ui_print("Wiping android secure...\n");
1283 if (!Wipe_AndSec())
1284 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001285 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001286 ui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001287 if (!Wipe(Restore_File_System))
1288 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001289 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001290
1291 if (!Mount(true))
1292 return false;
1293
Dees_Troyda8b55a2012-12-12 19:18:30 +00001294 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001295 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001296 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001297 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001298 // Backup is multiple archives
1299 LOGI("Backup is multiple archives.\n");
1300 sprintf(split_index, "%03i", index);
1301 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1302 while (TWFunc::Path_Exists(Full_FileName)) {
1303 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troy6da522d2012-10-12 15:19:33 -04001304 Command = "tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001305 LOGI("Restore command: '%s'\n", Command.c_str());
1306 system(Command.c_str());
1307 index++;
1308 sprintf(split_index, "%03i", index);
1309 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1310 }
1311 if (index == 0) {
1312 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1313 return false;
1314 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001315 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001316 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001317 LOGI("Restore command: '%s'\n", Command.c_str());
1318 system(Command.c_str());
1319 }
1320 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001321}
1322
1323bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001324 string Full_FileName, Command;
1325
Dees_Troyda8b55a2012-12-12 19:18:30 +00001326 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001327 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001328
1329 if (!Find_Partition_Size()) {
1330 LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
1331 return false;
1332 }
1333 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1334 if (backup_size > Size) {
1335 LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
1336 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1337 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1338 return false;
1339 }
1340
1341 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001342 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1343 LOGI("Restore command: '%s'\n", Command.c_str());
1344 system(Command.c_str());
1345 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001346}
1347
1348bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001349 string Full_FileName, Command;
1350
1351 ui_print("Restoring %s...\n", Display_Name.c_str());
1352 Full_FileName = restore_folder + "/" + Backup_FileName;
1353 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1354 Command = "erase_image " + MTD_Name;
1355 LOGI("Erase command: '%s'\n", Command.c_str());
1356 system(Command.c_str());
1357 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1358 LOGI("Restore command: '%s'\n", Command.c_str());
1359 system(Command.c_str());
1360 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001361}
Dees_Troy5bf43922012-09-07 16:07:55 -04001362
1363bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001364 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001365
Dees_Troyab10ee22012-09-21 14:27:30 -04001366 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001367 return false;
1368
Dees_Troy0550cfb2012-10-13 11:56:13 -04001369 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001370 if (Removable || Is_Encrypted) {
1371 if (!Mount(false))
1372 return true;
1373 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001374 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001375
1376 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001377 if (!ret || Size == 0) {
1378 if (!Get_Size_Via_df(Display_Error)) {
1379 if (!Was_Already_Mounted)
1380 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001381 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001382 }
1383 }
Dees_Troy51127312012-09-08 13:08:49 -04001384
Dees_Troy5bf43922012-09-07 16:07:55 -04001385 if (Has_Data_Media) {
1386 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001387 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001388 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1389 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001390 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001391 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001392 int bak = (int)(Backup_Size / 1048576LLU);
1393 int total = (int)(Size / 1048576LLU);
1394 int us = (int)(Used / 1048576LLU);
1395 int fre = (int)(Free / 1048576LLU);
1396 int datmed = (int)(data_media_used / 1048576LLU);
1397 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 -04001398 } else {
1399 if (!Was_Already_Mounted)
1400 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001401 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001402 }
Dees_Troye58d5262012-09-21 12:27:57 -04001403 } else if (Has_Android_Secure) {
1404 if (Mount(Display_Error))
1405 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001406 else {
1407 if (!Was_Already_Mounted)
1408 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001409 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001410 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001411 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001412 if (!Was_Already_Mounted)
1413 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001414 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001415}
Dees_Troy38bd7602012-09-14 13:33:53 -04001416
1417void TWPartition::Find_Actual_Block_Device(void) {
1418 if (Is_Decrypted) {
1419 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001420 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001421 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001422 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001423 Is_Present = true;
1424 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001425 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001426 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001427 Is_Present = true;
1428 } else
1429 Is_Present = false;
1430}
1431
1432void TWPartition::Recreate_Media_Folder(void) {
1433 string Command;
1434
1435 if (!Mount(true)) {
1436 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001437 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001438 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001439 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001440 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001441 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001442 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001443 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001444 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001445}
Dees_Troye58d5262012-09-21 12:27:57 -04001446
1447void TWPartition::Recreate_AndSec_Folder(void) {
1448 string Command;
1449
1450 if (!Has_Android_Secure)
1451 return;
1452
1453 if (!Mount(true)) {
1454 LOGE("Unable to recreate android secure folder.\n");
1455 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1456 LOGI("Recreating android secure folder.\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001457 Command = "umount " + Symlink_Mount_Point;
1458 system(Command.c_str());
Dees_Troyce2fe772012-09-28 12:34:33 -04001459 Command = "cd " + Mount_Point + " && mkdir .android_secure";
1460 system(Command.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001461 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1462 system(Command.c_str());
1463 }
1464}