5 export BACKUP_FILE
=sysupgrade.tgz
# file extracted by preinit
7 [ -x /usr
/bin
/ldd
] || ldd
() { LD_TRACE_LOADED_OBJECTS
=1 $
*; }
8 libs
() { ldd $
* 2>/dev
/null |
sed -E 's/(.* => )?(.*) .*/\2/'; }
10 install_file
() { # <file> [ <file> ... ]
13 if [ -L "$file" ]; then
14 target
="$(readlink -f "$file")"
15 dest
="$RAM_ROOT/$file"
17 dir
="$(dirname "$dest")"
19 ln -s "$target" "$dest"
23 dest
="$RAM_ROOT/$file"
24 [ -f "$file" -a ! -f "$dest" ] && {
25 dir
="$(dirname "$dest")"
36 [ -x "$src" ] && files
="$src $(libs $src)"
48 local default
="$1"; shift;
49 local answer
="$default"
51 [ "$INTERACTIVE" -eq 1 ] && {
53 0) echo -n "$* (y/N): ";;
54 *) echo -n "$* (Y/n): ";;
60 *) answer
="$default";;
67 [ -n "$VERBOSE" ] && [ "$VERBOSE" -ge 1 ] && echo "$@"
78 /bin
/mount |
awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
81 get_image
() { # <source> [ <command> ]
85 if [ -z "$cmd" ]; then
86 local magic
="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x
"')"
94 cat "$from" 2>/dev
/null |
$cmd
98 (get_image
"$@" |
dd bs
=2 count
=1 |
hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev
/null
102 (get_image
"$@" |
dd bs
=4 count
=1 |
hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev
/null
106 (get_image
"$@" |
dd bs
=8 count
=1 skip
=64) 2>/dev
/null
110 (get_image
"$@" |
dd bs
=1 count
=3 skip
=54) 2>/dev
/null
114 local magic
=$
(get_magic_gpt
"$@")
115 [ "$magic" = "EFI PART" ]
119 local magic
=$
(get_magic_vfat
"$@")
123 export_bootdevice
() {
124 local cmdline bootdisk rootpart uuid blockdev uevent line class
125 local MAJOR MINOR DEVNAME DEVTYPE
127 if read cmdline
< /proc
/cmdline
; then
130 rootpart
="${cmdline##*root=}"
131 rootpart
="${rootpart%% *}"
137 uevent
="/sys/class/block/${bootdisk##*/}/uevent"
142 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
])
143 uuid
="${rootpart#PARTUUID=}"
144 uuid
="${uuid%-[a-f0-9][a-f0-9]}"
145 for blockdev
in $
(find /dev
-type b
); do
146 set -- $
(dd if=$blockdev bs
=1 skip
=440 count
=4 2>/dev
/null |
hexdump -v -e '4/1 "%02x "')
147 if [ "$4$3$2$1" = "$uuid" ]; then
148 uevent
="/sys/class/block/${blockdev##*/}/uevent"
153 PARTUUID
=????????
-????
-????
-????
-??????????
02)
154 uuid
="${rootpart#PARTUUID=}"
156 for disk
in $
(find /dev
-type b
); do
157 set -- $
(dd if=$disk bs
=1 skip
=568 count
=16 2>/dev
/null |
hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"')
158 if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
159 uevent
="/sys/class/block/${disk##*/}/uevent"
165 uevent
="/sys/class/block/${rootpart##*/}/../uevent"
167 0x
[a-f0-9
][a-f0-9
][a-f0-9
] |
0x
[a-f0-9
][a-f0-9
][a-f0-9
][a-f0-9
] | \
168 [a-f0-9
][a-f0-9
][a-f0-9
] |
[a-f0-9
][a-f0-9
][a-f0-9
][a-f0-9
])
169 rootpart
=0x
${rootpart#0x}
170 for class
in /sys
/class
/block
/*; do
173 done < "$class/uevent"
174 if [ $
((rootpart
/256)) = $MAJOR -a $
((rootpart
%256)) = $MINOR ]; then
175 uevent
="$class/../uevent"
181 if [ -e "$uevent" ]; then
185 export BOOTDEV_MAJOR
=$MAJOR
186 export BOOTDEV_MINOR
=$MINOR
194 export_partdevice
() {
195 local var
="$1" offset
="$2"
196 local uevent line MAJOR MINOR DEVNAME DEVTYPE
198 for uevent
in /sys
/class
/block
/*/uevent
; do
202 if [ $BOOTDEV_MAJOR = $MAJOR -a $
(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
203 export "$var=$DEVNAME"
212 [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x
"')" = "3031" ] && {
213 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
219 get_partitions
() { # <device> <filename>
223 if [ -b "$disk" -o -f "$disk" ]; then
224 v
"Reading partition table from $filename..."
226 local magic
=$
(dd if="$disk" bs
=2 count
=1 skip
=255 2>/dev
/null
)
227 if [ "$magic" != $
'\x55\xAA' ]; then
228 v
"Invalid partition table on $disk"
232 rm -f "/tmp/partmap.$filename"
235 part_magic_efi
"$disk" && {
236 #export_partdevice will fail when partition number is greater than 15, as
237 #the partition major device number is not equal to the disk major device number
238 for part
in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
239 set -- $
(hexdump -v -n 48 -s "$((0x380 + $part * 0x80))" -e '4/4 "%08x"" "4/4 "%08x"" "4/4 "0x%08X "' "$disk")
242 local lba
="$(( $(hex_le32_to_cpu $4) * 0x100000000 + $(hex_le32_to_cpu $3) ))"
243 local end
="$(( $(hex_le32_to_cpu $6) * 0x100000000 + $(hex_le32_to_cpu $5) ))"
244 local num
="$(( $end - $lba ))"
246 [ "$type" = "00000000000000000000000000000000" ] && continue
248 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
251 for part
in 1 2 3 4; do
252 set -- $
(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
254 local type="$(( $(hex_le32_to_cpu $1) % 256))"
255 local lba
="$(( $(hex_le32_to_cpu $2) ))"
256 local num
="$(( $(hex_le32_to_cpu $3) ))"
258 [ $type -gt 0 ] ||
continue
260 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
271 # Flash firmware to MTD partition
273 # $(1): path to image
274 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
275 default_do_upgrade
() {
277 if [ -n "$UPGRADE_BACKUP" ]; then
278 get_image
"$1" "$2" | mtd
$MTD_ARGS $MTD_CONFIG_ARGS -j "$UPGRADE_BACKUP" write - "${PART_NAME:-image}"
280 get_image
"$1" "$2" | mtd
$MTD_ARGS write - "${PART_NAME:-image}"
282 [ $?
-ne 0 ] && exit 1