blob: 346c298b22aac23c0ac0ecc2ea47757847f71a22 [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>
bigbiff bigbiff9c754052013-01-09 09:09:08 -050031#include <iostream>
32#include <sstream>
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Dees_Troy657c3092012-09-10 20:32:10 -040034#ifdef TW_INCLUDE_CRYPTO
35 #include "cutils/properties.h"
36#endif
37
bigbiff bigbiffe60683a2013-02-22 20:55:50 -050038#include "libblkid/blkid.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039#include "variables.h"
40#include "common.h"
41#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040042#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040043#include "twrp-functions.hpp"
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -050044#include "twrpDigest.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050045#include "twrpTar.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040046extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040047 #include "mtdutils/mtdutils.h"
48 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000049#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
50 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
51#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040052}
Dees_Troy51a0e822012-09-05 15:24:24 -040053
bigbiff bigbiff9c754052013-01-09 09:09:08 -050054using namespace std;
55
Dees_Troy51a0e822012-09-05 15:24:24 -040056TWPartition::TWPartition(void) {
57 Can_Be_Mounted = false;
58 Can_Be_Wiped = false;
59 Wipe_During_Factory_Reset = false;
60 Wipe_Available_in_GUI = false;
61 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040062 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040063 SubPartition_Of = "";
64 Symlink_Path = "";
65 Symlink_Mount_Point = "";
66 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040067 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040068 Actual_Block_Device = "";
69 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040070 Alternate_Block_Device = "";
71 Removable = false;
72 Is_Present = false;
73 Length = 0;
74 Size = 0;
75 Used = 0;
76 Free = 0;
77 Backup_Size = 0;
78 Can_Be_Encrypted = false;
79 Is_Encrypted = false;
80 Is_Decrypted = false;
81 Decrypted_Block_Device = "";
82 Display_Name = "";
83 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040084 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040085 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040086 Backup_Method = NONE;
87 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040088 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040089 Is_Storage = false;
90 Storage_Path = "";
91 Current_File_System = "";
92 Fstab_File_System = "";
93 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +000094 Ignore_Blkid = false;
Dees_Troy16c2b312013-01-15 16:51:18 +000095 Retain_Layout_Version = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +000096#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
97 EcryptFS_Password = "";
98#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040099}
100
101TWPartition::~TWPartition(void) {
102 // Do nothing
103}
104
Dees_Troy5bf43922012-09-07 16:07:55 -0400105bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
106 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
107 int line_len = Line.size(), index = 0, item_index = 0;
108 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400109 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400110 strncpy(full_line, Line.c_str(), line_len);
111
Dees_Troy51127312012-09-08 13:08:49 -0400112 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400113 if (full_line[index] <= 32)
114 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400115 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400116 Mount_Point = full_line;
117 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400118 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400119 index = Mount_Point.size();
120 while (index < line_len) {
121 while (index < line_len && full_line[index] == '\0')
122 index++;
123 if (index >= line_len)
124 continue;
125 ptr = full_line + index;
126 if (item_index == 0) {
127 // File System
128 Fstab_File_System = ptr;
129 Current_File_System = ptr;
130 item_index++;
131 } else if (item_index == 1) {
132 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400133 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400134 MTD_Name = ptr;
135 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400136 } else if (Fstab_File_System == "bml") {
137 if (Mount_Point == "/boot")
138 MTD_Name = "boot";
139 else if (Mount_Point == "/recovery")
140 MTD_Name = "recovery";
141 Primary_Block_Device = ptr;
142 if (*ptr != '/')
143 LOGE("Until we get better BML support, you will have to find and provide the full block device path to the BML devices e.g. /dev/block/bml9 instead of the partition name\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400144 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400145 if (Display_Error)
146 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
147 else
148 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
149 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400150 } else {
151 Primary_Block_Device = ptr;
152 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400153 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400154 item_index++;
155 } else if (item_index > 1) {
156 if (*ptr == '/') {
157 // Alternate Block Device
158 Alternate_Block_Device = ptr;
159 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
160 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
161 // Partition length
162 ptr += 7;
163 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400164 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
165 // Custom flags, save for later so that new values aren't overwritten by defaults
166 ptr += 6;
167 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000168 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400169 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
170 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400171 } else {
172 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400173 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400174 }
175 }
176 while (index < line_len && full_line[index] != '\0')
177 index++;
178 }
179
180 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
181 if (Display_Error)
182 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
183 else
184 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
185 return 0;
186 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400187 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400188 Setup_File_System(Display_Error);
189 if (Mount_Point == "/system") {
190 Display_Name = "System";
191 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400192 } else if (Mount_Point == "/data") {
193 Display_Name = "Data";
194 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400195 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400196#ifdef RECOVERY_SDCARD_ON_DATA
197 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400198 Is_Storage = true;
199 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000200 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400201 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
202 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400203 Symlink_Mount_Point = "/emmc";
204 } else {
205 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400206 Symlink_Mount_Point = "/sdcard";
207 }
Dees_Troy16b74352012-11-14 22:27:31 +0000208 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
209 Storage_Path = "/data/media/0";
210 Symlink_Path = Storage_Path;
211 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
212 UnMount(true);
213 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400214#endif
215#ifdef TW_INCLUDE_CRYPTO
216 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400217 char crypto_blkdev[255];
218 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
219 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400220 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400221 DataManager::SetValue(TW_IS_DECRYPTED, 1);
222 Is_Encrypted = true;
223 Is_Decrypted = true;
224 Decrypted_Block_Device = crypto_blkdev;
225 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
226 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400227 Is_Encrypted = true;
228 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800229 Can_Be_Mounted = false;
230 Current_File_System = "emmc";
231 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400232 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
233 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
234 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800235 } else {
236 // Filesystem is not encrypted and the mount
237 // succeeded, so get it back to the original
238 // unmounted state
239 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400240 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400241 #ifdef RECOVERY_SDCARD_ON_DATA
242 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
243 Recreate_Media_Folder();
244 #endif
245#else
246 #ifdef RECOVERY_SDCARD_ON_DATA
247 Recreate_Media_Folder();
248 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400249#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400250 } else if (Mount_Point == "/cache") {
251 Display_Name = "Cache";
252 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400253 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400254 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troyb46a6842012-09-25 11:06:46 -0400255 LOGI("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500256 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
257 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400258 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400259 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400260 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 Display_Name = "DataData";
262 Is_SubPartition = true;
263 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 DataManager::SetValue(TW_HAS_DATADATA, 1);
265 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400266 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400267 Display_Name = "SD-Ext";
268 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400269 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400270 } else if (Mount_Point == "/boot") {
271 Display_Name = "Boot";
272 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400273 }
274#ifdef TW_EXTERNAL_STORAGE_PATH
275 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
276 Is_Storage = true;
277 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400278 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400279#else
280 if (Mount_Point == "/sdcard") {
281 Is_Storage = true;
282 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400283 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400284#ifndef RECOVERY_SDCARD_ON_DATA
285 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400286 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400287#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400288#endif
Dees_Troyb05ddee2013-01-28 20:24:50 +0000289 }
Dees_Troy8170a922012-09-18 15:40:25 -0400290#ifdef TW_INTERNAL_STORAGE_PATH
291 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
292 Is_Storage = true;
293 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400294#ifndef RECOVERY_SDCARD_ON_DATA
295 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400296 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400297#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400298 }
299#else
300 if (Mount_Point == "/emmc") {
301 Is_Storage = true;
302 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400303#ifndef RECOVERY_SDCARD_ON_DATA
304 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400305 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400306#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400307 }
308#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400309 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400310 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400311 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400312 }
313
Dees_Troy51127312012-09-08 13:08:49 -0400314 // Process any custom flags
315 if (Flags.size() > 0)
316 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400317 return true;
318}
319
320bool TWPartition::Process_Flags(string Flags, bool Display_Error) {
321 char flags[MAX_FSTAB_LINE_LENGTH];
322 int flags_len, index = 0;
323 char* ptr;
324
325 strcpy(flags, Flags.c_str());
326 flags_len = Flags.size();
327 for (index = 0; index < flags_len; index++) {
328 if (flags[index] == ';')
329 flags[index] = '\0';
330 }
331
332 index = 0;
333 while (index < flags_len) {
334 while (index < flags_len && flags[index] == '\0')
335 index++;
336 if (index >= flags_len)
337 continue;
338 ptr = flags + index;
339 if (strcmp(ptr, "removable") == 0) {
340 Removable = true;
341 } else if (strcmp(ptr, "storage") == 0) {
342 Is_Storage = true;
Dees_Troy63c8df72012-09-10 14:02:05 -0400343 } else if (strcmp(ptr, "canbewiped") == 0) {
344 Can_Be_Wiped = true;
345 } else if (strcmp(ptr, "wipeingui") == 0) {
346 Can_Be_Wiped = true;
347 Wipe_Available_in_GUI = true;
348 } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) {
349 Can_Be_Wiped = true;
350 Wipe_Available_in_GUI = true;
351 Wipe_During_Factory_Reset = true;
Dees_Troy51127312012-09-08 13:08:49 -0400352 } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) {
Dees_Troy2c4c26f2013-01-28 15:26:43 +0000353 ptr += 15;
Dees_Troy51127312012-09-08 13:08:49 -0400354 Is_SubPartition = true;
355 SubPartition_Of = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000356 } else if (strcmp(ptr, "ignoreblkid") == 0) {
357 Ignore_Blkid = true;
Dees_Troy16c2b312013-01-15 16:51:18 +0000358 } else if (strcmp(ptr, "retainlayoutversion") == 0) {
359 Retain_Layout_Version = true;
Dees_Troy51127312012-09-08 13:08:49 -0400360 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
361 ptr += 8;
362 Symlink_Path = ptr;
363 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
364 ptr += 8;
365 Display_Name = ptr;
366 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
367 ptr += 10;
368 Format_Block_Size = atoi(ptr);
369 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
370 ptr += 7;
371 Length = atoi(ptr);
372 } else {
373 if (Display_Error)
374 LOGE("Unhandled flag: '%s'\n", ptr);
375 else
376 LOGI("Unhandled flag: '%s'\n", ptr);
377 }
378 while (index < flags_len && flags[index] != '\0')
379 index++;
380 }
381 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400382}
383
Dees_Troy5bf43922012-09-07 16:07:55 -0400384bool TWPartition::Is_File_System(string File_System) {
385 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400386 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400387 File_System == "ext4" ||
388 File_System == "vfat" ||
389 File_System == "ntfs" ||
390 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500391 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400392 File_System == "auto")
393 return true;
394 else
395 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400396}
397
Dees_Troy5bf43922012-09-07 16:07:55 -0400398bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400399 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400400 return true;
401 else
402 return false;
403}
404
Dees_Troy51127312012-09-08 13:08:49 -0400405bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400406 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400407 if (mkdir(Path.c_str(), 0777) == -1) {
408 if (Display_Error)
409 LOGE("Can not create '%s' folder.\n", Path.c_str());
410 else
411 LOGI("Can not create '%s' folder.\n", Path.c_str());
412 return false;
413 } else {
414 LOGI("Created '%s' folder.\n", Path.c_str());
415 return true;
416 }
417 }
418 return true;
419}
420
Dees_Troy5bf43922012-09-07 16:07:55 -0400421void TWPartition::Setup_File_System(bool Display_Error) {
422 struct statfs st;
423
424 Can_Be_Mounted = true;
425 Can_Be_Wiped = true;
426
Dees_Troy5bf43922012-09-07 16:07:55 -0400427 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400428 Make_Dir(Mount_Point, 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 Backup_Method = FILES;
432}
433
434void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400435 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
436 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800437 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400438 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800439 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400440 Backup_Method = FLASH_UTILS;
441 else
Gary Peck82599a82012-11-21 16:23:12 -0800442 LOGI("Unhandled file system '%s' on image '%s'\n", Current_File_System.c_str(), Display_Name.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 if (Find_Partition_Size()) {
444 Used = Size;
445 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400446 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400447 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400448 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400449 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400450 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400451 }
452}
453
Dees_Troye58d5262012-09-21 12:27:57 -0400454void TWPartition::Setup_AndSec(void) {
455 Backup_Name = "and-sec";
456 Has_Android_Secure = true;
457 Symlink_Path = Mount_Point + "/.android_secure";
458 Symlink_Mount_Point = "/and-sec";
459 Backup_Path = Symlink_Mount_Point;
460 Make_Dir("/and-sec", true);
461 Recreate_AndSec_Folder();
462}
463
Dees_Troy5bf43922012-09-07 16:07:55 -0400464void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
465 char device[512], realDevice[512];
466
467 strcpy(device, Block.c_str());
468 memset(realDevice, 0, sizeof(realDevice));
469 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
470 {
471 strcpy(device, realDevice);
472 memset(realDevice, 0, sizeof(realDevice));
473 }
474
475 if (device[0] != '/') {
476 if (Display_Error)
477 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
478 else
479 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
480 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400481 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400482 Block = device;
483 return;
484 }
485}
486
Dees_Troy8e337f32012-10-13 22:07:49 -0400487void TWPartition::Mount_Storage_Retry(void) {
488 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500489 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400490 int retry_count = 5;
491 while (retry_count > 0 && !Mount(false)) {
492 usleep(500000);
493 retry_count--;
494 }
495 Mount(true);
496 }
497}
498
Dees_Troy38bd7602012-09-14 13:33:53 -0400499bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
500 FILE *fp = NULL;
501 char line[255];
502
503 fp = fopen("/proc/mtd", "rt");
504 if (fp == NULL) {
505 LOGE("Device does not support /proc/mtd\n");
506 return false;
507 }
508
509 while (fgets(line, sizeof(line), fp) != NULL)
510 {
511 char device[32], label[32];
512 unsigned long size = 0;
513 char* fstype = NULL;
514 int deviceId;
515
516 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
517
518 // Skip header and blank lines
519 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
520 continue;
521
522 // Strip off the trailing " from the label
523 label[strlen(label)-1] = '\0';
524
525 if (strcmp(label, MTD_Name.c_str()) == 0) {
526 // We found our device
527 // Strip off the trailing : from the device
528 device[strlen(device)-1] = '\0';
529 if (sscanf(device,"mtd%d", &deviceId) == 1) {
530 sprintf(device, "/dev/block/mtdblock%d", deviceId);
531 Primary_Block_Device = device;
532 }
533 }
534 }
535 fclose(fp);
536
537 return false;
538}
539
Dees_Troy51127312012-09-08 13:08:49 -0400540bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
541 struct statfs st;
542 string Local_Path = Mount_Point + "/.";
543
544 if (!Mount(Display_Error))
545 return false;
546
547 if (statfs(Local_Path.c_str(), &st) != 0) {
548 if (!Removable) {
549 if (Display_Error)
550 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
551 else
552 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
553 }
554 return false;
555 }
556 Size = (st.f_blocks * st.f_bsize);
557 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
558 Free = (st.f_bfree * st.f_bsize);
559 Backup_Size = Used;
560 return true;
561}
562
563bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400564 FILE* fp;
565 char command[255], line[512];
566 int include_block = 1;
567 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500568 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400569
570 if (!Mount(Display_Error))
571 return false;
572
Dees_Troy38bd7602012-09-14 13:33:53 -0400573 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400574 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500575 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400576 fp = fopen("/tmp/dfoutput.txt", "rt");
577 if (fp == NULL) {
578 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400579 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400580 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400581
582 while (fgets(line, sizeof(line), fp) != NULL)
583 {
584 unsigned long blocks, used, available;
585 char device[64];
586 char tmpString[64];
587
588 if (strncmp(line, "Filesystem", 10) == 0)
589 continue;
590 if (strlen(line) < min_len) {
591 include_block = 0;
592 continue;
593 }
594 if (include_block) {
595 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
596 } else {
597 // The device block string is so long that the df information is on the next line
598 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400599 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400600 while (tmpString[space_count] == 32)
601 space_count++;
602 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
603 }
604
605 // Adjust block size to byte size
606 Size = blocks * 1024ULL;
607 Used = used * 1024ULL;
608 Free = available * 1024ULL;
609 Backup_Size = Used;
610 }
611 fclose(fp);
612 return true;
613}
614
Dees_Troy5bf43922012-09-07 16:07:55 -0400615bool TWPartition::Find_Partition_Size(void) {
616 FILE* fp;
617 char line[512];
618 string tmpdevice;
619
igoriok87e3d932013-01-31 21:03:53 +0200620 fp = fopen("/proc/dumchar_info", "rt");
621 if (fp != NULL) {
622 while (fgets(line, sizeof(line), fp) != NULL)
623 {
624 char label[32], device[32];
625 unsigned long size = 0;
626
627 sscanf(line, "%s %lx %*lx %*lu %s", label, &size, device);
628
629 // Skip header, annotation and blank lines
630 if ((strncmp(device, "/dev/", 5) != 0) || (strlen(line) < 8))
631 continue;
632
633 tmpdevice = "/dev/";
634 tmpdevice += label;
635 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
636 Size = size;
637 fclose(fp);
638 return true;
639 }
640 }
641 }
642
Dees_Troy5bf43922012-09-07 16:07:55 -0400643 // In this case, we'll first get the partitions we care about (with labels)
644 fp = fopen("/proc/partitions", "rt");
645 if (fp == NULL)
646 return false;
647
648 while (fgets(line, sizeof(line), fp) != NULL)
649 {
650 unsigned long major, minor, blocks;
651 char device[512];
652 char tmpString[64];
653
Dees_Troy63c8df72012-09-10 14:02:05 -0400654 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400655 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
656
657 tmpdevice = "/dev/block/";
658 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400659 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400660 // Adjust block size to byte size
661 Size = blocks * 1024ULL;
662 fclose(fp);
663 return true;
664 }
665 }
666 fclose(fp);
667 return false;
668}
669
Dees_Troy5bf43922012-09-07 16:07:55 -0400670bool TWPartition::Is_Mounted(void) {
671 if (!Can_Be_Mounted)
672 return false;
673
674 struct stat st1, st2;
675 string test_path;
676
677 // Check to see if the mount point directory exists
678 test_path = Mount_Point + "/.";
679 if (stat(test_path.c_str(), &st1) != 0) return false;
680
681 // Check to see if the directory above the mount point exists
682 test_path = Mount_Point + "/../.";
683 if (stat(test_path.c_str(), &st2) != 0) return false;
684
685 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
686 int ret = (st1.st_dev != st2.st_dev) ? true : false;
687
688 return ret;
689}
690
691bool TWPartition::Mount(bool Display_Error) {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000692 int exfat_mounted = 0;
693
Dees_Troy5bf43922012-09-07 16:07:55 -0400694 if (Is_Mounted()) {
695 return true;
696 } else if (!Can_Be_Mounted) {
697 return false;
698 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400699
700 Find_Actual_Block_Device();
701
702 // Check the current file system before mounting
703 Check_FS_Type();
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000704 if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
Dees_Troye34c1332013-02-06 19:13:00 +0000705 string cmd = "/sbin/exfat-fuse -o big_writes,max_read=131072,max_write=131072 " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000706 LOGI("cmd: %s\n", cmd.c_str());
707 string result;
708 if (TWFunc::Exec_Cmd(cmd, result) != 0) {
709 LOGI("exfat-fuse failed to mount with result '%s', trying vfat\n", result.c_str());
710 Current_File_System = "vfat";
711 } else {
712#ifdef TW_NO_EXFAT_FUSE
713 UnMount(false);
714 // We'll let the kernel handle it but using exfat-fuse to detect if the file system is actually exfat
715 // Some kernels let us mount vfat as exfat which doesn't work out too well
716#else
717 exfat_mounted = 1;
718#endif
719 }
720 }
Dees_Troy22042032012-12-18 21:23:08 +0000721 if (Fstab_File_System == "yaffs2") {
722 // mount an MTD partition as a YAFFS2 filesystem.
723 mtd_scan_partitions();
724 const MtdPartition* partition;
725 partition = mtd_find_partition_by_name(MTD_Name.c_str());
726 if (partition == NULL) {
727 LOGE("Failed to find '%s' partition to mount at '%s'\n",
728 MTD_Name.c_str(), Mount_Point.c_str());
729 return false;
730 }
731 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
732 if (Display_Error)
733 LOGE("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
734 else
735 LOGI("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
736 return false;
737 } else
738 return true;
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000739 } else if (!exfat_mounted && mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
740#ifdef TW_NO_EXFAT_FUSE
741 if (Current_File_System == "exfat") {
742 LOGI("Mounting exfat failed, trying vfat...\n");
743 if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), "vfat", 0, NULL) != 0) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000744 if (Display_Error)
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000745 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy85f44ed2013-01-09 18:42:36 +0000746 else
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000747 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
748 LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
749 return false;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000750 }
Dees_Troyb05ddee2013-01-28 20:24:50 +0000751 } else {
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000752#endif
753 if (Display_Error)
754 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
755 else
756 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
757 LOGI("Actual block device: '%s', current file system: '%s'\n", Actual_Block_Device.c_str(), Current_File_System.c_str());
758 return false;
759#ifdef TW_NO_EXFAT_FUSE
Dees_Troy85f44ed2013-01-09 18:42:36 +0000760 }
761#endif
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000762 }
763#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
764 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
765 MetaEcfsFile += "/.MetaEcfsFile";
766 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
767 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
768 if (Display_Error)
769 LOGE("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
770 else
771 LOGI("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
772 } else {
773 LOGI("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
774 Is_Decrypted = true;
Dees_Troy51127312012-09-08 13:08:49 -0400775 }
Dees_Troy3f5c4e82013-02-01 15:16:59 +0000776 } else {
777 Is_Decrypted = false;
778 }
779#endif
780 if (Removable)
781 Update_Size(Display_Error);
782
783 if (!Symlink_Mount_Point.empty()) {
784 string Command, Result;
785 Command = "mount '" + Symlink_Path + "' '" + Symlink_Mount_Point + "'";
786 TWFunc::Exec_Cmd(Command, Result);
Dees_Troy5bf43922012-09-07 16:07:55 -0400787 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400788 return true;
789}
790
791bool TWPartition::UnMount(bool Display_Error) {
792 if (Is_Mounted()) {
793 int never_unmount_system;
794
795 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
796 if (never_unmount_system == 1 && Mount_Point == "/system")
797 return true; // Never unmount system if you're not supposed to unmount it
798
Dees_Troyc8bafa12013-01-10 15:43:00 +0000799#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
800 if (EcryptFS_Password.size() > 0) {
801 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
802 if (Display_Error)
803 LOGE("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
804 else
805 LOGI("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
806 } else {
807 LOGI("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
808 }
809 }
810#endif
811
Dees_Troy38bd7602012-09-14 13:33:53 -0400812 if (!Symlink_Mount_Point.empty())
813 umount(Symlink_Mount_Point.c_str());
814
Dees_Troyb05ddee2013-01-28 20:24:50 +0000815 umount(Mount_Point.c_str());
816 if (Is_Mounted()) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400817 if (Display_Error)
818 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
819 else
820 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
821 return false;
822 } else
823 return true;
824 } else {
825 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400826 }
827}
828
Gary Peck43acadf2012-11-21 21:19:01 -0800829bool TWPartition::Wipe(string New_File_System) {
Dees_Troy16c2b312013-01-15 16:51:18 +0000830 bool wiped = false, update_crypt = false;
831 int check;
832 string Layout_Filename = Mount_Point + "/.layout_version";
833
Dees_Troy38bd7602012-09-14 13:33:53 -0400834 if (!Can_Be_Wiped) {
835 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
836 return false;
837 }
838
Dees_Troyc51f1f92012-09-20 15:32:13 -0400839 if (Mount_Point == "/cache")
840 tmplog_offset = 0;
841
Dees_Troyce675462013-01-09 19:48:21 +0000842#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
843 if (Mount_Point == "/data" && Mount(false)) {
844 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
845 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
846 }
847#endif
848
Dees_Troy16c2b312013-01-15 16:51:18 +0000849 if (Retain_Layout_Version && Mount(false) && TWFunc::Path_Exists(Layout_Filename))
850 TWFunc::copy_file(Layout_Filename, "/.layout_version", 0600);
851 else
852 unlink("/.layout_version");
Dees_Troy38bd7602012-09-14 13:33:53 -0400853
Dees_Troy16c2b312013-01-15 16:51:18 +0000854 if (Has_Data_Media) {
855 wiped = Wipe_Data_Without_Wiping_Media();
856 } else {
Gary Pecke8bc5d72012-12-21 06:45:25 -0800857
Dees_Troy16c2b312013-01-15 16:51:18 +0000858 DataManager::GetValue(TW_RM_RF_VAR, check);
859
860 if (check)
861 wiped = Wipe_RMRF();
862 else if (New_File_System == "ext4")
863 wiped = Wipe_EXT4();
864 else if (New_File_System == "ext2" || New_File_System == "ext3")
865 wiped = Wipe_EXT23(New_File_System);
866 else if (New_File_System == "vfat")
867 wiped = Wipe_FAT();
868 else if (New_File_System == "exfat")
869 wiped = Wipe_EXFAT();
870 else if (New_File_System == "yaffs2")
871 wiped = Wipe_MTD();
872 else {
873 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
874 unlink("/.layout_version");
875 return false;
876 }
877 update_crypt = wiped;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800878 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400879
Gary Pecke8bc5d72012-12-21 06:45:25 -0800880 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000881#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
882 if (Mount_Point == "/data" && Mount(false)) {
883 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
884 Make_Dir("/data/system", true);
885 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
886 }
887 }
888#endif
Dees_Troy16c2b312013-01-15 16:51:18 +0000889
Dees_Troy1c1ac442013-01-17 21:42:14 +0000890 if (Mount_Point == "/cache")
891 DataManager::Output_Version();
892
Dees_Troy16c2b312013-01-15 16:51:18 +0000893 if (TWFunc::Path_Exists("/.layout_version") && Mount(false))
894 TWFunc::copy_file("/.layout_version", Layout_Filename, 0600);
895
896 if (update_crypt) {
897 Setup_File_System(false);
898 if (Is_Encrypted && !Is_Decrypted) {
899 // just wiped an encrypted partition back to its unencrypted state
900 Is_Encrypted = false;
901 Is_Decrypted = false;
902 Decrypted_Block_Device = "";
903 if (Mount_Point == "/data") {
904 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
905 DataManager::SetValue(TW_IS_DECRYPTED, 0);
906 }
Gary Pecke8bc5d72012-12-21 06:45:25 -0800907 }
908 }
909 }
910 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400911}
912
Gary Peck43acadf2012-11-21 21:19:01 -0800913bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800914 if (Is_File_System(Current_File_System))
915 return Wipe(Current_File_System);
916 else
917 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -0800918}
919
Dees_Troye58d5262012-09-21 12:27:57 -0400920bool TWPartition::Wipe_AndSec(void) {
921 if (!Has_Android_Secure)
922 return false;
923
Dees_Troye58d5262012-09-21 12:27:57 -0400924 if (!Mount(true))
925 return false;
926
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500927 ui_print("Wiping .android_secure\n");
928 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -0400929 return true;
930}
931
Dees_Troy51a0e822012-09-05 15:24:24 -0400932bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400933 if (Backup_Method == FILES)
934 return Backup_Tar(backup_folder);
935 else if (Backup_Method == DD)
936 return Backup_DD(backup_folder);
937 else if (Backup_Method == FLASH_UTILS)
938 return Backup_Dump_Image(backup_folder);
939 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
940 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400941}
942
Dees_Troy43d8b002012-09-17 16:00:01 -0400943bool TWPartition::Check_MD5(string restore_folder) {
944 string Full_Filename;
945 char split_filename[512];
946 int index = 0;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500947 twrpDigest md5sum;
Dees_Troy43d8b002012-09-17 16:00:01 -0400948
949 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400950 if (!TWFunc::Path_Exists(Full_Filename)) {
951 // This is a split archive, we presume
952 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
953 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500954 md5sum.setfn(split_filename);
955 if (md5sum.verify_md5digest() != 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400956 LOGE("MD5 failed to match on '%s'.\n", split_filename);
957 return false;
958 }
959 index++;
960 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400961 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400962 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400963 } else {
964 // Single file archive
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -0500965 md5sum.setfn(Full_Filename);
966 if (md5sum.verify_md5digest() != 0) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400967 LOGE("MD5 failed to match on '%s'.\n", split_filename);
968 return false;
969 } else
970 return true;
971 }
972 return false;
973}
974
Dees_Troy51a0e822012-09-05 15:24:24 -0400975bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -0800976 size_t first_period, second_period;
977 string Restore_File_System;
978
979 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
980 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
981
982 // Parse backup filename to extract the file system before wiping
983 first_period = Backup_FileName.find(".");
984 if (first_period == string::npos) {
985 LOGE("Unable to find file system (first period).\n");
986 return false;
987 }
988 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
989 second_period = Restore_File_System.find(".");
990 if (second_period == string::npos) {
991 LOGE("Unable to find file system (second period).\n");
992 return false;
993 }
994 Restore_File_System.resize(second_period);
995 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
996
997 if (Is_File_System(Restore_File_System))
998 return Restore_Tar(restore_folder, Restore_File_System);
999 else if (Is_Image(Restore_File_System)) {
1000 if (Restore_File_System == "emmc")
1001 return Restore_DD(restore_folder);
1002 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
1003 return Restore_Flash_Image(restore_folder);
1004 }
1005
Dees_Troy38bd7602012-09-14 13:33:53 -04001006 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
1007 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001008}
1009
1010string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001011 if (Backup_Method == NONE)
1012 return "none";
1013 else if (Backup_Method == FILES)
1014 return "files";
1015 else if (Backup_Method == DD)
1016 return "dd";
1017 else if (Backup_Method == FLASH_UTILS)
1018 return "flash_utils";
1019 else
1020 return "undefined";
1021 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -04001022}
1023
1024bool TWPartition::Decrypt(string Password) {
1025 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -04001026 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -04001027 return 1;
1028}
1029
1030bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001031 bool Save_Data_Media = Has_Data_Media;
1032
1033 if (!UnMount(true))
1034 return false;
1035
Dees_Troy38bd7602012-09-14 13:33:53 -04001036 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -08001037 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001038 Has_Data_Media = Save_Data_Media;
1039 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
1040 Recreate_Media_Folder();
1041 }
Dees_Troyb46a6842012-09-25 11:06:46 -04001042 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001043 return true;
1044 } else {
1045 Has_Data_Media = Save_Data_Media;
1046 LOGE("Unable to format to remove encryption.\n");
1047 return false;
1048 }
1049 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001050}
1051
1052void TWPartition::Check_FS_Type() {
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001053 const char* type;
1054 blkid_probe pr;
Dees_Troy5bf43922012-09-07 16:07:55 -04001055
Dees_Troy68cab492012-12-12 19:29:35 +00001056 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
1057 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -04001058
Dees_Troy38bd7602012-09-14 13:33:53 -04001059 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -04001060 if (!Is_Present)
1061 return;
Dees_Troy51127312012-09-08 13:08:49 -04001062
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001063 pr = blkid_new_probe_from_filename(Actual_Block_Device.c_str());
1064 if (blkid_do_fullprobe(pr)) {
1065 blkid_free_probe(pr);
1066 LOGI("Can't probe device %s\n", Actual_Block_Device.c_str());
1067 return;
Dees_Troy5bf43922012-09-07 16:07:55 -04001068 }
bigbiff bigbiffe60683a2013-02-22 20:55:50 -05001069 if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) < 0) {
1070 blkid_free_probe(pr);
1071 LOGI("can't find filesystem on device %s\n", Actual_Block_Device.c_str());
1072 return;
1073 }
1074 Current_File_System = type;
Dees_Troy51a0e822012-09-05 15:24:24 -04001075 return;
1076}
1077
Gary Peck43acadf2012-11-21 21:19:01 -08001078bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001079 if (!UnMount(true))
1080 return false;
1081
Dees_Troy43d8b002012-09-17 16:00:01 -04001082 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001083 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001084
1085 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
1086 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001087 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
1088 LOGI("mke2fs command: %s\n", command.c_str());
1089 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001090 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001091 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001092 ui_print("Done.\n");
1093 return true;
1094 } else {
1095 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1096 return false;
1097 }
1098 } else
1099 return Wipe_RMRF();
1100
1101 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001102}
1103
1104bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001105 if (!UnMount(true))
1106 return false;
1107
Dees_Troy43d8b002012-09-17 16:00:01 -04001108 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001109 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001110
1111 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
1112 Find_Actual_Block_Device();
1113 Command = "make_ext4fs";
1114 if (!Is_Decrypted && Length != 0) {
1115 // Only use length if we're not decrypted
1116 char len[32];
1117 sprintf(len, "%i", Length);
1118 Command += " -l ";
1119 Command += len;
1120 }
1121 Command += " " + Actual_Block_Device;
1122 LOGI("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001123 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001124 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001125 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001126 ui_print("Done.\n");
1127 return true;
1128 } else {
1129 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1130 return false;
1131 }
1132 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001133 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001134
1135 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001136}
1137
1138bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001139 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001140
Dees_Troy43d8b002012-09-17 16:00:01 -04001141 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001142 if (!UnMount(true))
1143 return false;
1144
1145 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1146 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001147 command = "mkdosfs " + Actual_Block_Device;
1148 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001149 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001150 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001151 ui_print("Done.\n");
1152 return true;
1153 } else {
1154 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1155 return false;
1156 }
1157 return true;
1158 }
1159 else
1160 return Wipe_RMRF();
1161
1162 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001163}
1164
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001165bool TWPartition::Wipe_EXFAT() {
1166 string command, result;
1167
1168 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1169 if (!UnMount(true))
1170 return false;
1171
1172 ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
1173 Find_Actual_Block_Device();
1174 command = "mkexfatfs " + Actual_Block_Device;
1175 if (TWFunc::Exec_Cmd(command, result) == 0) {
1176 Recreate_AndSec_Folder();
1177 ui_print("Done.\n");
1178 return true;
1179 } else {
1180 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1181 return false;
1182 }
1183 return true;
1184 }
1185 return false;
1186}
1187
Dees_Troy38bd7602012-09-14 13:33:53 -04001188bool TWPartition::Wipe_MTD() {
1189 if (!UnMount(true))
1190 return false;
1191
1192 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1193
1194 mtd_scan_partitions();
1195 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1196 if (mtd == NULL) {
1197 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1198 return false;
1199 }
1200
1201 MtdWriteContext* ctx = mtd_write_partition(mtd);
1202 if (ctx == NULL) {
1203 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1204 return false;
1205 }
1206 if (mtd_erase_blocks(ctx, -1) == -1) {
1207 mtd_write_close(ctx);
1208 LOGE("Failed to format '%s'", MTD_Name.c_str());
1209 return false;
1210 }
1211 if (mtd_write_close(ctx) != 0) {
1212 LOGE("Failed to close '%s'", MTD_Name.c_str());
1213 return false;
1214 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001215 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001216 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001217 ui_print("Done.\n");
1218 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001219}
1220
1221bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001222 if (!Mount(true))
1223 return false;
1224
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001225 ui_print("Removing all files under '%s'\n", Mount_Point.c_str());
1226 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001227 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001228 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001229}
1230
1231bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001232 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001233
1234 // This handles wiping data on devices with "sdcard" in /data/media
1235 if (!Mount(true))
1236 return false;
1237
1238 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001239
1240 DIR* d;
1241 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001242 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001243 struct dirent* de;
1244 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001245 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1246 // The media folder is the "internal sdcard"
1247 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1248 // the media folder for multi-user.
1249 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001250
1251 dir = "/data/";
1252 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001253 if (de->d_type == DT_DIR) {
1254 TWFunc::removeDir(dir, false);
bigbiff bigbiff98f1f902013-01-19 18:46:13 -05001255 } else if (de->d_type == DT_REG || de->d_type == DT_LNK || de->d_type == DT_FIFO || de->d_type == DT_SOCK) {
Dees_Troyce675462013-01-09 19:48:21 +00001256 if (!unlink(dir.c_str()))
1257 LOGI("Unable to unlink '%s'\n", dir.c_str());
1258 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001259 }
1260 closedir(d);
Dees_Troy16b74352012-11-14 22:27:31 +00001261 ui_print("Done.\n");
1262 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001263 }
Dees_Troy16b74352012-11-14 22:27:31 +00001264 ui_print("Dirent failed to open /data, error!\n");
1265 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001266}
1267
1268bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001269 char back_name[255], split_index[5];
1270 string Full_FileName, Split_FileName, Tar_Args, Command;
1271 int use_compression, index, backup_count;
1272 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001273 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001274 twrpTar tar;
1275 vector <string> files;
bigbiff bigbiffcdcfee42013-02-27 21:11:26 -05001276
Dees_Troy43d8b002012-09-17 16:00:01 -04001277 if (!Mount(true))
1278 return false;
1279
Dees_Troy2c50e182012-09-26 20:05:28 -04001280 if (Backup_Path == "/and-sec") {
1281 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1282 ui_print("Backing up %s...\n", "Android Secure");
1283 } else {
1284 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1285 ui_print("Backing up %s...\n", Display_Name.c_str());
1286 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001287
1288 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001289
1290 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1291 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001292 Full_FileName = backup_folder + "/" + Backup_FileName;
1293 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001294 // This backup needs to be split into multiple archives
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001295 ui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001296 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001297 tar.setdir(back_name);
1298 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001299 backup_count = tar.splitArchiveFork();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001300 if (backup_count == -1) {
1301 LOGE("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001302 return false;
1303 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001304 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001305 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001306 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001307 if (use_compression) {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001308 tar.setdir(Backup_Path);
1309 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001310 if (tar.createTarGZFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001311 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001312 string gzname = Full_FileName + ".gz";
1313 rename(gzname.c_str(), Full_FileName.c_str());
1314 }
1315 else {
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001316 tar.setdir(Backup_Path);
1317 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001318 if (tar.createTarFork() != 0)
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001319 return -1;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001320 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001321 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1322 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1323 return false;
1324 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001325 }
1326 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001327}
1328
1329bool TWPartition::Backup_DD(string backup_folder) {
igoriok87e3d932013-01-31 21:03:53 +02001330 char back_name[255], backup_size[32];
1331 string Full_FileName, Command, result, DD_BS;
Dees_Troy43d8b002012-09-17 16:00:01 -04001332 int use_compression;
1333
igoriok87e3d932013-01-31 21:03:53 +02001334 sprintf(backup_size, "%llu", Backup_Size);
1335 DD_BS = backup_size;
1336
Dees_Troyb46a6842012-09-25 11:06:46 -04001337 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001338 ui_print("Backing up %s...\n", Display_Name.c_str());
1339
1340 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1341 Backup_FileName = back_name;
1342
1343 Full_FileName = backup_folder + "/" + Backup_FileName;
1344
igoriok87e3d932013-01-31 21:03:53 +02001345 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'" + " bs=" + DD_BS + "c count=1";
Dees_Troy43d8b002012-09-17 16:00:01 -04001346 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001347 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001348 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1349 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001350 return false;
1351 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001352 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001353}
1354
1355bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001356 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001357 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001358 int use_compression;
1359
Dees_Troyb46a6842012-09-25 11:06:46 -04001360 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001361 ui_print("Backing up %s...\n", Display_Name.c_str());
1362
1363 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1364 Backup_FileName = back_name;
1365
1366 Full_FileName = backup_folder + "/" + Backup_FileName;
1367
1368 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1369 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001370 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001371 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1372 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1373 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1374 return false;
1375 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001376 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001377}
1378
Gary Peck43acadf2012-11-21 21:19:01 -08001379bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1380 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001381 int index = 0;
1382 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001383
Dees_Troye58d5262012-09-21 12:27:57 -04001384 if (Has_Android_Secure) {
1385 ui_print("Wiping android secure...\n");
1386 if (!Wipe_AndSec())
1387 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001388 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001389 ui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001390 if (!Wipe(Restore_File_System))
1391 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001392 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001393
1394 if (!Mount(true))
1395 return false;
1396
Dees_Troyda8b55a2012-12-12 19:18:30 +00001397 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001398 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001399 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001400 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001401 if (!TWFunc::Path_Exists(Full_FileName)) {
1402 // Backup is multiple archives
1403 LOGI("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001404 sprintf(split_index, "%03i", index);
1405 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001406 while (TWFunc::Path_Exists(Full_FileName)) {
1407 index++;
1408 ui_print("Restoring archive %i...\n", index);
1409 LOGI("Restoring '%s'...\n", Full_FileName.c_str());
1410 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001411 tar.setdir("/");
1412 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001413 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001414 return false;
1415 sprintf(split_index, "%03i", index);
1416 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1417 }
1418 if (index == 0) {
1419 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1420 return false;
1421 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001422 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001423 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001424 twrpTar tar;
bigbiff bigbiff3bf2b0e2013-01-21 21:26:43 -05001425 tar.setdir(Backup_Path);
1426 tar.setfn(Full_FileName);
bigbiff bigbiffe6594ab2013-02-17 20:18:31 -05001427 if (tar.extractTarFork() != 0)
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001428 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001429 }
1430 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001431}
1432
1433bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001434 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001435
Dees_Troyda8b55a2012-12-12 19:18:30 +00001436 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001437 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001438
1439 if (!Find_Partition_Size()) {
1440 LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
1441 return false;
1442 }
1443 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1444 if (backup_size > Size) {
1445 LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
1446 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1447 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1448 return false;
1449 }
1450
1451 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001452 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1453 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001454 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001455 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001456}
1457
1458bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001459 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001460
1461 ui_print("Restoring %s...\n", Display_Name.c_str());
1462 Full_FileName = restore_folder + "/" + Backup_FileName;
1463 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1464 Command = "erase_image " + MTD_Name;
1465 LOGI("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001466 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001467 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1468 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001469 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001470 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001471}
Dees_Troy5bf43922012-09-07 16:07:55 -04001472
1473bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001474 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001475
Dees_Troyab10ee22012-09-21 14:27:30 -04001476 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001477 return false;
1478
Dees_Troy0550cfb2012-10-13 11:56:13 -04001479 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001480 if (Removable || Is_Encrypted) {
1481 if (!Mount(false))
1482 return true;
1483 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001484 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001485
1486 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001487 if (!ret || Size == 0) {
1488 if (!Get_Size_Via_df(Display_Error)) {
1489 if (!Was_Already_Mounted)
1490 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001491 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001492 }
1493 }
Dees_Troy51127312012-09-08 13:08:49 -04001494
Dees_Troy5bf43922012-09-07 16:07:55 -04001495 if (Has_Data_Media) {
1496 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001497 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001498 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1499 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001500 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001501 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001502 int bak = (int)(Backup_Size / 1048576LLU);
1503 int total = (int)(Size / 1048576LLU);
1504 int us = (int)(Used / 1048576LLU);
1505 int fre = (int)(Free / 1048576LLU);
1506 int datmed = (int)(data_media_used / 1048576LLU);
1507 LOGI("Data backup size is %iMB, size: %iMB, used: %iMB, free: %iMB, in data/media: %iMB.\n", bak, total, us, fre, datmed);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001508 } else {
1509 if (!Was_Already_Mounted)
1510 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001511 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001512 }
Dees_Troye58d5262012-09-21 12:27:57 -04001513 } else if (Has_Android_Secure) {
1514 if (Mount(Display_Error))
1515 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001516 else {
1517 if (!Was_Already_Mounted)
1518 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001519 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001520 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001521 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001522 if (!Was_Already_Mounted)
1523 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001524 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001525}
Dees_Troy38bd7602012-09-14 13:33:53 -04001526
1527void TWPartition::Find_Actual_Block_Device(void) {
1528 if (Is_Decrypted) {
1529 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001530 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001531 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001532 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001533 Is_Present = true;
1534 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001535 return;
1536 }
1537 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001538 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001539 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001540 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001541 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001542 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001543 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001544}
1545
1546void TWPartition::Recreate_Media_Folder(void) {
1547 string Command;
1548
1549 if (!Mount(true)) {
1550 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001551 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001552 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001553 LOGI("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001554 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1555 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001556 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001557}
Dees_Troye58d5262012-09-21 12:27:57 -04001558
1559void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001560 if (!Has_Android_Secure)
1561 return;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001562 LOGI("Creating .android_secure: %s\n", Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001563 if (!Mount(true)) {
1564 LOGE("Unable to recreate android secure folder.\n");
1565 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1566 LOGI("Recreating android secure folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001567 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1568 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1569 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001570 }
1571}