blob: 0644fbae1c9799548c3c2f9b16c9f694a2868c70 [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();
252#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400253 }
254#endif
255#ifdef TW_INTERNAL_STORAGE_PATH
256 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
257 Is_Storage = true;
258 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400259#ifndef RECOVERY_SDCARD_ON_DATA
260 Setup_AndSec();
261#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400262 }
263#else
264 if (Mount_Point == "/emmc") {
265 Is_Storage = true;
266 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400267#ifndef RECOVERY_SDCARD_ON_DATA
268 Setup_AndSec();
269#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400270 }
271#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400273 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400274 Setup_Image(Display_Error);
275 if (Mount_Point == "/boot") {
276 int backup_display_size = (int)(Backup_Size / 1048576LLU);
277 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
278 if (Backup_Size == 0) {
279 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
280 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
281 } else
282 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
283 } else if (Mount_Point == "/recovery") {
284 int backup_display_size = (int)(Backup_Size / 1048576LLU);
285 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
286 if (Backup_Size == 0) {
287 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
288 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
289 } else
290 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
291 }
Dees_Troyb1dab8d2012-09-26 12:16:41 -0400292#ifdef SP1_NAME
293 string SP1_Path = "/";
294 SP1_Path += EXPAND(SP1_NAME);
295 if (Mount_Point == SP1_Path) {
296 int backup_display_size = (int)(Backup_Size / 1048576LLU);
297 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
298 }
299#endif
300#ifdef SP2_NAME
301 string SP2_Path = "/";
302 SP2_Path += EXPAND(SP2_NAME);
303 if (Mount_Point == SP2_Path) {
304 int backup_display_size = (int)(Backup_Size / 1048576LLU);
305 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
306 }
307#endif
308#ifdef SP3_NAME
309 string SP3_Path = "/";
310 SP3_Path += EXPAND(SP3_NAME);
311 if (Mount_Point == SP3_Path) {
312 int backup_display_size = (int)(Backup_Size / 1048576LLU);
313 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
314 }
315#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400316 }
317
Dees_Troy51127312012-09-08 13:08:49 -0400318 // Process any custom flags
319 if (Flags.size() > 0)
320 Process_Flags(Flags, Display_Error);
321
322 return true;
323}
324
325bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
326 char flags[MAX_FSTAB_LINE_LENGTH];
327 int flags_len, index = 0;
328 char* ptr;
329
330 strcpy(flags, Flags.c_str());
331 flags_len = Flags.size();
332 for (index = 0; index < flags_len; index++) {
333 if (flags[index] == ';')
334 flags[index] = '\0';
335 }
336
337 index = 0;
338 while (index < flags_len) {
339 while (index < flags_len && flags[index] == '\0')
340 index++;
341 if (index >= flags_len)
342 continue;
343 ptr = flags + index;
344 if (strcmp(ptr, "removable") == 0) {
345 Removable = true;
346 } else if (strcmp(ptr, "storage") == 0) {
347 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400348 } else if (strcmp(ptr, "canbewiped") == 0) {
349 Can_Be_Wiped = true;
350 } else if (strcmp(ptr, "wipeingui") == 0) {
351 Can_Be_Wiped = true;
352 Wipe_Available_in_GUI = true;
353 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
354 Can_Be_Wiped = true;
355 Wipe_Available_in_GUI = true;
356 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400357 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
358 ptr += 13;
359 Is_SubPartition = true;
360 SubPartition_Of = ptr;
361 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
362 ptr += 8;
363 Symlink_Path = ptr;
364 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
365 ptr += 8;
366 Display_Name = ptr;
367 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
368 ptr += 10;
369 Format_Block_Size = atoi(ptr);
370 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
371 ptr += 7;
372 Length = atoi(ptr);
373 } else {
374 if (Display_Error)
375 LOGE("Unhandled flag: '%s'\n", ptr);
376 else
377 LOGI("Unhandled flag: '%s'\n", ptr);
378 }
379 while (index < flags_len && flags[index] != '\0')
380 index++;
381 }
382 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400383}
384
Dees_Troy5bf43922012-09-07 16:07:55 -0400385bool TWPartition::Is_File_System(string File_System) {
386 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400387 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400388 File_System == "ext4" ||
389 File_System == "vfat" ||
390 File_System == "ntfs" ||
391 File_System == "yaffs2" ||
392 File_System == "auto")
393 return true;
394 else
395 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400396}
397
Dees_Troy5bf43922012-09-07 16:07:55 -0400398bool TWPartition::Is_Image(string File_System) {
399 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400400 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 return true;
402 else
403 return false;
404}
405
Dees_Troy51127312012-09-08 13:08:49 -0400406bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400407 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400408 if (mkdir(Path.c_str(), 0777) == -1) {
409 if (Display_Error)
410 LOGE("Can not create '%s' folder.\n", Path.c_str());
411 else
412 LOGI("Can not create '%s' folder.\n", Path.c_str());
413 return false;
414 } else {
415 LOGI("Created '%s' folder.\n", Path.c_str());
416 return true;
417 }
418 }
419 return true;
420}
421
Dees_Troy5bf43922012-09-07 16:07:55 -0400422void TWPartition::Setup_File_System(bool Display_Error) {
423 struct statfs st;
424
425 Can_Be_Mounted = true;
426 Can_Be_Wiped = true;
427
Dees_Troy5bf43922012-09-07 16:07:55 -0400428 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400429 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400430 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
431 Backup_Name = Display_Name;
432 Backup_Method = FILES;
433}
434
435void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400436 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
437 Backup_Name = Display_Name;
438 if (Fstab_File_System == "emmc")
439 Backup_Method = DD;
440 else if (Fstab_File_System == "mtd")
441 Backup_Method = FLASH_UTILS;
442 else
443 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
444 if (Find_Partition_Size()) {
445 Used = Size;
446 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400448 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400449 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400450 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400451 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400452 }
453}
454
Dees_Troye58d5262012-09-21 12:27:57 -0400455void TWPartition::Setup_AndSec(void) {
456 Backup_Name = "and-sec";
457 Has_Android_Secure = true;
458 Symlink_Path = Mount_Point + "/.android_secure";
459 Symlink_Mount_Point = "/and-sec";
460 Backup_Path = Symlink_Mount_Point;
461 Make_Dir("/and-sec", true);
462 Recreate_AndSec_Folder();
463}
464
Dees_Troy5bf43922012-09-07 16:07:55 -0400465void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
466 char device[512], realDevice[512];
467
468 strcpy(device, Block.c_str());
469 memset(realDevice, 0, sizeof(realDevice));
470 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
471 {
472 strcpy(device, realDevice);
473 memset(realDevice, 0, sizeof(realDevice));
474 }
475
476 if (device[0] != '/') {
477 if (Display_Error)
478 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
479 else
480 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
481 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400482 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400483 Block = device;
484 return;
485 }
486}
487
Dees_Troy38bd7602012-09-14 13:33:53 -0400488bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
489 FILE *fp = NULL;
490 char line[255];
491
492 fp = fopen("/proc/mtd", "rt");
493 if (fp == NULL) {
494 LOGE("Device does not support /proc/mtd\n");
495 return false;
496 }
497
498 while (fgets(line, sizeof(line), fp) != NULL)
499 {
500 char device[32], label[32];
501 unsigned long size = 0;
502 char* fstype = NULL;
503 int deviceId;
504
505 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
506
507 // Skip header and blank lines
508 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
509 continue;
510
511 // Strip off the trailing " from the label
512 label[strlen(label)-1] = '\0';
513
514 if (strcmp(label, MTD_Name.c_str()) == 0) {
515 // We found our device
516 // Strip off the trailing : from the device
517 device[strlen(device)-1] = '\0';
518 if (sscanf(device,"mtd%d", &deviceId) == 1) {
519 sprintf(device, "/dev/block/mtdblock%d", deviceId);
520 Primary_Block_Device = device;
521 }
522 }
523 }
524 fclose(fp);
525
526 return false;
527}
528
Dees_Troy51127312012-09-08 13:08:49 -0400529bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
530 struct statfs st;
531 string Local_Path = Mount_Point + "/.";
532
533 if (!Mount(Display_Error))
534 return false;
535
536 if (statfs(Local_Path.c_str(), &st) != 0) {
537 if (!Removable) {
538 if (Display_Error)
539 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
540 else
541 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
542 }
543 return false;
544 }
545 Size = (st.f_blocks * st.f_bsize);
546 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
547 Free = (st.f_bfree * st.f_bsize);
548 Backup_Size = Used;
549 return true;
550}
551
552bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400553 FILE* fp;
554 char command[255], line[512];
555 int include_block = 1;
556 unsigned int min_len;
557
558 if (!Mount(Display_Error))
559 return false;
560
Dees_Troy38bd7602012-09-14 13:33:53 -0400561 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400562 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400563 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400564 fp = fopen("/tmp/dfoutput.txt", "rt");
565 if (fp == NULL) {
566 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400567 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400568 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400569
570 while (fgets(line, sizeof(line), fp) != NULL)
571 {
572 unsigned long blocks, used, available;
573 char device[64];
574 char tmpString[64];
575
576 if (strncmp(line, "Filesystem", 10) == 0)
577 continue;
578 if (strlen(line) < min_len) {
579 include_block = 0;
580 continue;
581 }
582 if (include_block) {
583 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
584 } else {
585 // The device block string is so long that the df information is on the next line
586 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400587 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400588 while (tmpString[space_count] == 32)
589 space_count++;
590 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
591 }
592
593 // Adjust block size to byte size
594 Size = blocks * 1024ULL;
595 Used = used * 1024ULL;
596 Free = available * 1024ULL;
597 Backup_Size = Used;
598 }
599 fclose(fp);
600 return true;
601}
602
Dees_Troy5bf43922012-09-07 16:07:55 -0400603bool TWPartition::Find_Partition_Size(void) {
604 FILE* fp;
605 char line[512];
606 string tmpdevice;
607
608 // In this case, we'll first get the partitions we care about (with labels)
609 fp = fopen("/proc/partitions", "rt");
610 if (fp == NULL)
611 return false;
612
613 while (fgets(line, sizeof(line), fp) != NULL)
614 {
615 unsigned long major, minor, blocks;
616 char device[512];
617 char tmpString[64];
618
Dees_Troy63c8df72012-09-10 14:02:05 -0400619 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400620 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
621
622 tmpdevice = "/dev/block/";
623 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400624 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400625 // Adjust block size to byte size
626 Size = blocks * 1024ULL;
627 fclose(fp);
628 return true;
629 }
630 }
631 fclose(fp);
632 return false;
633}
634
Dees_Troy5bf43922012-09-07 16:07:55 -0400635bool TWPartition::Is_Mounted(void) {
636 if (!Can_Be_Mounted)
637 return false;
638
639 struct stat st1, st2;
640 string test_path;
641
642 // Check to see if the mount point directory exists
643 test_path = Mount_Point + "/.";
644 if (stat(test_path.c_str(), &st1) != 0) return false;
645
646 // Check to see if the directory above the mount point exists
647 test_path = Mount_Point + "/../.";
648 if (stat(test_path.c_str(), &st2) != 0) return false;
649
650 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
651 int ret = (st1.st_dev != st2.st_dev) ? true : false;
652
653 return ret;
654}
655
656bool TWPartition::Mount(bool Display_Error) {
657 if (Is_Mounted()) {
658 return true;
659 } else if (!Can_Be_Mounted) {
660 return false;
661 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400662
663 Find_Actual_Block_Device();
664
665 // Check the current file system before mounting
666 Check_FS_Type();
667
668 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
669 if (Display_Error)
670 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
671 else
672 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy9350b8d2012-09-27 12:38:38 -0400673 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 -0400674 return false;
675 } else {
676 if (Removable)
677 Update_Size(Display_Error);
678
679 if (!Symlink_Mount_Point.empty()) {
680 string Command;
681
682 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400683 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400684 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400685 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400686 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400687 return true;
688}
689
690bool TWPartition::UnMount(bool Display_Error) {
691 if (Is_Mounted()) {
692 int never_unmount_system;
693
694 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
695 if (never_unmount_system == 1 && Mount_Point == "/system")
696 return true; // Never unmount system if you're not supposed to unmount it
697
Dees_Troy38bd7602012-09-14 13:33:53 -0400698 if (!Symlink_Mount_Point.empty())
699 umount(Symlink_Mount_Point.c_str());
700
Dees_Troy5bf43922012-09-07 16:07:55 -0400701 if (umount(Mount_Point.c_str()) != 0) {
702 if (Display_Error)
703 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
704 else
705 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
706 return false;
707 } else
708 return true;
709 } else {
710 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400711 }
712}
713
714bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400715 if (!Can_Be_Wiped) {
716 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
717 return false;
718 }
719
Dees_Troyc51f1f92012-09-20 15:32:13 -0400720 if (Mount_Point == "/cache")
721 tmplog_offset = 0;
722
Dees_Troy38bd7602012-09-14 13:33:53 -0400723 if (Has_Data_Media)
724 return Wipe_Data_Without_Wiping_Media();
725
726 int check;
727 DataManager::GetValue(TW_RM_RF_VAR, check);
728 if (check)
729 return Wipe_RMRF();
730
731 if (Current_File_System == "ext4")
732 return Wipe_EXT4();
733
734 if (Current_File_System == "ext2" || Current_File_System == "ext3")
735 return Wipe_EXT23();
736
737 if (Current_File_System == "vfat")
738 return Wipe_FAT();
739
740 if (Current_File_System == "yaffs2")
741 return Wipe_MTD();
742
743 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
744 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400745}
746
Dees_Troye58d5262012-09-21 12:27:57 -0400747bool TWPartition::Wipe_AndSec(void) {
748 if (!Has_Android_Secure)
749 return false;
750
751 char cmd[512];
752
753 if (!Mount(true))
754 return false;
755
756 ui_print("Using rm -rf on .android_secure\n");
757 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
758
759 LOGI("rm -rf command is: '%s'\n", cmd);
760 system(cmd);
761 return true;
762}
763
Dees_Troy51a0e822012-09-05 15:24:24 -0400764bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400765 if (Backup_Method == FILES)
766 return Backup_Tar(backup_folder);
767 else if (Backup_Method == DD)
768 return Backup_DD(backup_folder);
769 else if (Backup_Method == FLASH_UTILS)
770 return Backup_Dump_Image(backup_folder);
771 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
772 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400773}
774
Dees_Troy43d8b002012-09-17 16:00:01 -0400775bool TWPartition::Check_MD5(string restore_folder) {
776 string Full_Filename;
777 char split_filename[512];
778 int index = 0;
779
780 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400781 if (!TWFunc::Path_Exists(Full_Filename)) {
782 // This is a split archive, we presume
783 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
784 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
785 if (TWFunc::Check_MD5(split_filename) == 0) {
786 LOGE("MD5 failed to match on '%s'.\n", split_filename);
787 return false;
788 }
789 index++;
790 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400791 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400792 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400793 } else {
794 // Single file archive
795 if (TWFunc::Check_MD5(Full_Filename) == 0) {
796 LOGE("MD5 failed to match on '%s'.\n", split_filename);
797 return false;
798 } else
799 return true;
800 }
801 return false;
802}
803
Dees_Troy51a0e822012-09-05 15:24:24 -0400804bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400805 if (Backup_Method == FILES)
806 return Restore_Tar(restore_folder);
807 else if (Backup_Method == DD)
808 return Restore_DD(restore_folder);
809 else if (Backup_Method == FLASH_UTILS)
810 return Restore_Flash_Image(restore_folder);
811 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
812 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400813}
814
815string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400816 if (Backup_Method == NONE)
817 return "none";
818 else if (Backup_Method == FILES)
819 return "files";
820 else if (Backup_Method == DD)
821 return "dd";
822 else if (Backup_Method == FLASH_UTILS)
823 return "flash_utils";
824 else
825 return "undefined";
826 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400827}
828
829bool TWPartition::Decrypt(string Password) {
830 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400831 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400832 return 1;
833}
834
835bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400836 bool Save_Data_Media = Has_Data_Media;
837
838 if (!UnMount(true))
839 return false;
840
841 Current_File_System = Fstab_File_System;
842 Is_Encrypted = false;
843 Is_Decrypted = false;
844 Decrypted_Block_Device = "";
845 Has_Data_Media = false;
846 if (Wipe()) {
847 Has_Data_Media = Save_Data_Media;
848 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
849 Recreate_Media_Folder();
850 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400851 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400852 return true;
853 } else {
854 Has_Data_Media = Save_Data_Media;
855 LOGE("Unable to format to remove encryption.\n");
856 return false;
857 }
858 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400859}
860
861void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400862 FILE *fp;
863 string blkCommand;
864 char blkOutput[255];
865 char* blk;
866 char* arg;
867 char* ptr;
868
869 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
870 return; // Running blkid on some mtd devices causes a massive crash
871
Dees_Troy38bd7602012-09-14 13:33:53 -0400872 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400873 if (!Is_Present)
874 return;
Dees_Troy51127312012-09-08 13:08:49 -0400875
Dees_Troy8170a922012-09-18 15:40:25 -0400876 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
877 system("rm /tmp/blkidoutput.txt");
878
879 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400880 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400881 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400882 if (fp == NULL)
883 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400884 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
885 {
886 blk = blkOutput;
887 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400888 while (*ptr > 32 && *ptr != ':') ptr++;
889 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400890 *ptr = 0;
891
892 // Increment by two, but verify that we don't hit a NULL
893 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400894 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400895
896 // Now, find the TYPE field
897 while (1)
898 {
899 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400900 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400901 if (*ptr != 0)
902 {
903 *ptr = 0;
904 ptr++;
905 }
906
907 if (strlen(arg) > 6)
908 {
909 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
910 }
911
912 if (*ptr == 0)
913 {
914 arg = NULL;
915 break;
916 }
917 }
918
919 if (arg && strlen(arg) > 7)
920 {
921 arg += 6; // Skip the TYPE=" portion
922 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
923 }
924 else
925 continue;
926
Dees_Troy63c8df72012-09-10 14:02:05 -0400927 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400928 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
929 Current_File_System = arg;
930 }
931 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400932 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400933 return;
934}
935
936bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400937 if (!UnMount(true))
938 return false;
939
Dees_Troy43d8b002012-09-17 16:00:01 -0400940 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400941 char command[512];
942
943 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
944 Find_Actual_Block_Device();
945 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
946 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400947 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400948 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400949 ui_print("Done.\n");
950 return true;
951 } else {
952 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
953 return false;
954 }
955 } else
956 return Wipe_RMRF();
957
958 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400959}
960
961bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400962 if (!UnMount(true))
963 return false;
964
Dees_Troy43d8b002012-09-17 16:00:01 -0400965 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400966 string Command;
967
968 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
969 Find_Actual_Block_Device();
970 Command = "make_ext4fs";
971 if (!Is_Decrypted && Length != 0) {
972 // Only use length if we're not decrypted
973 char len[32];
974 sprintf(len, "%i", Length);
975 Command += " -l ";
976 Command += len;
977 }
978 Command += " " + Actual_Block_Device;
979 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400980 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400981 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400982 ui_print("Done.\n");
983 return true;
984 } else {
985 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
986 return false;
987 }
988 } else
989 return Wipe_EXT23();
990
991 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400992}
993
994bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400995 char command[512];
996
Dees_Troy43d8b002012-09-17 16:00:01 -0400997 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400998 if (!UnMount(true))
999 return false;
1000
1001 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1002 Find_Actual_Block_Device();
1003 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -04001004 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001005 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001006 ui_print("Done.\n");
1007 return true;
1008 } else {
1009 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1010 return false;
1011 }
1012 return true;
1013 }
1014 else
1015 return Wipe_RMRF();
1016
1017 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001018}
1019
Dees_Troy38bd7602012-09-14 13:33:53 -04001020bool TWPartition::Wipe_MTD() {
1021 if (!UnMount(true))
1022 return false;
1023
1024 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1025
1026 mtd_scan_partitions();
1027 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1028 if (mtd == NULL) {
1029 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1030 return false;
1031 }
1032
1033 MtdWriteContext* ctx = mtd_write_partition(mtd);
1034 if (ctx == NULL) {
1035 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1036 return false;
1037 }
1038 if (mtd_erase_blocks(ctx, -1) == -1) {
1039 mtd_write_close(ctx);
1040 LOGE("Failed to format '%s'", MTD_Name.c_str());
1041 return false;
1042 }
1043 if (mtd_write_close(ctx) != 0) {
1044 LOGE("Failed to close '%s'", MTD_Name.c_str());
1045 return false;
1046 }
Dees_Troye58d5262012-09-21 12:27:57 -04001047 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001048 ui_print("Done.\n");
1049 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001050}
1051
1052bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001053 char cmd[512];
1054
1055 if (!Mount(true))
1056 return false;
1057
Dees_Troye58d5262012-09-21 12:27:57 -04001058 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1059 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001060
1061 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001062 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001063 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001064 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001065}
1066
1067bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001068 char cmd[256];
1069
1070 // This handles wiping data on devices with "sdcard" in /data/media
1071 if (!Mount(true))
1072 return false;
1073
1074 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001075 system("rm -f /data/*");
1076 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001077
1078 DIR* d;
1079 d = opendir("/data");
1080 if (d != NULL)
1081 {
1082 struct dirent* de;
1083 while ((de = readdir(d)) != NULL) {
1084 if (strcmp(de->d_name, "media") == 0) continue;
1085
1086 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001087 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001088 }
1089 closedir(d);
1090 }
1091 ui_print("Done.\n");
1092 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001093}
1094
1095bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001096 char back_name[255], split_index[5];
1097 string Full_FileName, Split_FileName, Tar_Args, Command;
1098 int use_compression, index, backup_count;
1099 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001100 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001101
1102 if (!Mount(true))
1103 return false;
1104
Dees_Troy2c50e182012-09-26 20:05:28 -04001105 if (Backup_Path == "/and-sec") {
1106 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1107 ui_print("Backing up %s...\n", "Android Secure");
1108 } else {
1109 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1110 ui_print("Backing up %s...\n", Display_Name.c_str());
1111 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001112
1113 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1114 if (use_compression)
1115 Tar_Args = "-cz";
1116 else
1117 Tar_Args = "-c";
1118
1119 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1120 Backup_FileName = back_name;
1121
Dees_Troy43d8b002012-09-17 16:00:01 -04001122 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1123 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001124 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001125 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001126 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001127 if (backup_count < 1) {
1128 LOGE("Error generating file list!\n");
1129 return false;
1130 }
1131 for (index=0; index<backup_count; index++) {
1132 sprintf(split_index, "%03i", index);
1133 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1134 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1135 LOGI("Backup command: '%s'\n", Command.c_str());
1136 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1137 system(Command.c_str()); // sending backup command formed earlier above
1138
Dees_Troy7c2dec82012-09-26 09:49:14 -04001139 file_size = TWFunc::Get_File_Size(Full_FileName);
1140 if (file_size == 0) {
1141 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 -04001142 return false;
1143 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001144 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001145 }
1146 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1147 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001148 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001149 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001150 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001151 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001152 else
Dees_Troye58d5262012-09-21 12:27:57 -04001153 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001154 LOGI("Backup command: '%s'\n", Command.c_str());
1155 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001156 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1157 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1158 return false;
1159 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001160 }
1161 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001162}
1163
1164bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001165 char back_name[255];
1166 string Full_FileName, Command;
1167 int use_compression;
1168
Dees_Troyb46a6842012-09-25 11:06:46 -04001169 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001170 ui_print("Backing up %s...\n", Display_Name.c_str());
1171
1172 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1173 Backup_FileName = back_name;
1174
1175 Full_FileName = backup_folder + "/" + Backup_FileName;
1176
1177 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1178 LOGI("Backup command: '%s'\n", Command.c_str());
1179 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001180 if (TWFunc::Get_File_Size(Full_FileName) != Backup_Size) {
1181 LOGE("Backup file size %lu for '%s' is does not match backup size %llu.\n", TWFunc::Get_File_Size(Full_FileName), Full_FileName.c_str(), Backup_Size);
1182 return false;
1183 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001184 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001185}
1186
1187bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001188 char back_name[255];
1189 string Full_FileName, Command;
1190 int use_compression;
1191
Dees_Troyb46a6842012-09-25 11:06:46 -04001192 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001193 ui_print("Backing up %s...\n", Display_Name.c_str());
1194
1195 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1196 Backup_FileName = back_name;
1197
1198 Full_FileName = backup_folder + "/" + Backup_FileName;
1199
1200 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1201 LOGI("Backup command: '%s'\n", Command.c_str());
1202 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001203 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1204 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1205 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1206 return false;
1207 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001208 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001209}
1210
1211bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001212 size_t first_period, second_period;
1213 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001214 int index = 0;
1215 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001216
Dees_Troyb46a6842012-09-25 11:06:46 -04001217 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001218 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1219
1220 // Parse backup filename to extract the file system before wiping
1221 first_period = Backup_FileName.find(".");
1222 if (first_period == string::npos) {
1223 LOGE("Unable to find file system (first period).\n");
1224 return false;
1225 }
1226 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1227 second_period = Restore_File_System.find(".");
1228 if (second_period == string::npos) {
1229 LOGE("Unable to find file system (second period).\n");
1230 return false;
1231 }
1232 Restore_File_System.resize(second_period);
1233 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1234 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001235 if (Has_Android_Secure) {
1236 ui_print("Wiping android secure...\n");
1237 if (!Wipe_AndSec())
1238 return false;
1239 } else if (!Wipe()) {
1240 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001241 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001242 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001243
1244 if (!Mount(true))
1245 return false;
1246
Dees_Troy43d8b002012-09-17 16:00:01 -04001247 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001248 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001249 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001250 // Backup is multiple archives
1251 LOGI("Backup is multiple archives.\n");
1252 sprintf(split_index, "%03i", index);
1253 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1254 while (TWFunc::Path_Exists(Full_FileName)) {
1255 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001256 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001257 LOGI("Restore command: '%s'\n", Command.c_str());
1258 system(Command.c_str());
1259 index++;
1260 sprintf(split_index, "%03i", index);
1261 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1262 }
1263 if (index == 0) {
1264 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1265 return false;
1266 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001267 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001268 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001269 LOGI("Restore command: '%s'\n", Command.c_str());
1270 system(Command.c_str());
1271 }
1272 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001273}
1274
1275bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001276 string Full_FileName, Command;
1277
Dees_Troyb46a6842012-09-25 11:06:46 -04001278 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001279 ui_print("Restoring %s...\n", Display_Name.c_str());
1280 Full_FileName = restore_folder + "/" + Backup_FileName;
1281 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1282 LOGI("Restore command: '%s'\n", Command.c_str());
1283 system(Command.c_str());
1284 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001285}
1286
1287bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001288 string Full_FileName, Command;
1289
Dees_Troyb46a6842012-09-25 11:06:46 -04001290 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001291 ui_print("Restoring %s...\n", Display_Name.c_str());
1292 Full_FileName = restore_folder + "/" + Backup_FileName;
1293 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1294 Command = "erase_image " + MTD_Name;
1295 LOGI("Erase command: '%s'\n", Command.c_str());
1296 system(Command.c_str());
1297 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1298 LOGI("Restore command: '%s'\n", Command.c_str());
1299 system(Command.c_str());
1300 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001301}
Dees_Troy5bf43922012-09-07 16:07:55 -04001302
1303bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001304 bool ret = false;
1305
Dees_Troyab10ee22012-09-21 14:27:30 -04001306 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001307 return false;
1308
Dees_Troy38bd7602012-09-14 13:33:53 -04001309 if (Removable || Is_Encrypted) {
1310 if (!Mount(false))
1311 return true;
1312 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001313 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001314
1315 ret = Get_Size_Via_statfs(Display_Error);
1316 if (!ret || Size == 0)
1317 if (!Get_Size_Via_df(Display_Error))
1318 return false;
1319
Dees_Troy5bf43922012-09-07 16:07:55 -04001320 if (Has_Data_Media) {
1321 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001322 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001323 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1324 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001325 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001326 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001327 int bak = (int)(Backup_Size / 1048576LLU);
1328 int total = (int)(Size / 1048576LLU);
1329 int us = (int)(Used / 1048576LLU);
1330 int fre = (int)(Free / 1048576LLU);
1331 int datmed = (int)(data_media_used / 1048576LLU);
1332 LOGI("Data backup size is %iMB, size: %iMB, used: %iMB, free: %iMB, in data/media: %iMB.\n", bak, total, us, fre, datmed);
Dees_Troy5bf43922012-09-07 16:07:55 -04001333 } else
1334 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001335 } else if (Has_Android_Secure) {
1336 if (Mount(Display_Error))
1337 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
1338 else
1339 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001340 }
1341 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001342}
Dees_Troy38bd7602012-09-14 13:33:53 -04001343
1344void TWPartition::Find_Actual_Block_Device(void) {
1345 if (Is_Decrypted) {
1346 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001347 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001348 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001349 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001350 Is_Present = true;
1351 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001352 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001353 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001354 Is_Present = true;
1355 } else
1356 Is_Present = false;
1357}
1358
1359void TWPartition::Recreate_Media_Folder(void) {
1360 string Command;
1361
1362 if (!Mount(true)) {
1363 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001364 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001365 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001366 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001367 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001368 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001369 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001370 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001371 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001372}
Dees_Troye58d5262012-09-21 12:27:57 -04001373
1374void TWPartition::Recreate_AndSec_Folder(void) {
1375 string Command;
1376
1377 if (!Has_Android_Secure)
1378 return;
1379
1380 if (!Mount(true)) {
1381 LOGE("Unable to recreate android secure folder.\n");
1382 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1383 LOGI("Recreating android secure folder.\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001384 Command = "umount " + Symlink_Mount_Point;
1385 system(Command.c_str());
Dees_Troyce2fe772012-09-28 12:34:33 -04001386 Command = "cd " + Mount_Point + " && mkdir .android_secure";
1387 system(Command.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001388 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1389 system(Command.c_str());
1390 }
1391}