5 [ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
6 libs() { ldd $* 2>/dev/null | sed -r 's/(.* => )?(.*) .*/\2/'; }
8 install_file() { # <file> [ <file> ... ]
11 if [ -L "$file" ]; then
12 target="$(readlink -f "$file")"
13 dest="$RAM_ROOT/$file"
15 dir="$(dirname "$dest")"
17 ln -s "$target" "$dest"
21 dest="$RAM_ROOT/$file"
22 [ -f "$file" -a ! -f "$dest" ] && {
23 dir="$(dirname "$dest")"
34 [ -x "$src" ] && files="$src $(libs $src)"
46 local default="$1"; shift;
47 local answer="$default"
49 [ "$INTERACTIVE" -eq 1 ] && {
51 0) echo -n "$* (y/N): ";;
52 *) echo -n "$* (Y/n): ";;
58 *) answer="$default";;
65 [ "$VERBOSE" -ge 1 ] && echo "$@"
76 /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
79 get_image() { # <source> [ <command> ]
83 if [ -z "$cat" ]; then
84 local magic="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
92 $cat "$from" 2>/dev/null
96 (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
100 (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
103 export_bootdevice() {
104 local cmdline uuid disk uevent
105 local MAJOR MINOR DEVNAME DEVTYPE
107 if read cmdline < /proc/cmdline; then
110 disk="${cmdline##*block2mtd=}"
114 disk="${cmdline##*root=}"
120 PARTUUID=[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]-02)
121 uuid="${disk#PARTUUID=}"
123 for disk in $(find /dev -type b); do
124 set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
125 if [ "$4$3$2$1" = "$uuid" ]; then
126 uevent="/sys/class/block/${disk##*/}/uevent"
132 uevent="/sys/class/block/${disk##*/}/uevent"
136 if [ -e "$uevent" ]; then
137 eval "$(sed "s/=\(.*\)/=\'\1\'/" < "$uevent")"
138 export BOOTDEV_MAJOR=$MAJOR
139 export BOOTDEV_MINOR=$MINOR
147 export_partdevice() {
148 local var="$1" offset="$2"
149 local uevent MAJOR MINOR DEVNAME DEVTYPE
151 for uevent in /sys/class/block/*/uevent; do
152 eval "$(sed "s/=\(.*\)/=\'\1\'/" < "$uevent")"
153 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
154 export "$var=$DEVNAME"
163 [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" == "3031" ] && {
164 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
170 get_partitions() { # <device> <filename>
174 if [ -b "$disk" -o -f "$disk" ]; then
175 v "Reading partition table from $filename..."
177 local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
178 if [ "$magic" != $'\x55\xAA' ]; then
179 v "Invalid partition table on $disk"
183 rm -f "/tmp/partmap.$filename"
186 for part in 1 2 3 4; do
187 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
189 local type="$(( $(hex_le32_to_cpu $1) % 256))"
190 local lba="$(( $(hex_le32_to_cpu $2) ))"
191 local num="$(( $(hex_le32_to_cpu $3) ))"
193 [ $type -gt 0 ] || continue
195 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
200 jffs2_copy_config() {
201 if grep rootfs_data /proc/mtd >/dev/null; then
203 mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
206 mtd jffs2write "$CONF_TAR" rootfs
210 # Flash firmware to MTD partition
212 # $(1): path to image
213 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
214 default_do_upgrade() {
216 if [ "$SAVE_CONFIG" -eq 1 ]; then
217 get_image "$1" "$2" | mtd $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
219 get_image "$1" "$2" | mtd write - "${PART_NAME:-image}"
223 do_upgrade_stage2() {
224 v "Performing system upgrade..."
225 if [ -n "$do_upgrade" ]; then
227 elif type 'platform_do_upgrade' >/dev/null 2>/dev/null; then
228 platform_do_upgrade "$IMAGE"
230 default_do_upgrade "$IMAGE"
233 if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev/null 2>/dev/null; then
237 v "Upgrade completed"
240 v "Rebooting system..."
244 echo b 2>/dev/null >/proc/sysrq-trigger