blob: 56c69b38630757e43e836e3a48f7fd5bb118ec81 [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") {
Dees_Troy094207a2012-09-26 12:00:39 -0400122 MTD_Name = ptr;
123 Find_MTD_Block_Device(MTD_Name);
Dees_Troy38bd7602012-09-14 13:33:53 -0400124 } 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_Troy5bf43922012-09-07 16:07:55 -0400171 } else if (Mount_Point == "/data") {
172 Display_Name = "Data";
173 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400174 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400175#ifdef RECOVERY_SDCARD_ON_DATA
176 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400177 Is_Storage = true;
178 Storage_Path = "/data/media";
Dees_Troyb46a6842012-09-25 11:06:46 -0400179 Recreate_Media_Folder();
Dees_Troy657c3092012-09-10 20:32:10 -0400180 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
181 Make_Dir("/emmc", Display_Error);
182 Symlink_Path = "/data/media";
183 Symlink_Mount_Point = "/emmc";
184 } else {
185 Make_Dir("/sdcard", Display_Error);
186 Symlink_Path = "/data/media";
187 Symlink_Mount_Point = "/sdcard";
188 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400189#endif
190#ifdef TW_INCLUDE_CRYPTO
191 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400192 char crypto_blkdev[255];
193 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
194 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400195 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400196 DataManager::SetValue(TW_IS_DECRYPTED, 1);
197 Is_Encrypted = true;
198 Is_Decrypted = true;
199 Decrypted_Block_Device = crypto_blkdev;
200 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
201 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400202 Is_Encrypted = true;
203 Is_Decrypted = false;
204 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
205 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
206 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400207 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400208#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400209 } else if (Mount_Point == "/cache") {
210 Display_Name = "Cache";
211 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400212 Wipe_During_Factory_Reset = true;
Dees_Troyb46a6842012-09-25 11:06:46 -0400213 if (!TWFunc::Path_Exists("/cache/recovery")) {
214 LOGI("Recreating /cache/recovery folder.\n");
215 TWFunc::Recursive_Mkdir("/cache/recovery");
216 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400217 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400218 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400219 Display_Name = "DataData";
220 Is_SubPartition = true;
221 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400222 DataManager::SetValue(TW_HAS_DATADATA, 1);
223 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400224 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400225 Display_Name = "SD-Ext";
226 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400227 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400228 }
229#ifdef TW_EXTERNAL_STORAGE_PATH
230 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
231 Is_Storage = true;
232 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400233 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400234 }
235#else
236 if (Mount_Point == "/sdcard") {
237 Is_Storage = true;
238 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400239 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400240#ifndef RECOVERY_SDCARD_ON_DATA
241 Setup_AndSec();
242#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400243 }
244#endif
245#ifdef TW_INTERNAL_STORAGE_PATH
246 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
247 Is_Storage = true;
248 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400249#ifndef RECOVERY_SDCARD_ON_DATA
250 Setup_AndSec();
251#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400252 }
253#else
254 if (Mount_Point == "/emmc") {
255 Is_Storage = true;
256 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400257#ifndef RECOVERY_SDCARD_ON_DATA
258 Setup_AndSec();
259#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400260 }
261#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400262 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400263 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 Setup_Image(Display_Error);
265 if (Mount_Point == "/boot") {
266 int backup_display_size = (int)(Backup_Size / 1048576LLU);
267 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
268 if (Backup_Size == 0) {
269 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
270 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
271 } else
272 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
273 } else if (Mount_Point == "/recovery") {
274 int backup_display_size = (int)(Backup_Size / 1048576LLU);
275 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
276 if (Backup_Size == 0) {
277 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
278 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
279 } else
280 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
281 }
282 }
283
Dees_Troy51127312012-09-08 13:08:49 -0400284 // Process any custom flags
285 if (Flags.size() > 0)
286 Process_Flags(Flags, Display_Error);
287
288 return true;
289}
290
291bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
292 char flags[MAX_FSTAB_LINE_LENGTH];
293 int flags_len, index = 0;
294 char* ptr;
295
296 strcpy(flags, Flags.c_str());
297 flags_len = Flags.size();
298 for (index = 0; index < flags_len; index++) {
299 if (flags[index] == ';')
300 flags[index] = '\0';
301 }
302
303 index = 0;
304 while (index < flags_len) {
305 while (index < flags_len && flags[index] == '\0')
306 index++;
307 if (index >= flags_len)
308 continue;
309 ptr = flags + index;
310 if (strcmp(ptr, "removable") == 0) {
311 Removable = true;
312 } else if (strcmp(ptr, "storage") == 0) {
313 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400314 } else if (strcmp(ptr, "canbewiped") == 0) {
315 Can_Be_Wiped = true;
316 } else if (strcmp(ptr, "wipeingui") == 0) {
317 Can_Be_Wiped = true;
318 Wipe_Available_in_GUI = true;
319 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
320 Can_Be_Wiped = true;
321 Wipe_Available_in_GUI = true;
322 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400323 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
324 ptr += 13;
325 Is_SubPartition = true;
326 SubPartition_Of = ptr;
327 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
328 ptr += 8;
329 Symlink_Path = ptr;
330 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
331 ptr += 8;
332 Display_Name = ptr;
333 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
334 ptr += 10;
335 Format_Block_Size = atoi(ptr);
336 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
337 ptr += 7;
338 Length = atoi(ptr);
339 } else {
340 if (Display_Error)
341 LOGE("Unhandled flag: '%s'\n", ptr);
342 else
343 LOGI("Unhandled flag: '%s'\n", ptr);
344 }
345 while (index < flags_len && flags[index] != '\0')
346 index++;
347 }
348 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400349}
350
Dees_Troy5bf43922012-09-07 16:07:55 -0400351bool TWPartition::Is_File_System(string File_System) {
352 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400353 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400354 File_System == "ext4" ||
355 File_System == "vfat" ||
356 File_System == "ntfs" ||
357 File_System == "yaffs2" ||
358 File_System == "auto")
359 return true;
360 else
361 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400362}
363
Dees_Troy5bf43922012-09-07 16:07:55 -0400364bool TWPartition::Is_Image(string File_System) {
365 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400366 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400367 return true;
368 else
369 return false;
370}
371
Dees_Troy51127312012-09-08 13:08:49 -0400372bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400373 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400374 if (mkdir(Path.c_str(), 0777) == -1) {
375 if (Display_Error)
376 LOGE("Can not create '%s' folder.\n", Path.c_str());
377 else
378 LOGI("Can not create '%s' folder.\n", Path.c_str());
379 return false;
380 } else {
381 LOGI("Created '%s' folder.\n", Path.c_str());
382 return true;
383 }
384 }
385 return true;
386}
387
Dees_Troy5bf43922012-09-07 16:07:55 -0400388void TWPartition::Setup_File_System(bool Display_Error) {
389 struct statfs st;
390
391 Can_Be_Mounted = true;
392 Can_Be_Wiped = true;
393
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400395 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400396 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
397 Backup_Name = Display_Name;
398 Backup_Method = FILES;
399}
400
401void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400402 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
403 Backup_Name = Display_Name;
404 if (Fstab_File_System == "emmc")
405 Backup_Method = DD;
406 else if (Fstab_File_System == "mtd")
407 Backup_Method = FLASH_UTILS;
408 else
409 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
410 if (Find_Partition_Size()) {
411 Used = Size;
412 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400413 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400414 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400415 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400416 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400417 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400418 }
419}
420
Dees_Troye58d5262012-09-21 12:27:57 -0400421void TWPartition::Setup_AndSec(void) {
422 Backup_Name = "and-sec";
423 Has_Android_Secure = true;
424 Symlink_Path = Mount_Point + "/.android_secure";
425 Symlink_Mount_Point = "/and-sec";
426 Backup_Path = Symlink_Mount_Point;
427 Make_Dir("/and-sec", true);
428 Recreate_AndSec_Folder();
429}
430
Dees_Troy5bf43922012-09-07 16:07:55 -0400431void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
432 char device[512], realDevice[512];
433
434 strcpy(device, Block.c_str());
435 memset(realDevice, 0, sizeof(realDevice));
436 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
437 {
438 strcpy(device, realDevice);
439 memset(realDevice, 0, sizeof(realDevice));
440 }
441
442 if (device[0] != '/') {
443 if (Display_Error)
444 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
445 else
446 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
447 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400448 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400449 Block = device;
450 return;
451 }
452}
453
Dees_Troy38bd7602012-09-14 13:33:53 -0400454bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
455 FILE *fp = NULL;
456 char line[255];
457
458 fp = fopen("/proc/mtd", "rt");
459 if (fp == NULL) {
460 LOGE("Device does not support /proc/mtd\n");
461 return false;
462 }
463
464 while (fgets(line, sizeof(line), fp) != NULL)
465 {
466 char device[32], label[32];
467 unsigned long size = 0;
468 char* fstype = NULL;
469 int deviceId;
470
471 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
472
473 // Skip header and blank lines
474 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
475 continue;
476
477 // Strip off the trailing " from the label
478 label[strlen(label)-1] = '\0';
479
480 if (strcmp(label, MTD_Name.c_str()) == 0) {
481 // We found our device
482 // Strip off the trailing : from the device
483 device[strlen(device)-1] = '\0';
484 if (sscanf(device,"mtd%d", &deviceId) == 1) {
485 sprintf(device, "/dev/block/mtdblock%d", deviceId);
486 Primary_Block_Device = device;
487 }
488 }
489 }
490 fclose(fp);
491
492 return false;
493}
494
Dees_Troy51127312012-09-08 13:08:49 -0400495bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
496 struct statfs st;
497 string Local_Path = Mount_Point + "/.";
498
499 if (!Mount(Display_Error))
500 return false;
501
502 if (statfs(Local_Path.c_str(), &st) != 0) {
503 if (!Removable) {
504 if (Display_Error)
505 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
506 else
507 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
508 }
509 return false;
510 }
511 Size = (st.f_blocks * st.f_bsize);
512 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
513 Free = (st.f_bfree * st.f_bsize);
514 Backup_Size = Used;
515 return true;
516}
517
518bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400519 FILE* fp;
520 char command[255], line[512];
521 int include_block = 1;
522 unsigned int min_len;
523
524 if (!Mount(Display_Error))
525 return false;
526
Dees_Troy38bd7602012-09-14 13:33:53 -0400527 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400528 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400529 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400530 fp = fopen("/tmp/dfoutput.txt", "rt");
531 if (fp == NULL) {
532 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400533 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400534 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400535
536 while (fgets(line, sizeof(line), fp) != NULL)
537 {
538 unsigned long blocks, used, available;
539 char device[64];
540 char tmpString[64];
541
542 if (strncmp(line, "Filesystem", 10) == 0)
543 continue;
544 if (strlen(line) < min_len) {
545 include_block = 0;
546 continue;
547 }
548 if (include_block) {
549 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
550 } else {
551 // The device block string is so long that the df information is on the next line
552 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400553 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400554 while (tmpString[space_count] == 32)
555 space_count++;
556 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
557 }
558
559 // Adjust block size to byte size
560 Size = blocks * 1024ULL;
561 Used = used * 1024ULL;
562 Free = available * 1024ULL;
563 Backup_Size = Used;
564 }
565 fclose(fp);
566 return true;
567}
568
Dees_Troy5bf43922012-09-07 16:07:55 -0400569bool TWPartition::Find_Partition_Size(void) {
570 FILE* fp;
571 char line[512];
572 string tmpdevice;
573
574 // In this case, we'll first get the partitions we care about (with labels)
575 fp = fopen("/proc/partitions", "rt");
576 if (fp == NULL)
577 return false;
578
579 while (fgets(line, sizeof(line), fp) != NULL)
580 {
581 unsigned long major, minor, blocks;
582 char device[512];
583 char tmpString[64];
584
Dees_Troy63c8df72012-09-10 14:02:05 -0400585 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400586 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
587
588 tmpdevice = "/dev/block/";
589 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400590 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400591 // Adjust block size to byte size
592 Size = blocks * 1024ULL;
593 fclose(fp);
594 return true;
595 }
596 }
597 fclose(fp);
598 return false;
599}
600
Dees_Troy5bf43922012-09-07 16:07:55 -0400601void TWPartition::Flip_Block_Device(void) {
602 string temp;
603
604 temp = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400605 Primary_Block_Device = Alternate_Block_Device;
Dees_Troy5bf43922012-09-07 16:07:55 -0400606 Alternate_Block_Device = temp;
607}
608
609bool TWPartition::Is_Mounted(void) {
610 if (!Can_Be_Mounted)
611 return false;
612
613 struct stat st1, st2;
614 string test_path;
615
616 // Check to see if the mount point directory exists
617 test_path = Mount_Point + "/.";
618 if (stat(test_path.c_str(), &st1) != 0) return false;
619
620 // Check to see if the directory above the mount point exists
621 test_path = Mount_Point + "/../.";
622 if (stat(test_path.c_str(), &st2) != 0) return false;
623
624 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
625 int ret = (st1.st_dev != st2.st_dev) ? true : false;
626
627 return ret;
628}
629
630bool TWPartition::Mount(bool Display_Error) {
631 if (Is_Mounted()) {
632 return true;
633 } else if (!Can_Be_Mounted) {
634 return false;
635 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400636
637 Find_Actual_Block_Device();
638
639 // Check the current file system before mounting
640 Check_FS_Type();
641
642 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
643 if (Display_Error)
644 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
645 else
646 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
647 return false;
648 } else {
649 if (Removable)
650 Update_Size(Display_Error);
651
652 if (!Symlink_Mount_Point.empty()) {
653 string Command;
654
655 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400656 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400657 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400658 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400659 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400660 return true;
661}
662
663bool TWPartition::UnMount(bool Display_Error) {
664 if (Is_Mounted()) {
665 int never_unmount_system;
666
667 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
668 if (never_unmount_system == 1 && Mount_Point == "/system")
669 return true; // Never unmount system if you're not supposed to unmount it
670
Dees_Troy38bd7602012-09-14 13:33:53 -0400671 if (!Symlink_Mount_Point.empty())
672 umount(Symlink_Mount_Point.c_str());
673
Dees_Troy5bf43922012-09-07 16:07:55 -0400674 if (umount(Mount_Point.c_str()) != 0) {
675 if (Display_Error)
676 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
677 else
678 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
679 return false;
680 } else
681 return true;
682 } else {
683 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400684 }
685}
686
687bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400688 if (!Can_Be_Wiped) {
689 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
690 return false;
691 }
692
Dees_Troyc51f1f92012-09-20 15:32:13 -0400693 if (Mount_Point == "/cache")
694 tmplog_offset = 0;
695
Dees_Troy38bd7602012-09-14 13:33:53 -0400696 if (Has_Data_Media)
697 return Wipe_Data_Without_Wiping_Media();
698
699 int check;
700 DataManager::GetValue(TW_RM_RF_VAR, check);
701 if (check)
702 return Wipe_RMRF();
703
704 if (Current_File_System == "ext4")
705 return Wipe_EXT4();
706
707 if (Current_File_System == "ext2" || Current_File_System == "ext3")
708 return Wipe_EXT23();
709
710 if (Current_File_System == "vfat")
711 return Wipe_FAT();
712
713 if (Current_File_System == "yaffs2")
714 return Wipe_MTD();
715
716 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
717 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400718}
719
Dees_Troye58d5262012-09-21 12:27:57 -0400720bool TWPartition::Wipe_AndSec(void) {
721 if (!Has_Android_Secure)
722 return false;
723
724 char cmd[512];
725
726 if (!Mount(true))
727 return false;
728
729 ui_print("Using rm -rf on .android_secure\n");
730 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
731
732 LOGI("rm -rf command is: '%s'\n", cmd);
733 system(cmd);
734 return true;
735}
736
Dees_Troy51a0e822012-09-05 15:24:24 -0400737bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400738 if (Backup_Method == FILES)
739 return Backup_Tar(backup_folder);
740 else if (Backup_Method == DD)
741 return Backup_DD(backup_folder);
742 else if (Backup_Method == FLASH_UTILS)
743 return Backup_Dump_Image(backup_folder);
744 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
745 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400746}
747
Dees_Troy43d8b002012-09-17 16:00:01 -0400748bool TWPartition::Check_MD5(string restore_folder) {
749 string Full_Filename;
750 char split_filename[512];
751 int index = 0;
752
753 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400754 if (!TWFunc::Path_Exists(Full_Filename)) {
755 // This is a split archive, we presume
756 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
757 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
758 if (TWFunc::Check_MD5(split_filename) == 0) {
759 LOGE("MD5 failed to match on '%s'.\n", split_filename);
760 return false;
761 }
762 index++;
763 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400764 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400765 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400766 } else {
767 // Single file archive
768 if (TWFunc::Check_MD5(Full_Filename) == 0) {
769 LOGE("MD5 failed to match on '%s'.\n", split_filename);
770 return false;
771 } else
772 return true;
773 }
774 return false;
775}
776
Dees_Troy51a0e822012-09-05 15:24:24 -0400777bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400778 if (Backup_Method == FILES)
779 return Restore_Tar(restore_folder);
780 else if (Backup_Method == DD)
781 return Restore_DD(restore_folder);
782 else if (Backup_Method == FLASH_UTILS)
783 return Restore_Flash_Image(restore_folder);
784 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
785 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400786}
787
788string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400789 if (Backup_Method == NONE)
790 return "none";
791 else if (Backup_Method == FILES)
792 return "files";
793 else if (Backup_Method == DD)
794 return "dd";
795 else if (Backup_Method == FLASH_UTILS)
796 return "flash_utils";
797 else
798 return "undefined";
799 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400800}
801
802bool TWPartition::Decrypt(string Password) {
803 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400804 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400805 return 1;
806}
807
808bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400809 bool Save_Data_Media = Has_Data_Media;
810
811 if (!UnMount(true))
812 return false;
813
814 Current_File_System = Fstab_File_System;
815 Is_Encrypted = false;
816 Is_Decrypted = false;
817 Decrypted_Block_Device = "";
818 Has_Data_Media = false;
819 if (Wipe()) {
820 Has_Data_Media = Save_Data_Media;
821 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
822 Recreate_Media_Folder();
823 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400824 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400825 return true;
826 } else {
827 Has_Data_Media = Save_Data_Media;
828 LOGE("Unable to format to remove encryption.\n");
829 return false;
830 }
831 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400832}
833
834void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400835 FILE *fp;
836 string blkCommand;
837 char blkOutput[255];
838 char* blk;
839 char* arg;
840 char* ptr;
841
842 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
843 return; // Running blkid on some mtd devices causes a massive crash
844
Dees_Troy38bd7602012-09-14 13:33:53 -0400845 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400846 if (!Is_Present)
847 return;
Dees_Troy51127312012-09-08 13:08:49 -0400848
Dees_Troy8170a922012-09-18 15:40:25 -0400849 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
850 system("rm /tmp/blkidoutput.txt");
851
852 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400853 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400854 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400855 if (fp == NULL)
856 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400857 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
858 {
859 blk = blkOutput;
860 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400861 while (*ptr > 32 && *ptr != ':') ptr++;
862 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400863 *ptr = 0;
864
865 // Increment by two, but verify that we don't hit a NULL
866 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400867 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400868
869 // Now, find the TYPE field
870 while (1)
871 {
872 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400873 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400874 if (*ptr != 0)
875 {
876 *ptr = 0;
877 ptr++;
878 }
879
880 if (strlen(arg) > 6)
881 {
882 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
883 }
884
885 if (*ptr == 0)
886 {
887 arg = NULL;
888 break;
889 }
890 }
891
892 if (arg && strlen(arg) > 7)
893 {
894 arg += 6; // Skip the TYPE=" portion
895 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
896 }
897 else
898 continue;
899
Dees_Troy63c8df72012-09-10 14:02:05 -0400900 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400901 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
902 Current_File_System = arg;
903 }
904 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400905 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400906 return;
907}
908
909bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400910 if (!UnMount(true))
911 return false;
912
Dees_Troy43d8b002012-09-17 16:00:01 -0400913 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400914 char command[512];
915
916 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
917 Find_Actual_Block_Device();
918 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
919 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400920 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400921 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400922 ui_print("Done.\n");
923 return true;
924 } else {
925 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
926 return false;
927 }
928 } else
929 return Wipe_RMRF();
930
931 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400932}
933
934bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400935 if (!UnMount(true))
936 return false;
937
Dees_Troy43d8b002012-09-17 16:00:01 -0400938 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400939 string Command;
940
941 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
942 Find_Actual_Block_Device();
943 Command = "make_ext4fs";
944 if (!Is_Decrypted && Length != 0) {
945 // Only use length if we're not decrypted
946 char len[32];
947 sprintf(len, "%i", Length);
948 Command += " -l ";
949 Command += len;
950 }
951 Command += " " + Actual_Block_Device;
952 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400953 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400954 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400955 ui_print("Done.\n");
956 return true;
957 } else {
958 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
959 return false;
960 }
961 } else
962 return Wipe_EXT23();
963
964 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400965}
966
967bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400968 char command[512];
969
Dees_Troy43d8b002012-09-17 16:00:01 -0400970 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400971 if (!UnMount(true))
972 return false;
973
974 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
975 Find_Actual_Block_Device();
976 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400977 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400978 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400979 ui_print("Done.\n");
980 return true;
981 } else {
982 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
983 return false;
984 }
985 return true;
986 }
987 else
988 return Wipe_RMRF();
989
990 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400991}
992
Dees_Troy38bd7602012-09-14 13:33:53 -0400993bool TWPartition::Wipe_MTD() {
994 if (!UnMount(true))
995 return false;
996
997 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
998
999 mtd_scan_partitions();
1000 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1001 if (mtd == NULL) {
1002 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1003 return false;
1004 }
1005
1006 MtdWriteContext* ctx = mtd_write_partition(mtd);
1007 if (ctx == NULL) {
1008 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1009 return false;
1010 }
1011 if (mtd_erase_blocks(ctx, -1) == -1) {
1012 mtd_write_close(ctx);
1013 LOGE("Failed to format '%s'", MTD_Name.c_str());
1014 return false;
1015 }
1016 if (mtd_write_close(ctx) != 0) {
1017 LOGE("Failed to close '%s'", MTD_Name.c_str());
1018 return false;
1019 }
Dees_Troye58d5262012-09-21 12:27:57 -04001020 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001021 ui_print("Done.\n");
1022 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001023}
1024
1025bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001026 char cmd[512];
1027
1028 if (!Mount(true))
1029 return false;
1030
Dees_Troye58d5262012-09-21 12:27:57 -04001031 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1032 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001033
1034 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001035 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001036 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001037 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001038}
1039
1040bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001041 char cmd[256];
1042
1043 // This handles wiping data on devices with "sdcard" in /data/media
1044 if (!Mount(true))
1045 return false;
1046
1047 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001048 system("rm -f /data/*");
1049 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001050
1051 DIR* d;
1052 d = opendir("/data");
1053 if (d != NULL)
1054 {
1055 struct dirent* de;
1056 while ((de = readdir(d)) != NULL) {
1057 if (strcmp(de->d_name, "media") == 0) continue;
1058
1059 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001060 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001061 }
1062 closedir(d);
1063 }
1064 ui_print("Done.\n");
1065 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001066}
1067
1068bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001069 char back_name[255], split_index[5];
1070 string Full_FileName, Split_FileName, Tar_Args, Command;
1071 int use_compression, index, backup_count;
1072 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001073 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001074
1075 if (!Mount(true))
1076 return false;
1077
Dees_Troyb46a6842012-09-25 11:06:46 -04001078 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001079 ui_print("Backing up %s...\n", Display_Name.c_str());
1080
1081 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1082 if (use_compression)
1083 Tar_Args = "-cz";
1084 else
1085 Tar_Args = "-c";
1086
1087 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1088 Backup_FileName = back_name;
1089
Dees_Troy43d8b002012-09-17 16:00:01 -04001090 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1091 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001092 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001093 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001094 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001095 if (backup_count < 1) {
1096 LOGE("Error generating file list!\n");
1097 return false;
1098 }
1099 for (index=0; index<backup_count; index++) {
1100 sprintf(split_index, "%03i", index);
1101 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1102 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1103 LOGI("Backup command: '%s'\n", Command.c_str());
1104 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1105 system(Command.c_str()); // sending backup command formed earlier above
1106
Dees_Troy7c2dec82012-09-26 09:49:14 -04001107 file_size = TWFunc::Get_File_Size(Full_FileName);
1108 if (file_size == 0) {
1109 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 -04001110 return false;
1111 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001112 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001113 }
1114 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1115 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001116 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001117 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001118 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001119 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001120 else
Dees_Troye58d5262012-09-21 12:27:57 -04001121 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001122 LOGI("Backup command: '%s'\n", Command.c_str());
1123 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001124 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1125 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1126 return false;
1127 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001128 }
1129 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001130}
1131
1132bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001133 char back_name[255];
1134 string Full_FileName, Command;
1135 int use_compression;
1136
Dees_Troyb46a6842012-09-25 11:06:46 -04001137 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001138 ui_print("Backing up %s...\n", Display_Name.c_str());
1139
1140 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1141 Backup_FileName = back_name;
1142
1143 Full_FileName = backup_folder + "/" + Backup_FileName;
1144
1145 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1146 LOGI("Backup command: '%s'\n", Command.c_str());
1147 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001148 if (TWFunc::Get_File_Size(Full_FileName) != Backup_Size) {
1149 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);
1150 return false;
1151 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001152 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001153}
1154
1155bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001156 char back_name[255];
1157 string Full_FileName, Command;
1158 int use_compression;
1159
Dees_Troyb46a6842012-09-25 11:06:46 -04001160 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001161 ui_print("Backing up %s...\n", Display_Name.c_str());
1162
1163 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1164 Backup_FileName = back_name;
1165
1166 Full_FileName = backup_folder + "/" + Backup_FileName;
1167
1168 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1169 LOGI("Backup command: '%s'\n", Command.c_str());
1170 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001171 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1172 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1173 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1174 return false;
1175 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001176 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001177}
1178
1179bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001180 size_t first_period, second_period;
1181 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001182 int index = 0;
1183 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001184
Dees_Troyb46a6842012-09-25 11:06:46 -04001185 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001186 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1187
1188 // Parse backup filename to extract the file system before wiping
1189 first_period = Backup_FileName.find(".");
1190 if (first_period == string::npos) {
1191 LOGE("Unable to find file system (first period).\n");
1192 return false;
1193 }
1194 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1195 second_period = Restore_File_System.find(".");
1196 if (second_period == string::npos) {
1197 LOGE("Unable to find file system (second period).\n");
1198 return false;
1199 }
1200 Restore_File_System.resize(second_period);
1201 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1202 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001203 if (Has_Android_Secure) {
1204 ui_print("Wiping android secure...\n");
1205 if (!Wipe_AndSec())
1206 return false;
1207 } else if (!Wipe()) {
1208 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001209 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001210 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001211
1212 if (!Mount(true))
1213 return false;
1214
Dees_Troy43d8b002012-09-17 16:00:01 -04001215 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001216 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001217 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001218 // Backup is multiple archives
1219 LOGI("Backup is multiple archives.\n");
1220 sprintf(split_index, "%03i", index);
1221 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1222 while (TWFunc::Path_Exists(Full_FileName)) {
1223 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001224 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001225 LOGI("Restore command: '%s'\n", Command.c_str());
1226 system(Command.c_str());
1227 index++;
1228 sprintf(split_index, "%03i", index);
1229 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1230 }
1231 if (index == 0) {
1232 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1233 return false;
1234 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001235 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001236 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001237 LOGI("Restore command: '%s'\n", Command.c_str());
1238 system(Command.c_str());
1239 }
1240 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001241}
1242
1243bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001244 string Full_FileName, Command;
1245
Dees_Troyb46a6842012-09-25 11:06:46 -04001246 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001247 ui_print("Restoring %s...\n", Display_Name.c_str());
1248 Full_FileName = restore_folder + "/" + Backup_FileName;
1249 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1250 LOGI("Restore command: '%s'\n", Command.c_str());
1251 system(Command.c_str());
1252 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001253}
1254
1255bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001256 string Full_FileName, Command;
1257
Dees_Troyb46a6842012-09-25 11:06:46 -04001258 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001259 ui_print("Restoring %s...\n", Display_Name.c_str());
1260 Full_FileName = restore_folder + "/" + Backup_FileName;
1261 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1262 Command = "erase_image " + MTD_Name;
1263 LOGI("Erase command: '%s'\n", Command.c_str());
1264 system(Command.c_str());
1265 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1266 LOGI("Restore command: '%s'\n", Command.c_str());
1267 system(Command.c_str());
1268 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001269}
Dees_Troy5bf43922012-09-07 16:07:55 -04001270
1271bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001272 bool ret = false;
1273
Dees_Troyab10ee22012-09-21 14:27:30 -04001274 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001275 return false;
1276
Dees_Troy38bd7602012-09-14 13:33:53 -04001277 if (Removable || Is_Encrypted) {
1278 if (!Mount(false))
1279 return true;
1280 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001281 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001282
1283 ret = Get_Size_Via_statfs(Display_Error);
1284 if (!ret || Size == 0)
1285 if (!Get_Size_Via_df(Display_Error))
1286 return false;
1287
Dees_Troy5bf43922012-09-07 16:07:55 -04001288 if (Has_Data_Media) {
1289 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001290 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001291 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1292 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001293 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001294 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001295 int bak = (int)(Backup_Size / 1048576LLU);
1296 int total = (int)(Size / 1048576LLU);
1297 int us = (int)(Used / 1048576LLU);
1298 int fre = (int)(Free / 1048576LLU);
1299 int datmed = (int)(data_media_used / 1048576LLU);
1300 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 -04001301 } else
1302 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001303 } else if (Has_Android_Secure) {
1304 if (Mount(Display_Error))
1305 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
1306 else
1307 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001308 }
1309 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001310}
Dees_Troy38bd7602012-09-14 13:33:53 -04001311
1312void TWPartition::Find_Actual_Block_Device(void) {
1313 if (Is_Decrypted) {
1314 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001315 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001316 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001317 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001318 Is_Present = true;
1319 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001320 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001321 Flip_Block_Device();
1322 Actual_Block_Device = Primary_Block_Device;
1323 Is_Present = true;
1324 } else
1325 Is_Present = false;
1326}
1327
1328void TWPartition::Recreate_Media_Folder(void) {
1329 string Command;
1330
1331 if (!Mount(true)) {
1332 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001333 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001334 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001335 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001336 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001337 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001338 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001339 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001340 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001341}
Dees_Troye58d5262012-09-21 12:27:57 -04001342
1343void TWPartition::Recreate_AndSec_Folder(void) {
1344 string Command;
1345
1346 if (!Has_Android_Secure)
1347 return;
1348
1349 if (!Mount(true)) {
1350 LOGE("Unable to recreate android secure folder.\n");
1351 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1352 LOGI("Recreating android secure folder.\n");
1353 TWFunc::Recursive_Mkdir(Symlink_Path);
1354 Command = "umount " + Symlink_Mount_Point;
1355 system(Command.c_str());
1356 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1357 system(Command.c_str());
1358 }
1359}