blob: 4c0baedcd760d9b27592db54581b722af457d82c [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_Troyc51f1f92012-09-20 15:32:13 -040045 #include "extra-functions.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040046}
Dees_Troy51a0e822012-09-05 15:24:24 -040047
48TWPartition::TWPartition(void) {
49 Can_Be_Mounted = false;
50 Can_Be_Wiped = false;
51 Wipe_During_Factory_Reset = false;
52 Wipe_Available_in_GUI = false;
53 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040054 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040055 SubPartition_Of = "";
56 Symlink_Path = "";
57 Symlink_Mount_Point = "";
58 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040059 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040060 Actual_Block_Device = "";
61 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040062 Alternate_Block_Device = "";
63 Removable = false;
64 Is_Present = false;
65 Length = 0;
66 Size = 0;
67 Used = 0;
68 Free = 0;
69 Backup_Size = 0;
70 Can_Be_Encrypted = false;
71 Is_Encrypted = false;
72 Is_Decrypted = false;
73 Decrypted_Block_Device = "";
74 Display_Name = "";
75 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040076 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040077 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040078 Backup_Method = NONE;
79 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040080 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040081 Is_Storage = false;
82 Storage_Path = "";
83 Current_File_System = "";
84 Fstab_File_System = "";
85 Format_Block_Size = 0;
86}
87
88TWPartition::~TWPartition(void) {
89 // Do nothing
90}
91
Dees_Troy5bf43922012-09-07 16:07:55 -040092bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
93 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
94 int line_len = Line.size(), index = 0, item_index = 0;
95 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -040096 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -040097
98 strncpy(full_line, Line.c_str(), line_len);
99
Dees_Troy51127312012-09-08 13:08:49 -0400100 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 if (full_line[index] <= 32)
102 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400103 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400104 Mount_Point = full_line;
105 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400106 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400107 index = Mount_Point.size();
108 while (index < line_len) {
109 while (index < line_len && full_line[index] == '\0')
110 index++;
111 if (index >= line_len)
112 continue;
113 ptr = full_line + index;
114 if (item_index == 0) {
115 // File System
116 Fstab_File_System = ptr;
117 Current_File_System = ptr;
118 item_index++;
119 } else if (item_index == 1) {
120 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400121 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400122 MTD_Name = ptr;
123 Find_MTD_Block_Device(MTD_Name);
Dees_Troy38bd7602012-09-14 13:33:53 -0400124 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400125 if (Display_Error)
126 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
127 else
128 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
129 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400130 } else {
131 Primary_Block_Device = ptr;
132 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400133 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400134 item_index++;
135 } else if (item_index > 1) {
136 if (*ptr == '/') {
137 // Alternate Block Device
138 Alternate_Block_Device = ptr;
139 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
140 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
141 // Partition length
142 ptr += 7;
143 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400144 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
145 // Custom flags, save for later so that new values aren't overwritten by defaults
146 ptr += 6;
147 Flags = ptr;
Dees_Troy38bd7602012-09-14 13:33:53 -0400148 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
149 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400150 } else {
151 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400152 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400153 }
154 }
155 while (index < line_len && full_line[index] != '\0')
156 index++;
157 }
158
159 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
160 if (Display_Error)
161 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
162 else
163 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
164 return 0;
165 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400166 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 Setup_File_System(Display_Error);
168 if (Mount_Point == "/system") {
169 Display_Name = "System";
170 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400171 } else if (Mount_Point == "/data") {
172 Display_Name = "Data";
173 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400174 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400175#ifdef RECOVERY_SDCARD_ON_DATA
176 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400177 Is_Storage = true;
178 Storage_Path = "/data/media";
Dees_Troyb46a6842012-09-25 11:06:46 -0400179 Recreate_Media_Folder();
Dees_Troy657c3092012-09-10 20:32:10 -0400180 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
181 Make_Dir("/emmc", Display_Error);
182 Symlink_Path = "/data/media";
183 Symlink_Mount_Point = "/emmc";
184 } else {
185 Make_Dir("/sdcard", Display_Error);
186 Symlink_Path = "/data/media";
187 Symlink_Mount_Point = "/sdcard";
188 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400189#endif
190#ifdef TW_INCLUDE_CRYPTO
191 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400192 char crypto_blkdev[255];
193 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
194 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400195 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400196 DataManager::SetValue(TW_IS_DECRYPTED, 1);
197 Is_Encrypted = true;
198 Is_Decrypted = true;
199 Decrypted_Block_Device = crypto_blkdev;
200 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
201 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400202 Is_Encrypted = true;
203 Is_Decrypted = false;
204 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
205 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
206 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400207 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400208#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400209 } else if (Mount_Point == "/cache") {
210 Display_Name = "Cache";
211 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400212 Wipe_During_Factory_Reset = true;
Dees_Troyb46a6842012-09-25 11:06:46 -0400213 if (!TWFunc::Path_Exists("/cache/recovery")) {
214 LOGI("Recreating /cache/recovery folder.\n");
215 TWFunc::Recursive_Mkdir("/cache/recovery");
216 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400218 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 Display_Name = "DataData";
220 Is_SubPartition = true;
221 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 DataManager::SetValue(TW_HAS_DATADATA, 1);
223 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400224 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 Display_Name = "SD-Ext";
226 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400227 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400228 }
229#ifdef TW_EXTERNAL_STORAGE_PATH
230 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
231 Is_Storage = true;
232 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400233 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400234 }
235#else
236 if (Mount_Point == "/sdcard") {
237 Is_Storage = true;
238 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400239 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400240#ifndef RECOVERY_SDCARD_ON_DATA
241 Setup_AndSec();
242#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400243 }
244#endif
245#ifdef TW_INTERNAL_STORAGE_PATH
246 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
247 Is_Storage = true;
248 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400249#ifndef RECOVERY_SDCARD_ON_DATA
250 Setup_AndSec();
251#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400252 }
253#else
254 if (Mount_Point == "/emmc") {
255 Is_Storage = true;
256 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400257#ifndef RECOVERY_SDCARD_ON_DATA
258 Setup_AndSec();
259#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400260 }
261#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400263 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 Setup_Image(Display_Error);
265 if (Mount_Point == "/boot") {
266 int backup_display_size = (int)(Backup_Size / 1048576LLU);
267 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
268 if (Backup_Size == 0) {
269 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
270 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
271 } else
272 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
273 } else if (Mount_Point == "/recovery") {
274 int backup_display_size = (int)(Backup_Size / 1048576LLU);
275 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
276 if (Backup_Size == 0) {
277 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
278 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
279 } else
280 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
281 }
Dees_Troyb1dab8d2012-09-26 12:16:41 -0400282#ifdef SP1_NAME
283 string SP1_Path = "/";
284 SP1_Path += EXPAND(SP1_NAME);
285 if (Mount_Point == SP1_Path) {
286 int backup_display_size = (int)(Backup_Size / 1048576LLU);
287 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
288 }
289#endif
290#ifdef SP2_NAME
291 string SP2_Path = "/";
292 SP2_Path += EXPAND(SP2_NAME);
293 if (Mount_Point == SP2_Path) {
294 int backup_display_size = (int)(Backup_Size / 1048576LLU);
295 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
296 }
297#endif
298#ifdef SP3_NAME
299 string SP3_Path = "/";
300 SP3_Path += EXPAND(SP3_NAME);
301 if (Mount_Point == SP3_Path) {
302 int backup_display_size = (int)(Backup_Size / 1048576LLU);
303 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
304 }
305#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400306 }
307
Dees_Troy51127312012-09-08 13:08:49 -0400308 // Process any custom flags
309 if (Flags.size() > 0)
310 Process_Flags(Flags, Display_Error);
311
312 return true;
313}
314
315bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
316 char flags[MAX_FSTAB_LINE_LENGTH];
317 int flags_len, index = 0;
318 char* ptr;
319
320 strcpy(flags, Flags.c_str());
321 flags_len = Flags.size();
322 for (index = 0; index < flags_len; index++) {
323 if (flags[index] == ';')
324 flags[index] = '\0';
325 }
326
327 index = 0;
328 while (index < flags_len) {
329 while (index < flags_len && flags[index] == '\0')
330 index++;
331 if (index >= flags_len)
332 continue;
333 ptr = flags + index;
334 if (strcmp(ptr, "removable") == 0) {
335 Removable = true;
336 } else if (strcmp(ptr, "storage") == 0) {
337 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400338 } else if (strcmp(ptr, "canbewiped") == 0) {
339 Can_Be_Wiped = true;
340 } else if (strcmp(ptr, "wipeingui") == 0) {
341 Can_Be_Wiped = true;
342 Wipe_Available_in_GUI = true;
343 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
344 Can_Be_Wiped = true;
345 Wipe_Available_in_GUI = true;
346 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400347 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
348 ptr += 13;
349 Is_SubPartition = true;
350 SubPartition_Of = ptr;
351 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
352 ptr += 8;
353 Symlink_Path = ptr;
354 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
355 ptr += 8;
356 Display_Name = ptr;
357 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
358 ptr += 10;
359 Format_Block_Size = atoi(ptr);
360 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
361 ptr += 7;
362 Length = atoi(ptr);
363 } else {
364 if (Display_Error)
365 LOGE("Unhandled flag: '%s'\n", ptr);
366 else
367 LOGI("Unhandled flag: '%s'\n", ptr);
368 }
369 while (index < flags_len && flags[index] != '\0')
370 index++;
371 }
372 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400373}
374
Dees_Troy5bf43922012-09-07 16:07:55 -0400375bool TWPartition::Is_File_System(string File_System) {
376 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400377 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400378 File_System == "ext4" ||
379 File_System == "vfat" ||
380 File_System == "ntfs" ||
381 File_System == "yaffs2" ||
382 File_System == "auto")
383 return true;
384 else
385 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400386}
387
Dees_Troy5bf43922012-09-07 16:07:55 -0400388bool TWPartition::Is_Image(string File_System) {
389 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400390 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400391 return true;
392 else
393 return false;
394}
395
Dees_Troy51127312012-09-08 13:08:49 -0400396bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400397 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400398 if (mkdir(Path.c_str(), 0777) == -1) {
399 if (Display_Error)
400 LOGE("Can not create '%s' folder.\n", Path.c_str());
401 else
402 LOGI("Can not create '%s' folder.\n", Path.c_str());
403 return false;
404 } else {
405 LOGI("Created '%s' folder.\n", Path.c_str());
406 return true;
407 }
408 }
409 return true;
410}
411
Dees_Troy5bf43922012-09-07 16:07:55 -0400412void TWPartition::Setup_File_System(bool Display_Error) {
413 struct statfs st;
414
415 Can_Be_Mounted = true;
416 Can_Be_Wiped = true;
417
Dees_Troy5bf43922012-09-07 16:07:55 -0400418 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400419 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400420 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
421 Backup_Name = Display_Name;
422 Backup_Method = FILES;
423}
424
425void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400426 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
427 Backup_Name = Display_Name;
428 if (Fstab_File_System == "emmc")
429 Backup_Method = DD;
430 else if (Fstab_File_System == "mtd")
431 Backup_Method = FLASH_UTILS;
432 else
433 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
434 if (Find_Partition_Size()) {
435 Used = Size;
436 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400437 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400438 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400439 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400441 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 }
443}
444
Dees_Troye58d5262012-09-21 12:27:57 -0400445void TWPartition::Setup_AndSec(void) {
446 Backup_Name = "and-sec";
447 Has_Android_Secure = true;
448 Symlink_Path = Mount_Point + "/.android_secure";
449 Symlink_Mount_Point = "/and-sec";
450 Backup_Path = Symlink_Mount_Point;
451 Make_Dir("/and-sec", true);
452 Recreate_AndSec_Folder();
453}
454
Dees_Troy5bf43922012-09-07 16:07:55 -0400455void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
456 char device[512], realDevice[512];
457
458 strcpy(device, Block.c_str());
459 memset(realDevice, 0, sizeof(realDevice));
460 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
461 {
462 strcpy(device, realDevice);
463 memset(realDevice, 0, sizeof(realDevice));
464 }
465
466 if (device[0] != '/') {
467 if (Display_Error)
468 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
469 else
470 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
471 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400472 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400473 Block = device;
474 return;
475 }
476}
477
Dees_Troy38bd7602012-09-14 13:33:53 -0400478bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
479 FILE *fp = NULL;
480 char line[255];
481
482 fp = fopen("/proc/mtd", "rt");
483 if (fp == NULL) {
484 LOGE("Device does not support /proc/mtd\n");
485 return false;
486 }
487
488 while (fgets(line, sizeof(line), fp) != NULL)
489 {
490 char device[32], label[32];
491 unsigned long size = 0;
492 char* fstype = NULL;
493 int deviceId;
494
495 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
496
497 // Skip header and blank lines
498 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
499 continue;
500
501 // Strip off the trailing " from the label
502 label[strlen(label)-1] = '\0';
503
504 if (strcmp(label, MTD_Name.c_str()) == 0) {
505 // We found our device
506 // Strip off the trailing : from the device
507 device[strlen(device)-1] = '\0';
508 if (sscanf(device,"mtd%d", &deviceId) == 1) {
509 sprintf(device, "/dev/block/mtdblock%d", deviceId);
510 Primary_Block_Device = device;
511 }
512 }
513 }
514 fclose(fp);
515
516 return false;
517}
518
Dees_Troy51127312012-09-08 13:08:49 -0400519bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
520 struct statfs st;
521 string Local_Path = Mount_Point + "/.";
522
523 if (!Mount(Display_Error))
524 return false;
525
526 if (statfs(Local_Path.c_str(), &st) != 0) {
527 if (!Removable) {
528 if (Display_Error)
529 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
530 else
531 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
532 }
533 return false;
534 }
535 Size = (st.f_blocks * st.f_bsize);
536 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
537 Free = (st.f_bfree * st.f_bsize);
538 Backup_Size = Used;
539 return true;
540}
541
542bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400543 FILE* fp;
544 char command[255], line[512];
545 int include_block = 1;
546 unsigned int min_len;
547
548 if (!Mount(Display_Error))
549 return false;
550
Dees_Troy38bd7602012-09-14 13:33:53 -0400551 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400552 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400553 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400554 fp = fopen("/tmp/dfoutput.txt", "rt");
555 if (fp == NULL) {
556 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400557 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400558 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400559
560 while (fgets(line, sizeof(line), fp) != NULL)
561 {
562 unsigned long blocks, used, available;
563 char device[64];
564 char tmpString[64];
565
566 if (strncmp(line, "Filesystem", 10) == 0)
567 continue;
568 if (strlen(line) < min_len) {
569 include_block = 0;
570 continue;
571 }
572 if (include_block) {
573 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
574 } else {
575 // The device block string is so long that the df information is on the next line
576 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400577 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400578 while (tmpString[space_count] == 32)
579 space_count++;
580 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
581 }
582
583 // Adjust block size to byte size
584 Size = blocks * 1024ULL;
585 Used = used * 1024ULL;
586 Free = available * 1024ULL;
587 Backup_Size = Used;
588 }
589 fclose(fp);
590 return true;
591}
592
Dees_Troy5bf43922012-09-07 16:07:55 -0400593bool TWPartition::Find_Partition_Size(void) {
594 FILE* fp;
595 char line[512];
596 string tmpdevice;
597
598 // In this case, we'll first get the partitions we care about (with labels)
599 fp = fopen("/proc/partitions", "rt");
600 if (fp == NULL)
601 return false;
602
603 while (fgets(line, sizeof(line), fp) != NULL)
604 {
605 unsigned long major, minor, blocks;
606 char device[512];
607 char tmpString[64];
608
Dees_Troy63c8df72012-09-10 14:02:05 -0400609 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400610 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
611
612 tmpdevice = "/dev/block/";
613 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400614 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400615 // Adjust block size to byte size
616 Size = blocks * 1024ULL;
617 fclose(fp);
618 return true;
619 }
620 }
621 fclose(fp);
622 return false;
623}
624
Dees_Troy5bf43922012-09-07 16:07:55 -0400625void TWPartition::Flip_Block_Device(void) {
626 string temp;
627
628 temp = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400629 Primary_Block_Device = Alternate_Block_Device;
Dees_Troy5bf43922012-09-07 16:07:55 -0400630 Alternate_Block_Device = temp;
631}
632
633bool TWPartition::Is_Mounted(void) {
634 if (!Can_Be_Mounted)
635 return false;
636
637 struct stat st1, st2;
638 string test_path;
639
640 // Check to see if the mount point directory exists
641 test_path = Mount_Point + "/.";
642 if (stat(test_path.c_str(), &st1) != 0) return false;
643
644 // Check to see if the directory above the mount point exists
645 test_path = Mount_Point + "/../.";
646 if (stat(test_path.c_str(), &st2) != 0) return false;
647
648 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
649 int ret = (st1.st_dev != st2.st_dev) ? true : false;
650
651 return ret;
652}
653
654bool TWPartition::Mount(bool Display_Error) {
655 if (Is_Mounted()) {
656 return true;
657 } else if (!Can_Be_Mounted) {
658 return false;
659 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400660
661 Find_Actual_Block_Device();
662
663 // Check the current file system before mounting
664 Check_FS_Type();
665
666 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
667 if (Display_Error)
668 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
669 else
670 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
671 return false;
672 } else {
673 if (Removable)
674 Update_Size(Display_Error);
675
676 if (!Symlink_Mount_Point.empty()) {
677 string Command;
678
679 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400680 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400681 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400682 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400683 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400684 return true;
685}
686
687bool TWPartition::UnMount(bool Display_Error) {
688 if (Is_Mounted()) {
689 int never_unmount_system;
690
691 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
692 if (never_unmount_system == 1 && Mount_Point == "/system")
693 return true; // Never unmount system if you're not supposed to unmount it
694
Dees_Troy38bd7602012-09-14 13:33:53 -0400695 if (!Symlink_Mount_Point.empty())
696 umount(Symlink_Mount_Point.c_str());
697
Dees_Troy5bf43922012-09-07 16:07:55 -0400698 if (umount(Mount_Point.c_str()) != 0) {
699 if (Display_Error)
700 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
701 else
702 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
703 return false;
704 } else
705 return true;
706 } else {
707 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400708 }
709}
710
711bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400712 if (!Can_Be_Wiped) {
713 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
714 return false;
715 }
716
Dees_Troyc51f1f92012-09-20 15:32:13 -0400717 if (Mount_Point == "/cache")
718 tmplog_offset = 0;
719
Dees_Troy38bd7602012-09-14 13:33:53 -0400720 if (Has_Data_Media)
721 return Wipe_Data_Without_Wiping_Media();
722
723 int check;
724 DataManager::GetValue(TW_RM_RF_VAR, check);
725 if (check)
726 return Wipe_RMRF();
727
728 if (Current_File_System == "ext4")
729 return Wipe_EXT4();
730
731 if (Current_File_System == "ext2" || Current_File_System == "ext3")
732 return Wipe_EXT23();
733
734 if (Current_File_System == "vfat")
735 return Wipe_FAT();
736
737 if (Current_File_System == "yaffs2")
738 return Wipe_MTD();
739
740 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
741 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400742}
743
Dees_Troye58d5262012-09-21 12:27:57 -0400744bool TWPartition::Wipe_AndSec(void) {
745 if (!Has_Android_Secure)
746 return false;
747
748 char cmd[512];
749
750 if (!Mount(true))
751 return false;
752
753 ui_print("Using rm -rf on .android_secure\n");
754 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
755
756 LOGI("rm -rf command is: '%s'\n", cmd);
757 system(cmd);
758 return true;
759}
760
Dees_Troy51a0e822012-09-05 15:24:24 -0400761bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400762 if (Backup_Method == FILES)
763 return Backup_Tar(backup_folder);
764 else if (Backup_Method == DD)
765 return Backup_DD(backup_folder);
766 else if (Backup_Method == FLASH_UTILS)
767 return Backup_Dump_Image(backup_folder);
768 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
769 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400770}
771
Dees_Troy43d8b002012-09-17 16:00:01 -0400772bool TWPartition::Check_MD5(string restore_folder) {
773 string Full_Filename;
774 char split_filename[512];
775 int index = 0;
776
777 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400778 if (!TWFunc::Path_Exists(Full_Filename)) {
779 // This is a split archive, we presume
780 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
781 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
782 if (TWFunc::Check_MD5(split_filename) == 0) {
783 LOGE("MD5 failed to match on '%s'.\n", split_filename);
784 return false;
785 }
786 index++;
787 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400788 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400789 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400790 } else {
791 // Single file archive
792 if (TWFunc::Check_MD5(Full_Filename) == 0) {
793 LOGE("MD5 failed to match on '%s'.\n", split_filename);
794 return false;
795 } else
796 return true;
797 }
798 return false;
799}
800
Dees_Troy51a0e822012-09-05 15:24:24 -0400801bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400802 if (Backup_Method == FILES)
803 return Restore_Tar(restore_folder);
804 else if (Backup_Method == DD)
805 return Restore_DD(restore_folder);
806 else if (Backup_Method == FLASH_UTILS)
807 return Restore_Flash_Image(restore_folder);
808 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
809 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400810}
811
812string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400813 if (Backup_Method == NONE)
814 return "none";
815 else if (Backup_Method == FILES)
816 return "files";
817 else if (Backup_Method == DD)
818 return "dd";
819 else if (Backup_Method == FLASH_UTILS)
820 return "flash_utils";
821 else
822 return "undefined";
823 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400824}
825
826bool TWPartition::Decrypt(string Password) {
827 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400828 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400829 return 1;
830}
831
832bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400833 bool Save_Data_Media = Has_Data_Media;
834
835 if (!UnMount(true))
836 return false;
837
838 Current_File_System = Fstab_File_System;
839 Is_Encrypted = false;
840 Is_Decrypted = false;
841 Decrypted_Block_Device = "";
842 Has_Data_Media = false;
843 if (Wipe()) {
844 Has_Data_Media = Save_Data_Media;
845 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
846 Recreate_Media_Folder();
847 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400848 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400849 return true;
850 } else {
851 Has_Data_Media = Save_Data_Media;
852 LOGE("Unable to format to remove encryption.\n");
853 return false;
854 }
855 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400856}
857
858void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400859 FILE *fp;
860 string blkCommand;
861 char blkOutput[255];
862 char* blk;
863 char* arg;
864 char* ptr;
865
866 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
867 return; // Running blkid on some mtd devices causes a massive crash
868
Dees_Troy38bd7602012-09-14 13:33:53 -0400869 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400870 if (!Is_Present)
871 return;
Dees_Troy51127312012-09-08 13:08:49 -0400872
Dees_Troy8170a922012-09-18 15:40:25 -0400873 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
874 system("rm /tmp/blkidoutput.txt");
875
876 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400877 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400878 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400879 if (fp == NULL)
880 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400881 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
882 {
883 blk = blkOutput;
884 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400885 while (*ptr > 32 && *ptr != ':') ptr++;
886 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400887 *ptr = 0;
888
889 // Increment by two, but verify that we don't hit a NULL
890 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400891 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400892
893 // Now, find the TYPE field
894 while (1)
895 {
896 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400897 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400898 if (*ptr != 0)
899 {
900 *ptr = 0;
901 ptr++;
902 }
903
904 if (strlen(arg) > 6)
905 {
906 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
907 }
908
909 if (*ptr == 0)
910 {
911 arg = NULL;
912 break;
913 }
914 }
915
916 if (arg && strlen(arg) > 7)
917 {
918 arg += 6; // Skip the TYPE=" portion
919 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
920 }
921 else
922 continue;
923
Dees_Troy63c8df72012-09-10 14:02:05 -0400924 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400925 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
926 Current_File_System = arg;
927 }
928 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400929 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400930 return;
931}
932
933bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400934 if (!UnMount(true))
935 return false;
936
Dees_Troy43d8b002012-09-17 16:00:01 -0400937 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400938 char command[512];
939
940 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
941 Find_Actual_Block_Device();
942 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
943 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400944 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400945 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400946 ui_print("Done.\n");
947 return true;
948 } else {
949 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
950 return false;
951 }
952 } else
953 return Wipe_RMRF();
954
955 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400956}
957
958bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400959 if (!UnMount(true))
960 return false;
961
Dees_Troy43d8b002012-09-17 16:00:01 -0400962 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400963 string Command;
964
965 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
966 Find_Actual_Block_Device();
967 Command = "make_ext4fs";
968 if (!Is_Decrypted && Length != 0) {
969 // Only use length if we're not decrypted
970 char len[32];
971 sprintf(len, "%i", Length);
972 Command += " -l ";
973 Command += len;
974 }
975 Command += " " + Actual_Block_Device;
976 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400977 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400978 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400979 ui_print("Done.\n");
980 return true;
981 } else {
982 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
983 return false;
984 }
985 } else
986 return Wipe_EXT23();
987
988 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400989}
990
991bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400992 char command[512];
993
Dees_Troy43d8b002012-09-17 16:00:01 -0400994 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400995 if (!UnMount(true))
996 return false;
997
998 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
999 Find_Actual_Block_Device();
1000 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -04001001 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001002 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001003 ui_print("Done.\n");
1004 return true;
1005 } else {
1006 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1007 return false;
1008 }
1009 return true;
1010 }
1011 else
1012 return Wipe_RMRF();
1013
1014 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001015}
1016
Dees_Troy38bd7602012-09-14 13:33:53 -04001017bool TWPartition::Wipe_MTD() {
1018 if (!UnMount(true))
1019 return false;
1020
1021 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1022
1023 mtd_scan_partitions();
1024 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1025 if (mtd == NULL) {
1026 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1027 return false;
1028 }
1029
1030 MtdWriteContext* ctx = mtd_write_partition(mtd);
1031 if (ctx == NULL) {
1032 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1033 return false;
1034 }
1035 if (mtd_erase_blocks(ctx, -1) == -1) {
1036 mtd_write_close(ctx);
1037 LOGE("Failed to format '%s'", MTD_Name.c_str());
1038 return false;
1039 }
1040 if (mtd_write_close(ctx) != 0) {
1041 LOGE("Failed to close '%s'", MTD_Name.c_str());
1042 return false;
1043 }
Dees_Troye58d5262012-09-21 12:27:57 -04001044 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001045 ui_print("Done.\n");
1046 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001047}
1048
1049bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001050 char cmd[512];
1051
1052 if (!Mount(true))
1053 return false;
1054
Dees_Troye58d5262012-09-21 12:27:57 -04001055 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1056 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001057
1058 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001059 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001060 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001061 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001062}
1063
1064bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001065 char cmd[256];
1066
1067 // This handles wiping data on devices with "sdcard" in /data/media
1068 if (!Mount(true))
1069 return false;
1070
1071 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001072 system("rm -f /data/*");
1073 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001074
1075 DIR* d;
1076 d = opendir("/data");
1077 if (d != NULL)
1078 {
1079 struct dirent* de;
1080 while ((de = readdir(d)) != NULL) {
1081 if (strcmp(de->d_name, "media") == 0) continue;
1082
1083 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001084 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001085 }
1086 closedir(d);
1087 }
1088 ui_print("Done.\n");
1089 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001090}
1091
1092bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001093 char back_name[255], split_index[5];
1094 string Full_FileName, Split_FileName, Tar_Args, Command;
1095 int use_compression, index, backup_count;
1096 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001097 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001098
1099 if (!Mount(true))
1100 return false;
1101
Dees_Troyb46a6842012-09-25 11:06:46 -04001102 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001103 ui_print("Backing up %s...\n", Display_Name.c_str());
1104
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 Flip_Block_Device();
1346 Actual_Block_Device = Primary_Block_Device;
1347 Is_Present = true;
1348 } else
1349 Is_Present = false;
1350}
1351
1352void TWPartition::Recreate_Media_Folder(void) {
1353 string Command;
1354
1355 if (!Mount(true)) {
1356 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001357 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001358 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001359 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001360 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001361 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001362 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001363 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001364 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001365}
Dees_Troye58d5262012-09-21 12:27:57 -04001366
1367void TWPartition::Recreate_AndSec_Folder(void) {
1368 string Command;
1369
1370 if (!Has_Android_Secure)
1371 return;
1372
1373 if (!Mount(true)) {
1374 LOGE("Unable to recreate android secure folder.\n");
1375 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1376 LOGI("Recreating android secure folder.\n");
1377 TWFunc::Recursive_Mkdir(Symlink_Path);
1378 Command = "umount " + Symlink_Mount_Point;
1379 system(Command.c_str());
1380 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1381 system(Command.c_str());
1382 }
1383}