blob: 7eac409f4c8df601cc554e41fca33e266ef987d1 [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_Troy5bf43922012-09-07 16:07:55 -040047}
Dees_Troy51a0e822012-09-05 15:24:24 -040048
bigbiff bigbiff9c754052013-01-09 09:09:08 -050049using namespace std;
50
Dees_Troy51a0e822012-09-05 15:24:24 -040051TWPartition::TWPartition(void) {
52 Can_Be_Mounted = false;
53 Can_Be_Wiped = false;
54 Wipe_During_Factory_Reset = false;
55 Wipe_Available_in_GUI = false;
56 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040057 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040058 SubPartition_Of = "";
59 Symlink_Path = "";
60 Symlink_Mount_Point = "";
61 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040062 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040063 Actual_Block_Device = "";
64 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040065 Alternate_Block_Device = "";
66 Removable = false;
67 Is_Present = false;
68 Length = 0;
69 Size = 0;
70 Used = 0;
71 Free = 0;
72 Backup_Size = 0;
73 Can_Be_Encrypted = false;
74 Is_Encrypted = false;
75 Is_Decrypted = false;
76 Decrypted_Block_Device = "";
77 Display_Name = "";
78 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040079 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040080 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040081 Backup_Method = NONE;
82 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040083 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040084 Is_Storage = false;
85 Storage_Path = "";
86 Current_File_System = "";
87 Fstab_File_System = "";
88 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +000089 Ignore_Blkid = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040090}
91
92TWPartition::~TWPartition(void) {
93 // Do nothing
94}
95
Dees_Troy5bf43922012-09-07 16:07:55 -040096bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
97 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
98 int line_len = Line.size(), index = 0, item_index = 0;
99 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400100 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 strncpy(full_line, Line.c_str(), line_len);
102
Dees_Troy51127312012-09-08 13:08:49 -0400103 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400104 if (full_line[index] <= 32)
105 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400106 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400107 Mount_Point = full_line;
108 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400109 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400110 index = Mount_Point.size();
111 while (index < line_len) {
112 while (index < line_len && full_line[index] == '\0')
113 index++;
114 if (index >= line_len)
115 continue;
116 ptr = full_line + index;
117 if (item_index == 0) {
118 // File System
119 Fstab_File_System = ptr;
120 Current_File_System = ptr;
121 item_index++;
122 } else if (item_index == 1) {
123 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400124 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400125 MTD_Name = ptr;
126 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400127 } else if (Fstab_File_System == "bml") {
128 if (Mount_Point == "/boot")
129 MTD_Name = "boot";
130 else if (Mount_Point == "/recovery")
131 MTD_Name = "recovery";
132 Primary_Block_Device = ptr;
133 if (*ptr != '/')
134 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 -0400135 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400136 if (Display_Error)
137 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
138 else
139 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
140 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400141 } else {
142 Primary_Block_Device = ptr;
143 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400144 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400145 item_index++;
146 } else if (item_index > 1) {
147 if (*ptr == '/') {
148 // Alternate Block Device
149 Alternate_Block_Device = ptr;
150 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
151 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
152 // Partition length
153 ptr += 7;
154 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400155 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
156 // Custom flags, save for later so that new values aren't overwritten by defaults
157 ptr += 6;
158 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000159 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400160 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
161 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400162 } else {
163 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400164 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400165 }
166 }
167 while (index < line_len && full_line[index] != '\0')
168 index++;
169 }
170
171 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
172 if (Display_Error)
173 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
174 else
175 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
176 return 0;
177 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400178 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400179 Setup_File_System(Display_Error);
180 if (Mount_Point == "/system") {
181 Display_Name = "System";
182 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400183 } else if (Mount_Point == "/data") {
184 Display_Name = "Data";
185 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400186 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400187#ifdef RECOVERY_SDCARD_ON_DATA
188 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400189 Is_Storage = true;
190 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000191 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400192 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
193 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400194 Symlink_Mount_Point = "/emmc";
195 } else {
196 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400197 Symlink_Mount_Point = "/sdcard";
198 }
Dees_Troy16b74352012-11-14 22:27:31 +0000199 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
200 Storage_Path = "/data/media/0";
201 Symlink_Path = Storage_Path;
202 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
203 UnMount(true);
204 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400205#endif
206#ifdef TW_INCLUDE_CRYPTO
207 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400208 char crypto_blkdev[255];
209 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
210 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400211 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400212 DataManager::SetValue(TW_IS_DECRYPTED, 1);
213 Is_Encrypted = true;
214 Is_Decrypted = true;
215 Decrypted_Block_Device = crypto_blkdev;
216 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
217 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 Is_Encrypted = true;
219 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800220 Can_Be_Mounted = false;
221 Current_File_System = "emmc";
222 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400223 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
224 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
225 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800226 } else {
227 // Filesystem is not encrypted and the mount
228 // succeeded, so get it back to the original
229 // unmounted state
230 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400231 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400232 #ifdef RECOVERY_SDCARD_ON_DATA
233 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
234 Recreate_Media_Folder();
235 #endif
236#else
237 #ifdef RECOVERY_SDCARD_ON_DATA
238 Recreate_Media_Folder();
239 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400240#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400241 } else if (Mount_Point == "/cache") {
242 Display_Name = "Cache";
243 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400244 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400245 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troyb46a6842012-09-25 11:06:46 -0400246 LOGI("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500247 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
248 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400249 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400251 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 Display_Name = "DataData";
253 Is_SubPartition = true;
254 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400255 DataManager::SetValue(TW_HAS_DATADATA, 1);
256 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400257 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 Display_Name = "SD-Ext";
259 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400260 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400261 } else if (Mount_Point == "/boot") {
262 Display_Name = "Boot";
263 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400264 }
265#ifdef TW_EXTERNAL_STORAGE_PATH
266 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
267 Is_Storage = true;
268 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400269 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400270 }
271#else
272 if (Mount_Point == "/sdcard") {
273 Is_Storage = true;
274 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400275 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400276#ifndef RECOVERY_SDCARD_ON_DATA
277 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400278 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400279#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400280 }
281#endif
282#ifdef TW_INTERNAL_STORAGE_PATH
283 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
284 Is_Storage = true;
285 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400286#ifndef RECOVERY_SDCARD_ON_DATA
287 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400288 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400289#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400290 }
291#else
292 if (Mount_Point == "/emmc") {
293 Is_Storage = true;
294 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400295#ifndef RECOVERY_SDCARD_ON_DATA
296 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400297 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400298#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400299 }
300#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400301 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400302 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400303 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400304 }
305
Dees_Troy51127312012-09-08 13:08:49 -0400306 // Process any custom flags
307 if (Flags.size() > 0)
308 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400309 return true;
310}
311
312bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
313 char flags[MAX_FSTAB_LINE_LENGTH];
314 int flags_len, index = 0;
315 char* ptr;
316
317 strcpy(flags, Flags.c_str());
318 flags_len = Flags.size();
319 for (index = 0; index < flags_len; index++) {
320 if (flags[index] == ';')
321 flags[index] = '\0';
322 }
323
324 index = 0;
325 while (index < flags_len) {
326 while (index < flags_len && flags[index] == '\0')
327 index++;
328 if (index >= flags_len)
329 continue;
330 ptr = flags + index;
331 if (strcmp(ptr, "removable") == 0) {
332 Removable = true;
333 } else if (strcmp(ptr, "storage") == 0) {
334 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400335 } else if (strcmp(ptr, "canbewiped") == 0) {
336 Can_Be_Wiped = true;
337 } else if (strcmp(ptr, "wipeingui") == 0) {
338 Can_Be_Wiped = true;
339 Wipe_Available_in_GUI = true;
340 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
341 Can_Be_Wiped = true;
342 Wipe_Available_in_GUI = true;
343 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400344 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
345 ptr += 13;
346 Is_SubPartition = true;
347 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000348 } else if (strcmp(ptr, "ignoreblkid") == 0) {
349 Ignore_Blkid = true;
Dees_Troy51127312012-09-08 13:08:49 -0400350 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
351 ptr += 8;
352 Symlink_Path = ptr;
353 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
354 ptr += 8;
355 Display_Name = ptr;
356 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
357 ptr += 10;
358 Format_Block_Size = atoi(ptr);
359 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
360 ptr += 7;
361 Length = atoi(ptr);
362 } else {
363 if (Display_Error)
364 LOGE("Unhandled flag: '%s'\n", ptr);
365 else
366 LOGI("Unhandled flag: '%s'\n", ptr);
367 }
368 while (index < flags_len && flags[index] != '\0')
369 index++;
370 }
371 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400372}
373
Dees_Troy5bf43922012-09-07 16:07:55 -0400374bool TWPartition::Is_File_System(string File_System) {
375 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400376 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400377 File_System == "ext4" ||
378 File_System == "vfat" ||
379 File_System == "ntfs" ||
380 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500381 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400382 File_System == "auto")
383 return true;
384 else
385 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400386}
387
Dees_Troy5bf43922012-09-07 16:07:55 -0400388bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400389 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400390 return true;
391 else
392 return false;
393}
394
Dees_Troy51127312012-09-08 13:08:49 -0400395bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400396 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400397 if (mkdir(Path.c_str(), 0777) == -1) {
398 if (Display_Error)
399 LOGE("Can not create '%s' folder.\n", Path.c_str());
400 else
401 LOGI("Can not create '%s' folder.\n", Path.c_str());
402 return false;
403 } else {
404 LOGI("Created '%s' folder.\n", Path.c_str());
405 return true;
406 }
407 }
408 return true;
409}
410
Dees_Troy5bf43922012-09-07 16:07:55 -0400411void TWPartition::Setup_File_System(bool Display_Error) {
412 struct statfs st;
413
414 Can_Be_Mounted = true;
415 Can_Be_Wiped = true;
416
Dees_Troy5bf43922012-09-07 16:07:55 -0400417 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400418 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400419 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
420 Backup_Name = Display_Name;
421 Backup_Method = FILES;
422}
423
424void TWPartition::Setup_Image(bool 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;
Gary Peck82599a82012-11-21 16:23:12 -0800427 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400428 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800429 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 Backup_Method = FLASH_UTILS;
431 else
Gary Peck82599a82012-11-21 16:23:12 -0800432 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 -0400433 if (Find_Partition_Size()) {
434 Used = Size;
435 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400436 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400437 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400438 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400439 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400440 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 }
442}
443
Dees_Troye58d5262012-09-21 12:27:57 -0400444void TWPartition::Setup_AndSec(void) {
445 Backup_Name = "and-sec";
446 Has_Android_Secure = true;
447 Symlink_Path = Mount_Point + "/.android_secure";
448 Symlink_Mount_Point = "/and-sec";
449 Backup_Path = Symlink_Mount_Point;
450 Make_Dir("/and-sec", true);
451 Recreate_AndSec_Folder();
452}
453
Dees_Troy5bf43922012-09-07 16:07:55 -0400454void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
455 char device[512], realDevice[512];
456
457 strcpy(device, Block.c_str());
458 memset(realDevice, 0, sizeof(realDevice));
459 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
460 {
461 strcpy(device, realDevice);
462 memset(realDevice, 0, sizeof(realDevice));
463 }
464
465 if (device[0] != '/') {
466 if (Display_Error)
467 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
468 else
469 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
470 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400471 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 Block = device;
473 return;
474 }
475}
476
Dees_Troy8e337f32012-10-13 22:07:49 -0400477void TWPartition::Mount_Storage_Retry(void) {
478 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500479 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400480 int retry_count = 5;
481 while (retry_count > 0 && !Mount(false)) {
482 usleep(500000);
483 retry_count--;
484 }
485 Mount(true);
486 }
487}
488
Dees_Troy38bd7602012-09-14 13:33:53 -0400489bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
490 FILE *fp = NULL;
491 char line[255];
492
493 fp = fopen("/proc/mtd", "rt");
494 if (fp == NULL) {
495 LOGE("Device does not support /proc/mtd\n");
496 return false;
497 }
498
499 while (fgets(line, sizeof(line), fp) != NULL)
500 {
501 char device[32], label[32];
502 unsigned long size = 0;
503 char* fstype = NULL;
504 int deviceId;
505
506 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
507
508 // Skip header and blank lines
509 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
510 continue;
511
512 // Strip off the trailing " from the label
513 label[strlen(label)-1] = '\0';
514
515 if (strcmp(label, MTD_Name.c_str()) == 0) {
516 // We found our device
517 // Strip off the trailing : from the device
518 device[strlen(device)-1] = '\0';
519 if (sscanf(device,"mtd%d", &deviceId) == 1) {
520 sprintf(device, "/dev/block/mtdblock%d", deviceId);
521 Primary_Block_Device = device;
522 }
523 }
524 }
525 fclose(fp);
526
527 return false;
528}
529
Dees_Troy51127312012-09-08 13:08:49 -0400530bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
531 struct statfs st;
532 string Local_Path = Mount_Point + "/.";
533
534 if (!Mount(Display_Error))
535 return false;
536
537 if (statfs(Local_Path.c_str(), &st) != 0) {
538 if (!Removable) {
539 if (Display_Error)
540 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
541 else
542 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
543 }
544 return false;
545 }
546 Size = (st.f_blocks * st.f_bsize);
547 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
548 Free = (st.f_bfree * st.f_bsize);
549 Backup_Size = Used;
550 return true;
551}
552
553bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400554 FILE* fp;
555 char command[255], line[512];
556 int include_block = 1;
557 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500558 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400559
560 if (!Mount(Display_Error))
561 return false;
562
Dees_Troy38bd7602012-09-14 13:33:53 -0400563 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400564 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500565 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400566 fp = fopen("/tmp/dfoutput.txt", "rt");
567 if (fp == NULL) {
568 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400569 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400570 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400571
572 while (fgets(line, sizeof(line), fp) != NULL)
573 {
574 unsigned long blocks, used, available;
575 char device[64];
576 char tmpString[64];
577
578 if (strncmp(line, "Filesystem", 10) == 0)
579 continue;
580 if (strlen(line) < min_len) {
581 include_block = 0;
582 continue;
583 }
584 if (include_block) {
585 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
586 } else {
587 // The device block string is so long that the df information is on the next line
588 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400589 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400590 while (tmpString[space_count] == 32)
591 space_count++;
592 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
593 }
594
595 // Adjust block size to byte size
596 Size = blocks * 1024ULL;
597 Used = used * 1024ULL;
598 Free = available * 1024ULL;
599 Backup_Size = Used;
600 }
601 fclose(fp);
602 return true;
603}
604
Dees_Troy5bf43922012-09-07 16:07:55 -0400605bool TWPartition::Find_Partition_Size(void) {
606 FILE* fp;
607 char line[512];
608 string tmpdevice;
609
610 // In this case, we'll first get the partitions we care about (with labels)
611 fp = fopen("/proc/partitions", "rt");
612 if (fp == NULL)
613 return false;
614
615 while (fgets(line, sizeof(line), fp) != NULL)
616 {
617 unsigned long major, minor, blocks;
618 char device[512];
619 char tmpString[64];
620
Dees_Troy63c8df72012-09-10 14:02:05 -0400621 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400622 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
623
624 tmpdevice = "/dev/block/";
625 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400626 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400627 // Adjust block size to byte size
628 Size = blocks * 1024ULL;
629 fclose(fp);
630 return true;
631 }
632 }
633 fclose(fp);
634 return false;
635}
636
Dees_Troy5bf43922012-09-07 16:07:55 -0400637bool TWPartition::Is_Mounted(void) {
638 if (!Can_Be_Mounted)
639 return false;
640
641 struct stat st1, st2;
642 string test_path;
643
644 // Check to see if the mount point directory exists
645 test_path = Mount_Point + "/.";
646 if (stat(test_path.c_str(), &st1) != 0) return false;
647
648 // Check to see if the directory above the mount point exists
649 test_path = Mount_Point + "/../.";
650 if (stat(test_path.c_str(), &st2) != 0) return false;
651
652 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
653 int ret = (st1.st_dev != st2.st_dev) ? true : false;
654
655 return ret;
656}
657
658bool TWPartition::Mount(bool Display_Error) {
659 if (Is_Mounted()) {
660 return true;
661 } else if (!Can_Be_Mounted) {
662 return false;
663 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400664
665 Find_Actual_Block_Device();
666
667 // Check the current file system before mounting
668 Check_FS_Type();
Dees_Troy22042032012-12-18 21:23:08 +0000669 if (Fstab_File_System == "yaffs2") {
670 // mount an MTD partition as a YAFFS2 filesystem.
671 mtd_scan_partitions();
672 const MtdPartition* partition;
673 partition = mtd_find_partition_by_name(MTD_Name.c_str());
674 if (partition == NULL) {
675 LOGE("Failed to find '%s' partition to mount at '%s'\n",
676 MTD_Name.c_str(), Mount_Point.c_str());
677 return false;
678 }
679 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
680 if (Display_Error)
681 LOGE("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
682 else
683 LOGI("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
684 return false;
685 } else
686 return true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500687 } else if (Fstab_File_System == "exfat") {
688 string cmd = "/sbin/exfat-fuse " + Actual_Block_Device + " " + Mount_Point;
689 LOGI("cmd: %s\n", cmd.c_str());
690 string result;
691 if (TWFunc::Exec_Cmd(cmd, result) != 0)
692 return false;
Dees_Troy22042032012-12-18 21:23:08 +0000693 } 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 -0400694 if (Display_Error)
695 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
696 else
697 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy9350b8d2012-09-27 12:38:38 -0400698 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 -0400699 return false;
700 } else {
701 if (Removable)
702 Update_Size(Display_Error);
703
704 if (!Symlink_Mount_Point.empty()) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500705 mount(Symlink_Path.c_str(), Symlink_Mount_Point.c_str(), Fstab_File_System.c_str(), NULL, NULL);
Dees_Troy51127312012-09-08 13:08:49 -0400706 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400707 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400708 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400709 return true;
710}
711
712bool TWPartition::UnMount(bool Display_Error) {
713 if (Is_Mounted()) {
714 int never_unmount_system;
715
716 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
717 if (never_unmount_system == 1 && Mount_Point == "/system")
718 return true; // Never unmount system if you're not supposed to unmount it
719
Dees_Troy38bd7602012-09-14 13:33:53 -0400720 if (!Symlink_Mount_Point.empty())
721 umount(Symlink_Mount_Point.c_str());
722
Dees_Troy5bf43922012-09-07 16:07:55 -0400723 if (umount(Mount_Point.c_str()) != 0) {
724 if (Display_Error)
725 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
726 else
727 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
728 return false;
729 } else
730 return true;
731 } else {
732 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400733 }
734}
735
Gary Peck43acadf2012-11-21 21:19:01 -0800736bool TWPartition::Wipe(string New_File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400737 if (!Can_Be_Wiped) {
738 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
739 return false;
740 }
741
Dees_Troyc51f1f92012-09-20 15:32:13 -0400742 if (Mount_Point == "/cache")
743 tmplog_offset = 0;
744
Dees_Troy38bd7602012-09-14 13:33:53 -0400745 if (Has_Data_Media)
746 return Wipe_Data_Without_Wiping_Media();
747
748 int check;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800749 bool wiped = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400750 DataManager::GetValue(TW_RM_RF_VAR, check);
Gary Pecke8bc5d72012-12-21 06:45:25 -0800751
Dees_Troy38bd7602012-09-14 13:33:53 -0400752 if (check)
Gary Pecke8bc5d72012-12-21 06:45:25 -0800753 wiped = Wipe_RMRF();
754 else if (New_File_System == "ext4")
755 wiped = Wipe_EXT4();
756 else if (New_File_System == "ext2" || New_File_System == "ext3")
757 wiped = Wipe_EXT23(New_File_System);
758 else if (New_File_System == "vfat")
759 wiped = Wipe_FAT();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500760 if (New_File_System == "exfat")
761 wiped = Wipe_EXFAT();
Gary Pecke8bc5d72012-12-21 06:45:25 -0800762 else if (New_File_System == "yaffs2")
763 wiped = Wipe_MTD();
764 else {
765 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
766 return false;
767 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400768
Gary Pecke8bc5d72012-12-21 06:45:25 -0800769 if (wiped) {
770 Setup_File_System(false);
771 if (Is_Encrypted && !Is_Decrypted) {
772 // just wiped an encrypted partition back to its unencrypted state
773 Is_Encrypted = false;
774 Is_Decrypted = false;
775 Decrypted_Block_Device = "";
776 if (Mount_Point == "/data") {
777 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
778 DataManager::SetValue(TW_IS_DECRYPTED, 0);
779 }
780 }
781 }
782 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400783}
784
Gary Peck43acadf2012-11-21 21:19:01 -0800785bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800786 if (Is_File_System(Current_File_System))
787 return Wipe(Current_File_System);
788 else
789 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -0800790}
791
Dees_Troye58d5262012-09-21 12:27:57 -0400792bool TWPartition::Wipe_AndSec(void) {
793 if (!Has_Android_Secure)
794 return false;
795
Dees_Troye58d5262012-09-21 12:27:57 -0400796 if (!Mount(true))
797 return false;
798
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500799 ui_print("Wiping .android_secure\n");
800 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -0400801 return true;
802}
803
Dees_Troy51a0e822012-09-05 15:24:24 -0400804bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400805 if (Backup_Method == FILES)
806 return Backup_Tar(backup_folder);
807 else if (Backup_Method == DD)
808 return Backup_DD(backup_folder);
809 else if (Backup_Method == FLASH_UTILS)
810 return Backup_Dump_Image(backup_folder);
811 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
812 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400813}
814
Dees_Troy43d8b002012-09-17 16:00:01 -0400815bool TWPartition::Check_MD5(string restore_folder) {
816 string Full_Filename;
817 char split_filename[512];
818 int index = 0;
819
820 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400821 if (!TWFunc::Path_Exists(Full_Filename)) {
822 // This is a split archive, we presume
823 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
824 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
825 if (TWFunc::Check_MD5(split_filename) == 0) {
826 LOGE("MD5 failed to match on '%s'.\n", split_filename);
827 return false;
828 }
829 index++;
830 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400831 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400832 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400833 } else {
834 // Single file archive
835 if (TWFunc::Check_MD5(Full_Filename) == 0) {
836 LOGE("MD5 failed to match on '%s'.\n", split_filename);
837 return false;
838 } else
839 return true;
840 }
841 return false;
842}
843
Dees_Troy51a0e822012-09-05 15:24:24 -0400844bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -0800845 size_t first_period, second_period;
846 string Restore_File_System;
847
848 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
849 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
850
851 // Parse backup filename to extract the file system before wiping
852 first_period = Backup_FileName.find(".");
853 if (first_period == string::npos) {
854 LOGE("Unable to find file system (first period).\n");
855 return false;
856 }
857 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
858 second_period = Restore_File_System.find(".");
859 if (second_period == string::npos) {
860 LOGE("Unable to find file system (second period).\n");
861 return false;
862 }
863 Restore_File_System.resize(second_period);
864 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
865
866 if (Is_File_System(Restore_File_System))
867 return Restore_Tar(restore_folder, Restore_File_System);
868 else if (Is_Image(Restore_File_System)) {
869 if (Restore_File_System == "emmc")
870 return Restore_DD(restore_folder);
871 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
872 return Restore_Flash_Image(restore_folder);
873 }
874
Dees_Troy38bd7602012-09-14 13:33:53 -0400875 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
876 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400877}
878
879string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400880 if (Backup_Method == NONE)
881 return "none";
882 else if (Backup_Method == FILES)
883 return "files";
884 else if (Backup_Method == DD)
885 return "dd";
886 else if (Backup_Method == FLASH_UTILS)
887 return "flash_utils";
888 else
889 return "undefined";
890 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400891}
892
893bool TWPartition::Decrypt(string Password) {
894 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400895 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400896 return 1;
897}
898
899bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400900 bool Save_Data_Media = Has_Data_Media;
901
902 if (!UnMount(true))
903 return false;
904
Dees_Troy38bd7602012-09-14 13:33:53 -0400905 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800906 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400907 Has_Data_Media = Save_Data_Media;
908 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
909 Recreate_Media_Folder();
910 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400911 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400912 return true;
913 } else {
914 Has_Data_Media = Save_Data_Media;
915 LOGE("Unable to format to remove encryption.\n");
916 return false;
917 }
918 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400919}
920
921void TWPartition::Check_FS_Type() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500922 string blkCommand, result;
923 string blkOutput;
Dees_Troy5bf43922012-09-07 16:07:55 -0400924 char* blk;
925 char* arg;
926 char* ptr;
927
Dees_Troy68cab492012-12-12 19:29:35 +0000928 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
929 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -0400930
Dees_Troy38bd7602012-09-14 13:33:53 -0400931 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400932 if (!Is_Present)
933 return;
Dees_Troy51127312012-09-08 13:08:49 -0400934
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500935 blkCommand = "blkid " + Actual_Block_Device;
936 TWFunc::Exec_Cmd(blkCommand, result);
937 std::stringstream line(result);
938 while (getline(line, blkOutput))
Dees_Troy5bf43922012-09-07 16:07:55 -0400939 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500940 blk = (char*) blkOutput.c_str();
941 ptr = (char*) blkOutput.c_str();
Dees_Troy63c8df72012-09-10 14:02:05 -0400942 while (*ptr > 32 && *ptr != ':') ptr++;
943 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400944 *ptr = 0;
945
946 // Increment by two, but verify that we don't hit a NULL
947 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400948 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400949
950 // Now, find the TYPE field
951 while (1)
952 {
953 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400954 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400955 if (*ptr != 0)
956 {
957 *ptr = 0;
958 ptr++;
959 }
960
961 if (strlen(arg) > 6)
962 {
963 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
964 }
965
966 if (*ptr == 0)
967 {
968 arg = NULL;
969 break;
970 }
971 }
972
973 if (arg && strlen(arg) > 7)
974 {
975 arg += 6; // Skip the TYPE=" portion
976 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
977 }
978 else
979 continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500980 if (Current_File_System != arg) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400981 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
982 Current_File_System = arg;
983 }
984 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400985 return;
986}
987
Gary Peck43acadf2012-11-21 21:19:01 -0800988bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400989 if (!UnMount(true))
990 return false;
991
Dees_Troy43d8b002012-09-17 16:00:01 -0400992 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500993 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -0400994
995 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
996 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500997 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
998 LOGI("mke2fs command: %s\n", command.c_str());
999 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001000 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001001 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001002 ui_print("Done.\n");
1003 return true;
1004 } else {
1005 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1006 return false;
1007 }
1008 } else
1009 return Wipe_RMRF();
1010
1011 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001012}
1013
1014bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001015 if (!UnMount(true))
1016 return false;
1017
Dees_Troy43d8b002012-09-17 16:00:01 -04001018 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001019 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001020
1021 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
1022 Find_Actual_Block_Device();
1023 Command = "make_ext4fs";
1024 if (!Is_Decrypted && Length != 0) {
1025 // Only use length if we're not decrypted
1026 char len[32];
1027 sprintf(len, "%i", Length);
1028 Command += " -l ";
1029 Command += len;
1030 }
1031 Command += " " + Actual_Block_Device;
1032 LOGI("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001033 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001034 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001035 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001036 ui_print("Done.\n");
1037 return true;
1038 } else {
1039 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1040 return false;
1041 }
1042 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001043 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001044
1045 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001046}
1047
1048bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001049 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001050
Dees_Troy43d8b002012-09-17 16:00:01 -04001051 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001052 if (!UnMount(true))
1053 return false;
1054
1055 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1056 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001057 command = "mkdosfs " + Actual_Block_Device;
1058 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001059 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001060 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001061 ui_print("Done.\n");
1062 return true;
1063 } else {
1064 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1065 return false;
1066 }
1067 return true;
1068 }
1069 else
1070 return Wipe_RMRF();
1071
1072 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001073}
1074
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001075bool TWPartition::Wipe_EXFAT() {
1076 string command, result;
1077
1078 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1079 if (!UnMount(true))
1080 return false;
1081
1082 ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
1083 Find_Actual_Block_Device();
1084 command = "mkexfatfs " + Actual_Block_Device;
1085 if (TWFunc::Exec_Cmd(command, result) == 0) {
1086 Recreate_AndSec_Folder();
1087 ui_print("Done.\n");
1088 return true;
1089 } else {
1090 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1091 return false;
1092 }
1093 return true;
1094 }
1095 return false;
1096}
1097
Dees_Troy38bd7602012-09-14 13:33:53 -04001098bool TWPartition::Wipe_MTD() {
1099 if (!UnMount(true))
1100 return false;
1101
1102 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1103
1104 mtd_scan_partitions();
1105 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1106 if (mtd == NULL) {
1107 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1108 return false;
1109 }
1110
1111 MtdWriteContext* ctx = mtd_write_partition(mtd);
1112 if (ctx == NULL) {
1113 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1114 return false;
1115 }
1116 if (mtd_erase_blocks(ctx, -1) == -1) {
1117 mtd_write_close(ctx);
1118 LOGE("Failed to format '%s'", MTD_Name.c_str());
1119 return false;
1120 }
1121 if (mtd_write_close(ctx) != 0) {
1122 LOGE("Failed to close '%s'", MTD_Name.c_str());
1123 return false;
1124 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001125 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001126 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001127 ui_print("Done.\n");
1128 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001129}
1130
1131bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001132 if (!Mount(true))
1133 return false;
1134
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001135 ui_print("Removing all files under '%s'\n", Mount_Point.c_str());
1136 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001137 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001138 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001139}
1140
1141bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001142 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001143
1144 // This handles wiping data on devices with "sdcard" in /data/media
1145 if (!Mount(true))
1146 return false;
1147
1148 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001149
1150 DIR* d;
1151 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001152 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001153 struct dirent* de;
1154 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001155 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1156 // The media folder is the "internal sdcard"
1157 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1158 // the media folder for multi-user.
1159 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001160
1161 dir = "/data/";
1162 dir.append(de->d_name);
1163 TWFunc::removeDir(dir, false);
Dees_Troy38bd7602012-09-14 13:33:53 -04001164 }
1165 closedir(d);
Dees_Troy16b74352012-11-14 22:27:31 +00001166 ui_print("Done.\n");
1167 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001168 }
Dees_Troy16b74352012-11-14 22:27:31 +00001169 ui_print("Dirent failed to open /data, error!\n");
1170 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001171}
1172
1173bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001174 char back_name[255], split_index[5];
1175 string Full_FileName, Split_FileName, Tar_Args, Command;
1176 int use_compression, index, backup_count;
1177 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001178 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001179 twrpTar tar;
1180 vector <string> files;
Dees_Troy43d8b002012-09-17 16:00:01 -04001181
1182 if (!Mount(true))
1183 return false;
1184
Dees_Troy2c50e182012-09-26 20:05:28 -04001185 if (Backup_Path == "/and-sec") {
1186 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1187 ui_print("Backing up %s...\n", "Android Secure");
1188 } else {
1189 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1190 ui_print("Backing up %s...\n", Display_Name.c_str());
1191 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001192
1193 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001194
1195 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1196 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001197 Full_FileName = backup_folder + "/" + Backup_FileName;
1198 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001199 // This backup needs to be split into multiple archives
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001200 ui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001201 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001202 backup_count = tar.Split_Archive(back_name, Full_FileName);
1203 if (backup_count == -1) {
1204 LOGE("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001205 return false;
1206 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001207 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001208 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001209 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001210 if (use_compression) {
1211 if (tar.createTGZ(Backup_Path, Full_FileName) != 0)
1212 return false;
1213 string gzname = Full_FileName + ".gz";
1214 rename(gzname.c_str(), Full_FileName.c_str());
1215 }
1216 else {
1217 if (tar.create(Backup_Path, Full_FileName) != 0)
1218 return false;
1219 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001220 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1221 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1222 return false;
1223 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001224 }
1225 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001226}
1227
1228bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001229 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001230 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001231 int use_compression;
1232
Dees_Troyb46a6842012-09-25 11:06:46 -04001233 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001234 ui_print("Backing up %s...\n", Display_Name.c_str());
1235
1236 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1237 Backup_FileName = back_name;
1238
1239 Full_FileName = backup_folder + "/" + Backup_FileName;
1240
1241 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1242 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001243 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001244 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1245 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001246 return false;
1247 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001248 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001249}
1250
1251bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001252 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001253 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001254 int use_compression;
1255
Dees_Troyb46a6842012-09-25 11:06:46 -04001256 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001257 ui_print("Backing up %s...\n", Display_Name.c_str());
1258
1259 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1260 Backup_FileName = back_name;
1261
1262 Full_FileName = backup_folder + "/" + Backup_FileName;
1263
1264 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1265 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001266 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001267 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1268 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1269 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1270 return false;
1271 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001272 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001273}
1274
Gary Peck43acadf2012-11-21 21:19:01 -08001275bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1276 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001277 int index = 0;
1278 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001279
Dees_Troye58d5262012-09-21 12:27:57 -04001280 if (Has_Android_Secure) {
1281 ui_print("Wiping android secure...\n");
1282 if (!Wipe_AndSec())
1283 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001284 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001285 ui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001286 if (!Wipe(Restore_File_System))
1287 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001288 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001289
1290 if (!Mount(true))
1291 return false;
1292
Dees_Troyda8b55a2012-12-12 19:18:30 +00001293 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001294 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001295 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001296 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001297 if (!TWFunc::Path_Exists(Full_FileName)) {
1298 // Backup is multiple archives
1299 LOGI("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001300 sprintf(split_index, "%03i", index);
1301 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001302 while (TWFunc::Path_Exists(Full_FileName)) {
1303 index++;
1304 ui_print("Restoring archive %i...\n", index);
1305 LOGI("Restoring '%s'...\n", Full_FileName.c_str());
1306 twrpTar tar;
1307 if (tar.extract("/", Full_FileName) != 0)
1308 return false;
1309 sprintf(split_index, "%03i", index);
1310 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1311 }
1312 if (index == 0) {
1313 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1314 return false;
1315 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001316 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001317 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001318 twrpTar tar;
1319 if (tar.extract(Backup_Path, Full_FileName) != 0)
1320 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001321 }
1322 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001323}
1324
1325bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001326 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001327
Dees_Troyda8b55a2012-12-12 19:18:30 +00001328 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001329 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001330
1331 if (!Find_Partition_Size()) {
1332 LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
1333 return false;
1334 }
1335 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1336 if (backup_size > Size) {
1337 LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
1338 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1339 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1340 return false;
1341 }
1342
1343 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001344 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1345 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001346 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001347 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001348}
1349
1350bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001351 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001352
1353 ui_print("Restoring %s...\n", Display_Name.c_str());
1354 Full_FileName = restore_folder + "/" + Backup_FileName;
1355 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1356 Command = "erase_image " + MTD_Name;
1357 LOGI("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001358 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001359 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1360 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001361 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001362 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001363}
Dees_Troy5bf43922012-09-07 16:07:55 -04001364
1365bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001366 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001367
Dees_Troyab10ee22012-09-21 14:27:30 -04001368 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001369 return false;
1370
Dees_Troy0550cfb2012-10-13 11:56:13 -04001371 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001372 if (Removable || Is_Encrypted) {
1373 if (!Mount(false))
1374 return true;
1375 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001376 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001377
1378 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001379 if (!ret || Size == 0) {
1380 if (!Get_Size_Via_df(Display_Error)) {
1381 if (!Was_Already_Mounted)
1382 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001383 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001384 }
1385 }
Dees_Troy51127312012-09-08 13:08:49 -04001386
Dees_Troy5bf43922012-09-07 16:07:55 -04001387 if (Has_Data_Media) {
1388 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001389 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001390 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1391 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001392 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001393 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001394 int bak = (int)(Backup_Size / 1048576LLU);
1395 int total = (int)(Size / 1048576LLU);
1396 int us = (int)(Used / 1048576LLU);
1397 int fre = (int)(Free / 1048576LLU);
1398 int datmed = (int)(data_media_used / 1048576LLU);
1399 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 -04001400 } else {
1401 if (!Was_Already_Mounted)
1402 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001403 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001404 }
Dees_Troye58d5262012-09-21 12:27:57 -04001405 } else if (Has_Android_Secure) {
1406 if (Mount(Display_Error))
1407 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001408 else {
1409 if (!Was_Already_Mounted)
1410 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001411 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001412 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001413 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001414 if (!Was_Already_Mounted)
1415 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001416 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001417}
Dees_Troy38bd7602012-09-14 13:33:53 -04001418
1419void TWPartition::Find_Actual_Block_Device(void) {
1420 if (Is_Decrypted) {
1421 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001422 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001423 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001424 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001425 Is_Present = true;
1426 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001427 return;
1428 }
1429 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001430 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001431 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001432 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001433 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001434 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001435 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001436}
1437
1438void TWPartition::Recreate_Media_Folder(void) {
1439 string Command;
1440
1441 if (!Mount(true)) {
1442 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001443 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001444 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001445 LOGI("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001446 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1447 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001448 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001449}
Dees_Troye58d5262012-09-21 12:27:57 -04001450
1451void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001452 if (!Has_Android_Secure)
1453 return;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001454 LOGI("Creating .android_secure: %s\n", Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001455 if (!Mount(true)) {
1456 LOGE("Unable to recreate android secure folder.\n");
1457 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1458 LOGI("Recreating android secure folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001459 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1460 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1461 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001462 }
1463}