blob: 076c1cfb1ceaa72d2321d610203f20749526959f [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
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "variables.h"
39#include "common.h"
40#include "partitions.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040041#include "data.hpp"
Dees_Troy43d8b002012-09-17 16:00:01 -040042#include "twrp-functions.hpp"
bigbiff bigbiff9c754052013-01-09 09:09:08 -050043#include "twrpTar.hpp"
Dees_Troy5bf43922012-09-07 16:07:55 -040044extern "C" {
Dees_Troy38bd7602012-09-14 13:33:53 -040045 #include "mtdutils/mtdutils.h"
46 #include "mtdutils/mounts.h"
Dees_Troy85f44ed2013-01-09 18:42:36 +000047#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
48 #include "crypto/libcrypt_samsung/include/libcrypt_samsung.h"
49#endif
Dees_Troy5bf43922012-09-07 16:07:55 -040050}
Dees_Troy51a0e822012-09-05 15:24:24 -040051
bigbiff bigbiff9c754052013-01-09 09:09:08 -050052using namespace std;
53
Dees_Troy51a0e822012-09-05 15:24:24 -040054TWPartition::TWPartition(void) {
55 Can_Be_Mounted = false;
56 Can_Be_Wiped = false;
57 Wipe_During_Factory_Reset = false;
58 Wipe_Available_in_GUI = false;
59 Is_SubPartition = false;
Dees_Troy2691f9d2012-09-24 11:15:49 -040060 Has_SubPartition = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040061 SubPartition_Of = "";
62 Symlink_Path = "";
63 Symlink_Mount_Point = "";
64 Mount_Point = "";
Dees_Troye58d5262012-09-21 12:27:57 -040065 Backup_Path = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040066 Actual_Block_Device = "";
67 Primary_Block_Device = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040068 Alternate_Block_Device = "";
69 Removable = false;
70 Is_Present = false;
71 Length = 0;
72 Size = 0;
73 Used = 0;
74 Free = 0;
75 Backup_Size = 0;
76 Can_Be_Encrypted = false;
77 Is_Encrypted = false;
78 Is_Decrypted = false;
79 Decrypted_Block_Device = "";
80 Display_Name = "";
81 Backup_Name = "";
Dees_Troy63c8df72012-09-10 14:02:05 -040082 Backup_FileName = "";
Dees_Troy38bd7602012-09-14 13:33:53 -040083 MTD_Name = "";
Dees_Troy51a0e822012-09-05 15:24:24 -040084 Backup_Method = NONE;
85 Has_Data_Media = false;
Dees_Troye58d5262012-09-21 12:27:57 -040086 Has_Android_Secure = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040087 Is_Storage = false;
88 Storage_Path = "";
89 Current_File_System = "";
90 Fstab_File_System = "";
91 Format_Block_Size = 0;
Dees_Troy68cab492012-12-12 19:29:35 +000092 Ignore_Blkid = false;
Dees_Troy85f44ed2013-01-09 18:42:36 +000093#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
94 EcryptFS_Password = "";
95#endif
Dees_Troy51a0e822012-09-05 15:24:24 -040096}
97
98TWPartition::~TWPartition(void) {
99 // Do nothing
100}
101
Dees_Troy5bf43922012-09-07 16:07:55 -0400102bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) {
103 char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH];
104 int line_len = Line.size(), index = 0, item_index = 0;
105 char* ptr;
Dees_Troy51127312012-09-08 13:08:49 -0400106 string Flags;
Dees_Troy5bf43922012-09-07 16:07:55 -0400107 strncpy(full_line, Line.c_str(), line_len);
108
Dees_Troy51127312012-09-08 13:08:49 -0400109 for (index = 0; index < line_len; index++) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400110 if (full_line[index] <= 32)
111 full_line[index] = '\0';
Dees_Troy5bf43922012-09-07 16:07:55 -0400112 }
Dees_Troy7c2dec82012-09-26 09:49:14 -0400113 Mount_Point = full_line;
114 LOGI("Processing '%s'\n", Mount_Point.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -0400115 Backup_Path = Mount_Point;
Dees_Troy5bf43922012-09-07 16:07:55 -0400116 index = Mount_Point.size();
117 while (index < line_len) {
118 while (index < line_len && full_line[index] == '\0')
119 index++;
120 if (index >= line_len)
121 continue;
122 ptr = full_line + index;
123 if (item_index == 0) {
124 // File System
125 Fstab_File_System = ptr;
126 Current_File_System = ptr;
127 item_index++;
128 } else if (item_index == 1) {
129 // Primary Block Device
Dees_Troy38bd7602012-09-14 13:33:53 -0400130 if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") {
Dees_Troy094207a2012-09-26 12:00:39 -0400131 MTD_Name = ptr;
132 Find_MTD_Block_Device(MTD_Name);
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400133 } else if (Fstab_File_System == "bml") {
134 if (Mount_Point == "/boot")
135 MTD_Name = "boot";
136 else if (Mount_Point == "/recovery")
137 MTD_Name = "recovery";
138 Primary_Block_Device = ptr;
139 if (*ptr != '/')
140 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 -0400141 } else if (*ptr != '/') {
Dees_Troy5bf43922012-09-07 16:07:55 -0400142 if (Display_Error)
143 LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
144 else
145 LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index);
146 return 0;
Dees_Troy38bd7602012-09-14 13:33:53 -0400147 } else {
148 Primary_Block_Device = ptr;
149 Find_Real_Block_Device(Primary_Block_Device, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400150 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400151 item_index++;
152 } else if (item_index > 1) {
153 if (*ptr == '/') {
154 // Alternate Block Device
155 Alternate_Block_Device = ptr;
156 Find_Real_Block_Device(Alternate_Block_Device, Display_Error);
157 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
158 // Partition length
159 ptr += 7;
160 Length = atoi(ptr);
Dees_Troy51127312012-09-08 13:08:49 -0400161 } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) {
162 // Custom flags, save for later so that new values aren't overwritten by defaults
163 ptr += 6;
164 Flags = ptr;
Dees_Troy68cab492012-12-12 19:29:35 +0000165 Process_Flags(Flags, Display_Error);
Dees_Troy38bd7602012-09-14 13:33:53 -0400166 } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) {
167 // Do nothing
Dees_Troy5bf43922012-09-07 16:07:55 -0400168 } else {
169 // Unhandled data
Dees_Troyab10ee22012-09-21 14:27:30 -0400170 LOGI("Unhandled fstab information: '%s', %i, line: '%s'\n", ptr, index, Line.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400171 }
172 }
173 while (index < line_len && full_line[index] != '\0')
174 index++;
175 }
176
177 if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) {
178 if (Display_Error)
179 LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str());
180 else
181 LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str());
182 return 0;
183 } else if (Is_File_System(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400184 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400185 Setup_File_System(Display_Error);
186 if (Mount_Point == "/system") {
187 Display_Name = "System";
188 Wipe_Available_in_GUI = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400189 } else if (Mount_Point == "/data") {
190 Display_Name = "Data";
191 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400192 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400193#ifdef RECOVERY_SDCARD_ON_DATA
194 Has_Data_Media = true;
Dees_Troy51127312012-09-08 13:08:49 -0400195 Is_Storage = true;
196 Storage_Path = "/data/media";
Dees_Troy16b74352012-11-14 22:27:31 +0000197 Symlink_Path = Storage_Path;
Dees_Troy657c3092012-09-10 20:32:10 -0400198 if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) {
199 Make_Dir("/emmc", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400200 Symlink_Mount_Point = "/emmc";
201 } else {
202 Make_Dir("/sdcard", Display_Error);
Dees_Troy657c3092012-09-10 20:32:10 -0400203 Symlink_Mount_Point = "/sdcard";
204 }
Dees_Troy16b74352012-11-14 22:27:31 +0000205 if (Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
206 Storage_Path = "/data/media/0";
207 Symlink_Path = Storage_Path;
208 DataManager::SetValue(TW_INTERNAL_PATH, "/data/media/0");
209 UnMount(true);
210 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400211#endif
212#ifdef TW_INCLUDE_CRYPTO
213 Can_Be_Encrypted = true;
Dees_Troy657c3092012-09-10 20:32:10 -0400214 char crypto_blkdev[255];
215 property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error");
216 if (strcmp(crypto_blkdev, "error") != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400217 DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device);
Dees_Troy657c3092012-09-10 20:32:10 -0400218 DataManager::SetValue(TW_IS_DECRYPTED, 1);
219 Is_Encrypted = true;
220 Is_Decrypted = true;
221 Decrypted_Block_Device = crypto_blkdev;
222 LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev);
223 } else if (!Mount(false)) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400224 Is_Encrypted = true;
225 Is_Decrypted = false;
Gary Peck82599a82012-11-21 16:23:12 -0800226 Can_Be_Mounted = false;
227 Current_File_System = "emmc";
228 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400229 DataManager::SetValue(TW_IS_ENCRYPTED, 1);
230 DataManager::SetValue(TW_CRYPTO_PASSWORD, "");
231 DataManager::SetValue("tw_crypto_display", "");
Gary Peck82599a82012-11-21 16:23:12 -0800232 } else {
233 // Filesystem is not encrypted and the mount
234 // succeeded, so get it back to the original
235 // unmounted state
236 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -0400237 }
Dees_Troy9b21af72012-10-01 15:51:46 -0400238 #ifdef RECOVERY_SDCARD_ON_DATA
239 if (!Is_Encrypted || (Is_Encrypted && Is_Decrypted))
240 Recreate_Media_Folder();
241 #endif
242#else
243 #ifdef RECOVERY_SDCARD_ON_DATA
244 Recreate_Media_Folder();
245 #endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400246#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400247 } else if (Mount_Point == "/cache") {
248 Display_Name = "Cache";
249 Wipe_Available_in_GUI = true;
Dees_Troy51127312012-09-08 13:08:49 -0400250 Wipe_During_Factory_Reset = true;
Dees_Troyce2fe772012-09-28 12:34:33 -0400251 if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) {
Dees_Troyb46a6842012-09-25 11:06:46 -0400252 LOGI("Recreating /cache/recovery folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500253 if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0)
254 return -1;
Dees_Troyb46a6842012-09-25 11:06:46 -0400255 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400256 } else if (Mount_Point == "/datadata") {
Dees_Troy51127312012-09-08 13:08:49 -0400257 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400258 Display_Name = "DataData";
259 Is_SubPartition = true;
260 SubPartition_Of = "/data";
Dees_Troy5bf43922012-09-07 16:07:55 -0400261 DataManager::SetValue(TW_HAS_DATADATA, 1);
262 } else if (Mount_Point == "/sd-ext") {
Dees_Troy51127312012-09-08 13:08:49 -0400263 Wipe_During_Factory_Reset = true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400264 Display_Name = "SD-Ext";
265 Wipe_Available_in_GUI = true;
Dees_Troyc51f1f92012-09-20 15:32:13 -0400266 Removable = true;
Dees_Troy2c50e182012-09-26 20:05:28 -0400267 } else if (Mount_Point == "/boot") {
268 Display_Name = "Boot";
269 DataManager::SetValue("tw_boot_is_mountable", 1);
Dees_Troy8170a922012-09-18 15:40:25 -0400270 }
271#ifdef TW_EXTERNAL_STORAGE_PATH
272 if (Mount_Point == EXPAND(TW_EXTERNAL_STORAGE_PATH)) {
273 Is_Storage = true;
274 Storage_Path = EXPAND(TW_EXTERNAL_STORAGE_PATH);
Dees_Troyc51f1f92012-09-20 15:32:13 -0400275 Removable = true;
Dees_Troy8170a922012-09-18 15:40:25 -0400276 }
277#else
278 if (Mount_Point == "/sdcard") {
279 Is_Storage = true;
280 Storage_Path = "/sdcard";
Dees_Troyc51f1f92012-09-20 15:32:13 -0400281 Removable = true;
Dees_Troye58d5262012-09-21 12:27:57 -0400282#ifndef RECOVERY_SDCARD_ON_DATA
283 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400284 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400285#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400286 }
287#endif
288#ifdef TW_INTERNAL_STORAGE_PATH
289 if (Mount_Point == EXPAND(TW_INTERNAL_STORAGE_PATH)) {
290 Is_Storage = true;
291 Storage_Path = EXPAND(TW_INTERNAL_STORAGE_PATH);
Dees_Troye58d5262012-09-21 12:27:57 -0400292#ifndef RECOVERY_SDCARD_ON_DATA
293 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400294 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400295#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400296 }
297#else
298 if (Mount_Point == "/emmc") {
299 Is_Storage = true;
300 Storage_Path = "/emmc";
Dees_Troye58d5262012-09-21 12:27:57 -0400301#ifndef RECOVERY_SDCARD_ON_DATA
302 Setup_AndSec();
Dees_Troy8e337f32012-10-13 22:07:49 -0400303 Mount_Storage_Retry();
Dees_Troye58d5262012-09-21 12:27:57 -0400304#endif
Dees_Troy8170a922012-09-18 15:40:25 -0400305 }
306#endif
Dees_Troy5bf43922012-09-07 16:07:55 -0400307 } else if (Is_Image(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400308 Find_Actual_Block_Device();
Dees_Troy5bf43922012-09-07 16:07:55 -0400309 Setup_Image(Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400310 }
311
Dees_Troy51127312012-09-08 13:08:49 -0400312 // Process any custom flags
313 if (Flags.size() > 0)
314 Process_Flags(Flags, Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -0400315 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;
Dees_Troy68cab492012-12-12 19:29:35 +0000354 } else if (strcmp(ptr, "ignoreblkid") == 0) {
355 Ignore_Blkid = true;
Dees_Troy51127312012-09-08 13:08:49 -0400356 } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) {
357 ptr += 8;
358 Symlink_Path = ptr;
359 } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) {
360 ptr += 8;
361 Display_Name = ptr;
362 } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) {
363 ptr += 10;
364 Format_Block_Size = atoi(ptr);
365 } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) {
366 ptr += 7;
367 Length = atoi(ptr);
368 } else {
369 if (Display_Error)
370 LOGE("Unhandled flag: '%s'\n", ptr);
371 else
372 LOGI("Unhandled flag: '%s'\n", ptr);
373 }
374 while (index < flags_len && flags[index] != '\0')
375 index++;
376 }
377 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400378}
379
Dees_Troy5bf43922012-09-07 16:07:55 -0400380bool TWPartition::Is_File_System(string File_System) {
381 if (File_System == "ext2" ||
Dees_Troy63c8df72012-09-10 14:02:05 -0400382 File_System == "ext3" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400383 File_System == "ext4" ||
384 File_System == "vfat" ||
385 File_System == "ntfs" ||
386 File_System == "yaffs2" ||
bigbiff bigbiff3e146522012-11-14 14:32:59 -0500387 File_System == "exfat" ||
Dees_Troy5bf43922012-09-07 16:07:55 -0400388 File_System == "auto")
389 return true;
390 else
391 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400392}
393
Dees_Troy5bf43922012-09-07 16:07:55 -0400394bool TWPartition::Is_Image(string File_System) {
Dees_Troy5fcd8f92012-10-16 12:22:05 -0400395 if (File_System == "emmc" || File_System == "mtd" || File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400396 return true;
397 else
398 return false;
399}
400
Dees_Troy51127312012-09-08 13:08:49 -0400401bool TWPartition::Make_Dir(string Path, bool Display_Error) {
Dees_Troy43d8b002012-09-17 16:00:01 -0400402 if (!TWFunc::Path_Exists(Path)) {
Dees_Troy51127312012-09-08 13:08:49 -0400403 if (mkdir(Path.c_str(), 0777) == -1) {
404 if (Display_Error)
405 LOGE("Can not create '%s' folder.\n", Path.c_str());
406 else
407 LOGI("Can not create '%s' folder.\n", Path.c_str());
408 return false;
409 } else {
410 LOGI("Created '%s' folder.\n", Path.c_str());
411 return true;
412 }
413 }
414 return true;
415}
416
Dees_Troy5bf43922012-09-07 16:07:55 -0400417void TWPartition::Setup_File_System(bool Display_Error) {
418 struct statfs st;
419
420 Can_Be_Mounted = true;
421 Can_Be_Wiped = true;
422
Dees_Troy5bf43922012-09-07 16:07:55 -0400423 // Make the mount point folder if it doesn't exist
Dees_Troy51127312012-09-08 13:08:49 -0400424 Make_Dir(Mount_Point, Display_Error);
Dees_Troy5bf43922012-09-07 16:07:55 -0400425 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
426 Backup_Name = Display_Name;
427 Backup_Method = FILES;
428}
429
430void TWPartition::Setup_Image(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400431 Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1);
432 Backup_Name = Display_Name;
Gary Peck82599a82012-11-21 16:23:12 -0800433 if (Current_File_System == "emmc")
Dees_Troy5bf43922012-09-07 16:07:55 -0400434 Backup_Method = DD;
Gary Peck82599a82012-11-21 16:23:12 -0800435 else if (Current_File_System == "mtd" || Current_File_System == "bml")
Dees_Troy5bf43922012-09-07 16:07:55 -0400436 Backup_Method = FLASH_UTILS;
437 else
Gary Peck82599a82012-11-21 16:23:12 -0800438 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 -0400439 if (Find_Partition_Size()) {
440 Used = Size;
441 Backup_Size = Size;
Dees_Troy51a0e822012-09-05 15:24:24 -0400442 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400443 if (Display_Error)
Dees_Troy38bd7602012-09-14 13:33:53 -0400444 LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400445 else
Dees_Troy38bd7602012-09-14 13:33:53 -0400446 LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str());
Dees_Troy51a0e822012-09-05 15:24:24 -0400447 }
448}
449
Dees_Troye58d5262012-09-21 12:27:57 -0400450void TWPartition::Setup_AndSec(void) {
451 Backup_Name = "and-sec";
452 Has_Android_Secure = true;
453 Symlink_Path = Mount_Point + "/.android_secure";
454 Symlink_Mount_Point = "/and-sec";
455 Backup_Path = Symlink_Mount_Point;
456 Make_Dir("/and-sec", true);
457 Recreate_AndSec_Folder();
458}
459
Dees_Troy5bf43922012-09-07 16:07:55 -0400460void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) {
461 char device[512], realDevice[512];
462
463 strcpy(device, Block.c_str());
464 memset(realDevice, 0, sizeof(realDevice));
465 while (readlink(device, realDevice, sizeof(realDevice)) > 0)
466 {
467 strcpy(device, realDevice);
468 memset(realDevice, 0, sizeof(realDevice));
469 }
470
471 if (device[0] != '/') {
472 if (Display_Error)
473 LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
474 else
475 LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str());
476 return;
Dees_Troy51a0e822012-09-05 15:24:24 -0400477 } else {
Dees_Troy5bf43922012-09-07 16:07:55 -0400478 Block = device;
479 return;
480 }
481}
482
Dees_Troy8e337f32012-10-13 22:07:49 -0400483void TWPartition::Mount_Storage_Retry(void) {
484 // On some devices, storage doesn't want to mount right away, retry and sleep
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500485 if (!Mount(true)) {
Dees_Troy8e337f32012-10-13 22:07:49 -0400486 int retry_count = 5;
487 while (retry_count > 0 && !Mount(false)) {
488 usleep(500000);
489 retry_count--;
490 }
491 Mount(true);
492 }
493}
494
Dees_Troy38bd7602012-09-14 13:33:53 -0400495bool TWPartition::Find_MTD_Block_Device(string MTD_Name) {
496 FILE *fp = NULL;
497 char line[255];
498
499 fp = fopen("/proc/mtd", "rt");
500 if (fp == NULL) {
501 LOGE("Device does not support /proc/mtd\n");
502 return false;
503 }
504
505 while (fgets(line, sizeof(line), fp) != NULL)
506 {
507 char device[32], label[32];
508 unsigned long size = 0;
509 char* fstype = NULL;
510 int deviceId;
511
512 sscanf(line, "%s %lx %*s %*c%s", device, &size, label);
513
514 // Skip header and blank lines
515 if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8))
516 continue;
517
518 // Strip off the trailing " from the label
519 label[strlen(label)-1] = '\0';
520
521 if (strcmp(label, MTD_Name.c_str()) == 0) {
522 // We found our device
523 // Strip off the trailing : from the device
524 device[strlen(device)-1] = '\0';
525 if (sscanf(device,"mtd%d", &deviceId) == 1) {
526 sprintf(device, "/dev/block/mtdblock%d", deviceId);
527 Primary_Block_Device = device;
528 }
529 }
530 }
531 fclose(fp);
532
533 return false;
534}
535
Dees_Troy51127312012-09-08 13:08:49 -0400536bool TWPartition::Get_Size_Via_statfs(bool Display_Error) {
537 struct statfs st;
538 string Local_Path = Mount_Point + "/.";
539
540 if (!Mount(Display_Error))
541 return false;
542
543 if (statfs(Local_Path.c_str(), &st) != 0) {
544 if (!Removable) {
545 if (Display_Error)
546 LOGE("Unable to statfs '%s'\n", Local_Path.c_str());
547 else
548 LOGI("Unable to statfs '%s'\n", Local_Path.c_str());
549 }
550 return false;
551 }
552 Size = (st.f_blocks * st.f_bsize);
553 Used = ((st.f_blocks - st.f_bfree) * st.f_bsize);
554 Free = (st.f_bfree * st.f_bsize);
555 Backup_Size = Used;
556 return true;
557}
558
559bool TWPartition::Get_Size_Via_df(bool Display_Error) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400560 FILE* fp;
561 char command[255], line[512];
562 int include_block = 1;
563 unsigned int min_len;
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500564 string result;
Dees_Troy5bf43922012-09-07 16:07:55 -0400565
566 if (!Mount(Display_Error))
567 return false;
568
Dees_Troy38bd7602012-09-14 13:33:53 -0400569 min_len = Actual_Block_Device.size() + 2;
Dees_Troy51127312012-09-08 13:08:49 -0400570 sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500571 TWFunc::Exec_Cmd(command, result);
Dees_Troy51127312012-09-08 13:08:49 -0400572 fp = fopen("/tmp/dfoutput.txt", "rt");
573 if (fp == NULL) {
574 LOGI("Unable to open /tmp/dfoutput.txt.\n");
Dees_Troy5bf43922012-09-07 16:07:55 -0400575 return false;
Dees_Troy51127312012-09-08 13:08:49 -0400576 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400577
578 while (fgets(line, sizeof(line), fp) != NULL)
579 {
580 unsigned long blocks, used, available;
581 char device[64];
582 char tmpString[64];
583
584 if (strncmp(line, "Filesystem", 10) == 0)
585 continue;
586 if (strlen(line) < min_len) {
587 include_block = 0;
588 continue;
589 }
590 if (include_block) {
591 sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available);
592 } else {
593 // The device block string is so long that the df information is on the next line
594 int space_count = 0;
Dees_Troye58d5262012-09-21 12:27:57 -0400595 sprintf(tmpString, "/dev/block/%s", Actual_Block_Device.c_str());
Dees_Troy5bf43922012-09-07 16:07:55 -0400596 while (tmpString[space_count] == 32)
597 space_count++;
598 sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available);
599 }
600
601 // Adjust block size to byte size
602 Size = blocks * 1024ULL;
603 Used = used * 1024ULL;
604 Free = available * 1024ULL;
605 Backup_Size = Used;
606 }
607 fclose(fp);
608 return true;
609}
610
Dees_Troy5bf43922012-09-07 16:07:55 -0400611bool TWPartition::Find_Partition_Size(void) {
612 FILE* fp;
613 char line[512];
614 string tmpdevice;
615
616 // In this case, we'll first get the partitions we care about (with labels)
617 fp = fopen("/proc/partitions", "rt");
618 if (fp == NULL)
619 return false;
620
621 while (fgets(line, sizeof(line), fp) != NULL)
622 {
623 unsigned long major, minor, blocks;
624 char device[512];
625 char tmpString[64];
626
Dees_Troy63c8df72012-09-10 14:02:05 -0400627 if (strlen(line) < 7 || line[0] == 'm') continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400628 sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device);
629
630 tmpdevice = "/dev/block/";
631 tmpdevice += device;
Dees_Troy38bd7602012-09-14 13:33:53 -0400632 if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) {
Dees_Troy5bf43922012-09-07 16:07:55 -0400633 // Adjust block size to byte size
634 Size = blocks * 1024ULL;
635 fclose(fp);
636 return true;
637 }
638 }
639 fclose(fp);
640 return false;
641}
642
Dees_Troy5bf43922012-09-07 16:07:55 -0400643bool TWPartition::Is_Mounted(void) {
644 if (!Can_Be_Mounted)
645 return false;
646
647 struct stat st1, st2;
648 string test_path;
649
650 // Check to see if the mount point directory exists
651 test_path = Mount_Point + "/.";
652 if (stat(test_path.c_str(), &st1) != 0) return false;
653
654 // Check to see if the directory above the mount point exists
655 test_path = Mount_Point + "/../.";
656 if (stat(test_path.c_str(), &st2) != 0) return false;
657
658 // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device
659 int ret = (st1.st_dev != st2.st_dev) ? true : false;
660
661 return ret;
662}
663
664bool TWPartition::Mount(bool Display_Error) {
665 if (Is_Mounted()) {
666 return true;
667 } else if (!Can_Be_Mounted) {
668 return false;
669 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400670
671 Find_Actual_Block_Device();
672
673 // Check the current file system before mounting
674 Check_FS_Type();
Dees_Troy22042032012-12-18 21:23:08 +0000675 if (Fstab_File_System == "yaffs2") {
676 // mount an MTD partition as a YAFFS2 filesystem.
677 mtd_scan_partitions();
678 const MtdPartition* partition;
679 partition = mtd_find_partition_by_name(MTD_Name.c_str());
680 if (partition == NULL) {
681 LOGE("Failed to find '%s' partition to mount at '%s'\n",
682 MTD_Name.c_str(), Mount_Point.c_str());
683 return false;
684 }
685 if (mtd_mount_partition(partition, Mount_Point.c_str(), Fstab_File_System.c_str(), 0)) {
686 if (Display_Error)
687 LOGE("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
688 else
689 LOGI("Failed to mount '%s' (MTD)\n", Mount_Point.c_str());
690 return false;
691 } else
692 return true;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000693 } else if (Current_File_System == "exfat" && TWFunc::Path_Exists("/sbin/exfat-fuse")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500694 string cmd = "/sbin/exfat-fuse " + Actual_Block_Device + " " + Mount_Point;
Dees_Troy85f44ed2013-01-09 18:42:36 +0000695 LOGI("cmd: %s\n", cmd.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500696 string result;
697 if (TWFunc::Exec_Cmd(cmd, result) != 0)
698 return false;
Dees_Troy22042032012-12-18 21:23:08 +0000699 } else if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400700 if (Display_Error)
701 LOGE("Unable to mount '%s'\n", Mount_Point.c_str());
702 else
703 LOGI("Unable to mount '%s'\n", Mount_Point.c_str());
Dees_Troy9350b8d2012-09-27 12:38:38 -0400704 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 -0400705 return false;
706 } else {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000707#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
Dees_Troy999b39d2013-01-14 15:36:13 +0000708 string MetaEcfsFile = EXPAND(TW_EXTERNAL_STORAGE_PATH);
709 MetaEcfsFile += "/.MetaEcfsFile";
710 if (EcryptFS_Password.size() > 0 && PartitionManager.Mount_By_Path("/data", false) && TWFunc::Path_Exists(MetaEcfsFile)) {
Dees_Troy85f44ed2013-01-09 18:42:36 +0000711 if (mount_ecryptfs_drive(EcryptFS_Password.c_str(), Mount_Point.c_str(), Mount_Point.c_str(), 0) != 0) {
712 if (Display_Error)
713 LOGE("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
714 else
715 LOGI("Unable to mount ecryptfs for '%s'\n", Mount_Point.c_str());
716 } else {
717 LOGI("Successfully mounted ecryptfs for '%s'\n", Mount_Point.c_str());
718 }
719 }
720#endif
Dees_Troy38bd7602012-09-14 13:33:53 -0400721 if (Removable)
722 Update_Size(Display_Error);
723
724 if (!Symlink_Mount_Point.empty()) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500725 mount(Symlink_Path.c_str(), Symlink_Mount_Point.c_str(), Fstab_File_System.c_str(), NULL, NULL);
Dees_Troy51127312012-09-08 13:08:49 -0400726 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400727 return true;
Dees_Troy5bf43922012-09-07 16:07:55 -0400728 }
Dees_Troy5bf43922012-09-07 16:07:55 -0400729 return true;
730}
731
732bool TWPartition::UnMount(bool Display_Error) {
733 if (Is_Mounted()) {
734 int never_unmount_system;
735
736 DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system);
737 if (never_unmount_system == 1 && Mount_Point == "/system")
738 return true; // Never unmount system if you're not supposed to unmount it
739
Dees_Troyc8bafa12013-01-10 15:43:00 +0000740#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
741 if (EcryptFS_Password.size() > 0) {
742 if (unmount_ecryptfs_drive(Mount_Point.c_str()) != 0) {
743 if (Display_Error)
744 LOGE("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
745 else
746 LOGI("Unable to unmount ecryptfs for '%s'\n", Mount_Point.c_str());
747 } else {
748 LOGI("Successfully unmounted ecryptfs for '%s'\n", Mount_Point.c_str());
749 }
750 }
751#endif
752
Dees_Troy38bd7602012-09-14 13:33:53 -0400753 if (!Symlink_Mount_Point.empty())
754 umount(Symlink_Mount_Point.c_str());
755
Dees_Troy5bf43922012-09-07 16:07:55 -0400756 if (umount(Mount_Point.c_str()) != 0) {
757 if (Display_Error)
758 LOGE("Unable to unmount '%s'\n", Mount_Point.c_str());
759 else
760 LOGI("Unable to unmount '%s'\n", Mount_Point.c_str());
761 return false;
762 } else
763 return true;
764 } else {
765 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -0400766 }
767}
768
Gary Peck43acadf2012-11-21 21:19:01 -0800769bool TWPartition::Wipe(string New_File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400770 if (!Can_Be_Wiped) {
771 LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str());
772 return false;
773 }
774
Dees_Troyc51f1f92012-09-20 15:32:13 -0400775 if (Mount_Point == "/cache")
776 tmplog_offset = 0;
777
Dees_Troyce675462013-01-09 19:48:21 +0000778#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
779 if (Mount_Point == "/data" && Mount(false)) {
780 if (TWFunc::Path_Exists("/data/system/edk_p_sd"))
781 TWFunc::copy_file("/data/system/edk_p_sd", "/tmp/edk_p_sd", 0600);
782 }
783#endif
784
Dees_Troy38bd7602012-09-14 13:33:53 -0400785 if (Has_Data_Media)
786 return Wipe_Data_Without_Wiping_Media();
787
788 int check;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800789 bool wiped = false;
Dees_Troy38bd7602012-09-14 13:33:53 -0400790 DataManager::GetValue(TW_RM_RF_VAR, check);
Gary Pecke8bc5d72012-12-21 06:45:25 -0800791
Dees_Troy38bd7602012-09-14 13:33:53 -0400792 if (check)
Gary Pecke8bc5d72012-12-21 06:45:25 -0800793 wiped = Wipe_RMRF();
794 else if (New_File_System == "ext4")
795 wiped = Wipe_EXT4();
796 else if (New_File_System == "ext2" || New_File_System == "ext3")
797 wiped = Wipe_EXT23(New_File_System);
798 else if (New_File_System == "vfat")
799 wiped = Wipe_FAT();
Dees_Troyce675462013-01-09 19:48:21 +0000800 else if (New_File_System == "exfat")
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500801 wiped = Wipe_EXFAT();
Gary Pecke8bc5d72012-12-21 06:45:25 -0800802 else if (New_File_System == "yaffs2")
803 wiped = Wipe_MTD();
804 else {
805 LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), New_File_System.c_str());
806 return false;
807 }
Dees_Troy38bd7602012-09-14 13:33:53 -0400808
Gary Pecke8bc5d72012-12-21 06:45:25 -0800809 if (wiped) {
Dees_Troyce675462013-01-09 19:48:21 +0000810#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
811 if (Mount_Point == "/data" && Mount(false)) {
812 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
813 Make_Dir("/data/system", true);
814 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
815 }
816 }
817#endif
Gary Pecke8bc5d72012-12-21 06:45:25 -0800818 Setup_File_System(false);
819 if (Is_Encrypted && !Is_Decrypted) {
820 // just wiped an encrypted partition back to its unencrypted state
821 Is_Encrypted = false;
822 Is_Decrypted = false;
823 Decrypted_Block_Device = "";
824 if (Mount_Point == "/data") {
825 DataManager::SetValue(TW_IS_ENCRYPTED, 0);
826 DataManager::SetValue(TW_IS_DECRYPTED, 0);
827 }
828 }
829 }
830 return wiped;
Dees_Troy51a0e822012-09-05 15:24:24 -0400831}
832
Gary Peck43acadf2012-11-21 21:19:01 -0800833bool TWPartition::Wipe() {
Gary Peck82599a82012-11-21 16:23:12 -0800834 if (Is_File_System(Current_File_System))
835 return Wipe(Current_File_System);
836 else
837 return Wipe(Fstab_File_System);
Gary Peck43acadf2012-11-21 21:19:01 -0800838}
839
Dees_Troye58d5262012-09-21 12:27:57 -0400840bool TWPartition::Wipe_AndSec(void) {
841 if (!Has_Android_Secure)
842 return false;
843
Dees_Troye58d5262012-09-21 12:27:57 -0400844 if (!Mount(true))
845 return false;
846
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500847 ui_print("Wiping .android_secure\n");
848 TWFunc::removeDir(Mount_Point + "/.android_secure/", true);
Dees_Troye58d5262012-09-21 12:27:57 -0400849 return true;
850}
851
Dees_Troy51a0e822012-09-05 15:24:24 -0400852bool TWPartition::Backup(string backup_folder) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400853 if (Backup_Method == FILES)
854 return Backup_Tar(backup_folder);
855 else if (Backup_Method == DD)
856 return Backup_DD(backup_folder);
857 else if (Backup_Method == FLASH_UTILS)
858 return Backup_Dump_Image(backup_folder);
859 LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str());
860 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400861}
862
Dees_Troy43d8b002012-09-17 16:00:01 -0400863bool TWPartition::Check_MD5(string restore_folder) {
864 string Full_Filename;
865 char split_filename[512];
866 int index = 0;
867
868 Full_Filename = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -0400869 if (!TWFunc::Path_Exists(Full_Filename)) {
870 // This is a split archive, we presume
871 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
872 while (index < 1000 && TWFunc::Path_Exists(split_filename)) {
873 if (TWFunc::Check_MD5(split_filename) == 0) {
874 LOGE("MD5 failed to match on '%s'.\n", split_filename);
875 return false;
876 }
877 index++;
878 sprintf(split_filename, "%s%03i", Full_Filename.c_str(), index);
Dees_Troy43d8b002012-09-17 16:00:01 -0400879 }
Dees_Troy4a2a1262012-09-18 09:33:47 -0400880 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -0400881 } else {
882 // Single file archive
883 if (TWFunc::Check_MD5(Full_Filename) == 0) {
884 LOGE("MD5 failed to match on '%s'.\n", split_filename);
885 return false;
886 } else
887 return true;
888 }
889 return false;
890}
891
Dees_Troy51a0e822012-09-05 15:24:24 -0400892bool TWPartition::Restore(string restore_folder) {
Gary Peck43acadf2012-11-21 21:19:01 -0800893 size_t first_period, second_period;
894 string Restore_File_System;
895
896 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
897 LOGI("Restore filename is: %s\n", Backup_FileName.c_str());
898
899 // Parse backup filename to extract the file system before wiping
900 first_period = Backup_FileName.find(".");
901 if (first_period == string::npos) {
902 LOGE("Unable to find file system (first period).\n");
903 return false;
904 }
905 Restore_File_System = Backup_FileName.substr(first_period + 1, Backup_FileName.size() - first_period - 1);
906 second_period = Restore_File_System.find(".");
907 if (second_period == string::npos) {
908 LOGE("Unable to find file system (second period).\n");
909 return false;
910 }
911 Restore_File_System.resize(second_period);
912 LOGI("Restore file system is: '%s'.\n", Restore_File_System.c_str());
913
914 if (Is_File_System(Restore_File_System))
915 return Restore_Tar(restore_folder, Restore_File_System);
916 else if (Is_Image(Restore_File_System)) {
917 if (Restore_File_System == "emmc")
918 return Restore_DD(restore_folder);
919 else if (Restore_File_System == "mtd" || Restore_File_System == "bml")
920 return Restore_Flash_Image(restore_folder);
921 }
922
Dees_Troy38bd7602012-09-14 13:33:53 -0400923 LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str());
924 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400925}
926
927string TWPartition::Backup_Method_By_Name() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400928 if (Backup_Method == NONE)
929 return "none";
930 else if (Backup_Method == FILES)
931 return "files";
932 else if (Backup_Method == DD)
933 return "dd";
934 else if (Backup_Method == FLASH_UTILS)
935 return "flash_utils";
936 else
937 return "undefined";
938 return "ERROR!";
Dees_Troy51a0e822012-09-05 15:24:24 -0400939}
940
941bool TWPartition::Decrypt(string Password) {
942 LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str());
Dees_Troy38bd7602012-09-14 13:33:53 -0400943 // Is this needed?
Dees_Troy51a0e822012-09-05 15:24:24 -0400944 return 1;
945}
946
947bool TWPartition::Wipe_Encryption() {
Dees_Troy38bd7602012-09-14 13:33:53 -0400948 bool Save_Data_Media = Has_Data_Media;
949
950 if (!UnMount(true))
951 return false;
952
Dees_Troy38bd7602012-09-14 13:33:53 -0400953 Has_Data_Media = false;
Gary Pecke8bc5d72012-12-21 06:45:25 -0800954 if (Wipe(Fstab_File_System)) {
Dees_Troy38bd7602012-09-14 13:33:53 -0400955 Has_Data_Media = Save_Data_Media;
956 if (Has_Data_Media && !Symlink_Mount_Point.empty()) {
957 Recreate_Media_Folder();
958 }
Dees_Troyb46a6842012-09-25 11:06:46 -0400959 ui_print("You may need to reboot recovery to be able to use /data again.\n");
Dees_Troy38bd7602012-09-14 13:33:53 -0400960 return true;
961 } else {
962 Has_Data_Media = Save_Data_Media;
963 LOGE("Unable to format to remove encryption.\n");
964 return false;
965 }
966 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400967}
968
969void TWPartition::Check_FS_Type() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500970 string blkCommand, result;
971 string blkOutput;
Dees_Troy5bf43922012-09-07 16:07:55 -0400972 char* blk;
973 char* arg;
974 char* ptr;
975
Dees_Troy68cab492012-12-12 19:29:35 +0000976 if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd" || Fstab_File_System == "bml" || Ignore_Blkid)
977 return; // Running blkid on some mtd devices causes a massive crash or needs to be skipped
Dees_Troy5bf43922012-09-07 16:07:55 -0400978
Dees_Troy38bd7602012-09-14 13:33:53 -0400979 Find_Actual_Block_Device();
Dees_Troy8170a922012-09-18 15:40:25 -0400980 if (!Is_Present)
981 return;
Dees_Troy51127312012-09-08 13:08:49 -0400982
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500983 blkCommand = "blkid " + Actual_Block_Device;
984 TWFunc::Exec_Cmd(blkCommand, result);
985 std::stringstream line(result);
986 while (getline(line, blkOutput))
Dees_Troy5bf43922012-09-07 16:07:55 -0400987 {
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500988 blk = (char*) blkOutput.c_str();
989 ptr = (char*) blkOutput.c_str();
Dees_Troy63c8df72012-09-10 14:02:05 -0400990 while (*ptr > 32 && *ptr != ':') ptr++;
991 if (*ptr == 0) continue;
Dees_Troy5bf43922012-09-07 16:07:55 -0400992 *ptr = 0;
993
994 // Increment by two, but verify that we don't hit a NULL
995 ptr++;
Dees_Troy63c8df72012-09-10 14:02:05 -0400996 if (*ptr != 0) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -0400997
998 // Now, find the TYPE field
999 while (1)
1000 {
1001 arg = ptr;
Dees_Troy63c8df72012-09-10 14:02:05 -04001002 while (*ptr > 32) ptr++;
Dees_Troy5bf43922012-09-07 16:07:55 -04001003 if (*ptr != 0)
1004 {
1005 *ptr = 0;
1006 ptr++;
1007 }
1008
1009 if (strlen(arg) > 6)
1010 {
1011 if (memcmp(arg, "TYPE=\"", 6) == 0) break;
1012 }
1013
1014 if (*ptr == 0)
1015 {
1016 arg = NULL;
1017 break;
1018 }
1019 }
1020
1021 if (arg && strlen(arg) > 7)
1022 {
1023 arg += 6; // Skip the TYPE=" portion
1024 arg[strlen(arg)-1] = '\0'; // Drop the tail quote
1025 }
1026 else
1027 continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001028 if (Current_File_System != arg) {
Dees_Troy5bf43922012-09-07 16:07:55 -04001029 LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg);
1030 Current_File_System = arg;
1031 }
1032 }
Dees_Troy51a0e822012-09-05 15:24:24 -04001033 return;
1034}
1035
Gary Peck43acadf2012-11-21 21:19:01 -08001036bool TWPartition::Wipe_EXT23(string File_System) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001037 if (!UnMount(true))
1038 return false;
1039
Dees_Troy43d8b002012-09-17 16:00:01 -04001040 if (TWFunc::Path_Exists("/sbin/mke2fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001041 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001042
1043 ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str());
1044 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001045 command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device;
1046 LOGI("mke2fs command: %s\n", command.c_str());
1047 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001048 Current_File_System = File_System;
Dees_Troye58d5262012-09-21 12:27:57 -04001049 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001050 ui_print("Done.\n");
1051 return true;
1052 } else {
1053 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1054 return false;
1055 }
1056 } else
1057 return Wipe_RMRF();
1058
1059 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001060}
1061
1062bool TWPartition::Wipe_EXT4() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001063 if (!UnMount(true))
1064 return false;
1065
Dees_Troy43d8b002012-09-17 16:00:01 -04001066 if (TWFunc::Path_Exists("/sbin/make_ext4fs")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001067 string Command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001068
1069 ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str());
1070 Find_Actual_Block_Device();
1071 Command = "make_ext4fs";
1072 if (!Is_Decrypted && Length != 0) {
1073 // Only use length if we're not decrypted
1074 char len[32];
1075 sprintf(len, "%i", Length);
1076 Command += " -l ";
1077 Command += len;
1078 }
1079 Command += " " + Actual_Block_Device;
1080 LOGI("make_ext4fs command: %s\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001081 if (TWFunc::Exec_Cmd(Command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001082 Current_File_System = "ext4";
Dees_Troye58d5262012-09-21 12:27:57 -04001083 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001084 ui_print("Done.\n");
1085 return true;
1086 } else {
1087 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1088 return false;
1089 }
1090 } else
Gary Peck43acadf2012-11-21 21:19:01 -08001091 return Wipe_EXT23("ext4");
Dees_Troy38bd7602012-09-14 13:33:53 -04001092
1093 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001094}
1095
1096bool TWPartition::Wipe_FAT() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001097 string command, result;
Dees_Troy38bd7602012-09-14 13:33:53 -04001098
Dees_Troy43d8b002012-09-17 16:00:01 -04001099 if (TWFunc::Path_Exists("/sbin/mkdosfs")) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001100 if (!UnMount(true))
1101 return false;
1102
1103 ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str());
1104 Find_Actual_Block_Device();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001105 command = "mkdosfs " + Actual_Block_Device;
1106 if (TWFunc::Exec_Cmd(command, result) == 0) {
Gary Pecke8bc5d72012-12-21 06:45:25 -08001107 Current_File_System = "vfat";
Dees_Troye58d5262012-09-21 12:27:57 -04001108 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001109 ui_print("Done.\n");
1110 return true;
1111 } else {
1112 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1113 return false;
1114 }
1115 return true;
1116 }
1117 else
1118 return Wipe_RMRF();
1119
1120 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001121}
1122
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001123bool TWPartition::Wipe_EXFAT() {
1124 string command, result;
1125
1126 if (TWFunc::Path_Exists("/sbin/mkexfatfs")) {
1127 if (!UnMount(true))
1128 return false;
1129
1130 ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str());
1131 Find_Actual_Block_Device();
1132 command = "mkexfatfs " + Actual_Block_Device;
1133 if (TWFunc::Exec_Cmd(command, result) == 0) {
1134 Recreate_AndSec_Folder();
1135 ui_print("Done.\n");
1136 return true;
1137 } else {
1138 LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str());
1139 return false;
1140 }
1141 return true;
1142 }
1143 return false;
1144}
1145
Dees_Troy38bd7602012-09-14 13:33:53 -04001146bool TWPartition::Wipe_MTD() {
1147 if (!UnMount(true))
1148 return false;
1149
1150 ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str());
1151
1152 mtd_scan_partitions();
1153 const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str());
1154 if (mtd == NULL) {
1155 LOGE("No mtd partition named '%s'", MTD_Name.c_str());
1156 return false;
1157 }
1158
1159 MtdWriteContext* ctx = mtd_write_partition(mtd);
1160 if (ctx == NULL) {
1161 LOGE("Can't write '%s', failed to format.", MTD_Name.c_str());
1162 return false;
1163 }
1164 if (mtd_erase_blocks(ctx, -1) == -1) {
1165 mtd_write_close(ctx);
1166 LOGE("Failed to format '%s'", MTD_Name.c_str());
1167 return false;
1168 }
1169 if (mtd_write_close(ctx) != 0) {
1170 LOGE("Failed to close '%s'", MTD_Name.c_str());
1171 return false;
1172 }
Gary Pecke8bc5d72012-12-21 06:45:25 -08001173 Current_File_System = "yaffs2";
Dees_Troye58d5262012-09-21 12:27:57 -04001174 Recreate_AndSec_Folder();
Dees_Troy38bd7602012-09-14 13:33:53 -04001175 ui_print("Done.\n");
1176 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001177}
1178
1179bool TWPartition::Wipe_RMRF() {
Dees_Troy38bd7602012-09-14 13:33:53 -04001180 if (!Mount(true))
1181 return false;
1182
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001183 ui_print("Removing all files under '%s'\n", Mount_Point.c_str());
1184 TWFunc::removeDir(Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001185 Recreate_AndSec_Folder();
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001186 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001187}
1188
1189bool TWPartition::Wipe_Data_Without_Wiping_Media() {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001190 string dir;
Dees_Troy38bd7602012-09-14 13:33:53 -04001191
1192 // This handles wiping data on devices with "sdcard" in /data/media
1193 if (!Mount(true))
1194 return false;
1195
1196 ui_print("Wiping data without wiping /data/media ...\n");
Dees_Troy38bd7602012-09-14 13:33:53 -04001197
1198 DIR* d;
1199 d = opendir("/data");
Dees_Troy16b74352012-11-14 22:27:31 +00001200 if (d != NULL) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001201 struct dirent* de;
1202 while ((de = readdir(d)) != NULL) {
Dees_Troy16b74352012-11-14 22:27:31 +00001203 if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1204 // The media folder is the "internal sdcard"
1205 // The .layout_version file is responsible for determining whether 4.2 decides up upgrade
1206 // the media folder for multi-user.
1207 if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001208
1209 dir = "/data/";
1210 dir.append(de->d_name);
Dees_Troyce675462013-01-09 19:48:21 +00001211 if (de->d_type == DT_DIR) {
1212 TWFunc::removeDir(dir, false);
1213 } else if (de->d_type == DT_REG || de->d_type == DT_LNK) {
1214 if (!unlink(dir.c_str()))
1215 LOGI("Unable to unlink '%s'\n", dir.c_str());
1216 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001217 }
1218 closedir(d);
Dees_Troy16b74352012-11-14 22:27:31 +00001219 ui_print("Done.\n");
Dees_Troyce675462013-01-09 19:48:21 +00001220#ifdef TW_INCLUDE_CRYPTO_SAMSUNG
1221 if (Mount_Point == "/data" && Mount(false)) {
1222 if (TWFunc::Path_Exists("/tmp/edk_p_sd")) {
1223 Make_Dir("/data/system", true);
1224 TWFunc::copy_file("/tmp/edk_p_sd", "/data/system/edk_p_sd", 0600);
1225 }
1226 }
1227#endif
Dees_Troy16b74352012-11-14 22:27:31 +00001228 return true;
Dees_Troy38bd7602012-09-14 13:33:53 -04001229 }
Dees_Troy16b74352012-11-14 22:27:31 +00001230 ui_print("Dirent failed to open /data, error!\n");
1231 return false;
Dees_Troy51a0e822012-09-05 15:24:24 -04001232}
1233
1234bool TWPartition::Backup_Tar(string backup_folder) {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001235 char back_name[255], split_index[5];
1236 string Full_FileName, Split_FileName, Tar_Args, Command;
1237 int use_compression, index, backup_count;
1238 struct stat st;
Dees_Troy7c2dec82012-09-26 09:49:14 -04001239 unsigned long long total_bsize = 0, file_size;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001240 twrpTar tar;
1241 vector <string> files;
Dees_Troy43d8b002012-09-17 16:00:01 -04001242
1243 if (!Mount(true))
1244 return false;
1245
Dees_Troy2c50e182012-09-26 20:05:28 -04001246 if (Backup_Path == "/and-sec") {
1247 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, "Android Secure", "Backing Up");
1248 ui_print("Backing up %s...\n", "Android Secure");
1249 } else {
1250 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
1251 ui_print("Backing up %s...\n", Display_Name.c_str());
1252 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001253
1254 DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression);
Dees_Troy43d8b002012-09-17 16:00:01 -04001255
1256 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1257 Backup_FileName = back_name;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001258 Full_FileName = backup_folder + "/" + Backup_FileName;
1259 if (Backup_Size > MAX_ARCHIVE_SIZE) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001260 // This backup needs to be split into multiple archives
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001261 ui_print("Breaking backup file into multiple archives...\n");
Dees_Troye58d5262012-09-21 12:27:57 -04001262 sprintf(back_name, "%s", Backup_Path.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001263 backup_count = tar.Split_Archive(back_name, Full_FileName);
1264 if (backup_count == -1) {
1265 LOGE("Error tarring split files!\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001266 return false;
1267 }
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001268 return true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001269 } else {
Dees_Troy4a2a1262012-09-18 09:33:47 -04001270 Full_FileName = backup_folder + "/" + Backup_FileName;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001271 if (use_compression) {
1272 if (tar.createTGZ(Backup_Path, Full_FileName) != 0)
1273 return false;
1274 string gzname = Full_FileName + ".gz";
1275 rename(gzname.c_str(), Full_FileName.c_str());
1276 }
1277 else {
1278 if (tar.create(Backup_Path, Full_FileName) != 0)
1279 return false;
1280 }
Dees_Troy7c2dec82012-09-26 09:49:14 -04001281 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1282 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1283 return false;
1284 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001285 }
1286 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001287}
1288
1289bool TWPartition::Backup_DD(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001290 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001291 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001292 int use_compression;
1293
Dees_Troyb46a6842012-09-25 11:06:46 -04001294 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001295 ui_print("Backing up %s...\n", Display_Name.c_str());
1296
1297 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1298 Backup_FileName = back_name;
1299
1300 Full_FileName = backup_folder + "/" + Backup_FileName;
1301
1302 Command = "dd if=" + Actual_Block_Device + " of='" + Full_FileName + "'";
1303 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001304 TWFunc::Exec_Cmd(Command, result);
Dees_Troyc154ac22012-10-12 15:36:47 -04001305 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1306 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
Dees_Troy7c2dec82012-09-26 09:49:14 -04001307 return false;
1308 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001309 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001310}
1311
1312bool TWPartition::Backup_Dump_Image(string backup_folder) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001313 char back_name[255];
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001314 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001315 int use_compression;
1316
Dees_Troyb46a6842012-09-25 11:06:46 -04001317 TWFunc::GUI_Operation_Text(TW_BACKUP_TEXT, Display_Name, "Backing Up");
Dees_Troy43d8b002012-09-17 16:00:01 -04001318 ui_print("Backing up %s...\n", Display_Name.c_str());
1319
1320 sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str());
1321 Backup_FileName = back_name;
1322
1323 Full_FileName = backup_folder + "/" + Backup_FileName;
1324
1325 Command = "dump_image " + MTD_Name + " '" + Full_FileName + "'";
1326 LOGI("Backup command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001327 TWFunc::Exec_Cmd(Command, result);
Dees_Troy7c2dec82012-09-26 09:49:14 -04001328 if (TWFunc::Get_File_Size(Full_FileName) == 0) {
1329 // Actual size may not match backup size due to bad blocks on MTD devices so just check for 0 bytes
1330 LOGE("Backup file size for '%s' is 0 bytes.\n", Full_FileName.c_str());
1331 return false;
1332 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001333 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001334}
1335
Gary Peck43acadf2012-11-21 21:19:01 -08001336bool TWPartition::Restore_Tar(string restore_folder, string Restore_File_System) {
1337 string Full_FileName, Command;
Dees_Troy4a2a1262012-09-18 09:33:47 -04001338 int index = 0;
1339 char split_index[5];
Dees_Troy43d8b002012-09-17 16:00:01 -04001340
Dees_Troye58d5262012-09-21 12:27:57 -04001341 if (Has_Android_Secure) {
1342 ui_print("Wiping android secure...\n");
1343 if (!Wipe_AndSec())
1344 return false;
Gary Peck43acadf2012-11-21 21:19:01 -08001345 } else {
Dees_Troye58d5262012-09-21 12:27:57 -04001346 ui_print("Wiping %s...\n", Display_Name.c_str());
Gary Peck43acadf2012-11-21 21:19:01 -08001347 if (!Wipe(Restore_File_System))
1348 return false;
Dees_Troye58d5262012-09-21 12:27:57 -04001349 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001350
1351 if (!Mount(true))
1352 return false;
1353
Dees_Troyda8b55a2012-12-12 19:18:30 +00001354 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001355 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy4a2a1262012-09-18 09:33:47 -04001356 Full_FileName = restore_folder + "/" + Backup_FileName;
Dees_Troy43d8b002012-09-17 16:00:01 -04001357 if (!TWFunc::Path_Exists(Full_FileName)) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001358 if (!TWFunc::Path_Exists(Full_FileName)) {
1359 // Backup is multiple archives
1360 LOGI("Backup is multiple archives.\n");
Dees_Troy4a2a1262012-09-18 09:33:47 -04001361 sprintf(split_index, "%03i", index);
1362 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001363 while (TWFunc::Path_Exists(Full_FileName)) {
1364 index++;
1365 ui_print("Restoring archive %i...\n", index);
1366 LOGI("Restoring '%s'...\n", Full_FileName.c_str());
1367 twrpTar tar;
1368 if (tar.extract("/", Full_FileName) != 0)
1369 return false;
1370 sprintf(split_index, "%03i", index);
1371 Full_FileName = restore_folder + "/" + Backup_FileName + split_index;
1372 }
1373 if (index == 0) {
1374 LOGE("Error locating restore file: '%s'\n", Full_FileName.c_str());
1375 return false;
1376 }
Dees_Troy4a2a1262012-09-18 09:33:47 -04001377 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001378 } else {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001379 twrpTar tar;
1380 if (tar.extract(Backup_Path, Full_FileName) != 0)
1381 return false;
Dees_Troy43d8b002012-09-17 16:00:01 -04001382 }
1383 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001384}
1385
1386bool TWPartition::Restore_DD(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001387 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001388
Dees_Troyda8b55a2012-12-12 19:18:30 +00001389 TWFunc::GUI_Operation_Text(TW_RESTORE_TEXT, Display_Name, "Restoring");
Dees_Troy43d8b002012-09-17 16:00:01 -04001390 Full_FileName = restore_folder + "/" + Backup_FileName;
Gary Peck15e623d2012-11-21 21:07:58 -08001391
1392 if (!Find_Partition_Size()) {
1393 LOGE("Unable to find partition size for '%s'\n", Mount_Point.c_str());
1394 return false;
1395 }
1396 unsigned long long backup_size = TWFunc::Get_File_Size(Full_FileName);
1397 if (backup_size > Size) {
1398 LOGE("Size (%iMB) of backup '%s' is larger than target device '%s' (%iMB)\n",
1399 (int)(backup_size / 1048576LLU), Full_FileName.c_str(),
1400 Actual_Block_Device.c_str(), (int)(Size / 1048576LLU));
1401 return false;
1402 }
1403
1404 ui_print("Restoring %s...\n", Display_Name.c_str());
Dees_Troy43d8b002012-09-17 16:00:01 -04001405 Command = "dd bs=4096 if='" + Full_FileName + "' of=" + Actual_Block_Device;
1406 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001407 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001408 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001409}
1410
1411bool TWPartition::Restore_Flash_Image(string restore_folder) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001412 string Full_FileName, Command, result;
Dees_Troy43d8b002012-09-17 16:00:01 -04001413
1414 ui_print("Restoring %s...\n", Display_Name.c_str());
1415 Full_FileName = restore_folder + "/" + Backup_FileName;
1416 // Sometimes flash image doesn't like to flash due to the first 2KB matching, so we erase first to ensure that it flashes
1417 Command = "erase_image " + MTD_Name;
1418 LOGI("Erase command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001419 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001420 Command = "flash_image " + MTD_Name + " '" + Full_FileName + "'";
1421 LOGI("Restore command: '%s'\n", Command.c_str());
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001422 TWFunc::Exec_Cmd(Command, result);
Dees_Troy43d8b002012-09-17 16:00:01 -04001423 return true;
Dees_Troy51a0e822012-09-05 15:24:24 -04001424}
Dees_Troy5bf43922012-09-07 16:07:55 -04001425
1426bool TWPartition::Update_Size(bool Display_Error) {
Dees_Troy0550cfb2012-10-13 11:56:13 -04001427 bool ret = false, Was_Already_Mounted = false;
Dees_Troy51127312012-09-08 13:08:49 -04001428
Dees_Troyab10ee22012-09-21 14:27:30 -04001429 if (!Can_Be_Mounted && !Is_Encrypted)
Dees_Troy5bf43922012-09-07 16:07:55 -04001430 return false;
1431
Dees_Troy0550cfb2012-10-13 11:56:13 -04001432 Was_Already_Mounted = Is_Mounted();
Dees_Troy38bd7602012-09-14 13:33:53 -04001433 if (Removable || Is_Encrypted) {
1434 if (!Mount(false))
1435 return true;
1436 } else if (!Mount(Display_Error))
Dees_Troy5bf43922012-09-07 16:07:55 -04001437 return false;
Dees_Troy51127312012-09-08 13:08:49 -04001438
1439 ret = Get_Size_Via_statfs(Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001440 if (!ret || Size == 0) {
1441 if (!Get_Size_Via_df(Display_Error)) {
1442 if (!Was_Already_Mounted)
1443 UnMount(false);
Dees_Troy51127312012-09-08 13:08:49 -04001444 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001445 }
1446 }
Dees_Troy51127312012-09-08 13:08:49 -04001447
Dees_Troy5bf43922012-09-07 16:07:55 -04001448 if (Has_Data_Media) {
1449 if (Mount(Display_Error)) {
Dees_Troy51127312012-09-08 13:08:49 -04001450 unsigned long long data_media_used, actual_data;
Dees_Troy43d8b002012-09-17 16:00:01 -04001451 Used = TWFunc::Get_Folder_Size("/data", Display_Error);
1452 data_media_used = TWFunc::Get_Folder_Size("/data/media", Display_Error);
Dees_Troy51127312012-09-08 13:08:49 -04001453 actual_data = Used - data_media_used;
Dees_Troy5bf43922012-09-07 16:07:55 -04001454 Backup_Size = actual_data;
Dees_Troy51127312012-09-08 13:08:49 -04001455 int bak = (int)(Backup_Size / 1048576LLU);
1456 int total = (int)(Size / 1048576LLU);
1457 int us = (int)(Used / 1048576LLU);
1458 int fre = (int)(Free / 1048576LLU);
1459 int datmed = (int)(data_media_used / 1048576LLU);
1460 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 -04001461 } else {
1462 if (!Was_Already_Mounted)
1463 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001464 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001465 }
Dees_Troye58d5262012-09-21 12:27:57 -04001466 } else if (Has_Android_Secure) {
1467 if (Mount(Display_Error))
1468 Backup_Size = TWFunc::Get_Folder_Size(Backup_Path, Display_Error);
Dees_Troy0550cfb2012-10-13 11:56:13 -04001469 else {
1470 if (!Was_Already_Mounted)
1471 UnMount(false);
Dees_Troye58d5262012-09-21 12:27:57 -04001472 return false;
Dees_Troy0550cfb2012-10-13 11:56:13 -04001473 }
Dees_Troy5bf43922012-09-07 16:07:55 -04001474 }
Dees_Troy0550cfb2012-10-13 11:56:13 -04001475 if (!Was_Already_Mounted)
1476 UnMount(false);
Dees_Troy5bf43922012-09-07 16:07:55 -04001477 return true;
Dees_Troy51127312012-09-08 13:08:49 -04001478}
Dees_Troy38bd7602012-09-14 13:33:53 -04001479
1480void TWPartition::Find_Actual_Block_Device(void) {
1481 if (Is_Decrypted) {
1482 Actual_Block_Device = Decrypted_Block_Device;
Dees_Troy43d8b002012-09-17 16:00:01 -04001483 if (TWFunc::Path_Exists(Primary_Block_Device))
Dees_Troy38bd7602012-09-14 13:33:53 -04001484 Is_Present = true;
Dees_Troy43d8b002012-09-17 16:00:01 -04001485 } else if (TWFunc::Path_Exists(Primary_Block_Device)) {
Dees_Troy38bd7602012-09-14 13:33:53 -04001486 Is_Present = true;
1487 Actual_Block_Device = Primary_Block_Device;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001488 return;
1489 }
1490 if (Is_Decrypted) {
Dees_Troy43d8b002012-09-17 16:00:01 -04001491 } else if (!Alternate_Block_Device.empty() && TWFunc::Path_Exists(Alternate_Block_Device)) {
Dees_Troy3f04d032012-10-07 18:20:09 -04001492 Actual_Block_Device = Alternate_Block_Device;
Dees_Troy38bd7602012-09-14 13:33:53 -04001493 Is_Present = true;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001494 } else {
Dees_Troy38bd7602012-09-14 13:33:53 -04001495 Is_Present = false;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001496 }
Dees_Troy38bd7602012-09-14 13:33:53 -04001497}
1498
1499void TWPartition::Recreate_Media_Folder(void) {
1500 string Command;
1501
1502 if (!Mount(true)) {
1503 LOGE("Unable to recreate /data/media folder.\n");
Dees_Troyb46a6842012-09-25 11:06:46 -04001504 } else if (!TWFunc::Path_Exists("/data/media")) {
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001505 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001506 LOGI("Recreating /data/media folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001507 mkdir("/data/media", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1508 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troy38bd7602012-09-14 13:33:53 -04001509 }
Dees_Troy43d8b002012-09-17 16:00:01 -04001510}
Dees_Troye58d5262012-09-21 12:27:57 -04001511
1512void TWPartition::Recreate_AndSec_Folder(void) {
Dees_Troye58d5262012-09-21 12:27:57 -04001513 if (!Has_Android_Secure)
1514 return;
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001515 LOGI("Creating .android_secure: %s\n", Symlink_Path.c_str());
Dees_Troye58d5262012-09-21 12:27:57 -04001516 if (!Mount(true)) {
1517 LOGE("Unable to recreate android secure folder.\n");
1518 } else if (!TWFunc::Path_Exists(Symlink_Path)) {
1519 LOGI("Recreating android secure folder.\n");
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001520 PartitionManager.Mount_By_Path(Symlink_Mount_Point, true);
1521 mkdir(Symlink_Path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
1522 PartitionManager.UnMount_By_Path(Symlink_Mount_Point, true);
Dees_Troye58d5262012-09-21 12:27:57 -04001523 }
1524}