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