blob: 27f855e5a8de14c61d8a83bbb48c4abba5e77ca1 [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_Troy5bf43922012-09-07 16:07:55 -040045}
Dees_Troy51a0e822012-09-05 15:24:24 -040046
47TWPartition::TWPartition(void) {
48 Can_Be_Mounted = false;
49 Can_Be_Wiped = false;
50 Wipe_During_Factory_Reset = false;
51 Wipe_Available_in_GUI = false;
52 Is_SubPartition = false;
53 SubPartition_Of = "";
54 Symlink_Path = "";
55 Symlink_Mount_Point = "";
56 Mount_Point = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040057 Actual_Block_Device = "";
58 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040059 Alternate_Block_Device = "";
60 Removable = false;
61 Is_Present = false;
62 Length = 0;
63 Size = 0;
64 Used = 0;
65 Free = 0;
66 Backup_Size = 0;
67 Can_Be_Encrypted = false;
68 Is_Encrypted = false;
69 Is_Decrypted = false;
70 Decrypted_Block_Device = "";
71 Display_Name = "";
72 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040073 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040074 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040075 Backup_Method = NONE;
76 Has_Data_Media = false;
77 Is_Storage = false;
78 Storage_Path = "";
79 Current_File_System = "";
80 Fstab_File_System = "";
81 Format_Block_Size = 0;
82}
83
84TWPartition::~TWPartition(void) {
85 // Do nothing
86}
87
Dees_Troy5bf43922012-09-07 16:07:55 -040088bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
89 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
90 int line_len = Line.size(), index = 0, item_index = 0;
91 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -040092 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -040093
94 strncpy(full_line, Line.c_str(), line_len);
95
Dees_Troy51127312012-09-08 13:08:49 -040096 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -040097 if (full_line[index] <= 32)
98 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -040099 }
100 string mount_pt(full_line);
101 Mount_Point = mount_pt;
102 index = Mount_Point.size();
103 while (index < line_len) {
104 while (index < line_len && full_line[index] == '\0')
105 index++;
106 if (index >= line_len)
107 continue;
108 ptr = full_line + index;
109 if (item_index == 0) {
110 // File System
111 Fstab_File_System = ptr;
112 Current_File_System = ptr;
113 item_index++;
114 } else if (item_index == 1) {
115 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400116 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
117 Primary_Block_Device = ptr;
118 Find_MTD_Block_Device(Primary_Block_Device);
119 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400120 if (Display_Error)
121 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
122 else
123 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
124 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400125 } else {
126 Primary_Block_Device = ptr;
127 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400128 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400129 item_index++;
130 } else if (item_index > 1) {
131 if (*ptr == '/') {
132 // Alternate Block Device
133 Alternate_Block_Device = ptr;
134 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
135 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
136 // Partition length
137 ptr += 7;
138 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400139 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
140 // Custom flags, save for later so that new values aren't overwritten by defaults
141 ptr += 6;
142 Flags = ptr;
Dees_Troy38bd7602012-09-14 13:33:53 -0400143 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
144 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400145 } else {
146 // Unhandled data
147 LOGI("Unhandled fstab information: '%s', %i\n", ptr, index);
148 }
149 }
150 while (index < line_len && full_line[index] != '\0')
151 index++;
152 }
153
154 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
155 if (Display_Error)
156 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
157 else
158 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
159 return 0;
160 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400161 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400162 Setup_File_System(Display_Error);
163 if (Mount_Point == "/system") {
164 Display_Name = "System";
165 Wipe_Available_in_GUI = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400166 MTD_Name = "system";
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 } else if (Mount_Point == "/data") {
168 Display_Name = "Data";
169 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400170 Wipe_During_Factory_Reset = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400171 MTD_Name = "userdata";
Dees_Troy5bf43922012-09-07 16:07:55 -0400172#ifdef RECOVERY_SDCARD_ON_DATA
173 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400174 Is_Storage = true;
175 Storage_Path = "/data/media";
Dees_Troy657c3092012-09-10 20:32:10 -0400176 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
177 Make_Dir("/emmc", Display_Error);
178 Symlink_Path = "/data/media";
179 Symlink_Mount_Point = "/emmc";
180 } else {
181 Make_Dir("/sdcard", Display_Error);
182 Symlink_Path = "/data/media";
183 Symlink_Mount_Point = "/sdcard";
184 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400185#endif
186#ifdef TW_INCLUDE_CRYPTO
187 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400188 char crypto_blkdev[255];
189 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
190 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400191 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400192 DataManager::SetValue(TW_IS_DECRYPTED, 1);
193 Is_Encrypted = true;
194 Is_Decrypted = true;
195 Decrypted_Block_Device = crypto_blkdev;
196 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
197 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400198 Is_Encrypted = true;
199 Is_Decrypted = false;
200 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
201 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
202 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400203 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400204#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400205 } else if (Mount_Point == "/cache") {
206 Display_Name = "Cache";
207 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400208 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400209 Update_Size(Display_Error);
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_Troy5bf43922012-09-07 16:07:55 -0400221 } else
222 Update_Size(Display_Error);
223 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400224 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 Setup_Image(Display_Error);
226 if (Mount_Point == "/boot") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400227 MTD_Name = "boot";
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 int backup_display_size = (int)(Backup_Size / 1048576LLU);
229 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
230 if (Backup_Size == 0) {
231 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
232 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
233 } else
234 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
235 } else if (Mount_Point == "/recovery") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400236 MTD_Name = "recovery";
Dees_Troy5bf43922012-09-07 16:07:55 -0400237 int backup_display_size = (int)(Backup_Size / 1048576LLU);
238 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
239 if (Backup_Size == 0) {
240 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
241 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
242 } else
243 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
244 }
245 }
246
Dees_Troy51127312012-09-08 13:08:49 -0400247 // Process any custom flags
248 if (Flags.size() > 0)
249 Process_Flags(Flags, Display_Error);
250
251 return true;
252}
253
254bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
255 char flags[MAX_FSTAB_LINE_LENGTH];
256 int flags_len, index = 0;
257 char* ptr;
258
259 strcpy(flags, Flags.c_str());
260 flags_len = Flags.size();
261 for (index = 0; index < flags_len; index++) {
262 if (flags[index] == ';')
263 flags[index] = '\0';
264 }
265
266 index = 0;
267 while (index < flags_len) {
268 while (index < flags_len && flags[index] == '\0')
269 index++;
270 if (index >= flags_len)
271 continue;
272 ptr = flags + index;
273 if (strcmp(ptr, "removable") == 0) {
274 Removable = true;
275 } else if (strcmp(ptr, "storage") == 0) {
276 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400277 } else if (strcmp(ptr, "canbewiped") == 0) {
278 Can_Be_Wiped = true;
279 } else if (strcmp(ptr, "wipeingui") == 0) {
280 Can_Be_Wiped = true;
281 Wipe_Available_in_GUI = true;
282 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
283 Can_Be_Wiped = true;
284 Wipe_Available_in_GUI = true;
285 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400286 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
287 ptr += 13;
288 Is_SubPartition = true;
289 SubPartition_Of = ptr;
290 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
291 ptr += 8;
292 Symlink_Path = ptr;
293 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
294 ptr += 8;
295 Display_Name = ptr;
296 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
297 ptr += 10;
298 Format_Block_Size = atoi(ptr);
299 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
300 ptr += 7;
301 Length = atoi(ptr);
302 } else {
303 if (Display_Error)
304 LOGE("Unhandled flag: '%s'\n", ptr);
305 else
306 LOGI("Unhandled flag: '%s'\n", ptr);
307 }
308 while (index < flags_len && flags[index] != '\0')
309 index++;
310 }
311 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400312}
313
Dees_Troy5bf43922012-09-07 16:07:55 -0400314bool TWPartition::Is_File_System(string File_System) {
315 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400316 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400317 File_System == "ext4" ||
318 File_System == "vfat" ||
319 File_System == "ntfs" ||
320 File_System == "yaffs2" ||
321 File_System == "auto")
322 return true;
323 else
324 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400325}
326
Dees_Troy5bf43922012-09-07 16:07:55 -0400327bool TWPartition::Is_Image(string File_System) {
328 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400329 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400330 return true;
331 else
332 return false;
333}
334
Dees_Troy51127312012-09-08 13:08:49 -0400335bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400336 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400337 if (mkdir(Path.c_str(), 0777) == -1) {
338 if (Display_Error)
339 LOGE("Can not create '%s' folder.\n", Path.c_str());
340 else
341 LOGI("Can not create '%s' folder.\n", Path.c_str());
342 return false;
343 } else {
344 LOGI("Created '%s' folder.\n", Path.c_str());
345 return true;
346 }
347 }
348 return true;
349}
350
Dees_Troy5bf43922012-09-07 16:07:55 -0400351void TWPartition::Setup_File_System(bool Display_Error) {
352 struct statfs st;
353
354 Can_Be_Mounted = true;
355 Can_Be_Wiped = true;
356
Dees_Troy5bf43922012-09-07 16:07:55 -0400357 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400358 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400359 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
360 Backup_Name = Display_Name;
361 Backup_Method = FILES;
362}
363
364void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400365 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
366 Backup_Name = Display_Name;
367 if (Fstab_File_System == "emmc")
368 Backup_Method = DD;
369 else if (Fstab_File_System == "mtd")
370 Backup_Method = FLASH_UTILS;
371 else
372 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
373 if (Find_Partition_Size()) {
374 Used = Size;
375 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400376 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400377 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400378 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400379 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400380 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400381 }
382}
383
Dees_Troy5bf43922012-09-07 16:07:55 -0400384void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
385 char device[512], realDevice[512];
386
387 strcpy(device, Block.c_str());
388 memset(realDevice, 0, sizeof(realDevice));
389 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
390 {
391 strcpy(device, realDevice);
392 memset(realDevice, 0, sizeof(realDevice));
393 }
394
395 if (device[0] != '/') {
396 if (Display_Error)
397 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
398 else
399 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
400 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400401 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400402 Block = device;
403 return;
404 }
405}
406
Dees_Troy38bd7602012-09-14 13:33:53 -0400407bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
408 FILE *fp = NULL;
409 char line[255];
410
411 fp = fopen("/proc/mtd", "rt");
412 if (fp == NULL) {
413 LOGE("Device does not support /proc/mtd\n");
414 return false;
415 }
416
417 while (fgets(line, sizeof(line), fp) != NULL)
418 {
419 char device[32], label[32];
420 unsigned long size = 0;
421 char* fstype = NULL;
422 int deviceId;
423
424 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
425
426 // Skip header and blank lines
427 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
428 continue;
429
430 // Strip off the trailing " from the label
431 label[strlen(label)-1] = '\0';
432
433 if (strcmp(label, MTD_Name.c_str()) == 0) {
434 // We found our device
435 // Strip off the trailing : from the device
436 device[strlen(device)-1] = '\0';
437 if (sscanf(device,"mtd%d", &deviceId) == 1) {
438 sprintf(device, "/dev/block/mtdblock%d", deviceId);
439 Primary_Block_Device = device;
440 }
441 }
442 }
443 fclose(fp);
444
445 return false;
446}
447
Dees_Troy51127312012-09-08 13:08:49 -0400448bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
449 struct statfs st;
450 string Local_Path = Mount_Point + "/.";
451
452 if (!Mount(Display_Error))
453 return false;
454
455 if (statfs(Local_Path.c_str(), &st) != 0) {
456 if (!Removable) {
457 if (Display_Error)
458 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
459 else
460 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
461 }
462 return false;
463 }
464 Size = (st.f_blocks * st.f_bsize);
465 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
466 Free = (st.f_bfree * st.f_bsize);
467 Backup_Size = Used;
468 return true;
469}
470
471bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400472 FILE* fp;
473 char command[255], line[512];
474 int include_block = 1;
475 unsigned int min_len;
476
477 if (!Mount(Display_Error))
478 return false;
479
Dees_Troy38bd7602012-09-14 13:33:53 -0400480 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400481 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400482 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400483 fp = fopen("/tmp/dfoutput.txt", "rt");
484 if (fp == NULL) {
485 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400486 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400487 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400488
489 while (fgets(line, sizeof(line), fp) != NULL)
490 {
491 unsigned long blocks, used, available;
492 char device[64];
493 char tmpString[64];
494
495 if (strncmp(line, "Filesystem", 10) == 0)
496 continue;
497 if (strlen(line) < min_len) {
498 include_block = 0;
499 continue;
500 }
501 if (include_block) {
502 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
503 } else {
504 // The device block string is so long that the df information is on the next line
505 int space_count = 0;
506 while (tmpString[space_count] == 32)
507 space_count++;
508 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
509 }
510
511 // Adjust block size to byte size
512 Size = blocks * 1024ULL;
513 Used = used * 1024ULL;
514 Free = available * 1024ULL;
515 Backup_Size = Used;
516 }
517 fclose(fp);
518 return true;
519}
520
Dees_Troy5bf43922012-09-07 16:07:55 -0400521bool TWPartition::Find_Partition_Size(void) {
522 FILE* fp;
523 char line[512];
524 string tmpdevice;
525
526 // In this case, we'll first get the partitions we care about (with labels)
527 fp = fopen("/proc/partitions", "rt");
528 if (fp == NULL)
529 return false;
530
531 while (fgets(line, sizeof(line), fp) != NULL)
532 {
533 unsigned long major, minor, blocks;
534 char device[512];
535 char tmpString[64];
536
Dees_Troy63c8df72012-09-10 14:02:05 -0400537 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400538 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
539
540 tmpdevice = "/dev/block/";
541 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400542 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400543 // Adjust block size to byte size
544 Size = blocks * 1024ULL;
545 fclose(fp);
546 return true;
547 }
548 }
549 fclose(fp);
550 return false;
551}
552
Dees_Troy5bf43922012-09-07 16:07:55 -0400553void TWPartition::Flip_Block_Device(void) {
554 string temp;
555
556 temp = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400557 Primary_Block_Device = Alternate_Block_Device;
Dees_Troy5bf43922012-09-07 16:07:55 -0400558 Alternate_Block_Device = temp;
559}
560
561bool TWPartition::Is_Mounted(void) {
562 if (!Can_Be_Mounted)
563 return false;
564
565 struct stat st1, st2;
566 string test_path;
567
568 // Check to see if the mount point directory exists
569 test_path = Mount_Point + "/.";
570 if (stat(test_path.c_str(), &st1) != 0) return false;
571
572 // Check to see if the directory above the mount point exists
573 test_path = Mount_Point + "/../.";
574 if (stat(test_path.c_str(), &st2) != 0) return false;
575
576 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
577 int ret = (st1.st_dev != st2.st_dev) ? true : false;
578
579 return ret;
580}
581
582bool TWPartition::Mount(bool Display_Error) {
583 if (Is_Mounted()) {
584 return true;
585 } else if (!Can_Be_Mounted) {
586 return false;
587 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400588
589 Find_Actual_Block_Device();
590
591 // Check the current file system before mounting
592 Check_FS_Type();
593
594 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
595 if (Display_Error)
596 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
597 else
598 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
599 return false;
600 } else {
601 if (Removable)
602 Update_Size(Display_Error);
603
604 if (!Symlink_Mount_Point.empty()) {
605 string Command;
606
607 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400608 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400609 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400610 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400611 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400612 return true;
613}
614
615bool TWPartition::UnMount(bool Display_Error) {
616 if (Is_Mounted()) {
617 int never_unmount_system;
618
619 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
620 if (never_unmount_system == 1 && Mount_Point == "/system")
621 return true; // Never unmount system if you're not supposed to unmount it
622
Dees_Troy38bd7602012-09-14 13:33:53 -0400623 if (!Symlink_Mount_Point.empty())
624 umount(Symlink_Mount_Point.c_str());
625
Dees_Troy5bf43922012-09-07 16:07:55 -0400626 if (umount(Mount_Point.c_str()) != 0) {
627 if (Display_Error)
628 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
629 else
630 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
631 return false;
632 } else
633 return true;
634 } else {
635 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400636 }
637}
638
639bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400640 if (!Can_Be_Wiped) {
641 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
642 return false;
643 }
644
645 if (Has_Data_Media)
646 return Wipe_Data_Without_Wiping_Media();
647
648 int check;
649 DataManager::GetValue(TW_RM_RF_VAR, check);
650 if (check)
651 return Wipe_RMRF();
652
653 if (Current_File_System == "ext4")
654 return Wipe_EXT4();
655
656 if (Current_File_System == "ext2" || Current_File_System == "ext3")
657 return Wipe_EXT23();
658
659 if (Current_File_System == "vfat")
660 return Wipe_FAT();
661
662 if (Current_File_System == "yaffs2")
663 return Wipe_MTD();
664
665 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
666 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400667}
668
669bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400670 if (Backup_Method == FILES)
671 return Backup_Tar(backup_folder);
672 else if (Backup_Method == DD)
673 return Backup_DD(backup_folder);
674 else if (Backup_Method == FLASH_UTILS)
675 return Backup_Dump_Image(backup_folder);
676 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
677 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400678}
679
Dees_Troy43d8b002012-09-17 16:00:01 -0400680bool TWPartition::Check_MD5(string restore_folder) {
681 string Full_Filename;
682 char split_filename[512];
683 int index = 0;
684
685 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400686 if (!TWFunc::Path_Exists(Full_Filename)) {
687 // This is a split archive, we presume
688 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
689 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
690 if (TWFunc::Check_MD5(split_filename) == 0) {
691 LOGE("MD5 failed to match on '%s'.\n", split_filename);
692 return false;
693 }
694 index++;
695 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400696 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400697 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400698 } else {
699 // Single file archive
700 if (TWFunc::Check_MD5(Full_Filename) == 0) {
701 LOGE("MD5 failed to match on '%s'.\n", split_filename);
702 return false;
703 } else
704 return true;
705 }
706 return false;
707}
708
Dees_Troy51a0e822012-09-05 15:24:24 -0400709bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400710 if (Backup_Method == FILES)
711 return Restore_Tar(restore_folder);
712 else if (Backup_Method == DD)
713 return Restore_DD(restore_folder);
714 else if (Backup_Method == FLASH_UTILS)
715 return Restore_Flash_Image(restore_folder);
716 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
717 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400718}
719
720string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400721 if (Backup_Method == NONE)
722 return "none";
723 else if (Backup_Method == FILES)
724 return "files";
725 else if (Backup_Method == DD)
726 return "dd";
727 else if (Backup_Method == FLASH_UTILS)
728 return "flash_utils";
729 else
730 return "undefined";
731 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400732}
733
734bool TWPartition::Decrypt(string Password) {
735 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400736 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400737 return 1;
738}
739
740bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400741 bool Save_Data_Media = Has_Data_Media;
742
743 if (!UnMount(true))
744 return false;
745
746 Current_File_System = Fstab_File_System;
747 Is_Encrypted = false;
748 Is_Decrypted = false;
749 Decrypted_Block_Device = "";
750 Has_Data_Media = false;
751 if (Wipe()) {
752 Has_Data_Media = Save_Data_Media;
753 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
754 Recreate_Media_Folder();
755 }
756 return true;
757 } else {
758 Has_Data_Media = Save_Data_Media;
759 LOGE("Unable to format to remove encryption.\n");
760 return false;
761 }
762 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400763}
764
765void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400766 FILE *fp;
767 string blkCommand;
768 char blkOutput[255];
769 char* blk;
770 char* arg;
771 char* ptr;
772
773 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
774 return; // Running blkid on some mtd devices causes a massive crash
775
Dees_Troy38bd7602012-09-14 13:33:53 -0400776 Find_Actual_Block_Device();
777 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy51127312012-09-08 13:08:49 -0400778
Dees_Troy43d8b002012-09-17 16:00:01 -0400779 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400780 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy5bf43922012-09-07 16:07:55 -0400781 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
782 {
783 blk = blkOutput;
784 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400785 while (*ptr > 32 && *ptr != ':') ptr++;
786 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400787 *ptr = 0;
788
789 // Increment by two, but verify that we don't hit a NULL
790 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400791 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400792
793 // Now, find the TYPE field
794 while (1)
795 {
796 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400797 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400798 if (*ptr != 0)
799 {
800 *ptr = 0;
801 ptr++;
802 }
803
804 if (strlen(arg) > 6)
805 {
806 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
807 }
808
809 if (*ptr == 0)
810 {
811 arg = NULL;
812 break;
813 }
814 }
815
816 if (arg && strlen(arg) > 7)
817 {
818 arg += 6; // Skip the TYPE=" portion
819 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
820 }
821 else
822 continue;
823
Dees_Troy63c8df72012-09-10 14:02:05 -0400824 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400825 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
826 Current_File_System = arg;
827 }
828 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400829 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400830 return;
831}
832
833bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400834 if (!UnMount(true))
835 return false;
836
Dees_Troy43d8b002012-09-17 16:00:01 -0400837 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400838 char command[512];
839
840 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
841 Find_Actual_Block_Device();
842 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
843 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400844 if (system(command) == 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400845 ui_print("Done.\n");
846 return true;
847 } else {
848 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
849 return false;
850 }
851 } else
852 return Wipe_RMRF();
853
854 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400855}
856
857bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400858 if (!UnMount(true))
859 return false;
860
Dees_Troy43d8b002012-09-17 16:00:01 -0400861 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400862 string Command;
863
864 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
865 Find_Actual_Block_Device();
866 Command = "make_ext4fs";
867 if (!Is_Decrypted && Length != 0) {
868 // Only use length if we're not decrypted
869 char len[32];
870 sprintf(len, "%i", Length);
871 Command += " -l ";
872 Command += len;
873 }
874 Command += " " + Actual_Block_Device;
875 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400876 if (system(Command.c_str()) == 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400877 ui_print("Done.\n");
878 return true;
879 } else {
880 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
881 return false;
882 }
883 } else
884 return Wipe_EXT23();
885
886 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400887}
888
889bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400890 char command[512];
891
892 if (Backup_Name == "and-sec") // Don't format if it's android secure
893 return Wipe_RMRF();
894
Dees_Troy43d8b002012-09-17 16:00:01 -0400895 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400896 if (!UnMount(true))
897 return false;
898
899 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
900 Find_Actual_Block_Device();
901 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400902 if (system(command) == 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400903 ui_print("Done.\n");
904 return true;
905 } else {
906 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
907 return false;
908 }
909 return true;
910 }
911 else
912 return Wipe_RMRF();
913
914 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400915}
916
Dees_Troy38bd7602012-09-14 13:33:53 -0400917bool TWPartition::Wipe_MTD() {
918 if (!UnMount(true))
919 return false;
920
921 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
922
923 mtd_scan_partitions();
924 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
925 if (mtd == NULL) {
926 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
927 return false;
928 }
929
930 MtdWriteContext* ctx = mtd_write_partition(mtd);
931 if (ctx == NULL) {
932 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
933 return false;
934 }
935 if (mtd_erase_blocks(ctx, -1) == -1) {
936 mtd_write_close(ctx);
937 LOGE("Failed to format '%s'", MTD_Name.c_str());
938 return false;
939 }
940 if (mtd_write_close(ctx) != 0) {
941 LOGE("Failed to close '%s'", MTD_Name.c_str());
942 return false;
943 }
944 ui_print("Done.\n");
945 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400946}
947
948bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400949 char cmd[512];
950
951 if (!Mount(true))
952 return false;
953
954 if (Backup_Name == "and-sec") {
955 ui_print("Using rm -rf on .android_secure\n");
956 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
957 } else {
958 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
959 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
960 }
961
962 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -0400963 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -0400964 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400965}
966
967bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400968 char cmd[256];
969
970 // This handles wiping data on devices with "sdcard" in /data/media
971 if (!Mount(true))
972 return false;
973
974 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400975 system("rm -f /data/*");
976 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -0400977
978 DIR* d;
979 d = opendir("/data");
980 if (d != NULL)
981 {
982 struct dirent* de;
983 while ((de = readdir(d)) != NULL) {
984 if (strcmp(de->d_name, "media") == 0) continue;
985
986 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400987 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -0400988 }
989 closedir(d);
990 }
991 ui_print("Done.\n");
992 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400993}
994
995bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -0400996 char back_name[255], split_index[5];
997 string Full_FileName, Split_FileName, Tar_Args, Command;
998 int use_compression, index, backup_count;
999 struct stat st;
1000 unsigned long long total_bsize = 0;
Dees_Troy43d8b002012-09-17 16:00:01 -04001001
1002 if (!Mount(true))
1003 return false;
1004
1005 ui_print("Backing up %s...\n", Display_Name.c_str());
1006
1007 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1008 if (use_compression)
1009 Tar_Args = "-cz";
1010 else
1011 Tar_Args = "-c";
1012
1013 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1014 Backup_FileName = back_name;
1015
Dees_Troy43d8b002012-09-17 16:00:01 -04001016 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1017 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001018 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
1019 sprintf(back_name, "%s", Mount_Point.c_str());
1020 backup_count = make_file_list(back_name);
1021 if (backup_count < 1) {
1022 LOGE("Error generating file list!\n");
1023 return false;
1024 }
1025 for (index=0; index<backup_count; index++) {
1026 sprintf(split_index, "%03i", index);
1027 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1028 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1029 LOGI("Backup command: '%s'\n", Command.c_str());
1030 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1031 system(Command.c_str()); // sending backup command formed earlier above
1032
1033 if (stat(Full_FileName.c_str(), &st) != 0 || st.st_size == 0) {
1034 LOGE("File size is zero bytes. Aborting...\n\n"); // oh noes! file size is 0, abort! abort!
1035 return false;
1036 }
1037 total_bsize += st.st_size;
1038 }
1039 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1040 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001041 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001042 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001043 if (Has_Data_Media)
1044 Command = "cd " + Mount_Point + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
1045 else
1046 Command = "cd " + Mount_Point + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
1047 LOGI("Backup command: '%s'\n", Command.c_str());
1048 system(Command.c_str());
1049 }
1050 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001051}
1052
1053bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001054 char back_name[255];
1055 string Full_FileName, Command;
1056 int use_compression;
1057
1058 ui_print("Backing up %s...\n", Display_Name.c_str());
1059
1060 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1061 Backup_FileName = back_name;
1062
1063 Full_FileName = backup_folder + "/" + Backup_FileName;
1064
1065 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1066 LOGI("Backup command: '%s'\n", Command.c_str());
1067 system(Command.c_str());
1068 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001069}
1070
1071bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001072 char back_name[255];
1073 string Full_FileName, Command;
1074 int use_compression;
1075
1076 ui_print("Backing up %s...\n", Display_Name.c_str());
1077
1078 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1079 Backup_FileName = back_name;
1080
1081 Full_FileName = backup_folder + "/" + Backup_FileName;
1082
1083 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1084 LOGI("Backup command: '%s'\n", Command.c_str());
1085 system(Command.c_str());
1086 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001087}
1088
1089bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001090 size_t first_period, second_period;
1091 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001092 int index = 0;
1093 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001094
1095 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1096
1097 // Parse backup filename to extract the file system before wiping
1098 first_period = Backup_FileName.find(".");
1099 if (first_period == string::npos) {
1100 LOGE("Unable to find file system (first period).\n");
1101 return false;
1102 }
1103 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1104 second_period = Restore_File_System.find(".");
1105 if (second_period == string::npos) {
1106 LOGE("Unable to find file system (second period).\n");
1107 return false;
1108 }
1109 Restore_File_System.resize(second_period);
1110 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1111 Current_File_System = Restore_File_System;
1112 ui_print("Wiping %s...\n", Display_Name.c_str());
1113 if (!Wipe())
1114 return false;
1115
1116 if (!Mount(true))
1117 return false;
1118
Dees_Troy43d8b002012-09-17 16:00:01 -04001119 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001120 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001121 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001122 // Backup is multiple archives
1123 LOGI("Backup is multiple archives.\n");
1124 sprintf(split_index, "%03i", index);
1125 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1126 while (TWFunc::Path_Exists(Full_FileName)) {
1127 ui_print("Restoring archive %i...\n", index + 1);
1128 Command = "cd " + Mount_Point + " && tar -xf '" + Full_FileName + "'";
1129 LOGI("Restore command: '%s'\n", Command.c_str());
1130 system(Command.c_str());
1131 index++;
1132 sprintf(split_index, "%03i", index);
1133 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1134 }
1135 if (index == 0) {
1136 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1137 return false;
1138 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001139 } else {
1140 Command = "cd " + Mount_Point + " && tar -xf '" + Full_FileName + "'";
1141 LOGI("Restore command: '%s'\n", Command.c_str());
1142 system(Command.c_str());
1143 }
1144 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001145}
1146
1147bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001148 string Full_FileName, Command;
1149
1150 ui_print("Restoring %s...\n", Display_Name.c_str());
1151 Full_FileName = restore_folder + "/" + Backup_FileName;
1152 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1153 LOGI("Restore command: '%s'\n", Command.c_str());
1154 system(Command.c_str());
1155 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001156}
1157
1158bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001159 string Full_FileName, Command;
1160
1161 ui_print("Restoring %s...\n", Display_Name.c_str());
1162 Full_FileName = restore_folder + "/" + Backup_FileName;
1163 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1164 Command = "erase_image " + MTD_Name;
1165 LOGI("Erase command: '%s'\n", Command.c_str());
1166 system(Command.c_str());
1167 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1168 LOGI("Restore command: '%s'\n", Command.c_str());
1169 system(Command.c_str());
1170 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001171}
Dees_Troy5bf43922012-09-07 16:07:55 -04001172
1173bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001174 bool ret = false;
1175
Dees_Troy5bf43922012-09-07 16:07:55 -04001176 if (!Can_Be_Mounted)
1177 return false;
1178
Dees_Troy38bd7602012-09-14 13:33:53 -04001179 if (Removable || Is_Encrypted) {
1180 if (!Mount(false))
1181 return true;
1182 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001183 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001184
1185 ret = Get_Size_Via_statfs(Display_Error);
1186 if (!ret || Size == 0)
1187 if (!Get_Size_Via_df(Display_Error))
1188 return false;
1189
Dees_Troy5bf43922012-09-07 16:07:55 -04001190 if (Has_Data_Media) {
1191 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001192 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001193 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1194 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001195 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001196 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001197 int bak = (int)(Backup_Size / 1048576LLU);
1198 int total = (int)(Size / 1048576LLU);
1199 int us = (int)(Used / 1048576LLU);
1200 int fre = (int)(Free / 1048576LLU);
1201 int datmed = (int)(data_media_used / 1048576LLU);
1202 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 -04001203 } else
1204 return false;
1205 }
1206 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001207}
Dees_Troy38bd7602012-09-14 13:33:53 -04001208
1209void TWPartition::Find_Actual_Block_Device(void) {
1210 if (Is_Decrypted) {
1211 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001212 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001213 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001214 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001215 Is_Present = true;
1216 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001217 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001218 Flip_Block_Device();
1219 Actual_Block_Device = Primary_Block_Device;
1220 Is_Present = true;
1221 } else
1222 Is_Present = false;
1223}
1224
1225void TWPartition::Recreate_Media_Folder(void) {
1226 string Command;
1227
1228 if (!Mount(true)) {
1229 LOGE("Unable to recreate /data/media folder.\n");
1230 } else {
1231 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001232 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001233 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001234 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001235 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001236 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001237 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001238}