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 bootdisk="${cmdline##*block2mtd=}"
131 bootdisk="${bootdisk%%,*}"
134 rootpart="${cmdline##*root=}"
135 rootpart="${rootpart%% *}"
141 uevent="/sys/class/block/${bootdisk##*/}/uevent"
146 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])
147 uuid="${rootpart#PARTUUID=}"
148 uuid="${uuid%-[a-f0-9][a-f0-9]}"
149 for blockdev in $(find /dev -type b); do
150 set -- $(dd if=$blockdev bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
151 if [ "$4$3$2$1" = "$uuid" ]; then
152 uevent="/sys/class/block/${blockdev##*/}/uevent"
157 PARTUUID=????????-????-????-????-??????????02)
158 uuid="${rootpart#PARTUUID=}"
160 for disk in $(find /dev -type b); do
161 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"')
162 if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
163 uevent="/sys/class/block/${disk##*/}/uevent"
169 uevent="/sys/class/block/${rootpart##*/}/../uevent"
171 0x[a-f0-9][a-f0-9][a-f0-9] | 0x[a-f0-9][a-f0-9][a-f0-9][a-f0-9] | \
172 [a-f0-9][a-f0-9][a-f0-9] | [a-f0-9][a-f0-9][a-f0-9][a-f0-9])
173 rootpart=0x${rootpart#0x}
174 for class in /sys/class/block/*; do
177 done < "$class/uevent"
178 if [ $((rootpart/256)) = $MAJOR -a $((rootpart%256)) = $MINOR ]; then
179 uevent="$class/../uevent"
185 if [ -e "$uevent" ]; then
189 export BOOTDEV_MAJOR=$MAJOR
190 export BOOTDEV_MINOR=$MINOR
198 export_partdevice() {
199 local var="$1" offset="$2"
200 local uevent line MAJOR MINOR DEVNAME DEVTYPE
202 for uevent in /sys/class/block/*/uevent; do
206 if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
207 export "$var=$DEVNAME"
216 [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" = "3031" ] && {
217 echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
223 get_partitions() { # <device> <filename>
227 if [ -b "$disk" -o -f "$disk" ]; then
228 v "Reading partition table from $filename..."
230 local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
231 if [ "$magic" != $'\x55\xAA' ]; then
232 v "Invalid partition table on $disk"
236 rm -f "/tmp/partmap.$filename"
239 part_magic_efi "$disk" && {
240 #export_partdevice will fail when partition number is greater than 15, as
241 #the partition major device number is not equal to the disk major device number
242 for part in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
243 set -- $(hexdump -v -n 48 -s "$((0x380 + $part * 0x80))" -e '4/4 "%08x"" "4/4 "%08x"" "4/4 "0x%08X "' "$disk")
246 local lba="$(( $(hex_le32_to_cpu $4) * 0x100000000 + $(hex_le32_to_cpu $3) ))"
247 local end="$(( $(hex_le32_to_cpu $6) * 0x100000000 + $(hex_le32_to_cpu $5) ))"
248 local num="$(( $end - $lba ))"
250 [ "$type" = "00000000000000000000000000000000" ] && continue
252 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
255 for part in 1 2 3 4; do
256 set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
258 local type="$(( $(hex_le32_to_cpu $1) % 256))"
259 local lba="$(( $(hex_le32_to_cpu $2) ))"
260 local num="$(( $(hex_le32_to_cpu $3) ))"
262 [ $type -gt 0 ] || continue
264 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
275 # Flash firmware to MTD partition
277 # $(1): path to image
278 # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
279 default_do_upgrade() {
281 if [ -n "$UPGRADE_BACKUP" ]; then
282 get_image "$1" "$2" | mtd $MTD_ARGS $MTD_CONFIG_ARGS -j "$UPGRADE_BACKUP" write - "${PART_NAME:-image}"
284 get_image "$1" "$2" | mtd $MTD_ARGS write - "${PART_NAME:-image}"
286 [ $? -ne 0 ] && exit 1