jffs2reset: use jffs2_mark if rootfs_data isn't mounted
[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 jffs2_mark(struct volume *v);
31
32 static int
33 ask_user(int argc, char **argv)
34 {
35 if ((argc < 2) || strcmp(argv[1], "-y")) {
36 ULOG_WARN("This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
37 if (getchar() != 'y')
38 return -1;
39 }
40 return 0;
41
42 }
43
44 static int jffs2_reset(struct volume *v)
45 {
46 char *mp;
47
48 mp = find_mount_point(v->blk, 1);
49 if (mp) {
50 ULOG_INFO("%s is mounted as %s, only erasing files\n", v->blk, mp);
51 fs_state_set("/overlay", FS_STATE_PENDING);
52 overlay_delete(mp, false);
53 mount(mp, "/", NULL, MS_REMOUNT, 0);
54 } else {
55 ULOG_INFO("%s is not mounted\n", v->blk);
56 return jffs2_mark(v);
57 }
58
59 return 0;
60 }
61
62 static int jffs2_mark(struct volume *v)
63 {
64 __u32 deadc0de = __cpu_to_be32(0xdeadc0de);
65 size_t sz;
66 int fd;
67
68 fd = open(v->blk, O_WRONLY);
69 ULOG_INFO("%s will be erased on next mount\n", v->blk);
70 if (!fd) {
71 ULOG_ERR("opening %s failed\n", v->blk);
72 return -1;
73 }
74
75 sz = write(fd, &deadc0de, sizeof(deadc0de));
76 close(fd);
77
78 if (sz != 4) {
79 ULOG_ERR("writing %s failed: %s\n", v->blk, strerror(errno));
80 return -1;
81 }
82
83 return 0;
84 }
85
86 int main(int argc, char **argv)
87 {
88 struct volume *v;
89
90 if (ask_user(argc, argv))
91 return -1;
92
93 /*
94 * TODO: Currently this only checks if kernel supports OverlayFS. We
95 * should check if there is a mount point using it with rootfs_data
96 * as upperdir.
97 */
98 if (find_filesystem("overlay")) {
99 ULOG_ERR("overlayfs not supported by kernel\n");
100 return -1;
101 }
102
103 v = volume_find("rootfs_data");
104 if (!v) {
105 ULOG_ERR("MTD partition 'rootfs_data' not found\n");
106 return -1;
107 }
108
109 if (!strcmp(*argv, "jffs2mark"))
110 return jffs2_mark(v);
111 return jffs2_reset(v);
112 }