gdk-pixbuf-csource-native (2.12.11): Switch to FILESPATHPKG
[openembedded.git] / recipes / openprotium-init / files / reflash
blobf2947822f60224182fbbe75f625f91dd00524ad7
1 #!/bin/sh
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.
17 flimg=$1
18 if [ -z "$flimg" ]; then
19 echo "Usage: reflash <image file>"
20 exit 1
23 if [ \! -f $flimg -o \! -r $flimg ]; then
25 # not a file or not readable
27 echo "error: Image file [$flimg] not available"
28 exit 1
31 dmesg | grep StorCenter >/dev/null 2>&1
32 if [ $? -ne 0 ]; then
33 exit 0
36 blksize=512
37 mtd=/proc/mtd
38 mtab=/proc/mounts
39 mntdir=/tmp/fs.$$
41 ktag=kernel
42 fstag=filesystem
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`
63 if [ $r -ne 0 ]; then
64 echo "error: Kernel partition is not block aligned."
65 exit 1
69 # Make sure we are block aligned
71 fsblks=`dc $fssize $blksize / p`
72 r=`dc $fssize $blksize % p`
73 if [ $r -ne 0 ]; then
74 echo "error: Filesystem partition is not block aligned."
75 exit 1
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."
83 exit 1
87 # Is fsdev mounted?
89 grep $fsdev $mtab > /dev/null 2>&1
90 if [ $? -eq 0 ]; then
91 echo "error: $fsdev mounted"
92 exit 1
96 # If root is a jffs2 then close enough, im out
98 grep jffs2 $mtab > /dev/null 2>&1
99 if [ $? -eq 0 ]; then
100 echo "error: $fsdev may be mounted"
101 exit 1
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"
112 exit 1
114 echo "Preserving /linuxrc in /tmp/$$"
115 cp $mntdir/linuxrc* /tmp/$$
116 umount $mntdir
118 echo "Image:"
119 echo " Name : $flimg"
120 echo " Length: $flsize"
121 echo
122 echo "Kernel:"
123 echo " Device: $kdev"
124 echo " Length: $ksize"
125 echo " Blocks: $kblks"
126 echo
127 echo "Filesystem:"
128 echo " Device: $fsdev"
129 echo " Length: $fssize"
130 echo " Blocks: $fsblks"
131 echo
132 echo 'Ready to flash, Continue? (yes/no)'
133 read continue
134 if [ "z$continue" != "zyes" ]; then
135 rm -rf $mntdir /tmp/$$
136 exit 0
140 # Lets do the flash
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
149 sync
150 sleep 5
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"
158 exit 1
160 echo "Restoring /linuxrc"
161 cp /tmp/$$/linuxrc* $mntdir
162 umount $mntdir
163 rm -rf $mntdir /tmp/$$