3 # Open Protium Reflash. This script will take a firmware image consisting
4 # of a compressed linux kernel image, concatentated with a jffs2 root
5 # filesystem image. The kernel MTD device is discovered by locating
6 # the MTD partition with the tag "kernel" and the filesystem MTD device
7 # is dicovered by locating the MTD partition with the tag "filesystem."
8 # There is no TOC inside the firmware images so there is no direct way
9 # to validate that the sizes of the parts in the firmware match the
10 # existing MTD partitions. So there could be a mismatch. However, a
11 # a mismatch size will be detect as this script mounts the newly laid
12 # done filesystem, a mismatch guarantees this to fail. That being said
13 # the script does validate the total size to prevent overwriting
14 # uboot. Furthermore the script makes sure the fsdev is not in use and
15 # that the various images are block aligned.
18 if [ -z "$flimg" ]; then
19 echo "Usage: reflash <image file>"
23 if [ \
! -f $flimg -o \
! -r $flimg ]; then
25 # not a file or not readable
27 echo "error: Image file [$flimg] not available"
31 dmesg |
grep StorCenter
>/dev
/null
2>&1
44 kdev
=` grep $ktag $mtd | awk -F: '{print $1}' | sed -e 's?mtd?/dev/mtdblock/?g'`
45 fsdev
=`grep $fstag $mtd | awk -F: '{print $1}' | sed -e 's?mtd?/dev/mtdblock/?g'`
47 flsize
=`ls -l $flimg | awk '{print $5}'`
48 ksize
=`grep $ktag $mtd | awk '{print "0x" $2}'`
49 fssize
=`grep $fstag $mtd | awk '{print "0x" $2}'`
52 # Size comes out of dc in exp notation and test wont accept a hex number
53 # so dumo it in hex then use awk to convert to decimal
55 size
=0x
`dc 16 o $ksize $fssize + p`
56 size
=`echo $size | awk '{printf ("%d",$1)}'`
59 # Make sure we are block aligned
61 kblks
=`dc $ksize $blksize / p`
62 r
=`dc $ksize $blksize % p`
64 echo "error: Kernel partition is not block aligned."
69 # Make sure we are block aligned
71 fsblks
=`dc $fssize $blksize / p`
72 r
=`dc $fssize $blksize % p`
74 echo "error: Filesystem partition is not block aligned."
79 # Check to see that we have enough room
81 if [ $flsize -gt $size ]; then
82 echo "error: Image size is bigger then available space."
89 grep $fsdev $mtab > /dev
/null
2>&1
91 echo "error: $fsdev mounted"
96 # If root is a jffs2 then close enough, im out
98 grep jffs2
$mtab > /dev
/null
2>&1
100 echo "error: $fsdev may be mounted"
106 # Mount fsdev and save fsdev/linuxrc
108 mkdir
$mntdir /tmp
/$$
109 mount
-t jffs2
$fsdev $mntdir
110 if [ $?
-ne 0 ]; then
111 echo "error: Unable to mount $fsdev"
114 echo "Preserving /linuxrc in /tmp/$$"
115 cp $mntdir/linuxrc
* /tmp
/$$
119 echo " Name : $flimg"
120 echo " Length: $flsize"
123 echo " Device: $kdev"
124 echo " Length: $ksize"
125 echo " Blocks: $kblks"
128 echo " Device: $fsdev"
129 echo " Length: $fssize"
130 echo " Blocks: $fsblks"
132 echo 'Ready to flash, Continue? (yes/no)'
134 if [ "z$continue" != "zyes" ]; then
135 rm -rf $mntdir /tmp
/$$
142 echo Preserving existing flash
in: $flimg.sav.$$
143 dd of
=$flimg.sav.$$
if=$kdev bs
=$blksize count
=$kblks
144 dd of
=$flimg.sav.$$
if=$fsdev bs
=$blksize count
=$fsblks seek
=$kblks
146 echo Flashing new firmware....
147 dd if=$flimg of
=$kdev bs
=$blksize count
=$kblks
148 dd if=$flimg of
=$fsdev bs
=$blksize count
=$fsblks skip
=$kblks
153 # Mount fsdev and restore fsdev/linuxrc
155 mount
-t jffs2
$fsdev $mntdir
156 if [ $?
-ne 0 ]; then
157 echo "error: Unable to re-mount $fsdev"
160 echo "Restoring /linuxrc"
161 cp /tmp
/$$
/linuxrc
* $mntdir
163 rm -rf $mntdir /tmp
/$$