forked from sndnvaps/miui_recovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot_device.cpp
executable file
·807 lines (726 loc) · 24.1 KB
/
root_device.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <linux/input.h>
#include <sys/reboot.h>
#include <libgen.h>
#include <sys/wait.h>
#include <sys/limits.h>
#include <dirent.h>
#include <signal.h>
#include "bootloader.h"
#include "common.h"
#include "root_device.hpp"
#include "roots.h"
#include "nandroid.h"
extern "C" {
#include "libcrecovery/common.h"
#include "miui/src/miui.h"
#include "miui_intent.h"
#include "flashutils/flashutils.h"
}
#include "mtdutils/mounts.h"
#include "minzip/DirUtil.h"
#include "edify/expr.h"
#include "cutils/android_reboot.h"
#include "cutils/properties.h"
#include "minui/minui.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <vector>
using namespace std;
/*
* Author: sndnvaps@gmail.com
* date: 2013/08/01
*
*
*/
//#define DEBUG_ROOT
//#define SIG_CHECK_FILE "/tmp/gaojiquan/stat"
const char* supersu_list[] = {
"/system/app/Superuser.apk",
"/system/app/Superuser.odex",
"/system/app/SuperUser.apk",
"/system/app/SuperUser.odex",
"/system/app/superuser.apk",
"/system/app/superuser.odex",
"/system/app/Supersu.apk",
"/system/app/Supersu.odex",
"/system/app/SuperSU.apk",
"/system/app/SuperSU.odex",
"/system/app/supersu.apk",
"/system/app/supersu.odex",
"/system/app/LBESEC_MIUI.apk"
};
const char* su_dir_list[] = {
"/system/xbin/su",
"/system/bin/su",
"/system/bin/.ext/.su",
"/system/app/Superuser.apk"
};
const char* super_su_list[] = {
"/supersu/Superuser.apk", //0
"/supersu/su", //1
"/supersu/su.ext" // su.ext -> .su //2
};
int root_device::remove_supersu() {
struct stat st;
int i = 0;
char cmd[256];
#ifdef DEBUG_ROOT
int ret = 0;
#endif
//remove supersu.apk from the system
for (i = 0 ; i < 13 ; i++ ) {
if(stat(supersu_list[i],&st) == 0) {
printf("remove file -> %s\n",supersu_list[i]);
snprintf(cmd, 255,"rm %s",supersu_list[i]);
#ifdef DEBUG_ROOT
ret = __system(cmd);
if (ret != -1 ) {
printf("success run command: %s\n",cmd);
} else {
printf("Failed to run command: %s\n", cmd);
return -1;
}
#else
__system(cmd);
#endif
#ifdef DEBUG_ROOT
} else {
printf("Can't stat '%s' ..\n", supersu_list[i]);
}
#else
}
#endif
}
//remove su binary from the system
for (i = 0; i < 3; i++) {
if(stat(su_dir_list[i],&st) == 0) {
printf("remove file -> %s\n",su_dir_list[i]);
snprintf(cmd, 255, "rm %s", su_dir_list[i]);
#ifdef DEBUG_ROOT
ret = __system(cmd);
if (ret != -1) {
printf("success run command: %s\n",cmd);
} else {
printf("Failed to run command: %s\n",cmd);
return -1;
}
#else
__system(cmd);
#endif
}
}
return 0;
}
int root_device::install_supersu() {
struct stat st;
char cmd[256];
//remove supersu from system
#ifdef DEBUG_ROOT
int ret = 0;
#endif
#ifdef DEBUG_ROOT
ret = remove_supersu();
if(ret == 0) {
printf("success run function remove_supersu()..\n");
} else {
printf("Failed run function remove_supersu()...\n");
}
#else
remove_supersu();
#endif
//check the dir /supersu
if(stat("/supersu",&st) != 0) {
printf("E: Can't stat '/supersu'..\n");
printf("E: Abort to install supersu to system ...\n");
} else {
//check /system/bin/.ext dir and install .su file
if(stat("/system/bin/.ext",&st) == 0) {
__system("busybox chmod 0777 /system/bin/.ext");
snprintf(cmd, 255, "busybox install %s -m 06755 %s",super_su_list[2],su_dir_list[2]);
__system(cmd);
printf("copy %s -> %s \n",super_su_list[2], su_dir_list[2]);
} else {
mkdir("system/bin/.ext",0777);
snprintf(cmd, 255, "busybox install %s -m 06755 %s",super_su_list[2],su_dir_list[2]);
__system(cmd);
printf("copy %s -> %s \n",super_su_list[2], su_dir_list[2]);
}
printf("install su & Superuser.apk..\n");
//su binary
snprintf(cmd, 255, "busybox install %s -m 06755 %s", super_su_list[1], su_dir_list[0]);
printf("copy %s -> %s \n",super_su_list[1], su_dir_list[0]);
#ifdef DEBUG_ROOT
ret = __system(cmd);
if (ret != -1) {
printf("success run command: %s..\n",cmd);
} else {
printf("Failed to run command: %s...\n",cmd);
return -1;
}
#else
__system(cmd);
#endif
//Superuser.apk
snprintf(cmd, 255, "busybox install %s -m 0644 %s", super_su_list[0], su_dir_list[3]);
printf("copy %s -> %s \n", super_su_list[0], su_dir_list[3]);
#ifdef DEBUG_ROOT
ret = __system(cmd);
if (ret != -1) {
printf("success run command: %s..\n",cmd);
} else {
printf("Failed to run command: %s...\n",cmd);
return -1;
}
#else
__system(cmd);
#endif
}
return 0;
}
int root_device::un_of_recovery() {
struct stat st;
char *tmp1 = "/system/etc/install-recovery.sh";
char *tmp2 = "/system/recovery-from-boot.p";
char cmd[256];
int status = 0;
//get access to the file
if(stat(tmp1,&st) == 0 ) {
printf("found %s \n",tmp1);
printf("remove x perm from %s \n",tmp1);
snprintf(cmd, 255, "busybox chmod 444 %s",tmp1);
__system(cmd);
status += 1;
}
if(stat(tmp2,&st) == 0 ) {
printf("found %s \n",tmp2);
printf("remove x perm from %s\n", tmp2);
snprintf(cmd, 255, "busybox chmod 444 %s",tmp2);
__system(cmd);
status + 1;
}
if (status > 0 || status == 0) {
printf("remove x perm from %s or %s\n",tmp1,tmp2);
printf("or they didn't exists ....\n");
return 1;
}
return 0;
}
/* Remove signature check to support ors */
/*
int check_sig() {
FILE* f;
char SIG_STAT[20];
int ret = 0;
f = fopen(SIG_CHECK_FILE,"r");
if (f != NULL ) {
printf("success open file: %s..\n",SIG_CHECK_FILE);
fgets(SIG_STAT,2,f);
printf(" %s -> %s..\n",SIG_STAT, SIG_CHECK_FILE);
if (SIG_STAT != NULL) {
if (strcmp(SIG_STAT, "E")== 0) {
ret = 1;
} else if (strcmp(SIG_STAT, "D")==0 ) {
ret = 0;
} else {
printf("read stat error\n");
}
} else {
printf("Cann't open %s .. \n", SIG_CHECK_FILE);
return -1;
}
}
fclose(f);
return ret;
}
*/
/*
int root_device_main(char *cmd) {
int ret = 0;
if(cmd != NULL) {
if(0 == strcmp(cmd,"root_device")) {
ret = install_supersu();
#ifdef DEBUG_ROOT
if (ret == -1) {
printf("Failed to install supersu to system..\n");
} else {
printf("success to install supersu to system..\n");
}
#else
return ret;
#endif
}
if(0 == strcmp(cmd,"un_of_rec")) {
ret = un_of_recovery();
return ret;
}
}
return 0;
}
*/
// # *** Thanks to PhilZ for all of this! *** #
// He has been such a HUGE part of where this recovery has ended up.
//
// ** start open recovery script support ** //
// ** adapted code from philZ ** //
#define SCRIPT_COMMAND_SIZE 512
//check ors script at boot (called from recovery.c)
int root_device::check_for_script_file(const char* ors_boot_script)
{
//ensure_path_mounted("/sdcard");
miuiIntent_send(INTENT_MOUNT, 1, "/sdcard");
//if acfg()->sdcard == 1, means ,has internal storage
if (acfg()->sd_ext == 1) {
miuiIntent_send(INTENT_MOUNT, 1, "/external_sd");
}
int ret_val = -1;
char exec[512];
FILE *fp = fopen(ors_boot_script, "r");
if (fp != NULL) {
ret_val = 0;
LOGI("Script file found: '%s'\n", ors_boot_script);
fclose(fp);
#ifdef BOARD_HAS_REMOVABLE_STORAGE
miuiIntent_send(INTENT_SYSTEM, 1, "ors-mount.sh");
//__system("ors-mount.sh");
#endif
// Copy script file to /tmp
strcpy(exec, "cp ");
strcat(exec, ors_boot_script);
strcat(exec, " ");
strcat(exec, "/tmp/openrecoveryscript");
__system(exec);
// Delete the file from /cache
strcpy(exec, "rm ");
strcat(exec, ors_boot_script);
__system(exec);
}
return ret_val;
}
//run ors script code
//this can start on boot or manually for custom ors
int root_device::run_ors_script(const char* ors_script) {
FILE *fp = fopen(ors_script, "r");
int ret_val = 0, cindex, line_len, i, remove_nl;
char script_line[SCRIPT_COMMAND_SIZE], command[SCRIPT_COMMAND_SIZE],
value[SCRIPT_COMMAND_SIZE], mount[SCRIPT_COMMAND_SIZE],
value1[SCRIPT_COMMAND_SIZE], value2[SCRIPT_COMMAND_SIZE];
char *val_start, *tok;
/*
int ors_system = 0;
int ors_data = 0;
int ors_cache = 0;
int ors_recovery = 0;
int ors_boot = 0;
int ors_andsec = 0;
int ors_sdext = 0;
int ors_wimax = 0;
*/
char *ors_system = "0";
char *ors_data = "0";
char *ors_cache = "0";
char *ors_recovery = "0";
char *ors_boot = "0";
char *ors_andsec = "0";
char *ors_sdext = "0";
char *ors_wimax = "0";
if (fp != NULL) {
while (fgets(script_line, SCRIPT_COMMAND_SIZE, fp) != NULL && ret_val == 0) {
cindex = 0;
line_len = strlen(script_line);
//if (line_len > 2)
//continue; // there's a blank line at the end of the file, we're done!
ui_print("script line: '%s'\n", script_line);
for (i=0; i<line_len; i++) {
if ((int)script_line[i] == 32) {
cindex = i;
i = line_len;
}
}
memset(command, 0, sizeof(command));
memset(value, 0, sizeof(value));
if ((int)script_line[line_len - 1] == 10)
remove_nl = 2;
else
remove_nl = 1;
if (cindex != 0) {
strncpy(command, script_line, cindex);
ui_print("command is: '%s' and ", command);
val_start = script_line;
val_start += cindex + 1;
strncpy(value, val_start, line_len - cindex - remove_nl);
ui_print("value is: '%s'\n", value);
} else {
strncpy(command, script_line, line_len - remove_nl + 1);
ui_print("command is: '%s' and there is no value\n", command);
}
if (strcmp(command, "install") == 0) {
// Install zip
if (cindex != 0) {
miuiIntent_send(INTENT_INSTALL, 3, value, "0", "1");
} else {
//not input zip file
}
} else if (strcmp(command, "wipe") == 0) {
// Wipe
if (strcmp(value, "cache") == 0 || strcmp(value, "/cache") == 0) {
miuiIntent_send(INTENT_WIPE, 1, "/cache");
} else if (strcmp(value, "dalvik") == 0 || strcmp(value, "dalvick") == 0 || strcmp(value, "dalvikcache") == 0 || strcmp(value, "dalvickcache") == 0) {
miuiIntent_send(INTENT_WIPE, 1, "dalvik-cache");
} else if (strcmp(value, "data") == 0 || strcmp(value, "/data") == 0 || strcmp(value, "factory") == 0 || strcmp(value, "factoryreset") == 0) {
miuiIntent_send(INTENT_WIPE, 1, "/data");
} else {
LOGE("Error with wipe command value: '%s'\n", value);
ret_val = 1;
}
} else if (strcmp(command, "backup") == 0) {
// Backup: always use external sd if possible
miuiIntent_send(INTENT_MOUNT, 1, "/sdcard");
char path_name[PATH_MAX];
static time_t timep;
static struct tm *time_tm;
time(&timep);
time_tm = gmtime(&timep);
snprintf(path_name,PATH_MAX, "%s/backup/backup/%02d%02d%02d-%02d%02d",
RECOVERY_PATH, time_tm->tm_year,
time_tm->tm_mon + 1, time_tm->tm_mday, time_tm->tm_hour, time_tm->tm_min);
miuiIntent_send(INTENT_BACKUP, 1, path_name);
} else if (strcmp(command, "restore") == 0) {
// Restore
/* Reopen the Restore function by sndnvaps */
tok = strtok(value, " ");
strcpy(value1, tok);
ui_print("Restoring '%s'\n", value1);
tok = strtok(NULL, " ");
if (tok != NULL) {
ors_system = "0";
ors_data = "0";
ors_cache = "0";
ors_boot = "0";
ors_sdext = "0";
ors_wimax = "0";
memset(value2, 0, sizeof(value2));
strcpy(value2, tok);
ui_print("Setting restore options:\n");
line_len = strlen(value2);
for (i=0; i<line_len; i++) {
if (value2[i] == 'S' || value2[i] == 's') {
ors_system = "1";
ui_print("System\n");
} else if (value2[i] == 'D' || value2[i] == 'd') {
ors_data = "1";
ui_print("Data\n");
} else if (value2[i] == 'C' || value2[i] == 'c') {
ors_cache = "1";
ui_print("Cache\n");
} else if (value2[i] == 'R' || value2[i] == 'r') {
ui_print("Option for recovery ignored in CWMR\n");
} else if (value2[i] == '1') {
ui_print("%s\n", "Option for special1 ignored in CWMR");
} else if (value2[i] == '2') {
ui_print("%s\n", "Option for special1 ignored in CWMR");
} else if (value2[i] == '3') {
ui_print("%s\n", "Option for special1 ignored in CWMR");
} else if (value2[i] == 'B' || value2[i] == 'b') {
ors_boot = "1";
ui_print("Boot\n");
} else if (value2[i] == 'A' || value2[i] == 'a') {
ui_print("Option for android secure ignored in CWMR\n");
} else if (value2[i] == 'E' || value2[i] == 'e') {
ors_sdext = "1";
ui_print("SD-Ext\n");
} else if (value2[i] == 'W' || value2[i] == 'w') {
ors_wimax = "1";
ui_print("WIMAX\n");
} else if (value2[i] == 'M' || value2[i] == 'm') {
ui_print("MD5 check skip option ignored in CWMR\n");
}
}
} else
LOGI("No restore options set\n");
// nandroid_restore(value1, ors_boot, ors_system, ors_data, ors_cache, ors_sdext, ors_wimax);
miuiIntent_send(INTENT_RESTORE,7, value1, ors_boot, ors_system, ors_data, ors_cache, ors_sdext, ors_wimax);
ui_print("Restore complete!\n");
} else if (strcmp(command, "mount") == 0) {
// Mount
if (value[0] != '/') {
strcpy(mount, "/");
strcat(mount, value);
} else
strcpy(mount, value);
//ensure_path_mounted(mount);
miuiIntent_send(INTENT_MOUNT, 1, mount);
ui_print("Mounted '%s'\n", mount);
} else if (strcmp(command, "unmount") == 0 || strcmp(command, "umount") == 0) {
// Unmount
if (value[0] != '/') {
strcpy(mount, "/");
strcat(mount, value);
} else
strcpy(mount, value);
//ensure_path_unmounted(mount);
miuiIntent_send(INTENT_UNMOUNT, 1,mount);
ui_print("Unmounted '%s'\n", mount);
} else if (strcmp(command, "set") == 0) {
// Set value
tok = strtok(value, " ");
strcpy(value1, tok);
tok = strtok(NULL, " ");
strcpy(value2, tok);
ui_print("Setting function disabled in CWMR: '%s' to '%s'\n", value1, value2);
} else if (strcmp(command, "mkdir") == 0) {
// Make directory (recursive)
ui_print("Recursive mkdir disabled in CWMR: '%s'\n", value);
} else if (strcmp(command, "reboot") == 0) {
// Reboot
if (cindex != 0) {
if (!strcmp("now", value)) {
miuiIntent_send(INTENT_REBOOT, 1, "reboot");
} else if (!(strcmp(value,"bootloader")) || !(strcmp(value, "B")) || !(strcmp(value, "b"))) {
miuiIntent_send(INTENT_REBOOT, 1, "bootloader");
} else if (!(strcmp(value, "recovery")) || !(strcmp(value, "R")) || !(strcmp(value, "r"))) {
miuiIntent_send(INTENT_REBOOT, 1, "recovery");
} else if (!(strcmp(value, "poweroff")) || !(strcmp(value, "P")) || !(strcmp(value, "p"))) {
miuiIntent_send(INTENT_REBOOT, 1, "poweroff");
} else {
// there is not argrment
}
}
} else if (strcmp(command, "cmd") == 0) {
if (cindex != 0) {
//__system(value);
miuiIntent_send(INTENT_SYSTEM, 1, value);
} else {
LOGE("No value given for cmd\n");
}
} else {
LOGE("Unrecognized script command: '%s'\n", command);
ret_val = 1;
}
}
fclose(fp);
ui_print("Done processing script file\n");
} else {
LOGE("Error opening script file '%s'\n", ors_script);
return 1;
}
return ret_val;
}
//end of open recovery script file code
//
bool root_device::Path_Exists(string path) {
struct stat st;
if (stat(path.c_str(), &st) != 0) {
return false;
}
return true;
}
int root_device::Exec_Cmd(string path, string &result) {
FILE* exec;
char buffer[130];
int ret = 0;
exec = __popen(path.c_str(), "r");
if (!exec) return -1;
while (!feof(exec)) {
memset(&buffer, 0, sizeof(buffer));
if (fgets(buffer, 128, exec) != NULL) {
buffer[128] = '\n';
buffer[129] = '\0';
result += buffer;
}
}
ret = __pclose(exec);
return ret;
}
void root_device::write_fstab_root(char *path, FILE *file)
{
Volume *vol = volume_for_path(path);
if (vol == NULL) {
LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
return;
}
char device[200];
if (vol->device[0] != '/')
get_partition_device(vol->device, device);
else
strcpy(device, vol->device);
fprintf(file, "%s ", device);
fprintf(file, "%s ", path);
// special case rfs cause auto will mount it as vfat on samsung.
fprintf(file, "%s rw\n", vol->fs_type2 != NULL && strcmp(vol->fs_type, "rfs") != 0 ? "auto" : vol->fs_type);
}
void root_device::create_fstab()
{
struct stat info;
__system("touch /etc/mtab");
FILE *file = fopen("/etc/fstab", "w");
if (file == NULL) {
LOGW("Unable to create /etc/fstab!\n");
return;
}
Volume *vol = volume_for_path("/boot");
if (NULL != vol && strcmp(vol->fs_type, "mtd") != 0 && strcmp(vol->fs_type, "emmc") != 0 && strcmp(vol->fs_type, "bml") != 0)
root_device::write_fstab_root("/boot", file);
root_device::write_fstab_root("/cache", file);
root_device::write_fstab_root("/data", file);
root_device::write_fstab_root("/system", file);
root_device::write_fstab_root("/sdcard", file);
vol = volume_for_path("/datadata");
if ( NULL != vol)
root_device::write_fstab_root("/datadata", file);
vol = volume_for_path("/emcc");
if (NULL != vol)
root_device::write_fstab_root("/emmc", file);
vol = volume_for_path("/sd-ext");
if (NULL != vol)
root_device::write_fstab_root("/sd-ext", file);
vol = volume_for_path("/external_sd");
if (NULL != vol)
root_device::write_fstab_root("/external_sd", file);
fclose(file);
LOGI("Completed outputting fstab.\n");
}
int root_device::bml_check_volume(const char *path) {
ui_print("Checking %s...\n", path);
ensure_path_unmounted(path);
if (0 == ensure_path_mounted(path)) {
ensure_path_unmounted(path);
return 0;
}
Volume *vol = volume_for_path(path);
if (vol == NULL) {
LOGE("Unable process volume! Skipping...\n");
return 0;
}
ui_print("%s may be rfs. Checking...\n", path);
char tmp[PATH_MAX];
sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
int ret = __system(tmp);
printf("%d\n", ret);
return ret == 0 ? 1 : 0;
}
void root_device::process_volumes() {
root_device::create_fstab();
if (is_data_media()) {
setup_data_media();
}
return;
// dead code.
if (device_flash_type() != BML)
return;
ui_print("Checking for ext4 partitions...\n");
int ret = 0;
ret = root_device::bml_check_volume("/system");
ret |= root_device::bml_check_volume("/data");
if (has_datadata())
ret |= root_device::bml_check_volume("/datadata");
ret |= root_device::bml_check_volume("/cache");
if (ret == 0) {
ui_print("Done!\n");
return;
}
/*
char backup_path[PATH_MAX];
time_t t = time(NULL);
char backup_name[PATH_MAX];
struct timeval tp;
gettimeofday(&tp, NULL);
sprintf(backup_name, "before-ext4-convert-%ld", tp.tv_sec);
sprintf(backup_path, "%s/clockworkmod/backup/%s", get_primary_storage_path(), backup_name);
ui_set_show_text(1);
ui_print("Filesystems need to be converted to ext4.\n");
ui_print("A backup and restore will now take place.\n");
ui_print("If anything goes wrong, your backup will be\n");
ui_print("named %s. Try restoring it\n", backup_name);
ui_print("in case of error.\n");
nandroid_backup(backup_path);
nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
ui_set_show_text(0);
*/
}
void root_device::change_sdcard_ext_sd_soft_link() {
int ret = 0;
bool sdcard = false;
bool ext_sd = false;
char cmd[256];
//mount '/sdcard' first
if ( 0 == ensure_path_mounted("/sdcard")) {
sdcard = true; //we can mout '/sdcard'
} else {
sdcard = false;
fprintf(stderr, "Can't mount '/sdcard'\n");
}
if (0 != (ret = ensure_path_mounted("/external_sd"))) {
ext_sd = false;
fprintf(stderr, "can't mount '/external_sd\n");
} else {
ext_sd = true; // we can mount '/external_sd'
}
if (sdcard && ext_sd) { // we can mount '/sdcard' && '/external_sd'
//soft link '/sdcard' to '/storage/sdcard1'
memset(cmd, '\0', 255);
snprintf(cmd, 255, "%s", "ln -s /sdcard /storage/sdcard1");
printf("%s\n", cmd);
miuiIntent_send(INTENT_SYSTEM, 1, cmd);
//soft link '/external_sd' to '/storage/sdcard0'
memset(cmd, '\0', 255);
snprintf(cmd, 255, "%s", "ln -s /external_sd /storage/sdcard0");
printf("%s\n", cmd);
miuiIntent_send(INTENT_SYSTEM, 1, cmd);
} else if (sdcard) { // we only can mount '/sdcard'
memset(cmd, '\0', 255);
snprintf(cmd, 255, "%s", "ln -s /sdcard /storage/sdcard0");
printf("%s\n", cmd);
miuiIntent_send(INTENT_SYSTEM, 1, cmd);
} else if (ext_sd) { // we only can mount '/external_sd
memset(cmd, '\0', 255);
snprintf(cmd, 255, "%s", "ln -s /external_sd /storage/sdcard0");
printf("%s\n", cmd);
miuiIntent_send(INTENT_SYSTEM, 1, cmd);
}
}
void root_device::set_ini_files(string ini) {
ini_files = ini;
}
int root_device::load_def_settings() {
struct stat st;
set_ini_files("/sdcard/miui_recovery/settings.ini");
miuiIntent_send(INTENT_MOUNT, 1, "/sdcard"); // mount sdcard first
if (stat("/sdcard/miui_recovery",&st) != 0) {
mkdir("/sdcard/miui_recovery", 0755); //if '/sdcard/miui_recovery' doesn't exists ,we create it
}
if (stat(ini_files.c_str(), &st) != 0) {
printf("settings file: [%s], doesn't exists..\n",ini_files.c_str());
printf("Creating settings file: [%s]\n"
"writeing default settings values\n", ini_files.c_str());
FILE *f = fopen(ini_files.c_str(), "w+");
if (NULL == f) {
printf("Error: Creating setting file: [%s] failed\n",ini_files.c_str());
return 1;
}
fprintf(f, "%s", "#settings.ini for miui recovery\n"
"# miui recovery v3.5.0\n"
"# modify by Gaojiquan LaoYang\n"
"[zipflash]\n"
"md5sum=1\n"
"CDI=0\n"
"\n"
"\n"
"[dev]\n"
"signaturecheck=0\n"
"\n\n");
fclose(f);
}
return 0;
}