blob: 5217227744c5f6abd9d30daba92573e02ced45e1 [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;
686 LOGI("Full_Filename: '%s'\n", Full_Filename.c_str());
687 if (!TWFunc::Path_Exists(Full_Filename)) {
688 // This is a split archive, we presume
689 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
690 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
691 if (TWFunc::Check_MD5(split_filename) == 0) {
692 LOGE("MD5 failed to match on '%s'.\n", split_filename);
693 return false;
694 }
695 index++;
696 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
697 LOGI("Full_Filename: '%s'\n", Full_Filename.c_str());
698 }
699 } else {
700 // Single file archive
701 if (TWFunc::Check_MD5(Full_Filename) == 0) {
702 LOGE("MD5 failed to match on '%s'.\n", split_filename);
703 return false;
704 } else
705 return true;
706 }
707 return false;
708}
709
Dees_Troy51a0e822012-09-05 15:24:24 -0400710bool TWPartition::Restore(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400711 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400712 if (Backup_Method == FILES)
713 return Restore_Tar(restore_folder);
714 else if (Backup_Method == DD)
715 return Restore_DD(restore_folder);
716 else if (Backup_Method == FLASH_UTILS)
717 return Restore_Flash_Image(restore_folder);
718 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
719 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400720}
721
722string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400723 if (Backup_Method == NONE)
724 return "none";
725 else if (Backup_Method == FILES)
726 return "files";
727 else if (Backup_Method == DD)
728 return "dd";
729 else if (Backup_Method == FLASH_UTILS)
730 return "flash_utils";
731 else
732 return "undefined";
733 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400734}
735
736bool TWPartition::Decrypt(string Password) {
737 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400738 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400739 return 1;
740}
741
742bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400743 bool Save_Data_Media = Has_Data_Media;
744
745 if (!UnMount(true))
746 return false;
747
748 Current_File_System = Fstab_File_System;
749 Is_Encrypted = false;
750 Is_Decrypted = false;
751 Decrypted_Block_Device = "";
752 Has_Data_Media = false;
753 if (Wipe()) {
754 Has_Data_Media = Save_Data_Media;
755 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
756 Recreate_Media_Folder();
757 }
758 return true;
759 } else {
760 Has_Data_Media = Save_Data_Media;
761 LOGE("Unable to format to remove encryption.\n");
762 return false;
763 }
764 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400765}
766
767void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400768 FILE *fp;
769 string blkCommand;
770 char blkOutput[255];
771 char* blk;
772 char* arg;
773 char* ptr;
774
775 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
776 return; // Running blkid on some mtd devices causes a massive crash
777
Dees_Troy38bd7602012-09-14 13:33:53 -0400778 Find_Actual_Block_Device();
779 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy51127312012-09-08 13:08:49 -0400780
Dees_Troy43d8b002012-09-17 16:00:01 -0400781 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400782 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy5bf43922012-09-07 16:07:55 -0400783 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
784 {
785 blk = blkOutput;
786 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400787 while (*ptr > 32 && *ptr != ':') ptr++;
788 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400789 *ptr = 0;
790
791 // Increment by two, but verify that we don't hit a NULL
792 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400793 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400794
795 // Now, find the TYPE field
796 while (1)
797 {
798 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400799 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400800 if (*ptr != 0)
801 {
802 *ptr = 0;
803 ptr++;
804 }
805
806 if (strlen(arg) > 6)
807 {
808 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
809 }
810
811 if (*ptr == 0)
812 {
813 arg = NULL;
814 break;
815 }
816 }
817
818 if (arg && strlen(arg) > 7)
819 {
820 arg += 6; // Skip the TYPE=" portion
821 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
822 }
823 else
824 continue;
825
Dees_Troy63c8df72012-09-10 14:02:05 -0400826 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400827 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
828 Current_File_System = arg;
829 }
830 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400831 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400832 return;
833}
834
835bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400836 if (!UnMount(true))
837 return false;
838
Dees_Troy43d8b002012-09-17 16:00:01 -0400839 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400840 char command[512];
841
842 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
843 Find_Actual_Block_Device();
844 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
845 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400846 if (system(command) == 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400847 ui_print("Done.\n");
848 return true;
849 } else {
850 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
851 return false;
852 }
853 } else
854 return Wipe_RMRF();
855
856 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400857}
858
859bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400860 if (!UnMount(true))
861 return false;
862
Dees_Troy43d8b002012-09-17 16:00:01 -0400863 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400864 string Command;
865
866 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
867 Find_Actual_Block_Device();
868 Command = "make_ext4fs";
869 if (!Is_Decrypted && Length != 0) {
870 // Only use length if we're not decrypted
871 char len[32];
872 sprintf(len, "%i", Length);
873 Command += " -l ";
874 Command += len;
875 }
876 Command += " " + Actual_Block_Device;
877 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400878 if (system(Command.c_str()) == 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_EXT23();
887
888 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400889}
890
891bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400892 char command[512];
893
894 if (Backup_Name == "and-sec") // Don't format if it's android secure
895 return Wipe_RMRF();
896
Dees_Troy43d8b002012-09-17 16:00:01 -0400897 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400898 if (!UnMount(true))
899 return false;
900
901 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
902 Find_Actual_Block_Device();
903 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400904 if (system(command) == 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400905 ui_print("Done.\n");
906 return true;
907 } else {
908 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
909 return false;
910 }
911 return true;
912 }
913 else
914 return Wipe_RMRF();
915
916 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400917}
918
Dees_Troy38bd7602012-09-14 13:33:53 -0400919bool TWPartition::Wipe_MTD() {
920 if (!UnMount(true))
921 return false;
922
923 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
924
925 mtd_scan_partitions();
926 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
927 if (mtd == NULL) {
928 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
929 return false;
930 }
931
932 MtdWriteContext* ctx = mtd_write_partition(mtd);
933 if (ctx == NULL) {
934 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
935 return false;
936 }
937 if (mtd_erase_blocks(ctx, -1) == -1) {
938 mtd_write_close(ctx);
939 LOGE("Failed to format '%s'", MTD_Name.c_str());
940 return false;
941 }
942 if (mtd_write_close(ctx) != 0) {
943 LOGE("Failed to close '%s'", MTD_Name.c_str());
944 return false;
945 }
946 ui_print("Done.\n");
947 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400948}
949
950bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400951 char cmd[512];
952
953 if (!Mount(true))
954 return false;
955
956 if (Backup_Name == "and-sec") {
957 ui_print("Using rm -rf on .android_secure\n");
958 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
959 } else {
960 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
961 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
962 }
963
964 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -0400965 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -0400966 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400967}
968
969bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400970 char cmd[256];
971
972 // This handles wiping data on devices with "sdcard" in /data/media
973 if (!Mount(true))
974 return false;
975
976 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -0400977 system("rm -f /data/*");
978 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -0400979
980 DIR* d;
981 d = opendir("/data");
982 if (d != NULL)
983 {
984 struct dirent* de;
985 while ((de = readdir(d)) != NULL) {
986 if (strcmp(de->d_name, "media") == 0) continue;
987
988 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -0400989 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -0400990 }
991 closedir(d);
992 }
993 ui_print("Done.\n");
994 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400995}
996
997bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400998 char back_name[255];
999 string Full_FileName, Tar_Args, Command;
1000 int use_compression;
1001
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
1016 Full_FileName = backup_folder + "/" + Backup_FileName;
1017 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1018 // This backup needs to be split into multiple archives
1019 LOGE("Multiple archive splitting is not implemented yet!\n");
1020 return false;
1021 } else {
1022 if (Has_Data_Media)
1023 Command = "cd " + Mount_Point + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
1024 else
1025 Command = "cd " + Mount_Point + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
1026 LOGI("Backup command: '%s'\n", Command.c_str());
1027 system(Command.c_str());
1028 }
1029 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001030}
1031
1032bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001033 char back_name[255];
1034 string Full_FileName, Command;
1035 int use_compression;
1036
1037 ui_print("Backing up %s...\n", Display_Name.c_str());
1038
1039 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1040 Backup_FileName = back_name;
1041
1042 Full_FileName = backup_folder + "/" + Backup_FileName;
1043
1044 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1045 LOGI("Backup command: '%s'\n", Command.c_str());
1046 system(Command.c_str());
1047 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001048}
1049
1050bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001051 char back_name[255];
1052 string Full_FileName, Command;
1053 int use_compression;
1054
1055 ui_print("Backing up %s...\n", Display_Name.c_str());
1056
1057 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1058 Backup_FileName = back_name;
1059
1060 Full_FileName = backup_folder + "/" + Backup_FileName;
1061
1062 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1063 LOGI("Backup command: '%s'\n", Command.c_str());
1064 system(Command.c_str());
1065 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001066}
1067
1068bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001069 size_t first_period, second_period;
1070 string Restore_File_System, Full_FileName, Command;
1071
1072 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1073
1074 // Parse backup filename to extract the file system before wiping
1075 first_period = Backup_FileName.find(".");
1076 if (first_period == string::npos) {
1077 LOGE("Unable to find file system (first period).\n");
1078 return false;
1079 }
1080 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1081 second_period = Restore_File_System.find(".");
1082 if (second_period == string::npos) {
1083 LOGE("Unable to find file system (second period).\n");
1084 return false;
1085 }
1086 Restore_File_System.resize(second_period);
1087 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1088 Current_File_System = Restore_File_System;
1089 ui_print("Wiping %s...\n", Display_Name.c_str());
1090 if (!Wipe())
1091 return false;
1092
1093 if (!Mount(true))
1094 return false;
1095
1096 Full_FileName = restore_folder + "/" + Backup_FileName;
1097 ui_print("Restoring %s...\n", Display_Name.c_str());
1098 if (!TWFunc::Path_Exists(Full_FileName)) {
1099 // This backup is multiple archives
1100 LOGE("Multiple archive not implemented yet.\n");
1101 return false;
1102 } else {
1103 Command = "cd " + Mount_Point + " && tar -xf '" + Full_FileName + "'";
1104 LOGI("Restore command: '%s'\n", Command.c_str());
1105 system(Command.c_str());
1106 }
1107 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001108}
1109
1110bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001111 string Full_FileName, Command;
1112
1113 ui_print("Restoring %s...\n", Display_Name.c_str());
1114 Full_FileName = restore_folder + "/" + Backup_FileName;
1115 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1116 LOGI("Restore command: '%s'\n", Command.c_str());
1117 system(Command.c_str());
1118 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001119}
1120
1121bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001122 string Full_FileName, Command;
1123
1124 ui_print("Restoring %s...\n", Display_Name.c_str());
1125 Full_FileName = restore_folder + "/" + Backup_FileName;
1126 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1127 Command = "erase_image " + MTD_Name;
1128 LOGI("Erase command: '%s'\n", Command.c_str());
1129 system(Command.c_str());
1130 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1131 LOGI("Restore command: '%s'\n", Command.c_str());
1132 system(Command.c_str());
1133 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001134}
Dees_Troy5bf43922012-09-07 16:07:55 -04001135
1136bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001137 bool ret = false;
1138
Dees_Troy5bf43922012-09-07 16:07:55 -04001139 if (!Can_Be_Mounted)
1140 return false;
1141
Dees_Troy38bd7602012-09-14 13:33:53 -04001142 if (Removable || Is_Encrypted) {
1143 if (!Mount(false))
1144 return true;
1145 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001146 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001147
1148 ret = Get_Size_Via_statfs(Display_Error);
1149 if (!ret || Size == 0)
1150 if (!Get_Size_Via_df(Display_Error))
1151 return false;
1152
Dees_Troy5bf43922012-09-07 16:07:55 -04001153 if (Has_Data_Media) {
1154 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001155 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001156 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1157 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001158 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001159 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001160 int bak = (int)(Backup_Size / 1048576LLU);
1161 int total = (int)(Size / 1048576LLU);
1162 int us = (int)(Used / 1048576LLU);
1163 int fre = (int)(Free / 1048576LLU);
1164 int datmed = (int)(data_media_used / 1048576LLU);
1165 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 -04001166 } else
1167 return false;
1168 }
1169 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001170}
Dees_Troy38bd7602012-09-14 13:33:53 -04001171
1172void TWPartition::Find_Actual_Block_Device(void) {
1173 if (Is_Decrypted) {
1174 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001175 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001176 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001177 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001178 Is_Present = true;
1179 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001180 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001181 Flip_Block_Device();
1182 Actual_Block_Device = Primary_Block_Device;
1183 Is_Present = true;
1184 } else
1185 Is_Present = false;
1186}
1187
1188void TWPartition::Recreate_Media_Folder(void) {
1189 string Command;
1190
1191 if (!Mount(true)) {
1192 LOGE("Unable to recreate /data/media folder.\n");
1193 } else {
1194 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001195 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001196 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001197 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001198 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001199 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001200 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001201}