feb37d2913de3220d579018972c0cb1508f6881a
[project/fstools.git] / backend / jffs2.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/stat.h>
15 #include <sys/types.h>
16 #include <sys/mount.h>
17
18 #include <asm/byteorder.h>
19
20 #include <errno.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <glob.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29
30 #include "../fs-state.h"
31 #include "../lib/mtd.h"
32
33 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
34
35 void
36 foreachdir(const char *dir, int (*cb)(const char*))
37 {
38         char globdir[256];
39         glob_t gl;
40         int j;
41
42         if (dir[strlen(dir) - 1] == '/')
43                 snprintf(globdir, 256, "%s*", dir);
44         else
45                 snprintf(globdir, 256, "%s/*", dir);
46
47         if (!glob(globdir, GLOB_NOESCAPE | GLOB_MARK | GLOB_ONLYDIR, NULL, &gl))
48                 for (j = 0; j < gl.gl_pathc; j++)
49                         foreachdir(gl.gl_pathv[j], cb);
50
51         cb(dir);
52 }
53
54 static int
55 jffs2_mount(void)
56 {
57         char rootfs_data[32];
58         int fd;
59
60         if (mkdir("/tmp/overlay", 0755)) {
61                 fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
62                 return -1;
63         }
64
65         if (find_mtd_block("rootfs_data", rootfs_data, sizeof(rootfs_data))) {
66                 fprintf(stderr, "rootfs_data does not exist\n");
67                 return -1;
68         }
69
70         if (mount(rootfs_data, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
71                 fprintf(stderr, "failed to mount -t jffs2 %s /tmp/overlay: %s\n", rootfs_data, strerror(errno));
72                 return -1;
73         }
74
75         find_mtd_char("rootfs_data", rootfs_data, sizeof(rootfs_data));
76
77         fd = mtd_load(rootfs_data);
78         if (fd > 0) {
79                 int ret = mtd_unlock(fd);
80                 close(fd);
81                 return ret;
82         }
83
84         return -1;
85 }
86
87 static int
88 switch2jffs(void)
89 {
90         struct stat s;
91         char mtd[32];
92         int ret;
93
94         if (!stat(SWITCH_JFFS2, &s)) {
95                 fprintf(stderr, "jffs2 switch already running\n");
96                 return -1;
97         }
98
99         if (!find_mtd_block("rootfs_patches", mtd, sizeof(mtd)))
100                 return 0;
101
102         if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
103                 fprintf(stderr, "no rootfs_data was found\n");
104                 return -1;
105         }
106
107         creat("/tmp/.switch_jffs2", 0600);
108         ret = mount(mtd, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
109         unlink("/tmp/.switch_jffs2");
110         if (ret) {
111                 fprintf(stderr, "failed - mount -t jffs2 %s /rom/overlay: %s\n", mtd, strerror(errno));
112                 return -1;
113         }
114
115         if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
116                 fprintf(stderr, "failed - mount -o remount,ro none: %s\n", strerror(errno));
117                 return -1;
118         }
119
120         system("cp -a /tmp/root/* /rom/overlay");
121
122         if (pivot("/rom", "/mnt")) {
123                 fprintf(stderr, "failed - pivot /rom /mnt: %s\n", strerror(errno));
124                 return -1;
125         }
126
127         if (mount_move("/mnt", "/tmp/root", "")) {
128                 fprintf(stderr, "failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
129                 return -1;
130         }
131
132         return fopivot("/overlay", "/rom");
133 }
134
135 int
136 handle_whiteout(const char *dir)
137 {
138         struct stat s;
139         char link[256];
140         ssize_t sz;
141         struct dirent **namelist;
142         int n;
143
144         n = scandir(dir, &namelist, NULL, NULL);
145
146         if (n < 1)
147                 return -1;
148
149         while (n--) {
150                 char file[256];
151
152                 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
153                 if (!lstat(file, &s) && S_ISLNK(s.st_mode)) {
154                         sz = readlink(file, link, sizeof(link) - 1);
155                         if (sz > 0) {
156                                 char *orig;
157
158                                 link[sz] = '\0';
159                                 orig = strstr(&file[1], "/");
160                                 if (orig && !strcmp(link, "(overlay-whiteout)"))
161                                         unlink(orig);
162                         }
163                 }
164                 free(namelist[n]);
165         }
166         free(namelist);
167
168         return 0;
169 }
170
171 static int
172 ask_user(int argc, char **argv)
173 {
174         if ((argc < 2) || strcmp(argv[1], "-y")) {
175                 fprintf(stderr, "This will erase all settings and remove any installed packages. Are you sure? [N/y]\n");
176                 if (getchar() != 'y')
177                         return -1;
178         }
179         return 0;
180
181 }
182
183 static int
184 handle_rmdir(const char *dir)
185 {
186         struct stat s;
187         struct dirent **namelist;
188         int n;
189
190         n = scandir(dir, &namelist, NULL, NULL);
191
192         if (n < 1)
193                 return -1;
194
195         while (n--) {
196                 char file[256];
197
198                 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
199                 if (!lstat(file, &s) && !S_ISDIR(s.st_mode))
200                         unlink(file);
201                 free(namelist[n]);
202         }
203         free(namelist);
204
205         rmdir(dir);
206
207         return 0;
208 }
209
210 static int
211 jffs2_reset(int argc, char **argv)
212 {
213         char mtd[32];
214         char *mp;
215
216         if (ask_user(argc, argv))
217                 return -1;
218
219         if (find_filesystem("overlay")) {
220                 fprintf(stderr, "overlayfs not found\n");
221                 return -1;
222         }
223
224         if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
225                 fprintf(stderr, "no rootfs_data was found\n");
226                 return -1;
227         }
228
229         mp = find_mount_point(mtd, "jffs2");
230         if (mp) {
231                 fprintf(stderr, "%s is mounted as %s, only erasing files\n", mtd, mp);
232                 foreachdir(mp, handle_rmdir);
233                 mount(mp, "/", NULL, MS_REMOUNT, 0);
234         } else {
235                 int fd;
236                 fprintf(stderr, "%s is not mounted, erasing it\n", mtd);
237                 find_mtd_char("rootfs_data", mtd, sizeof(mtd));
238                 fd = mtd_load(mtd);
239                 if (fd > 0) {
240                         mtd_erase(fd, 0, mtdsize / erasesize);
241                         close(fd);
242                 }
243         }
244
245         return 0;
246 }
247
248 static int
249 jffs2_mark(int argc, char **argv)
250 {
251         FILE *fp;
252         __u32 deadc0de = __cpu_to_be32(0xdeadc0de);
253         char mtd[32];
254         size_t sz;
255
256         if (ask_user(argc, argv))
257                 return -1;
258
259         if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
260                 fprintf(stderr, "no rootfs_data was found\n");
261                 return -1;
262         }
263
264         fp = fopen(mtd, "w");
265         fprintf(stderr, "%s - marking with deadc0de\n", mtd);
266         if (!fp) {
267                 fprintf(stderr, "opening %s failed\n", mtd);
268                 return -1;
269         }
270
271         sz = fwrite(&deadc0de, sizeof(deadc0de), 1, fp);
272         fclose(fp);
273
274         if (sz != 1) {
275                 fprintf(stderr, "writing %s failed: %s\n", mtd, strerror(errno));
276                 return -1;
277         }
278
279         return 0;
280 }
281
282 int
283 jffs2_switch(int argc, char **argv)
284 {
285         char mtd[32];
286         char *mp;
287         int ret = -1;
288
289         if (find_overlay_mount("overlayfs:/tmp/root"))
290                 return -1;
291
292         if (find_filesystem("overlay")) {
293                 fprintf(stderr, "overlayfs not found\n");
294                 return ret;
295         }
296
297         find_mtd_block("rootfs_data", mtd, sizeof(mtd));
298         mp = find_mount_point(mtd, NULL);
299         if (mp) {
300                 fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", mtd, mp);
301                 return -1;
302         }
303
304         if (find_mtd_char("rootfs_data", mtd, sizeof(mtd))) {
305                 fprintf(stderr, "no rootfs_data was found\n");
306                 return ret;
307         }
308
309         switch (mtd_identify(mtd)) {
310         case FS_NONE:
311                 fprintf(stderr, "no jffs2 marker found\n");
312                 /* fall through */
313
314         case FS_DEADCODE:
315                 ret = switch2jffs();
316                 if (!ret) {
317                         fprintf(stderr, "doing fo cleanup\n");
318                         umount2("/tmp/root", MNT_DETACH);
319                         foreachdir("/overlay/", handle_whiteout);
320                 }
321                 break;
322
323         case FS_JFFS2:
324                 ret = jffs2_mount();
325                 if (ret)
326                         break;
327                 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
328                         fprintf(stderr, "switching to jffs2 failed\n");
329                         ret = -1;
330                 }
331                 break;
332         }
333
334         return ret;
335 }
336
337 static int mtd_mount_jffs2(void)
338 {
339         char rootfs_data[32];
340         int fd;
341
342         if (mkdir("/tmp/overlay", 0755)) {
343                 fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
344                 return -1;
345         }
346
347         if (find_mtd_block("rootfs_data", rootfs_data, sizeof(rootfs_data))) {
348                 fprintf(stderr, "rootfs_data does not exist\n");
349                 return -1;
350         }
351
352         if (mount(rootfs_data, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
353                 fprintf(stderr, "failed to mount -t jffs2 %s /tmp/overlay: %s\n", rootfs_data, strerror(errno));
354                 return -1;
355         }
356
357         find_mtd_char("rootfs_data", rootfs_data, sizeof(rootfs_data));
358
359         fd = mtd_load(rootfs_data);
360         if (fd) {
361                 int ret = mtd_unlock(fd);
362                 close(fd);
363                 return ret;
364         }
365
366         return -1;
367 }
368
369 static int overlay_mount(void)
370 {
371         char mtd[32];
372         char *mp;
373
374         find_mtd_block("rootfs_data", mtd, sizeof(mtd));
375         mp = find_mount_point(mtd, NULL);
376         if (mp) {
377                 fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", mtd, mp);
378                 return -1;
379         }
380
381         mtd_mount_jffs2();
382
383         extroot_prefix = "/tmp/overlay";
384         if (!backend_mount("extroot")) {
385                 fprintf(stderr, "fs-state: switched to extroot\n");
386                 return 0;
387         }
388
389         fprintf(stderr, "switching to jffs2\n");
390         if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
391                 fprintf(stderr, "switching to jffs2 failed - fallback to ramoverlay\n");
392                 return ramoverlay();
393         }
394
395         return -1;
396 }
397
398 static struct backend_handler jffs2_handlers[] = {
399 {
400         .name = "jffs2reset",
401         .cli = jffs2_reset,
402 }, {
403         .name = "jffs2mark",
404         .cli = jffs2_mark,
405 }};
406
407 static struct backend overlay_backend = {
408         .name = "overlay",
409         .num_handlers = ARRAY_SIZE(jffs2_handlers),
410         .handlers = jffs2_handlers,
411         .mount = overlay_mount,
412 };
413 BACKEND(overlay_backend);