switch to _DEFAULT_SOURCE for modern glibc compat
[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 <libubox/ulog.h>
19
20 #include <fcntl.h>
21 #include <dirent.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "libfstools/libfstools.h"
28 #include "libfstools/volume.h"
29
30 static int
31 handle_rmdir(const char *dir)
32 {
33 struct dirent *dt;
34 struct stat st;
35 DIR *d;
36 int fd;
37
38 d = opendir(dir);
39 if (!d)
40 return -1;
41
42 fd = dirfd(d);
43
44 while ((dt = readdir(d)) != NULL) {
45 if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode))
46 continue;
47
48 unlinkat(fd, dt->d_name, 0);
49 }
50
51 closedir(d);
52 rmdir(dir);
53
54 return 0;
55 }
56
57 static int
58 ask_user(int argc, char **argv)
59 {
60 if ((argc < 2) || strcmp(argv[1], "-y")) {
61 ULOG_WARN("This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
62 if (getchar() != 'y')
63 return -1;
64 }
65 return 0;
66
67 }
68
69 static int
70 jffs2_reset(int argc, char **argv)
71 {
72 struct volume *v;
73 char *mp;
74
75 if (ask_user(argc, argv))
76 return -1;
77
78 if (find_filesystem("overlay")) {
79 ULOG_ERR("overlayfs not supported by kernel\n");
80 return -1;
81 }
82
83 v = volume_find("rootfs_data");
84 if (!v) {
85 ULOG_ERR("MTD partition 'rootfs_data' not found\n");
86 return -1;
87 }
88
89 mp = find_mount_point(v->blk, 1);
90 if (mp) {
91 ULOG_INFO("%s is mounted as %s, only erasing files\n", v->blk, mp);
92 foreachdir(mp, handle_rmdir);
93 mount(mp, "/", NULL, MS_REMOUNT, 0);
94 } else {
95 ULOG_INFO("%s is not mounted, erasing it\n", v->blk);
96 volume_erase_all(v);
97 }
98
99 return 0;
100 }
101
102 static int
103 jffs2_mark(int argc, char **argv)
104 {
105 __u32 deadc0de = __cpu_to_be32(0xdeadc0de);
106 struct volume *v;
107 size_t sz;
108 int fd;
109
110 if (ask_user(argc, argv))
111 return -1;
112
113 v = volume_find("rootfs_data");
114 if (!v) {
115 ULOG_ERR("MTD partition 'rootfs_data' not found\n");
116 return -1;
117 }
118
119 fd = open(v->blk, O_WRONLY);
120 ULOG_INFO("%s - marking with deadc0de\n", v->blk);
121 if (!fd) {
122 ULOG_ERR("opening %s failed\n", v->blk);
123 return -1;
124 }
125
126 sz = write(fd, &deadc0de, sizeof(deadc0de));
127 close(fd);
128
129 if (sz != 4) {
130 ULOG_ERR("writing %s failed: %s\n", v->blk, strerror(errno));
131 return -1;
132 }
133
134 return 0;
135 }
136
137 int main(int argc, char **argv)
138 {
139 if (!strcmp(*argv, "jffs2mark"))
140 return jffs2_mark(argc, argv);
141 return jffs2_reset(argc, argv);
142 }