4 Copyright (C) 2006-2009 Jonathan Zarate
13 #include <sys/statfs.h>
16 #include <sys/reboot.h>
19 #define MTD_WRITE_CMD "mtd-write"
22 #define MTD_WRITE_CMD "/tmp/mtd-write"
25 void prepare_upgrade(void)
29 // stop non-essential stuff & free up some memory
30 exec_service("upgrade-start");
31 for (n
= 30; n
> 0; --n
) {
33 if (nvram_match("action_service", "")) break; // this is cleared at the end
35 unlink("/var/log/messages");
36 unlink("/var/log/messages.0");
40 void wi_upgrade(char *url
, int len
, char *boundary
)
43 const char *error
= "Error reading file";
49 reset
= (strcmp(webcgi_safeget("_reset", "0"), "1") == 0);
52 // quickly check if JFFS2 is mounted by checking if /jffs/ is not squashfs
54 if ((statfs("/jffs", &sf
) != 0) || (sf
.f_type
!= 0x73717368)) {
55 error
= "JFFS2 is currently in use. Since an upgrade may overwrite the "
56 "JFFS2 partition, please backup the contents, disable JFFS2, then reboot the router";
61 // skip the rest of the header
62 if (!skip_header(&len
)) goto ERROR
;
64 if (len
< (1 * 1024 * 1024)) {
65 error
= "Invalid file";
69 // -- anything after here ends in a reboot --
73 signal(SIGTERM
, SIG_IGN
);
74 signal(SIGINT
, SIG_IGN
);
75 signal(SIGHUP
, SIG_IGN
);
76 signal(SIGQUIT
, SIG_IGN
);
79 system("cp reboot.asp /tmp"); // copy to memory
83 char fifo
[] = "/tmp/flashXXXXXX";
87 if ((mktemp(fifo
) == NULL
) ||
88 (mkfifo(fifo
, S_IRWXU
) < 0)) {
89 error
= "Unable to create a fifo";
93 char *wargv
[] = { MTD_WRITE_CMD
, "-w", "-i", fifo
, "-d", "linux", NULL
};
94 if (_eval(wargv
, ">/tmp/.mtd-write", 0, &pid
) != 0) {
95 error
= "Unable to start flash program";
99 if ((f
= fopen(fifo
, "w")) == NULL
) {
100 error
= "Unable to start pipe for mtd write";
104 // !!! This will actually write the boundary. But since mtd-write
105 // uses trx length... -- zzz
108 if ((n
= web_read(buf
, MIN(len
, sizeof(buf
)))) <= 0) {
112 if (safe_fwrite(buf
, 1, n
, f
) != n
) {
113 error
= "Error writing to pipe";
125 if (pid
!= -1) waitpid(pid
, &n
, 0);
127 if (error
== NULL
&& reset
) {
128 set_action(ACT_IDLE
);
129 eval("mtd-erase", "-d", "nvram");
131 set_action(ACT_REBOOT
);
133 if (resmsg_fread("/tmp/.mtd-write"))
136 if (error
) resmsg_set(error
);
140 void wo_flash(char *url
)
143 parse_asp("/tmp/reboot.asp");
147 printf("\n\n -- reboot -- \n\n");
148 set_action(ACT_IDLE
);
150 // disconnect ppp - need this for PPTP/L2TP/PPPOE to finish gracefully
151 killall("xl2tpd", SIGTERM
);
152 killall("pppd", SIGTERM
);
161 parse_asp("error.asp");