| 2 | | # |
|---|
| 3 | | # tftp flash script for wireless routers |
|---|
| 4 | | # |
|---|
| 5 | | # Copyright (c) 2006, 2007 by Thorsten Glaser <tg@mirbsd.de> |
|---|
| 6 | | # Copyright (C) 2006 by Waldemar Brodkorb <wbx@freewrt.org> |
|---|
| 7 | | # Copyright (C) 2004 by Oleg I. Vdovikin <oleg@cs.msu.su> |
|---|
| 8 | | # |
|---|
| 9 | | # This program is free software; you can redistribute it and/or modify |
|---|
| 10 | | # it under the terms of the GNU General Public License as published by |
|---|
| 11 | | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 12 | | # (at your option) any later version. |
|---|
| 13 | | # |
|---|
| 14 | | # This program is distributed in the hope that it will be useful, |
|---|
| 15 | | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 17 | | # General Public License for more details. |
|---|
| 18 | | # |
|---|
| 19 | | # You should have received a copy of the GNU General Public License |
|---|
| 20 | | # along with this program; if not, write to the Free Software |
|---|
| 21 | | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| | 2 | #- |
|---|
| | 3 | # This file is part of the FreeWRT project. FreeWRT is copyrighted |
|---|
| | 4 | # material, please see the LICENCE file in the top-level directory |
|---|
| | 5 | # or at http://www.freewrt.org/licence for details. |
|---|
| 38 | | Notes for Asus routers: |
|---|
| 39 | | be sure POWER led is flashing (if this is not the case, |
|---|
| 40 | | power off the device, then hold the reset button pushed |
|---|
| 41 | | while powering it on again). |
|---|
| 42 | | |
|---|
| 43 | | 1) connect your PC to the LAN port |
|---|
| 44 | | 2) be sure your link is up and has an address in the |
|---|
| 45 | | range 192.168.1.0/24 but other than 192.168.1.1, |
|---|
| 46 | | or specify an IP address where to flash to |
|---|
| 47 | | |
|---|
| 48 | | EOF |
|---|
| | 17 | die() { |
|---|
| | 18 | printf >&2 '%s: %s\n' "$me" "$*" |
|---|
| 52 | | # find an available tftp client |
|---|
| 53 | | tftp= |
|---|
| 54 | | for t in tftp atftp; do |
|---|
| 55 | | if which $t >/dev/null 2>&1; then |
|---|
| 56 | | tftp=$t |
|---|
| 57 | | break |
|---|
| | 22 | usage() { |
|---|
| | 23 | cat >&2 <<-EOF |
|---|
| | 24 | Syntax: $me [-c tftp] [-F | -f fwcf.bin] [-t type] file [ip] |
|---|
| | 25 | Valid types are: |
|---|
| | 26 | wl500g wl500gd wl500gp wl500gx linksys |
|---|
| | 27 | Meta-types (no support for -f or -F): |
|---|
| | 28 | wl500gx = wl500gd or wl500gp |
|---|
| | 29 | linksys = all linksys models |
|---|
| | 30 | EOF |
|---|
| | 31 | exit 1 |
|---|
| | 32 | } |
|---|
| | 33 | |
|---|
| | 34 | imgtype=auto |
|---|
| | 35 | flashtype=auto |
|---|
| | 36 | integer dopad=0 |
|---|
| | 37 | integer padsize=0 |
|---|
| | 38 | padfile= |
|---|
| | 39 | tftp=tftp |
|---|
| | 40 | |
|---|
| | 41 | while getopts ":c:Ff:t:" ch; do |
|---|
| | 42 | case $ch in |
|---|
| | 43 | (c) tftp=$OPTARG |
|---|
| | 44 | [[ -x $tftp ]] || tftp=$(which $tftp 2>&-) |
|---|
| | 45 | [[ -x $tftp ]] || die "TFTP client '$OPTARG' not found" |
|---|
| | 46 | ;; |
|---|
| | 47 | (F) dopad=1 |
|---|
| | 48 | padfile= |
|---|
| | 49 | ;; |
|---|
| | 50 | (f) dopad=1 |
|---|
| | 51 | padfile=$OPTARG |
|---|
| | 52 | [[ -s $padfile ]] || die "Invalid pad file '$padfile'" |
|---|
| | 53 | ;; |
|---|
| | 54 | (t) flashtype=$OPTARG |
|---|
| | 55 | ;; |
|---|
| | 56 | (:) echo >&2 "$me: Error: missing argument to -$OPTARG" |
|---|
| | 57 | usage |
|---|
| | 58 | ;; |
|---|
| | 59 | (*) echo >&2 "$me: Error: invalid option -$OPTARG" |
|---|
| | 60 | usage |
|---|
| | 61 | ;; |
|---|
| | 62 | esac |
|---|
| | 63 | done |
|---|
| | 64 | shift $((OPTIND - 1)) |
|---|
| | 65 | |
|---|
| | 66 | [[ $# -lt 1 || $# -gt 2 ]] && usage |
|---|
| | 67 | imgfile=$1 |
|---|
| | 68 | ip=${2:-192.168.1.1} |
|---|
| | 69 | |
|---|
| | 70 | [[ $imgfile = *@( )* ]] && die Spaces in filenames are not supported |
|---|
| | 71 | [[ $ip = +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]] || \ |
|---|
| | 72 | die "Invalid IPv4 address '$ip'" |
|---|
| | 73 | |
|---|
| | 74 | [[ $imgtype = auto ]] && case ${imgfile%.[bt][ir][nx]} in |
|---|
| | 75 | *-asus-wl500g-premium-*) |
|---|
| | 76 | imgtype=wl500gp ;; |
|---|
| | 77 | *-asus-wl500g-deluxe-*) |
|---|
| | 78 | imgtype=wl500gd ;; |
|---|
| | 79 | *-asus-wl500g-*) |
|---|
| | 80 | imgtype=wl500g ;; |
|---|
| | 81 | *-linksys-*) |
|---|
| | 82 | imgtype=linksys ;; |
|---|
| | 83 | *) |
|---|
| | 84 | die "Cannot determine model from filename '$imgfile', use -t option" ;; |
|---|
| | 85 | esac |
|---|
| | 86 | |
|---|
| | 87 | case $imgtype in |
|---|
| | 88 | wl500g) |
|---|
| | 89 | flashtype=wl500g ;; |
|---|
| | 90 | wl500gd|wl500gp|wl500gx) |
|---|
| | 91 | flashtype=wl500gx ;; |
|---|
| | 92 | linksys) |
|---|
| | 93 | flashtype=linksys ;; |
|---|
| | 94 | *) |
|---|
| | 95 | die "Unknown model '$imgtype'" ;; |
|---|
| | 96 | esac |
|---|
| | 97 | |
|---|
| | 98 | if (( dopad )); then |
|---|
| | 99 | case $imgtype in |
|---|
| | 100 | wl500gp) |
|---|
| | 101 | padsize=7929856 ;; |
|---|
| | 102 | *) |
|---|
| | 103 | die "Cannot use -F or -f with a $imgtype yet" ;; |
|---|
| | 104 | esac |
|---|
| | 105 | TF=$(mktemp /tmp/fwflash.XXXXXXXXXX) || die Cannot mktemp |
|---|
| | 106 | TI=$(mktemp /tmp/fwflash.XXXXXXXXXX) || { rm $TF; die Cannot mktemp; } |
|---|
| | 107 | trap 'rm -f $TF $TI; exit 0' 0 |
|---|
| | 108 | trap 'rm -f $TF $TI; exit 1' 1 2 3 5 13 15 |
|---|
| | 109 | randsrc=/dev/arandom |
|---|
| | 110 | [[ -e $randsrc ]] || randsrc=/dev/urandom |
|---|
| | 111 | [[ -e $randsrc ]] || randsrc=/dev/random |
|---|
| | 112 | [[ -e $randsrc ]] || die Cannot find an entropy source |
|---|
| | 113 | dd if=$randsrc of=$TF bs=131072 count=1 2>&- |
|---|
| | 114 | if [[ -n $padfile ]]; then |
|---|
| | 115 | dd if="$padfile" of=$TF conv=notrunc 2>&- |
|---|
| | 116 | echo "TRX file constructed from root='$imgfile' and fwcf='$padfile'" |
|---|
| | 117 | else |
|---|
| | 118 | printf 'FWCF\x18\x00\x00\x01\x01\x00\x00\x10\x12\x00\x11\x00\x00\x00\x00\x00\x74\x01\x7C\x18' | \ |
|---|
| | 119 | dd of=$TF conv=notrunc 2>&- |
|---|
| | 120 | echo "TRX file constructed from '$imgfile' and empty fwcf filesystem" |
|---|
| 85 | | *-asus-wl500g-*.bin | *-asus-wl500g-*.trx) |
|---|
| 86 | | echo Confirming IP address setting... |
|---|
| 87 | | printf "get ASUSSPACELINK\x01\x01\xa8\xc0 /dev/null\nquit\n" | $tftp $ip |
|---|
| 88 | | echo Flashing $ip using "$file"... |
|---|
| 89 | | printf "binary\nput $file ASUSSPACELINK\nquit\n" | $tftp $ip |
|---|
| 90 | | echo Please wait until leds stops flashing. Device will automatically reboot. |
|---|
| 91 | | ;; |
|---|
| 92 | | *-linksys-*.bin) |
|---|
| 93 | | echo Flashing $ip using "$file"... |
|---|
| 94 | | printf "rexmt 1\ntrace\nbinary\nput $file\nquit\n" | $tftp $ip |
|---|
| 95 | | echo Unit will automatically reboot within 3-5 minutes. Do not power off. |
|---|
| 96 | | ;; |
|---|
| 97 | | *) |
|---|
| 98 | | echo Unsupported model "'$file'" |
|---|
| 99 | | usage |
|---|
| | 148 | linksys) |
|---|
| | 149 | echo Flashing image... |
|---|
| | 150 | printf 'rexmt 1\ntrace\nbinary\nput %s\nquit\n' "$imgfile" | $tftp $ip |
|---|
| | 151 | echo Please wait 3-5 minutes. |
|---|
| | 152 | echo This device will reboot itself automatically. |
|---|
| 102 | | echo After that you can login via: ssh admin@$ip |
|---|
| 103 | | echo Default password, unless changed in menuconfig, is: FreeWRT |
|---|
| | 155 | cat <<-EOF |
|---|
| | 156 | After flashing is finished and the device has rebooted, it will need |
|---|
| | 157 | to restart once if the filesystem type is squashfs with jffs2 overlay. |
|---|
| | 158 | The power LED will flash quickly as long as startup is in progress. |
|---|
| | 159 | |
|---|
| | 160 | Once it has finished flashing, you should be able to access the device |
|---|
| | 161 | via ssh to the "admin" user, for example: ssh -l admin $ip |
|---|
| | 162 | |
|---|
| | 163 | If you didn't change the password in menuconfig, it's "FreeWRT" (case |
|---|
| | 164 | sensitive and without the quotation marks). Give the device a while to |
|---|
| | 165 | generate the SSH keys for dropbear first. |
|---|
| | 166 | |
|---|
| | 167 | After device initialisation and key generation has finished, it is |
|---|
| | 168 | highly recommended to log in and isse the command "fwcf commit" so that |
|---|
| | 169 | the SSH host keys will be stored in the configuration filesystem and |
|---|
| | 170 | do not have to be regenerated each time the device starts. |
|---|
| | 171 | |
|---|
| | 172 | EOF |
|---|
| | 173 | exit 0 |
|---|