blob: 3ef206a80559dfaa2d549c727737904efa7214a1 [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_Troy5bf43922012-09-07 16:07:55 -040045}
Dees_Troy51a0e822012-09-05 15:24:24 -040046
47TWPartition::TWPartition(void) {
48 Can_Be_Mounted = false;
49 Can_Be_Wiped = false;
50 Wipe_During_Factory_Reset = false;
51 Wipe_Available_in_GUI = false;
52 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040053 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040054 SubPartition_Of = "";
55 Symlink_Path = "";
56 Symlink_Mount_Point = "";
57 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040058 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040059 Actual_Block_Device = "";
60 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040061 Alternate_Block_Device = "";
62 Removable = false;
63 Is_Present = false;
64 Length = 0;
65 Size = 0;
66 Used = 0;
67 Free = 0;
68 Backup_Size = 0;
69 Can_Be_Encrypted = false;
70 Is_Encrypted = false;
71 Is_Decrypted = false;
72 Decrypted_Block_Device = "";
73 Display_Name = "";
74 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040075 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040076 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040077 Backup_Method = NONE;
78 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040079 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040080 Is_Storage = false;
81 Storage_Path = "";
82 Current_File_System = "";
83 Fstab_File_System = "";
84 Format_Block_Size = 0;
85}
86
87TWPartition::~TWPartition(void) {
88 // Do nothing
89}
90
Dees_Troy5bf43922012-09-07 16:07:55 -040091bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
92 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
93 int line_len = Line.size(), index = 0, item_index = 0;
94 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -040095 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -040096
97 strncpy(full_line, Line.c_str(), line_len);
98
Dees_Troy51127312012-09-08 13:08:49 -040099 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400100 if (full_line[index] <= 32)
101 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400102 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400103 Mount_Point = full_line;
104 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400105 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400106 index = Mount_Point.size();
107 while (index < line_len) {
108 while (index < line_len && full_line[index] == '\0')
109 index++;
110 if (index >= line_len)
111 continue;
112 ptr = full_line + index;
113 if (item_index == 0) {
114 // File System
115 Fstab_File_System = ptr;
116 Current_File_System = ptr;
117 item_index++;
118 } else if (item_index == 1) {
119 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400120 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400121 MTD_Name = ptr;
122 Find_MTD_Block_Device(MTD_Name);
Dees_Troy38bd7602012-09-14 13:33:53 -0400123 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400124 if (Display_Error)
125 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
126 else
127 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
128 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400129 } else {
130 Primary_Block_Device = ptr;
131 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400132 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400133 item_index++;
134 } else if (item_index > 1) {
135 if (*ptr == '/') {
136 // Alternate Block Device
137 Alternate_Block_Device = ptr;
138 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
139 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
140 // Partition length
141 ptr += 7;
142 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400143 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
144 // Custom flags, save for later so that new values aren't overwritten by defaults
145 ptr += 6;
146 Flags = ptr;
Dees_Troy38bd7602012-09-14 13:33:53 -0400147 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
148 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400149 } else {
150 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400151 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400152 }
153 }
154 while (index < line_len && full_line[index] != '\0')
155 index++;
156 }
157
158 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
159 if (Display_Error)
160 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
161 else
162 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
163 return 0;
164 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400165 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400166 Setup_File_System(Display_Error);
167 if (Mount_Point == "/system") {
168 Display_Name = "System";
169 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400170 } else if (Mount_Point == "/data") {
171 Display_Name = "Data";
172 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400173 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400174#ifdef RECOVERY_SDCARD_ON_DATA
175 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400176 Is_Storage = true;
177 Storage_Path = "/data/media";
Dees_Troyb46a6842012-09-25 11:06:46 -0400178 Recreate_Media_Folder();
Dees_Troy657c3092012-09-10 20:32:10 -0400179 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
180 Make_Dir("/emmc", Display_Error);
181 Symlink_Path = "/data/media";
182 Symlink_Mount_Point = "/emmc";
183 } else {
184 Make_Dir("/sdcard", Display_Error);
185 Symlink_Path = "/data/media";
186 Symlink_Mount_Point = "/sdcard";
187 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400188#endif
189#ifdef TW_INCLUDE_CRYPTO
190 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400191 char crypto_blkdev[255];
192 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
193 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400194 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400195 DataManager::SetValue(TW_IS_DECRYPTED, 1);
196 Is_Encrypted = true;
197 Is_Decrypted = true;
198 Decrypted_Block_Device = crypto_blkdev;
199 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
200 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400201 Is_Encrypted = true;
202 Is_Decrypted = false;
203 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
204 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
205 DataManager::SetValue("tw_crypto_display", "");
Dees_Troy51127312012-09-08 13:08:49 -0400206 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400207#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400208 } else if (Mount_Point == "/cache") {
209 Display_Name = "Cache";
210 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400211 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400212 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
213 string Recreate_Command = "cd /cache && mkdir recovery";
Dees_Troyb46a6842012-09-25 11:06:46 -0400214 LOGI("Recreating /cache/recovery folder.\n");
Dees_Troyce2fe772012-09-28 12:34:33 -0400215 system(Recreate_Command.c_str());
Dees_Troyb46a6842012-09-25 11:06:46 -0400216 }
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 -0400628bool TWPartition::Is_Mounted(void) {
629 if (!Can_Be_Mounted)
630 return false;
631
632 struct stat st1, st2;
633 string test_path;
634
635 // Check to see if the mount point directory exists
636 test_path = Mount_Point + "/.";
637 if (stat(test_path.c_str(), &st1) != 0) return false;
638
639 // Check to see if the directory above the mount point exists
640 test_path = Mount_Point + "/../.";
641 if (stat(test_path.c_str(), &st2) != 0) return false;
642
643 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
644 int ret = (st1.st_dev != st2.st_dev) ? true : false;
645
646 return ret;
647}
648
649bool TWPartition::Mount(bool Display_Error) {
650 if (Is_Mounted()) {
651 return true;
652 } else if (!Can_Be_Mounted) {
653 return false;
654 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400655
656 Find_Actual_Block_Device();
657
658 // Check the current file system before mounting
659 Check_FS_Type();
660
661 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
662 if (Display_Error)
663 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
664 else
665 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy9350b8d2012-09-27 12:38:38 -0400666 LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400667 return false;
668 } else {
669 if (Removable)
670 Update_Size(Display_Error);
671
672 if (!Symlink_Mount_Point.empty()) {
673 string Command;
674
675 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -0400676 system(Command.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400677 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400678 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400679 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400680 return true;
681}
682
683bool TWPartition::UnMount(bool Display_Error) {
684 if (Is_Mounted()) {
685 int never_unmount_system;
686
687 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
688 if (never_unmount_system == 1 && Mount_Point == "/system")
689 return true; // Never unmount system if you're not supposed to unmount it
690
Dees_Troy38bd7602012-09-14 13:33:53 -0400691 if (!Symlink_Mount_Point.empty())
692 umount(Symlink_Mount_Point.c_str());
693
Dees_Troy5bf43922012-09-07 16:07:55 -0400694 if (umount(Mount_Point.c_str()) != 0) {
695 if (Display_Error)
696 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
697 else
698 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
699 return false;
700 } else
701 return true;
702 } else {
703 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400704 }
705}
706
707bool TWPartition::Wipe() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400708 if (!Can_Be_Wiped) {
709 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
710 return false;
711 }
712
Dees_Troyc51f1f92012-09-20 15:32:13 -0400713 if (Mount_Point == "/cache")
714 tmplog_offset = 0;
715
Dees_Troy38bd7602012-09-14 13:33:53 -0400716 if (Has_Data_Media)
717 return Wipe_Data_Without_Wiping_Media();
718
719 int check;
720 DataManager::GetValue(TW_RM_RF_VAR, check);
721 if (check)
722 return Wipe_RMRF();
723
724 if (Current_File_System == "ext4")
725 return Wipe_EXT4();
726
727 if (Current_File_System == "ext2" || Current_File_System == "ext3")
728 return Wipe_EXT23();
729
730 if (Current_File_System == "vfat")
731 return Wipe_FAT();
732
733 if (Current_File_System == "yaffs2")
734 return Wipe_MTD();
735
736 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str());
737 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400738}
739
Dees_Troye58d5262012-09-21 12:27:57 -0400740bool TWPartition::Wipe_AndSec(void) {
741 if (!Has_Android_Secure)
742 return false;
743
744 char cmd[512];
745
746 if (!Mount(true))
747 return false;
748
749 ui_print("Using rm -rf on .android_secure\n");
750 sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str());
751
752 LOGI("rm -rf command is: '%s'\n", cmd);
753 system(cmd);
754 return true;
755}
756
Dees_Troy51a0e822012-09-05 15:24:24 -0400757bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400758 if (Backup_Method == FILES)
759 return Backup_Tar(backup_folder);
760 else if (Backup_Method == DD)
761 return Backup_DD(backup_folder);
762 else if (Backup_Method == FLASH_UTILS)
763 return Backup_Dump_Image(backup_folder);
764 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
765 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400766}
767
Dees_Troy43d8b002012-09-17 16:00:01 -0400768bool TWPartition::Check_MD5(string restore_folder) {
769 string Full_Filename;
770 char split_filename[512];
771 int index = 0;
772
773 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400774 if (!TWFunc::Path_Exists(Full_Filename)) {
775 // This is a split archive, we presume
776 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
777 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
778 if (TWFunc::Check_MD5(split_filename) == 0) {
779 LOGE("MD5 failed to match on '%s'.\n", split_filename);
780 return false;
781 }
782 index++;
783 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400784 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400785 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400786 } else {
787 // Single file archive
788 if (TWFunc::Check_MD5(Full_Filename) == 0) {
789 LOGE("MD5 failed to match on '%s'.\n", split_filename);
790 return false;
791 } else
792 return true;
793 }
794 return false;
795}
796
Dees_Troy51a0e822012-09-05 15:24:24 -0400797bool TWPartition::Restore(string restore_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400798 if (Backup_Method == FILES)
799 return Restore_Tar(restore_folder);
800 else if (Backup_Method == DD)
801 return Restore_DD(restore_folder);
802 else if (Backup_Method == FLASH_UTILS)
803 return Restore_Flash_Image(restore_folder);
804 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
805 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400806}
807
808string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400809 if (Backup_Method == NONE)
810 return "none";
811 else if (Backup_Method == FILES)
812 return "files";
813 else if (Backup_Method == DD)
814 return "dd";
815 else if (Backup_Method == FLASH_UTILS)
816 return "flash_utils";
817 else
818 return "undefined";
819 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400820}
821
822bool TWPartition::Decrypt(string Password) {
823 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400824 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400825 return 1;
826}
827
828bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400829 bool Save_Data_Media = Has_Data_Media;
830
831 if (!UnMount(true))
832 return false;
833
834 Current_File_System = Fstab_File_System;
835 Is_Encrypted = false;
836 Is_Decrypted = false;
837 Decrypted_Block_Device = "";
838 Has_Data_Media = false;
839 if (Wipe()) {
840 Has_Data_Media = Save_Data_Media;
841 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
842 Recreate_Media_Folder();
843 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400844 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400845 return true;
846 } else {
847 Has_Data_Media = Save_Data_Media;
848 LOGE("Unable to format to remove encryption.\n");
849 return false;
850 }
851 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400852}
853
854void TWPartition::Check_FS_Type() {
Dees_Troy5bf43922012-09-07 16:07:55 -0400855 FILE *fp;
856 string blkCommand;
857 char blkOutput[255];
858 char* blk;
859 char* arg;
860 char* ptr;
861
862 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd")
863 return; // Running blkid on some mtd devices causes a massive crash
864
Dees_Troy38bd7602012-09-14 13:33:53 -0400865 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400866 if (!Is_Present)
867 return;
Dees_Troy51127312012-09-08 13:08:49 -0400868
Dees_Troy8170a922012-09-18 15:40:25 -0400869 if (TWFunc::Path_Exists("/tmp/blkidoutput.txt"))
870 system("rm /tmp/blkidoutput.txt");
871
872 blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt";
Dees_Troy43d8b002012-09-17 16:00:01 -0400873 system(blkCommand.c_str());
Dees_Troy51127312012-09-08 13:08:49 -0400874 fp = fopen("/tmp/blkidoutput.txt", "rt");
Dees_Troy8170a922012-09-18 15:40:25 -0400875 if (fp == NULL)
876 return;
Dees_Troy5bf43922012-09-07 16:07:55 -0400877 while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL)
878 {
879 blk = blkOutput;
880 ptr = blkOutput;
Dees_Troy63c8df72012-09-10 14:02:05 -0400881 while (*ptr > 32 && *ptr != ':') ptr++;
882 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400883 *ptr = 0;
884
885 // Increment by two, but verify that we don't hit a NULL
886 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400887 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400888
889 // Now, find the TYPE field
890 while (1)
891 {
892 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -0400893 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400894 if (*ptr != 0)
895 {
896 *ptr = 0;
897 ptr++;
898 }
899
900 if (strlen(arg) > 6)
901 {
902 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
903 }
904
905 if (*ptr == 0)
906 {
907 arg = NULL;
908 break;
909 }
910 }
911
912 if (arg && strlen(arg) > 7)
913 {
914 arg += 6; // Skip the TYPE=" portion
915 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
916 }
917 else
918 continue;
919
Dees_Troy63c8df72012-09-10 14:02:05 -0400920 if (strcmp(Current_File_System.c_str(), arg) != 0) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400921 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
922 Current_File_System = arg;
923 }
924 }
Dees_Troy43d8b002012-09-17 16:00:01 -0400925 fclose(fp);
Dees_Troy51a0e822012-09-05 15:24:24 -0400926 return;
927}
928
929bool TWPartition::Wipe_EXT23() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400930 if (!UnMount(true))
931 return false;
932
Dees_Troy43d8b002012-09-17 16:00:01 -0400933 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400934 char command[512];
935
936 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
937 Find_Actual_Block_Device();
938 sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str());
939 LOGI("mke2fs command: %s\n", command);
Dees_Troy43d8b002012-09-17 16:00:01 -0400940 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400941 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400942 ui_print("Done.\n");
943 return true;
944 } else {
945 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
946 return false;
947 }
948 } else
949 return Wipe_RMRF();
950
951 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400952}
953
954bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400955 if (!UnMount(true))
956 return false;
957
Dees_Troy43d8b002012-09-17 16:00:01 -0400958 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400959 string Command;
960
961 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
962 Find_Actual_Block_Device();
963 Command = "make_ext4fs";
964 if (!Is_Decrypted && Length != 0) {
965 // Only use length if we're not decrypted
966 char len[32];
967 sprintf(len, "%i", Length);
968 Command += " -l ";
969 Command += len;
970 }
971 Command += " " + Actual_Block_Device;
972 LOGI("make_ext4fs command: %s\n", Command.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -0400973 if (system(Command.c_str()) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400974 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400975 ui_print("Done.\n");
976 return true;
977 } else {
978 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
979 return false;
980 }
981 } else
982 return Wipe_EXT23();
983
984 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400985}
986
987bool TWPartition::Wipe_FAT() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400988 char command[512];
989
Dees_Troy43d8b002012-09-17 16:00:01 -0400990 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400991 if (!UnMount(true))
992 return false;
993
994 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
995 Find_Actual_Block_Device();
996 sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it
Dees_Troy43d8b002012-09-17 16:00:01 -0400997 if (system(command) == 0) {
Dees_Troye58d5262012-09-21 12:27:57 -0400998 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -0400999 ui_print("Done.\n");
1000 return true;
1001 } else {
1002 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1003 return false;
1004 }
1005 return true;
1006 }
1007 else
1008 return Wipe_RMRF();
1009
1010 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001011}
1012
Dees_Troy38bd7602012-09-14 13:33:53 -04001013bool TWPartition::Wipe_MTD() {
1014 if (!UnMount(true))
1015 return false;
1016
1017 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1018
1019 mtd_scan_partitions();
1020 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1021 if (mtd == NULL) {
1022 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1023 return false;
1024 }
1025
1026 MtdWriteContext* ctx = mtd_write_partition(mtd);
1027 if (ctx == NULL) {
1028 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1029 return false;
1030 }
1031 if (mtd_erase_blocks(ctx, -1) == -1) {
1032 mtd_write_close(ctx);
1033 LOGE("Failed to format '%s'", MTD_Name.c_str());
1034 return false;
1035 }
1036 if (mtd_write_close(ctx) != 0) {
1037 LOGE("Failed to close '%s'", MTD_Name.c_str());
1038 return false;
1039 }
Dees_Troye58d5262012-09-21 12:27:57 -04001040 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001041 ui_print("Done.\n");
1042 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001043}
1044
1045bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001046 char cmd[512];
1047
1048 if (!Mount(true))
1049 return false;
1050
Dees_Troye58d5262012-09-21 12:27:57 -04001051 ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str());
1052 sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001053
1054 LOGI("rm -rf command is: '%s'\n", cmd);
Dees_Troy43d8b002012-09-17 16:00:01 -04001055 system(cmd);
Dees_Troye58d5262012-09-21 12:27:57 -04001056 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001057 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001058}
1059
1060bool TWPartition::Wipe_Data_Without_Wiping_Media() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001061 char cmd[256];
1062
1063 // This handles wiping data on devices with "sdcard" in /data/media
1064 if (!Mount(true))
1065 return false;
1066
1067 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001068 system("rm -f /data/*");
1069 system("rm -f /data/.*");
Dees_Troy38bd7602012-09-14 13:33:53 -04001070
1071 DIR* d;
1072 d = opendir("/data");
1073 if (d != NULL)
1074 {
1075 struct dirent* de;
1076 while ((de = readdir(d)) != NULL) {
1077 if (strcmp(de->d_name, "media") == 0) continue;
1078
1079 sprintf(cmd, "rm -fr /data/%s", de->d_name);
Dees_Troy43d8b002012-09-17 16:00:01 -04001080 system(cmd);
Dees_Troy38bd7602012-09-14 13:33:53 -04001081 }
1082 closedir(d);
1083 }
1084 ui_print("Done.\n");
1085 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001086}
1087
1088bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001089 char back_name[255], split_index[5];
1090 string Full_FileName, Split_FileName, Tar_Args, Command;
1091 int use_compression, index, backup_count;
1092 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001093 unsigned long long total_bsize = 0, file_size;
Dees_Troy43d8b002012-09-17 16:00:01 -04001094
1095 if (!Mount(true))
1096 return false;
1097
Dees_Troy2c50e182012-09-26 20:05:28 -04001098 if (Backup_Path == "/and-sec") {
1099 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1100 ui_print("Backing up %s...\n", "Android Secure");
1101 } else {
1102 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1103 ui_print("Backing up %s...\n", Display_Name.c_str());
1104 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001105
1106 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
1107 if (use_compression)
1108 Tar_Args = "-cz";
1109 else
1110 Tar_Args = "-c";
1111
1112 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1113 Backup_FileName = back_name;
1114
Dees_Troy43d8b002012-09-17 16:00:01 -04001115 if (Backup_Size > MAX_ARCHIVE_SIZE) {
1116 // This backup needs to be split into multiple archives
Dees_Troy4a2a1262012-09-18 09:33:47 -04001117 ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001118 sprintf(back_name, "%s", Backup_Path.c_str());
Dees_Troy9df963c2012-09-26 08:58:12 -04001119 backup_count = MakeList::Make_File_List(back_name);
Dees_Troy4a2a1262012-09-18 09:33:47 -04001120 if (backup_count < 1) {
1121 LOGE("Error generating file list!\n");
1122 return false;
1123 }
1124 for (index=0; index<backup_count; index++) {
1125 sprintf(split_index, "%03i", index);
1126 Full_FileName = backup_folder + "/" + Backup_FileName + split_index;
1127 Command = "tar " + Tar_Args + " -f '" + Full_FileName + "' -T /tmp/list/filelist" + split_index;
1128 LOGI("Backup command: '%s'\n", Command.c_str());
1129 ui_print("Backup archive %i of %i...\n", (index + 1), backup_count);
1130 system(Command.c_str()); // sending backup command formed earlier above
1131
Dees_Troy7c2dec82012-09-26 09:49:14 -04001132 file_size = TWFunc::Get_File_Size(Full_FileName);
1133 if (file_size == 0) {
1134 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 -04001135 return false;
1136 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001137 total_bsize += file_size;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001138 }
1139 ui_print(" * Total size: %llu bytes.\n", total_bsize);
1140 system("cd /tmp && rm -rf list");
Dees_Troy43d8b002012-09-17 16:00:01 -04001141 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001142 Full_FileName = backup_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001143 if (Has_Data_Media)
Dees_Troye58d5262012-09-21 12:27:57 -04001144 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " ./ --exclude='media*' -f '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001145 else
Dees_Troye58d5262012-09-21 12:27:57 -04001146 Command = "cd " + Backup_Path + " && tar " + Tar_Args + " -f '" + Full_FileName + "' ./*";
Dees_Troy43d8b002012-09-17 16:00:01 -04001147 LOGI("Backup command: '%s'\n", Command.c_str());
1148 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001149 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1150 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1151 return false;
1152 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001153 }
1154 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001155}
1156
1157bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001158 char back_name[255];
1159 string Full_FileName, Command;
1160 int use_compression;
1161
Dees_Troyb46a6842012-09-25 11:06:46 -04001162 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001163 ui_print("Backing up %s...\n", Display_Name.c_str());
1164
1165 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1166 Backup_FileName = back_name;
1167
1168 Full_FileName = backup_folder + "/" + Backup_FileName;
1169
1170 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1171 LOGI("Backup command: '%s'\n", Command.c_str());
1172 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001173 if (TWFunc::Get_File_Size(Full_FileName) != Backup_Size) {
1174 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);
1175 return false;
1176 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001177 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001178}
1179
1180bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001181 char back_name[255];
1182 string Full_FileName, Command;
1183 int use_compression;
1184
Dees_Troyb46a6842012-09-25 11:06:46 -04001185 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001186 ui_print("Backing up %s...\n", Display_Name.c_str());
1187
1188 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1189 Backup_FileName = back_name;
1190
1191 Full_FileName = backup_folder + "/" + Backup_FileName;
1192
1193 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1194 LOGI("Backup command: '%s'\n", Command.c_str());
1195 system(Command.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001196 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1197 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1198 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1199 return false;
1200 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001201 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001202}
1203
1204bool TWPartition::Restore_Tar(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001205 size_t first_period, second_period;
1206 string Restore_File_System, Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001207 int index = 0;
1208 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001209
Dees_Troyb46a6842012-09-25 11:06:46 -04001210 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001211 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
1212
1213 // Parse backup filename to extract the file system before wiping
1214 first_period = Backup_FileName.find(".");
1215 if (first_period == string::npos) {
1216 LOGE("Unable to find file system (first period).\n");
1217 return false;
1218 }
1219 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
1220 second_period = Restore_File_System.find(".");
1221 if (second_period == string::npos) {
1222 LOGE("Unable to find file system (second period).\n");
1223 return false;
1224 }
1225 Restore_File_System.resize(second_period);
1226 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
1227 Current_File_System = Restore_File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001228 if (Has_Android_Secure) {
1229 ui_print("Wiping android secure...\n");
1230 if (!Wipe_AndSec())
1231 return false;
1232 } else if (!Wipe()) {
1233 ui_print("Wiping %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001234 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001235 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001236
1237 if (!Mount(true))
1238 return false;
1239
Dees_Troy43d8b002012-09-17 16:00:01 -04001240 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001241 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001242 if (!TWFunc::Path_Exists(Full_FileName)) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001243 // Backup is multiple archives
1244 LOGI("Backup is multiple archives.\n");
1245 sprintf(split_index, "%03i", index);
1246 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1247 while (TWFunc::Path_Exists(Full_FileName)) {
1248 ui_print("Restoring archive %i...\n", index + 1);
Dees_Troye58d5262012-09-21 12:27:57 -04001249 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy4a2a1262012-09-18 09:33:47 -04001250 LOGI("Restore command: '%s'\n", Command.c_str());
1251 system(Command.c_str());
1252 index++;
1253 sprintf(split_index, "%03i", index);
1254 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1255 }
1256 if (index == 0) {
1257 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1258 return false;
1259 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001260 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001261 Command = "cd " + Backup_Path + " && tar -xf '" + Full_FileName + "'";
Dees_Troy43d8b002012-09-17 16:00:01 -04001262 LOGI("Restore command: '%s'\n", Command.c_str());
1263 system(Command.c_str());
1264 }
1265 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001266}
1267
1268bool TWPartition::Restore_DD(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001269 string Full_FileName, Command;
1270
Dees_Troyb46a6842012-09-25 11:06:46 -04001271 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001272 ui_print("Restoring %s...\n", Display_Name.c_str());
1273 Full_FileName = restore_folder + "/" + Backup_FileName;
1274 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1275 LOGI("Restore command: '%s'\n", Command.c_str());
1276 system(Command.c_str());
1277 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001278}
1279
1280bool TWPartition::Restore_Flash_Image(string restore_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001281 string Full_FileName, Command;
1282
Dees_Troyb46a6842012-09-25 11:06:46 -04001283 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001284 ui_print("Restoring %s...\n", Display_Name.c_str());
1285 Full_FileName = restore_folder + "/" + Backup_FileName;
1286 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1287 Command = "erase_image " + MTD_Name;
1288 LOGI("Erase command: '%s'\n", Command.c_str());
1289 system(Command.c_str());
1290 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1291 LOGI("Restore command: '%s'\n", Command.c_str());
1292 system(Command.c_str());
1293 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001294}
Dees_Troy5bf43922012-09-07 16:07:55 -04001295
1296bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy51127312012-09-08 13:08:49 -04001297 bool ret = false;
1298
Dees_Troyab10ee22012-09-21 14:27:30 -04001299 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001300 return false;
1301
Dees_Troy38bd7602012-09-14 13:33:53 -04001302 if (Removable || Is_Encrypted) {
1303 if (!Mount(false))
1304 return true;
1305 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001306 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001307
1308 ret = Get_Size_Via_statfs(Display_Error);
1309 if (!ret || Size == 0)
1310 if (!Get_Size_Via_df(Display_Error))
1311 return false;
1312
Dees_Troy5bf43922012-09-07 16:07:55 -04001313 if (Has_Data_Media) {
1314 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001315 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001316 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1317 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001318 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001319 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001320 int bak = (int)(Backup_Size / 1048576LLU);
1321 int total = (int)(Size / 1048576LLU);
1322 int us = (int)(Used / 1048576LLU);
1323 int fre = (int)(Free / 1048576LLU);
1324 int datmed = (int)(data_media_used / 1048576LLU);
1325 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 -04001326 } else
1327 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001328 } else if (Has_Android_Secure) {
1329 if (Mount(Display_Error))
1330 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
1331 else
1332 return false;
Dees_Troy5bf43922012-09-07 16:07:55 -04001333 }
1334 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001335}
Dees_Troy38bd7602012-09-14 13:33:53 -04001336
1337void TWPartition::Find_Actual_Block_Device(void) {
1338 if (Is_Decrypted) {
1339 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001340 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001341 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001342 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001343 Is_Present = true;
1344 Actual_Block_Device = Primary_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001345 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001346 Actual_Block_Device = Primary_Block_Device;
1347 Is_Present = true;
1348 } else
1349 Is_Present = false;
1350}
1351
1352void TWPartition::Recreate_Media_Folder(void) {
1353 string Command;
1354
1355 if (!Mount(true)) {
1356 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001357 } else if (!TWFunc::Path_Exists("/data/media")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001358 LOGI("Recreating /data/media folder.\n");
Dees_Troy43d8b002012-09-17 16:00:01 -04001359 system("cd /data && mkdir media && chmod 755 media");
Dees_Troy38bd7602012-09-14 13:33:53 -04001360 Command = "umount " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001361 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001362 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
Dees_Troy43d8b002012-09-17 16:00:01 -04001363 system(Command.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001364 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001365}
Dees_Troye58d5262012-09-21 12:27:57 -04001366
1367void TWPartition::Recreate_AndSec_Folder(void) {
1368 string Command;
1369
1370 if (!Has_Android_Secure)
1371 return;
1372
1373 if (!Mount(true)) {
1374 LOGE("Unable to recreate android secure folder.\n");
1375 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1376 LOGI("Recreating android secure folder.\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001377 Command = "umount " + Symlink_Mount_Point;
1378 system(Command.c_str());
Dees_Troyce2fe772012-09-28 12:34:33 -04001379 Command = "cd " + Mount_Point + " && mkdir .android_secure";
1380 system(Command.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001381 Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point;
1382 system(Command.c_str());
1383 }
1384}