Tomato 1.28
[tomato.git] / release / src / router / rc / jffs2.c
bloba50eda7da980cd85f1750e70eb49ec3cee5e7fef
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 <errno.h>
14 // #define TEST_INTEGRITY
17 static void error(const char *message)
19 char s[512];
21 snprintf(s, sizeof(s), "Error %s JFFS2. Check the logs to see if they contain more details about this error.", message);
22 notice_set("jffs2", s);
25 void start_jffs2(void)
27 if (!nvram_match("jffs2_on", "1")) {
28 notice_set("jffs2", "");
29 return;
32 int format = 0;
33 char s[256];
34 int size;
35 int part;
36 const char *p;
38 if (!wait_action_idle(10)) return;
40 if (!mtd_getinfo("jffs2", &part, &size)) return;
42 if (nvram_match("jffs2_format", "1")) {
43 nvram_set("jffs2_format", "0");
45 if (!mtd_erase("jffs2")) {
46 error("formatting");
47 return;
50 format = 1;
53 sprintf(s, "%d", size);
54 p = nvram_get("jffs2_size");
55 if ((p == NULL) || (strcmp(p, s) != 0)) {
56 if (format) {
57 nvram_set("jffs2_size", s);
58 nvram_commit_x();
60 else if ((p != NULL) && (*p != 0)) {
61 error("verifying known size of");
62 return;
66 if (!mtd_unlock("jffs2")) {
67 error("unlocking");
68 return;
71 modprobe("jffs2");
73 sprintf(s, "/dev/mtdblock/%d", part);
74 if (mount(s, "/jffs", "jffs2", MS_NOATIME|MS_NODIRATIME, "") != 0) {
75 modprobe_r("jffs2");
76 error("mounting");
77 return;
80 #ifdef TEST_INTEGRITY
81 int test;
83 if (format) {
84 if (f_write("/jffs/.tomato_do_not_erase", &size, sizeof(size), 0, 0) != sizeof(size)) {
85 stop_jffs2();
86 error("setting integrity test for");
87 return;
91 if ((f_read("/jffs/.tomato_do_not_erase", &test, sizeof(test)) != sizeof(test)) || (test != size)) {
92 stop_jffs2();
93 error("testing integrity of");
94 return;
96 #endif
98 notice_set("jffs2", format ? "Formatted." : "");
100 if (((p = nvram_get("jffs2_exec")) != NULL) && (*p != 0)) {
101 chdir("/jffs");
102 xstart(p);
103 chdir("/");
107 void stop_jffs2(void)
109 if (!wait_action_idle(10)) return;
111 notice_set("jffs2", "");
112 umount("/jffs");
113 modprobe_r("jffs2");