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