cosmetics
[project/fstools.git] / jffs2reset.c
1 /*
2  * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <sys/mount.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17
18 #include <fcntl.h>
19 #include <dirent.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "libfstools/libfstools.h"
26 #include "libfstools/volume.h"
27
28 static int
29 handle_rmdir(const char *dir)
30 {
31         struct stat s;
32         struct dirent **namelist;
33         int n;
34
35         n = scandir(dir, &namelist, NULL, NULL);
36
37         if (n < 1)
38                 return -1;
39
40         while (n--) {
41                 char file[256];
42
43                 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
44                 if (!lstat(file, &s) && !S_ISDIR(s.st_mode))
45                         unlink(file);
46                 free(namelist[n]);
47         }
48         free(namelist);
49
50         rmdir(dir);
51
52         return 0;
53 }
54
55 static int
56 ask_user(int argc, char **argv)
57 {
58         if ((argc < 2) || strcmp(argv[1], "-y")) {
59                 fprintf(stderr, "This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
60                 if (getchar() != 'y')
61                         return -1;
62         }
63         return 0;
64
65 }
66
67 static int
68 jffs2_reset(int argc, char **argv)
69 {
70         struct volume *v;
71         char *mp;
72
73         if (ask_user(argc, argv))
74                 return -1;
75
76         if (find_filesystem("overlay")) {
77                 fprintf(stderr, "overlayfs not found\n");
78                 return -1;
79         }
80
81         v = volume_find("rootfs_data");
82         if (!v) {
83                 fprintf(stderr, "no rootfs_data was found\n");
84                 return -1;
85         }
86
87         mp = find_mount_point(v->blk, "jffs2");
88         if (mp) {
89                 fprintf(stderr, "%s is mounted as %s, only erasing files\n", v->blk, mp);
90                 foreachdir(mp, handle_rmdir);
91                 mount(mp, "/", NULL, MS_REMOUNT, 0);
92         } else {
93                 fprintf(stderr, "%s is not mounted, erasing it\n", v->blk);
94                 volume_erase_all(v);
95         }
96
97         return 0;
98 }
99
100 static int
101 jffs2_mark(int argc, char **argv)
102 {
103         __u32 deadc0de = __cpu_to_be32(0xdeadc0de);
104         struct volume *v;
105         size_t sz;
106         int fd;
107
108         if (ask_user(argc, argv))
109                 return -1;
110
111         v = volume_find("rootfs_data");
112         if (!v) {
113                 fprintf(stderr, "no rootfs_data was found\n");
114                 return -1;
115         }
116
117         fd = open(v->blk, O_WRONLY);
118         fprintf(stderr, "%s - marking with deadc0de\n", v->blk);
119         if (!fd) {
120                 fprintf(stderr, "opening %s failed\n", v->blk);
121                 return -1;
122         }
123
124         sz = write(fd, &deadc0de, sizeof(deadc0de));
125         close(fd);
126
127         if (sz != 4) {
128                 fprintf(stderr, "writing %s failed: %s\n", v->blk, strerror(errno));
129                 return -1;
130         }
131
132         return 0;
133 }
134
135 int main(int argc, char **argv)
136 {
137         if (!strcmp(*argv, "jffs2mark"))
138                 return jffs2_mark(argc, argv);
139         return jffs2_reset(argc, argv);
140 }