Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | /* 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_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 28 | #include <sys/mount.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | #include <unistd.h> |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 30 | #include <dirent.h> |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 31 | |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 32 | #ifdef TW_INCLUDE_CRYPTO |
| 33 | #include "cutils/properties.h" |
| 34 | #endif |
| 35 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | #include "variables.h" |
| 37 | #include "common.h" |
| 38 | #include "partitions.hpp" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 39 | #include "data.hpp" |
| 40 | extern "C" { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 41 | #include "mtdutils/mtdutils.h" |
| 42 | #include "mtdutils/mounts.h" |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 43 | #include "extra-functions.h" |
| 44 | int __system(const char *command); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 45 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 46 | |
| 47 | TWPartition::TWPartition(void) { |
| 48 | Can_Be_Mounted = false; |
| 49 | Can_Be_Wiped = false; |
| 50 | Wipe_During_Factory_Reset = false; |
| 51 | Wipe_Available_in_GUI = false; |
| 52 | Is_SubPartition = false; |
| 53 | SubPartition_Of = ""; |
| 54 | Symlink_Path = ""; |
| 55 | Symlink_Mount_Point = ""; |
| 56 | Mount_Point = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 57 | Actual_Block_Device = ""; |
| 58 | Primary_Block_Device = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 59 | Alternate_Block_Device = ""; |
| 60 | Removable = false; |
| 61 | Is_Present = false; |
| 62 | Length = 0; |
| 63 | Size = 0; |
| 64 | Used = 0; |
| 65 | Free = 0; |
| 66 | Backup_Size = 0; |
| 67 | Can_Be_Encrypted = false; |
| 68 | Is_Encrypted = false; |
| 69 | Is_Decrypted = false; |
| 70 | Decrypted_Block_Device = ""; |
| 71 | Display_Name = ""; |
| 72 | Backup_Name = ""; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 73 | Backup_FileName = ""; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 74 | MTD_Name = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 75 | Backup_Method = NONE; |
| 76 | Has_Data_Media = false; |
| 77 | Is_Storage = false; |
| 78 | Storage_Path = ""; |
| 79 | Current_File_System = ""; |
| 80 | Fstab_File_System = ""; |
| 81 | Format_Block_Size = 0; |
| 82 | } |
| 83 | |
| 84 | TWPartition::~TWPartition(void) { |
| 85 | // Do nothing |
| 86 | } |
| 87 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 88 | bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) { |
| 89 | char full_line[MAX_FSTAB_LINE_LENGTH], item[MAX_FSTAB_LINE_LENGTH]; |
| 90 | int line_len = Line.size(), index = 0, item_index = 0; |
| 91 | char* ptr; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 92 | string Flags; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 93 | |
| 94 | strncpy(full_line, Line.c_str(), line_len); |
| 95 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 96 | for (index = 0; index < line_len; index++) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 97 | if (full_line[index] <= 32) |
| 98 | full_line[index] = '\0'; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 99 | } |
| 100 | string mount_pt(full_line); |
| 101 | Mount_Point = mount_pt; |
| 102 | index = Mount_Point.size(); |
| 103 | while (index < line_len) { |
| 104 | while (index < line_len && full_line[index] == '\0') |
| 105 | index++; |
| 106 | if (index >= line_len) |
| 107 | continue; |
| 108 | ptr = full_line + index; |
| 109 | if (item_index == 0) { |
| 110 | // File System |
| 111 | Fstab_File_System = ptr; |
| 112 | Current_File_System = ptr; |
| 113 | item_index++; |
| 114 | } else if (item_index == 1) { |
| 115 | // Primary Block Device |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 116 | if (Fstab_File_System == "mtd" || Fstab_File_System == "yaffs2") { |
| 117 | Primary_Block_Device = ptr; |
| 118 | Find_MTD_Block_Device(Primary_Block_Device); |
| 119 | } else if (*ptr != '/') { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 120 | if (Display_Error) |
| 121 | LOGE("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index); |
| 122 | else |
| 123 | LOGI("Invalid block device on '%s', '%s', %i\n", Line.c_str(), ptr, index); |
| 124 | return 0; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 125 | } else { |
| 126 | Primary_Block_Device = ptr; |
| 127 | Find_Real_Block_Device(Primary_Block_Device, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 128 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 129 | item_index++; |
| 130 | } else if (item_index > 1) { |
| 131 | if (*ptr == '/') { |
| 132 | // Alternate Block Device |
| 133 | Alternate_Block_Device = ptr; |
| 134 | Find_Real_Block_Device(Alternate_Block_Device, Display_Error); |
| 135 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 136 | // Partition length |
| 137 | ptr += 7; |
| 138 | Length = atoi(ptr); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 139 | } else if (strlen(ptr) > 6 && strncmp(ptr, "flags=", 6) == 0) { |
| 140 | // Custom flags, save for later so that new values aren't overwritten by defaults |
| 141 | ptr += 6; |
| 142 | Flags = ptr; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 143 | } else if (strlen(ptr) == 4 && (strncmp(ptr, "NULL", 4) == 0 || strncmp(ptr, "null", 4) == 0 || strncmp(ptr, "null", 4) == 0)) { |
| 144 | // Do nothing |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 145 | } else { |
| 146 | // Unhandled data |
| 147 | LOGI("Unhandled fstab information: '%s', %i\n", ptr, index); |
| 148 | } |
| 149 | } |
| 150 | while (index < line_len && full_line[index] != '\0') |
| 151 | index++; |
| 152 | } |
| 153 | |
| 154 | if (!Is_File_System(Fstab_File_System) && !Is_Image(Fstab_File_System)) { |
| 155 | if (Display_Error) |
| 156 | LOGE("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
| 157 | else |
| 158 | LOGI("Unknown File System: '%s'\n", Fstab_File_System.c_str()); |
| 159 | return 0; |
| 160 | } else if (Is_File_System(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 161 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 162 | Setup_File_System(Display_Error); |
| 163 | if (Mount_Point == "/system") { |
| 164 | Display_Name = "System"; |
| 165 | Wipe_Available_in_GUI = true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 166 | MTD_Name = "system"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 167 | } else if (Mount_Point == "/data") { |
| 168 | Display_Name = "Data"; |
| 169 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 170 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 171 | MTD_Name = "userdata"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 172 | #ifdef RECOVERY_SDCARD_ON_DATA |
| 173 | Has_Data_Media = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 174 | Is_Storage = true; |
| 175 | Storage_Path = "/data/media"; |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 176 | if (strcmp(EXPAND(TW_EXTERNAL_STORAGE_PATH), "/sdcard") == 0) { |
| 177 | Make_Dir("/emmc", Display_Error); |
| 178 | Symlink_Path = "/data/media"; |
| 179 | Symlink_Mount_Point = "/emmc"; |
| 180 | } else { |
| 181 | Make_Dir("/sdcard", Display_Error); |
| 182 | Symlink_Path = "/data/media"; |
| 183 | Symlink_Mount_Point = "/sdcard"; |
| 184 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 185 | #endif |
| 186 | #ifdef TW_INCLUDE_CRYPTO |
| 187 | Can_Be_Encrypted = true; |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 188 | char crypto_blkdev[255]; |
| 189 | property_get("ro.crypto.fs_crypto_blkdev", crypto_blkdev, "error"); |
| 190 | if (strcmp(crypto_blkdev, "error") != 0) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 191 | DataManager::SetValue(TW_DATA_BLK_DEVICE, Primary_Block_Device); |
Dees_Troy | 657c309 | 2012-09-10 20:32:10 -0400 | [diff] [blame] | 192 | DataManager::SetValue(TW_IS_DECRYPTED, 1); |
| 193 | Is_Encrypted = true; |
| 194 | Is_Decrypted = true; |
| 195 | Decrypted_Block_Device = crypto_blkdev; |
| 196 | LOGI("Data already decrypted, new block device: '%s'\n", crypto_blkdev); |
| 197 | } else if (!Mount(false)) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 198 | Is_Encrypted = true; |
| 199 | Is_Decrypted = false; |
| 200 | DataManager::SetValue(TW_IS_ENCRYPTED, 1); |
| 201 | DataManager::SetValue(TW_CRYPTO_PASSWORD, ""); |
| 202 | DataManager::SetValue("tw_crypto_display", ""); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 203 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 204 | #endif |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 205 | } else if (Mount_Point == "/cache") { |
| 206 | Display_Name = "Cache"; |
| 207 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 208 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 209 | Update_Size(Display_Error); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 210 | MTD_Name = "cache"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 211 | } else if (Mount_Point == "/datadata") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 212 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 213 | Display_Name = "DataData"; |
| 214 | Is_SubPartition = true; |
| 215 | SubPartition_Of = "/data"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 216 | DataManager::SetValue(TW_HAS_DATADATA, 1); |
| 217 | } else if (Mount_Point == "/sd-ext") { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 218 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 219 | Display_Name = "SD-Ext"; |
| 220 | Wipe_Available_in_GUI = true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 221 | } else |
| 222 | Update_Size(Display_Error); |
| 223 | } else if (Is_Image(Fstab_File_System)) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 224 | Find_Actual_Block_Device(); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 225 | Setup_Image(Display_Error); |
| 226 | if (Mount_Point == "/boot") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 227 | MTD_Name = "boot"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 228 | int backup_display_size = (int)(Backup_Size / 1048576LLU); |
| 229 | DataManager::SetValue(TW_BACKUP_BOOT_SIZE, backup_display_size); |
| 230 | if (Backup_Size == 0) { |
| 231 | DataManager::SetValue(TW_HAS_BOOT_PARTITION, 0); |
| 232 | DataManager::SetValue(TW_BACKUP_BOOT_VAR, 0); |
| 233 | } else |
| 234 | DataManager::SetValue(TW_HAS_BOOT_PARTITION, 1); |
| 235 | } else if (Mount_Point == "/recovery") { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 236 | MTD_Name = "recovery"; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 237 | int backup_display_size = (int)(Backup_Size / 1048576LLU); |
| 238 | DataManager::SetValue(TW_BACKUP_RECOVERY_SIZE, backup_display_size); |
| 239 | if (Backup_Size == 0) { |
| 240 | DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 0); |
| 241 | DataManager::SetValue(TW_BACKUP_RECOVERY_VAR, 0); |
| 242 | } else |
| 243 | DataManager::SetValue(TW_HAS_RECOVERY_PARTITION, 1); |
| 244 | } |
| 245 | } |
| 246 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 247 | // Process any custom flags |
| 248 | if (Flags.size() > 0) |
| 249 | Process_Flags(Flags, Display_Error); |
| 250 | |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | bool TWPartition::Process_Flags(string Flags, bool Display_Error) { |
| 255 | char flags[MAX_FSTAB_LINE_LENGTH]; |
| 256 | int flags_len, index = 0; |
| 257 | char* ptr; |
| 258 | |
| 259 | strcpy(flags, Flags.c_str()); |
| 260 | flags_len = Flags.size(); |
| 261 | for (index = 0; index < flags_len; index++) { |
| 262 | if (flags[index] == ';') |
| 263 | flags[index] = '\0'; |
| 264 | } |
| 265 | |
| 266 | index = 0; |
| 267 | while (index < flags_len) { |
| 268 | while (index < flags_len && flags[index] == '\0') |
| 269 | index++; |
| 270 | if (index >= flags_len) |
| 271 | continue; |
| 272 | ptr = flags + index; |
| 273 | if (strcmp(ptr, "removable") == 0) { |
| 274 | Removable = true; |
| 275 | } else if (strcmp(ptr, "storage") == 0) { |
| 276 | Is_Storage = true; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 277 | } else if (strcmp(ptr, "canbewiped") == 0) { |
| 278 | Can_Be_Wiped = true; |
| 279 | } else if (strcmp(ptr, "wipeingui") == 0) { |
| 280 | Can_Be_Wiped = true; |
| 281 | Wipe_Available_in_GUI = true; |
| 282 | } else if (strcmp(ptr, "wipeduringfactoryreset") == 0) { |
| 283 | Can_Be_Wiped = true; |
| 284 | Wipe_Available_in_GUI = true; |
| 285 | Wipe_During_Factory_Reset = true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 286 | } else if (strlen(ptr) > 15 && strncmp(ptr, "subpartitionof=", 15) == 0) { |
| 287 | ptr += 13; |
| 288 | Is_SubPartition = true; |
| 289 | SubPartition_Of = ptr; |
| 290 | } else if (strlen(ptr) > 8 && strncmp(ptr, "symlink=", 8) == 0) { |
| 291 | ptr += 8; |
| 292 | Symlink_Path = ptr; |
| 293 | } else if (strlen(ptr) > 8 && strncmp(ptr, "display=", 8) == 0) { |
| 294 | ptr += 8; |
| 295 | Display_Name = ptr; |
| 296 | } else if (strlen(ptr) > 10 && strncmp(ptr, "blocksize=", 10) == 0) { |
| 297 | ptr += 10; |
| 298 | Format_Block_Size = atoi(ptr); |
| 299 | } else if (strlen(ptr) > 7 && strncmp(ptr, "length=", 7) == 0) { |
| 300 | ptr += 7; |
| 301 | Length = atoi(ptr); |
| 302 | } else { |
| 303 | if (Display_Error) |
| 304 | LOGE("Unhandled flag: '%s'\n", ptr); |
| 305 | else |
| 306 | LOGI("Unhandled flag: '%s'\n", ptr); |
| 307 | } |
| 308 | while (index < flags_len && flags[index] != '\0') |
| 309 | index++; |
| 310 | } |
| 311 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 314 | bool TWPartition::Is_File_System(string File_System) { |
| 315 | if (File_System == "ext2" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 316 | File_System == "ext3" || |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 317 | File_System == "ext4" || |
| 318 | File_System == "vfat" || |
| 319 | File_System == "ntfs" || |
| 320 | File_System == "yaffs2" || |
| 321 | File_System == "auto") |
| 322 | return true; |
| 323 | else |
| 324 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 325 | } |
| 326 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 327 | bool TWPartition::Is_Image(string File_System) { |
| 328 | if (File_System == "emmc" || |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 329 | File_System == "mtd") |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 330 | return true; |
| 331 | else |
| 332 | return false; |
| 333 | } |
| 334 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 335 | bool TWPartition::Make_Dir(string Path, bool Display_Error) { |
| 336 | if (!Path_Exists(Path)) { |
| 337 | if (mkdir(Path.c_str(), 0777) == -1) { |
| 338 | if (Display_Error) |
| 339 | LOGE("Can not create '%s' folder.\n", Path.c_str()); |
| 340 | else |
| 341 | LOGI("Can not create '%s' folder.\n", Path.c_str()); |
| 342 | return false; |
| 343 | } else { |
| 344 | LOGI("Created '%s' folder.\n", Path.c_str()); |
| 345 | return true; |
| 346 | } |
| 347 | } |
| 348 | return true; |
| 349 | } |
| 350 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 351 | void TWPartition::Setup_File_System(bool Display_Error) { |
| 352 | struct statfs st; |
| 353 | |
| 354 | Can_Be_Mounted = true; |
| 355 | Can_Be_Wiped = true; |
| 356 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 357 | // Make the mount point folder if it doesn't exist |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 358 | Make_Dir(Mount_Point, Display_Error); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 359 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 360 | Backup_Name = Display_Name; |
| 361 | Backup_Method = FILES; |
| 362 | } |
| 363 | |
| 364 | void TWPartition::Setup_Image(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 365 | Display_Name = Mount_Point.substr(1, Mount_Point.size() - 1); |
| 366 | Backup_Name = Display_Name; |
| 367 | if (Fstab_File_System == "emmc") |
| 368 | Backup_Method = DD; |
| 369 | else if (Fstab_File_System == "mtd") |
| 370 | Backup_Method = FLASH_UTILS; |
| 371 | else |
| 372 | LOGI("Unhandled file system '%s' on image '%s'\n", Fstab_File_System.c_str(), Display_Name.c_str()); |
| 373 | if (Find_Partition_Size()) { |
| 374 | Used = Size; |
| 375 | Backup_Size = Size; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 376 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 377 | if (Display_Error) |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 378 | LOGE("Unable to find parition size for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 379 | else |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 380 | LOGI("Unable to find parition size for '%s'\n", Mount_Point.c_str()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 384 | void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { |
| 385 | char device[512], realDevice[512]; |
| 386 | |
| 387 | strcpy(device, Block.c_str()); |
| 388 | memset(realDevice, 0, sizeof(realDevice)); |
| 389 | while (readlink(device, realDevice, sizeof(realDevice)) > 0) |
| 390 | { |
| 391 | strcpy(device, realDevice); |
| 392 | memset(realDevice, 0, sizeof(realDevice)); |
| 393 | } |
| 394 | |
| 395 | if (device[0] != '/') { |
| 396 | if (Display_Error) |
| 397 | LOGE("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
| 398 | else |
| 399 | LOGI("Invalid symlink path '%s' found on block device '%s'\n", device, Block.c_str()); |
| 400 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 401 | } else { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 402 | Block = device; |
| 403 | return; |
| 404 | } |
| 405 | } |
| 406 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 407 | bool TWPartition::Find_MTD_Block_Device(string MTD_Name) { |
| 408 | FILE *fp = NULL; |
| 409 | char line[255]; |
| 410 | |
| 411 | fp = fopen("/proc/mtd", "rt"); |
| 412 | if (fp == NULL) { |
| 413 | LOGE("Device does not support /proc/mtd\n"); |
| 414 | return false; |
| 415 | } |
| 416 | |
| 417 | while (fgets(line, sizeof(line), fp) != NULL) |
| 418 | { |
| 419 | char device[32], label[32]; |
| 420 | unsigned long size = 0; |
| 421 | char* fstype = NULL; |
| 422 | int deviceId; |
| 423 | |
| 424 | sscanf(line, "%s %lx %*s %*c%s", device, &size, label); |
| 425 | |
| 426 | // Skip header and blank lines |
| 427 | if ((strcmp(device, "dev:") == 0) || (strlen(line) < 8)) |
| 428 | continue; |
| 429 | |
| 430 | // Strip off the trailing " from the label |
| 431 | label[strlen(label)-1] = '\0'; |
| 432 | |
| 433 | if (strcmp(label, MTD_Name.c_str()) == 0) { |
| 434 | // We found our device |
| 435 | // Strip off the trailing : from the device |
| 436 | device[strlen(device)-1] = '\0'; |
| 437 | if (sscanf(device,"mtd%d", &deviceId) == 1) { |
| 438 | sprintf(device, "/dev/block/mtdblock%d", deviceId); |
| 439 | Primary_Block_Device = device; |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | fclose(fp); |
| 444 | |
| 445 | return false; |
| 446 | } |
| 447 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 448 | bool TWPartition::Get_Size_Via_statfs(bool Display_Error) { |
| 449 | struct statfs st; |
| 450 | string Local_Path = Mount_Point + "/."; |
| 451 | |
| 452 | if (!Mount(Display_Error)) |
| 453 | return false; |
| 454 | |
| 455 | if (statfs(Local_Path.c_str(), &st) != 0) { |
| 456 | if (!Removable) { |
| 457 | if (Display_Error) |
| 458 | LOGE("Unable to statfs '%s'\n", Local_Path.c_str()); |
| 459 | else |
| 460 | LOGI("Unable to statfs '%s'\n", Local_Path.c_str()); |
| 461 | } |
| 462 | return false; |
| 463 | } |
| 464 | Size = (st.f_blocks * st.f_bsize); |
| 465 | Used = ((st.f_blocks - st.f_bfree) * st.f_bsize); |
| 466 | Free = (st.f_bfree * st.f_bsize); |
| 467 | Backup_Size = Used; |
| 468 | return true; |
| 469 | } |
| 470 | |
| 471 | bool TWPartition::Get_Size_Via_df(bool Display_Error) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 472 | FILE* fp; |
| 473 | char command[255], line[512]; |
| 474 | int include_block = 1; |
| 475 | unsigned int min_len; |
| 476 | |
| 477 | if (!Mount(Display_Error)) |
| 478 | return false; |
| 479 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 480 | min_len = Actual_Block_Device.size() + 2; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 481 | sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); |
| 482 | __system(command); |
| 483 | fp = fopen("/tmp/dfoutput.txt", "rt"); |
| 484 | if (fp == NULL) { |
| 485 | LOGI("Unable to open /tmp/dfoutput.txt.\n"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 486 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 487 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 488 | |
| 489 | while (fgets(line, sizeof(line), fp) != NULL) |
| 490 | { |
| 491 | unsigned long blocks, used, available; |
| 492 | char device[64]; |
| 493 | char tmpString[64]; |
| 494 | |
| 495 | if (strncmp(line, "Filesystem", 10) == 0) |
| 496 | continue; |
| 497 | if (strlen(line) < min_len) { |
| 498 | include_block = 0; |
| 499 | continue; |
| 500 | } |
| 501 | if (include_block) { |
| 502 | sscanf(line, "%s %lu %lu %lu", device, &blocks, &used, &available); |
| 503 | } else { |
| 504 | // The device block string is so long that the df information is on the next line |
| 505 | int space_count = 0; |
| 506 | while (tmpString[space_count] == 32) |
| 507 | space_count++; |
| 508 | sscanf(line + space_count, "%lu %lu %lu", &blocks, &used, &available); |
| 509 | } |
| 510 | |
| 511 | // Adjust block size to byte size |
| 512 | Size = blocks * 1024ULL; |
| 513 | Used = used * 1024ULL; |
| 514 | Free = available * 1024ULL; |
| 515 | Backup_Size = Used; |
| 516 | } |
| 517 | fclose(fp); |
| 518 | return true; |
| 519 | } |
| 520 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 521 | unsigned long long TWPartition::Get_Folder_Size(string Path, bool Display_Error) { |
| 522 | DIR* d; |
| 523 | struct dirent* de; |
| 524 | struct stat st; |
| 525 | char path2[1024], filename[1024]; |
| 526 | unsigned long long dusize = 0; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 527 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 528 | // Make a copy of path in case the data in the pointer gets overwritten later |
| 529 | strcpy(path2, Path.c_str()); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 530 | |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 531 | d = opendir(path2); |
| 532 | if (d == NULL) |
| 533 | { |
| 534 | LOGE("error opening '%s'\n", path2); |
| 535 | return 0; |
| 536 | } |
| 537 | |
| 538 | while ((de = readdir(d)) != NULL) |
| 539 | { |
| 540 | if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) |
| 541 | { |
| 542 | strcpy(filename, path2); |
| 543 | strcat(filename, "/"); |
| 544 | strcat(filename, de->d_name); |
| 545 | dusize += Get_Folder_Size(filename, Display_Error); |
| 546 | } |
| 547 | else if (de->d_type == DT_REG) |
| 548 | { |
| 549 | strcpy(filename, path2); |
| 550 | strcat(filename, "/"); |
| 551 | strcat(filename, de->d_name); |
| 552 | stat(filename, &st); |
| 553 | dusize += (unsigned long long)(st.st_size); |
| 554 | } |
| 555 | } |
| 556 | closedir(d); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 557 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 558 | return dusize; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | bool TWPartition::Find_Partition_Size(void) { |
| 562 | FILE* fp; |
| 563 | char line[512]; |
| 564 | string tmpdevice; |
| 565 | |
| 566 | // In this case, we'll first get the partitions we care about (with labels) |
| 567 | fp = fopen("/proc/partitions", "rt"); |
| 568 | if (fp == NULL) |
| 569 | return false; |
| 570 | |
| 571 | while (fgets(line, sizeof(line), fp) != NULL) |
| 572 | { |
| 573 | unsigned long major, minor, blocks; |
| 574 | char device[512]; |
| 575 | char tmpString[64]; |
| 576 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 577 | if (strlen(line) < 7 || line[0] == 'm') continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 578 | sscanf(line + 1, "%lu %lu %lu %s", &major, &minor, &blocks, device); |
| 579 | |
| 580 | tmpdevice = "/dev/block/"; |
| 581 | tmpdevice += device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 582 | if (tmpdevice == Primary_Block_Device || tmpdevice == Alternate_Block_Device) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 583 | // Adjust block size to byte size |
| 584 | Size = blocks * 1024ULL; |
| 585 | fclose(fp); |
| 586 | return true; |
| 587 | } |
| 588 | } |
| 589 | fclose(fp); |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | bool TWPartition::Path_Exists(string Path) { |
| 594 | // Check to see if the Path exists |
| 595 | struct statfs st; |
| 596 | |
| 597 | if (statfs(Path.c_str(), &st) != 0) |
| 598 | return false; |
| 599 | else |
| 600 | return true; |
| 601 | } |
| 602 | |
| 603 | void TWPartition::Flip_Block_Device(void) { |
| 604 | string temp; |
| 605 | |
| 606 | temp = Alternate_Block_Device; |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 607 | Primary_Block_Device = Alternate_Block_Device; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 608 | Alternate_Block_Device = temp; |
| 609 | } |
| 610 | |
| 611 | bool TWPartition::Is_Mounted(void) { |
| 612 | if (!Can_Be_Mounted) |
| 613 | return false; |
| 614 | |
| 615 | struct stat st1, st2; |
| 616 | string test_path; |
| 617 | |
| 618 | // Check to see if the mount point directory exists |
| 619 | test_path = Mount_Point + "/."; |
| 620 | if (stat(test_path.c_str(), &st1) != 0) return false; |
| 621 | |
| 622 | // Check to see if the directory above the mount point exists |
| 623 | test_path = Mount_Point + "/../."; |
| 624 | if (stat(test_path.c_str(), &st2) != 0) return false; |
| 625 | |
| 626 | // Compare the device IDs -- if they match then we're (probably) using tmpfs instead of an actual device |
| 627 | int ret = (st1.st_dev != st2.st_dev) ? true : false; |
| 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | bool TWPartition::Mount(bool Display_Error) { |
| 633 | if (Is_Mounted()) { |
| 634 | return true; |
| 635 | } else if (!Can_Be_Mounted) { |
| 636 | return false; |
| 637 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 638 | |
| 639 | Find_Actual_Block_Device(); |
| 640 | |
| 641 | // Check the current file system before mounting |
| 642 | Check_FS_Type(); |
| 643 | |
| 644 | if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) { |
| 645 | if (Display_Error) |
| 646 | LOGE("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 647 | else |
| 648 | LOGI("Unable to mount '%s'\n", Mount_Point.c_str()); |
| 649 | return false; |
| 650 | } else { |
| 651 | if (Removable) |
| 652 | Update_Size(Display_Error); |
| 653 | |
| 654 | if (!Symlink_Mount_Point.empty()) { |
| 655 | string Command; |
| 656 | |
| 657 | Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point; |
| 658 | __system(Command.c_str()); |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 659 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 660 | return true; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 661 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 662 | return true; |
| 663 | } |
| 664 | |
| 665 | bool TWPartition::UnMount(bool Display_Error) { |
| 666 | if (Is_Mounted()) { |
| 667 | int never_unmount_system; |
| 668 | |
| 669 | DataManager::GetValue(TW_DONT_UNMOUNT_SYSTEM, never_unmount_system); |
| 670 | if (never_unmount_system == 1 && Mount_Point == "/system") |
| 671 | return true; // Never unmount system if you're not supposed to unmount it |
| 672 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 673 | if (!Symlink_Mount_Point.empty()) |
| 674 | umount(Symlink_Mount_Point.c_str()); |
| 675 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 676 | if (umount(Mount_Point.c_str()) != 0) { |
| 677 | if (Display_Error) |
| 678 | LOGE("Unable to unmount '%s'\n", Mount_Point.c_str()); |
| 679 | else |
| 680 | LOGI("Unable to unmount '%s'\n", Mount_Point.c_str()); |
| 681 | return false; |
| 682 | } else |
| 683 | return true; |
| 684 | } else { |
| 685 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
| 689 | bool TWPartition::Wipe() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 690 | if (!Can_Be_Wiped) { |
| 691 | LOGE("Partition '%s' cannot be wiped.\n", Mount_Point.c_str()); |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | if (Has_Data_Media) |
| 696 | return Wipe_Data_Without_Wiping_Media(); |
| 697 | |
| 698 | int check; |
| 699 | DataManager::GetValue(TW_RM_RF_VAR, check); |
| 700 | if (check) |
| 701 | return Wipe_RMRF(); |
| 702 | |
| 703 | if (Current_File_System == "ext4") |
| 704 | return Wipe_EXT4(); |
| 705 | |
| 706 | if (Current_File_System == "ext2" || Current_File_System == "ext3") |
| 707 | return Wipe_EXT23(); |
| 708 | |
| 709 | if (Current_File_System == "vfat") |
| 710 | return Wipe_FAT(); |
| 711 | |
| 712 | if (Current_File_System == "yaffs2") |
| 713 | return Wipe_MTD(); |
| 714 | |
| 715 | LOGE("Unable to wipe '%s' -- unknown file system '%s'\n", Mount_Point.c_str(), Current_File_System.c_str()); |
| 716 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | bool TWPartition::Backup(string backup_folder) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 720 | if (Backup_Method == FILES) |
| 721 | return Backup_Tar(backup_folder); |
| 722 | else if (Backup_Method == DD) |
| 723 | return Backup_DD(backup_folder); |
| 724 | else if (Backup_Method == FLASH_UTILS) |
| 725 | return Backup_Dump_Image(backup_folder); |
| 726 | LOGE("Unknown backup method for '%s'\n", Mount_Point.c_str()); |
| 727 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | bool TWPartition::Restore(string restore_folder) { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 731 | if (Backup_Method == FILES) |
| 732 | return Restore_Tar(restore_folder); |
| 733 | else if (Backup_Method == DD) |
| 734 | return Restore_DD(restore_folder); |
| 735 | else if (Backup_Method == FLASH_UTILS) |
| 736 | return Restore_Flash_Image(restore_folder); |
| 737 | LOGE("Unknown restore method for '%s'\n", Mount_Point.c_str()); |
| 738 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | string TWPartition::Backup_Method_By_Name() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 742 | if (Backup_Method == NONE) |
| 743 | return "none"; |
| 744 | else if (Backup_Method == FILES) |
| 745 | return "files"; |
| 746 | else if (Backup_Method == DD) |
| 747 | return "dd"; |
| 748 | else if (Backup_Method == FLASH_UTILS) |
| 749 | return "flash_utils"; |
| 750 | else |
| 751 | return "undefined"; |
| 752 | return "ERROR!"; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | bool TWPartition::Decrypt(string Password) { |
| 756 | LOGI("STUB TWPartition::Decrypt, password: '%s'\n", Password.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 757 | // Is this needed? |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 758 | return 1; |
| 759 | } |
| 760 | |
| 761 | bool TWPartition::Wipe_Encryption() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 762 | bool Save_Data_Media = Has_Data_Media; |
| 763 | |
| 764 | if (!UnMount(true)) |
| 765 | return false; |
| 766 | |
| 767 | Current_File_System = Fstab_File_System; |
| 768 | Is_Encrypted = false; |
| 769 | Is_Decrypted = false; |
| 770 | Decrypted_Block_Device = ""; |
| 771 | Has_Data_Media = false; |
| 772 | if (Wipe()) { |
| 773 | Has_Data_Media = Save_Data_Media; |
| 774 | if (Has_Data_Media && !Symlink_Mount_Point.empty()) { |
| 775 | Recreate_Media_Folder(); |
| 776 | } |
| 777 | return true; |
| 778 | } else { |
| 779 | Has_Data_Media = Save_Data_Media; |
| 780 | LOGE("Unable to format to remove encryption.\n"); |
| 781 | return false; |
| 782 | } |
| 783 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void TWPartition::Check_FS_Type() { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 787 | FILE *fp; |
| 788 | string blkCommand; |
| 789 | char blkOutput[255]; |
| 790 | char* blk; |
| 791 | char* arg; |
| 792 | char* ptr; |
| 793 | |
| 794 | if (Fstab_File_System == "yaffs2" || Fstab_File_System == "mtd") |
| 795 | return; // Running blkid on some mtd devices causes a massive crash |
| 796 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 797 | Find_Actual_Block_Device(); |
| 798 | blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt"; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 799 | |
| 800 | __system(blkCommand.c_str()); |
| 801 | fp = fopen("/tmp/blkidoutput.txt", "rt"); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 802 | while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL) |
| 803 | { |
| 804 | blk = blkOutput; |
| 805 | ptr = blkOutput; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 806 | while (*ptr > 32 && *ptr != ':') ptr++; |
| 807 | if (*ptr == 0) continue; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 808 | *ptr = 0; |
| 809 | |
| 810 | // Increment by two, but verify that we don't hit a NULL |
| 811 | ptr++; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 812 | if (*ptr != 0) ptr++; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 813 | |
| 814 | // Now, find the TYPE field |
| 815 | while (1) |
| 816 | { |
| 817 | arg = ptr; |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 818 | while (*ptr > 32) ptr++; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 819 | if (*ptr != 0) |
| 820 | { |
| 821 | *ptr = 0; |
| 822 | ptr++; |
| 823 | } |
| 824 | |
| 825 | if (strlen(arg) > 6) |
| 826 | { |
| 827 | if (memcmp(arg, "TYPE=\"", 6) == 0) break; |
| 828 | } |
| 829 | |
| 830 | if (*ptr == 0) |
| 831 | { |
| 832 | arg = NULL; |
| 833 | break; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | if (arg && strlen(arg) > 7) |
| 838 | { |
| 839 | arg += 6; // Skip the TYPE=" portion |
| 840 | arg[strlen(arg)-1] = '\0'; // Drop the tail quote |
| 841 | } |
| 842 | else |
| 843 | continue; |
| 844 | |
Dees_Troy | 63c8df7 | 2012-09-10 14:02:05 -0400 | [diff] [blame] | 845 | if (strcmp(Current_File_System.c_str(), arg) != 0) { |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 846 | LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg); |
| 847 | Current_File_System = arg; |
| 848 | } |
| 849 | } |
| 850 | __pclose(fp); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 851 | return; |
| 852 | } |
| 853 | |
| 854 | bool TWPartition::Wipe_EXT23() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 855 | if (!UnMount(true)) |
| 856 | return false; |
| 857 | |
| 858 | if (Path_Exists("/sbin/mke2fs")) { |
| 859 | char command[512]; |
| 860 | |
| 861 | ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str()); |
| 862 | Find_Actual_Block_Device(); |
| 863 | sprintf(command, "mke2fs -t %s -m 0 %s", Current_File_System.c_str(), Actual_Block_Device.c_str()); |
| 864 | LOGI("mke2fs command: %s\n", command); |
| 865 | if (__system(command) == 0) { |
| 866 | ui_print("Done.\n"); |
| 867 | return true; |
| 868 | } else { |
| 869 | LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
| 870 | return false; |
| 871 | } |
| 872 | } else |
| 873 | return Wipe_RMRF(); |
| 874 | |
| 875 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | bool TWPartition::Wipe_EXT4() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 879 | if (!UnMount(true)) |
| 880 | return false; |
| 881 | |
| 882 | if (Path_Exists("/sbin/make_ext4fs")) { |
| 883 | string Command; |
| 884 | |
| 885 | ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str()); |
| 886 | Find_Actual_Block_Device(); |
| 887 | Command = "make_ext4fs"; |
| 888 | if (!Is_Decrypted && Length != 0) { |
| 889 | // Only use length if we're not decrypted |
| 890 | char len[32]; |
| 891 | sprintf(len, "%i", Length); |
| 892 | Command += " -l "; |
| 893 | Command += len; |
| 894 | } |
| 895 | Command += " " + Actual_Block_Device; |
| 896 | LOGI("make_ext4fs command: %s\n", Command.c_str()); |
| 897 | if (__system(Command.c_str()) == 0) { |
| 898 | ui_print("Done.\n"); |
| 899 | return true; |
| 900 | } else { |
| 901 | LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
| 902 | return false; |
| 903 | } |
| 904 | } else |
| 905 | return Wipe_EXT23(); |
| 906 | |
| 907 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | bool TWPartition::Wipe_FAT() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 911 | char command[512]; |
| 912 | |
| 913 | if (Backup_Name == "and-sec") // Don't format if it's android secure |
| 914 | return Wipe_RMRF(); |
| 915 | |
| 916 | if (Path_Exists("/sbin/mkdosfs")) { |
| 917 | if (!UnMount(true)) |
| 918 | return false; |
| 919 | |
| 920 | ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str()); |
| 921 | Find_Actual_Block_Device(); |
| 922 | sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it |
| 923 | if (__system(command) == 0) { |
| 924 | ui_print("Done.\n"); |
| 925 | return true; |
| 926 | } else { |
| 927 | LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str()); |
| 928 | return false; |
| 929 | } |
| 930 | return true; |
| 931 | } |
| 932 | else |
| 933 | return Wipe_RMRF(); |
| 934 | |
| 935 | return false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 936 | } |
| 937 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 938 | bool TWPartition::Wipe_MTD() { |
| 939 | if (!UnMount(true)) |
| 940 | return false; |
| 941 | |
| 942 | ui_print("MTD Formatting \"%s\"\n", MTD_Name.c_str()); |
| 943 | |
| 944 | mtd_scan_partitions(); |
| 945 | const MtdPartition* mtd = mtd_find_partition_by_name(MTD_Name.c_str()); |
| 946 | if (mtd == NULL) { |
| 947 | LOGE("No mtd partition named '%s'", MTD_Name.c_str()); |
| 948 | return false; |
| 949 | } |
| 950 | |
| 951 | MtdWriteContext* ctx = mtd_write_partition(mtd); |
| 952 | if (ctx == NULL) { |
| 953 | LOGE("Can't write '%s', failed to format.", MTD_Name.c_str()); |
| 954 | return false; |
| 955 | } |
| 956 | if (mtd_erase_blocks(ctx, -1) == -1) { |
| 957 | mtd_write_close(ctx); |
| 958 | LOGE("Failed to format '%s'", MTD_Name.c_str()); |
| 959 | return false; |
| 960 | } |
| 961 | if (mtd_write_close(ctx) != 0) { |
| 962 | LOGE("Failed to close '%s'", MTD_Name.c_str()); |
| 963 | return false; |
| 964 | } |
| 965 | ui_print("Done.\n"); |
| 966 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | bool TWPartition::Wipe_RMRF() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 970 | char cmd[512]; |
| 971 | |
| 972 | if (!Mount(true)) |
| 973 | return false; |
| 974 | |
| 975 | if (Backup_Name == "and-sec") { |
| 976 | ui_print("Using rm -rf on .android_secure\n"); |
| 977 | sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str()); |
| 978 | } else { |
| 979 | ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str()); |
| 980 | sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str()); |
| 981 | } |
| 982 | |
| 983 | LOGI("rm -rf command is: '%s'\n", cmd); |
| 984 | __system(cmd); |
| 985 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | bool TWPartition::Wipe_Data_Without_Wiping_Media() { |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 989 | char cmd[256]; |
| 990 | |
| 991 | // This handles wiping data on devices with "sdcard" in /data/media |
| 992 | if (!Mount(true)) |
| 993 | return false; |
| 994 | |
| 995 | ui_print("Wiping data without wiping /data/media ...\n"); |
| 996 | __system("rm -f /data/*"); |
| 997 | __system("rm -f /data/.*"); |
| 998 | |
| 999 | DIR* d; |
| 1000 | d = opendir("/data"); |
| 1001 | if (d != NULL) |
| 1002 | { |
| 1003 | struct dirent* de; |
| 1004 | while ((de = readdir(d)) != NULL) { |
| 1005 | if (strcmp(de->d_name, "media") == 0) continue; |
| 1006 | |
| 1007 | sprintf(cmd, "rm -fr /data/%s", de->d_name); |
| 1008 | __system(cmd); |
| 1009 | } |
| 1010 | closedir(d); |
| 1011 | } |
| 1012 | ui_print("Done.\n"); |
| 1013 | return true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | bool TWPartition::Backup_Tar(string backup_folder) { |
| 1017 | LOGI("STUB TWPartition::Backup_Tar, backup_folder: '%s'\n", backup_folder.c_str()); |
| 1018 | return 1; |
| 1019 | } |
| 1020 | |
| 1021 | bool TWPartition::Backup_DD(string backup_folder) { |
| 1022 | LOGI("STUB TWPartition::Backup_DD, backup_folder: '%s'\n", backup_folder.c_str()); |
| 1023 | return 1; |
| 1024 | } |
| 1025 | |
| 1026 | bool TWPartition::Backup_Dump_Image(string backup_folder) { |
| 1027 | LOGI("STUB TWPartition::Backup_Dump_Image, backup_folder: '%s'\n", backup_folder.c_str()); |
| 1028 | return 1; |
| 1029 | } |
| 1030 | |
| 1031 | bool TWPartition::Restore_Tar(string restore_folder) { |
| 1032 | LOGI("STUB TWPartition::Restore_Tar, backup_folder: '%s'\n", restore_folder.c_str()); |
| 1033 | return 1; |
| 1034 | } |
| 1035 | |
| 1036 | bool TWPartition::Restore_DD(string restore_folder) { |
| 1037 | LOGI("STUB TWPartition::Restore_DD, backup_folder: '%s'\n", restore_folder.c_str()); |
| 1038 | return 1; |
| 1039 | } |
| 1040 | |
| 1041 | bool TWPartition::Restore_Flash_Image(string restore_folder) { |
| 1042 | LOGI("STUB TWPartition::Restore_Flash_Image, backup_folder: '%s'\n", restore_folder.c_str()); |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 1043 | // might erase image first just to ensure that it flashes |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1044 | return 1; |
| 1045 | } |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1046 | |
| 1047 | bool TWPartition::Update_Size(bool Display_Error) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1048 | bool ret = false; |
| 1049 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1050 | if (!Can_Be_Mounted) |
| 1051 | return false; |
| 1052 | |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 1053 | if (Removable || Is_Encrypted) { |
| 1054 | if (!Mount(false)) |
| 1055 | return true; |
| 1056 | } else if (!Mount(Display_Error)) |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1057 | return false; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1058 | |
| 1059 | ret = Get_Size_Via_statfs(Display_Error); |
| 1060 | if (!ret || Size == 0) |
| 1061 | if (!Get_Size_Via_df(Display_Error)) |
| 1062 | return false; |
| 1063 | |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1064 | if (Has_Data_Media) { |
| 1065 | if (Mount(Display_Error)) { |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1066 | unsigned long long data_media_used, actual_data; |
| 1067 | data_media_used = Get_Folder_Size("/data/media", Display_Error); |
| 1068 | actual_data = Used - data_media_used; |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1069 | Backup_Size = actual_data; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1070 | int bak = (int)(Backup_Size / 1048576LLU); |
| 1071 | int total = (int)(Size / 1048576LLU); |
| 1072 | int us = (int)(Used / 1048576LLU); |
| 1073 | int fre = (int)(Free / 1048576LLU); |
| 1074 | int datmed = (int)(data_media_used / 1048576LLU); |
| 1075 | LOGI("Data backup size is %iMB, size: %iMB, used: %iMB, free: %iMB, in data/media: %iMB.\n", bak, total, us, fre, datmed); |
Dees_Troy | 5bf4392 | 2012-09-07 16:07:55 -0400 | [diff] [blame] | 1076 | } else |
| 1077 | return false; |
| 1078 | } |
| 1079 | return true; |
Dees_Troy | 5112731 | 2012-09-08 13:08:49 -0400 | [diff] [blame] | 1080 | } |
Dees_Troy | 38bd760 | 2012-09-14 13:33:53 -0400 | [diff] [blame^] | 1081 | |
| 1082 | void TWPartition::Find_Actual_Block_Device(void) { |
| 1083 | if (Is_Decrypted) { |
| 1084 | Actual_Block_Device = Decrypted_Block_Device; |
| 1085 | if (Path_Exists(Primary_Block_Device)) |
| 1086 | Is_Present = true; |
| 1087 | } else if (Path_Exists(Primary_Block_Device)) { |
| 1088 | Is_Present = true; |
| 1089 | Actual_Block_Device = Primary_Block_Device; |
| 1090 | } else if (!Alternate_Block_Device.empty() && Path_Exists(Alternate_Block_Device)) { |
| 1091 | Flip_Block_Device(); |
| 1092 | Actual_Block_Device = Primary_Block_Device; |
| 1093 | Is_Present = true; |
| 1094 | } else |
| 1095 | Is_Present = false; |
| 1096 | } |
| 1097 | |
| 1098 | void TWPartition::Recreate_Media_Folder(void) { |
| 1099 | string Command; |
| 1100 | |
| 1101 | if (!Mount(true)) { |
| 1102 | LOGE("Unable to recreate /data/media folder.\n"); |
| 1103 | } else { |
| 1104 | LOGI("Recreating /data/media folder.\n"); |
| 1105 | __system("cd /data && mkdir media && chmod 755 media"); |
| 1106 | Command = "umount " + Symlink_Mount_Point; |
| 1107 | __system(Command.c_str()); |
| 1108 | Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point; |
| 1109 | __system(Command.c_str()); |
| 1110 | } |
| 1111 | } |