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 "$cmd" ]; 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 |
$cmd
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 bootdisk rootpart uuid blockdev uevent line
105 local MAJOR MINOR DEVNAME DEVTYPE
107 if read cmdline
< /proc
/cmdline
; then
110 bootdisk
="${cmdline##*block2mtd=}"
111 bootdisk
="${bootdisk%%,*}"
114 rootpart
="${cmdline##*root=}"
115 rootpart
="${rootpart%% *}"
121 uevent
="/sys/class/block/${bootdisk##*/}/uevent"
126 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
]-[a-f0-9
][a-f0-9
])
127 uuid
="${rootpart#PARTUUID=}"
128 uuid
="${uuid%-[a-f0-9][a-f0-9]}"
129 for blockdev
in $
(find /dev
-type b
); do
130 set -- $
(dd if=$blockdev bs
=1 skip
=440 count
=4 2>/dev
/null |
hexdump -v -e '4/1 "%02x "')
131 if [ "$4$3$2$1" = "$uuid" ]; then
132 uevent
="/sys/class/block/${blockdev##*/}/uevent"
138 uevent
="/sys/class/block/${rootpart##*/}/../uevent"
142 if [ -e "$uevent" ]; then
146 export BOOTDEV_MAJOR
=$MAJOR
147 export BOOTDEV_MINOR
=$MINOR
155 export_partdevice
() {
156 local var
="$1" offset
="$2"
157 local uevent line MAJOR MINOR DEVNAME DEVTYPE
159 for uevent
in /sys
/class
/block
/*/uevent
; do
163 if [ $BOOTDEV_MAJOR = $MAJOR -a $
(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
164 export "$var=$DEVNAME"
173 [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x
"')" = "3031" ] && {
174 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
180 get_partitions
() { # <device> <filename>
184 if [ -b "$disk" -o -f "$disk" ]; then
185 v
"Reading partition table from $filename..."
187 local magic
=$
(dd if="$disk" bs
=2 count
=1 skip
=255 2>/dev
/null
)
188 if [ "$magic" != $
'\x55\xAA' ]; then
189 v
"Invalid partition table on $disk"
193 rm -f "/tmp/partmap.$filename"
196 for part
in 1 2 3 4; do
197 set -- $
(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
199 local type="$(( $(hex_le32_to_cpu $1) % 256))"
200 local lba
="$(( $(hex_le32_to_cpu $2) ))"
201 local num
="$(( $(hex_le32_to_cpu $3) ))"
203 [ $type -gt 0 ] ||
continue
205 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
210 jffs2_copy_config
() {
211 if grep rootfs_data
/proc
/mtd
>/dev
/null
; then
213 mtd
-e rootfs_data jffs2write
"$CONF_TAR" rootfs_data
216 mtd jffs2write
"$CONF_TAR" rootfs
225 # Flash firmware to MTD partition
227 # $(1): path to image
228 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
229 default_do_upgrade
() {
231 if [ "$SAVE_CONFIG" -eq 1 ]; then
232 get_image
"$1" "$2" | mtd
$MTD_ARGS $MTD_CONFIG_ARGS -j "$CONF_TAR" write - "${PART_NAME:-image}"
234 get_image
"$1" "$2" | mtd
$MTD_ARGS write - "${PART_NAME:-image}"
236 [ $?
-ne 0 ] && exit 1
239 do_upgrade_stage2
() {
240 v
"Performing system upgrade..."
241 if [ -n "$do_upgrade" ]; then
243 elif type 'platform_do_upgrade' >/dev
/null
2>/dev
/null
; then
244 platform_do_upgrade
"$IMAGE"
246 default_do_upgrade
"$IMAGE"
249 if [ "$SAVE_CONFIG" -eq 1 ] && type 'platform_copy_config' >/dev
/null
2>/dev
/null
; then
253 v
"Upgrade completed"
256 v
"Rebooting system..."
260 echo b
2>/dev
/null
>/proc
/sysrq-trigger