tag rc6
[openwrt/svn-archive/openwrt.git] / package / webif / files / www / cgi-bin / webif / system-upgrade.sh
1 #!/usr/bin/webif-page -p /bin/sh
2 . /usr/lib/webif/webif.sh
3
4 do_upgrade() {
5 # free some memory :)
6 ps | grep -vE 'Command|init|\[[kbmj]|httpd|haserl|bin/sh|awk|kill|ps|webif' | awk '{ print $1 }' | xargs kill -KILL
7 MEMFREE="$(awk 'BEGIN{ mem = 0 } ($1 == "MemFree:") || ($1 == "Cached:") {mem += int($2)} END{print mem}' /proc/meminfo)"
8 empty "$ERASE_FS" || MTD_OPT="-e linux"
9 if [ $(($MEMFREE)) -ge 4096 ]; then
10 bstrip "$BOUNDARY" > /tmp/firmware.bin
11 mtd $MTD_OPT -q -r write /tmp/firmware.bin linux
12 else
13 # Not enough memory for storing the firmware on tmpfs
14 bstrip "$BOUNDARY" | mtd $MTD_OPT -q -q -r write - linux
15 fi
16 echo "@TR<<done>>."
17 }
18
19 read_var() {
20 NAME=""
21 while :; do
22 read LINE
23 LINE="${LINE%%[^0-9A-Za-z]}"
24 equal "$LINE" "$BOUNDARY" && read LINE
25 empty "$NAME$LINE" && exit
26 case "${LINE%%:*}" in
27 Content-Disposition)
28 NAME="${LINE##*; name=\"}"
29 NAME="${NAME%%\"*}"
30 ;;
31 esac
32 empty "$LINE" && return
33 done
34 }
35
36
37 NOINPUT=1
38 header "System" "Firmware Upgrade" "@TR<<Firmware Upgrade>>"
39
40 equal "$REQUEST_METHOD" "GET" && {
41 cat <<EOF
42 <script type="text/javascript">
43
44 function statusupdate() {
45 document.getElementById("form_submit").style.display = "none";
46 document.getElementById("status_text").style.display = "inline";
47
48 return true;
49 }
50 function printStatus() {
51 document.write('<div style="display: none; font-size: 14pt; font-weight: bold;" id="status_text" />@TR<<Upgrading...>>&nbsp;</div>');
52 }
53 </script>
54 <form method="POST" name="upgrade" action="$SCRIPT_NAME" enctype="multipart/form-data" onSubmit="statusupdate()">
55 <table style="width: 90%; text-align: left;" border="0" cellpadding="2" cellspacing="2" align="center">
56 <tbody>
57 <tr>
58 <td>@TR<<Options>>:</td>
59 <td>
60 <input type="checkbox" name="erase_fs" value="1" />@TR<<Erase_JFFS2|Erase JFFS2 partition>>
61 </td>
62 </tr>
63 <tr>
64 <td>@TR<<Firmware_image|Firmware image to upload:>></td>
65 <td>
66 <input type="file" name="firmware" />
67 </td>
68 </tr>
69 <tr>
70 <td />
71 <td>
72 <script type="text/javascript">printStatus()</script>
73 <input id="form_submit" type="submit" name="submit" value="@TR<<Upgrade>>" onClick="statusupdate()" />
74 </td>
75 </tr>
76 </tbody>
77 </table>
78 </form>
79 EOF
80 }
81 equal "$REQUEST_METHOD" "POST" && {
82 equal "${CONTENT_TYPE%%;*}" "multipart/form-data" || ERR=1
83 BOUNDARY="${CONTENT_TYPE##*boundary=}"
84 empty "$BOUNDARY" && ERR=1
85
86 empty "$ERR" || {
87 echo "Wrong data format"
88 footer
89 exit
90 }
91 cat <<EOF
92 <div style="margin: auto; text-align: left">
93 <pre>
94 EOF
95 while :; do
96 read_var
97 empty "$NAME" && exit
98 case "$NAME" in
99 erase_fs)
100 ERASE_FS=1
101 bstrip "$BOUNDARY" > /dev/null
102 ;;
103 firmware) do_upgrade;;
104 esac
105 done
106 cat <<EOF
107 </div>
108 EOF
109 }
110
111 footer
112
113 ##WEBIF:name:System:400:Firmware Upgrade