1 # . this file to load the following utility functions
4 # return (output) the character device name for flash parition "name"
5 # /proc/mtd has the general form:
6 # dev: size erasesize name
7 # mtd5: 00020000 00020000 "FIS directory"
8 # use this rather than hard-wiring the device because the partition
9 # table can change - looking in /proc/mtd is more reliable.
11 sed -n 's!^\(mtd[0-9][0-9]*\):[^"]*"'"$1"'"$!/dev/\1!p' /proc/mtd
15 # as mtdev but output the name of the block (not character) device
17 sed -n 's!^mtd\([0-9][0-9]*\):[^"]*"'"$1"'"$!/dev/mtdblock\1!p' /proc/mtd
21 # the size of the partition as a hexadecimal value (with 0x at the front)
23 sed -n 's!^mtd[0-9][0-9]*: \([^ ]*\)[^"]*"'"$1"'"$!0x\1!p' /proc/mtd
26 # sysvalmatch "section" "name" 'pattern' "configuration file"
27 # sysvalof "section" "name" "configuration file"
28 # sysval "section" "name"
29 # outputs the value of the SysConf variable 'name' from section 'section',
30 # this is a bit gross, when it gets a match it copies the value to the
31 # hold space, if no match it jumps over the copy, at the end ($) it copies
32 # the hold space to the pattern space and prints the result, thus it only
33 # ever prints the last match
34 # BUG FIX: busybox sed doesn't initialise the hold space and crashes if it
35 # is used before initialisation, so temporarily this script does it's own
37 # NOTE: these functions should only be used internally, add entries to 'config'
38 # below if necessary. This is because 'config' does the defaulting and in the
39 # recovering case (zero or absent SysConf) /etc/default/sysconf only contains
42 # sed -n '/^\['"$1"'\]$/,/^\[.*\]$/s/^'"$2"'=\('"$3"'\)$/\1/;tH;bE;:H;h;:E;$g;$p' "$4"
43 sed -n '/^\['"$1"'\]$/,/^\[.*\]$/s/^'"$2"'=\('"$3"'\)$/\1/p' "$4" | sed -n '$p'
46 sysvalmatch "$1" "$2" '.*' "$3"
49 sysvalof "$1" "$2" /etc/default/sysconf
53 # convenience callers for specific values to avoid mis-typing in scripts
54 # NOTE: this function does the defaulting, 'sysval' does not! Validity
55 # of the sysconf file is determined by the presence of the all important
60 test -r /etc/default/sysconf &&
61 mac="$(sysvalmatch network hw_addr '[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]:[0-9A-Za-z][0-9A-Za-z]' /etc/default/sysconf)"
66 host) if test -n "$(sysval network disk_server_name)"
68 sysval network disk_server_name
69 elif test -n "$(sysval network default_server_name)"
71 sysval network default_server_name
73 echo "$mac" | sed -n 's/^..:..:..:\(..\):\(..\):\(..\)$/LKG\1\2\3/p'
75 domain) sysval network w_d_name;;
76 iface) if test -n "$(sysval network lan_interface)"
78 sysval network lan_interface
82 ip) if test -n "$(sysval network ip_addr)"
84 sysval network ip_addr
88 netmask)sysval network netmask;;
89 gateway)sysval network gateway;;
90 dns) sysval network dns_server1;;
91 dns2) sysval network dns_server2;;
92 dns3) sysval network dns_server3;;
93 boot) if test -n "$(sysval network bootproto)"
95 sysval network bootproto
103 # These are the defaults for an invalid mac address, use the compiled
104 # in hardware address.
106 mac) echo "00:02:B3:02:02:01";;
107 host) echo "brokenslug";;
109 ip) echo 192.168.1.77;;
117 # Validate an interface name by making sure that it exists
118 # in /proc/net/dev (and is not lo). The listing outputs the
119 # interface followed by a :, the check function looks for
120 # something of the form '$1[a-zA-Z0-9]*:' and outputs the
121 # part preceding the ':'
123 sed -n '/^[ ]*lo:/d;s/^[ ]*\('"$1"'[a-zA-Z0-9]*\):.*$/\1/p;tE;d;:E;q' /proc/net/dev
126 # checkmount "mountpoint"
127 # tests an already mounted mountpoint to see whether to attempt to
128 # boot with this as root. Returns success if it appears ok.
130 # basic test for init (the kernel will try to load this)
131 # but require a shell in bin/sh and no .recovery too
132 test \( ! -f "$1/.recovery" \) -a \
133 \( -d "$1/initrd" -o -d "$1/mnt" \) -a \
134 \( -x "$1/bin/sh" -o -h "$1/bin/sh" \) -a \
135 \( -x "$1/sbin/init" -o -h "$1/sbin/init" -o \
136 -x "$1/etc/init" -o -h "$1/etc/init" -o \
137 -x "$1/bin/init" -o -h "$1/bin/init" \)
140 # swivel "new root" "old root"
141 # NOTE: the arguments must be paths relative to /, bad things
142 # will happen if the arguments themselves start with /
143 # Pivot to a new root. This does all the fancy pivot_root stuff
144 # including closing streams and does a umount /proc - it doesn't
145 # matter if this fails (failure codes are ignored), but if /proc
146 # was mounted it must be restored by the caller on return.
147 # Normally this function never returns!
148 # On return 0,1,2 are connected to /dev/console - this may not
149 # have been true before!
153 # This is just-in-case the called mounted /proc and was
154 # unable to close it because of the streams
155 umount /proc 2>/dev/null
158 # everything must move out of the old root, this process
159 # is $2/bin/sh so it must die, IO is redirected
160 # just in case - typically it will be to a device so it
161 # won't hold the old root open.
162 # the exec here is the first point at which the old root
163 # is unused - before the exec regardless of the close of
164 # 0,1,2 above ash still has *this* shell script open!
166 # init closes all file descriptors, there's no point
167 # supplying it with fds.
168 # NOTE: this used to use $2/usr/sbin/chroot, however on
169 # linux / is already . when the command is executed
170 # therefore it is essential to use the local (new root)
171 # chroot to ensure it gets the correct shared libraries.
172 exec usr/sbin/chroot . bin/sh -c "\
173 test -x sbin/init && exec sbin/init
174 test -x etc/init && exec etc/init
175 test -x bin/init && exec bin/init
182 # recovery - must restore the old root
184 sbin/pivot_root . "$1"
185 # cd is back to $1 - either pivot_root doesn't change it and the
186 # chroot above was not executed, or pivot_root does change it and
187 # has just changed it back!
188 exec <>/dev/console >&0 2>&0
192 # bring that interface up with the configured ip and other
195 local ip hostname router subnet iface HOSTNAME NETMASK BROADCAST
199 hostname="$(config host)"
200 router="$(config gateway)"
205 # only if an ip was specified
206 subnet="$(config netmask)"
211 # First try udhcpc - note that the /boot/udhcpc.script
212 # simply records the values returned and the udhcpc
213 # is not left running so this will only work for
214 # the lease length time!
216 if test "$(config boot)" != static
218 test -n "$hostname" && HOSTNAME="-H $hostname"
219 # The script writes the required shell variable assignments
220 # to file descriptor 9
221 eval $(udhcpc -i "$iface" -n -q -r "$ip" $HOSTNAME -s /boot/udhcpc.script 9>&1 >/dev/null)
224 test -n "$broadcast" && BROADCAST="broadcast $broadcast"
225 test -n "$subnet" && NETMASK="netmask $subnet"
227 if ifconfig "$iface" "$ip" $NETMASK $BROADCAST
231 route add default gw "$route" dev "$iface"
235 ifconfig "$iface" down
241 # take the interface down
246 # mountflash "flash device" "flash root directory" {mount options}
247 # Finds and mounts the flash file system on the given directory
252 test -n "$ffsdev" -a -b "$ffsdev" || {
253 echo "$0: unable to find flash file system to copy ($ffsdev)" >&2
259 test -n "$ffsdir" -a -d "$ffsdir" || {
260 echo "$0: mountflash $ffsdir: not a directory (internal error)" >&2
265 mount -t jffs2 "$@" "$ffsdev" "$ffsdir" || {
266 echo "$0: $ffsdev: unable to mount flash file system on $ffsdir" >&2
272 # umountflash [-r] "flash device"
273 # unmount any instance of the given flash device, if -r is specified a mount on
274 # root is an error, otherwise a mount on root is ignored (and remains).
276 local rootok ffsno ffsdev
283 # The argument is ffsdev
285 ffsno="$(devio "<<$ffsdev" prd)"
286 test -n "$ffsno" -a "$ffsno" -ge 0 || {
287 echo "$0: $ffsdev: device number $ffsno is not valid, cannot continue." >&2
291 # Make sure that Flashdisk isn't mounted on /
292 if test -z "$rootok" -a "$(devio "<</etc/init.d/sysconfsetup" prd)" -eq "$ffsno"
294 echo "$0: $ffsdev is mounted on /, use turnup ram" >&2
298 # The function is currently always used interactively, so output
299 echo "$0: umounting any existing mount of $ffsdev" >&2
301 # check each mount point, do this last first because otherwise nested
302 # mounts of ffsdev cannot be umounted.
304 local device mp type options stuff
306 read device mp type options stuff
307 test -z "$device" && return 0
309 # handle following entries first
310 ffs_umount || return 1
312 # handle this entry, since this is currently only used for unmounting
313 # the flash root partition we know a file which must exist...
316 */jffs2)test "$(devio "<<$mp/etc/init.d/sysconfsetup" prd 2>/dev/null)" -ne "$ffsno" ||
318 echo "$0: $mp: unable to umount $ffsdev" >&2
326 ffs_umount </proc/mounts || {
327 echo "$0: umount $ffsdev from all mount points then re-run reflash" >&2