blob: a7adf9984dd801476d332a5b7f22b1e410f587d1 [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;
85}
86
87TWPartition::~TWPartition(void) {
88 // Do nothing
89}
90
Dees_Troy5bf43922012-09-07 16:07:55 -040091bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
92 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
93 int line_len = Line.size(), index = 0, item_index = 0;
94 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -040095 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -040096
97 strncpy(full_line, Line.c_str(), line_len);
98
Dees_Troy51127312012-09-08 13:08:49 -040099 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400100 if (full_line[index] <= 32)
101 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400102 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400103 Mount_Point = full_line;
104 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400105 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400106 index = Mount_Point.size();
107 while (index < line_len) {
108 while (index < line_len && full_line[index] == '\0')
109 index++;
110 if (index >= line_len)
111 continue;
112 ptr = full_line + index;
113 if (item_index == 0) {
114 // File System
115 Fstab_File_System = ptr;
116 Current_File_System = ptr;
117 item_index++;
118 } else if (item_index == 1) {
119 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400120 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400121 MTD_Name = ptr;
122 Find_MTD_Block_Device(MTD_Name);
Dees_Troy38bd7602012-09-14 13:33:53 -0400123 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400124 if (Display_Error)
125 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
126 else
127 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
128 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400129 } else {
130 Primary_Block_Device = ptr;
131 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400133 item_index++;
134 } else if (item_index > 1) {
135 if (*ptr == '/') {
136 // Alternate Block Device
137 Alternate_Block_Device = ptr;
138 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
139 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
140 // Partition length
141 ptr += 7;
142 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400143 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
144 // Custom flags, save for later so that new values aren't overwritten by defaults
145 ptr += 6;
146 Flags = ptr;
Dees_Troy38bd7602012-09-14 13:33:53 -0400147 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
148 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400149 } else {
150 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400151 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400152 }
153 }
154 while (index < line_len && full_line[index] != '\0')
155 index++;
156 }
157
158 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
159 if (Display_Error)
160 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
161 else
162 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
163 return 0;
164 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400165 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 Setup_File_System(Display_Error);
167 if (Mount_Point == "/system") {
168 Display_Name = "System";
169 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400170 } else if (Mount_Point == "/data") {
171 Display_Name = "Data";
172 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400173 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400174#ifdef RECOVERY_SDCARD_ON_DATA
175 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400176 Is_Storage = true;
177 Storage_Path = "/data/media";
Dees_Troy657c3092012-09-10 20:32:10 -0400178 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
179 Make_Dir("/emmc", Display_Error);
180 Symlink_Path = "/data/media";
181 Symlink_Mount_Point = "/emmc";
182 } else {
183 Make_Dir("/sdcard", Display_Error);
184 Symlink_Path = "/data/media";
185 Symlink_Mount_Point = "/sdcard";
186 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400187#endif
188#ifdef TW_INCLUDE_CRYPTO
189 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400190 char crypto_blkdev[255];
191 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
192 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400193 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400194 DataManager::SetValue(TW_IS_DECRYPTED, 1);
195 Is_Encrypted = true;
196 Is_Decrypted = true;
197 Decrypted_Block_Device = crypto_blkdev;
198 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
199 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400200 Is_Encrypted = true;
201 Is_Decrypted = false;
202 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
203 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
204 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400205 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400206 #ifdef RECOVERY_SDCARD_ON_DATA
207 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
208 Recreate_Media_Folder();
209 #endif
210#else
211 #ifdef RECOVERY_SDCARD_ON_DATA
212 Recreate_Media_Folder();
213 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400214#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 } else if (Mount_Point == "/cache") {
216 Display_Name = "Cache";
217 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400218 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400219 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
220 string Recreate_Command = "cd /cache && mkdir recovery";
Dees_Troyb46a6842012-09-25 11:06:46 -0400221 LOGI("Recreating /cache/recovery folder.\n");
Dees_Troyce2fe772012-09-28 12:34:33 -0400222 system(Recreate_Command.c_str());
Dees_Troyb46a6842012-09-25 11:06:46 -0400223 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400225 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400226 Display_Name = "DataData";
227 Is_SubPartition = true;
228 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 DataManager::SetValue(TW_HAS_DATADATA, 1);
230 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400231 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400232 Display_Name = "SD-Ext";
233 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400234 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400235 } else if (Mount_Point == "/boot") {
236 Display_Name = "Boot";
237 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400238 }
239#ifdef TW_EXTERNAL_STORAGE_PATH
240 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
241 Is_Storage = true;
242 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400243 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400244 }
245#else
246 if (Mount_Point == "/sdcard") {
247 Is_Storage = true;
248 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400249 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400250#ifndef RECOVERY_SDCARD_ON_DATA
251 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400252 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400253#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400254 }
255#endif
256#ifdef TW_INTERNAL_STORAGE_PATH
257 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
258 Is_Storage = true;
259 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400260#ifndef RECOVERY_SDCARD_ON_DATA
261 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400262 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400263#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400264 }
265#else
266 if (Mount_Point == "/emmc") {
267 Is_Storage = true;
268 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400269#ifndef RECOVERY_SDCARD_ON_DATA
270 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400271 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400272#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400273 }
274#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400276 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400277 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400278 }
279
Dees_Troy51127312012-09-08 13:08:49 -0400280 // Process any custom flags
281 if (Flags.size() > 0)
282 Process_Flags(Flags, Display_Error);
283
284 return true;
285}
286
287bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
288 char flags[MAX_FSTAB_LINE_LENGTH];
289 int flags_len, index = 0;
290 char* ptr;
291
292 strcpy(flags, Flags.c_str());
293 flags_len = Flags.size();
294 for (index = 0; index < flags_len; index++) {
295 if (flags[index] == ';')
296 flags[index] = '\0';
297 }
298
299 index = 0;
300 while (index < flags_len) {
301 while (index < flags_len && flags[index] == '\0')
302 index++;
303 if (index >= flags_len)
304 continue;
305 ptr = flags + index;
306 if (strcmp(ptr, "removable") == 0) {
307 Removable = true;
308 } else if (strcmp(ptr, "storage") == 0) {
309 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400310 } else if (strcmp(ptr, "canbewiped") == 0) {
311 Can_Be_Wiped = true;
312 } else if (strcmp(ptr, "wipeingui") == 0) {
313 Can_Be_Wiped = true;
314 Wipe_Available_in_GUI = true;
315 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
316 Can_Be_Wiped = true;
317 Wipe_Available_in_GUI = true;
318 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400319 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
320 ptr += 13;
321 Is_SubPartition = true;
322 SubPartition_Of = ptr;
323 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
324 ptr += 8;
325 Symlink_Path = ptr;
326 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
327 ptr += 8;
328 Display_Name = ptr;
329 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
330 ptr += 10;
331 Format_Block_Size = atoi(ptr);
332 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
333 ptr += 7;
334 Length = atoi(ptr);
335 } else {
336 if (Display_Error)
337 LOGE("Unhandled flag: '%s'\n", ptr);
338 else
339 LOGI("Unhandled flag: '%s'\n", ptr);
340 }
341 while (index < flags_len && flags[index] != '\0')
342 index++;
343 }
344 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400345}
346
Dees_Troy5bf43922012-09-07 16:07:55 -0400347bool TWPartition::Is_File_System(string File_System) {
348 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400349 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400350 File_System == "ext4" ||
351 File_System == "vfat" ||
352 File_System == "ntfs" ||
353 File_System == "yaffs2" ||
354 File_System == "auto")
355 return true;
356 else
357 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400358}
359
Dees_Troy5bf43922012-09-07 16:07:55 -0400360bool TWPartition::Is_Image(string File_System) {
361 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400362 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400363 return true;
364 else
365 return false;
366}
367
Dees_Troy51127312012-09-08 13:08:49 -0400368bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400369 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400370 if (mkdir(Path.c_str(), 0777) == -1) {
371 if (Display_Error)
372 LOGE("Can not create '%s' folder.\n", Path.c_str());
373 else
374 LOGI("Can not create '%s' folder.\n", Path.c_str());
375 return false;
376 } else {
377 LOGI("Created '%s' folder.\n", Path.c_str());
378 return true;
379 }
380 }
381 return true;
382}
383
Dees_Troy5bf43922012-09-07 16:07:55 -0400384void TWPartition::Setup_File_System(bool Display_Error) {
385 struct statfs st;
386
387 Can_Be_Mounted = true;
388 Can_Be_Wiped = true;
389
Dees_Troy5bf43922012-09-07 16:07:55 -0400390 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400391 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400392 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
393 Backup_Name = Display_Name;
394 Backup_Method = FILES;
395}
396
397void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400398 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
399 Backup_Name = Display_Name;
400 if (Fstab_File_System == "emmc")
401 Backup_Method = DD;
402 else if (Fstab_File_System == "mtd")
403 Backup_Method = FLASH_UTILS;
404 else
405 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
406 if (Find_Partition_Size()) {
407 Used = Size;
408 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400409 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400410 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400411 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400412 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400413 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400414 }
415}
416
Dees_Troye58d5262012-09-21 12:27:57 -0400417void TWPartition::Setup_AndSec(void) {
418 Backup_Name = "and-sec";
419 Has_Android_Secure = true;
420 Symlink_Path = Mount_Point + "/.android_secure";
421 Symlink_Mount_Point = "/and-sec";
422 Backup_Path = Symlink_Mount_Point;
423 Make_Dir("/and-sec", true);
424 Recreate_AndSec_Folder();
425}
426
Dees_Troy5bf43922012-09-07 16:07:55 -0400427void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
428 char device[512], realDevice[512];
429
430 strcpy(device, Block.c_str());
431 memset(realDevice, 0, sizeof(realDevice));
432 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
433 {
434 strcpy(device, realDevice);
435 memset(realDevice, 0, sizeof(realDevice));
436 }
437
438 if (device[0] != '/') {
439 if (Display_Error)
440 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
441 else
442 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
443 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400445 Block = device;
446 return;
447 }
448}
449
Dees_Troy8e337f32012-10-13 22:07:49 -0400450void TWPartition::Mount_Storage_Retry(void) {
451 // On some devices, storage doesn't want to mount right away, retry and sleep
452 if (!Mount(false)) {
453 int retry_count = 5;
454 while (retry_count > 0 && !Mount(false)) {
455 usleep(500000);
456 retry_count--;
457 }
458 Mount(true);
459 }
460}
461
Dees_Troy38bd7602012-09-14 13:33:53 -0400462bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
463 FILE *fp = NULL;
464 char line[255];
465
466 fp = fopen("/proc/mtd", "rt");
467 if (fp == NULL) {
468 LOGE("Device does not support /proc/mtd\n");
469 return false;
470 }
471
472 while (fgets(line, sizeof(line), fp) != NULL)
473 {
474 char device[32], label[32];
475 unsigned long size = 0;
476 char* fstype = NULL;
477 int deviceId;
478
479 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
480
481 // Skip header and blank lines
482 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
483 continue;
484
485 // Strip off the trailing " from the label
486 label[strlen(label)-1] = '\0';
487
488 if (strcmp(label, MTD_Name.c_str()) == 0) {
489 // We found our device
490 // Strip off the trailing : from the device
491 device[strlen(device)-1] = '\0';
492 if (sscanf(device,"mtd%d", &deviceId) == 1) {
493 sprintf(device, "/dev/block/mtdblock%d", deviceId);
494 Primary_Block_Device = device;
495 }
496 }
497 }
498 fclose(fp);
499
500 return false;
501}
502
Dees_Troy51127312012-09-08 13:08:49 -0400503bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
504 struct statfs st;
505 string Local_Path = Mount_Point + "/.";
506
507 if (!Mount(Display_Error))
508 return false;
509
510 if (statfs(Local_Path.c_str(), &st) != 0) {
511 if (!Removable) {
512 if (Display_Error)
513 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
514 else
515 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
516 }
517 return false;
518 }
519 Size = (st.f_blocks * st.f_bsize);
520 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
521 Free = (st.f_bfree * st.f_bsize);
522 Backup_Size = Used;
523 return true;
524}
525
526bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400527 FILE* fp;
528 char command[255], line[512];
529 int include_block = 1;
530 unsigned int min_len;
531
532 if (!Mount(Display_Error))
533 return false;
534
Dees_Troy38bd7602012-09-14 13:33:53 -0400535 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400536 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400537 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400538 fp = fopen("/tmp/dfoutput.txt", "rt");
539 if (fp == NULL) {
540 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400541 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400542 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400543
544 while (fgets(line, sizeof(line), fp) != NULL)
545 {
546 unsigned long blocks, used, available;
547 char device[64];
548 char tmpString[64];
549
550 if (strncmp(line, "Filesystem", 10) == 0)
551 continue;
552 if (strlen(line) < min_len) {
553 include_block = 0;
554 continue;
555 }
556 if (include_block) {
557 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
558 } else {
559 // The device block string is so long that the df information is on the next line
560 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400561 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400562 while (tmpString[space_count] == 32)
563 space_count++;
564 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
565 }
566
567 // Adjust block size to byte size
568 Size = blocks * 1024ULL;
569 Used = used * 1024ULL;
570 Free = available * 1024ULL;
571 Backup_Size = Used;
572 }
573 fclose(fp);
574 return true;
575}
576
Dees_Troy5bf43922012-09-07 16:07:55 -0400577bool TWPartition::Find_Partition_Size(void) {
578 FILE* fp;
579 char line[512];
580 string tmpdevice;
581
582 // In this case, we'll first get the partitions we care about (with labels)
583 fp = fopen("/proc/partitions", "rt");
584 if (fp == NULL)
585 return false;
586
587 while (fgets(line, sizeof(line), fp) != NULL)
588 {
589 unsigned long major, minor, blocks;
590 char device[512];
591 char tmpString[64];
592
Dees_Troy63c8df72012-09-10 14:02:05 -0400593 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400594 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
595
596 tmpdevice = "/dev/block/";
597 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400598 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400599 // Adjust block size to byte size
600 Size = blocks * 1024ULL;
601 fclose(fp);
602 return true;
603 }
604 }
605 fclose(fp);
606 return false;
607}
608
Dees_Troy5bf43922012-09-07 16:07:55 -0400609bool TWPartition::Is_Mounted(void) {
610 if (!Can_Be_Mounted)
611 return false;
612
613 struct stat st1, st2;
614 string test_path;
615
616 // Check to see if the mount point directory exists
617 test_path = Mount_Point + "/.";
618 if (stat(test_path.c_str(), &st1) != 0) return false;
619
620 // Check to see if the directory above the mount point exists
621 test_path = Mount_Point + "/../.";
622 if (stat(test_path.c_str(), &st2) != 0) return false;
623
624 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
625 int ret = (st1.st_dev != st2.st_dev) ? true : false;
626
627 return ret;
628}
629
630bool TWPartition::Mount(bool Display_Error) {
631 if (Is_Mounted()) {
632 return true;
633 } else if (!Can_Be_Mounted) {
634 return false;
635 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400636
637 Find_Actual_Block_Device();
638
639 // Check the current file system before mounting
640 Check_FS_Type();
641
642 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
643 if (Display_Error)
644 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
645 else
646 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy9350b8d2012-09-27 12:38:38 -0400647 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 -0400648 return false;
649 } else {
650 if (Removable)
651 Update_Size(Display_Error);
652
653 if (!Symlink_Mount_Point.empty()) {
654 string Command;
655
656 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400657 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400658 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400659 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400660 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400661 return true;
662}
663
664bool TWPartition::UnMount(bool Display_Error) {
665 if (Is_Mounted()) {
666 int never_unmount_system;
667
668 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
669 if (never_unmount_system == 1 && Mount_Point == "/system")
670 return true; // Never unmount system if you're not supposed to unmount it
671
Dees_Troy38bd7602012-09-14 13:33:53 -0400672 if (!Symlink_Mount_Point.empty())
673 umount(Symlink_Mount_Point.c_str());
674
Dees_Troy5bf43922012-09-07 16:07:55 -0400675 if (umount(Mount_Point.c_str()) != 0) {
676 if (Display_Error)
677 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
678 else
679 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
680 return false;
681 } else
682 return true;
683 } else {
684 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400685 }
686}
687
688bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400689 if (!Can_Be_Wiped) {
690 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
691 return false;
692 }
693
Dees_Troyc51f1f92012-09-20 15:32:13 -0400694 if (Mount_Point == "/cache")
695 tmplog_offset = 0;
696
Dees_Troy38bd7602012-09-14 13:33:53 -0400697 if (Has_Data_Media)
698 return Wipe_Data_Without_Wiping_Media();
699
700 int check;
701 DataManager::GetValue(TW_RM_RF_VAR, check);
702 if (check)
703 return Wipe_RMRF();
704
705 if (Current_File_System == "ext4")
706 return Wipe_EXT4();
707
708 if (Current_File_System == "ext2" || Current_File_System == "ext3")
709 return Wipe_EXT23();
710
711 if (Current_File_System == "vfat")
712 return Wipe_FAT();
713
714 if (Current_File_System == "yaffs2")
715 return Wipe_MTD();
716
717 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
718 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400719}
720
Dees_Troye58d5262012-09-21 12:27:57 -0400721bool TWPartition::Wipe_AndSec(void) {
722 if (!Has_Android_Secure)
723 return false;
724
725 char cmd[512];
726
727 if (!Mount(true))
728 return false;
729
730 ui_print("Using rm -rf on .android_secure\n");
731 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
732
733 LOGI("rm -rf command is: '%s'\n", cmd);
734 system(cmd);
735 return true;
736}
737
Dees_Troy51a0e822012-09-05 15:24:24 -0400738bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400739 if (Backup_Method == FILES)
740 return Backup_Tar(backup_folder);
741 else if (Backup_Method == DD)
742 return Backup_DD(backup_folder);
743 else if (Backup_Method == FLASH_UTILS)
744 return Backup_Dump_Image(backup_folder);
745 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
746 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400747}
748
Dees_Troy43d8b002012-09-17 16:00:01 -0400749bool TWPartition::Check_MD5(string restore_folder) {
750 string Full_Filename;
751 char split_filename[512];
752 int index = 0;
753
754 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400755 if (!TWFunc::Path_Exists(Full_Filename)) {
756 // This is a split archive, we presume
757 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
758 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
759 if (TWFunc::Check_MD5(split_filename) == 0) {
760 LOGE("MD5 failed to match on '%s'.\n", split_filename);
761 return false;
762 }
763 index++;
764 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400765 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400766 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400767 } else {
768 // Single file archive
769 if (TWFunc::Check_MD5(Full_Filename) == 0) {
770 LOGE("MD5 failed to match on '%s'.\n", split_filename);
771 return false;
772 } else
773 return true;
774 }
775 return false;
776}
777
Dees_Troy51a0e822012-09-05 15:24:24 -0400778bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400779 if (Backup_Method == FILES)
780 return Restore_Tar(restore_folder);
781 else if (Backup_Method == DD)
782 return Restore_DD(restore_folder);
783 else if (Backup_Method == FLASH_UTILS)
784 return Restore_Flash_Image(restore_folder);
785 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
786 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400787}
788
789string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400790 if (Backup_Method == NONE)
791 return "none";
792 else if (Backup_Method == FILES)
793 return "files";
794 else if (Backup_Method == DD)
795 return "dd";
796 else if (Backup_Method == FLASH_UTILS)
797 return "flash_utils";
798 else
799 return "undefined";
800 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400801}
802
803bool TWPartition::Decrypt(string Password) {
804 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400805 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400806 return 1;
807}
808
809bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400810 bool Save_Data_Media = Has_Data_Media;
811
812 if (!UnMount(true))
813 return false;
814
815 Current_File_System = Fstab_File_System;
816 Is_Encrypted = false;
817 Is_Decrypted = false;
818 Decrypted_Block_Device = "";
819 Has_Data_Media = false;
820 if (Wipe()) {
821 Has_Data_Media = Save_Data_Media;
822 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
823 Recreate_Media_Folder();
824 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400825 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400826 return true;
827 } else {
828 Has_Data_Media = Save_Data_Media;
829 LOGE("Unable to format to remove encryption.\n");
830 return false;
831 }
832 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400833}
834
835void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400836 FILE *fp;
837 string blkCommand;
838 char blkOutput[255];
839 char* blk;
840 char* arg;
841 char* ptr;
842
843 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
844 return; // Running blkid on some mtd devices causes a massive crash
845
Dees_Troy38bd7602012-09-14 13:33:53 -0400846 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400847 if (!Is_Present)
848 return;
Dees_Troy51127312012-09-08 13:08:49 -0400849
Dees_Troy8170a922012-09-18 15:40:25 -0400850 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
851 system("rm /tmp/blkidoutput.txt");
852
853 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400854 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400855 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400856 if (fp == NULL)
857 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400858 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
859 {
860 blk = blkOutput;
861 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400862 while (*ptr > 32 && *ptr != ':') ptr++;
863 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400864 *ptr = 0;
865
866 // Increment by two, but verify that we don't hit a NULL
867 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400868 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400869
870 // Now, find the TYPE field
871 while (1)
872 {
873 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400874 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400875 if (*ptr != 0)
876 {
877 *ptr = 0;
878 ptr++;
879 }
880
881 if (strlen(arg) > 6)
882 {
883 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
884 }
885
886 if (*ptr == 0)
887 {
888 arg = NULL;
889 break;
890 }
891 }
892
893 if (arg && strlen(arg) > 7)
894 {
895 arg += 6; // Skip the TYPE=" portion
896 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
897 }
898 else
899 continue;
900
Dees_Troy63c8df72012-09-10 14:02:05 -0400901 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400902 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
903 Current_File_System = arg;
904 }
905 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400906 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400907 return;
908}
909
910bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400911 if (!UnMount(true))
912 return false;
913
Dees_Troy43d8b002012-09-17 16:00:01 -0400914 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400915 char command[512];
916
917 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
918 Find_Actual_Block_Device();
919 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
920 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400921 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400922 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400923 ui_print("Done.\n");
924 return true;
925 } else {
926 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
927 return false;
928 }
929 } else
930 return Wipe_RMRF();
931
932 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400933}
934
935bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400936 if (!UnMount(true))
937 return false;
938
Dees_Troy43d8b002012-09-17 16:00:01 -0400939 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400940 string Command;
941
942 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
943 Find_Actual_Block_Device();
944 Command = "make_ext4fs";
945 if (!Is_Decrypted && Length != 0) {
946 // Only use length if we're not decrypted
947 char len[32];
948 sprintf(len, "%i", Length);
949 Command += " -l ";
950 Command += len;
951 }
952 Command += " " + Actual_Block_Device;
953 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400954 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400955 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400956 ui_print("Done.\n");
957 return true;
958 } else {
959 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
960 return false;
961 }
962 } else
963 return Wipe_EXT23();
964
965 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400966}
967
968bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400969 char command[512];
970
Dees_Troy43d8b002012-09-17 16:00:01 -0400971 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400972 if (!UnMount(true))
973 return false;
974
975 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
976 Find_Actual_Block_Device();
977 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400978 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400979 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400980 ui_print("Done.\n");
981 return true;
982 } else {
983 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
984 return false;
985 }
986 return true;
987 }
988 else
989 return Wipe_RMRF();
990
991 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400992}
993
Dees_Troy38bd7602012-09-14 13:33:53 -0400994bool TWPartition::Wipe_MTD() {
995 if (!UnMount(true))
996 return false;
997
998 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
999
1000 mtd_scan_partitions();
1001 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1002 if (mtd == NULL) {
1003 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1004 return false;
1005 }
1006
1007 MtdWriteContext* ctx = mtd_write_partition(mtd);
1008 if (ctx == NULL) {
1009 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1010 return false;
1011 }
1012 if (mtd_erase_blocks(ctx, -1) == -1) {
1013 mtd_write_close(ctx);
1014 LOGE("Failed to format '%s'", MTD_Name.c_str());
1015 return false;
1016 }
1017 if (mtd_write_close(ctx) != 0) {
1018 LOGE("Failed to close '%s'", MTD_Name.c_str());
1019 return false;
1020 }
Dees_Troye58d5262012-09-21 12:27:57 -04001021 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001022 ui_print("Done.\n");
1023 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001024}
1025
1026bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001027 char cmd[512];
1028
1029 if (!Mount(true))
1030 return false;
1031
Dees_Troye58d5262012-09-21 12:27:57 -04001032 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1033 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001034
1035 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001036 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001037 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001038 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001039}
1040
1041bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001042 char cmd[256];
1043
1044 // This handles wiping data on devices with "sdcard" in /data/media
1045 if (!Mount(true))
1046 return false;
1047
1048 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001049 system("rm -f /data/*");
1050 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001051
1052 DIR* d;
1053 d = opendir("/data");
1054 if (d != NULL)
1055 {
1056 struct dirent* de;
1057 while ((de = readdir(d)) != NULL) {
1058 if (strcmp(de->d_name, "media") == 0) continue;
1059
1060 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001061 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001062 }
1063 closedir(d);
1064 }
1065 ui_print("Done.\n");
1066 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001067}
1068
1069bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001070 char back_name[255], split_index[5];
1071 string Full_FileName, Split_FileName, Tar_Args, Command;
1072 int use_compression, index, backup_count;
1073 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001074 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001075
1076 if (!Mount(true))
1077 return false;
1078
Dees_Troy2c50e182012-09-26 20:05:28 -04001079 if (Backup_Path == "/and-sec") {
1080 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1081 ui_print("Backing up %s...\n", "Android Secure");
1082 } else {
1083 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1084 ui_print("Backing up %s...\n", Display_Name.c_str());
1085 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001086
1087 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1088 if (use_compression)
1089 Tar_Args = "-cz";
1090 else
1091 Tar_Args = "-c";
1092
1093 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1094 Backup_FileName = back_name;
1095
Dees_Troy43d8b002012-09-17 16:00:01 -04001096 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1097 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001098 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001099 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001100 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001101 if (backup_count < 1) {
1102 LOGE("Error generating file list!\n");
1103 return false;
1104 }
1105 for (index=0; index<backup_count; index++) {
1106 sprintf(split_index, "%03i", index);
1107 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1108 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1109 LOGI("Backup command: '%s'\n", Command.c_str());
1110 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1111 system(Command.c_str()); // sending backup command formed earlier above
1112
Dees_Troy7c2dec82012-09-26 09:49:14 -04001113 file_size = TWFunc::Get_File_Size(Full_FileName);
1114 if (file_size == 0) {
1115 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 -04001116 return false;
1117 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001118 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001119 }
1120 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1121 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001122 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001123 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001124 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001125 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001126 else
Dees_Troye58d5262012-09-21 12:27:57 -04001127 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001128 LOGI("Backup command: '%s'\n", Command.c_str());
1129 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001130 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1131 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1132 return false;
1133 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001134 }
1135 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001136}
1137
1138bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001139 char back_name[255];
1140 string Full_FileName, Command;
1141 int use_compression;
1142
Dees_Troyb46a6842012-09-25 11:06:46 -04001143 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001144 ui_print("Backing up %s...\n", Display_Name.c_str());
1145
1146 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1147 Backup_FileName = back_name;
1148
1149 Full_FileName = backup_folder + "/" + Backup_FileName;
1150
1151 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1152 LOGI("Backup command: '%s'\n", Command.c_str());
1153 system(Command.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001154 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1155 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001156 return false;
1157 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001158 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001159}
1160
1161bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001162 char back_name[255];
1163 string Full_FileName, Command;
1164 int use_compression;
1165
Dees_Troyb46a6842012-09-25 11:06:46 -04001166 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001167 ui_print("Backing up %s...\n", Display_Name.c_str());
1168
1169 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1170 Backup_FileName = back_name;
1171
1172 Full_FileName = backup_folder + "/" + Backup_FileName;
1173
1174 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1175 LOGI("Backup command: '%s'\n", Command.c_str());
1176 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001177 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1178 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1179 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1180 return false;
1181 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001182 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001183}
1184
1185bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001186 size_t first_period, second_period;
1187 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001188 int index = 0;
1189 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001190
Dees_Troyb46a6842012-09-25 11:06:46 -04001191 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001192 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1193
1194 // Parse backup filename to extract the file system before wiping
1195 first_period = Backup_FileName.find(".");
1196 if (first_period == string::npos) {
1197 LOGE("Unable to find file system (first period).\n");
1198 return false;
1199 }
1200 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1201 second_period = Restore_File_System.find(".");
1202 if (second_period == string::npos) {
1203 LOGE("Unable to find file system (second period).\n");
1204 return false;
1205 }
1206 Restore_File_System.resize(second_period);
1207 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1208 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001209 if (Has_Android_Secure) {
1210 ui_print("Wiping android secure...\n");
1211 if (!Wipe_AndSec())
1212 return false;
1213 } else if (!Wipe()) {
1214 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001215 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001216 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001217
1218 if (!Mount(true))
1219 return false;
1220
Dees_Troy43d8b002012-09-17 16:00:01 -04001221 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001222 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001223 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001224 // Backup is multiple archives
1225 LOGI("Backup is multiple archives.\n");
1226 sprintf(split_index, "%03i", index);
1227 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1228 while (TWFunc::Path_Exists(Full_FileName)) {
1229 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troy6da522d2012-10-12 15:19:33 -04001230 Command = "tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001231 LOGI("Restore command: '%s'\n", Command.c_str());
1232 system(Command.c_str());
1233 index++;
1234 sprintf(split_index, "%03i", index);
1235 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1236 }
1237 if (index == 0) {
1238 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1239 return false;
1240 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001241 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001242 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001243 LOGI("Restore command: '%s'\n", Command.c_str());
1244 system(Command.c_str());
1245 }
1246 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001247}
1248
1249bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001250 string Full_FileName, Command;
1251
Dees_Troyb46a6842012-09-25 11:06:46 -04001252 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001253 ui_print("Restoring %s...\n", Display_Name.c_str());
1254 Full_FileName = restore_folder + "/" + Backup_FileName;
1255 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1256 LOGI("Restore command: '%s'\n", Command.c_str());
1257 system(Command.c_str());
1258 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001259}
1260
1261bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001262 string Full_FileName, Command;
1263
Dees_Troyb46a6842012-09-25 11:06:46 -04001264 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001265 ui_print("Restoring %s...\n", Display_Name.c_str());
1266 Full_FileName = restore_folder + "/" + Backup_FileName;
1267 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1268 Command = "erase_image " + MTD_Name;
1269 LOGI("Erase command: '%s'\n", Command.c_str());
1270 system(Command.c_str());
1271 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1272 LOGI("Restore command: '%s'\n", Command.c_str());
1273 system(Command.c_str());
1274 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001275}
Dees_Troy5bf43922012-09-07 16:07:55 -04001276
1277bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001278 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001279
Dees_Troyab10ee22012-09-21 14:27:30 -04001280 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001281 return false;
1282
Dees_Troy0550cfb2012-10-13 11:56:13 -04001283 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001284 if (Removable || Is_Encrypted) {
1285 if (!Mount(false))
1286 return true;
1287 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001288 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001289
1290 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001291 if (!ret || Size == 0) {
1292 if (!Get_Size_Via_df(Display_Error)) {
1293 if (!Was_Already_Mounted)
1294 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001295 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001296 }
1297 }
Dees_Troy51127312012-09-08 13:08:49 -04001298
Dees_Troy5bf43922012-09-07 16:07:55 -04001299 if (Has_Data_Media) {
1300 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001301 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001302 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1303 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001304 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001305 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001306 int bak = (int)(Backup_Size / 1048576LLU);
1307 int total = (int)(Size / 1048576LLU);
1308 int us = (int)(Used / 1048576LLU);
1309 int fre = (int)(Free / 1048576LLU);
1310 int datmed = (int)(data_media_used / 1048576LLU);
1311 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 -04001312 } else {
1313 if (!Was_Already_Mounted)
1314 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001315 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001316 }
Dees_Troye58d5262012-09-21 12:27:57 -04001317 } else if (Has_Android_Secure) {
1318 if (Mount(Display_Error))
1319 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001320 else {
1321 if (!Was_Already_Mounted)
1322 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001323 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001324 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001325 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001326 if (!Was_Already_Mounted)
1327 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001328 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001329}
Dees_Troy38bd7602012-09-14 13:33:53 -04001330
1331void TWPartition::Find_Actual_Block_Device(void) {
1332 if (Is_Decrypted) {
1333 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001334 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001335 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001336 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001337 Is_Present = true;
1338 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001339 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001340 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001341 Is_Present = true;
1342 } else
1343 Is_Present = false;
1344}
1345
1346void TWPartition::Recreate_Media_Folder(void) {
1347 string Command;
1348
1349 if (!Mount(true)) {
1350 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001351 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001352 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001353 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001354 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001355 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001356 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001357 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001358 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001359}
Dees_Troye58d5262012-09-21 12:27:57 -04001360
1361void TWPartition::Recreate_AndSec_Folder(void) {
1362 string Command;
1363
1364 if (!Has_Android_Secure)
1365 return;
1366
1367 if (!Mount(true)) {
1368 LOGE("Unable to recreate android secure folder.\n");
1369 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1370 LOGI("Recreating android secure folder.\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001371 Command = "umount " + Symlink_Mount_Point;
1372 system(Command.c_str());
Dees_Troyce2fe772012-09-28 12:34:33 -04001373 Command = "cd " + Mount_Point + " && mkdir .android_secure";
1374 system(Command.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001375 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1376 system(Command.c_str());
1377 }
1378}