utility app: fix flash read/write problem on mtd devices
[keystone-demos/matrix-apps.git] / utility_flash / cgi-bin / flashwrite.cgi
1 #!/bin/sh
2 echo Content-type: text/html
3 echo 
4 echo 
6 cat << EOM
7 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'>
8 <head><meta http-equiv='Pragma' CONTENT='no-cache' /> <meta http-equiv='content-type' content='text/html; charset=utf-8' /> <title>C6x Linux Web Control Panel</title><link rel='stylesheet' href='/default.css' type='text/css' />
9 </head>
10 <body>
11 <div id='wrapper'><div id='logo'><h1>Flash Write</h1>
12 </div><div id='header'><div id='menu'><ul>
13 </ul></div></div></div>
14 <div id='page'>
15 EOM
17 display_footer()
18 {
19 cat << EOM2
20 <hr>
21 <div style='clear: both;'>&nbsp;</div>
22 </div>
23 <div id='footer'><p id='legal'>( c ) 2013 Texas Instruments Incorporated&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p></div>
24 </body>
25 </html>
26 EOM2
27 }
28 TMPFILE=`mktemp`
29 TMPFILE_DATA=`mktemp`
30 cat > $TMPFILE
31 partition=$(cat $TMPFILE | sed -n '4p'| sed 's/.$//')
32 mtddevname=$(cat /proc/mtd | awk 'NR < 4' | awk -v pat="$partition" '$0 ~ pat {printf $1}' | sed 's/://')
33 mtddev="/dev/"$mtddevname
34 soffset=$(head -8 $TMPFILE | wc -c)
35 eoffset=$(tail -1 $TMPFILE | wc -c)
36 fsize=$(cat $TMPFILE | wc -c)
37 osize=$(((($fsize-$eoffset)-$soffset)-2))
39 check_params()
40 {
41 #check for 0 size file
42 if [ $osize -eq 0 ] ; then
43 cat << EOM1
44 <div id='content'>
45 <p>Bad Input file ...</p>
46 </div>
47 EOM1
48 display_footer
49 rm -f $TMPFILE
50 exit
51 fi
53 #check for the NAND partition
54 if [ "$mtddevname" = "" ] ; then
55 cat << EOM1
56 <div id='content'>
57 <p>Could not find Nand partition for <i>$partition</i> ...</p>
58 <br>
59 </div>
60 EOM1
61 display_footer
62 rm -f $TMPFILE
63 exit
64 fi
66 mtdsz="0x"$(cat /proc/mtd | awk 'NR < 4' | awk -v pat="$partition" '$0 ~ pat {printf $2}')
67 mtdsz=$(($mtdsz))
68 if [ $osize -gt $mtdsz ] ; then
69 cat << EOM1
70 <div id='content'>
71 <p>Input File too large (File Size=$osize, MTD Size=$mtdsz)</p>
72 </div>
73 EOM1
74 display_footer
75 rm -f $TMPFILE
76 exit
77 fi
78 }
80 erase_flash()
81 {
82 cat << EOM1
83 <div id='content'>
84 <p>Erasing mtd device <i>$mtddev</i> ...</p>
85 <br>
86 </div>
87 EOM1
88 flash_eraseall -q $mtddev
89 }
91 program_flash()
92 {
93 dd if=$TMPFILE of=$TMPFILE_DATA skip=$soffset bs=1 count=$osize 2> /dev/null
94 cat << EOM1
95 <div id='content'>
96 <p>Writing <i>$osize</i> bytes to <i>$mtddev</i> ...</p>
97 </div>
98 EOM1
99 nandwrite -q $mtddev $TMPFILE_DATA -p
100 sync
101 cat << EOM2
102 <div id='content'>
103 <p>Write to <i>$mtddev</i> complete</p>
104 </div>
105 EOM2
108 check_params
109 erase_flash
110 program_flash
112 #cleanup
113 rm -f $TMPFILE
114 rm -f $TMPFILE_DATA
116 display_footer