-
Notifications
You must be signed in to change notification settings - Fork 3
/
extra-functions.c
2128 lines (1914 loc) · 56.9 KB
/
extra-functions.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) 2011 The Android Open Source 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 <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <ctype.h>
#include "cutils/misc.h"
#include "cutils/properties.h"
#include <dirent.h>
#include <getopt.h>
#include <linux/input.h>
#include <signal.h>
#include <sys/limits.h>
#include <termios.h>
#include <time.h>
#include "tw_reboot.h"
#include "bootloader.h"
#include "common.h"
#include "extra-functions.h"
#include "cutils/properties.h"
#include "install.h"
#include "minui/minui.h"
#include "minzip/DirUtil.h"
#include "minzip/Zip.h"
#include "recovery_ui.h"
#include "roots.h"
#include "ddftw.h"
#include "backstore.h"
#include "themes.h"
//kang system() from bionic/libc/unistd and rename it __system() so we can be even more hackish :)
#undef _PATH_BSHELL
#define _PATH_BSHELL "/sbin/sh"
extern char **environ;
// sdcard partitioning variables
char swapsize[32];
int swap;
char extsize[32];
int ext;
int ext_format; // 3 or 4
int
__system(const char *command)
{
pid_t pid;
sig_t intsave, quitsave;
sigset_t mask, omask;
int pstat;
char *argp[] = {"sh", "-c", NULL, NULL};
if (!command) /* just checking... */
return(1);
argp[2] = (char *)command;
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, &omask);
switch (pid = vfork()) {
case -1: /* error */
sigprocmask(SIG_SETMASK, &omask, NULL);
return(-1);
case 0: /* child */
sigprocmask(SIG_SETMASK, &omask, NULL);
execve(_PATH_BSHELL, argp, environ);
_exit(127);
}
intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN);
quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
pid = waitpid(pid, (int *)&pstat, 0);
sigprocmask(SIG_SETMASK, &omask, NULL);
(void)bsd_signal(SIGINT, intsave);
(void)bsd_signal(SIGQUIT, quitsave);
return (pid == -1 ? -1 : pstat);
}
static struct pid {
struct pid *next;
FILE *fp;
pid_t pid;
} *pidlist;
FILE *
__popen(const char *program, const char *type)
{
struct pid * volatile cur;
FILE *iop;
int pdes[2];
pid_t pid;
if ((*type != 'r' && *type != 'w') || type[1] != '\0') {
errno = EINVAL;
return (NULL);
}
if ((cur = malloc(sizeof(struct pid))) == NULL)
return (NULL);
if (pipe(pdes) < 0) {
free(cur);
return (NULL);
}
switch (pid = vfork()) {
case -1: /* Error. */
(void)close(pdes[0]);
(void)close(pdes[1]);
free(cur);
return (NULL);
/* NOTREACHED */
case 0: /* Child. */
{
struct pid *pcur;
/*
* because vfork() instead of fork(), must leak FILE *,
* but luckily we are terminally headed for an execl()
*/
for (pcur = pidlist; pcur; pcur = pcur->next)
close(fileno(pcur->fp));
if (*type == 'r') {
int tpdes1 = pdes[1];
(void) close(pdes[0]);
/*
* We must NOT modify pdes, due to the
* semantics of vfork.
*/
if (tpdes1 != STDOUT_FILENO) {
(void)dup2(tpdes1, STDOUT_FILENO);
(void)close(tpdes1);
tpdes1 = STDOUT_FILENO;
}
} else {
(void)close(pdes[1]);
if (pdes[0] != STDIN_FILENO) {
(void)dup2(pdes[0], STDIN_FILENO);
(void)close(pdes[0]);
}
}
execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL);
_exit(127);
/* NOTREACHED */
}
}
/* Parent; assume fdopen can't fail. */
if (*type == 'r') {
iop = fdopen(pdes[0], type);
(void)close(pdes[1]);
} else {
iop = fdopen(pdes[1], type);
(void)close(pdes[0]);
}
/* Link into list of file descriptors. */
cur->fp = iop;
cur->pid = pid;
cur->next = pidlist;
pidlist = cur;
return (iop);
}
/*
* pclose --
* Pclose returns -1 if stream is not associated with a `popened' command,
* if already `pclosed', or waitpid returns an error.
*/
int
__pclose(FILE *iop)
{
struct pid *cur, *last;
int pstat;
pid_t pid;
/* Find the appropriate file pointer. */
for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
if (cur->fp == iop)
break;
if (cur == NULL)
return (-1);
(void)fclose(iop);
do {
pid = waitpid(cur->pid, &pstat, 0);
} while (pid == -1 && errno == EINTR);
/* Remove the entry from the linked list. */
if (last == NULL)
pidlist = cur->next;
else
last->next = cur->next;
free(cur);
return (pid == -1 ? -1 : pstat);
}
#define CMDLINE_SERIALNO "androidboot.serialno="
#define CMDLINE_SERIALNO_LEN (strlen(CMDLINE_SERIALNO))
#define CPUINFO_SERIALNO "Serial"
#define CPUINFO_SERIALNO_LEN (strlen(CPUINFO_SERIALNO))
#define CPUINFO_HARDWARE "Hardware"
#define CPUINFO_HARDWARE_LEN (strlen(CPUINFO_HARDWARE))
void get_device_id()
{
FILE *fp;
char line[2048];
char hardware_id[32];
char* token;
// Assign a blank device_id to start with
device_id[0] = 0;
// First, try the cmdline to see if the serial number was supplied
fp = fopen("/proc/cmdline", "rt");
if (fp != NULL)
{
// First step, read the line. For cmdline, it's one long line
fgets(line, sizeof(line), fp);
fclose(fp);
// Now, let's tokenize the string
token = strtok(line, " ");
// Let's walk through the line, looking for the CMDLINE_SERIALNO token
while (token)
{
// We don't need to verify the length of token, because if it's too short, it will mismatch CMDLINE_SERIALNO at the NULL
if (memcmp(token, CMDLINE_SERIALNO, CMDLINE_SERIALNO_LEN) == 0)
{
// We found the serial number!
strcpy(device_id, token + CMDLINE_SERIALNO_LEN);
return;
}
token = strtok(NULL, " ");
}
}
// Now we'll try cpuinfo for a serial number
fp = fopen("/proc/cpuinfo", "rt");
if (fp != NULL)
{
while (fgets(line, sizeof(line), fp) != NULL) { // First step, read the line.
if (memcmp(line, CPUINFO_SERIALNO, CPUINFO_SERIALNO_LEN) == 0) // check the beginning of the line for "Serial"
{
// We found the serial number!
token = line + CPUINFO_SERIALNO_LEN; // skip past "Serial"
while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
if (*token != NULL) {
token[30] = 0;
if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
memset(device_id, 0, sizeof(device_id));
strncpy(device_id, token, strlen(token) - 1);
} else {
strcpy(device_id, token);
}
LOGI("=> serial from cpuinfo: '%s'\n", device_id);
fclose(fp);
return;
}
} else if (memcmp(line, CPUINFO_HARDWARE, CPUINFO_HARDWARE_LEN) == 0) {// We're also going to look for the hardware line in cpuinfo and save it for later in case we don't find the device ID
// We found the hardware ID
token = line + CPUINFO_HARDWARE_LEN; // skip past "Hardware"
while ((*token > 0 && *token <= 32 ) || *token == ':') token++; // skip over all spaces and the colon
if (*token != NULL) {
token[30] = 0;
if (token[strlen(token)-1] == 10) { // checking for endline chars and dropping them from the end of the string if needed
memset(hardware_id, 0, sizeof(hardware_id));
strncpy(hardware_id, token, strlen(token) - 1);
} else {
strcpy(hardware_id, token);
}
LOGI("=> hardware id from cpuinfo: '%s'\n", hardware_id);
}
}
}
fclose(fp);
}
if (hardware_id[0] != 0) {
LOGW("\nusing hardware id for device id: '%s'\n", hardware_id);
strcpy(device_id, hardware_id);
return;
}
strcpy(device_id, "serialno");
LOGE("=> device id not found, using '%s'.", device_id);
return;
}
void show_fake_main_menu() {
// Main Menu
#define ITEM_APPLY_SDCARD 0
#define ITEM_NANDROID_MENU 1
#define ITEM_MAIN_WIPE_MENU 2
#define ITEM_ADVANCED_MENU 3
#define ITEM_MOUNT_MENU 4
#define ITEM_USB_TOGGLE 5
#define ITEM_REBOOT 6
#define ITEM_SHUTDOWN 7
char** headers = prepend_title((const char**)MENU_HEADERS);
char* MENU_ITEMS[] = { "Install Zip",
"Nandroid Menu",
"Wipe Menu",
"Advanced Menu",
"Mount Menu",
"USB Storage Toggle",
"Reboot system now",
"Power down system",
NULL };
go_home = 0;
go_menu = 0;
menu_loc_idx = 0;
ui_reset_progress();
int chosen_item = get_menu_selection(headers, MENU_ITEMS, 0, 0);
// device-specific code may take some action here. It may
// return one of the core actions handled in the switch
// statement below.
chosen_item = device_perform_action(chosen_item);
switch (chosen_item) {
case ITEM_APPLY_SDCARD:
install_zip_menu(0);
break;
case ITEM_NANDROID_MENU:
nandroid_menu();
break;
case ITEM_MAIN_WIPE_MENU:
main_wipe_menu();
break;
case ITEM_ADVANCED_MENU:
advanced_menu();
break;
case ITEM_MOUNT_MENU:
mount_menu(0);
break;
case ITEM_USB_TOGGLE:
usb_storage_toggle();
break;
case ITEM_REBOOT:
go_reboot = 1;
return;
case ITEM_SHUTDOWN:
tw_reboot(rb_poweroff);
break;
}
if (go_menu) {
advanced_menu();
}
if (go_restart || go_home) {
go_restart = 0;
go_home = 0;
}
ui_end_menu();
return;
}
/*partial kangbang from system/vold
TODO: Currently only one mount is supported, defaulting
/mnt/sdcard to lun0 and anything else gets no love. Fix this.
*/
#ifndef CUSTOM_LUN_FILE
#define CUSTOM_LUN_FILE "/sys/devices/platform/usb_mass_storage/lun"
#endif
int usb_storage_enable(void)
{
int fd;
Volume *vol = volume_for_path("/sdcard");
if (!vol)
{
LOGE("Unable to locate volume information.");
return -1;
}
if ((fd = open(CUSTOM_LUN_FILE"0/file", O_WRONLY)) < 0)
{
LOGE("Unable to open ums lunfile: (%s)", strerror(errno));
return -1;
}
if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
(!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
LOGE("Unable to write to ums lunfile: (%s)", strerror(errno));
close(fd);
return -1;
}
close(fd);
return 0;
}
int usb_storage_disable(void)
{
int fd;
if ((fd = open(CUSTOM_LUN_FILE"0/file", O_WRONLY)) < 0)
{
LOGE("Unable to open ums lunfile: (%s)", strerror(errno));
return -1;
}
char ch = 0;
if (write(fd, &ch, 1) < 0)
{
LOGE("Unable to write to ums lunfile: (%s)", strerror(errno));
close(fd);
return -1;
}
close(fd);
return 0;
}
void usb_storage_toggle()
{
/*maybe make this a header instead?
static char* headers[] = { "Mounting USB as storage device",
"",
NULL
};
*/
ui_print("\nMounting USB as storage device...");
if (usb_storage_enable())
return;
ui_clear_key_queue();
ui_print("\nUSB as storage device mounted!\n");
ui_print("\nPress Power to disable,");
ui_print("\nand return to menu\n");
for (;;) {
int key = ui_wait_key();
if (key == KEY_POWER) {
ui_print("\nDisabling USB as storage device...");
usb_storage_disable();
break;
}
}
return;
}
char* zip_verify()
{
char* tmp_set = (char*)malloc(40);
strcpy(tmp_set, "[ ] Zip Signature Verification");
if (DataManager_GetIntValue(TW_SIGNED_ZIP_VERIFY_VAR) == 1) {
tmp_set[1] = 'x';
}
return tmp_set;
}
char* reboot_after_flash()
{
char* tmp_set = (char*)malloc(40);
strcpy(tmp_set, "[ ] Reboot After Successful Flash");
if (DataManager_GetIntValue(TW_REBOOT_AFTER_FLASH_VAR) == 1) {
tmp_set[1] = 'x';
}
return tmp_set;
}
char* force_md5_check()
{
char* tmp_set = (char*)malloc(40);
strcpy(tmp_set, "[ ] Force md5 verification");
if (DataManager_GetIntValue(TW_FORCE_MD5_CHECK_VAR) == 1) {
tmp_set[1] = 'x';
}
return tmp_set;
}
char* sort_by_date_option()
{
char* tmp_set = (char*)malloc(40);
strcpy(tmp_set, "[ ] Sort Zips by Date");
if (DataManager_GetIntValue(TW_SORT_FILES_BY_DATE_VAR) == 1) {
tmp_set[1] = 'x';
}
return tmp_set;
}
char* rm_rf_option()
{
char* tmp_set = (char*)malloc(40);
strcpy(tmp_set, "[ ] rm -rf Instead of Format");
if (DataManager_GetIntValue(TW_RM_RF_VAR) == 1) {
tmp_set[1] = 'x';
}
return tmp_set;
}
void install_zip_menu(int pIdx)
{
// INSTALL ZIP MENU
#define ITEM_CHOOSE_ZIP 0
#define ITEM_FLASH_ZIPS 1
#define ITEM_CLEAR_ZIPS 2
#define ITEM_WIPE_CACHE_DALVIK 3
#define ITEM_SORT_BY_DATE 4
#define ITEM_REBOOT_AFTER_FLASH 5
#define ITEM_TOGGLE_SIG 6
#define ITEM_TOGGLE_FORCE_MD5 7
#define ITEM_ZIP_RBOOT 8
#define ITEM_ZIP_BACK 9
ui_set_background(BACKGROUND_ICON_FLASH_ZIP);
static char* MENU_FLASH_HEADERS[] = { "Install Zip Menu",
"Flash Zip From SD Card:",
NULL };
char* MENU_INSTALL_ZIP[] = { "--> Choose Zip To Flash",
"Flash Zips Now",
"Clear Zip Queue",
"Wipe Cache and Dalvik Cache",
sort_by_date_option(),
reboot_after_flash(),
zip_verify(),
force_md5_check(),
"--> Reboot To System",
"<-- Back To Main Menu",
NULL };
char** headers = prepend_title(MENU_FLASH_HEADERS);
int zip_loop_index, status;
inc_menu_loc(ITEM_ZIP_BACK);
for (;;)
{
int chosen_item = get_menu_selection(headers, MENU_INSTALL_ZIP, 0, pIdx);
pIdx = chosen_item;
switch (chosen_item)
{
case ITEM_CHOOSE_ZIP:
if (multi_zip_index < 10) {
status = sdcard_directory(DataManager_GetStrValue(TW_ZIP_LOCATION_VAR));
} else {
ui_print("Maximum of %i zips queued.\n", multi_zip_index);
}
break;
case ITEM_FLASH_ZIPS:
if (multi_zip_index == 0) {
ui_print("No zips selected to install.\n");
} else {
for (zip_loop_index; zip_loop_index < multi_zip_index; zip_loop_index++) {
LOGI("Installing %s\n", multi_zip_array[zip_loop_index]);
status = install_zip_package(multi_zip_array[zip_loop_index]);
ui_reset_progress(); // reset status bar so it doesnt run off the screen
if (status != INSTALL_SUCCESS) {
if (notError == 1) {
ui_set_background(BACKGROUND_ICON_ERROR);
ui_print("Installation aborted due to an error.\n");
multi_zip_index = 0; // clear zip queue
break;
}
} // end if (status != INSTALL_SUCCESS)
} // end for loop
if (!ui_text_visible()) {
multi_zip_index = 0; // clear zip queue
return; // reboot if logs aren't visible
} else {
if (DataManager_GetIntValue(TW_REBOOT_AFTER_FLASH_VAR)) {
tw_reboot(rb_system);
return;
}
if (go_home != 1) {
ui_print("\nInstall from sdcard complete.\n");
}
multi_zip_index = 0; // clear zip queue
} // end if (!ui_text_visible())
} // end if (multi_zip_index == 0)
break;
case ITEM_CLEAR_ZIPS:
multi_zip_index = 0;
ui_print("\nZip Queue Cleared\n\n");
break;
case ITEM_WIPE_CACHE_DALVIK:
ui_print("\n-- Wiping Cache Partition...\n");
erase_volume("/cache");
ui_print("-- Cache Partition Wipe Complete!\n");
wipe_dalvik_cache();
break;
case ITEM_REBOOT_AFTER_FLASH:
DataManager_ToggleIntValue(TW_REBOOT_AFTER_FLASH_VAR);
break;
case ITEM_SORT_BY_DATE:
DataManager_ToggleIntValue(TW_SORT_FILES_BY_DATE_VAR);
break;
case ITEM_TOGGLE_SIG:
DataManager_ToggleIntValue(TW_SIGNED_ZIP_VERIFY_VAR);
break;
case ITEM_TOGGLE_FORCE_MD5:
DataManager_ToggleIntValue(TW_FORCE_MD5_CHECK_VAR);
break;
case ITEM_ZIP_RBOOT:
tw_reboot(rb_system);
break;
case ITEM_ZIP_BACK:
dec_menu_loc();
ui_set_background(BACKGROUND_ICON_MAIN);
return;
}
if (go_home) {
dec_menu_loc();
return;
}
break;
}
ui_end_menu();
dec_menu_loc();
install_zip_menu(pIdx);
}
void wipe_dalvik_cache()
{
ui_set_background(BACKGROUND_ICON_WIPE);
ensure_path_mounted("/data");
ensure_path_mounted("/cache");
ui_print("\n-- Wiping Dalvik Cache Directories...\n");
__system("rm -rf /data/dalvik-cache");
ui_print("Cleaned: /data/dalvik-cache...\n");
__system("rm -rf /cache/dalvik-cache");
ui_print("Cleaned: /cache/dalvik-cache...\n");
__system("rm -rf /cache/dc");
ui_print("Cleaned: /cache/dc\n");
struct stat st;
if (0 != stat(sde.blk, &st))
{
ui_print("/sd-ext not present, skipping\n");
} else {
__system("mount /sd-ext");
LOGI("Mounting /sd-ext\n");
if (stat("/sd-ext/dalvik-cache",&st) == 0)
{
__system("rm -rf /sd-ext/dalvik-cache");
ui_print("Cleaned: /sd-ext/dalvik-cache...\n");
}
}
ensure_path_unmounted("/data");
ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n");
ui_set_background(BACKGROUND_ICON_MAIN);
if (!ui_text_visible()) return;
}
void reboot_menu()
{
#define ITEM_SYSTEM 0
#define ITEM_RECOVERY 1
#define ITEM_BOOTLOADER 2
#define ITEM_POWEROFF 3
#define ITEM_BACKK 4
static char* MENU_REBOOT_HEADERS[] = { "Reboot Menu",
"Choose Your Destiny:",
NULL };
// REBOOT MENU
char* MENU_REBOOT[] = { "Reboot To System",
"Reboot To Recovery",
"Reboot To Bootloader",
"Power Off",
"<-- Back To Advanced Menu",
NULL };
char** headers = prepend_title(MENU_REBOOT_HEADERS);
// This is an old, common method for ensuring a flush
sync();
sync();
inc_menu_loc(ITEM_BACKK);
for (;;)
{
int chosen_item = get_menu_selection(headers, MENU_REBOOT, 0, 0);
switch (chosen_item)
{
case ITEM_RECOVERY:
tw_reboot(rb_recovery);
break;
case ITEM_BOOTLOADER:
tw_reboot(rb_bootloader);
break;
case ITEM_POWEROFF:
tw_reboot(rb_poweroff);
break;
case ITEM_SYSTEM:
tw_reboot(rb_system);
break;
case ITEM_BACKK:
dec_menu_loc();
return;
}
if (go_home) {
dec_menu_loc();
return;
}
}
}
// BATTERY STATS
void wipe_battery_stats()
{
ensure_path_mounted("/data");
struct stat st;
if (0 != stat("/data/system/batterystats.bin", &st))
{
ui_print("No Battery Stats Found. No Need To Wipe.\n");
} else {
ui_set_background(BACKGROUND_ICON_WIPE);
remove("/data/system/batterystats.bin");
ui_print("Cleared: Battery Stats...\n");
ensure_path_unmounted("/data");
}
}
// ROTATION SETTINGS
void wipe_rotate_data()
{
ui_set_background(BACKGROUND_ICON_WIPE);
ensure_path_mounted("/data");
__system("rm -r /data/misc/akmd*");
__system("rm -r /data/misc/rild*");
ui_print("Cleared: Rotatation Data...\n");
ensure_path_unmounted("/data");
}
void fix_perms()
{
ensure_path_mounted("/data");
ensure_path_mounted("/system");
ui_show_progress(1,30);
ui_print("\n-- Fixing Permissions\n");
ui_print("This may take a few minutes.\n");
__system("./sbin/fix_permissions.sh");
ui_print("-- Done.\n\n");
ui_reset_progress();
}
void advanced_menu()
{
// ADVANCED MENU
#ifdef BOARD_HAS_NO_REAL_SDCARD
#define ITEM_REBOOT_MENU 0
#define ITEM_FORMAT_MENU 1
#define ITEM_FIX_PERM 2
#define ITEM_ALL_SETTINGS 3
#define ITEM_SDCARD_PART 99
#define ITEM_CPY_LOG 4
#define ADVANCED_MENU_BACK 5
char* MENU_ADVANCED[] = { "Reboot Menu",
"Format Menu",
"Fix Permissions",
"Change twrp Settings",
"Copy recovery log to /sdcard",
"<-- Back To Main Menu",
NULL };
#else
#define ITEM_REBOOT_MENU 0
#define ITEM_FORMAT_MENU 1
#define ITEM_FIX_PERM 2
#define ITEM_ALL_SETTINGS 3
#define ITEM_SDCARD_PART 4
#define ITEM_CPY_LOG 5
#define ADVANCED_MENU_BACK 6
char* MENU_ADVANCED[] = { "Reboot Menu",
"Format Menu",
"Fix Permissions",
"Change twrp Settings",
"Partition SD Card",
"Copy recovery log to /sdcard",
"<-- Back To Main Menu",
NULL };
#endif
static char* MENU_ADVANCED_HEADERS[] = { "Advanced Menu",
"Reboot, Format, or twrp!",
NULL };
char** headers = prepend_title(MENU_ADVANCED_HEADERS);
inc_menu_loc(ADVANCED_MENU_BACK);
for (;;)
{
int chosen_item = get_menu_selection(headers, MENU_ADVANCED, 0, 0);
switch (chosen_item)
{
case ITEM_REBOOT_MENU:
reboot_menu();
break;
case ITEM_FORMAT_MENU:
format_menu();
break;
case ITEM_FIX_PERM:
fix_perms();
break;
case ITEM_ALL_SETTINGS:
all_settings_menu(0);
break;
case ITEM_SDCARD_PART:
show_menu_partition();
break;
case ITEM_CPY_LOG:
ensure_path_mounted("/sdcard");
__system("cp /tmp/recovery.log /sdcard");
sync();
ui_print("Copied recovery log to /sdcard.\n");
break;
case ADVANCED_MENU_BACK:
dec_menu_loc();
return;
}
if (go_home) {
dec_menu_loc();
return;
}
}
}
// kang'd this from recovery.c cuz there wasnt a recovery.h!
int
erase_volume(const char *volume) {
ui_print("Formatting %s...\n", volume);
if (strcmp(volume, "/cache") == 0) {
// Any part of the log we'd copied to cache is now gone.
// Reset the pointer so we copy from the beginning of the temp
// log.
tmplog_offset = 0;
}
return format_volume(volume);
}
// FORMAT MENU
void
format_menu()
{
struct stat st;
#define ITEM_FORMAT_SYSTEM 0
#define ITEM_FORMAT_DATA 1
#define ITEM_FORMAT_CACHE 2
#define ITEM_FORMAT_SDCARD 3
#define ITEM_FORMAT_SDEXT 4
#define ITEM_FORMAT_BACK 5
char* part_headers[] = { "Format Menu",
"Choose a Partition to Format: ",
NULL };
char* part_items[] = { "Format SYSTEM (/system)",
"Format DATA (/data)",
"Format CACHE (/cache)",
"Format SDCARD (/sdcard)",
"Format SD-EXT (/sd-ext)",
"<-- Back To Advanced Menu",
NULL };
char** headers = prepend_title(part_headers);
inc_menu_loc(ITEM_FORMAT_BACK);
for (;;)
{
ui_set_background(BACKGROUND_ICON_WIPE_CHOOSE);
int chosen_item = get_menu_selection(headers, part_items, 0, 0);
switch (chosen_item)
{
case ITEM_FORMAT_SYSTEM:
confirm_format("SYSTEM", "/system");
break;
case ITEM_FORMAT_DATA:
confirm_format("DATA", "/data");
break;
case ITEM_FORMAT_CACHE:
confirm_format("CACHE", "/cache");
break;
case ITEM_FORMAT_SDCARD:
confirm_format("SDCARD", "/sdcard");
break;
case ITEM_FORMAT_SDEXT:
if (stat(sde.blk,&st) == 0)
{
__system("mount /sd-ext");
confirm_format("SD-EXT", "/sd-ext");
} else {
ui_print("\n/sd-ext not detected! Aborting.\n");
}
break;
case ITEM_FORMAT_BACK:
dec_menu_loc();
ui_set_background(BACKGROUND_ICON_MAIN);
return;
}
if (go_home) {
dec_menu_loc();
ui_set_background(BACKGROUND_ICON_MAIN);
return;
}
}
}
void
confirm_format(char* volume_name, char* volume_path) {
char* confirm_headers[] = { "Confirm Format of Partition: ",
volume_name,
"",
" THIS CAN NOT BE UNDONE!",
NULL };
char* items[] = { "No",
"Yes -- Permanently Format",
NULL };
char** headers = prepend_title(confirm_headers);
inc_menu_loc(0);
int chosen_item = get_menu_selection(headers, items, 1, 0);
if (chosen_item != 1) {
dec_menu_loc();
return;
} else {
ui_set_background(BACKGROUND_ICON_WIPE);
ui_print("\n-- Wiping %s Partition...\n", volume_name);
erase_volume(volume_path);
ui_print("-- %s Partition Wipe Complete!\n", volume_name);
dec_menu_loc();
}
}
int get_battery_level(void)
{
static int lastVal = -1;
static time_t nextSecCheck = 0;
struct timeval curTime;
gettimeofday(&curTime, NULL);
if (curTime.tv_sec > nextSecCheck)
{
char cap_s[4];
FILE * cap = fopen("/sys/class/power_supply/battery/capacity","rt");
if (cap)