flasher: look for more bootfile tars
[pandora-misc.git] / flasher / rootfs / etc / updater.sh
1 #/bin/sh
2 #
3 # on-target updater script for Pandora
4 # Copyright (c) 2010, GraÅžvydas Ignotas
5
6 set -e
7
8 . /etc/updater_funcs.sh
9
10 status_uboot="missing"
11 status_uboot_env="already empty"
12 status_uimage="missing"
13 status_rootfs="missing"
14
15 mkdir /mnt/ubifs 2> /dev/null || true
16
17 if [ ! -e /dev/mmcblk0p1 ]
18 then
19         # hmh what's up with the SD card?
20         log "Can't access SD card, please reinsert and press enter"
21         read a < /dev/tty3
22         test -e /dev/mmcblk0p1
23 fi
24
25 mdevs="/dev/mmcblk0p1 /dev/mmcblk0p2"
26 bootfiles="bootf.tgz bootfiles.tar.gz uimage.tar.gz"
27
28 cd /tmp
29
30 for md in $mdevs
31 do
32         mpoint="/mnt/`basename $md`"
33         keep_mpoint="no"
34         mkdir $mpoint 2> /dev/null || true
35         if ! mount $md $mpoint
36         then
37                 continue;
38         fi
39
40         for bf in $bootfiles
41         do
42                 if [ -e $mpoint/$bf ]
43                 then
44                         tar xzvf $mpoint/$bf
45                 fi
46         done
47
48         if [ -x $mpoint/finalize.sh ]
49         then
50                 finalize_mpoint=$mpoint
51                 keep_mpoint="yes"
52         fi
53
54         if [ -e $mpoint/rootfs.img -a -e $mpoint/rootfs.md5 -a -z "$rootfs_file" ]
55         then
56                 rootfs_file=$mpoint/rootfs.img
57                 rootfs_md5=`head -n 1 $mpoint/rootfs.md5 | cut -c 1-32`
58                 rootfs_mpoint=$mpoint
59                 keep_mpoint="yes"
60         fi
61
62         if [ "$keep_mpoint" != "yes" ]
63         then
64                 umount $mpoint
65         fi
66 done
67
68 if [ -e u-boot.bin ]
69 then
70         size=`stat -c %s u-boot.bin`
71         dd if=/dev/mtdblock1 of=u-boot.bin.flashed bs=$size count=1
72         if ! cmp u-boot.bin u-boot.bin.flashed
73         then
74                 flash_eraseall /dev/mtd1
75                 nandwrite -p /dev/mtd1 u-boot.bin
76                 log "u-boot.bin flashed."
77                 status_uboot="flashed"
78         else
79                 log "u-boot.bin already flashed, skipped."
80                 status_uboot="already there"
81         fi
82 else
83         log "u-boot is missing, skipped."
84 fi
85
86 if ! cmp /dev/mtdblock2 /usr/local/erasednandblk
87 then
88         flash_eraseall /dev/mtd2
89         status_uboot_env="cleared"
90         log "u-boot environment cleared."
91 fi
92
93 if [ -e uImage ]
94 then
95         # attempt to mount and check
96         ubiattach /dev/ubi_ctrl -m 3 || true
97         mount -t ubifs ubi0:boot /mnt/ubifs  2> /dev/null || true
98         if ! cmp uImage /mnt/ubifs/uImage  2> /dev/null
99         then
100                 log "flashing uImage.."
101                 umount /mnt/ubifs  2> /dev/null || true
102                 ubidetach /dev/ubi_ctrl -d 0  2> /dev/null || true
103                 # format just in case, should be harmless even it's formated already
104                 ubiformat /dev/mtd3 -s 512 -y
105                 ubiattach /dev/ubi_ctrl -m 3
106                 ubimkvol /dev/ubi0 -m -N boot
107                 mount -t ubifs ubi0:boot /mnt/ubifs
108                 cp uImage /mnt/ubifs/uImage
109                 status_uimage="flashed"
110                 log "done"
111         else
112                 log "uImage already flashed, skipped."
113                 status_uimage="already there"
114         fi
115         umount /mnt/ubifs
116         ubidetach /dev/ubi_ctrl -d 0
117 else
118         log "uImage is missing, skipped."
119 fi
120
121 if [ -n "$rootfs_file" ]
122 then
123         log "Calculating checksum, this might take a few minutes.."
124         real_md5=`md5sum $rootfs_file | cut -c 1-32`
125         if [ "$real_md5" = "$rootfs_md5" ]
126         then
127                 log $real_md5
128         else
129                 log "INCORRECT"
130                 false
131         fi
132
133         # be destructive here, too hard to validate rootfs to bother
134         log "formatting.."
135         ubiformat /dev/mtd4 -s 512 -y
136         ubiattach /dev/ubi_ctrl -m 4
137         ubimkvol /dev/ubi0 -m -N rootfs
138         log "done."
139         log "Writing rootfs, please wait. This may take up to 15 minutes.."
140         ubiupdatevol /dev/ubi0_0 $rootfs_file
141         ubidetach /dev/ubi_ctrl -d 0
142         status_rootfs="flashed"
143         log "done."
144 else
145         log "root filesystem is missing, skipped."
146 fi
147
148 if [ -n "$finalize_mpoint" ]
149 then
150         log "running finalize script.."
151         cd $finalize_mpoint
152         if ! ./finalize.sh
153         then
154                 log "finalize.sh failed."
155                 false
156         fi
157         log "done."
158         cd /tmp
159         umount $finalize_mpoint
160 fi
161
162 if [ -n "$rootfs_mpoint" ]
163 then
164         umount $rootfs_mpoint 2> /dev/null || true
165 fi
166
167 log "======================="
168 log "results:"
169 log "u-boot:     $status_uboot"
170 log "u-boot-env: $status_uboot_env"
171 log "kernel:     $status_uimage"
172 log "rootfs:     $status_rootfs $rootfs_file"
173
174 exit 0