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[ -d /tmp/wget2nand ] && {
12	echo "/tmp/wget2nand already exists"
13	exit 1
14}
15
16# first get an address for br-lan using udhcpc
17killall udhcpc
18/sbin/udhcpc -i br-lan
19
20# need to find the wget server from the command line
21url=$1
22[ -z "$url" ] && {
23	echo "No URL specified for image TGZ"
24	echo "Usage : $0 URL"
25	exit 1
26}
27
28boot="$(find_mtd_part 'Routerboard NAND Boot')"
29main="$(find_mtd_part 'rootfs')"
30[ -z "$boot" -o -z "$main" ] && {
31	echo "Cannot find NAND Flash partitions"
32	exit 1
33}
34
35echo "Erasing filesystem."
36mtd erase Boot 2>/dev/null >/dev/null
37mtd erase Main 2>/dev/null >/dev/null
38
39echo "Mounting $main as new root and $boot as boot partition"
40
41mkdir /tmp/wget2nand/
42mkdir /tmp/wget2nand-boot
43mount -t yaffs2 "$main" /tmp/wget2nand/
44mount -t yaffs2 "$boot" /tmp/wget2nand-boot
45
46echo "Copying filesystem..."
47( wget -O - $url/openwrt-rb532-rootfs.tgz) | ( cd /tmp/wget2nand/; tar xvz )
48wget -O /tmp/wget2nand-boot/kernel $url/openwrt-rb532-vmlinux
49
50# No need to patch the kernel, this was done during the build process
51chmod +x /tmp/wget2nand-boot/kernel
52
53# make sure everything is written before we unmount the partitions
54echo "chmod ugo+x /" > /tmp/wget2nand/etc/uci-defaults/set_root_permission
55sync
56ls /tmp/wget2nand-boot/
57ls /tmp/wget2nand/
58# use kexec if present
59[ -x /usr/bin/kexec ] && {
60	kexec -l /tmp/wget2nand-boot/kernel --command-line="$(cat /proc/cmdline) root=$main rootfstype=yaffs2"
61	kexec -e
62}
63
64# unmount the partitions and remove the directories into which they were mounted
65umount /tmp/wget2nand-boot
66umount /tmp/wget2nand
67rmdir /tmp/wget2nand-boot
68rmdir /tmp/wget2nand
69
70# all done
71echo "Image written, you can now reboot.  Remember to change the boot source to Boot from Nand"
72