blob: 505e310c5497934c800e6d858df01799f0a4891e [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition class for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040028#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040029#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040030#include <dirent.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040031
Dees_Troy657c3092012-09-10 20:32:10 -040032#ifdef TW_INCLUDE_CRYPTO
33 #include "cutils/properties.h"
34#endif
35
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
37#include "common.h"
38#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
Dees_Troy9df963c2012-09-26 08:58:12 -040041#include "makelist.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040043 #include "mtdutils/mtdutils.h"
44 #include "mtdutils/mounts.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040045}
Dees_Troy51a0e822012-09-05 15:24:24 -040046
47TWPartition::TWPartition(void) {
48 Can_Be_Mounted = false;
49 Can_Be_Wiped = false;
50 Wipe_During_Factory_Reset = false;
51 Wipe_Available_in_GUI = false;
52 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040053 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040054 SubPartition_Of = "";
55 Symlink_Path = "";
56 Symlink_Mount_Point = "";
57 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040058 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040059 Actual_Block_Device = "";
60 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040061 Alternate_Block_Device = "";
62 Removable = false;
63 Is_Present = false;
64 Length = 0;
65 Size = 0;
66 Used = 0;
67 Free = 0;
68 Backup_Size = 0;
69 Can_Be_Encrypted = false;
70 Is_Encrypted = false;
71 Is_Decrypted = false;
72 Decrypted_Block_Device = "";
73 Display_Name = "";
74 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040075 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040076 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040077 Backup_Method = NONE;
78 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040079 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040080 Is_Storage = false;
81 Storage_Path = "";
82 Current_File_System = "";
83 Fstab_File_System = "";
84 Format_Block_Size = 0;
85}
86
87TWPartition::~TWPartition(void) {
88 // Do nothing
89}
90
Dees_Troy5bf43922012-09-07 16:07:55 -040091bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
92 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
93 int line_len = Line.size(), index = 0, item_index = 0;
94 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -040095 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -040096
97 strncpy(full_line, Line.c_str(), line_len);
98
Dees_Troy51127312012-09-08 13:08:49 -040099 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400100 if (full_line[index] <= 32)
101 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400102 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400103 Mount_Point = full_line;
104 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400105 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400106 index = Mount_Point.size();
107 while (index < line_len) {
108 while (index < line_len && full_line[index] == '\0')
109 index++;
110 if (index >= line_len)
111 continue;
112 ptr = full_line + index;
113 if (item_index == 0) {
114 // File System
115 Fstab_File_System = ptr;
116 Current_File_System = ptr;
117 item_index++;
118 } else if (item_index == 1) {
119 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400120 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400121 MTD_Name = ptr;
122 Find_MTD_Block_Device(MTD_Name);
Dees_Troy38bd7602012-09-14 13:33:53 -0400123 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400124 if (Display_Error)
125 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
126 else
127 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
128 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400129 } else {
130 Primary_Block_Device = ptr;
131 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400133 item_index++;
134 } else if (item_index > 1) {
135 if (*ptr == '/') {
136 // Alternate Block Device
137 Alternate_Block_Device = ptr;
138 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
139 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
140 // Partition length
141 ptr += 7;
142 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400143 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
144 // Custom flags, save for later so that new values aren't overwritten by defaults
145 ptr += 6;
146 Flags = ptr;
Dees_Troy38bd7602012-09-14 13:33:53 -0400147 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
148 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400149 } else {
150 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400151 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400152 }
153 }
154 while (index < line_len && full_line[index] != '\0')
155 index++;
156 }
157
158 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
159 if (Display_Error)
160 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
161 else
162 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
163 return 0;
164 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400165 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 Setup_File_System(Display_Error);
167 if (Mount_Point == "/system") {
168 Display_Name = "System";
169 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400170 } else if (Mount_Point == "/data") {
171 Display_Name = "Data";
172 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400173 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400174#ifdef RECOVERY_SDCARD_ON_DATA
175 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400176 Is_Storage = true;
177 Storage_Path = "/data/media";
Dees_Troy657c3092012-09-10 20:32:10 -0400178 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
179 Make_Dir("/emmc", Display_Error);
180 Symlink_Path = "/data/media";
181 Symlink_Mount_Point = "/emmc";
182 } else {
183 Make_Dir("/sdcard", Display_Error);
184 Symlink_Path = "/data/media";
185 Symlink_Mount_Point = "/sdcard";
186 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400187#endif
188#ifdef TW_INCLUDE_CRYPTO
189 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400190 char crypto_blkdev[255];
191 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
192 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400193 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400194 DataManager::SetValue(TW_IS_DECRYPTED, 1);
195 Is_Encrypted = true;
196 Is_Decrypted = true;
197 Decrypted_Block_Device = crypto_blkdev;
198 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
199 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400200 Is_Encrypted = true;
201 Is_Decrypted = false;
202 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
203 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
204 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400205 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400206 #ifdef RECOVERY_SDCARD_ON_DATA
207 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
208 Recreate_Media_Folder();
209 #endif
210#else
211 #ifdef RECOVERY_SDCARD_ON_DATA
212 Recreate_Media_Folder();
213 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400214#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 } else if (Mount_Point == "/cache") {
216 Display_Name = "Cache";
217 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400218 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400219 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
220 string Recreate_Command = "cd /cache && mkdir recovery";
Dees_Troyb46a6842012-09-25 11:06:46 -0400221 LOGI("Recreating /cache/recovery folder.\n");
Dees_Troyce2fe772012-09-28 12:34:33 -0400222 system(Recreate_Command.c_str());
Dees_Troyb46a6842012-09-25 11:06:46 -0400223 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400225 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400226 Display_Name = "DataData";
227 Is_SubPartition = true;
228 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 DataManager::SetValue(TW_HAS_DATADATA, 1);
230 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400231 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400232 Display_Name = "SD-Ext";
233 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400234 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400235 } else if (Mount_Point == "/boot") {
236 Display_Name = "Boot";
237 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400238 }
239#ifdef TW_EXTERNAL_STORAGE_PATH
240 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
241 Is_Storage = true;
242 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400243 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400244 }
245#else
246 if (Mount_Point == "/sdcard") {
247 Is_Storage = true;
248 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400249 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400250#ifndef RECOVERY_SDCARD_ON_DATA
251 Setup_AndSec();
252#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400253 }
254#endif
255#ifdef TW_INTERNAL_STORAGE_PATH
256 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
257 Is_Storage = true;
258 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400259#ifndef RECOVERY_SDCARD_ON_DATA
260 Setup_AndSec();
261#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400262 }
263#else
264 if (Mount_Point == "/emmc") {
265 Is_Storage = true;
266 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400267#ifndef RECOVERY_SDCARD_ON_DATA
268 Setup_AndSec();
269#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400270 }
271#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400272 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400273 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400274 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400275 }
276
Dees_Troy51127312012-09-08 13:08:49 -0400277 // Process any custom flags
278 if (Flags.size() > 0)
279 Process_Flags(Flags, Display_Error);
280
281 return true;
282}
283
284bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
285 char flags[MAX_FSTAB_LINE_LENGTH];
286 int flags_len, index = 0;
287 char* ptr;
288
289 strcpy(flags, Flags.c_str());
290 flags_len = Flags.size();
291 for (index = 0; index < flags_len; index++) {
292 if (flags[index] == ';')
293 flags[index] = '\0';
294 }
295
296 index = 0;
297 while (index < flags_len) {
298 while (index < flags_len && flags[index] == '\0')
299 index++;
300 if (index >= flags_len)
301 continue;
302 ptr = flags + index;
303 if (strcmp(ptr, "removable") == 0) {
304 Removable = true;
305 } else if (strcmp(ptr, "storage") == 0) {
306 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400307 } else if (strcmp(ptr, "canbewiped") == 0) {
308 Can_Be_Wiped = true;
309 } else if (strcmp(ptr, "wipeingui") == 0) {
310 Can_Be_Wiped = true;
311 Wipe_Available_in_GUI = true;
312 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
313 Can_Be_Wiped = true;
314 Wipe_Available_in_GUI = true;
315 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400316 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
317 ptr += 13;
318 Is_SubPartition = true;
319 SubPartition_Of = ptr;
320 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
321 ptr += 8;
322 Symlink_Path = ptr;
323 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
324 ptr += 8;
325 Display_Name = ptr;
326 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
327 ptr += 10;
328 Format_Block_Size = atoi(ptr);
329 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
330 ptr += 7;
331 Length = atoi(ptr);
332 } else {
333 if (Display_Error)
334 LOGE("Unhandled flag: '%s'\n", ptr);
335 else
336 LOGI("Unhandled flag: '%s'\n", ptr);
337 }
338 while (index < flags_len && flags[index] != '\0')
339 index++;
340 }
341 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400342}
343
Dees_Troy5bf43922012-09-07 16:07:55 -0400344bool TWPartition::Is_File_System(string File_System) {
345 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400346 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400347 File_System == "ext4" ||
348 File_System == "vfat" ||
349 File_System == "ntfs" ||
350 File_System == "yaffs2" ||
351 File_System == "auto")
352 return true;
353 else
354 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400355}
356
Dees_Troy5bf43922012-09-07 16:07:55 -0400357bool TWPartition::Is_Image(string File_System) {
358 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400359 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400360 return true;
361 else
362 return false;
363}
364
Dees_Troy51127312012-09-08 13:08:49 -0400365bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400366 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400367 if (mkdir(Path.c_str(), 0777) == -1) {
368 if (Display_Error)
369 LOGE("Can not create '%s' folder.\n", Path.c_str());
370 else
371 LOGI("Can not create '%s' folder.\n", Path.c_str());
372 return false;
373 } else {
374 LOGI("Created '%s' folder.\n", Path.c_str());
375 return true;
376 }
377 }
378 return true;
379}
380
Dees_Troy5bf43922012-09-07 16:07:55 -0400381void TWPartition::Setup_File_System(bool Display_Error) {
382 struct statfs st;
383
384 Can_Be_Mounted = true;
385 Can_Be_Wiped = true;
386
Dees_Troy5bf43922012-09-07 16:07:55 -0400387 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400388 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400389 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
390 Backup_Name = Display_Name;
391 Backup_Method = FILES;
392}
393
394void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400395 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
396 Backup_Name = Display_Name;
397 if (Fstab_File_System == "emmc")
398 Backup_Method = DD;
399 else if (Fstab_File_System == "mtd")
400 Backup_Method = FLASH_UTILS;
401 else
402 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
403 if (Find_Partition_Size()) {
404 Used = Size;
405 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400406 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400407 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400408 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400409 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400410 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400411 }
412}
413
Dees_Troye58d5262012-09-21 12:27:57 -0400414void TWPartition::Setup_AndSec(void) {
415 Backup_Name = "and-sec";
416 Has_Android_Secure = true;
417 Symlink_Path = Mount_Point + "/.android_secure";
418 Symlink_Mount_Point = "/and-sec";
419 Backup_Path = Symlink_Mount_Point;
420 Make_Dir("/and-sec", true);
421 Recreate_AndSec_Folder();
422}
423
Dees_Troy5bf43922012-09-07 16:07:55 -0400424void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
425 char device[512], realDevice[512];
426
427 strcpy(device, Block.c_str());
428 memset(realDevice, 0, sizeof(realDevice));
429 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
430 {
431 strcpy(device, realDevice);
432 memset(realDevice, 0, sizeof(realDevice));
433 }
434
435 if (device[0] != '/') {
436 if (Display_Error)
437 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
438 else
439 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
440 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400441 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400442 Block = device;
443 return;
444 }
445}
446
Dees_Troy38bd7602012-09-14 13:33:53 -0400447bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
448 FILE *fp = NULL;
449 char line[255];
450
451 fp = fopen("/proc/mtd", "rt");
452 if (fp == NULL) {
453 LOGE("Device does not support /proc/mtd\n");
454 return false;
455 }
456
457 while (fgets(line, sizeof(line), fp) != NULL)
458 {
459 char device[32], label[32];
460 unsigned long size = 0;
461 char* fstype = NULL;
462 int deviceId;
463
464 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
465
466 // Skip header and blank lines
467 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
468 continue;
469
470 // Strip off the trailing " from the label
471 label[strlen(label)-1] = '\0';
472
473 if (strcmp(label, MTD_Name.c_str()) == 0) {
474 // We found our device
475 // Strip off the trailing : from the device
476 device[strlen(device)-1] = '\0';
477 if (sscanf(device,"mtd%d", &deviceId) == 1) {
478 sprintf(device, "/dev/block/mtdblock%d", deviceId);
479 Primary_Block_Device = device;
480 }
481 }
482 }
483 fclose(fp);
484
485 return false;
486}
487
Dees_Troy51127312012-09-08 13:08:49 -0400488bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
489 struct statfs st;
490 string Local_Path = Mount_Point + "/.";
491
492 if (!Mount(Display_Error))
493 return false;
494
495 if (statfs(Local_Path.c_str(), &st) != 0) {
496 if (!Removable) {
497 if (Display_Error)
498 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
499 else
500 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
501 }
502 return false;
503 }
504 Size = (st.f_blocks * st.f_bsize);
505 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
506 Free = (st.f_bfree * st.f_bsize);
507 Backup_Size = Used;
508 return true;
509}
510
511bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400512 FILE* fp;
513 char command[255], line[512];
514 int include_block = 1;
515 unsigned int min_len;
516
517 if (!Mount(Display_Error))
518 return false;
519
Dees_Troy38bd7602012-09-14 13:33:53 -0400520 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400521 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400522 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400523 fp = fopen("/tmp/dfoutput.txt", "rt");
524 if (fp == NULL) {
525 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400526 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400527 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400528
529 while (fgets(line, sizeof(line), fp) != NULL)
530 {
531 unsigned long blocks, used, available;
532 char device[64];
533 char tmpString[64];
534
535 if (strncmp(line, "Filesystem", 10) == 0)
536 continue;
537 if (strlen(line) < min_len) {
538 include_block = 0;
539 continue;
540 }
541 if (include_block) {
542 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
543 } else {
544 // The device block string is so long that the df information is on the next line
545 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400546 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400547 while (tmpString[space_count] == 32)
548 space_count++;
549 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
550 }
551
552 // Adjust block size to byte size
553 Size = blocks * 1024ULL;
554 Used = used * 1024ULL;
555 Free = available * 1024ULL;
556 Backup_Size = Used;
557 }
558 fclose(fp);
559 return true;
560}
561
Dees_Troy5bf43922012-09-07 16:07:55 -0400562bool TWPartition::Find_Partition_Size(void) {
563 FILE* fp;
564 char line[512];
565 string tmpdevice;
566
567 // In this case, we'll first get the partitions we care about (with labels)
568 fp = fopen("/proc/partitions", "rt");
569 if (fp == NULL)
570 return false;
571
572 while (fgets(line, sizeof(line), fp) != NULL)
573 {
574 unsigned long major, minor, blocks;
575 char device[512];
576 char tmpString[64];
577
Dees_Troy63c8df72012-09-10 14:02:05 -0400578 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400579 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
580
581 tmpdevice = "/dev/block/";
582 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400583 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400584 // Adjust block size to byte size
585 Size = blocks * 1024ULL;
586 fclose(fp);
587 return true;
588 }
589 }
590 fclose(fp);
591 return false;
592}
593
Dees_Troy5bf43922012-09-07 16:07:55 -0400594bool TWPartition::Is_Mounted(void) {
595 if (!Can_Be_Mounted)
596 return false;
597
598 struct stat st1, st2;
599 string test_path;
600
601 // Check to see if the mount point directory exists
602 test_path = Mount_Point + "/.";
603 if (stat(test_path.c_str(), &st1) != 0) return false;
604
605 // Check to see if the directory above the mount point exists
606 test_path = Mount_Point + "/../.";
607 if (stat(test_path.c_str(), &st2) != 0) return false;
608
609 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
610 int ret = (st1.st_dev != st2.st_dev) ? true : false;
611
612 return ret;
613}
614
615bool TWPartition::Mount(bool Display_Error) {
616 if (Is_Mounted()) {
617 return true;
618 } else if (!Can_Be_Mounted) {
619 return false;
620 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400621
622 Find_Actual_Block_Device();
623
624 // Check the current file system before mounting
625 Check_FS_Type();
626
627 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
628 if (Display_Error)
629 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
630 else
631 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy9350b8d2012-09-27 12:38:38 -0400632 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 -0400633 return false;
634 } else {
635 if (Removable)
636 Update_Size(Display_Error);
637
638 if (!Symlink_Mount_Point.empty()) {
639 string Command;
640
641 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400642 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400643 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400644 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400645 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400646 return true;
647}
648
649bool TWPartition::UnMount(bool Display_Error) {
650 if (Is_Mounted()) {
651 int never_unmount_system;
652
653 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
654 if (never_unmount_system == 1 && Mount_Point == "/system")
655 return true; // Never unmount system if you're not supposed to unmount it
656
Dees_Troy38bd7602012-09-14 13:33:53 -0400657 if (!Symlink_Mount_Point.empty())
658 umount(Symlink_Mount_Point.c_str());
659
Dees_Troy5bf43922012-09-07 16:07:55 -0400660 if (umount(Mount_Point.c_str()) != 0) {
661 if (Display_Error)
662 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
663 else
664 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
665 return false;
666 } else
667 return true;
668 } else {
669 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400670 }
671}
672
673bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400674 if (!Can_Be_Wiped) {
675 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
676 return false;
677 }
678
Dees_Troyc51f1f92012-09-20 15:32:13 -0400679 if (Mount_Point == "/cache")
680 tmplog_offset = 0;
681
Dees_Troy38bd7602012-09-14 13:33:53 -0400682 if (Has_Data_Media)
683 return Wipe_Data_Without_Wiping_Media();
684
685 int check;
686 DataManager::GetValue(TW_RM_RF_VAR, check);
687 if (check)
688 return Wipe_RMRF();
689
690 if (Current_File_System == "ext4")
691 return Wipe_EXT4();
692
693 if (Current_File_System == "ext2" || Current_File_System == "ext3")
694 return Wipe_EXT23();
695
696 if (Current_File_System == "vfat")
697 return Wipe_FAT();
698
699 if (Current_File_System == "yaffs2")
700 return Wipe_MTD();
701
702 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
703 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400704}
705
Dees_Troye58d5262012-09-21 12:27:57 -0400706bool TWPartition::Wipe_AndSec(void) {
707 if (!Has_Android_Secure)
708 return false;
709
710 char cmd[512];
711
712 if (!Mount(true))
713 return false;
714
715 ui_print("Using rm -rf on .android_secure\n");
716 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
717
718 LOGI("rm -rf command is: '%s'\n", cmd);
719 system(cmd);
720 return true;
721}
722
Dees_Troy51a0e822012-09-05 15:24:24 -0400723bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400724 if (Backup_Method == FILES)
725 return Backup_Tar(backup_folder);
726 else if (Backup_Method == DD)
727 return Backup_DD(backup_folder);
728 else if (Backup_Method == FLASH_UTILS)
729 return Backup_Dump_Image(backup_folder);
730 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
731 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400732}
733
Dees_Troy43d8b002012-09-17 16:00:01 -0400734bool TWPartition::Check_MD5(string restore_folder) {
735 string Full_Filename;
736 char split_filename[512];
737 int index = 0;
738
739 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400740 if (!TWFunc::Path_Exists(Full_Filename)) {
741 // This is a split archive, we presume
742 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
743 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
744 if (TWFunc::Check_MD5(split_filename) == 0) {
745 LOGE("MD5 failed to match on '%s'.\n", split_filename);
746 return false;
747 }
748 index++;
749 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400750 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400751 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400752 } else {
753 // Single file archive
754 if (TWFunc::Check_MD5(Full_Filename) == 0) {
755 LOGE("MD5 failed to match on '%s'.\n", split_filename);
756 return false;
757 } else
758 return true;
759 }
760 return false;
761}
762
Dees_Troy51a0e822012-09-05 15:24:24 -0400763bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400764 if (Backup_Method == FILES)
765 return Restore_Tar(restore_folder);
766 else if (Backup_Method == DD)
767 return Restore_DD(restore_folder);
768 else if (Backup_Method == FLASH_UTILS)
769 return Restore_Flash_Image(restore_folder);
770 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
771 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400772}
773
774string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400775 if (Backup_Method == NONE)
776 return "none";
777 else if (Backup_Method == FILES)
778 return "files";
779 else if (Backup_Method == DD)
780 return "dd";
781 else if (Backup_Method == FLASH_UTILS)
782 return "flash_utils";
783 else
784 return "undefined";
785 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400786}
787
788bool TWPartition::Decrypt(string Password) {
789 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400790 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400791 return 1;
792}
793
794bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400795 bool Save_Data_Media = Has_Data_Media;
796
797 if (!UnMount(true))
798 return false;
799
800 Current_File_System = Fstab_File_System;
801 Is_Encrypted = false;
802 Is_Decrypted = false;
803 Decrypted_Block_Device = "";
804 Has_Data_Media = false;
805 if (Wipe()) {
806 Has_Data_Media = Save_Data_Media;
807 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
808 Recreate_Media_Folder();
809 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400810 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400811 return true;
812 } else {
813 Has_Data_Media = Save_Data_Media;
814 LOGE("Unable to format to remove encryption.\n");
815 return false;
816 }
817 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400818}
819
820void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400821 FILE *fp;
822 string blkCommand;
823 char blkOutput[255];
824 char* blk;
825 char* arg;
826 char* ptr;
827
828 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
829 return; // Running blkid on some mtd devices causes a massive crash
830
Dees_Troy38bd7602012-09-14 13:33:53 -0400831 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400832 if (!Is_Present)
833 return;
Dees_Troy51127312012-09-08 13:08:49 -0400834
Dees_Troy8170a922012-09-18 15:40:25 -0400835 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
836 system("rm /tmp/blkidoutput.txt");
837
838 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400839 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400840 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400841 if (fp == NULL)
842 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400843 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
844 {
845 blk = blkOutput;
846 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400847 while (*ptr > 32 && *ptr != ':') ptr++;
848 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400849 *ptr = 0;
850
851 // Increment by two, but verify that we don't hit a NULL
852 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400853 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400854
855 // Now, find the TYPE field
856 while (1)
857 {
858 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400859 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400860 if (*ptr != 0)
861 {
862 *ptr = 0;
863 ptr++;
864 }
865
866 if (strlen(arg) > 6)
867 {
868 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
869 }
870
871 if (*ptr == 0)
872 {
873 arg = NULL;
874 break;
875 }
876 }
877
878 if (arg && strlen(arg) > 7)
879 {
880 arg += 6; // Skip the TYPE=" portion
881 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
882 }
883 else
884 continue;
885
Dees_Troy63c8df72012-09-10 14:02:05 -0400886 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400887 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
888 Current_File_System = arg;
889 }
890 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400891 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400892 return;
893}
894
895bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400896 if (!UnMount(true))
897 return false;
898
Dees_Troy43d8b002012-09-17 16:00:01 -0400899 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400900 char command[512];
901
902 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
903 Find_Actual_Block_Device();
904 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
905 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400906 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400907 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400908 ui_print("Done.\n");
909 return true;
910 } else {
911 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
912 return false;
913 }
914 } else
915 return Wipe_RMRF();
916
917 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400918}
919
920bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400921 if (!UnMount(true))
922 return false;
923
Dees_Troy43d8b002012-09-17 16:00:01 -0400924 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400925 string Command;
926
927 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
928 Find_Actual_Block_Device();
929 Command = "make_ext4fs";
930 if (!Is_Decrypted && Length != 0) {
931 // Only use length if we're not decrypted
932 char len[32];
933 sprintf(len, "%i", Length);
934 Command += " -l ";
935 Command += len;
936 }
937 Command += " " + Actual_Block_Device;
938 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400939 if (system(Command.c_str()) == 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_EXT23();
949
950 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400951}
952
953bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400954 char command[512];
955
Dees_Troy43d8b002012-09-17 16:00:01 -0400956 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400957 if (!UnMount(true))
958 return false;
959
960 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
961 Find_Actual_Block_Device();
962 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400963 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400964 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400965 ui_print("Done.\n");
966 return true;
967 } else {
968 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
969 return false;
970 }
971 return true;
972 }
973 else
974 return Wipe_RMRF();
975
976 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400977}
978
Dees_Troy38bd7602012-09-14 13:33:53 -0400979bool TWPartition::Wipe_MTD() {
980 if (!UnMount(true))
981 return false;
982
983 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
984
985 mtd_scan_partitions();
986 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
987 if (mtd == NULL) {
988 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
989 return false;
990 }
991
992 MtdWriteContext* ctx = mtd_write_partition(mtd);
993 if (ctx == NULL) {
994 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
995 return false;
996 }
997 if (mtd_erase_blocks(ctx, -1) == -1) {
998 mtd_write_close(ctx);
999 LOGE("Failed to format '%s'", MTD_Name.c_str());
1000 return false;
1001 }
1002 if (mtd_write_close(ctx) != 0) {
1003 LOGE("Failed to close '%s'", MTD_Name.c_str());
1004 return false;
1005 }
Dees_Troye58d5262012-09-21 12:27:57 -04001006 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001007 ui_print("Done.\n");
1008 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001009}
1010
1011bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001012 char cmd[512];
1013
1014 if (!Mount(true))
1015 return false;
1016
Dees_Troye58d5262012-09-21 12:27:57 -04001017 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1018 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001019
1020 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001021 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001022 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001023 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001024}
1025
1026bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001027 char cmd[256];
1028
1029 // This handles wiping data on devices with "sdcard" in /data/media
1030 if (!Mount(true))
1031 return false;
1032
1033 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001034 system("rm -f /data/*");
1035 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001036
1037 DIR* d;
1038 d = opendir("/data");
1039 if (d != NULL)
1040 {
1041 struct dirent* de;
1042 while ((de = readdir(d)) != NULL) {
1043 if (strcmp(de->d_name, "media") == 0) continue;
1044
1045 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001046 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001047 }
1048 closedir(d);
1049 }
1050 ui_print("Done.\n");
1051 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001052}
1053
1054bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001055 char back_name[255], split_index[5];
1056 string Full_FileName, Split_FileName, Tar_Args, Command;
1057 int use_compression, index, backup_count;
1058 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001059 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001060
1061 if (!Mount(true))
1062 return false;
1063
Dees_Troy2c50e182012-09-26 20:05:28 -04001064 if (Backup_Path == "/and-sec") {
1065 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1066 ui_print("Backing up %s...\n", "Android Secure");
1067 } else {
1068 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1069 ui_print("Backing up %s...\n", Display_Name.c_str());
1070 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001071
1072 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1073 if (use_compression)
1074 Tar_Args = "-cz";
1075 else
1076 Tar_Args = "-c";
1077
1078 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1079 Backup_FileName = back_name;
1080
Dees_Troy43d8b002012-09-17 16:00:01 -04001081 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1082 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001083 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001084 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001085 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001086 if (backup_count < 1) {
1087 LOGE("Error generating file list!\n");
1088 return false;
1089 }
1090 for (index=0; index<backup_count; index++) {
1091 sprintf(split_index, "%03i", index);
1092 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1093 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1094 LOGI("Backup command: '%s'\n", Command.c_str());
1095 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1096 system(Command.c_str()); // sending backup command formed earlier above
1097
Dees_Troy7c2dec82012-09-26 09:49:14 -04001098 file_size = TWFunc::Get_File_Size(Full_FileName);
1099 if (file_size == 0) {
1100 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 -04001101 return false;
1102 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001103 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001104 }
1105 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1106 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001107 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001108 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001109 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001110 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001111 else
Dees_Troye58d5262012-09-21 12:27:57 -04001112 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001113 LOGI("Backup command: '%s'\n", Command.c_str());
1114 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001115 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1116 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1117 return false;
1118 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001119 }
1120 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001121}
1122
1123bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001124 char back_name[255];
1125 string Full_FileName, Command;
1126 int use_compression;
1127
Dees_Troyb46a6842012-09-25 11:06:46 -04001128 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001129 ui_print("Backing up %s...\n", Display_Name.c_str());
1130
1131 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1132 Backup_FileName = back_name;
1133
1134 Full_FileName = backup_folder + "/" + Backup_FileName;
1135
1136 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1137 LOGI("Backup command: '%s'\n", Command.c_str());
1138 system(Command.c_str());
Dees_Troyc154ac22012-10-12 15:36:47 -04001139 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1140 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001141 return false;
1142 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001143 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001144}
1145
1146bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001147 char back_name[255];
1148 string Full_FileName, Command;
1149 int use_compression;
1150
Dees_Troyb46a6842012-09-25 11:06:46 -04001151 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001152 ui_print("Backing up %s...\n", Display_Name.c_str());
1153
1154 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1155 Backup_FileName = back_name;
1156
1157 Full_FileName = backup_folder + "/" + Backup_FileName;
1158
1159 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1160 LOGI("Backup command: '%s'\n", Command.c_str());
1161 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001162 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1163 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1164 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1165 return false;
1166 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001167 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001168}
1169
1170bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001171 size_t first_period, second_period;
1172 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001173 int index = 0;
1174 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001175
Dees_Troyb46a6842012-09-25 11:06:46 -04001176 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001177 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1178
1179 // Parse backup filename to extract the file system before wiping
1180 first_period = Backup_FileName.find(".");
1181 if (first_period == string::npos) {
1182 LOGE("Unable to find file system (first period).\n");
1183 return false;
1184 }
1185 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1186 second_period = Restore_File_System.find(".");
1187 if (second_period == string::npos) {
1188 LOGE("Unable to find file system (second period).\n");
1189 return false;
1190 }
1191 Restore_File_System.resize(second_period);
1192 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1193 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001194 if (Has_Android_Secure) {
1195 ui_print("Wiping android secure...\n");
1196 if (!Wipe_AndSec())
1197 return false;
1198 } else if (!Wipe()) {
1199 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001200 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001201 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001202
1203 if (!Mount(true))
1204 return false;
1205
Dees_Troy43d8b002012-09-17 16:00:01 -04001206 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001207 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001208 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001209 // Backup is multiple archives
1210 LOGI("Backup is multiple archives.\n");
1211 sprintf(split_index, "%03i", index);
1212 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1213 while (TWFunc::Path_Exists(Full_FileName)) {
1214 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troy6da522d2012-10-12 15:19:33 -04001215 Command = "tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001216 LOGI("Restore command: '%s'\n", Command.c_str());
1217 system(Command.c_str());
1218 index++;
1219 sprintf(split_index, "%03i", index);
1220 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1221 }
1222 if (index == 0) {
1223 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1224 return false;
1225 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001226 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001227 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001228 LOGI("Restore command: '%s'\n", Command.c_str());
1229 system(Command.c_str());
1230 }
1231 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001232}
1233
1234bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001235 string Full_FileName, Command;
1236
Dees_Troyb46a6842012-09-25 11:06:46 -04001237 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001238 ui_print("Restoring %s...\n", Display_Name.c_str());
1239 Full_FileName = restore_folder + "/" + Backup_FileName;
1240 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1241 LOGI("Restore command: '%s'\n", Command.c_str());
1242 system(Command.c_str());
1243 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001244}
1245
1246bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001247 string Full_FileName, Command;
1248
Dees_Troyb46a6842012-09-25 11:06:46 -04001249 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001250 ui_print("Restoring %s...\n", Display_Name.c_str());
1251 Full_FileName = restore_folder + "/" + Backup_FileName;
1252 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1253 Command = "erase_image " + MTD_Name;
1254 LOGI("Erase command: '%s'\n", Command.c_str());
1255 system(Command.c_str());
1256 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1257 LOGI("Restore command: '%s'\n", Command.c_str());
1258 system(Command.c_str());
1259 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001260}
Dees_Troy5bf43922012-09-07 16:07:55 -04001261
1262bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001263 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001264
Dees_Troyab10ee22012-09-21 14:27:30 -04001265 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001266 return false;
1267
Dees_Troy0550cfb2012-10-13 11:56:13 -04001268 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001269 if (Removable || Is_Encrypted) {
1270 if (!Mount(false))
1271 return true;
1272 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001273 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001274
1275 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001276 if (!ret || Size == 0) {
1277 if (!Get_Size_Via_df(Display_Error)) {
1278 if (!Was_Already_Mounted)
1279 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001280 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001281 }
1282 }
Dees_Troy51127312012-09-08 13:08:49 -04001283
Dees_Troy5bf43922012-09-07 16:07:55 -04001284 if (Has_Data_Media) {
1285 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001286 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001287 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1288 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001289 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001290 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001291 int bak = (int)(Backup_Size / 1048576LLU);
1292 int total = (int)(Size / 1048576LLU);
1293 int us = (int)(Used / 1048576LLU);
1294 int fre = (int)(Free / 1048576LLU);
1295 int datmed = (int)(data_media_used / 1048576LLU);
1296 LOGI("Data backup size is %iMB, size: %iMB, used: %iMB, free: %iMB, in data/media: %iMB.\n", bak, total, us, fre, datmed);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001297 } else {
1298 if (!Was_Already_Mounted)
1299 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001300 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001301 }
Dees_Troye58d5262012-09-21 12:27:57 -04001302 } else if (Has_Android_Secure) {
1303 if (Mount(Display_Error))
1304 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001305 else {
1306 if (!Was_Already_Mounted)
1307 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001308 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001309 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001310 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001311 if (!Was_Already_Mounted)
1312 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001313 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001314}
Dees_Troy38bd7602012-09-14 13:33:53 -04001315
1316void TWPartition::Find_Actual_Block_Device(void) {
1317 if (Is_Decrypted) {
1318 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001319 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001320 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001321 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001322 Is_Present = true;
1323 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001324 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001325 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 Is_Present = true;
1327 } else
1328 Is_Present = false;
1329}
1330
1331void TWPartition::Recreate_Media_Folder(void) {
1332 string Command;
1333
1334 if (!Mount(true)) {
1335 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001336 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001337 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001338 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001339 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001340 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001341 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001342 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001343 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001344}
Dees_Troye58d5262012-09-21 12:27:57 -04001345
1346void TWPartition::Recreate_AndSec_Folder(void) {
1347 string Command;
1348
1349 if (!Has_Android_Secure)
1350 return;
1351
1352 if (!Mount(true)) {
1353 LOGE("Unable to recreate android secure folder.\n");
1354 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1355 LOGI("Recreating android secure folder.\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001356 Command = "umount " + Symlink_Mount_Point;
1357 system(Command.c_str());
Dees_Troyce2fe772012-09-28 12:34:33 -04001358 Command = "cd " + Mount_Point + " && mkdir .android_secure";
1359 system(Command.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001360 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1361 system(Command.c_str());
1362 }
1363}