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>
18 #include <libubox/ulog.h>
27 #include "libfstools/libfstools.h"
28 #include "libfstools/volume.h"
31 handle_rmdir(const char *dir)
44 while ((dt = readdir(d)) != NULL) {
45 if (fstatat(fd, dt->d_name, &st, AT_SYMLINK_NOFOLLOW) || S_ISDIR(st.st_mode))
48 unlinkat(fd, dt->d_name, 0);
58 ask_user(int argc, char **argv)
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");
70 jffs2_reset(int argc, char **argv)
75 if (ask_user(argc, argv))
78 if (find_filesystem("overlay")) {
79 ULOG_ERR("overlayfs not supported by kernel\n");
83 v = volume_find("rootfs_data");
85 ULOG_ERR("MTD partition 'rootfs_data' not found\n");
89 mp = find_mount_point(v->blk, 1);
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);
95 ULOG_INFO("%s is not mounted, erasing it\n", v->blk);
103 jffs2_mark(int argc, char **argv)
105 __u32 deadc0de = __cpu_to_be32(0xdeadc0de);
110 if (ask_user(argc, argv))
113 v = volume_find("rootfs_data");
115 ULOG_ERR("MTD partition 'rootfs_data' not found\n");
119 fd = open(v->blk, O_WRONLY);
120 ULOG_INFO("%s - marking with deadc0de\n", v->blk);
122 ULOG_ERR("opening %s failed\n", v->blk);
126 sz = write(fd, &deadc0de, sizeof(deadc0de));
130 ULOG_ERR("writing %s failed: %s\n", v->blk, strerror(errno));
137 int main(int argc, char **argv)
139 if (!strcmp(*argv, "jffs2mark"))
140 return jffs2_mark(argc, argv);
141 return jffs2_reset(argc, argv);