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>
17 #include <sys/reboot.h>
18 #include <libubox/ulog.h>
29 #include <mtd/ubi-user.h>
31 #include "libfstools/libfstools.h"
32 #include "libfstools/volume.h"
34 static int jffs2_mark(struct volume
*v
);
39 ULOG_WARN("This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
45 static int jffs2_reset(struct volume
*v
, int reset
, int keep
)
49 mp
= find_mount_point(v
->blk
, 1);
51 ULOG_INFO("%s is mounted as %s, only erasing files\n", v
->blk
, mp
);
52 fs_state_set("/overlay", FS_STATE_PENDING
);
53 overlay_delete(mp
, keep
);
54 mount(mp
, "/", NULL
, MS_REMOUNT
, 0);
56 ULOG_INFO("%s is not mounted\n", v
->blk
);
71 static int jffs2_mark(struct volume
*v
)
73 __u32 deadc0de
= __cpu_to_be32(0xdeadc0de);
77 fd
= open(v
->blk
, O_WRONLY
);
78 ULOG_INFO("%s will be erased on next mount\n", v
->blk
);
80 ULOG_ERR("opening %s failed\n", v
->blk
);
84 if (volume_identify(v
) == FS_UBIFS
) {
86 int ret
= ioctl(fd
, UBI_IOCVOLUP
, &llz
);
91 sz
= write(fd
, &deadc0de
, sizeof(deadc0de
));
95 ULOG_ERR("writing %s failed: %m\n", v
->blk
);
102 int main(int argc
, char **argv
)
105 int ch
, yes
= 0, reset
= 0, keep
= 0;
106 while ((ch
= getopt(argc
, argv
, "yrk")) != -1) {
121 if (!yes
&& ask_user())
125 * TODO: Currently this only checks if kernel supports OverlayFS. We
126 * should check if there is a mount point using it with rootfs_data
129 if (find_filesystem("overlay")) {
130 ULOG_ERR("overlayfs not supported by kernel\n");
134 v
= volume_find("rootfs_data");
136 ULOG_ERR("MTD partition 'rootfs_data' not found\n");
141 if (!strcmp(*argv
, "jffs2mark"))
142 return jffs2_mark(v
);
143 return jffs2_reset(v
, reset
, keep
);