blob: 42a44812cd86807d9644d725c4aa1a7216f846b6 [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_Troy5bf43922012-09-07 16:07:55 -040041extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040042 #include "mtdutils/mtdutils.h"
43 #include "mtdutils/mounts.h"
Dees_Troy43d8b002012-09-17 16:00:01 -040044 #include "makelist.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 }
104 string mount_pt(full_line);
105 Mount_Point = mount_pt;
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") {
122 Primary_Block_Device = ptr;
123 Find_MTD_Block_Device(Primary_Block_Device);
124 } 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_Troy38bd7602012-09-14 13:33:53 -0400171 MTD_Name = "system";
Dees_Troy5bf43922012-09-07 16:07:55 -0400172 } else if (Mount_Point == "/data") {
173 Display_Name = "Data";
174 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400175 Wipe_During_Factory_Reset = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400176 MTD_Name = "userdata";
Dees_Troy5bf43922012-09-07 16:07:55 -0400177#ifdef RECOVERY_SDCARD_ON_DATA
178 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400179 Is_Storage = true;
180 Storage_Path = "/data/media";
Dees_Troy657c3092012-09-10 20:32:10 -0400181 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
182 Make_Dir("/emmc", Display_Error);
183 Symlink_Path = "/data/media";
184 Symlink_Mount_Point = "/emmc";
185 } else {
186 Make_Dir("/sdcard", Display_Error);
187 Symlink_Path = "/data/media";
188 Symlink_Mount_Point = "/sdcard";
189 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400190#endif
191#ifdef TW_INCLUDE_CRYPTO
192 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400193 char crypto_blkdev[255];
194 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
195 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400196 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400197 DataManager::SetValue(TW_IS_DECRYPTED, 1);
198 Is_Encrypted = true;
199 Is_Decrypted = true;
200 Decrypted_Block_Device = crypto_blkdev;
201 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
202 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400203 Is_Encrypted = true;
204 Is_Decrypted = false;
205 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
206 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
207 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400208 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400209#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400210 } else if (Mount_Point == "/cache") {
211 Display_Name = "Cache";
212 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400213 Wipe_During_Factory_Reset = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400214 MTD_Name = "cache";
Dees_Troy5bf43922012-09-07 16:07:55 -0400215 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400216 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 Display_Name = "DataData";
218 Is_SubPartition = true;
219 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 DataManager::SetValue(TW_HAS_DATADATA, 1);
221 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400222 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400223 Display_Name = "SD-Ext";
224 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400225 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400226 }
227#ifdef TW_EXTERNAL_STORAGE_PATH
228 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
229 Is_Storage = true;
230 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400231 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400232 }
233#else
234 if (Mount_Point == "/sdcard") {
235 Is_Storage = true;
236 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400237 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400238#ifndef RECOVERY_SDCARD_ON_DATA
239 Setup_AndSec();
240#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400241 }
242#endif
243#ifdef TW_INTERNAL_STORAGE_PATH
244 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
245 Is_Storage = true;
246 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400247#ifndef RECOVERY_SDCARD_ON_DATA
248 Setup_AndSec();
249#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400250 }
251#else
252 if (Mount_Point == "/emmc") {
253 Is_Storage = true;
254 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400255#ifndef RECOVERY_SDCARD_ON_DATA
256 Setup_AndSec();
257#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400258 }
259#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400260 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400261 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 Setup_Image(Display_Error);
263 if (Mount_Point == "/boot") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400264 MTD_Name = "boot";
Dees_Troy5bf43922012-09-07 16:07:55 -0400265 int backup_display_size = (int)(Backup_Size / 1048576LLU);
266 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
267 if (Backup_Size == 0) {
268 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
269 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
270 } else
271 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
272 } else if (Mount_Point == "/recovery") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400273 MTD_Name = "recovery";
Dees_Troy5bf43922012-09-07 16:07:55 -0400274 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 }
282 }
283
Dees_Troy51127312012-09-08 13:08:49 -0400284 // Process any custom flags
285 if (Flags.size() > 0)
286 Process_Flags(Flags, Display_Error);
287
288 return true;
289}
290
291bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
292 char flags[MAX_FSTAB_LINE_LENGTH];
293 int flags_len, index = 0;
294 char* ptr;
295
296 strcpy(flags, Flags.c_str());
297 flags_len = Flags.size();
298 for (index = 0; index < flags_len; index++) {
299 if (flags[index] == ';')
300 flags[index] = '\0';
301 }
302
303 index = 0;
304 while (index < flags_len) {
305 while (index < flags_len && flags[index] == '\0')
306 index++;
307 if (index >= flags_len)
308 continue;
309 ptr = flags + index;
310 if (strcmp(ptr, "removable") == 0) {
311 Removable = true;
312 } else if (strcmp(ptr, "storage") == 0) {
313 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400314 } else if (strcmp(ptr, "canbewiped") == 0) {
315 Can_Be_Wiped = true;
316 } else if (strcmp(ptr, "wipeingui") == 0) {
317 Can_Be_Wiped = true;
318 Wipe_Available_in_GUI = true;
319 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
320 Can_Be_Wiped = true;
321 Wipe_Available_in_GUI = true;
322 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400323 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
324 ptr += 13;
325 Is_SubPartition = true;
326 SubPartition_Of = ptr;
327 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
328 ptr += 8;
329 Symlink_Path = ptr;
330 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
331 ptr += 8;
332 Display_Name = ptr;
333 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
334 ptr += 10;
335 Format_Block_Size = atoi(ptr);
336 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
337 ptr += 7;
338 Length = atoi(ptr);
339 } else {
340 if (Display_Error)
341 LOGE("Unhandled flag: '%s'\n", ptr);
342 else
343 LOGI("Unhandled flag: '%s'\n", ptr);
344 }
345 while (index < flags_len && flags[index] != '\0')
346 index++;
347 }
348 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349}
350
Dees_Troy5bf43922012-09-07 16:07:55 -0400351bool TWPartition::Is_File_System(string File_System) {
352 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400353 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 File_System == "ext4" ||
355 File_System == "vfat" ||
356 File_System == "ntfs" ||
357 File_System == "yaffs2" ||
358 File_System == "auto")
359 return true;
360 else
361 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400362}
363
Dees_Troy5bf43922012-09-07 16:07:55 -0400364bool TWPartition::Is_Image(string File_System) {
365 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400366 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 return true;
368 else
369 return false;
370}
371
Dees_Troy51127312012-09-08 13:08:49 -0400372bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400373 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400374 if (mkdir(Path.c_str(), 0777) == -1) {
375 if (Display_Error)
376 LOGE("Can not create '%s' folder.\n", Path.c_str());
377 else
378 LOGI("Can not create '%s' folder.\n", Path.c_str());
379 return false;
380 } else {
381 LOGI("Created '%s' folder.\n", Path.c_str());
382 return true;
383 }
384 }
385 return true;
386}
387
Dees_Troy5bf43922012-09-07 16:07:55 -0400388void TWPartition::Setup_File_System(bool Display_Error) {
389 struct statfs st;
390
391 Can_Be_Mounted = true;
392 Can_Be_Wiped = true;
393
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400395 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400396 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
397 Backup_Name = Display_Name;
398 Backup_Method = FILES;
399}
400
401void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400402 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
403 Backup_Name = Display_Name;
404 if (Fstab_File_System == "emmc")
405 Backup_Method = DD;
406 else if (Fstab_File_System == "mtd")
407 Backup_Method = FLASH_UTILS;
408 else
409 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
410 if (Find_Partition_Size()) {
411 Used = Size;
412 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400413 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400414 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400415 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400416 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400417 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 }
419}
420
Dees_Troye58d5262012-09-21 12:27:57 -0400421void TWPartition::Setup_AndSec(void) {
422 Backup_Name = "and-sec";
423 Has_Android_Secure = true;
424 Symlink_Path = Mount_Point + "/.android_secure";
425 Symlink_Mount_Point = "/and-sec";
426 Backup_Path = Symlink_Mount_Point;
427 Make_Dir("/and-sec", true);
428 Recreate_AndSec_Folder();
429}
430
Dees_Troy5bf43922012-09-07 16:07:55 -0400431void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
432 char device[512], realDevice[512];
433
434 strcpy(device, Block.c_str());
435 memset(realDevice, 0, sizeof(realDevice));
436 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
437 {
438 strcpy(device, realDevice);
439 memset(realDevice, 0, sizeof(realDevice));
440 }
441
442 if (device[0] != '/') {
443 if (Display_Error)
444 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
445 else
446 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
447 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400448 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400449 Block = device;
450 return;
451 }
452}
453
Dees_Troy38bd7602012-09-14 13:33:53 -0400454bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
455 FILE *fp = NULL;
456 char line[255];
457
458 fp = fopen("/proc/mtd", "rt");
459 if (fp == NULL) {
460 LOGE("Device does not support /proc/mtd\n");
461 return false;
462 }
463
464 while (fgets(line, sizeof(line), fp) != NULL)
465 {
466 char device[32], label[32];
467 unsigned long size = 0;
468 char* fstype = NULL;
469 int deviceId;
470
471 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
472
473 // Skip header and blank lines
474 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
475 continue;
476
477 // Strip off the trailing " from the label
478 label[strlen(label)-1] = '\0';
479
480 if (strcmp(label, MTD_Name.c_str()) == 0) {
481 // We found our device
482 // Strip off the trailing : from the device
483 device[strlen(device)-1] = '\0';
484 if (sscanf(device,"mtd%d", &deviceId) == 1) {
485 sprintf(device, "/dev/block/mtdblock%d", deviceId);
486 Primary_Block_Device = device;
487 }
488 }
489 }
490 fclose(fp);
491
492 return false;
493}
494
Dees_Troy51127312012-09-08 13:08:49 -0400495bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
496 struct statfs st;
497 string Local_Path = Mount_Point + "/.";
498
499 if (!Mount(Display_Error))
500 return false;
501
502 if (statfs(Local_Path.c_str(), &st) != 0) {
503 if (!Removable) {
504 if (Display_Error)
505 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
506 else
507 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
508 }
509 return false;
510 }
511 Size = (st.f_blocks * st.f_bsize);
512 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
513 Free = (st.f_bfree * st.f_bsize);
514 Backup_Size = Used;
515 return true;
516}
517
518bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400519 FILE* fp;
520 char command[255], line[512];
521 int include_block = 1;
522 unsigned int min_len;
523
524 if (!Mount(Display_Error))
525 return false;
526
Dees_Troy38bd7602012-09-14 13:33:53 -0400527 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400528 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400530 fp = fopen("/tmp/dfoutput.txt", "rt");
531 if (fp == NULL) {
532 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400533 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400534 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400535
536 while (fgets(line, sizeof(line), fp) != NULL)
537 {
538 unsigned long blocks, used, available;
539 char device[64];
540 char tmpString[64];
541
542 if (strncmp(line, "Filesystem", 10) == 0)
543 continue;
544 if (strlen(line) < min_len) {
545 include_block = 0;
546 continue;
547 }
548 if (include_block) {
549 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
550 } else {
551 // The device block string is so long that the df information is on the next line
552 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400553 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400554 while (tmpString[space_count] == 32)
555 space_count++;
556 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
557 }
558
559 // Adjust block size to byte size
560 Size = blocks * 1024ULL;
561 Used = used * 1024ULL;
562 Free = available * 1024ULL;
563 Backup_Size = Used;
564 }
565 fclose(fp);
566 return true;
567}
568
Dees_Troy5bf43922012-09-07 16:07:55 -0400569bool TWPartition::Find_Partition_Size(void) {
570 FILE* fp;
571 char line[512];
572 string tmpdevice;
573
574 // In this case, we'll first get the partitions we care about (with labels)
575 fp = fopen("/proc/partitions", "rt");
576 if (fp == NULL)
577 return false;
578
579 while (fgets(line, sizeof(line), fp) != NULL)
580 {
581 unsigned long major, minor, blocks;
582 char device[512];
583 char tmpString[64];
584
Dees_Troy63c8df72012-09-10 14:02:05 -0400585 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400586 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
587
588 tmpdevice = "/dev/block/";
589 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400590 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400591 // Adjust block size to byte size
592 Size = blocks * 1024ULL;
593 fclose(fp);
594 return true;
595 }
596 }
597 fclose(fp);
598 return false;
599}
600
Dees_Troy5bf43922012-09-07 16:07:55 -0400601void TWPartition::Flip_Block_Device(void) {
602 string temp;
603
604 temp = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400605 Primary_Block_Device = Alternate_Block_Device;
Dees_Troy5bf43922012-09-07 16:07:55 -0400606 Alternate_Block_Device = temp;
607}
608
609bool TWPartition::Is_Mounted(void) {
610 if (!Can_Be_Mounted)
611 return false;
612
613 struct stat st1, st2;
614 string test_path;
615
616 // Check to see if the mount point directory exists
617 test_path = Mount_Point + "/.";
618 if (stat(test_path.c_str(), &st1) != 0) return false;
619
620 // Check to see if the directory above the mount point exists
621 test_path = Mount_Point + "/../.";
622 if (stat(test_path.c_str(), &st2) != 0) return false;
623
624 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
625 int ret = (st1.st_dev != st2.st_dev) ? true : false;
626
627 return ret;
628}
629
630bool TWPartition::Mount(bool Display_Error) {
631 if (Is_Mounted()) {
632 return true;
633 } else if (!Can_Be_Mounted) {
634 return false;
635 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400636
637 Find_Actual_Block_Device();
638
639 // Check the current file system before mounting
640 Check_FS_Type();
641
642 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
643 if (Display_Error)
644 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
645 else
646 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
647 return false;
648 } else {
649 if (Removable)
650 Update_Size(Display_Error);
651
652 if (!Symlink_Mount_Point.empty()) {
653 string Command;
654
655 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400656 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400657 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400658 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400659 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400660 return true;
661}
662
663bool TWPartition::UnMount(bool Display_Error) {
664 if (Is_Mounted()) {
665 int never_unmount_system;
666
667 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
668 if (never_unmount_system == 1 && Mount_Point == "/system")
669 return true; // Never unmount system if you're not supposed to unmount it
670
Dees_Troy38bd7602012-09-14 13:33:53 -0400671 if (!Symlink_Mount_Point.empty())
672 umount(Symlink_Mount_Point.c_str());
673
Dees_Troy5bf43922012-09-07 16:07:55 -0400674 if (umount(Mount_Point.c_str()) != 0) {
675 if (Display_Error)
676 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
677 else
678 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
679 return false;
680 } else
681 return true;
682 } else {
683 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400684 }
685}
686
687bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400688 if (!Can_Be_Wiped) {
689 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
690 return false;
691 }
692
Dees_Troyc51f1f92012-09-20 15:32:13 -0400693 if (Mount_Point == "/cache")
694 tmplog_offset = 0;
695
Dees_Troy38bd7602012-09-14 13:33:53 -0400696 if (Has_Data_Media)
697 return Wipe_Data_Without_Wiping_Media();
698
699 int check;
700 DataManager::GetValue(TW_RM_RF_VAR, check);
701 if (check)
702 return Wipe_RMRF();
703
704 if (Current_File_System == "ext4")
705 return Wipe_EXT4();
706
707 if (Current_File_System == "ext2" || Current_File_System == "ext3")
708 return Wipe_EXT23();
709
710 if (Current_File_System == "vfat")
711 return Wipe_FAT();
712
713 if (Current_File_System == "yaffs2")
714 return Wipe_MTD();
715
716 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
717 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400718}
719
Dees_Troye58d5262012-09-21 12:27:57 -0400720bool TWPartition::Wipe_AndSec(void) {
721 if (!Has_Android_Secure)
722 return false;
723
724 char cmd[512];
725
726 if (!Mount(true))
727 return false;
728
729 ui_print("Using rm -rf on .android_secure\n");
730 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
731
732 LOGI("rm -rf command is: '%s'\n", cmd);
733 system(cmd);
734 return true;
735}
736
Dees_Troy51a0e822012-09-05 15:24:24 -0400737bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400738 if (Backup_Method == FILES)
739 return Backup_Tar(backup_folder);
740 else if (Backup_Method == DD)
741 return Backup_DD(backup_folder);
742 else if (Backup_Method == FLASH_UTILS)
743 return Backup_Dump_Image(backup_folder);
744 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
745 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400746}
747
Dees_Troy43d8b002012-09-17 16:00:01 -0400748bool TWPartition::Check_MD5(string restore_folder) {
749 string Full_Filename;
750 char split_filename[512];
751 int index = 0;
752
753 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400754 if (!TWFunc::Path_Exists(Full_Filename)) {
755 // This is a split archive, we presume
756 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
757 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
758 if (TWFunc::Check_MD5(split_filename) == 0) {
759 LOGE("MD5 failed to match on '%s'.\n", split_filename);
760 return false;
761 }
762 index++;
763 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400764 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400765 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400766 } else {
767 // Single file archive
768 if (TWFunc::Check_MD5(Full_Filename) == 0) {
769 LOGE("MD5 failed to match on '%s'.\n", split_filename);
770 return false;
771 } else
772 return true;
773 }
774 return false;
775}
776
Dees_Troy51a0e822012-09-05 15:24:24 -0400777bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400778 if (Backup_Method == FILES)
779 return Restore_Tar(restore_folder);
780 else if (Backup_Method == DD)
781 return Restore_DD(restore_folder);
782 else if (Backup_Method == FLASH_UTILS)
783 return Restore_Flash_Image(restore_folder);
784 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
785 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400786}
787
788string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400789 if (Backup_Method == NONE)
790 return "none";
791 else if (Backup_Method == FILES)
792 return "files";
793 else if (Backup_Method == DD)
794 return "dd";
795 else if (Backup_Method == FLASH_UTILS)
796 return "flash_utils";
797 else
798 return "undefined";
799 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400800}
801
802bool TWPartition::Decrypt(string Password) {
803 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400804 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400805 return 1;
806}
807
808bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400809 bool Save_Data_Media = Has_Data_Media;
810
811 if (!UnMount(true))
812 return false;
813
814 Current_File_System = Fstab_File_System;
815 Is_Encrypted = false;
816 Is_Decrypted = false;
817 Decrypted_Block_Device = "";
818 Has_Data_Media = false;
819 if (Wipe()) {
820 Has_Data_Media = Save_Data_Media;
821 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
822 Recreate_Media_Folder();
823 }
824 return true;
825 } else {
826 Has_Data_Media = Save_Data_Media;
827 LOGE("Unable to format to remove encryption.\n");
828 return false;
829 }
830 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400831}
832
833void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400834 FILE *fp;
835 string blkCommand;
836 char blkOutput[255];
837 char* blk;
838 char* arg;
839 char* ptr;
840
841 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
842 return; // Running blkid on some mtd devices causes a massive crash
843
Dees_Troy38bd7602012-09-14 13:33:53 -0400844 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400845 if (!Is_Present)
846 return;
Dees_Troy51127312012-09-08 13:08:49 -0400847
Dees_Troy8170a922012-09-18 15:40:25 -0400848 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
849 system("rm /tmp/blkidoutput.txt");
850
851 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400852 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400853 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400854 if (fp == NULL)
855 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400856 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
857 {
858 blk = blkOutput;
859 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400860 while (*ptr > 32 && *ptr != ':') ptr++;
861 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400862 *ptr = 0;
863
864 // Increment by two, but verify that we don't hit a NULL
865 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400866 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400867
868 // Now, find the TYPE field
869 while (1)
870 {
871 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400872 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400873 if (*ptr != 0)
874 {
875 *ptr = 0;
876 ptr++;
877 }
878
879 if (strlen(arg) > 6)
880 {
881 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
882 }
883
884 if (*ptr == 0)
885 {
886 arg = NULL;
887 break;
888 }
889 }
890
891 if (arg && strlen(arg) > 7)
892 {
893 arg += 6; // Skip the TYPE=" portion
894 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
895 }
896 else
897 continue;
898
Dees_Troy63c8df72012-09-10 14:02:05 -0400899 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400900 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
901 Current_File_System = arg;
902 }
903 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400904 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400905 return;
906}
907
908bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400909 if (!UnMount(true))
910 return false;
911
Dees_Troy43d8b002012-09-17 16:00:01 -0400912 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400913 char command[512];
914
915 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
916 Find_Actual_Block_Device();
917 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
918 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400919 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400920 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400921 ui_print("Done.\n");
922 return true;
923 } else {
924 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
925 return false;
926 }
927 } else
928 return Wipe_RMRF();
929
930 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400931}
932
933bool TWPartition::Wipe_EXT4() {
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/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400938 string Command;
939
940 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
941 Find_Actual_Block_Device();
942 Command = "make_ext4fs";
943 if (!Is_Decrypted && Length != 0) {
944 // Only use length if we're not decrypted
945 char len[32];
946 sprintf(len, "%i", Length);
947 Command += " -l ";
948 Command += len;
949 }
950 Command += " " + Actual_Block_Device;
951 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400952 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400953 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400954 ui_print("Done.\n");
955 return true;
956 } else {
957 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
958 return false;
959 }
960 } else
961 return Wipe_EXT23();
962
963 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400964}
965
966bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400967 char command[512];
968
Dees_Troy43d8b002012-09-17 16:00:01 -0400969 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400970 if (!UnMount(true))
971 return false;
972
973 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
974 Find_Actual_Block_Device();
975 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400976 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400977 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400978 ui_print("Done.\n");
979 return true;
980 } else {
981 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
982 return false;
983 }
984 return true;
985 }
986 else
987 return Wipe_RMRF();
988
989 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400990}
991
Dees_Troy38bd7602012-09-14 13:33:53 -0400992bool TWPartition::Wipe_MTD() {
993 if (!UnMount(true))
994 return false;
995
996 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
997
998 mtd_scan_partitions();
999 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1000 if (mtd == NULL) {
1001 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1002 return false;
1003 }
1004
1005 MtdWriteContext* ctx = mtd_write_partition(mtd);
1006 if (ctx == NULL) {
1007 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1008 return false;
1009 }
1010 if (mtd_erase_blocks(ctx, -1) == -1) {
1011 mtd_write_close(ctx);
1012 LOGE("Failed to format '%s'", MTD_Name.c_str());
1013 return false;
1014 }
1015 if (mtd_write_close(ctx) != 0) {
1016 LOGE("Failed to close '%s'", MTD_Name.c_str());
1017 return false;
1018 }
Dees_Troye58d5262012-09-21 12:27:57 -04001019 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001020 ui_print("Done.\n");
1021 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001022}
1023
1024bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001025 char cmd[512];
1026
1027 if (!Mount(true))
1028 return false;
1029
Dees_Troye58d5262012-09-21 12:27:57 -04001030 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1031 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001032
1033 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001034 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001035 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001036 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001037}
1038
1039bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001040 char cmd[256];
1041
1042 // This handles wiping data on devices with "sdcard" in /data/media
1043 if (!Mount(true))
1044 return false;
1045
1046 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001047 system("rm -f /data/*");
1048 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001049
1050 DIR* d;
1051 d = opendir("/data");
1052 if (d != NULL)
1053 {
1054 struct dirent* de;
1055 while ((de = readdir(d)) != NULL) {
1056 if (strcmp(de->d_name, "media") == 0) continue;
1057
1058 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001059 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001060 }
1061 closedir(d);
1062 }
1063 ui_print("Done.\n");
1064 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001065}
1066
1067bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001068 char back_name[255], split_index[5];
1069 string Full_FileName, Split_FileName, Tar_Args, Command;
1070 int use_compression, index, backup_count;
1071 struct stat st;
1072 unsigned long long total_bsize = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -04001073
1074 if (!Mount(true))
1075 return false;
1076
1077 ui_print("Backing up %s...\n", Display_Name.c_str());
1078
1079 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1080 if (use_compression)
1081 Tar_Args = "-cz";
1082 else
1083 Tar_Args = "-c";
1084
1085 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1086 Backup_FileName = back_name;
1087
Dees_Troy43d8b002012-09-17 16:00:01 -04001088 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1089 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001090 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001091 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001092 backup_count = make_file_list(back_name);
1093 if (backup_count < 1) {
1094 LOGE("Error generating file list!\n");
1095 return false;
1096 }
1097 for (index=0; index<backup_count; index++) {
1098 sprintf(split_index, "%03i", index);
1099 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1100 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1101 LOGI("Backup command: '%s'\n", Command.c_str());
1102 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1103 system(Command.c_str()); // sending backup command formed earlier above
1104
1105 if (stat(Full_FileName.c_str(), &st) != 0 || st.st_size == 0) {
1106 LOGE("File size is zero bytes. Aborting...\n\n"); // oh noes! file size is 0, abort! abort!
1107 return false;
1108 }
1109 total_bsize += st.st_size;
1110 }
1111 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1112 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001113 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001114 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001115 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001116 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001117 else
Dees_Troye58d5262012-09-21 12:27:57 -04001118 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001119 LOGI("Backup command: '%s'\n", Command.c_str());
1120 system(Command.c_str());
1121 }
1122 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001123}
1124
1125bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001126 char back_name[255];
1127 string Full_FileName, Command;
1128 int use_compression;
1129
1130 ui_print("Backing up %s...\n", Display_Name.c_str());
1131
1132 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1133 Backup_FileName = back_name;
1134
1135 Full_FileName = backup_folder + "/" + Backup_FileName;
1136
1137 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1138 LOGI("Backup command: '%s'\n", Command.c_str());
1139 system(Command.c_str());
1140 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001141}
1142
1143bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001144 char back_name[255];
1145 string Full_FileName, Command;
1146 int use_compression;
1147
1148 ui_print("Backing up %s...\n", Display_Name.c_str());
1149
1150 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1151 Backup_FileName = back_name;
1152
1153 Full_FileName = backup_folder + "/" + Backup_FileName;
1154
1155 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1156 LOGI("Backup command: '%s'\n", Command.c_str());
1157 system(Command.c_str());
1158 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001159}
1160
1161bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001162 size_t first_period, second_period;
1163 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001164 int index = 0;
1165 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001166
1167 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1168
1169 // Parse backup filename to extract the file system before wiping
1170 first_period = Backup_FileName.find(".");
1171 if (first_period == string::npos) {
1172 LOGE("Unable to find file system (first period).\n");
1173 return false;
1174 }
1175 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1176 second_period = Restore_File_System.find(".");
1177 if (second_period == string::npos) {
1178 LOGE("Unable to find file system (second period).\n");
1179 return false;
1180 }
1181 Restore_File_System.resize(second_period);
1182 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1183 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001184 if (Has_Android_Secure) {
1185 ui_print("Wiping android secure...\n");
1186 if (!Wipe_AndSec())
1187 return false;
1188 } else if (!Wipe()) {
1189 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001190 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001191 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001192
1193 if (!Mount(true))
1194 return false;
1195
Dees_Troy43d8b002012-09-17 16:00:01 -04001196 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001197 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001198 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001199 // Backup is multiple archives
1200 LOGI("Backup is multiple archives.\n");
1201 sprintf(split_index, "%03i", index);
1202 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1203 while (TWFunc::Path_Exists(Full_FileName)) {
1204 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001205 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001206 LOGI("Restore command: '%s'\n", Command.c_str());
1207 system(Command.c_str());
1208 index++;
1209 sprintf(split_index, "%03i", index);
1210 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1211 }
1212 if (index == 0) {
1213 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1214 return false;
1215 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001216 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001217 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001218 LOGI("Restore command: '%s'\n", Command.c_str());
1219 system(Command.c_str());
1220 }
1221 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001222}
1223
1224bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001225 string Full_FileName, Command;
1226
1227 ui_print("Restoring %s...\n", Display_Name.c_str());
1228 Full_FileName = restore_folder + "/" + Backup_FileName;
1229 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1230 LOGI("Restore command: '%s'\n", Command.c_str());
1231 system(Command.c_str());
1232 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001233}
1234
1235bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001236 string Full_FileName, Command;
1237
1238 ui_print("Restoring %s...\n", Display_Name.c_str());
1239 Full_FileName = restore_folder + "/" + Backup_FileName;
1240 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1241 Command = "erase_image " + MTD_Name;
1242 LOGI("Erase command: '%s'\n", Command.c_str());
1243 system(Command.c_str());
1244 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1245 LOGI("Restore command: '%s'\n", Command.c_str());
1246 system(Command.c_str());
1247 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001248}
Dees_Troy5bf43922012-09-07 16:07:55 -04001249
1250bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001251 bool ret = false;
1252
Dees_Troyab10ee22012-09-21 14:27:30 -04001253 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001254 return false;
1255
Dees_Troy38bd7602012-09-14 13:33:53 -04001256 if (Removable || Is_Encrypted) {
1257 if (!Mount(false))
1258 return true;
1259 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001260 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001261
1262 ret = Get_Size_Via_statfs(Display_Error);
1263 if (!ret || Size == 0)
1264 if (!Get_Size_Via_df(Display_Error))
1265 return false;
1266
Dees_Troy5bf43922012-09-07 16:07:55 -04001267 if (Has_Data_Media) {
1268 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001269 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001270 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1271 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001272 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001273 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001274 int bak = (int)(Backup_Size / 1048576LLU);
1275 int total = (int)(Size / 1048576LLU);
1276 int us = (int)(Used / 1048576LLU);
1277 int fre = (int)(Free / 1048576LLU);
1278 int datmed = (int)(data_media_used / 1048576LLU);
1279 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 -04001280 } else
1281 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001282 } else if (Has_Android_Secure) {
1283 if (Mount(Display_Error))
1284 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
1285 else
1286 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001287 }
1288 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001289}
Dees_Troy38bd7602012-09-14 13:33:53 -04001290
1291void TWPartition::Find_Actual_Block_Device(void) {
1292 if (Is_Decrypted) {
1293 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001294 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001295 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001296 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001297 Is_Present = true;
1298 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001299 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001300 Flip_Block_Device();
1301 Actual_Block_Device = Primary_Block_Device;
1302 Is_Present = true;
1303 } else
1304 Is_Present = false;
1305}
1306
1307void TWPartition::Recreate_Media_Folder(void) {
1308 string Command;
1309
1310 if (!Mount(true)) {
1311 LOGE("Unable to recreate /data/media folder.\n");
1312 } else {
1313 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001314 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001315 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001316 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001317 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001318 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001319 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001320}
Dees_Troye58d5262012-09-21 12:27:57 -04001321
1322void TWPartition::Recreate_AndSec_Folder(void) {
1323 string Command;
1324
1325 if (!Has_Android_Secure)
1326 return;
1327
1328 if (!Mount(true)) {
1329 LOGE("Unable to recreate android secure folder.\n");
1330 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1331 LOGI("Recreating android secure folder.\n");
1332 TWFunc::Recursive_Mkdir(Symlink_Path);
1333 Command = "umount " + Symlink_Mount_Point;
1334 system(Command.c_str());
1335 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1336 system(Command.c_str());
1337 }
1338}