2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
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
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.
14 #include <sys/mount.h>
15 #include <sys/types.h>
25 #include "libfstools/libfstools.h"
26 #include "libfstools/volume.h"
29 handle_rmdir(const char *dir)
32 struct dirent **namelist;
35 n = scandir(dir, &namelist, NULL, NULL);
43 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
44 if (!lstat(file, &s) && !S_ISDIR(s.st_mode))
56 ask_user(int argc, char **argv)
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");
68 jffs2_reset(int argc, char **argv)
73 if (ask_user(argc, argv))
76 if (find_filesystem("overlay")) {
77 fprintf(stderr, "overlayfs not found\n");
81 v = volume_find("rootfs_data");
83 fprintf(stderr, "no rootfs_data was found\n");
87 mp = find_mount_point(v->blk, "jffs2");
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);
93 fprintf(stderr, "%s is not mounted, erasing it\n", v->blk);
101 jffs2_mark(int argc, char **argv)
103 __u32 deadc0de = __cpu_to_be32(0xdeadc0de);
108 if (ask_user(argc, argv))
111 v = volume_find("rootfs_data");
113 fprintf(stderr, "no rootfs_data was found\n");
117 fd = open(v->blk, O_WRONLY);
118 fprintf(stderr, "%s - marking with deadc0de\n", v->blk);
120 fprintf(stderr, "opening %s failed\n", v->blk);
124 sz = write(fd, &deadc0de, sizeof(deadc0de));
128 fprintf(stderr, "writing %s failed: %s\n", v->blk, strerror(errno));
135 int main(int argc, char **argv)
137 if (!strcmp(*argv, "jffs2mark"))
138 return jffs2_mark(argc, argv);
139 return jffs2_reset(argc, argv);