1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/reboot.h>
6 #include <sys/wait.h>
7 #include <cutils/android_reboot.h>
8 #include <cutils/partition_utils.h>
9
10 const char *mkfs = "/system/bin/make_ext4fs";
11
setup_fs(const char * blockdev)12 int setup_fs(const char *blockdev)
13 {
14 char buf[256], path[128];
15 pid_t child;
16 int status, n;
17 pid_t pid;
18
19 /* we might be looking at an indirect reference */
20 n = readlink(blockdev, path, sizeof(path) - 1);
21 if (n > 0) {
22 path[n] = 0;
23 if (!memcmp(path, "/dev/block/", 11))
24 blockdev = path + 11;
25 }
26
27 if (strchr(blockdev,'/')) {
28 fprintf(stderr,"not a block device name: %s\n", blockdev);
29 return 0;
30 }
31
32 snprintf(buf, sizeof(buf), "/sys/fs/ext4/%s", blockdev);
33 if (access(buf, F_OK) == 0) {
34 fprintf(stderr,"device %s already has a filesystem\n", blockdev);
35 return 0;
36 }
37 snprintf(buf, sizeof(buf), "/dev/block/%s", blockdev);
38
39 if (!partition_wiped(buf)) {
40 fprintf(stderr,"device %s not wiped, probably encrypted, not wiping\n", blockdev);
41 return 0;
42 }
43
44 fprintf(stderr,"+++\n");
45
46 child = fork();
47 if (child < 0) {
48 fprintf(stderr,"error: setup_fs: fork failed\n");
49 return 0;
50 }
51 if (child == 0) {
52 execl(mkfs, mkfs, buf, NULL);
53 exit(-1);
54 }
55
56 while ((pid=waitpid(-1, &status, 0)) != child) {
57 if (pid == -1) {
58 fprintf(stderr, "error: setup_fs: waitpid failed!\n");
59 return 1;
60 }
61 }
62
63 fprintf(stderr,"---\n");
64 return 1;
65 }
66
67
main(int argc,char ** argv)68 int main(int argc, char **argv)
69 {
70 int need_reboot = 0;
71
72 while (argc > 1) {
73 if (strlen(argv[1]) < 128)
74 need_reboot |= setup_fs(argv[1]);
75 argv++;
76 argc--;
77 }
78
79 if (need_reboot) {
80 fprintf(stderr,"REBOOT!\n");
81 android_reboot(ANDROID_RB_RESTART, 0, 0);
82 exit(-1);
83 }
84 return 0;
85 }
86