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