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.
15 #include <sys/types.h>
16 #include <sys/mount.h>
18 #include <asm/byteorder.h>
30 #include "../fs-state.h"
31 #include "../lib/mtd.h"
33 #define SWITCH_JFFS2 "/tmp/.switch_jffs2"
36 foreachdir(const char *dir, int (*cb)(const char*))
42 if (dir[strlen(dir) - 1] == '/')
43 snprintf(globdir, 256, "%s*", dir);
45 snprintf(globdir, 256, "%s/*", dir);
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);
60 if (mkdir("/tmp/overlay", 0755)) {
61 fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
65 if (find_mtd_block("rootfs_data", rootfs_data, sizeof(rootfs_data))) {
66 fprintf(stderr, "rootfs_data does not exist\n");
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));
75 find_mtd_char("rootfs_data", rootfs_data, sizeof(rootfs_data));
77 fd = mtd_load(rootfs_data);
79 int ret = mtd_unlock(fd);
94 if (!stat(SWITCH_JFFS2, &s)) {
95 fprintf(stderr, "jffs2 switch already running\n");
99 if (!find_mtd_block("rootfs_patches", mtd, sizeof(mtd)))
102 if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
103 fprintf(stderr, "no rootfs_data was found\n");
107 creat("/tmp/.switch_jffs2", 0600);
108 ret = mount(mtd, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
109 unlink("/tmp/.switch_jffs2");
111 fprintf(stderr, "failed - mount -t jffs2 %s /rom/overlay: %s\n", mtd, strerror(errno));
115 if (mount("none", "/", NULL, MS_NOATIME | MS_REMOUNT, 0)) {
116 fprintf(stderr, "failed - mount -o remount,ro none: %s\n", strerror(errno));
120 system("cp -a /tmp/root/* /rom/overlay");
122 if (pivot("/rom", "/mnt")) {
123 fprintf(stderr, "failed - pivot /rom /mnt: %s\n", strerror(errno));
127 if (mount_move("/mnt", "/tmp/root", "")) {
128 fprintf(stderr, "failed - mount -o move /mnt /tmp/root %s\n", strerror(errno));
132 return fopivot("/overlay", "/rom");
136 handle_whiteout(const char *dir)
141 struct dirent **namelist;
144 n = scandir(dir, &namelist, NULL, NULL);
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);
159 orig = strstr(&file[1], "/");
160 if (orig && !strcmp(link, "(overlay-whiteout)"))
172 ask_user(int argc, char **argv)
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')
184 handle_rmdir(const char *dir)
187 struct dirent **namelist;
190 n = scandir(dir, &namelist, NULL, NULL);
198 snprintf(file, sizeof(file), "%s%s", dir, namelist[n]->d_name);
199 if (!lstat(file, &s) && !S_ISDIR(s.st_mode))
211 jffs2_reset(int argc, char **argv)
216 if (ask_user(argc, argv))
219 if (find_filesystem("overlay")) {
220 fprintf(stderr, "overlayfs not found\n");
224 if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
225 fprintf(stderr, "no rootfs_data was found\n");
229 mp = find_mount_point(mtd, "jffs2");
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);
236 fprintf(stderr, "%s is not mounted, erasing it\n", mtd);
237 find_mtd_char("rootfs_data", mtd, sizeof(mtd));
240 mtd_erase(fd, 0, mtdsize / erasesize);
249 jffs2_mark(int argc, char **argv)
252 __u32 deadc0de = __cpu_to_be32(0xdeadc0de);
256 if (ask_user(argc, argv))
259 if (find_mtd_block("rootfs_data", mtd, sizeof(mtd))) {
260 fprintf(stderr, "no rootfs_data was found\n");
264 fp = fopen(mtd, "w");
265 fprintf(stderr, "%s - marking with deadc0de\n", mtd);
267 fprintf(stderr, "opening %s failed\n", mtd);
271 sz = fwrite(&deadc0de, sizeof(deadc0de), 1, fp);
275 fprintf(stderr, "writing %s failed: %s\n", mtd, strerror(errno));
283 jffs2_switch(int argc, char **argv)
289 if (find_overlay_mount("overlayfs:/tmp/root"))
292 if (find_filesystem("overlay")) {
293 fprintf(stderr, "overlayfs not found\n");
297 find_mtd_block("rootfs_data", mtd, sizeof(mtd));
298 mp = find_mount_point(mtd, NULL);
300 fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", mtd, mp);
304 if (find_mtd_char("rootfs_data", mtd, sizeof(mtd))) {
305 fprintf(stderr, "no rootfs_data was found\n");
309 switch (mtd_identify(mtd)) {
311 fprintf(stderr, "no jffs2 marker found\n");
317 fprintf(stderr, "doing fo cleanup\n");
318 umount2("/tmp/root", MNT_DETACH);
319 foreachdir("/overlay/", handle_whiteout);
327 if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
328 fprintf(stderr, "switching to jffs2 failed\n");
337 static int mtd_mount_jffs2(void)
339 char rootfs_data[32];
342 if (mkdir("/tmp/overlay", 0755)) {
343 fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
347 if (find_mtd_block("rootfs_data", rootfs_data, sizeof(rootfs_data))) {
348 fprintf(stderr, "rootfs_data does not exist\n");
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));
357 find_mtd_char("rootfs_data", rootfs_data, sizeof(rootfs_data));
359 fd = mtd_load(rootfs_data);
361 int ret = mtd_unlock(fd);
369 static int overlay_mount(void)
374 find_mtd_block("rootfs_data", mtd, sizeof(mtd));
375 mp = find_mount_point(mtd, NULL);
377 fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", mtd, mp);
383 extroot_prefix = "/tmp/overlay";
384 if (!backend_mount("extroot")) {
385 fprintf(stderr, "fs-state: switched to extroot\n");
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");
398 static struct backend_handler jffs2_handlers[] = {
400 .name = "jffs2reset",
407 static struct backend overlay_backend = {
409 .num_handlers = ARRAY_SIZE(jffs2_handlers),
410 .handlers = jffs2_handlers,
411 .mount = overlay_mount,
413 BACKEND(overlay_backend);