blob: 12e1f5339ca537478e9dd29ccb92172c37051e26 [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_Troyb46a6842012-09-25 11:06:46 -0400178 Recreate_Media_Folder();
Dees_Troy657c3092012-09-10 20:32:10 -0400179 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
180 Make_Dir("/emmc", Display_Error);
181 Symlink_Path = "/data/media";
182 Symlink_Mount_Point = "/emmc";
183 } else {
184 Make_Dir("/sdcard", Display_Error);
185 Symlink_Path = "/data/media";
186 Symlink_Mount_Point = "/sdcard";
187 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400188#endif
189#ifdef TW_INCLUDE_CRYPTO
190 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400191 char crypto_blkdev[255];
192 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
193 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400194 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400195 DataManager::SetValue(TW_IS_DECRYPTED, 1);
196 Is_Encrypted = true;
197 Is_Decrypted = true;
198 Decrypted_Block_Device = crypto_blkdev;
199 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
200 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400201 Is_Encrypted = true;
202 Is_Decrypted = false;
203 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
204 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
205 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400206 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400207#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400208 } else if (Mount_Point == "/cache") {
209 Display_Name = "Cache";
210 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400211 Wipe_During_Factory_Reset = true;
Dees_Troyb46a6842012-09-25 11:06:46 -0400212 if (!TWFunc::Path_Exists("/cache/recovery")) {
213 LOGI("Recreating /cache/recovery folder.\n");
214 TWFunc::Recursive_Mkdir("/cache/recovery");
215 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400216 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400217 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400218 Display_Name = "DataData";
219 Is_SubPartition = true;
220 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400221 DataManager::SetValue(TW_HAS_DATADATA, 1);
222 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400223 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 Display_Name = "SD-Ext";
225 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400226 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400227 } else if (Mount_Point == "/boot") {
228 Display_Name = "Boot";
229 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400230 }
231#ifdef TW_EXTERNAL_STORAGE_PATH
232 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
233 Is_Storage = true;
234 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400235 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400236 }
237#else
238 if (Mount_Point == "/sdcard") {
239 Is_Storage = true;
240 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400241 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400242#ifndef RECOVERY_SDCARD_ON_DATA
243 Setup_AndSec();
244#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400245 }
246#endif
247#ifdef TW_INTERNAL_STORAGE_PATH
248 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
249 Is_Storage = true;
250 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400251#ifndef RECOVERY_SDCARD_ON_DATA
252 Setup_AndSec();
253#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400254 }
255#else
256 if (Mount_Point == "/emmc") {
257 Is_Storage = true;
258 Storage_Path = "/emmc";
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#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400265 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400266 Setup_Image(Display_Error);
267 if (Mount_Point == "/boot") {
268 int backup_display_size = (int)(Backup_Size / 1048576LLU);
269 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
270 if (Backup_Size == 0) {
271 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
272 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
273 } else
274 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
275 } else if (Mount_Point == "/recovery") {
276 int backup_display_size = (int)(Backup_Size / 1048576LLU);
277 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
278 if (Backup_Size == 0) {
279 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
280 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
281 } else
282 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
283 }
Dees_Troyb1dab8d2012-09-26 12:16:41 -0400284#ifdef SP1_NAME
285 string SP1_Path = "/";
286 SP1_Path += EXPAND(SP1_NAME);
287 if (Mount_Point == SP1_Path) {
288 int backup_display_size = (int)(Backup_Size / 1048576LLU);
289 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
290 }
291#endif
292#ifdef SP2_NAME
293 string SP2_Path = "/";
294 SP2_Path += EXPAND(SP2_NAME);
295 if (Mount_Point == SP2_Path) {
296 int backup_display_size = (int)(Backup_Size / 1048576LLU);
297 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
298 }
299#endif
300#ifdef SP3_NAME
301 string SP3_Path = "/";
302 SP3_Path += EXPAND(SP3_NAME);
303 if (Mount_Point == SP3_Path) {
304 int backup_display_size = (int)(Backup_Size / 1048576LLU);
305 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
306 }
307#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400308 }
309
Dees_Troy51127312012-09-08 13:08:49 -0400310 // Process any custom flags
311 if (Flags.size() > 0)
312 Process_Flags(Flags, Display_Error);
313
314 return true;
315}
316
317bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
318 char flags[MAX_FSTAB_LINE_LENGTH];
319 int flags_len, index = 0;
320 char* ptr;
321
322 strcpy(flags, Flags.c_str());
323 flags_len = Flags.size();
324 for (index = 0; index < flags_len; index++) {
325 if (flags[index] == ';')
326 flags[index] = '\0';
327 }
328
329 index = 0;
330 while (index < flags_len) {
331 while (index < flags_len && flags[index] == '\0')
332 index++;
333 if (index >= flags_len)
334 continue;
335 ptr = flags + index;
336 if (strcmp(ptr, "removable") == 0) {
337 Removable = true;
338 } else if (strcmp(ptr, "storage") == 0) {
339 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400340 } else if (strcmp(ptr, "canbewiped") == 0) {
341 Can_Be_Wiped = true;
342 } else if (strcmp(ptr, "wipeingui") == 0) {
343 Can_Be_Wiped = true;
344 Wipe_Available_in_GUI = true;
345 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
346 Can_Be_Wiped = true;
347 Wipe_Available_in_GUI = true;
348 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400349 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
350 ptr += 13;
351 Is_SubPartition = true;
352 SubPartition_Of = ptr;
353 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
354 ptr += 8;
355 Symlink_Path = ptr;
356 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
357 ptr += 8;
358 Display_Name = ptr;
359 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
360 ptr += 10;
361 Format_Block_Size = atoi(ptr);
362 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
363 ptr += 7;
364 Length = atoi(ptr);
365 } else {
366 if (Display_Error)
367 LOGE("Unhandled flag: '%s'\n", ptr);
368 else
369 LOGI("Unhandled flag: '%s'\n", ptr);
370 }
371 while (index < flags_len && flags[index] != '\0')
372 index++;
373 }
374 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400375}
376
Dees_Troy5bf43922012-09-07 16:07:55 -0400377bool TWPartition::Is_File_System(string File_System) {
378 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400379 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400380 File_System == "ext4" ||
381 File_System == "vfat" ||
382 File_System == "ntfs" ||
383 File_System == "yaffs2" ||
384 File_System == "auto")
385 return true;
386 else
387 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400388}
389
Dees_Troy5bf43922012-09-07 16:07:55 -0400390bool TWPartition::Is_Image(string File_System) {
391 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400392 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400393 return true;
394 else
395 return false;
396}
397
Dees_Troy51127312012-09-08 13:08:49 -0400398bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400399 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400400 if (mkdir(Path.c_str(), 0777) == -1) {
401 if (Display_Error)
402 LOGE("Can not create '%s' folder.\n", Path.c_str());
403 else
404 LOGI("Can not create '%s' folder.\n", Path.c_str());
405 return false;
406 } else {
407 LOGI("Created '%s' folder.\n", Path.c_str());
408 return true;
409 }
410 }
411 return true;
412}
413
Dees_Troy5bf43922012-09-07 16:07:55 -0400414void TWPartition::Setup_File_System(bool Display_Error) {
415 struct statfs st;
416
417 Can_Be_Mounted = true;
418 Can_Be_Wiped = true;
419
Dees_Troy5bf43922012-09-07 16:07:55 -0400420 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400421 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400422 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
423 Backup_Name = Display_Name;
424 Backup_Method = FILES;
425}
426
427void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400428 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
429 Backup_Name = Display_Name;
430 if (Fstab_File_System == "emmc")
431 Backup_Method = DD;
432 else if (Fstab_File_System == "mtd")
433 Backup_Method = FLASH_UTILS;
434 else
435 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
436 if (Find_Partition_Size()) {
437 Used = Size;
438 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400439 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400441 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400443 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400444 }
445}
446
Dees_Troye58d5262012-09-21 12:27:57 -0400447void TWPartition::Setup_AndSec(void) {
448 Backup_Name = "and-sec";
449 Has_Android_Secure = true;
450 Symlink_Path = Mount_Point + "/.android_secure";
451 Symlink_Mount_Point = "/and-sec";
452 Backup_Path = Symlink_Mount_Point;
453 Make_Dir("/and-sec", true);
454 Recreate_AndSec_Folder();
455}
456
Dees_Troy5bf43922012-09-07 16:07:55 -0400457void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
458 char device[512], realDevice[512];
459
460 strcpy(device, Block.c_str());
461 memset(realDevice, 0, sizeof(realDevice));
462 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
463 {
464 strcpy(device, realDevice);
465 memset(realDevice, 0, sizeof(realDevice));
466 }
467
468 if (device[0] != '/') {
469 if (Display_Error)
470 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
471 else
472 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
473 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400474 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400475 Block = device;
476 return;
477 }
478}
479
Dees_Troy38bd7602012-09-14 13:33:53 -0400480bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
481 FILE *fp = NULL;
482 char line[255];
483
484 fp = fopen("/proc/mtd", "rt");
485 if (fp == NULL) {
486 LOGE("Device does not support /proc/mtd\n");
487 return false;
488 }
489
490 while (fgets(line, sizeof(line), fp) != NULL)
491 {
492 char device[32], label[32];
493 unsigned long size = 0;
494 char* fstype = NULL;
495 int deviceId;
496
497 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
498
499 // Skip header and blank lines
500 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
501 continue;
502
503 // Strip off the trailing " from the label
504 label[strlen(label)-1] = '\0';
505
506 if (strcmp(label, MTD_Name.c_str()) == 0) {
507 // We found our device
508 // Strip off the trailing : from the device
509 device[strlen(device)-1] = '\0';
510 if (sscanf(device,"mtd%d", &deviceId) == 1) {
511 sprintf(device, "/dev/block/mtdblock%d", deviceId);
512 Primary_Block_Device = device;
513 }
514 }
515 }
516 fclose(fp);
517
518 return false;
519}
520
Dees_Troy51127312012-09-08 13:08:49 -0400521bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
522 struct statfs st;
523 string Local_Path = Mount_Point + "/.";
524
525 if (!Mount(Display_Error))
526 return false;
527
528 if (statfs(Local_Path.c_str(), &st) != 0) {
529 if (!Removable) {
530 if (Display_Error)
531 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
532 else
533 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
534 }
535 return false;
536 }
537 Size = (st.f_blocks * st.f_bsize);
538 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
539 Free = (st.f_bfree * st.f_bsize);
540 Backup_Size = Used;
541 return true;
542}
543
544bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400545 FILE* fp;
546 char command[255], line[512];
547 int include_block = 1;
548 unsigned int min_len;
549
550 if (!Mount(Display_Error))
551 return false;
552
Dees_Troy38bd7602012-09-14 13:33:53 -0400553 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400554 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400555 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400556 fp = fopen("/tmp/dfoutput.txt", "rt");
557 if (fp == NULL) {
558 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400559 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400560 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400561
562 while (fgets(line, sizeof(line), fp) != NULL)
563 {
564 unsigned long blocks, used, available;
565 char device[64];
566 char tmpString[64];
567
568 if (strncmp(line, "Filesystem", 10) == 0)
569 continue;
570 if (strlen(line) < min_len) {
571 include_block = 0;
572 continue;
573 }
574 if (include_block) {
575 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
576 } else {
577 // The device block string is so long that the df information is on the next line
578 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400579 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400580 while (tmpString[space_count] == 32)
581 space_count++;
582 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
583 }
584
585 // Adjust block size to byte size
586 Size = blocks * 1024ULL;
587 Used = used * 1024ULL;
588 Free = available * 1024ULL;
589 Backup_Size = Used;
590 }
591 fclose(fp);
592 return true;
593}
594
Dees_Troy5bf43922012-09-07 16:07:55 -0400595bool TWPartition::Find_Partition_Size(void) {
596 FILE* fp;
597 char line[512];
598 string tmpdevice;
599
600 // In this case, we'll first get the partitions we care about (with labels)
601 fp = fopen("/proc/partitions", "rt");
602 if (fp == NULL)
603 return false;
604
605 while (fgets(line, sizeof(line), fp) != NULL)
606 {
607 unsigned long major, minor, blocks;
608 char device[512];
609 char tmpString[64];
610
Dees_Troy63c8df72012-09-10 14:02:05 -0400611 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400612 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
613
614 tmpdevice = "/dev/block/";
615 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400616 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400617 // Adjust block size to byte size
618 Size = blocks * 1024ULL;
619 fclose(fp);
620 return true;
621 }
622 }
623 fclose(fp);
624 return false;
625}
626
Dees_Troy5bf43922012-09-07 16:07:55 -0400627void TWPartition::Flip_Block_Device(void) {
628 string temp;
629
630 temp = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400631 Primary_Block_Device = Alternate_Block_Device;
Dees_Troy5bf43922012-09-07 16:07:55 -0400632 Alternate_Block_Device = temp;
633}
634
635bool 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());
673 return false;
674 } else {
675 if (Removable)
676 Update_Size(Display_Error);
677
678 if (!Symlink_Mount_Point.empty()) {
679 string Command;
680
681 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400682 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400683 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400684 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400685 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400686 return true;
687}
688
689bool TWPartition::UnMount(bool Display_Error) {
690 if (Is_Mounted()) {
691 int never_unmount_system;
692
693 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
694 if (never_unmount_system == 1 && Mount_Point == "/system")
695 return true; // Never unmount system if you're not supposed to unmount it
696
Dees_Troy38bd7602012-09-14 13:33:53 -0400697 if (!Symlink_Mount_Point.empty())
698 umount(Symlink_Mount_Point.c_str());
699
Dees_Troy5bf43922012-09-07 16:07:55 -0400700 if (umount(Mount_Point.c_str()) != 0) {
701 if (Display_Error)
702 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
703 else
704 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
705 return false;
706 } else
707 return true;
708 } else {
709 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400710 }
711}
712
713bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400714 if (!Can_Be_Wiped) {
715 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
716 return false;
717 }
718
Dees_Troyc51f1f92012-09-20 15:32:13 -0400719 if (Mount_Point == "/cache")
720 tmplog_offset = 0;
721
Dees_Troy38bd7602012-09-14 13:33:53 -0400722 if (Has_Data_Media)
723 return Wipe_Data_Without_Wiping_Media();
724
725 int check;
726 DataManager::GetValue(TW_RM_RF_VAR, check);
727 if (check)
728 return Wipe_RMRF();
729
730 if (Current_File_System == "ext4")
731 return Wipe_EXT4();
732
733 if (Current_File_System == "ext2" || Current_File_System == "ext3")
734 return Wipe_EXT23();
735
736 if (Current_File_System == "vfat")
737 return Wipe_FAT();
738
739 if (Current_File_System == "yaffs2")
740 return Wipe_MTD();
741
742 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
743 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400744}
745
Dees_Troye58d5262012-09-21 12:27:57 -0400746bool TWPartition::Wipe_AndSec(void) {
747 if (!Has_Android_Secure)
748 return false;
749
750 char cmd[512];
751
752 if (!Mount(true))
753 return false;
754
755 ui_print("Using rm -rf on .android_secure\n");
756 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
757
758 LOGI("rm -rf command is: '%s'\n", cmd);
759 system(cmd);
760 return true;
761}
762
Dees_Troy51a0e822012-09-05 15:24:24 -0400763bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400764 if (Backup_Method == FILES)
765 return Backup_Tar(backup_folder);
766 else if (Backup_Method == DD)
767 return Backup_DD(backup_folder);
768 else if (Backup_Method == FLASH_UTILS)
769 return Backup_Dump_Image(backup_folder);
770 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
771 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400772}
773
Dees_Troy43d8b002012-09-17 16:00:01 -0400774bool TWPartition::Check_MD5(string restore_folder) {
775 string Full_Filename;
776 char split_filename[512];
777 int index = 0;
778
779 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400780 if (!TWFunc::Path_Exists(Full_Filename)) {
781 // This is a split archive, we presume
782 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
783 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
784 if (TWFunc::Check_MD5(split_filename) == 0) {
785 LOGE("MD5 failed to match on '%s'.\n", split_filename);
786 return false;
787 }
788 index++;
789 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400790 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400791 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400792 } else {
793 // Single file archive
794 if (TWFunc::Check_MD5(Full_Filename) == 0) {
795 LOGE("MD5 failed to match on '%s'.\n", split_filename);
796 return false;
797 } else
798 return true;
799 }
800 return false;
801}
802
Dees_Troy51a0e822012-09-05 15:24:24 -0400803bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400804 if (Backup_Method == FILES)
805 return Restore_Tar(restore_folder);
806 else if (Backup_Method == DD)
807 return Restore_DD(restore_folder);
808 else if (Backup_Method == FLASH_UTILS)
809 return Restore_Flash_Image(restore_folder);
810 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
811 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400812}
813
814string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400815 if (Backup_Method == NONE)
816 return "none";
817 else if (Backup_Method == FILES)
818 return "files";
819 else if (Backup_Method == DD)
820 return "dd";
821 else if (Backup_Method == FLASH_UTILS)
822 return "flash_utils";
823 else
824 return "undefined";
825 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400826}
827
828bool TWPartition::Decrypt(string Password) {
829 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400830 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400831 return 1;
832}
833
834bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400835 bool Save_Data_Media = Has_Data_Media;
836
837 if (!UnMount(true))
838 return false;
839
840 Current_File_System = Fstab_File_System;
841 Is_Encrypted = false;
842 Is_Decrypted = false;
843 Decrypted_Block_Device = "";
844 Has_Data_Media = false;
845 if (Wipe()) {
846 Has_Data_Media = Save_Data_Media;
847 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
848 Recreate_Media_Folder();
849 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400850 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400851 return true;
852 } else {
853 Has_Data_Media = Save_Data_Media;
854 LOGE("Unable to format to remove encryption.\n");
855 return false;
856 }
857 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400858}
859
860void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400861 FILE *fp;
862 string blkCommand;
863 char blkOutput[255];
864 char* blk;
865 char* arg;
866 char* ptr;
867
868 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
869 return; // Running blkid on some mtd devices causes a massive crash
870
Dees_Troy38bd7602012-09-14 13:33:53 -0400871 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400872 if (!Is_Present)
873 return;
Dees_Troy51127312012-09-08 13:08:49 -0400874
Dees_Troy8170a922012-09-18 15:40:25 -0400875 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
876 system("rm /tmp/blkidoutput.txt");
877
878 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400879 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400880 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400881 if (fp == NULL)
882 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400883 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
884 {
885 blk = blkOutput;
886 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400887 while (*ptr > 32 && *ptr != ':') ptr++;
888 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400889 *ptr = 0;
890
891 // Increment by two, but verify that we don't hit a NULL
892 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400893 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400894
895 // Now, find the TYPE field
896 while (1)
897 {
898 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400899 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400900 if (*ptr != 0)
901 {
902 *ptr = 0;
903 ptr++;
904 }
905
906 if (strlen(arg) > 6)
907 {
908 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
909 }
910
911 if (*ptr == 0)
912 {
913 arg = NULL;
914 break;
915 }
916 }
917
918 if (arg && strlen(arg) > 7)
919 {
920 arg += 6; // Skip the TYPE=" portion
921 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
922 }
923 else
924 continue;
925
Dees_Troy63c8df72012-09-10 14:02:05 -0400926 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400927 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
928 Current_File_System = arg;
929 }
930 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400931 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400932 return;
933}
934
935bool TWPartition::Wipe_EXT23() {
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/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400940 char command[512];
941
942 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
943 Find_Actual_Block_Device();
944 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
945 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400946 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400947 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400948 ui_print("Done.\n");
949 return true;
950 } else {
951 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
952 return false;
953 }
954 } else
955 return Wipe_RMRF();
956
957 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400958}
959
960bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400961 if (!UnMount(true))
962 return false;
963
Dees_Troy43d8b002012-09-17 16:00:01 -0400964 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400965 string Command;
966
967 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
968 Find_Actual_Block_Device();
969 Command = "make_ext4fs";
970 if (!Is_Decrypted && Length != 0) {
971 // Only use length if we're not decrypted
972 char len[32];
973 sprintf(len, "%i", Length);
974 Command += " -l ";
975 Command += len;
976 }
977 Command += " " + Actual_Block_Device;
978 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400979 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400980 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400981 ui_print("Done.\n");
982 return true;
983 } else {
984 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
985 return false;
986 }
987 } else
988 return Wipe_EXT23();
989
990 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400991}
992
993bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400994 char command[512];
995
Dees_Troy43d8b002012-09-17 16:00:01 -0400996 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400997 if (!UnMount(true))
998 return false;
999
1000 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1001 Find_Actual_Block_Device();
1002 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -04001003 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001004 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001005 ui_print("Done.\n");
1006 return true;
1007 } else {
1008 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1009 return false;
1010 }
1011 return true;
1012 }
1013 else
1014 return Wipe_RMRF();
1015
1016 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001017}
1018
Dees_Troy38bd7602012-09-14 13:33:53 -04001019bool TWPartition::Wipe_MTD() {
1020 if (!UnMount(true))
1021 return false;
1022
1023 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1024
1025 mtd_scan_partitions();
1026 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1027 if (mtd == NULL) {
1028 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1029 return false;
1030 }
1031
1032 MtdWriteContext* ctx = mtd_write_partition(mtd);
1033 if (ctx == NULL) {
1034 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1035 return false;
1036 }
1037 if (mtd_erase_blocks(ctx, -1) == -1) {
1038 mtd_write_close(ctx);
1039 LOGE("Failed to format '%s'", MTD_Name.c_str());
1040 return false;
1041 }
1042 if (mtd_write_close(ctx) != 0) {
1043 LOGE("Failed to close '%s'", MTD_Name.c_str());
1044 return false;
1045 }
Dees_Troye58d5262012-09-21 12:27:57 -04001046 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001047 ui_print("Done.\n");
1048 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001049}
1050
1051bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001052 char cmd[512];
1053
1054 if (!Mount(true))
1055 return false;
1056
Dees_Troye58d5262012-09-21 12:27:57 -04001057 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1058 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001059
1060 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001061 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001062 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001063 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001064}
1065
1066bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001067 char cmd[256];
1068
1069 // This handles wiping data on devices with "sdcard" in /data/media
1070 if (!Mount(true))
1071 return false;
1072
1073 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001074 system("rm -f /data/*");
1075 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001076
1077 DIR* d;
1078 d = opendir("/data");
1079 if (d != NULL)
1080 {
1081 struct dirent* de;
1082 while ((de = readdir(d)) != NULL) {
1083 if (strcmp(de->d_name, "media") == 0) continue;
1084
1085 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001086 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001087 }
1088 closedir(d);
1089 }
1090 ui_print("Done.\n");
1091 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001092}
1093
1094bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001095 char back_name[255], split_index[5];
1096 string Full_FileName, Split_FileName, Tar_Args, Command;
1097 int use_compression, index, backup_count;
1098 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001099 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001100
1101 if (!Mount(true))
1102 return false;
1103
Dees_Troy2c50e182012-09-26 20:05:28 -04001104 if (Backup_Path == "/and-sec") {
1105 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1106 ui_print("Backing up %s...\n", "Android Secure");
1107 } else {
1108 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1109 ui_print("Backing up %s...\n", Display_Name.c_str());
1110 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001111
1112 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1113 if (use_compression)
1114 Tar_Args = "-cz";
1115 else
1116 Tar_Args = "-c";
1117
1118 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1119 Backup_FileName = back_name;
1120
Dees_Troy43d8b002012-09-17 16:00:01 -04001121 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1122 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001123 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001124 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001125 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001126 if (backup_count < 1) {
1127 LOGE("Error generating file list!\n");
1128 return false;
1129 }
1130 for (index=0; index<backup_count; index++) {
1131 sprintf(split_index, "%03i", index);
1132 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1133 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1134 LOGI("Backup command: '%s'\n", Command.c_str());
1135 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1136 system(Command.c_str()); // sending backup command formed earlier above
1137
Dees_Troy7c2dec82012-09-26 09:49:14 -04001138 file_size = TWFunc::Get_File_Size(Full_FileName);
1139 if (file_size == 0) {
1140 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 -04001141 return false;
1142 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001143 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001144 }
1145 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1146 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001147 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001148 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001149 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001150 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001151 else
Dees_Troye58d5262012-09-21 12:27:57 -04001152 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001153 LOGI("Backup command: '%s'\n", Command.c_str());
1154 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001155 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1156 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1157 return false;
1158 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001159 }
1160 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001161}
1162
1163bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001164 char back_name[255];
1165 string Full_FileName, Command;
1166 int use_compression;
1167
Dees_Troyb46a6842012-09-25 11:06:46 -04001168 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001169 ui_print("Backing up %s...\n", Display_Name.c_str());
1170
1171 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1172 Backup_FileName = back_name;
1173
1174 Full_FileName = backup_folder + "/" + Backup_FileName;
1175
1176 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1177 LOGI("Backup command: '%s'\n", Command.c_str());
1178 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001179 if (TWFunc::Get_File_Size(Full_FileName) != Backup_Size) {
1180 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);
1181 return false;
1182 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001183 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001184}
1185
1186bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001187 char back_name[255];
1188 string Full_FileName, Command;
1189 int use_compression;
1190
Dees_Troyb46a6842012-09-25 11:06:46 -04001191 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001192 ui_print("Backing up %s...\n", Display_Name.c_str());
1193
1194 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1195 Backup_FileName = back_name;
1196
1197 Full_FileName = backup_folder + "/" + Backup_FileName;
1198
1199 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1200 LOGI("Backup command: '%s'\n", Command.c_str());
1201 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001202 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1203 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1204 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1205 return false;
1206 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001207 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001208}
1209
1210bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001211 size_t first_period, second_period;
1212 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001213 int index = 0;
1214 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001215
Dees_Troyb46a6842012-09-25 11:06:46 -04001216 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001217 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1218
1219 // Parse backup filename to extract the file system before wiping
1220 first_period = Backup_FileName.find(".");
1221 if (first_period == string::npos) {
1222 LOGE("Unable to find file system (first period).\n");
1223 return false;
1224 }
1225 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1226 second_period = Restore_File_System.find(".");
1227 if (second_period == string::npos) {
1228 LOGE("Unable to find file system (second period).\n");
1229 return false;
1230 }
1231 Restore_File_System.resize(second_period);
1232 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1233 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001234 if (Has_Android_Secure) {
1235 ui_print("Wiping android secure...\n");
1236 if (!Wipe_AndSec())
1237 return false;
1238 } else if (!Wipe()) {
1239 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001240 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001241 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001242
1243 if (!Mount(true))
1244 return false;
1245
Dees_Troy43d8b002012-09-17 16:00:01 -04001246 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001247 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001248 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001249 // Backup is multiple archives
1250 LOGI("Backup is multiple archives.\n");
1251 sprintf(split_index, "%03i", index);
1252 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1253 while (TWFunc::Path_Exists(Full_FileName)) {
1254 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001255 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001256 LOGI("Restore command: '%s'\n", Command.c_str());
1257 system(Command.c_str());
1258 index++;
1259 sprintf(split_index, "%03i", index);
1260 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1261 }
1262 if (index == 0) {
1263 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1264 return false;
1265 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001266 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001267 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001268 LOGI("Restore command: '%s'\n", Command.c_str());
1269 system(Command.c_str());
1270 }
1271 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001272}
1273
1274bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001275 string Full_FileName, Command;
1276
Dees_Troyb46a6842012-09-25 11:06:46 -04001277 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001278 ui_print("Restoring %s...\n", Display_Name.c_str());
1279 Full_FileName = restore_folder + "/" + Backup_FileName;
1280 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1281 LOGI("Restore command: '%s'\n", Command.c_str());
1282 system(Command.c_str());
1283 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001284}
1285
1286bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001287 string Full_FileName, Command;
1288
Dees_Troyb46a6842012-09-25 11:06:46 -04001289 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001290 ui_print("Restoring %s...\n", Display_Name.c_str());
1291 Full_FileName = restore_folder + "/" + Backup_FileName;
1292 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1293 Command = "erase_image " + MTD_Name;
1294 LOGI("Erase command: '%s'\n", Command.c_str());
1295 system(Command.c_str());
1296 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1297 LOGI("Restore command: '%s'\n", Command.c_str());
1298 system(Command.c_str());
1299 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001300}
Dees_Troy5bf43922012-09-07 16:07:55 -04001301
1302bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001303 bool ret = false;
1304
Dees_Troyab10ee22012-09-21 14:27:30 -04001305 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001306 return false;
1307
Dees_Troy38bd7602012-09-14 13:33:53 -04001308 if (Removable || Is_Encrypted) {
1309 if (!Mount(false))
1310 return true;
1311 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001312 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001313
1314 ret = Get_Size_Via_statfs(Display_Error);
1315 if (!ret || Size == 0)
1316 if (!Get_Size_Via_df(Display_Error))
1317 return false;
1318
Dees_Troy5bf43922012-09-07 16:07:55 -04001319 if (Has_Data_Media) {
1320 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001321 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001322 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1323 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001324 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001325 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001326 int bak = (int)(Backup_Size / 1048576LLU);
1327 int total = (int)(Size / 1048576LLU);
1328 int us = (int)(Used / 1048576LLU);
1329 int fre = (int)(Free / 1048576LLU);
1330 int datmed = (int)(data_media_used / 1048576LLU);
1331 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 -04001332 } else
1333 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001334 } else if (Has_Android_Secure) {
1335 if (Mount(Display_Error))
1336 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
1337 else
1338 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001339 }
1340 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001341}
Dees_Troy38bd7602012-09-14 13:33:53 -04001342
1343void TWPartition::Find_Actual_Block_Device(void) {
1344 if (Is_Decrypted) {
1345 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001346 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001347 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001348 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001349 Is_Present = true;
1350 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001351 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001352 Flip_Block_Device();
1353 Actual_Block_Device = Primary_Block_Device;
1354 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");
1384 TWFunc::Recursive_Mkdir(Symlink_Path);
1385 Command = "umount " + Symlink_Mount_Point;
1386 system(Command.c_str());
1387 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1388 system(Command.c_str());
1389 }
1390}