blob: 4d1c9c5ad33b34c3ec0f611ea4ddfd464ac50046 [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_Troy2c50e182012-09-26 20:05:28 -0400228 } else if (Mount_Point == "/boot") {
229 Display_Name = "Boot";
230 DataManager::SetValue("tw_boot_is_mountable", 1);
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") {
269 int backup_display_size = (int)(Backup_Size / 1048576LLU);
270 DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size);
271 if (Backup_Size == 0) {
272 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0);
273 DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0);
274 } else
275 DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1);
276 } else if (Mount_Point == "/recovery") {
277 int backup_display_size = (int)(Backup_Size / 1048576LLU);
278 DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size);
279 if (Backup_Size == 0) {
280 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0);
281 DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0);
282 } else
283 DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1);
284 }
Dees_Troyb1dab8d2012-09-26 12:16:41 -0400285#ifdef SP1_NAME
286 string SP1_Path = "/";
287 SP1_Path += EXPAND(SP1_NAME);
288 if (Mount_Point == SP1_Path) {
289 int backup_display_size = (int)(Backup_Size / 1048576LLU);
290 DataManager::SetValue(TW_BACKUP_SP1_SIZE, backup_display_size);
291 }
292#endif
293#ifdef SP2_NAME
294 string SP2_Path = "/";
295 SP2_Path += EXPAND(SP2_NAME);
296 if (Mount_Point == SP2_Path) {
297 int backup_display_size = (int)(Backup_Size / 1048576LLU);
298 DataManager::SetValue(TW_BACKUP_SP2_SIZE, backup_display_size);
299 }
300#endif
301#ifdef SP3_NAME
302 string SP3_Path = "/";
303 SP3_Path += EXPAND(SP3_NAME);
304 if (Mount_Point == SP3_Path) {
305 int backup_display_size = (int)(Backup_Size / 1048576LLU);
306 DataManager::SetValue(TW_BACKUP_SP3_SIZE, backup_display_size);
307 }
308#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400309 }
310
Dees_Troy51127312012-09-08 13:08:49 -0400311 // Process any custom flags
312 if (Flags.size() > 0)
313 Process_Flags(Flags, Display_Error);
314
315 return true;
316}
317
318bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
319 char flags[MAX_FSTAB_LINE_LENGTH];
320 int flags_len, index = 0;
321 char* ptr;
322
323 strcpy(flags, Flags.c_str());
324 flags_len = Flags.size();
325 for (index = 0; index < flags_len; index++) {
326 if (flags[index] == ';')
327 flags[index] = '\0';
328 }
329
330 index = 0;
331 while (index < flags_len) {
332 while (index < flags_len && flags[index] == '\0')
333 index++;
334 if (index >= flags_len)
335 continue;
336 ptr = flags + index;
337 if (strcmp(ptr, "removable") == 0) {
338 Removable = true;
339 } else if (strcmp(ptr, "storage") == 0) {
340 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400341 } else if (strcmp(ptr, "canbewiped") == 0) {
342 Can_Be_Wiped = true;
343 } else if (strcmp(ptr, "wipeingui") == 0) {
344 Can_Be_Wiped = true;
345 Wipe_Available_in_GUI = true;
346 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
347 Can_Be_Wiped = true;
348 Wipe_Available_in_GUI = true;
349 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400350 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
351 ptr += 13;
352 Is_SubPartition = true;
353 SubPartition_Of = ptr;
354 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
355 ptr += 8;
356 Symlink_Path = ptr;
357 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
358 ptr += 8;
359 Display_Name = ptr;
360 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
361 ptr += 10;
362 Format_Block_Size = atoi(ptr);
363 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
364 ptr += 7;
365 Length = atoi(ptr);
366 } else {
367 if (Display_Error)
368 LOGE("Unhandled flag: '%s'\n", ptr);
369 else
370 LOGI("Unhandled flag: '%s'\n", ptr);
371 }
372 while (index < flags_len && flags[index] != '\0')
373 index++;
374 }
375 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400376}
377
Dees_Troy5bf43922012-09-07 16:07:55 -0400378bool TWPartition::Is_File_System(string File_System) {
379 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400380 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400381 File_System == "ext4" ||
382 File_System == "vfat" ||
383 File_System == "ntfs" ||
384 File_System == "yaffs2" ||
385 File_System == "auto")
386 return true;
387 else
388 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400389}
390
Dees_Troy5bf43922012-09-07 16:07:55 -0400391bool TWPartition::Is_Image(string File_System) {
392 if (File_System == "emmc" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400393 File_System == "mtd")
Dees_Troy5bf43922012-09-07 16:07:55 -0400394 return true;
395 else
396 return false;
397}
398
Dees_Troy51127312012-09-08 13:08:49 -0400399bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400400 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400401 if (mkdir(Path.c_str(), 0777) == -1) {
402 if (Display_Error)
403 LOGE("Can not create '%s' folder.\n", Path.c_str());
404 else
405 LOGI("Can not create '%s' folder.\n", Path.c_str());
406 return false;
407 } else {
408 LOGI("Created '%s' folder.\n", Path.c_str());
409 return true;
410 }
411 }
412 return true;
413}
414
Dees_Troy5bf43922012-09-07 16:07:55 -0400415void TWPartition::Setup_File_System(bool Display_Error) {
416 struct statfs st;
417
418 Can_Be_Mounted = true;
419 Can_Be_Wiped = true;
420
Dees_Troy5bf43922012-09-07 16:07:55 -0400421 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400422 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400423 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
424 Backup_Name = Display_Name;
425 Backup_Method = FILES;
426}
427
428void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400429 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
430 Backup_Name = Display_Name;
431 if (Fstab_File_System == "emmc")
432 Backup_Method = DD;
433 else if (Fstab_File_System == "mtd")
434 Backup_Method = FLASH_UTILS;
435 else
436 LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str());
437 if (Find_Partition_Size()) {
438 Used = Size;
439 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400440 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400441 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400442 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400444 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400445 }
446}
447
Dees_Troye58d5262012-09-21 12:27:57 -0400448void TWPartition::Setup_AndSec(void) {
449 Backup_Name = "and-sec";
450 Has_Android_Secure = true;
451 Symlink_Path = Mount_Point + "/.android_secure";
452 Symlink_Mount_Point = "/and-sec";
453 Backup_Path = Symlink_Mount_Point;
454 Make_Dir("/and-sec", true);
455 Recreate_AndSec_Folder();
456}
457
Dees_Troy5bf43922012-09-07 16:07:55 -0400458void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
459 char device[512], realDevice[512];
460
461 strcpy(device, Block.c_str());
462 memset(realDevice, 0, sizeof(realDevice));
463 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
464 {
465 strcpy(device, realDevice);
466 memset(realDevice, 0, sizeof(realDevice));
467 }
468
469 if (device[0] != '/') {
470 if (Display_Error)
471 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
472 else
473 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
474 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400475 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400476 Block = device;
477 return;
478 }
479}
480
Dees_Troy38bd7602012-09-14 13:33:53 -0400481bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
482 FILE *fp = NULL;
483 char line[255];
484
485 fp = fopen("/proc/mtd", "rt");
486 if (fp == NULL) {
487 LOGE("Device does not support /proc/mtd\n");
488 return false;
489 }
490
491 while (fgets(line, sizeof(line), fp) != NULL)
492 {
493 char device[32], label[32];
494 unsigned long size = 0;
495 char* fstype = NULL;
496 int deviceId;
497
498 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
499
500 // Skip header and blank lines
501 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
502 continue;
503
504 // Strip off the trailing " from the label
505 label[strlen(label)-1] = '\0';
506
507 if (strcmp(label, MTD_Name.c_str()) == 0) {
508 // We found our device
509 // Strip off the trailing : from the device
510 device[strlen(device)-1] = '\0';
511 if (sscanf(device,"mtd%d", &deviceId) == 1) {
512 sprintf(device, "/dev/block/mtdblock%d", deviceId);
513 Primary_Block_Device = device;
514 }
515 }
516 }
517 fclose(fp);
518
519 return false;
520}
521
Dees_Troy51127312012-09-08 13:08:49 -0400522bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
523 struct statfs st;
524 string Local_Path = Mount_Point + "/.";
525
526 if (!Mount(Display_Error))
527 return false;
528
529 if (statfs(Local_Path.c_str(), &st) != 0) {
530 if (!Removable) {
531 if (Display_Error)
532 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
533 else
534 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
535 }
536 return false;
537 }
538 Size = (st.f_blocks * st.f_bsize);
539 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
540 Free = (st.f_bfree * st.f_bsize);
541 Backup_Size = Used;
542 return true;
543}
544
545bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400546 FILE* fp;
547 char command[255], line[512];
548 int include_block = 1;
549 unsigned int min_len;
550
551 if (!Mount(Display_Error))
552 return false;
553
Dees_Troy38bd7602012-09-14 13:33:53 -0400554 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400555 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400556 system(command);
Dees_Troy51127312012-09-08 13:08:49 -0400557 fp = fopen("/tmp/dfoutput.txt", "rt");
558 if (fp == NULL) {
559 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400560 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400561 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400562
563 while (fgets(line, sizeof(line), fp) != NULL)
564 {
565 unsigned long blocks, used, available;
566 char device[64];
567 char tmpString[64];
568
569 if (strncmp(line, "Filesystem", 10) == 0)
570 continue;
571 if (strlen(line) < min_len) {
572 include_block = 0;
573 continue;
574 }
575 if (include_block) {
576 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
577 } else {
578 // The device block string is so long that the df information is on the next line
579 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400580 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400581 while (tmpString[space_count] == 32)
582 space_count++;
583 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
584 }
585
586 // Adjust block size to byte size
587 Size = blocks * 1024ULL;
588 Used = used * 1024ULL;
589 Free = available * 1024ULL;
590 Backup_Size = Used;
591 }
592 fclose(fp);
593 return true;
594}
595
Dees_Troy5bf43922012-09-07 16:07:55 -0400596bool TWPartition::Find_Partition_Size(void) {
597 FILE* fp;
598 char line[512];
599 string tmpdevice;
600
601 // In this case, we'll first get the partitions we care about (with labels)
602 fp = fopen("/proc/partitions", "rt");
603 if (fp == NULL)
604 return false;
605
606 while (fgets(line, sizeof(line), fp) != NULL)
607 {
608 unsigned long major, minor, blocks;
609 char device[512];
610 char tmpString[64];
611
Dees_Troy63c8df72012-09-10 14:02:05 -0400612 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400613 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
614
615 tmpdevice = "/dev/block/";
616 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400617 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400618 // Adjust block size to byte size
619 Size = blocks * 1024ULL;
620 fclose(fp);
621 return true;
622 }
623 }
624 fclose(fp);
625 return false;
626}
627
Dees_Troy5bf43922012-09-07 16:07:55 -0400628void TWPartition::Flip_Block_Device(void) {
629 string temp;
630
631 temp = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400632 Primary_Block_Device = Alternate_Block_Device;
Dees_Troy5bf43922012-09-07 16:07:55 -0400633 Alternate_Block_Device = temp;
634}
635
636bool TWPartition::Is_Mounted(void) {
637 if (!Can_Be_Mounted)
638 return false;
639
640 struct stat st1, st2;
641 string test_path;
642
643 // Check to see if the mount point directory exists
644 test_path = Mount_Point + "/.";
645 if (stat(test_path.c_str(), &st1) != 0) return false;
646
647 // Check to see if the directory above the mount point exists
648 test_path = Mount_Point + "/../.";
649 if (stat(test_path.c_str(), &st2) != 0) return false;
650
651 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
652 int ret = (st1.st_dev != st2.st_dev) ? true : false;
653
654 return ret;
655}
656
657bool TWPartition::Mount(bool Display_Error) {
658 if (Is_Mounted()) {
659 return true;
660 } else if (!Can_Be_Mounted) {
661 return false;
662 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400663
664 Find_Actual_Block_Device();
665
666 // Check the current file system before mounting
667 Check_FS_Type();
668
669 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
670 if (Display_Error)
671 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
672 else
673 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
674 return false;
675 } else {
676 if (Removable)
677 Update_Size(Display_Error);
678
679 if (!Symlink_Mount_Point.empty()) {
680 string Command;
681
682 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400683 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400684 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400685 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400686 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400687 return true;
688}
689
690bool TWPartition::UnMount(bool Display_Error) {
691 if (Is_Mounted()) {
692 int never_unmount_system;
693
694 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
695 if (never_unmount_system == 1 && Mount_Point == "/system")
696 return true; // Never unmount system if you're not supposed to unmount it
697
Dees_Troy38bd7602012-09-14 13:33:53 -0400698 if (!Symlink_Mount_Point.empty())
699 umount(Symlink_Mount_Point.c_str());
700
Dees_Troy5bf43922012-09-07 16:07:55 -0400701 if (umount(Mount_Point.c_str()) != 0) {
702 if (Display_Error)
703 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
704 else
705 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
706 return false;
707 } else
708 return true;
709 } else {
710 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400711 }
712}
713
714bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400715 if (!Can_Be_Wiped) {
716 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
717 return false;
718 }
719
Dees_Troyc51f1f92012-09-20 15:32:13 -0400720 if (Mount_Point == "/cache")
721 tmplog_offset = 0;
722
Dees_Troy38bd7602012-09-14 13:33:53 -0400723 if (Has_Data_Media)
724 return Wipe_Data_Without_Wiping_Media();
725
726 int check;
727 DataManager::GetValue(TW_RM_RF_VAR, check);
728 if (check)
729 return Wipe_RMRF();
730
731 if (Current_File_System == "ext4")
732 return Wipe_EXT4();
733
734 if (Current_File_System == "ext2" || Current_File_System == "ext3")
735 return Wipe_EXT23();
736
737 if (Current_File_System == "vfat")
738 return Wipe_FAT();
739
740 if (Current_File_System == "yaffs2")
741 return Wipe_MTD();
742
743 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
744 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400745}
746
Dees_Troye58d5262012-09-21 12:27:57 -0400747bool TWPartition::Wipe_AndSec(void) {
748 if (!Has_Android_Secure)
749 return false;
750
751 char cmd[512];
752
753 if (!Mount(true))
754 return false;
755
756 ui_print("Using rm -rf on .android_secure\n");
757 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
758
759 LOGI("rm -rf command is: '%s'\n", cmd);
760 system(cmd);
761 return true;
762}
763
Dees_Troy51a0e822012-09-05 15:24:24 -0400764bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400765 if (Backup_Method == FILES)
766 return Backup_Tar(backup_folder);
767 else if (Backup_Method == DD)
768 return Backup_DD(backup_folder);
769 else if (Backup_Method == FLASH_UTILS)
770 return Backup_Dump_Image(backup_folder);
771 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
772 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400773}
774
Dees_Troy43d8b002012-09-17 16:00:01 -0400775bool TWPartition::Check_MD5(string restore_folder) {
776 string Full_Filename;
777 char split_filename[512];
778 int index = 0;
779
780 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400781 if (!TWFunc::Path_Exists(Full_Filename)) {
782 // This is a split archive, we presume
783 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
784 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
785 if (TWFunc::Check_MD5(split_filename) == 0) {
786 LOGE("MD5 failed to match on '%s'.\n", split_filename);
787 return false;
788 }
789 index++;
790 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400791 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400792 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400793 } else {
794 // Single file archive
795 if (TWFunc::Check_MD5(Full_Filename) == 0) {
796 LOGE("MD5 failed to match on '%s'.\n", split_filename);
797 return false;
798 } else
799 return true;
800 }
801 return false;
802}
803
Dees_Troy51a0e822012-09-05 15:24:24 -0400804bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400805 if (Backup_Method == FILES)
806 return Restore_Tar(restore_folder);
807 else if (Backup_Method == DD)
808 return Restore_DD(restore_folder);
809 else if (Backup_Method == FLASH_UTILS)
810 return Restore_Flash_Image(restore_folder);
811 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
812 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400813}
814
815string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400816 if (Backup_Method == NONE)
817 return "none";
818 else if (Backup_Method == FILES)
819 return "files";
820 else if (Backup_Method == DD)
821 return "dd";
822 else if (Backup_Method == FLASH_UTILS)
823 return "flash_utils";
824 else
825 return "undefined";
826 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400827}
828
829bool TWPartition::Decrypt(string Password) {
830 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400831 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400832 return 1;
833}
834
835bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400836 bool Save_Data_Media = Has_Data_Media;
837
838 if (!UnMount(true))
839 return false;
840
841 Current_File_System = Fstab_File_System;
842 Is_Encrypted = false;
843 Is_Decrypted = false;
844 Decrypted_Block_Device = "";
845 Has_Data_Media = false;
846 if (Wipe()) {
847 Has_Data_Media = Save_Data_Media;
848 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
849 Recreate_Media_Folder();
850 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400851 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400852 return true;
853 } else {
854 Has_Data_Media = Save_Data_Media;
855 LOGE("Unable to format to remove encryption.\n");
856 return false;
857 }
858 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400859}
860
861void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400862 FILE *fp;
863 string blkCommand;
864 char blkOutput[255];
865 char* blk;
866 char* arg;
867 char* ptr;
868
869 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
870 return; // Running blkid on some mtd devices causes a massive crash
871
Dees_Troy38bd7602012-09-14 13:33:53 -0400872 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400873 if (!Is_Present)
874 return;
Dees_Troy51127312012-09-08 13:08:49 -0400875
Dees_Troy8170a922012-09-18 15:40:25 -0400876 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
877 system("rm /tmp/blkidoutput.txt");
878
879 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400880 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400881 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400882 if (fp == NULL)
883 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400884 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
885 {
886 blk = blkOutput;
887 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400888 while (*ptr > 32 && *ptr != ':') ptr++;
889 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400890 *ptr = 0;
891
892 // Increment by two, but verify that we don't hit a NULL
893 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400894 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400895
896 // Now, find the TYPE field
897 while (1)
898 {
899 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400900 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400901 if (*ptr != 0)
902 {
903 *ptr = 0;
904 ptr++;
905 }
906
907 if (strlen(arg) > 6)
908 {
909 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
910 }
911
912 if (*ptr == 0)
913 {
914 arg = NULL;
915 break;
916 }
917 }
918
919 if (arg && strlen(arg) > 7)
920 {
921 arg += 6; // Skip the TYPE=" portion
922 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
923 }
924 else
925 continue;
926
Dees_Troy63c8df72012-09-10 14:02:05 -0400927 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400928 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
929 Current_File_System = arg;
930 }
931 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400932 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400933 return;
934}
935
936bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400937 if (!UnMount(true))
938 return false;
939
Dees_Troy43d8b002012-09-17 16:00:01 -0400940 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400941 char command[512];
942
943 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
944 Find_Actual_Block_Device();
945 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
946 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400947 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400948 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400949 ui_print("Done.\n");
950 return true;
951 } else {
952 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
953 return false;
954 }
955 } else
956 return Wipe_RMRF();
957
958 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400959}
960
961bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400962 if (!UnMount(true))
963 return false;
964
Dees_Troy43d8b002012-09-17 16:00:01 -0400965 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400966 string Command;
967
968 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
969 Find_Actual_Block_Device();
970 Command = "make_ext4fs";
971 if (!Is_Decrypted && Length != 0) {
972 // Only use length if we're not decrypted
973 char len[32];
974 sprintf(len, "%i", Length);
975 Command += " -l ";
976 Command += len;
977 }
978 Command += " " + Actual_Block_Device;
979 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400980 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400981 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400982 ui_print("Done.\n");
983 return true;
984 } else {
985 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
986 return false;
987 }
988 } else
989 return Wipe_EXT23();
990
991 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400992}
993
994bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400995 char command[512];
996
Dees_Troy43d8b002012-09-17 16:00:01 -0400997 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400998 if (!UnMount(true))
999 return false;
1000
1001 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1002 Find_Actual_Block_Device();
1003 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -04001004 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -04001005 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001006 ui_print("Done.\n");
1007 return true;
1008 } else {
1009 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1010 return false;
1011 }
1012 return true;
1013 }
1014 else
1015 return Wipe_RMRF();
1016
1017 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001018}
1019
Dees_Troy38bd7602012-09-14 13:33:53 -04001020bool TWPartition::Wipe_MTD() {
1021 if (!UnMount(true))
1022 return false;
1023
1024 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1025
1026 mtd_scan_partitions();
1027 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1028 if (mtd == NULL) {
1029 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1030 return false;
1031 }
1032
1033 MtdWriteContext* ctx = mtd_write_partition(mtd);
1034 if (ctx == NULL) {
1035 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1036 return false;
1037 }
1038 if (mtd_erase_blocks(ctx, -1) == -1) {
1039 mtd_write_close(ctx);
1040 LOGE("Failed to format '%s'", MTD_Name.c_str());
1041 return false;
1042 }
1043 if (mtd_write_close(ctx) != 0) {
1044 LOGE("Failed to close '%s'", MTD_Name.c_str());
1045 return false;
1046 }
Dees_Troye58d5262012-09-21 12:27:57 -04001047 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001048 ui_print("Done.\n");
1049 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001050}
1051
1052bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001053 char cmd[512];
1054
1055 if (!Mount(true))
1056 return false;
1057
Dees_Troye58d5262012-09-21 12:27:57 -04001058 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1059 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001060
1061 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001062 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001063 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001064 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001065}
1066
1067bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001068 char cmd[256];
1069
1070 // This handles wiping data on devices with "sdcard" in /data/media
1071 if (!Mount(true))
1072 return false;
1073
1074 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001075 system("rm -f /data/*");
1076 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001077
1078 DIR* d;
1079 d = opendir("/data");
1080 if (d != NULL)
1081 {
1082 struct dirent* de;
1083 while ((de = readdir(d)) != NULL) {
1084 if (strcmp(de->d_name, "media") == 0) continue;
1085
1086 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001087 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001088 }
1089 closedir(d);
1090 }
1091 ui_print("Done.\n");
1092 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001093}
1094
1095bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001096 char back_name[255], split_index[5];
1097 string Full_FileName, Split_FileName, Tar_Args, Command;
1098 int use_compression, index, backup_count;
1099 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001100 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001101
1102 if (!Mount(true))
1103 return false;
1104
Dees_Troy2c50e182012-09-26 20:05:28 -04001105 if (Backup_Path == "/and-sec") {
1106 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1107 ui_print("Backing up %s...\n", "Android Secure");
1108 } else {
1109 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1110 ui_print("Backing up %s...\n", Display_Name.c_str());
1111 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001112
1113 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1114 if (use_compression)
1115 Tar_Args = "-cz";
1116 else
1117 Tar_Args = "-c";
1118
1119 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1120 Backup_FileName = back_name;
1121
Dees_Troy43d8b002012-09-17 16:00:01 -04001122 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1123 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001124 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001125 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001126 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001127 if (backup_count < 1) {
1128 LOGE("Error generating file list!\n");
1129 return false;
1130 }
1131 for (index=0; index<backup_count; index++) {
1132 sprintf(split_index, "%03i", index);
1133 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1134 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1135 LOGI("Backup command: '%s'\n", Command.c_str());
1136 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1137 system(Command.c_str()); // sending backup command formed earlier above
1138
Dees_Troy7c2dec82012-09-26 09:49:14 -04001139 file_size = TWFunc::Get_File_Size(Full_FileName);
1140 if (file_size == 0) {
1141 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 -04001142 return false;
1143 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001144 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001145 }
1146 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1147 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001148 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001149 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001150 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001151 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001152 else
Dees_Troye58d5262012-09-21 12:27:57 -04001153 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001154 LOGI("Backup command: '%s'\n", Command.c_str());
1155 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001156 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1157 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1158 return false;
1159 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001160 }
1161 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001162}
1163
1164bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001165 char back_name[255];
1166 string Full_FileName, Command;
1167 int use_compression;
1168
Dees_Troyb46a6842012-09-25 11:06:46 -04001169 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001170 ui_print("Backing up %s...\n", Display_Name.c_str());
1171
1172 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1173 Backup_FileName = back_name;
1174
1175 Full_FileName = backup_folder + "/" + Backup_FileName;
1176
1177 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1178 LOGI("Backup command: '%s'\n", Command.c_str());
1179 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001180 if (TWFunc::Get_File_Size(Full_FileName) != Backup_Size) {
1181 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);
1182 return false;
1183 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001184 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001185}
1186
1187bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001188 char back_name[255];
1189 string Full_FileName, Command;
1190 int use_compression;
1191
Dees_Troyb46a6842012-09-25 11:06:46 -04001192 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001193 ui_print("Backing up %s...\n", Display_Name.c_str());
1194
1195 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1196 Backup_FileName = back_name;
1197
1198 Full_FileName = backup_folder + "/" + Backup_FileName;
1199
1200 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1201 LOGI("Backup command: '%s'\n", Command.c_str());
1202 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001203 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1204 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1205 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1206 return false;
1207 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001208 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001209}
1210
1211bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001212 size_t first_period, second_period;
1213 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001214 int index = 0;
1215 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001216
Dees_Troyb46a6842012-09-25 11:06:46 -04001217 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001218 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1219
1220 // Parse backup filename to extract the file system before wiping
1221 first_period = Backup_FileName.find(".");
1222 if (first_period == string::npos) {
1223 LOGE("Unable to find file system (first period).\n");
1224 return false;
1225 }
1226 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1227 second_period = Restore_File_System.find(".");
1228 if (second_period == string::npos) {
1229 LOGE("Unable to find file system (second period).\n");
1230 return false;
1231 }
1232 Restore_File_System.resize(second_period);
1233 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1234 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001235 if (Has_Android_Secure) {
1236 ui_print("Wiping android secure...\n");
1237 if (!Wipe_AndSec())
1238 return false;
1239 } else if (!Wipe()) {
1240 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001241 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001242 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001243
1244 if (!Mount(true))
1245 return false;
1246
Dees_Troy43d8b002012-09-17 16:00:01 -04001247 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001248 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001249 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001250 // Backup is multiple archives
1251 LOGI("Backup is multiple archives.\n");
1252 sprintf(split_index, "%03i", index);
1253 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1254 while (TWFunc::Path_Exists(Full_FileName)) {
1255 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001256 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001257 LOGI("Restore command: '%s'\n", Command.c_str());
1258 system(Command.c_str());
1259 index++;
1260 sprintf(split_index, "%03i", index);
1261 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1262 }
1263 if (index == 0) {
1264 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1265 return false;
1266 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001267 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001268 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001269 LOGI("Restore command: '%s'\n", Command.c_str());
1270 system(Command.c_str());
1271 }
1272 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001273}
1274
1275bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001276 string Full_FileName, Command;
1277
Dees_Troyb46a6842012-09-25 11:06:46 -04001278 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001279 ui_print("Restoring %s...\n", Display_Name.c_str());
1280 Full_FileName = restore_folder + "/" + Backup_FileName;
1281 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1282 LOGI("Restore command: '%s'\n", Command.c_str());
1283 system(Command.c_str());
1284 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001285}
1286
1287bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001288 string Full_FileName, Command;
1289
Dees_Troyb46a6842012-09-25 11:06:46 -04001290 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001291 ui_print("Restoring %s...\n", Display_Name.c_str());
1292 Full_FileName = restore_folder + "/" + Backup_FileName;
1293 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1294 Command = "erase_image " + MTD_Name;
1295 LOGI("Erase command: '%s'\n", Command.c_str());
1296 system(Command.c_str());
1297 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1298 LOGI("Restore command: '%s'\n", Command.c_str());
1299 system(Command.c_str());
1300 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001301}
Dees_Troy5bf43922012-09-07 16:07:55 -04001302
1303bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001304 bool ret = false;
1305
Dees_Troyab10ee22012-09-21 14:27:30 -04001306 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001307 return false;
1308
Dees_Troy38bd7602012-09-14 13:33:53 -04001309 if (Removable || Is_Encrypted) {
1310 if (!Mount(false))
1311 return true;
1312 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001313 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001314
1315 ret = Get_Size_Via_statfs(Display_Error);
1316 if (!ret || Size == 0)
1317 if (!Get_Size_Via_df(Display_Error))
1318 return false;
1319
Dees_Troy5bf43922012-09-07 16:07:55 -04001320 if (Has_Data_Media) {
1321 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001322 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001323 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1324 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001325 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001326 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001327 int bak = (int)(Backup_Size / 1048576LLU);
1328 int total = (int)(Size / 1048576LLU);
1329 int us = (int)(Used / 1048576LLU);
1330 int fre = (int)(Free / 1048576LLU);
1331 int datmed = (int)(data_media_used / 1048576LLU);
1332 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 -04001333 } else
1334 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001335 } else if (Has_Android_Secure) {
1336 if (Mount(Display_Error))
1337 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
1338 else
1339 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001340 }
1341 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001342}
Dees_Troy38bd7602012-09-14 13:33:53 -04001343
1344void TWPartition::Find_Actual_Block_Device(void) {
1345 if (Is_Decrypted) {
1346 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001347 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001348 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001349 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001350 Is_Present = true;
1351 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001352 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001353 Flip_Block_Device();
1354 Actual_Block_Device = Primary_Block_Device;
1355 Is_Present = true;
1356 } else
1357 Is_Present = false;
1358}
1359
1360void TWPartition::Recreate_Media_Folder(void) {
1361 string Command;
1362
1363 if (!Mount(true)) {
1364 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001365 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001366 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001367 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001368 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001369 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001370 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001371 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001372 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001373}
Dees_Troye58d5262012-09-21 12:27:57 -04001374
1375void TWPartition::Recreate_AndSec_Folder(void) {
1376 string Command;
1377
1378 if (!Has_Android_Secure)
1379 return;
1380
1381 if (!Mount(true)) {
1382 LOGE("Unable to recreate android secure folder.\n");
1383 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1384 LOGI("Recreating android secure folder.\n");
1385 TWFunc::Recursive_Mkdir(Symlink_Path);
1386 Command = "umount " + Symlink_Mount_Point;
1387 system(Command.c_str());
1388 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1389 system(Command.c_str());
1390 }
1391}