ar71xx: improve wget2nand script
[openwrt/svn-archive/archive.git] / target / linux / ar71xx / base-files / sbin / wget2nand
1 #!/bin/sh
2 # wget2nand
3 # This script can be used to download a TGZ file from your build system which
4 # contains the files to be installed on the NAND flash on your RB1xx card.
5 # The one parameter is the URL of the TGZ file to be downloaded.
6 # Licence GPL V2
7 # Author david.goodenough@linkchoose.co.uk
8 # Based on cf2nand from RB532 support
9 . /etc/functions.sh
10
11 wget2nand_dir=/tmp/wget2nand
12 mnt_kernel=$wget2nand_dir/mnt_kernel
13 mnt_rootfs=$wget2nand_dir/mnt_rootfs
14 src_rootfs=$wget2nand_dir/rootfs.tgz
15 src_kernel=$wget2nand_dir/kernel
16
17 [ -d "$wget2nand_dir" ] && {
18 echo "$wget2nand_dir already exists"
19 exit 1
20 }
21
22 # need to find the wget server from the command line
23 url=$1
24 [ -z "$url" ] && {
25 echo "No URL specified for image TGZ"
26 echo "Usage : $0 URL"
27 exit 1
28 }
29
30 url_kernel=$url/openwrt-ar71xx-vmlinux.elf
31 url_rootfs=$url/openwrt-ar71xx-rootfs.tgz
32
33 mtd_kernel="$(find_mtd_part 'kernel')"
34 mtd_rootfs="$(find_mtd_part 'rootfs')"
35 [ -z "$mtd_kernel" -o -z "$mtd_rootfs" ] && {
36 echo "Cannot find NAND Flash partitions"
37 exit 1
38 }
39
40 # first get an address for br-lan using udhcpc
41 killall udhcpc
42 /sbin/udhcpc -i br-lan
43
44 mkdir "$wget2nand_dir"
45 wget $url_kernel -O "$src_kernel" || {
46 echo "Unable to download $url_kernel"
47 exit 1
48 }
49
50 wget $url_rootfs -O "$src_rootfs" || {
51 echo "Unable to download $url_rootfs"
52 exit 1
53 }
54
55 echo "Erasing filesystem..."
56 mtd erase kernel 2>/dev/null >/dev/null
57 mtd erase rootfs 2>/dev/null >/dev/null
58
59 echo "Mounting $mtd_rootfs as new root and $mtd_kernel as kernel partition"
60
61 mkdir "$mnt_kernel"
62 mkdir "$mnt_rootfs"
63 mount -t yaffs2 "$mtd_kernel" "$mnt_kernel"
64 mount -t yaffs2 "$mtd_rootfs" "$mnt_rootfs"
65
66 echo "Copying kernel..."
67 cp $src_kernel $mnt_kernel/kernel
68 chmod +x $mnt_kernel/kernel
69
70 echo "Preparing filesystem..."
71 ( cd "$mnt_rootfs"; tar xvz -f "$src_rootfs" )
72
73 # make sure everything is written before we unmount the partitions
74 echo "chmod ugo+x /" > $mnt_rootfs/etc/uci-defaults/set_root_permission
75 sync
76 ls $mnt_kernel >/dev/null
77 ls $mnt_rootfs >/dev/null
78
79 echo "Cleaning up..."
80 # unmount the partitions and remove the directories into which they were mounted
81 umount $mnt_kernel
82 umount $mnt_rootfs
83 rm -rf $wget2nand_dir
84
85 # all done
86 echo "Image written, you can now reboot. Remember to change the boot source to Boot from Nand"