blob: 6f213091190934e6f3e87375a65bd63521f3c703 [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001/* Partition class for TWRP
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 * The code was written from scratch by Dees_Troy dees_troy at
18 * yahoo
19 *
20 * Copyright (c) 2012
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/stat.h>
27#include <sys/vfs.h>
Dees_Troy5bf43922012-09-07 16:07:55 -040028#include <sys/mount.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040029#include <unistd.h>
Dees_Troy51127312012-09-08 13:08:49 -040030#include <dirent.h>
Dees_Troy51a0e822012-09-05 15:24:24 -040031
Dees_Troy657c3092012-09-10 20:32:10 -040032#ifdef TW_INCLUDE_CRYPTO
33 #include "cutils/properties.h"
34#endif
35
Dees_Troy51a0e822012-09-05 15:24:24 -040036#include "variables.h"
37#include "common.h"
38#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040039#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040040#include "twrp-functions.hpp"
Dees_Troy9df963c2012-09-26 08:58:12 -040041#include "makelist.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040043 #include "mtdutils/mtdutils.h"
44 #include "mtdutils/mounts.h"
Dees_Troyc51f1f92012-09-20 15:32:13 -040045 #include "extra-functions.h"
Dees_Troy5bf43922012-09-07 16:07:55 -040046}
Dees_Troy51a0e822012-09-05 15:24:24 -040047
48TWPartition::TWPartition(void) {
49 Can_Be_Mounted = false;
50 Can_Be_Wiped = false;
51 Wipe_During_Factory_Reset = false;
52 Wipe_Available_in_GUI = false;
53 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040054 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040055 SubPartition_Of = "";
56 Symlink_Path = "";
57 Symlink_Mount_Point = "";
58 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040059 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040060 Actual_Block_Device = "";
61 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040062 Alternate_Block_Device = "";
63 Removable = false;
64 Is_Present = false;
65 Length = 0;
66 Size = 0;
67 Used = 0;
68 Free = 0;
69 Backup_Size = 0;
70 Can_Be_Encrypted = false;
71 Is_Encrypted = false;
72 Is_Decrypted = false;
73 Decrypted_Block_Device = "";
74 Display_Name = "";
75 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040076 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040077 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040078 Backup_Method = NONE;
79 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040080 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040081 Is_Storage = false;
82 Storage_Path = "";
83 Current_File_System = "";
84 Fstab_File_System = "";
85 Format_Block_Size = 0;
86}
87
88TWPartition::~TWPartition(void) {
89 // Do nothing
90}
91
Dees_Troy5bf43922012-09-07 16:07:55 -040092bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
93 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
94 int line_len = Line.size(), index = 0, item_index = 0;
95 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -040096 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -040097
98 strncpy(full_line, Line.c_str(), line_len);
99
Dees_Troy51127312012-09-08 13:08:49 -0400100 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400101 if (full_line[index] <= 32)
102 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400103 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400104 Mount_Point = full_line;
105 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400106 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400107 index = Mount_Point.size();
108 while (index < line_len) {
109 while (index < line_len && full_line[index] == '\0')
110 index++;
111 if (index >= line_len)
112 continue;
113 ptr = full_line + index;
114 if (item_index == 0) {
115 // File System
116 Fstab_File_System = ptr;
117 Current_File_System = ptr;
118 item_index++;
119 } else if (item_index == 1) {
120 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400121 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
122 Primary_Block_Device = ptr;
123 Find_MTD_Block_Device(Primary_Block_Device);
124 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400125 if (Display_Error)
126 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
127 else
128 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
129 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400130 } else {
131 Primary_Block_Device = ptr;
132 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400133 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400134 item_index++;
135 } else if (item_index > 1) {
136 if (*ptr == '/') {
137 // Alternate Block Device
138 Alternate_Block_Device = ptr;
139 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
140 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
141 // Partition length
142 ptr += 7;
143 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400144 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
145 // Custom flags, save for later so that new values aren't overwritten by defaults
146 ptr += 6;
147 Flags = ptr;
Dees_Troy38bd7602012-09-14 13:33:53 -0400148 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
149 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400150 } else {
151 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400152 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400153 }
154 }
155 while (index < line_len && full_line[index] != '\0')
156 index++;
157 }
158
159 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
160 if (Display_Error)
161 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
162 else
163 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
164 return 0;
165 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400166 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400167 Setup_File_System(Display_Error);
168 if (Mount_Point == "/system") {
169 Display_Name = "System";
170 Wipe_Available_in_GUI = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400171 MTD_Name = "system";
Dees_Troy5bf43922012-09-07 16:07:55 -0400172 } else if (Mount_Point == "/data") {
173 Display_Name = "Data";
174 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400175 Wipe_During_Factory_Reset = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400176 MTD_Name = "userdata";
Dees_Troy5bf43922012-09-07 16:07:55 -0400177#ifdef RECOVERY_SDCARD_ON_DATA
178 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400179 Is_Storage = true;
180 Storage_Path = "/data/media";
Dees_Troyb46a6842012-09-25 11:06:46 -0400181 Recreate_Media_Folder();
Dees_Troy657c3092012-09-10 20:32:10 -0400182 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
183 Make_Dir("/emmc", Display_Error);
184 Symlink_Path = "/data/media";
185 Symlink_Mount_Point = "/emmc";
186 } else {
187 Make_Dir("/sdcard", Display_Error);
188 Symlink_Path = "/data/media";
189 Symlink_Mount_Point = "/sdcard";
190 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400191#endif
192#ifdef TW_INCLUDE_CRYPTO
193 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400194 char crypto_blkdev[255];
195 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
196 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400197 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400198 DataManager::SetValue(TW_IS_DECRYPTED, 1);
199 Is_Encrypted = true;
200 Is_Decrypted = true;
201 Decrypted_Block_Device = crypto_blkdev;
202 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
203 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400204 Is_Encrypted = true;
205 Is_Decrypted = false;
206 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
207 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
208 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400209 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400210#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400211 } else if (Mount_Point == "/cache") {
212 Display_Name = "Cache";
213 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400214 Wipe_During_Factory_Reset = true;
Dees_Troy38bd7602012-09-14 13:33:53 -0400215 MTD_Name = "cache";
Dees_Troyb46a6842012-09-25 11:06:46 -0400216 if (!TWFunc::Path_Exists("/cache/recovery")) {
217 LOGI("Recreating /cache/recovery folder.\n");
218 TWFunc::Recursive_Mkdir("/cache/recovery");
219 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400220 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400221 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 Display_Name = "DataData";
223 Is_SubPartition = true;
224 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 DataManager::SetValue(TW_HAS_DATADATA, 1);
226 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400227 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400228 Display_Name = "SD-Ext";
229 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400230 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400231 }
232#ifdef TW_EXTERNAL_STORAGE_PATH
233 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
234 Is_Storage = true;
235 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400236 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400237 }
238#else
239 if (Mount_Point == "/sdcard") {
240 Is_Storage = true;
241 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400242 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400243#ifndef RECOVERY_SDCARD_ON_DATA
244 Setup_AndSec();
245#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400246 }
247#endif
248#ifdef TW_INTERNAL_STORAGE_PATH
249 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
250 Is_Storage = true;
251 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400252#ifndef RECOVERY_SDCARD_ON_DATA
253 Setup_AndSec();
254#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400255 }
256#else
257 if (Mount_Point == "/emmc") {
258 Is_Storage = true;
259 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400260#ifndef RECOVERY_SDCARD_ON_DATA
261 Setup_AndSec();
262#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400263 }
264#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400265 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400266 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400267 Setup_Image(Display_Error);
268 if (Mount_Point == "/boot") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400269 MTD_Name = "boot";
Dees_Troy5bf43922012-09-07 16:07:55 -0400270 int backup_display_size = (int)(Backup_Size / 1048576LLU);
271 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
272 if (Backup_Size == 0) {
273 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
274 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
275 } else
276 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
277 } else if (Mount_Point == "/recovery") {
Dees_Troy38bd7602012-09-14 13:33:53 -0400278 MTD_Name = "recovery";
Dees_Troy5bf43922012-09-07 16:07:55 -0400279 int backup_display_size = (int)(Backup_Size / 1048576LLU);
280 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
281 if (Backup_Size == 0) {
282 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
283 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
284 } else
285 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
286 }
287 }
288
Dees_Troy51127312012-09-08 13:08:49 -0400289 // Process any custom flags
290 if (Flags.size() > 0)
291 Process_Flags(Flags, Display_Error);
292
293 return true;
294}
295
296bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
297 char flags[MAX_FSTAB_LINE_LENGTH];
298 int flags_len, index = 0;
299 char* ptr;
300
301 strcpy(flags, Flags.c_str());
302 flags_len = Flags.size();
303 for (index = 0; index < flags_len; index++) {
304 if (flags[index] == ';')
305 flags[index] = '\0';
306 }
307
308 index = 0;
309 while (index < flags_len) {
310 while (index < flags_len && flags[index] == '\0')
311 index++;
312 if (index >= flags_len)
313 continue;
314 ptr = flags + index;
315 if (strcmp(ptr, "removable") == 0) {
316 Removable = true;
317 } else if (strcmp(ptr, "storage") == 0) {
318 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400319 } else if (strcmp(ptr, "canbewiped") == 0) {
320 Can_Be_Wiped = true;
321 } else if (strcmp(ptr, "wipeingui") == 0) {
322 Can_Be_Wiped = true;
323 Wipe_Available_in_GUI = true;
324 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
325 Can_Be_Wiped = true;
326 Wipe_Available_in_GUI = true;
327 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400328 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
329 ptr += 13;
330 Is_SubPartition = true;
331 SubPartition_Of = ptr;
332 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
333 ptr += 8;
334 Symlink_Path = ptr;
335 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
336 ptr += 8;
337 Display_Name = ptr;
338 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
339 ptr += 10;
340 Format_Block_Size = atoi(ptr);
341 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
342 ptr += 7;
343 Length = atoi(ptr);
344 } else {
345 if (Display_Error)
346 LOGE("Unhandled flag: '%s'\n", ptr);
347 else
348 LOGI("Unhandled flag: '%s'\n", ptr);
349 }
350 while (index < flags_len && flags[index] != '\0')
351 index++;
352 }
353 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400354}
355
Dees_Troy5bf43922012-09-07 16:07:55 -0400356bool TWPartition::Is_File_System(string File_System) {
357 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400358 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400359 File_System == "ext4" ||
360 File_System == "vfat" ||
361 File_System == "ntfs" ||
362 File_System == "yaffs2" ||
363 File_System == "auto")
364 return true;
365 else
366 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400367}
368
Dees_Troy5bf43922012-09-07 16:07:55 -0400369bool TWPartition::Is_Image(string File_System) {
370 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400371 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400372 return true;
373 else
374 return false;
375}
376
Dees_Troy51127312012-09-08 13:08:49 -0400377bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400378 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400379 if (mkdir(Path.c_str(), 0777) == -1) {
380 if (Display_Error)
381 LOGE("Can not create '%s' folder.\n", Path.c_str());
382 else
383 LOGI("Can not create '%s' folder.\n", Path.c_str());
384 return false;
385 } else {
386 LOGI("Created '%s' folder.\n", Path.c_str());
387 return true;
388 }
389 }
390 return true;
391}
392
Dees_Troy5bf43922012-09-07 16:07:55 -0400393void TWPartition::Setup_File_System(bool Display_Error) {
394 struct statfs st;
395
396 Can_Be_Mounted = true;
397 Can_Be_Wiped = true;
398
Dees_Troy5bf43922012-09-07 16:07:55 -0400399 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400400 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400401 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
402 Backup_Name = Display_Name;
403 Backup_Method = FILES;
404}
405
406void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400407 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
408 Backup_Name = Display_Name;
409 if (Fstab_File_System == "emmc")
410 Backup_Method = DD;
411 else if (Fstab_File_System == "mtd")
412 Backup_Method = FLASH_UTILS;
413 else
414 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
415 if (Find_Partition_Size()) {
416 Used = Size;
417 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400419 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400420 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400421 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400422 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400423 }
424}
425
Dees_Troye58d5262012-09-21 12:27:57 -0400426void TWPartition::Setup_AndSec(void) {
427 Backup_Name = "and-sec";
428 Has_Android_Secure = true;
429 Symlink_Path = Mount_Point + "/.android_secure";
430 Symlink_Mount_Point = "/and-sec";
431 Backup_Path = Symlink_Mount_Point;
432 Make_Dir("/and-sec", true);
433 Recreate_AndSec_Folder();
434}
435
Dees_Troy5bf43922012-09-07 16:07:55 -0400436void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
437 char device[512], realDevice[512];
438
439 strcpy(device, Block.c_str());
440 memset(realDevice, 0, sizeof(realDevice));
441 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
442 {
443 strcpy(device, realDevice);
444 memset(realDevice, 0, sizeof(realDevice));
445 }
446
447 if (device[0] != '/') {
448 if (Display_Error)
449 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
450 else
451 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
452 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400453 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400454 Block = device;
455 return;
456 }
457}
458
Dees_Troy38bd7602012-09-14 13:33:53 -0400459bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
460 FILE *fp = NULL;
461 char line[255];
462
463 fp = fopen("/proc/mtd", "rt");
464 if (fp == NULL) {
465 LOGE("Device does not support /proc/mtd\n");
466 return false;
467 }
468
469 while (fgets(line, sizeof(line), fp) != NULL)
470 {
471 char device[32], label[32];
472 unsigned long size = 0;
473 char* fstype = NULL;
474 int deviceId;
475
476 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
477
478 // Skip header and blank lines
479 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
480 continue;
481
482 // Strip off the trailing " from the label
483 label[strlen(label)-1] = '\0';
484
485 if (strcmp(label, MTD_Name.c_str()) == 0) {
486 // We found our device
487 // Strip off the trailing : from the device
488 device[strlen(device)-1] = '\0';
489 if (sscanf(device,"mtd%d", &deviceId) == 1) {
490 sprintf(device, "/dev/block/mtdblock%d", deviceId);
491 Primary_Block_Device = device;
492 }
493 }
494 }
495 fclose(fp);
496
497 return false;
498}
499
Dees_Troy51127312012-09-08 13:08:49 -0400500bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
501 struct statfs st;
502 string Local_Path = Mount_Point + "/.";
503
504 if (!Mount(Display_Error))
505 return false;
506
507 if (statfs(Local_Path.c_str(), &st) != 0) {
508 if (!Removable) {
509 if (Display_Error)
510 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
511 else
512 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
513 }
514 return false;
515 }
516 Size = (st.f_blocks * st.f_bsize);
517 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
518 Free = (st.f_bfree * st.f_bsize);
519 Backup_Size = Used;
520 return true;
521}
522
523bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400524 FILE* fp;
525 char command[255], line[512];
526 int include_block = 1;
527 unsigned int min_len;
528
529 if (!Mount(Display_Error))
530 return false;
531
Dees_Troy38bd7602012-09-14 13:33:53 -0400532 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400533 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400534 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400535 fp = fopen("/tmp/dfoutput.txt", "rt");
536 if (fp == NULL) {
537 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400538 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400539 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400540
541 while (fgets(line, sizeof(line), fp) != NULL)
542 {
543 unsigned long blocks, used, available;
544 char device[64];
545 char tmpString[64];
546
547 if (strncmp(line, "Filesystem", 10) == 0)
548 continue;
549 if (strlen(line) < min_len) {
550 include_block = 0;
551 continue;
552 }
553 if (include_block) {
554 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
555 } else {
556 // The device block string is so long that the df information is on the next line
557 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400558 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400559 while (tmpString[space_count] == 32)
560 space_count++;
561 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
562 }
563
564 // Adjust block size to byte size
565 Size = blocks * 1024ULL;
566 Used = used * 1024ULL;
567 Free = available * 1024ULL;
568 Backup_Size = Used;
569 }
570 fclose(fp);
571 return true;
572}
573
Dees_Troy5bf43922012-09-07 16:07:55 -0400574bool TWPartition::Find_Partition_Size(void) {
575 FILE* fp;
576 char line[512];
577 string tmpdevice;
578
579 // In this case, we'll first get the partitions we care about (with labels)
580 fp = fopen("/proc/partitions", "rt");
581 if (fp == NULL)
582 return false;
583
584 while (fgets(line, sizeof(line), fp) != NULL)
585 {
586 unsigned long major, minor, blocks;
587 char device[512];
588 char tmpString[64];
589
Dees_Troy63c8df72012-09-10 14:02:05 -0400590 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400591 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
592
593 tmpdevice = "/dev/block/";
594 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400595 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400596 // Adjust block size to byte size
597 Size = blocks * 1024ULL;
598 fclose(fp);
599 return true;
600 }
601 }
602 fclose(fp);
603 return false;
604}
605
Dees_Troy5bf43922012-09-07 16:07:55 -0400606void TWPartition::Flip_Block_Device(void) {
607 string temp;
608
609 temp = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400610 Primary_Block_Device = Alternate_Block_Device;
Dees_Troy5bf43922012-09-07 16:07:55 -0400611 Alternate_Block_Device = temp;
612}
613
614bool TWPartition::Is_Mounted(void) {
615 if (!Can_Be_Mounted)
616 return false;
617
618 struct stat st1, st2;
619 string test_path;
620
621 // Check to see if the mount point directory exists
622 test_path = Mount_Point + "/.";
623 if (stat(test_path.c_str(), &st1) != 0) return false;
624
625 // Check to see if the directory above the mount point exists
626 test_path = Mount_Point + "/../.";
627 if (stat(test_path.c_str(), &st2) != 0) return false;
628
629 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
630 int ret = (st1.st_dev != st2.st_dev) ? true : false;
631
632 return ret;
633}
634
635bool TWPartition::Mount(bool Display_Error) {
636 if (Is_Mounted()) {
637 return true;
638 } else if (!Can_Be_Mounted) {
639 return false;
640 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400641
642 Find_Actual_Block_Device();
643
644 // Check the current file system before mounting
645 Check_FS_Type();
646
647 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
648 if (Display_Error)
649 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
650 else
651 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
652 return false;
653 } else {
654 if (Removable)
655 Update_Size(Display_Error);
656
657 if (!Symlink_Mount_Point.empty()) {
658 string Command;
659
660 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400661 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400662 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400663 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400664 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400665 return true;
666}
667
668bool TWPartition::UnMount(bool Display_Error) {
669 if (Is_Mounted()) {
670 int never_unmount_system;
671
672 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
673 if (never_unmount_system == 1 && Mount_Point == "/system")
674 return true; // Never unmount system if you're not supposed to unmount it
675
Dees_Troy38bd7602012-09-14 13:33:53 -0400676 if (!Symlink_Mount_Point.empty())
677 umount(Symlink_Mount_Point.c_str());
678
Dees_Troy5bf43922012-09-07 16:07:55 -0400679 if (umount(Mount_Point.c_str()) != 0) {
680 if (Display_Error)
681 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
682 else
683 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
684 return false;
685 } else
686 return true;
687 } else {
688 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400689 }
690}
691
692bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400693 if (!Can_Be_Wiped) {
694 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
695 return false;
696 }
697
Dees_Troyc51f1f92012-09-20 15:32:13 -0400698 if (Mount_Point == "/cache")
699 tmplog_offset = 0;
700
Dees_Troy38bd7602012-09-14 13:33:53 -0400701 if (Has_Data_Media)
702 return Wipe_Data_Without_Wiping_Media();
703
704 int check;
705 DataManager::GetValue(TW_RM_RF_VAR, check);
706 if (check)
707 return Wipe_RMRF();
708
709 if (Current_File_System == "ext4")
710 return Wipe_EXT4();
711
712 if (Current_File_System == "ext2" || Current_File_System == "ext3")
713 return Wipe_EXT23();
714
715 if (Current_File_System == "vfat")
716 return Wipe_FAT();
717
718 if (Current_File_System == "yaffs2")
719 return Wipe_MTD();
720
721 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
722 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400723}
724
Dees_Troye58d5262012-09-21 12:27:57 -0400725bool TWPartition::Wipe_AndSec(void) {
726 if (!Has_Android_Secure)
727 return false;
728
729 char cmd[512];
730
731 if (!Mount(true))
732 return false;
733
734 ui_print("Using rm -rf on .android_secure\n");
735 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
736
737 LOGI("rm -rf command is: '%s'\n", cmd);
738 system(cmd);
739 return true;
740}
741
Dees_Troy51a0e822012-09-05 15:24:24 -0400742bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400743 if (Backup_Method == FILES)
744 return Backup_Tar(backup_folder);
745 else if (Backup_Method == DD)
746 return Backup_DD(backup_folder);
747 else if (Backup_Method == FLASH_UTILS)
748 return Backup_Dump_Image(backup_folder);
749 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
750 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400751}
752
Dees_Troy43d8b002012-09-17 16:00:01 -0400753bool TWPartition::Check_MD5(string restore_folder) {
754 string Full_Filename;
755 char split_filename[512];
756 int index = 0;
757
758 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400759 if (!TWFunc::Path_Exists(Full_Filename)) {
760 // This is a split archive, we presume
761 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
762 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
763 if (TWFunc::Check_MD5(split_filename) == 0) {
764 LOGE("MD5 failed to match on '%s'.\n", split_filename);
765 return false;
766 }
767 index++;
768 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400769 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400770 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400771 } else {
772 // Single file archive
773 if (TWFunc::Check_MD5(Full_Filename) == 0) {
774 LOGE("MD5 failed to match on '%s'.\n", split_filename);
775 return false;
776 } else
777 return true;
778 }
779 return false;
780}
781
Dees_Troy51a0e822012-09-05 15:24:24 -0400782bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400783 if (Backup_Method == FILES)
784 return Restore_Tar(restore_folder);
785 else if (Backup_Method == DD)
786 return Restore_DD(restore_folder);
787 else if (Backup_Method == FLASH_UTILS)
788 return Restore_Flash_Image(restore_folder);
789 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
790 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400791}
792
793string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400794 if (Backup_Method == NONE)
795 return "none";
796 else if (Backup_Method == FILES)
797 return "files";
798 else if (Backup_Method == DD)
799 return "dd";
800 else if (Backup_Method == FLASH_UTILS)
801 return "flash_utils";
802 else
803 return "undefined";
804 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400805}
806
807bool TWPartition::Decrypt(string Password) {
808 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400809 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400810 return 1;
811}
812
813bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400814 bool Save_Data_Media = Has_Data_Media;
815
816 if (!UnMount(true))
817 return false;
818
819 Current_File_System = Fstab_File_System;
820 Is_Encrypted = false;
821 Is_Decrypted = false;
822 Decrypted_Block_Device = "";
823 Has_Data_Media = false;
824 if (Wipe()) {
825 Has_Data_Media = Save_Data_Media;
826 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
827 Recreate_Media_Folder();
828 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400829 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400830 return true;
831 } else {
832 Has_Data_Media = Save_Data_Media;
833 LOGE("Unable to format to remove encryption.\n");
834 return false;
835 }
836 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400837}
838
839void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400840 FILE *fp;
841 string blkCommand;
842 char blkOutput[255];
843 char* blk;
844 char* arg;
845 char* ptr;
846
847 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
848 return; // Running blkid on some mtd devices causes a massive crash
849
Dees_Troy38bd7602012-09-14 13:33:53 -0400850 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400851 if (!Is_Present)
852 return;
Dees_Troy51127312012-09-08 13:08:49 -0400853
Dees_Troy8170a922012-09-18 15:40:25 -0400854 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
855 system("rm /tmp/blkidoutput.txt");
856
857 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400858 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400859 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400860 if (fp == NULL)
861 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400862 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
863 {
864 blk = blkOutput;
865 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400866 while (*ptr > 32 && *ptr != ':') ptr++;
867 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400868 *ptr = 0;
869
870 // Increment by two, but verify that we don't hit a NULL
871 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400872 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400873
874 // Now, find the TYPE field
875 while (1)
876 {
877 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400878 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400879 if (*ptr != 0)
880 {
881 *ptr = 0;
882 ptr++;
883 }
884
885 if (strlen(arg) > 6)
886 {
887 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
888 }
889
890 if (*ptr == 0)
891 {
892 arg = NULL;
893 break;
894 }
895 }
896
897 if (arg && strlen(arg) > 7)
898 {
899 arg += 6; // Skip the TYPE=" portion
900 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
901 }
902 else
903 continue;
904
Dees_Troy63c8df72012-09-10 14:02:05 -0400905 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400906 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
907 Current_File_System = arg;
908 }
909 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400910 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400911 return;
912}
913
914bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400915 if (!UnMount(true))
916 return false;
917
Dees_Troy43d8b002012-09-17 16:00:01 -0400918 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400919 char command[512];
920
921 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
922 Find_Actual_Block_Device();
923 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
924 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400925 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400926 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400927 ui_print("Done.\n");
928 return true;
929 } else {
930 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
931 return false;
932 }
933 } else
934 return Wipe_RMRF();
935
936 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400937}
938
939bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400940 if (!UnMount(true))
941 return false;
942
Dees_Troy43d8b002012-09-17 16:00:01 -0400943 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400944 string Command;
945
946 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
947 Find_Actual_Block_Device();
948 Command = "make_ext4fs";
949 if (!Is_Decrypted && Length != 0) {
950 // Only use length if we're not decrypted
951 char len[32];
952 sprintf(len, "%i", Length);
953 Command += " -l ";
954 Command += len;
955 }
956 Command += " " + Actual_Block_Device;
957 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400958 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400959 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400960 ui_print("Done.\n");
961 return true;
962 } else {
963 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
964 return false;
965 }
966 } else
967 return Wipe_EXT23();
968
969 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400970}
971
972bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400973 char command[512];
974
Dees_Troy43d8b002012-09-17 16:00:01 -0400975 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400976 if (!UnMount(true))
977 return false;
978
979 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
980 Find_Actual_Block_Device();
981 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400982 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400983 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400984 ui_print("Done.\n");
985 return true;
986 } else {
987 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
988 return false;
989 }
990 return true;
991 }
992 else
993 return Wipe_RMRF();
994
995 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400996}
997
Dees_Troy38bd7602012-09-14 13:33:53 -0400998bool TWPartition::Wipe_MTD() {
999 if (!UnMount(true))
1000 return false;
1001
1002 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1003
1004 mtd_scan_partitions();
1005 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1006 if (mtd == NULL) {
1007 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1008 return false;
1009 }
1010
1011 MtdWriteContext* ctx = mtd_write_partition(mtd);
1012 if (ctx == NULL) {
1013 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1014 return false;
1015 }
1016 if (mtd_erase_blocks(ctx, -1) == -1) {
1017 mtd_write_close(ctx);
1018 LOGE("Failed to format '%s'", MTD_Name.c_str());
1019 return false;
1020 }
1021 if (mtd_write_close(ctx) != 0) {
1022 LOGE("Failed to close '%s'", MTD_Name.c_str());
1023 return false;
1024 }
Dees_Troye58d5262012-09-21 12:27:57 -04001025 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001026 ui_print("Done.\n");
1027 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001028}
1029
1030bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001031 char cmd[512];
1032
1033 if (!Mount(true))
1034 return false;
1035
Dees_Troye58d5262012-09-21 12:27:57 -04001036 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1037 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001038
1039 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001040 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001041 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001042 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001043}
1044
1045bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001046 char cmd[256];
1047
1048 // This handles wiping data on devices with "sdcard" in /data/media
1049 if (!Mount(true))
1050 return false;
1051
1052 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001053 system("rm -f /data/*");
1054 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001055
1056 DIR* d;
1057 d = opendir("/data");
1058 if (d != NULL)
1059 {
1060 struct dirent* de;
1061 while ((de = readdir(d)) != NULL) {
1062 if (strcmp(de->d_name, "media") == 0) continue;
1063
1064 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001065 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001066 }
1067 closedir(d);
1068 }
1069 ui_print("Done.\n");
1070 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001071}
1072
1073bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001074 char back_name[255], split_index[5];
1075 string Full_FileName, Split_FileName, Tar_Args, Command;
1076 int use_compression, index, backup_count;
1077 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001078 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001079
1080 if (!Mount(true))
1081 return false;
1082
Dees_Troyb46a6842012-09-25 11:06:46 -04001083 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001084 ui_print("Backing up %s...\n", Display_Name.c_str());
1085
1086 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1087 if (use_compression)
1088 Tar_Args = "-cz";
1089 else
1090 Tar_Args = "-c";
1091
1092 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1093 Backup_FileName = back_name;
1094
Dees_Troy43d8b002012-09-17 16:00:01 -04001095 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1096 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001097 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001098 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001099 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001100 if (backup_count < 1) {
1101 LOGE("Error generating file list!\n");
1102 return false;
1103 }
1104 for (index=0; index<backup_count; index++) {
1105 sprintf(split_index, "%03i", index);
1106 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1107 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1108 LOGI("Backup command: '%s'\n", Command.c_str());
1109 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1110 system(Command.c_str()); // sending backup command formed earlier above
1111
Dees_Troy7c2dec82012-09-26 09:49:14 -04001112 file_size = TWFunc::Get_File_Size(Full_FileName);
1113 if (file_size == 0) {
1114 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str()); // oh noes! file size is 0, abort! abort!
Dees_Troy4a2a1262012-09-18 09:33:47 -04001115 return false;
1116 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001117 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001118 }
1119 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1120 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001121 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001122 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001123 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001124 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001125 else
Dees_Troye58d5262012-09-21 12:27:57 -04001126 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001127 LOGI("Backup command: '%s'\n", Command.c_str());
1128 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001129 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1130 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1131 return false;
1132 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001133 }
1134 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001135}
1136
1137bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001138 char back_name[255];
1139 string Full_FileName, Command;
1140 int use_compression;
1141
Dees_Troyb46a6842012-09-25 11:06:46 -04001142 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001143 ui_print("Backing up %s...\n", Display_Name.c_str());
1144
1145 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1146 Backup_FileName = back_name;
1147
1148 Full_FileName = backup_folder + "/" + Backup_FileName;
1149
1150 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1151 LOGI("Backup command: '%s'\n", Command.c_str());
1152 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001153 if (TWFunc::Get_File_Size(Full_FileName) != Backup_Size) {
1154 LOGE("Backup file size %lu for '%s' is does not match backup size %llu.\n", TWFunc::Get_File_Size(Full_FileName), Full_FileName.c_str(), Backup_Size);
1155 return false;
1156 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001157 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001158}
1159
1160bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001161 char back_name[255];
1162 string Full_FileName, Command;
1163 int use_compression;
1164
Dees_Troyb46a6842012-09-25 11:06:46 -04001165 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001166 ui_print("Backing up %s...\n", Display_Name.c_str());
1167
1168 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1169 Backup_FileName = back_name;
1170
1171 Full_FileName = backup_folder + "/" + Backup_FileName;
1172
1173 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1174 LOGI("Backup command: '%s'\n", Command.c_str());
1175 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001176 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1177 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1178 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1179 return false;
1180 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001181 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001182}
1183
1184bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001185 size_t first_period, second_period;
1186 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001187 int index = 0;
1188 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001189
Dees_Troyb46a6842012-09-25 11:06:46 -04001190 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001191 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1192
1193 // Parse backup filename to extract the file system before wiping
1194 first_period = Backup_FileName.find(".");
1195 if (first_period == string::npos) {
1196 LOGE("Unable to find file system (first period).\n");
1197 return false;
1198 }
1199 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1200 second_period = Restore_File_System.find(".");
1201 if (second_period == string::npos) {
1202 LOGE("Unable to find file system (second period).\n");
1203 return false;
1204 }
1205 Restore_File_System.resize(second_period);
1206 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1207 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001208 if (Has_Android_Secure) {
1209 ui_print("Wiping android secure...\n");
1210 if (!Wipe_AndSec())
1211 return false;
1212 } else if (!Wipe()) {
1213 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001214 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001215 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001216
1217 if (!Mount(true))
1218 return false;
1219
Dees_Troy43d8b002012-09-17 16:00:01 -04001220 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001221 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001222 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001223 // Backup is multiple archives
1224 LOGI("Backup is multiple archives.\n");
1225 sprintf(split_index, "%03i", index);
1226 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1227 while (TWFunc::Path_Exists(Full_FileName)) {
1228 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001229 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001230 LOGI("Restore command: '%s'\n", Command.c_str());
1231 system(Command.c_str());
1232 index++;
1233 sprintf(split_index, "%03i", index);
1234 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1235 }
1236 if (index == 0) {
1237 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1238 return false;
1239 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001240 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001241 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001242 LOGI("Restore command: '%s'\n", Command.c_str());
1243 system(Command.c_str());
1244 }
1245 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001246}
1247
1248bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001249 string Full_FileName, Command;
1250
Dees_Troyb46a6842012-09-25 11:06:46 -04001251 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001252 ui_print("Restoring %s...\n", Display_Name.c_str());
1253 Full_FileName = restore_folder + "/" + Backup_FileName;
1254 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1255 LOGI("Restore command: '%s'\n", Command.c_str());
1256 system(Command.c_str());
1257 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001258}
1259
1260bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001261 string Full_FileName, Command;
1262
Dees_Troyb46a6842012-09-25 11:06:46 -04001263 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001264 ui_print("Restoring %s...\n", Display_Name.c_str());
1265 Full_FileName = restore_folder + "/" + Backup_FileName;
1266 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1267 Command = "erase_image " + MTD_Name;
1268 LOGI("Erase command: '%s'\n", Command.c_str());
1269 system(Command.c_str());
1270 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1271 LOGI("Restore command: '%s'\n", Command.c_str());
1272 system(Command.c_str());
1273 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001274}
Dees_Troy5bf43922012-09-07 16:07:55 -04001275
1276bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001277 bool ret = false;
1278
Dees_Troyab10ee22012-09-21 14:27:30 -04001279 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001280 return false;
1281
Dees_Troy38bd7602012-09-14 13:33:53 -04001282 if (Removable || Is_Encrypted) {
1283 if (!Mount(false))
1284 return true;
1285 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001286 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001287
1288 ret = Get_Size_Via_statfs(Display_Error);
1289 if (!ret || Size == 0)
1290 if (!Get_Size_Via_df(Display_Error))
1291 return false;
1292
Dees_Troy5bf43922012-09-07 16:07:55 -04001293 if (Has_Data_Media) {
1294 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001295 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001296 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1297 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001298 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001299 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001300 int bak = (int)(Backup_Size / 1048576LLU);
1301 int total = (int)(Size / 1048576LLU);
1302 int us = (int)(Used / 1048576LLU);
1303 int fre = (int)(Free / 1048576LLU);
1304 int datmed = (int)(data_media_used / 1048576LLU);
1305 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 -04001306 } else
1307 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001308 } else if (Has_Android_Secure) {
1309 if (Mount(Display_Error))
1310 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
1311 else
1312 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001313 }
1314 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001315}
Dees_Troy38bd7602012-09-14 13:33:53 -04001316
1317void TWPartition::Find_Actual_Block_Device(void) {
1318 if (Is_Decrypted) {
1319 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001320 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001321 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001322 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001323 Is_Present = true;
1324 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001325 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001326 Flip_Block_Device();
1327 Actual_Block_Device = Primary_Block_Device;
1328 Is_Present = true;
1329 } else
1330 Is_Present = false;
1331}
1332
1333void TWPartition::Recreate_Media_Folder(void) {
1334 string Command;
1335
1336 if (!Mount(true)) {
1337 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001338 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001339 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001340 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001341 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001342 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001343 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001344 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001345 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001346}
Dees_Troye58d5262012-09-21 12:27:57 -04001347
1348void TWPartition::Recreate_AndSec_Folder(void) {
1349 string Command;
1350
1351 if (!Has_Android_Secure)
1352 return;
1353
1354 if (!Mount(true)) {
1355 LOGE("Unable to recreate android secure folder.\n");
1356 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1357 LOGI("Recreating android secure folder.\n");
1358 TWFunc::Recursive_Mkdir(Symlink_Path);
1359 Command = "umount " + Symlink_Mount_Point;
1360 system(Command.c_str());
1361 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1362 system(Command.c_str());
1363 }
1364}