forked from CyanogenMod/android_bootable_recovery
-
Notifications
You must be signed in to change notification settings - Fork 57
/
extendedcommands.c
1914 lines (1675 loc) · 61.9 KB
/
extendedcommands.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2014 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <libgen.h>
#include <limits.h>
#include <linux/input.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/limits.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "adb_install.h"
#include "bmlutils/bmlutils.h"
#include "bootloader.h"
#include "common.h"
#include "cutils/android_reboot.h"
#include "cutils/properties.h"
#include "edify/expr.h"
#include "extendedcommands.h"
#include "firmware.h"
#include "flashutils/flashutils.h"
#include "install.h"
#include "make_ext4fs.h"
#include "minui/minui.h"
#include "minzip/DirUtil.h"
#include "mmcutils/mmcutils.h"
#include "mounts.h"
#include "mtdutils/mtdutils.h"
#include "nandroid.h"
#include "recovery_settings.h"
#include "recovery_ui.h"
#include "roots.h"
#include "voldclient/voldclient.h"
// top fixed menu items, those before extra storage volumes
#define FIXED_TOP_INSTALL_ZIP_MENUS 1
// bottom fixed menu items, those after extra storage volumes
#define FIXED_BOTTOM_INSTALL_ZIP_MENUS 3
#define FIXED_INSTALL_ZIP_MENUS (FIXED_TOP_INSTALL_ZIP_MENUS + FIXED_BOTTOM_INSTALL_ZIP_MENUS)
// number of actions added for each volume by add_nandroid_options_for_volume()
// these go on top of menu list
#define NANDROID_ACTIONS_NUM 4
// number of fixed bottom entries after volume actions
#define NANDROID_FIXED_ENTRIES 2
#if defined(ENABLE_LOKI) && defined(BOARD_NATIVE_DUALBOOT_SINGLEDATA)
#define FIXED_ADVANCED_ENTRIES 10
#elif !defined(ENABLE_LOKI) && defined(BOARD_NATIVE_DUALBOOT_SINGLEDATA)
#define FIXED_ADVANCED_ENTRIES 9
#elif defined(ENABLE_LOKI) && !defined(BOARD_NATIVE_DUALBOOT_SINGLEDATA)
#define FIXED_ADVANCED_ENTRIES 8
#else
#define FIXED_ADVANCED_ENTRIES 7
#endif
extern struct selabel_handle *sehandle;
int signature_check_enabled = 0;
typedef struct {
char mount[255];
char unmount[255];
char path[PATH_MAX];
} MountMenuEntry;
typedef struct {
char txt[255];
char path[PATH_MAX];
char type[255];
} FormatMenuEntry;
typedef struct {
char *name;
int can_mount;
int can_format;
} MFMatrix;
// Prototypes of private functions that are used before defined
static void show_choose_zip_menu(const char *mount_point);
static void format_sdcard(const char* volume);
static int can_partition(const char* volume);
static int is_path_mounted(const char* path);
static int get_filtered_menu_selection(const char** headers, char** items, int menu_only, int initial_selection, int items_count) {
int index;
int offset = 0;
int* translate_table = (int*)malloc(sizeof(int) * items_count);
char* items_new[items_count];
for (index = 0; index < items_count; index++) {
items_new[index] = items[index];
}
for (index = 0; index < items_count; index++) {
if (items_new[index] == NULL)
continue;
char *item = items_new[index];
items_new[index] = NULL;
items_new[offset] = item;
translate_table[offset] = index;
offset++;
}
items_new[offset] = NULL;
initial_selection = translate_table[initial_selection];
int ret = get_menu_selection(headers, items_new, menu_only, initial_selection);
if (ret < 0 || ret >= offset) {
free(translate_table);
return ret;
}
ret = translate_table[ret];
free(translate_table);
return ret;
}
static void write_string_to_file(const char* filename, const char* string) {
ensure_path_mounted(filename);
char tmp[PATH_MAX];
sprintf(tmp, "mkdir -p $(dirname %s)", filename);
__system(tmp);
FILE *file = fopen(filename, "w");
if (file != NULL) {
fprintf(file, "%s", string);
fclose(file);
}
}
void write_recovery_version() {
char path[PATH_MAX];
sprintf(path, "%s/%s", get_primary_storage_path(), RECOVERY_VERSION_FILE);
write_string_to_file(path, EXPAND(RECOVERY_VERSION) "\n" EXPAND(TARGET_DEVICE));
// force unmount /data for /data/media devices as we call this on recovery exit
preserve_data_media(0);
ensure_path_unmounted(path);
preserve_data_media(1);
}
static void write_last_install_path(const char* install_path) {
char path[PATH_MAX];
//sprintf(path, "%s%s%s", get_primary_storage_path(), (is_data_media() ? "/0/" : "/"), RECOVERY_LAST_INSTALL_FILE);
sprintf(path, "%s/%s", get_primary_storage_path(), RECOVERY_LAST_INSTALL_FILE);
write_string_to_file(path, install_path);
}
static char* read_last_install_path() {
static char path[PATH_MAX];
sprintf(path, "%s/%s", get_primary_storage_path(), RECOVERY_LAST_INSTALL_FILE);
ensure_path_mounted(path);
FILE *f = fopen(path, "r");
if (f != NULL) {
fgets(path, PATH_MAX, f);
fclose(f);
return path;
}
return NULL;
}
static void toggle_signature_check() {
signature_check_enabled = !signature_check_enabled;
ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
}
#ifdef ENABLE_LOKI
int loki_support_enabled = 1;
void toggle_loki_support() {
loki_support_enabled = !loki_support_enabled;
ui_print("Loki Support: %s\n", loki_support_enabled ? "Enabled" : "Disabled");
}
#endif
int install_zip(const char* packagefilepath) {
ui_print("\n-- Installing: %s\n", packagefilepath);
if (device_flash_type() == MTD) {
set_sdcard_update_bootloader_message();
}
int status = install_package(packagefilepath);
ui_reset_progress();
if (status != INSTALL_SUCCESS) {
ui_set_background(BACKGROUND_ICON_ERROR);
ui_print("Installation aborted.\n");
return 1;
}
#ifdef ENABLE_LOKI
if (loki_support_enabled) {
ui_print("Checking if loki-fying is needed\n");
status = loki_check();
if (status != INSTALL_SUCCESS) {
ui_set_background(BACKGROUND_ICON_ERROR);
return 1;
}
}
#endif
ui_set_background(BACKGROUND_ICON_CLOCKWORK);
ui_print("\nInstall from sdcard complete.\n");
return 0;
}
int show_install_update_menu() {
char buf[100];
int i = 0, chosen_item = 0;
static char* install_menu_items[MAX_NUM_MANAGED_VOLUMES + FIXED_INSTALL_ZIP_MENUS + 1];
char* primary_path = get_primary_storage_path();
char** extra_paths = get_extra_storage_paths();
int num_extra_volumes = get_num_extra_volumes();
memset(install_menu_items, 0, MAX_NUM_MANAGED_VOLUMES + FIXED_INSTALL_ZIP_MENUS + 1);
static const char* headers[] = { "Install update from zip file", "", NULL };
// FIXED_TOP_INSTALL_ZIP_MENUS
sprintf(buf, "choose zip from %s", primary_path);
install_menu_items[0] = strdup(buf);
// extra storage volumes (vold managed)
for (i = 0; i < num_extra_volumes; i++) {
sprintf(buf, "choose zip from %s", extra_paths[i]);
install_menu_items[FIXED_TOP_INSTALL_ZIP_MENUS + i] = strdup(buf);
}
// FIXED_BOTTOM_INSTALL_ZIP_MENUS
install_menu_items[FIXED_TOP_INSTALL_ZIP_MENUS + num_extra_volumes] = "choose zip from last install folder";
install_menu_items[FIXED_TOP_INSTALL_ZIP_MENUS + num_extra_volumes + 1] = "install zip from sideload";
install_menu_items[FIXED_TOP_INSTALL_ZIP_MENUS + num_extra_volumes + 2] = "toggle signature verification";
// extra NULL for GO_BACK
install_menu_items[FIXED_TOP_INSTALL_ZIP_MENUS + num_extra_volumes + 3] = NULL;
for (;;) {
chosen_item = get_menu_selection(headers, install_menu_items, 0, 0);
if (chosen_item == 0) {
show_choose_zip_menu(primary_path);
} else if (chosen_item >= FIXED_TOP_INSTALL_ZIP_MENUS && chosen_item < FIXED_TOP_INSTALL_ZIP_MENUS + num_extra_volumes) {
show_choose_zip_menu(extra_paths[chosen_item - FIXED_TOP_INSTALL_ZIP_MENUS]);
} else if (chosen_item == FIXED_TOP_INSTALL_ZIP_MENUS + num_extra_volumes) {
char *last_path_used = read_last_install_path();
if (last_path_used == NULL)
show_choose_zip_menu(primary_path);
else
show_choose_zip_menu(last_path_used);
} else if (chosen_item == FIXED_TOP_INSTALL_ZIP_MENUS + num_extra_volumes + 1) {
apply_from_adb();
} else if (chosen_item == FIXED_TOP_INSTALL_ZIP_MENUS + num_extra_volumes + 2) {
toggle_signature_check();
} else {
// GO_BACK or REFRESH (chosen_item < 0)
goto out;
}
}
out:
free(install_menu_items[0]);
if (extra_paths != NULL) {
for (i = 0; i < num_extra_volumes; i++)
free(install_menu_items[FIXED_TOP_INSTALL_ZIP_MENUS + i]);
}
return chosen_item;
}
static void free_string_array(char** array) {
if (array == NULL)
return;
char* cursor = array[0];
int i = 0;
while (cursor != NULL) {
free(cursor);
cursor = array[++i];
}
free(array);
}
static char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles) {
char path[PATH_MAX] = "";
DIR *dir;
struct dirent *de;
int total = 0;
int i;
char** files = NULL;
int pass;
*numFiles = 0;
int dirLen = strlen(directory);
dir = opendir(directory);
if (dir == NULL) {
ui_print("Couldn't open directory.\n");
return NULL;
}
unsigned int extension_length = 0;
if (fileExtensionOrDirectory != NULL)
extension_length = strlen(fileExtensionOrDirectory);
int isCounting = 1;
i = 0;
for (pass = 0; pass < 2; pass++) {
while ((de = readdir(dir)) != NULL) {
// skip hidden files
if (de->d_name[0] == '.')
continue;
// NULL means that we are gathering directories, so skip this
if (fileExtensionOrDirectory != NULL) {
// make sure that we can have the desired extension (prevent seg fault)
if (strlen(de->d_name) < extension_length)
continue;
// compare the extension
if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
continue;
} else {
struct stat info;
char fullFileName[PATH_MAX];
strcpy(fullFileName, directory);
strcat(fullFileName, de->d_name);
lstat(fullFileName, &info);
// make sure it is a directory
if (!(S_ISDIR(info.st_mode)))
continue;
}
if (pass == 0) {
total++;
continue;
}
files[i] = (char*)malloc(dirLen + strlen(de->d_name) + 2);
strcpy(files[i], directory);
strcat(files[i], de->d_name);
if (fileExtensionOrDirectory == NULL)
strcat(files[i], "/");
i++;
}
if (pass == 1)
break;
if (total == 0)
break;
rewinddir(dir);
*numFiles = total;
files = (char**)malloc((total + 1) * sizeof(char*));
files[total] = NULL;
}
if (closedir(dir) < 0) {
LOGE("Failed to close directory.\n");
}
if (total == 0) {
return NULL;
}
// sort the result
if (files != NULL) {
for (i = 0; i < total; i++) {
int curMax = -1;
int j;
for (j = 0; j < total - i; j++) {
if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
curMax = j;
}
char* temp = files[curMax];
files[curMax] = files[total - i - 1];
files[total - i - 1] = temp;
}
}
return files;
}
// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
static char* choose_file_menu(const char* basedir, const char* fileExtensionOrDirectory, const char* headers[]) {
const char* fixed_headers[20];
int numFiles = 0;
int numDirs = 0;
int i;
char* return_value = NULL;
char directory[PATH_MAX];
int dir_len = strlen(basedir);
strcpy(directory, basedir);
// Append a trailing slash if necessary
if (directory[dir_len - 1] != '/') {
strcat(directory, "/");
dir_len++;
}
i = 0;
while (headers[i]) {
fixed_headers[i] = headers[i];
i++;
}
fixed_headers[i] = directory;
fixed_headers[i + 1] = "";
fixed_headers[i + 2] = NULL;
char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
char** dirs = NULL;
if (fileExtensionOrDirectory != NULL)
dirs = gather_files(directory, NULL, &numDirs);
int total = numDirs + numFiles;
if (total == 0) {
ui_print("No files found.\n");
} else {
char** list = (char**)malloc((total + 1) * sizeof(char*));
list[total] = NULL;
for (i = 0; i < numDirs; i++) {
list[i] = strdup(dirs[i] + dir_len);
}
for (i = 0; i < numFiles; i++) {
list[numDirs + i] = strdup(files[i] + dir_len);
}
for (;;) {
int chosen_item = get_menu_selection(fixed_headers, list, 0, 0);
if (chosen_item == GO_BACK || chosen_item == REFRESH)
break;
if (chosen_item < numDirs) {
char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
if (subret != NULL) {
return_value = strdup(subret);
free(subret);
break;
}
continue;
}
return_value = strdup(files[chosen_item - numDirs]);
break;
}
free_string_array(list);
}
free_string_array(files);
free_string_array(dirs);
return return_value;
}
static void show_choose_zip_menu(const char *mount_point) {
if (ensure_path_mounted(mount_point) != 0) {
LOGE("Can't mount %s\n", mount_point);
return;
}
static const char* headers[] = { "Choose a zip to apply", "", NULL };
char* file = choose_file_menu(mount_point, ".zip", headers);
if (file == NULL)
return;
char confirm[PATH_MAX];
sprintf(confirm, "Yes - Install %s", basename(file));
if (confirm_selection("Confirm install?", confirm)) {
install_zip(file);
write_last_install_path(dirname(file));
}
free(file);
}
static void show_nandroid_restore_menu(const char* path) {
if (ensure_path_mounted(path) != 0) {
LOGE("Can't mount %s\n", path);
return;
}
static const char* headers[] = { "Choose an image to restore", "", NULL };
char tmp[PATH_MAX];
sprintf(tmp, "%s/clockworkmod/backup/", path);
char* file = choose_file_menu(tmp, NULL, headers);
if (file == NULL)
return;
if (confirm_selection("Confirm restore?", "Yes - Restore")) {
unsigned char flags = NANDROID_BOOT | NANDROID_SYSTEM | NANDROID_DATA
| NANDROID_CACHE | NANDROID_SDEXT;
nandroid_restore(file, flags);
}
free(file);
}
static void show_nandroid_delete_menu(const char* path) {
if (ensure_path_mounted(path) != 0) {
LOGE("Can't mount %s\n", path);
return;
}
static const char* headers[] = { "Choose an image to delete", "", NULL };
char tmp[PATH_MAX];
sprintf(tmp, "%s/clockworkmod/backup/", path);
char* file = choose_file_menu(tmp, NULL, headers);
if (file == NULL)
return;
if (confirm_selection("Confirm delete?", "Yes - Delete")) {
sprintf(tmp, "rm -rf %s", file);
__system(tmp);
}
free(file);
}
static int control_usb_storage(bool on) {
int i = 0;
int num = 0;
for (i = 0; i < get_num_volumes(); i++) {
Volume *v = get_device_volumes() + i;
if (fs_mgr_is_voldmanaged(v) && vold_is_volume_available(v->mount_point)) {
if (on) {
vold_share_volume(v->mount_point);
} else {
vold_unshare_volume(v->mount_point, 1);
}
property_set("sys.storage.ums_enabled", on ? "1" : "0");
num++;
}
}
return num;
}
static void show_mount_usb_storage_menu() {
// Enable USB storage using vold
if (!control_usb_storage(true))
return;
static const char* headers[] = { "USB Mass Storage device",
"Leaving this menu unmounts",
"your SD card from your PC.",
"",
NULL
};
static char* list[] = { "Unmount", NULL };
for (;;) {
int chosen_item = get_menu_selection(headers, list, 0, 0);
if (chosen_item == GO_BACK || chosen_item == 0)
break;
}
// Disable USB storage
control_usb_storage(false);
}
int confirm_selection(const char* title, const char* confirm) {
struct stat info;
int ret = 0;
char path[PATH_MAX];
sprintf(path, "%s/%s", get_primary_storage_path(), RECOVERY_NO_CONFIRM_FILE);
ensure_path_mounted(path);
if (0 == stat(path, &info))
return 1;
#ifdef BOARD_NATIVE_DUALBOOT
char buf[PATH_MAX];
device_build_selection_title(buf, title);
title = (char*)&buf;
#endif
int many_confirm;
char* confirm_str = strdup(confirm);
const char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
int old_val = ui_is_showing_back_button();
ui_set_showing_back_button(0);
sprintf(path, "%s/%s", get_primary_storage_path(), RECOVERY_MANY_CONFIRM_FILE);
ensure_path_mounted(path);
many_confirm = 0 == stat(path, &info);
if (many_confirm) {
char* items[] = { "No",
"No",
"No",
"No",
"No",
"No",
"No",
confirm_str, // Yes, [7]
"No",
"No",
"No",
NULL };
int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
ret = (chosen_item == 7);
} else {
char* items[] = { "No",
confirm_str, // Yes, [1]
NULL };
int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
ret = (chosen_item == 1);
}
free(confirm_str);
ui_set_showing_back_button(old_val);
return ret;
}
int format_device(const char *device, const char *path, const char *fs_type) {
#ifdef BOARD_NATIVE_DUALBOOT_SINGLEDATA
if(device_truedualboot_format_device(device, path, fs_type) <= 0)
return 0;
#endif
if (is_data_media_volume_path(path)) {
return format_unknown_device(NULL, path, NULL);
}
if (strstr(path, "/data") == path && is_data_media()) {
return format_unknown_device(NULL, path, NULL);
}
Volume* v = volume_for_path(path);
if (v == NULL) {
// silent failure for sd-ext
if (strcmp(path, "/sd-ext") != 0)
LOGE("unknown volume '%s'\n", path);
return -1;
}
if (strcmp(fs_type, "ramdisk") == 0) {
// you can't format the ramdisk.
LOGE("can't format_volume \"%s\"", path);
return -1;
}
if (strcmp(fs_type, "rfs") == 0) {
if (ensure_path_unmounted(path) != 0) {
LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
return -1;
}
if (0 != format_rfs_device(device, path)) {
LOGE("format_volume: format_rfs_device failed on %s\n", device);
return -1;
}
return 0;
}
if (strcmp(v->mount_point, path) != 0) {
return format_unknown_device(v->blk_device, path, NULL);
}
if (ensure_path_unmounted(path) != 0) {
LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
return -1;
}
if (strcmp(fs_type, "yaffs2") == 0 || strcmp(fs_type, "mtd") == 0) {
mtd_scan_partitions();
const MtdPartition* partition = mtd_find_partition_by_name(device);
if (partition == NULL) {
LOGE("format_volume: no MTD partition \"%s\"\n", device);
return -1;
}
MtdWriteContext *write = mtd_write_partition(partition);
if (write == NULL) {
LOGW("format_volume: can't open MTD \"%s\"\n", device);
return -1;
} else if (mtd_erase_blocks(write, -1) == (off_t) - 1) {
LOGW("format_volume: can't erase MTD \"%s\"\n", device);
mtd_write_close(write);
return -1;
} else if (mtd_write_close(write)) {
LOGW("format_volume: can't close MTD \"%s\"\n", device);
return -1;
}
return 0;
}
if (strcmp(fs_type, "ext4") == 0) {
int length = 0;
if (strcmp(v->fs_type, "ext4") == 0) {
// Our desired filesystem matches the one in fstab, respect v->length
length = v->length;
}
#ifdef USE_MKE2FS_FORMAT
char ext4_cmd[PATH_MAX];
sprintf(ext4_cmd, "/sbin/mke2fs -T ext4 -b 4096 -m 0 -F %s", device);
int result = __system(ext4_cmd);
#else
int result = make_ext4fs(device, length, v->mount_point, sehandle);
#endif
if (result != 0) {
LOGE("format_volume: format ext4 fs failed on %s\n", device);
return -1;
}
#ifdef USE_MKE2FS_FORMAT
#ifdef NEED_SELINUX_FIX
if (0 == strcmp(v->mount_point, "/data") ||
0 == strcmp(v->mount_point, "/system") ||
0 == strcmp(v->mount_point, "/cache"))
{
ensure_path_mounted(v->mount_point);
char tmp[PATH_MAX];
sprintf(tmp, "%s/lost+found", v->mount_point);
if (selinux_android_restorecon(tmp, 0) < 0 || selinux_android_restorecon(v->mount_point, 0) < 0) {
LOGW("restorecon: error restoring %s context\n",v->mount_point);
//return -1;
}
ensure_path_unmounted(v->mount_point);
}
#endif
#endif
return 0;
}
#ifdef USE_F2FS
if (strcmp(fs_type, "f2fs") == 0) {
char* args[] = { "mkfs.f2fs", v->blk_device };
if (make_f2fs_main(2, args) != 0) {
LOGE("format_volume: mkfs.f2fs failed on %s\n", v->blk_device);
return -1;
}
return 0;
}
#endif
return format_unknown_device(device, path, fs_type);
}
int format_unknown_device(const char *device, const char* path, const char *fs_type) {
LOGI("Formatting unknown device.\n");
if (fs_type != NULL && get_flash_type(fs_type) != UNSUPPORTED)
return erase_raw_partition(fs_type, device);
// if this is SDEXT:, don't worry about it if it does not exist.
if (0 == strcmp(path, "/sd-ext")) {
struct stat st;
Volume *vol = volume_for_path("/sd-ext");
if (vol == NULL || 0 != stat(vol->blk_device, &st)) {
LOGI("No app2sd partition found. Skipping format of /sd-ext.\n");
return 0;
}
}
if (NULL != fs_type) {
if (strcmp("ext3", fs_type) == 0) {
LOGI("Formatting ext3 device.\n");
if (0 != ensure_path_unmounted(path)) {
LOGE("Error while unmounting %s.\n", path);
return -12;
}
return format_ext3_device(device);
}
if (strcmp("ext2", fs_type) == 0) {
LOGI("Formatting ext2 device.\n");
if (0 != ensure_path_unmounted(path)) {
LOGE("Error while unmounting %s.\n", path);
return -12;
}
return format_ext2_device(device);
}
}
if (0 != ensure_path_mounted(path)) {
ui_print("Error mounting %s!\n", path);
ui_print("Skipping format...\n");
return 0;
}
char tmp[PATH_MAX];
if (strcmp(path, "/data") == 0) {
sprintf(tmp, "cd /data ; for f in $(ls -A | grep -v ^media$); do rm -rf $f; done");
__system(tmp);
sprintf(tmp, "cd /data ; for f in $(ls -A | grep -v ^media$); do chattr -R -i $f; rm -rf $f; done");
__system(tmp);
// if the /data/media sdcard has already been migrated for android 4.2,
// prevent the migration from happening again by writing the .layout_version
struct stat st;
if (0 == lstat("/data/media/0", &st)) {
char* layout_version = "2";
FILE* f = fopen("/data/.layout_version", "wb");
if (NULL != f) {
fwrite(layout_version, 1, 2, f);
fclose(f);
} else {
LOGI("error opening /data/.layout_version for write.\n");
}
} else {
LOGI("/data/media/0 not found. migration may occur.\n");
}
} else {
sprintf(tmp, "chattr -R -i %s", path);
__system(tmp);
sprintf(tmp, "rm -rf %s/*", path);
__system(tmp);
sprintf(tmp, "rm -rf %s/.*", path);
__system(tmp);
}
if (strstr(path, ".android_secure") == NULL) ensure_path_unmounted(path);
return 0;
}
static MFMatrix get_mnt_fmt_capabilities(char *fs_type, char *mount_point) {
MFMatrix mfm = { mount_point, 1, 1 };
const int NUM_FS_TYPES = 6;
MFMatrix *fs_matrix = malloc(NUM_FS_TYPES * sizeof(MFMatrix));
// Defined capabilities: fs_type mnt fmt
fs_matrix[0] = (MFMatrix){ "bml", 0, 1 };
fs_matrix[1] = (MFMatrix){ "datamedia", 0, 1 };
fs_matrix[2] = (MFMatrix){ "emmc", 0, 1 };
fs_matrix[3] = (MFMatrix){ "mtd", 0, 0 };
fs_matrix[4] = (MFMatrix){ "ramdisk", 0, 0 };
fs_matrix[5] = (MFMatrix){ "swap", 0, 0 };
const int NUM_MNT_PNTS = 6;
MFMatrix *mp_matrix = malloc(NUM_MNT_PNTS * sizeof(MFMatrix));
// Defined capabilities: mount_point mnt fmt
mp_matrix[0] = (MFMatrix){ "/misc", 0, 0 };
mp_matrix[1] = (MFMatrix){ "/radio", 0, 0 };
mp_matrix[2] = (MFMatrix){ "/bootloader", 0, 0 };
mp_matrix[3] = (MFMatrix){ "/recovery", 0, 0 };
mp_matrix[4] = (MFMatrix){ "/efs", 0, 0 };
mp_matrix[5] = (MFMatrix){ "/wimax", 0, 0 };
int i;
for (i = 0; i < NUM_FS_TYPES; i++) {
if (strcmp(fs_type, fs_matrix[i].name) == 0) {
mfm.can_mount = fs_matrix[i].can_mount;
mfm.can_format = fs_matrix[i].can_format;
}
}
for (i = 0; i < NUM_MNT_PNTS; i++) {
if (strcmp(mount_point, mp_matrix[i].name) == 0) {
mfm.can_mount = mp_matrix[i].can_mount;
mfm.can_format = mp_matrix[i].can_format;
}
}
free(fs_matrix);
free(mp_matrix);
// User-defined capabilities
char *custom_mp;
char custom_forbidden_mount[PROPERTY_VALUE_MAX];
char custom_forbidden_format[PROPERTY_VALUE_MAX];
property_get("ro.cwm.forbid_mount", custom_forbidden_mount, "");
property_get("ro.cwm.forbid_format", custom_forbidden_format, "");
custom_mp = strtok(custom_forbidden_mount, ",");
while (custom_mp != NULL) {
if (strcmp(mount_point, custom_mp) == 0) {
mfm.can_mount = 0;
}
custom_mp = strtok(NULL, ",");
}
custom_mp = strtok(custom_forbidden_format, ",");
while (custom_mp != NULL) {
if (strcmp(mount_point, custom_mp) == 0) {
mfm.can_format = 0;
}
custom_mp = strtok(NULL, ",");
}
return mfm;
}
static int is_ums_capable() {
// control_usb_storage() only supports vold managed storage
int i;
// If USB volume is available, UMS not possible (assumes one USB/device)
for (i = 0; i < get_num_volumes(); i++) {
Volume *v = get_device_volumes() + i;
if (fs_mgr_is_voldmanaged(v) && vold_is_volume_available(v->mount_point)
&& (strcasestr(v->label, "usb") || strcasestr(v->label, "otg")))
return 0;
}
// No USB storage found, look for any other vold managed storage
for (i = 0; i < get_num_volumes(); i++) {
Volume *v = get_device_volumes() + i;
if (fs_mgr_is_voldmanaged(v) && vold_is_volume_available(v->mount_point))
return 1;
}
return 0;
}
int show_partition_menu() {
static const char* headers[] = { "Mounts and Storage Menu", "", NULL };
static char* confirm_format = "Confirm format?";
static char* confirm = "Yes - Format";
char confirm_string[255];
static MountMenuEntry* mount_menu = NULL;
static FormatMenuEntry* format_menu = NULL;
static char* list[256];
int i, mountable_volumes, formatable_volumes;
int num_volumes;
int chosen_item = 0;
int menu_entries = 0;
struct menu_extras {
int dm; // boolean: enable wipe data media
int ums; // boolean: enable mount usb mass storage
int idm; // index of wipe dm entry in list[]
int iums; // index of ums entry in list[]
};
struct menu_extras me;
num_volumes = get_num_volumes();
if (!num_volumes)
return 0;
mountable_volumes = 0;
formatable_volumes = 0;
mount_menu = malloc(num_volumes * sizeof(MountMenuEntry));
format_menu = malloc(num_volumes * sizeof(FormatMenuEntry));
for (i = 0; i < num_volumes; i++) {
Volume* v = get_device_volumes() + i;
if (fs_mgr_is_voldmanaged(v) && !vold_is_volume_available(v->mount_point)) {
continue;
}
MFMatrix mfm = get_mnt_fmt_capabilities(v->fs_type, v->mount_point);
if (mfm.can_mount) {
sprintf(mount_menu[mountable_volumes].mount, "mount %s", v->mount_point);
sprintf(mount_menu[mountable_volumes].unmount, "unmount %s", v->mount_point);
sprintf(mount_menu[mountable_volumes].path, "%s", v->mount_point);
++mountable_volumes;
}
if (mfm.can_format) {
sprintf(format_menu[formatable_volumes].txt, "format %s", v->mount_point);
sprintf(format_menu[formatable_volumes].path, "%s", v->mount_point);
sprintf(format_menu[formatable_volumes].type, "%s", v->fs_type);
++formatable_volumes;
}
}
for (;;) {
for (i = 0; i < mountable_volumes; i++) {
MountMenuEntry* e = &mount_menu[i];
if (is_path_mounted(e->path))
list[i] = e->unmount;
else
list[i] = e->mount;
}
for (i = 0; i < formatable_volumes; i++) {
FormatMenuEntry* e = &format_menu[i];
list[mountable_volumes + i] = e->txt;
}
menu_entries = mountable_volumes + formatable_volumes;
me = (struct menu_extras){ 0, 0, 0, 0 };
if (me.dm = is_data_media()) {
me.idm = menu_entries;
list[me.idm] = "format /data and /data/media (/sdcard)";
menu_entries++;
}
if (me.ums = is_ums_capable()) {
me.iums = menu_entries;
list[me.iums] = "mount USB storage";
menu_entries++;
}