Execute .autorun scripts when mounting jffs partition
[tomato.git] / release / src / router / rc / jffs2.c
blob298246d8755bd64bde9e1d043e2ca54e670fb358
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
16 #ifdef LINUX26
17 #define JFFS_NAME "jffs2"
18 #else
19 #define JFFS_NAME "jffs"
20 #endif
22 static void error(const char *message)
24 char s[512];
26 snprintf(s, sizeof(s), "Error %s JFFS. Check the logs to see if they contain more details about this error.", message);
27 notice_set("jffs", s);
30 void start_jffs2(void)
32 if (!nvram_match("jffs2_on", "1")) {
33 notice_set("jffs", "");
34 return;
37 int format = 0;
38 char s[256];
39 int size;
40 int part;
41 const char *p;
43 if (!wait_action_idle(10)) return;
45 if (!mtd_getinfo("jffs2", &part, &size)) return;
47 if (nvram_match("jffs2_format", "1")) {
48 nvram_set("jffs2_format", "0");
50 if (!mtd_erase("jffs2")) {
51 error("formatting");
52 return;
55 format = 1;
58 sprintf(s, "%d", size);
59 p = nvram_get("jffs2_size");
60 if ((p == NULL) || (strcmp(p, s) != 0)) {
61 if (format) {
62 nvram_set("jffs2_size", s);
63 nvram_commit_x();
65 else if ((p != NULL) && (*p != 0)) {
66 error("verifying known size of");
67 return;
71 if (!mtd_unlock("jffs2")) {
72 error("unlocking");
73 return;
76 modprobe(JFFS_NAME);
78 sprintf(s, MTD_BLKDEV(%d), part);
79 if (mount(s, "/jffs", JFFS_NAME, MS_NOATIME|MS_NODIRATIME, "") != 0) {
80 modprobe_r(JFFS_NAME);
81 error("mounting");
82 return;
85 #ifdef TEST_INTEGRITY
86 int test;
88 if (format) {
89 if (f_write("/jffs/.tomato_do_not_erase", &size, sizeof(size), 0, 0) != sizeof(size)) {
90 stop_jffs2();
91 error("setting integrity test for");
92 return;
96 if ((f_read("/jffs/.tomato_do_not_erase", &test, sizeof(test)) != sizeof(test)) || (test != size)) {
97 stop_jffs2();
98 error("testing integrity of");
99 return;
101 #endif
103 notice_set("jffs", format ? "Formatted." : "Loaded.");
105 if (((p = nvram_get("jffs2_exec")) != NULL) && (*p != 0)) {
106 chdir("/jffs");
107 xstart(p);
108 chdir("/");
110 run_userfile("/jffs", ".autorun", NULL, 0);
113 void stop_jffs2(void)
115 if (!wait_action_idle(10)) return;
117 notice_set("jffs", "stopped");
118 umount("/jffs");
119 modprobe_r(JFFS_NAME);