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