Udpxy v1.0-Chipmunk-build21
[tomato.git] / release / src / router / rc / jffs2.c
blob3f55f69b889a6ddcfd942bc210fb5e9f45c776ad
1 /*
3 Tomato Firmware
4 Copyright (C) 2006-2009 Jonathan Zarate
6 */
8 #include "rc.h"
10 #include <sys/mount.h>
11 #include <sys/stat.h>
12 #include <sys/statfs.h>
13 #include <errno.h>
14 #ifndef MNT_DETACH
15 #define MNT_DETACH 0x00000002
16 #endif
18 // #define TEST_INTEGRITY
20 #ifdef TCONFIG_JFFSV1
21 #define JFFS_NAME "jffs"
22 #else
23 #define JFFS_NAME "jffs2"
24 #endif
26 static void error(const char *message)
28 char s[512];
30 snprintf(s, sizeof(s), "Error %s JFFS. Check the logs to see if they contain more details about this error.", message);
31 notice_set("jffs", s);
34 void start_jffs2(void)
36 if (!nvram_match("jffs2_on", "1")) {
37 notice_set("jffs", "");
38 return;
41 int format = 0;
42 char s[256];
43 int size;
44 int part;
45 const char *p;
46 struct statfs sf;
48 if (!wait_action_idle(10)) return;
50 if (!mtd_getinfo("jffs2", &part, &size)) return;
52 if (nvram_match("jffs2_format", "1")) {
53 nvram_set("jffs2_format", "0");
55 if (!mtd_erase("jffs2")) {
56 error("formatting");
57 return;
60 format = 1;
63 sprintf(s, "%d", size);
64 p = nvram_get("jffs2_size");
65 if ((p == NULL) || (strcmp(p, s) != 0)) {
66 if (format) {
67 nvram_set("jffs2_size", s);
68 nvram_commit_x();
70 else if ((p != NULL) && (*p != 0)) {
71 error("verifying known size of");
72 return;
76 if ((statfs("/jffs", &sf) == 0) && (sf.f_type != 0x73717368 /* squashfs */)) {
77 // already mounted
78 notice_set("jffs", format ? "Formatted" : "Loaded");
79 return;
82 if (!mtd_unlock("jffs2")) {
83 error("unlocking");
84 return;
87 modprobe(JFFS_NAME);
89 sprintf(s, MTD_BLKDEV(%d), part);
90 if (mount(s, "/jffs", JFFS_NAME, MS_NOATIME, "") != 0) {
91 modprobe_r(JFFS_NAME);
92 error("mounting");
93 return;
96 #ifdef TEST_INTEGRITY
97 int test;
99 if (format) {
100 if (f_write("/jffs/.tomato_do_not_erase", &size, sizeof(size), 0, 0) != sizeof(size)) {
101 stop_jffs2();
102 error("setting integrity test for");
103 return;
107 if ((f_read("/jffs/.tomato_do_not_erase", &test, sizeof(test)) != sizeof(test)) || (test != size)) {
108 stop_jffs2();
109 error("testing integrity of");
110 return;
112 #endif
114 notice_set("jffs", format ? "Formatted" : "Loaded");
116 if (((p = nvram_get("jffs2_exec")) != NULL) && (*p != 0)) {
117 chdir("/jffs");
118 system(p);
119 chdir("/");
121 run_userfile("/jffs", ".autorun", "/jffs", 3);
124 void stop_jffs2(void)
126 struct statfs sf;
128 if (!wait_action_idle(10)) return;
130 if ((statfs("/jffs", &sf) == 0) && (sf.f_type != 0x73717368)) {
131 // is mounted
132 run_userfile("/jffs", ".autostop", "/jffs", 5);
133 run_nvscript("script_autostop", "/jffs", 5);
136 notice_set("jffs", "Stopped");
137 umount2("/jffs", MNT_DETACH);
138 modprobe_r(JFFS_NAME);