blob: 633011ba69762d5712b7372d15955c40f9104a7a [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 -0400627bool TWPartition::Is_Mounted(void) {
628 if (!Can_Be_Mounted)
629 return false;
630
631 struct stat st1, st2;
632 string test_path;
633
634 // Check to see if the mount point directory exists
635 test_path = Mount_Point + "/.";
636 if (stat(test_path.c_str(), &st1) != 0) return false;
637
638 // Check to see if the directory above the mount point exists
639 test_path = Mount_Point + "/../.";
640 if (stat(test_path.c_str(), &st2) != 0) return false;
641
642 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
643 int ret = (st1.st_dev != st2.st_dev) ? true : false;
644
645 return ret;
646}
647
648bool TWPartition::Mount(bool Display_Error) {
649 if (Is_Mounted()) {
650 return true;
651 } else if (!Can_Be_Mounted) {
652 return false;
653 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400654
655 Find_Actual_Block_Device();
656
657 // Check the current file system before mounting
658 Check_FS_Type();
659
660 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
661 if (Display_Error)
662 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
663 else
664 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy9350b8d2012-09-27 12:38:38 -0400665 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 -0400666 return false;
667 } else {
668 if (Removable)
669 Update_Size(Display_Error);
670
671 if (!Symlink_Mount_Point.empty()) {
672 string Command;
673
674 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400675 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400676 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400677 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400678 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400679 return true;
680}
681
682bool TWPartition::UnMount(bool Display_Error) {
683 if (Is_Mounted()) {
684 int never_unmount_system;
685
686 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
687 if (never_unmount_system == 1 && Mount_Point == "/system")
688 return true; // Never unmount system if you're not supposed to unmount it
689
Dees_Troy38bd7602012-09-14 13:33:53 -0400690 if (!Symlink_Mount_Point.empty())
691 umount(Symlink_Mount_Point.c_str());
692
Dees_Troy5bf43922012-09-07 16:07:55 -0400693 if (umount(Mount_Point.c_str()) != 0) {
694 if (Display_Error)
695 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
696 else
697 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
698 return false;
699 } else
700 return true;
701 } else {
702 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400703 }
704}
705
706bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400707 if (!Can_Be_Wiped) {
708 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
709 return false;
710 }
711
Dees_Troyc51f1f92012-09-20 15:32:13 -0400712 if (Mount_Point == "/cache")
713 tmplog_offset = 0;
714
Dees_Troy38bd7602012-09-14 13:33:53 -0400715 if (Has_Data_Media)
716 return Wipe_Data_Without_Wiping_Media();
717
718 int check;
719 DataManager::GetValue(TW_RM_RF_VAR, check);
720 if (check)
721 return Wipe_RMRF();
722
723 if (Current_File_System == "ext4")
724 return Wipe_EXT4();
725
726 if (Current_File_System == "ext2" || Current_File_System == "ext3")
727 return Wipe_EXT23();
728
729 if (Current_File_System == "vfat")
730 return Wipe_FAT();
731
732 if (Current_File_System == "yaffs2")
733 return Wipe_MTD();
734
735 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
736 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400737}
738
Dees_Troye58d5262012-09-21 12:27:57 -0400739bool TWPartition::Wipe_AndSec(void) {
740 if (!Has_Android_Secure)
741 return false;
742
743 char cmd[512];
744
745 if (!Mount(true))
746 return false;
747
748 ui_print("Using rm -rf on .android_secure\n");
749 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
750
751 LOGI("rm -rf command is: '%s'\n", cmd);
752 system(cmd);
753 return true;
754}
755
Dees_Troy51a0e822012-09-05 15:24:24 -0400756bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400757 if (Backup_Method == FILES)
758 return Backup_Tar(backup_folder);
759 else if (Backup_Method == DD)
760 return Backup_DD(backup_folder);
761 else if (Backup_Method == FLASH_UTILS)
762 return Backup_Dump_Image(backup_folder);
763 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
764 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400765}
766
Dees_Troy43d8b002012-09-17 16:00:01 -0400767bool TWPartition::Check_MD5(string restore_folder) {
768 string Full_Filename;
769 char split_filename[512];
770 int index = 0;
771
772 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400773 if (!TWFunc::Path_Exists(Full_Filename)) {
774 // This is a split archive, we presume
775 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
776 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
777 if (TWFunc::Check_MD5(split_filename) == 0) {
778 LOGE("MD5 failed to match on '%s'.\n", split_filename);
779 return false;
780 }
781 index++;
782 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400783 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400784 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400785 } else {
786 // Single file archive
787 if (TWFunc::Check_MD5(Full_Filename) == 0) {
788 LOGE("MD5 failed to match on '%s'.\n", split_filename);
789 return false;
790 } else
791 return true;
792 }
793 return false;
794}
795
Dees_Troy51a0e822012-09-05 15:24:24 -0400796bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400797 if (Backup_Method == FILES)
798 return Restore_Tar(restore_folder);
799 else if (Backup_Method == DD)
800 return Restore_DD(restore_folder);
801 else if (Backup_Method == FLASH_UTILS)
802 return Restore_Flash_Image(restore_folder);
803 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
804 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400805}
806
807string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400808 if (Backup_Method == NONE)
809 return "none";
810 else if (Backup_Method == FILES)
811 return "files";
812 else if (Backup_Method == DD)
813 return "dd";
814 else if (Backup_Method == FLASH_UTILS)
815 return "flash_utils";
816 else
817 return "undefined";
818 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400819}
820
821bool TWPartition::Decrypt(string Password) {
822 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400823 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400824 return 1;
825}
826
827bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400828 bool Save_Data_Media = Has_Data_Media;
829
830 if (!UnMount(true))
831 return false;
832
833 Current_File_System = Fstab_File_System;
834 Is_Encrypted = false;
835 Is_Decrypted = false;
836 Decrypted_Block_Device = "";
837 Has_Data_Media = false;
838 if (Wipe()) {
839 Has_Data_Media = Save_Data_Media;
840 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
841 Recreate_Media_Folder();
842 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400843 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400844 return true;
845 } else {
846 Has_Data_Media = Save_Data_Media;
847 LOGE("Unable to format to remove encryption.\n");
848 return false;
849 }
850 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400851}
852
853void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400854 FILE *fp;
855 string blkCommand;
856 char blkOutput[255];
857 char* blk;
858 char* arg;
859 char* ptr;
860
861 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
862 return; // Running blkid on some mtd devices causes a massive crash
863
Dees_Troy38bd7602012-09-14 13:33:53 -0400864 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400865 if (!Is_Present)
866 return;
Dees_Troy51127312012-09-08 13:08:49 -0400867
Dees_Troy8170a922012-09-18 15:40:25 -0400868 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
869 system("rm /tmp/blkidoutput.txt");
870
871 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400872 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400873 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400874 if (fp == NULL)
875 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400876 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
877 {
878 blk = blkOutput;
879 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400880 while (*ptr > 32 && *ptr != ':') ptr++;
881 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400882 *ptr = 0;
883
884 // Increment by two, but verify that we don't hit a NULL
885 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400886 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400887
888 // Now, find the TYPE field
889 while (1)
890 {
891 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400892 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400893 if (*ptr != 0)
894 {
895 *ptr = 0;
896 ptr++;
897 }
898
899 if (strlen(arg) > 6)
900 {
901 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
902 }
903
904 if (*ptr == 0)
905 {
906 arg = NULL;
907 break;
908 }
909 }
910
911 if (arg && strlen(arg) > 7)
912 {
913 arg += 6; // Skip the TYPE=" portion
914 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
915 }
916 else
917 continue;
918
Dees_Troy63c8df72012-09-10 14:02:05 -0400919 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400920 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
921 Current_File_System = arg;
922 }
923 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400924 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400925 return;
926}
927
928bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400929 if (!UnMount(true))
930 return false;
931
Dees_Troy43d8b002012-09-17 16:00:01 -0400932 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400933 char command[512];
934
935 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
936 Find_Actual_Block_Device();
937 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
938 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400939 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400940 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400941 ui_print("Done.\n");
942 return true;
943 } else {
944 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
945 return false;
946 }
947 } else
948 return Wipe_RMRF();
949
950 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400951}
952
953bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400954 if (!UnMount(true))
955 return false;
956
Dees_Troy43d8b002012-09-17 16:00:01 -0400957 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400958 string Command;
959
960 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
961 Find_Actual_Block_Device();
962 Command = "make_ext4fs";
963 if (!Is_Decrypted && Length != 0) {
964 // Only use length if we're not decrypted
965 char len[32];
966 sprintf(len, "%i", Length);
967 Command += " -l ";
968 Command += len;
969 }
970 Command += " " + Actual_Block_Device;
971 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400972 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400973 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400974 ui_print("Done.\n");
975 return true;
976 } else {
977 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
978 return false;
979 }
980 } else
981 return Wipe_EXT23();
982
983 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400984}
985
986bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400987 char command[512];
988
Dees_Troy43d8b002012-09-17 16:00:01 -0400989 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400990 if (!UnMount(true))
991 return false;
992
993 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
994 Find_Actual_Block_Device();
995 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400996 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400997 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400998 ui_print("Done.\n");
999 return true;
1000 } else {
1001 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1002 return false;
1003 }
1004 return true;
1005 }
1006 else
1007 return Wipe_RMRF();
1008
1009 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001010}
1011
Dees_Troy38bd7602012-09-14 13:33:53 -04001012bool TWPartition::Wipe_MTD() {
1013 if (!UnMount(true))
1014 return false;
1015
1016 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1017
1018 mtd_scan_partitions();
1019 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1020 if (mtd == NULL) {
1021 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1022 return false;
1023 }
1024
1025 MtdWriteContext* ctx = mtd_write_partition(mtd);
1026 if (ctx == NULL) {
1027 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1028 return false;
1029 }
1030 if (mtd_erase_blocks(ctx, -1) == -1) {
1031 mtd_write_close(ctx);
1032 LOGE("Failed to format '%s'", MTD_Name.c_str());
1033 return false;
1034 }
1035 if (mtd_write_close(ctx) != 0) {
1036 LOGE("Failed to close '%s'", MTD_Name.c_str());
1037 return false;
1038 }
Dees_Troye58d5262012-09-21 12:27:57 -04001039 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001040 ui_print("Done.\n");
1041 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001042}
1043
1044bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001045 char cmd[512];
1046
1047 if (!Mount(true))
1048 return false;
1049
Dees_Troye58d5262012-09-21 12:27:57 -04001050 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1051 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001052
1053 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001054 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001055 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001056 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001057}
1058
1059bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001060 char cmd[256];
1061
1062 // This handles wiping data on devices with "sdcard" in /data/media
1063 if (!Mount(true))
1064 return false;
1065
1066 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001067 system("rm -f /data/*");
1068 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001069
1070 DIR* d;
1071 d = opendir("/data");
1072 if (d != NULL)
1073 {
1074 struct dirent* de;
1075 while ((de = readdir(d)) != NULL) {
1076 if (strcmp(de->d_name, "media") == 0) continue;
1077
1078 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001079 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001080 }
1081 closedir(d);
1082 }
1083 ui_print("Done.\n");
1084 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001085}
1086
1087bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001088 char back_name[255], split_index[5];
1089 string Full_FileName, Split_FileName, Tar_Args, Command;
1090 int use_compression, index, backup_count;
1091 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001092 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001093
1094 if (!Mount(true))
1095 return false;
1096
Dees_Troy2c50e182012-09-26 20:05:28 -04001097 if (Backup_Path == "/and-sec") {
1098 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1099 ui_print("Backing up %s...\n", "Android Secure");
1100 } else {
1101 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1102 ui_print("Backing up %s...\n", Display_Name.c_str());
1103 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001104
1105 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1106 if (use_compression)
1107 Tar_Args = "-cz";
1108 else
1109 Tar_Args = "-c";
1110
1111 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1112 Backup_FileName = back_name;
1113
Dees_Troy43d8b002012-09-17 16:00:01 -04001114 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1115 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001116 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001117 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001118 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001119 if (backup_count < 1) {
1120 LOGE("Error generating file list!\n");
1121 return false;
1122 }
1123 for (index=0; index<backup_count; index++) {
1124 sprintf(split_index, "%03i", index);
1125 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1126 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1127 LOGI("Backup command: '%s'\n", Command.c_str());
1128 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1129 system(Command.c_str()); // sending backup command formed earlier above
1130
Dees_Troy7c2dec82012-09-26 09:49:14 -04001131 file_size = TWFunc::Get_File_Size(Full_FileName);
1132 if (file_size == 0) {
1133 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 -04001134 return false;
1135 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001136 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001137 }
1138 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1139 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001140 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001141 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001142 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001143 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001144 else
Dees_Troye58d5262012-09-21 12:27:57 -04001145 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001146 LOGI("Backup command: '%s'\n", Command.c_str());
1147 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001148 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1149 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1150 return false;
1151 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001152 }
1153 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001154}
1155
1156bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001157 char back_name[255];
1158 string Full_FileName, Command;
1159 int use_compression;
1160
Dees_Troyb46a6842012-09-25 11:06:46 -04001161 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001162 ui_print("Backing up %s...\n", Display_Name.c_str());
1163
1164 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1165 Backup_FileName = back_name;
1166
1167 Full_FileName = backup_folder + "/" + Backup_FileName;
1168
1169 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1170 LOGI("Backup command: '%s'\n", Command.c_str());
1171 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001172 if (TWFunc::Get_File_Size(Full_FileName) != Backup_Size) {
1173 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);
1174 return false;
1175 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001176 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001177}
1178
1179bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001180 char back_name[255];
1181 string Full_FileName, Command;
1182 int use_compression;
1183
Dees_Troyb46a6842012-09-25 11:06:46 -04001184 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001185 ui_print("Backing up %s...\n", Display_Name.c_str());
1186
1187 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1188 Backup_FileName = back_name;
1189
1190 Full_FileName = backup_folder + "/" + Backup_FileName;
1191
1192 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1193 LOGI("Backup command: '%s'\n", Command.c_str());
1194 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001195 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1196 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1197 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1198 return false;
1199 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001200 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001201}
1202
1203bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001204 size_t first_period, second_period;
1205 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001206 int index = 0;
1207 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001208
Dees_Troyb46a6842012-09-25 11:06:46 -04001209 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001210 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1211
1212 // Parse backup filename to extract the file system before wiping
1213 first_period = Backup_FileName.find(".");
1214 if (first_period == string::npos) {
1215 LOGE("Unable to find file system (first period).\n");
1216 return false;
1217 }
1218 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1219 second_period = Restore_File_System.find(".");
1220 if (second_period == string::npos) {
1221 LOGE("Unable to find file system (second period).\n");
1222 return false;
1223 }
1224 Restore_File_System.resize(second_period);
1225 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1226 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001227 if (Has_Android_Secure) {
1228 ui_print("Wiping android secure...\n");
1229 if (!Wipe_AndSec())
1230 return false;
1231 } else if (!Wipe()) {
1232 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001233 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001234 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001235
1236 if (!Mount(true))
1237 return false;
1238
Dees_Troy43d8b002012-09-17 16:00:01 -04001239 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001240 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001241 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001242 // Backup is multiple archives
1243 LOGI("Backup is multiple archives.\n");
1244 sprintf(split_index, "%03i", index);
1245 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1246 while (TWFunc::Path_Exists(Full_FileName)) {
1247 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001248 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001249 LOGI("Restore command: '%s'\n", Command.c_str());
1250 system(Command.c_str());
1251 index++;
1252 sprintf(split_index, "%03i", index);
1253 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1254 }
1255 if (index == 0) {
1256 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1257 return false;
1258 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001259 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001260 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001261 LOGI("Restore command: '%s'\n", Command.c_str());
1262 system(Command.c_str());
1263 }
1264 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001265}
1266
1267bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001268 string Full_FileName, Command;
1269
Dees_Troyb46a6842012-09-25 11:06:46 -04001270 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001271 ui_print("Restoring %s...\n", Display_Name.c_str());
1272 Full_FileName = restore_folder + "/" + Backup_FileName;
1273 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1274 LOGI("Restore command: '%s'\n", Command.c_str());
1275 system(Command.c_str());
1276 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001277}
1278
1279bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001280 string Full_FileName, Command;
1281
Dees_Troyb46a6842012-09-25 11:06:46 -04001282 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001283 ui_print("Restoring %s...\n", Display_Name.c_str());
1284 Full_FileName = restore_folder + "/" + Backup_FileName;
1285 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1286 Command = "erase_image " + MTD_Name;
1287 LOGI("Erase command: '%s'\n", Command.c_str());
1288 system(Command.c_str());
1289 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1290 LOGI("Restore command: '%s'\n", Command.c_str());
1291 system(Command.c_str());
1292 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001293}
Dees_Troy5bf43922012-09-07 16:07:55 -04001294
1295bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001296 bool ret = false;
1297
Dees_Troyab10ee22012-09-21 14:27:30 -04001298 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001299 return false;
1300
Dees_Troy38bd7602012-09-14 13:33:53 -04001301 if (Removable || Is_Encrypted) {
1302 if (!Mount(false))
1303 return true;
1304 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001305 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001306
1307 ret = Get_Size_Via_statfs(Display_Error);
1308 if (!ret || Size == 0)
1309 if (!Get_Size_Via_df(Display_Error))
1310 return false;
1311
Dees_Troy5bf43922012-09-07 16:07:55 -04001312 if (Has_Data_Media) {
1313 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001314 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001315 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1316 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001317 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001318 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001319 int bak = (int)(Backup_Size / 1048576LLU);
1320 int total = (int)(Size / 1048576LLU);
1321 int us = (int)(Used / 1048576LLU);
1322 int fre = (int)(Free / 1048576LLU);
1323 int datmed = (int)(data_media_used / 1048576LLU);
1324 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 -04001325 } else
1326 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001327 } else if (Has_Android_Secure) {
1328 if (Mount(Display_Error))
1329 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
1330 else
1331 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001332 }
1333 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001334}
Dees_Troy38bd7602012-09-14 13:33:53 -04001335
1336void TWPartition::Find_Actual_Block_Device(void) {
1337 if (Is_Decrypted) {
1338 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001339 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001340 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001341 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001342 Is_Present = true;
1343 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001344 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001345 Actual_Block_Device = Primary_Block_Device;
1346 Is_Present = true;
1347 } else
1348 Is_Present = false;
1349}
1350
1351void TWPartition::Recreate_Media_Folder(void) {
1352 string Command;
1353
1354 if (!Mount(true)) {
1355 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001356 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001357 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001358 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001359 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001360 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001361 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001362 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001363 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001364}
Dees_Troye58d5262012-09-21 12:27:57 -04001365
1366void TWPartition::Recreate_AndSec_Folder(void) {
1367 string Command;
1368
1369 if (!Has_Android_Secure)
1370 return;
1371
1372 if (!Mount(true)) {
1373 LOGE("Unable to recreate android secure folder.\n");
1374 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1375 LOGI("Recreating android secure folder.\n");
1376 TWFunc::Recursive_Mkdir(Symlink_Path);
1377 Command = "umount " + Symlink_Mount_Point;
1378 system(Command.c_str());
1379 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1380 system(Command.c_str());
1381 }
1382}