blob: 341dee7048fefaa9569784376d4902d52b7dc53f [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;
54 SubPartition_Of = "";
55 Symlink_Path = "";
56 Symlink_Mount_Point = "";
57 Mount_Point = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040058 Actual_Block_Device = "";
59 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040060 Alternate_Block_Device = "";
61 Removable = false;
62 Is_Present = false;
63 Length = 0;
64 Size = 0;
65 Used = 0;
66 Free = 0;
67 Backup_Size = 0;
68 Can_Be_Encrypted = false;
69 Is_Encrypted = false;
70 Is_Decrypted = false;
71 Decrypted_Block_Device = "";
72 Display_Name = "";
73 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040074 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040075 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040076 Backup_Method = NONE;
77 Has_Data_Media = false;
78 Is_Storage = false;
79 Storage_Path = "";
80 Current_File_System = "";
81 Fstab_File_System = "";
82 Format_Block_Size = 0;
83}
84
85TWPartition::~TWPartition(void) {
86 // Do nothing
87}
88
Dees_Troy5bf43922012-09-07 16:07:55 -040089bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
90 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
91 int line_len = Line.size(), index = 0, item_index = 0;
92 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -040093 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -040094
95 strncpy(full_line, Line.c_str(), line_len);
96
Dees_Troy51127312012-09-08 13:08:49 -040097 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -040098 if (full_line[index] <= 32)
99 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400100 }
101 string mount_pt(full_line);
102 Mount_Point = mount_pt;
103 index = Mount_Point.size();
104 while (index < line_len) {
105 while (index < line_len && full_line[index] == '\0')
106 index++;
107 if (index >= line_len)
108 continue;
109 ptr = full_line + index;
110 if (item_index == 0) {
111 // File System
112 Fstab_File_System = ptr;
113 Current_File_System = ptr;
114 item_index++;
115 } else if (item_index == 1) {
116 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400117 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
118 Primary_Block_Device = ptr;
119 Find_MTD_Block_Device(Primary_Block_Device);
120 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400121 if (Display_Error)
122 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
123 else
124 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
125 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400126 } else {
127 Primary_Block_Device = ptr;
128 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400129 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400130 item_index++;
131 } else if (item_index > 1) {
132 if (*ptr == '/') {
133 // Alternate Block Device
134 Alternate_Block_Device = ptr;
135 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
136 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
137 // Partition length
138 ptr += 7;
139 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400140 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
141 // Custom flags, save for later so that new values aren't overwritten by defaults
142 ptr += 6;
143 Flags = ptr;
Dees_Troy38bd7602012-09-14 13:33:53 -0400144 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
145 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400146 } else {
147 // Unhandled data
148 LOGI("Unhandled fstab information: '%s', %i\n", ptr, index);
149 }
150 }
151 while (index < line_len && full_line[index] != '\0')
152 index++;
153 }
154
155 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
156 if (Display_Error)
157 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
158 else
159 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
160 return 0;
161 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400162 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400163 Setup_File_System(Display_Error);
164 if (Mount_Point == "/system") {
165 Display_Name = "System";
166 Wipe_Available_in_GUI = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400167 MTD_Name = "system";
Dees_Troy5bf43922012-09-07 16:07:55 -0400168 } else if (Mount_Point == "/data") {
169 Display_Name = "Data";
170 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400171 Wipe_During_Factory_Reset = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400172 MTD_Name = "userdata";
Dees_Troy5bf43922012-09-07 16:07:55 -0400173#ifdef RECOVERY_SDCARD_ON_DATA
174 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400175 Is_Storage = true;
176 Storage_Path = "/data/media";
Dees_Troy657c3092012-09-10 20:32:10 -0400177 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
178 Make_Dir("/emmc", Display_Error);
179 Symlink_Path = "/data/media";
180 Symlink_Mount_Point = "/emmc";
181 } else {
182 Make_Dir("/sdcard", Display_Error);
183 Symlink_Path = "/data/media";
184 Symlink_Mount_Point = "/sdcard";
185 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400186#endif
187#ifdef TW_INCLUDE_CRYPTO
188 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400189 char crypto_blkdev[255];
190 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
191 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400192 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400193 DataManager::SetValue(TW_IS_DECRYPTED, 1);
194 Is_Encrypted = true;
195 Is_Decrypted = true;
196 Decrypted_Block_Device = crypto_blkdev;
197 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
198 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400199 Is_Encrypted = true;
200 Is_Decrypted = false;
201 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
202 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
203 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400204 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400205#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400206 } else if (Mount_Point == "/cache") {
207 Display_Name = "Cache";
208 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400209 Wipe_During_Factory_Reset = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400210 MTD_Name = "cache";
Dees_Troy5bf43922012-09-07 16:07:55 -0400211 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400212 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400213 Display_Name = "DataData";
214 Is_SubPartition = true;
215 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400216 DataManager::SetValue(TW_HAS_DATADATA, 1);
217 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400218 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 Display_Name = "SD-Ext";
220 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400221 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400222 }
223#ifdef TW_EXTERNAL_STORAGE_PATH
224 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
225 Is_Storage = true;
226 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400227 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400228 }
229#else
230 if (Mount_Point == "/sdcard") {
231 Is_Storage = true;
232 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400233 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400234 }
235#endif
236#ifdef TW_INTERNAL_STORAGE_PATH
237 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
238 Is_Storage = true;
239 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
240 }
241#else
242 if (Mount_Point == "/emmc") {
243 Is_Storage = true;
244 Storage_Path = "/emmc";
245 }
246#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400248 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400249 Setup_Image(Display_Error);
250 if (Mount_Point == "/boot") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400251 MTD_Name = "boot";
Dees_Troy5bf43922012-09-07 16:07:55 -0400252 int backup_display_size = (int)(Backup_Size / 1048576LLU);
253 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
254 if (Backup_Size == 0) {
255 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
256 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
257 } else
258 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
259 } else if (Mount_Point == "/recovery") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400260 MTD_Name = "recovery";
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 int backup_display_size = (int)(Backup_Size / 1048576LLU);
262 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
263 if (Backup_Size == 0) {
264 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
265 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
266 } else
267 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
268 }
269 }
270
Dees_Troy51127312012-09-08 13:08:49 -0400271 // Process any custom flags
272 if (Flags.size() > 0)
273 Process_Flags(Flags, Display_Error);
274
275 return true;
276}
277
278bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
279 char flags[MAX_FSTAB_LINE_LENGTH];
280 int flags_len, index = 0;
281 char* ptr;
282
283 strcpy(flags, Flags.c_str());
284 flags_len = Flags.size();
285 for (index = 0; index < flags_len; index++) {
286 if (flags[index] == ';')
287 flags[index] = '\0';
288 }
289
290 index = 0;
291 while (index < flags_len) {
292 while (index < flags_len && flags[index] == '\0')
293 index++;
294 if (index >= flags_len)
295 continue;
296 ptr = flags + index;
297 if (strcmp(ptr, "removable") == 0) {
298 Removable = true;
299 } else if (strcmp(ptr, "storage") == 0) {
300 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400301 } else if (strcmp(ptr, "canbewiped") == 0) {
302 Can_Be_Wiped = true;
303 } else if (strcmp(ptr, "wipeingui") == 0) {
304 Can_Be_Wiped = true;
305 Wipe_Available_in_GUI = true;
306 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
307 Can_Be_Wiped = true;
308 Wipe_Available_in_GUI = true;
309 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400310 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
311 ptr += 13;
312 Is_SubPartition = true;
313 SubPartition_Of = ptr;
314 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
315 ptr += 8;
316 Symlink_Path = ptr;
317 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
318 ptr += 8;
319 Display_Name = ptr;
320 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
321 ptr += 10;
322 Format_Block_Size = atoi(ptr);
323 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
324 ptr += 7;
325 Length = atoi(ptr);
326 } else {
327 if (Display_Error)
328 LOGE("Unhandled flag: '%s'\n", ptr);
329 else
330 LOGI("Unhandled flag: '%s'\n", ptr);
331 }
332 while (index < flags_len && flags[index] != '\0')
333 index++;
334 }
335 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400336}
337
Dees_Troy5bf43922012-09-07 16:07:55 -0400338bool TWPartition::Is_File_System(string File_System) {
339 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400340 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400341 File_System == "ext4" ||
342 File_System == "vfat" ||
343 File_System == "ntfs" ||
344 File_System == "yaffs2" ||
345 File_System == "auto")
346 return true;
347 else
348 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349}
350
Dees_Troy5bf43922012-09-07 16:07:55 -0400351bool TWPartition::Is_Image(string File_System) {
352 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400353 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 return true;
355 else
356 return false;
357}
358
Dees_Troy51127312012-09-08 13:08:49 -0400359bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400360 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400361 if (mkdir(Path.c_str(), 0777) == -1) {
362 if (Display_Error)
363 LOGE("Can not create '%s' folder.\n", Path.c_str());
364 else
365 LOGI("Can not create '%s' folder.\n", Path.c_str());
366 return false;
367 } else {
368 LOGI("Created '%s' folder.\n", Path.c_str());
369 return true;
370 }
371 }
372 return true;
373}
374
Dees_Troy5bf43922012-09-07 16:07:55 -0400375void TWPartition::Setup_File_System(bool Display_Error) {
376 struct statfs st;
377
378 Can_Be_Mounted = true;
379 Can_Be_Wiped = true;
380
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400382 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400383 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
384 Backup_Name = Display_Name;
385 Backup_Method = FILES;
386}
387
388void TWPartition::Setup_Image(bool 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 if (Fstab_File_System == "emmc")
392 Backup_Method = DD;
393 else if (Fstab_File_System == "mtd")
394 Backup_Method = FLASH_UTILS;
395 else
396 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
397 if (Find_Partition_Size()) {
398 Used = Size;
399 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400400 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400402 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400403 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400404 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400405 }
406}
407
Dees_Troy5bf43922012-09-07 16:07:55 -0400408void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
409 char device[512], realDevice[512];
410
411 strcpy(device, Block.c_str());
412 memset(realDevice, 0, sizeof(realDevice));
413 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
414 {
415 strcpy(device, realDevice);
416 memset(realDevice, 0, sizeof(realDevice));
417 }
418
419 if (device[0] != '/') {
420 if (Display_Error)
421 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
422 else
423 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
424 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400425 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400426 Block = device;
427 return;
428 }
429}
430
Dees_Troy38bd7602012-09-14 13:33:53 -0400431bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
432 FILE *fp = NULL;
433 char line[255];
434
435 fp = fopen("/proc/mtd", "rt");
436 if (fp == NULL) {
437 LOGE("Device does not support /proc/mtd\n");
438 return false;
439 }
440
441 while (fgets(line, sizeof(line), fp) != NULL)
442 {
443 char device[32], label[32];
444 unsigned long size = 0;
445 char* fstype = NULL;
446 int deviceId;
447
448 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
449
450 // Skip header and blank lines
451 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
452 continue;
453
454 // Strip off the trailing " from the label
455 label[strlen(label)-1] = '\0';
456
457 if (strcmp(label, MTD_Name.c_str()) == 0) {
458 // We found our device
459 // Strip off the trailing : from the device
460 device[strlen(device)-1] = '\0';
461 if (sscanf(device,"mtd%d", &deviceId) == 1) {
462 sprintf(device, "/dev/block/mtdblock%d", deviceId);
463 Primary_Block_Device = device;
464 }
465 }
466 }
467 fclose(fp);
468
469 return false;
470}
471
Dees_Troy51127312012-09-08 13:08:49 -0400472bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
473 struct statfs st;
474 string Local_Path = Mount_Point + "/.";
475
476 if (!Mount(Display_Error))
477 return false;
478
479 if (statfs(Local_Path.c_str(), &st) != 0) {
480 if (!Removable) {
481 if (Display_Error)
482 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
483 else
484 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
485 }
486 return false;
487 }
488 Size = (st.f_blocks * st.f_bsize);
489 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
490 Free = (st.f_bfree * st.f_bsize);
491 Backup_Size = Used;
492 return true;
493}
494
495bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400496 FILE* fp;
497 char command[255], line[512];
498 int include_block = 1;
499 unsigned int min_len;
500
501 if (!Mount(Display_Error))
502 return false;
503
Dees_Troy38bd7602012-09-14 13:33:53 -0400504 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400505 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400506 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400507 fp = fopen("/tmp/dfoutput.txt", "rt");
508 if (fp == NULL) {
509 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400510 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400511 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400512
513 while (fgets(line, sizeof(line), fp) != NULL)
514 {
515 unsigned long blocks, used, available;
516 char device[64];
517 char tmpString[64];
518
519 if (strncmp(line, "Filesystem", 10) == 0)
520 continue;
521 if (strlen(line) < min_len) {
522 include_block = 0;
523 continue;
524 }
525 if (include_block) {
526 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
527 } else {
528 // The device block string is so long that the df information is on the next line
529 int space_count = 0;
530 while (tmpString[space_count] == 32)
531 space_count++;
532 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
533 }
534
535 // Adjust block size to byte size
536 Size = blocks * 1024ULL;
537 Used = used * 1024ULL;
538 Free = available * 1024ULL;
539 Backup_Size = Used;
540 }
541 fclose(fp);
542 return true;
543}
544
Dees_Troy5bf43922012-09-07 16:07:55 -0400545bool TWPartition::Find_Partition_Size(void) {
546 FILE* fp;
547 char line[512];
548 string tmpdevice;
549
550 // In this case, we'll first get the partitions we care about (with labels)
551 fp = fopen("/proc/partitions", "rt");
552 if (fp == NULL)
553 return false;
554
555 while (fgets(line, sizeof(line), fp) != NULL)
556 {
557 unsigned long major, minor, blocks;
558 char device[512];
559 char tmpString[64];
560
Dees_Troy63c8df72012-09-10 14:02:05 -0400561 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400562 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
563
564 tmpdevice = "/dev/block/";
565 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400566 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400567 // Adjust block size to byte size
568 Size = blocks * 1024ULL;
569 fclose(fp);
570 return true;
571 }
572 }
573 fclose(fp);
574 return false;
575}
576
Dees_Troy5bf43922012-09-07 16:07:55 -0400577void TWPartition::Flip_Block_Device(void) {
578 string temp;
579
580 temp = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400581 Primary_Block_Device = Alternate_Block_Device;
Dees_Troy5bf43922012-09-07 16:07:55 -0400582 Alternate_Block_Device = temp;
583}
584
585bool TWPartition::Is_Mounted(void) {
586 if (!Can_Be_Mounted)
587 return false;
588
589 struct stat st1, st2;
590 string test_path;
591
592 // Check to see if the mount point directory exists
593 test_path = Mount_Point + "/.";
594 if (stat(test_path.c_str(), &st1) != 0) return false;
595
596 // Check to see if the directory above the mount point exists
597 test_path = Mount_Point + "/../.";
598 if (stat(test_path.c_str(), &st2) != 0) return false;
599
600 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
601 int ret = (st1.st_dev != st2.st_dev) ? true : false;
602
603 return ret;
604}
605
606bool TWPartition::Mount(bool Display_Error) {
607 if (Is_Mounted()) {
608 return true;
609 } else if (!Can_Be_Mounted) {
610 return false;
611 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400612
613 Find_Actual_Block_Device();
614
615 // Check the current file system before mounting
616 Check_FS_Type();
617
618 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
619 if (Display_Error)
620 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
621 else
622 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
623 return false;
624 } else {
625 if (Removable)
626 Update_Size(Display_Error);
627
628 if (!Symlink_Mount_Point.empty()) {
629 string Command;
630
631 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400632 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400633 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400634 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400635 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400636 return true;
637}
638
639bool TWPartition::UnMount(bool Display_Error) {
640 if (Is_Mounted()) {
641 int never_unmount_system;
642
643 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
644 if (never_unmount_system == 1 && Mount_Point == "/system")
645 return true; // Never unmount system if you're not supposed to unmount it
646
Dees_Troy38bd7602012-09-14 13:33:53 -0400647 if (!Symlink_Mount_Point.empty())
648 umount(Symlink_Mount_Point.c_str());
649
Dees_Troy5bf43922012-09-07 16:07:55 -0400650 if (umount(Mount_Point.c_str()) != 0) {
651 if (Display_Error)
652 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
653 else
654 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
655 return false;
656 } else
657 return true;
658 } else {
659 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400660 }
661}
662
663bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400664 if (!Can_Be_Wiped) {
665 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
666 return false;
667 }
668
Dees_Troyc51f1f92012-09-20 15:32:13 -0400669 if (Mount_Point == "/cache")
670 tmplog_offset = 0;
671
Dees_Troy38bd7602012-09-14 13:33:53 -0400672 if (Has_Data_Media)
673 return Wipe_Data_Without_Wiping_Media();
674
675 int check;
676 DataManager::GetValue(TW_RM_RF_VAR, check);
677 if (check)
678 return Wipe_RMRF();
679
680 if (Current_File_System == "ext4")
681 return Wipe_EXT4();
682
683 if (Current_File_System == "ext2" || Current_File_System == "ext3")
684 return Wipe_EXT23();
685
686 if (Current_File_System == "vfat")
687 return Wipe_FAT();
688
689 if (Current_File_System == "yaffs2")
690 return Wipe_MTD();
691
692 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
693 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400694}
695
696bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400697 if (Backup_Method == FILES)
698 return Backup_Tar(backup_folder);
699 else if (Backup_Method == DD)
700 return Backup_DD(backup_folder);
701 else if (Backup_Method == FLASH_UTILS)
702 return Backup_Dump_Image(backup_folder);
703 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
704 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400705}
706
Dees_Troy43d8b002012-09-17 16:00:01 -0400707bool TWPartition::Check_MD5(string restore_folder) {
708 string Full_Filename;
709 char split_filename[512];
710 int index = 0;
711
712 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400713 if (!TWFunc::Path_Exists(Full_Filename)) {
714 // This is a split archive, we presume
715 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
716 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
717 if (TWFunc::Check_MD5(split_filename) == 0) {
718 LOGE("MD5 failed to match on '%s'.\n", split_filename);
719 return false;
720 }
721 index++;
722 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400723 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400724 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400725 } else {
726 // Single file archive
727 if (TWFunc::Check_MD5(Full_Filename) == 0) {
728 LOGE("MD5 failed to match on '%s'.\n", split_filename);
729 return false;
730 } else
731 return true;
732 }
733 return false;
734}
735
Dees_Troy51a0e822012-09-05 15:24:24 -0400736bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400737 if (Backup_Method == FILES)
738 return Restore_Tar(restore_folder);
739 else if (Backup_Method == DD)
740 return Restore_DD(restore_folder);
741 else if (Backup_Method == FLASH_UTILS)
742 return Restore_Flash_Image(restore_folder);
743 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
744 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400745}
746
747string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400748 if (Backup_Method == NONE)
749 return "none";
750 else if (Backup_Method == FILES)
751 return "files";
752 else if (Backup_Method == DD)
753 return "dd";
754 else if (Backup_Method == FLASH_UTILS)
755 return "flash_utils";
756 else
757 return "undefined";
758 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400759}
760
761bool TWPartition::Decrypt(string Password) {
762 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400763 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400764 return 1;
765}
766
767bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400768 bool Save_Data_Media = Has_Data_Media;
769
770 if (!UnMount(true))
771 return false;
772
773 Current_File_System = Fstab_File_System;
774 Is_Encrypted = false;
775 Is_Decrypted = false;
776 Decrypted_Block_Device = "";
777 Has_Data_Media = false;
778 if (Wipe()) {
779 Has_Data_Media = Save_Data_Media;
780 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
781 Recreate_Media_Folder();
782 }
783 return true;
784 } else {
785 Has_Data_Media = Save_Data_Media;
786 LOGE("Unable to format to remove encryption.\n");
787 return false;
788 }
789 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400790}
791
792void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400793 FILE *fp;
794 string blkCommand;
795 char blkOutput[255];
796 char* blk;
797 char* arg;
798 char* ptr;
799
800 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
801 return; // Running blkid on some mtd devices causes a massive crash
802
Dees_Troy38bd7602012-09-14 13:33:53 -0400803 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400804 if (!Is_Present)
805 return;
Dees_Troy51127312012-09-08 13:08:49 -0400806
Dees_Troy8170a922012-09-18 15:40:25 -0400807 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
808 system("rm /tmp/blkidoutput.txt");
809
810 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400811 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400812 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400813 if (fp == NULL)
814 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400815 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
816 {
817 blk = blkOutput;
818 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400819 while (*ptr > 32 && *ptr != ':') ptr++;
820 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400821 *ptr = 0;
822
823 // Increment by two, but verify that we don't hit a NULL
824 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400825 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400826
827 // Now, find the TYPE field
828 while (1)
829 {
830 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400831 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400832 if (*ptr != 0)
833 {
834 *ptr = 0;
835 ptr++;
836 }
837
838 if (strlen(arg) > 6)
839 {
840 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
841 }
842
843 if (*ptr == 0)
844 {
845 arg = NULL;
846 break;
847 }
848 }
849
850 if (arg && strlen(arg) > 7)
851 {
852 arg += 6; // Skip the TYPE=" portion
853 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
854 }
855 else
856 continue;
857
Dees_Troy63c8df72012-09-10 14:02:05 -0400858 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400859 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
860 Current_File_System = arg;
861 }
862 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400863 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400864 return;
865}
866
867bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400868 if (!UnMount(true))
869 return false;
870
Dees_Troy43d8b002012-09-17 16:00:01 -0400871 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400872 char command[512];
873
874 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
875 Find_Actual_Block_Device();
876 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
877 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400878 if (system(command) == 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400879 ui_print("Done.\n");
880 return true;
881 } else {
882 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
883 return false;
884 }
885 } else
886 return Wipe_RMRF();
887
888 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400889}
890
891bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400892 if (!UnMount(true))
893 return false;
894
Dees_Troy43d8b002012-09-17 16:00:01 -0400895 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400896 string Command;
897
898 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
899 Find_Actual_Block_Device();
900 Command = "make_ext4fs";
901 if (!Is_Decrypted && Length != 0) {
902 // Only use length if we're not decrypted
903 char len[32];
904 sprintf(len, "%i", Length);
905 Command += " -l ";
906 Command += len;
907 }
908 Command += " " + Actual_Block_Device;
909 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400910 if (system(Command.c_str()) == 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400911 ui_print("Done.\n");
912 return true;
913 } else {
914 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
915 return false;
916 }
917 } else
918 return Wipe_EXT23();
919
920 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400921}
922
923bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400924 char command[512];
925
926 if (Backup_Name == "and-sec") // Don't format if it's android secure
927 return Wipe_RMRF();
928
Dees_Troy43d8b002012-09-17 16:00:01 -0400929 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400930 if (!UnMount(true))
931 return false;
932
933 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
934 Find_Actual_Block_Device();
935 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400936 if (system(command) == 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400937 ui_print("Done.\n");
938 return true;
939 } else {
940 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
941 return false;
942 }
943 return true;
944 }
945 else
946 return Wipe_RMRF();
947
948 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400949}
950
Dees_Troy38bd7602012-09-14 13:33:53 -0400951bool TWPartition::Wipe_MTD() {
952 if (!UnMount(true))
953 return false;
954
955 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
956
957 mtd_scan_partitions();
958 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
959 if (mtd == NULL) {
960 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
961 return false;
962 }
963
964 MtdWriteContext* ctx = mtd_write_partition(mtd);
965 if (ctx == NULL) {
966 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
967 return false;
968 }
969 if (mtd_erase_blocks(ctx, -1) == -1) {
970 mtd_write_close(ctx);
971 LOGE("Failed to format '%s'", MTD_Name.c_str());
972 return false;
973 }
974 if (mtd_write_close(ctx) != 0) {
975 LOGE("Failed to close '%s'", MTD_Name.c_str());
976 return false;
977 }
978 ui_print("Done.\n");
979 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400980}
981
982bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400983 char cmd[512];
984
985 if (!Mount(true))
986 return false;
987
988 if (Backup_Name == "and-sec") {
989 ui_print("Using rm -rf on .android_secure\n");
990 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
991 } else {
992 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
993 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
994 }
995
996 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -0400997 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -0400998 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400999}
1000
1001bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001002 char cmd[256];
1003
1004 // This handles wiping data on devices with "sdcard" in /data/media
1005 if (!Mount(true))
1006 return false;
1007
1008 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001009 system("rm -f /data/*");
1010 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001011
1012 DIR* d;
1013 d = opendir("/data");
1014 if (d != NULL)
1015 {
1016 struct dirent* de;
1017 while ((de = readdir(d)) != NULL) {
1018 if (strcmp(de->d_name, "media") == 0) continue;
1019
1020 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001021 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001022 }
1023 closedir(d);
1024 }
1025 ui_print("Done.\n");
1026 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001027}
1028
1029bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001030 char back_name[255], split_index[5];
1031 string Full_FileName, Split_FileName, Tar_Args, Command;
1032 int use_compression, index, backup_count;
1033 struct stat st;
1034 unsigned long long total_bsize = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -04001035
1036 if (!Mount(true))
1037 return false;
1038
1039 ui_print("Backing up %s...\n", Display_Name.c_str());
1040
1041 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1042 if (use_compression)
1043 Tar_Args = "-cz";
1044 else
1045 Tar_Args = "-c";
1046
1047 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1048 Backup_FileName = back_name;
1049
Dees_Troy43d8b002012-09-17 16:00:01 -04001050 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1051 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001052 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
1053 sprintf(back_name, "%s", Mount_Point.c_str());
1054 backup_count = make_file_list(back_name);
1055 if (backup_count < 1) {
1056 LOGE("Error generating file list!\n");
1057 return false;
1058 }
1059 for (index=0; index<backup_count; index++) {
1060 sprintf(split_index, "%03i", index);
1061 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1062 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1063 LOGI("Backup command: '%s'\n", Command.c_str());
1064 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1065 system(Command.c_str()); // sending backup command formed earlier above
1066
1067 if (stat(Full_FileName.c_str(), &st) != 0 || st.st_size == 0) {
1068 LOGE("File size is zero bytes. Aborting...\n\n"); // oh noes! file size is 0, abort! abort!
1069 return false;
1070 }
1071 total_bsize += st.st_size;
1072 }
1073 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1074 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001075 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001076 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001077 if (Has_Data_Media)
1078 Command = "cd " + Mount_Point + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
1079 else
1080 Command = "cd " + Mount_Point + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
1081 LOGI("Backup command: '%s'\n", Command.c_str());
1082 system(Command.c_str());
1083 }
1084 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001085}
1086
1087bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001088 char back_name[255];
1089 string Full_FileName, Command;
1090 int use_compression;
1091
1092 ui_print("Backing up %s...\n", Display_Name.c_str());
1093
1094 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1095 Backup_FileName = back_name;
1096
1097 Full_FileName = backup_folder + "/" + Backup_FileName;
1098
1099 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1100 LOGI("Backup command: '%s'\n", Command.c_str());
1101 system(Command.c_str());
1102 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001103}
1104
1105bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001106 char back_name[255];
1107 string Full_FileName, Command;
1108 int use_compression;
1109
1110 ui_print("Backing up %s...\n", Display_Name.c_str());
1111
1112 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1113 Backup_FileName = back_name;
1114
1115 Full_FileName = backup_folder + "/" + Backup_FileName;
1116
1117 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1118 LOGI("Backup command: '%s'\n", Command.c_str());
1119 system(Command.c_str());
1120 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001121}
1122
1123bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001124 size_t first_period, second_period;
1125 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001126 int index = 0;
1127 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001128
1129 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1130
1131 // Parse backup filename to extract the file system before wiping
1132 first_period = Backup_FileName.find(".");
1133 if (first_period == string::npos) {
1134 LOGE("Unable to find file system (first period).\n");
1135 return false;
1136 }
1137 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1138 second_period = Restore_File_System.find(".");
1139 if (second_period == string::npos) {
1140 LOGE("Unable to find file system (second period).\n");
1141 return false;
1142 }
1143 Restore_File_System.resize(second_period);
1144 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1145 Current_File_System = Restore_File_System;
1146 ui_print("Wiping %s...\n", Display_Name.c_str());
1147 if (!Wipe())
1148 return false;
1149
1150 if (!Mount(true))
1151 return false;
1152
Dees_Troy43d8b002012-09-17 16:00:01 -04001153 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001154 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001155 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001156 // Backup is multiple archives
1157 LOGI("Backup is multiple archives.\n");
1158 sprintf(split_index, "%03i", index);
1159 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1160 while (TWFunc::Path_Exists(Full_FileName)) {
1161 ui_print("Restoring archive %i...\n", index + 1);
1162 Command = "cd " + Mount_Point + " && tar -xf '" + Full_FileName + "'";
1163 LOGI("Restore command: '%s'\n", Command.c_str());
1164 system(Command.c_str());
1165 index++;
1166 sprintf(split_index, "%03i", index);
1167 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1168 }
1169 if (index == 0) {
1170 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1171 return false;
1172 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001173 } else {
1174 Command = "cd " + Mount_Point + " && tar -xf '" + Full_FileName + "'";
1175 LOGI("Restore command: '%s'\n", Command.c_str());
1176 system(Command.c_str());
1177 }
1178 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001179}
1180
1181bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001182 string Full_FileName, Command;
1183
1184 ui_print("Restoring %s...\n", Display_Name.c_str());
1185 Full_FileName = restore_folder + "/" + Backup_FileName;
1186 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1187 LOGI("Restore command: '%s'\n", Command.c_str());
1188 system(Command.c_str());
1189 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001190}
1191
1192bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001193 string Full_FileName, Command;
1194
1195 ui_print("Restoring %s...\n", Display_Name.c_str());
1196 Full_FileName = restore_folder + "/" + Backup_FileName;
1197 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1198 Command = "erase_image " + MTD_Name;
1199 LOGI("Erase command: '%s'\n", Command.c_str());
1200 system(Command.c_str());
1201 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1202 LOGI("Restore command: '%s'\n", Command.c_str());
1203 system(Command.c_str());
1204 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001205}
Dees_Troy5bf43922012-09-07 16:07:55 -04001206
1207bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001208 bool ret = false;
1209
Dees_Troy5bf43922012-09-07 16:07:55 -04001210 if (!Can_Be_Mounted)
1211 return false;
1212
Dees_Troy38bd7602012-09-14 13:33:53 -04001213 if (Removable || Is_Encrypted) {
1214 if (!Mount(false))
1215 return true;
1216 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001217 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001218
1219 ret = Get_Size_Via_statfs(Display_Error);
1220 if (!ret || Size == 0)
1221 if (!Get_Size_Via_df(Display_Error))
1222 return false;
1223
Dees_Troy5bf43922012-09-07 16:07:55 -04001224 if (Has_Data_Media) {
1225 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001226 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001227 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1228 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001229 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001230 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001231 int bak = (int)(Backup_Size / 1048576LLU);
1232 int total = (int)(Size / 1048576LLU);
1233 int us = (int)(Used / 1048576LLU);
1234 int fre = (int)(Free / 1048576LLU);
1235 int datmed = (int)(data_media_used / 1048576LLU);
1236 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 -04001237 } else
1238 return false;
1239 }
1240 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001241}
Dees_Troy38bd7602012-09-14 13:33:53 -04001242
1243void TWPartition::Find_Actual_Block_Device(void) {
1244 if (Is_Decrypted) {
1245 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001246 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001247 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001248 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001249 Is_Present = true;
1250 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001251 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001252 Flip_Block_Device();
1253 Actual_Block_Device = Primary_Block_Device;
1254 Is_Present = true;
1255 } else
1256 Is_Present = false;
1257}
1258
1259void TWPartition::Recreate_Media_Folder(void) {
1260 string Command;
1261
1262 if (!Mount(true)) {
1263 LOGE("Unable to recreate /data/media folder.\n");
1264 } else {
1265 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001266 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001267 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001268 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001269 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001270 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001271 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001272}