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 line
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
140 export BOOTDEV_MAJOR
=$MAJOR
141 export BOOTDEV_MINOR
=$MINOR
149 export_partdevice
() {
150 local var
="$1" offset
="$2"
151 local uevent line MAJOR MINOR DEVNAME DEVTYPE
153 for uevent
in /sys
/class
/block
/*/uevent
; do
157 if [ $BOOTDEV_MAJOR = $MAJOR -a $
(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
158 export "$var=$DEVNAME"
167 [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x
"')" == "3031" ] && {
168 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
174 get_partitions
() { # <device> <filename>
178 if [ -b "$disk" -o -f "$disk" ]; then
179 v
"Reading partition table from $filename..."
181 local magic
=$
(dd if="$disk" bs
=2 count
=1 skip
=255 2>/dev
/null
)
182 if [ "$magic" != $
'\x55\xAA' ]; then
183 v
"Invalid partition table on $disk"
187 rm -f "/tmp/partmap.$filename"
190 for part
in 1 2 3 4; do
191 set -- $
(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
193 local type="$(( $(hex_le32_to_cpu $1) % 256))"
194 local lba
="$(( $(hex_le32_to_cpu $2) ))"
195 local num
="$(( $(hex_le32_to_cpu $3) ))"
197 [ $type -gt 0 ] ||
continue
199 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
204 jffs2_copy_config
() {
205 if grep rootfs_data
/proc
/mtd
>/dev
/null
; then
207 mtd
-e rootfs_data jffs2write
"$CONF_TAR" rootfs_data
210 mtd jffs2write
"$CONF_TAR" rootfs
214 # Flash firmware to MTD partition
216 # $(1): path to image
217 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
218 default_do_upgrade
() {
220 if [ "$SAVE_CONFIG" -eq 1 ]; then
221 get_image
"$1" "$2" | mtd
$MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
223 get_image
"$1" "$2" | mtd
write - "${PART_NAME:-image}"
227 do_upgrade_stage2
() {
228 v
"Performing system upgrade..."
229 if [ -n "$do_upgrade" ]; then
231 elif type 'platform_do_upgrade' >/dev
/null
2>/dev
/null
; then
232 platform_do_upgrade
"$IMAGE"
234 default_do_upgrade
"$IMAGE"
237 if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev
/null
2>/dev
/null
; then
241 v
"Upgrade completed"
244 v
"Rebooting system..."
248 echo b
2>/dev
/null
>/proc
/sysrq-trigger